1 //===- StructurizeCFG.cpp -------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Transforms/Scalar/StructurizeCFG.h"
10 #include "llvm/ADT/DenseMap.h"
11 #include "llvm/ADT/MapVector.h"
12 #include "llvm/ADT/SCCIterator.h"
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/ADT/SmallPtrSet.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/Analysis/InstructionSimplify.h"
17 #include "llvm/Analysis/LegacyDivergenceAnalysis.h"
18 #include "llvm/Analysis/RegionInfo.h"
19 #include "llvm/Analysis/RegionIterator.h"
20 #include "llvm/Analysis/RegionPass.h"
21 #include "llvm/IR/Argument.h"
22 #include "llvm/IR/BasicBlock.h"
23 #include "llvm/IR/CFG.h"
24 #include "llvm/IR/Constant.h"
25 #include "llvm/IR/Constants.h"
26 #include "llvm/IR/Dominators.h"
27 #include "llvm/IR/Function.h"
28 #include "llvm/IR/InstrTypes.h"
29 #include "llvm/IR/Instruction.h"
30 #include "llvm/IR/Instructions.h"
31 #include "llvm/IR/Metadata.h"
32 #include "llvm/IR/PassManager.h"
33 #include "llvm/IR/PatternMatch.h"
34 #include "llvm/IR/Type.h"
35 #include "llvm/IR/Use.h"
36 #include "llvm/IR/User.h"
37 #include "llvm/IR/Value.h"
38 #include "llvm/IR/ValueHandle.h"
39 #include "llvm/InitializePasses.h"
40 #include "llvm/Pass.h"
41 #include "llvm/Support/Casting.h"
42 #include "llvm/Support/CommandLine.h"
43 #include "llvm/Support/Debug.h"
44 #include "llvm/Support/ErrorHandling.h"
45 #include "llvm/Support/raw_ostream.h"
46 #include "llvm/Transforms/Scalar.h"
47 #include "llvm/Transforms/Utils.h"
48 #include "llvm/Transforms/Utils/Local.h"
49 #include "llvm/Transforms/Utils/SSAUpdater.h"
55 using namespace llvm::PatternMatch
;
57 #define DEBUG_TYPE "structurizecfg"
59 // The name for newly created blocks.
60 const char FlowBlockName
[] = "Flow";
64 static cl::opt
<bool> ForceSkipUniformRegions(
65 "structurizecfg-skip-uniform-regions",
67 cl::desc("Force whether the StructurizeCFG pass skips uniform regions"),
71 RelaxedUniformRegions("structurizecfg-relaxed-uniform-regions", cl::Hidden
,
72 cl::desc("Allow relaxed uniform region checks"),
75 // Definition of the complex types used in this pass.
77 using BBValuePair
= std::pair
<BasicBlock
*, Value
*>;
79 using RNVector
= SmallVector
<RegionNode
*, 8>;
80 using BBVector
= SmallVector
<BasicBlock
*, 8>;
81 using BranchVector
= SmallVector
<BranchInst
*, 8>;
82 using BBValueVector
= SmallVector
<BBValuePair
, 2>;
84 using BBSet
= SmallPtrSet
<BasicBlock
*, 8>;
86 using PhiMap
= MapVector
<PHINode
*, BBValueVector
>;
87 using BB2BBVecMap
= MapVector
<BasicBlock
*, BBVector
>;
89 using BBPhiMap
= DenseMap
<BasicBlock
*, PhiMap
>;
90 using BBPredicates
= DenseMap
<BasicBlock
*, Value
*>;
91 using PredMap
= DenseMap
<BasicBlock
*, BBPredicates
>;
92 using BB2BBMap
= DenseMap
<BasicBlock
*, BasicBlock
*>;
94 // A traits type that is intended to be used in graph algorithms. The graph
95 // traits starts at an entry node, and traverses the RegionNodes that are in
97 struct SubGraphTraits
{
98 using NodeRef
= std::pair
<RegionNode
*, SmallDenseSet
<RegionNode
*> *>;
99 using BaseSuccIterator
= GraphTraits
<RegionNode
*>::ChildIteratorType
;
101 // This wraps a set of Nodes into the iterator, so we know which edges to
103 class WrappedSuccIterator
104 : public iterator_adaptor_base
<
105 WrappedSuccIterator
, BaseSuccIterator
,
106 typename
std::iterator_traits
<BaseSuccIterator
>::iterator_category
,
107 NodeRef
, std::ptrdiff_t, NodeRef
*, NodeRef
> {
108 SmallDenseSet
<RegionNode
*> *Nodes
;
111 WrappedSuccIterator(BaseSuccIterator It
, SmallDenseSet
<RegionNode
*> *Nodes
)
112 : iterator_adaptor_base(It
), Nodes(Nodes
) {}
114 NodeRef
operator*() const { return {*I
, Nodes
}; }
117 static bool filterAll(const NodeRef
&N
) { return true; }
118 static bool filterSet(const NodeRef
&N
) { return N
.second
->count(N
.first
); }
120 using ChildIteratorType
=
121 filter_iterator
<WrappedSuccIterator
, bool (*)(const NodeRef
&)>;
123 static NodeRef
getEntryNode(Region
*R
) {
124 return {GraphTraits
<Region
*>::getEntryNode(R
), nullptr};
127 static NodeRef
getEntryNode(NodeRef N
) { return N
; }
129 static iterator_range
<ChildIteratorType
> children(const NodeRef
&N
) {
130 auto *filter
= N
.second
? &filterSet
: &filterAll
;
131 return make_filter_range(
132 make_range
<WrappedSuccIterator
>(
133 {GraphTraits
<RegionNode
*>::child_begin(N
.first
), N
.second
},
134 {GraphTraits
<RegionNode
*>::child_end(N
.first
), N
.second
}),
138 static ChildIteratorType
child_begin(const NodeRef
&N
) {
139 return children(N
).begin();
142 static ChildIteratorType
child_end(const NodeRef
&N
) {
143 return children(N
).end();
147 /// Finds the nearest common dominator of a set of BasicBlocks.
149 /// For every BB you add to the set, you can specify whether we "remember" the
150 /// block. When you get the common dominator, you can also ask whether it's one
151 /// of the blocks we remembered.
152 class NearestCommonDominator
{
154 BasicBlock
*Result
= nullptr;
155 bool ResultIsRemembered
= false;
157 /// Add BB to the resulting dominator.
158 void addBlock(BasicBlock
*BB
, bool Remember
) {
161 ResultIsRemembered
= Remember
;
165 BasicBlock
*NewResult
= DT
->findNearestCommonDominator(Result
, BB
);
166 if (NewResult
!= Result
)
167 ResultIsRemembered
= false;
169 ResultIsRemembered
|= Remember
;
174 explicit NearestCommonDominator(DominatorTree
*DomTree
) : DT(DomTree
) {}
176 void addBlock(BasicBlock
*BB
) {
177 addBlock(BB
, /* Remember = */ false);
180 void addAndRememberBlock(BasicBlock
*BB
) {
181 addBlock(BB
, /* Remember = */ true);
184 /// Get the nearest common dominator of all the BBs added via addBlock() and
185 /// addAndRememberBlock().
186 BasicBlock
*result() { return Result
; }
188 /// Is the BB returned by getResult() one of the blocks we added to the set
189 /// with addAndRememberBlock()?
190 bool resultIsRememberedBlock() { return ResultIsRemembered
; }
193 /// Transforms the control flow graph on one single entry/exit region
196 /// After the transform all "If"/"Then"/"Else" style control flow looks like
208 /// | | 1 = "If" block, calculates the condition
209 /// 4 | 2 = "Then" subregion, runs if the condition is true
210 /// | / 3 = "Flow" blocks, newly inserted flow blocks, rejoins the flow
211 /// |/ 4 = "Else" optional subregion, runs if the condition is false
212 /// 5 5 = "End" block, also rejoins the control flow
215 /// Control flow is expressed as a branch where the true exit goes into the
216 /// "Then"/"Else" region, while the false exit skips the region
217 /// The condition for the optional "Else" region is expressed as a PHI node.
218 /// The incoming values of the PHI node are true for the "If" edge and false
219 /// for the "Then" edge.
221 /// Additionally to that even complicated loops look like this:
228 /// | / 1 = "Entry" block
229 /// |/ 2 = "Loop" optional subregion, with all exits at "Flow" block
230 /// 3 3 = "Flow" block, with back edge to entry block
234 /// The back edge of the "Flow" block is always on the false side of the branch
235 /// while the true side continues the general flow. So the loop condition
236 /// consist of a network of PHI nodes where the true incoming values expresses
237 /// breaks and the false values expresses continue states.
239 class StructurizeCFG
{
241 ConstantInt
*BoolTrue
;
242 ConstantInt
*BoolFalse
;
243 UndefValue
*BoolUndef
;
246 Region
*ParentRegion
;
248 LegacyDivergenceAnalysis
*DA
= nullptr;
251 SmallVector
<RegionNode
*, 8> Order
;
254 SmallVector
<WeakVH
, 8> AffectedPhis
;
255 BBPhiMap DeletedPhis
;
256 BB2BBVecMap AddedPhis
;
259 BranchVector Conditions
;
263 BranchVector LoopConds
;
265 RegionNode
*PrevNode
;
269 void analyzeLoops(RegionNode
*N
);
271 Value
*buildCondition(BranchInst
*Term
, unsigned Idx
, bool Invert
);
273 void gatherPredicates(RegionNode
*N
);
277 void insertConditions(bool Loops
);
279 void delPhiValues(BasicBlock
*From
, BasicBlock
*To
);
281 void addPhiValues(BasicBlock
*From
, BasicBlock
*To
);
285 void simplifyAffectedPhis();
287 void killTerminator(BasicBlock
*BB
);
289 void changeExit(RegionNode
*Node
, BasicBlock
*NewExit
,
290 bool IncludeDominator
);
292 BasicBlock
*getNextFlow(BasicBlock
*Dominator
);
294 BasicBlock
*needPrefix(bool NeedEmpty
);
296 BasicBlock
*needPostfix(BasicBlock
*Flow
, bool ExitUseAllowed
);
298 void setPrevNode(BasicBlock
*BB
);
300 bool dominatesPredicates(BasicBlock
*BB
, RegionNode
*Node
);
302 bool isPredictableTrue(RegionNode
*Node
);
304 void wireFlow(bool ExitUseAllowed
, BasicBlock
*LoopEnd
);
306 void handleLoops(bool ExitUseAllowed
, BasicBlock
*LoopEnd
);
313 void init(Region
*R
);
314 bool run(Region
*R
, DominatorTree
*DT
);
315 bool makeUniformRegion(Region
*R
, LegacyDivergenceAnalysis
*DA
);
318 class StructurizeCFGLegacyPass
: public RegionPass
{
319 bool SkipUniformRegions
;
324 explicit StructurizeCFGLegacyPass(bool SkipUniformRegions_
= false)
325 : RegionPass(ID
), SkipUniformRegions(SkipUniformRegions_
) {
326 if (ForceSkipUniformRegions
.getNumOccurrences())
327 SkipUniformRegions
= ForceSkipUniformRegions
.getValue();
328 initializeStructurizeCFGLegacyPassPass(*PassRegistry::getPassRegistry());
331 bool runOnRegion(Region
*R
, RGPassManager
&RGM
) override
{
334 if (SkipUniformRegions
) {
335 LegacyDivergenceAnalysis
*DA
= &getAnalysis
<LegacyDivergenceAnalysis
>();
336 if (SCFG
.makeUniformRegion(R
, DA
))
339 DominatorTree
*DT
= &getAnalysis
<DominatorTreeWrapperPass
>().getDomTree();
340 return SCFG
.run(R
, DT
);
343 StringRef
getPassName() const override
{ return "Structurize control flow"; }
345 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
346 if (SkipUniformRegions
)
347 AU
.addRequired
<LegacyDivergenceAnalysis
>();
348 AU
.addRequiredID(LowerSwitchID
);
349 AU
.addRequired
<DominatorTreeWrapperPass
>();
351 AU
.addPreserved
<DominatorTreeWrapperPass
>();
352 RegionPass::getAnalysisUsage(AU
);
356 } // end anonymous namespace
358 char StructurizeCFGLegacyPass::ID
= 0;
360 INITIALIZE_PASS_BEGIN(StructurizeCFGLegacyPass
, "structurizecfg",
361 "Structurize the CFG", false, false)
362 INITIALIZE_PASS_DEPENDENCY(LegacyDivergenceAnalysis
)
363 INITIALIZE_PASS_DEPENDENCY(LowerSwitchLegacyPass
)
364 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass
)
365 INITIALIZE_PASS_DEPENDENCY(RegionInfoPass
)
366 INITIALIZE_PASS_END(StructurizeCFGLegacyPass
, "structurizecfg",
367 "Structurize the CFG", false, false)
369 /// Build up the general order of nodes, by performing a topological sort of the
370 /// parent region's nodes, while ensuring that there is no outer cycle node
371 /// between any two inner cycle nodes.
372 void StructurizeCFG::orderNodes() {
373 Order
.resize(std::distance(GraphTraits
<Region
*>::nodes_begin(ParentRegion
),
374 GraphTraits
<Region
*>::nodes_end(ParentRegion
)));
378 SmallDenseSet
<RegionNode
*> Nodes
;
379 auto EntryNode
= SubGraphTraits::getEntryNode(ParentRegion
);
381 // A list of range indices of SCCs in Order, to be processed.
382 SmallVector
<std::pair
<unsigned, unsigned>, 8> WorkList
;
383 unsigned I
= 0, E
= Order
.size();
385 // Run through all the SCCs in the subgraph starting with Entry.
387 scc_iterator
<SubGraphTraits::NodeRef
, SubGraphTraits
>::begin(
389 !SCCI
.isAtEnd(); ++SCCI
) {
392 // An SCC up to the size of 2, can be reduced to an entry (the last node),
393 // and a possible additional node. Therefore, it is already in order, and
394 // there is no need to add it to the work-list.
395 unsigned Size
= SCC
.size();
397 WorkList
.emplace_back(I
, I
+ Size
);
399 // Add the SCC nodes to the Order array.
400 for (auto &N
: SCC
) {
401 assert(I
< E
&& "SCC size mismatch!");
402 Order
[I
++] = N
.first
;
405 assert(I
== E
&& "SCC size mismatch!");
407 // If there are no more SCCs to order, then we are done.
408 if (WorkList
.empty())
411 std::tie(I
, E
) = WorkList
.pop_back_val();
413 // Collect the set of nodes in the SCC's subgraph. These are only the
414 // possible child nodes; we do not add the entry (last node) otherwise we
415 // will have the same exact SCC all over again.
417 Nodes
.insert(Order
.begin() + I
, Order
.begin() + E
- 1);
419 // Update the entry node.
420 EntryNode
.first
= Order
[E
- 1];
421 EntryNode
.second
= &Nodes
;
425 /// Determine the end of the loops
426 void StructurizeCFG::analyzeLoops(RegionNode
*N
) {
427 if (N
->isSubRegion()) {
428 // Test for exit as back edge
429 BasicBlock
*Exit
= N
->getNodeAs
<Region
>()->getExit();
430 if (Visited
.count(Exit
))
431 Loops
[Exit
] = N
->getEntry();
434 // Test for successors as back edge
435 BasicBlock
*BB
= N
->getNodeAs
<BasicBlock
>();
436 BranchInst
*Term
= cast
<BranchInst
>(BB
->getTerminator());
438 for (BasicBlock
*Succ
: Term
->successors())
439 if (Visited
.count(Succ
))
444 /// Build the condition for one edge
445 Value
*StructurizeCFG::buildCondition(BranchInst
*Term
, unsigned Idx
,
447 Value
*Cond
= Invert
? BoolFalse
: BoolTrue
;
448 if (Term
->isConditional()) {
449 Cond
= Term
->getCondition();
451 if (Idx
!= (unsigned)Invert
)
452 Cond
= invertCondition(Cond
);
457 /// Analyze the predecessors of each block and build up predicates
458 void StructurizeCFG::gatherPredicates(RegionNode
*N
) {
459 RegionInfo
*RI
= ParentRegion
->getRegionInfo();
460 BasicBlock
*BB
= N
->getEntry();
461 BBPredicates
&Pred
= Predicates
[BB
];
462 BBPredicates
&LPred
= LoopPreds
[BB
];
464 for (BasicBlock
*P
: predecessors(BB
)) {
465 // Ignore it if it's a branch from outside into our region entry
466 if (!ParentRegion
->contains(P
))
469 Region
*R
= RI
->getRegionFor(P
);
470 if (R
== ParentRegion
) {
471 // It's a top level block in our region
472 BranchInst
*Term
= cast
<BranchInst
>(P
->getTerminator());
473 for (unsigned i
= 0, e
= Term
->getNumSuccessors(); i
!= e
; ++i
) {
474 BasicBlock
*Succ
= Term
->getSuccessor(i
);
478 if (Visited
.count(P
)) {
479 // Normal forward edge
480 if (Term
->isConditional()) {
481 // Try to treat it like an ELSE block
482 BasicBlock
*Other
= Term
->getSuccessor(!i
);
483 if (Visited
.count(Other
) && !Loops
.count(Other
) &&
484 !Pred
.count(Other
) && !Pred
.count(P
)) {
486 Pred
[Other
] = BoolFalse
;
491 Pred
[P
] = buildCondition(Term
, i
, false);
494 LPred
[P
] = buildCondition(Term
, i
, true);
498 // It's an exit from a sub region
499 while (R
->getParent() != ParentRegion
)
502 // Edge from inside a subregion to its entry, ignore it
506 BasicBlock
*Entry
= R
->getEntry();
507 if (Visited
.count(Entry
))
508 Pred
[Entry
] = BoolTrue
;
510 LPred
[Entry
] = BoolFalse
;
515 /// Collect various loop and predicate infos
516 void StructurizeCFG::collectInfos() {
524 // Reset the visited nodes
527 for (RegionNode
*RN
: reverse(Order
)) {
528 LLVM_DEBUG(dbgs() << "Visiting: "
529 << (RN
->isSubRegion() ? "SubRegion with entry: " : "")
530 << RN
->getEntry()->getName() << "\n");
532 // Analyze all the conditions leading to a node
533 gatherPredicates(RN
);
535 // Remember that we've seen this node
536 Visited
.insert(RN
->getEntry());
538 // Find the last back edges
543 /// Insert the missing branch conditions
544 void StructurizeCFG::insertConditions(bool Loops
) {
545 BranchVector
&Conds
= Loops
? LoopConds
: Conditions
;
546 Value
*Default
= Loops
? BoolTrue
: BoolFalse
;
547 SSAUpdater PhiInserter
;
549 for (BranchInst
*Term
: Conds
) {
550 assert(Term
->isConditional());
552 BasicBlock
*Parent
= Term
->getParent();
553 BasicBlock
*SuccTrue
= Term
->getSuccessor(0);
554 BasicBlock
*SuccFalse
= Term
->getSuccessor(1);
556 PhiInserter
.Initialize(Boolean
, "");
557 PhiInserter
.AddAvailableValue(&Func
->getEntryBlock(), Default
);
558 PhiInserter
.AddAvailableValue(Loops
? SuccFalse
: Parent
, Default
);
560 BBPredicates
&Preds
= Loops
? LoopPreds
[SuccFalse
] : Predicates
[SuccTrue
];
562 NearestCommonDominator
Dominator(DT
);
563 Dominator
.addBlock(Parent
);
565 Value
*ParentValue
= nullptr;
566 for (std::pair
<BasicBlock
*, Value
*> BBAndPred
: Preds
) {
567 BasicBlock
*BB
= BBAndPred
.first
;
568 Value
*Pred
= BBAndPred
.second
;
574 PhiInserter
.AddAvailableValue(BB
, Pred
);
575 Dominator
.addAndRememberBlock(BB
);
579 Term
->setCondition(ParentValue
);
581 if (!Dominator
.resultIsRememberedBlock())
582 PhiInserter
.AddAvailableValue(Dominator
.result(), Default
);
584 Term
->setCondition(PhiInserter
.GetValueInMiddleOfBlock(Parent
));
589 /// Remove all PHI values coming from "From" into "To" and remember
590 /// them in DeletedPhis
591 void StructurizeCFG::delPhiValues(BasicBlock
*From
, BasicBlock
*To
) {
592 PhiMap
&Map
= DeletedPhis
[To
];
593 for (PHINode
&Phi
: To
->phis()) {
594 bool Recorded
= false;
595 while (Phi
.getBasicBlockIndex(From
) != -1) {
596 Value
*Deleted
= Phi
.removeIncomingValue(From
, false);
597 Map
[&Phi
].push_back(std::make_pair(From
, Deleted
));
599 AffectedPhis
.push_back(&Phi
);
606 /// Add a dummy PHI value as soon as we knew the new predecessor
607 void StructurizeCFG::addPhiValues(BasicBlock
*From
, BasicBlock
*To
) {
608 for (PHINode
&Phi
: To
->phis()) {
609 Value
*Undef
= UndefValue::get(Phi
.getType());
610 Phi
.addIncoming(Undef
, From
);
612 AddedPhis
[To
].push_back(From
);
615 /// Add the real PHI value as soon as everything is set up
616 void StructurizeCFG::setPhiValues() {
617 SmallVector
<PHINode
*, 8> InsertedPhis
;
618 SSAUpdater
Updater(&InsertedPhis
);
619 for (const auto &AddedPhi
: AddedPhis
) {
620 BasicBlock
*To
= AddedPhi
.first
;
621 const BBVector
&From
= AddedPhi
.second
;
623 if (!DeletedPhis
.count(To
))
626 PhiMap
&Map
= DeletedPhis
[To
];
627 for (const auto &PI
: Map
) {
628 PHINode
*Phi
= PI
.first
;
629 Value
*Undef
= UndefValue::get(Phi
->getType());
630 Updater
.Initialize(Phi
->getType(), "");
631 Updater
.AddAvailableValue(&Func
->getEntryBlock(), Undef
);
632 Updater
.AddAvailableValue(To
, Undef
);
634 NearestCommonDominator
Dominator(DT
);
635 Dominator
.addBlock(To
);
636 for (const auto &VI
: PI
.second
) {
637 Updater
.AddAvailableValue(VI
.first
, VI
.second
);
638 Dominator
.addAndRememberBlock(VI
.first
);
641 if (!Dominator
.resultIsRememberedBlock())
642 Updater
.AddAvailableValue(Dominator
.result(), Undef
);
644 for (BasicBlock
*FI
: From
)
645 Phi
->setIncomingValueForBlock(FI
, Updater
.GetValueAtEndOfBlock(FI
));
646 AffectedPhis
.push_back(Phi
);
649 DeletedPhis
.erase(To
);
651 assert(DeletedPhis
.empty());
653 AffectedPhis
.append(InsertedPhis
.begin(), InsertedPhis
.end());
656 void StructurizeCFG::simplifyAffectedPhis() {
660 SimplifyQuery
Q(Func
->getParent()->getDataLayout());
662 for (WeakVH VH
: AffectedPhis
) {
663 if (auto Phi
= dyn_cast_or_null
<PHINode
>(VH
)) {
664 if (auto NewValue
= SimplifyInstruction(Phi
, Q
)) {
665 Phi
->replaceAllUsesWith(NewValue
);
666 Phi
->eraseFromParent();
674 /// Remove phi values from all successors and then remove the terminator.
675 void StructurizeCFG::killTerminator(BasicBlock
*BB
) {
676 Instruction
*Term
= BB
->getTerminator();
680 for (BasicBlock
*Succ
: successors(BB
))
681 delPhiValues(BB
, Succ
);
684 DA
->removeValue(Term
);
685 Term
->eraseFromParent();
688 /// Let node exit(s) point to NewExit
689 void StructurizeCFG::changeExit(RegionNode
*Node
, BasicBlock
*NewExit
,
690 bool IncludeDominator
) {
691 if (Node
->isSubRegion()) {
692 Region
*SubRegion
= Node
->getNodeAs
<Region
>();
693 BasicBlock
*OldExit
= SubRegion
->getExit();
694 BasicBlock
*Dominator
= nullptr;
696 // Find all the edges from the sub region to the exit.
697 // We use make_early_inc_range here because we modify BB's terminator.
698 for (BasicBlock
*BB
: llvm::make_early_inc_range(predecessors(OldExit
))) {
699 if (!SubRegion
->contains(BB
))
702 // Modify the edges to point to the new exit
703 delPhiValues(BB
, OldExit
);
704 BB
->getTerminator()->replaceUsesOfWith(OldExit
, NewExit
);
705 addPhiValues(BB
, NewExit
);
707 // Find the new dominator (if requested)
708 if (IncludeDominator
) {
712 Dominator
= DT
->findNearestCommonDominator(Dominator
, BB
);
716 // Change the dominator (if requested)
718 DT
->changeImmediateDominator(NewExit
, Dominator
);
720 // Update the region info
721 SubRegion
->replaceExit(NewExit
);
723 BasicBlock
*BB
= Node
->getNodeAs
<BasicBlock
>();
725 BranchInst::Create(NewExit
, BB
);
726 addPhiValues(BB
, NewExit
);
727 if (IncludeDominator
)
728 DT
->changeImmediateDominator(NewExit
, BB
);
732 /// Create a new flow node and update dominator tree and region info
733 BasicBlock
*StructurizeCFG::getNextFlow(BasicBlock
*Dominator
) {
734 LLVMContext
&Context
= Func
->getContext();
735 BasicBlock
*Insert
= Order
.empty() ? ParentRegion
->getExit() :
736 Order
.back()->getEntry();
737 BasicBlock
*Flow
= BasicBlock::Create(Context
, FlowBlockName
,
739 DT
->addNewBlock(Flow
, Dominator
);
740 ParentRegion
->getRegionInfo()->setRegionFor(Flow
, ParentRegion
);
744 /// Create a new or reuse the previous node as flow node
745 BasicBlock
*StructurizeCFG::needPrefix(bool NeedEmpty
) {
746 BasicBlock
*Entry
= PrevNode
->getEntry();
748 if (!PrevNode
->isSubRegion()) {
749 killTerminator(Entry
);
750 if (!NeedEmpty
|| Entry
->getFirstInsertionPt() == Entry
->end())
754 // create a new flow node
755 BasicBlock
*Flow
= getNextFlow(Entry
);
758 changeExit(PrevNode
, Flow
, true);
759 PrevNode
= ParentRegion
->getBBNode(Flow
);
763 /// Returns the region exit if possible, otherwise just a new flow node
764 BasicBlock
*StructurizeCFG::needPostfix(BasicBlock
*Flow
,
765 bool ExitUseAllowed
) {
766 if (!Order
.empty() || !ExitUseAllowed
)
767 return getNextFlow(Flow
);
769 BasicBlock
*Exit
= ParentRegion
->getExit();
770 DT
->changeImmediateDominator(Exit
, Flow
);
771 addPhiValues(Flow
, Exit
);
775 /// Set the previous node
776 void StructurizeCFG::setPrevNode(BasicBlock
*BB
) {
777 PrevNode
= ParentRegion
->contains(BB
) ? ParentRegion
->getBBNode(BB
)
781 /// Does BB dominate all the predicates of Node?
782 bool StructurizeCFG::dominatesPredicates(BasicBlock
*BB
, RegionNode
*Node
) {
783 BBPredicates
&Preds
= Predicates
[Node
->getEntry()];
784 return llvm::all_of(Preds
, [&](std::pair
<BasicBlock
*, Value
*> Pred
) {
785 return DT
->dominates(BB
, Pred
.first
);
789 /// Can we predict that this node will always be called?
790 bool StructurizeCFG::isPredictableTrue(RegionNode
*Node
) {
791 BBPredicates
&Preds
= Predicates
[Node
->getEntry()];
792 bool Dominated
= false;
794 // Regionentry is always true
798 for (std::pair
<BasicBlock
*, Value
*> Pred
: Preds
) {
799 BasicBlock
*BB
= Pred
.first
;
800 Value
*V
= Pred
.second
;
805 if (!Dominated
&& DT
->dominates(BB
, PrevNode
->getEntry()))
809 // TODO: The dominator check is too strict
813 /// Take one node from the order vector and wire it up
814 void StructurizeCFG::wireFlow(bool ExitUseAllowed
,
815 BasicBlock
*LoopEnd
) {
816 RegionNode
*Node
= Order
.pop_back_val();
817 Visited
.insert(Node
->getEntry());
819 if (isPredictableTrue(Node
)) {
820 // Just a linear flow
822 changeExit(PrevNode
, Node
->getEntry(), true);
826 // Insert extra prefix node (or reuse last one)
827 BasicBlock
*Flow
= needPrefix(false);
829 // Insert extra postfix node (or use exit instead)
830 BasicBlock
*Entry
= Node
->getEntry();
831 BasicBlock
*Next
= needPostfix(Flow
, ExitUseAllowed
);
833 // let it point to entry and next block
834 Conditions
.push_back(BranchInst::Create(Entry
, Next
, BoolUndef
, Flow
));
835 addPhiValues(Flow
, Entry
);
836 DT
->changeImmediateDominator(Entry
, Flow
);
839 while (!Order
.empty() && !Visited
.count(LoopEnd
) &&
840 dominatesPredicates(Entry
, Order
.back())) {
841 handleLoops(false, LoopEnd
);
844 changeExit(PrevNode
, Next
, false);
849 void StructurizeCFG::handleLoops(bool ExitUseAllowed
,
850 BasicBlock
*LoopEnd
) {
851 RegionNode
*Node
= Order
.back();
852 BasicBlock
*LoopStart
= Node
->getEntry();
854 if (!Loops
.count(LoopStart
)) {
855 wireFlow(ExitUseAllowed
, LoopEnd
);
859 if (!isPredictableTrue(Node
))
860 LoopStart
= needPrefix(true);
862 LoopEnd
= Loops
[Node
->getEntry()];
863 wireFlow(false, LoopEnd
);
864 while (!Visited
.count(LoopEnd
)) {
865 handleLoops(false, LoopEnd
);
868 // If the start of the loop is the entry block, we can't branch to it so
869 // insert a new dummy entry block.
870 Function
*LoopFunc
= LoopStart
->getParent();
871 if (LoopStart
== &LoopFunc
->getEntryBlock()) {
872 LoopStart
->setName("entry.orig");
874 BasicBlock
*NewEntry
=
875 BasicBlock::Create(LoopStart
->getContext(),
879 BranchInst::Create(LoopStart
, NewEntry
);
880 DT
->setNewRoot(NewEntry
);
883 // Create an extra loop end node
884 LoopEnd
= needPrefix(false);
885 BasicBlock
*Next
= needPostfix(LoopEnd
, ExitUseAllowed
);
886 LoopConds
.push_back(BranchInst::Create(Next
, LoopStart
,
887 BoolUndef
, LoopEnd
));
888 addPhiValues(LoopEnd
, LoopStart
);
892 /// After this function control flow looks like it should be, but
893 /// branches and PHI nodes only have undefined conditions.
894 void StructurizeCFG::createFlow() {
895 BasicBlock
*Exit
= ParentRegion
->getExit();
896 bool EntryDominatesExit
= DT
->dominates(ParentRegion
->getEntry(), Exit
);
898 AffectedPhis
.clear();
907 while (!Order
.empty()) {
908 handleLoops(EntryDominatesExit
, nullptr);
912 changeExit(PrevNode
, Exit
, EntryDominatesExit
);
914 assert(EntryDominatesExit
);
917 /// Handle a rare case where the disintegrated nodes instructions
918 /// no longer dominate all their uses. Not sure if this is really necessary
919 void StructurizeCFG::rebuildSSA() {
921 for (BasicBlock
*BB
: ParentRegion
->blocks())
922 for (Instruction
&I
: *BB
) {
923 bool Initialized
= false;
924 // We may modify the use list as we iterate over it, so we use
925 // make_early_inc_range.
926 for (Use
&U
: llvm::make_early_inc_range(I
.uses())) {
927 Instruction
*User
= cast
<Instruction
>(U
.getUser());
928 if (User
->getParent() == BB
) {
930 } else if (PHINode
*UserPN
= dyn_cast
<PHINode
>(User
)) {
931 if (UserPN
->getIncomingBlock(U
) == BB
)
935 if (DT
->dominates(&I
, User
))
939 Value
*Undef
= UndefValue::get(I
.getType());
940 Updater
.Initialize(I
.getType(), "");
941 Updater
.AddAvailableValue(&Func
->getEntryBlock(), Undef
);
942 Updater
.AddAvailableValue(BB
, &I
);
945 Updater
.RewriteUseAfterInsertions(U
);
950 static bool hasOnlyUniformBranches(Region
*R
, unsigned UniformMDKindID
,
951 const LegacyDivergenceAnalysis
&DA
) {
952 // Bool for if all sub-regions are uniform.
953 bool SubRegionsAreUniform
= true;
954 // Count of how many direct children are conditional.
955 unsigned ConditionalDirectChildren
= 0;
957 for (auto E
: R
->elements()) {
958 if (!E
->isSubRegion()) {
959 auto Br
= dyn_cast
<BranchInst
>(E
->getEntry()->getTerminator());
960 if (!Br
|| !Br
->isConditional())
963 if (!DA
.isUniform(Br
))
966 // One of our direct children is conditional.
967 ConditionalDirectChildren
++;
969 LLVM_DEBUG(dbgs() << "BB: " << Br
->getParent()->getName()
970 << " has uniform terminator\n");
972 // Explicitly refuse to treat regions as uniform if they have non-uniform
973 // subregions. We cannot rely on DivergenceAnalysis for branches in
974 // subregions because those branches may have been removed and re-created,
975 // so we look for our metadata instead.
977 // Warning: It would be nice to treat regions as uniform based only on
978 // their direct child basic blocks' terminators, regardless of whether
979 // subregions are uniform or not. However, this requires a very careful
980 // look at SIAnnotateControlFlow to make sure nothing breaks there.
981 for (auto BB
: E
->getNodeAs
<Region
>()->blocks()) {
982 auto Br
= dyn_cast
<BranchInst
>(BB
->getTerminator());
983 if (!Br
|| !Br
->isConditional())
986 if (!Br
->getMetadata(UniformMDKindID
)) {
987 // Early exit if we cannot have relaxed uniform regions.
988 if (!RelaxedUniformRegions
)
991 SubRegionsAreUniform
= false;
998 // Our region is uniform if:
999 // 1. All conditional branches that are direct children are uniform (checked
1002 // a. All sub-regions are uniform.
1003 // b. There is one or less conditional branches among the direct children.
1004 return SubRegionsAreUniform
|| (ConditionalDirectChildren
<= 1);
1007 void StructurizeCFG::init(Region
*R
) {
1008 LLVMContext
&Context
= R
->getEntry()->getContext();
1010 Boolean
= Type::getInt1Ty(Context
);
1011 BoolTrue
= ConstantInt::getTrue(Context
);
1012 BoolFalse
= ConstantInt::getFalse(Context
);
1013 BoolUndef
= UndefValue::get(Boolean
);
1018 bool StructurizeCFG::makeUniformRegion(Region
*R
,
1019 LegacyDivergenceAnalysis
*DA
) {
1020 if (R
->isTopLevelRegion())
1024 // TODO: We could probably be smarter here with how we handle sub-regions.
1025 // We currently rely on the fact that metadata is set by earlier invocations
1026 // of the pass on sub-regions, and that this metadata doesn't get lost --
1027 // but we shouldn't rely on metadata for correctness!
1028 unsigned UniformMDKindID
=
1029 R
->getEntry()->getContext().getMDKindID("structurizecfg.uniform");
1031 if (hasOnlyUniformBranches(R
, UniformMDKindID
, *DA
)) {
1032 LLVM_DEBUG(dbgs() << "Skipping region with uniform control flow: " << *R
1035 // Mark all direct child block terminators as having been treated as
1036 // uniform. To account for a possible future in which non-uniform
1037 // sub-regions are treated more cleverly, indirect children are not
1038 // marked as uniform.
1039 MDNode
*MD
= MDNode::get(R
->getEntry()->getParent()->getContext(), {});
1040 for (RegionNode
*E
: R
->elements()) {
1041 if (E
->isSubRegion())
1044 if (Instruction
*Term
= E
->getEntry()->getTerminator())
1045 Term
->setMetadata(UniformMDKindID
, MD
);
1053 /// Run the transformation for each region found
1054 bool StructurizeCFG::run(Region
*R
, DominatorTree
*DT
) {
1055 if (R
->isTopLevelRegion())
1060 Func
= R
->getEntry()->getParent();
1066 insertConditions(false);
1067 insertConditions(true);
1069 simplifyAffectedPhis();
1075 DeletedPhis
.clear();
1086 Pass
*llvm::createStructurizeCFGPass(bool SkipUniformRegions
) {
1087 return new StructurizeCFGLegacyPass(SkipUniformRegions
);
1090 static void addRegionIntoQueue(Region
&R
, std::vector
<Region
*> &Regions
) {
1091 Regions
.push_back(&R
);
1092 for (const auto &E
: R
)
1093 addRegionIntoQueue(*E
, Regions
);
1096 PreservedAnalyses
StructurizeCFGPass::run(Function
&F
,
1097 FunctionAnalysisManager
&AM
) {
1099 bool Changed
= false;
1100 DominatorTree
*DT
= &AM
.getResult
<DominatorTreeAnalysis
>(F
);
1101 auto &RI
= AM
.getResult
<RegionInfoAnalysis
>(F
);
1102 std::vector
<Region
*> Regions
;
1103 addRegionIntoQueue(*RI
.getTopLevelRegion(), Regions
);
1104 while (!Regions
.empty()) {
1105 Region
*R
= Regions
.back();
1106 StructurizeCFG SCFG
;
1108 Changed
|= SCFG
.run(R
, DT
);
1112 return PreservedAnalyses::all();
1113 PreservedAnalyses PA
;
1114 PA
.preserve
<DominatorTreeAnalysis
>();