LLVM 20.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
49 cl::location(RegionInfo::printStyle),
51 cl::desc("style of printing regions"),
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// 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
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
121}
122
124
127
128 auto DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
129 auto PDT = &getAnalysis<PostDominatorTreeWrapperPass>().getPostDomTree();
130 auto DF = &getAnalysis<DominanceFrontierWrapperPass>().getDominanceFrontier();
131
132 RI.recalculate(F, DT, PDT, DF);
133 return false;
134}
135
137 RI.releaseMemory();
138}
139
141 RI.verifyAnalysis();
142}
143
145 AU.setPreservesAll();
149}
150
152 RI.print(OS);
153}
154
155#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
157 RI.dump();
158}
159#endif
160
161char RegionInfoPass::ID = 0;
162
164 "Detect single entry single exit regions", true, true)
169 "Detect single entry single exit regions", true, true)
170
171// Create methods available outside of this file, to use them
172// "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
173// the link time optimization.
174
175namespace llvm {
176
178 return new RegionInfoPass();
179 }
180
181} // end namespace llvm
182
183//===----------------------------------------------------------------------===//
184// RegionInfoAnalysis implementation
185//
186
187AnalysisKey RegionInfoAnalysis::Key;
188
190 RegionInfo RI;
191 auto *DT = &AM.getResult<DominatorTreeAnalysis>(F);
192 auto *PDT = &AM.getResult<PostDominatorTreeAnalysis>(F);
194
195 RI.recalculate(F, DT, PDT, DF);
196 return RI;
197}
198
200 : OS(OS) {}
201
204 OS << "Region Tree for function: " << F.getName() << "\n";
206
207 return PreservedAnalyses::all();
208}
209
212 AM.getResult<RegionInfoAnalysis>(F).verifyAnalysis();
213
214 return PreservedAnalyses::all();
215}
basic Basic Alias true
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
Definition: CommandLine.h:686
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:622
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:57
#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:168
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:166
This templated class represents "all analyses that operate over <a particular IR unit>" (e....
Definition: Analysis.h:49
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:292
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:410
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:61
Represents analyses that only rely on functions' control flow.
Definition: Analysis.h:72
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:310
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:111
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:117
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:252
Analysis pass that exposes the RegionInfo for a function.
Definition: RegionInfo.h:965
RegionInfo run(Function &F, FunctionAnalysisManager &AM)
Definition: RegionInfo.cpp:189
Analysis that detects all canonical Regions.
Definition: RegionInfo.h:672
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:136
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
Definition: RegionInfo.cpp:125
~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:140
void print(raw_ostream &OS, const Module *) const override
print - Print out the internal state of the pass.
Definition: RegionInfo.cpp:151
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: RegionInfo.cpp:144
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: RegionInfo.cpp:202
RegionInfoPrinterPass(raw_ostream &OS)
Definition: RegionInfo.cpp:199
void view()
Opens a viewer to show the GraphViz visualization of the regions.
Definition: RegionInfo.cpp:110
~RegionInfo() override
void viewOnly()
Opens a viewer to show the GraphViz visualization of this region without instructions in the BasicBlo...
Definition: RegionInfo.cpp:112
void recalculate(Function &F, DominatorTree *DT, PostDominatorTree *PDT, DominanceFrontier *DF)
Definition: RegionInfo.cpp:97
void updateStatistics(Region *R) final
Definition: RegionInfo.cpp:89
bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &)
Handle invalidation explicitly.
Definition: RegionInfo.cpp:80
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)
Definition: RegionInfo.cpp:63
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:711
LocationClass< Ty > location(Ty &L)
Definition: CommandLine.h:463
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:177
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:28
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: RegionInfo.cpp:210