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