blob: 6ef36dcad57f6ab0e9fd79d243d9668a7ebb43e3 [file] [log] [blame]
Nick Lewycky0b682452013-07-27 01:24:00 +00001//===-- CFG.cpp - BasicBlock analysis --------------------------------------==//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Nick Lewycky0b682452013-07-27 01:24:00 +00006//
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"
Nick Lewycky0b682452013-07-27 01:24:00 +000015#include "llvm/ADT/SmallSet.h"
Nick Lewycky0b682452013-07-27 01:24:00 +000016#include "llvm/Analysis/LoopInfo.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000017#include "llvm/IR/Dominators.h"
Nick Lewycky0b682452013-07-27 01:24:00 +000018
19using namespace llvm;
20
21/// FindFunctionBackedges - Analyze the specified function to find all of the
22/// loop backedges in the function and return them. This is a relatively cheap
23/// (compared to computing dominators and loop info) analysis.
24///
25/// The output is added to Result, as pairs of <from,to> edge info.
26void llvm::FindFunctionBackedges(const Function &F,
27 SmallVectorImpl<std::pair<const BasicBlock*,const BasicBlock*> > &Result) {
28 const BasicBlock *BB = &F.getEntryBlock();
Ramkumar Ramachandra40c3e032015-01-13 03:46:47 +000029 if (succ_empty(BB))
Nick Lewycky0b682452013-07-27 01:24:00 +000030 return;
31
32 SmallPtrSet<const BasicBlock*, 8> Visited;
33 SmallVector<std::pair<const BasicBlock*, succ_const_iterator>, 8> VisitStack;
34 SmallPtrSet<const BasicBlock*, 8> InStack;
35
36 Visited.insert(BB);
37 VisitStack.push_back(std::make_pair(BB, succ_begin(BB)));
38 InStack.insert(BB);
39 do {
40 std::pair<const BasicBlock*, succ_const_iterator> &Top = VisitStack.back();
41 const BasicBlock *ParentBB = Top.first;
42 succ_const_iterator &I = Top.second;
43
44 bool FoundNew = false;
45 while (I != succ_end(ParentBB)) {
46 BB = *I++;
David Blaikie70573dc2014-11-19 07:49:26 +000047 if (Visited.insert(BB).second) {
Nick Lewycky0b682452013-07-27 01:24:00 +000048 FoundNew = true;
49 break;
50 }
51 // Successor is in VisitStack, it's a back edge.
52 if (InStack.count(BB))
53 Result.push_back(std::make_pair(ParentBB, BB));
54 }
55
56 if (FoundNew) {
57 // Go down one level if there is a unvisited successor.
58 InStack.insert(BB);
59 VisitStack.push_back(std::make_pair(BB, succ_begin(BB)));
60 } else {
61 // Go up one level.
62 InStack.erase(VisitStack.pop_back_val().first);
63 }
64 } while (!VisitStack.empty());
65}
66
67/// GetSuccessorNumber - Search for the specified successor of basic block BB
68/// and return its position in the terminator instruction's list of
69/// successors. It is an error to call this with a block that is not a
70/// successor.
Rong Xua1f61fe2015-11-20 23:02:06 +000071unsigned llvm::GetSuccessorNumber(const BasicBlock *BB,
72 const BasicBlock *Succ) {
Chandler Carruthedb12a82018-10-15 10:04:59 +000073 const Instruction *Term = BB->getTerminator();
Nick Lewycky0b682452013-07-27 01:24:00 +000074#ifndef NDEBUG
75 unsigned e = Term->getNumSuccessors();
76#endif
77 for (unsigned i = 0; ; ++i) {
78 assert(i != e && "Didn't find edge?");
79 if (Term->getSuccessor(i) == Succ)
80 return i;
81 }
82}
83
84/// isCriticalEdge - Return true if the specified edge is a critical edge.
85/// Critical edges are edges from a block with multiple successors to a block
86/// with multiple predecessors.
Chandler Carruthb99a24682018-10-15 09:17:09 +000087bool llvm::isCriticalEdge(const Instruction *TI, unsigned SuccNum,
Nick Lewycky0b682452013-07-27 01:24:00 +000088 bool AllowIdenticalEdges) {
Chandler Carruthb99a24682018-10-15 09:17:09 +000089 assert(TI->isTerminator() && "Must be a terminator to have successors!");
Nick Lewycky0b682452013-07-27 01:24:00 +000090 assert(SuccNum < TI->getNumSuccessors() && "Illegal edge specification!");
91 if (TI->getNumSuccessors() == 1) return false;
92
93 const BasicBlock *Dest = TI->getSuccessor(SuccNum);
94 const_pred_iterator I = pred_begin(Dest), E = pred_end(Dest);
95
96 // If there is more than one predecessor, this is a critical edge...
97 assert(I != E && "No preds, but we have an edge to the block?");
98 const BasicBlock *FirstPred = *I;
99 ++I; // Skip one edge due to the incoming arc from TI.
100 if (!AllowIdenticalEdges)
101 return I != E;
102
103 // If AllowIdenticalEdges is true, then we allow this edge to be considered
104 // non-critical iff all preds come from TI's block.
Erik Verbruggene706b882014-03-25 09:06:18 +0000105 for (; I != E; ++I)
106 if (*I != FirstPred)
Nick Lewycky0b682452013-07-27 01:24:00 +0000107 return true;
Nick Lewycky0b682452013-07-27 01:24:00 +0000108 return false;
109}
110
111// LoopInfo contains a mapping from basic block to the innermost loop. Find
112// the outermost loop in the loop nest that contains BB.
Jakub Staszakd184e2d2013-08-20 23:04:15 +0000113static const Loop *getOutermostLoop(const LoopInfo *LI, const BasicBlock *BB) {
Nick Lewycky0b682452013-07-27 01:24:00 +0000114 const Loop *L = LI->getLoopFor(BB);
115 if (L) {
116 while (const Loop *Parent = L->getParentLoop())
117 L = Parent;
118 }
119 return L;
120}
121
122// True if there is a loop which contains both BB1 and BB2.
Jakub Staszakd184e2d2013-08-20 23:04:15 +0000123static bool loopContainsBoth(const LoopInfo *LI,
Nick Lewycky0b682452013-07-27 01:24:00 +0000124 const BasicBlock *BB1, const BasicBlock *BB2) {
125 const Loop *L1 = getOutermostLoop(LI, BB1);
126 const Loop *L2 = getOutermostLoop(LI, BB2);
Craig Topper9f008862014-04-15 04:59:12 +0000127 return L1 != nullptr && L1 == L2;
Nick Lewycky0b682452013-07-27 01:24:00 +0000128}
129
Bruno Cardoso Lopes7900ef82015-06-24 17:53:17 +0000130bool llvm::isPotentiallyReachableFromMany(
131 SmallVectorImpl<BasicBlock *> &Worklist, BasicBlock *StopBB,
132 const DominatorTree *DT, const LoopInfo *LI) {
Nick Lewycky0b682452013-07-27 01:24:00 +0000133 // When the stop block is unreachable, it's dominated from everywhere,
134 // regardless of whether there's a path between the two blocks.
135 if (DT && !DT->isReachableFromEntry(StopBB))
Craig Topper9f008862014-04-15 04:59:12 +0000136 DT = nullptr;
Nick Lewycky0b682452013-07-27 01:24:00 +0000137
138 // Limit the number of blocks we visit. The goal is to avoid run-away compile
139 // times on large CFGs without hampering sensible code. Arbitrarily chosen.
140 unsigned Limit = 32;
Matthias Braunb30f2f512016-01-30 01:24:31 +0000141 SmallPtrSet<const BasicBlock*, 32> Visited;
Nick Lewycky0b682452013-07-27 01:24:00 +0000142 do {
143 BasicBlock *BB = Worklist.pop_back_val();
David Blaikie70573dc2014-11-19 07:49:26 +0000144 if (!Visited.insert(BB).second)
Nick Lewycky0b682452013-07-27 01:24:00 +0000145 continue;
146 if (BB == StopBB)
147 return true;
148 if (DT && DT->dominates(BB, StopBB))
149 return true;
150 if (LI && loopContainsBoth(LI, BB, StopBB))
151 return true;
152
153 if (!--Limit) {
154 // We haven't been able to prove it one way or the other. Conservatively
155 // answer true -- that there is potentially a path.
156 return true;
157 }
158
Craig Topper9f008862014-04-15 04:59:12 +0000159 if (const Loop *Outer = LI ? getOutermostLoop(LI, BB) : nullptr) {
Nick Lewycky0b682452013-07-27 01:24:00 +0000160 // All blocks in a single loop are reachable from all other blocks. From
161 // any of these blocks, we can skip directly to the exits of the loop,
162 // ignoring any other blocks inside the loop body.
163 Outer->getExitBlocks(Worklist);
164 } else {
Benjamin Kramer3c29c072014-02-10 14:17:42 +0000165 Worklist.append(succ_begin(BB), succ_end(BB));
Nick Lewycky0b682452013-07-27 01:24:00 +0000166 }
167 } while (!Worklist.empty());
168
Nick Lewycky8d2e86d2013-08-13 00:03:47 +0000169 // We have exhausted all possible paths and are certain that 'To' can not be
170 // reached from 'From'.
Nick Lewycky0b682452013-07-27 01:24:00 +0000171 return false;
172}
Nick Lewycky8d2e86d2013-08-13 00:03:47 +0000173
174bool llvm::isPotentiallyReachable(const BasicBlock *A, const BasicBlock *B,
Jakub Staszakd184e2d2013-08-20 23:04:15 +0000175 const DominatorTree *DT, const LoopInfo *LI) {
Nick Lewycky8d2e86d2013-08-13 00:03:47 +0000176 assert(A->getParent() == B->getParent() &&
177 "This analysis is function-local!");
178
179 SmallVector<BasicBlock*, 32> Worklist;
180 Worklist.push_back(const_cast<BasicBlock*>(A));
181
Bruno Cardoso Lopes7900ef82015-06-24 17:53:17 +0000182 return isPotentiallyReachableFromMany(Worklist, const_cast<BasicBlock *>(B),
183 DT, LI);
Nick Lewycky8d2e86d2013-08-13 00:03:47 +0000184}
185
186bool llvm::isPotentiallyReachable(const Instruction *A, const Instruction *B,
Jakub Staszakd184e2d2013-08-20 23:04:15 +0000187 const DominatorTree *DT, const LoopInfo *LI) {
Nick Lewycky8d2e86d2013-08-13 00:03:47 +0000188 assert(A->getParent()->getParent() == B->getParent()->getParent() &&
189 "This analysis is function-local!");
190
191 SmallVector<BasicBlock*, 32> Worklist;
192
193 if (A->getParent() == B->getParent()) {
194 // The same block case is special because it's the only time we're looking
195 // within a single block to see which instruction comes first. Once we
196 // start looking at multiple blocks, the first instruction of the block is
197 // reachable, so we only need to determine reachability between whole
198 // blocks.
199 BasicBlock *BB = const_cast<BasicBlock *>(A->getParent());
200
201 // If the block is in a loop then we can reach any instruction in the block
202 // from any other instruction in the block by going around a backedge.
Craig Topper9f008862014-04-15 04:59:12 +0000203 if (LI && LI->getLoopFor(BB) != nullptr)
Nick Lewycky8d2e86d2013-08-13 00:03:47 +0000204 return true;
205
206 // Linear scan, start at 'A', see whether we hit 'B' or the end first.
Duncan P. N. Exon Smith5a82c912015-10-10 00:53:03 +0000207 for (BasicBlock::const_iterator I = A->getIterator(), E = BB->end(); I != E;
208 ++I) {
Nick Lewycky8d2e86d2013-08-13 00:03:47 +0000209 if (&*I == B)
210 return true;
211 }
212
213 // Can't be in a loop if it's the entry block -- the entry block may not
214 // have predecessors.
215 if (BB == &BB->getParent()->getEntryBlock())
216 return false;
217
218 // Otherwise, continue doing the normal per-BB CFG walk.
Benjamin Kramer3c29c072014-02-10 14:17:42 +0000219 Worklist.append(succ_begin(BB), succ_end(BB));
Nick Lewycky8d2e86d2013-08-13 00:03:47 +0000220
221 if (Worklist.empty()) {
222 // We've proven that there's no path!
223 return false;
224 }
225 } else {
226 Worklist.push_back(const_cast<BasicBlock*>(A->getParent()));
227 }
228
Nick Lewycky66d7eb92019-04-01 20:03:16 +0000229 if (DT) {
230 if (DT->isReachableFromEntry(A->getParent()) !=
231 DT->isReachableFromEntry(B->getParent()))
232 return false;
233 if (A->getParent() == &A->getParent()->getParent()->getEntryBlock() &&
234 DT->isReachableFromEntry(B->getParent()))
235 return true;
236 if (B->getParent() == &A->getParent()->getParent()->getEntryBlock() &&
237 DT->isReachableFromEntry(A->getParent()))
238 return false;
239 }
Nick Lewycky8d2e86d2013-08-13 00:03:47 +0000240
Bruno Cardoso Lopes7900ef82015-06-24 17:53:17 +0000241 return isPotentiallyReachableFromMany(
242 Worklist, const_cast<BasicBlock *>(B->getParent()), DT, LI);
Nick Lewycky8d2e86d2013-08-13 00:03:47 +0000243}