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/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"
21 using namespace llvm::PatternMatch;
23 #define DEBUG_TYPE "structurizecfg"
27 // Definition of the complex types used in this pass.
29 typedef std::pair<BasicBlock *, Value *> BBValuePair;
31 typedef SmallVector<RegionNode*, 8> RNVector;
32 typedef SmallVector<BasicBlock*, 8> BBVector;
33 typedef SmallVector<BranchInst*, 8> BranchVector;
34 typedef SmallVector<BBValuePair, 2> BBValueVector;
36 typedef SmallPtrSet<BasicBlock *, 8> BBSet;
38 typedef MapVector<PHINode *, BBValueVector> PhiMap;
39 typedef MapVector<BasicBlock *, BBVector> BB2BBVecMap;
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;
47 // The name for newly created blocks.
49 static const char *const FlowBlockName = "Flow";
51 /// @brief Find the nearest common dominator for multiple BasicBlocks
53 /// Helper class for StructurizeCFG
54 /// TODO: Maybe move into common code
55 class NearestCommonDominator {
58 DTN2UnsignedMap IndexMap;
62 bool ExplicitMentioned;
65 /// \brief Start a new query
66 NearestCommonDominator(DominatorTree *DomTree) {
71 /// \brief Add BB to the resulting dominator
72 void addBlock(BasicBlock *BB, bool Remember = true) {
73 DomTreeNode *Node = DT->getNode(BB);
76 unsigned Numbering = 0;
77 for (;Node;Node = Node->getIDom())
78 IndexMap[Node] = ++Numbering;
81 ExplicitMentioned = Remember;
85 for (;Node;Node = Node->getIDom())
86 if (IndexMap.count(Node))
91 assert(Node && "Dominator tree invalid!");
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;
103 /// \brief Is "Result" one of the BBs added with "Remember" = True?
104 bool wasResultExplicitMentioned() {
105 return ExplicitMentioned;
108 /// \brief Get the query result
109 BasicBlock *getResult() {
114 /// @brief Transforms the control flow graph on one single entry/exit region
117 /// After the transform all "If"/"Then"/"Else" style control flow looks like
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
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.
142 /// Additionally to that even complicated loops look like this:
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
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 {
161 ConstantInt *BoolTrue;
162 ConstantInt *BoolFalse;
163 UndefValue *BoolUndef;
166 Region *ParentRegion;
173 BBPhiMap DeletedPhis;
174 BB2BBVecMap AddedPhis;
177 BranchVector Conditions;
181 BranchVector LoopConds;
183 RegionNode *PrevNode;
187 void analyzeLoops(RegionNode *N);
189 Value *invert(Value *Condition);
191 Value *buildCondition(BranchInst *Term, unsigned Idx, bool Invert);
193 void gatherPredicates(RegionNode *N);
197 void insertConditions(bool Loops);
199 void delPhiValues(BasicBlock *From, BasicBlock *To);
201 void addPhiValues(BasicBlock *From, BasicBlock *To);
205 void killTerminator(BasicBlock *BB);
207 void changeExit(RegionNode *Node, BasicBlock *NewExit,
208 bool IncludeDominator);
210 BasicBlock *getNextFlow(BasicBlock *Dominator);
212 BasicBlock *needPrefix(bool NeedEmpty);
214 BasicBlock *needPostfix(BasicBlock *Flow, bool ExitUseAllowed);
216 void setPrevNode(BasicBlock *BB);
218 bool dominatesPredicates(BasicBlock *BB, RegionNode *Node);
220 bool isPredictableTrue(RegionNode *Node);
222 void wireFlow(bool ExitUseAllowed, BasicBlock *LoopEnd);
224 void handleLoops(bool ExitUseAllowed, BasicBlock *LoopEnd);
235 initializeStructurizeCFGPass(*PassRegistry::getPassRegistry());
238 using Pass::doInitialization;
239 bool doInitialization(Region *R, RGPassManager &RGM) override;
241 bool runOnRegion(Region *R, RGPassManager &RGM) override;
243 const char *getPassName() const override {
244 return "Structurize control flow";
247 void getAnalysisUsage(AnalysisUsage &AU) const override {
248 AU.addRequiredID(LowerSwitchID);
249 AU.addRequired<DominatorTreeWrapperPass>();
250 AU.addPreserved<DominatorTreeWrapperPass>();
251 RegionPass::getAnalysisUsage(AU);
255 } // end anonymous namespace
257 char StructurizeCFG::ID = 0;
259 INITIALIZE_PASS_BEGIN(StructurizeCFG, "structurizecfg", "Structurize the CFG",
261 INITIALIZE_PASS_DEPENDENCY(LowerSwitch)
262 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
263 INITIALIZE_PASS_DEPENDENCY(RegionInfoPass)
264 INITIALIZE_PASS_END(StructurizeCFG, "structurizecfg", "Structurize the CFG",
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();
271 Boolean = Type::getInt1Ty(Context);
272 BoolTrue = ConstantInt::getTrue(Context);
273 BoolFalse = ConstantInt::getFalse(Context);
274 BoolUndef = UndefValue::get(Boolean);
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 const std::vector<RegionNode *> &Nodes = *I;
284 Order.append(Nodes.begin(), Nodes.end());
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();
297 // Test for sucessors as back edge
298 BasicBlock *BB = N->getNodeAs<BasicBlock>();
299 BranchInst *Term = cast<BranchInst>(BB->getTerminator());
301 for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) {
302 BasicBlock *Succ = Term->getSuccessor(i);
304 if (Visited.count(Succ))
310 /// \brief Invert the given condition
311 Value *StructurizeCFG::invert(Value *Condition) {
312 // First: Check if it's a constant
313 if (Condition == BoolTrue)
316 if (Condition == BoolFalse)
319 if (Condition == BoolUndef)
322 // Second: If the condition is already inverted, return the original value
323 if (match(Condition, m_Not(m_Value(Condition))))
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))))
334 // Last option: Create a new instruction
335 return BinaryOperator::CreateNot(Condition, "", Parent->getTerminator());
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());
345 llvm_unreachable("Unhandled condition to invert");
348 /// \brief Build the condition for one edge
349 Value *StructurizeCFG::buildCondition(BranchInst *Term, unsigned Idx,
351 Value *Cond = Invert ? BoolFalse : BoolTrue;
352 if (Term->isConditional()) {
353 Cond = Term->getCondition();
355 if (Idx != (unsigned)Invert)
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];
368 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
371 // Ignore it if it's a branch from outside into our region entry
372 if (!ParentRegion->contains(*PI))
375 Region *R = RI->getRegionFor(*PI);
376 if (R == ParentRegion) {
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);
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)) {
393 Pred[Other] = BoolFalse;
394 Pred[*PI] = BoolTrue;
398 Pred[*PI] = buildCondition(Term, i, false);
402 LPred[*PI] = buildCondition(Term, i, true);
408 // It's an exit from a sub region
409 while (R->getParent() != ParentRegion)
412 // Edge from inside a subregion to its entry, ignore it
416 BasicBlock *Entry = R->getEntry();
417 if (Visited.count(Entry))
418 Pred[Entry] = BoolTrue;
420 LPred[Entry] = BoolFalse;
425 /// \brief Collect various loop and predicate infos
426 void StructurizeCFG::collectInfos() {
434 // Reset the visited nodes
437 for (RNVector::reverse_iterator OI = Order.rbegin(), OE = Order.rend();
440 // Analyze all the conditions leading to a node
441 gatherPredicates(*OI);
443 // Remember that we've seen this node
444 Visited.insert((*OI)->getEntry());
446 // Find the last back edges
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;
457 for (BranchInst *Term : Conds) {
458 assert(Term->isConditional());
460 BasicBlock *Parent = Term->getParent();
461 BasicBlock *SuccTrue = Term->getSuccessor(0);
462 BasicBlock *SuccFalse = Term->getSuccessor(1);
464 PhiInserter.Initialize(Boolean, "");
465 PhiInserter.AddAvailableValue(&Func->getEntryBlock(), Default);
466 PhiInserter.AddAvailableValue(Loops ? SuccFalse : Parent, Default);
468 BBPredicates &Preds = Loops ? LoopPreds[SuccFalse] : Predicates[SuccTrue];
470 NearestCommonDominator Dominator(DT);
471 Dominator.addBlock(Parent, false);
473 Value *ParentValue = nullptr;
474 for (BBPredicates::iterator PI = Preds.begin(), PE = Preds.end();
477 if (PI->first == Parent) {
478 ParentValue = PI->second;
481 PhiInserter.AddAvailableValue(PI->first, PI->second);
482 Dominator.addBlock(PI->first);
486 Term->setCondition(ParentValue);
488 if (!Dominator.wasResultExplicitMentioned())
489 PhiInserter.AddAvailableValue(Dominator.getResult(), Default);
491 Term->setCondition(PhiInserter.GetValueInMiddleOfBlock(Parent));
496 /// \brief Remove all PHI values coming from "From" into "To" and remember
497 /// them in DeletedPhis
498 void StructurizeCFG::delPhiValues(BasicBlock *From, BasicBlock *To) {
499 PhiMap &Map = DeletedPhis[To];
500 for (BasicBlock::iterator I = To->begin(), E = To->end();
501 I != E && isa<PHINode>(*I);) {
503 PHINode &Phi = cast<PHINode>(*I++);
504 while (Phi.getBasicBlockIndex(From) != -1) {
505 Value *Deleted = Phi.removeIncomingValue(From, false);
506 Map[&Phi].push_back(std::make_pair(From, Deleted));
511 /// \brief Add a dummy PHI value as soon as we knew the new predecessor
512 void StructurizeCFG::addPhiValues(BasicBlock *From, BasicBlock *To) {
513 for (BasicBlock::iterator I = To->begin(), E = To->end();
514 I != E && isa<PHINode>(*I);) {
516 PHINode &Phi = cast<PHINode>(*I++);
517 Value *Undef = UndefValue::get(Phi.getType());
518 Phi.addIncoming(Undef, From);
520 AddedPhis[To].push_back(From);
523 /// \brief Add the real PHI value as soon as everything is set up
524 void StructurizeCFG::setPhiValues() {
526 for (BB2BBVecMap::iterator AI = AddedPhis.begin(), AE = AddedPhis.end();
529 BasicBlock *To = AI->first;
530 BBVector &From = AI->second;
532 if (!DeletedPhis.count(To))
535 PhiMap &Map = DeletedPhis[To];
536 for (PhiMap::iterator PI = Map.begin(), PE = Map.end();
539 PHINode *Phi = PI->first;
540 Value *Undef = UndefValue::get(Phi->getType());
541 Updater.Initialize(Phi->getType(), "");
542 Updater.AddAvailableValue(&Func->getEntryBlock(), Undef);
543 Updater.AddAvailableValue(To, Undef);
545 NearestCommonDominator Dominator(DT);
546 Dominator.addBlock(To, false);
547 for (BBValueVector::iterator VI = PI->second.begin(),
548 VE = PI->second.end(); VI != VE; ++VI) {
550 Updater.AddAvailableValue(VI->first, VI->second);
551 Dominator.addBlock(VI->first);
554 if (!Dominator.wasResultExplicitMentioned())
555 Updater.AddAvailableValue(Dominator.getResult(), Undef);
557 for (BBVector::iterator FI = From.begin(), FE = From.end();
560 int Idx = Phi->getBasicBlockIndex(*FI);
562 Phi->setIncomingValue(Idx, Updater.GetValueAtEndOfBlock(*FI));
566 DeletedPhis.erase(To);
568 assert(DeletedPhis.empty());
571 /// \brief Remove phi values from all successors and then remove the terminator.
572 void StructurizeCFG::killTerminator(BasicBlock *BB) {
573 TerminatorInst *Term = BB->getTerminator();
577 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB);
580 delPhiValues(BB, *SI);
583 Term->eraseFromParent();
586 /// \brief Let node exit(s) point to NewExit
587 void StructurizeCFG::changeExit(RegionNode *Node, BasicBlock *NewExit,
588 bool IncludeDominator) {
589 if (Node->isSubRegion()) {
590 Region *SubRegion = Node->getNodeAs<Region>();
591 BasicBlock *OldExit = SubRegion->getExit();
592 BasicBlock *Dominator = nullptr;
594 // Find all the edges from the sub region to the exit
595 for (pred_iterator I = pred_begin(OldExit), E = pred_end(OldExit);
598 BasicBlock *BB = *I++;
599 if (!SubRegion->contains(BB))
602 // Modify the edges to point to the new exit
603 delPhiValues(BB, OldExit);
604 BB->getTerminator()->replaceUsesOfWith(OldExit, NewExit);
605 addPhiValues(BB, NewExit);
607 // Find the new dominator (if requested)
608 if (IncludeDominator) {
612 Dominator = DT->findNearestCommonDominator(Dominator, BB);
616 // Change the dominator (if requested)
618 DT->changeImmediateDominator(NewExit, Dominator);
620 // Update the region info
621 SubRegion->replaceExit(NewExit);
624 BasicBlock *BB = Node->getNodeAs<BasicBlock>();
626 BranchInst::Create(NewExit, BB);
627 addPhiValues(BB, NewExit);
628 if (IncludeDominator)
629 DT->changeImmediateDominator(NewExit, BB);
633 /// \brief Create a new flow node and update dominator tree and region info
634 BasicBlock *StructurizeCFG::getNextFlow(BasicBlock *Dominator) {
635 LLVMContext &Context = Func->getContext();
636 BasicBlock *Insert = Order.empty() ? ParentRegion->getExit() :
637 Order.back()->getEntry();
638 BasicBlock *Flow = BasicBlock::Create(Context, FlowBlockName,
640 DT->addNewBlock(Flow, Dominator);
641 ParentRegion->getRegionInfo()->setRegionFor(Flow, ParentRegion);
645 /// \brief Create a new or reuse the previous node as flow node
646 BasicBlock *StructurizeCFG::needPrefix(bool NeedEmpty) {
647 BasicBlock *Entry = PrevNode->getEntry();
649 if (!PrevNode->isSubRegion()) {
650 killTerminator(Entry);
651 if (!NeedEmpty || Entry->getFirstInsertionPt() == Entry->end())
656 // create a new flow node
657 BasicBlock *Flow = getNextFlow(Entry);
660 changeExit(PrevNode, Flow, true);
661 PrevNode = ParentRegion->getBBNode(Flow);
665 /// \brief Returns the region exit if possible, otherwise just a new flow node
666 BasicBlock *StructurizeCFG::needPostfix(BasicBlock *Flow,
667 bool ExitUseAllowed) {
668 if (Order.empty() && ExitUseAllowed) {
669 BasicBlock *Exit = ParentRegion->getExit();
670 DT->changeImmediateDominator(Exit, Flow);
671 addPhiValues(Flow, Exit);
674 return getNextFlow(Flow);
677 /// \brief Set the previous node
678 void StructurizeCFG::setPrevNode(BasicBlock *BB) {
679 PrevNode = ParentRegion->contains(BB) ? ParentRegion->getBBNode(BB)
683 /// \brief Does BB dominate all the predicates of Node ?
684 bool StructurizeCFG::dominatesPredicates(BasicBlock *BB, RegionNode *Node) {
685 BBPredicates &Preds = Predicates[Node->getEntry()];
686 for (BBPredicates::iterator PI = Preds.begin(), PE = Preds.end();
689 if (!DT->dominates(BB, PI->first))
695 /// \brief Can we predict that this node will always be called?
696 bool StructurizeCFG::isPredictableTrue(RegionNode *Node) {
697 BBPredicates &Preds = Predicates[Node->getEntry()];
698 bool Dominated = false;
700 // Regionentry is always true
704 for (BBPredicates::iterator I = Preds.begin(), E = Preds.end();
707 if (I->second != BoolTrue)
710 if (!Dominated && DT->dominates(I->first, PrevNode->getEntry()))
714 // TODO: The dominator check is too strict
718 /// Take one node from the order vector and wire it up
719 void StructurizeCFG::wireFlow(bool ExitUseAllowed,
720 BasicBlock *LoopEnd) {
721 RegionNode *Node = Order.pop_back_val();
722 Visited.insert(Node->getEntry());
724 if (isPredictableTrue(Node)) {
725 // Just a linear flow
727 changeExit(PrevNode, Node->getEntry(), true);
732 // Insert extra prefix node (or reuse last one)
733 BasicBlock *Flow = needPrefix(false);
735 // Insert extra postfix node (or use exit instead)
736 BasicBlock *Entry = Node->getEntry();
737 BasicBlock *Next = needPostfix(Flow, ExitUseAllowed);
739 // let it point to entry and next block
740 Conditions.push_back(BranchInst::Create(Entry, Next, BoolUndef, Flow));
741 addPhiValues(Flow, Entry);
742 DT->changeImmediateDominator(Entry, Flow);
745 while (!Order.empty() && !Visited.count(LoopEnd) &&
746 dominatesPredicates(Entry, Order.back())) {
747 handleLoops(false, LoopEnd);
750 changeExit(PrevNode, Next, false);
755 void StructurizeCFG::handleLoops(bool ExitUseAllowed,
756 BasicBlock *LoopEnd) {
757 RegionNode *Node = Order.back();
758 BasicBlock *LoopStart = Node->getEntry();
760 if (!Loops.count(LoopStart)) {
761 wireFlow(ExitUseAllowed, LoopEnd);
765 if (!isPredictableTrue(Node))
766 LoopStart = needPrefix(true);
768 LoopEnd = Loops[Node->getEntry()];
769 wireFlow(false, LoopEnd);
770 while (!Visited.count(LoopEnd)) {
771 handleLoops(false, LoopEnd);
774 // If the start of the loop is the entry block, we can't branch to it so
775 // insert a new dummy entry block.
776 Function *LoopFunc = LoopStart->getParent();
777 if (LoopStart == &LoopFunc->getEntryBlock()) {
778 LoopStart->setName("entry.orig");
780 BasicBlock *NewEntry =
781 BasicBlock::Create(LoopStart->getContext(),
785 BranchInst::Create(LoopStart, NewEntry);
788 // Create an extra loop end node
789 LoopEnd = needPrefix(false);
790 BasicBlock *Next = needPostfix(LoopEnd, ExitUseAllowed);
791 LoopConds.push_back(BranchInst::Create(Next, LoopStart,
792 BoolUndef, LoopEnd));
793 addPhiValues(LoopEnd, LoopStart);
797 /// After this function control flow looks like it should be, but
798 /// branches and PHI nodes only have undefined conditions.
799 void StructurizeCFG::createFlow() {
800 BasicBlock *Exit = ParentRegion->getExit();
801 bool EntryDominatesExit = DT->dominates(ParentRegion->getEntry(), Exit);
811 while (!Order.empty()) {
812 handleLoops(EntryDominatesExit, nullptr);
816 changeExit(PrevNode, Exit, EntryDominatesExit);
818 assert(EntryDominatesExit);
821 /// Handle a rare case where the disintegrated nodes instructions
822 /// no longer dominate all their uses. Not sure if this is really nessasary
823 void StructurizeCFG::rebuildSSA() {
825 for (const auto &BB : ParentRegion->blocks())
826 for (BasicBlock::iterator II = BB->begin(), IE = BB->end();
829 bool Initialized = false;
830 for (auto I = II->use_begin(), E = II->use_end(); I != E;) {
832 Instruction *User = cast<Instruction>(U.getUser());
833 if (User->getParent() == BB) {
836 } else if (PHINode *UserPN = dyn_cast<PHINode>(User)) {
837 if (UserPN->getIncomingBlock(U) == BB)
841 if (DT->dominates(II, User))
845 Value *Undef = UndefValue::get(II->getType());
846 Updater.Initialize(II->getType(), "");
847 Updater.AddAvailableValue(&Func->getEntryBlock(), Undef);
848 Updater.AddAvailableValue(BB, II);
851 Updater.RewriteUseAfterInsertions(U);
856 /// \brief Run the transformation for each region found
857 bool StructurizeCFG::runOnRegion(Region *R, RGPassManager &RGM) {
858 if (R->isTopLevelRegion())
861 Func = R->getEntry()->getParent();
864 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
869 insertConditions(false);
870 insertConditions(true);
888 /// \brief Create the pass
889 Pass *llvm::createStructurizeCFGPass() {
890 return new StructurizeCFG();