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