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/Support/CommandLine.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include <algorithm>
22 #include <iterator>
23 #include <set>
24
25 using namespace llvm;
26
27 #define DEBUG_TYPE "region"
28
29 namespace llvm {
30 template class RegionBase<RegionTraits<Function>>;
31 template class RegionNodeBase<RegionTraits<Function>>;
32 template class RegionInfoBase<RegionTraits<Function>>;
33 }
34
35 STATISTIC(numRegions, "The # of regions");
36 STATISTIC(numSimpleRegions, "The # of simple regions");
37
38 // Always verify if expensive checking is enabled.
39
40 static cl::opt<bool,true>
41 VerifyRegionInfoX(
42 "verify-region-info",
43 cl::location(RegionInfoBase<RegionTraits<Function>>::VerifyRegionInfo),
44 cl::desc("Verify region info (time consuming)"));
45
46
47 static cl::opt<Region::PrintStyle, true> printStyleX("print-region-style",
48 cl::location(RegionInfo::printStyle),
49 cl::Hidden,
50 cl::desc("style of printing regions"),
51 cl::values(
52 clEnumValN(Region::PrintNone, "none", "print no details"),
53 clEnumValN(Region::PrintBB, "bb",
54 "print regions in detail with block_iterator"),
55 clEnumValN(Region::PrintRN, "rn",
56 "print regions in detail with element_iterator"),
57 clEnumValEnd));
58
59
60 //===----------------------------------------------------------------------===//
61 // Region implementation
62 //
63
Region(BasicBlock * Entry,BasicBlock * Exit,RegionInfo * RI,DominatorTree * DT,Region * Parent)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
~Region()71 Region::~Region() { }
72
73 //===----------------------------------------------------------------------===//
74 // RegionInfo implementation
75 //
76
RegionInfo()77 RegionInfo::RegionInfo() :
78 RegionInfoBase<RegionTraits<Function>>() {
79
80 }
81
~RegionInfo()82 RegionInfo::~RegionInfo() {
83
84 }
85
updateStatistics(Region * R)86 void RegionInfo::updateStatistics(Region *R) {
87 ++numRegions;
88
89 // TODO: Slow. Should only be enabled if -stats is used.
90 if (R->isSimple())
91 ++numSimpleRegions;
92 }
93
recalculate(Function & F,DominatorTree * DT_,PostDominatorTree * PDT_,DominanceFrontier * DF_)94 void RegionInfo::recalculate(Function &F, DominatorTree *DT_,
95 PostDominatorTree *PDT_, DominanceFrontier *DF_) {
96 DT = DT_;
97 PDT = PDT_;
98 DF = DF_;
99
100 TopLevelRegion = new Region(&F.getEntryBlock(), nullptr,
101 this, DT, nullptr);
102 updateStatistics(TopLevelRegion);
103 calculate(F);
104 }
105
106 //===----------------------------------------------------------------------===//
107 // RegionInfoPass implementation
108 //
109
RegionInfoPass()110 RegionInfoPass::RegionInfoPass() : FunctionPass(ID) {
111 initializeRegionInfoPassPass(*PassRegistry::getPassRegistry());
112 }
113
~RegionInfoPass()114 RegionInfoPass::~RegionInfoPass() {
115
116 }
117
runOnFunction(Function & F)118 bool RegionInfoPass::runOnFunction(Function &F) {
119 releaseMemory();
120
121 auto DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
122 auto PDT = &getAnalysis<PostDominatorTree>();
123 auto DF = &getAnalysis<DominanceFrontier>();
124
125 RI.recalculate(F, DT, PDT, DF);
126 return false;
127 }
128
releaseMemory()129 void RegionInfoPass::releaseMemory() {
130 RI.releaseMemory();
131 }
132
verifyAnalysis() const133 void RegionInfoPass::verifyAnalysis() const {
134 RI.verifyAnalysis();
135 }
136
getAnalysisUsage(AnalysisUsage & AU) const137 void RegionInfoPass::getAnalysisUsage(AnalysisUsage &AU) const {
138 AU.setPreservesAll();
139 AU.addRequiredTransitive<DominatorTreeWrapperPass>();
140 AU.addRequired<PostDominatorTree>();
141 AU.addRequired<DominanceFrontier>();
142 }
143
print(raw_ostream & OS,const Module *) const144 void RegionInfoPass::print(raw_ostream &OS, const Module *) const {
145 RI.print(OS);
146 }
147
148 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const149 void RegionInfoPass::dump() const {
150 RI.dump();
151 }
152 #endif
153
154 char RegionInfoPass::ID = 0;
155
156 INITIALIZE_PASS_BEGIN(RegionInfoPass, "regions",
157 "Detect single entry single exit regions", true, true)
158 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
159 INITIALIZE_PASS_DEPENDENCY(PostDominatorTree)
160 INITIALIZE_PASS_DEPENDENCY(DominanceFrontier)
161 INITIALIZE_PASS_END(RegionInfoPass, "regions",
162 "Detect single entry single exit regions", true, true)
163
164 // Create methods available outside of this file, to use them
165 // "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
166 // the link time optimization.
167
168 namespace llvm {
createRegionInfoPass()169 FunctionPass *createRegionInfoPass() {
170 return new RegionInfoPass();
171 }
172 }
173
174