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