LLVM 23.0.0git
CFG.cpp
Go to the documentation of this file.
1//===-- CFG.cpp - BasicBlock analysis --------------------------------------==//
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// This family of functions performs analyses on basic blocks, and instructions
10// contained within basic blocks.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Analysis/CFG.h"
16#include "llvm/IR/Dominators.h"
19
20using namespace llvm;
21
22// The max number of basic blocks explored during reachability analysis between
23// two basic blocks. This is kept reasonably small to limit compile time when
24// repeatedly used by clients of this analysis (such as captureTracking).
26 "dom-tree-reachability-max-bbs-to-explore", cl::Hidden,
27 cl::desc("Max number of BBs to explore for reachability analysis"),
28 cl::init(32));
29
30/// FindFunctionBackedges - Analyze the specified function to find all of the
31/// loop backedges in the function and return them. This is a relatively cheap
32/// (compared to computing dominators and loop info) analysis.
33///
34/// The output is added to Result, as pairs of <from,to> edge info.
36 SmallVectorImpl<std::pair<const BasicBlock*,const BasicBlock*> > &Result) {
37 const BasicBlock *BB = &F.getEntryBlock();
38
39 // In the DFS traversal, we maintain three states: unvisited, visited in the
40 // past, and visited and currently in the DFS stack. If we have an edge to a
41 // block in the stack, we have found a backedge.
42 enum VisitState : uint8_t { Unvisited = 0, Visited = 1, InStack = 2 };
43 SmallVector<VisitState> BlockState(F.getMaxBlockNumber(), Unvisited);
44 struct StackEntry {
45 const BasicBlock *BB;
47 const_succ_iterator SuccEnd;
48
49 StackEntry(const BasicBlock *BB)
50 : BB(BB), SuccIt(nullptr), SuccEnd(nullptr) {
51 auto Succs = successors(BB);
52 SuccIt = Succs.begin();
53 SuccEnd = Succs.end();
54 }
55 };
57
58 BlockState[BB->getNumber()] = InStack;
59 VisitStack.emplace_back(BB);
60 do {
61 StackEntry &Top = VisitStack.back();
62 bool FoundNew = false;
63 while (Top.SuccIt != Top.SuccEnd) {
64 BB = *Top.SuccIt++;
65 if (BlockState[BB->getNumber()] == Unvisited) {
66 // Unvisited successor => go down one level.
67 BlockState[BB->getNumber()] = InStack;
68 VisitStack.emplace_back(BB);
69 FoundNew = true;
70 break;
71 }
72 // Successor in VisitStack => backedge.
73 if (BlockState[BB->getNumber()] == InStack)
74 Result.emplace_back(Top.BB, BB);
75 }
76
77 // Go up one level.
78 if (!FoundNew) {
79 BlockState[Top.BB->getNumber()] = Visited;
80 VisitStack.pop_back();
81 }
82 } while (!VisitStack.empty());
83}
84
85/// GetSuccessorNumber - Search for the specified successor of basic block BB
86/// and return its position in the terminator instruction's list of
87/// successors. It is an error to call this with a block that is not a
88/// successor.
90 const BasicBlock *Succ) {
91 const Instruction *Term = BB->getTerminator();
92#ifndef NDEBUG
93 unsigned e = Term->getNumSuccessors();
94#endif
95 for (unsigned i = 0; ; ++i) {
96 assert(i != e && "Didn't find edge?");
97 if (Term->getSuccessor(i) == Succ)
98 return i;
99 }
100}
101
102/// isCriticalEdge - Return true if the specified edge is a critical edge.
103/// Critical edges are edges from a block with multiple successors to a block
104/// with multiple predecessors.
105bool llvm::isCriticalEdge(const Instruction *TI, unsigned SuccNum,
106 bool AllowIdenticalEdges) {
107 assert(SuccNum < TI->getNumSuccessors() && "Illegal edge specification!");
108 return isCriticalEdge(TI, TI->getSuccessor(SuccNum), AllowIdenticalEdges);
109}
110
111bool llvm::isCriticalEdge(const Instruction *TI, const BasicBlock *Dest,
112 bool AllowIdenticalEdges) {
113 assert(TI->isTerminator() && "Must be a terminator to have successors!");
114 if (TI->getNumSuccessors() == 1) return false;
115
117 "No edge between TI's block and Dest.");
118
119 const_pred_iterator I = pred_begin(Dest), E = pred_end(Dest);
120
121 // If there is more than one predecessor, this is a critical edge...
122 assert(I != E && "No preds, but we have an edge to the block?");
123 const BasicBlock *FirstPred = *I;
124 ++I; // Skip one edge due to the incoming arc from TI.
125 if (!AllowIdenticalEdges)
126 return I != E;
127
128 // If AllowIdenticalEdges is true, then we allow this edge to be considered
129 // non-critical iff all preds come from TI's block.
130 for (; I != E; ++I)
131 if (*I != FirstPred)
132 return true;
133 return false;
134}
135
136// LoopInfo contains a mapping from basic block to the innermost loop. Find
137// the outermost loop in the loop nest that contains BB.
138static const Loop *getOutermostLoop(const LoopInfo *LI, const BasicBlock *BB) {
139 const Loop *L = LI->getLoopFor(BB);
140 return L ? L->getOutermostLoop() : nullptr;
141}
142
143template <class StopSetT>
145 const StopSetT &StopSet,
146 const SmallPtrSetImpl<BasicBlock *> *ExclusionSet,
147 const DominatorTree *DT, const LoopInfo *LI) {
148 // When a stop block is unreachable, it's dominated from everywhere,
149 // regardless of whether there's a path between the two blocks.
150 if (DT) {
151 for (auto *BB : StopSet) {
152 if (!DT->isReachableFromEntry(BB)) {
153 DT = nullptr;
154 break;
155 }
156 }
157 }
158
159 // We can't skip directly from a block that dominates the stop block if the
160 // exclusion block is potentially in between.
161 if (ExclusionSet && !ExclusionSet->empty())
162 DT = nullptr;
163
164 // Normally any block in a loop is reachable from any other block in a loop,
165 // however excluded blocks might partition the body of a loop to make that
166 // untrue.
167 SmallPtrSet<const Loop *, 8> LoopsWithHoles;
168 if (LI && ExclusionSet) {
169 for (auto *BB : *ExclusionSet) {
170 if (const Loop *L = getOutermostLoop(LI, BB))
171 LoopsWithHoles.insert(L);
172 }
173 }
174
176 if (LI) {
177 for (auto *StopSetBB : StopSet) {
178 if (const Loop *L = getOutermostLoop(LI, StopSetBB))
179 StopLoops.insert(L);
180 }
181 }
182
183 unsigned Limit = DefaultMaxBBsToExplore;
185 do {
186 BasicBlock *BB = Worklist.pop_back_val();
187 if (!Visited.insert(BB).second)
188 continue;
189 if (StopSet.contains(BB))
190 return true;
191 if (ExclusionSet && ExclusionSet->count(BB))
192 continue;
193 if (DT) {
194 if (llvm::any_of(StopSet, [&](const BasicBlock *StopBB) {
195 return DT->dominates(BB, StopBB);
196 }))
197 return true;
198 }
199
200 const Loop *Outer = nullptr;
201 if (LI) {
202 Outer = getOutermostLoop(LI, BB);
203 // If we're in a loop with a hole, not all blocks in the loop are
204 // reachable from all other blocks. That implies we can't simply jump to
205 // the loop's exit blocks, as that exit might need to pass through an
206 // excluded block. Clear Outer so we process BB's successors.
207 if (LoopsWithHoles.count(Outer))
208 Outer = nullptr;
209 if (StopLoops.contains(Outer))
210 return true;
211 }
212
213 if (!--Limit) {
214 // We haven't been able to prove it one way or the other. Conservatively
215 // answer true -- that there is potentially a path.
216 return true;
217 }
218
219 if (Outer) {
220 // All blocks in a single loop are reachable from all other blocks. From
221 // any of these blocks, we can skip directly to the exits of the loop,
222 // ignoring any other blocks inside the loop body.
223 Outer->getExitBlocks(Worklist);
224 } else {
225 Worklist.append(succ_begin(BB), succ_end(BB));
226 }
227 } while (!Worklist.empty());
228
229 // We have exhausted all possible paths and are certain that 'To' can not be
230 // reached from 'From'.
231 return false;
232}
233
234template <class T> class SingleEntrySet {
235public:
236 using const_iterator = const T *;
237
238 SingleEntrySet(T Elem) : Elem(Elem) {}
239
240 bool contains(T Other) const { return Elem == Other; }
241
242 const_iterator begin() const { return &Elem; }
243 const_iterator end() const { return &Elem + 1; }
244
245private:
246 T Elem;
247};
248
250 SmallVectorImpl<BasicBlock *> &Worklist, const BasicBlock *StopBB,
251 const SmallPtrSetImpl<BasicBlock *> *ExclusionSet, const DominatorTree *DT,
252 const LoopInfo *LI) {
254 Worklist, SingleEntrySet<const BasicBlock *>(StopBB), ExclusionSet, DT,
255 LI);
256}
257
261 const SmallPtrSetImpl<BasicBlock *> *ExclusionSet, const DominatorTree *DT,
262 const LoopInfo *LI) {
264 Worklist, StopSet, ExclusionSet, DT, LI);
265}
266
268 const BasicBlock *A, const BasicBlock *B,
269 const SmallPtrSetImpl<BasicBlock *> *ExclusionSet, const DominatorTree *DT,
270 const LoopInfo *LI) {
271 assert(A->getParent() == B->getParent() &&
272 "This analysis is function-local!");
273
274 if (DT) {
276 return false;
277 if (!ExclusionSet || ExclusionSet->empty()) {
278 if (A->isEntryBlock() && DT->isReachableFromEntry(B))
279 return true;
280 if (B->isEntryBlock() && DT->isReachableFromEntry(A))
281 return false;
282 }
283 }
284
286 Worklist.push_back(const_cast<BasicBlock*>(A));
287
288 return isPotentiallyReachableFromMany(Worklist, B, ExclusionSet, DT, LI);
289}
290
292 const Instruction *A, const Instruction *B,
293 const SmallPtrSetImpl<BasicBlock *> *ExclusionSet, const DominatorTree *DT,
294 const LoopInfo *LI) {
295 assert(A->getParent()->getParent() == B->getParent()->getParent() &&
296 "This analysis is function-local!");
297
298 if (A->getParent() == B->getParent()) {
299 // The same block case is special because it's the only time we're looking
300 // within a single block to see which instruction comes first. Once we
301 // start looking at multiple blocks, the first instruction of the block is
302 // reachable, so we only need to determine reachability between whole
303 // blocks.
304 BasicBlock *BB = const_cast<BasicBlock *>(A->getParent());
305
306 // If the block is in a loop then we can reach any instruction in the block
307 // from any other instruction in the block by going around a backedge.
308 if (LI && LI->getLoopFor(BB) != nullptr)
309 return true;
310
311 // If A comes before B, then B is definitively reachable from A.
312 if (A == B || A->comesBefore(B))
313 return true;
314
315 // Can't be in a loop if it's the entry block -- the entry block may not
316 // have predecessors.
317 if (BB->isEntryBlock())
318 return false;
319
320 // Otherwise, continue doing the normal per-BB CFG walk.
322 Worklist.append(succ_begin(BB), succ_end(BB));
323 if (Worklist.empty()) {
324 // We've proven that there's no path!
325 return false;
326 }
327
328 return isPotentiallyReachableFromMany(Worklist, B->getParent(),
329 ExclusionSet, DT, LI);
330 }
331
333 A->getParent(), B->getParent(), ExclusionSet, DT, LI);
334}
335
337 if (auto *CB = dyn_cast<CallBase>(&I))
338 return CB->hasFnAttr(Attribute::NoReturn);
339 return false;
340}
341
342// A basic block can only return if it terminates with a ReturnInst and does not
343// contain calls to noreturn functions.
344static bool basicBlockCanReturn(const BasicBlock &BB) {
346 return false;
348}
349
350// FIXME: this doesn't handle recursion.
354
355 Visited.insert(&F.front());
356 Worklist.push_back(&F.front());
357
358 do {
359 const BasicBlock *BB = Worklist.pop_back_val();
360 if (basicBlockCanReturn(*BB))
361 return true;
362 for (const BasicBlock *Succ : successors(BB))
363 if (Visited.insert(Succ).second)
364 Worklist.push_back(Succ);
365 } while (!Worklist.empty());
366
367 return false;
368}
369
371 const BasicBlock &Dest) {
372 assert(Src.getParent() == Dest.getParent());
373 if (!Src.getParent()->isPresplitCoroutine())
374 return false;
375 if (auto *SW = dyn_cast<SwitchInst>(Src.getTerminator()))
376 if (auto *Intr = dyn_cast<IntrinsicInst>(SW->getCondition()))
377 return Intr->getIntrinsicID() == Intrinsic::coro_suspend &&
378 SW->getDefaultDest() == &Dest;
379 return false;
380}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static cl::opt< unsigned > DefaultMaxBBsToExplore("dom-tree-reachability-max-bbs-to-explore", cl::Hidden, cl::desc("Max number of BBs to explore for reachability analysis"), cl::init(32))
static bool isReachableImpl(SmallVectorImpl< BasicBlock * > &Worklist, const StopSetT &StopSet, const SmallPtrSetImpl< BasicBlock * > *ExclusionSet, const DominatorTree *DT, const LoopInfo *LI)
Definition CFG.cpp:144
static const Loop * getOutermostLoop(const LoopInfo *LI, const BasicBlock *BB)
Definition CFG.cpp:138
static bool instructionDoesNotReturn(const Instruction &I)
Definition CFG.cpp:336
static bool basicBlockCanReturn(const BasicBlock &BB)
Definition CFG.cpp:344
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define T
const_iterator end() const
Definition CFG.cpp:243
const_iterator begin() const
Definition CFG.cpp:242
const T * const_iterator
Definition CFG.cpp:236
bool contains(T Other) const
Definition CFG.cpp:240
SingleEntrySet(T Elem)
Definition CFG.cpp:238
LLVM Basic Block Representation.
Definition BasicBlock.h:62
unsigned getNumber() const
Definition BasicBlock.h:95
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
LLVM_ABI bool isEntryBlock() const
Return true if this is the entry block of the containing function.
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition BasicBlock.h:233
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:159
LLVM_ABI bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
LLVM_ABI unsigned getNumSuccessors() const LLVM_READONLY
Return the number of successors that this instruction has.
LLVM_ABI BasicBlock * getSuccessor(unsigned Idx) const LLVM_READONLY
Return the specified successor. This instruction must be a terminator.
bool isTerminator() const
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool contains(ConstPtrType Ptr) const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
const ParentTy * getParent() const
Definition ilist_node.h:34
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
LLVM_ABI unsigned GetSuccessorNumber(const BasicBlock *BB, const BasicBlock *Succ)
Search for the specified successor of basic block BB and return its position in the terminator instru...
Definition CFG.cpp:89
auto pred_end(const MachineBasicBlock *BB)
LLVM_ABI bool isPotentiallyReachableFromMany(SmallVectorImpl< BasicBlock * > &Worklist, const BasicBlock *StopBB, const SmallPtrSetImpl< BasicBlock * > *ExclusionSet, const DominatorTree *DT=nullptr, const LoopInfo *LI=nullptr)
Determine whether there is at least one path from a block in 'Worklist' to 'StopBB' without passing t...
Definition CFG.cpp:249
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
auto successors(const MachineBasicBlock *BB)
PredIterator< const BasicBlock, Value::const_user_iterator > const_pred_iterator
Definition CFG.h:106
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1746
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1753
LLVM_ABI bool isManyPotentiallyReachableFromMany(SmallVectorImpl< BasicBlock * > &Worklist, const SmallPtrSetImpl< const BasicBlock * > &StopSet, const SmallPtrSetImpl< BasicBlock * > *ExclusionSet, const DominatorTree *DT=nullptr, const LoopInfo *LI=nullptr)
Determine whether there is a potentially a path from at least one block in 'Worklist' to at least one...
Definition CFG.cpp:258
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
RNSuccIterator< NodeRef, BlockT, RegionT > succ_begin(NodeRef Node)
@ Other
Any other memory.
Definition ModRef.h:68
RNSuccIterator< NodeRef, BlockT, RegionT > succ_end(NodeRef Node)
LLVM_ABI bool isCriticalEdge(const Instruction *TI, unsigned SuccNum, bool AllowIdenticalEdges=false)
Return true if the specified edge is a critical edge.
Definition CFG.cpp:105
auto pred_begin(const MachineBasicBlock *BB)
LLVM_ABI bool isPresplitCoroSuspendExitEdge(const BasicBlock &Src, const BasicBlock &Dest)
Definition CFG.cpp:370
auto predecessors(const MachineBasicBlock *BB)
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1947
Instruction::const_succ_iterator const_succ_iterator
Definition CFG.h:139
LLVM_ABI void FindFunctionBackedges(const Function &F, SmallVectorImpl< std::pair< const BasicBlock *, const BasicBlock * > > &Result)
Analyze the specified function to find all of the loop backedges in the function and return them.
Definition CFG.cpp:35
LLVM_ABI bool canReturn(const Function &F)
Return true if there is at least a path through which F can return, false if there is no such path.
Definition CFG.cpp:351
LLVM_ABI bool isPotentiallyReachable(const Instruction *From, const Instruction *To, const SmallPtrSetImpl< BasicBlock * > *ExclusionSet=nullptr, const DominatorTree *DT=nullptr, const LoopInfo *LI=nullptr)
Determine whether instruction 'To' is reachable from 'From', without passing through any blocks in Ex...
Definition CFG.cpp:291