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