LLVM 19.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
19#include "llvm/Config/llvm-config.h"
20#include "llvm/IR/Function.h"
23
24using namespace llvm;
25
26#define DEBUG_TYPE "region"
27
28namespace llvm {
29
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
43 "verify-region-info",
45 cl::desc("Verify region info (time consuming)"));
46
48 cl::location(RegionInfo::printStyle),
50 cl::desc("style of printing regions"),
52 clEnumValN(Region::PrintNone, "none", "print no details"),
53 clEnumValN(Region::PrintBB, "bb",
54 "print regions in detail with block_iterator"),
55 clEnumValN(Region::PrintRN, "rn",
56 "print regions in detail with element_iterator")));
57
58//===----------------------------------------------------------------------===//
59// Region implementation
60//
61
63 RegionInfo* RI,
64 DominatorTree *DT, Region *Parent) :
65 RegionBase<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
81 // Check whether the analysis, all analyses on functions, or the function's
82 // CFG has been preserved.
83 auto PAC = PA.getChecker<RegionInfoAnalysis>();
84 return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
85 PAC.preservedSet<CFGAnalyses>());
86}
87
89 ++numRegions;
90
91 // TODO: Slow. Should only be enabled if -stats is used.
92 if (R->isSimple())
93 ++numSimpleRegions;
94}
95
98 DT = DT_;
99 PDT = PDT_;
100 DF = DF_;
101
102 TopLevelRegion = new Region(&F.getEntryBlock(), nullptr,
103 this, DT, nullptr);
104 updateStatistics(TopLevelRegion);
105 calculate(F);
106}
107
108#ifndef NDEBUG
110
112#endif
113
114//===----------------------------------------------------------------------===//
115// RegionInfoPass implementation
116//
117
120}
121
123
126
127 auto DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
128 auto PDT = &getAnalysis<PostDominatorTreeWrapperPass>().getPostDomTree();
129 auto DF = &getAnalysis<DominanceFrontierWrapperPass>().getDominanceFrontier();
130
131 RI.recalculate(F, DT, PDT, DF);
132 return false;
133}
134
136 RI.releaseMemory();
137}
138
140 RI.verifyAnalysis();
141}
142
144 AU.setPreservesAll();
148}
149
151 RI.print(OS);
152}
153
154#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
156 RI.dump();
157}
158#endif
159
160char RegionInfoPass::ID = 0;
161
163 "Detect single entry single exit regions", true, true)
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
177 return new RegionInfoPass();
178 }
179
180} // end namespace llvm
181
182//===----------------------------------------------------------------------===//
183// RegionInfoAnalysis implementation
184//
185
186AnalysisKey RegionInfoAnalysis::Key;
187
189 RegionInfo RI;
190 auto *DT = &AM.getResult<DominatorTreeAnalysis>(F);
191 auto *PDT = &AM.getResult<PostDominatorTreeAnalysis>(F);
193
194 RI.recalculate(F, DT, PDT, DF);
195 return RI;
196}
197
199 : OS(OS) {}
200
203 OS << "Region Tree for function: " << F.getName() << "\n";
205
206 return PreservedAnalyses::all();
207}
208
211 AM.getResult<RegionInfoAnalysis>(F).verifyAnalysis();
212
213 return PreservedAnalyses::all();
214}
basic Basic Alias true
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
Definition: CommandLine.h:693
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:529
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:55
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:59
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:52
static cl::opt< bool, true > VerifyRegionInfoX("verify-region-info", cl::location(RegionInfoBase< RegionTraits< Function > >::VerifyRegionInfo), cl::desc("Verify region info (time consuming)"))
regions
Definition: RegionInfo.cpp:167
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")))
raw_pwrite_stream & OS
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 <a particular IR unit>" (e....
Definition: Analysis.h:47
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:387
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:348
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:500
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:60
Represents analyses that only rely on functions' control flow.
Definition: Analysis.h:70
Analysis pass which computes a DominanceFrontier.
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:279
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:317
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:162
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:311
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
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:109
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:115
PreservedAnalysisChecker getChecker() const
Build a checker for this PreservedAnalyses and the specified analysis type.
Definition: Analysis.h:264
A single entry single exit Region.
Definition: RegionInfo.h:254
Analysis pass that exposes the RegionInfo for a function.
Definition: RegionInfo.h:967
RegionInfo run(Function &F, FunctionAnalysisManager &AM)
Definition: RegionInfo.cpp:188
Analysis that detects all canonical Regions.
Definition: RegionInfo.h:674
void print(raw_ostream &OS) const
void verifyAnalysis() const
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
Definition: RegionInfo.cpp:135
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
Definition: RegionInfo.cpp:124
~RegionInfoPass() override
void verifyAnalysis() const override
verifyAnalysis() - This member can be implemented by a analysis pass to check state of analysis infor...
Definition: RegionInfo.cpp:139
void print(raw_ostream &OS, const Module *) const override
print - Print out the internal state of the pass.
Definition: RegionInfo.cpp:150
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: RegionInfo.cpp:143
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: RegionInfo.cpp:201
RegionInfoPrinterPass(raw_ostream &OS)
Definition: RegionInfo.cpp:198
void view()
Opens a viewer to show the GraphViz visualization of the regions.
Definition: RegionInfo.cpp:109
~RegionInfo() override
void viewOnly()
Opens a viewer to show the GraphViz visualization of this region without instructions in the BasicBlo...
Definition: RegionInfo.cpp:111
void recalculate(Function &F, DominatorTree *DT, PostDominatorTree *PDT, DominanceFrontier *DF)
Definition: RegionInfo.cpp:96
void updateStatistics(Region *R) final
Definition: RegionInfo.cpp:88
bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &)
Handle invalidation explicitly.
Definition: RegionInfo.cpp:79
A RegionNode represents a subregion or a BasicBlock that is part of a Region.
Definition: RegionInfo.h:117
Region(BasicBlock *Entry, BasicBlock *Exit, RegionInfo *RI, DominatorTree *DT, Region *Parent=nullptr)
Definition: RegionInfo.cpp:62
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
Definition: CommandLine.h:718
LocationClass< Ty > location(Ty &L)
Definition: CommandLine.h:470
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr)
void initializeRegionInfoPassPass(PassRegistry &)
FunctionPass * createRegionInfoPass()
Definition: RegionInfo.cpp:176
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:26
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: RegionInfo.cpp:209