LLVM  9.0.0svn
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"
15 #include "llvm/ADT/SmallSet.h"
16 #include "llvm/Analysis/LoopInfo.h"
17 #include "llvm/IR/Dominators.h"
18 
19 using 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.
27  SmallVectorImpl<std::pair<const BasicBlock*,const BasicBlock*> > &Result) {
28  const BasicBlock *BB = &F.getEntryBlock();
29  if (succ_empty(BB))
30  return;
31 
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++;
47  if (Visited.insert(BB).second) {
48  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.
72  const BasicBlock *Succ) {
73  const Instruction *Term = BB->getTerminator();
74 #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.
87 bool llvm::isCriticalEdge(const Instruction *TI, unsigned SuccNum,
88  bool AllowIdenticalEdges) {
89  assert(TI->isTerminator() && "Must be a terminator to have successors!");
90  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.
105  for (; I != E; ++I)
106  if (*I != FirstPred)
107  return true;
108  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.
113 static const Loop *getOutermostLoop(const LoopInfo *LI, const BasicBlock *BB) {
114  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.
123 static bool loopContainsBoth(const LoopInfo *LI,
124  const BasicBlock *BB1, const BasicBlock *BB2) {
125  const Loop *L1 = getOutermostLoop(LI, BB1);
126  const Loop *L2 = getOutermostLoop(LI, BB2);
127  return L1 != nullptr && L1 == L2;
128 }
129 
131  SmallVectorImpl<BasicBlock *> &Worklist, BasicBlock *StopBB,
132  const DominatorTree *DT, const LoopInfo *LI) {
133  // 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))
136  DT = nullptr;
137 
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;
142  do {
143  BasicBlock *BB = Worklist.pop_back_val();
144  if (!Visited.insert(BB).second)
145  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 
159  if (const Loop *Outer = LI ? getOutermostLoop(LI, BB) : nullptr) {
160  // 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 {
165  Worklist.append(succ_begin(BB), succ_end(BB));
166  }
167  } while (!Worklist.empty());
168 
169  // We have exhausted all possible paths and are certain that 'To' can not be
170  // reached from 'From'.
171  return false;
172 }
173 
175  const DominatorTree *DT, const LoopInfo *LI) {
176  assert(A->getParent() == B->getParent() &&
177  "This analysis is function-local!");
178 
180  Worklist.push_back(const_cast<BasicBlock*>(A));
181 
182  return isPotentiallyReachableFromMany(Worklist, const_cast<BasicBlock *>(B),
183  DT, LI);
184 }
185 
187  const DominatorTree *DT, const LoopInfo *LI) {
188  assert(A->getParent()->getParent() == B->getParent()->getParent() &&
189  "This analysis is function-local!");
190 
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.
203  if (LI && LI->getLoopFor(BB) != nullptr)
204  return true;
205 
206  // Linear scan, start at 'A', see whether we hit 'B' or the end first.
207  for (BasicBlock::const_iterator I = A->getIterator(), E = BB->end(); I != E;
208  ++I) {
209  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.
219  Worklist.append(succ_begin(BB), succ_end(BB));
220 
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 
229  if (A->getParent() == &A->getParent()->getParent()->getEntryBlock())
230  return true;
231  if (B->getParent() == &A->getParent()->getParent()->getEntryBlock())
232  return false;
233 
235  Worklist, const_cast<BasicBlock *>(B->getParent()), DT, LI);
236 }
This class represents lattice values for constants.
Definition: AllocatorList.h:23
BasicBlock * getSuccessor(unsigned Idx) const
Return the specified successor. This instruction must be a terminator.
bool isTerminator() const
Definition: Instruction.h:128
F(f)
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.cpp:137
bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
Definition: Dominators.cpp:299
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
Definition: LoopInfo.h:689
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:41
Interval::succ_iterator succ_begin(Interval *I)
succ_begin/succ_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Definition: Interval.h:102
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:144
unsigned getNumSuccessors() const
Return the number of successors that this instruction has.
Interval::succ_iterator succ_end(Interval *I)
Definition: Interval.h:105
const BasicBlock & getEntryBlock() const
Definition: Function.h:639
static const Loop * getOutermostLoop(const LoopInfo *LI, const BasicBlock *BB)
Definition: CFG.cpp:113
static bool loopContainsBoth(const LoopInfo *LI, const BasicBlock *BB1, const BasicBlock *BB2)
Definition: CFG.cpp:123
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
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:370
Interval::pred_iterator pred_begin(Interval *I)
pred_begin/pred_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Definition: Interval.h:112
Interval::pred_iterator pred_end(Interval *I)
Definition: Interval.h:115
bool isPotentiallyReachable(const Instruction *From, const Instruction *To, const DominatorTree *DT=nullptr, const LoopInfo *LI=nullptr)
Determine whether instruction &#39;To&#39; is reachable from &#39;From&#39;, returning true if uncertain.
Definition: CFG.cpp:186
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:381
self_iterator getIterator()
Definition: ilist_node.h:81
bool isCriticalEdge(const Instruction *TI, unsigned SuccNum, bool AllowIdenticalEdges=false)
Return true if the specified edge is a critical edge.
Definition: CFG.cpp:87
bool succ_empty(const Instruction *I)
Definition: CFG.h:253
Iterator for intrusive lists based on ilist_node.
bool erase(PtrType Ptr)
erase - If the set contains the specified pointer, remove it and return true, otherwise return false...
Definition: SmallPtrSet.h:377
iterator end()
Definition: BasicBlock.h:270
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:839
bool dominates(const Instruction *Def, const Use &U) const
Return true if Def dominates a use in User.
Definition: Dominators.cpp:248
LLVM_NODISCARD T pop_back_val()
Definition: SmallVector.h:373
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:386
LoopT * getParentLoop() const
Definition: LoopInfo.h:100
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:71
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:55
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:464
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:106
#define I(x, y, z)
Definition: MD5.cpp:58
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:26
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool isPotentiallyReachableFromMany(SmallVectorImpl< BasicBlock *> &Worklist, BasicBlock *StopBB, const DominatorTree *DT=nullptr, const LoopInfo *LI=nullptr)
Determine whether there is at least one path from a block in &#39;Worklist&#39; to &#39;StopBB&#39;, returning true if uncertain.
Definition: CFG.cpp:130
const BasicBlock * getParent() const
Definition: Instruction.h:66