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