1259698Sdim//===-- CFG.cpp - BasicBlock analysis --------------------------------------==//
2259698Sdim//
3259698Sdim//                     The LLVM Compiler Infrastructure
4259698Sdim//
5259698Sdim// This file is distributed under the University of Illinois Open Source
6259698Sdim// License. See LICENSE.TXT for details.
7259698Sdim//
8259698Sdim//===----------------------------------------------------------------------===//
9259698Sdim//
10259698Sdim// This family of functions performs analyses on basic blocks, and instructions
11259698Sdim// contained within basic blocks.
12259698Sdim//
13259698Sdim//===----------------------------------------------------------------------===//
14259698Sdim
15259698Sdim#include "llvm/Analysis/CFG.h"
16259698Sdim
17259698Sdim#include "llvm/ADT/SmallSet.h"
18259698Sdim#include "llvm/Analysis/Dominators.h"
19259698Sdim#include "llvm/Analysis/LoopInfo.h"
20259698Sdim
21259698Sdimusing namespace llvm;
22259698Sdim
23259698Sdim/// FindFunctionBackedges - Analyze the specified function to find all of the
24259698Sdim/// loop backedges in the function and return them.  This is a relatively cheap
25259698Sdim/// (compared to computing dominators and loop info) analysis.
26259698Sdim///
27259698Sdim/// The output is added to Result, as pairs of <from,to> edge info.
28259698Sdimvoid llvm::FindFunctionBackedges(const Function &F,
29259698Sdim     SmallVectorImpl<std::pair<const BasicBlock*,const BasicBlock*> > &Result) {
30259698Sdim  const BasicBlock *BB = &F.getEntryBlock();
31259698Sdim  if (succ_begin(BB) == succ_end(BB))
32259698Sdim    return;
33259698Sdim
34259698Sdim  SmallPtrSet<const BasicBlock*, 8> Visited;
35259698Sdim  SmallVector<std::pair<const BasicBlock*, succ_const_iterator>, 8> VisitStack;
36259698Sdim  SmallPtrSet<const BasicBlock*, 8> InStack;
37259698Sdim
38259698Sdim  Visited.insert(BB);
39259698Sdim  VisitStack.push_back(std::make_pair(BB, succ_begin(BB)));
40259698Sdim  InStack.insert(BB);
41259698Sdim  do {
42259698Sdim    std::pair<const BasicBlock*, succ_const_iterator> &Top = VisitStack.back();
43259698Sdim    const BasicBlock *ParentBB = Top.first;
44259698Sdim    succ_const_iterator &I = Top.second;
45259698Sdim
46259698Sdim    bool FoundNew = false;
47259698Sdim    while (I != succ_end(ParentBB)) {
48259698Sdim      BB = *I++;
49259698Sdim      if (Visited.insert(BB)) {
50259698Sdim        FoundNew = true;
51259698Sdim        break;
52259698Sdim      }
53259698Sdim      // Successor is in VisitStack, it's a back edge.
54259698Sdim      if (InStack.count(BB))
55259698Sdim        Result.push_back(std::make_pair(ParentBB, BB));
56259698Sdim    }
57259698Sdim
58259698Sdim    if (FoundNew) {
59259698Sdim      // Go down one level if there is a unvisited successor.
60259698Sdim      InStack.insert(BB);
61259698Sdim      VisitStack.push_back(std::make_pair(BB, succ_begin(BB)));
62259698Sdim    } else {
63259698Sdim      // Go up one level.
64259698Sdim      InStack.erase(VisitStack.pop_back_val().first);
65259698Sdim    }
66259698Sdim  } while (!VisitStack.empty());
67259698Sdim}
68259698Sdim
69259698Sdim/// GetSuccessorNumber - Search for the specified successor of basic block BB
70259698Sdim/// and return its position in the terminator instruction's list of
71259698Sdim/// successors.  It is an error to call this with a block that is not a
72259698Sdim/// successor.
73259698Sdimunsigned llvm::GetSuccessorNumber(BasicBlock *BB, BasicBlock *Succ) {
74259698Sdim  TerminatorInst *Term = BB->getTerminator();
75259698Sdim#ifndef NDEBUG
76259698Sdim  unsigned e = Term->getNumSuccessors();
77259698Sdim#endif
78259698Sdim  for (unsigned i = 0; ; ++i) {
79259698Sdim    assert(i != e && "Didn't find edge?");
80259698Sdim    if (Term->getSuccessor(i) == Succ)
81259698Sdim      return i;
82259698Sdim  }
83259698Sdim}
84259698Sdim
85259698Sdim/// isCriticalEdge - Return true if the specified edge is a critical edge.
86259698Sdim/// Critical edges are edges from a block with multiple successors to a block
87259698Sdim/// with multiple predecessors.
88259698Sdimbool llvm::isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum,
89259698Sdim                          bool AllowIdenticalEdges) {
90259698Sdim  assert(SuccNum < TI->getNumSuccessors() && "Illegal edge specification!");
91259698Sdim  if (TI->getNumSuccessors() == 1) return false;
92259698Sdim
93259698Sdim  const BasicBlock *Dest = TI->getSuccessor(SuccNum);
94259698Sdim  const_pred_iterator I = pred_begin(Dest), E = pred_end(Dest);
95259698Sdim
96259698Sdim  // If there is more than one predecessor, this is a critical edge...
97259698Sdim  assert(I != E && "No preds, but we have an edge to the block?");
98259698Sdim  const BasicBlock *FirstPred = *I;
99259698Sdim  ++I;        // Skip one edge due to the incoming arc from TI.
100259698Sdim  if (!AllowIdenticalEdges)
101259698Sdim    return I != E;
102259698Sdim
103259698Sdim  // If AllowIdenticalEdges is true, then we allow this edge to be considered
104259698Sdim  // non-critical iff all preds come from TI's block.
105259698Sdim  while (I != E) {
106259698Sdim    const BasicBlock *P = *I;
107259698Sdim    if (P != FirstPred)
108259698Sdim      return true;
109259698Sdim    // Note: leave this as is until no one ever compiles with either gcc 4.0.1
110259698Sdim    // or Xcode 2. This seems to work around the pred_iterator assert in PR 2207
111259698Sdim    E = pred_end(P);
112259698Sdim    ++I;
113259698Sdim  }
114259698Sdim  return false;
115259698Sdim}
116259698Sdim
117259698Sdim// LoopInfo contains a mapping from basic block to the innermost loop. Find
118259698Sdim// the outermost loop in the loop nest that contains BB.
119259698Sdimstatic const Loop *getOutermostLoop(const LoopInfo *LI, const BasicBlock *BB) {
120259698Sdim  const Loop *L = LI->getLoopFor(BB);
121259698Sdim  if (L) {
122259698Sdim    while (const Loop *Parent = L->getParentLoop())
123259698Sdim      L = Parent;
124259698Sdim  }
125259698Sdim  return L;
126259698Sdim}
127259698Sdim
128259698Sdim// True if there is a loop which contains both BB1 and BB2.
129259698Sdimstatic bool loopContainsBoth(const LoopInfo *LI,
130259698Sdim                             const BasicBlock *BB1, const BasicBlock *BB2) {
131259698Sdim  const Loop *L1 = getOutermostLoop(LI, BB1);
132259698Sdim  const Loop *L2 = getOutermostLoop(LI, BB2);
133259698Sdim  return L1 != NULL && L1 == L2;
134259698Sdim}
135259698Sdim
136259698Sdimstatic bool isPotentiallyReachableInner(SmallVectorImpl<BasicBlock *> &Worklist,
137259698Sdim                                        BasicBlock *StopBB,
138259698Sdim                                        const DominatorTree *DT,
139259698Sdim                                        const LoopInfo *LI) {
140259698Sdim  // When the stop block is unreachable, it's dominated from everywhere,
141259698Sdim  // regardless of whether there's a path between the two blocks.
142259698Sdim  if (DT && !DT->isReachableFromEntry(StopBB))
143259698Sdim    DT = 0;
144259698Sdim
145259698Sdim  // Limit the number of blocks we visit. The goal is to avoid run-away compile
146259698Sdim  // times on large CFGs without hampering sensible code. Arbitrarily chosen.
147259698Sdim  unsigned Limit = 32;
148259698Sdim  SmallSet<const BasicBlock*, 64> Visited;
149259698Sdim  do {
150259698Sdim    BasicBlock *BB = Worklist.pop_back_val();
151259698Sdim    if (!Visited.insert(BB))
152259698Sdim      continue;
153259698Sdim    if (BB == StopBB)
154259698Sdim      return true;
155259698Sdim    if (DT && DT->dominates(BB, StopBB))
156259698Sdim      return true;
157259698Sdim    if (LI && loopContainsBoth(LI, BB, StopBB))
158259698Sdim      return true;
159259698Sdim
160259698Sdim    if (!--Limit) {
161259698Sdim      // We haven't been able to prove it one way or the other. Conservatively
162259698Sdim      // answer true -- that there is potentially a path.
163259698Sdim      return true;
164259698Sdim    }
165259698Sdim
166259698Sdim    if (const Loop *Outer = LI ? getOutermostLoop(LI, BB) : 0) {
167259698Sdim      // All blocks in a single loop are reachable from all other blocks. From
168259698Sdim      // any of these blocks, we can skip directly to the exits of the loop,
169259698Sdim      // ignoring any other blocks inside the loop body.
170259698Sdim      Outer->getExitBlocks(Worklist);
171259698Sdim    } else {
172259698Sdim      for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I)
173259698Sdim        Worklist.push_back(*I);
174259698Sdim    }
175259698Sdim  } while (!Worklist.empty());
176259698Sdim
177259698Sdim  // We have exhausted all possible paths and are certain that 'To' can not be
178259698Sdim  // reached from 'From'.
179259698Sdim  return false;
180259698Sdim}
181259698Sdim
182259698Sdimbool llvm::isPotentiallyReachable(const BasicBlock *A, const BasicBlock *B,
183259698Sdim                                  const DominatorTree *DT, const LoopInfo *LI) {
184259698Sdim  assert(A->getParent() == B->getParent() &&
185259698Sdim         "This analysis is function-local!");
186259698Sdim
187259698Sdim  SmallVector<BasicBlock*, 32> Worklist;
188259698Sdim  Worklist.push_back(const_cast<BasicBlock*>(A));
189259698Sdim
190259698Sdim  return isPotentiallyReachableInner(Worklist, const_cast<BasicBlock*>(B),
191259698Sdim                                     DT, LI);
192259698Sdim}
193259698Sdim
194259698Sdimbool llvm::isPotentiallyReachable(const Instruction *A, const Instruction *B,
195259698Sdim                                  const DominatorTree *DT, const LoopInfo *LI) {
196259698Sdim  assert(A->getParent()->getParent() == B->getParent()->getParent() &&
197259698Sdim         "This analysis is function-local!");
198259698Sdim
199259698Sdim  SmallVector<BasicBlock*, 32> Worklist;
200259698Sdim
201259698Sdim  if (A->getParent() == B->getParent()) {
202259698Sdim    // The same block case is special because it's the only time we're looking
203259698Sdim    // within a single block to see which instruction comes first. Once we
204259698Sdim    // start looking at multiple blocks, the first instruction of the block is
205259698Sdim    // reachable, so we only need to determine reachability between whole
206259698Sdim    // blocks.
207259698Sdim    BasicBlock *BB = const_cast<BasicBlock *>(A->getParent());
208259698Sdim
209259698Sdim    // If the block is in a loop then we can reach any instruction in the block
210259698Sdim    // from any other instruction in the block by going around a backedge.
211259698Sdim    if (LI && LI->getLoopFor(BB) != 0)
212259698Sdim      return true;
213259698Sdim
214259698Sdim    // Linear scan, start at 'A', see whether we hit 'B' or the end first.
215259698Sdim    for (BasicBlock::const_iterator I = A, E = BB->end(); I != E; ++I) {
216259698Sdim      if (&*I == B)
217259698Sdim        return true;
218259698Sdim    }
219259698Sdim
220259698Sdim    // Can't be in a loop if it's the entry block -- the entry block may not
221259698Sdim    // have predecessors.
222259698Sdim    if (BB == &BB->getParent()->getEntryBlock())
223259698Sdim      return false;
224259698Sdim
225259698Sdim    // Otherwise, continue doing the normal per-BB CFG walk.
226259698Sdim    for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I)
227259698Sdim      Worklist.push_back(*I);
228259698Sdim
229259698Sdim    if (Worklist.empty()) {
230259698Sdim      // We've proven that there's no path!
231259698Sdim      return false;
232259698Sdim    }
233259698Sdim  } else {
234259698Sdim    Worklist.push_back(const_cast<BasicBlock*>(A->getParent()));
235259698Sdim  }
236259698Sdim
237259698Sdim  if (A->getParent() == &A->getParent()->getParent()->getEntryBlock())
238259698Sdim    return true;
239259698Sdim  if (B->getParent() == &A->getParent()->getParent()->getEntryBlock())
240259698Sdim    return false;
241259698Sdim
242259698Sdim  return isPotentiallyReachableInner(Worklist,
243259698Sdim                                     const_cast<BasicBlock*>(B->getParent()),
244259698Sdim                                     DT, LI);
245259698Sdim}
246