1 //===- RegionInfo.cpp - SESE region detection analysis --------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
8 // Detects single entry single exit regions in the control flow graph.
9 //===----------------------------------------------------------------------===//
11 #include "llvm/Analysis/RegionInfo.h"
12 #include "llvm/ADT/Statistic.h"
14 #include "llvm/Analysis/RegionPrinter.h"
16 #include "llvm/Analysis/RegionInfoImpl.h"
17 #include "llvm/Config/llvm-config.h"
18 #include "llvm/IR/Function.h"
19 #include "llvm/IR/PassManager.h"
20 #include "llvm/Pass.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/Compiler.h"
23 #include "llvm/Support/raw_ostream.h"
27 #define DEBUG_TYPE "region"
31 template class RegionBase
<RegionTraits
<Function
>>;
32 template class RegionNodeBase
<RegionTraits
<Function
>>;
33 template class RegionInfoBase
<RegionTraits
<Function
>>;
35 } // end namespace llvm
37 STATISTIC(numRegions
, "The # of regions");
38 STATISTIC(numSimpleRegions
, "The # of simple regions");
40 // Always verify if expensive checking is enabled.
42 static cl::opt
<bool,true>
45 cl::location(RegionInfoBase
<RegionTraits
<Function
>>::VerifyRegionInfo
),
46 cl::desc("Verify region info (time consuming)"));
48 static cl::opt
<Region::PrintStyle
, true> printStyleX("print-region-style",
49 cl::location(RegionInfo::printStyle
),
51 cl::desc("style of printing regions"),
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")));
59 //===----------------------------------------------------------------------===//
60 // Region implementation
63 Region::Region(BasicBlock
*Entry
, BasicBlock
*Exit
,
65 DominatorTree
*DT
, Region
*Parent
) :
66 RegionBase
<RegionTraits
<Function
>>(Entry
, Exit
, RI
, DT
, Parent
) {
70 Region::~Region() = default;
72 //===----------------------------------------------------------------------===//
73 // RegionInfo implementation
76 RegionInfo::RegionInfo() = default;
78 RegionInfo::~RegionInfo() = default;
80 bool RegionInfo::invalidate(Function
&F
, const PreservedAnalyses
&PA
,
81 FunctionAnalysisManager::Invalidator
&) {
82 // Check whether the analysis, all analyses on functions, or the function's
83 // CFG has been preserved.
84 auto PAC
= PA
.getChecker
<RegionInfoAnalysis
>();
85 return !(PAC
.preserved() || PAC
.preservedSet
<AllAnalysesOn
<Function
>>() ||
86 PAC
.preservedSet
<CFGAnalyses
>());
89 void RegionInfo::updateStatistics(Region
*R
) {
92 // TODO: Slow. Should only be enabled if -stats is used.
97 void RegionInfo::recalculate(Function
&F
, DominatorTree
*DT_
,
98 PostDominatorTree
*PDT_
, DominanceFrontier
*DF_
) {
103 TopLevelRegion
= new Region(&F
.getEntryBlock(), nullptr,
105 updateStatistics(TopLevelRegion
);
110 void RegionInfo::view() { viewRegion(this); }
112 void RegionInfo::viewOnly() { viewRegionOnly(this); }
115 //===----------------------------------------------------------------------===//
116 // RegionInfoPass implementation
119 RegionInfoPass::RegionInfoPass() : FunctionPass(ID
) {
120 initializeRegionInfoPassPass(*PassRegistry::getPassRegistry());
123 RegionInfoPass::~RegionInfoPass() = default;
125 bool RegionInfoPass::runOnFunction(Function
&F
) {
128 auto DT
= &getAnalysis
<DominatorTreeWrapperPass
>().getDomTree();
129 auto PDT
= &getAnalysis
<PostDominatorTreeWrapperPass
>().getPostDomTree();
130 auto DF
= &getAnalysis
<DominanceFrontierWrapperPass
>().getDominanceFrontier();
132 RI
.recalculate(F
, DT
, PDT
, DF
);
136 void RegionInfoPass::releaseMemory() {
140 void RegionInfoPass::verifyAnalysis() const {
144 void RegionInfoPass::getAnalysisUsage(AnalysisUsage
&AU
) const {
145 AU
.setPreservesAll();
146 AU
.addRequiredTransitive
<DominatorTreeWrapperPass
>();
147 AU
.addRequired
<PostDominatorTreeWrapperPass
>();
148 AU
.addRequired
<DominanceFrontierWrapperPass
>();
151 void RegionInfoPass::print(raw_ostream
&OS
, const Module
*) const {
155 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
156 LLVM_DUMP_METHOD
void RegionInfoPass::dump() const {
161 char RegionInfoPass::ID
= 0;
163 INITIALIZE_PASS_BEGIN(RegionInfoPass
, "regions",
164 "Detect single entry single exit regions", true, true)
165 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass
)
166 INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass
)
167 INITIALIZE_PASS_DEPENDENCY(DominanceFrontierWrapperPass
)
168 INITIALIZE_PASS_END(RegionInfoPass
, "regions",
169 "Detect single entry single exit regions", true, true)
171 // Create methods available outside of this file, to use them
172 // "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
173 // the link time optimization.
177 FunctionPass
*createRegionInfoPass() {
178 return new RegionInfoPass();
181 } // end namespace llvm
183 //===----------------------------------------------------------------------===//
184 // RegionInfoAnalysis implementation
187 AnalysisKey
RegionInfoAnalysis::Key
;
189 RegionInfo
RegionInfoAnalysis::run(Function
&F
, FunctionAnalysisManager
&AM
) {
191 auto *DT
= &AM
.getResult
<DominatorTreeAnalysis
>(F
);
192 auto *PDT
= &AM
.getResult
<PostDominatorTreeAnalysis
>(F
);
193 auto *DF
= &AM
.getResult
<DominanceFrontierAnalysis
>(F
);
195 RI
.recalculate(F
, DT
, PDT
, DF
);
199 RegionInfoPrinterPass::RegionInfoPrinterPass(raw_ostream
&OS
)
202 PreservedAnalyses
RegionInfoPrinterPass::run(Function
&F
,
203 FunctionAnalysisManager
&AM
) {
204 OS
<< "Region Tree for function: " << F
.getName() << "\n";
205 AM
.getResult
<RegionInfoAnalysis
>(F
).print(OS
);
207 return PreservedAnalyses::all();
210 PreservedAnalyses
RegionInfoVerifierPass::run(Function
&F
,
211 FunctionAnalysisManager
&AM
) {
212 AM
.getResult
<RegionInfoAnalysis
>(F
).verifyAnalysis();
214 return PreservedAnalyses::all();