Add support for ImmutablePasses, which are not run, and cannot be
[oota-llvm.git] / lib / VMCore / PassManagerT.h
1 //===- PassManagerT.h - Container for Passes ---------------------*- C++ -*--=//
2 //
3 // This file defines the PassManagerT class.  This class is used to hold,
4 // maintain, and optimize execution of Pass's.  The PassManager class ensures
5 // that analysis results are available before a pass runs, and that Pass's are
6 // destroyed when the PassManager is destroyed.
7 //
8 // The PassManagerT template is instantiated three times to do its job.  The
9 // public PassManager class is a Pimpl around the PassManagerT<Module> interface
10 // to avoid having all of the PassManager clients being exposed to the
11 // implementation details herein.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_PASSMANAGER_T_H
16 #define LLVM_PASSMANAGER_T_H
17
18 #include "llvm/Pass.h"
19 #include "Support/CommandLine.h"
20 #include "Support/LeakDetector.h"
21 #include <algorithm>
22 #include <iostream>
23 class Annotable;
24
25 //===----------------------------------------------------------------------===//
26 // Pass debugging information.  Often it is useful to find out what pass is
27 // running when a crash occurs in a utility.  When this library is compiled with
28 // debugging on, a command line option (--debug-pass) is enabled that causes the
29 // pass name to be printed before it executes.
30 //
31
32 // Different debug levels that can be enabled...
33 enum PassDebugLevel {
34   None, Arguments, Structure, Executions, Details
35 };
36
37 static cl::opt<enum PassDebugLevel>
38 PassDebugging("debug-pass", cl::Hidden,
39               cl::desc("Print PassManager debugging information"),
40               cl::values(
41   clEnumVal(None      , "disable debug output"),
42   clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
43   clEnumVal(Structure , "print pass structure before run()"),
44   clEnumVal(Executions, "print pass name before it is executed"),
45   clEnumVal(Details   , "print pass details when it is executed"),
46                          0));
47
48 //===----------------------------------------------------------------------===//
49 // PMDebug class - a set of debugging functions, that are not to be
50 // instantiated by the template.
51 //
52 struct PMDebug {
53   static void PerformPassStartupStuff(Pass *P) {
54     // If debugging is enabled, print out argument information...
55     if (PassDebugging >= Arguments) {
56       std::cerr << "Pass Arguments: ";
57       PrintArgumentInformation(P);
58       std::cerr << "\n";
59
60       // Print the pass execution structure
61       if (PassDebugging >= Structure)
62         P->dumpPassStructure();
63     }
64   }
65
66   static void PrintArgumentInformation(const Pass *P);
67   static void PrintPassInformation(unsigned,const char*,Pass *, Annotable *);
68   static void PrintAnalysisSetInfo(unsigned,const char*,Pass *P,
69                                    const std::vector<AnalysisID> &);
70 };
71
72
73 //===----------------------------------------------------------------------===//
74 // TimingInfo Class - This class is used to calculate information about the
75 // amount of time each pass takes to execute.  This only happens when
76 // -time-passes is enabled on the command line.
77 //
78 struct TimeRecord {      // TimeRecord - Data we collect and print for each pass
79   double Elapsed;        // Wall clock time elapsed in seconds
80   double UserTime;       // User time elapsed
81   double SystemTime;     // System time elapsed
82   unsigned long MaxRSS;  // Maximum resident set size (in bytes)
83   unsigned long RSSTemp; // Temp for calculating maxrss
84
85   TimeRecord() : Elapsed(0), UserTime(0), SystemTime(0), MaxRSS(0) {}
86   void passStart(const TimeRecord &T);
87   void passEnd(const TimeRecord &T);
88   void sum(const TimeRecord &TR);
89   bool operator<(const TimeRecord &TR) const;
90
91   void print(const char *PassName, const TimeRecord &TotalTime) const;
92 };
93
94 class TimingInfo {
95   std::map<Pass*, TimeRecord> TimingData;
96   TimingInfo() {}   // Private ctor, must use create member
97 public:
98   // Create method.  If Timing is enabled, this creates and returns a new timing
99   // object, otherwise it returns null.
100   //
101   static TimingInfo *create();
102
103   // TimingDtor - Print out information about timing information
104   ~TimingInfo();
105
106   void passStarted(Pass *P);
107   void passEnded(Pass *P);
108 };
109
110 //===----------------------------------------------------------------------===//
111 // Declare the PassManagerTraits which will be specialized...
112 //
113 template<class UnitType> class PassManagerTraits;   // Do not define.
114
115
116 //===----------------------------------------------------------------------===//
117 // PassManagerT - Container object for passes.  The PassManagerT destructor
118 // deletes all passes contained inside of the PassManagerT, so you shouldn't 
119 // delete passes manually, and all passes should be dynamically allocated.
120 //
121 template<typename UnitType>
122 class PassManagerT : public PassManagerTraits<UnitType>,public AnalysisResolver{
123   typedef PassManagerTraits<UnitType> Traits;
124   typedef typename Traits::PassClass       PassClass;
125   typedef typename Traits::SubPassClass SubPassClass;
126   typedef typename Traits::BatcherClass BatcherClass;
127   typedef typename Traits::ParentClass   ParentClass;
128
129   friend typename Traits::PassClass;
130   friend typename Traits::SubPassClass;  
131   friend class Traits;
132   friend class ImmutablePass;
133
134   std::vector<PassClass*> Passes;    // List of passes to run
135   std::vector<ImmutablePass*> ImmutablePasses;  // List of immutable passes
136
137   // The parent of this pass manager...
138   ParentClass * const Parent;
139
140   // The current batcher if one is in use, or null
141   BatcherClass *Batcher;
142
143   // CurrentAnalyses - As the passes are being run, this map contains the
144   // analyses that are available to the current pass for use.  This is accessed
145   // through the getAnalysis() function in this class and in Pass.
146   //
147   std::map<AnalysisID, Pass*> CurrentAnalyses;
148
149   // LastUseOf - This map keeps track of the last usage in our pipeline of a
150   // particular pass.  When executing passes, the memory for .first is free'd
151   // after .second is run.
152   //
153   std::map<Pass*, Pass*> LastUseOf;
154
155 public:
156   PassManagerT(ParentClass *Par = 0) : Parent(Par), Batcher(0) {}
157   ~PassManagerT() {
158     // Delete all of the contained passes...
159     for (typename std::vector<PassClass*>::iterator
160            I = Passes.begin(), E = Passes.end(); I != E; ++I)
161       delete *I;
162
163     for (std::vector<ImmutablePass*>::iterator
164            I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
165       delete *I;
166   }
167
168   // run - Run all of the queued passes on the specified module in an optimal
169   // way.
170   virtual bool runOnUnit(UnitType *M) {
171     bool MadeChanges = false;
172     closeBatcher();
173     CurrentAnalyses.clear();
174
175     // Add any immutable passes to the CurrentAnalyses set...
176     for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i)
177       if (const PassInfo *PI = ImmutablePasses[i]->getPassInfo()) {
178         CurrentAnalyses[PI] = ImmutablePasses[i];
179
180         const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
181         for (unsigned i = 0, e = II.size(); i != e; ++i)
182           CurrentAnalyses[II[i]] = ImmutablePasses[i];
183       }
184
185
186     // LastUserOf - This contains the inverted LastUseOfMap...
187     std::map<Pass *, std::vector<Pass*> > LastUserOf;
188     for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(),
189                                           E = LastUseOf.end(); I != E; ++I)
190       LastUserOf[I->second].push_back(I->first);
191
192
193     // Output debug information...
194     if (Parent == 0) PMDebug::PerformPassStartupStuff(this);
195
196     // Run all of the passes
197     for (unsigned i = 0, e = Passes.size(); i < e; ++i) {
198       PassClass *P = Passes[i];
199       
200       PMDebug::PrintPassInformation(getDepth(), "Executing Pass", P,
201                                     (Annotable*)M);
202
203       // Get information about what analyses the pass uses...
204       AnalysisUsage AnUsage;
205       P->getAnalysisUsage(AnUsage);
206       PMDebug::PrintAnalysisSetInfo(getDepth(), "Required", P,
207                                     AnUsage.getRequiredSet());
208
209       // All Required analyses should be available to the pass as it runs!  Here
210       // we fill in the AnalysisImpls member of the pass so that it can
211       // successfully use the getAnalysis() method to retrieve the
212       // implementations it needs.
213       //
214       P->AnalysisImpls.clear();
215       P->AnalysisImpls.reserve(AnUsage.getRequiredSet().size());
216       for (std::vector<const PassInfo *>::const_iterator
217              I = AnUsage.getRequiredSet().begin(), 
218              E = AnUsage.getRequiredSet().end(); I != E; ++I) {
219         Pass *Impl = getAnalysisOrNullUp(*I);
220         if (Impl == 0) {
221           std::cerr << "Analysis '" << (*I)->getPassName()
222                     << "' used but not available!";
223           assert(0 && "Analysis used but not available!");
224         } else if (PassDebugging == Details) {
225           if ((*I)->getPassName() != std::string(Impl->getPassName()))
226             std::cerr << "    Interface '" << (*I)->getPassName()
227                     << "' implemented by '" << Impl->getPassName() << "'\n";
228         }
229         P->AnalysisImpls.push_back(std::make_pair(*I, Impl));
230       }
231
232       // Run the sub pass!
233       startPass(P);
234       bool Changed = runPass(P, M);
235       endPass(P);
236       MadeChanges |= Changed;
237
238       // Check for memory leaks by the pass...
239       LeakDetector::checkForGarbage(std::string("after running pass '") +
240                                     P->getPassName() + "'");
241
242       if (Changed)
243         PMDebug::PrintPassInformation(getDepth()+1, "Made Modification", P,
244                                       (Annotable*)M);
245       PMDebug::PrintAnalysisSetInfo(getDepth(), "Preserved", P,
246                                     AnUsage.getPreservedSet());
247
248
249       // Erase all analyses not in the preserved set...
250       if (!AnUsage.preservesAll()) {
251         const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
252         for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(),
253                E = CurrentAnalyses.end(); I != E; )
254           if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) !=
255               PreservedSet.end())
256             ++I; // This analysis is preserved, leave it in the available set...
257           else {
258             if (!dynamic_cast<ImmutablePass*>(I->second)) {
259 #if MAP_DOESNT_HAVE_BROKEN_ERASE_MEMBER
260               I = CurrentAnalyses.erase(I);   // Analysis not preserved!
261 #else
262               // GCC 2.95.3 STL doesn't have correct erase member!
263               CurrentAnalyses.erase(I);
264               I = CurrentAnalyses.begin();
265 #endif
266             } else {
267               ++I;
268             }
269           }
270       }
271
272       // Add the current pass to the set of passes that have been run, and are
273       // thus available to users.
274       //
275       if (const PassInfo *PI = P->getPassInfo()) {
276         CurrentAnalyses[PI] = P;
277
278         // This pass is the current implementation of all of the interfaces it
279         // implements as well.
280         //
281         const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
282         for (unsigned i = 0, e = II.size(); i != e; ++i)
283           CurrentAnalyses[II[i]] = P;
284       }
285
286       // Free memory for any passes that we are the last use of...
287       std::vector<Pass*> &DeadPass = LastUserOf[P];
288       for (std::vector<Pass*>::iterator I = DeadPass.begin(),E = DeadPass.end();
289            I != E; ++I) {
290         PMDebug::PrintPassInformation(getDepth()+1, "Freeing Pass", *I,
291                                       (Annotable*)M);
292         (*I)->releaseMemory();
293       }
294     }
295     return MadeChanges;
296   }
297
298   // dumpPassStructure - Implement the -debug-passes=PassStructure option
299   virtual void dumpPassStructure(unsigned Offset = 0) {
300     std::cerr << std::string(Offset*2, ' ') << Traits::getPMName()
301               << " Pass Manager\n";
302     for (typename std::vector<PassClass*>::iterator
303            I = Passes.begin(), E = Passes.end(); I != E; ++I) {
304       PassClass *P = *I;
305       P->dumpPassStructure(Offset+1);
306
307       // Loop through and see which classes are destroyed after this one...
308       for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(),
309                                             E = LastUseOf.end(); I != E; ++I) {
310         if (P == I->second) {
311           std::cerr << "--" << std::string(Offset*2, ' ');
312           I->first->dumpPassStructure(0);
313         }
314       }
315     }
316   }
317
318   Pass *getAnalysisOrNullDown(const PassInfo *ID) const {
319     std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID);
320
321     if (I != CurrentAnalyses.end())
322       return I->second;  // Found it.
323
324     if (Batcher)
325       return ((AnalysisResolver*)Batcher)->getAnalysisOrNullDown(ID);
326     return 0;
327   }
328
329   Pass *getAnalysisOrNullUp(const PassInfo *ID) const {
330     std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID);
331     if (I != CurrentAnalyses.end())
332       return I->second;  // Found it.
333
334     if (Parent)          // Try scanning...
335       return Parent->getAnalysisOrNullUp(ID);
336     return 0;
337   }
338
339   // {start/end}Pass - Called when a pass is started, it just propogates
340   // information up to the top level PassManagerT object to tell it that a pass
341   // has started or ended.  This is used to gather timing information about
342   // passes.
343   //
344   void startPass(Pass *P) {
345     if (Parent) Parent->startPass(P);
346     else PassStarted(P);
347   }
348   void endPass(Pass *P) {
349     if (Parent) Parent->endPass(P);
350     else PassEnded(P);
351   }
352
353   // markPassUsed - Inform higher level pass managers (and ourselves)
354   // that these analyses are being used by this pass.  This is used to
355   // make sure that analyses are not free'd before we have to use
356   // them...
357   //
358   void markPassUsed(const PassInfo *P, Pass *User) {
359     std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(P);
360
361     if (I != CurrentAnalyses.end()) {
362       LastUseOf[I->second] = User;    // Local pass, extend the lifetime
363     } else {
364       // Pass not in current available set, must be a higher level pass
365       // available to us, propogate to parent pass manager...  We tell the
366       // parent that we (the passmanager) are using the analysis so that it
367       // frees the analysis AFTER this pass manager runs.
368       //
369       if (Parent) {
370         Parent->markPassUsed(P, this);
371       } else {
372         assert(0 && "Pass available but not found! "
373                "Perhaps this is a module pass requiring a function pass?");
374       }
375     }
376   }
377   
378   // Return the number of parent PassManagers that exist
379   virtual unsigned getDepth() const {
380     if (Parent == 0) return 0;
381     return 1 + Parent->getDepth();
382   }
383
384   virtual unsigned getNumContainedPasses() const { return Passes.size(); }
385   virtual const Pass *getContainedPass(unsigned N) const {
386     assert(N < Passes.size() && "Pass number out of range!");
387     return Passes[N];
388   }
389
390   // add - Add a pass to the queue of passes to run.  This gives ownership of
391   // the Pass to the PassManager.  When the PassManager is destroyed, the pass
392   // will be destroyed as well, so there is no need to delete the pass.  This
393   // implies that all passes MUST be new'd.
394   //
395   void add(PassClass *P) {
396     // Get information about what analyses the pass uses...
397     AnalysisUsage AnUsage;
398     P->getAnalysisUsage(AnUsage);
399     const std::vector<AnalysisID> &Required = AnUsage.getRequiredSet();
400
401     // Loop over all of the analyses used by this pass,
402     for (std::vector<AnalysisID>::const_iterator I = Required.begin(),
403            E = Required.end(); I != E; ++I) {
404       if (getAnalysisOrNullDown(*I) == 0)
405         add((PassClass*)(*I)->createPass());
406     }
407
408     // Tell the pass to add itself to this PassManager... the way it does so
409     // depends on the class of the pass, and is critical to laying out passes in
410     // an optimal order..
411     //
412     P->addToPassManager(this, AnUsage);
413   }
414
415 private:
416
417   // addPass - These functions are used to implement the subclass specific
418   // behaviors present in PassManager.  Basically the add(Pass*) method ends up
419   // reflecting its behavior into a Pass::addToPassManager call.  Subclasses of
420   // Pass override it specifically so that they can reflect the type
421   // information inherent in "this" back to the PassManager.
422   //
423   // For generic Pass subclasses (which are interprocedural passes), we simply
424   // add the pass to the end of the pass list and terminate any accumulation of
425   // FunctionPass's that are present.
426   //
427   void addPass(PassClass *P, AnalysisUsage &AnUsage) {
428     const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
429
430     // FIXME: If this pass being added isn't killed by any of the passes in the
431     // batcher class then we can reorder to pass to execute before the batcher
432     // does, which will potentially allow us to batch more passes!
433     //
434     //const std::vector<AnalysisID> &ProvidedSet = AnUsage.getProvidedSet();
435     if (Batcher /*&& ProvidedSet.empty()*/)
436       closeBatcher();                     // This pass cannot be batched!
437     
438     // Set the Resolver instance variable in the Pass so that it knows where to 
439     // find this object...
440     //
441     setAnalysisResolver(P, this);
442     Passes.push_back(P);
443
444     // Inform higher level pass managers (and ourselves) that these analyses are
445     // being used by this pass.  This is used to make sure that analyses are not
446     // free'd before we have to use them...
447     //
448     for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
449            E = RequiredSet.end(); I != E; ++I)
450       markPassUsed(*I, P);     // Mark *I as used by P
451
452     // Erase all analyses not in the preserved set...
453     if (!AnUsage.preservesAll()) {
454       const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
455       for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(),
456              E = CurrentAnalyses.end(); I != E; ) {
457         if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
458             PreservedSet.end()) {             // Analysis not preserved!
459           CurrentAnalyses.erase(I);           // Remove from available analyses
460           I = CurrentAnalyses.begin();
461         } else {
462           ++I;
463         }
464       }
465     }
466
467     // Add this pass to the currently available set...
468     if (const PassInfo *PI = P->getPassInfo()) {
469       CurrentAnalyses[PI] = P;
470
471       // This pass is the current implementation of all of the interfaces it
472       // implements as well.
473       //
474       const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
475       for (unsigned i = 0, e = II.size(); i != e; ++i)
476         CurrentAnalyses[II[i]] = P;
477     }
478
479     // For now assume that our results are never used...
480     LastUseOf[P] = P;
481   }
482   
483   // For FunctionPass subclasses, we must be sure to batch the FunctionPass's
484   // together in a BatcherClass object so that all of the analyses are run
485   // together a function at a time.
486   //
487   void addPass(SubPassClass *MP, AnalysisUsage &AnUsage) {
488     if (Batcher == 0) // If we don't have a batcher yet, make one now.
489       Batcher = new BatcherClass(this);
490     // The Batcher will queue the passes up
491     MP->addToPassManager(Batcher, AnUsage);
492   }
493
494   // closeBatcher - Terminate the batcher that is being worked on.
495   void closeBatcher() {
496     if (Batcher) {
497       Passes.push_back(Batcher);
498       Batcher = 0;
499     }
500   }
501
502 public:
503   // When an ImmutablePass is added, it gets added to the top level pass
504   // manager.
505   void addPass(ImmutablePass *IP, AnalysisUsage &AU) {
506     if (Parent) { // Make sure this request goes to the top level passmanager...
507       Parent->addPass(IP, AU);
508       return;
509     }
510
511     // Set the Resolver instance variable in the Pass so that it knows where to 
512     // find this object...
513     //
514     setAnalysisResolver(IP, this);
515     ImmutablePasses.push_back(IP);
516     
517     // Add this pass to the currently available set...
518     if (const PassInfo *PI = IP->getPassInfo()) {
519       CurrentAnalyses[PI] = IP;
520
521       // This pass is the current implementation of all of the interfaces it
522       // implements as well.
523       //
524       const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
525       for (unsigned i = 0, e = II.size(); i != e; ++i)
526         CurrentAnalyses[II[i]] = IP;
527     }
528   }
529 };
530
531
532
533 //===----------------------------------------------------------------------===//
534 // PassManagerTraits<BasicBlock> Specialization
535 //
536 // This pass manager is used to group together all of the BasicBlockPass's
537 // into a single unit.
538 //
539 template<> struct PassManagerTraits<BasicBlock> : public BasicBlockPass {
540   // PassClass - The type of passes tracked by this PassManager
541   typedef BasicBlockPass PassClass;
542
543   // SubPassClass - The types of classes that should be collated together
544   // This is impossible to match, so BasicBlock instantiations of PassManagerT
545   // do not collate.
546   //
547   typedef PassManagerT<Module> SubPassClass;
548
549   // BatcherClass - The type to use for collation of subtypes... This class is
550   // never instantiated for the PassManager<BasicBlock>, but it must be an 
551   // instance of PassClass to typecheck.
552   //
553   typedef PassClass BatcherClass;
554
555   // ParentClass - The type of the parent PassManager...
556   typedef PassManagerT<Function> ParentClass;
557
558   // PMType - The type of the passmanager that subclasses this class
559   typedef PassManagerT<BasicBlock> PMType;
560
561   // runPass - Specify how the pass should be run on the UnitType
562   static bool runPass(PassClass *P, BasicBlock *M) {
563     // todo, init and finalize
564     return P->runOnBasicBlock(*M);
565   }
566
567   // Dummy implementation of PassStarted/PassEnded
568   static void PassStarted(Pass *P) {}
569   static void PassEnded(Pass *P) {}
570
571   // getPMName() - Return the name of the unit the PassManager operates on for
572   // debugging.
573   const char *getPMName() const { return "BasicBlock"; }
574   virtual const char *getPassName() const { return "BasicBlock Pass Manager"; }
575
576   // Implement the BasicBlockPass interface...
577   virtual bool doInitialization(Module &M);
578   virtual bool doInitialization(Function &F);
579   virtual bool runOnBasicBlock(BasicBlock &BB);
580   virtual bool doFinalization(Function &F);
581   virtual bool doFinalization(Module &M);
582
583   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
584     AU.setPreservesAll();
585   }
586 };
587
588
589
590 //===----------------------------------------------------------------------===//
591 // PassManagerTraits<Function> Specialization
592 //
593 // This pass manager is used to group together all of the FunctionPass's
594 // into a single unit.
595 //
596 template<> struct PassManagerTraits<Function> : public FunctionPass {
597   // PassClass - The type of passes tracked by this PassManager
598   typedef FunctionPass PassClass;
599
600   // SubPassClass - The types of classes that should be collated together
601   typedef BasicBlockPass SubPassClass;
602
603   // BatcherClass - The type to use for collation of subtypes...
604   typedef PassManagerT<BasicBlock> BatcherClass;
605
606   // ParentClass - The type of the parent PassManager...
607   typedef PassManagerT<Module> ParentClass;
608
609   // PMType - The type of the passmanager that subclasses this class
610   typedef PassManagerT<Function> PMType;
611
612   // runPass - Specify how the pass should be run on the UnitType
613   static bool runPass(PassClass *P, Function *F) {
614     return P->runOnFunction(*F);
615   }
616
617   // Dummy implementation of PassStarted/PassEnded
618   static void PassStarted(Pass *P) {}
619   static void PassEnded(Pass *P) {}
620
621   // getPMName() - Return the name of the unit the PassManager operates on for
622   // debugging.
623   const char *getPMName() const { return "Function"; }
624   virtual const char *getPassName() const { return "Function Pass Manager"; }
625
626   // Implement the FunctionPass interface...
627   virtual bool doInitialization(Module &M);
628   virtual bool runOnFunction(Function &F);
629   virtual bool doFinalization(Module &M);
630
631   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
632     AU.setPreservesAll();
633   }
634 };
635
636
637
638 //===----------------------------------------------------------------------===//
639 // PassManagerTraits<Module> Specialization
640 //
641 // This is the top level PassManager implementation that holds generic passes.
642 //
643 template<> struct PassManagerTraits<Module> : public Pass {
644   // PassClass - The type of passes tracked by this PassManager
645   typedef Pass PassClass;
646
647   // SubPassClass - The types of classes that should be collated together
648   typedef FunctionPass SubPassClass;
649
650   // BatcherClass - The type to use for collation of subtypes...
651   typedef PassManagerT<Function> BatcherClass;
652
653   // ParentClass - The type of the parent PassManager...
654   typedef AnalysisResolver ParentClass;
655
656   // runPass - Specify how the pass should be run on the UnitType
657   static bool runPass(PassClass *P, Module *M) { return P->run(*M); }
658
659   // getPMName() - Return the name of the unit the PassManager operates on for
660   // debugging.
661   const char *getPMName() const { return "Module"; }
662   virtual const char *getPassName() const { return "Module Pass Manager"; }
663
664   // TimingInformation - This data member maintains timing information for each
665   // of the passes that is executed.
666   //
667   TimingInfo *TimeInfo;
668
669   // PassStarted/Ended - This callback is notified any time a pass is started
670   // or stops.  This is used to collect timing information about the different
671   // passes being executed.
672   //
673   void PassStarted(Pass *P) {
674     if (TimeInfo) TimeInfo->passStarted(P);
675   }
676   void PassEnded(Pass *P) {
677     if (TimeInfo) TimeInfo->passEnded(P);
678   }
679
680   // run - Implement the PassManager interface...
681   bool run(Module &M) {
682     TimeInfo = TimingInfo::create();
683     bool Result = ((PassManagerT<Module>*)this)->runOnUnit(&M);
684     if (TimeInfo) {
685       delete TimeInfo;
686       TimeInfo = 0;
687     }
688     return Result;
689   }
690
691   // PassManagerTraits constructor - Create a timing info object if the user
692   // specified timing info should be collected on the command line.
693   //
694   PassManagerTraits() : TimeInfo(0) {}
695 };
696
697
698
699 //===----------------------------------------------------------------------===//
700 // PassManagerTraits Method Implementations
701 //
702
703 // PassManagerTraits<BasicBlock> Implementations
704 //
705 inline bool PassManagerTraits<BasicBlock>::doInitialization(Module &M) {
706   bool Changed = false;
707   for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
708     ((PMType*)this)->Passes[i]->doInitialization(M);
709   return Changed;
710 }
711
712 inline bool PassManagerTraits<BasicBlock>::doInitialization(Function &F) {
713   bool Changed = false;
714   for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
715     ((PMType*)this)->Passes[i]->doInitialization(F);
716   return Changed;
717 }
718
719 inline bool PassManagerTraits<BasicBlock>::runOnBasicBlock(BasicBlock &BB) {
720   return ((PMType*)this)->runOnUnit(&BB);
721 }
722
723 inline bool PassManagerTraits<BasicBlock>::doFinalization(Function &F) {
724   bool Changed = false;
725   for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
726     ((PMType*)this)->Passes[i]->doFinalization(F);
727   return Changed;
728 }
729
730 inline bool PassManagerTraits<BasicBlock>::doFinalization(Module &M) {
731   bool Changed = false;
732   for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
733     ((PMType*)this)->Passes[i]->doFinalization(M);
734   return Changed;
735 }
736
737
738 // PassManagerTraits<Function> Implementations
739 //
740 inline bool PassManagerTraits<Function>::doInitialization(Module &M) {
741   bool Changed = false;
742   for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
743     ((PMType*)this)->Passes[i]->doInitialization(M);
744   return Changed;
745 }
746
747 inline bool PassManagerTraits<Function>::runOnFunction(Function &F) {
748   return ((PMType*)this)->runOnUnit(&F);
749 }
750
751 inline bool PassManagerTraits<Function>::doFinalization(Module &M) {
752   bool Changed = false;
753   for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
754     ((PMType*)this)->Passes[i]->doFinalization(M);
755   return Changed;
756 }
757
758 #endif