Move TimingInfo into PassManagers.h so that other libs can use it.
[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/Support/Timer.h"
16
17 using namespace llvm;
18 class llvm::PMDataManager;
19 class llvm::PMStack;
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 hierarcy uses multiple inheritance but pass managers do not derive
34 // 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 namespace llvm {
89
90 /// FunctionPassManager and PassManager, two top level managers, serve 
91 /// as the public interface of pass manager infrastructure.
92 enum TopLevelManagerType {
93   TLM_Function,  // FunctionPassManager
94   TLM_Pass       // PassManager
95 };
96     
97 //===----------------------------------------------------------------------===//
98 // PMTopLevelManager
99 //
100 /// PMTopLevelManager manages LastUser info and collects common APIs used by
101 /// top level pass managers.
102 class PMTopLevelManager {
103 public:
104
105   virtual unsigned getNumContainedManagers() {
106     return PassManagers.size();
107   }
108
109   /// Schedule pass P for execution. Make sure that passes required by
110   /// P are run before P is run. Update analysis info maintained by
111   /// the manager. Remove dead passes. This is a recursive function.
112   void schedulePass(Pass *P);
113
114   /// This is implemented by top level pass manager and used by 
115   /// schedulePass() to add analysis info passes that are not available.
116   virtual void addTopLevelPass(Pass  *P) = 0;
117
118   /// Set pass P as the last user of the given analysis passes.
119   void setLastUser(std::vector<Pass *> &AnalysisPasses, Pass *P);
120
121   /// Collect passes whose last user is P
122   void collectLastUses(std::vector<Pass *> &LastUses, Pass *P);
123
124   /// Find the pass that implements Analysis AID. Search immutable
125   /// passes and all pass managers. If desired pass is not found
126   /// then return NULL.
127   Pass *findAnalysisPass(AnalysisID AID);
128
129   PMTopLevelManager(enum TopLevelManagerType t);
130   virtual ~PMTopLevelManager(); 
131
132   /// Add immutable pass and initialize it.
133   inline void addImmutablePass(ImmutablePass *P) {
134     P->initializePass();
135     ImmutablePasses.push_back(P);
136   }
137
138   inline std::vector<ImmutablePass *>& getImmutablePasses() {
139     return ImmutablePasses;
140   }
141
142   void addPassManager(Pass *Manager) {
143     PassManagers.push_back(Manager);
144   }
145
146   // Add Manager into the list of managers that are not directly
147   // maintained by this top level pass manager
148   inline void addIndirectPassManager(PMDataManager *Manager) {
149     IndirectPassManagers.push_back(Manager);
150   }
151
152   // Print passes managed by this top level manager.
153   void dumpPasses() const;
154   void dumpArguments() const;
155
156   void initializeAllAnalysisInfo();
157
158   // Active Pass Managers
159   PMStack activeStack;
160
161 protected:
162   
163   /// Collection of pass managers
164   std::vector<Pass *> PassManagers;
165
166 private:
167
168   /// Collection of pass managers that are not directly maintained
169   /// by this pass manager
170   std::vector<PMDataManager *> IndirectPassManagers;
171
172   // Map to keep track of last user of the analysis pass.
173   // LastUser->second is the last user of Lastuser->first.
174   std::map<Pass *, Pass *> LastUser;
175
176   /// Immutable passes are managed by top level manager.
177   std::vector<ImmutablePass *> ImmutablePasses;
178 };
179
180
181   
182 //===----------------------------------------------------------------------===//
183 // PMDataManager
184
185 /// PMDataManager provides the common place to manage the analysis data
186 /// used by pass managers.
187 class PMDataManager {
188 public:
189   PMDataManager(int Depth) : TPM(NULL), Depth(Depth) {
190     initializeAnalysisInfo();
191   }
192
193   virtual ~PMDataManager();
194
195   /// Return true IFF pass P's required analysis set does not required new
196   /// manager.
197   bool manageablePass(Pass *P);
198
199   /// Augment AvailableAnalysis by adding analysis made available by pass P.
200   void recordAvailableAnalysis(Pass *P);
201
202   /// Remove Analysis that is not preserved by the pass
203   void removeNotPreservedAnalysis(Pass *P);
204   
205   /// Remove dead passes
206   void removeDeadPasses(Pass *P, std::string &Msg);
207
208   /// Add pass P into the PassVector. Update 
209   /// AvailableAnalysis appropriately if ProcessAnalysis is true.
210   void add(Pass *P, bool ProcessAnalysis = true);
211
212   /// Initialize available analysis information.
213   void initializeAnalysisInfo() { 
214     TransferLastUses.clear();
215     AvailableAnalysis.clear();
216   }
217
218   /// Populate RequiredPasses with the analysis pass that are required by
219   /// pass P.
220   void collectRequiredAnalysisPasses(std::vector<Pass *> &RequiredPasses,
221                                      Pass *P);
222
223   /// All Required analyses should be available to the pass as it runs!  Here
224   /// we fill in the AnalysisImpls member of the pass so that it can
225   /// successfully use the getAnalysis() method to retrieve the
226   /// implementations it needs.
227   void initializeAnalysisImpl(Pass *P);
228
229   /// Find the pass that implements Analysis AID. If desired pass is not found
230   /// then return NULL.
231   Pass *findAnalysisPass(AnalysisID AID, bool Direction);
232
233   // Access toplevel manager
234   PMTopLevelManager *getTopLevelManager() { return TPM; }
235   void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
236
237   unsigned getDepth() const { return Depth; }
238
239   // Print routines used by debug-pass
240   void dumpLastUses(Pass *P, unsigned Offset) const;
241   void dumpPassArguments() const;
242   void dumpPassInfo(Pass *P,  std::string &Msg1, std::string &Msg2) const;
243   void dumpAnalysisSetInfo(const char *Msg, Pass *P,
244                            const std::vector<AnalysisID> &Set) const;
245
246   std::vector<Pass *>& getTransferredLastUses() {
247     return TransferLastUses;
248   }
249
250   virtual unsigned getNumContainedPasses() { 
251     return PassVector.size();
252   }
253
254   virtual PassManagerType getPassManagerType() { 
255     assert ( 0 && "Invalid use of getPassManagerType");
256     return PMT_Unknown; 
257   }
258 protected:
259
260   // If a FunctionPass F is the last user of ModulePass info M
261   // then the F's manager, not F, records itself as a last user of M.
262   // Current pass manage is requesting parent manager to record parent
263   // manager as the last user of these TrransferLastUses passes.
264   std::vector<Pass *> TransferLastUses;
265
266   // Top level manager.
267   PMTopLevelManager *TPM;
268
269   // Collection of pass that are managed by this manager
270   std::vector<Pass *> PassVector;
271
272 private:
273   // Set of available Analysis. This information is used while scheduling 
274   // pass. If a pass requires an analysis which is not not available then 
275   // equired analysis pass is scheduled to run before the pass itself is 
276   // scheduled to run.
277   std::map<AnalysisID, Pass*> AvailableAnalysis;
278
279   unsigned Depth;
280 };
281
282 //===----------------------------------------------------------------------===//
283 // FPPassManager
284 //
285 /// FPPassManager manages BBPassManagers and FunctionPasses.
286 /// It batches all function passes and basic block pass managers together and 
287 /// sequence them to process one function at a time before processing next 
288 /// function.
289
290 class FPPassManager : public ModulePass, public PMDataManager {
291  
292 public:
293   FPPassManager(int Depth) : PMDataManager(Depth) { }
294   
295   /// run - Execute all of the passes scheduled for execution.  Keep track of
296   /// whether any of the passes modifies the module, and if so, return true.
297   bool runOnFunction(Function &F);
298   bool runOnModule(Module &M);
299
300   /// doInitialization - Run all of the initializers for the function passes.
301   ///
302   bool doInitialization(Module &M);
303   
304   /// doFinalization - Run all of the initializers for the function passes.
305   ///
306   bool doFinalization(Module &M);
307
308   /// Pass Manager itself does not invalidate any analysis info.
309   void getAnalysisUsage(AnalysisUsage &Info) const {
310     Info.setPreservesAll();
311   }
312
313   // Print passes managed by this manager
314   void dumpPassStructure(unsigned Offset);
315
316   FunctionPass *getContainedPass(unsigned N) {
317     assert ( N < PassVector.size() && "Pass number out of range!");
318     FunctionPass *FP = static_cast<FunctionPass *>(PassVector[N]);
319     return FP;
320   }
321
322   virtual PassManagerType getPassManagerType() { 
323     return PMT_FunctionPassManager; 
324   }
325 };
326
327 //===----------------------------------------------------------------------===//
328 // TimingInfo Class - This class is used to calculate information about the
329 // amount of time each pass takes to execute.  This only happens when
330 // -time-passes is enabled on the command line.
331 //
332
333 class TimingInfo {
334   std::map<Pass*, Timer> TimingData;
335   TimerGroup TG;
336
337 public:
338   // Use 'create' member to get this.
339   TimingInfo() : TG("... Pass execution timing report ...") {}
340   
341   // TimingDtor - Print out information about timing information
342   ~TimingInfo() {
343     // Delete all of the timers...
344     TimingData.clear();
345     // TimerGroup is deleted next, printing the report.
346   }
347
348   // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
349   // to a non null value (if the -time-passes option is enabled) or it leaves it
350   // null.  It may be called multiple times.
351   static void createTheTimeInfo();
352
353   void passStarted(Pass *P) {
354
355     if (dynamic_cast<PMDataManager *>(P)) 
356       return;
357
358     std::map<Pass*, Timer>::iterator I = TimingData.find(P);
359     if (I == TimingData.end())
360       I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
361     I->second.startTimer();
362   }
363   void passEnded(Pass *P) {
364
365     if (dynamic_cast<PMDataManager *>(P)) 
366       return;
367
368     std::map<Pass*, Timer>::iterator I = TimingData.find(P);
369     assert (I != TimingData.end() && "passStarted/passEnded not nested right!");
370     I->second.stopTimer();
371   }
372 };
373
374 extern TimingInfo *getTheTimeInfo();
375 }
376