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