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"
13 #include "llvm/InitializePasses.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/Support/CommandLine.h"
21 #include "llvm/Support/Compiler.h"
25 #define DEBUG_TYPE "region"
29 template class RegionBase
<RegionTraits
<Function
>>;
30 template class RegionNodeBase
<RegionTraits
<Function
>>;
31 template class RegionInfoBase
<RegionTraits
<Function
>>;
33 } // end namespace llvm
35 STATISTIC(numRegions
, "The # of regions");
36 STATISTIC(numSimpleRegions
, "The # of simple regions");
38 // Always verify if expensive checking is enabled.
40 static cl::opt
<bool,true>
43 cl::location(RegionInfoBase
<RegionTraits
<Function
>>::VerifyRegionInfo
),
44 cl::desc("Verify region info (time consuming)"));
46 static cl::opt
<Region::PrintStyle
, true> printStyleX("print-region-style",
47 cl::location(RegionInfo::printStyle
),
49 cl::desc("style of printing regions"),
51 clEnumValN(Region::PrintNone
, "none", "print no details"),
52 clEnumValN(Region::PrintBB
, "bb",
53 "print regions in detail with block_iterator"),
54 clEnumValN(Region::PrintRN
, "rn",
55 "print regions in detail with element_iterator")));
57 //===----------------------------------------------------------------------===//
58 // Region implementation
61 Region::Region(BasicBlock
*Entry
, BasicBlock
*Exit
,
63 DominatorTree
*DT
, Region
*Parent
) :
64 RegionBase
<RegionTraits
<Function
>>(Entry
, Exit
, RI
, DT
, Parent
) {
68 Region::~Region() = default;
70 //===----------------------------------------------------------------------===//
71 // RegionInfo implementation
74 RegionInfo::RegionInfo() = default;
76 RegionInfo::~RegionInfo() = default;
78 bool RegionInfo::invalidate(Function
&F
, const PreservedAnalyses
&PA
,
79 FunctionAnalysisManager::Invalidator
&) {
80 // Check whether the analysis, all analyses on functions, or the function's
81 // CFG has been preserved.
82 auto PAC
= PA
.getChecker
<RegionInfoAnalysis
>();
83 return !(PAC
.preserved() || PAC
.preservedSet
<AllAnalysesOn
<Function
>>() ||
84 PAC
.preservedSet
<CFGAnalyses
>());
87 void RegionInfo::updateStatistics(Region
*R
) {
90 // TODO: Slow. Should only be enabled if -stats is used.
95 void RegionInfo::recalculate(Function
&F
, DominatorTree
*DT_
,
96 PostDominatorTree
*PDT_
, DominanceFrontier
*DF_
) {
101 TopLevelRegion
= new Region(&F
.getEntryBlock(), nullptr,
103 updateStatistics(TopLevelRegion
);
108 void RegionInfo::view() { viewRegion(this); }
110 void RegionInfo::viewOnly() { viewRegionOnly(this); }
113 //===----------------------------------------------------------------------===//
114 // RegionInfoPass implementation
117 RegionInfoPass::RegionInfoPass() : FunctionPass(ID
) {
118 initializeRegionInfoPassPass(*PassRegistry::getPassRegistry());
121 RegionInfoPass::~RegionInfoPass() = default;
123 bool RegionInfoPass::runOnFunction(Function
&F
) {
126 auto DT
= &getAnalysis
<DominatorTreeWrapperPass
>().getDomTree();
127 auto PDT
= &getAnalysis
<PostDominatorTreeWrapperPass
>().getPostDomTree();
128 auto DF
= &getAnalysis
<DominanceFrontierWrapperPass
>().getDominanceFrontier();
130 RI
.recalculate(F
, DT
, PDT
, DF
);
134 void RegionInfoPass::releaseMemory() {
138 void RegionInfoPass::verifyAnalysis() const {
142 void RegionInfoPass::getAnalysisUsage(AnalysisUsage
&AU
) const {
143 AU
.setPreservesAll();
144 AU
.addRequiredTransitive
<DominatorTreeWrapperPass
>();
145 AU
.addRequired
<PostDominatorTreeWrapperPass
>();
146 AU
.addRequired
<DominanceFrontierWrapperPass
>();
149 void RegionInfoPass::print(raw_ostream
&OS
, const Module
*) const {
153 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
154 LLVM_DUMP_METHOD
void RegionInfoPass::dump() const {
159 char RegionInfoPass::ID
= 0;
161 INITIALIZE_PASS_BEGIN(RegionInfoPass
, "regions",
162 "Detect single entry single exit regions", true, true)
163 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass
)
164 INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass
)
165 INITIALIZE_PASS_DEPENDENCY(DominanceFrontierWrapperPass
)
166 INITIALIZE_PASS_END(RegionInfoPass
, "regions",
167 "Detect single entry single exit regions", true, true)
169 // Create methods available outside of this file, to use them
170 // "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
171 // the link time optimization.
175 FunctionPass
*createRegionInfoPass() {
176 return new RegionInfoPass();
179 } // end namespace llvm
181 //===----------------------------------------------------------------------===//
182 // RegionInfoAnalysis implementation
185 AnalysisKey
RegionInfoAnalysis::Key
;
187 RegionInfo
RegionInfoAnalysis::run(Function
&F
, FunctionAnalysisManager
&AM
) {
189 auto *DT
= &AM
.getResult
<DominatorTreeAnalysis
>(F
);
190 auto *PDT
= &AM
.getResult
<PostDominatorTreeAnalysis
>(F
);
191 auto *DF
= &AM
.getResult
<DominanceFrontierAnalysis
>(F
);
193 RI
.recalculate(F
, DT
, PDT
, DF
);
197 RegionInfoPrinterPass::RegionInfoPrinterPass(raw_ostream
&OS
)
200 PreservedAnalyses
RegionInfoPrinterPass::run(Function
&F
,
201 FunctionAnalysisManager
&AM
) {
202 OS
<< "Region Tree for function: " << F
.getName() << "\n";
203 AM
.getResult
<RegionInfoAnalysis
>(F
).print(OS
);
205 return PreservedAnalyses::all();
208 PreservedAnalyses
RegionInfoVerifierPass::run(Function
&F
,
209 FunctionAnalysisManager
&AM
) {
210 AM
.getResult
<RegionInfoAnalysis
>(F
).verifyAnalysis();
212 return PreservedAnalyses::all();