X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;ds=sidebyside;f=lib%2FAnalysis%2FCFG.cpp;h=69637606328b14f41dd031e53b537d2452f15f88;hb=5d853bf42d854ba45615dfda928088ec5edf06c7;hp=a5ed21afbff3cf39c8f66b0d148786d4a92fc308;hpb=81e480463d8bb57776d03cebfd083762909023f1;p=oota-llvm.git diff --git a/lib/Analysis/CFG.cpp b/lib/Analysis/CFG.cpp index a5ed21afbff..69637606328 100644 --- a/lib/Analysis/CFG.cpp +++ b/lib/Analysis/CFG.cpp @@ -13,10 +13,9 @@ //===----------------------------------------------------------------------===// #include "llvm/Analysis/CFG.h" - #include "llvm/ADT/SmallSet.h" -#include "llvm/Analysis/Dominators.h" #include "llvm/Analysis/LoopInfo.h" +#include "llvm/IR/Dominators.h" using namespace llvm; @@ -102,21 +101,15 @@ bool llvm::isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum, // If AllowIdenticalEdges is true, then we allow this edge to be considered // non-critical iff all preds come from TI's block. - while (I != E) { - const BasicBlock *P = *I; - if (P != FirstPred) + for (; I != E; ++I) + if (*I != FirstPred) return true; - // Note: leave this as is until no one ever compiles with either gcc 4.0.1 - // or Xcode 2. This seems to work around the pred_iterator assert in PR 2207 - E = pred_end(P); - ++I; - } return false; } // LoopInfo contains a mapping from basic block to the innermost loop. Find // the outermost loop in the loop nest that contains BB. -static const Loop *getOutermostLoop(LoopInfo *LI, const BasicBlock *BB) { +static const Loop *getOutermostLoop(const LoopInfo *LI, const BasicBlock *BB) { const Loop *L = LI->getLoopFor(BB); if (L) { while (const Loop *Parent = L->getParentLoop()) @@ -126,60 +119,17 @@ static const Loop *getOutermostLoop(LoopInfo *LI, const BasicBlock *BB) { } // True if there is a loop which contains both BB1 and BB2. -static bool loopContainsBoth(LoopInfo *LI, +static bool loopContainsBoth(const LoopInfo *LI, const BasicBlock *BB1, const BasicBlock *BB2) { const Loop *L1 = getOutermostLoop(LI, BB1); const Loop *L2 = getOutermostLoop(LI, BB2); return L1 != NULL && L1 == L2; } -static bool isPotentiallyReachableSameBlock(const Instruction *A, - const Instruction *B, - LoopInfo *LI) { - // The same block case is special because it's the only time we're looking - // within a single block to see which comes first. Once we start looking at - // multiple blocks, the first instruction of the block is reachable, so we - // only need to determine reachability between whole blocks. - - const BasicBlock *BB = A->getParent(); - // If the block is in a loop then we can reach any instruction in the block - // from any other instruction in the block by going around the backedge. - // Check whether we're in a loop (or aren't sure). - - // Can't be in a loop if it's the entry block -- the entry block may not - // have predecessors. - bool HasLoop = BB != &BB->getParent()->getEntryBlock(); - - // Can't be in a loop if LoopInfo doesn't know about it. - if (LI && HasLoop) { - HasLoop = LI->getLoopFor(BB) != 0; - } - if (HasLoop) - return true; - - // Linear scan, start at 'A', see whether we hit 'B' or the end first. - for (BasicBlock::const_iterator I = A, E = BB->end(); I != E; ++I) { - if (&*I == B) - return true; - } - return false; -} - -bool llvm::isPotentiallyReachable(const Instruction *A, const Instruction *B, - DominatorTree *DT, LoopInfo *LI) { - assert(A->getParent()->getParent() == B->getParent()->getParent() && - "This analysis is function-local!"); - - const BasicBlock *StopBB = B->getParent(); - - if (A->getParent() == B->getParent()) - return isPotentiallyReachableSameBlock(A, B, LI); - - if (A->getParent() == &A->getParent()->getParent()->getEntryBlock()) - return true; - if (B->getParent() == &A->getParent()->getParent()->getEntryBlock()) - return false; - +static bool isPotentiallyReachableInner(SmallVectorImpl &Worklist, + BasicBlock *StopBB, + const DominatorTree *DT, + const LoopInfo *LI) { // When the stop block is unreachable, it's dominated from everywhere, // regardless of whether there's a path between the two blocks. if (DT && !DT->isReachableFromEntry(StopBB)) @@ -188,11 +138,7 @@ bool llvm::isPotentiallyReachable(const Instruction *A, const Instruction *B, // Limit the number of blocks we visit. The goal is to avoid run-away compile // times on large CFGs without hampering sensible code. Arbitrarily chosen. unsigned Limit = 32; - SmallSet Visited; - SmallVector Worklist; - Worklist.push_back(const_cast(A->getParent())); - do { BasicBlock *BB = Worklist.pop_back_val(); if (!Visited.insert(BB)) @@ -216,12 +162,75 @@ bool llvm::isPotentiallyReachable(const Instruction *A, const Instruction *B, // ignoring any other blocks inside the loop body. Outer->getExitBlocks(Worklist); } else { - for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) - Worklist.push_back(*I); + Worklist.append(succ_begin(BB), succ_end(BB)); } } while (!Worklist.empty()); - // We have exhaustived all possible paths and are certain that 'To' can not - // be reached from 'From'. + // We have exhausted all possible paths and are certain that 'To' can not be + // reached from 'From'. return false; } + +bool llvm::isPotentiallyReachable(const BasicBlock *A, const BasicBlock *B, + const DominatorTree *DT, const LoopInfo *LI) { + assert(A->getParent() == B->getParent() && + "This analysis is function-local!"); + + SmallVector Worklist; + Worklist.push_back(const_cast(A)); + + return isPotentiallyReachableInner(Worklist, const_cast(B), + DT, LI); +} + +bool llvm::isPotentiallyReachable(const Instruction *A, const Instruction *B, + const DominatorTree *DT, const LoopInfo *LI) { + assert(A->getParent()->getParent() == B->getParent()->getParent() && + "This analysis is function-local!"); + + SmallVector Worklist; + + if (A->getParent() == B->getParent()) { + // The same block case is special because it's the only time we're looking + // within a single block to see which instruction comes first. Once we + // start looking at multiple blocks, the first instruction of the block is + // reachable, so we only need to determine reachability between whole + // blocks. + BasicBlock *BB = const_cast(A->getParent()); + + // If the block is in a loop then we can reach any instruction in the block + // from any other instruction in the block by going around a backedge. + if (LI && LI->getLoopFor(BB) != 0) + return true; + + // Linear scan, start at 'A', see whether we hit 'B' or the end first. + for (BasicBlock::const_iterator I = A, E = BB->end(); I != E; ++I) { + if (&*I == B) + return true; + } + + // Can't be in a loop if it's the entry block -- the entry block may not + // have predecessors. + if (BB == &BB->getParent()->getEntryBlock()) + return false; + + // Otherwise, continue doing the normal per-BB CFG walk. + Worklist.append(succ_begin(BB), succ_end(BB)); + + if (Worklist.empty()) { + // We've proven that there's no path! + return false; + } + } else { + Worklist.push_back(const_cast(A->getParent())); + } + + if (A->getParent() == &A->getParent()->getParent()->getEntryBlock()) + return true; + if (B->getParent() == &A->getParent()->getParent()->getEntryBlock()) + return false; + + return isPotentiallyReachableInner(Worklist, + const_cast(B->getParent()), + DT, LI); +}