76302db4e9ebda60097c1e5285021de9b819b3f3
[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 using namespace llvm;
17 class llvm::PMDataManager;
18 class llvm::PMStack;
19
20 //===----------------------------------------------------------------------===//
21 // Overview:
22 // The Pass Manager Infrastructure manages passes. It's responsibilities are:
23 // 
24 //   o Manage optimization pass execution order
25 //   o Make required Analysis information available before pass P is run
26 //   o Release memory occupied by dead passes
27 //   o If Analysis information is dirtied by a pass then regenerate Analysis 
28 //     information before it is consumed by another pass.
29 //
30 // Pass Manager Infrastructure uses multiple pass managers.  They are
31 // PassManager, FunctionPassManager, MPPassManager, FPPassManager, BBPassManager.
32 // This class hierarcy uses multiple inheritance but pass managers do not derive
33 // from another pass manager.
34 //
35 // PassManager and FunctionPassManager are two top-level pass manager that
36 // represents the external interface of this entire pass manager infrastucture.
37 //
38 // Important classes :
39 //
40 // [o] class PMTopLevelManager;
41 //
42 // Two top level managers, PassManager and FunctionPassManager, derive from 
43 // PMTopLevelManager. PMTopLevelManager manages information used by top level 
44 // managers such as last user info.
45 //
46 // [o] class PMDataManager;
47 //
48 // PMDataManager manages information, e.g. list of available analysis info, 
49 // used by a pass manager to manage execution order of passes. It also provides
50 // a place to implement common pass manager APIs. All pass managers derive from
51 // PMDataManager.
52 //
53 // [o] class BBPassManager : public FunctionPass, public PMDataManager;
54 //
55 // BBPassManager manages BasicBlockPasses.
56 //
57 // [o] class FunctionPassManager;
58 //
59 // This is a external interface used by JIT to manage FunctionPasses. This
60 // interface relies on FunctionPassManagerImpl to do all the tasks.
61 //
62 // [o] class FunctionPassManagerImpl : public ModulePass, PMDataManager,
63 //                                     public PMTopLevelManager;
64 //
65 // FunctionPassManagerImpl is a top level manager. It manages FPPassManagers
66 //
67 // [o] class FPPassManager : public ModulePass, public PMDataManager;
68 //
69 // FPPassManager manages FunctionPasses and BBPassManagers
70 //
71 // [o] class MPPassManager : public Pass, public PMDataManager;
72 //
73 // MPPassManager manages ModulePasses and FPPassManagers
74 //
75 // [o] class PassManager;
76 //
77 // This is a external interface used by various tools to manages passes. It
78 // relies on PassManagerImpl to do all the tasks.
79 //
80 // [o] class PassManagerImpl : public Pass, public PMDataManager,
81 //                             public PMDTopLevelManager
82 //
83 // PassManagerImpl is a top level pass manager responsible for managing
84 // MPPassManagers.
85 //===----------------------------------------------------------------------===//
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(Pass *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<Pass *> 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   /// Return true IFF pass P's required analysis set does not required new
208   /// manager.
209   bool manageablePass(Pass *P);
210
211   /// Augment AvailableAnalysis by adding analysis made available by pass P.
212   void recordAvailableAnalysis(Pass *P);
213
214   /// verifyPreservedAnalysis -- Verify analysis presreved by pass P.
215   void verifyPreservedAnalysis(Pass *P);
216
217   /// Remove Analysis that is not preserved by the pass
218   void removeNotPreservedAnalysis(Pass *P);
219   
220   /// Remove dead passes
221   void removeDeadPasses(Pass *P, std::string Msg, enum PassDebuggingString);
222
223   /// Add pass P into the PassVector. Update 
224   /// AvailableAnalysis appropriately if ProcessAnalysis is true.
225   void add(Pass *P, bool ProcessAnalysis = true);
226
227   /// Add RequiredPass into list of lower level passes required by pass P.
228   /// RequiredPass is run on the fly by Pass Manager when P requests it
229   /// through getAnalysis interface.
230   virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
231
232   virtual Pass * getOnTheFlyPass(Pass *P, const PassInfo *PI, Function &F) {
233     assert (0 && "Unable to find on the fly pass");
234     return NULL;
235   }
236
237   /// Initialize available analysis information.
238   void initializeAnalysisInfo() { 
239     AvailableAnalysis.clear();
240     for (unsigned i = 0; i < PMT_Last; ++i)
241       InheritedAnalysis[i] = NULL;
242   }
243
244   // Return true if P preserves high level analysis used by other
245   // passes that are managed by this manager.
246   bool preserveHigherLevelAnalysis(Pass *P);
247
248
249   /// Populate RequiredPasses with analysis pass that are required by
250   /// pass P and are available. Populate ReqPassNotAvailable with analysis
251   /// pass that are required by pass P but are not available.
252   void collectRequiredAnalysis(SmallVector<Pass *, 8> &RequiredPasses,
253                                SmallVector<AnalysisID, 8> &ReqPassNotAvailable,
254                                Pass *P);
255
256   /// All Required analyses should be available to the pass as it runs!  Here
257   /// we fill in the AnalysisImpls member of the pass so that it can
258   /// successfully use the getAnalysis() method to retrieve the
259   /// implementations it needs.
260   void initializeAnalysisImpl(Pass *P);
261
262   /// Find the pass that implements Analysis AID. If desired pass is not found
263   /// then return NULL.
264   Pass *findAnalysisPass(AnalysisID AID, bool Direction);
265
266   // Access toplevel manager
267   PMTopLevelManager *getTopLevelManager() { return TPM; }
268   void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
269
270   unsigned getDepth() const { return Depth; }
271
272   // Print routines used by debug-pass
273   void dumpLastUses(Pass *P, unsigned Offset) const;
274   void dumpPassArguments() const;
275   void dumpPassInfo(Pass *P, enum PassDebuggingString S1,
276                     enum PassDebuggingString S2, std::string Msg);
277   void dumpAnalysisSetInfo(const char *Msg, Pass *P,
278                            const std::vector<AnalysisID> &Set) const;
279
280   virtual unsigned getNumContainedPasses() { 
281     return PassVector.size();
282   }
283
284   virtual PassManagerType getPassManagerType() const { 
285     assert ( 0 && "Invalid use of getPassManagerType");
286     return PMT_Unknown; 
287   }
288
289   std::map<AnalysisID, Pass*> *getAvailableAnalysis() {
290     return &AvailableAnalysis;
291   }
292
293   // Collect AvailableAnalysis from all the active Pass Managers.
294   void populateInheritedAnalysis(PMStack &PMS) {
295     unsigned Index = 0;
296     for (PMStack::iterator I = PMS.begin(), E = PMS.end();
297          I != E; ++I)
298       InheritedAnalysis[Index++] = (*I)->getAvailableAnalysis();
299   }
300
301 protected:
302
303   // Top level manager.
304   PMTopLevelManager *TPM;
305
306   // Collection of pass that are managed by this manager
307   std::vector<Pass *> PassVector;
308
309   // Collection of Analysis provided by Parent pass manager and
310   // used by current pass manager. At at time there can not be more
311   // then PMT_Last active pass mangers.
312   std::map<AnalysisID, Pass *> *InheritedAnalysis[PMT_Last];
313
314 private:
315   // Set of available Analysis. This information is used while scheduling 
316   // pass. If a pass requires an analysis which is not not available then 
317   // equired analysis pass is scheduled to run before the pass itself is 
318   // scheduled to run.
319   std::map<AnalysisID, Pass*> AvailableAnalysis;
320
321   // Collection of higher level analysis used by the pass managed by
322   // this manager.
323   std::vector<Pass *> HigherLevelAnalysis;
324
325   unsigned Depth;
326 };
327
328 //===----------------------------------------------------------------------===//
329 // FPPassManager
330 //
331 /// FPPassManager manages BBPassManagers and FunctionPasses.
332 /// It batches all function passes and basic block pass managers together and 
333 /// sequence them to process one function at a time before processing next 
334 /// function.
335
336 class FPPassManager : public ModulePass, public PMDataManager {
337  
338 public:
339   static char ID;
340   explicit FPPassManager(int Depth) 
341   : ModulePass((intptr_t)&ID), PMDataManager(Depth) { }
342   
343   /// run - Execute all of the passes scheduled for execution.  Keep track of
344   /// whether any of the passes modifies the module, and if so, return true.
345   bool runOnFunction(Function &F);
346   bool runOnModule(Module &M);
347
348   /// doInitialization - Run all of the initializers for the function passes.
349   ///
350   bool doInitialization(Module &M);
351   
352   /// doFinalization - Run all of the finalizers for the function passes.
353   ///
354   bool doFinalization(Module &M);
355
356   /// Pass Manager itself does not invalidate any analysis info.
357   void getAnalysisUsage(AnalysisUsage &Info) const {
358     Info.setPreservesAll();
359   }
360
361   // Print passes managed by this manager
362   void dumpPassStructure(unsigned Offset);
363
364   virtual const char *getPassName() const {
365     return "Function Pass Manager";
366   }
367
368   FunctionPass *getContainedPass(unsigned N) {
369     assert ( N < PassVector.size() && "Pass number out of range!");
370     FunctionPass *FP = static_cast<FunctionPass *>(PassVector[N]);
371     return FP;
372   }
373
374   virtual PassManagerType getPassManagerType() const { 
375     return PMT_FunctionPassManager; 
376   }
377 };
378
379 }
380
381 extern void StartPassTimer(Pass *);
382 extern void StopPassTimer(Pass *);