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"
15#include "llvm/Analysis/CycleAnalysis.h"
16#include "llvm/Analysis/LoopInfo.h"
17#include "llvm/IR/Dominators.h"
18#include "llvm/IR/IntrinsicInst.h"
19#include "llvm/Support/CommandLine.h"
20
21using namespace llvm;
22
23// The max number of basic blocks explored during reachability analysis between
24// two basic blocks. This is kept reasonably small to limit compile time when
25// repeatedly used by clients of this analysis (such as captureTracking).
26static cl::opt<unsigned> DefaultMaxBBsToExplore(
27 "dom-tree-reachability-max-bbs-to-explore", cl::Hidden,
28 cl::desc("Max number of BBs to explore for reachability analysis"),
29 cl::init(Val: 32));
30
31/// FindFunctionBackedges - Analyze the specified function to find all of the
32/// loop backedges in the function and return them. This is a relatively cheap
33/// (compared to computing dominators and loop info) analysis.
34///
35/// The output is added to Result, as pairs of <from,to> edge info.
36void llvm::FindFunctionBackedges(const Function &F,
37 SmallVectorImpl<std::pair<const BasicBlock*,const BasicBlock*> > &Result) {
38 const BasicBlock *BB = &F.getEntryBlock();
39
40 // In the DFS traversal, we maintain three states: unvisited, visited in the
41 // past, and visited and currently in the DFS stack. If we have an edge to a
42 // block in the stack, we have found a backedge.
43 enum VisitState : uint8_t { Unvisited = 0, Visited = 1, InStack = 2 };
44 SmallVector<VisitState> BlockState(F.getMaxBlockNumber(), Unvisited);
45 struct StackEntry {
46 const BasicBlock *BB;
47 const_succ_iterator SuccIt;
48 const_succ_iterator SuccEnd;
49
50 StackEntry(const BasicBlock *BB)
51 : BB(BB), SuccIt(nullptr), SuccEnd(nullptr) {
52 auto Succs = successors(BB);
53 SuccIt = Succs.begin();
54 SuccEnd = Succs.end();
55 }
56 };
57 SmallVector<StackEntry, 8> VisitStack;
58
59 BlockState[BB->getNumber()] = InStack;
60 VisitStack.emplace_back(Args&: BB);
61 do {
62 StackEntry &Top = VisitStack.back();
63 bool FoundNew = false;
64 while (Top.SuccIt != Top.SuccEnd) {
65 BB = *Top.SuccIt++;
66 if (BlockState[BB->getNumber()] == Unvisited) {
67 // Unvisited successor => go down one level.
68 BlockState[BB->getNumber()] = InStack;
69 VisitStack.emplace_back(Args&: BB);
70 FoundNew = true;
71 break;
72 }
73 // Successor in VisitStack => backedge.
74 if (BlockState[BB->getNumber()] == InStack)
75 Result.emplace_back(Args&: Top.BB, Args&: BB);
76 }
77
78 // Go up one level.
79 if (!FoundNew) {
80 BlockState[Top.BB->getNumber()] = Visited;
81 VisitStack.pop_back();
82 }
83 } while (!VisitStack.empty());
84}
85
86/// GetSuccessorNumber - Search for the specified successor of basic block BB
87/// and return its position in the terminator instruction's list of
88/// successors. It is an error to call this with a block that is not a
89/// successor.
90unsigned llvm::GetSuccessorNumber(const BasicBlock *BB,
91 const BasicBlock *Succ) {
92 const Instruction *Term = BB->getTerminator();
93#ifndef NDEBUG
94 unsigned e = Term->getNumSuccessors();
95#endif
96 for (unsigned i = 0; ; ++i) {
97 assert(i != e && "Didn't find edge?");
98 if (Term->getSuccessor(Idx: i) == Succ)
99 return i;
100 }
101}
102
103/// isCriticalEdge - Return true if the specified edge is a critical edge.
104/// Critical edges are edges from a block with multiple successors to a block
105/// with multiple predecessors.
106bool llvm::isCriticalEdge(const Instruction *TI, unsigned SuccNum,
107 bool AllowIdenticalEdges) {
108 assert(SuccNum < TI->getNumSuccessors() && "Illegal edge specification!");
109 return isCriticalEdge(TI, Succ: TI->getSuccessor(Idx: SuccNum), AllowIdenticalEdges);
110}
111
112bool llvm::isCriticalEdge(const Instruction *TI, const BasicBlock *Dest,
113 bool AllowIdenticalEdges) {
114 assert(TI->isTerminator() && "Must be a terminator to have successors!");
115 if (TI->getNumSuccessors() == 1) return false;
116
117 assert(is_contained(predecessors(Dest), TI->getParent()) &&
118 "No edge between TI's block and Dest.");
119
120 const_pred_iterator I = pred_begin(BB: Dest), E = pred_end(BB: Dest);
121
122 // If there is more than one predecessor, this is a critical edge...
123 assert(I != E && "No preds, but we have an edge to the block?");
124 const BasicBlock *FirstPred = *I;
125 ++I; // Skip one edge due to the incoming arc from TI.
126 if (!AllowIdenticalEdges)
127 return I != E;
128
129 // If AllowIdenticalEdges is true, then we allow this edge to be considered
130 // non-critical iff all preds come from TI's block.
131 for (; I != E; ++I)
132 if (*I != FirstPred)
133 return true;
134 return false;
135}
136
137// LoopInfo contains a mapping from basic block to the innermost loop. Find
138// the outermost loop in the loop nest that contains BB.
139static const Loop *getOutermostLoop(const LoopInfo *LI, const BasicBlock *BB) {
140 const Loop *L = LI->getLoopFor(BB);
141 return L ? L->getOutermostLoop() : nullptr;
142}
143
144template <class StopSetT>
145static bool isReachableImpl(SmallVectorImpl<BasicBlock *> &Worklist,
146 const StopSetT &StopSet,
147 const SmallPtrSetImpl<BasicBlock *> *ExclusionSet,
148 const DominatorTree *DT, const LoopInfo *LI,
149 const CycleInfo *CI) {
150 // If both LI and CI are passed, use CI, which gives us more information.
151 if (CI)
152 LI = nullptr;
153
154 // When a stop block is unreachable, it's dominated from everywhere,
155 // regardless of whether there's a path between the two blocks.
156 if (DT) {
157 for (auto *BB : StopSet) {
158 if (!DT->isReachableFromEntry(BB)) {
159 DT = nullptr;
160 break;
161 }
162 }
163 }
164
165 // We can't skip directly from a block that dominates the stop block if the
166 // exclusion block is potentially in between.
167 if (ExclusionSet && !ExclusionSet->empty())
168 DT = nullptr;
169
170 // Normally any block in a loop is reachable from any other block in a loop,
171 // however excluded blocks might partition the body of a loop to make that
172 // untrue.
173 SmallPtrSet<const Loop *, 8> LoopsWithHoles;
174 if (LI && ExclusionSet) {
175 for (auto *BB : *ExclusionSet) {
176 if (const Loop *L = getOutermostLoop(LI, BB))
177 LoopsWithHoles.insert(Ptr: L);
178 }
179 }
180
181 SmallPtrSet<const Cycle *, 8> CyclesWithHoles;
182 if (CI && ExclusionSet) {
183 for (auto *BB : *ExclusionSet) {
184 if (const Cycle *C = CI->getTopLevelParentCycle(Block: BB))
185 CyclesWithHoles.insert(Ptr: C);
186 }
187 }
188
189 SmallPtrSet<const Loop *, 2> StopLoops;
190 if (LI) {
191 for (auto *StopSetBB : StopSet) {
192 if (const Loop *L = getOutermostLoop(LI, StopSetBB))
193 StopLoops.insert(Ptr: L);
194 }
195 }
196
197 SmallPtrSet<const Cycle *, 2> StopCycles;
198 if (CI) {
199 for (auto *StopSetBB : StopSet) {
200 if (const Cycle *C = CI->getTopLevelParentCycle(Block: StopSetBB))
201 StopCycles.insert(Ptr: C);
202 }
203 }
204
205 unsigned Limit = DefaultMaxBBsToExplore;
206 SmallPtrSet<const BasicBlock*, 32> Visited;
207 do {
208 BasicBlock *BB = Worklist.pop_back_val();
209 if (!Visited.insert(Ptr: BB).second)
210 continue;
211 if (StopSet.contains(BB))
212 return true;
213 if (ExclusionSet && ExclusionSet->count(Ptr: BB))
214 continue;
215 if (DT) {
216 if (llvm::any_of(StopSet, [&](const BasicBlock *StopBB) {
217 return DT->dominates(A: BB, B: StopBB);
218 }))
219 return true;
220 }
221
222 const Loop *OuterL = nullptr;
223 if (LI) {
224 OuterL = getOutermostLoop(LI, BB);
225 // If we're in a loop with a hole, not all blocks in the loop are
226 // reachable from all other blocks. That implies we can't simply jump to
227 // the loop's exit blocks, as that exit might need to pass through an
228 // excluded block. Clear Outer so we process BB's successors.
229 if (LoopsWithHoles.count(Ptr: OuterL))
230 OuterL = nullptr;
231 else if (StopLoops.contains(Ptr: OuterL))
232 return true;
233 }
234
235 const Cycle *OuterC = nullptr;
236 if (CI) {
237 OuterC = CI->getTopLevelParentCycle(Block: BB);
238 if (OuterC) {
239 if (CyclesWithHoles.count(Ptr: OuterC))
240 OuterC = nullptr;
241 else if (StopCycles.contains(Ptr: OuterC))
242 return true;
243 } else {
244 // If BB is not part of a cycle, then it can't reach any block that
245 // dominates it. An exception is if the block is unreachable, as all
246 // reachable blocks dominate an unreachable block.
247 if (DT && DT->isReachableFromEntry(A: BB) &&
248 llvm::all_of(StopSet, [&](const BasicBlock *StopBB) {
249 return DT->dominates(A: StopBB, B: BB);
250 }))
251 continue;
252 }
253 }
254
255 if (!--Limit) {
256 // We haven't been able to prove it one way or the other. Conservatively
257 // answer true -- that there is potentially a path.
258 return true;
259 }
260
261 if (OuterL) {
262 // All blocks in a single loop are reachable from all other blocks. From
263 // any of these blocks, we can skip directly to the exits of the loop,
264 // ignoring any other blocks inside the loop body.
265 OuterL->getExitBlocks(ExitBlocks&: Worklist);
266 } else if (OuterC) {
267 OuterC->getExitBlocks(TmpStorage&: Worklist);
268 } else {
269 Worklist.append(in_start: succ_begin(BB), in_end: succ_end(BB));
270 }
271 } while (!Worklist.empty());
272
273 // We have exhausted all possible paths and are certain that 'To' can not be
274 // reached from 'From'.
275 return false;
276}
277
278template <class T> class SingleEntrySet {
279public:
280 using const_iterator = const T *;
281
282 SingleEntrySet(T Elem) : Elem(Elem) {}
283
284 bool contains(T Other) const { return Elem == Other; }
285
286 const_iterator begin() const { return &Elem; }
287 const_iterator end() const { return &Elem + 1; }
288
289private:
290 T Elem;
291};
292
293bool llvm::isPotentiallyReachableFromMany(
294 SmallVectorImpl<BasicBlock *> &Worklist, const BasicBlock *StopBB,
295 const SmallPtrSetImpl<BasicBlock *> *ExclusionSet, const DominatorTree *DT,
296 const LoopInfo *LI, const CycleInfo *CI) {
297 return isReachableImpl<SingleEntrySet<const BasicBlock *>>(
298 Worklist, StopSet: SingleEntrySet<const BasicBlock *>(StopBB), ExclusionSet, DT,
299 LI, CI);
300}
301
302bool llvm::isManyPotentiallyReachableFromMany(
303 SmallVectorImpl<BasicBlock *> &Worklist,
304 const SmallPtrSetImpl<const BasicBlock *> &StopSet,
305 const SmallPtrSetImpl<BasicBlock *> *ExclusionSet, const DominatorTree *DT,
306 const LoopInfo *LI, const CycleInfo *CI) {
307 return isReachableImpl<SmallPtrSetImpl<const BasicBlock *>>(
308 Worklist, StopSet, ExclusionSet, DT, LI, CI);
309}
310
311bool llvm::isPotentiallyReachable(
312 const BasicBlock *A, const BasicBlock *B,
313 const SmallPtrSetImpl<BasicBlock *> *ExclusionSet, const DominatorTree *DT,
314 const LoopInfo *LI, const CycleInfo *CI) {
315 assert(A->getParent() == B->getParent() &&
316 "This analysis is function-local!");
317
318 if (DT) {
319 if (DT->isReachableFromEntry(A) && !DT->isReachableFromEntry(A: B))
320 return false;
321 if (!ExclusionSet || ExclusionSet->empty()) {
322 if (A->isEntryBlock() && DT->isReachableFromEntry(A: B))
323 return true;
324 if (B->isEntryBlock() && DT->isReachableFromEntry(A))
325 return false;
326 }
327 }
328
329 SmallVector<BasicBlock*, 32> Worklist;
330 Worklist.push_back(Elt: const_cast<BasicBlock*>(A));
331
332 return isPotentiallyReachableFromMany(Worklist, StopBB: B, ExclusionSet, DT, LI, CI);
333}
334
335bool llvm::isPotentiallyReachable(
336 const Instruction *A, const Instruction *B,
337 const SmallPtrSetImpl<BasicBlock *> *ExclusionSet, const DominatorTree *DT,
338 const LoopInfo *LI, const CycleInfo *CI) {
339 assert(A->getParent()->getParent() == B->getParent()->getParent() &&
340 "This analysis is function-local!");
341
342 if (A->getParent() == B->getParent()) {
343 // The same block case is special because it's the only time we're looking
344 // within a single block to see which instruction comes first. Once we
345 // start looking at multiple blocks, the first instruction of the block is
346 // reachable, so we only need to determine reachability between whole
347 // blocks.
348 BasicBlock *BB = const_cast<BasicBlock *>(A->getParent());
349
350 // If A comes before B, then B is definitively reachable from A.
351 if (A == B || A->comesBefore(Other: B))
352 return true;
353
354 // If the block is in a cycle (and there are no excluded blocks), then we
355 // can reach any instruction in the block from any other instruction in the
356 // block by going around a backedge.
357 if (!ExclusionSet || ExclusionSet->empty()) {
358 // If cycle info is available, we can know for sure whether or not a
359 // block is part of a cycle.
360 if (CI)
361 return CI->getCycle(Block: BB) != nullptr;
362
363 // If only loop info is available, even if the block is not part of a
364 // natural loop, it may still be part of an irreducible cycle.
365 if (LI && LI->getLoopFor(BB) != nullptr)
366 return true;
367 }
368
369 // Can't be in a loop if it's the entry block -- the entry block may not
370 // have predecessors.
371 if (BB->isEntryBlock())
372 return false;
373
374 // Otherwise, continue doing the normal per-BB CFG walk.
375 SmallVector<BasicBlock*, 32> Worklist;
376 Worklist.append(in_start: succ_begin(BB), in_end: succ_end(BB));
377 if (Worklist.empty()) {
378 // We've proven that there's no path!
379 return false;
380 }
381
382 return isPotentiallyReachableFromMany(Worklist, StopBB: B->getParent(),
383 ExclusionSet, DT, LI, CI);
384 }
385
386 return isPotentiallyReachable(A: A->getParent(), B: B->getParent(), ExclusionSet,
387 DT, LI, CI);
388}
389
390static bool instructionDoesNotReturn(const Instruction &I) {
391 if (auto *CB = dyn_cast<CallBase>(Val: &I))
392 return CB->hasFnAttr(Kind: Attribute::NoReturn);
393 return false;
394}
395
396// A basic block can only return if it terminates with a ReturnInst and does not
397// contain calls to noreturn functions.
398static bool basicBlockCanReturn(const BasicBlock &BB) {
399 if (!isa<ReturnInst>(Val: BB.getTerminator()))
400 return false;
401 return none_of(Range: BB, P: instructionDoesNotReturn);
402}
403
404// FIXME: this doesn't handle recursion.
405bool llvm::canReturn(const Function &F) {
406 SmallVector<const BasicBlock *, 16> Worklist;
407 SmallPtrSet<const BasicBlock *, 16> Visited;
408
409 Visited.insert(Ptr: &F.front());
410 Worklist.push_back(Elt: &F.front());
411
412 do {
413 const BasicBlock *BB = Worklist.pop_back_val();
414 if (basicBlockCanReturn(BB: *BB))
415 return true;
416 for (const BasicBlock *Succ : successors(BB))
417 if (Visited.insert(Ptr: Succ).second)
418 Worklist.push_back(Elt: Succ);
419 } while (!Worklist.empty());
420
421 return false;
422}
423
424bool llvm::isPresplitCoroSuspendExitEdge(const BasicBlock &Src,
425 const BasicBlock &Dest) {
426 assert(Src.getParent() == Dest.getParent());
427 if (!Src.getParent()->isPresplitCoroutine())
428 return false;
429 if (auto *SW = dyn_cast<SwitchInst>(Val: Src.getTerminator()))
430 if (auto *Intr = dyn_cast<IntrinsicInst>(Val: SW->getCondition()))
431 return Intr->getIntrinsicID() == Intrinsic::coro_suspend &&
432 SW->getDefaultDest() == &Dest;
433 return false;
434}
435