1 //===- RegionInfo.cpp - SESE region detection analysis --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
9 // Detects single entry single exit regions in the control flow graph.
10 //===----------------------------------------------------------------------===//
12 #include "llvm/Analysis/RegionInfo.h"
13 #include "llvm/ADT/Statistic.h"
15 #include "llvm/Analysis/RegionPrinter.h"
17 #include "llvm/Analysis/RegionInfoImpl.h"
18 #include "llvm/Config/llvm-config.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/PassManager.h"
21 #include "llvm/Pass.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/Compiler.h"
24 #include "llvm/Support/raw_ostream.h"
28 #define DEBUG_TYPE "region"
32 template class RegionBase
<RegionTraits
<Function
>>;
33 template class RegionNodeBase
<RegionTraits
<Function
>>;
34 template class RegionInfoBase
<RegionTraits
<Function
>>;
36 } // end namespace llvm
38 STATISTIC(numRegions
, "The # of regions");
39 STATISTIC(numSimpleRegions
, "The # of simple regions");
41 // Always verify if expensive checking is enabled.
43 static cl::opt
<bool,true>
46 cl::location(RegionInfoBase
<RegionTraits
<Function
>>::VerifyRegionInfo
),
47 cl::desc("Verify region info (time consuming)"));
49 static cl::opt
<Region::PrintStyle
, true> printStyleX("print-region-style",
50 cl::location(RegionInfo::printStyle
),
52 cl::desc("style of printing regions"),
54 clEnumValN(Region::PrintNone
, "none", "print no details"),
55 clEnumValN(Region::PrintBB
, "bb",
56 "print regions in detail with block_iterator"),
57 clEnumValN(Region::PrintRN
, "rn",
58 "print regions in detail with element_iterator")));
60 //===----------------------------------------------------------------------===//
61 // Region implementation
64 Region::Region(BasicBlock
*Entry
, BasicBlock
*Exit
,
66 DominatorTree
*DT
, Region
*Parent
) :
67 RegionBase
<RegionTraits
<Function
>>(Entry
, Exit
, RI
, DT
, Parent
) {
71 Region::~Region() = default;
73 //===----------------------------------------------------------------------===//
74 // RegionInfo implementation
77 RegionInfo::RegionInfo() = default;
79 RegionInfo::~RegionInfo() = default;
81 bool RegionInfo::invalidate(Function
&F
, const PreservedAnalyses
&PA
,
82 FunctionAnalysisManager::Invalidator
&) {
83 // Check whether the analysis, all analyses on functions, or the function's
84 // CFG has been preserved.
85 auto PAC
= PA
.getChecker
<RegionInfoAnalysis
>();
86 return !(PAC
.preserved() || PAC
.preservedSet
<AllAnalysesOn
<Function
>>() ||
87 PAC
.preservedSet
<CFGAnalyses
>());
90 void RegionInfo::updateStatistics(Region
*R
) {
93 // TODO: Slow. Should only be enabled if -stats is used.
98 void RegionInfo::recalculate(Function
&F
, DominatorTree
*DT_
,
99 PostDominatorTree
*PDT_
, DominanceFrontier
*DF_
) {
104 TopLevelRegion
= new Region(&F
.getEntryBlock(), nullptr,
106 updateStatistics(TopLevelRegion
);
111 void RegionInfo::view() { viewRegion(this); }
113 void RegionInfo::viewOnly() { viewRegionOnly(this); }
116 //===----------------------------------------------------------------------===//
117 // RegionInfoPass implementation
120 RegionInfoPass::RegionInfoPass() : FunctionPass(ID
) {
121 initializeRegionInfoPassPass(*PassRegistry::getPassRegistry());
124 RegionInfoPass::~RegionInfoPass() = default;
126 bool RegionInfoPass::runOnFunction(Function
&F
) {
129 auto DT
= &getAnalysis
<DominatorTreeWrapperPass
>().getDomTree();
130 auto PDT
= &getAnalysis
<PostDominatorTreeWrapperPass
>().getPostDomTree();
131 auto DF
= &getAnalysis
<DominanceFrontierWrapperPass
>().getDominanceFrontier();
133 RI
.recalculate(F
, DT
, PDT
, DF
);
137 void RegionInfoPass::releaseMemory() {
141 void RegionInfoPass::verifyAnalysis() const {
145 void RegionInfoPass::getAnalysisUsage(AnalysisUsage
&AU
) const {
146 AU
.setPreservesAll();
147 AU
.addRequiredTransitive
<DominatorTreeWrapperPass
>();
148 AU
.addRequired
<PostDominatorTreeWrapperPass
>();
149 AU
.addRequired
<DominanceFrontierWrapperPass
>();
152 void RegionInfoPass::print(raw_ostream
&OS
, const Module
*) const {
156 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
157 LLVM_DUMP_METHOD
void RegionInfoPass::dump() const {
162 char RegionInfoPass::ID
= 0;
164 INITIALIZE_PASS_BEGIN(RegionInfoPass
, "regions",
165 "Detect single entry single exit regions", true, true)
166 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass
)
167 INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass
)
168 INITIALIZE_PASS_DEPENDENCY(DominanceFrontierWrapperPass
)
169 INITIALIZE_PASS_END(RegionInfoPass
, "regions",
170 "Detect single entry single exit regions", true, true)
172 // Create methods available outside of this file, to use them
173 // "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
174 // the link time optimization.
178 FunctionPass
*createRegionInfoPass() {
179 return new RegionInfoPass();
182 } // end namespace llvm
184 //===----------------------------------------------------------------------===//
185 // RegionInfoAnalysis implementation
188 AnalysisKey
RegionInfoAnalysis::Key
;
190 RegionInfo
RegionInfoAnalysis::run(Function
&F
, FunctionAnalysisManager
&AM
) {
192 auto *DT
= &AM
.getResult
<DominatorTreeAnalysis
>(F
);
193 auto *PDT
= &AM
.getResult
<PostDominatorTreeAnalysis
>(F
);
194 auto *DF
= &AM
.getResult
<DominanceFrontierAnalysis
>(F
);
196 RI
.recalculate(F
, DT
, PDT
, DF
);
200 RegionInfoPrinterPass::RegionInfoPrinterPass(raw_ostream
&OS
)
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
);
208 return PreservedAnalyses::all();
211 PreservedAnalyses
RegionInfoVerifierPass::run(Function
&F
,
212 FunctionAnalysisManager
&AM
) {
213 AM
.getResult
<RegionInfoAnalysis
>(F
).verifyAnalysis();
215 return PreservedAnalyses::all();