LCOV - code coverage report
Current view: top level - lib/Analysis - RegionInfo.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 58 69 84.1 %
Date: 2018-07-13 00:08:38 Functions: 15 19 78.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- RegionInfo.cpp - SESE region detection 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             : // Detects single entry single exit regions in the control flow graph.
      10             : //===----------------------------------------------------------------------===//
      11             : 
      12             : #include "llvm/Analysis/RegionInfo.h"
      13             : #include "llvm/ADT/Statistic.h"
      14             : #ifndef NDEBUG
      15             : #include "llvm/Analysis/RegionPrinter.h"
      16             : #endif
      17             : #include "llvm/Analysis/RegionInfoImpl.h"
      18             : #include "llvm/Config/llvm-config.h"
      19             : #include "llvm/IR/Function.h"
      20             : #include "llvm/IR/PassManager.h"
      21             : #include "llvm/Pass.h"
      22             : #include "llvm/Support/CommandLine.h"
      23             : #include "llvm/Support/Compiler.h"
      24             : #include "llvm/Support/raw_ostream.h"
      25             : 
      26             : using namespace llvm;
      27             : 
      28             : #define DEBUG_TYPE "region"
      29             : 
      30             : namespace llvm {
      31             : 
      32             : template class RegionBase<RegionTraits<Function>>;
      33             : template class RegionNodeBase<RegionTraits<Function>>;
      34             : template class RegionInfoBase<RegionTraits<Function>>;
      35             : 
      36             : } // end namespace llvm
      37             : 
      38             : STATISTIC(numRegions,       "The # of regions");
      39             : STATISTIC(numSimpleRegions, "The # of simple regions");
      40             : 
      41             : // Always verify if expensive checking is enabled.
      42             : 
      43             : static cl::opt<bool,true>
      44       99743 : VerifyRegionInfoX(
      45             :   "verify-region-info",
      46      199486 :   cl::location(RegionInfoBase<RegionTraits<Function>>::VerifyRegionInfo),
      47      199486 :   cl::desc("Verify region info (time consuming)"));
      48             : 
      49       99743 : static cl::opt<Region::PrintStyle, true> printStyleX("print-region-style",
      50      199486 :   cl::location(RegionInfo::printStyle),
      51             :   cl::Hidden,
      52       99743 :   cl::desc("style of printing regions"),
      53      498715 :   cl::values(
      54             :     clEnumValN(Region::PrintNone, "none",  "print no details"),
      55             :     clEnumValN(Region::PrintBB, "bb",
      56             :                "print regions in detail with block_iterator"),
      57             :     clEnumValN(Region::PrintRN, "rn",
      58      299229 :                "print regions in detail with element_iterator")));
      59             : 
      60             : //===----------------------------------------------------------------------===//
      61             : // Region implementation
      62             : //
      63             : 
      64       30998 : Region::Region(BasicBlock *Entry, BasicBlock *Exit,
      65             :                RegionInfo* RI,
      66       30998 :                DominatorTree *DT, Region *Parent) :
      67       30998 :   RegionBase<RegionTraits<Function>>(Entry, Exit, RI, DT, Parent) {
      68             : 
      69       30998 : }
      70             : 
      71             : Region::~Region() = default;
      72             : 
      73             : //===----------------------------------------------------------------------===//
      74             : // RegionInfo implementation
      75             : //
      76             : 
      77             : RegionInfo::RegionInfo() = default;
      78             : 
      79             : RegionInfo::~RegionInfo() = default;
      80             : 
      81           0 : bool RegionInfo::invalidate(Function &F, const PreservedAnalyses &PA,
      82             :                             FunctionAnalysisManager::Invalidator &) {
      83             :   // Check whether the analysis, all analyses on functions, or the function's
      84             :   // CFG have been preserved.
      85             :   auto PAC = PA.getChecker<RegionInfoAnalysis>();
      86           0 :   return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
      87           0 :            PAC.preservedSet<CFGAnalyses>());
      88             : }
      89             : 
      90       29694 : void RegionInfo::updateStatistics(Region *R) {
      91             :   ++numRegions;
      92             : 
      93             :   // TODO: Slow. Should only be enabled if -stats is used.
      94       29694 :   if (R->isSimple())
      95             :     ++numSimpleRegions;
      96       29694 : }
      97             : 
      98       22865 : void RegionInfo::recalculate(Function &F, DominatorTree *DT_,
      99             :                              PostDominatorTree *PDT_, DominanceFrontier *DF_) {
     100       22865 :   DT = DT_;
     101       22865 :   PDT = PDT_;
     102       22865 :   DF = DF_;
     103             : 
     104       22865 :   TopLevelRegion = new Region(&F.getEntryBlock(), nullptr,
     105       22865 :                               this, DT, nullptr);
     106       22865 :   updateStatistics(TopLevelRegion);
     107       22865 :   calculate(F);
     108       22865 : }
     109             : 
     110             : #ifndef NDEBUG
     111             : void RegionInfo::view() { viewRegion(this); }
     112             : 
     113             : void RegionInfo::viewOnly() { viewRegionOnly(this); }
     114             : #endif
     115             : 
     116             : //===----------------------------------------------------------------------===//
     117             : // RegionInfoPass implementation
     118             : //
     119             : 
     120        9292 : RegionInfoPass::RegionInfoPass() : FunctionPass(ID) {
     121        4646 :   initializeRegionInfoPassPass(*PassRegistry::getPassRegistry());
     122        4646 : }
     123             : 
     124             : RegionInfoPass::~RegionInfoPass() = default;
     125             : 
     126       22857 : bool RegionInfoPass::runOnFunction(Function &F) {
     127       22857 :   releaseMemory();
     128             : 
     129       22857 :   auto DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
     130       22857 :   auto PDT = &getAnalysis<PostDominatorTreeWrapperPass>().getPostDomTree();
     131       22857 :   auto DF = &getAnalysis<DominanceFrontierWrapperPass>().getDominanceFrontier();
     132             : 
     133       22857 :   RI.recalculate(F, DT, PDT, DF);
     134       22857 :   return false;
     135             : }
     136             : 
     137       45669 : void RegionInfoPass::releaseMemory() {
     138       45669 :   RI.releaseMemory();
     139       45669 : }
     140             : 
     141           0 : void RegionInfoPass::verifyAnalysis() const {
     142           0 :     RI.verifyAnalysis();
     143           0 : }
     144             : 
     145        4646 : void RegionInfoPass::getAnalysisUsage(AnalysisUsage &AU) const {
     146             :   AU.setPreservesAll();
     147             :   AU.addRequiredTransitive<DominatorTreeWrapperPass>();
     148             :   AU.addRequired<PostDominatorTreeWrapperPass>();
     149             :   AU.addRequired<DominanceFrontierWrapperPass>();
     150        4646 : }
     151             : 
     152           8 : void RegionInfoPass::print(raw_ostream &OS, const Module *) const {
     153           8 :   RI.print(OS);
     154           8 : }
     155             : 
     156             : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
     157             : LLVM_DUMP_METHOD void RegionInfoPass::dump() const {
     158             :   RI.dump();
     159             : }
     160             : #endif
     161             : 
     162             : char RegionInfoPass::ID = 0;
     163             : 
     164       55571 : INITIALIZE_PASS_BEGIN(RegionInfoPass, "regions",
     165             :                 "Detect single entry single exit regions", true, true)
     166       55571 : INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
     167       55571 : INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
     168       55571 : INITIALIZE_PASS_DEPENDENCY(DominanceFrontierWrapperPass)
     169      484164 : INITIALIZE_PASS_END(RegionInfoPass, "regions",
     170             :                 "Detect single entry single exit regions", true, true)
     171             : 
     172             : // Create methods available outside of this file, to use them
     173             : // "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
     174             : // the link time optimization.
     175             : 
     176             : namespace llvm {
     177             : 
     178           0 :   FunctionPass *createRegionInfoPass() {
     179           0 :     return new RegionInfoPass();
     180             :   }
     181             : 
     182             : } // end namespace llvm
     183             : 
     184             : //===----------------------------------------------------------------------===//
     185             : // RegionInfoAnalysis implementation
     186             : //
     187             : 
     188             : AnalysisKey RegionInfoAnalysis::Key;
     189             : 
     190           8 : RegionInfo RegionInfoAnalysis::run(Function &F, FunctionAnalysisManager &AM) {
     191           8 :   RegionInfo RI;
     192             :   auto *DT = &AM.getResult<DominatorTreeAnalysis>(F);
     193             :   auto *PDT = &AM.getResult<PostDominatorTreeAnalysis>(F);
     194             :   auto *DF = &AM.getResult<DominanceFrontierAnalysis>(F);
     195             : 
     196           8 :   RI.recalculate(F, DT, PDT, DF);
     197           8 :   return RI;
     198             : }
     199             : 
     200           2 : RegionInfoPrinterPass::RegionInfoPrinterPass(raw_ostream &OS)
     201           2 :   : OS(OS) {}
     202             : 
     203           2 : PreservedAnalyses RegionInfoPrinterPass::run(Function &F,
     204             :                                              FunctionAnalysisManager &AM) {
     205           2 :   OS << "Region Tree for function: " << F.getName() << "\n";
     206           2 :   AM.getResult<RegionInfoAnalysis>(F).print(OS);
     207             : 
     208           2 :   return PreservedAnalyses::all();
     209             : }
     210             : 
     211           0 : PreservedAnalyses RegionInfoVerifierPass::run(Function &F,
     212             :                                               FunctionAnalysisManager &AM) {
     213           0 :   AM.getResult<RegionInfoAnalysis>(F).verifyAnalysis();
     214             : 
     215           0 :   return PreservedAnalyses::all();
     216      299229 : }

Generated by: LCOV version 1.13