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