23a26130fc37f52c6bd34375cfbfc3bb8ea203ec
[oota-llvm.git] / lib / VMCore / PassManager.cpp
1 //===- PassManager.cpp - LLVM Pass Infrastructure Implementation ----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Devang Patel and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the LLVM Pass Manager infrastructure. 
11 //
12 //===----------------------------------------------------------------------===//
13
14
15 #include "llvm/PassManagers.h"
16 #include "llvm/Support/CommandLine.h"
17 #include "llvm/Support/Timer.h"
18 #include "llvm/Module.h"
19 #include "llvm/ModuleProvider.h"
20 #include "llvm/Support/Streams.h"
21 #include "llvm/Support/ManagedStatic.h"
22 #include <algorithm>
23 #include <vector>
24 #include <map>
25
26 // See PassManagers.h for Pass Manager infrastructure overview.
27
28 namespace llvm {
29
30 //===----------------------------------------------------------------------===//
31 // Pass debugging information.  Often it is useful to find out what pass is
32 // running when a crash occurs in a utility.  When this library is compiled with
33 // debugging on, a command line option (--debug-pass) is enabled that causes the
34 // pass name to be printed before it executes.
35 //
36
37 // Different debug levels that can be enabled...
38 enum PassDebugLevel {
39   None, Arguments, Structure, Executions, Details
40 };
41
42 static cl::opt<enum PassDebugLevel>
43 PassDebugging("debug-pass", cl::Hidden,
44                   cl::desc("Print PassManager debugging information"),
45                   cl::values(
46   clEnumVal(None      , "disable debug output"),
47   clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
48   clEnumVal(Structure , "print pass structure before run()"),
49   clEnumVal(Executions, "print pass name before it is executed"),
50   clEnumVal(Details   , "print pass details when it is executed"),
51                              clEnumValEnd));
52 } // End of llvm namespace
53
54 namespace {
55
56 //===----------------------------------------------------------------------===//
57 // BBPassManager
58 //
59 /// BBPassManager manages BasicBlockPass. It batches all the
60 /// pass together and sequence them to process one basic block before
61 /// processing next basic block.
62 class VISIBILITY_HIDDEN BBPassManager : public PMDataManager, 
63                                         public FunctionPass {
64
65 public:
66   BBPassManager(int Depth) : PMDataManager(Depth) { }
67
68   /// Execute all of the passes scheduled for execution.  Keep track of
69   /// whether any of the passes modifies the function, and if so, return true.
70   bool runOnFunction(Function &F);
71
72   /// Pass Manager itself does not invalidate any analysis info.
73   void getAnalysisUsage(AnalysisUsage &Info) const {
74     Info.setPreservesAll();
75   }
76
77   bool doInitialization(Module &M);
78   bool doInitialization(Function &F);
79   bool doFinalization(Module &M);
80   bool doFinalization(Function &F);
81
82   virtual const char *getPassName() const {
83     return "BasicBlock Pass  Manager";
84   }
85
86   // Print passes managed by this manager
87   void dumpPassStructure(unsigned Offset) {
88     llvm::cerr << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n";
89     for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
90       BasicBlockPass *BP = getContainedPass(Index);
91       BP->dumpPassStructure(Offset + 1);
92       dumpLastUses(BP, Offset+1);
93     }
94   }
95
96   BasicBlockPass *getContainedPass(unsigned N) {
97     assert ( N < PassVector.size() && "Pass number out of range!");
98     BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
99     return BP;
100   }
101
102   virtual PassManagerType getPassManagerType() const { 
103     return PMT_BasicBlockPassManager; 
104   }
105 };
106
107 }
108
109 namespace llvm {
110
111 //===----------------------------------------------------------------------===//
112 // FunctionPassManagerImpl
113 //
114 /// FunctionPassManagerImpl manages FPPassManagers
115 class FunctionPassManagerImpl : public Pass,
116                                 public PMDataManager,
117                                 public PMTopLevelManager {
118 public:
119
120   FunctionPassManagerImpl(int Depth) : PMDataManager(Depth),
121                                        PMTopLevelManager(TLM_Function) { }
122
123   /// add - Add a pass to the queue of passes to run.  This passes ownership of
124   /// the Pass to the PassManager.  When the PassManager is destroyed, the pass
125   /// will be destroyed as well, so there is no need to delete the pass.  This
126   /// implies that all passes MUST be allocated with 'new'.
127   void add(Pass *P) {
128     schedulePass(P);
129   }
130  
131   /// run - Execute all of the passes scheduled for execution.  Keep track of
132   /// whether any of the passes modifies the module, and if so, return true.
133   bool run(Function &F);
134
135   /// doInitialization - Run all of the initializers for the function passes.
136   ///
137   bool doInitialization(Module &M);
138   
139   /// doFinalization - Run all of the initializers for the function passes.
140   ///
141   bool doFinalization(Module &M);
142
143   /// Pass Manager itself does not invalidate any analysis info.
144   void getAnalysisUsage(AnalysisUsage &Info) const {
145     Info.setPreservesAll();
146   }
147
148   inline void addTopLevelPass(Pass *P) {
149
150     if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
151       
152       // P is a immutable pass and it will be managed by this
153       // top level manager. Set up analysis resolver to connect them.
154       AnalysisResolver *AR = new AnalysisResolver(*this);
155       P->setResolver(AR);
156       initializeAnalysisImpl(P);
157       addImmutablePass(IP);
158       recordAvailableAnalysis(IP);
159     } else {
160       P->assignPassManager(activeStack);
161     }
162
163   }
164
165   FPPassManager *getContainedManager(unsigned N) {
166     assert ( N < PassManagers.size() && "Pass number out of range!");
167     FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
168     return FP;
169   }
170
171 };
172
173 //===----------------------------------------------------------------------===//
174 // MPPassManager
175 //
176 /// MPPassManager manages ModulePasses and function pass managers.
177 /// It batches all Module passes  passes and function pass managers together and
178 /// sequence them to process one module.
179 class MPPassManager : public Pass, public PMDataManager {
180  
181 public:
182   MPPassManager(int Depth) : PMDataManager(Depth) { }
183   
184   /// run - Execute all of the passes scheduled for execution.  Keep track of
185   /// whether any of the passes modifies the module, and if so, return true.
186   bool runOnModule(Module &M);
187
188   /// Pass Manager itself does not invalidate any analysis info.
189   void getAnalysisUsage(AnalysisUsage &Info) const {
190     Info.setPreservesAll();
191   }
192
193   virtual const char *getPassName() const {
194     return "Module Pass Manager";
195   }
196
197   // Print passes managed by this manager
198   void dumpPassStructure(unsigned Offset) {
199     llvm::cerr << std::string(Offset*2, ' ') << "ModulePass Manager\n";
200     for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
201       ModulePass *MP = getContainedPass(Index);
202       MP->dumpPassStructure(Offset + 1);
203       dumpLastUses(MP, Offset+1);
204     }
205   }
206
207   ModulePass *getContainedPass(unsigned N) {
208     assert ( N < PassVector.size() && "Pass number out of range!");
209     ModulePass *MP = static_cast<ModulePass *>(PassVector[N]);
210     return MP;
211   }
212
213   virtual PassManagerType getPassManagerType() const { 
214     return PMT_ModulePassManager; 
215   }
216 };
217
218 //===----------------------------------------------------------------------===//
219 // PassManagerImpl
220 //
221 /// PassManagerImpl manages MPPassManagers
222 class PassManagerImpl : public Pass,
223                         public PMDataManager,
224                         public PMTopLevelManager {
225
226 public:
227
228   PassManagerImpl(int Depth) : PMDataManager(Depth),
229                                PMTopLevelManager(TLM_Pass) { }
230
231   /// add - Add a pass to the queue of passes to run.  This passes ownership of
232   /// the Pass to the PassManager.  When the PassManager is destroyed, the pass
233   /// will be destroyed as well, so there is no need to delete the pass.  This
234   /// implies that all passes MUST be allocated with 'new'.
235   void add(Pass *P) {
236     schedulePass(P);
237   }
238  
239   /// run - Execute all of the passes scheduled for execution.  Keep track of
240   /// whether any of the passes modifies the module, and if so, return true.
241   bool run(Module &M);
242
243   /// Pass Manager itself does not invalidate any analysis info.
244   void getAnalysisUsage(AnalysisUsage &Info) const {
245     Info.setPreservesAll();
246   }
247
248   inline void addTopLevelPass(Pass *P) {
249
250     if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
251       
252       // P is a immutable pass and it will be managed by this
253       // top level manager. Set up analysis resolver to connect them.
254       AnalysisResolver *AR = new AnalysisResolver(*this);
255       P->setResolver(AR);
256       initializeAnalysisImpl(P);
257       addImmutablePass(IP);
258       recordAvailableAnalysis(IP);
259     } else {
260       P->assignPassManager(activeStack);
261     }
262
263   }
264
265   MPPassManager *getContainedManager(unsigned N) {
266     assert ( N < PassManagers.size() && "Pass number out of range!");
267     MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
268     return MP;
269   }
270
271 };
272
273 } // End of llvm namespace
274
275 namespace {
276
277 //===----------------------------------------------------------------------===//
278 // TimingInfo Class - This class is used to calculate information about the
279 // amount of time each pass takes to execute.  This only happens when
280 // -time-passes is enabled on the command line.
281 //
282
283 class VISIBILITY_HIDDEN TimingInfo {
284   std::map<Pass*, Timer> TimingData;
285   TimerGroup TG;
286
287 public:
288   // Use 'create' member to get this.
289   TimingInfo() : TG("... Pass execution timing report ...") {}
290   
291   // TimingDtor - Print out information about timing information
292   ~TimingInfo() {
293     // Delete all of the timers...
294     TimingData.clear();
295     // TimerGroup is deleted next, printing the report.
296   }
297
298   // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
299   // to a non null value (if the -time-passes option is enabled) or it leaves it
300   // null.  It may be called multiple times.
301   static void createTheTimeInfo();
302
303   void passStarted(Pass *P) {
304
305     if (dynamic_cast<PMDataManager *>(P)) 
306       return;
307
308     std::map<Pass*, Timer>::iterator I = TimingData.find(P);
309     if (I == TimingData.end())
310       I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
311     I->second.startTimer();
312   }
313   void passEnded(Pass *P) {
314
315     if (dynamic_cast<PMDataManager *>(P)) 
316       return;
317
318     std::map<Pass*, Timer>::iterator I = TimingData.find(P);
319     assert (I != TimingData.end() && "passStarted/passEnded not nested right!");
320     I->second.stopTimer();
321   }
322 };
323
324 static TimingInfo *TheTimeInfo;
325
326 } // End of anon namespace
327
328 //===----------------------------------------------------------------------===//
329 // PMTopLevelManager implementation
330
331 /// Initialize top level manager. Create first pass manager.
332 PMTopLevelManager::PMTopLevelManager (enum TopLevelManagerType t) {
333
334   if (t == TLM_Pass) {
335     MPPassManager *MPP = new MPPassManager(1);
336     MPP->setTopLevelManager(this);
337     addPassManager(MPP);
338     activeStack.push(MPP);
339   } 
340   else if (t == TLM_Function) {
341     FPPassManager *FPP = new FPPassManager(1);
342     FPP->setTopLevelManager(this);
343     addPassManager(FPP);
344     activeStack.push(FPP);
345   } 
346 }
347
348 /// Set pass P as the last user of the given analysis passes.
349 void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses, 
350                                     Pass *P) {
351
352   for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(),
353          E = AnalysisPasses.end(); I != E; ++I) {
354     Pass *AP = *I;
355     LastUser[AP] = P;
356     // If AP is the last user of other passes then make P last user of
357     // such passes.
358     for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
359            LUE = LastUser.end(); LUI != LUE; ++LUI) {
360       if (LUI->second == AP)
361         LastUser[LUI->first] = P;
362     }
363   }
364 }
365
366 /// Collect passes whose last user is P
367 void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses,
368                                             Pass *P) {
369    for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
370           LUE = LastUser.end(); LUI != LUE; ++LUI)
371       if (LUI->second == P)
372         LastUses.push_back(LUI->first);
373 }
374
375 /// Schedule pass P for execution. Make sure that passes required by
376 /// P are run before P is run. Update analysis info maintained by
377 /// the manager. Remove dead passes. This is a recursive function.
378 void PMTopLevelManager::schedulePass(Pass *P) {
379
380   // TODO : Allocate function manager for this pass, other wise required set
381   // may be inserted into previous function manager
382
383   // If this Analysis is already requested by one of the previous pass
384   // and it is still available then do not insert new pass in the queue again.
385   if (findAnalysisPass(P->getPassInfo()))
386       return;
387
388   AnalysisUsage AnUsage;
389   P->getAnalysisUsage(AnUsage);
390   const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
391   for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
392          E = RequiredSet.end(); I != E; ++I) {
393
394     Pass *AnalysisPass = findAnalysisPass(*I);
395     if (!AnalysisPass) {
396       // Schedule this analysis run first.
397       AnalysisPass = (*I)->createPass();
398       schedulePass(AnalysisPass);
399     }
400   }
401
402   // Now all required passes are available.
403   addTopLevelPass(P);
404 }
405
406 /// Find the pass that implements Analysis AID. Search immutable
407 /// passes and all pass managers. If desired pass is not found
408 /// then return NULL.
409 Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
410
411   Pass *P = NULL;
412   // Check pass managers
413   for (std::vector<Pass *>::iterator I = PassManagers.begin(),
414          E = PassManagers.end(); P == NULL && I != E; ++I) {
415     PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
416     assert(PMD && "This is not a PassManager");
417     P = PMD->findAnalysisPass(AID, false);
418   }
419
420   // Check other pass managers
421   for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
422          E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
423     P = (*I)->findAnalysisPass(AID, false);
424
425   for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
426          E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
427     const PassInfo *PI = (*I)->getPassInfo();
428     if (PI == AID)
429       P = *I;
430
431     // If Pass not found then check the interfaces implemented by Immutable Pass
432     if (!P) {
433       const std::vector<const PassInfo*> &ImmPI = PI->getInterfacesImplemented();
434       if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
435         P = *I;
436     }
437   }
438
439   return P;
440 }
441
442 // Print passes managed by this top level manager.
443 void PMTopLevelManager::dumpPasses() const {
444
445   if (PassDebugging < Structure)
446     return;
447
448   // Print out the immutable passes
449   for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
450     ImmutablePasses[i]->dumpPassStructure(0);
451   }
452   
453   for (std::vector<Pass *>::const_iterator I = PassManagers.begin(),
454          E = PassManagers.end(); I != E; ++I)
455     (*I)->dumpPassStructure(1);
456 }
457
458 void PMTopLevelManager::dumpArguments() const {
459
460   if (PassDebugging < Arguments)
461     return;
462
463   cerr << "Pass Arguments: ";
464   for (std::vector<Pass *>::const_iterator I = PassManagers.begin(),
465          E = PassManagers.end(); I != E; ++I) {
466     PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
467     assert(PMD && "This is not a PassManager");
468     PMD->dumpPassArguments();
469   }
470   cerr << "\n";
471 }
472
473 void PMTopLevelManager::initializeAllAnalysisInfo() {
474   
475   for (std::vector<Pass *>::iterator I = PassManagers.begin(),
476          E = PassManagers.end(); I != E; ++I) {
477     PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
478     assert(PMD && "This is not a PassManager");
479     PMD->initializeAnalysisInfo();
480   }
481   
482   // Initailize other pass managers
483   for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
484          E = IndirectPassManagers.end(); I != E; ++I)
485     (*I)->initializeAnalysisInfo();
486 }
487
488 /// Destructor
489 PMTopLevelManager::~PMTopLevelManager() {
490   for (std::vector<Pass *>::iterator I = PassManagers.begin(),
491          E = PassManagers.end(); I != E; ++I)
492     delete *I;
493   
494   for (std::vector<ImmutablePass *>::iterator
495          I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
496     delete *I;
497   
498   PassManagers.clear();
499 }
500
501 //===----------------------------------------------------------------------===//
502 // PMDataManager implementation
503
504 /// Return true IFF pass P's required analysis set does not required new
505 /// manager.
506 bool PMDataManager::manageablePass(Pass *P) {
507
508   // TODO 
509   // If this pass is not preserving information that is required by a
510   // pass maintained by higher level pass manager then do not insert
511   // this pass into current manager. Use new manager. For example,
512   // For example, If FunctionPass F is not preserving ModulePass Info M1
513   // that is used by another ModulePass M2 then do not insert F in
514   // current function pass manager.
515   return true;
516 }
517
518 /// Augement AvailableAnalysis by adding analysis made available by pass P.
519 void PMDataManager::recordAvailableAnalysis(Pass *P) {
520                                                 
521   if (const PassInfo *PI = P->getPassInfo()) {
522     AvailableAnalysis[PI] = P;
523
524     //This pass is the current implementation of all of the interfaces it
525     //implements as well.
526     const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
527     for (unsigned i = 0, e = II.size(); i != e; ++i)
528       AvailableAnalysis[II[i]] = P;
529   }
530 }
531
532 /// Remove Analyss not preserved by Pass P
533 void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
534   AnalysisUsage AnUsage;
535   P->getAnalysisUsage(AnUsage);
536
537   if (AnUsage.getPreservesAll())
538     return;
539
540   const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
541   for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
542          E = AvailableAnalysis.end(); I != E; ) {
543     std::map<AnalysisID, Pass*>::iterator Info = I++;
544     if (std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) == 
545         PreservedSet.end()) {
546       // Remove this analysis
547       if (!dynamic_cast<ImmutablePass*>(Info->second))
548         AvailableAnalysis.erase(Info);
549     }
550   }
551 }
552
553 /// Remove analysis passes that are not used any longer
554 void PMDataManager::removeDeadPasses(Pass *P, std::string Msg,
555                                      enum PassDebuggingString DBG_STR) {
556
557   std::vector<Pass *> DeadPasses;
558   TPM->collectLastUses(DeadPasses, P);
559
560   for (std::vector<Pass *>::iterator I = DeadPasses.begin(),
561          E = DeadPasses.end(); I != E; ++I) {
562
563     dumpPassInfo(*I, FREEING_MSG, DBG_STR, Msg);
564
565     if (TheTimeInfo) TheTimeInfo->passStarted(*I);
566     (*I)->releaseMemory();
567     if (TheTimeInfo) TheTimeInfo->passEnded(*I);
568
569     std::map<AnalysisID, Pass*>::iterator Pos = 
570       AvailableAnalysis.find((*I)->getPassInfo());
571     
572     // It is possible that pass is already removed from the AvailableAnalysis
573     if (Pos != AvailableAnalysis.end())
574       AvailableAnalysis.erase(Pos);
575   }
576 }
577
578 /// Add pass P into the PassVector. Update 
579 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
580 void PMDataManager::add(Pass *P, 
581                         bool ProcessAnalysis) {
582
583   // This manager is going to manage pass P. Set up analysis resolver
584   // to connect them.
585   AnalysisResolver *AR = new AnalysisResolver(*this);
586   P->setResolver(AR);
587
588   // If a FunctionPass F is the last user of ModulePass info M
589   // then the F's manager, not F, records itself as a last user of M.
590   std::vector<Pass *> TransferLastUses;
591
592   if (ProcessAnalysis) {
593
594     // At the moment, this pass is the last user of all required passes.
595     std::vector<Pass *> LastUses;
596     std::vector<Pass *> RequiredPasses;
597     unsigned PDepth = this->getDepth();
598
599     collectRequiredAnalysisPasses(RequiredPasses, P);
600     for (std::vector<Pass *>::iterator I = RequiredPasses.begin(),
601            E = RequiredPasses.end(); I != E; ++I) {
602       Pass *PRequired = *I;
603       unsigned RDepth = 0;
604
605       PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
606       RDepth = DM.getDepth();
607
608       if (PDepth == RDepth)
609         LastUses.push_back(PRequired);
610       else if (PDepth >  RDepth) {
611         // Let the parent claim responsibility of last use
612         TransferLastUses.push_back(PRequired);
613       } else {
614         // Note : This feature is not yet implemented
615         assert (0 && 
616                 "Unable to handle Pass that requires lower level Analysis pass");
617       }
618     }
619
620     // Set P as P's last user until someone starts using P.
621     // However, if P is a Pass Manager then it does not need
622     // to record its last user.
623     if (!dynamic_cast<PMDataManager *>(P))
624       LastUses.push_back(P);
625     TPM->setLastUser(LastUses, P);
626
627     if (!TransferLastUses.empty()) {
628       Pass *My_PM = dynamic_cast<Pass *>(this);
629       TPM->setLastUser(TransferLastUses, My_PM);
630       TransferLastUses.clear();
631     }
632
633     // Take a note of analysis required and made available by this pass.
634     // Remove the analysis not preserved by this pass
635     removeNotPreservedAnalysis(P);
636     recordAvailableAnalysis(P);
637   }
638
639   // Add pass
640   PassVector.push_back(P);
641 }
642
643 /// Populate RequiredPasses with the analysis pass that are required by
644 /// pass P.
645 void PMDataManager::collectRequiredAnalysisPasses(std::vector<Pass *> &RP,
646                                                   Pass *P) {
647   AnalysisUsage AnUsage;
648   P->getAnalysisUsage(AnUsage);
649   const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
650   for (std::vector<AnalysisID>::const_iterator 
651          I = RequiredSet.begin(), E = RequiredSet.end();
652        I != E; ++I) {
653     Pass *AnalysisPass = findAnalysisPass(*I, true);
654     assert (AnalysisPass && "Analysis pass is not available");
655     RP.push_back(AnalysisPass);
656   }
657
658   const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
659   for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
660          E = IDs.end(); I != E; ++I) {
661     Pass *AnalysisPass = findAnalysisPass(*I, true);
662     assert (AnalysisPass && "Analysis pass is not available");
663     RP.push_back(AnalysisPass);
664   }
665 }
666
667 // All Required analyses should be available to the pass as it runs!  Here
668 // we fill in the AnalysisImpls member of the pass so that it can
669 // successfully use the getAnalysis() method to retrieve the
670 // implementations it needs.
671 //
672 void PMDataManager::initializeAnalysisImpl(Pass *P) {
673   AnalysisUsage AnUsage;
674   P->getAnalysisUsage(AnUsage);
675  
676   for (std::vector<const PassInfo *>::const_iterator
677          I = AnUsage.getRequiredSet().begin(),
678          E = AnUsage.getRequiredSet().end(); I != E; ++I) {
679     Pass *Impl = findAnalysisPass(*I, true);
680     if (Impl == 0)
681       assert(0 && "Analysis used but not available!");
682     AnalysisResolver *AR = P->getResolver();
683     AR->addAnalysisImplsPair(*I, Impl);
684   }
685 }
686
687 /// Find the pass that implements Analysis AID. If desired pass is not found
688 /// then return NULL.
689 Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
690
691   // Check if AvailableAnalysis map has one entry.
692   std::map<AnalysisID, Pass*>::const_iterator I =  AvailableAnalysis.find(AID);
693
694   if (I != AvailableAnalysis.end())
695     return I->second;
696
697   // Search Parents through TopLevelManager
698   if (SearchParent)
699     return TPM->findAnalysisPass(AID);
700   
701   return NULL;
702 }
703
704 // Print list of passes that are last used by P.
705 void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
706
707   std::vector<Pass *> LUses;
708   
709   assert (TPM && "Top Level Manager is missing");
710   TPM->collectLastUses(LUses, P);
711   
712   for (std::vector<Pass *>::iterator I = LUses.begin(),
713          E = LUses.end(); I != E; ++I) {
714     llvm::cerr << "--" << std::string(Offset*2, ' ');
715     (*I)->dumpPassStructure(0);
716   }
717 }
718
719 void PMDataManager::dumpPassArguments() const {
720   for(std::vector<Pass *>::const_iterator I = PassVector.begin(),
721         E = PassVector.end(); I != E; ++I) {
722     if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
723       PMD->dumpPassArguments();
724     else
725       if (const PassInfo *PI = (*I)->getPassInfo())
726         if (!PI->isAnalysisGroup())
727           cerr << " -" << PI->getPassArgument();
728   }
729 }
730
731 void PMDataManager:: dumpPassInfo(Pass *P, enum PassDebuggingString S1,
732                                   enum PassDebuggingString S2,
733                                   std::string Msg) {
734   if (PassDebugging < Executions)
735     return;
736   cerr << (void*)this << std::string(getDepth()*2+1, ' ');
737   switch (S1) {
738   case EXECUTION_MSG:
739     cerr << "Executing Pass '" << P->getPassName();
740     break;
741   case MODIFICATION_MSG:
742     cerr << "' Made Modification '" << P->getPassName();
743     break;
744   case FREEING_MSG:
745     cerr << " Freeing Pass '" << P->getPassName();
746     break;
747   default:
748     break;
749   }
750   switch (S2) {
751   case ON_BASICBLOCK_MSG:
752     cerr << "' on BasicBlock '" << Msg << "...\n";
753     break;
754   case ON_FUNCTION_MSG:
755     cerr << "' on Function '" << Msg << "...\n";
756     break;
757   case ON_MODULE_MSG:
758     cerr << "' on Module '"  << Msg << "...\n";
759     break;
760   case ON_LOOP_MSG:
761     cerr << "' on Loop " << Msg << "...\n";
762     break;
763   case ON_CG_MSG:
764     cerr << "' on Call Graph " << Msg << "...\n";
765     break;
766   default:
767     break;
768   }
769 }
770
771 void PMDataManager::dumpAnalysisSetInfo(const char *Msg, Pass *P,
772                                         const std::vector<AnalysisID> &Set) 
773   const {
774   if (PassDebugging >= Details && !Set.empty()) {
775     cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
776       for (unsigned i = 0; i != Set.size(); ++i) {
777         if (i) cerr << ",";
778         cerr << " " << Set[i]->getPassName();
779       }
780       cerr << "\n";
781   }
782 }
783
784 // Destructor
785 PMDataManager::~PMDataManager() {
786   
787   for (std::vector<Pass *>::iterator I = PassVector.begin(),
788          E = PassVector.end(); I != E; ++I)
789     delete *I;
790   
791   PassVector.clear();
792 }
793
794 //===----------------------------------------------------------------------===//
795 // NOTE: Is this the right place to define this method ?
796 // getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
797 Pass *AnalysisResolver::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
798   return PM.findAnalysisPass(ID, dir);
799 }
800
801 //===----------------------------------------------------------------------===//
802 // BBPassManager implementation
803
804 /// Execute all of the passes scheduled for execution by invoking 
805 /// runOnBasicBlock method.  Keep track of whether any of the passes modifies 
806 /// the function, and if so, return true.
807 bool
808 BBPassManager::runOnFunction(Function &F) {
809
810   if (F.isDeclaration())
811     return false;
812
813   bool Changed = doInitialization(F);
814
815   for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
816     for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
817       BasicBlockPass *BP = getContainedPass(Index);
818       AnalysisUsage AnUsage;
819       BP->getAnalysisUsage(AnUsage);
820
821       dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, (*I).getName());
822       dumpAnalysisSetInfo("Required", BP, AnUsage.getRequiredSet());
823
824       initializeAnalysisImpl(BP);
825
826       if (TheTimeInfo) TheTimeInfo->passStarted(BP);
827       Changed |= BP->runOnBasicBlock(*I);
828       if (TheTimeInfo) TheTimeInfo->passEnded(BP);
829
830       if (Changed) 
831         dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG, (*I).getName());
832       dumpAnalysisSetInfo("Preserved", BP, AnUsage.getPreservedSet());
833
834       removeNotPreservedAnalysis(BP);
835       recordAvailableAnalysis(BP);
836       removeDeadPasses(BP, (*I).getName(), ON_BASICBLOCK_MSG);
837                        
838     }
839   return Changed |= doFinalization(F);
840 }
841
842 // Implement doInitialization and doFinalization
843 inline bool BBPassManager::doInitialization(Module &M) {
844   bool Changed = false;
845
846   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
847     BasicBlockPass *BP = getContainedPass(Index);
848     Changed |= BP->doInitialization(M);
849   }
850
851   return Changed;
852 }
853
854 inline bool BBPassManager::doFinalization(Module &M) {
855   bool Changed = false;
856
857   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
858     BasicBlockPass *BP = getContainedPass(Index);
859     Changed |= BP->doFinalization(M);
860   }
861
862   return Changed;
863 }
864
865 inline bool BBPassManager::doInitialization(Function &F) {
866   bool Changed = false;
867
868   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
869     BasicBlockPass *BP = getContainedPass(Index);
870     Changed |= BP->doInitialization(F);
871   }
872
873   return Changed;
874 }
875
876 inline bool BBPassManager::doFinalization(Function &F) {
877   bool Changed = false;
878
879   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
880     BasicBlockPass *BP = getContainedPass(Index);
881     Changed |= BP->doFinalization(F);
882   }
883
884   return Changed;
885 }
886
887
888 //===----------------------------------------------------------------------===//
889 // FunctionPassManager implementation
890
891 /// Create new Function pass manager
892 FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
893   FPM = new FunctionPassManagerImpl(0);
894   // FPM is the top level manager.
895   FPM->setTopLevelManager(FPM);
896
897   PMDataManager *PMD = dynamic_cast<PMDataManager *>(FPM);
898   AnalysisResolver *AR = new AnalysisResolver(*PMD);
899   FPM->setResolver(AR);
900   
901   MP = P;
902 }
903
904 FunctionPassManager::~FunctionPassManager() {
905   delete FPM;
906 }
907
908 /// add - Add a pass to the queue of passes to run.  This passes
909 /// ownership of the Pass to the PassManager.  When the
910 /// PassManager_X is destroyed, the pass will be destroyed as well, so
911 /// there is no need to delete the pass. (TODO delete passes.)
912 /// This implies that all passes MUST be allocated with 'new'.
913 void FunctionPassManager::add(Pass *P) { 
914   FPM->add(P);
915 }
916
917 /// run - Execute all of the passes scheduled for execution.  Keep
918 /// track of whether any of the passes modifies the function, and if
919 /// so, return true.
920 ///
921 bool FunctionPassManager::run(Function &F) {
922   std::string errstr;
923   if (MP->materializeFunction(&F, &errstr)) {
924     cerr << "Error reading bytecode file: " << errstr << "\n";
925     abort();
926   }
927   return FPM->run(F);
928 }
929
930
931 /// doInitialization - Run all of the initializers for the function passes.
932 ///
933 bool FunctionPassManager::doInitialization() {
934   return FPM->doInitialization(*MP->getModule());
935 }
936
937 /// doFinalization - Run all of the initializers for the function passes.
938 ///
939 bool FunctionPassManager::doFinalization() {
940   return FPM->doFinalization(*MP->getModule());
941 }
942
943 //===----------------------------------------------------------------------===//
944 // FunctionPassManagerImpl implementation
945 //
946 inline bool FunctionPassManagerImpl::doInitialization(Module &M) {
947   bool Changed = false;
948
949   for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {  
950     FPPassManager *FP = getContainedManager(Index);
951     Changed |= FP->doInitialization(M);
952   }
953
954   return Changed;
955 }
956
957 inline bool FunctionPassManagerImpl::doFinalization(Module &M) {
958   bool Changed = false;
959
960   for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {  
961     FPPassManager *FP = getContainedManager(Index);
962     Changed |= FP->doFinalization(M);
963   }
964
965   return Changed;
966 }
967
968 // Execute all the passes managed by this top level manager.
969 // Return true if any function is modified by a pass.
970 bool FunctionPassManagerImpl::run(Function &F) {
971
972   bool Changed = false;
973
974   TimingInfo::createTheTimeInfo();
975
976   dumpArguments();
977   dumpPasses();
978
979   initializeAllAnalysisInfo();
980   for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {  
981     FPPassManager *FP = getContainedManager(Index);
982     Changed |= FP->runOnFunction(F);
983   }
984   return Changed;
985 }
986
987 //===----------------------------------------------------------------------===//
988 // FPPassManager implementation
989
990 /// Print passes managed by this manager
991 void FPPassManager::dumpPassStructure(unsigned Offset) {
992   llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
993   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
994     FunctionPass *FP = getContainedPass(Index);
995     FP->dumpPassStructure(Offset + 1);
996     dumpLastUses(FP, Offset+1);
997   }
998 }
999
1000
1001 /// Execute all of the passes scheduled for execution by invoking 
1002 /// runOnFunction method.  Keep track of whether any of the passes modifies 
1003 /// the function, and if so, return true.
1004 bool FPPassManager::runOnFunction(Function &F) {
1005
1006   bool Changed = false;
1007
1008   if (F.isDeclaration())
1009     return false;
1010
1011   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1012     FunctionPass *FP = getContainedPass(Index);
1013
1014     AnalysisUsage AnUsage;
1015     FP->getAnalysisUsage(AnUsage);
1016
1017     dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
1018     dumpAnalysisSetInfo("Required", FP, AnUsage.getRequiredSet());
1019
1020     initializeAnalysisImpl(FP);
1021
1022     if (TheTimeInfo) TheTimeInfo->passStarted(FP);
1023     Changed |= FP->runOnFunction(F);
1024     if (TheTimeInfo) TheTimeInfo->passEnded(FP);
1025
1026     if (Changed) 
1027       dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
1028     dumpAnalysisSetInfo("Preserved", FP, AnUsage.getPreservedSet());
1029
1030     removeNotPreservedAnalysis(FP);
1031     recordAvailableAnalysis(FP);
1032     removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
1033   }
1034   return Changed;
1035 }
1036
1037 bool FPPassManager::runOnModule(Module &M) {
1038
1039   bool Changed = doInitialization(M);
1040
1041   for(Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1042     this->runOnFunction(*I);
1043
1044   return Changed |= doFinalization(M);
1045 }
1046
1047 inline bool FPPassManager::doInitialization(Module &M) {
1048   bool Changed = false;
1049
1050   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {  
1051     FunctionPass *FP = getContainedPass(Index);
1052     Changed |= FP->doInitialization(M);
1053   }
1054
1055   return Changed;
1056 }
1057
1058 inline bool FPPassManager::doFinalization(Module &M) {
1059   bool Changed = false;
1060
1061   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {  
1062     FunctionPass *FP = getContainedPass(Index);
1063     Changed |= FP->doFinalization(M);
1064   }
1065
1066   return Changed;
1067 }
1068
1069 //===----------------------------------------------------------------------===//
1070 // MPPassManager implementation
1071
1072 /// Execute all of the passes scheduled for execution by invoking 
1073 /// runOnModule method.  Keep track of whether any of the passes modifies 
1074 /// the module, and if so, return true.
1075 bool
1076 MPPassManager::runOnModule(Module &M) {
1077   bool Changed = false;
1078
1079   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1080     ModulePass *MP = getContainedPass(Index);
1081
1082     AnalysisUsage AnUsage;
1083     MP->getAnalysisUsage(AnUsage);
1084
1085     dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
1086     dumpAnalysisSetInfo("Required", MP, AnUsage.getRequiredSet());
1087
1088     initializeAnalysisImpl(MP);
1089
1090     if (TheTimeInfo) TheTimeInfo->passStarted(MP);
1091     Changed |= MP->runOnModule(M);
1092     if (TheTimeInfo) TheTimeInfo->passEnded(MP);
1093
1094     if (Changed) 
1095       dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
1096                    M.getModuleIdentifier());
1097     dumpAnalysisSetInfo("Preserved", MP, AnUsage.getPreservedSet());
1098       
1099     removeNotPreservedAnalysis(MP);
1100     recordAvailableAnalysis(MP);
1101     removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
1102   }
1103   return Changed;
1104 }
1105
1106 //===----------------------------------------------------------------------===//
1107 // PassManagerImpl implementation
1108 //
1109 /// run - Execute all of the passes scheduled for execution.  Keep track of
1110 /// whether any of the passes modifies the module, and if so, return true.
1111 bool PassManagerImpl::run(Module &M) {
1112
1113   bool Changed = false;
1114
1115   TimingInfo::createTheTimeInfo();
1116
1117   dumpArguments();
1118   dumpPasses();
1119
1120   initializeAllAnalysisInfo();
1121   for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {  
1122     MPPassManager *MP = getContainedManager(Index);
1123     Changed |= MP->runOnModule(M);
1124   }
1125   return Changed;
1126 }
1127
1128 //===----------------------------------------------------------------------===//
1129 // PassManager implementation
1130
1131 /// Create new pass manager
1132 PassManager::PassManager() {
1133   PM = new PassManagerImpl(0);
1134   // PM is the top level manager
1135   PM->setTopLevelManager(PM);
1136 }
1137
1138 PassManager::~PassManager() {
1139   delete PM;
1140 }
1141
1142 /// add - Add a pass to the queue of passes to run.  This passes ownership of
1143 /// the Pass to the PassManager.  When the PassManager is destroyed, the pass
1144 /// will be destroyed as well, so there is no need to delete the pass.  This
1145 /// implies that all passes MUST be allocated with 'new'.
1146 void 
1147 PassManager::add(Pass *P) {
1148   PM->add(P);
1149 }
1150
1151 /// run - Execute all of the passes scheduled for execution.  Keep track of
1152 /// whether any of the passes modifies the module, and if so, return true.
1153 bool
1154 PassManager::run(Module &M) {
1155   return PM->run(M);
1156 }
1157
1158 //===----------------------------------------------------------------------===//
1159 // TimingInfo Class - This class is used to calculate information about the
1160 // amount of time each pass takes to execute.  This only happens with
1161 // -time-passes is enabled on the command line.
1162 //
1163 bool llvm::TimePassesIsEnabled = false;
1164 static cl::opt<bool,true>
1165 EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1166             cl::desc("Time each pass, printing elapsed time for each on exit"));
1167
1168 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1169 // a non null value (if the -time-passes option is enabled) or it leaves it
1170 // null.  It may be called multiple times.
1171 void TimingInfo::createTheTimeInfo() {
1172   if (!TimePassesIsEnabled || TheTimeInfo) return;
1173
1174   // Constructed the first time this is called, iff -time-passes is enabled.
1175   // This guarantees that the object will be constructed before static globals,
1176   // thus it will be destroyed before them.
1177   static ManagedStatic<TimingInfo> TTI;
1178   TheTimeInfo = &*TTI;
1179 }
1180
1181 /// If TimingInfo is enabled then start pass timer.
1182 void StartPassTimer(Pass *P) {
1183   if (TheTimeInfo) 
1184     TheTimeInfo->passStarted(P);
1185 }
1186
1187 /// If TimingInfo is enabled then stop pass timer.
1188 void StopPassTimer(Pass *P) {
1189   if (TheTimeInfo) 
1190     TheTimeInfo->passEnded(P);
1191 }
1192
1193 //===----------------------------------------------------------------------===//
1194 // PMStack implementation
1195 //
1196
1197 // Pop Pass Manager from the stack and clear its analysis info.
1198 void PMStack::pop() {
1199
1200   PMDataManager *Top = this->top();
1201   Top->initializeAnalysisInfo();
1202
1203   S.pop_back();
1204 }
1205
1206 // Push PM on the stack and set its top level manager.
1207 void PMStack::push(Pass *P) {
1208
1209   PMDataManager *Top = NULL;
1210   PMDataManager *PM = dynamic_cast<PMDataManager *>(P);
1211   assert (PM && "Unable to push. Pass Manager expected");
1212
1213   if (this->empty()) {
1214     Top = PM;
1215   } 
1216   else {
1217     Top = this->top();
1218     PMTopLevelManager *TPM = Top->getTopLevelManager();
1219
1220     assert (TPM && "Unable to find top level manager");
1221     TPM->addIndirectPassManager(PM);
1222     PM->setTopLevelManager(TPM);
1223   }
1224
1225   AnalysisResolver *AR = new AnalysisResolver(*Top);
1226   P->setResolver(AR);
1227
1228   S.push_back(PM);
1229 }
1230
1231 // Dump content of the pass manager stack.
1232 void PMStack::dump() {
1233   for(std::deque<PMDataManager *>::iterator I = S.begin(),
1234         E = S.end(); I != E; ++I) {
1235     Pass *P = dynamic_cast<Pass *>(*I);
1236     printf ("%s ", P->getPassName());
1237   }
1238   if (!S.empty())
1239     printf ("\n");
1240 }
1241
1242 /// Find appropriate Module Pass Manager in the PM Stack and
1243 /// add self into that manager. 
1244 void ModulePass::assignPassManager(PMStack &PMS, 
1245                                    PassManagerType PreferredType) {
1246
1247   // Find Module Pass Manager
1248   while(!PMS.empty()) {
1249     PassManagerType TopPMType = PMS.top()->getPassManagerType();
1250     if (TopPMType == PreferredType)
1251       break; // We found desired pass manager
1252     else if (TopPMType > PMT_ModulePassManager)
1253       PMS.pop();    // Pop children pass managers
1254     else
1255       break;
1256   }
1257
1258   PMS.top()->add(this);
1259 }
1260
1261 /// Find appropriate Function Pass Manager or Call Graph Pass Manager
1262 /// in the PM Stack and add self into that manager. 
1263 void FunctionPass::assignPassManager(PMStack &PMS,
1264                                      PassManagerType PreferredType) {
1265
1266   // Find Module Pass Manager (TODO : Or Call Graph Pass Manager)
1267   while(!PMS.empty()) {
1268     if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1269       PMS.pop();
1270     else
1271       break; 
1272   }
1273   FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top());
1274
1275   // Create new Function Pass Manager
1276   if (!FPP) {
1277     assert(!PMS.empty() && "Unable to create Function Pass Manager");
1278     PMDataManager *PMD = PMS.top();
1279
1280     // [1] Create new Function Pass Manager
1281     FPP = new FPPassManager(PMD->getDepth() + 1);
1282
1283     // [2] Set up new manager's top level manager
1284     PMTopLevelManager *TPM = PMD->getTopLevelManager();
1285     TPM->addIndirectPassManager(FPP);
1286
1287     // [3] Assign manager to manage this new manager. This may create
1288     // and push new managers into PMS
1289     Pass *P = dynamic_cast<Pass *>(FPP);
1290
1291     // If Call Graph Pass Manager is active then use it to manage
1292     // this new Function Pass manager.
1293     if (PMD->getPassManagerType() == PMT_CallGraphPassManager)
1294       P->assignPassManager(PMS, PMT_CallGraphPassManager);
1295     else
1296       P->assignPassManager(PMS);
1297
1298     // [4] Push new manager into PMS
1299     PMS.push(FPP);
1300   }
1301
1302   // Assign FPP as the manager of this pass.
1303   FPP->add(this);
1304 }
1305
1306 /// Find appropriate Basic Pass Manager or Call Graph Pass Manager
1307 /// in the PM Stack and add self into that manager. 
1308 void BasicBlockPass::assignPassManager(PMStack &PMS,
1309                                        PassManagerType PreferredType) {
1310
1311   BBPassManager *BBP = NULL;
1312
1313   // Basic Pass Manager is a leaf pass manager. It does not handle
1314   // any other pass manager.
1315   if (!PMS.empty()) {
1316     BBP = dynamic_cast<BBPassManager *>(PMS.top());
1317   }
1318
1319   // If leaf manager is not Basic Block Pass manager then create new
1320   // basic Block Pass manager.
1321
1322   if (!BBP) {
1323     assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1324     PMDataManager *PMD = PMS.top();
1325
1326     // [1] Create new Basic Block Manager
1327     BBP = new BBPassManager(PMD->getDepth() + 1);
1328
1329     // [2] Set up new manager's top level manager
1330     // Basic Block Pass Manager does not live by itself
1331     PMTopLevelManager *TPM = PMD->getTopLevelManager();
1332     TPM->addIndirectPassManager(BBP);
1333
1334     // [3] Assign manager to manage this new manager. This may create
1335     // and push new managers into PMS
1336     Pass *P = dynamic_cast<Pass *>(BBP);
1337     P->assignPassManager(PMS);
1338
1339     // [4] Push new manager into PMS
1340     PMS.push(BBP);
1341   }
1342
1343   // Assign BBP as the manager of this pass.
1344   BBP->add(this);
1345 }
1346
1347