1 //===-- CFG.cpp - BasicBlock analysis --------------------------------------==//
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 // This family of functions performs analyses on basic blocks, and instructions
10 // contained within basic blocks.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Analysis/CFG.h"
15 #include "llvm/Analysis/LoopInfo.h"
16 #include "llvm/IR/Dominators.h"
17 #include "llvm/Support/CommandLine.h"
21 // The max number of basic blocks explored during reachability analysis between
22 // two basic blocks. This is kept reasonably small to limit compile time when
23 // repeatedly used by clients of this analysis (such as captureTracking).
24 static cl::opt
<unsigned> DefaultMaxBBsToExplore(
25 "dom-tree-reachability-max-bbs-to-explore", cl::Hidden
,
26 cl::desc("Max number of BBs to explore for reachability analysis"),
29 /// FindFunctionBackedges - Analyze the specified function to find all of the
30 /// loop backedges in the function and return them. This is a relatively cheap
31 /// (compared to computing dominators and loop info) analysis.
33 /// The output is added to Result, as pairs of <from,to> edge info.
34 void llvm::FindFunctionBackedges(const Function
&F
,
35 SmallVectorImpl
<std::pair
<const BasicBlock
*,const BasicBlock
*> > &Result
) {
36 const BasicBlock
*BB
= &F
.getEntryBlock();
40 SmallPtrSet
<const BasicBlock
*, 8> Visited
;
41 SmallVector
<std::pair
<const BasicBlock
*, const_succ_iterator
>, 8> VisitStack
;
42 SmallPtrSet
<const BasicBlock
*, 8> InStack
;
45 VisitStack
.push_back(std::make_pair(BB
, succ_begin(BB
)));
48 std::pair
<const BasicBlock
*, const_succ_iterator
> &Top
= VisitStack
.back();
49 const BasicBlock
*ParentBB
= Top
.first
;
50 const_succ_iterator
&I
= Top
.second
;
52 bool FoundNew
= false;
53 while (I
!= succ_end(ParentBB
)) {
55 if (Visited
.insert(BB
).second
) {
59 // Successor is in VisitStack, it's a back edge.
60 if (InStack
.count(BB
))
61 Result
.push_back(std::make_pair(ParentBB
, BB
));
65 // Go down one level if there is a unvisited successor.
67 VisitStack
.push_back(std::make_pair(BB
, succ_begin(BB
)));
70 InStack
.erase(VisitStack
.pop_back_val().first
);
72 } while (!VisitStack
.empty());
75 /// GetSuccessorNumber - Search for the specified successor of basic block BB
76 /// and return its position in the terminator instruction's list of
77 /// successors. It is an error to call this with a block that is not a
79 unsigned llvm::GetSuccessorNumber(const BasicBlock
*BB
,
80 const BasicBlock
*Succ
) {
81 const Instruction
*Term
= BB
->getTerminator();
83 unsigned e
= Term
->getNumSuccessors();
85 for (unsigned i
= 0; ; ++i
) {
86 assert(i
!= e
&& "Didn't find edge?");
87 if (Term
->getSuccessor(i
) == Succ
)
92 /// isCriticalEdge - Return true if the specified edge is a critical edge.
93 /// Critical edges are edges from a block with multiple successors to a block
94 /// with multiple predecessors.
95 bool llvm::isCriticalEdge(const Instruction
*TI
, unsigned SuccNum
,
96 bool AllowIdenticalEdges
) {
97 assert(SuccNum
< TI
->getNumSuccessors() && "Illegal edge specification!");
98 return isCriticalEdge(TI
, TI
->getSuccessor(SuccNum
), AllowIdenticalEdges
);
101 bool llvm::isCriticalEdge(const Instruction
*TI
, const BasicBlock
*Dest
,
102 bool AllowIdenticalEdges
) {
103 assert(TI
->isTerminator() && "Must be a terminator to have successors!");
104 if (TI
->getNumSuccessors() == 1) return false;
106 assert(is_contained(predecessors(Dest
), TI
->getParent()) &&
107 "No edge between TI's block and Dest.");
109 const_pred_iterator I
= pred_begin(Dest
), E
= pred_end(Dest
);
111 // If there is more than one predecessor, this is a critical edge...
112 assert(I
!= E
&& "No preds, but we have an edge to the block?");
113 const BasicBlock
*FirstPred
= *I
;
114 ++I
; // Skip one edge due to the incoming arc from TI.
115 if (!AllowIdenticalEdges
)
118 // If AllowIdenticalEdges is true, then we allow this edge to be considered
119 // non-critical iff all preds come from TI's block.
126 // LoopInfo contains a mapping from basic block to the innermost loop. Find
127 // the outermost loop in the loop nest that contains BB.
128 static const Loop
*getOutermostLoop(const LoopInfo
*LI
, const BasicBlock
*BB
) {
129 const Loop
*L
= LI
->getLoopFor(BB
);
130 return L
? L
->getOutermostLoop() : nullptr;
133 bool llvm::isPotentiallyReachableFromMany(
134 SmallVectorImpl
<BasicBlock
*> &Worklist
, const BasicBlock
*StopBB
,
135 const SmallPtrSetImpl
<BasicBlock
*> *ExclusionSet
, const DominatorTree
*DT
,
136 const LoopInfo
*LI
) {
137 // When the stop block is unreachable, it's dominated from everywhere,
138 // regardless of whether there's a path between the two blocks.
139 if (DT
&& !DT
->isReachableFromEntry(StopBB
))
142 // We can't skip directly from a block that dominates the stop block if the
143 // exclusion block is potentially in between.
144 if (ExclusionSet
&& !ExclusionSet
->empty())
147 // Normally any block in a loop is reachable from any other block in a loop,
148 // however excluded blocks might partition the body of a loop to make that
150 SmallPtrSet
<const Loop
*, 8> LoopsWithHoles
;
151 if (LI
&& ExclusionSet
) {
152 for (auto *BB
: *ExclusionSet
) {
153 if (const Loop
*L
= getOutermostLoop(LI
, BB
))
154 LoopsWithHoles
.insert(L
);
158 const Loop
*StopLoop
= LI
? getOutermostLoop(LI
, StopBB
) : nullptr;
160 unsigned Limit
= DefaultMaxBBsToExplore
;
161 SmallPtrSet
<const BasicBlock
*, 32> Visited
;
163 BasicBlock
*BB
= Worklist
.pop_back_val();
164 if (!Visited
.insert(BB
).second
)
168 if (ExclusionSet
&& ExclusionSet
->count(BB
))
170 if (DT
&& DT
->dominates(BB
, StopBB
))
173 const Loop
*Outer
= nullptr;
175 Outer
= getOutermostLoop(LI
, BB
);
176 // If we're in a loop with a hole, not all blocks in the loop are
177 // reachable from all other blocks. That implies we can't simply jump to
178 // the loop's exit blocks, as that exit might need to pass through an
179 // excluded block. Clear Outer so we process BB's successors.
180 if (LoopsWithHoles
.count(Outer
))
182 if (StopLoop
&& Outer
== StopLoop
)
187 // We haven't been able to prove it one way or the other. Conservatively
188 // answer true -- that there is potentially a path.
193 // All blocks in a single loop are reachable from all other blocks. From
194 // any of these blocks, we can skip directly to the exits of the loop,
195 // ignoring any other blocks inside the loop body.
196 Outer
->getExitBlocks(Worklist
);
198 Worklist
.append(succ_begin(BB
), succ_end(BB
));
200 } while (!Worklist
.empty());
202 // We have exhausted all possible paths and are certain that 'To' can not be
203 // reached from 'From'.
207 bool llvm::isPotentiallyReachable(
208 const BasicBlock
*A
, const BasicBlock
*B
,
209 const SmallPtrSetImpl
<BasicBlock
*> *ExclusionSet
, const DominatorTree
*DT
,
210 const LoopInfo
*LI
) {
211 assert(A
->getParent() == B
->getParent() &&
212 "This analysis is function-local!");
215 if (DT
->isReachableFromEntry(A
) && !DT
->isReachableFromEntry(B
))
217 if (!ExclusionSet
|| ExclusionSet
->empty()) {
218 if (A
->isEntryBlock() && DT
->isReachableFromEntry(B
))
220 if (B
->isEntryBlock() && DT
->isReachableFromEntry(A
))
225 SmallVector
<BasicBlock
*, 32> Worklist
;
226 Worklist
.push_back(const_cast<BasicBlock
*>(A
));
228 return isPotentiallyReachableFromMany(Worklist
, B
, ExclusionSet
, DT
, LI
);
231 bool llvm::isPotentiallyReachable(
232 const Instruction
*A
, const Instruction
*B
,
233 const SmallPtrSetImpl
<BasicBlock
*> *ExclusionSet
, const DominatorTree
*DT
,
234 const LoopInfo
*LI
) {
235 assert(A
->getParent()->getParent() == B
->getParent()->getParent() &&
236 "This analysis is function-local!");
238 if (A
->getParent() == B
->getParent()) {
239 // The same block case is special because it's the only time we're looking
240 // within a single block to see which instruction comes first. Once we
241 // start looking at multiple blocks, the first instruction of the block is
242 // reachable, so we only need to determine reachability between whole
244 BasicBlock
*BB
= const_cast<BasicBlock
*>(A
->getParent());
246 // If the block is in a loop then we can reach any instruction in the block
247 // from any other instruction in the block by going around a backedge.
248 if (LI
&& LI
->getLoopFor(BB
) != nullptr)
251 // If A comes before B, then B is definitively reachable from A.
252 if (A
== B
|| A
->comesBefore(B
))
255 // Can't be in a loop if it's the entry block -- the entry block may not
256 // have predecessors.
257 if (BB
->isEntryBlock())
260 // Otherwise, continue doing the normal per-BB CFG walk.
261 SmallVector
<BasicBlock
*, 32> Worklist
;
262 Worklist
.append(succ_begin(BB
), succ_end(BB
));
263 if (Worklist
.empty()) {
264 // We've proven that there's no path!
268 return isPotentiallyReachableFromMany(Worklist
, B
->getParent(),
269 ExclusionSet
, DT
, LI
);
272 return isPotentiallyReachable(
273 A
->getParent(), B
->getParent(), ExclusionSet
, DT
, LI
);