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

Generated by: LCOV version 1.13