LLVM  mainline
RegionInfo.cpp
Go to the documentation of this file.
00001 //===- RegionInfo.cpp - SESE region detection analysis --------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 // Detects single entry single exit regions in the control flow graph.
00010 //===----------------------------------------------------------------------===//
00011 
00012 #include "llvm/Analysis/RegionInfo.h"
00013 #include "llvm/ADT/PostOrderIterator.h"
00014 #include "llvm/ADT/Statistic.h"
00015 #include "llvm/Analysis/LoopInfo.h"
00016 #include "llvm/Analysis/RegionInfoImpl.h"
00017 #include "llvm/Analysis/RegionIterator.h"
00018 #include "llvm/Support/CommandLine.h"
00019 #include "llvm/Support/Debug.h"
00020 #include "llvm/Support/ErrorHandling.h"
00021 #include <algorithm>
00022 #include <iterator>
00023 #include <set>
00024 #ifndef NDEBUG
00025 #include "llvm/Analysis/RegionPrinter.h"
00026 #endif
00027 
00028 using namespace llvm;
00029 
00030 #define DEBUG_TYPE "region"
00031 
00032 namespace llvm {
00033 template class RegionBase<RegionTraits<Function>>;
00034 template class RegionNodeBase<RegionTraits<Function>>;
00035 template class RegionInfoBase<RegionTraits<Function>>;
00036 }
00037 
00038 STATISTIC(numRegions,       "The # of regions");
00039 STATISTIC(numSimpleRegions, "The # of simple regions");
00040 
00041 // Always verify if expensive checking is enabled.
00042 
00043 static cl::opt<bool,true>
00044 VerifyRegionInfoX(
00045   "verify-region-info",
00046   cl::location(RegionInfoBase<RegionTraits<Function>>::VerifyRegionInfo),
00047   cl::desc("Verify region info (time consuming)"));
00048 
00049 
00050 static cl::opt<Region::PrintStyle, true> printStyleX("print-region-style",
00051   cl::location(RegionInfo::printStyle),
00052   cl::Hidden,
00053   cl::desc("style of printing regions"),
00054   cl::values(
00055     clEnumValN(Region::PrintNone, "none",  "print no details"),
00056     clEnumValN(Region::PrintBB, "bb",
00057                "print regions in detail with block_iterator"),
00058     clEnumValN(Region::PrintRN, "rn",
00059                "print regions in detail with element_iterator"),
00060     clEnumValEnd));
00061 
00062 
00063 //===----------------------------------------------------------------------===//
00064 // Region implementation
00065 //
00066 
00067 Region::Region(BasicBlock *Entry, BasicBlock *Exit,
00068                RegionInfo* RI,
00069                DominatorTree *DT, Region *Parent) :
00070   RegionBase<RegionTraits<Function>>(Entry, Exit, RI, DT, Parent) {
00071 
00072 }
00073 
00074 Region::~Region() { }
00075 
00076 //===----------------------------------------------------------------------===//
00077 // RegionInfo implementation
00078 //
00079 
00080 RegionInfo::RegionInfo() :
00081   RegionInfoBase<RegionTraits<Function>>() {
00082 
00083 }
00084 
00085 RegionInfo::~RegionInfo() {
00086 
00087 }
00088 
00089 void RegionInfo::updateStatistics(Region *R) {
00090   ++numRegions;
00091 
00092   // TODO: Slow. Should only be enabled if -stats is used.
00093   if (R->isSimple())
00094     ++numSimpleRegions;
00095 }
00096 
00097 void RegionInfo::recalculate(Function &F, DominatorTree *DT_,
00098                              PostDominatorTree *PDT_, DominanceFrontier *DF_) {
00099   DT = DT_;
00100   PDT = PDT_;
00101   DF = DF_;
00102 
00103   TopLevelRegion = new Region(&F.getEntryBlock(), nullptr,
00104                               this, DT, nullptr);
00105   updateStatistics(TopLevelRegion);
00106   calculate(F);
00107 }
00108 
00109 #ifndef NDEBUG
00110 void RegionInfo::view() { viewRegion(this); }
00111 
00112 void RegionInfo::viewOnly() { viewRegionOnly(this); }
00113 #endif
00114 
00115 //===----------------------------------------------------------------------===//
00116 // RegionInfoPass implementation
00117 //
00118 
00119 RegionInfoPass::RegionInfoPass() : FunctionPass(ID) {
00120   initializeRegionInfoPassPass(*PassRegistry::getPassRegistry());
00121 }
00122 
00123 RegionInfoPass::~RegionInfoPass() {
00124 
00125 }
00126 
00127 bool RegionInfoPass::runOnFunction(Function &F) {
00128   releaseMemory();
00129 
00130   auto DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
00131   auto PDT = &getAnalysis<PostDominatorTree>();
00132   auto DF = &getAnalysis<DominanceFrontier>();
00133 
00134   RI.recalculate(F, DT, PDT, DF);
00135   return false;
00136 }
00137 
00138 void RegionInfoPass::releaseMemory() {
00139   RI.releaseMemory();
00140 }
00141 
00142 void RegionInfoPass::verifyAnalysis() const {
00143     RI.verifyAnalysis();
00144 }
00145 
00146 void RegionInfoPass::getAnalysisUsage(AnalysisUsage &AU) const {
00147   AU.setPreservesAll();
00148   AU.addRequiredTransitive<DominatorTreeWrapperPass>();
00149   AU.addRequired<PostDominatorTree>();
00150   AU.addRequired<DominanceFrontier>();
00151 }
00152 
00153 void RegionInfoPass::print(raw_ostream &OS, const Module *) const {
00154   RI.print(OS);
00155 }
00156 
00157 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
00158 void RegionInfoPass::dump() const {
00159   RI.dump();
00160 }
00161 #endif
00162 
00163 char RegionInfoPass::ID = 0;
00164 
00165 INITIALIZE_PASS_BEGIN(RegionInfoPass, "regions",
00166                 "Detect single entry single exit regions", true, true)
00167 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
00168 INITIALIZE_PASS_DEPENDENCY(PostDominatorTree)
00169 INITIALIZE_PASS_DEPENDENCY(DominanceFrontier)
00170 INITIALIZE_PASS_END(RegionInfoPass, "regions",
00171                 "Detect single entry single exit regions", true, true)
00172 
00173 // Create methods available outside of this file, to use them
00174 // "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
00175 // the link time optimization.
00176 
00177 namespace llvm {
00178   FunctionPass *createRegionInfoPass() {
00179     return new RegionInfoPass();
00180   }
00181 }
00182