LLVM API Documentation

StructurizeCFG.cpp
Go to the documentation of this file.
00001 //===-- StructurizeCFG.cpp ------------------------------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #include "llvm/Transforms/Scalar.h"
00011 #include "llvm/ADT/MapVector.h"
00012 #include "llvm/ADT/SCCIterator.h"
00013 #include "llvm/Analysis/RegionInfo.h"
00014 #include "llvm/Analysis/RegionIterator.h"
00015 #include "llvm/Analysis/RegionPass.h"
00016 #include "llvm/IR/Module.h"
00017 #include "llvm/IR/PatternMatch.h"
00018 #include "llvm/Transforms/Utils/SSAUpdater.h"
00019 
00020 using namespace llvm;
00021 using namespace llvm::PatternMatch;
00022 
00023 #define DEBUG_TYPE "structurizecfg"
00024 
00025 namespace {
00026 
00027 // Definition of the complex types used in this pass.
00028 
00029 typedef std::pair<BasicBlock *, Value *> BBValuePair;
00030 
00031 typedef SmallVector<RegionNode*, 8> RNVector;
00032 typedef SmallVector<BasicBlock*, 8> BBVector;
00033 typedef SmallVector<BranchInst*, 8> BranchVector;
00034 typedef SmallVector<BBValuePair, 2> BBValueVector;
00035 
00036 typedef SmallPtrSet<BasicBlock *, 8> BBSet;
00037 
00038 typedef MapVector<PHINode *, BBValueVector> PhiMap;
00039 typedef MapVector<BasicBlock *, BBVector> BB2BBVecMap;
00040 
00041 typedef DenseMap<DomTreeNode *, unsigned> DTN2UnsignedMap;
00042 typedef DenseMap<BasicBlock *, PhiMap> BBPhiMap;
00043 typedef DenseMap<BasicBlock *, Value *> BBPredicates;
00044 typedef DenseMap<BasicBlock *, BBPredicates> PredMap;
00045 typedef DenseMap<BasicBlock *, BasicBlock*> BB2BBMap;
00046 
00047 // The name for newly created blocks.
00048 
00049 static const char *const FlowBlockName = "Flow";
00050 
00051 /// @brief Find the nearest common dominator for multiple BasicBlocks
00052 ///
00053 /// Helper class for StructurizeCFG
00054 /// TODO: Maybe move into common code
00055 class NearestCommonDominator {
00056   DominatorTree *DT;
00057 
00058   DTN2UnsignedMap IndexMap;
00059 
00060   BasicBlock *Result;
00061   unsigned ResultIndex;
00062   bool ExplicitMentioned;
00063 
00064 public:
00065   /// \brief Start a new query
00066   NearestCommonDominator(DominatorTree *DomTree) {
00067     DT = DomTree;
00068     Result = nullptr;
00069   }
00070 
00071   /// \brief Add BB to the resulting dominator
00072   void addBlock(BasicBlock *BB, bool Remember = true) {
00073     DomTreeNode *Node = DT->getNode(BB);
00074 
00075     if (!Result) {
00076       unsigned Numbering = 0;
00077       for (;Node;Node = Node->getIDom())
00078         IndexMap[Node] = ++Numbering;
00079       Result = BB;
00080       ResultIndex = 1;
00081       ExplicitMentioned = Remember;
00082       return;
00083     }
00084 
00085     for (;Node;Node = Node->getIDom())
00086       if (IndexMap.count(Node))
00087         break;
00088       else
00089         IndexMap[Node] = 0;
00090 
00091     assert(Node && "Dominator tree invalid!");
00092 
00093     unsigned Numbering = IndexMap[Node];
00094     if (Numbering > ResultIndex) {
00095       Result = Node->getBlock();
00096       ResultIndex = Numbering;
00097       ExplicitMentioned = Remember && (Result == BB);
00098     } else if (Numbering == ResultIndex) {
00099       ExplicitMentioned |= Remember;
00100     }
00101   }
00102 
00103   /// \brief Is "Result" one of the BBs added with "Remember" = True?
00104   bool wasResultExplicitMentioned() {
00105     return ExplicitMentioned;
00106   }
00107 
00108   /// \brief Get the query result
00109   BasicBlock *getResult() {
00110     return Result;
00111   }
00112 };
00113 
00114 /// @brief Transforms the control flow graph on one single entry/exit region
00115 /// at a time.
00116 ///
00117 /// After the transform all "If"/"Then"/"Else" style control flow looks like
00118 /// this:
00119 ///
00120 /// \verbatim
00121 /// 1
00122 /// ||
00123 /// | |
00124 /// 2 |
00125 /// | /
00126 /// |/
00127 /// 3
00128 /// ||   Where:
00129 /// | |  1 = "If" block, calculates the condition
00130 /// 4 |  2 = "Then" subregion, runs if the condition is true
00131 /// | /  3 = "Flow" blocks, newly inserted flow blocks, rejoins the flow
00132 /// |/   4 = "Else" optional subregion, runs if the condition is false
00133 /// 5    5 = "End" block, also rejoins the control flow
00134 /// \endverbatim
00135 ///
00136 /// Control flow is expressed as a branch where the true exit goes into the
00137 /// "Then"/"Else" region, while the false exit skips the region
00138 /// The condition for the optional "Else" region is expressed as a PHI node.
00139 /// The incomming values of the PHI node are true for the "If" edge and false
00140 /// for the "Then" edge.
00141 ///
00142 /// Additionally to that even complicated loops look like this:
00143 ///
00144 /// \verbatim
00145 /// 1
00146 /// ||
00147 /// | |
00148 /// 2 ^  Where:
00149 /// | /  1 = "Entry" block
00150 /// |/   2 = "Loop" optional subregion, with all exits at "Flow" block
00151 /// 3    3 = "Flow" block, with back edge to entry block
00152 /// |
00153 /// \endverbatim
00154 ///
00155 /// The back edge of the "Flow" block is always on the false side of the branch
00156 /// while the true side continues the general flow. So the loop condition
00157 /// consist of a network of PHI nodes where the true incoming values expresses
00158 /// breaks and the false values expresses continue states.
00159 class StructurizeCFG : public RegionPass {
00160   Type *Boolean;
00161   ConstantInt *BoolTrue;
00162   ConstantInt *BoolFalse;
00163   UndefValue *BoolUndef;
00164 
00165   Function *Func;
00166   Region *ParentRegion;
00167 
00168   DominatorTree *DT;
00169 
00170   RNVector Order;
00171   BBSet Visited;
00172 
00173   BBPhiMap DeletedPhis;
00174   BB2BBVecMap AddedPhis;
00175 
00176   PredMap Predicates;
00177   BranchVector Conditions;
00178 
00179   BB2BBMap Loops;
00180   PredMap LoopPreds;
00181   BranchVector LoopConds;
00182 
00183   RegionNode *PrevNode;
00184 
00185   void orderNodes();
00186 
00187   void analyzeLoops(RegionNode *N);
00188 
00189   Value *invert(Value *Condition);
00190 
00191   Value *buildCondition(BranchInst *Term, unsigned Idx, bool Invert);
00192 
00193   void gatherPredicates(RegionNode *N);
00194 
00195   void collectInfos();
00196 
00197   void insertConditions(bool Loops);
00198 
00199   void delPhiValues(BasicBlock *From, BasicBlock *To);
00200 
00201   void addPhiValues(BasicBlock *From, BasicBlock *To);
00202 
00203   void setPhiValues();
00204 
00205   void killTerminator(BasicBlock *BB);
00206 
00207   void changeExit(RegionNode *Node, BasicBlock *NewExit,
00208                   bool IncludeDominator);
00209 
00210   BasicBlock *getNextFlow(BasicBlock *Dominator);
00211 
00212   BasicBlock *needPrefix(bool NeedEmpty);
00213 
00214   BasicBlock *needPostfix(BasicBlock *Flow, bool ExitUseAllowed);
00215 
00216   void setPrevNode(BasicBlock *BB);
00217 
00218   bool dominatesPredicates(BasicBlock *BB, RegionNode *Node);
00219 
00220   bool isPredictableTrue(RegionNode *Node);
00221 
00222   void wireFlow(bool ExitUseAllowed, BasicBlock *LoopEnd);
00223 
00224   void handleLoops(bool ExitUseAllowed, BasicBlock *LoopEnd);
00225 
00226   void createFlow();
00227 
00228   void rebuildSSA();
00229 
00230 public:
00231   static char ID;
00232 
00233   StructurizeCFG() :
00234     RegionPass(ID) {
00235     initializeStructurizeCFGPass(*PassRegistry::getPassRegistry());
00236   }
00237 
00238   using Pass::doInitialization;
00239   bool doInitialization(Region *R, RGPassManager &RGM) override;
00240 
00241   bool runOnRegion(Region *R, RGPassManager &RGM) override;
00242 
00243   const char *getPassName() const override {
00244     return "Structurize control flow";
00245   }
00246 
00247   void getAnalysisUsage(AnalysisUsage &AU) const override {
00248     AU.addRequiredID(LowerSwitchID);
00249     AU.addRequired<DominatorTreeWrapperPass>();
00250     AU.addPreserved<DominatorTreeWrapperPass>();
00251     RegionPass::getAnalysisUsage(AU);
00252   }
00253 };
00254 
00255 } // end anonymous namespace
00256 
00257 char StructurizeCFG::ID = 0;
00258 
00259 INITIALIZE_PASS_BEGIN(StructurizeCFG, "structurizecfg", "Structurize the CFG",
00260                       false, false)
00261 INITIALIZE_PASS_DEPENDENCY(LowerSwitch)
00262 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
00263 INITIALIZE_PASS_DEPENDENCY(RegionInfoPass)
00264 INITIALIZE_PASS_END(StructurizeCFG, "structurizecfg", "Structurize the CFG",
00265                     false, false)
00266 
00267 /// \brief Initialize the types and constants used in the pass
00268 bool StructurizeCFG::doInitialization(Region *R, RGPassManager &RGM) {
00269   LLVMContext &Context = R->getEntry()->getContext();
00270 
00271   Boolean = Type::getInt1Ty(Context);
00272   BoolTrue = ConstantInt::getTrue(Context);
00273   BoolFalse = ConstantInt::getFalse(Context);
00274   BoolUndef = UndefValue::get(Boolean);
00275 
00276   return false;
00277 }
00278 
00279 /// \brief Build up the general order of nodes
00280 void StructurizeCFG::orderNodes() {
00281   scc_iterator<Region *> I = scc_begin(ParentRegion);
00282   for (Order.clear(); !I.isAtEnd(); ++I) {
00283     const std::vector<RegionNode *> &Nodes = *I;
00284     Order.append(Nodes.begin(), Nodes.end());
00285   }
00286 }
00287 
00288 /// \brief Determine the end of the loops
00289 void StructurizeCFG::analyzeLoops(RegionNode *N) {
00290   if (N->isSubRegion()) {
00291     // Test for exit as back edge
00292     BasicBlock *Exit = N->getNodeAs<Region>()->getExit();
00293     if (Visited.count(Exit))
00294       Loops[Exit] = N->getEntry();
00295 
00296   } else {
00297     // Test for sucessors as back edge
00298     BasicBlock *BB = N->getNodeAs<BasicBlock>();
00299     BranchInst *Term = cast<BranchInst>(BB->getTerminator());
00300 
00301     for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) {
00302       BasicBlock *Succ = Term->getSuccessor(i);
00303 
00304       if (Visited.count(Succ))
00305         Loops[Succ] = BB;
00306     }
00307   }
00308 }
00309 
00310 /// \brief Invert the given condition
00311 Value *StructurizeCFG::invert(Value *Condition) {
00312   // First: Check if it's a constant
00313   if (Condition == BoolTrue)
00314     return BoolFalse;
00315 
00316   if (Condition == BoolFalse)
00317     return BoolTrue;
00318 
00319   if (Condition == BoolUndef)
00320     return BoolUndef;
00321 
00322   // Second: If the condition is already inverted, return the original value
00323   if (match(Condition, m_Not(m_Value(Condition))))
00324     return Condition;
00325 
00326   if (Instruction *Inst = dyn_cast<Instruction>(Condition)) {
00327     // Third: Check all the users for an invert
00328     BasicBlock *Parent = Inst->getParent();
00329     for (User *U : Condition->users())
00330       if (Instruction *I = dyn_cast<Instruction>(U))
00331         if (I->getParent() == Parent && match(I, m_Not(m_Specific(Condition))))
00332           return I;
00333 
00334     // Last option: Create a new instruction
00335     return BinaryOperator::CreateNot(Condition, "", Parent->getTerminator());
00336   }
00337 
00338   if (Argument *Arg = dyn_cast<Argument>(Condition)) {
00339     BasicBlock &EntryBlock = Arg->getParent()->getEntryBlock();
00340     return BinaryOperator::CreateNot(Condition,
00341                                      Arg->getName() + ".inv",
00342                                      EntryBlock.getTerminator());
00343   }
00344 
00345   llvm_unreachable("Unhandled condition to invert");
00346 }
00347 
00348 /// \brief Build the condition for one edge
00349 Value *StructurizeCFG::buildCondition(BranchInst *Term, unsigned Idx,
00350                                       bool Invert) {
00351   Value *Cond = Invert ? BoolFalse : BoolTrue;
00352   if (Term->isConditional()) {
00353     Cond = Term->getCondition();
00354 
00355     if (Idx != (unsigned)Invert)
00356       Cond = invert(Cond);
00357   }
00358   return Cond;
00359 }
00360 
00361 /// \brief Analyze the predecessors of each block and build up predicates
00362 void StructurizeCFG::gatherPredicates(RegionNode *N) {
00363   RegionInfo *RI = ParentRegion->getRegionInfo();
00364   BasicBlock *BB = N->getEntry();
00365   BBPredicates &Pred = Predicates[BB];
00366   BBPredicates &LPred = LoopPreds[BB];
00367 
00368   for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
00369        PI != PE; ++PI) {
00370 
00371     // Ignore it if it's a branch from outside into our region entry
00372     if (!ParentRegion->contains(*PI))
00373       continue;
00374 
00375     Region *R = RI->getRegionFor(*PI);
00376     if (R == ParentRegion) {
00377 
00378       // It's a top level block in our region
00379       BranchInst *Term = cast<BranchInst>((*PI)->getTerminator());
00380       for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) {
00381         BasicBlock *Succ = Term->getSuccessor(i);
00382         if (Succ != BB)
00383           continue;
00384 
00385         if (Visited.count(*PI)) {
00386           // Normal forward edge
00387           if (Term->isConditional()) {
00388             // Try to treat it like an ELSE block
00389             BasicBlock *Other = Term->getSuccessor(!i);
00390             if (Visited.count(Other) && !Loops.count(Other) &&
00391                 !Pred.count(Other) && !Pred.count(*PI)) {
00392 
00393               Pred[Other] = BoolFalse;
00394               Pred[*PI] = BoolTrue;
00395               continue;
00396             }
00397           }
00398           Pred[*PI] = buildCondition(Term, i, false);
00399 
00400         } else {
00401           // Back edge
00402           LPred[*PI] = buildCondition(Term, i, true);
00403         }
00404       }
00405 
00406     } else {
00407 
00408       // It's an exit from a sub region
00409       while (R->getParent() != ParentRegion)
00410         R = R->getParent();
00411 
00412       // Edge from inside a subregion to its entry, ignore it
00413       if (*R == *N)
00414         continue;
00415 
00416       BasicBlock *Entry = R->getEntry();
00417       if (Visited.count(Entry))
00418         Pred[Entry] = BoolTrue;
00419       else
00420         LPred[Entry] = BoolFalse;
00421     }
00422   }
00423 }
00424 
00425 /// \brief Collect various loop and predicate infos
00426 void StructurizeCFG::collectInfos() {
00427   // Reset predicate
00428   Predicates.clear();
00429 
00430   // and loop infos
00431   Loops.clear();
00432   LoopPreds.clear();
00433 
00434   // Reset the visited nodes
00435   Visited.clear();
00436 
00437   for (RNVector::reverse_iterator OI = Order.rbegin(), OE = Order.rend();
00438        OI != OE; ++OI) {
00439 
00440     // Analyze all the conditions leading to a node
00441     gatherPredicates(*OI);
00442 
00443     // Remember that we've seen this node
00444     Visited.insert((*OI)->getEntry());
00445 
00446     // Find the last back edges
00447     analyzeLoops(*OI);
00448   }
00449 }
00450 
00451 /// \brief Insert the missing branch conditions
00452 void StructurizeCFG::insertConditions(bool Loops) {
00453   BranchVector &Conds = Loops ? LoopConds : Conditions;
00454   Value *Default = Loops ? BoolTrue : BoolFalse;
00455   SSAUpdater PhiInserter;
00456 
00457   for (BranchInst *Term : Conds) {
00458     assert(Term->isConditional());
00459 
00460     BasicBlock *Parent = Term->getParent();
00461     BasicBlock *SuccTrue = Term->getSuccessor(0);
00462     BasicBlock *SuccFalse = Term->getSuccessor(1);
00463 
00464     PhiInserter.Initialize(Boolean, "");
00465     PhiInserter.AddAvailableValue(&Func->getEntryBlock(), Default);
00466     PhiInserter.AddAvailableValue(Loops ? SuccFalse : Parent, Default);
00467 
00468     BBPredicates &Preds = Loops ? LoopPreds[SuccFalse] : Predicates[SuccTrue];
00469 
00470     NearestCommonDominator Dominator(DT);
00471     Dominator.addBlock(Parent, false);
00472 
00473     Value *ParentValue = nullptr;
00474     for (BBPredicates::iterator PI = Preds.begin(), PE = Preds.end();
00475          PI != PE; ++PI) {
00476 
00477       if (PI->first == Parent) {
00478         ParentValue = PI->second;
00479         break;
00480       }
00481       PhiInserter.AddAvailableValue(PI->first, PI->second);
00482       Dominator.addBlock(PI->first);
00483     }
00484 
00485     if (ParentValue) {
00486       Term->setCondition(ParentValue);
00487     } else {
00488       if (!Dominator.wasResultExplicitMentioned())
00489         PhiInserter.AddAvailableValue(Dominator.getResult(), Default);
00490 
00491       Term->setCondition(PhiInserter.GetValueInMiddleOfBlock(Parent));
00492     }
00493   }
00494 }
00495 
00496 /// \brief Remove all PHI values coming from "From" into "To" and remember
00497 /// them in DeletedPhis
00498 void StructurizeCFG::delPhiValues(BasicBlock *From, BasicBlock *To) {
00499   PhiMap &Map = DeletedPhis[To];
00500   for (BasicBlock::iterator I = To->begin(), E = To->end();
00501        I != E && isa<PHINode>(*I);) {
00502 
00503     PHINode &Phi = cast<PHINode>(*I++);
00504     while (Phi.getBasicBlockIndex(From) != -1) {
00505       Value *Deleted = Phi.removeIncomingValue(From, false);
00506       Map[&Phi].push_back(std::make_pair(From, Deleted));
00507     }
00508   }
00509 }
00510 
00511 /// \brief Add a dummy PHI value as soon as we knew the new predecessor
00512 void StructurizeCFG::addPhiValues(BasicBlock *From, BasicBlock *To) {
00513   for (BasicBlock::iterator I = To->begin(), E = To->end();
00514        I != E && isa<PHINode>(*I);) {
00515 
00516     PHINode &Phi = cast<PHINode>(*I++);
00517     Value *Undef = UndefValue::get(Phi.getType());
00518     Phi.addIncoming(Undef, From);
00519   }
00520   AddedPhis[To].push_back(From);
00521 }
00522 
00523 /// \brief Add the real PHI value as soon as everything is set up
00524 void StructurizeCFG::setPhiValues() {
00525   SSAUpdater Updater;
00526   for (BB2BBVecMap::iterator AI = AddedPhis.begin(), AE = AddedPhis.end();
00527        AI != AE; ++AI) {
00528 
00529     BasicBlock *To = AI->first;
00530     BBVector &From = AI->second;
00531 
00532     if (!DeletedPhis.count(To))
00533       continue;
00534 
00535     PhiMap &Map = DeletedPhis[To];
00536     for (PhiMap::iterator PI = Map.begin(), PE = Map.end();
00537          PI != PE; ++PI) {
00538 
00539       PHINode *Phi = PI->first;
00540       Value *Undef = UndefValue::get(Phi->getType());
00541       Updater.Initialize(Phi->getType(), "");
00542       Updater.AddAvailableValue(&Func->getEntryBlock(), Undef);
00543       Updater.AddAvailableValue(To, Undef);
00544 
00545       NearestCommonDominator Dominator(DT);
00546       Dominator.addBlock(To, false);
00547       for (BBValueVector::iterator VI = PI->second.begin(),
00548            VE = PI->second.end(); VI != VE; ++VI) {
00549 
00550         Updater.AddAvailableValue(VI->first, VI->second);
00551         Dominator.addBlock(VI->first);
00552       }
00553 
00554       if (!Dominator.wasResultExplicitMentioned())
00555         Updater.AddAvailableValue(Dominator.getResult(), Undef);
00556 
00557       for (BBVector::iterator FI = From.begin(), FE = From.end();
00558            FI != FE; ++FI) {
00559 
00560         int Idx = Phi->getBasicBlockIndex(*FI);
00561         assert(Idx != -1);
00562         Phi->setIncomingValue(Idx, Updater.GetValueAtEndOfBlock(*FI));
00563       }
00564     }
00565 
00566     DeletedPhis.erase(To);
00567   }
00568   assert(DeletedPhis.empty());
00569 }
00570 
00571 /// \brief Remove phi values from all successors and then remove the terminator.
00572 void StructurizeCFG::killTerminator(BasicBlock *BB) {
00573   TerminatorInst *Term = BB->getTerminator();
00574   if (!Term)
00575     return;
00576 
00577   for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB);
00578        SI != SE; ++SI) {
00579 
00580     delPhiValues(BB, *SI);
00581   }
00582 
00583   Term->eraseFromParent();
00584 }
00585 
00586 /// \brief Let node exit(s) point to NewExit
00587 void StructurizeCFG::changeExit(RegionNode *Node, BasicBlock *NewExit,
00588                                 bool IncludeDominator) {
00589   if (Node->isSubRegion()) {
00590     Region *SubRegion = Node->getNodeAs<Region>();
00591     BasicBlock *OldExit = SubRegion->getExit();
00592     BasicBlock *Dominator = nullptr;
00593 
00594     // Find all the edges from the sub region to the exit
00595     for (pred_iterator I = pred_begin(OldExit), E = pred_end(OldExit);
00596          I != E;) {
00597 
00598       BasicBlock *BB = *I++;
00599       if (!SubRegion->contains(BB))
00600         continue;
00601 
00602       // Modify the edges to point to the new exit
00603       delPhiValues(BB, OldExit);
00604       BB->getTerminator()->replaceUsesOfWith(OldExit, NewExit);
00605       addPhiValues(BB, NewExit);
00606 
00607       // Find the new dominator (if requested)
00608       if (IncludeDominator) {
00609         if (!Dominator)
00610           Dominator = BB;
00611         else
00612           Dominator = DT->findNearestCommonDominator(Dominator, BB);
00613       }
00614     }
00615 
00616     // Change the dominator (if requested)
00617     if (Dominator)
00618       DT->changeImmediateDominator(NewExit, Dominator);
00619 
00620     // Update the region info
00621     SubRegion->replaceExit(NewExit);
00622 
00623   } else {
00624     BasicBlock *BB = Node->getNodeAs<BasicBlock>();
00625     killTerminator(BB);
00626     BranchInst::Create(NewExit, BB);
00627     addPhiValues(BB, NewExit);
00628     if (IncludeDominator)
00629       DT->changeImmediateDominator(NewExit, BB);
00630   }
00631 }
00632 
00633 /// \brief Create a new flow node and update dominator tree and region info
00634 BasicBlock *StructurizeCFG::getNextFlow(BasicBlock *Dominator) {
00635   LLVMContext &Context = Func->getContext();
00636   BasicBlock *Insert = Order.empty() ? ParentRegion->getExit() :
00637                        Order.back()->getEntry();
00638   BasicBlock *Flow = BasicBlock::Create(Context, FlowBlockName,
00639                                         Func, Insert);
00640   DT->addNewBlock(Flow, Dominator);
00641   ParentRegion->getRegionInfo()->setRegionFor(Flow, ParentRegion);
00642   return Flow;
00643 }
00644 
00645 /// \brief Create a new or reuse the previous node as flow node
00646 BasicBlock *StructurizeCFG::needPrefix(bool NeedEmpty) {
00647   BasicBlock *Entry = PrevNode->getEntry();
00648 
00649   if (!PrevNode->isSubRegion()) {
00650     killTerminator(Entry);
00651     if (!NeedEmpty || Entry->getFirstInsertionPt() == Entry->end())
00652       return Entry;
00653 
00654   }
00655 
00656   // create a new flow node
00657   BasicBlock *Flow = getNextFlow(Entry);
00658 
00659   // and wire it up
00660   changeExit(PrevNode, Flow, true);
00661   PrevNode = ParentRegion->getBBNode(Flow);
00662   return Flow;
00663 }
00664 
00665 /// \brief Returns the region exit if possible, otherwise just a new flow node
00666 BasicBlock *StructurizeCFG::needPostfix(BasicBlock *Flow,
00667                                         bool ExitUseAllowed) {
00668   if (Order.empty() && ExitUseAllowed) {
00669     BasicBlock *Exit = ParentRegion->getExit();
00670     DT->changeImmediateDominator(Exit, Flow);
00671     addPhiValues(Flow, Exit);
00672     return Exit;
00673   }
00674   return getNextFlow(Flow);
00675 }
00676 
00677 /// \brief Set the previous node
00678 void StructurizeCFG::setPrevNode(BasicBlock *BB) {
00679   PrevNode = ParentRegion->contains(BB) ? ParentRegion->getBBNode(BB)
00680                                         : nullptr;
00681 }
00682 
00683 /// \brief Does BB dominate all the predicates of Node ?
00684 bool StructurizeCFG::dominatesPredicates(BasicBlock *BB, RegionNode *Node) {
00685   BBPredicates &Preds = Predicates[Node->getEntry()];
00686   for (BBPredicates::iterator PI = Preds.begin(), PE = Preds.end();
00687        PI != PE; ++PI) {
00688 
00689     if (!DT->dominates(BB, PI->first))
00690       return false;
00691   }
00692   return true;
00693 }
00694 
00695 /// \brief Can we predict that this node will always be called?
00696 bool StructurizeCFG::isPredictableTrue(RegionNode *Node) {
00697   BBPredicates &Preds = Predicates[Node->getEntry()];
00698   bool Dominated = false;
00699 
00700   // Regionentry is always true
00701   if (!PrevNode)
00702     return true;
00703 
00704   for (BBPredicates::iterator I = Preds.begin(), E = Preds.end();
00705        I != E; ++I) {
00706 
00707     if (I->second != BoolTrue)
00708       return false;
00709 
00710     if (!Dominated && DT->dominates(I->first, PrevNode->getEntry()))
00711       Dominated = true;
00712   }
00713 
00714   // TODO: The dominator check is too strict
00715   return Dominated;
00716 }
00717 
00718 /// Take one node from the order vector and wire it up
00719 void StructurizeCFG::wireFlow(bool ExitUseAllowed,
00720                               BasicBlock *LoopEnd) {
00721   RegionNode *Node = Order.pop_back_val();
00722   Visited.insert(Node->getEntry());
00723 
00724   if (isPredictableTrue(Node)) {
00725     // Just a linear flow
00726     if (PrevNode) {
00727       changeExit(PrevNode, Node->getEntry(), true);
00728     }
00729     PrevNode = Node;
00730 
00731   } else {
00732     // Insert extra prefix node (or reuse last one)
00733     BasicBlock *Flow = needPrefix(false);
00734 
00735     // Insert extra postfix node (or use exit instead)
00736     BasicBlock *Entry = Node->getEntry();
00737     BasicBlock *Next = needPostfix(Flow, ExitUseAllowed);
00738 
00739     // let it point to entry and next block
00740     Conditions.push_back(BranchInst::Create(Entry, Next, BoolUndef, Flow));
00741     addPhiValues(Flow, Entry);
00742     DT->changeImmediateDominator(Entry, Flow);
00743 
00744     PrevNode = Node;
00745     while (!Order.empty() && !Visited.count(LoopEnd) &&
00746            dominatesPredicates(Entry, Order.back())) {
00747       handleLoops(false, LoopEnd);
00748     }
00749 
00750     changeExit(PrevNode, Next, false);
00751     setPrevNode(Next);
00752   }
00753 }
00754 
00755 void StructurizeCFG::handleLoops(bool ExitUseAllowed,
00756                                  BasicBlock *LoopEnd) {
00757   RegionNode *Node = Order.back();
00758   BasicBlock *LoopStart = Node->getEntry();
00759 
00760   if (!Loops.count(LoopStart)) {
00761     wireFlow(ExitUseAllowed, LoopEnd);
00762     return;
00763   }
00764 
00765   if (!isPredictableTrue(Node))
00766     LoopStart = needPrefix(true);
00767 
00768   LoopEnd = Loops[Node->getEntry()];
00769   wireFlow(false, LoopEnd);
00770   while (!Visited.count(LoopEnd)) {
00771     handleLoops(false, LoopEnd);
00772   }
00773 
00774   // If the start of the loop is the entry block, we can't branch to it so
00775   // insert a new dummy entry block.
00776   Function *LoopFunc = LoopStart->getParent();
00777   if (LoopStart == &LoopFunc->getEntryBlock()) {
00778     LoopStart->setName("entry.orig");
00779 
00780     BasicBlock *NewEntry =
00781       BasicBlock::Create(LoopStart->getContext(),
00782                          "entry",
00783                          LoopFunc,
00784                          LoopStart);
00785     BranchInst::Create(LoopStart, NewEntry);
00786   }
00787 
00788   // Create an extra loop end node
00789   LoopEnd = needPrefix(false);
00790   BasicBlock *Next = needPostfix(LoopEnd, ExitUseAllowed);
00791   LoopConds.push_back(BranchInst::Create(Next, LoopStart,
00792                                          BoolUndef, LoopEnd));
00793   addPhiValues(LoopEnd, LoopStart);
00794   setPrevNode(Next);
00795 }
00796 
00797 /// After this function control flow looks like it should be, but
00798 /// branches and PHI nodes only have undefined conditions.
00799 void StructurizeCFG::createFlow() {
00800   BasicBlock *Exit = ParentRegion->getExit();
00801   bool EntryDominatesExit = DT->dominates(ParentRegion->getEntry(), Exit);
00802 
00803   DeletedPhis.clear();
00804   AddedPhis.clear();
00805   Conditions.clear();
00806   LoopConds.clear();
00807 
00808   PrevNode = nullptr;
00809   Visited.clear();
00810 
00811   while (!Order.empty()) {
00812     handleLoops(EntryDominatesExit, nullptr);
00813   }
00814 
00815   if (PrevNode)
00816     changeExit(PrevNode, Exit, EntryDominatesExit);
00817   else
00818     assert(EntryDominatesExit);
00819 }
00820 
00821 /// Handle a rare case where the disintegrated nodes instructions
00822 /// no longer dominate all their uses. Not sure if this is really nessasary
00823 void StructurizeCFG::rebuildSSA() {
00824   SSAUpdater Updater;
00825   for (const auto &BB : ParentRegion->blocks())
00826     for (BasicBlock::iterator II = BB->begin(), IE = BB->end();
00827          II != IE; ++II) {
00828 
00829       bool Initialized = false;
00830       for (auto I = II->use_begin(), E = II->use_end(); I != E;) {
00831         Use &U = *I++;
00832         Instruction *User = cast<Instruction>(U.getUser());
00833         if (User->getParent() == BB) {
00834           continue;
00835 
00836         } else if (PHINode *UserPN = dyn_cast<PHINode>(User)) {
00837           if (UserPN->getIncomingBlock(U) == BB)
00838             continue;
00839         }
00840 
00841         if (DT->dominates(II, User))
00842           continue;
00843 
00844         if (!Initialized) {
00845           Value *Undef = UndefValue::get(II->getType());
00846           Updater.Initialize(II->getType(), "");
00847           Updater.AddAvailableValue(&Func->getEntryBlock(), Undef);
00848           Updater.AddAvailableValue(BB, II);
00849           Initialized = true;
00850         }
00851         Updater.RewriteUseAfterInsertions(U);
00852       }
00853     }
00854 }
00855 
00856 /// \brief Run the transformation for each region found
00857 bool StructurizeCFG::runOnRegion(Region *R, RGPassManager &RGM) {
00858   if (R->isTopLevelRegion())
00859     return false;
00860 
00861   Func = R->getEntry()->getParent();
00862   ParentRegion = R;
00863 
00864   DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
00865 
00866   orderNodes();
00867   collectInfos();
00868   createFlow();
00869   insertConditions(false);
00870   insertConditions(true);
00871   setPhiValues();
00872   rebuildSSA();
00873 
00874   // Cleanup
00875   Order.clear();
00876   Visited.clear();
00877   DeletedPhis.clear();
00878   AddedPhis.clear();
00879   Predicates.clear();
00880   Conditions.clear();
00881   Loops.clear();
00882   LoopPreds.clear();
00883   LoopConds.clear();
00884 
00885   return true;
00886 }
00887 
00888 /// \brief Create the pass
00889 Pass *llvm::createStructurizeCFGPass() {
00890   return new StructurizeCFG();
00891 }