Make 64bit args and float args work correct with calls. Thanks to Chris
[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 "llvm/Support/CommandLine.h"
27 #include "llvm/Support/LeakDetector.h"
28 #include "llvm/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                          clEnumValEnd));
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 #if defined(_MSC_VER) || defined(__INTEL_COMPILER) || defined(__HP_aCC)
146   friend PassClass;
147   friend SubPassClass;
148 #else
149   friend class PassManagerTraits<UnitType>::PassClass;
150   friend class PassManagerTraits<UnitType>::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         Pass *AP = (*I)->createPass();
459         if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (AP)) { add(IP); }
460         else if (PassClass *RP = dynamic_cast<PassClass *> (AP)) { add(RP); }
461         else { assert (0 && "Wrong kind of pass for this PassManager"); }
462       }
463     }
464
465     // Tell the pass to add itself to this PassManager... the way it does so
466     // depends on the class of the pass, and is critical to laying out passes in
467     // an optimal order..
468     //
469     P->addToPassManager(this, AnUsage);
470   }
471
472   // add - H4x0r an ImmutablePass into a PassManager that might not be
473   // expecting one.
474   //
475   void add(ImmutablePass *P) {
476     // Get information about what analyses the pass uses...
477     AnalysisUsage AnUsage;
478     P->getAnalysisUsage(AnUsage);
479     const std::vector<AnalysisID> &Required = AnUsage.getRequiredSet();
480
481     // Loop over all of the analyses used by this pass,
482     for (std::vector<AnalysisID>::const_iterator I = Required.begin(),
483            E = Required.end(); I != E; ++I) {
484       if (getAnalysisOrNullDown(*I) == 0) {
485         Pass *AP = (*I)->createPass();
486         if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (AP)) add(IP);
487         else if (PassClass *RP = dynamic_cast<PassClass *> (AP)) add(RP);
488         else assert (0 && "Wrong kind of pass for this PassManager");
489       }
490     }
491
492     // Add the ImmutablePass to this PassManager.
493     addPass(P, AnUsage);
494   }
495
496 private:
497   // addPass - These functions are used to implement the subclass specific
498   // behaviors present in PassManager.  Basically the add(Pass*) method ends up
499   // reflecting its behavior into a Pass::addToPassManager call.  Subclasses of
500   // Pass override it specifically so that they can reflect the type
501   // information inherent in "this" back to the PassManager.
502   //
503   // For generic Pass subclasses (which are interprocedural passes), we simply
504   // add the pass to the end of the pass list and terminate any accumulation of
505   // FunctionPass's that are present.
506   //
507   void addPass(PassClass *P, AnalysisUsage &AnUsage) {
508     const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
509
510     // FIXME: If this pass being added isn't killed by any of the passes in the
511     // batcher class then we can reorder to pass to execute before the batcher
512     // does, which will potentially allow us to batch more passes!
513     //
514     //const std::vector<AnalysisID> &ProvidedSet = AnUsage.getProvidedSet();
515     if (Batcher /*&& ProvidedSet.empty()*/)
516       closeBatcher();                     // This pass cannot be batched!
517     
518     // Set the Resolver instance variable in the Pass so that it knows where to 
519     // find this object...
520     //
521     setAnalysisResolver(P, this);
522     Passes.push_back(P);
523
524     // Inform higher level pass managers (and ourselves) that these analyses are
525     // being used by this pass.  This is used to make sure that analyses are not
526     // free'd before we have to use them...
527     //
528     for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
529            E = RequiredSet.end(); I != E; ++I)
530       markPassUsed(*I, P);     // Mark *I as used by P
531
532     // Erase all analyses not in the preserved set...
533     if (!AnUsage.getPreservesAll()) {
534       const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
535       for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(),
536              E = CurrentAnalyses.end(); I != E; ) {
537         if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
538             PreservedSet.end()) {             // Analysis not preserved!
539           CurrentAnalyses.erase(I);           // Remove from available analyses
540           I = CurrentAnalyses.begin();
541         } else {
542           ++I;
543         }
544       }
545     }
546
547     // Add this pass to the currently available set...
548     if (const PassInfo *PI = P->getPassInfo()) {
549       CurrentAnalyses[PI] = P;
550
551       // This pass is the current implementation of all of the interfaces it
552       // implements as well.
553       //
554       const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
555       for (unsigned i = 0, e = II.size(); i != e; ++i)
556         CurrentAnalyses[II[i]] = P;
557     }
558
559     // For now assume that our results are never used...
560     LastUseOf[P] = P;
561   }
562   
563   // For FunctionPass subclasses, we must be sure to batch the FunctionPass's
564   // together in a BatcherClass object so that all of the analyses are run
565   // together a function at a time.
566   //
567   void addPass(SubPassClass *MP, AnalysisUsage &AnUsage) {
568     if (Batcher == 0) // If we don't have a batcher yet, make one now.
569       Batcher = new BatcherClass(this);
570     // The Batcher will queue the passes up
571     MP->addToPassManager(Batcher, AnUsage);
572   }
573
574   // closeBatcher - Terminate the batcher that is being worked on.
575   void closeBatcher() {
576     if (Batcher) {
577       Passes.push_back(Batcher);
578       Batcher = 0;
579     }
580   }
581
582 public:
583   // When an ImmutablePass is added, it gets added to the top level pass
584   // manager.
585   void addPass(ImmutablePass *IP, AnalysisUsage &AU) {
586     if (Parent) { // Make sure this request goes to the top level passmanager...
587       Parent->addPass(IP, AU);
588       return;
589     }
590
591     // Set the Resolver instance variable in the Pass so that it knows where to 
592     // find this object...
593     //
594     setAnalysisResolver(IP, this);
595     ImmutablePasses.push_back(IP);
596
597     // All Required analyses should be available to the pass as it initializes!
598     // Here we fill in the AnalysisImpls member of the pass so that it can
599     // successfully use the getAnalysis() method to retrieve the implementations
600     // it needs.
601     //
602     IP->AnalysisImpls.clear();
603     IP->AnalysisImpls.reserve(AU.getRequiredSet().size());
604     for (std::vector<const PassInfo *>::const_iterator 
605            I = AU.getRequiredSet().begin(),
606            E = AU.getRequiredSet().end(); I != E; ++I) {
607       Pass *Impl = getAnalysisOrNullUp(*I);
608       if (Impl == 0) {
609         std::cerr << "Analysis '" << (*I)->getPassName()
610                   << "' used but not available!";
611         assert(0 && "Analysis used but not available!");
612       } else if (PassDebugging == Details) {
613         if ((*I)->getPassName() != std::string(Impl->getPassName()))
614           std::cerr << "    Interface '" << (*I)->getPassName()
615                     << "' implemented by '" << Impl->getPassName() << "'\n";
616       }
617       IP->AnalysisImpls.push_back(std::make_pair(*I, Impl));
618     }
619     
620     // Initialize the immutable pass...
621     IP->initializePass();
622   }
623 };
624
625
626
627 //===----------------------------------------------------------------------===//
628 // PassManagerTraits<BasicBlock> Specialization
629 //
630 // This pass manager is used to group together all of the BasicBlockPass's
631 // into a single unit.
632 //
633 template<> class PassManagerTraits<BasicBlock> : public BasicBlockPass {
634 public:
635   // PassClass - The type of passes tracked by this PassManager
636   typedef BasicBlockPass PassClass;
637
638   // SubPassClass - The types of classes that should be collated together
639   // This is impossible to match, so BasicBlock instantiations of PassManagerT
640   // do not collate.
641   //
642   typedef PassManagerT<Module> SubPassClass;
643
644   // BatcherClass - The type to use for collation of subtypes... This class is
645   // never instantiated for the PassManager<BasicBlock>, but it must be an 
646   // instance of PassClass to typecheck.
647   //
648   typedef PassClass BatcherClass;
649
650   // ParentClass - The type of the parent PassManager...
651   typedef PassManagerT<Function> ParentClass;
652
653   // PMType - The type of the passmanager that subclasses this class
654   typedef PassManagerT<BasicBlock> PMType;
655
656   // runPass - Specify how the pass should be run on the UnitType
657   static bool runPass(PassClass *P, BasicBlock *M) {
658     // todo, init and finalize
659     return P->runOnBasicBlock(*M);
660   }
661
662   // getPMName() - Return the name of the unit the PassManager operates on for
663   // debugging.
664   const char *getPMName() const { return "BasicBlock"; }
665   virtual const char *getPassName() const { return "BasicBlock Pass Manager"; }
666
667   // Implement the BasicBlockPass interface...
668   virtual bool doInitialization(Module &M);
669   virtual bool doInitialization(Function &F);
670   virtual bool runOnBasicBlock(BasicBlock &BB);
671   virtual bool doFinalization(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<Function> Specialization
683 //
684 // This pass manager is used to group together all of the FunctionPass's
685 // into a single unit.
686 //
687 template<> class PassManagerTraits<Function> : public FunctionPass {
688 public:
689   // PassClass - The type of passes tracked by this PassManager
690   typedef FunctionPass PassClass;
691
692   // SubPassClass - The types of classes that should be collated together
693   typedef BasicBlockPass SubPassClass;
694
695   // BatcherClass - The type to use for collation of subtypes...
696   typedef PassManagerT<BasicBlock> BatcherClass;
697
698   // ParentClass - The type of the parent PassManager...
699   typedef PassManagerT<Module> ParentClass;
700
701   // PMType - The type of the passmanager that subclasses this class
702   typedef PassManagerT<Function> PMType;
703
704   // runPass - Specify how the pass should be run on the UnitType
705   static bool runPass(PassClass *P, Function *F) {
706     return P->runOnFunction(*F);
707   }
708
709   // getPMName() - Return the name of the unit the PassManager operates on for
710   // debugging.
711   const char *getPMName() const { return "Function"; }
712   virtual const char *getPassName() const { return "Function Pass Manager"; }
713
714   // Implement the FunctionPass interface...
715   virtual bool doInitialization(Module &M);
716   virtual bool runOnFunction(Function &F);
717   virtual bool doFinalization(Module &M);
718
719   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
720     AU.setPreservesAll();
721   }
722 };
723
724
725
726 //===----------------------------------------------------------------------===//
727 // PassManagerTraits<Module> Specialization
728 //
729 // This is the top level PassManager implementation that holds generic passes.
730 //
731 template<> class PassManagerTraits<Module> : public ModulePass {
732 public:
733   // PassClass - The type of passes tracked by this PassManager
734   typedef ModulePass PassClass;
735
736   // SubPassClass - The types of classes that should be collated together
737   typedef FunctionPass SubPassClass;
738
739   // BatcherClass - The type to use for collation of subtypes...
740   typedef PassManagerT<Function> BatcherClass;
741
742   // ParentClass - The type of the parent PassManager...
743   typedef AnalysisResolver ParentClass;
744
745   // runPass - Specify how the pass should be run on the UnitType
746   static bool runPass(PassClass *P, Module *M) { return P->runOnModule(*M); }
747
748   // getPMName() - Return the name of the unit the PassManager operates on for
749   // debugging.
750   const char *getPMName() const { return "Module"; }
751   virtual const char *getPassName() const { return "Module Pass Manager"; }
752
753   // runOnModule - Implement the PassManager interface.
754   bool runOnModule(Module &M) {
755     return ((PassManagerT<Module>*)this)->runOnUnit(&M);
756   }
757 };
758
759
760
761 //===----------------------------------------------------------------------===//
762 // PassManagerTraits Method Implementations
763 //
764
765 // PassManagerTraits<BasicBlock> Implementations
766 //
767 inline bool PassManagerTraits<BasicBlock>::doInitialization(Module &M) {
768   bool Changed = false;
769   for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
770     ((PMType*)this)->Passes[i]->doInitialization(M);
771   return Changed;
772 }
773
774 inline bool PassManagerTraits<BasicBlock>::doInitialization(Function &F) {
775   bool Changed = false;
776   for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
777     ((PMType*)this)->Passes[i]->doInitialization(F);
778   return Changed;
779 }
780
781 inline bool PassManagerTraits<BasicBlock>::runOnBasicBlock(BasicBlock &BB) {
782   return ((PMType*)this)->runOnUnit(&BB);
783 }
784
785 inline bool PassManagerTraits<BasicBlock>::doFinalization(Function &F) {
786   bool Changed = false;
787   for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
788     ((PMType*)this)->Passes[i]->doFinalization(F);
789   return Changed;
790 }
791
792 inline bool PassManagerTraits<BasicBlock>::doFinalization(Module &M) {
793   bool Changed = false;
794   for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
795     ((PMType*)this)->Passes[i]->doFinalization(M);
796   return Changed;
797 }
798
799
800 // PassManagerTraits<Function> Implementations
801 //
802 inline bool PassManagerTraits<Function>::doInitialization(Module &M) {
803   bool Changed = false;
804   for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
805     ((PMType*)this)->Passes[i]->doInitialization(M);
806   return Changed;
807 }
808
809 inline bool PassManagerTraits<Function>::runOnFunction(Function &F) {
810   return ((PMType*)this)->runOnUnit(&F);
811 }
812
813 inline bool PassManagerTraits<Function>::doFinalization(Module &M) {
814   bool Changed = false;
815   for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
816     ((PMType*)this)->Passes[i]->doFinalization(M);
817   return Changed;
818 }
819
820 } // End llvm namespace
821
822 #endif