LLVM 24.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// RegionTraits<Function> implementation
61//
62
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
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
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
103 DT = DT_;
104 PDT = PDT_;
105 DF = DF_;
106
107 TopLevelRegion = new Region(&F.getEntryBlock(), nullptr,
108 this, DT, nullptr);
110 calculate(F);
111}
112
113#ifndef NDEBUG
115
117#endif
118
119//===----------------------------------------------------------------------===//
120// RegionInfoPass implementation
121//
122
124
126
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, PDT, DF);
135 return false;
136}
137
139 RI.releaseMemory();
140}
141
143 RI.verifyAnalysis();
144}
145
152
153void RegionInfoPass::print(raw_ostream &OS, const Module *) const {
154 RI.print(OS);
155}
156
157#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
159 RI.dump();
160}
161#endif
162
163char RegionInfoPass::ID = 0;
164
166 "Detect single entry single exit regions", true, true)
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
182
183} // end namespace llvm
184
185//===----------------------------------------------------------------------===//
186// RegionInfoAnalysis implementation
187//
188
189AnalysisKey RegionInfoAnalysis::Key;
190
192 RegionInfo RI;
193 auto *DT = &AM.getResult<DominatorTreeAnalysis>(F);
194 auto *PDT = &AM.getResult<PostDominatorTreeAnalysis>(F);
196
197 RI.recalculate(F, DT, PDT, DF);
198 return RI;
199}
200
203
206 OS << "Region Tree for function: " << F.getName() << "\n";
208
209 return PreservedAnalyses::all();
210}
211
#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:686
#define LLVM_EXPORT_TEMPLATE
Definition Compiler.h:217
static RegisterPass< DebugifyFunctionPass > DF("debugify-function", "Attach debug info to a function")
#define F(x, y, z)
Definition MD5.cpp:54
#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:171
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:241
Legacy analysis pass which computes a DominatorTree.
Definition Dominators.h:277
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:122
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:68
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:255
RegionBase(BlockT *Entry, BlockT *Exit, RegionInfoT *RI, DomTreeT *DT, RegionT *Parent=nullptr)
Analysis pass that exposes the RegionInfo for a function.
Definition RegionInfo.h:968
LLVM_ABI RegionInfo run(Function &F, FunctionAnalysisManager &AM)
Analysis that detects all canonical Regions.
Definition RegionInfo.h:675
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...
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
LLVM_ABI RegionInfoPrinterPass(raw_ostream &OS)
LLVM_ABI RegionInfo()
LLVM_ABI void view()
Opens a viewer to show the GraphViz visualization of the regions.
LLVM_ABI void viewOnly()
Opens a viewer to show the GraphViz visualization of this region without instructions in the BasicBlo...
LLVM_ABI void recalculate(Function &F, DominatorTree *DT, PostDominatorTree *PDT, DominanceFrontier *DF)
LLVM_ABI bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &)
Handle invalidation explicitly.
LLVM_ABI ~RegionInfo()
A RegionNode represents a subregion or a BasicBlock that is part of a Region.
Definition RegionInfo.h:118
LLVM_ABI ~Region()
LLVM_ABI 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
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)