2013-06-20 04:18:24 +08:00
|
|
|
//===-- StructurizeCFG.cpp ------------------------------------------------===//
|
2012-12-20 06:10:31 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-06-20 04:18:24 +08:00
|
|
|
#define DEBUG_TYPE "structurizecfg"
|
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2013-03-26 18:24:20 +08:00
|
|
|
#include "llvm/ADT/MapVector.h"
|
2013-05-24 01:10:37 +08:00
|
|
|
#include "llvm/ADT/SCCIterator.h"
|
2012-12-20 06:10:31 +08:00
|
|
|
#include "llvm/Analysis/RegionInfo.h"
|
2013-01-02 18:22:59 +08:00
|
|
|
#include "llvm/Analysis/RegionIterator.h"
|
2012-12-20 06:10:31 +08:00
|
|
|
#include "llvm/Analysis/RegionPass.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2013-02-16 19:27:50 +08:00
|
|
|
#include "llvm/Support/PatternMatch.h"
|
2013-05-24 01:10:37 +08:00
|
|
|
#include "llvm/Transforms/Utils/SSAUpdater.h"
|
2012-12-20 06:10:31 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
2013-02-16 19:27:50 +08:00
|
|
|
using namespace llvm::PatternMatch;
|
2012-12-20 06:10:31 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Definition of the complex types used in this pass.
|
|
|
|
|
|
|
|
typedef std::pair<BasicBlock *, Value *> BBValuePair;
|
|
|
|
|
|
|
|
typedef SmallVector<RegionNode*, 8> RNVector;
|
|
|
|
typedef SmallVector<BasicBlock*, 8> BBVector;
|
2013-02-09 06:24:37 +08:00
|
|
|
typedef SmallVector<BranchInst*, 8> BranchVector;
|
2012-12-20 06:10:31 +08:00
|
|
|
typedef SmallVector<BBValuePair, 2> BBValueVector;
|
|
|
|
|
2013-02-09 06:24:37 +08:00
|
|
|
typedef SmallPtrSet<BasicBlock *, 8> BBSet;
|
|
|
|
|
2013-03-26 18:24:20 +08:00
|
|
|
typedef MapVector<PHINode *, BBValueVector> PhiMap;
|
|
|
|
typedef MapVector<BasicBlock *, BBVector> BB2BBVecMap;
|
|
|
|
|
2013-02-16 19:27:29 +08:00
|
|
|
typedef DenseMap<DomTreeNode *, unsigned> DTN2UnsignedMap;
|
2012-12-20 06:10:31 +08:00
|
|
|
typedef DenseMap<BasicBlock *, PhiMap> BBPhiMap;
|
|
|
|
typedef DenseMap<BasicBlock *, Value *> BBPredicates;
|
|
|
|
typedef DenseMap<BasicBlock *, BBPredicates> PredMap;
|
2013-02-16 19:27:45 +08:00
|
|
|
typedef DenseMap<BasicBlock *, BasicBlock*> BB2BBMap;
|
2012-12-20 06:10:31 +08:00
|
|
|
|
|
|
|
// The name for newly created blocks.
|
|
|
|
|
2013-07-16 09:17:10 +08:00
|
|
|
static const char *const FlowBlockName = "Flow";
|
2012-12-20 06:10:31 +08:00
|
|
|
|
2013-02-16 19:27:29 +08:00
|
|
|
/// @brief Find the nearest common dominator for multiple BasicBlocks
|
|
|
|
///
|
2013-06-20 04:18:24 +08:00
|
|
|
/// Helper class for StructurizeCFG
|
2013-02-16 19:27:29 +08:00
|
|
|
/// TODO: Maybe move into common code
|
|
|
|
class NearestCommonDominator {
|
|
|
|
DominatorTree *DT;
|
|
|
|
|
|
|
|
DTN2UnsignedMap IndexMap;
|
|
|
|
|
|
|
|
BasicBlock *Result;
|
|
|
|
unsigned ResultIndex;
|
|
|
|
bool ExplicitMentioned;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/// \brief Start a new query
|
|
|
|
NearestCommonDominator(DominatorTree *DomTree) {
|
|
|
|
DT = DomTree;
|
|
|
|
Result = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Add BB to the resulting dominator
|
|
|
|
void addBlock(BasicBlock *BB, bool Remember = true) {
|
|
|
|
DomTreeNode *Node = DT->getNode(BB);
|
|
|
|
|
|
|
|
if (Result == 0) {
|
|
|
|
unsigned Numbering = 0;
|
|
|
|
for (;Node;Node = Node->getIDom())
|
|
|
|
IndexMap[Node] = ++Numbering;
|
|
|
|
Result = BB;
|
|
|
|
ResultIndex = 1;
|
|
|
|
ExplicitMentioned = Remember;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;Node;Node = Node->getIDom())
|
|
|
|
if (IndexMap.count(Node))
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
IndexMap[Node] = 0;
|
|
|
|
|
|
|
|
assert(Node && "Dominator tree invalid!");
|
|
|
|
|
|
|
|
unsigned Numbering = IndexMap[Node];
|
|
|
|
if (Numbering > ResultIndex) {
|
|
|
|
Result = Node->getBlock();
|
|
|
|
ResultIndex = Numbering;
|
|
|
|
ExplicitMentioned = Remember && (Result == BB);
|
|
|
|
} else if (Numbering == ResultIndex) {
|
|
|
|
ExplicitMentioned |= Remember;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Is "Result" one of the BBs added with "Remember" = True?
|
|
|
|
bool wasResultExplicitMentioned() {
|
|
|
|
return ExplicitMentioned;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Get the query result
|
|
|
|
BasicBlock *getResult() {
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-12-20 06:10:31 +08:00
|
|
|
/// @brief Transforms the control flow graph on one single entry/exit region
|
|
|
|
/// at a time.
|
|
|
|
///
|
|
|
|
/// After the transform all "If"/"Then"/"Else" style control flow looks like
|
|
|
|
/// this:
|
|
|
|
///
|
|
|
|
/// \verbatim
|
|
|
|
/// 1
|
|
|
|
/// ||
|
|
|
|
/// | |
|
|
|
|
/// 2 |
|
|
|
|
/// | /
|
2013-06-20 04:18:24 +08:00
|
|
|
/// |/
|
2012-12-20 06:10:31 +08:00
|
|
|
/// 3
|
|
|
|
/// || Where:
|
|
|
|
/// | | 1 = "If" block, calculates the condition
|
|
|
|
/// 4 | 2 = "Then" subregion, runs if the condition is true
|
|
|
|
/// | / 3 = "Flow" blocks, newly inserted flow blocks, rejoins the flow
|
|
|
|
/// |/ 4 = "Else" optional subregion, runs if the condition is false
|
|
|
|
/// 5 5 = "End" block, also rejoins the control flow
|
|
|
|
/// \endverbatim
|
|
|
|
///
|
|
|
|
/// Control flow is expressed as a branch where the true exit goes into the
|
|
|
|
/// "Then"/"Else" region, while the false exit skips the region
|
|
|
|
/// The condition for the optional "Else" region is expressed as a PHI node.
|
|
|
|
/// The incomming values of the PHI node are true for the "If" edge and false
|
|
|
|
/// for the "Then" edge.
|
|
|
|
///
|
|
|
|
/// Additionally to that even complicated loops look like this:
|
|
|
|
///
|
|
|
|
/// \verbatim
|
|
|
|
/// 1
|
|
|
|
/// ||
|
|
|
|
/// | |
|
|
|
|
/// 2 ^ Where:
|
|
|
|
/// | / 1 = "Entry" block
|
|
|
|
/// |/ 2 = "Loop" optional subregion, with all exits at "Flow" block
|
|
|
|
/// 3 3 = "Flow" block, with back edge to entry block
|
|
|
|
/// |
|
|
|
|
/// \endverbatim
|
|
|
|
///
|
|
|
|
/// The back edge of the "Flow" block is always on the false side of the branch
|
|
|
|
/// while the true side continues the general flow. So the loop condition
|
|
|
|
/// consist of a network of PHI nodes where the true incoming values expresses
|
|
|
|
/// breaks and the false values expresses continue states.
|
2013-06-20 04:18:24 +08:00
|
|
|
class StructurizeCFG : public RegionPass {
|
2012-12-20 06:10:31 +08:00
|
|
|
Type *Boolean;
|
|
|
|
ConstantInt *BoolTrue;
|
|
|
|
ConstantInt *BoolFalse;
|
|
|
|
UndefValue *BoolUndef;
|
|
|
|
|
|
|
|
Function *Func;
|
|
|
|
Region *ParentRegion;
|
|
|
|
|
|
|
|
DominatorTree *DT;
|
|
|
|
|
|
|
|
RNVector Order;
|
2013-02-09 06:24:38 +08:00
|
|
|
BBSet Visited;
|
2013-02-16 19:27:45 +08:00
|
|
|
|
2012-12-20 06:10:31 +08:00
|
|
|
BBPhiMap DeletedPhis;
|
2013-02-09 06:24:35 +08:00
|
|
|
BB2BBVecMap AddedPhis;
|
2013-02-16 19:27:45 +08:00
|
|
|
|
|
|
|
PredMap Predicates;
|
2013-02-09 06:24:37 +08:00
|
|
|
BranchVector Conditions;
|
2012-12-20 06:10:31 +08:00
|
|
|
|
2013-02-16 19:27:45 +08:00
|
|
|
BB2BBMap Loops;
|
|
|
|
PredMap LoopPreds;
|
|
|
|
BranchVector LoopConds;
|
2012-12-20 06:10:31 +08:00
|
|
|
|
2013-02-16 19:27:45 +08:00
|
|
|
RegionNode *PrevNode;
|
2012-12-20 06:10:31 +08:00
|
|
|
|
2013-02-16 19:27:45 +08:00
|
|
|
void orderNodes();
|
2013-02-09 06:24:37 +08:00
|
|
|
|
2013-02-16 19:27:45 +08:00
|
|
|
void analyzeLoops(RegionNode *N);
|
2012-12-20 06:10:31 +08:00
|
|
|
|
2013-02-16 19:27:50 +08:00
|
|
|
Value *invert(Value *Condition);
|
|
|
|
|
2013-02-16 19:27:45 +08:00
|
|
|
Value *buildCondition(BranchInst *Term, unsigned Idx, bool Invert);
|
2012-12-20 06:10:31 +08:00
|
|
|
|
2013-02-16 19:27:45 +08:00
|
|
|
void gatherPredicates(RegionNode *N);
|
2012-12-20 06:10:31 +08:00
|
|
|
|
|
|
|
void collectInfos();
|
|
|
|
|
2013-02-16 19:27:45 +08:00
|
|
|
void insertConditions(bool Loops);
|
2013-02-09 06:24:37 +08:00
|
|
|
|
2013-02-09 06:24:35 +08:00
|
|
|
void delPhiValues(BasicBlock *From, BasicBlock *To);
|
|
|
|
|
|
|
|
void addPhiValues(BasicBlock *From, BasicBlock *To);
|
|
|
|
|
|
|
|
void setPhiValues();
|
|
|
|
|
2012-12-20 06:10:31 +08:00
|
|
|
void killTerminator(BasicBlock *BB);
|
|
|
|
|
2013-02-09 06:24:38 +08:00
|
|
|
void changeExit(RegionNode *Node, BasicBlock *NewExit,
|
|
|
|
bool IncludeDominator);
|
|
|
|
|
|
|
|
BasicBlock *getNextFlow(BasicBlock *Dominator);
|
|
|
|
|
2013-02-16 19:27:45 +08:00
|
|
|
BasicBlock *needPrefix(bool NeedEmpty);
|
2012-12-20 06:10:31 +08:00
|
|
|
|
2013-02-09 06:24:38 +08:00
|
|
|
BasicBlock *needPostfix(BasicBlock *Flow, bool ExitUseAllowed);
|
2012-12-20 06:10:31 +08:00
|
|
|
|
2013-02-16 19:27:45 +08:00
|
|
|
void setPrevNode(BasicBlock *BB);
|
2012-12-20 06:10:31 +08:00
|
|
|
|
2013-02-09 06:24:38 +08:00
|
|
|
bool dominatesPredicates(BasicBlock *BB, RegionNode *Node);
|
|
|
|
|
2013-02-16 19:27:45 +08:00
|
|
|
bool isPredictableTrue(RegionNode *Node);
|
|
|
|
|
|
|
|
void wireFlow(bool ExitUseAllowed, BasicBlock *LoopEnd);
|
2013-02-09 06:24:38 +08:00
|
|
|
|
2013-02-16 19:27:45 +08:00
|
|
|
void handleLoops(bool ExitUseAllowed, BasicBlock *LoopEnd);
|
2012-12-20 06:10:31 +08:00
|
|
|
|
|
|
|
void createFlow();
|
|
|
|
|
|
|
|
void rebuildSSA();
|
|
|
|
|
|
|
|
public:
|
2013-06-20 04:18:24 +08:00
|
|
|
static char ID;
|
2012-12-20 06:10:31 +08:00
|
|
|
|
2013-06-20 04:18:24 +08:00
|
|
|
StructurizeCFG() :
|
|
|
|
RegionPass(ID) {
|
2013-10-03 01:04:59 +08:00
|
|
|
initializeStructurizeCFGPass(*PassRegistry::getPassRegistry());
|
2012-12-20 06:10:31 +08:00
|
|
|
}
|
|
|
|
|
2013-03-01 17:46:11 +08:00
|
|
|
using Pass::doInitialization;
|
2012-12-20 06:10:31 +08:00
|
|
|
virtual bool doInitialization(Region *R, RGPassManager &RGM);
|
|
|
|
|
|
|
|
virtual bool runOnRegion(Region *R, RGPassManager &RGM);
|
|
|
|
|
|
|
|
virtual const char *getPassName() const {
|
2013-06-20 04:18:24 +08:00
|
|
|
return "Structurize control flow";
|
2012-12-20 06:10:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const {
|
2013-10-03 01:04:59 +08:00
|
|
|
AU.addRequiredID(LowerSwitchID);
|
2012-12-20 06:10:31 +08:00
|
|
|
AU.addRequired<DominatorTree>();
|
|
|
|
AU.addPreserved<DominatorTree>();
|
|
|
|
RegionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2013-06-20 04:18:24 +08:00
|
|
|
char StructurizeCFG::ID = 0;
|
|
|
|
|
|
|
|
INITIALIZE_PASS_BEGIN(StructurizeCFG, "structurizecfg", "Structurize the CFG",
|
|
|
|
false, false)
|
2013-10-03 01:04:59 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(LowerSwitch)
|
2013-06-20 04:18:24 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(DominatorTree)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(RegionInfo)
|
|
|
|
INITIALIZE_PASS_END(StructurizeCFG, "structurizecfg", "Structurize the CFG",
|
|
|
|
false, false)
|
2012-12-20 06:10:31 +08:00
|
|
|
|
|
|
|
/// \brief Initialize the types and constants used in the pass
|
2013-06-20 04:18:24 +08:00
|
|
|
bool StructurizeCFG::doInitialization(Region *R, RGPassManager &RGM) {
|
2012-12-20 06:10:31 +08:00
|
|
|
LLVMContext &Context = R->getEntry()->getContext();
|
|
|
|
|
|
|
|
Boolean = Type::getInt1Ty(Context);
|
|
|
|
BoolTrue = ConstantInt::getTrue(Context);
|
|
|
|
BoolFalse = ConstantInt::getFalse(Context);
|
|
|
|
BoolUndef = UndefValue::get(Boolean);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Build up the general order of nodes
|
2013-06-20 04:18:24 +08:00
|
|
|
void StructurizeCFG::orderNodes() {
|
2012-12-20 06:10:31 +08:00
|
|
|
scc_iterator<Region *> I = scc_begin(ParentRegion),
|
|
|
|
E = scc_end(ParentRegion);
|
|
|
|
for (Order.clear(); I != E; ++I) {
|
|
|
|
std::vector<RegionNode *> &Nodes = *I;
|
|
|
|
Order.append(Nodes.begin(), Nodes.end());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-16 19:27:45 +08:00
|
|
|
/// \brief Determine the end of the loops
|
2013-06-20 04:18:24 +08:00
|
|
|
void StructurizeCFG::analyzeLoops(RegionNode *N) {
|
2013-02-16 19:27:45 +08:00
|
|
|
if (N->isSubRegion()) {
|
|
|
|
// Test for exit as back edge
|
|
|
|
BasicBlock *Exit = N->getNodeAs<Region>()->getExit();
|
|
|
|
if (Visited.count(Exit))
|
|
|
|
Loops[Exit] = N->getEntry();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// Test for sucessors as back edge
|
|
|
|
BasicBlock *BB = N->getNodeAs<BasicBlock>();
|
|
|
|
BranchInst *Term = cast<BranchInst>(BB->getTerminator());
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) {
|
|
|
|
BasicBlock *Succ = Term->getSuccessor(i);
|
|
|
|
|
|
|
|
if (Visited.count(Succ))
|
|
|
|
Loops[Succ] = BB;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-16 19:27:50 +08:00
|
|
|
/// \brief Invert the given condition
|
2013-06-20 04:18:24 +08:00
|
|
|
Value *StructurizeCFG::invert(Value *Condition) {
|
2013-02-16 19:27:50 +08:00
|
|
|
// First: Check if it's a constant
|
|
|
|
if (Condition == BoolTrue)
|
|
|
|
return BoolFalse;
|
|
|
|
|
|
|
|
if (Condition == BoolFalse)
|
|
|
|
return BoolTrue;
|
|
|
|
|
|
|
|
if (Condition == BoolUndef)
|
|
|
|
return BoolUndef;
|
|
|
|
|
|
|
|
// Second: If the condition is already inverted, return the original value
|
|
|
|
if (match(Condition, m_Not(m_Value(Condition))))
|
|
|
|
return Condition;
|
|
|
|
|
2013-11-23 03:24:37 +08:00
|
|
|
if (Instruction *Inst = dyn_cast<Instruction>(Condition)) {
|
|
|
|
// Third: Check all the users for an invert
|
|
|
|
BasicBlock *Parent = Inst->getParent();
|
|
|
|
for (Value::use_iterator I = Condition->use_begin(),
|
|
|
|
E = Condition->use_end(); I != E; ++I) {
|
|
|
|
|
|
|
|
Instruction *User = dyn_cast<Instruction>(*I);
|
|
|
|
if (!User || User->getParent() != Parent)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (match(*I, m_Not(m_Specific(Condition))))
|
|
|
|
return *I;
|
|
|
|
}
|
2013-02-16 19:27:50 +08:00
|
|
|
|
2013-11-23 03:24:37 +08:00
|
|
|
// Last option: Create a new instruction
|
|
|
|
return BinaryOperator::CreateNot(Condition, "", Parent->getTerminator());
|
|
|
|
}
|
2013-02-16 19:27:50 +08:00
|
|
|
|
2013-11-23 03:24:37 +08:00
|
|
|
if (Argument *Arg = dyn_cast<Argument>(Condition)) {
|
|
|
|
BasicBlock &EntryBlock = Arg->getParent()->getEntryBlock();
|
|
|
|
return BinaryOperator::CreateNot(Condition,
|
|
|
|
Arg->getName() + ".inv",
|
|
|
|
EntryBlock.getTerminator());
|
2013-02-16 19:27:50 +08:00
|
|
|
}
|
|
|
|
|
2013-11-23 03:24:37 +08:00
|
|
|
llvm_unreachable("Unhandled condition to invert");
|
2013-02-16 19:27:50 +08:00
|
|
|
}
|
|
|
|
|
2013-02-09 06:24:37 +08:00
|
|
|
/// \brief Build the condition for one edge
|
2013-06-20 04:18:24 +08:00
|
|
|
Value *StructurizeCFG::buildCondition(BranchInst *Term, unsigned Idx,
|
|
|
|
bool Invert) {
|
2013-02-09 06:24:37 +08:00
|
|
|
Value *Cond = Invert ? BoolFalse : BoolTrue;
|
|
|
|
if (Term->isConditional()) {
|
|
|
|
Cond = Term->getCondition();
|
2012-12-20 06:10:31 +08:00
|
|
|
|
2013-06-04 09:03:03 +08:00
|
|
|
if (Idx != (unsigned)Invert)
|
2013-02-16 19:27:50 +08:00
|
|
|
Cond = invert(Cond);
|
2013-02-09 06:24:37 +08:00
|
|
|
}
|
|
|
|
return Cond;
|
|
|
|
}
|
2012-12-20 06:10:31 +08:00
|
|
|
|
2013-02-09 06:24:37 +08:00
|
|
|
/// \brief Analyze the predecessors of each block and build up predicates
|
2013-06-20 04:18:24 +08:00
|
|
|
void StructurizeCFG::gatherPredicates(RegionNode *N) {
|
2013-02-09 06:24:37 +08:00
|
|
|
RegionInfo *RI = ParentRegion->getRegionInfo();
|
|
|
|
BasicBlock *BB = N->getEntry();
|
|
|
|
BBPredicates &Pred = Predicates[BB];
|
2013-02-16 19:27:45 +08:00
|
|
|
BBPredicates &LPred = LoopPreds[BB];
|
2012-12-20 06:10:31 +08:00
|
|
|
|
2013-02-09 06:24:37 +08:00
|
|
|
for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
|
|
|
|
PI != PE; ++PI) {
|
2012-12-20 06:10:31 +08:00
|
|
|
|
2013-02-16 19:27:45 +08:00
|
|
|
// Ignore it if it's a branch from outside into our region entry
|
|
|
|
if (!ParentRegion->contains(*PI))
|
2013-02-09 06:24:37 +08:00
|
|
|
continue;
|
2012-12-20 06:10:31 +08:00
|
|
|
|
2013-02-09 06:24:37 +08:00
|
|
|
Region *R = RI->getRegionFor(*PI);
|
|
|
|
if (R == ParentRegion) {
|
2012-12-20 06:10:31 +08:00
|
|
|
|
2013-02-09 06:24:37 +08:00
|
|
|
// It's a top level block in our region
|
|
|
|
BranchInst *Term = cast<BranchInst>((*PI)->getTerminator());
|
|
|
|
for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) {
|
|
|
|
BasicBlock *Succ = Term->getSuccessor(i);
|
|
|
|
if (Succ != BB)
|
|
|
|
continue;
|
2012-12-20 06:10:31 +08:00
|
|
|
|
2013-02-09 06:24:37 +08:00
|
|
|
if (Visited.count(*PI)) {
|
|
|
|
// Normal forward edge
|
|
|
|
if (Term->isConditional()) {
|
|
|
|
// Try to treat it like an ELSE block
|
|
|
|
BasicBlock *Other = Term->getSuccessor(!i);
|
2013-02-16 19:27:45 +08:00
|
|
|
if (Visited.count(Other) && !Loops.count(Other) &&
|
2013-02-09 06:24:37 +08:00
|
|
|
!Pred.count(Other) && !Pred.count(*PI)) {
|
|
|
|
|
|
|
|
Pred[Other] = BoolFalse;
|
|
|
|
Pred[*PI] = BoolTrue;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2013-02-16 19:27:45 +08:00
|
|
|
Pred[*PI] = buildCondition(Term, i, false);
|
2013-06-20 04:18:24 +08:00
|
|
|
|
2013-02-09 06:24:37 +08:00
|
|
|
} else {
|
|
|
|
// Back edge
|
2013-02-16 19:27:45 +08:00
|
|
|
LPred[*PI] = buildCondition(Term, i, true);
|
2013-02-09 06:24:37 +08:00
|
|
|
}
|
|
|
|
}
|
2012-12-20 06:10:31 +08:00
|
|
|
|
2013-02-09 06:24:37 +08:00
|
|
|
} else {
|
2012-12-20 06:10:31 +08:00
|
|
|
|
2013-02-09 06:24:37 +08:00
|
|
|
// It's an exit from a sub region
|
|
|
|
while(R->getParent() != ParentRegion)
|
|
|
|
R = R->getParent();
|
|
|
|
|
|
|
|
// Edge from inside a subregion to its entry, ignore it
|
|
|
|
if (R == N)
|
2012-12-20 06:10:31 +08:00
|
|
|
continue;
|
2013-02-09 06:24:37 +08:00
|
|
|
|
|
|
|
BasicBlock *Entry = R->getEntry();
|
2013-02-16 19:27:45 +08:00
|
|
|
if (Visited.count(Entry))
|
|
|
|
Pred[Entry] = BoolTrue;
|
|
|
|
else
|
|
|
|
LPred[Entry] = BoolFalse;
|
2012-12-20 06:10:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Collect various loop and predicate infos
|
2013-06-20 04:18:24 +08:00
|
|
|
void StructurizeCFG::collectInfos() {
|
2012-12-20 06:10:31 +08:00
|
|
|
// Reset predicate
|
|
|
|
Predicates.clear();
|
|
|
|
|
|
|
|
// and loop infos
|
2013-02-16 19:27:45 +08:00
|
|
|
Loops.clear();
|
|
|
|
LoopPreds.clear();
|
2012-12-20 06:10:31 +08:00
|
|
|
|
2013-02-09 06:24:37 +08:00
|
|
|
// Reset the visited nodes
|
|
|
|
Visited.clear();
|
|
|
|
|
|
|
|
for (RNVector::reverse_iterator OI = Order.rbegin(), OE = Order.rend();
|
|
|
|
OI != OE; ++OI) {
|
2012-12-20 06:10:31 +08:00
|
|
|
|
|
|
|
// Analyze all the conditions leading to a node
|
2013-02-16 19:27:45 +08:00
|
|
|
gatherPredicates(*OI);
|
2012-12-20 06:10:31 +08:00
|
|
|
|
2013-02-09 06:24:37 +08:00
|
|
|
// Remember that we've seen this node
|
2013-02-09 06:24:38 +08:00
|
|
|
Visited.insert((*OI)->getEntry());
|
2012-12-20 06:10:31 +08:00
|
|
|
|
2013-02-16 19:27:45 +08:00
|
|
|
// Find the last back edges
|
|
|
|
analyzeLoops(*OI);
|
2013-02-09 06:24:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Insert the missing branch conditions
|
2013-06-20 04:18:24 +08:00
|
|
|
void StructurizeCFG::insertConditions(bool Loops) {
|
2013-02-16 19:27:45 +08:00
|
|
|
BranchVector &Conds = Loops ? LoopConds : Conditions;
|
|
|
|
Value *Default = Loops ? BoolTrue : BoolFalse;
|
2013-02-09 06:24:37 +08:00
|
|
|
SSAUpdater PhiInserter;
|
|
|
|
|
2013-02-16 19:27:45 +08:00
|
|
|
for (BranchVector::iterator I = Conds.begin(),
|
|
|
|
E = Conds.end(); I != E; ++I) {
|
2013-02-09 06:24:37 +08:00
|
|
|
|
|
|
|
BranchInst *Term = *I;
|
|
|
|
assert(Term->isConditional());
|
|
|
|
|
2013-02-16 19:27:45 +08:00
|
|
|
BasicBlock *Parent = Term->getParent();
|
|
|
|
BasicBlock *SuccTrue = Term->getSuccessor(0);
|
|
|
|
BasicBlock *SuccFalse = Term->getSuccessor(1);
|
2013-02-16 19:27:40 +08:00
|
|
|
|
2013-02-09 06:24:37 +08:00
|
|
|
PhiInserter.Initialize(Boolean, "");
|
2013-02-16 19:27:40 +08:00
|
|
|
PhiInserter.AddAvailableValue(&Func->getEntryBlock(), Default);
|
2013-02-16 19:27:45 +08:00
|
|
|
PhiInserter.AddAvailableValue(Loops ? SuccFalse : Parent, Default);
|
2013-02-09 06:24:37 +08:00
|
|
|
|
2013-02-16 19:27:45 +08:00
|
|
|
BBPredicates &Preds = Loops ? LoopPreds[SuccFalse] : Predicates[SuccTrue];
|
2013-02-16 19:27:40 +08:00
|
|
|
|
|
|
|
NearestCommonDominator Dominator(DT);
|
|
|
|
Dominator.addBlock(Parent, false);
|
|
|
|
|
|
|
|
Value *ParentValue = 0;
|
2013-02-09 06:24:37 +08:00
|
|
|
for (BBPredicates::iterator PI = Preds.begin(), PE = Preds.end();
|
|
|
|
PI != PE; ++PI) {
|
|
|
|
|
2013-02-16 19:27:40 +08:00
|
|
|
if (PI->first == Parent) {
|
|
|
|
ParentValue = PI->second;
|
|
|
|
break;
|
|
|
|
}
|
2013-02-09 06:24:37 +08:00
|
|
|
PhiInserter.AddAvailableValue(PI->first, PI->second);
|
2013-02-16 19:27:40 +08:00
|
|
|
Dominator.addBlock(PI->first);
|
2013-02-09 06:24:37 +08:00
|
|
|
}
|
|
|
|
|
2013-02-16 19:27:40 +08:00
|
|
|
if (ParentValue) {
|
|
|
|
Term->setCondition(ParentValue);
|
|
|
|
} else {
|
|
|
|
if (!Dominator.wasResultExplicitMentioned())
|
|
|
|
PhiInserter.AddAvailableValue(Dominator.getResult(), Default);
|
|
|
|
|
2013-02-09 06:24:37 +08:00
|
|
|
Term->setCondition(PhiInserter.GetValueInMiddleOfBlock(Parent));
|
2013-02-16 19:27:40 +08:00
|
|
|
}
|
2012-12-20 06:10:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-09 06:24:35 +08:00
|
|
|
/// \brief Remove all PHI values coming from "From" into "To" and remember
|
|
|
|
/// them in DeletedPhis
|
2013-06-20 04:18:24 +08:00
|
|
|
void StructurizeCFG::delPhiValues(BasicBlock *From, BasicBlock *To) {
|
2013-02-09 06:24:35 +08:00
|
|
|
PhiMap &Map = DeletedPhis[To];
|
|
|
|
for (BasicBlock::iterator I = To->begin(), E = To->end();
|
|
|
|
I != E && isa<PHINode>(*I);) {
|
|
|
|
|
|
|
|
PHINode &Phi = cast<PHINode>(*I++);
|
|
|
|
while (Phi.getBasicBlockIndex(From) != -1) {
|
|
|
|
Value *Deleted = Phi.removeIncomingValue(From, false);
|
|
|
|
Map[&Phi].push_back(std::make_pair(From, Deleted));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Add a dummy PHI value as soon as we knew the new predecessor
|
2013-06-20 04:18:24 +08:00
|
|
|
void StructurizeCFG::addPhiValues(BasicBlock *From, BasicBlock *To) {
|
2013-02-09 06:24:35 +08:00
|
|
|
for (BasicBlock::iterator I = To->begin(), E = To->end();
|
|
|
|
I != E && isa<PHINode>(*I);) {
|
|
|
|
|
|
|
|
PHINode &Phi = cast<PHINode>(*I++);
|
|
|
|
Value *Undef = UndefValue::get(Phi.getType());
|
|
|
|
Phi.addIncoming(Undef, From);
|
|
|
|
}
|
|
|
|
AddedPhis[To].push_back(From);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Add the real PHI value as soon as everything is set up
|
2013-06-20 04:18:24 +08:00
|
|
|
void StructurizeCFG::setPhiValues() {
|
2013-02-09 06:24:35 +08:00
|
|
|
SSAUpdater Updater;
|
|
|
|
for (BB2BBVecMap::iterator AI = AddedPhis.begin(), AE = AddedPhis.end();
|
|
|
|
AI != AE; ++AI) {
|
|
|
|
|
|
|
|
BasicBlock *To = AI->first;
|
|
|
|
BBVector &From = AI->second;
|
|
|
|
|
|
|
|
if (!DeletedPhis.count(To))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
PhiMap &Map = DeletedPhis[To];
|
|
|
|
for (PhiMap::iterator PI = Map.begin(), PE = Map.end();
|
|
|
|
PI != PE; ++PI) {
|
|
|
|
|
|
|
|
PHINode *Phi = PI->first;
|
|
|
|
Value *Undef = UndefValue::get(Phi->getType());
|
|
|
|
Updater.Initialize(Phi->getType(), "");
|
|
|
|
Updater.AddAvailableValue(&Func->getEntryBlock(), Undef);
|
|
|
|
Updater.AddAvailableValue(To, Undef);
|
|
|
|
|
2013-02-16 19:27:35 +08:00
|
|
|
NearestCommonDominator Dominator(DT);
|
|
|
|
Dominator.addBlock(To, false);
|
2013-02-09 06:24:35 +08:00
|
|
|
for (BBValueVector::iterator VI = PI->second.begin(),
|
|
|
|
VE = PI->second.end(); VI != VE; ++VI) {
|
|
|
|
|
|
|
|
Updater.AddAvailableValue(VI->first, VI->second);
|
2013-02-16 19:27:35 +08:00
|
|
|
Dominator.addBlock(VI->first);
|
2013-02-09 06:24:35 +08:00
|
|
|
}
|
|
|
|
|
2013-02-16 19:27:35 +08:00
|
|
|
if (!Dominator.wasResultExplicitMentioned())
|
|
|
|
Updater.AddAvailableValue(Dominator.getResult(), Undef);
|
|
|
|
|
2013-02-09 06:24:35 +08:00
|
|
|
for (BBVector::iterator FI = From.begin(), FE = From.end();
|
|
|
|
FI != FE; ++FI) {
|
|
|
|
|
|
|
|
int Idx = Phi->getBasicBlockIndex(*FI);
|
|
|
|
assert(Idx != -1);
|
|
|
|
Phi->setIncomingValue(Idx, Updater.GetValueAtEndOfBlock(*FI));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DeletedPhis.erase(To);
|
|
|
|
}
|
|
|
|
assert(DeletedPhis.empty());
|
|
|
|
}
|
|
|
|
|
2013-02-09 06:24:38 +08:00
|
|
|
/// \brief Remove phi values from all successors and then remove the terminator.
|
2013-06-20 04:18:24 +08:00
|
|
|
void StructurizeCFG::killTerminator(BasicBlock *BB) {
|
2012-12-20 06:10:31 +08:00
|
|
|
TerminatorInst *Term = BB->getTerminator();
|
|
|
|
if (!Term)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB);
|
|
|
|
SI != SE; ++SI) {
|
|
|
|
|
|
|
|
delPhiValues(BB, *SI);
|
|
|
|
}
|
|
|
|
|
|
|
|
Term->eraseFromParent();
|
|
|
|
}
|
|
|
|
|
2013-02-09 06:24:38 +08:00
|
|
|
/// \brief Let node exit(s) point to NewExit
|
2013-06-20 04:18:24 +08:00
|
|
|
void StructurizeCFG::changeExit(RegionNode *Node, BasicBlock *NewExit,
|
|
|
|
bool IncludeDominator) {
|
2013-02-09 06:24:38 +08:00
|
|
|
if (Node->isSubRegion()) {
|
|
|
|
Region *SubRegion = Node->getNodeAs<Region>();
|
|
|
|
BasicBlock *OldExit = SubRegion->getExit();
|
|
|
|
BasicBlock *Dominator = 0;
|
2012-12-20 06:10:31 +08:00
|
|
|
|
2013-02-09 06:24:38 +08:00
|
|
|
// Find all the edges from the sub region to the exit
|
|
|
|
for (pred_iterator I = pred_begin(OldExit), E = pred_end(OldExit);
|
|
|
|
I != E;) {
|
2012-12-20 06:10:31 +08:00
|
|
|
|
2013-02-09 06:24:38 +08:00
|
|
|
BasicBlock *BB = *I++;
|
|
|
|
if (!SubRegion->contains(BB))
|
|
|
|
continue;
|
2012-12-20 06:10:31 +08:00
|
|
|
|
2013-02-09 06:24:38 +08:00
|
|
|
// Modify the edges to point to the new exit
|
|
|
|
delPhiValues(BB, OldExit);
|
|
|
|
BB->getTerminator()->replaceUsesOfWith(OldExit, NewExit);
|
|
|
|
addPhiValues(BB, NewExit);
|
|
|
|
|
|
|
|
// Find the new dominator (if requested)
|
|
|
|
if (IncludeDominator) {
|
|
|
|
if (!Dominator)
|
|
|
|
Dominator = BB;
|
|
|
|
else
|
|
|
|
Dominator = DT->findNearestCommonDominator(Dominator, BB);
|
|
|
|
}
|
|
|
|
}
|
2012-12-20 06:10:31 +08:00
|
|
|
|
2013-02-09 06:24:38 +08:00
|
|
|
// Change the dominator (if requested)
|
|
|
|
if (Dominator)
|
|
|
|
DT->changeImmediateDominator(NewExit, Dominator);
|
2012-12-20 06:10:31 +08:00
|
|
|
|
2013-02-09 06:24:38 +08:00
|
|
|
// Update the region info
|
|
|
|
SubRegion->replaceExit(NewExit);
|
2012-12-20 06:10:31 +08:00
|
|
|
|
|
|
|
} else {
|
2013-02-09 06:24:38 +08:00
|
|
|
BasicBlock *BB = Node->getNodeAs<BasicBlock>();
|
|
|
|
killTerminator(BB);
|
|
|
|
BranchInst::Create(NewExit, BB);
|
|
|
|
addPhiValues(BB, NewExit);
|
|
|
|
if (IncludeDominator)
|
|
|
|
DT->changeImmediateDominator(NewExit, BB);
|
2012-12-20 06:10:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Create a new flow node and update dominator tree and region info
|
2013-06-20 04:18:24 +08:00
|
|
|
BasicBlock *StructurizeCFG::getNextFlow(BasicBlock *Dominator) {
|
2012-12-20 06:10:31 +08:00
|
|
|
LLVMContext &Context = Func->getContext();
|
|
|
|
BasicBlock *Insert = Order.empty() ? ParentRegion->getExit() :
|
|
|
|
Order.back()->getEntry();
|
|
|
|
BasicBlock *Flow = BasicBlock::Create(Context, FlowBlockName,
|
|
|
|
Func, Insert);
|
2013-02-09 06:24:38 +08:00
|
|
|
DT->addNewBlock(Flow, Dominator);
|
2012-12-20 06:10:31 +08:00
|
|
|
ParentRegion->getRegionInfo()->setRegionFor(Flow, ParentRegion);
|
|
|
|
return Flow;
|
|
|
|
}
|
|
|
|
|
2013-02-09 06:24:38 +08:00
|
|
|
/// \brief Create a new or reuse the previous node as flow node
|
2013-06-20 04:18:24 +08:00
|
|
|
BasicBlock *StructurizeCFG::needPrefix(bool NeedEmpty) {
|
2013-02-16 19:27:45 +08:00
|
|
|
BasicBlock *Entry = PrevNode->getEntry();
|
2013-02-09 06:24:38 +08:00
|
|
|
|
2013-02-16 19:27:45 +08:00
|
|
|
if (!PrevNode->isSubRegion()) {
|
|
|
|
killTerminator(Entry);
|
|
|
|
if (!NeedEmpty || Entry->getFirstInsertionPt() == Entry->end())
|
|
|
|
return Entry;
|
2013-02-09 06:24:38 +08:00
|
|
|
|
2013-06-20 04:18:24 +08:00
|
|
|
}
|
2013-02-09 06:24:38 +08:00
|
|
|
|
2013-02-16 19:27:45 +08:00
|
|
|
// create a new flow node
|
|
|
|
BasicBlock *Flow = getNextFlow(Entry);
|
2013-02-09 06:24:38 +08:00
|
|
|
|
2013-02-16 19:27:45 +08:00
|
|
|
// and wire it up
|
|
|
|
changeExit(PrevNode, Flow, true);
|
|
|
|
PrevNode = ParentRegion->getBBNode(Flow);
|
|
|
|
return Flow;
|
2013-02-09 06:24:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Returns the region exit if possible, otherwise just a new flow node
|
2013-06-20 04:18:24 +08:00
|
|
|
BasicBlock *StructurizeCFG::needPostfix(BasicBlock *Flow,
|
|
|
|
bool ExitUseAllowed) {
|
2013-02-09 06:24:38 +08:00
|
|
|
if (Order.empty() && ExitUseAllowed) {
|
|
|
|
BasicBlock *Exit = ParentRegion->getExit();
|
|
|
|
DT->changeImmediateDominator(Exit, Flow);
|
|
|
|
addPhiValues(Flow, Exit);
|
|
|
|
return Exit;
|
|
|
|
}
|
|
|
|
return getNextFlow(Flow);
|
|
|
|
}
|
|
|
|
|
2013-02-16 19:27:45 +08:00
|
|
|
/// \brief Set the previous node
|
2013-06-20 04:18:24 +08:00
|
|
|
void StructurizeCFG::setPrevNode(BasicBlock *BB) {
|
2013-02-16 19:27:45 +08:00
|
|
|
PrevNode = ParentRegion->contains(BB) ? ParentRegion->getBBNode(BB) : 0;
|
2013-02-09 06:24:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Does BB dominate all the predicates of Node ?
|
2013-06-20 04:18:24 +08:00
|
|
|
bool StructurizeCFG::dominatesPredicates(BasicBlock *BB, RegionNode *Node) {
|
2013-02-09 06:24:38 +08:00
|
|
|
BBPredicates &Preds = Predicates[Node->getEntry()];
|
|
|
|
for (BBPredicates::iterator PI = Preds.begin(), PE = Preds.end();
|
|
|
|
PI != PE; ++PI) {
|
|
|
|
|
|
|
|
if (!DT->dominates(BB, PI->first))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-12-20 06:10:31 +08:00
|
|
|
/// \brief Can we predict that this node will always be called?
|
2013-06-20 04:18:24 +08:00
|
|
|
bool StructurizeCFG::isPredictableTrue(RegionNode *Node) {
|
2013-02-16 19:27:45 +08:00
|
|
|
BBPredicates &Preds = Predicates[Node->getEntry()];
|
|
|
|
bool Dominated = false;
|
|
|
|
|
|
|
|
// Regionentry is always true
|
|
|
|
if (PrevNode == 0)
|
|
|
|
return true;
|
2012-12-20 06:10:31 +08:00
|
|
|
|
|
|
|
for (BBPredicates::iterator I = Preds.begin(), E = Preds.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
|
|
|
|
if (I->second != BoolTrue)
|
|
|
|
return false;
|
|
|
|
|
2013-02-16 19:27:45 +08:00
|
|
|
if (!Dominated && DT->dominates(I->first, PrevNode->getEntry()))
|
2012-12-20 06:10:31 +08:00
|
|
|
Dominated = true;
|
|
|
|
}
|
2013-02-09 06:24:38 +08:00
|
|
|
|
|
|
|
// TODO: The dominator check is too strict
|
2012-12-20 06:10:31 +08:00
|
|
|
return Dominated;
|
|
|
|
}
|
|
|
|
|
2013-02-09 06:24:38 +08:00
|
|
|
/// Take one node from the order vector and wire it up
|
2013-06-20 04:18:24 +08:00
|
|
|
void StructurizeCFG::wireFlow(bool ExitUseAllowed,
|
|
|
|
BasicBlock *LoopEnd) {
|
2013-02-09 06:24:38 +08:00
|
|
|
RegionNode *Node = Order.pop_back_val();
|
2013-02-16 19:27:45 +08:00
|
|
|
Visited.insert(Node->getEntry());
|
2012-12-20 06:10:31 +08:00
|
|
|
|
2013-02-16 19:27:45 +08:00
|
|
|
if (isPredictableTrue(Node)) {
|
2013-02-09 06:24:38 +08:00
|
|
|
// Just a linear flow
|
2013-02-16 19:27:45 +08:00
|
|
|
if (PrevNode) {
|
|
|
|
changeExit(PrevNode, Node->getEntry(), true);
|
2013-02-09 06:24:38 +08:00
|
|
|
}
|
2013-02-16 19:27:45 +08:00
|
|
|
PrevNode = Node;
|
2012-12-20 06:10:31 +08:00
|
|
|
|
|
|
|
} else {
|
2013-02-09 06:24:38 +08:00
|
|
|
// Insert extra prefix node (or reuse last one)
|
2013-02-16 19:27:45 +08:00
|
|
|
BasicBlock *Flow = needPrefix(false);
|
2012-12-20 06:10:31 +08:00
|
|
|
|
2013-02-09 06:24:38 +08:00
|
|
|
// Insert extra postfix node (or use exit instead)
|
|
|
|
BasicBlock *Entry = Node->getEntry();
|
2013-02-16 19:27:45 +08:00
|
|
|
BasicBlock *Next = needPostfix(Flow, ExitUseAllowed);
|
2013-02-09 06:24:38 +08:00
|
|
|
|
|
|
|
// let it point to entry and next block
|
|
|
|
Conditions.push_back(BranchInst::Create(Entry, Next, BoolUndef, Flow));
|
|
|
|
addPhiValues(Flow, Entry);
|
|
|
|
DT->changeImmediateDominator(Entry, Flow);
|
|
|
|
|
2013-02-16 19:27:45 +08:00
|
|
|
PrevNode = Node;
|
|
|
|
while (!Order.empty() && !Visited.count(LoopEnd) &&
|
2013-02-09 06:24:38 +08:00
|
|
|
dominatesPredicates(Entry, Order.back())) {
|
2013-02-16 19:27:45 +08:00
|
|
|
handleLoops(false, LoopEnd);
|
2012-12-20 06:10:31 +08:00
|
|
|
}
|
|
|
|
|
2013-02-16 19:27:45 +08:00
|
|
|
changeExit(PrevNode, Next, false);
|
|
|
|
setPrevNode(Next);
|
2012-12-20 06:10:31 +08:00
|
|
|
}
|
2013-02-16 19:27:45 +08:00
|
|
|
}
|
2012-12-20 06:10:31 +08:00
|
|
|
|
2013-06-20 04:18:24 +08:00
|
|
|
void StructurizeCFG::handleLoops(bool ExitUseAllowed,
|
|
|
|
BasicBlock *LoopEnd) {
|
2013-02-16 19:27:45 +08:00
|
|
|
RegionNode *Node = Order.back();
|
|
|
|
BasicBlock *LoopStart = Node->getEntry();
|
|
|
|
|
|
|
|
if (!Loops.count(LoopStart)) {
|
|
|
|
wireFlow(ExitUseAllowed, LoopEnd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isPredictableTrue(Node))
|
|
|
|
LoopStart = needPrefix(true);
|
|
|
|
|
|
|
|
LoopEnd = Loops[Node->getEntry()];
|
|
|
|
wireFlow(false, LoopEnd);
|
|
|
|
while (!Visited.count(LoopEnd)) {
|
|
|
|
handleLoops(false, LoopEnd);
|
|
|
|
}
|
|
|
|
|
2013-11-23 03:24:39 +08:00
|
|
|
// If the start of the loop is the entry block, we can't branch to it so
|
|
|
|
// insert a new dummy entry block.
|
|
|
|
Function *LoopFunc = LoopStart->getParent();
|
|
|
|
if (LoopStart == &LoopFunc->getEntryBlock()) {
|
|
|
|
LoopStart->setName("entry.orig");
|
|
|
|
|
|
|
|
BasicBlock *NewEntry =
|
|
|
|
BasicBlock::Create(LoopStart->getContext(),
|
|
|
|
"entry",
|
|
|
|
LoopFunc,
|
|
|
|
LoopStart);
|
|
|
|
BranchInst::Create(LoopStart, NewEntry);
|
|
|
|
}
|
|
|
|
|
2013-02-16 19:27:45 +08:00
|
|
|
// Create an extra loop end node
|
|
|
|
LoopEnd = needPrefix(false);
|
|
|
|
BasicBlock *Next = needPostfix(LoopEnd, ExitUseAllowed);
|
|
|
|
LoopConds.push_back(BranchInst::Create(Next, LoopStart,
|
|
|
|
BoolUndef, LoopEnd));
|
|
|
|
addPhiValues(LoopEnd, LoopStart);
|
|
|
|
setPrevNode(Next);
|
2012-12-20 06:10:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// After this function control flow looks like it should be, but
|
2013-02-09 06:24:38 +08:00
|
|
|
/// branches and PHI nodes only have undefined conditions.
|
2013-06-20 04:18:24 +08:00
|
|
|
void StructurizeCFG::createFlow() {
|
2013-02-09 06:24:38 +08:00
|
|
|
BasicBlock *Exit = ParentRegion->getExit();
|
|
|
|
bool EntryDominatesExit = DT->dominates(ParentRegion->getEntry(), Exit);
|
|
|
|
|
2012-12-20 06:10:31 +08:00
|
|
|
DeletedPhis.clear();
|
2013-02-09 06:24:35 +08:00
|
|
|
AddedPhis.clear();
|
2013-02-09 06:24:38 +08:00
|
|
|
Conditions.clear();
|
2013-02-16 19:27:45 +08:00
|
|
|
LoopConds.clear();
|
2012-12-20 06:10:31 +08:00
|
|
|
|
2013-02-16 19:27:45 +08:00
|
|
|
PrevNode = 0;
|
|
|
|
Visited.clear();
|
2013-02-09 06:24:38 +08:00
|
|
|
|
2013-02-16 19:27:45 +08:00
|
|
|
while (!Order.empty()) {
|
|
|
|
handleLoops(EntryDominatesExit, 0);
|
2012-12-20 06:10:31 +08:00
|
|
|
}
|
|
|
|
|
2013-02-16 19:27:45 +08:00
|
|
|
if (PrevNode)
|
|
|
|
changeExit(PrevNode, Exit, EntryDominatesExit);
|
2013-02-09 06:24:38 +08:00
|
|
|
else
|
|
|
|
assert(EntryDominatesExit);
|
2012-12-20 06:10:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Handle a rare case where the disintegrated nodes instructions
|
|
|
|
/// no longer dominate all their uses. Not sure if this is really nessasary
|
2013-06-20 04:18:24 +08:00
|
|
|
void StructurizeCFG::rebuildSSA() {
|
2012-12-20 06:10:31 +08:00
|
|
|
SSAUpdater Updater;
|
|
|
|
for (Region::block_iterator I = ParentRegion->block_begin(),
|
|
|
|
E = ParentRegion->block_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
|
|
|
|
BasicBlock *BB = *I;
|
|
|
|
for (BasicBlock::iterator II = BB->begin(), IE = BB->end();
|
|
|
|
II != IE; ++II) {
|
|
|
|
|
|
|
|
bool Initialized = false;
|
|
|
|
for (Use *I = &II->use_begin().getUse(), *Next; I; I = Next) {
|
|
|
|
|
|
|
|
Next = I->getNext();
|
|
|
|
|
|
|
|
Instruction *User = cast<Instruction>(I->getUser());
|
|
|
|
if (User->getParent() == BB) {
|
|
|
|
continue;
|
|
|
|
|
|
|
|
} else if (PHINode *UserPN = dyn_cast<PHINode>(User)) {
|
|
|
|
if (UserPN->getIncomingBlock(*I) == BB)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (DT->dominates(II, User))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!Initialized) {
|
|
|
|
Value *Undef = UndefValue::get(II->getType());
|
|
|
|
Updater.Initialize(II->getType(), "");
|
|
|
|
Updater.AddAvailableValue(&Func->getEntryBlock(), Undef);
|
|
|
|
Updater.AddAvailableValue(BB, II);
|
|
|
|
Initialized = true;
|
|
|
|
}
|
|
|
|
Updater.RewriteUseAfterInsertions(*I);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Run the transformation for each region found
|
2013-06-20 04:18:24 +08:00
|
|
|
bool StructurizeCFG::runOnRegion(Region *R, RGPassManager &RGM) {
|
2012-12-20 06:10:31 +08:00
|
|
|
if (R->isTopLevelRegion())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Func = R->getEntry()->getParent();
|
|
|
|
ParentRegion = R;
|
|
|
|
|
|
|
|
DT = &getAnalysis<DominatorTree>();
|
|
|
|
|
|
|
|
orderNodes();
|
|
|
|
collectInfos();
|
|
|
|
createFlow();
|
2013-02-16 19:27:45 +08:00
|
|
|
insertConditions(false);
|
|
|
|
insertConditions(true);
|
2013-02-09 06:24:35 +08:00
|
|
|
setPhiValues();
|
2012-12-20 06:10:31 +08:00
|
|
|
rebuildSSA();
|
|
|
|
|
2013-02-09 06:24:37 +08:00
|
|
|
// Cleanup
|
2012-12-20 06:10:31 +08:00
|
|
|
Order.clear();
|
|
|
|
Visited.clear();
|
|
|
|
DeletedPhis.clear();
|
2013-02-09 06:24:35 +08:00
|
|
|
AddedPhis.clear();
|
2013-02-16 19:27:45 +08:00
|
|
|
Predicates.clear();
|
2013-02-09 06:24:37 +08:00
|
|
|
Conditions.clear();
|
2013-02-16 19:27:45 +08:00
|
|
|
Loops.clear();
|
|
|
|
LoopPreds.clear();
|
|
|
|
LoopConds.clear();
|
2012-12-20 06:10:31 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Create the pass
|
2013-06-20 04:18:24 +08:00
|
|
|
Pass *llvm::createStructurizeCFGPass() {
|
|
|
|
return new StructurizeCFG();
|
2012-12-20 06:10:31 +08:00
|
|
|
}
|