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