LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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_PASSMANAGERS_H
15 #define LLVM_PASSMANAGERS_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 by JIT 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 '"
105  MODIFICATION_MSG, // "' Made Modification '"
106  FREEING_MSG, // " Freeing Pass '"
107  ON_BASICBLOCK_MSG, // "' on BasicBlock '" + PassName + "'...\n"
108  ON_FUNCTION_MSG, // "' on Function '" + FunctionName + "'...\n"
109  ON_MODULE_MSG, // "' on Module '" + ModuleName + "'...\n"
110  ON_REGION_MSG, // " 'on Region ...\n'"
111  ON_LOOP_MSG, // " 'on Loop ...\n'"
112  ON_CG_MSG // "' on Call Graph ...\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(0), M(0) {} // When P is releaseMemory'd.
125  : P(p), V(&v), M(0) {} // When P is run on V
127  : P(p), V(0), M(&m) {} // When P is run on M
128 
129  /// print - Emit information about this stack frame to OS.
130  virtual void print(raw_ostream &OS) const;
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  /// Find analysis usage information for the pass P.
200 
201  virtual ~PMTopLevelManager();
202 
203  /// Add immutable pass and initialize it.
205  P->initializePass();
206  ImmutablePasses.push_back(P);
207  }
208 
210  return ImmutablePasses;
211  }
212 
213  void addPassManager(PMDataManager *Manager) {
214  PassManagers.push_back(Manager);
215  }
216 
217  // Add Manager into the list of managers that are not directly
218  // maintained by this top level pass manager
219  inline void addIndirectPassManager(PMDataManager *Manager) {
220  IndirectPassManagers.push_back(Manager);
221  }
222 
223  // Print passes managed by this top level manager.
224  void dumpPasses() const;
225  void dumpArguments() const;
226 
227  // Active Pass Managers
229 
230 protected:
231 
232  /// Collection of pass managers
234 
235 private:
236 
237  /// Collection of pass managers that are not directly maintained
238  /// by this pass manager
239  SmallVector<PMDataManager *, 8> IndirectPassManagers;
240 
241  // Map to keep track of last user of the analysis pass.
242  // LastUser->second is the last user of Lastuser->first.
243  DenseMap<Pass *, Pass *> LastUser;
244 
245  // Map to keep track of passes that are last used by a pass.
246  // This inverse map is initialized at PM->run() based on
247  // LastUser map.
248  DenseMap<Pass *, SmallPtrSet<Pass *, 8> > InversedLastUser;
249 
250  /// Immutable passes are managed by top level manager.
251  SmallVector<ImmutablePass *, 8> ImmutablePasses;
252 
254 };
255 
256 
257 
258 //===----------------------------------------------------------------------===//
259 // PMDataManager
260 
261 /// PMDataManager provides the common place to manage the analysis data
262 /// used by pass managers.
264 public:
265 
266  explicit PMDataManager() : TPM(NULL), Depth(0) {
268  }
269 
270  virtual ~PMDataManager();
271 
272  virtual Pass *getAsPass() = 0;
273 
274  /// Augment AvailableAnalysis by adding analysis made available by pass P.
276 
277  /// verifyPreservedAnalysis -- Verify analysis presreved by pass P.
279 
280  /// Remove Analysis that is not preserved by the pass
282 
283  /// Remove dead passes used by P.
284  void removeDeadPasses(Pass *P, StringRef Msg,
285  enum PassDebuggingString);
286 
287  /// Remove P.
288  void freePass(Pass *P, StringRef Msg,
289  enum PassDebuggingString);
290 
291  /// Add pass P into the PassVector. Update
292  /// AvailableAnalysis appropriately if ProcessAnalysis is true.
293  void add(Pass *P, bool ProcessAnalysis = true);
294 
295  /// Add RequiredPass into list of lower level passes required by pass P.
296  /// RequiredPass is run on the fly by Pass Manager when P requests it
297  /// through getAnalysis interface.
298  virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
299 
300  virtual Pass *getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F);
301 
302  /// Initialize available analysis information.
304  AvailableAnalysis.clear();
305  for (unsigned i = 0; i < PMT_Last; ++i)
306  InheritedAnalysis[i] = NULL;
307  }
308 
309  // Return true if P preserves high level analysis used by other
310  // passes that are managed by this manager.
312 
313 
314  /// Populate RequiredPasses with analysis pass that are required by
315  /// pass P and are available. Populate ReqPassNotAvailable with analysis
316  /// pass that are required by pass P but are not available.
318  SmallVectorImpl<AnalysisID> &ReqPassNotAvailable,
319  Pass *P);
320 
321  /// All Required analyses should be available to the pass as it runs! Here
322  /// we fill in the AnalysisImpls member of the pass so that it can
323  /// successfully use the getAnalysis() method to retrieve the
324  /// implementations it needs.
326 
327  /// Find the pass that implements Analysis AID. If desired pass is not found
328  /// then return NULL.
329  Pass *findAnalysisPass(AnalysisID AID, bool Direction);
330 
331  // Access toplevel manager
334 
335  unsigned getDepth() const { return Depth; }
336  void setDepth(unsigned newDepth) { Depth = newDepth; }
337 
338  // Print routines used by debug-pass
339  void dumpLastUses(Pass *P, unsigned Offset) const;
340  void dumpPassArguments() const;
341  void dumpPassInfo(Pass *P, enum PassDebuggingString S1,
342  enum PassDebuggingString S2, StringRef Msg);
343  void dumpRequiredSet(const Pass *P) const;
344  void dumpPreservedSet(const Pass *P) const;
345 
346  unsigned getNumContainedPasses() const {
347  return (unsigned)PassVector.size();
348  }
349 
351  assert ( 0 && "Invalid use of getPassManagerType");
352  return PMT_Unknown;
353  }
354 
356  return &AvailableAnalysis;
357  }
358 
359  // Collect AvailableAnalysis from all the active Pass Managers.
361  unsigned Index = 0;
362  for (PMStack::iterator I = PMS.begin(), E = PMS.end();
363  I != E; ++I)
364  InheritedAnalysis[Index++] = (*I)->getAvailableAnalysis();
365  }
366 
367 protected:
368 
369  // Top level manager.
371 
372  // Collection of pass that are managed by this manager
374 
375  // Collection of Analysis provided by Parent pass manager and
376  // used by current pass manager. At at time there can not be more
377  // then PMT_Last active pass mangers.
379 
380  /// isPassDebuggingExecutionsOrMore - Return true if -debug-pass=Executions
381  /// or higher is specified.
382  bool isPassDebuggingExecutionsOrMore() const;
383 
384 private:
385  void dumpAnalysisUsage(StringRef Msg, const Pass *P,
386  const AnalysisUsage::VectorType &Set) const;
387 
388  // Set of available Analysis. This information is used while scheduling
389  // pass. If a pass requires an analysis which is not available then
390  // the required analysis pass is scheduled to run before the pass itself is
391  // scheduled to run.
392  DenseMap<AnalysisID, Pass*> AvailableAnalysis;
393 
394  // Collection of higher level analysis used by the pass managed by
395  // this manager.
396  SmallVector<Pass *, 8> HigherLevelAnalysis;
397 
398  unsigned Depth;
399 };
400 
401 //===----------------------------------------------------------------------===//
402 // FPPassManager
403 //
404 /// FPPassManager manages BBPassManagers and FunctionPasses.
405 /// It batches all function passes and basic block pass managers together and
406 /// sequence them to process one function at a time before processing next
407 /// function.
408 class FPPassManager : public ModulePass, public PMDataManager {
409 public:
410  static char ID;
411  explicit FPPassManager()
412  : ModulePass(ID), PMDataManager() { }
413 
414  /// run - Execute all of the passes scheduled for execution. Keep track of
415  /// whether any of the passes modifies the module, and if so, return true.
416  bool runOnFunction(Function &F);
417  bool runOnModule(Module &M);
418 
419  /// cleanup - After running all passes, clean up pass manager cache.
420  void cleanup();
421 
422  /// doInitialization - Overrides ModulePass doInitialization for global
423  /// initialization tasks
424  ///
426 
427  /// doInitialization - Run all of the initializers for the function passes.
428  ///
429  bool doInitialization(Module &M);
430 
431  /// doFinalization - Overrides ModulePass doFinalization for global
432  /// finalization tasks
433  ///
435 
436  /// doFinalization - Run all of the finalizers for the function passes.
437  ///
438  bool doFinalization(Module &M);
439 
440  virtual PMDataManager *getAsPMDataManager() { return this; }
441  virtual Pass *getAsPass() { return this; }
442 
443  /// Pass Manager itself does not invalidate any analysis info.
444  void getAnalysisUsage(AnalysisUsage &Info) const {
445  Info.setPreservesAll();
446  }
447 
448  // Print passes managed by this manager
449  void dumpPassStructure(unsigned Offset);
450 
451  virtual const char *getPassName() const {
452  return "Function Pass Manager";
453  }
454 
456  assert ( N < PassVector.size() && "Pass number out of range!");
457  FunctionPass *FP = static_cast<FunctionPass *>(PassVector[N]);
458  return FP;
459  }
460 
463  }
464 };
465 
466 Timer *getPassTimer(Pass *);
467 
468 }
469 
470 #endif
PMTopLevelManager * TPM
bool preserveHigherLevelAnalysis(Pass *P)
PassManagerType
Definition: Pass.h:55
PassManagerPrettyStackEntry(Pass *p, Value &v)
bool doFinalization(Module &M)
Pass * findAnalysisPass(AnalysisID AID, bool Direction)
virtual PMDataManager * getAsPMDataManager()
void dumpLastUses(Pass *P, unsigned Offset) const
The main container class for the LLVM Intermediate Representation.
Definition: Module.h:112
std::vector< PMDataManager * >::const_reverse_iterator iterator
unsigned getNumContainedManagers() const
virtual Pass * getAsPass()
void collectRequiredAnalysis(SmallVectorImpl< Pass * > &RequiredPasses, SmallVectorImpl< AnalysisID > &ReqPassNotAvailable, Pass *P)
void dumpRequiredSet(const Pass *P) const
bool doInitialization(Module &M)
F(f)
bool runOnModule(Module &M)
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()
Definition: Pass.cpp:123
virtual PassManagerType getPassManagerType() const
DenseMap< AnalysisID, Pass * > * InheritedAnalysis[PMT_Last]
Timer * getPassTimer(Pass *)
If TimingInfo is enabled then start pass timer.
void setDepth(unsigned newDepth)
void addPassManager(PMDataManager *Manager)
void freePass(Pass *P, StringRef Msg, enum PassDebuggingString)
Remove P.
bool isPassDebuggingExecutionsOrMore() const
virtual ~PMTopLevelManager()
Destructor.
virtual bool doFinalization(Module &)
Definition: Pass.h:115
void verifyPreservedAnalysis(Pass *P)
verifyPreservedAnalysis – Verify analysis presreved by pass P.
void initializeAnalysisInfo()
Initialize available analysis information.
virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass)
void populateInheritedAnalysis(PMStack &PMS)
void initializeAnalysisImpl(Pass *P)
unsigned getDepth() const
PassManagerPrettyStackEntry(Pass *p, Module &m)
iterator end() const
#define T
void add(Pass *P, bool ProcessAnalysis=true)
unsigned getNumContainedPasses() const
virtual Pass * getAsPass()=0
virtual const char * getPassName() const
virtual bool doInitialization(Module &)
Definition: Pass.h:110
#define P(N)
void dumpPreservedSet(const Pass *P) const
void addIndirectPassManager(PMDataManager *Manager)
void collectLastUses(SmallVectorImpl< Pass * > &LastUses, Pass *P)
Collect passes whose last user is P.
SmallVectorImpl< ImmutablePass * > & getImmutablePasses()
void getAnalysisUsage(AnalysisUsage &Info) const
Pass Manager itself does not invalidate any analysis info.
iterator begin() const
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
void recordAvailableAnalysis(Pass *P)
Augment AvailableAnalysis by adding analysis made available by pass P.
FPPassManager.
Definition: Pass.h:59
void removeNotPreservedAnalysis(Pass *P)
Remove Analysis that is not preserved by the pass.
void dump() const
bool runOnFunction(Function &F)
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
DenseMap< AnalysisID, Pass * > * getAvailableAnalysis()
AnalysisUsage * findAnalysisUsage(Pass *P)
Find analysis usage information for the pass P.
void setTopLevelManager(PMTopLevelManager *T)
SmallVector< PMDataManager *, 8 > PassManagers
Collection of pass managers.
virtual void print(raw_ostream &OS) const
print - Emit information about this stack frame to OS.
FunctionPass * getContainedPass(unsigned N)
void cleanup()
cleanup - After running all passes, clean up pass manager cache.
void dumpPassStructure(unsigned Offset)
Print passes managed by this manager.
#define I(x, y, z)
Definition: MD5.cpp:54
#define N
const void * AnalysisID
Definition: Pass.h:47
void push(PMDataManager *PM)
Pass * findAnalysisPass(AnalysisID AID)
void dumpPassArguments() const
LLVM Value Representation.
Definition: Value.h:66
PMTopLevelManager * getTopLevelManager()
virtual PassManagerType getPassManagerType() const