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