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