1 //===-- StructurizeCFG.cpp ------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Transforms/Scalar.h"
11 #include "llvm/ADT/MapVector.h"
12 #include "llvm/ADT/SCCIterator.h"
13 #include "llvm/ADT/PostOrderIterator.h"
14 #include "llvm/Analysis/LoopInfo.h"
15 #include "llvm/Analysis/RegionInfo.h"
16 #include "llvm/Analysis/RegionIterator.h"
17 #include "llvm/Analysis/RegionPass.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/IR/PatternMatch.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Transforms/Utils/SSAUpdater.h"
24 using namespace llvm::PatternMatch;
26 #define DEBUG_TYPE "structurizecfg"
30 // Definition of the complex types used in this pass.
32 typedef std::pair<BasicBlock *, Value *> BBValuePair;
34 typedef SmallVector<RegionNode*, 8> RNVector;
35 typedef SmallVector<BasicBlock*, 8> BBVector;
36 typedef SmallVector<BranchInst*, 8> BranchVector;
37 typedef SmallVector<BBValuePair, 2> BBValueVector;
39 typedef SmallPtrSet<BasicBlock *, 8> BBSet;
41 typedef MapVector<PHINode *, BBValueVector> PhiMap;
42 typedef MapVector<BasicBlock *, BBVector> BB2BBVecMap;
44 typedef DenseMap<DomTreeNode *, unsigned> DTN2UnsignedMap;
45 typedef DenseMap<BasicBlock *, PhiMap> BBPhiMap;
46 typedef DenseMap<BasicBlock *, Value *> BBPredicates;
47 typedef DenseMap<BasicBlock *, BBPredicates> PredMap;
48 typedef DenseMap<BasicBlock *, BasicBlock*> BB2BBMap;
50 // The name for newly created blocks.
52 static const char *const FlowBlockName = "Flow";
54 /// @brief Find the nearest common dominator for multiple BasicBlocks
56 /// Helper class for StructurizeCFG
57 /// TODO: Maybe move into common code
58 class NearestCommonDominator {
61 DTN2UnsignedMap IndexMap;
65 bool ExplicitMentioned;
68 /// \brief Start a new query
69 NearestCommonDominator(DominatorTree *DomTree) {
74 /// \brief Add BB to the resulting dominator
75 void addBlock(BasicBlock *BB, bool Remember = true) {
76 DomTreeNode *Node = DT->getNode(BB);
79 unsigned Numbering = 0;
80 for (;Node;Node = Node->getIDom())
81 IndexMap[Node] = ++Numbering;
84 ExplicitMentioned = Remember;
88 for (;Node;Node = Node->getIDom())
89 if (IndexMap.count(Node))
94 assert(Node && "Dominator tree invalid!");
96 unsigned Numbering = IndexMap[Node];
97 if (Numbering > ResultIndex) {
98 Result = Node->getBlock();
99 ResultIndex = Numbering;
100 ExplicitMentioned = Remember && (Result == BB);
101 } else if (Numbering == ResultIndex) {
102 ExplicitMentioned |= Remember;
106 /// \brief Is "Result" one of the BBs added with "Remember" = True?
107 bool wasResultExplicitMentioned() {
108 return ExplicitMentioned;
111 /// \brief Get the query result
112 BasicBlock *getResult() {
117 /// @brief Transforms the control flow graph on one single entry/exit region
120 /// After the transform all "If"/"Then"/"Else" style control flow looks like
132 /// | | 1 = "If" block, calculates the condition
133 /// 4 | 2 = "Then" subregion, runs if the condition is true
134 /// | / 3 = "Flow" blocks, newly inserted flow blocks, rejoins the flow
135 /// |/ 4 = "Else" optional subregion, runs if the condition is false
136 /// 5 5 = "End" block, also rejoins the control flow
139 /// Control flow is expressed as a branch where the true exit goes into the
140 /// "Then"/"Else" region, while the false exit skips the region
141 /// The condition for the optional "Else" region is expressed as a PHI node.
142 /// The incomming values of the PHI node are true for the "If" edge and false
143 /// for the "Then" edge.
145 /// Additionally to that even complicated loops look like this:
152 /// | / 1 = "Entry" block
153 /// |/ 2 = "Loop" optional subregion, with all exits at "Flow" block
154 /// 3 3 = "Flow" block, with back edge to entry block
158 /// The back edge of the "Flow" block is always on the false side of the branch
159 /// while the true side continues the general flow. So the loop condition
160 /// consist of a network of PHI nodes where the true incoming values expresses
161 /// breaks and the false values expresses continue states.
162 class StructurizeCFG : public RegionPass {
164 ConstantInt *BoolTrue;
165 ConstantInt *BoolFalse;
166 UndefValue *BoolUndef;
169 Region *ParentRegion;
177 BBPhiMap DeletedPhis;
178 BB2BBVecMap AddedPhis;
181 BranchVector Conditions;
185 BranchVector LoopConds;
187 RegionNode *PrevNode;
191 void analyzeLoops(RegionNode *N);
193 Value *invert(Value *Condition);
195 Value *buildCondition(BranchInst *Term, unsigned Idx, bool Invert);
197 void gatherPredicates(RegionNode *N);
201 void insertConditions(bool Loops);
203 void delPhiValues(BasicBlock *From, BasicBlock *To);
205 void addPhiValues(BasicBlock *From, BasicBlock *To);
209 void killTerminator(BasicBlock *BB);
211 void changeExit(RegionNode *Node, BasicBlock *NewExit,
212 bool IncludeDominator);
214 BasicBlock *getNextFlow(BasicBlock *Dominator);
216 BasicBlock *needPrefix(bool NeedEmpty);
218 BasicBlock *needPostfix(BasicBlock *Flow, bool ExitUseAllowed);
220 void setPrevNode(BasicBlock *BB);
222 bool dominatesPredicates(BasicBlock *BB, RegionNode *Node);
224 bool isPredictableTrue(RegionNode *Node);
226 void wireFlow(bool ExitUseAllowed, BasicBlock *LoopEnd);
228 void handleLoops(bool ExitUseAllowed, BasicBlock *LoopEnd);
239 initializeStructurizeCFGPass(*PassRegistry::getPassRegistry());
242 using Pass::doInitialization;
243 bool doInitialization(Region *R, RGPassManager &RGM) override;
245 bool runOnRegion(Region *R, RGPassManager &RGM) override;
247 const char *getPassName() const override {
248 return "Structurize control flow";
251 void getAnalysisUsage(AnalysisUsage &AU) const override {
252 AU.addRequiredID(LowerSwitchID);
253 AU.addRequired<DominatorTreeWrapperPass>();
254 AU.addRequired<LoopInfoWrapperPass>();
255 AU.addPreserved<DominatorTreeWrapperPass>();
256 RegionPass::getAnalysisUsage(AU);
260 } // end anonymous namespace
262 char StructurizeCFG::ID = 0;
264 INITIALIZE_PASS_BEGIN(StructurizeCFG, "structurizecfg", "Structurize the CFG",
266 INITIALIZE_PASS_DEPENDENCY(LowerSwitch)
267 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
268 INITIALIZE_PASS_DEPENDENCY(RegionInfoPass)
269 INITIALIZE_PASS_END(StructurizeCFG, "structurizecfg", "Structurize the CFG",
272 /// \brief Initialize the types and constants used in the pass
273 bool StructurizeCFG::doInitialization(Region *R, RGPassManager &RGM) {
274 LLVMContext &Context = R->getEntry()->getContext();
276 Boolean = Type::getInt1Ty(Context);
277 BoolTrue = ConstantInt::getTrue(Context);
278 BoolFalse = ConstantInt::getFalse(Context);
279 BoolUndef = UndefValue::get(Boolean);
284 /// \brief Build up the general order of nodes
285 void StructurizeCFG::orderNodes() {
287 ReversePostOrderTraversal<Region*> RPOT(ParentRegion);
288 TempOrder.append(RPOT.begin(), RPOT.end());
290 std::map<Loop*, unsigned> LoopBlocks;
293 // The reverse post-order traversal of the list gives us an ordering close
294 // to what we want. The only problem with it is that sometimes backedges
295 // for outer loops will be visited before backedges for inner loops.
296 for (RegionNode *RN : TempOrder) {
297 BasicBlock *BB = RN->getEntry();
298 Loop *Loop = LI->getLoopFor(BB);
299 if (!LoopBlocks.count(Loop)) {
300 LoopBlocks[Loop] = 1;
306 unsigned CurrentLoopDepth = 0;
307 Loop *CurrentLoop = nullptr;
309 for (RNVector::iterator I = TempOrder.begin(), E = TempOrder.end(); I != E; ++I) {
310 BasicBlock *BB = (*I)->getEntry();
311 unsigned LoopDepth = LI->getLoopDepth(BB);
313 if (std::find(Order.begin(), Order.end(), *I) != Order.end())
316 if (LoopDepth < CurrentLoopDepth) {
317 // Make sure we have visited all blocks in this loop before moving back to
320 RNVector::iterator LoopI = I;
321 while(LoopBlocks[CurrentLoop]) {
323 BasicBlock *LoopBB = (*LoopI)->getEntry();
324 if (LI->getLoopFor(LoopBB) == CurrentLoop) {
325 LoopBlocks[CurrentLoop]--;
326 Order.push_back(*LoopI);
331 CurrentLoop = LI->getLoopFor(BB);
333 LoopBlocks[CurrentLoop]--;
336 CurrentLoopDepth = LoopDepth;
340 // This pass originally used a post-order traversal and then operated on
341 // the list in reverse. Now that we are using a reverse post-order traversal
342 // rather than re-working the whole pass to operate on the list in order,
343 // we just reverse the list and continue to operate on it in reverse.
344 std::reverse(Order.begin(), Order.end());
347 /// \brief Determine the end of the loops
348 void StructurizeCFG::analyzeLoops(RegionNode *N) {
349 if (N->isSubRegion()) {
350 // Test for exit as back edge
351 BasicBlock *Exit = N->getNodeAs<Region>()->getExit();
352 if (Visited.count(Exit))
353 Loops[Exit] = N->getEntry();
356 // Test for sucessors as back edge
357 BasicBlock *BB = N->getNodeAs<BasicBlock>();
358 BranchInst *Term = cast<BranchInst>(BB->getTerminator());
360 for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) {
361 BasicBlock *Succ = Term->getSuccessor(i);
363 if (Visited.count(Succ)) {
370 /// \brief Invert the given condition
371 Value *StructurizeCFG::invert(Value *Condition) {
372 // First: Check if it's a constant
373 if (Condition == BoolTrue)
376 if (Condition == BoolFalse)
379 if (Condition == BoolUndef)
382 // Second: If the condition is already inverted, return the original value
383 if (match(Condition, m_Not(m_Value(Condition))))
386 if (Instruction *Inst = dyn_cast<Instruction>(Condition)) {
387 // Third: Check all the users for an invert
388 BasicBlock *Parent = Inst->getParent();
389 for (User *U : Condition->users())
390 if (Instruction *I = dyn_cast<Instruction>(U))
391 if (I->getParent() == Parent && match(I, m_Not(m_Specific(Condition))))
394 // Last option: Create a new instruction
395 return BinaryOperator::CreateNot(Condition, "", Parent->getTerminator());
398 if (Argument *Arg = dyn_cast<Argument>(Condition)) {
399 BasicBlock &EntryBlock = Arg->getParent()->getEntryBlock();
400 return BinaryOperator::CreateNot(Condition,
401 Arg->getName() + ".inv",
402 EntryBlock.getTerminator());
405 llvm_unreachable("Unhandled condition to invert");
408 /// \brief Build the condition for one edge
409 Value *StructurizeCFG::buildCondition(BranchInst *Term, unsigned Idx,
411 Value *Cond = Invert ? BoolFalse : BoolTrue;
412 if (Term->isConditional()) {
413 Cond = Term->getCondition();
415 if (Idx != (unsigned)Invert)
421 /// \brief Analyze the predecessors of each block and build up predicates
422 void StructurizeCFG::gatherPredicates(RegionNode *N) {
423 RegionInfo *RI = ParentRegion->getRegionInfo();
424 BasicBlock *BB = N->getEntry();
425 BBPredicates &Pred = Predicates[BB];
426 BBPredicates &LPred = LoopPreds[BB];
428 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
431 // Ignore it if it's a branch from outside into our region entry
432 if (!ParentRegion->contains(*PI))
435 Region *R = RI->getRegionFor(*PI);
436 if (R == ParentRegion) {
438 // It's a top level block in our region
439 BranchInst *Term = cast<BranchInst>((*PI)->getTerminator());
440 for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) {
441 BasicBlock *Succ = Term->getSuccessor(i);
445 if (Visited.count(*PI)) {
446 // Normal forward edge
447 if (Term->isConditional()) {
448 // Try to treat it like an ELSE block
449 BasicBlock *Other = Term->getSuccessor(!i);
450 if (Visited.count(Other) && !Loops.count(Other) &&
451 !Pred.count(Other) && !Pred.count(*PI)) {
453 Pred[Other] = BoolFalse;
454 Pred[*PI] = BoolTrue;
458 Pred[*PI] = buildCondition(Term, i, false);
462 LPred[*PI] = buildCondition(Term, i, true);
468 // It's an exit from a sub region
469 while (R->getParent() != ParentRegion)
472 // Edge from inside a subregion to its entry, ignore it
476 BasicBlock *Entry = R->getEntry();
477 if (Visited.count(Entry))
478 Pred[Entry] = BoolTrue;
480 LPred[Entry] = BoolFalse;
485 /// \brief Collect various loop and predicate infos
486 void StructurizeCFG::collectInfos() {
494 // Reset the visited nodes
497 for (RNVector::reverse_iterator OI = Order.rbegin(), OE = Order.rend();
500 DEBUG(dbgs() << "Visiting: " <<
501 ((*OI)->isSubRegion() ? "SubRegion with entry: " : "") <<
502 (*OI)->getEntry()->getName() << " Loop Depth: " << LI->getLoopDepth((*OI)->getEntry()) << "\n");
504 // Analyze all the conditions leading to a node
505 gatherPredicates(*OI);
507 // Remember that we've seen this node
508 Visited.insert((*OI)->getEntry());
510 // Find the last back edges
515 /// \brief Insert the missing branch conditions
516 void StructurizeCFG::insertConditions(bool Loops) {
517 BranchVector &Conds = Loops ? LoopConds : Conditions;
518 Value *Default = Loops ? BoolTrue : BoolFalse;
519 SSAUpdater PhiInserter;
521 for (BranchInst *Term : Conds) {
522 assert(Term->isConditional());
524 BasicBlock *Parent = Term->getParent();
525 BasicBlock *SuccTrue = Term->getSuccessor(0);
526 BasicBlock *SuccFalse = Term->getSuccessor(1);
528 PhiInserter.Initialize(Boolean, "");
529 PhiInserter.AddAvailableValue(&Func->getEntryBlock(), Default);
530 PhiInserter.AddAvailableValue(Loops ? SuccFalse : Parent, Default);
532 BBPredicates &Preds = Loops ? LoopPreds[SuccFalse] : Predicates[SuccTrue];
534 NearestCommonDominator Dominator(DT);
535 Dominator.addBlock(Parent, false);
537 Value *ParentValue = nullptr;
538 for (BBPredicates::iterator PI = Preds.begin(), PE = Preds.end();
541 if (PI->first == Parent) {
542 ParentValue = PI->second;
545 PhiInserter.AddAvailableValue(PI->first, PI->second);
546 Dominator.addBlock(PI->first);
550 Term->setCondition(ParentValue);
552 if (!Dominator.wasResultExplicitMentioned())
553 PhiInserter.AddAvailableValue(Dominator.getResult(), Default);
555 Term->setCondition(PhiInserter.GetValueInMiddleOfBlock(Parent));
560 /// \brief Remove all PHI values coming from "From" into "To" and remember
561 /// them in DeletedPhis
562 void StructurizeCFG::delPhiValues(BasicBlock *From, BasicBlock *To) {
563 PhiMap &Map = DeletedPhis[To];
564 for (BasicBlock::iterator I = To->begin(), E = To->end();
565 I != E && isa<PHINode>(*I);) {
567 PHINode &Phi = cast<PHINode>(*I++);
568 while (Phi.getBasicBlockIndex(From) != -1) {
569 Value *Deleted = Phi.removeIncomingValue(From, false);
570 Map[&Phi].push_back(std::make_pair(From, Deleted));
575 /// \brief Add a dummy PHI value as soon as we knew the new predecessor
576 void StructurizeCFG::addPhiValues(BasicBlock *From, BasicBlock *To) {
577 for (BasicBlock::iterator I = To->begin(), E = To->end();
578 I != E && isa<PHINode>(*I);) {
580 PHINode &Phi = cast<PHINode>(*I++);
581 Value *Undef = UndefValue::get(Phi.getType());
582 Phi.addIncoming(Undef, From);
584 AddedPhis[To].push_back(From);
587 /// \brief Add the real PHI value as soon as everything is set up
588 void StructurizeCFG::setPhiValues() {
590 for (BB2BBVecMap::iterator AI = AddedPhis.begin(), AE = AddedPhis.end();
593 BasicBlock *To = AI->first;
594 BBVector &From = AI->second;
596 if (!DeletedPhis.count(To))
599 PhiMap &Map = DeletedPhis[To];
600 for (PhiMap::iterator PI = Map.begin(), PE = Map.end();
603 PHINode *Phi = PI->first;
604 Value *Undef = UndefValue::get(Phi->getType());
605 Updater.Initialize(Phi->getType(), "");
606 Updater.AddAvailableValue(&Func->getEntryBlock(), Undef);
607 Updater.AddAvailableValue(To, Undef);
609 NearestCommonDominator Dominator(DT);
610 Dominator.addBlock(To, false);
611 for (BBValueVector::iterator VI = PI->second.begin(),
612 VE = PI->second.end(); VI != VE; ++VI) {
614 Updater.AddAvailableValue(VI->first, VI->second);
615 Dominator.addBlock(VI->first);
618 if (!Dominator.wasResultExplicitMentioned())
619 Updater.AddAvailableValue(Dominator.getResult(), Undef);
621 for (BBVector::iterator FI = From.begin(), FE = From.end();
624 int Idx = Phi->getBasicBlockIndex(*FI);
626 Phi->setIncomingValue(Idx, Updater.GetValueAtEndOfBlock(*FI));
630 DeletedPhis.erase(To);
632 assert(DeletedPhis.empty());
635 /// \brief Remove phi values from all successors and then remove the terminator.
636 void StructurizeCFG::killTerminator(BasicBlock *BB) {
637 TerminatorInst *Term = BB->getTerminator();
641 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB);
644 delPhiValues(BB, *SI);
647 Term->eraseFromParent();
650 /// \brief Let node exit(s) point to NewExit
651 void StructurizeCFG::changeExit(RegionNode *Node, BasicBlock *NewExit,
652 bool IncludeDominator) {
653 if (Node->isSubRegion()) {
654 Region *SubRegion = Node->getNodeAs<Region>();
655 BasicBlock *OldExit = SubRegion->getExit();
656 BasicBlock *Dominator = nullptr;
658 // Find all the edges from the sub region to the exit
659 for (pred_iterator I = pred_begin(OldExit), E = pred_end(OldExit);
662 BasicBlock *BB = *I++;
663 if (!SubRegion->contains(BB))
666 // Modify the edges to point to the new exit
667 delPhiValues(BB, OldExit);
668 BB->getTerminator()->replaceUsesOfWith(OldExit, NewExit);
669 addPhiValues(BB, NewExit);
671 // Find the new dominator (if requested)
672 if (IncludeDominator) {
676 Dominator = DT->findNearestCommonDominator(Dominator, BB);
680 // Change the dominator (if requested)
682 DT->changeImmediateDominator(NewExit, Dominator);
684 // Update the region info
685 SubRegion->replaceExit(NewExit);
688 BasicBlock *BB = Node->getNodeAs<BasicBlock>();
690 BranchInst::Create(NewExit, BB);
691 addPhiValues(BB, NewExit);
692 if (IncludeDominator)
693 DT->changeImmediateDominator(NewExit, BB);
697 /// \brief Create a new flow node and update dominator tree and region info
698 BasicBlock *StructurizeCFG::getNextFlow(BasicBlock *Dominator) {
699 LLVMContext &Context = Func->getContext();
700 BasicBlock *Insert = Order.empty() ? ParentRegion->getExit() :
701 Order.back()->getEntry();
702 BasicBlock *Flow = BasicBlock::Create(Context, FlowBlockName,
704 DT->addNewBlock(Flow, Dominator);
705 ParentRegion->getRegionInfo()->setRegionFor(Flow, ParentRegion);
709 /// \brief Create a new or reuse the previous node as flow node
710 BasicBlock *StructurizeCFG::needPrefix(bool NeedEmpty) {
711 BasicBlock *Entry = PrevNode->getEntry();
713 if (!PrevNode->isSubRegion()) {
714 killTerminator(Entry);
715 if (!NeedEmpty || Entry->getFirstInsertionPt() == Entry->end())
720 // create a new flow node
721 BasicBlock *Flow = getNextFlow(Entry);
724 changeExit(PrevNode, Flow, true);
725 PrevNode = ParentRegion->getBBNode(Flow);
729 /// \brief Returns the region exit if possible, otherwise just a new flow node
730 BasicBlock *StructurizeCFG::needPostfix(BasicBlock *Flow,
731 bool ExitUseAllowed) {
732 if (Order.empty() && ExitUseAllowed) {
733 BasicBlock *Exit = ParentRegion->getExit();
734 DT->changeImmediateDominator(Exit, Flow);
735 addPhiValues(Flow, Exit);
738 return getNextFlow(Flow);
741 /// \brief Set the previous node
742 void StructurizeCFG::setPrevNode(BasicBlock *BB) {
743 PrevNode = ParentRegion->contains(BB) ? ParentRegion->getBBNode(BB)
747 /// \brief Does BB dominate all the predicates of Node ?
748 bool StructurizeCFG::dominatesPredicates(BasicBlock *BB, RegionNode *Node) {
749 BBPredicates &Preds = Predicates[Node->getEntry()];
750 for (BBPredicates::iterator PI = Preds.begin(), PE = Preds.end();
753 if (!DT->dominates(BB, PI->first))
759 /// \brief Can we predict that this node will always be called?
760 bool StructurizeCFG::isPredictableTrue(RegionNode *Node) {
761 BBPredicates &Preds = Predicates[Node->getEntry()];
762 bool Dominated = false;
764 // Regionentry is always true
768 for (BBPredicates::iterator I = Preds.begin(), E = Preds.end();
771 if (I->second != BoolTrue)
774 if (!Dominated && DT->dominates(I->first, PrevNode->getEntry()))
778 // TODO: The dominator check is too strict
782 /// Take one node from the order vector and wire it up
783 void StructurizeCFG::wireFlow(bool ExitUseAllowed,
784 BasicBlock *LoopEnd) {
785 RegionNode *Node = Order.pop_back_val();
786 Visited.insert(Node->getEntry());
788 if (isPredictableTrue(Node)) {
789 // Just a linear flow
791 changeExit(PrevNode, Node->getEntry(), true);
796 // Insert extra prefix node (or reuse last one)
797 BasicBlock *Flow = needPrefix(false);
799 // Insert extra postfix node (or use exit instead)
800 BasicBlock *Entry = Node->getEntry();
801 BasicBlock *Next = needPostfix(Flow, ExitUseAllowed);
803 // let it point to entry and next block
804 Conditions.push_back(BranchInst::Create(Entry, Next, BoolUndef, Flow));
805 addPhiValues(Flow, Entry);
806 DT->changeImmediateDominator(Entry, Flow);
809 while (!Order.empty() && !Visited.count(LoopEnd) &&
810 dominatesPredicates(Entry, Order.back())) {
811 handleLoops(false, LoopEnd);
814 changeExit(PrevNode, Next, false);
819 void StructurizeCFG::handleLoops(bool ExitUseAllowed,
820 BasicBlock *LoopEnd) {
821 RegionNode *Node = Order.back();
822 BasicBlock *LoopStart = Node->getEntry();
824 if (!Loops.count(LoopStart)) {
825 wireFlow(ExitUseAllowed, LoopEnd);
829 if (!isPredictableTrue(Node))
830 LoopStart = needPrefix(true);
832 LoopEnd = Loops[Node->getEntry()];
833 wireFlow(false, LoopEnd);
834 while (!Visited.count(LoopEnd)) {
835 handleLoops(false, LoopEnd);
838 // If the start of the loop is the entry block, we can't branch to it so
839 // insert a new dummy entry block.
840 Function *LoopFunc = LoopStart->getParent();
841 if (LoopStart == &LoopFunc->getEntryBlock()) {
842 LoopStart->setName("entry.orig");
844 BasicBlock *NewEntry =
845 BasicBlock::Create(LoopStart->getContext(),
849 BranchInst::Create(LoopStart, NewEntry);
852 // Create an extra loop end node
853 LoopEnd = needPrefix(false);
854 BasicBlock *Next = needPostfix(LoopEnd, ExitUseAllowed);
855 LoopConds.push_back(BranchInst::Create(Next, LoopStart,
856 BoolUndef, LoopEnd));
857 addPhiValues(LoopEnd, LoopStart);
861 /// After this function control flow looks like it should be, but
862 /// branches and PHI nodes only have undefined conditions.
863 void StructurizeCFG::createFlow() {
864 BasicBlock *Exit = ParentRegion->getExit();
865 bool EntryDominatesExit = DT->dominates(ParentRegion->getEntry(), Exit);
875 while (!Order.empty()) {
876 handleLoops(EntryDominatesExit, nullptr);
880 changeExit(PrevNode, Exit, EntryDominatesExit);
882 assert(EntryDominatesExit);
885 /// Handle a rare case where the disintegrated nodes instructions
886 /// no longer dominate all their uses. Not sure if this is really nessasary
887 void StructurizeCFG::rebuildSSA() {
889 for (const auto &BB : ParentRegion->blocks())
890 for (BasicBlock::iterator II = BB->begin(), IE = BB->end();
893 bool Initialized = false;
894 for (auto I = II->use_begin(), E = II->use_end(); I != E;) {
896 Instruction *User = cast<Instruction>(U.getUser());
897 if (User->getParent() == BB) {
900 } else if (PHINode *UserPN = dyn_cast<PHINode>(User)) {
901 if (UserPN->getIncomingBlock(U) == BB)
905 if (DT->dominates(II, User))
909 Value *Undef = UndefValue::get(II->getType());
910 Updater.Initialize(II->getType(), "");
911 Updater.AddAvailableValue(&Func->getEntryBlock(), Undef);
912 Updater.AddAvailableValue(BB, II);
915 Updater.RewriteUseAfterInsertions(U);
920 /// \brief Run the transformation for each region found
921 bool StructurizeCFG::runOnRegion(Region *R, RGPassManager &RGM) {
922 if (R->isTopLevelRegion())
925 Func = R->getEntry()->getParent();
928 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
929 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
934 insertConditions(false);
935 insertConditions(true);
953 /// \brief Create the pass
954 Pass *llvm::createStructurizeCFGPass() {
955 return new StructurizeCFG();