[Modules] Fix potential ODR violations by sinking the DEBUG_TYPE
[oota-llvm.git] / lib / Transforms / Scalar / StructurizeCFG.cpp
1 //===-- StructurizeCFG.cpp ------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/Transforms/Scalar.h"
11 #include "llvm/ADT/MapVector.h"
12 #include "llvm/ADT/SCCIterator.h"
13 #include "llvm/Analysis/RegionInfo.h"
14 #include "llvm/Analysis/RegionIterator.h"
15 #include "llvm/Analysis/RegionPass.h"
16 #include "llvm/IR/Module.h"
17 #include "llvm/IR/PatternMatch.h"
18 #include "llvm/Transforms/Utils/SSAUpdater.h"
19
20 using namespace llvm;
21 using namespace llvm::PatternMatch;
22
23 #define DEBUG_TYPE "structurizecfg"
24
25 namespace {
26
27 // Definition of the complex types used in this pass.
28
29 typedef std::pair<BasicBlock *, Value *> BBValuePair;
30
31 typedef SmallVector<RegionNode*, 8> RNVector;
32 typedef SmallVector<BasicBlock*, 8> BBVector;
33 typedef SmallVector<BranchInst*, 8> BranchVector;
34 typedef SmallVector<BBValuePair, 2> BBValueVector;
35
36 typedef SmallPtrSet<BasicBlock *, 8> BBSet;
37
38 typedef MapVector<PHINode *, BBValueVector> PhiMap;
39 typedef MapVector<BasicBlock *, BBVector> BB2BBVecMap;
40
41 typedef DenseMap<DomTreeNode *, unsigned> DTN2UnsignedMap;
42 typedef DenseMap<BasicBlock *, PhiMap> BBPhiMap;
43 typedef DenseMap<BasicBlock *, Value *> BBPredicates;
44 typedef DenseMap<BasicBlock *, BBPredicates> PredMap;
45 typedef DenseMap<BasicBlock *, BasicBlock*> BB2BBMap;
46
47 // The name for newly created blocks.
48
49 static const char *const FlowBlockName = "Flow";
50
51 /// @brief Find the nearest common dominator for multiple BasicBlocks
52 ///
53 /// Helper class for StructurizeCFG
54 /// TODO: Maybe move into common code
55 class NearestCommonDominator {
56   DominatorTree *DT;
57
58   DTN2UnsignedMap IndexMap;
59
60   BasicBlock *Result;
61   unsigned ResultIndex;
62   bool ExplicitMentioned;
63
64 public:
65   /// \brief Start a new query
66   NearestCommonDominator(DominatorTree *DomTree) {
67     DT = DomTree;
68     Result = 0;
69   }
70
71   /// \brief Add BB to the resulting dominator
72   void addBlock(BasicBlock *BB, bool Remember = true) {
73     DomTreeNode *Node = DT->getNode(BB);
74
75     if (Result == 0) {
76       unsigned Numbering = 0;
77       for (;Node;Node = Node->getIDom())
78         IndexMap[Node] = ++Numbering;
79       Result = BB;
80       ResultIndex = 1;
81       ExplicitMentioned = Remember;
82       return;
83     }
84
85     for (;Node;Node = Node->getIDom())
86       if (IndexMap.count(Node))
87         break;
88       else
89         IndexMap[Node] = 0;
90
91     assert(Node && "Dominator tree invalid!");
92
93     unsigned Numbering = IndexMap[Node];
94     if (Numbering > ResultIndex) {
95       Result = Node->getBlock();
96       ResultIndex = Numbering;
97       ExplicitMentioned = Remember && (Result == BB);
98     } else if (Numbering == ResultIndex) {
99       ExplicitMentioned |= Remember;
100     }
101   }
102
103   /// \brief Is "Result" one of the BBs added with "Remember" = True?
104   bool wasResultExplicitMentioned() {
105     return ExplicitMentioned;
106   }
107
108   /// \brief Get the query result
109   BasicBlock *getResult() {
110     return Result;
111   }
112 };
113
114 /// @brief Transforms the control flow graph on one single entry/exit region
115 /// at a time.
116 ///
117 /// After the transform all "If"/"Then"/"Else" style control flow looks like
118 /// this:
119 ///
120 /// \verbatim
121 /// 1
122 /// ||
123 /// | |
124 /// 2 |
125 /// | /
126 /// |/
127 /// 3
128 /// ||   Where:
129 /// | |  1 = "If" block, calculates the condition
130 /// 4 |  2 = "Then" subregion, runs if the condition is true
131 /// | /  3 = "Flow" blocks, newly inserted flow blocks, rejoins the flow
132 /// |/   4 = "Else" optional subregion, runs if the condition is false
133 /// 5    5 = "End" block, also rejoins the control flow
134 /// \endverbatim
135 ///
136 /// Control flow is expressed as a branch where the true exit goes into the
137 /// "Then"/"Else" region, while the false exit skips the region
138 /// The condition for the optional "Else" region is expressed as a PHI node.
139 /// The incomming values of the PHI node are true for the "If" edge and false
140 /// for the "Then" edge.
141 ///
142 /// Additionally to that even complicated loops look like this:
143 ///
144 /// \verbatim
145 /// 1
146 /// ||
147 /// | |
148 /// 2 ^  Where:
149 /// | /  1 = "Entry" block
150 /// |/   2 = "Loop" optional subregion, with all exits at "Flow" block
151 /// 3    3 = "Flow" block, with back edge to entry block
152 /// |
153 /// \endverbatim
154 ///
155 /// The back edge of the "Flow" block is always on the false side of the branch
156 /// while the true side continues the general flow. So the loop condition
157 /// consist of a network of PHI nodes where the true incoming values expresses
158 /// breaks and the false values expresses continue states.
159 class StructurizeCFG : public RegionPass {
160   Type *Boolean;
161   ConstantInt *BoolTrue;
162   ConstantInt *BoolFalse;
163   UndefValue *BoolUndef;
164
165   Function *Func;
166   Region *ParentRegion;
167
168   DominatorTree *DT;
169
170   RNVector Order;
171   BBSet Visited;
172
173   BBPhiMap DeletedPhis;
174   BB2BBVecMap AddedPhis;
175
176   PredMap Predicates;
177   BranchVector Conditions;
178
179   BB2BBMap Loops;
180   PredMap LoopPreds;
181   BranchVector LoopConds;
182
183   RegionNode *PrevNode;
184
185   void orderNodes();
186
187   void analyzeLoops(RegionNode *N);
188
189   Value *invert(Value *Condition);
190
191   Value *buildCondition(BranchInst *Term, unsigned Idx, bool Invert);
192
193   void gatherPredicates(RegionNode *N);
194
195   void collectInfos();
196
197   void insertConditions(bool Loops);
198
199   void delPhiValues(BasicBlock *From, BasicBlock *To);
200
201   void addPhiValues(BasicBlock *From, BasicBlock *To);
202
203   void setPhiValues();
204
205   void killTerminator(BasicBlock *BB);
206
207   void changeExit(RegionNode *Node, BasicBlock *NewExit,
208                   bool IncludeDominator);
209
210   BasicBlock *getNextFlow(BasicBlock *Dominator);
211
212   BasicBlock *needPrefix(bool NeedEmpty);
213
214   BasicBlock *needPostfix(BasicBlock *Flow, bool ExitUseAllowed);
215
216   void setPrevNode(BasicBlock *BB);
217
218   bool dominatesPredicates(BasicBlock *BB, RegionNode *Node);
219
220   bool isPredictableTrue(RegionNode *Node);
221
222   void wireFlow(bool ExitUseAllowed, BasicBlock *LoopEnd);
223
224   void handleLoops(bool ExitUseAllowed, BasicBlock *LoopEnd);
225
226   void createFlow();
227
228   void rebuildSSA();
229
230 public:
231   static char ID;
232
233   StructurizeCFG() :
234     RegionPass(ID) {
235     initializeStructurizeCFGPass(*PassRegistry::getPassRegistry());
236   }
237
238   using Pass::doInitialization;
239   bool doInitialization(Region *R, RGPassManager &RGM) override;
240
241   bool runOnRegion(Region *R, RGPassManager &RGM) override;
242
243   const char *getPassName() const override {
244     return "Structurize control flow";
245   }
246
247   void getAnalysisUsage(AnalysisUsage &AU) const override {
248     AU.addRequiredID(LowerSwitchID);
249     AU.addRequired<DominatorTreeWrapperPass>();
250     AU.addPreserved<DominatorTreeWrapperPass>();
251     RegionPass::getAnalysisUsage(AU);
252   }
253 };
254
255 } // end anonymous namespace
256
257 char StructurizeCFG::ID = 0;
258
259 INITIALIZE_PASS_BEGIN(StructurizeCFG, "structurizecfg", "Structurize the CFG",
260                       false, false)
261 INITIALIZE_PASS_DEPENDENCY(LowerSwitch)
262 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
263 INITIALIZE_PASS_DEPENDENCY(RegionInfo)
264 INITIALIZE_PASS_END(StructurizeCFG, "structurizecfg", "Structurize the CFG",
265                     false, false)
266
267 /// \brief Initialize the types and constants used in the pass
268 bool StructurizeCFG::doInitialization(Region *R, RGPassManager &RGM) {
269   LLVMContext &Context = R->getEntry()->getContext();
270
271   Boolean = Type::getInt1Ty(Context);
272   BoolTrue = ConstantInt::getTrue(Context);
273   BoolFalse = ConstantInt::getFalse(Context);
274   BoolUndef = UndefValue::get(Boolean);
275
276   return false;
277 }
278
279 /// \brief Build up the general order of nodes
280 void StructurizeCFG::orderNodes() {
281   scc_iterator<Region *> I = scc_begin(ParentRegion);
282   for (Order.clear(); !I.isAtEnd(); ++I) {
283     std::vector<RegionNode *> &Nodes = *I;
284     Order.append(Nodes.begin(), Nodes.end());
285   }
286 }
287
288 /// \brief Determine the end of the loops
289 void StructurizeCFG::analyzeLoops(RegionNode *N) {
290   if (N->isSubRegion()) {
291     // Test for exit as back edge
292     BasicBlock *Exit = N->getNodeAs<Region>()->getExit();
293     if (Visited.count(Exit))
294       Loops[Exit] = N->getEntry();
295
296   } else {
297     // Test for sucessors as back edge
298     BasicBlock *BB = N->getNodeAs<BasicBlock>();
299     BranchInst *Term = cast<BranchInst>(BB->getTerminator());
300
301     for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) {
302       BasicBlock *Succ = Term->getSuccessor(i);
303
304       if (Visited.count(Succ))
305         Loops[Succ] = BB;
306     }
307   }
308 }
309
310 /// \brief Invert the given condition
311 Value *StructurizeCFG::invert(Value *Condition) {
312   // First: Check if it's a constant
313   if (Condition == BoolTrue)
314     return BoolFalse;
315
316   if (Condition == BoolFalse)
317     return BoolTrue;
318
319   if (Condition == BoolUndef)
320     return BoolUndef;
321
322   // Second: If the condition is already inverted, return the original value
323   if (match(Condition, m_Not(m_Value(Condition))))
324     return Condition;
325
326   if (Instruction *Inst = dyn_cast<Instruction>(Condition)) {
327     // Third: Check all the users for an invert
328     BasicBlock *Parent = Inst->getParent();
329     for (User *U : Condition->users())
330       if (Instruction *I = dyn_cast<Instruction>(U))
331         if (I->getParent() == Parent && match(I, m_Not(m_Specific(Condition))))
332           return I;
333
334     // Last option: Create a new instruction
335     return BinaryOperator::CreateNot(Condition, "", Parent->getTerminator());
336   }
337
338   if (Argument *Arg = dyn_cast<Argument>(Condition)) {
339     BasicBlock &EntryBlock = Arg->getParent()->getEntryBlock();
340     return BinaryOperator::CreateNot(Condition,
341                                      Arg->getName() + ".inv",
342                                      EntryBlock.getTerminator());
343   }
344
345   llvm_unreachable("Unhandled condition to invert");
346 }
347
348 /// \brief Build the condition for one edge
349 Value *StructurizeCFG::buildCondition(BranchInst *Term, unsigned Idx,
350                                       bool Invert) {
351   Value *Cond = Invert ? BoolFalse : BoolTrue;
352   if (Term->isConditional()) {
353     Cond = Term->getCondition();
354
355     if (Idx != (unsigned)Invert)
356       Cond = invert(Cond);
357   }
358   return Cond;
359 }
360
361 /// \brief Analyze the predecessors of each block and build up predicates
362 void StructurizeCFG::gatherPredicates(RegionNode *N) {
363   RegionInfo *RI = ParentRegion->getRegionInfo();
364   BasicBlock *BB = N->getEntry();
365   BBPredicates &Pred = Predicates[BB];
366   BBPredicates &LPred = LoopPreds[BB];
367
368   for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
369        PI != PE; ++PI) {
370
371     // Ignore it if it's a branch from outside into our region entry
372     if (!ParentRegion->contains(*PI))
373       continue;
374
375     Region *R = RI->getRegionFor(*PI);
376     if (R == ParentRegion) {
377
378       // It's a top level block in our region
379       BranchInst *Term = cast<BranchInst>((*PI)->getTerminator());
380       for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) {
381         BasicBlock *Succ = Term->getSuccessor(i);
382         if (Succ != BB)
383           continue;
384
385         if (Visited.count(*PI)) {
386           // Normal forward edge
387           if (Term->isConditional()) {
388             // Try to treat it like an ELSE block
389             BasicBlock *Other = Term->getSuccessor(!i);
390             if (Visited.count(Other) && !Loops.count(Other) &&
391                 !Pred.count(Other) && !Pred.count(*PI)) {
392
393               Pred[Other] = BoolFalse;
394               Pred[*PI] = BoolTrue;
395               continue;
396             }
397           }
398           Pred[*PI] = buildCondition(Term, i, false);
399
400         } else {
401           // Back edge
402           LPred[*PI] = buildCondition(Term, i, true);
403         }
404       }
405
406     } else {
407
408       // It's an exit from a sub region
409       while(R->getParent() != ParentRegion)
410         R = R->getParent();
411
412       // Edge from inside a subregion to its entry, ignore it
413       if (R == N)
414         continue;
415
416       BasicBlock *Entry = R->getEntry();
417       if (Visited.count(Entry))
418         Pred[Entry] = BoolTrue;
419       else
420         LPred[Entry] = BoolFalse;
421     }
422   }
423 }
424
425 /// \brief Collect various loop and predicate infos
426 void StructurizeCFG::collectInfos() {
427   // Reset predicate
428   Predicates.clear();
429
430   // and loop infos
431   Loops.clear();
432   LoopPreds.clear();
433
434   // Reset the visited nodes
435   Visited.clear();
436
437   for (RNVector::reverse_iterator OI = Order.rbegin(), OE = Order.rend();
438        OI != OE; ++OI) {
439
440     // Analyze all the conditions leading to a node
441     gatherPredicates(*OI);
442
443     // Remember that we've seen this node
444     Visited.insert((*OI)->getEntry());
445
446     // Find the last back edges
447     analyzeLoops(*OI);
448   }
449 }
450
451 /// \brief Insert the missing branch conditions
452 void StructurizeCFG::insertConditions(bool Loops) {
453   BranchVector &Conds = Loops ? LoopConds : Conditions;
454   Value *Default = Loops ? BoolTrue : BoolFalse;
455   SSAUpdater PhiInserter;
456
457   for (BranchVector::iterator I = Conds.begin(),
458        E = Conds.end(); I != E; ++I) {
459
460     BranchInst *Term = *I;
461     assert(Term->isConditional());
462
463     BasicBlock *Parent = Term->getParent();
464     BasicBlock *SuccTrue = Term->getSuccessor(0);
465     BasicBlock *SuccFalse = Term->getSuccessor(1);
466
467     PhiInserter.Initialize(Boolean, "");
468     PhiInserter.AddAvailableValue(&Func->getEntryBlock(), Default);
469     PhiInserter.AddAvailableValue(Loops ? SuccFalse : Parent, Default);
470
471     BBPredicates &Preds = Loops ? LoopPreds[SuccFalse] : Predicates[SuccTrue];
472
473     NearestCommonDominator Dominator(DT);
474     Dominator.addBlock(Parent, false);
475
476     Value *ParentValue = 0;
477     for (BBPredicates::iterator PI = Preds.begin(), PE = Preds.end();
478          PI != PE; ++PI) {
479
480       if (PI->first == Parent) {
481         ParentValue = PI->second;
482         break;
483       }
484       PhiInserter.AddAvailableValue(PI->first, PI->second);
485       Dominator.addBlock(PI->first);
486     }
487
488     if (ParentValue) {
489       Term->setCondition(ParentValue);
490     } else {
491       if (!Dominator.wasResultExplicitMentioned())
492         PhiInserter.AddAvailableValue(Dominator.getResult(), Default);
493
494       Term->setCondition(PhiInserter.GetValueInMiddleOfBlock(Parent));
495     }
496   }
497 }
498
499 /// \brief Remove all PHI values coming from "From" into "To" and remember
500 /// them in DeletedPhis
501 void StructurizeCFG::delPhiValues(BasicBlock *From, BasicBlock *To) {
502   PhiMap &Map = DeletedPhis[To];
503   for (BasicBlock::iterator I = To->begin(), E = To->end();
504        I != E && isa<PHINode>(*I);) {
505
506     PHINode &Phi = cast<PHINode>(*I++);
507     while (Phi.getBasicBlockIndex(From) != -1) {
508       Value *Deleted = Phi.removeIncomingValue(From, false);
509       Map[&Phi].push_back(std::make_pair(From, Deleted));
510     }
511   }
512 }
513
514 /// \brief Add a dummy PHI value as soon as we knew the new predecessor
515 void StructurizeCFG::addPhiValues(BasicBlock *From, BasicBlock *To) {
516   for (BasicBlock::iterator I = To->begin(), E = To->end();
517        I != E && isa<PHINode>(*I);) {
518
519     PHINode &Phi = cast<PHINode>(*I++);
520     Value *Undef = UndefValue::get(Phi.getType());
521     Phi.addIncoming(Undef, From);
522   }
523   AddedPhis[To].push_back(From);
524 }
525
526 /// \brief Add the real PHI value as soon as everything is set up
527 void StructurizeCFG::setPhiValues() {
528   SSAUpdater Updater;
529   for (BB2BBVecMap::iterator AI = AddedPhis.begin(), AE = AddedPhis.end();
530        AI != AE; ++AI) {
531
532     BasicBlock *To = AI->first;
533     BBVector &From = AI->second;
534
535     if (!DeletedPhis.count(To))
536       continue;
537
538     PhiMap &Map = DeletedPhis[To];
539     for (PhiMap::iterator PI = Map.begin(), PE = Map.end();
540          PI != PE; ++PI) {
541
542       PHINode *Phi = PI->first;
543       Value *Undef = UndefValue::get(Phi->getType());
544       Updater.Initialize(Phi->getType(), "");
545       Updater.AddAvailableValue(&Func->getEntryBlock(), Undef);
546       Updater.AddAvailableValue(To, Undef);
547
548       NearestCommonDominator Dominator(DT);
549       Dominator.addBlock(To, false);
550       for (BBValueVector::iterator VI = PI->second.begin(),
551            VE = PI->second.end(); VI != VE; ++VI) {
552
553         Updater.AddAvailableValue(VI->first, VI->second);
554         Dominator.addBlock(VI->first);
555       }
556
557       if (!Dominator.wasResultExplicitMentioned())
558         Updater.AddAvailableValue(Dominator.getResult(), Undef);
559
560       for (BBVector::iterator FI = From.begin(), FE = From.end();
561            FI != FE; ++FI) {
562
563         int Idx = Phi->getBasicBlockIndex(*FI);
564         assert(Idx != -1);
565         Phi->setIncomingValue(Idx, Updater.GetValueAtEndOfBlock(*FI));
566       }
567     }
568
569     DeletedPhis.erase(To);
570   }
571   assert(DeletedPhis.empty());
572 }
573
574 /// \brief Remove phi values from all successors and then remove the terminator.
575 void StructurizeCFG::killTerminator(BasicBlock *BB) {
576   TerminatorInst *Term = BB->getTerminator();
577   if (!Term)
578     return;
579
580   for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB);
581        SI != SE; ++SI) {
582
583     delPhiValues(BB, *SI);
584   }
585
586   Term->eraseFromParent();
587 }
588
589 /// \brief Let node exit(s) point to NewExit
590 void StructurizeCFG::changeExit(RegionNode *Node, BasicBlock *NewExit,
591                                 bool IncludeDominator) {
592   if (Node->isSubRegion()) {
593     Region *SubRegion = Node->getNodeAs<Region>();
594     BasicBlock *OldExit = SubRegion->getExit();
595     BasicBlock *Dominator = 0;
596
597     // Find all the edges from the sub region to the exit
598     for (pred_iterator I = pred_begin(OldExit), E = pred_end(OldExit);
599          I != E;) {
600
601       BasicBlock *BB = *I++;
602       if (!SubRegion->contains(BB))
603         continue;
604
605       // Modify the edges to point to the new exit
606       delPhiValues(BB, OldExit);
607       BB->getTerminator()->replaceUsesOfWith(OldExit, NewExit);
608       addPhiValues(BB, NewExit);
609
610       // Find the new dominator (if requested)
611       if (IncludeDominator) {
612         if (!Dominator)
613           Dominator = BB;
614         else
615           Dominator = DT->findNearestCommonDominator(Dominator, BB);
616       }
617     }
618
619     // Change the dominator (if requested)
620     if (Dominator)
621       DT->changeImmediateDominator(NewExit, Dominator);
622
623     // Update the region info
624     SubRegion->replaceExit(NewExit);
625
626   } else {
627     BasicBlock *BB = Node->getNodeAs<BasicBlock>();
628     killTerminator(BB);
629     BranchInst::Create(NewExit, BB);
630     addPhiValues(BB, NewExit);
631     if (IncludeDominator)
632       DT->changeImmediateDominator(NewExit, BB);
633   }
634 }
635
636 /// \brief Create a new flow node and update dominator tree and region info
637 BasicBlock *StructurizeCFG::getNextFlow(BasicBlock *Dominator) {
638   LLVMContext &Context = Func->getContext();
639   BasicBlock *Insert = Order.empty() ? ParentRegion->getExit() :
640                        Order.back()->getEntry();
641   BasicBlock *Flow = BasicBlock::Create(Context, FlowBlockName,
642                                         Func, Insert);
643   DT->addNewBlock(Flow, Dominator);
644   ParentRegion->getRegionInfo()->setRegionFor(Flow, ParentRegion);
645   return Flow;
646 }
647
648 /// \brief Create a new or reuse the previous node as flow node
649 BasicBlock *StructurizeCFG::needPrefix(bool NeedEmpty) {
650   BasicBlock *Entry = PrevNode->getEntry();
651
652   if (!PrevNode->isSubRegion()) {
653     killTerminator(Entry);
654     if (!NeedEmpty || Entry->getFirstInsertionPt() == Entry->end())
655       return Entry;
656
657   }
658
659   // create a new flow node
660   BasicBlock *Flow = getNextFlow(Entry);
661
662   // and wire it up
663   changeExit(PrevNode, Flow, true);
664   PrevNode = ParentRegion->getBBNode(Flow);
665   return Flow;
666 }
667
668 /// \brief Returns the region exit if possible, otherwise just a new flow node
669 BasicBlock *StructurizeCFG::needPostfix(BasicBlock *Flow,
670                                         bool ExitUseAllowed) {
671   if (Order.empty() && ExitUseAllowed) {
672     BasicBlock *Exit = ParentRegion->getExit();
673     DT->changeImmediateDominator(Exit, Flow);
674     addPhiValues(Flow, Exit);
675     return Exit;
676   }
677   return getNextFlow(Flow);
678 }
679
680 /// \brief Set the previous node
681 void StructurizeCFG::setPrevNode(BasicBlock *BB) {
682   PrevNode =  ParentRegion->contains(BB) ? ParentRegion->getBBNode(BB) : 0;
683 }
684
685 /// \brief Does BB dominate all the predicates of Node ?
686 bool StructurizeCFG::dominatesPredicates(BasicBlock *BB, RegionNode *Node) {
687   BBPredicates &Preds = Predicates[Node->getEntry()];
688   for (BBPredicates::iterator PI = Preds.begin(), PE = Preds.end();
689        PI != PE; ++PI) {
690
691     if (!DT->dominates(BB, PI->first))
692       return false;
693   }
694   return true;
695 }
696
697 /// \brief Can we predict that this node will always be called?
698 bool StructurizeCFG::isPredictableTrue(RegionNode *Node) {
699   BBPredicates &Preds = Predicates[Node->getEntry()];
700   bool Dominated = false;
701
702   // Regionentry is always true
703   if (PrevNode == 0)
704     return true;
705
706   for (BBPredicates::iterator I = Preds.begin(), E = Preds.end();
707        I != E; ++I) {
708
709     if (I->second != BoolTrue)
710       return false;
711
712     if (!Dominated && DT->dominates(I->first, PrevNode->getEntry()))
713       Dominated = true;
714   }
715
716   // TODO: The dominator check is too strict
717   return Dominated;
718 }
719
720 /// Take one node from the order vector and wire it up
721 void StructurizeCFG::wireFlow(bool ExitUseAllowed,
722                               BasicBlock *LoopEnd) {
723   RegionNode *Node = Order.pop_back_val();
724   Visited.insert(Node->getEntry());
725
726   if (isPredictableTrue(Node)) {
727     // Just a linear flow
728     if (PrevNode) {
729       changeExit(PrevNode, Node->getEntry(), true);
730     }
731     PrevNode = Node;
732
733   } else {
734     // Insert extra prefix node (or reuse last one)
735     BasicBlock *Flow = needPrefix(false);
736
737     // Insert extra postfix node (or use exit instead)
738     BasicBlock *Entry = Node->getEntry();
739     BasicBlock *Next = needPostfix(Flow, ExitUseAllowed);
740
741     // let it point to entry and next block
742     Conditions.push_back(BranchInst::Create(Entry, Next, BoolUndef, Flow));
743     addPhiValues(Flow, Entry);
744     DT->changeImmediateDominator(Entry, Flow);
745
746     PrevNode = Node;
747     while (!Order.empty() && !Visited.count(LoopEnd) &&
748            dominatesPredicates(Entry, Order.back())) {
749       handleLoops(false, LoopEnd);
750     }
751
752     changeExit(PrevNode, Next, false);
753     setPrevNode(Next);
754   }
755 }
756
757 void StructurizeCFG::handleLoops(bool ExitUseAllowed,
758                                  BasicBlock *LoopEnd) {
759   RegionNode *Node = Order.back();
760   BasicBlock *LoopStart = Node->getEntry();
761
762   if (!Loops.count(LoopStart)) {
763     wireFlow(ExitUseAllowed, LoopEnd);
764     return;
765   }
766
767   if (!isPredictableTrue(Node))
768     LoopStart = needPrefix(true);
769
770   LoopEnd = Loops[Node->getEntry()];
771   wireFlow(false, LoopEnd);
772   while (!Visited.count(LoopEnd)) {
773     handleLoops(false, LoopEnd);
774   }
775
776   // If the start of the loop is the entry block, we can't branch to it so
777   // insert a new dummy entry block.
778   Function *LoopFunc = LoopStart->getParent();
779   if (LoopStart == &LoopFunc->getEntryBlock()) {
780     LoopStart->setName("entry.orig");
781
782     BasicBlock *NewEntry =
783       BasicBlock::Create(LoopStart->getContext(),
784                          "entry",
785                          LoopFunc,
786                          LoopStart);
787     BranchInst::Create(LoopStart, NewEntry);
788   }
789
790   // Create an extra loop end node
791   LoopEnd = needPrefix(false);
792   BasicBlock *Next = needPostfix(LoopEnd, ExitUseAllowed);
793   LoopConds.push_back(BranchInst::Create(Next, LoopStart,
794                                          BoolUndef, LoopEnd));
795   addPhiValues(LoopEnd, LoopStart);
796   setPrevNode(Next);
797 }
798
799 /// After this function control flow looks like it should be, but
800 /// branches and PHI nodes only have undefined conditions.
801 void StructurizeCFG::createFlow() {
802   BasicBlock *Exit = ParentRegion->getExit();
803   bool EntryDominatesExit = DT->dominates(ParentRegion->getEntry(), Exit);
804
805   DeletedPhis.clear();
806   AddedPhis.clear();
807   Conditions.clear();
808   LoopConds.clear();
809
810   PrevNode = 0;
811   Visited.clear();
812
813   while (!Order.empty()) {
814     handleLoops(EntryDominatesExit, 0);
815   }
816
817   if (PrevNode)
818     changeExit(PrevNode, Exit, EntryDominatesExit);
819   else
820     assert(EntryDominatesExit);
821 }
822
823 /// Handle a rare case where the disintegrated nodes instructions
824 /// no longer dominate all their uses. Not sure if this is really nessasary
825 void StructurizeCFG::rebuildSSA() {
826   SSAUpdater Updater;
827   for (const auto &BB : ParentRegion->blocks())
828     for (BasicBlock::iterator II = BB->begin(), IE = BB->end();
829          II != IE; ++II) {
830
831       bool Initialized = false;
832       for (auto I = II->use_begin(), E = II->use_end(); I != E;) {
833         Use &U = *I++;
834         Instruction *User = cast<Instruction>(U.getUser());
835         if (User->getParent() == BB) {
836           continue;
837
838         } else if (PHINode *UserPN = dyn_cast<PHINode>(User)) {
839           if (UserPN->getIncomingBlock(U) == BB)
840             continue;
841         }
842
843         if (DT->dominates(II, User))
844           continue;
845
846         if (!Initialized) {
847           Value *Undef = UndefValue::get(II->getType());
848           Updater.Initialize(II->getType(), "");
849           Updater.AddAvailableValue(&Func->getEntryBlock(), Undef);
850           Updater.AddAvailableValue(BB, II);
851           Initialized = true;
852         }
853         Updater.RewriteUseAfterInsertions(U);
854       }
855     }
856 }
857
858 /// \brief Run the transformation for each region found
859 bool StructurizeCFG::runOnRegion(Region *R, RGPassManager &RGM) {
860   if (R->isTopLevelRegion())
861     return false;
862
863   Func = R->getEntry()->getParent();
864   ParentRegion = R;
865
866   DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
867
868   orderNodes();
869   collectInfos();
870   createFlow();
871   insertConditions(false);
872   insertConditions(true);
873   setPhiValues();
874   rebuildSSA();
875
876   // Cleanup
877   Order.clear();
878   Visited.clear();
879   DeletedPhis.clear();
880   AddedPhis.clear();
881   Predicates.clear();
882   Conditions.clear();
883   Loops.clear();
884   LoopPreds.clear();
885   LoopConds.clear();
886
887   return true;
888 }
889
890 /// \brief Create the pass
891 Pass *llvm::createStructurizeCFGPass() {
892   return new StructurizeCFG();
893 }