LLVM  4.0.0
RegionInfo.cpp
Go to the documentation of this file.
1 //===- RegionInfo.cpp - SESE region detection analysis --------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 // Detects single entry single exit regions in the control flow graph.
10 //===----------------------------------------------------------------------===//
11 
14 #include "llvm/ADT/Statistic.h"
15 #include "llvm/Analysis/LoopInfo.h"
18 #include "llvm/IR/PassManager.h"
20 #include "llvm/Support/Debug.h"
22 #ifndef NDEBUG
24 #endif
25 
26 using namespace llvm;
27 
28 #define DEBUG_TYPE "region"
29 
30 namespace llvm {
31 template class RegionBase<RegionTraits<Function>>;
34 }
35 
36 STATISTIC(numRegions, "The # of regions");
37 STATISTIC(numSimpleRegions, "The # of simple regions");
38 
39 // Always verify if expensive checking is enabled.
40 
41 static cl::opt<bool,true>
43  "verify-region-info",
45  cl::desc("Verify region info (time consuming)"));
46 
47 
48 static cl::opt<Region::PrintStyle, true> printStyleX("print-region-style",
50  cl::Hidden,
51  cl::desc("style of printing regions"),
52  cl::values(
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 //===----------------------------------------------------------------------===//
61 // Region implementation
62 //
63 
65  RegionInfo* RI,
66  DominatorTree *DT, Region *Parent) :
67  RegionBase<RegionTraits<Function>>(Entry, Exit, RI, DT, Parent) {
68 
69 }
70 
72 
73 //===----------------------------------------------------------------------===//
74 // RegionInfo implementation
75 //
76 
79 
80 }
81 
83 
84 }
85 
87  ++numRegions;
88 
89  // TODO: Slow. Should only be enabled if -stats is used.
90  if (R->isSimple())
91  ++numSimpleRegions;
92 }
93 
96  DT = DT_;
97  PDT = PDT_;
98  DF = DF_;
99 
100  TopLevelRegion = new Region(&F.getEntryBlock(), nullptr,
101  this, DT, nullptr);
102  updateStatistics(TopLevelRegion);
103  calculate(F);
104 }
105 
106 #ifndef NDEBUG
107 void RegionInfo::view() { viewRegion(this); }
108 
110 #endif
111 
112 //===----------------------------------------------------------------------===//
113 // RegionInfoPass implementation
114 //
115 
118 }
119 
121 
122 }
123 
125  releaseMemory();
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 
150 void RegionInfoPass::print(raw_ostream &OS, const Module *) const {
151  RI.print(OS);
152 }
153 
154 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
156  RI.dump();
157 }
158 #endif
159 
160 char 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 
174 namespace llvm {
176  return new RegionInfoPass();
177  }
178 }
179 
180 //===----------------------------------------------------------------------===//
181 // RegionInfoAnalysis implementation
182 //
183 
184 AnalysisKey RegionInfoAnalysis::Key;
185 
187  RegionInfo RI;
188  auto *DT = &AM.getResult<DominatorTreeAnalysis>(F);
189  auto *PDT = &AM.getResult<PostDominatorTreeAnalysis>(F);
190  auto *DF = &AM.getResult<DominanceFrontierAnalysis>(F);
191 
192  RI.recalculate(F, DT, PDT, DF);
193  return RI;
194 }
195 
197  : OS(OS) {}
198 
201  OS << "Region Tree for function: " << F.getName() << "\n";
202  AM.getResult<RegionInfoAnalysis>(F).print(OS);
203 
204  return PreservedAnalyses::all();
205 }
206 
209  AM.getResult<RegionInfoAnalysis>(F).verifyAnalysis();
210 
211  return PreservedAnalyses::all();
212 }
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: RegionInfo.cpp:207
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
void viewRegion(llvm::RegionInfo *RI)
Open a viewer to display the GraphViz vizualization of the analysis result.
STATISTIC(NumFunctions,"Total number of functions")
regions
Definition: RegionInfo.cpp:167
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:450
bool isSimple() const
Is this a simple region?
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:52
void print(raw_ostream &OS, const Module *) const override
print - Print out the internal state of the pass.
Definition: RegionInfo.cpp:150
FunctionPass * createRegionInfoPass()
Definition: RegionInfo.cpp:175
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:189
void recalculate(Function &F, DominatorTree *DT, PostDominatorTree *PDT, DominanceFrontier *DF)
Definition: RegionInfo.cpp:94
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
void initializeRegionInfoPassPass(PassRegistry &)
Analysis that detects all canonical Regions.
Definition: RegionInfo.h:77
void viewRegionOnly(llvm::RegionInfo *RI)
Open a viewer to display the GraphViz vizualization of the analysis result.
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:191
void view()
Opens a viewer to show the GraphViz visualization of the regions.
Definition: RegionInfo.cpp:107
INITIALIZE_PASS_BEGIN(RegionInfoPass,"regions","Detect single entry single exit regions", true, true) INITIALIZE_PASS_END(RegionInfoPass
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:53
Detect single entry single exit true
Definition: RegionInfo.cpp:167
Analysis pass which computes a DominanceFrontier.
A RegionNode represents a subregion or a BasicBlock that is part of a Region.
Definition: RegionInfo.h:112
RegionInfoPrinterPass(raw_ostream &OS)
Definition: RegionInfo.cpp:196
void dump() const
Definition: RegionInfo.cpp:155
void print(raw_ostream &OS) const
void verifyAnalysis() const
#define F(x, y, z)
Definition: MD5.cpp:51
Region(BasicBlock *Entry, BasicBlock *Exit, RegionInfo *RI, DominatorTree *DT, Region *Parent=nullptr)
Definition: RegionInfo.cpp:64
void verifyAnalysis() const override
verifyAnalysis() - This member can be implemented by a analysis pass to check state of analysis infor...
Definition: RegionInfo.cpp:139
~RegionInfo() override
Definition: RegionInfo.cpp:82
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: RegionInfo.cpp:199
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:96
ValuesClass values(OptsTy...Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
Definition: CommandLine.h:615
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:107
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs...ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:653
LLVM Basic Block Representation.
Definition: BasicBlock.h:51
Represent the analysis usage information of a pass.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
RegionInfo run(Function &F, FunctionAnalysisManager &AM)
Definition: RegionInfo.cpp:186
void viewOnly()
Opens a viewer to show the GraphViz visualization of this region without instructions in the BasicBlo...
Definition: RegionInfo.cpp:109
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:113
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")))
~RegionInfoPass() override
Definition: RegionInfo.cpp:120
Analysis pass which computes a PostDominatorTree.
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
const BasicBlock & getEntryBlock() const
Definition: Function.h:519
A single entry single exit Region.
Definition: RegionInfo.h:73
Analysis pass that exposes the RegionInfo for a function.
Definition: RegionInfo.h:934
void setPreservesAll()
Set by analyses that do not transform their input at all.
PostDominatorTree Class - Concrete subclass of DominatorTree that is used to compute the post-dominat...
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
Definition: CommandLine.h:590
AnalysisUsage & addRequiredTransitive()
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass...
Definition: RegionInfo.cpp:124
void updateStatistics(Region *R) final
Definition: RegionInfo.cpp:86
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
A container for analyses that lazily runs them and caches their results.
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:217
This header defines various interfaces for pass management in LLVM.
static cl::opt< bool, true > VerifyRegionInfoX("verify-region-info", cl::location(RegionInfoBase< RegionTraits< Function >>::VerifyRegionInfo), cl::desc("Verify region info (time consuming)"))
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:64
LocationClass< Ty > location(Ty &L)
Definition: CommandLine.h:411