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