LLVM 22.0.0git
RegionInfo.cpp
Go to the documentation of this file.
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
12#include "llvm/ADT/Statistic.h"
15#ifndef NDEBUG
17#endif
20#include "llvm/Config/llvm-config.h"
21#include "llvm/IR/Function.h"
24
25using namespace llvm;
26
27#define DEBUG_TYPE "region"
28
29namespace llvm {
30
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
44 "verify-region-info",
46 cl::desc("Verify region info (time consuming)"));
47
51 cl::desc("style of printing regions"),
53 clEnumValN(Region::PrintNone, "none", "print no details"),
55 "print regions in detail with block_iterator"),
57 "print regions in detail with element_iterator")));
58
59//===----------------------------------------------------------------------===//
60// Region implementation
61//
62
64 RegionInfo* RI,
65 DominatorTree *DT, Region *Parent) :
66 RegionBase<RegionTraits<Function>>(Entry, Exit, RI, DT, Parent) {
67
68}
69
70Region::~Region() = default;
71
72//===----------------------------------------------------------------------===//
73// RegionInfo implementation
74//
75
76RegionInfo::RegionInfo() = default;
77
78RegionInfo::~RegionInfo() = default;
79
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>());
87}
88
90 ++numRegions;
91
92 // TODO: Slow. Should only be enabled if -stats is used.
93 if (R->isSimple())
94 ++numSimpleRegions;
95}
96
99 DT = DT_;
100 PDT = PDT_;
101 DF = DF_;
102
103 TopLevelRegion = new Region(&F.getEntryBlock(), nullptr,
104 this, DT, nullptr);
105 updateStatistics(TopLevelRegion);
106 calculate(F);
107}
108
109#ifndef NDEBUG
111
113#endif
114
115//===----------------------------------------------------------------------===//
116// RegionInfoPass implementation
117//
118
120
122
125
126 auto DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
127 auto PDT = &getAnalysis<PostDominatorTreeWrapperPass>().getPostDomTree();
128 auto DF = &getAnalysis<DominanceFrontierWrapperPass>().getDominanceFrontier();
129
130 RI.recalculate(F, DT, PDT, DF);
131 return false;
132}
133
135 RI.releaseMemory();
136}
137
139 RI.verifyAnalysis();
140}
141
148
149void RegionInfoPass::print(raw_ostream &OS, const Module *) const {
150 RI.print(OS);
151}
152
153#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
155 RI.dump();
156}
157#endif
158
159char RegionInfoPass::ID = 0;
160
162 "Detect single entry single exit regions", true, true)
167 "Detect single entry single exit regions", true, true)
168
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.
172
173namespace llvm {
174
178
179} // end namespace llvm
180
181//===----------------------------------------------------------------------===//
182// RegionInfoAnalysis implementation
183//
184
185AnalysisKey RegionInfoAnalysis::Key;
186
188 RegionInfo RI;
189 auto *DT = &AM.getResult<DominatorTreeAnalysis>(F);
190 auto *PDT = &AM.getResult<PostDominatorTreeAnalysis>(F);
192
193 RI.recalculate(F, DT, PDT, DF);
194 return RI;
195}
196
199
202 OS << "Region Tree for function: " << F.getName() << "\n";
204
205 return PreservedAnalyses::all();
206}
207
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:638
static RegisterPass< DebugifyFunctionPass > DF("debugify-function", "Attach debug info to a function")
#define F(x, y, z)
Definition MD5.cpp:55
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
static cl::opt< bool, true > VerifyRegionInfoX("verify-region-info", cl::location(RegionInfoBase< RegionTraits< Function > >::VerifyRegionInfo), cl::desc("Verify region info (time consuming)"))
static cl::opt< Region::PrintStyle, true > printStyleX("print-region-style", cl::location(RegionInfo::printStyle), cl::Hidden, cl::desc("style of printing regions"), cl::values(clEnumValN(Region::PrintNone, "none", "print no details"), clEnumValN(Region::PrintBB, "bb", "print regions in detail with block_iterator"), clEnumValN(Region::PrintRN, "rn", "print regions in detail with element_iterator")))
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:167
This templated class represents "all analyses that operate over <aparticular IR unit>" (e....
Definition Analysis.h:50
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
void setPreservesAll()
Set by analyses that do not transform their input at all.
AnalysisUsage & addRequiredTransitive()
LLVM Basic Block Representation.
Definition BasicBlock.h:62
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
Analysis pass which computes a DominanceFrontier.
void print(raw_ostream &OS, const Module *=nullptr) const override
print - Print out the internal state of the pass.
Analysis pass which computes a DominatorTree.
Definition Dominators.h:284
Legacy analysis pass which computes a DominatorTree.
Definition Dominators.h:322
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:165
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
FunctionPass(char &pid)
Definition Pass.h:316
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information ...
Analysis pass which computes a PostDominatorTree.
PostDominatorTree Class - Concrete subclass of DominatorTree that is used to compute the post-dominat...
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalysisChecker getChecker() const
Build a checker for this PreservedAnalyses and the specified analysis type.
Definition Analysis.h:275
A single entry single exit Region.
Definition RegionInfo.h:252
RegionBase(BlockT *Entry, BlockT *Exit, RegionInfoT *RI, DomTreeT *DT, RegionT *Parent=nullptr)
Analysis pass that exposes the RegionInfo for a function.
Definition RegionInfo.h:965
RegionInfo run(Function &F, FunctionAnalysisManager &AM)
Analysis that detects all canonical Regions.
Definition RegionInfo.h:672
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
~RegionInfoPass() override
void verifyAnalysis() const override
verifyAnalysis() - This member can be implemented by a analysis pass to check state of analysis infor...
void print(raw_ostream &OS, const Module *) const override
print - Print out the internal state of the pass.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
RegionInfoPrinterPass(raw_ostream &OS)
void view()
Opens a viewer to show the GraphViz visualization of the regions.
~RegionInfo() override
void viewOnly()
Opens a viewer to show the GraphViz visualization of this region without instructions in the BasicBlo...
void recalculate(Function &F, DominatorTree *DT, PostDominatorTree *PDT, DominanceFrontier *DF)
void updateStatistics(Region *R) final
bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &)
Handle invalidation explicitly.
A RegionNode represents a subregion or a BasicBlock that is part of a Region.
Definition RegionInfo.h:115
Region(BasicBlock *Entry, BasicBlock *Exit, RegionInfo *RI, DominatorTree *DT, Region *Parent=nullptr)
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
LocationClass< Ty > location(Ty &L)
This is an optimization pass for GlobalISel generic memory operations.
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr, unsigned DynamicVGPRBlockSize=0)
bool RegionInfoBase< Tr >::VerifyRegionInfo
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
LLVM_ABI FunctionPass * createRegionInfoPass()
void viewRegion(llvm::RegionInfo *RI)
Open a viewer to display the GraphViz vizualization of the analysis result.
void viewRegionOnly(llvm::RegionInfo *RI)
Open a viewer to display the GraphViz vizualization of the analysis result.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)