PMDataManager does not maintain LastUser info.
[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/PassManager.h"
16 #include "llvm/Module.h"
17 #include "llvm/ModuleProvider.h"
18 #include "llvm/Support/Streams.h"
19 #include <vector>
20 #include <map>
21 using namespace llvm;
22
23 //===----------------------------------------------------------------------===//
24 // Overview:
25 // The Pass Manager Infrastructure manages passes. It's responsibilities are:
26 // 
27 //   o Manage optimization pass execution order
28 //   o Make required Analysis information available before pass P is run
29 //   o Release memory occupied by dead passes
30 //   o If Analysis information is dirtied by a pass then regenerate Analysis 
31 //     information before it is consumed by another pass.
32 //
33 // Pass Manager Infrastructure uses multipe pass managers. They are PassManager,
34 // FunctionPassManager, ModulePassManager, BasicBlockPassManager. This class 
35 // hierarcy uses multiple inheritance but pass managers do not derive from
36 // another pass manager.
37 //
38 // PassManager and FunctionPassManager are two top level pass manager that
39 // represents the external interface of this entire pass manager infrastucture.
40 //
41 // Important classes :
42 //
43 // [o] class PMTopLevelManager;
44 //
45 // Two top level managers, PassManager and FunctionPassManager, derive from 
46 // PMTopLevelManager. PMTopLevelManager manages information used by top level 
47 // managers such as last user info.
48 //
49 // [o] class PMDataManager;
50 //
51 // PMDataManager manages information, e.g. list of available analysis info, 
52 // used by a pass manager to manage execution order of passes. It also provides
53 // a place to implement common pass manager APIs. All pass managers derive from
54 // PMDataManager.
55 //
56 // [o] class BasicBlockPassManager : public FunctionPass, public PMDataManager;
57 //
58 // BasicBlockPassManager manages BasicBlockPasses.
59 //
60 // [o] class FunctionPassManager;
61 //
62 // This is a external interface used by JIT to manage FunctionPasses. This
63 // interface relies on FunctionPassManagerImpl to do all the tasks.
64 //
65 // [o] class FunctionPassManagerImpl : public ModulePass, PMDataManager,
66 //                                     public PMTopLevelManager;
67 //
68 // FunctionPassManagerImpl is a top level manager. It manages FunctionPasses
69 // and BasicBlockPassManagers.
70 //
71 // [o] class ModulePassManager : public Pass, public PMDataManager;
72 //
73 // ModulePassManager manages ModulePasses and FunctionPassManagerImpls.
74 //
75 // [o] class PassManager;
76 //
77 // This is a external interface used by various tools to manages passes. It
78 // relies on PassManagerImpl to do all the tasks.
79 //
80 // [o] class PassManagerImpl : public Pass, public PMDataManager,
81 //                             public PMDTopLevelManager
82 //
83 // PassManagerImpl is a top level pass manager responsible for managing
84 // ModulePassManagers.
85 //===----------------------------------------------------------------------===//
86
87 namespace llvm {
88
89 //===----------------------------------------------------------------------===//
90 // PMTopLevelManager
91 //
92 /// PMTopLevelManager manages LastUser info and collects common APIs used by
93 /// top level pass managers.
94 class PMTopLevelManager {
95
96 public:
97
98   inline std::vector<Pass *>::iterator passManagersBegin() { 
99     return PassManagers.begin(); 
100   }
101
102   inline std::vector<Pass *>::iterator passManagersEnd() { 
103     return PassManagers.end();
104   }
105
106   /// Schedule pass P for execution. Make sure that passes required by
107   /// P are run before P is run. Update analysis info maintained by
108   /// the manager. Remove dead passes. This is a recursive function.
109   void schedulePass(Pass *P, Pass *PM);
110
111   /// This is implemented by top level pass manager and used by 
112   /// schedulePass() to add analysis info passes that are not available.
113   virtual void addTopLevelPass(Pass  *P) = 0;
114
115   /// Set pass P as the last user of the given analysis passes.
116   void setLastUser(std::vector<Pass *> &AnalysisPasses, Pass *P);
117
118   /// Collect passes whose last user is P
119   void collectLastUses(std::vector<Pass *> &LastUses, Pass *P);
120
121   virtual ~PMTopLevelManager() {
122     PassManagers.clear();
123   }
124
125   /// Add immutable pass and initialize it.
126   inline void addImmutablePass(ImmutablePass *P) {
127     P->initializePass();
128     ImmutablePasses.push_back(P);
129   }
130
131   inline std::vector<ImmutablePass *>& getImmutablePasses() {
132     return ImmutablePasses;
133   }
134
135 private:
136   
137   /// Collection of pass managers
138   std::vector<Pass *> PassManagers;
139
140   // Map to keep track of last user of the analysis pass.
141   // LastUser->second is the last user of Lastuser->first.
142   std::map<Pass *, Pass *> LastUser;
143
144   /// Immutable passes are managed by top level manager.
145   std::vector<ImmutablePass *> ImmutablePasses;
146 };
147   
148 /// Set pass P as the last user of the given analysis passes.
149 void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses, 
150                                     Pass *P) {
151
152   for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(),
153          E = AnalysisPasses.end(); I != E; ++I) {
154     Pass *AP = *I;
155     LastUser[AP] = P;
156     // If AP is the last user of other passes then make P last user of
157     // such passes.
158     for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
159            LUE = LastUser.end(); LUI != LUE; ++LUI) {
160       if (LUI->second == AP)
161         LastUser[LUI->first] = P;
162     }
163   }
164
165 }
166
167 /// Collect passes whose last user is P
168 void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses,
169                                             Pass *P) {
170    for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
171           LUE = LastUser.end(); LUI != LUE; ++LUI)
172       if (LUI->second == P)
173         LastUses.push_back(LUI->first);
174 }
175
176 /// Schedule pass P for execution. Make sure that passes required by
177 /// P are run before P is run. Update analysis info maintained by
178 /// the manager. Remove dead passes. This is a recursive function.
179 void PMTopLevelManager::schedulePass(Pass *P, Pass *PM) {
180
181   // TODO : Allocate function manager for this pass, other wise required set
182   // may be inserted into previous function manager
183
184   AnalysisUsage AnUsage;
185   P->getAnalysisUsage(AnUsage);
186   const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
187   for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
188          E = RequiredSet.end(); I != E; ++I) {
189
190     Pass *AnalysisPass = NULL; // FIXME PM->getAnalysisPass(*I, true);
191     if (!AnalysisPass) {
192       // Schedule this analysis run first.
193       AnalysisPass = (*I)->createPass();
194       schedulePass(AnalysisPass, PM);
195     }
196   }
197
198   // Now all required passes are available.
199   addTopLevelPass(P);
200 }
201
202
203 //===----------------------------------------------------------------------===//
204 // PMDataManager
205
206 /// PMDataManager provides the common place to manage the analysis data
207 /// used by pass managers.
208 class PMDataManager {
209
210 public:
211
212   PMDataManager() : TPM(NULL) {
213     initializeAnalysisInfo();
214   }
215
216   /// Return true IFF pass P's required analysis set does not required new
217   /// manager.
218   bool manageablePass(Pass *P);
219
220   Pass *getAnalysisPass(AnalysisID AID) const {
221
222     std::map<AnalysisID, Pass*>::const_iterator I = 
223       AvailableAnalysis.find(AID);
224
225     if (I != AvailableAnalysis.end())
226       return NULL;
227     else
228       return I->second;
229   }
230
231   /// Augment AvailableAnalysis by adding analysis made available by pass P.
232   void recordAvailableAnalysis(Pass *P);
233
234   /// Remove Analysis that is not preserved by the pass
235   void removeNotPreservedAnalysis(Pass *P);
236   
237   /// Remove dead passes
238   void removeDeadPasses(Pass *P);
239
240   /// Add pass P into the PassVector. Update 
241   /// AvailableAnalysis appropriately if ProcessAnalysis is true.
242   void addPassToManager (Pass *P, bool ProcessAnalysis = true);
243
244   // Initialize available analysis information.
245   void initializeAnalysisInfo() { 
246     AvailableAnalysis.clear();
247
248     // Include immutable passes into AvailableAnalysis vector.
249     std::vector<ImmutablePass *> &ImmutablePasses =  TPM->getImmutablePasses();
250     for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
251            E = ImmutablePasses.end(); I != E; ++I) 
252       recordAvailableAnalysis(*I);
253   }
254
255   // All Required analyses should be available to the pass as it runs!  Here
256   // we fill in the AnalysisImpls member of the pass so that it can
257   // successfully use the getAnalysis() method to retrieve the
258   // implementations it needs.
259   //
260  void initializeAnalysisImpl(Pass *P);
261
262   inline std::vector<Pass *>::iterator passVectorBegin() { 
263     return PassVector.begin(); 
264   }
265
266   inline std::vector<Pass *>::iterator passVectorEnd() { 
267     return PassVector.end();
268   }
269
270   // Access toplevel manager
271   PMTopLevelManager *getTopLevelManager() { return TPM; }
272   void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
273
274 private:
275   // Set of available Analysis. This information is used while scheduling 
276   // pass. If a pass requires an analysis which is not not available then 
277   // equired analysis pass is scheduled to run before the pass itself is 
278   // scheduled to run.
279   std::map<AnalysisID, Pass*> AvailableAnalysis;
280
281   // Collection of pass that are managed by this manager
282   std::vector<Pass *> PassVector;
283
284   // Top level manager.
285   // TODO : Make it a reference.
286   PMTopLevelManager *TPM;
287 };
288
289 /// BasicBlockPassManager_New manages BasicBlockPass. It batches all the
290 /// pass together and sequence them to process one basic block before
291 /// processing next basic block.
292 class BasicBlockPassManager_New : public PMDataManager, 
293                                   public FunctionPass {
294
295 public:
296   BasicBlockPassManager_New() { }
297
298   /// Add a pass into a passmanager queue. 
299   bool addPass(Pass *p);
300   
301   /// Execute all of the passes scheduled for execution.  Keep track of
302   /// whether any of the passes modifies the function, and if so, return true.
303   bool runOnFunction(Function &F);
304
305   /// Return true IFF AnalysisID AID is currently available.
306   /// TODO : Replace this method with getAnalysisPass()
307   Pass *getAnalysisPassFromManager(AnalysisID AID);
308
309   /// Pass Manager itself does not invalidate any analysis info.
310   void getAnalysisUsage(AnalysisUsage &Info) const {
311     Info.setPreservesAll();
312   }
313
314 private:
315 };
316
317 /// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
318 /// It batches all function passes and basic block pass managers together and
319 /// sequence them to process one function at a time before processing next
320 /// function.
321 class FunctionPassManagerImpl_New : public ModulePass, 
322                                     public PMDataManager,
323                                     public PMTopLevelManager {
324 public:
325   FunctionPassManagerImpl_New(ModuleProvider *P) { /* TODO */ }
326   FunctionPassManagerImpl_New() { 
327     activeBBPassManager = NULL;
328   }
329   ~FunctionPassManagerImpl_New() { /* TODO */ };
330  
331   inline void addTopLevelPass(Pass *P) { 
332     addPass(P);
333   }
334
335   /// add - Add a pass to the queue of passes to run.  This passes
336   /// ownership of the Pass to the PassManager.  When the
337   /// PassManager_X is destroyed, the pass will be destroyed as well, so
338   /// there is no need to delete the pass. (TODO delete passes.)
339   /// This implies that all passes MUST be allocated with 'new'.
340   void add(Pass *P) { 
341     schedulePass(P, this);
342   }
343
344   /// Add pass into the pass manager queue.
345   bool addPass(Pass *P);
346
347   /// Execute all of the passes scheduled for execution.  Keep
348   /// track of whether any of the passes modifies the function, and if
349   /// so, return true.
350   bool runOnModule(Module &M);
351   bool runOnFunction(Function &F);
352
353   /// Return true IFF AnalysisID AID is currently available.
354   /// TODO : Replace this method with getAnalysisPass()
355   Pass *getAnalysisPassFromManager(AnalysisID AID);
356
357   /// doInitialization - Run all of the initializers for the function passes.
358   ///
359   bool doInitialization(Module &M);
360   
361   /// doFinalization - Run all of the initializers for the function passes.
362   ///
363   bool doFinalization(Module &M);
364
365   /// Pass Manager itself does not invalidate any analysis info.
366   void getAnalysisUsage(AnalysisUsage &Info) const {
367     Info.setPreservesAll();
368   }
369
370 private:
371   // Active Pass Managers
372   BasicBlockPassManager_New *activeBBPassManager;
373 };
374
375 /// ModulePassManager_New manages ModulePasses and function pass managers.
376 /// It batches all Module passes  passes and function pass managers together and
377 /// sequence them to process one module.
378 class ModulePassManager_New : public PMDataManager {
379  
380 public:
381   ModulePassManager_New() { activeFunctionPassManager = NULL; }
382   
383   /// Add a pass into a passmanager queue. 
384   bool addPass(Pass *p);
385   
386   /// run - Execute all of the passes scheduled for execution.  Keep track of
387   /// whether any of the passes modifies the module, and if so, return true.
388   bool runOnModule(Module &M);
389
390   /// Return true IFF AnalysisID AID is currently available.
391   /// TODO : Replace this method with getAnalysisPass()
392   Pass *getAnalysisPassFromManager(AnalysisID AID);
393
394   /// Pass Manager itself does not invalidate any analysis info.
395   void getAnalysisUsage(AnalysisUsage &Info) const {
396     Info.setPreservesAll();
397   }
398
399 private:
400   // Active Pass Manager
401   FunctionPassManagerImpl_New *activeFunctionPassManager;
402 };
403
404 /// PassManager_New manages ModulePassManagers
405 class PassManagerImpl_New : public Pass,
406                             public PMDataManager,
407                             public PMTopLevelManager {
408
409 public:
410
411   /// add - Add a pass to the queue of passes to run.  This passes ownership of
412   /// the Pass to the PassManager.  When the PassManager is destroyed, the pass
413   /// will be destroyed as well, so there is no need to delete the pass.  This
414   /// implies that all passes MUST be allocated with 'new'.
415   void add(Pass *P) {
416     schedulePass(P, this);
417   }
418  
419   /// run - Execute all of the passes scheduled for execution.  Keep track of
420   /// whether any of the passes modifies the module, and if so, return true.
421   bool run(Module &M);
422
423   /// Return true IFF AnalysisID AID is currently available.
424   /// TODO : Replace this method with getAnalysisPass()
425   Pass *getAnalysisPassFromManager(AnalysisID AID);
426
427   /// Pass Manager itself does not invalidate any analysis info.
428   void getAnalysisUsage(AnalysisUsage &Info) const {
429     Info.setPreservesAll();
430   }
431
432   inline void addTopLevelPass(Pass *P) {
433     addPass(P);
434   }
435
436 private:
437
438   /// Add a pass into a passmanager queue.
439   bool addPass(Pass *p);
440
441   // Collection of pass managers
442   std::vector<ModulePassManager_New *> PassManagers;
443
444   // Active Pass Manager
445   ModulePassManager_New *activeManager;
446 };
447
448 } // End of llvm namespace
449
450 //===----------------------------------------------------------------------===//
451 // PMDataManager implementation
452
453 /// Return true IFF pass P's required analysis set does not required new
454 /// manager.
455 bool PMDataManager::manageablePass(Pass *P) {
456
457   // TODO 
458   // If this pass is not preserving information that is required by a
459   // pass maintained by higher level pass manager then do not insert
460   // this pass into current manager. Use new manager. For example,
461   // For example, If FunctionPass F is not preserving ModulePass Info M1
462   // that is used by another ModulePass M2 then do not insert F in
463   // current function pass manager.
464   return true;
465 }
466
467 /// Augement AvailableAnalysis by adding analysis made available by pass P.
468 void PMDataManager::recordAvailableAnalysis(Pass *P) {
469                                                 
470   if (const PassInfo *PI = P->getPassInfo()) {
471     AvailableAnalysis[PI] = P;
472
473     //This pass is the current implementation of all of the interfaces it
474     //implements as well.
475     const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
476     for (unsigned i = 0, e = II.size(); i != e; ++i)
477       AvailableAnalysis[II[i]] = P;
478   }
479 }
480
481 /// Remove Analyss not preserved by Pass P
482 void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
483   AnalysisUsage AnUsage;
484   P->getAnalysisUsage(AnUsage);
485
486   if (AnUsage.getPreservesAll())
487     return;
488
489   const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
490   for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
491          E = AvailableAnalysis.end(); I != E; ++I ) {
492     if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) == 
493         PreservedSet.end()) {
494       // Remove this analysis
495       std::map<AnalysisID, Pass*>::iterator J = I++;
496       AvailableAnalysis.erase(J);
497     }
498   }
499 }
500
501 /// Remove analysis passes that are not used any longer
502 void PMDataManager::removeDeadPasses(Pass *P) {
503   // TODO : reimplement
504 }
505
506 /// Add pass P into the PassVector. Update 
507 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
508 void PMDataManager::addPassToManager(Pass *P, 
509                                      bool ProcessAnalysis) {
510
511   if (ProcessAnalysis) {
512     // Take a note of analysis required and made available by this pass.
513     // Remove the analysis not preserved by this pass
514     initializeAnalysisImpl(P);
515     removeNotPreservedAnalysis(P);
516     recordAvailableAnalysis(P);
517   }
518
519   // Add pass
520   PassVector.push_back(P);
521 }
522
523 // All Required analyses should be available to the pass as it runs!  Here
524 // we fill in the AnalysisImpls member of the pass so that it can
525 // successfully use the getAnalysis() method to retrieve the
526 // implementations it needs.
527 //
528 void PMDataManager::initializeAnalysisImpl(Pass *P) {
529   AnalysisUsage AnUsage;
530   P->getAnalysisUsage(AnUsage);
531  
532   for (std::vector<const PassInfo *>::const_iterator
533          I = AnUsage.getRequiredSet().begin(),
534          E = AnUsage.getRequiredSet().end(); I != E; ++I) {
535     Pass *Impl = getAnalysisPass(*I);
536     if (Impl == 0)
537       assert(0 && "Analysis used but not available!");
538     // TODO:  P->AnalysisImpls.push_back(std::make_pair(*I, Impl));
539   }
540 }
541
542 //===----------------------------------------------------------------------===//
543 // BasicBlockPassManager_New implementation
544
545 /// Add pass P into PassVector and return true. If this pass is not
546 /// manageable by this manager then return false.
547 bool
548 BasicBlockPassManager_New::addPass(Pass *P) {
549
550   BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
551   if (!BP)
552     return false;
553
554   // If this pass does not preserve anlysis that is used by other passes
555   // managed by this manager than it is not a suiable pass for this manager.
556   if (!manageablePass(P))
557     return false;
558
559   addPassToManager (BP);
560
561   return true;
562 }
563
564 /// Execute all of the passes scheduled for execution by invoking 
565 /// runOnBasicBlock method.  Keep track of whether any of the passes modifies 
566 /// the function, and if so, return true.
567 bool
568 BasicBlockPassManager_New::runOnFunction(Function &F) {
569
570   bool Changed = false;
571   initializeAnalysisInfo();
572
573   for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
574     for (std::vector<Pass *>::iterator itr = passVectorBegin(),
575            e = passVectorEnd(); itr != e; ++itr) {
576       Pass *P = *itr;
577       
578       BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
579       Changed |= BP->runOnBasicBlock(*I);
580       removeNotPreservedAnalysis(P);
581       recordAvailableAnalysis(P);
582       removeDeadPasses(P);
583     }
584   return Changed;
585 }
586
587 /// Return true IFF AnalysisID AID is currently available.
588 /// TODO : Replace this method with getAnalysisPass()
589 Pass * BasicBlockPassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
590   return getAnalysisPass(AID);
591 }
592
593 //===----------------------------------------------------------------------===//
594 // FunctionPassManager_New implementation
595
596 /// Create new Function pass manager
597 FunctionPassManager_New::FunctionPassManager_New() {
598   FPM = new FunctionPassManagerImpl_New();
599 }
600
601 /// add - Add a pass to the queue of passes to run.  This passes
602 /// ownership of the Pass to the PassManager.  When the
603 /// PassManager_X is destroyed, the pass will be destroyed as well, so
604 /// there is no need to delete the pass. (TODO delete passes.)
605 /// This implies that all passes MUST be allocated with 'new'.
606 void FunctionPassManager_New::add(Pass *P) { 
607   FPM->add(P);
608 }
609
610 /// Execute all of the passes scheduled for execution.  Keep
611 /// track of whether any of the passes modifies the function, and if
612 /// so, return true.
613 bool FunctionPassManager_New::runOnModule(Module &M) {
614   return FPM->runOnModule(M);
615 }
616
617 /// run - Execute all of the passes scheduled for execution.  Keep
618 /// track of whether any of the passes modifies the function, and if
619 /// so, return true.
620 ///
621 bool FunctionPassManager_New::run(Function &F) {
622   std::string errstr;
623   if (MP->materializeFunction(&F, &errstr)) {
624     cerr << "Error reading bytecode file: " << errstr << "\n";
625     abort();
626   }
627   return FPM->runOnFunction(F);
628 }
629
630
631 /// doInitialization - Run all of the initializers for the function passes.
632 ///
633 bool FunctionPassManager_New::doInitialization() {
634   return FPM->doInitialization(*MP->getModule());
635 }
636
637 /// doFinalization - Run all of the initializers for the function passes.
638 ///
639 bool FunctionPassManager_New::doFinalization() {
640   return FPM->doFinalization(*MP->getModule());
641 }
642
643 //===----------------------------------------------------------------------===//
644 // FunctionPassManagerImpl_New implementation
645
646 /// Add pass P into the pass manager queue. If P is a BasicBlockPass then
647 /// either use it into active basic block pass manager or create new basic
648 /// block pass manager to handle pass P.
649 bool
650 FunctionPassManagerImpl_New::addPass(Pass *P) {
651
652   // If P is a BasicBlockPass then use BasicBlockPassManager_New.
653   if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
654
655     if (!activeBBPassManager
656         || !activeBBPassManager->addPass(BP)) {
657
658       // TODO : intialize AvailableAnalysis info
659
660       activeBBPassManager = new BasicBlockPassManager_New();
661       addPassToManager(activeBBPassManager, false);
662       if (!activeBBPassManager->addPass(BP))
663         assert(0 && "Unable to add Pass");
664     }
665     return true;
666   }
667
668   FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
669   if (!FP)
670     return false;
671
672   // If this pass does not preserve anlysis that is used by other passes
673   // managed by this manager than it is not a suiable pass for this manager.
674   if (!manageablePass(P))
675     return false;
676
677   addPassToManager (FP);
678   // TODO : intialize AvailableAnalysis info
679   activeBBPassManager = NULL;
680   return true;
681 }
682
683 /// Execute all of the passes scheduled for execution by invoking 
684 /// runOnFunction method.  Keep track of whether any of the passes modifies 
685 /// the function, and if so, return true.
686 bool FunctionPassManagerImpl_New::runOnModule(Module &M) {
687
688   bool Changed = false;
689   initializeAnalysisInfo();
690
691   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
692     for (std::vector<Pass *>::iterator itr = passVectorBegin(),
693            e = passVectorEnd(); itr != e; ++itr) {
694       Pass *P = *itr;
695       
696       FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
697       Changed |= FP->runOnFunction(*I);
698       removeNotPreservedAnalysis(P);
699       recordAvailableAnalysis(P);
700       removeDeadPasses(P);
701     }
702   return Changed;
703 }
704
705 /// Execute all of the passes scheduled for execution by invoking 
706 /// runOnFunction method.  Keep track of whether any of the passes modifies 
707 /// the function, and if so, return true.
708 bool FunctionPassManagerImpl_New::runOnFunction(Function &F) {
709
710   bool Changed = false;
711   initializeAnalysisInfo();
712
713   for (std::vector<Pass *>::iterator itr = passVectorBegin(),
714          e = passVectorEnd(); itr != e; ++itr) {
715     Pass *P = *itr;
716     
717     FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
718     Changed |= FP->runOnFunction(F);
719     removeNotPreservedAnalysis(P);
720     recordAvailableAnalysis(P);
721     removeDeadPasses(P);
722   }
723   return Changed;
724 }
725
726
727 /// Return true IFF AnalysisID AID is currently available.
728 /// TODO : Replace this method with getAnalysisPass()
729 Pass *FunctionPassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
730
731   Pass *P = getAnalysisPass(AID);
732   if (P)
733     return P;
734
735   if (activeBBPassManager && 
736       activeBBPassManager->getAnalysisPass(AID) != 0)
737     return activeBBPassManager->getAnalysisPass(AID);
738
739   // TODO : Check inactive managers
740   return NULL;
741 }
742
743 inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
744   bool Changed = false;
745
746   for (std::vector<Pass *>::iterator itr = passVectorBegin(),
747          e = passVectorEnd(); itr != e; ++itr) {
748     Pass *P = *itr;
749     
750     FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
751     Changed |= FP->doInitialization(M);
752   }
753
754   return Changed;
755 }
756
757 inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
758   bool Changed = false;
759
760   for (std::vector<Pass *>::iterator itr = passVectorBegin(),
761          e = passVectorEnd(); itr != e; ++itr) {
762     Pass *P = *itr;
763     
764     FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
765     Changed |= FP->doFinalization(M);
766   }
767
768
769   return Changed;
770 }
771
772 //===----------------------------------------------------------------------===//
773 // ModulePassManager implementation
774
775 /// Add P into pass vector if it is manageble. If P is a FunctionPass
776 /// then use FunctionPassManagerImpl_New to manage it. Return false if P
777 /// is not manageable by this manager.
778 bool
779 ModulePassManager_New::addPass(Pass *P) {
780
781   // If P is FunctionPass then use function pass maanager.
782   if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
783
784     if (!activeFunctionPassManager
785         || !activeFunctionPassManager->addPass(P)) {
786
787       // TODO : intialize AvailableAnalysis info
788       activeFunctionPassManager = NULL;
789
790       activeFunctionPassManager = new FunctionPassManagerImpl_New();
791       addPassToManager(activeFunctionPassManager, false);
792       if (!activeFunctionPassManager->addPass(FP))
793         assert(0 && "Unable to add pass");
794     }
795     return true;
796   }
797
798   ModulePass *MP = dynamic_cast<ModulePass *>(P);
799   if (!MP)
800     return false;
801
802   // If this pass does not preserve anlysis that is used by other passes
803   // managed by this manager than it is not a suiable pass for this manager.
804   if (!manageablePass(P))
805     return false;
806
807   addPassToManager(MP);
808   // TODO : intialize AvailableAnalysis info
809   activeFunctionPassManager = NULL;
810   return true;
811 }
812
813
814 /// Execute all of the passes scheduled for execution by invoking 
815 /// runOnModule method.  Keep track of whether any of the passes modifies 
816 /// the module, and if so, return true.
817 bool
818 ModulePassManager_New::runOnModule(Module &M) {
819   bool Changed = false;
820   initializeAnalysisInfo();
821
822   for (std::vector<Pass *>::iterator itr = passVectorBegin(),
823          e = passVectorEnd(); itr != e; ++itr) {
824     Pass *P = *itr;
825
826     ModulePass *MP = dynamic_cast<ModulePass*>(P);
827     Changed |= MP->runOnModule(M);
828     removeNotPreservedAnalysis(P);
829     recordAvailableAnalysis(P);
830     removeDeadPasses(P);
831   }
832   return Changed;
833 }
834
835 /// Return true IFF AnalysisID AID is currently available.
836 /// TODO : Replace this method with getAnalysisPass()
837 Pass *ModulePassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
838
839   Pass *P = getAnalysisPass(AID);
840   if (P)
841     return P;
842
843   if (activeFunctionPassManager && 
844       activeFunctionPassManager->getAnalysisPass(AID) != 0)
845     return activeFunctionPassManager->getAnalysisPass(AID);
846
847   // TODO : Check inactive managers
848   return NULL;
849 }
850
851 //===----------------------------------------------------------------------===//
852 // PassManagerImpl implementation
853
854 /// Return true IFF AnalysisID AID is currently available.
855 /// TODO : Replace this method with getAnalysisPass()
856 Pass *PassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
857
858   Pass *P = NULL;
859   for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
860          e = PassManagers.end(); !P && itr != e; ++itr)
861     P  = (*itr)->getAnalysisPassFromManager(AID);
862   return P;
863 }
864
865 // PassManager_New implementation
866 /// Add P into active pass manager or use new module pass manager to
867 /// manage it.
868 bool PassManagerImpl_New::addPass(Pass *P) {
869
870   if (!activeManager || !activeManager->addPass(P)) {
871     activeManager = new ModulePassManager_New();
872     PassManagers.push_back(activeManager);
873     return activeManager->addPass(P);
874   }
875   return true;
876 }
877
878 /// run - Execute all of the passes scheduled for execution.  Keep track of
879 /// whether any of the passes modifies the module, and if so, return true.
880 bool PassManagerImpl_New::run(Module &M) {
881
882   bool Changed = false;
883   for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
884          e = PassManagers.end(); itr != e; ++itr) {
885     ModulePassManager_New *pm = *itr;
886     Changed |= pm->runOnModule(M);
887   }
888   return Changed;
889 }
890
891 //===----------------------------------------------------------------------===//
892 // PassManager implementation
893
894 /// Create new pass manager
895 PassManager_New::PassManager_New() {
896   PM = new PassManagerImpl_New();
897 }
898
899 /// add - Add a pass to the queue of passes to run.  This passes ownership of
900 /// the Pass to the PassManager.  When the PassManager is destroyed, the pass
901 /// will be destroyed as well, so there is no need to delete the pass.  This
902 /// implies that all passes MUST be allocated with 'new'.
903 void 
904 PassManager_New::add(Pass *P) {
905   PM->add(P);
906 }
907
908 /// run - Execute all of the passes scheduled for execution.  Keep track of
909 /// whether any of the passes modifies the module, and if so, return true.
910 bool
911 PassManager_New::run(Module &M) {
912   return PM->run(M);
913 }
914