LLVM  3.7.0
LegacyPassManagers.h
Go to the documentation of this file.
1 //===- LegacyPassManagers.h - Legacy Pass Infrastructure --------*- C++ -*-===//
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 declares the LLVM Pass Manager infrastructure.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_IR_LEGACYPASSMANAGERS_H
15 #define LLVM_IR_LEGACYPASSMANAGERS_H
16 
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/Pass.h"
22 #include <map>
23 #include <vector>
24 
25 //===----------------------------------------------------------------------===//
26 // Overview:
27 // The Pass Manager Infrastructure manages passes. It's responsibilities are:
28 //
29 // o Manage optimization pass execution order
30 // o Make required Analysis information available before pass P is run
31 // o Release memory occupied by dead passes
32 // o If Analysis information is dirtied by a pass then regenerate Analysis
33 // information before it is consumed by another pass.
34 //
35 // Pass Manager Infrastructure uses multiple pass managers. They are
36 // PassManager, FunctionPassManager, MPPassManager, FPPassManager, BBPassManager.
37 // This class hierarchy uses multiple inheritance but pass managers do not
38 // derive from another pass manager.
39 //
40 // PassManager and FunctionPassManager are two top-level pass manager that
41 // represents the external interface of this entire pass manager infrastucture.
42 //
43 // Important classes :
44 //
45 // [o] class PMTopLevelManager;
46 //
47 // Two top level managers, PassManager and FunctionPassManager, derive from
48 // PMTopLevelManager. PMTopLevelManager manages information used by top level
49 // managers such as last user info.
50 //
51 // [o] class PMDataManager;
52 //
53 // PMDataManager manages information, e.g. list of available analysis info,
54 // used by a pass manager to manage execution order of passes. It also provides
55 // a place to implement common pass manager APIs. All pass managers derive from
56 // PMDataManager.
57 //
58 // [o] class BBPassManager : public FunctionPass, public PMDataManager;
59 //
60 // BBPassManager manages BasicBlockPasses.
61 //
62 // [o] class FunctionPassManager;
63 //
64 // This is a external interface used to manage FunctionPasses. This
65 // interface relies on FunctionPassManagerImpl to do all the tasks.
66 //
67 // [o] class FunctionPassManagerImpl : public ModulePass, PMDataManager,
68 // public PMTopLevelManager;
69 //
70 // FunctionPassManagerImpl is a top level manager. It manages FPPassManagers
71 //
72 // [o] class FPPassManager : public ModulePass, public PMDataManager;
73 //
74 // FPPassManager manages FunctionPasses and BBPassManagers
75 //
76 // [o] class MPPassManager : public Pass, public PMDataManager;
77 //
78 // MPPassManager manages ModulePasses and FPPassManagers
79 //
80 // [o] class PassManager;
81 //
82 // This is a external interface used by various tools to manages passes. It
83 // relies on PassManagerImpl to do all the tasks.
84 //
85 // [o] class PassManagerImpl : public Pass, public PMDataManager,
86 // public PMTopLevelManager
87 //
88 // PassManagerImpl is a top level pass manager responsible for managing
89 // MPPassManagers.
90 //===----------------------------------------------------------------------===//
91 
93 
94 namespace llvm {
95  class Module;
96  class Pass;
97  class StringRef;
98  class Value;
99  class Timer;
100  class PMDataManager;
101 
102 // enums for debugging strings
104  EXECUTION_MSG, // "Executing Pass '" + PassName
105  MODIFICATION_MSG, // "Made Modification '" + PassName
106  FREEING_MSG, // " Freeing Pass '" + PassName
107  ON_BASICBLOCK_MSG, // "' on BasicBlock '" + InstructionName + "'...\n"
108  ON_FUNCTION_MSG, // "' on Function '" + FunctionName + "'...\n"
109  ON_MODULE_MSG, // "' on Module '" + ModuleName + "'...\n"
110  ON_REGION_MSG, // "' on Region '" + Msg + "'...\n'"
111  ON_LOOP_MSG, // "' on Loop '" + Msg + "'...\n'"
112  ON_CG_MSG // "' on Call Graph Nodes '" + Msg + "'...\n'"
113 };
114 
115 /// PassManagerPrettyStackEntry - This is used to print informative information
116 /// about what pass is running when/if a stack trace is generated.
118  Pass *P;
119  Value *V;
120  Module *M;
121 public:
123  : P(p), V(nullptr), M(nullptr) {} // When P is releaseMemory'd.
125  : P(p), V(&v), M(nullptr) {} // When P is run on V
127  : P(p), V(nullptr), M(&m) {} // When P is run on M
128 
129  /// print - Emit information about this stack frame to OS.
130  void print(raw_ostream &OS) const override;
131 };
132 
133 
134 //===----------------------------------------------------------------------===//
135 // PMStack
136 //
137 /// PMStack - This class implements a stack data structure of PMDataManager
138 /// pointers.
139 ///
140 /// Top level pass managers (see PassManager.cpp) maintain active Pass Managers
141 /// using PMStack. Each Pass implements assignPassManager() to connect itself
142 /// with appropriate manager. assignPassManager() walks PMStack to find
143 /// suitable manager.
144 class PMStack {
145 public:
146  typedef std::vector<PMDataManager *>::const_reverse_iterator iterator;
147  iterator begin() const { return S.rbegin(); }
148  iterator end() const { return S.rend(); }
149 
150  void pop();
151  PMDataManager *top() const { return S.back(); }
152  void push(PMDataManager *PM);
153  bool empty() const { return S.empty(); }
154 
155  void dump() const;
156 
157 private:
158  std::vector<PMDataManager *> S;
159 };
160 
161 
162 //===----------------------------------------------------------------------===//
163 // PMTopLevelManager
164 //
165 /// PMTopLevelManager manages LastUser info and collects common APIs used by
166 /// top level pass managers.
168 protected:
169  explicit PMTopLevelManager(PMDataManager *PMDM);
170 
171  unsigned getNumContainedManagers() const {
172  return (unsigned)PassManagers.size();
173  }
174 
176 
177 private:
178  virtual PMDataManager *getAsPMDataManager() = 0;
179  virtual PassManagerType getTopLevelPassManagerType() = 0;
180 
181 public:
182  /// Schedule pass P for execution. Make sure that passes required by
183  /// P are run before P is run. Update analysis info maintained by
184  /// the manager. Remove dead passes. This is a recursive function.
185  void schedulePass(Pass *P);
186 
187  /// Set pass P as the last user of the given analysis passes.
188  void setLastUser(ArrayRef<Pass*> AnalysisPasses, Pass *P);
189 
190  /// Collect passes whose last user is P
191  void collectLastUses(SmallVectorImpl<Pass *> &LastUses, Pass *P);
192 
193  /// Find the pass that implements Analysis AID. Search immutable
194  /// passes and all pass managers. If desired pass is not found
195  /// then return NULL.
197 
198  /// Retrieve the PassInfo for an analysis.
199  const PassInfo *findAnalysisPassInfo(AnalysisID AID) const;
200 
201  /// Find analysis usage information for the pass P.
203 
204  virtual ~PMTopLevelManager();
205 
206  /// Add immutable pass and initialize it.
208  P->initializePass();
209  ImmutablePasses.push_back(P);
210  }
211 
213  return ImmutablePasses;
214  }
215 
216  void addPassManager(PMDataManager *Manager) {
217  PassManagers.push_back(Manager);
218  }
219 
220  // Add Manager into the list of managers that are not directly
221  // maintained by this top level pass manager
222  inline void addIndirectPassManager(PMDataManager *Manager) {
223  IndirectPassManagers.push_back(Manager);
224  }
225 
226  // Print passes managed by this top level manager.
227  void dumpPasses() const;
228  void dumpArguments() const;
229 
230  // Active Pass Managers
232 
233 protected:
234 
235  /// Collection of pass managers
237 
238 private:
239 
240  /// Collection of pass managers that are not directly maintained
241  /// by this pass manager
242  SmallVector<PMDataManager *, 8> IndirectPassManagers;
243 
244  // Map to keep track of last user of the analysis pass.
245  // LastUser->second is the last user of Lastuser->first.
246  DenseMap<Pass *, Pass *> LastUser;
247 
248  // Map to keep track of passes that are last used by a pass.
249  // This inverse map is initialized at PM->run() based on
250  // LastUser map.
251  DenseMap<Pass *, SmallPtrSet<Pass *, 8> > InversedLastUser;
252 
253  /// Immutable passes are managed by top level manager.
254  SmallVector<ImmutablePass *, 16> ImmutablePasses;
255 
257 
258  /// Collection of PassInfo objects found via analysis IDs and in this top
259  /// level manager. This is used to memoize queries to the pass registry.
260  /// FIXME: This is an egregious hack because querying the pass registry is
261  /// either slow or racy.
262  mutable DenseMap<AnalysisID, const PassInfo *> AnalysisPassInfos;
263 };
264 
265 
266 
267 //===----------------------------------------------------------------------===//
268 // PMDataManager
269 
270 /// PMDataManager provides the common place to manage the analysis data
271 /// used by pass managers.
273 public:
274 
275  explicit PMDataManager() : TPM(nullptr), Depth(0) {
277  }
278 
279  virtual ~PMDataManager();
280 
281  virtual Pass *getAsPass() = 0;
282 
283  /// Augment AvailableAnalysis by adding analysis made available by pass P.
285 
286  /// verifyPreservedAnalysis -- Verify analysis presreved by pass P.
288 
289  /// Remove Analysis that is not preserved by the pass
291 
292  /// Remove dead passes used by P.
293  void removeDeadPasses(Pass *P, StringRef Msg,
294  enum PassDebuggingString);
295 
296  /// Remove P.
297  void freePass(Pass *P, StringRef Msg,
298  enum PassDebuggingString);
299 
300  /// Add pass P into the PassVector. Update
301  /// AvailableAnalysis appropriately if ProcessAnalysis is true.
302  void add(Pass *P, bool ProcessAnalysis = true);
303 
304  /// Add RequiredPass into list of lower level passes required by pass P.
305  /// RequiredPass is run on the fly by Pass Manager when P requests it
306  /// through getAnalysis interface.
307  virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
308 
309  virtual Pass *getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F);
310 
311  /// Initialize available analysis information.
313  AvailableAnalysis.clear();
314  for (unsigned i = 0; i < PMT_Last; ++i)
315  InheritedAnalysis[i] = nullptr;
316  }
317 
318  // Return true if P preserves high level analysis used by other
319  // passes that are managed by this manager.
321 
322 
323  /// Populate RequiredPasses with analysis pass that are required by
324  /// pass P and are available. Populate ReqPassNotAvailable with analysis
325  /// pass that are required by pass P but are not available.
327  SmallVectorImpl<AnalysisID> &ReqPassNotAvailable,
328  Pass *P);
329 
330  /// All Required analyses should be available to the pass as it runs! Here
331  /// we fill in the AnalysisImpls member of the pass so that it can
332  /// successfully use the getAnalysis() method to retrieve the
333  /// implementations it needs.
335 
336  /// Find the pass that implements Analysis AID. If desired pass is not found
337  /// then return NULL.
338  Pass *findAnalysisPass(AnalysisID AID, bool Direction);
339 
340  // Access toplevel manager
343 
344  unsigned getDepth() const { return Depth; }
345  void setDepth(unsigned newDepth) { Depth = newDepth; }
346 
347  // Print routines used by debug-pass
348  void dumpLastUses(Pass *P, unsigned Offset) const;
349  void dumpPassArguments() const;
350  void dumpPassInfo(Pass *P, enum PassDebuggingString S1,
351  enum PassDebuggingString S2, StringRef Msg);
352  void dumpRequiredSet(const Pass *P) const;
353  void dumpPreservedSet(const Pass *P) const;
354 
355  unsigned getNumContainedPasses() const {
356  return (unsigned)PassVector.size();
357  }
358 
360  assert ( 0 && "Invalid use of getPassManagerType");
361  return PMT_Unknown;
362  }
363 
365  return &AvailableAnalysis;
366  }
367 
368  // Collect AvailableAnalysis from all the active Pass Managers.
370  unsigned Index = 0;
371  for (PMStack::iterator I = PMS.begin(), E = PMS.end();
372  I != E; ++I)
373  InheritedAnalysis[Index++] = (*I)->getAvailableAnalysis();
374  }
375 
376 protected:
377 
378  // Top level manager.
380 
381  // Collection of pass that are managed by this manager
383 
384  // Collection of Analysis provided by Parent pass manager and
385  // used by current pass manager. At at time there can not be more
386  // then PMT_Last active pass mangers.
388 
389  /// isPassDebuggingExecutionsOrMore - Return true if -debug-pass=Executions
390  /// or higher is specified.
391  bool isPassDebuggingExecutionsOrMore() const;
392 
393 private:
394  void dumpAnalysisUsage(StringRef Msg, const Pass *P,
395  const AnalysisUsage::VectorType &Set) const;
396 
397  // Set of available Analysis. This information is used while scheduling
398  // pass. If a pass requires an analysis which is not available then
399  // the required analysis pass is scheduled to run before the pass itself is
400  // scheduled to run.
401  DenseMap<AnalysisID, Pass*> AvailableAnalysis;
402 
403  // Collection of higher level analysis used by the pass managed by
404  // this manager.
405  SmallVector<Pass *, 16> HigherLevelAnalysis;
406 
407  unsigned Depth;
408 };
409 
410 //===----------------------------------------------------------------------===//
411 // FPPassManager
412 //
413 /// FPPassManager manages BBPassManagers and FunctionPasses.
414 /// It batches all function passes and basic block pass managers together and
415 /// sequence them to process one function at a time before processing next
416 /// function.
417 class FPPassManager : public ModulePass, public PMDataManager {
418 public:
419  static char ID;
420  explicit FPPassManager()
421  : ModulePass(ID), PMDataManager() { }
422 
423  /// run - Execute all of the passes scheduled for execution. Keep track of
424  /// whether any of the passes modifies the module, and if so, return true.
425  bool runOnFunction(Function &F);
426  bool runOnModule(Module &M) override;
427 
428  /// cleanup - After running all passes, clean up pass manager cache.
429  void cleanup();
430 
431  /// doInitialization - Overrides ModulePass doInitialization for global
432  /// initialization tasks
433  ///
435 
436  /// doInitialization - Run all of the initializers for the function passes.
437  ///
438  bool doInitialization(Module &M) override;
439 
440  /// doFinalization - Overrides ModulePass doFinalization for global
441  /// finalization tasks
442  ///
444 
445  /// doFinalization - Run all of the finalizers for the function passes.
446  ///
447  bool doFinalization(Module &M) override;
448 
449  PMDataManager *getAsPMDataManager() override { return this; }
450  Pass *getAsPass() override { return this; }
451 
452  /// Pass Manager itself does not invalidate any analysis info.
453  void getAnalysisUsage(AnalysisUsage &Info) const override {
454  Info.setPreservesAll();
455  }
456 
457  // Print passes managed by this manager
458  void dumpPassStructure(unsigned Offset) override;
459 
460  const char *getPassName() const override {
461  return "Function Pass Manager";
462  }
463 
465  assert ( N < PassVector.size() && "Pass number out of range!");
466  FunctionPass *FP = static_cast<FunctionPass *>(PassVector[N]);
467  return FP;
468  }
469 
472  }
473 };
474 
475 Timer *getPassTimer(Pass *);
476 
477 }
478 
479 #endif
PMTopLevelManager * TPM
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:82
bool preserveHigherLevelAnalysis(Pass *P)
PassManagerType
Different types of internal pass managers.
Definition: Pass.h:55
PassManagerPrettyStackEntry(Pass *p, Value &v)
Pass * findAnalysisPass(AnalysisID AID, bool Direction)
Find the pass that implements Analysis AID.
void dumpLastUses(Pass *P, unsigned Offset) const
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
bool doInitialization(Module &M) override
doInitialization - Run all of the initializers for the function passes.
std::vector< PMDataManager * >::const_reverse_iterator iterator
const char * getPassName() const override
getPassName - Return a nice clean name for a pass.
unsigned getNumContainedManagers() const
void collectRequiredAnalysis(SmallVectorImpl< Pass * > &RequiredPasses, SmallVectorImpl< AnalysisID > &ReqPassNotAvailable, Pass *P)
Populate RequiredPasses with analysis pass that are required by pass P and are available.
void dumpRequiredSet(const Pass *P) const
F(f)
PMDataManager * getAsPMDataManager() override
PMTopLevelManager(PMDataManager *PMDM)
Initialize top level manager. Create first pass manager.
void dumpPassInfo(Pass *P, enum PassDebuggingString S1, enum PassDebuggingString S2, StringRef Msg)
virtual void initializePass()
initializePass - This method may be overriden by immutable passes to allow them to perform various in...
Definition: Pass.cpp:126
virtual PassManagerType getPassManagerType() const
DenseMap< AnalysisID, Pass * > * InheritedAnalysis[PMT_Last]
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.
void setDepth(unsigned newDepth)
void addPassManager(PMDataManager *Manager)
void schedulePass(Pass *P)
Schedule pass P for execution.
void freePass(Pass *P, StringRef Msg, enum PassDebuggingString)
Remove P.
void dumpPassStructure(unsigned Offset) override
Print passes managed by this manager.
bool isPassDebuggingExecutionsOrMore() const
isPassDebuggingExecutionsOrMore - Return true if -debug-pass=Executions or higher is specified...
virtual ~PMTopLevelManager()
Destructor.
virtual bool doFinalization(Module &)
doFinalization - Virtual method overriden by subclasses to do any necessary clean up after all passes...
Definition: Pass.h:116
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APInt.h:33
void verifyPreservedAnalysis(Pass *P)
verifyPreservedAnalysis – Verify analysis presreved by pass P.
void initializeAnalysisInfo()
Initialize available analysis information.
virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass)
Add RequiredPass into list of lower level passes required by pass P.
void populateInheritedAnalysis(PMStack &PMS)
FPPassManager manages BBPassManagers and FunctionPasses.
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...
unsigned getDepth() const
PassManagerPrettyStackEntry(Pass *p, Module &m)
iterator end() const
#define T
void add(Pass *P, bool ProcessAnalysis=true)
Add pass P into the PassVector.
unsigned getNumContainedPasses() const
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:31
virtual Pass * getAsPass()=0
bool doFinalization(Module &M) override
doFinalization - Run all of the finalizers for the function passes.
virtual bool doInitialization(Module &)
doInitialization - Virtual method overridden by subclasses to do any necessary initialization before ...
Definition: Pass.h:111
#define P(N)
void dumpPreservedSet(const Pass *P) const
void print(raw_ostream &OS) const override
print - Emit information about this stack frame to OS.
void addIndirectPassManager(PMDataManager *Manager)
void collectLastUses(SmallVectorImpl< Pass * > &LastUses, Pass *P)
Collect passes whose last user is P.
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:294
SmallVectorImpl< ImmutablePass * > & getImmutablePasses()
PassInfo class - An instance of this class exists for every pass known by the system, and can be obtained from a live Pass by calling its getPassInfo() method.
Definition: PassInfo.h:30
aarch64 type AArch64 Type Promotion Pass
PrettyStackTraceEntry - This class is used to represent a frame of the "pretty" stack trace that is d...
iterator begin() const
bool runOnModule(Module &M) override
runOnModule - Virtual method overriden by subclasses to process the module being operated on...
void setLastUser(ArrayRef< Pass * > AnalysisPasses, Pass *P)
Set pass P as the last user of the given analysis passes.
void addImmutablePass(ImmutablePass *P)
Add immutable pass and initialize it.
bool empty() const
ImmutablePass class - This class is used to provide information that does not need to be run...
Definition: Pass.h:262
void recordAvailableAnalysis(Pass *P)
Augment AvailableAnalysis by adding analysis made available by pass P.
FPPassManager.
Definition: Pass.h:59
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
void removeNotPreservedAnalysis(Pass *P)
Remove Analysis that is not preserved by the pass.
void dump() const
bool runOnFunction(Function &F)
run - Execute all of the passes scheduled for execution.
void removeDeadPasses(Pass *P, StringRef Msg, enum PassDebuggingString)
Remove dead passes used by P.
virtual Pass * getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F)
SmallVector< Pass *, 16 > PassVector
PMDataManager * top() const
Pass * getAsPass() override
void getAnalysisUsage(AnalysisUsage &Info) const override
Pass Manager itself does not invalidate any analysis info.
DenseMap< AnalysisID, Pass * > * getAvailableAnalysis()
void setPreservesAll()
Set by analyses that do not transform their input at all.
AnalysisUsage * findAnalysisUsage(Pass *P)
Find analysis usage information for the pass P.
const PassInfo * findAnalysisPassInfo(AnalysisID AID) const
Retrieve the PassInfo for an analysis.
void setTopLevelManager(PMTopLevelManager *T)
SmallVector< PMDataManager *, 8 > PassManagers
Collection of pass managers.
FunctionPass * getContainedPass(unsigned N)
void cleanup()
cleanup - After running all passes, clean up pass manager cache.
#define I(x, y, z)
Definition: MD5.cpp:54
#define N
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:236
const void * AnalysisID
Definition: Pass.h:47
void push(PMDataManager *PM)
PMDataManager provides the common place to manage the analysis data used by pass managers.
Pass * findAnalysisPass(AnalysisID AID)
Find the pass that implements Analysis AID.
void dumpPassArguments() const
LLVM Value Representation.
Definition: Value.h:69
PMTopLevelManager * getTopLevelManager()
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:38
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
PassManagerType getPassManagerType() const override