LLVM 22.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 if (succ_empty(BB))
39 return;
40
44
45 Visited.insert(BB);
46 VisitStack.push_back(std::make_pair(BB, succ_begin(BB)));
47 InStack.insert(BB);
48 do {
49 std::pair<const BasicBlock *, const_succ_iterator> &Top = VisitStack.back();
50 const BasicBlock *ParentBB = Top.first;
51 const_succ_iterator &I = Top.second;
52
53 bool FoundNew = false;
54 while (I != succ_end(ParentBB)) {
55 BB = *I++;
56 if (Visited.insert(BB).second) {
57 FoundNew = true;
58 break;
59 }
60 // Successor is in VisitStack, it's a back edge.
61 if (InStack.count(BB))
62 Result.push_back(std::make_pair(ParentBB, BB));
63 }
64
65 if (FoundNew) {
66 // Go down one level if there is a unvisited successor.
67 InStack.insert(BB);
68 VisitStack.push_back(std::make_pair(BB, succ_begin(BB)));
69 } else {
70 // Go up one level.
71 InStack.erase(VisitStack.pop_back_val().first);
72 }
73 } while (!VisitStack.empty());
74}
75
76/// GetSuccessorNumber - Search for the specified successor of basic block BB
77/// and return its position in the terminator instruction's list of
78/// successors. It is an error to call this with a block that is not a
79/// successor.
81 const BasicBlock *Succ) {
82 const Instruction *Term = BB->getTerminator();
83#ifndef NDEBUG
84 unsigned e = Term->getNumSuccessors();
85#endif
86 for (unsigned i = 0; ; ++i) {
87 assert(i != e && "Didn't find edge?");
88 if (Term->getSuccessor(i) == Succ)
89 return i;
90 }
91}
92
93/// isCriticalEdge - Return true if the specified edge is a critical edge.
94/// Critical edges are edges from a block with multiple successors to a block
95/// with multiple predecessors.
96bool llvm::isCriticalEdge(const Instruction *TI, unsigned SuccNum,
97 bool AllowIdenticalEdges) {
98 assert(SuccNum < TI->getNumSuccessors() && "Illegal edge specification!");
99 return isCriticalEdge(TI, TI->getSuccessor(SuccNum), AllowIdenticalEdges);
100}
101
102bool llvm::isCriticalEdge(const Instruction *TI, const BasicBlock *Dest,
103 bool AllowIdenticalEdges) {
104 assert(TI->isTerminator() && "Must be a terminator to have successors!");
105 if (TI->getNumSuccessors() == 1) return false;
106
108 "No edge between TI's block and Dest.");
109
110 const_pred_iterator I = pred_begin(Dest), E = pred_end(Dest);
111
112 // If there is more than one predecessor, this is a critical edge...
113 assert(I != E && "No preds, but we have an edge to the block?");
114 const BasicBlock *FirstPred = *I;
115 ++I; // Skip one edge due to the incoming arc from TI.
116 if (!AllowIdenticalEdges)
117 return I != E;
118
119 // If AllowIdenticalEdges is true, then we allow this edge to be considered
120 // non-critical iff all preds come from TI's block.
121 for (; I != E; ++I)
122 if (*I != FirstPred)
123 return true;
124 return false;
125}
126
127// LoopInfo contains a mapping from basic block to the innermost loop. Find
128// the outermost loop in the loop nest that contains BB.
129static const Loop *getOutermostLoop(const LoopInfo *LI, const BasicBlock *BB) {
130 const Loop *L = LI->getLoopFor(BB);
131 return L ? L->getOutermostLoop() : nullptr;
132}
133
134template <class StopSetT>
136 const StopSetT &StopSet,
137 const SmallPtrSetImpl<BasicBlock *> *ExclusionSet,
138 const DominatorTree *DT, const LoopInfo *LI) {
139 // When a stop block is unreachable, it's dominated from everywhere,
140 // regardless of whether there's a path between the two blocks.
141 if (DT) {
142 for (auto *BB : StopSet) {
143 if (!DT->isReachableFromEntry(BB)) {
144 DT = nullptr;
145 break;
146 }
147 }
148 }
149
150 // We can't skip directly from a block that dominates the stop block if the
151 // exclusion block is potentially in between.
152 if (ExclusionSet && !ExclusionSet->empty())
153 DT = nullptr;
154
155 // Normally any block in a loop is reachable from any other block in a loop,
156 // however excluded blocks might partition the body of a loop to make that
157 // untrue.
158 SmallPtrSet<const Loop *, 8> LoopsWithHoles;
159 if (LI && ExclusionSet) {
160 for (auto *BB : *ExclusionSet) {
161 if (const Loop *L = getOutermostLoop(LI, BB))
162 LoopsWithHoles.insert(L);
163 }
164 }
165
167 if (LI) {
168 for (auto *StopSetBB : StopSet) {
169 if (const Loop *L = getOutermostLoop(LI, StopSetBB))
170 StopLoops.insert(L);
171 }
172 }
173
174 unsigned Limit = DefaultMaxBBsToExplore;
176 do {
177 BasicBlock *BB = Worklist.pop_back_val();
178 if (!Visited.insert(BB).second)
179 continue;
180 if (StopSet.contains(BB))
181 return true;
182 if (ExclusionSet && ExclusionSet->count(BB))
183 continue;
184 if (DT) {
185 if (llvm::any_of(StopSet, [&](const BasicBlock *StopBB) {
186 return DT->dominates(BB, StopBB);
187 }))
188 return true;
189 }
190
191 const Loop *Outer = nullptr;
192 if (LI) {
193 Outer = getOutermostLoop(LI, BB);
194 // If we're in a loop with a hole, not all blocks in the loop are
195 // reachable from all other blocks. That implies we can't simply jump to
196 // the loop's exit blocks, as that exit might need to pass through an
197 // excluded block. Clear Outer so we process BB's successors.
198 if (LoopsWithHoles.count(Outer))
199 Outer = nullptr;
200 if (StopLoops.contains(Outer))
201 return true;
202 }
203
204 if (!--Limit) {
205 // We haven't been able to prove it one way or the other. Conservatively
206 // answer true -- that there is potentially a path.
207 return true;
208 }
209
210 if (Outer) {
211 // All blocks in a single loop are reachable from all other blocks. From
212 // any of these blocks, we can skip directly to the exits of the loop,
213 // ignoring any other blocks inside the loop body.
214 Outer->getExitBlocks(Worklist);
215 } else {
216 Worklist.append(succ_begin(BB), succ_end(BB));
217 }
218 } while (!Worklist.empty());
219
220 // We have exhausted all possible paths and are certain that 'To' can not be
221 // reached from 'From'.
222 return false;
223}
224
225template <class T> class SingleEntrySet {
226public:
227 using const_iterator = const T *;
228
229 SingleEntrySet(T Elem) : Elem(Elem) {}
230
231 bool contains(T Other) const { return Elem == Other; }
232
233 const_iterator begin() const { return &Elem; }
234 const_iterator end() const { return &Elem + 1; }
235
236private:
237 T Elem;
238};
239
241 SmallVectorImpl<BasicBlock *> &Worklist, const BasicBlock *StopBB,
242 const SmallPtrSetImpl<BasicBlock *> *ExclusionSet, const DominatorTree *DT,
243 const LoopInfo *LI) {
244 return isReachableImpl<SingleEntrySet<const BasicBlock *>>(
245 Worklist, SingleEntrySet<const BasicBlock *>(StopBB), ExclusionSet, DT,
246 LI);
247}
248
252 const SmallPtrSetImpl<BasicBlock *> *ExclusionSet, const DominatorTree *DT,
253 const LoopInfo *LI) {
254 return isReachableImpl<SmallPtrSetImpl<const BasicBlock *>>(
255 Worklist, StopSet, ExclusionSet, DT, LI);
256}
257
259 const BasicBlock *A, const BasicBlock *B,
260 const SmallPtrSetImpl<BasicBlock *> *ExclusionSet, const DominatorTree *DT,
261 const LoopInfo *LI) {
262 assert(A->getParent() == B->getParent() &&
263 "This analysis is function-local!");
264
265 if (DT) {
267 return false;
268 if (!ExclusionSet || ExclusionSet->empty()) {
269 if (A->isEntryBlock() && DT->isReachableFromEntry(B))
270 return true;
271 if (B->isEntryBlock() && DT->isReachableFromEntry(A))
272 return false;
273 }
274 }
275
277 Worklist.push_back(const_cast<BasicBlock*>(A));
278
279 return isPotentiallyReachableFromMany(Worklist, B, ExclusionSet, DT, LI);
280}
281
283 const Instruction *A, const Instruction *B,
284 const SmallPtrSetImpl<BasicBlock *> *ExclusionSet, const DominatorTree *DT,
285 const LoopInfo *LI) {
286 assert(A->getParent()->getParent() == B->getParent()->getParent() &&
287 "This analysis is function-local!");
288
289 if (A->getParent() == B->getParent()) {
290 // The same block case is special because it's the only time we're looking
291 // within a single block to see which instruction comes first. Once we
292 // start looking at multiple blocks, the first instruction of the block is
293 // reachable, so we only need to determine reachability between whole
294 // blocks.
295 BasicBlock *BB = const_cast<BasicBlock *>(A->getParent());
296
297 // If the block is in a loop then we can reach any instruction in the block
298 // from any other instruction in the block by going around a backedge.
299 if (LI && LI->getLoopFor(BB) != nullptr)
300 return true;
301
302 // If A comes before B, then B is definitively reachable from A.
303 if (A == B || A->comesBefore(B))
304 return true;
305
306 // Can't be in a loop if it's the entry block -- the entry block may not
307 // have predecessors.
308 if (BB->isEntryBlock())
309 return false;
310
311 // Otherwise, continue doing the normal per-BB CFG walk.
313 Worklist.append(succ_begin(BB), succ_end(BB));
314 if (Worklist.empty()) {
315 // We've proven that there's no path!
316 return false;
317 }
318
319 return isPotentiallyReachableFromMany(Worklist, B->getParent(),
320 ExclusionSet, DT, LI);
321 }
322
324 A->getParent(), B->getParent(), ExclusionSet, DT, LI);
325}
326
328 if (auto *CB = dyn_cast<CallBase>(&I))
329 return CB->hasFnAttr(Attribute::NoReturn);
330 return false;
331}
332
333// A basic block can only return if it terminates with a ReturnInst and does not
334// contain calls to noreturn functions.
335static bool basicBlockCanReturn(const BasicBlock &BB) {
336 if (!isa<ReturnInst>(BB.getTerminator()))
337 return false;
339}
340
341// FIXME: this doesn't handle recursion.
345
346 Visited.insert(&F.front());
347 Worklist.push_back(&F.front());
348
349 do {
350 const BasicBlock *BB = Worklist.pop_back_val();
351 if (basicBlockCanReturn(*BB))
352 return true;
353 for (const BasicBlock *Succ : successors(BB))
354 if (Visited.insert(Succ).second)
355 Worklist.push_back(Succ);
356 } while (!Worklist.empty());
357
358 return false;
359}
360
362 const BasicBlock &Dest) {
363 assert(Src.getParent() == Dest.getParent());
364 if (!Src.getParent()->isPresplitCoroutine())
365 return false;
366 if (auto *SW = dyn_cast<SwitchInst>(Src.getTerminator()))
367 if (auto *Intr = dyn_cast<IntrinsicInst>(SW->getCondition()))
368 return Intr->getIntrinsicID() == Intrinsic::coro_suspend &&
369 SW->getDefaultDest() == &Dest;
370 return false;
371}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned Intr
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
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:135
static const Loop * getOutermostLoop(const LoopInfo *LI, const BasicBlock *BB)
Definition: CFG.cpp:129
static bool instructionDoesNotReturn(const Instruction &I)
Definition: CFG.cpp:327
static bool basicBlockCanReturn(const BasicBlock &BB)
Definition: CFG.cpp:335
std::optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1328
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
const_iterator end() const
Definition: CFG.cpp:234
const_iterator begin() const
Definition: CFG.cpp:233
bool contains(T Other) const
Definition: CFG.cpp:231
SingleEntrySet(T Elem)
Definition: CFG.cpp:229
LLVM Basic Block Representation.
Definition: BasicBlock.h:62
LLVM_ABI bool isEntryBlock() const
Return true if this is the entry block of the containing function.
Definition: BasicBlock.cpp:549
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:213
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:165
LLVM_ABI bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
Definition: Dominators.cpp:334
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.
Definition: Dominators.cpp:135
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
Definition: Instruction.h:315
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...
Definition: SmallPtrSet.h:380
bool erase(PtrType Ptr)
Remove pointer from the set.
Definition: SmallPtrSet.h:418
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:470
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:401
bool contains(ConstPtrType Ptr) const
Definition: SmallPtrSet.h:476
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:541
bool empty() const
Definition: SmallVector.h:82
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:574
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:684
void push_back(const T &Elt)
Definition: SmallVector.h:414
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
const ParentTy * getParent() const
Definition: ilist_node.h:34
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:444
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
bool succ_empty(const Instruction *I)
Definition: CFG.h:256
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:80
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:240
auto successors(const MachineBasicBlock *BB)
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:1751
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:1758
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:249
RNSuccIterator< NodeRef, BlockT, RegionT > succ_begin(NodeRef Node)
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:96
auto pred_begin(const MachineBasicBlock *BB)
LLVM_ABI bool isPresplitCoroSuspendExitEdge(const BasicBlock &Src, const BasicBlock &Dest)
Definition: CFG.cpp:361
auto predecessors(const MachineBasicBlock *BB)
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1916
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:342
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:282