LLVM 20.0.0git
FixIrreducible.cpp
Go to the documentation of this file.
1//===- FixIrreducible.cpp - Convert irreducible control-flow into loops ---===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// An irreducible SCC is one which has multiple "header" blocks, i.e., blocks
10// with control-flow edges incident from outside the SCC. This pass converts a
11// irreducible SCC into a natural loop by applying the following transformation:
12//
13// 1. Collect the set of headers H of the SCC.
14// 2. Collect the set of predecessors P of these headers. These may be inside as
15// well as outside the SCC.
16// 3. Create block N and redirect every edge from set P to set H through N.
17//
18// This converts the SCC into a natural loop with N as the header: N is the only
19// block with edges incident from outside the SCC, and all backedges in the SCC
20// are incident on N, i.e., for every backedge, the head now dominates the tail.
21//
22// INPUT CFG: The blocks A and B form an irreducible loop with two headers.
23//
24// Entry
25// / \
26// v v
27// A ----> B
28// ^ /|
29// `----' |
30// v
31// Exit
32//
33// OUTPUT CFG: Edges incident on A and B are now redirected through a
34// new block N, forming a natural loop consisting of N, A and B.
35//
36// Entry
37// |
38// v
39// .---> N <---.
40// / / \ \
41// | / \ |
42// \ v v /
43// `-- A B --'
44// |
45// v
46// Exit
47//
48// The transformation is applied to every maximal SCC that is not already
49// recognized as a loop. The pass operates on all maximal SCCs found in the
50// function body outside of any loop, as well as those found inside each loop,
51// including inside any newly created loops. This ensures that any SCC hidden
52// inside a maximal SCC is also transformed.
53//
54// The actual transformation is handled by the ControlFlowHub, which redirects
55// specified control flow edges through a set of guard blocks. This also moves
56// every PHINode in an outgoing block to the hub. Since the hub dominates all
57// the outgoing blocks, each such PHINode continues to dominate its uses. Since
58// every header in an SCC has at least two predecessors, every value used in the
59// header (or later) but defined in a predecessor (or earlier) is represented by
60// a PHINode in a header. Hence the above handling of PHINodes is sufficient and
61// no further processing is required to restore SSA.
62//
63// Limitation: The pass cannot handle switch statements and indirect
64// branches. Both must be lowered to plain branches first.
65//
66//===----------------------------------------------------------------------===//
67
73#include "llvm/Pass.h"
77
78#define DEBUG_TYPE "fix-irreducible"
79
80using namespace llvm;
81
82namespace {
83struct FixIrreducible : public FunctionPass {
84 static char ID;
85 FixIrreducible() : FunctionPass(ID) {
87 }
88
89 void getAnalysisUsage(AnalysisUsage &AU) const override {
94 }
95
96 bool runOnFunction(Function &F) override;
97};
98} // namespace
99
100char FixIrreducible::ID = 0;
101
102FunctionPass *llvm::createFixIrreduciblePass() { return new FixIrreducible(); }
103
104INITIALIZE_PASS_BEGIN(FixIrreducible, "fix-irreducible",
105 "Convert irreducible control-flow into natural loops",
106 false /* Only looks at CFG */, false /* Analysis Pass */)
110 "Convert irreducible control-flow into natural loops",
111 false /* Only looks at CFG */, false /* Analysis Pass */)
112
113// When a new loop is created, existing children of the parent loop may now be
114// fully inside the new loop. Reconnect these as children of the new loop.
115static void reconnectChildLoops(LoopInfo &LI, Loop *ParentLoop, Loop *NewLoop,
117 SetVector<BasicBlock *> &Headers) {
118 auto &CandidateLoops = ParentLoop ? ParentLoop->getSubLoopsVector()
119 : LI.getTopLevelLoopsVector();
120 // The new loop cannot be its own child, and any candidate is a
121 // child iff its header is owned by the new loop. Move all the
122 // children to a new vector.
123 auto FirstChild = std::partition(
124 CandidateLoops.begin(), CandidateLoops.end(), [&](Loop *L) {
125 return L == NewLoop || !Blocks.contains(L->getHeader());
126 });
127 SmallVector<Loop *, 8> ChildLoops(FirstChild, CandidateLoops.end());
128 CandidateLoops.erase(FirstChild, CandidateLoops.end());
129
130 for (Loop *Child : ChildLoops) {
131 LLVM_DEBUG(dbgs() << "child loop: " << Child->getHeader()->getName()
132 << "\n");
133 // TODO: A child loop whose header is also a header in the current
134 // SCC gets destroyed since its backedges are removed. That may
135 // not be necessary if we can retain such backedges.
136 if (Headers.count(Child->getHeader())) {
137 for (auto *BB : Child->blocks()) {
138 if (LI.getLoopFor(BB) != Child)
139 continue;
140 LI.changeLoopFor(BB, NewLoop);
141 LLVM_DEBUG(dbgs() << "moved block from child: " << BB->getName()
142 << "\n");
143 }
144 std::vector<Loop *> GrandChildLoops;
145 std::swap(GrandChildLoops, Child->getSubLoopsVector());
146 for (auto *GrandChildLoop : GrandChildLoops) {
147 GrandChildLoop->setParentLoop(nullptr);
148 NewLoop->addChildLoop(GrandChildLoop);
149 }
150 LI.destroy(Child);
151 LLVM_DEBUG(dbgs() << "subsumed child loop (common header)\n");
152 continue;
153 }
154
155 Child->setParentLoop(nullptr);
156 NewLoop->addChildLoop(Child);
157 LLVM_DEBUG(dbgs() << "added child loop to new loop\n");
158 }
159}
160
161// Given a set of blocks and headers in an irreducible SCC, convert it into a
162// natural loop. Also insert this new loop at its appropriate place in the
163// hierarchy of loops.
165 Loop *ParentLoop,
167 SetVector<BasicBlock *> &Headers) {
168#ifndef NDEBUG
169 // All headers are part of the SCC
170 for (auto *H : Headers) {
171 assert(Blocks.count(H));
172 }
173#endif
174
175 SetVector<BasicBlock *> Predecessors;
176 for (auto *H : Headers) {
177 for (auto *P : predecessors(H)) {
178 Predecessors.insert(P);
179 }
180 }
181
183 dbgs() << "Found predecessors:";
184 for (auto P : Predecessors) {
185 dbgs() << " " << P->getName();
186 }
187 dbgs() << "\n");
188
189 // Redirect all the backedges through a "hub" consisting of a series
190 // of guard blocks that manage the flow of control from the
191 // predecessors to the headers.
192 ControlFlowHub CHub;
193 for (BasicBlock *P : Predecessors) {
194 auto *Branch = cast<BranchInst>(P->getTerminator());
195 BasicBlock *Succ0 = Branch->getSuccessor(0);
196 Succ0 = Headers.count(Succ0) ? Succ0 : nullptr;
197 BasicBlock *Succ1 =
198 Branch->isUnconditional() ? nullptr : Branch->getSuccessor(1);
199 Succ1 = Succ1 && Headers.count(Succ1) ? Succ1 : nullptr;
200 CHub.addBranch(P, Succ0, Succ1);
201
202 LLVM_DEBUG(dbgs() << "Added branch: " << P->getName() << " -> "
203 << (Succ0 ? Succ0->getName() : "") << " "
204 << (Succ1 ? Succ1->getName() : "") << "\n");
205 }
206
208 DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
209 CHub.finalize(&DTU, GuardBlocks, "irr");
210#if defined(EXPENSIVE_CHECKS)
211 assert(DT.verify(DominatorTree::VerificationLevel::Full));
212#else
213 assert(DT.verify(DominatorTree::VerificationLevel::Fast));
214#endif
215
216 // Create a new loop from the now-transformed cycle
217 auto NewLoop = LI.AllocateLoop();
218 if (ParentLoop) {
219 ParentLoop->addChildLoop(NewLoop);
220 } else {
221 LI.addTopLevelLoop(NewLoop);
222 }
223
224 // Add the guard blocks to the new loop. The first guard block is
225 // the head of all the backedges, and it is the first to be inserted
226 // in the loop. This ensures that it is recognized as the
227 // header. Since the new loop is already in LoopInfo, the new blocks
228 // are also propagated up the chain of parent loops.
229 for (auto *G : GuardBlocks) {
230 LLVM_DEBUG(dbgs() << "added guard block: " << G->getName() << "\n");
231 NewLoop->addBasicBlockToLoop(G, LI);
232 }
233
234 // Add the SCC blocks to the new loop.
235 for (auto *BB : Blocks) {
236 NewLoop->addBlockEntry(BB);
237 if (LI.getLoopFor(BB) == ParentLoop) {
238 LLVM_DEBUG(dbgs() << "moved block from parent: " << BB->getName()
239 << "\n");
240 LI.changeLoopFor(BB, NewLoop);
241 } else {
242 LLVM_DEBUG(dbgs() << "added block from child: " << BB->getName() << "\n");
243 }
244 }
245 LLVM_DEBUG(dbgs() << "header for new loop: "
246 << NewLoop->getHeader()->getName() << "\n");
247
248 reconnectChildLoops(LI, ParentLoop, NewLoop, Blocks, Headers);
249
250 NewLoop->verifyLoop();
251 if (ParentLoop) {
252 ParentLoop->verifyLoop();
253 }
254#if defined(EXPENSIVE_CHECKS)
255 LI.verify(DT);
256#endif // EXPENSIVE_CHECKS
257}
258
259namespace llvm {
260// Enable the graph traits required for traversing a Loop body.
261template <> struct GraphTraits<Loop> : LoopBodyTraits {};
262} // namespace llvm
263
264// Overloaded wrappers to go with the function template below.
265static BasicBlock *unwrapBlock(BasicBlock *B) { return B; }
267
270 SetVector<BasicBlock *> &Headers) {
271 createNaturalLoopInternal(LI, DT, nullptr, Blocks, Headers);
272}
273
276 SetVector<BasicBlock *> &Headers) {
277 createNaturalLoopInternal(LI, DT, &L, Blocks, Headers);
278}
279
280// Convert irreducible SCCs; Graph G may be a Function* or a Loop&.
281template <class Graph>
282static bool makeReducible(LoopInfo &LI, DominatorTree &DT, Graph &&G) {
283 bool Changed = false;
284 for (auto Scc = scc_begin(G); !Scc.isAtEnd(); ++Scc) {
285 if (Scc->size() < 2)
286 continue;
288 LLVM_DEBUG(dbgs() << "Found SCC:");
289 for (auto N : *Scc) {
290 auto BB = unwrapBlock(N);
291 LLVM_DEBUG(dbgs() << " " << BB->getName());
292 Blocks.insert(BB);
293 }
294 LLVM_DEBUG(dbgs() << "\n");
295
296 // Minor optimization: The SCC blocks are usually discovered in an order
297 // that is the opposite of the order in which these blocks appear as branch
298 // targets. This results in a lot of condition inversions in the control
299 // flow out of the new ControlFlowHub, which can be mitigated if the orders
300 // match. So we discover the headers using the reverse of the block order.
302 LLVM_DEBUG(dbgs() << "Found headers:");
303 for (auto *BB : reverse(Blocks)) {
304 for (const auto P : predecessors(BB)) {
305 // Skip unreachable predecessors.
306 if (!DT.isReachableFromEntry(P))
307 continue;
308 if (!Blocks.count(P)) {
309 LLVM_DEBUG(dbgs() << " " << BB->getName());
310 Headers.insert(BB);
311 break;
312 }
313 }
314 }
315 LLVM_DEBUG(dbgs() << "\n");
316
317 if (Headers.size() == 1) {
318 assert(LI.isLoopHeader(Headers.front()));
319 LLVM_DEBUG(dbgs() << "Natural loop with a single header: skipped\n");
320 continue;
321 }
322 createNaturalLoop(LI, DT, G, Blocks, Headers);
323 Changed = true;
324 }
325 return Changed;
326}
327
329 LLVM_DEBUG(dbgs() << "===== Fix irreducible control-flow in function: "
330 << F.getName() << "\n");
331
332 assert(hasOnlySimpleTerminator(F) && "Unsupported block terminator.");
333
334 bool Changed = false;
335 SmallVector<Loop *, 8> WorkList;
336
337 LLVM_DEBUG(dbgs() << "visiting top-level\n");
338 Changed |= makeReducible(LI, DT, &F);
339
340 // Any SCCs reduced are now already in the list of top-level loops, so simply
341 // add them all to the worklist.
342 append_range(WorkList, LI);
343
344 while (!WorkList.empty()) {
345 auto L = WorkList.pop_back_val();
346 LLVM_DEBUG(dbgs() << "visiting loop with header "
347 << L->getHeader()->getName() << "\n");
348 Changed |= makeReducible(LI, DT, *L);
349 // Any SCCs reduced are now already in the list of child loops, so simply
350 // add them all to the worklist.
351 WorkList.append(L->begin(), L->end());
352 }
353
354 return Changed;
355}
356
357bool FixIrreducible::runOnFunction(Function &F) {
358 auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
359 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
360 return FixIrreducibleImpl(F, LI, DT);
361}
362
365 auto &LI = AM.getResult<LoopAnalysis>(F);
366 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
367 if (!FixIrreducibleImpl(F, LI, DT))
368 return PreservedAnalyses::all();
372 return PA;
373}
arm execution domain fix
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_DEBUG(X)
Definition: Debug.h:101
DenseMap< Block *, BlockRelaxAux > Blocks
Definition: ELF_riscv.cpp:507
static void createNaturalLoop(LoopInfo &LI, DominatorTree &DT, Function *F, SetVector< BasicBlock * > &Blocks, SetVector< BasicBlock * > &Headers)
static bool FixIrreducibleImpl(Function &F, LoopInfo &LI, DominatorTree &DT)
fix irreducible
fix Convert irreducible control flow into natural static false void reconnectChildLoops(LoopInfo &LI, Loop *ParentLoop, Loop *NewLoop, SetVector< BasicBlock * > &Blocks, SetVector< BasicBlock * > &Headers)
static BasicBlock * unwrapBlock(BasicBlock *B)
fix Convert irreducible control flow into natural loops
static bool makeReducible(LoopInfo &LI, DominatorTree &DT, Graph &&G)
static void createNaturalLoopInternal(LoopInfo &LI, DominatorTree &DT, Loop *ParentLoop, SetVector< BasicBlock * > &Blocks, SetVector< BasicBlock * > &Headers)
#define F(x, y, z)
Definition: MD5.cpp:55
#define G(x, y, z)
Definition: MD5.cpp:56
#define H(x, y, z)
Definition: MD5.cpp:57
#define P(N)
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:55
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:57
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:52
This builds on the llvm/ADT/GraphTraits.h file to find the strongly connected components (SCCs) of a ...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:405
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:279
bool verify(VerificationLevel VL=VerificationLevel::Full) const
verify - checks if the tree is correct.
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:317
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:162
bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
Definition: Dominators.cpp:321
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:310
virtual bool runOnFunction(Function &F)=0
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
Analysis pass that exposes the LoopInfo for a function.
Definition: LoopInfo.h:566
void verifyLoop() const
Verify loop structure.
void addChildLoop(LoopT *NewChild)
Add the specified loop to be a child of this loop.
void verify(const DominatorTreeBase< BlockT, false > &DomTree) const
void addTopLevelLoop(LoopT *New)
This adds the specified loop to the collection of top-level loops.
LoopT * AllocateLoop(ArgsTy &&...Args)
bool isLoopHeader(const BlockT *BB) const
void changeLoopFor(BlockT *BB, LoopT *L)
Change the top-level loop that contains BB to the specified loop.
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
The legacy pass manager's analysis pass to compute loop information.
Definition: LoopInfo.h:593
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:39
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:98
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:117
void preserve()
Mark an analysis as preserved.
Definition: Analysis.h:131
A vector that has set insertion semantics.
Definition: SetVector.h:57
size_type size() const
Determine the number of elements in the SetVector.
Definition: SetVector.h:98
const value_type & front() const
Return the first element of the SetVector.
Definition: SetVector.h:143
size_type count(const key_type &key) const
Count the number of elements of a given key in the SetVector.
Definition: SetVector.h:264
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:162
bool empty() const
Definition: SmallVector.h:94
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:696
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
bool hasOnlySimpleTerminator(const Function &F)
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition: STLExtras.h:2098
scc_iterator< T > scc_begin(const T &G)
Construct the begin iterator for a deduced graph type T.
Definition: SCCIterator.h:233
auto reverse(ContainerTy &&C)
Definition: STLExtras.h:419
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
FunctionPass * createFixIrreduciblePass()
void initializeFixIrreduciblePass(PassRegistry &)
auto predecessors(const MachineBasicBlock *BB)
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:860
#define N
Given a set of branch descriptors [BB, Succ0, Succ1], create a "hub" such that the control flow from ...
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
std::pair< const Loop *, BasicBlock * > NodeRef
Definition: LoopIterator.h:41