LCOV - code coverage report
Current view: top level - lib/Analysis - CFG.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 82 82 100.0 %
Date: 2017-09-14 15:23:50 Functions: 8 8 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===-- CFG.cpp - BasicBlock analysis --------------------------------------==//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : // This family of functions performs analyses on basic blocks, and instructions
      11             : // contained within basic blocks.
      12             : //
      13             : //===----------------------------------------------------------------------===//
      14             : 
      15             : #include "llvm/Analysis/CFG.h"
      16             : #include "llvm/ADT/SmallSet.h"
      17             : #include "llvm/Analysis/LoopInfo.h"
      18             : #include "llvm/IR/Dominators.h"
      19             : 
      20             : using namespace llvm;
      21             : 
      22             : /// FindFunctionBackedges - Analyze the specified function to find all of the
      23             : /// loop backedges in the function and return them.  This is a relatively cheap
      24             : /// (compared to computing dominators and loop info) analysis.
      25             : ///
      26             : /// The output is added to Result, as pairs of <from,to> edge info.
      27      309753 : void llvm::FindFunctionBackedges(const Function &F,
      28             :      SmallVectorImpl<std::pair<const BasicBlock*,const BasicBlock*> > &Result) {
      29      309753 :   const BasicBlock *BB = &F.getEntryBlock();
      30      619506 :   if (succ_empty(BB))
      31      213715 :     return;
      32             : 
      33      192076 :   SmallPtrSet<const BasicBlock*, 8> Visited;
      34      192076 :   SmallVector<std::pair<const BasicBlock*, succ_const_iterator>, 8> VisitStack;
      35      192076 :   SmallPtrSet<const BasicBlock*, 8> InStack;
      36             : 
      37       96038 :   Visited.insert(BB);
      38      288114 :   VisitStack.push_back(std::make_pair(BB, succ_begin(BB)));
      39       96038 :   InStack.insert(BB);
      40             :   do {
      41     5320240 :     std::pair<const BasicBlock*, succ_const_iterator> &Top = VisitStack.back();
      42     2660120 :     const BasicBlock *ParentBB = Top.first;
      43     2660120 :     succ_const_iterator &I = Top.second;
      44             : 
      45     2660120 :     bool FoundNew = false;
      46     6257040 :     while (I != succ_end(ParentBB)) {
      47     5251323 :       BB = *I++;
      48     1750441 :       if (Visited.insert(BB).second) {
      49             :         FoundNew = true;
      50             :         break;
      51             :       }
      52             :       // Successor is in VisitStack, it's a back edge.
      53      468400 :       if (InStack.count(BB))
      54       57696 :         Result.push_back(std::make_pair(ParentBB, BB));
      55             :     }
      56             : 
      57     2660120 :     if (FoundNew) {
      58             :       // Go down one level if there is a unvisited successor.
      59     1282041 :       InStack.insert(BB);
      60     3846123 :       VisitStack.push_back(std::make_pair(BB, succ_begin(BB)));
      61             :     } else {
      62             :       // Go up one level.
      63     2756158 :       InStack.erase(VisitStack.pop_back_val().first);
      64             :     }
      65     2660120 :   } while (!VisitStack.empty());
      66             : }
      67             : 
      68             : /// GetSuccessorNumber - Search for the specified successor of basic block BB
      69             : /// and return its position in the terminator instruction's list of
      70             : /// successors.  It is an error to call this with a block that is not a
      71             : /// successor.
      72        3337 : unsigned llvm::GetSuccessorNumber(const BasicBlock *BB,
      73             :     const BasicBlock *Succ) {
      74        3337 :   const TerminatorInst *Term = BB->getTerminator();
      75             : #ifndef NDEBUG
      76             :   unsigned e = Term->getNumSuccessors();
      77             : #endif
      78        3779 :   for (unsigned i = 0; ; ++i) {
      79         442 :     assert(i != e && "Didn't find edge?");
      80        3779 :     if (Term->getSuccessor(i) == Succ)
      81        3337 :       return i;
      82             :   }
      83             : }
      84             : 
      85             : /// isCriticalEdge - Return true if the specified edge is a critical edge.
      86             : /// Critical edges are edges from a block with multiple successors to a block
      87             : /// with multiple predecessors.
      88        8294 : bool llvm::isCriticalEdge(const TerminatorInst *TI, unsigned SuccNum,
      89             :                           bool AllowIdenticalEdges) {
      90             :   assert(SuccNum < TI->getNumSuccessors() && "Illegal edge specification!");
      91        8294 :   if (TI->getNumSuccessors() == 1) return false;
      92             : 
      93        5860 :   const BasicBlock *Dest = TI->getSuccessor(SuccNum);
      94       11720 :   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        5860 :   const BasicBlock *FirstPred = *I;
      99        5860 :   ++I;        // Skip one edge due to the incoming arc from TI.
     100        5860 :   if (!AllowIdenticalEdges)
     101        5804 :     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          66 :   for (; I != E; ++I)
     106          63 :     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      639920 : static const Loop *getOutermostLoop(const LoopInfo *LI, const BasicBlock *BB) {
     114     1279840 :   const Loop *L = LI->getLoopFor(BB);
     115       39366 :   if (L) {
     116       42989 :     while (const Loop *Parent = L->getParentLoop())
     117             :       L = Parent;
     118             :   }
     119      639920 :   return L;
     120             : }
     121             : 
     122             : // True if there is a loop which contains both BB1 and BB2.
     123      216022 : static bool loopContainsBoth(const LoopInfo *LI,
     124             :                              const BasicBlock *BB1, const BasicBlock *BB2) {
     125      216022 :   const Loop *L1 = getOutermostLoop(LI, BB1);
     126      216022 :   const Loop *L2 = getOutermostLoop(LI, BB2);
     127      216022 :   return L1 != nullptr && L1 == L2;
     128             : }
     129             : 
     130      110037 : bool llvm::isPotentiallyReachableFromMany(
     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      110037 :   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      110037 :   unsigned Limit = 32;
     141      110037 :   SmallPtrSet<const BasicBlock*, 32> Visited;
     142             :   do {
     143     2252295 :     BasicBlock *BB = Worklist.pop_back_val();
     144     2252295 :     if (!Visited.insert(BB).second)
     145      386714 :       continue;
     146     1865581 :     if (BB == StopBB)
     147             :       return true;
     148     1860465 :     if (DT && DT->dominates(BB, StopBB))
     149             :       return true;
     150     1854636 :     if (LI && loopContainsBoth(LI, BB, StopBB))
     151             :       return true;
     152             : 
     153     1850697 :     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     1804281 :     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       15384 :       Outer->getExitBlocks(Worklist);
     164             :     } else {
     165     3577794 :       Worklist.append(succ_begin(BB), succ_end(BB));
     166             :     }
     167     2190995 :   } 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             : 
     174       24377 : bool llvm::isPotentiallyReachable(const BasicBlock *A, const BasicBlock *B,
     175             :                                   const DominatorTree *DT, const LoopInfo *LI) {
     176             :   assert(A->getParent() == B->getParent() &&
     177             :          "This analysis is function-local!");
     178             : 
     179       48754 :   SmallVector<BasicBlock*, 32> Worklist;
     180       24377 :   Worklist.push_back(const_cast<BasicBlock*>(A));
     181             : 
     182             :   return isPotentiallyReachableFromMany(Worklist, const_cast<BasicBlock *>(B),
     183       48754 :                                         DT, LI);
     184             : }
     185             : 
     186       84832 : bool llvm::isPotentiallyReachable(const Instruction *A, const Instruction *B,
     187             :                                   const DominatorTree *DT, const LoopInfo *LI) {
     188             :   assert(A->getParent()->getParent() == B->getParent()->getParent() &&
     189             :          "This analysis is function-local!");
     190             : 
     191      169664 :   SmallVector<BasicBlock*, 32> Worklist;
     192             : 
     193       84832 :   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        2010 :     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        2187 :     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        7380 :     for (BasicBlock::const_iterator I = A->getIterator(), E = BB->end(); I != E;
     208             :          ++I) {
     209        9272 :       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          20 :     if (BB == &BB->getParent()->getEntryBlock())
     216             :       return false;
     217             : 
     218             :     // Otherwise, continue doing the normal per-BB CFG walk.
     219          12 :     Worklist.append(succ_begin(BB), succ_end(BB));
     220             : 
     221           6 :     if (Worklist.empty()) {
     222             :       // We've proven that there's no path!
     223             :       return false;
     224             :     }
     225             :   } else {
     226       82822 :     Worklist.push_back(const_cast<BasicBlock*>(A->getParent()));
     227             :   }
     228             : 
     229      165656 :   if (A->getParent() == &A->getParent()->getParent()->getEntryBlock())
     230             :     return true;
     231      165640 :   if (B->getParent() == &A->getParent()->getParent()->getEntryBlock())
     232             :     return false;
     233             : 
     234             :   return isPotentiallyReachableFromMany(
     235       52571 :       Worklist, const_cast<BasicBlock *>(B->getParent()), DT, LI);
     236             : }

Generated by: LCOV version 1.13