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