LLVM  4.0.0
RegionPass.cpp
Go to the documentation of this file.
1 //===- RegionPass.cpp - Region Pass and Region Pass Manager ---------------===//
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 //
10 // This file implements RegionPass and RGPassManager. All region optimization
11 // and transformation passes are derived from RegionPass. RGPassManager is
12 // responsible for managing RegionPasses.
13 // Most of this code has been COPIED from LoopPass.cpp
14 //
15 //===----------------------------------------------------------------------===//
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/Timer.h"
21 using namespace llvm;
22 
23 #define DEBUG_TYPE "regionpassmgr"
24 
25 //===----------------------------------------------------------------------===//
26 // RGPassManager
27 //
28 
29 char RGPassManager::ID = 0;
30 
33  skipThisRegion = false;
34  redoThisRegion = false;
35  RI = nullptr;
36  CurrentRegion = nullptr;
37 }
38 
39 // Recurse through all subregions and all regions into RQ.
40 static void addRegionIntoQueue(Region &R, std::deque<Region *> &RQ) {
41  RQ.push_back(&R);
42  for (const auto &E : R)
43  addRegionIntoQueue(*E, RQ);
44 }
45 
46 /// Pass Manager itself does not invalidate any analysis info.
49  Info.setPreservesAll();
50 }
51 
52 /// run - Execute all of the passes scheduled for execution. Keep track of
53 /// whether any of the passes modifies the function, and if so, return true.
55  RI = &getAnalysis<RegionInfoPass>().getRegionInfo();
56  bool Changed = false;
57 
58  // Collect inherited analysis from Module level pass manager.
60 
62 
63  if (RQ.empty()) // No regions, skip calling finalizers
64  return false;
65 
66  // Initialization
67  for (Region *R : RQ) {
68  for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
70  Changed |= RP->doInitialization(R, *this);
71  }
72  }
73 
74  // Walk Regions
75  while (!RQ.empty()) {
76 
77  CurrentRegion = RQ.back();
78  skipThisRegion = false;
79  redoThisRegion = false;
80 
81  // Run all passes on the current Region.
82  for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
84 
87  CurrentRegion->getNameStr());
88  dumpRequiredSet(P);
89  }
90 
92 
93  {
94  PassManagerPrettyStackEntry X(P, *CurrentRegion->getEntry());
95 
96  TimeRegion PassTimer(getPassTimer(P));
97  Changed |= P->runOnRegion(CurrentRegion, *this);
98  }
99 
101  if (Changed)
103  skipThisRegion ? "<deleted>" :
104  CurrentRegion->getNameStr());
105  dumpPreservedSet(P);
106  }
107 
108  if (!skipThisRegion) {
109  // Manually check that this region is still healthy. This is done
110  // instead of relying on RegionInfo::verifyRegion since RegionInfo
111  // is a function pass and it's really expensive to verify every
112  // Region in the function every time. That level of checking can be
113  // enabled with the -verify-region-info option.
114  {
115  TimeRegion PassTimer(getPassTimer(P));
116  CurrentRegion->verifyRegion();
117  }
118 
119  // Then call the regular verifyAnalysis functions.
121  }
122 
126  (!isPassDebuggingExecutionsOrMore() || skipThisRegion) ?
127  "<deleted>" : CurrentRegion->getNameStr(),
128  ON_REGION_MSG);
129 
130  if (skipThisRegion)
131  // Do not run other passes on this region.
132  break;
133  }
134 
135  // If the region was deleted, release all the region passes. This frees up
136  // some memory, and avoids trouble with the pass manager trying to call
137  // verifyAnalysis on them.
138  if (skipThisRegion)
139  for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
140  Pass *P = getContainedPass(Index);
141  freePass(P, "<deleted>", ON_REGION_MSG);
142  }
143 
144  // Pop the region from queue after running all passes.
145  RQ.pop_back();
146 
147  if (redoThisRegion)
148  RQ.push_back(CurrentRegion);
149 
150  // Free all region nodes created in region passes.
151  RI->clearNodeCache();
152  }
153 
154  // Finalization
155  for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
157  Changed |= P->doFinalization();
158  }
159 
160  // Print the region tree after all pass.
161  DEBUG(
162  dbgs() << "\nRegion tree of function " << F.getName()
163  << " after all region Pass:\n";
164  RI->dump();
165  dbgs() << "\n";
166  );
167 
168  return Changed;
169 }
170 
171 /// Print passes managed by this manager
173  errs().indent(Offset*2) << "Region Pass Manager\n";
174  for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
175  Pass *P = getContainedPass(Index);
176  P->dumpPassStructure(Offset + 1);
177  dumpLastUses(P, Offset+1);
178  }
179 }
180 
181 namespace {
182 //===----------------------------------------------------------------------===//
183 // PrintRegionPass
184 class PrintRegionPass : public RegionPass {
185 private:
186  std::string Banner;
187  raw_ostream &Out; // raw_ostream to print on.
188 
189 public:
190  static char ID;
191  PrintRegionPass(const std::string &B, raw_ostream &o)
192  : RegionPass(ID), Banner(B), Out(o) {}
193 
194  void getAnalysisUsage(AnalysisUsage &AU) const override {
195  AU.setPreservesAll();
196  }
197 
198  bool runOnRegion(Region *R, RGPassManager &RGM) override {
199  Out << Banner;
200  for (const auto *BB : R->blocks()) {
201  if (BB)
202  BB->print(Out);
203  else
204  Out << "Printing <null> Block";
205  }
206 
207  return false;
208  }
209 };
210 
211 char PrintRegionPass::ID = 0;
212 } //end anonymous namespace
213 
214 //===----------------------------------------------------------------------===//
215 // RegionPass
216 
217 // Check if this pass is suitable for the current RGPassManager, if
218 // available. This pass P is not suitable for a RGPassManager if P
219 // is not preserving higher level analysis info used by other
220 // RGPassManager passes. In such case, pop RGPassManager from the
221 // stack. This will force assignPassManager() to create new
222 // LPPassManger as expected.
224 
225  // Find RGPassManager
226  while (!PMS.empty() &&
228  PMS.pop();
229 
230 
231  // If this pass is destroying high level information that is used
232  // by other passes that are managed by LPM then do not insert
233  // this pass in current LPM. Use new RGPassManager.
234  if (PMS.top()->getPassManagerType() == PMT_RegionPassManager &&
235  !PMS.top()->preserveHigherLevelAnalysis(this))
236  PMS.pop();
237 }
238 
239 /// Assign pass manager to manage this pass.
241  PassManagerType PreferredType) {
242  // Find RGPassManager
243  while (!PMS.empty() &&
245  PMS.pop();
246 
247  RGPassManager *RGPM;
248 
249  // Create new Region Pass Manager if it does not exist.
251  RGPM = (RGPassManager*)PMS.top();
252  else {
253 
254  assert (!PMS.empty() && "Unable to create Region Pass Manager");
255  PMDataManager *PMD = PMS.top();
256 
257  // [1] Create new Region Pass Manager
258  RGPM = new RGPassManager();
259  RGPM->populateInheritedAnalysis(PMS);
260 
261  // [2] Set up new manager's top level manager
262  PMTopLevelManager *TPM = PMD->getTopLevelManager();
263  TPM->addIndirectPassManager(RGPM);
264 
265  // [3] Assign manager to manage this new manager. This may create
266  // and push new managers into PMS
267  TPM->schedulePass(RGPM);
268 
269  // [4] Push new manager into PMS
270  PMS.push(RGPM);
271  }
272 
273  RGPM->add(this);
274 }
275 
276 /// Get the printer pass
278  const std::string &Banner) const {
279  return new PrintRegionPass(Banner, O);
280 }
PMTopLevelManager * TPM
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:81
bool preserveHigherLevelAnalysis(Pass *P)
PassManagerType
Different types of internal pass managers.
Definition: Pass.h:54
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
std::string getNameStr() const
Returns the name of the Region.
void dumpLastUses(Pass *P, unsigned Offset) const
virtual void dumpPassStructure(unsigned Offset=0)
Definition: Pass.cpp:59
Pass * createPrinterPass(raw_ostream &O, const std::string &Banner) const override
Get a pass to print the LLVM IR in the region.
Definition: RegionPass.cpp:277
static void addRegionIntoQueue(Region &R, std::deque< Region * > &RQ)
Definition: RegionPass.cpp:40
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
void dumpRequiredSet(const Pass *P) const
RegionT * getTopLevelRegion() const
Definition: RegionInfo.h:838
void dumpPassInfo(Pass *P, enum PassDebuggingString S1, enum PassDebuggingString S2, StringRef Msg)
The pass manager to schedule RegionPasses.
Definition: RegionPass.h:84
virtual PassManagerType getPassManagerType() const
PMTopLevelManager manages LastUser info and collects common APIs used by top level pass managers...
Timer * getPassTimer(Pass *)
If TimingInfo is enabled then start pass timer.
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:191
The TimeRegion class is used as a helper class to call the startTimer() and stopTimer() methods of th...
Definition: Timer.h:140
void schedulePass(Pass *P)
Schedule pass P for execution.
AnalysisUsage & addRequired()
void freePass(Pass *P, StringRef Msg, enum PassDebuggingString)
Remove P.
virtual bool doInitialization(Region *R, RGPassManager &RGM)
Definition: RegionPass.h:64
bool isPassDebuggingExecutionsOrMore() const
isPassDebuggingExecutionsOrMore - Return true if -debug-pass=Executions or higher is specified...
void verifyPreservedAnalysis(Pass *P)
verifyPreservedAnalysis – Verify analysis presreved by pass P.
void populateInheritedAnalysis(PMStack &PMS)
static char ID
Definition: RegionPass.h:92
block_range blocks()
Returns a range view of the basic blocks in the region.
Definition: RegionInfo.h:611
PMStack - This class implements a stack data structure of PMDataManager pointers. ...
void initializeAnalysisImpl(Pass *P)
All Required analyses should be available to the pass as it runs! Here we fill in the AnalysisImpls m...
PassManagerPrettyStackEntry - This is used to print informative information about what pass is runnin...
#define F(x, y, z)
Definition: MD5.cpp:51
void add(Pass *P, bool ProcessAnalysis=true)
Add pass P into the PassVector.
unsigned getNumContainedPasses() const
static GCRegistry::Add< OcamlGC > B("ocaml","ocaml 3.10-compatible GC")
virtual bool doFinalization()
Definition: RegionPass.h:65
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
void dumpPassStructure(unsigned Offset) override
Print passes managed by this manager.
Definition: RegionPass.cpp:172
#define P(N)
void assignPassManager(PMStack &PMS, PassManagerType PMT=PMT_RegionPassManager) override
Assign pass manager to manage this pass.
Definition: RegionPass.cpp:240
void preparePassManager(PMStack &PMS) override
Check if available pass managers are suitable for this pass or not.
Definition: RegionPass.cpp:223
void dumpPreservedSet(const Pass *P) const
A pass that runs on each Region in a function.
Definition: RegionPass.h:34
void addIndirectPassManager(PMDataManager *Manager)
Represent the analysis usage information of a pass.
bool runOnFunction(Function &F) override
Execute all of the passes scheduled for execution.
Definition: RegionPass.cpp:54
uint32_t Offset
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
Pass * getContainedPass(unsigned N)
Get passes contained by this manager.
Definition: RegionPass.h:113
bool empty() const
void recordAvailableAnalysis(Pass *P)
Augment AvailableAnalysis by adding analysis made available by pass P.
void removeNotPreservedAnalysis(Pass *P)
Remove Analysis that is not preserved by the pass.
void removeDeadPasses(Pass *P, StringRef Msg, enum PassDebuggingString)
Remove dead passes used by P.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
PMDataManager * top() const
void setPreservesAll()
Set by analyses that do not transform their input at all.
RGPassManager.
Definition: Pass.h:60
void verifyRegion() const
Verify if the region is a correct region.
virtual bool runOnRegion(Region *R, RGPassManager &RGM)=0
Run the pass on a specific Region.
void push(PMDataManager *PM)
PMDataManager provides the common place to manage the analysis data used by pass managers.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void clearNodeCache()
Clear the Node Cache for all Regions.
Definition: RegionInfo.h:843
BlockT * getEntry() const
Get the entry BasicBlock of the Region.
Definition: RegionInfo.h:316
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
#define DEBUG(X)
Definition: Debug.h:100
void getAnalysisUsage(AnalysisUsage &Info) const override
Pass Manager itself does not invalidate any analysis info.
Definition: RegionPass.cpp:47