Add default constructor to APSInt
[oota-llvm.git] / include / llvm / PassManagers.h
1 //===- llvm/PassManager.h - Pass Inftrastructre classes  --------*- 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 #include "llvm/PassManager.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/ADT/SmallPtrSet.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include <deque>
19 #include <map>
20
21 //===----------------------------------------------------------------------===//
22 // Overview:
23 // The Pass Manager Infrastructure manages passes. It's responsibilities are:
24 // 
25 //   o Manage optimization pass execution order
26 //   o Make required Analysis information available before pass P is run
27 //   o Release memory occupied by dead passes
28 //   o If Analysis information is dirtied by a pass then regenerate Analysis 
29 //     information before it is consumed by another pass.
30 //
31 // Pass Manager Infrastructure uses multiple pass managers.  They are
32 // PassManager, FunctionPassManager, MPPassManager, FPPassManager, BBPassManager.
33 // This class hierarchy uses multiple inheritance but pass managers do not
34 // derive from another pass manager.
35 //
36 // PassManager and FunctionPassManager are two top-level pass manager that
37 // represents the external interface of this entire pass manager infrastucture.
38 //
39 // Important classes :
40 //
41 // [o] class PMTopLevelManager;
42 //
43 // Two top level managers, PassManager and FunctionPassManager, derive from 
44 // PMTopLevelManager. PMTopLevelManager manages information used by top level 
45 // managers such as last user info.
46 //
47 // [o] class PMDataManager;
48 //
49 // PMDataManager manages information, e.g. list of available analysis info, 
50 // used by a pass manager to manage execution order of passes. It also provides
51 // a place to implement common pass manager APIs. All pass managers derive from
52 // PMDataManager.
53 //
54 // [o] class BBPassManager : public FunctionPass, public PMDataManager;
55 //
56 // BBPassManager manages BasicBlockPasses.
57 //
58 // [o] class FunctionPassManager;
59 //
60 // This is a external interface used by JIT to manage FunctionPasses. This
61 // interface relies on FunctionPassManagerImpl to do all the tasks.
62 //
63 // [o] class FunctionPassManagerImpl : public ModulePass, PMDataManager,
64 //                                     public PMTopLevelManager;
65 //
66 // FunctionPassManagerImpl is a top level manager. It manages FPPassManagers
67 //
68 // [o] class FPPassManager : public ModulePass, public PMDataManager;
69 //
70 // FPPassManager manages FunctionPasses and BBPassManagers
71 //
72 // [o] class MPPassManager : public Pass, public PMDataManager;
73 //
74 // MPPassManager manages ModulePasses and FPPassManagers
75 //
76 // [o] class PassManager;
77 //
78 // This is a external interface used by various tools to manages passes. It
79 // relies on PassManagerImpl to do all the tasks.
80 //
81 // [o] class PassManagerImpl : public Pass, public PMDataManager,
82 //                             public PMDTopLevelManager
83 //
84 // PassManagerImpl is a top level pass manager responsible for managing
85 // MPPassManagers.
86 //===----------------------------------------------------------------------===//
87
88 #ifndef PASSMANAGERS_H
89 #define PASSMANAGERS_H
90
91 #include "llvm/Pass.h"
92 #include <deque>
93
94 namespace llvm {
95
96 /// FunctionPassManager and PassManager, two top level managers, serve 
97 /// as the public interface of pass manager infrastructure.
98 enum TopLevelManagerType {
99   TLM_Function,  // FunctionPassManager
100   TLM_Pass       // PassManager
101 };
102     
103 // enums for debugging strings
104 enum PassDebuggingString {
105   EXECUTION_MSG, // "Executing Pass '"
106   MODIFICATION_MSG, // "' Made Modification '"
107   FREEING_MSG, // " Freeing Pass '"
108   ON_BASICBLOCK_MSG, // "'  on BasicBlock '" + PassName + "'...\n"
109   ON_FUNCTION_MSG, // "' on Function '" + FunctionName + "'...\n"
110   ON_MODULE_MSG, // "' on Module '" + ModuleName + "'...\n"
111   ON_LOOP_MSG, // " 'on Loop ...\n'"
112   ON_CG_MSG // "' on Call Graph ...\n'"
113 };  
114
115 //===----------------------------------------------------------------------===//
116 // PMStack
117 //
118 /// PMStack
119 /// Top level pass managers (see PassManager.cpp) maintain active Pass Managers 
120 /// using PMStack. Each Pass implements assignPassManager() to connect itself
121 /// with appropriate manager. assignPassManager() walks PMStack to find
122 /// suitable manager.
123 ///
124 /// PMStack is just a wrapper around standard deque that overrides pop() and
125 /// push() methods.
126 class PMStack {
127 public:
128   typedef std::deque<PMDataManager *>::reverse_iterator iterator;
129   iterator begin() { return S.rbegin(); }
130   iterator end() { return S.rend(); }
131
132   void handleLastUserOverflow();
133
134   void pop();
135   inline PMDataManager *top() { return S.back(); }
136   void push(PMDataManager *PM);
137   inline bool empty() { return S.empty(); }
138
139   void dump();
140 private:
141   std::deque<PMDataManager *> S;
142 };
143
144
145 //===----------------------------------------------------------------------===//
146 // PMTopLevelManager
147 //
148 /// PMTopLevelManager manages LastUser info and collects common APIs used by
149 /// top level pass managers.
150 class PMTopLevelManager {
151 public:
152
153   virtual unsigned getNumContainedManagers() const {
154     return (unsigned)PassManagers.size();
155   }
156
157   /// Schedule pass P for execution. Make sure that passes required by
158   /// P are run before P is run. Update analysis info maintained by
159   /// the manager. Remove dead passes. This is a recursive function.
160   void schedulePass(Pass *P);
161
162   /// This is implemented by top level pass manager and used by 
163   /// schedulePass() to add analysis info passes that are not available.
164   virtual void addTopLevelPass(Pass  *P) = 0;
165
166   /// Set pass P as the last user of the given analysis passes.
167   void setLastUser(SmallVector<Pass *, 12> &AnalysisPasses, Pass *P);
168
169   /// Collect passes whose last user is P
170   void collectLastUses(SmallVector<Pass *, 12> &LastUses, Pass *P);
171
172   /// Find the pass that implements Analysis AID. Search immutable
173   /// passes and all pass managers. If desired pass is not found
174   /// then return NULL.
175   Pass *findAnalysisPass(AnalysisID AID);
176
177   /// Find analysis usage information for the pass P.
178   AnalysisUsage *findAnalysisUsage(Pass *P);
179
180   explicit PMTopLevelManager(enum TopLevelManagerType t);
181   virtual ~PMTopLevelManager(); 
182
183   /// Add immutable pass and initialize it.
184   inline void addImmutablePass(ImmutablePass *P) {
185     P->initializePass();
186     ImmutablePasses.push_back(P);
187   }
188
189   inline SmallVector<ImmutablePass *, 8>& getImmutablePasses() {
190     return ImmutablePasses;
191   }
192
193   void addPassManager(PMDataManager *Manager) {
194     PassManagers.push_back(Manager);
195   }
196
197   // Add Manager into the list of managers that are not directly
198   // maintained by this top level pass manager
199   inline void addIndirectPassManager(PMDataManager *Manager) {
200     IndirectPassManagers.push_back(Manager);
201   }
202
203   // Print passes managed by this top level manager.
204   void dumpPasses() const;
205   void dumpArguments() const;
206
207   void initializeAllAnalysisInfo();
208
209   // Active Pass Managers
210   PMStack activeStack;
211
212 protected:
213   
214   /// Collection of pass managers
215   SmallVector<PMDataManager *, 8> PassManagers;
216
217 private:
218
219   /// Collection of pass managers that are not directly maintained
220   /// by this pass manager
221   SmallVector<PMDataManager *, 8> IndirectPassManagers;
222
223   // Map to keep track of last user of the analysis pass.
224   // LastUser->second is the last user of Lastuser->first.
225   DenseMap<Pass *, Pass *> LastUser;
226
227   // Map to keep track of passes that are last used by a pass.
228   // This inverse map is initialized at PM->run() based on
229   // LastUser map.
230   DenseMap<Pass *, SmallPtrSet<Pass *, 8> > InversedLastUser;
231
232   /// Immutable passes are managed by top level manager.
233   SmallVector<ImmutablePass *, 8> ImmutablePasses;
234
235   DenseMap<Pass *, AnalysisUsage *> AnUsageMap;
236 };
237
238
239   
240 //===----------------------------------------------------------------------===//
241 // PMDataManager
242
243 /// PMDataManager provides the common place to manage the analysis data
244 /// used by pass managers.
245 class PMDataManager {
246 public:
247
248   explicit PMDataManager(int Depth) : TPM(NULL), Depth(Depth) {
249     initializeAnalysisInfo();
250   }
251
252   virtual ~PMDataManager();
253
254   /// Augment AvailableAnalysis by adding analysis made available by pass P.
255   void recordAvailableAnalysis(Pass *P);
256
257   /// verifyPreservedAnalysis -- Verify analysis presreved by pass P.
258   void verifyPreservedAnalysis(Pass *P);
259
260   /// verifyDomInfo -- Verify dominator information if it is available.
261   void verifyDomInfo(Pass &P, Function &F);
262
263   /// Remove Analysis that is not preserved by the pass
264   void removeNotPreservedAnalysis(Pass *P);
265   
266   /// Remove dead passes
267   void removeDeadPasses(Pass *P, const char *Msg, enum PassDebuggingString);
268
269   /// Add pass P into the PassVector. Update 
270   /// AvailableAnalysis appropriately if ProcessAnalysis is true.
271   void add(Pass *P, bool ProcessAnalysis = true);
272
273   /// Add RequiredPass into list of lower level passes required by pass P.
274   /// RequiredPass is run on the fly by Pass Manager when P requests it
275   /// through getAnalysis interface.
276   virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
277
278   virtual Pass * getOnTheFlyPass(Pass *P, const PassInfo *PI, Function &F) {
279     assert (0 && "Unable to find on the fly pass");
280     return NULL;
281   }
282
283   /// Initialize available analysis information.
284   void initializeAnalysisInfo() { 
285     AvailableAnalysis.clear();
286     for (unsigned i = 0; i < PMT_Last; ++i)
287       InheritedAnalysis[i] = NULL;
288   }
289
290   // Return true if P preserves high level analysis used by other
291   // passes that are managed by this manager.
292   bool preserveHigherLevelAnalysis(Pass *P);
293
294
295   /// Populate RequiredPasses with analysis pass that are required by
296   /// pass P and are available. Populate ReqPassNotAvailable with analysis
297   /// pass that are required by pass P but are not available.
298   void collectRequiredAnalysis(SmallVector<Pass *, 8> &RequiredPasses,
299                                SmallVector<AnalysisID, 8> &ReqPassNotAvailable,
300                                Pass *P);
301
302   /// All Required analyses should be available to the pass as it runs!  Here
303   /// we fill in the AnalysisImpls member of the pass so that it can
304   /// successfully use the getAnalysis() method to retrieve the
305   /// implementations it needs.
306   void initializeAnalysisImpl(Pass *P);
307
308   /// Find the pass that implements Analysis AID. If desired pass is not found
309   /// then return NULL.
310   Pass *findAnalysisPass(AnalysisID AID, bool Direction);
311
312   // Access toplevel manager
313   PMTopLevelManager *getTopLevelManager() { return TPM; }
314   void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
315
316   unsigned getDepth() const { return Depth; }
317
318   // Print routines used by debug-pass
319   void dumpLastUses(Pass *P, unsigned Offset) const;
320   void dumpPassArguments() const;
321   void dumpPassInfo(Pass *P, enum PassDebuggingString S1,
322                     enum PassDebuggingString S2, const char *Msg);
323   void dumpRequiredSet(const Pass *P) const;
324   void dumpPreservedSet(const Pass *P) const;
325
326   virtual unsigned getNumContainedPasses() const {
327     return (unsigned)PassVector.size();
328   }
329
330   virtual PassManagerType getPassManagerType() const { 
331     assert ( 0 && "Invalid use of getPassManagerType");
332     return PMT_Unknown; 
333   }
334
335   std::map<AnalysisID, Pass*> *getAvailableAnalysis() {
336     return &AvailableAnalysis;
337   }
338
339   // Collect AvailableAnalysis from all the active Pass Managers.
340   void populateInheritedAnalysis(PMStack &PMS) {
341     unsigned Index = 0;
342     for (PMStack::iterator I = PMS.begin(), E = PMS.end();
343          I != E; ++I)
344       InheritedAnalysis[Index++] = (*I)->getAvailableAnalysis();
345   }
346
347 protected:
348
349   // Top level manager.
350   PMTopLevelManager *TPM;
351
352   // Collection of pass that are managed by this manager
353   SmallVector<Pass *, 16> PassVector;
354
355   // Collection of Analysis provided by Parent pass manager and
356   // used by current pass manager. At at time there can not be more
357   // then PMT_Last active pass mangers.
358   std::map<AnalysisID, Pass *> *InheritedAnalysis[PMT_Last];
359
360 private:
361   void dumpAnalysisUsage(const char *Msg, const Pass *P,
362                            const AnalysisUsage::VectorType &Set) const;
363
364   // Set of available Analysis. This information is used while scheduling 
365   // pass. If a pass requires an analysis which is not not available then 
366   // equired analysis pass is scheduled to run before the pass itself is 
367   // scheduled to run.
368   std::map<AnalysisID, Pass*> AvailableAnalysis;
369
370   // Collection of higher level analysis used by the pass managed by
371   // this manager.
372   SmallVector<Pass *, 8> HigherLevelAnalysis;
373
374   unsigned Depth;
375 };
376
377 //===----------------------------------------------------------------------===//
378 // FPPassManager
379 //
380 /// FPPassManager manages BBPassManagers and FunctionPasses.
381 /// It batches all function passes and basic block pass managers together and 
382 /// sequence them to process one function at a time before processing next 
383 /// function.
384
385 class FPPassManager : public ModulePass, public PMDataManager {
386  
387 public:
388   static char ID;
389   explicit FPPassManager(int Depth) 
390   : ModulePass(intptr_t(&ID)), PMDataManager(Depth) { }
391   
392   /// run - Execute all of the passes scheduled for execution.  Keep track of
393   /// whether any of the passes modifies the module, and if so, return true.
394   bool runOnFunction(Function &F);
395   bool runOnModule(Module &M);
396
397   /// doInitialization - Run all of the initializers for the function passes.
398   ///
399   bool doInitialization(Module &M);
400   
401   /// doFinalization - Run all of the finalizers for the function passes.
402   ///
403   bool doFinalization(Module &M);
404
405   /// Pass Manager itself does not invalidate any analysis info.
406   void getAnalysisUsage(AnalysisUsage &Info) const {
407     Info.setPreservesAll();
408   }
409
410   // Print passes managed by this manager
411   void dumpPassStructure(unsigned Offset);
412
413   virtual const char *getPassName() const {
414     return "Function Pass Manager";
415   }
416
417   FunctionPass *getContainedPass(unsigned N) {
418     assert ( N < PassVector.size() && "Pass number out of range!");
419     FunctionPass *FP = static_cast<FunctionPass *>(PassVector[N]);
420     return FP;
421   }
422
423   virtual PassManagerType getPassManagerType() const { 
424     return PMT_FunctionPassManager; 
425   }
426 };
427
428 }
429
430 extern void StartPassTimer(llvm::Pass *);
431 extern void StopPassTimer(llvm::Pass *);
432
433 #endif
434