While initializing AvailableAnalysis info, make ImmutablePasses available
[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 //===----------------------------------------------------------------------===//
177 // PMDataManager
178
179 /// PMDataManager provides the common place to manage the analysis data
180 /// used by pass managers.
181 class PMDataManager {
182
183 public:
184
185   PMDataManager() : TPM(NULL) {
186     initializeAnalysisInfo();
187   }
188
189   /// Return true IFF pass P's required analysis set does not required new
190   /// manager.
191   bool manageablePass(Pass *P);
192
193   Pass *getAnalysisPass(AnalysisID AID) const {
194
195     std::map<AnalysisID, Pass*>::const_iterator I = 
196       AvailableAnalysis.find(AID);
197
198     if (I != AvailableAnalysis.end())
199       return NULL;
200     else
201       return I->second;
202   }
203
204   /// Augment AvailableAnalysis by adding analysis made available by pass P.
205   void recordAvailableAnalysis(Pass *P);
206
207   /// Remove Analysis that is not preserved by the pass
208   void removeNotPreservedAnalysis(Pass *P);
209   
210   /// Remove dead passes
211   void removeDeadPasses(Pass *P);
212
213   /// Add pass P into the PassVector. Update 
214   /// AvailableAnalysis appropriately if ProcessAnalysis is true.
215   void addPassToManager (Pass *P, bool ProcessAnalysis = true);
216
217   // Initialize available analysis information.
218   void initializeAnalysisInfo() { 
219     AvailableAnalysis.clear();
220     LastUser.clear();
221
222     // Include immutable passes into AvailableAnalysis vector.
223     std::vector<ImmutablePass *> &ImmutablePasses =  TPM->getImmutablePasses();
224     for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
225            E = ImmutablePasses.end(); I != E; ++I) 
226       recordAvailableAnalysis(*I);
227   }
228
229   // All Required analyses should be available to the pass as it runs!  Here
230   // we fill in the AnalysisImpls member of the pass so that it can
231   // successfully use the getAnalysis() method to retrieve the
232   // implementations it needs.
233   //
234  void initializeAnalysisImpl(Pass *P);
235
236   inline std::vector<Pass *>::iterator passVectorBegin() { 
237     return PassVector.begin(); 
238   }
239
240   inline std::vector<Pass *>::iterator passVectorEnd() { 
241     return PassVector.end();
242   }
243
244   inline void setLastUser(Pass *P, Pass *LU) {
245     LastUser[P] = LU; 
246     // TODO : Check if pass P is available.
247   }
248
249   // Access toplevel manager
250   PMTopLevelManager *getTopLevelManager() { return TPM; }
251   void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
252
253 private:
254   // Set of available Analysis. This information is used while scheduling 
255   // pass. If a pass requires an analysis which is not not available then 
256   // equired analysis pass is scheduled to run before the pass itself is 
257   // scheduled to run.
258   std::map<AnalysisID, Pass*> AvailableAnalysis;
259
260   // Map to keep track of last user of the analysis pass.
261   // LastUser->second is the last user of Lastuser->first.
262   std::map<Pass *, Pass *> LastUser;
263
264   // Collection of pass that are managed by this manager
265   std::vector<Pass *> PassVector;
266
267   // Top level manager.
268   // TODO : Make it a reference.
269   PMTopLevelManager *TPM;
270 };
271
272 /// BasicBlockPassManager_New manages BasicBlockPass. It batches all the
273 /// pass together and sequence them to process one basic block before
274 /// processing next basic block.
275 class BasicBlockPassManager_New : public PMDataManager, 
276                                   public FunctionPass {
277
278 public:
279   BasicBlockPassManager_New() { }
280
281   /// Add a pass into a passmanager queue. 
282   bool addPass(Pass *p);
283   
284   /// Execute all of the passes scheduled for execution.  Keep track of
285   /// whether any of the passes modifies the function, and if so, return true.
286   bool runOnFunction(Function &F);
287
288   /// Return true IFF AnalysisID AID is currently available.
289   Pass *getAnalysisPassFromManager(AnalysisID AID);
290
291   /// Pass Manager itself does not invalidate any analysis info.
292   void getAnalysisUsage(AnalysisUsage &Info) const {
293     Info.setPreservesAll();
294   }
295
296 private:
297 };
298
299 /// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
300 /// It batches all function passes and basic block pass managers together and
301 /// sequence them to process one function at a time before processing next
302 /// function.
303 class FunctionPassManagerImpl_New : public PMDataManager,
304                                     public ModulePass {
305 public:
306   FunctionPassManagerImpl_New(ModuleProvider *P) { /* TODO */ }
307   FunctionPassManagerImpl_New() { 
308     activeBBPassManager = NULL;
309   }
310   ~FunctionPassManagerImpl_New() { /* TODO */ };
311  
312   /// add - Add a pass to the queue of passes to run.  This passes
313   /// ownership of the Pass to the PassManager.  When the
314   /// PassManager_X is destroyed, the pass will be destroyed as well, so
315   /// there is no need to delete the pass. (TODO delete passes.)
316   /// This implies that all passes MUST be allocated with 'new'.
317   void add(Pass *P) { /* TODO*/  }
318
319   /// Add pass into the pass manager queue.
320   bool addPass(Pass *P);
321
322   /// Execute all of the passes scheduled for execution.  Keep
323   /// track of whether any of the passes modifies the function, and if
324   /// so, return true.
325   bool runOnModule(Module &M);
326   bool runOnFunction(Function &F);
327
328   /// Return true IFF AnalysisID AID is currently available.
329   Pass *getAnalysisPassFromManager(AnalysisID AID);
330
331   /// doInitialization - Run all of the initializers for the function passes.
332   ///
333   bool doInitialization(Module &M);
334   
335   /// doFinalization - Run all of the initializers for the function passes.
336   ///
337   bool doFinalization(Module &M);
338
339   /// Pass Manager itself does not invalidate any analysis info.
340   void getAnalysisUsage(AnalysisUsage &Info) const {
341     Info.setPreservesAll();
342   }
343
344 private:
345   // Active Pass Managers
346   BasicBlockPassManager_New *activeBBPassManager;
347 };
348
349 /// ModulePassManager_New manages ModulePasses and function pass managers.
350 /// It batches all Module passes  passes and function pass managers together and
351 /// sequence them to process one module.
352 class ModulePassManager_New : public PMDataManager {
353  
354 public:
355   ModulePassManager_New() { activeFunctionPassManager = NULL; }
356   
357   /// Add a pass into a passmanager queue. 
358   bool addPass(Pass *p);
359   
360   /// run - Execute all of the passes scheduled for execution.  Keep track of
361   /// whether any of the passes modifies the module, and if so, return true.
362   bool runOnModule(Module &M);
363
364   /// Return true IFF AnalysisID AID is currently available.
365   Pass *getAnalysisPassFromManager(AnalysisID AID);
366
367   /// Pass Manager itself does not invalidate any analysis info.
368   void getAnalysisUsage(AnalysisUsage &Info) const {
369     Info.setPreservesAll();
370   }
371
372 private:
373   // Active Pass Manager
374   FunctionPassManagerImpl_New *activeFunctionPassManager;
375 };
376
377 /// PassManager_New manages ModulePassManagers
378 class PassManagerImpl_New : public PMDataManager {
379
380 public:
381
382   /// add - Add a pass to the queue of passes to run.  This passes ownership of
383   /// the Pass to the PassManager.  When the PassManager is destroyed, the pass
384   /// will be destroyed as well, so there is no need to delete the pass.  This
385   /// implies that all passes MUST be allocated with 'new'.
386   void add(Pass *P);
387  
388   /// run - Execute all of the passes scheduled for execution.  Keep track of
389   /// whether any of the passes modifies the module, and if so, return true.
390   bool run(Module &M);
391
392   /// Return true IFF AnalysisID AID is currently available.
393   Pass *getAnalysisPassFromManager(AnalysisID AID);
394
395   /// Pass Manager itself does not invalidate any analysis info.
396   void getAnalysisUsage(AnalysisUsage &Info) const {
397     Info.setPreservesAll();
398   }
399
400 private:
401
402   /// Add a pass into a passmanager queue. This is used by schedulePasses
403   bool addPass(Pass *p);
404
405   /// Schedule pass P for execution. Make sure that passes required by
406   /// P are run before P is run. Update analysis info maintained by
407   /// the manager. Remove dead passes. This is a recursive function.
408   void schedulePass(Pass *P);
409
410   /// Schedule all passes collected in pass queue using add(). Add all the
411   /// schedule passes into various manager's queue using addPass().
412   void schedulePasses();
413
414   // Collection of pass managers
415   std::vector<ModulePassManager_New *> PassManagers;
416
417   // Active Pass Manager
418   ModulePassManager_New *activeManager;
419 };
420
421 } // End of llvm namespace
422
423 //===----------------------------------------------------------------------===//
424 // PMDataManager implementation
425
426 /// Return true IFF pass P's required analysis set does not required new
427 /// manager.
428 bool PMDataManager::manageablePass(Pass *P) {
429
430   // TODO 
431   // If this pass is not preserving information that is required by a
432   // pass maintained by higher level pass manager then do not insert
433   // this pass into current manager. Use new manager. For example,
434   // For example, If FunctionPass F is not preserving ModulePass Info M1
435   // that is used by another ModulePass M2 then do not insert F in
436   // current function pass manager.
437   return true;
438 }
439
440 /// Augement AvailableAnalysis by adding analysis made available by pass P.
441 void PMDataManager::recordAvailableAnalysis(Pass *P) {
442                                                 
443   if (const PassInfo *PI = P->getPassInfo()) {
444     AvailableAnalysis[PI] = P;
445
446     //This pass is the current implementation of all of the interfaces it
447     //implements as well.
448     const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
449     for (unsigned i = 0, e = II.size(); i != e; ++i)
450       AvailableAnalysis[II[i]] = P;
451   }
452 }
453
454 /// Remove Analyss not preserved by Pass P
455 void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
456   AnalysisUsage AnUsage;
457   P->getAnalysisUsage(AnUsage);
458
459   if (AnUsage.getPreservesAll())
460     return;
461
462   const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
463   for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
464          E = AvailableAnalysis.end(); I != E; ++I ) {
465     if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) == 
466         PreservedSet.end()) {
467       // Remove this analysis
468       std::map<AnalysisID, Pass*>::iterator J = I++;
469       AvailableAnalysis.erase(J);
470     }
471   }
472 }
473
474 /// Remove analysis passes that are not used any longer
475 void PMDataManager::removeDeadPasses(Pass *P) {
476
477   for (std::map<Pass *, Pass *>::iterator I = LastUser.begin(),
478          E = LastUser.end(); I !=E; ++I) {
479     if (I->second == P) {
480       Pass *deadPass = I->first;
481       deadPass->releaseMemory();
482
483       std::map<AnalysisID, Pass*>::iterator Pos = 
484         AvailableAnalysis.find(deadPass->getPassInfo());
485       
486       assert (Pos != AvailableAnalysis.end() &&
487               "Pass is not available");
488       AvailableAnalysis.erase(Pos);
489     }
490   }
491 }
492
493 /// Add pass P into the PassVector. Update 
494 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
495 void PMDataManager::addPassToManager(Pass *P, 
496                                      bool ProcessAnalysis) {
497
498   if (ProcessAnalysis) {
499     // Take a note of analysis required and made available by this pass
500     initializeAnalysisImpl(P);
501     recordAvailableAnalysis(P);
502
503     // Remove the analysis not preserved by this pass
504     removeNotPreservedAnalysis(P);
505   }
506
507   // Add pass
508   PassVector.push_back(P);
509 }
510
511 // All Required analyses should be available to the pass as it runs!  Here
512 // we fill in the AnalysisImpls member of the pass so that it can
513 // successfully use the getAnalysis() method to retrieve the
514 // implementations it needs.
515 //
516 void PMDataManager::initializeAnalysisImpl(Pass *P) {
517   AnalysisUsage AnUsage;
518   P->getAnalysisUsage(AnUsage);
519  
520   for (std::vector<const PassInfo *>::const_iterator
521          I = AnUsage.getRequiredSet().begin(),
522          E = AnUsage.getRequiredSet().end(); I != E; ++I) {
523     Pass *Impl = getAnalysisPass(*I);
524     if (Impl == 0)
525       assert(0 && "Analysis used but not available!");
526     // TODO:  P->AnalysisImpls.push_back(std::make_pair(*I, Impl));
527   }
528 }
529
530 //===----------------------------------------------------------------------===//
531 // BasicBlockPassManager_New implementation
532
533 /// Add pass P into PassVector and return true. If this pass is not
534 /// manageable by this manager then return false.
535 bool
536 BasicBlockPassManager_New::addPass(Pass *P) {
537
538   BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
539   if (!BP)
540     return false;
541
542   // If this pass does not preserve anlysis that is used by other passes
543   // managed by this manager than it is not a suiable pass for this manager.
544   if (!manageablePass(P))
545     return false;
546
547   addPassToManager (BP);
548
549   return true;
550 }
551
552 /// Execute all of the passes scheduled for execution by invoking 
553 /// runOnBasicBlock method.  Keep track of whether any of the passes modifies 
554 /// the function, and if so, return true.
555 bool
556 BasicBlockPassManager_New::runOnFunction(Function &F) {
557
558   bool Changed = false;
559   initializeAnalysisInfo();
560
561   for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
562     for (std::vector<Pass *>::iterator itr = passVectorBegin(),
563            e = passVectorEnd(); itr != e; ++itr) {
564       Pass *P = *itr;
565       
566       recordAvailableAnalysis(P);
567       BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
568       Changed |= BP->runOnBasicBlock(*I);
569       removeNotPreservedAnalysis(P);
570       removeDeadPasses(P);
571     }
572   return Changed;
573 }
574
575 /// Return true IFF AnalysisID AID is currently available.
576 Pass * BasicBlockPassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
577   return getAnalysisPass(AID);
578 }
579
580 //===----------------------------------------------------------------------===//
581 // FunctionPassManager_New implementation
582
583 /// Create new Function pass manager
584 FunctionPassManager_New::FunctionPassManager_New() {
585   FPM = new FunctionPassManagerImpl_New();
586 }
587
588 /// add - Add a pass to the queue of passes to run.  This passes
589 /// ownership of the Pass to the PassManager.  When the
590 /// PassManager_X is destroyed, the pass will be destroyed as well, so
591 /// there is no need to delete the pass. (TODO delete passes.)
592 /// This implies that all passes MUST be allocated with 'new'.
593 void FunctionPassManager_New::add(Pass *P) { 
594   FPM->add(P);
595 }
596
597 /// Execute all of the passes scheduled for execution.  Keep
598 /// track of whether any of the passes modifies the function, and if
599 /// so, return true.
600 bool FunctionPassManager_New::runOnModule(Module &M) {
601   return FPM->runOnModule(M);
602 }
603
604 /// run - Execute all of the passes scheduled for execution.  Keep
605 /// track of whether any of the passes modifies the function, and if
606 /// so, return true.
607 ///
608 bool FunctionPassManager_New::run(Function &F) {
609   std::string errstr;
610   if (MP->materializeFunction(&F, &errstr)) {
611     cerr << "Error reading bytecode file: " << errstr << "\n";
612     abort();
613   }
614   return FPM->runOnFunction(F);
615 }
616
617
618 /// doInitialization - Run all of the initializers for the function passes.
619 ///
620 bool FunctionPassManager_New::doInitialization() {
621   return FPM->doInitialization(*MP->getModule());
622 }
623
624 /// doFinalization - Run all of the initializers for the function passes.
625 ///
626 bool FunctionPassManager_New::doFinalization() {
627   return FPM->doFinalization(*MP->getModule());
628 }
629
630 //===----------------------------------------------------------------------===//
631 // FunctionPassManagerImpl_New implementation
632
633 /// Add pass P into the pass manager queue. If P is a BasicBlockPass then
634 /// either use it into active basic block pass manager or create new basic
635 /// block pass manager to handle pass P.
636 bool
637 FunctionPassManagerImpl_New::addPass(Pass *P) {
638
639   // If P is a BasicBlockPass then use BasicBlockPassManager_New.
640   if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
641
642     if (!activeBBPassManager
643         || !activeBBPassManager->addPass(BP)) {
644
645       activeBBPassManager = new BasicBlockPassManager_New();
646       addPassToManager(activeBBPassManager, false);
647       if (!activeBBPassManager->addPass(BP))
648         assert(0 && "Unable to add Pass");
649     }
650     return true;
651   }
652
653   FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
654   if (!FP)
655     return false;
656
657   // If this pass does not preserve anlysis that is used by other passes
658   // managed by this manager than it is not a suiable pass for this manager.
659   if (!manageablePass(P))
660     return false;
661
662   addPassToManager (FP);
663   activeBBPassManager = NULL;
664   return true;
665 }
666
667 /// Execute all of the passes scheduled for execution by invoking 
668 /// runOnFunction method.  Keep track of whether any of the passes modifies 
669 /// the function, and if so, return true.
670 bool FunctionPassManagerImpl_New::runOnModule(Module &M) {
671
672   bool Changed = false;
673   initializeAnalysisInfo();
674
675   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
676     for (std::vector<Pass *>::iterator itr = passVectorBegin(),
677            e = passVectorEnd(); itr != e; ++itr) {
678       Pass *P = *itr;
679       
680       recordAvailableAnalysis(P);
681       FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
682       Changed |= FP->runOnFunction(*I);
683       removeNotPreservedAnalysis(P);
684       removeDeadPasses(P);
685     }
686   return Changed;
687 }
688
689 /// Execute all of the passes scheduled for execution by invoking 
690 /// runOnFunction method.  Keep track of whether any of the passes modifies 
691 /// the function, and if so, return true.
692 bool FunctionPassManagerImpl_New::runOnFunction(Function &F) {
693
694   bool Changed = false;
695   initializeAnalysisInfo();
696
697   for (std::vector<Pass *>::iterator itr = passVectorBegin(),
698          e = passVectorEnd(); itr != e; ++itr) {
699     Pass *P = *itr;
700     
701     recordAvailableAnalysis(P);
702     FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
703     Changed |= FP->runOnFunction(F);
704     removeNotPreservedAnalysis(P);
705     removeDeadPasses(P);
706   }
707   return Changed;
708 }
709
710
711 /// Return true IFF AnalysisID AID is currently available.
712 Pass *FunctionPassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
713
714   Pass *P = getAnalysisPass(AID);
715   if (P)
716     return P;
717
718   if (activeBBPassManager && 
719       activeBBPassManager->getAnalysisPass(AID) != 0)
720     return activeBBPassManager->getAnalysisPass(AID);
721
722   // TODO : Check inactive managers
723   return NULL;
724 }
725
726 inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
727   bool Changed = false;
728
729   for (std::vector<Pass *>::iterator itr = passVectorBegin(),
730          e = passVectorEnd(); itr != e; ++itr) {
731     Pass *P = *itr;
732     
733     FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
734     Changed |= FP->doInitialization(M);
735   }
736
737   return Changed;
738 }
739
740 inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
741   bool Changed = false;
742
743   for (std::vector<Pass *>::iterator itr = passVectorBegin(),
744          e = passVectorEnd(); itr != e; ++itr) {
745     Pass *P = *itr;
746     
747     FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
748     Changed |= FP->doFinalization(M);
749   }
750
751
752   return Changed;
753 }
754
755 //===----------------------------------------------------------------------===//
756 // ModulePassManager implementation
757
758 /// Add P into pass vector if it is manageble. If P is a FunctionPass
759 /// then use FunctionPassManagerImpl_New to manage it. Return false if P
760 /// is not manageable by this manager.
761 bool
762 ModulePassManager_New::addPass(Pass *P) {
763
764   // If P is FunctionPass then use function pass maanager.
765   if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
766
767     activeFunctionPassManager = NULL;
768
769     if (!activeFunctionPassManager
770         || !activeFunctionPassManager->addPass(P)) {
771
772       activeFunctionPassManager = new FunctionPassManagerImpl_New();
773       addPassToManager(activeFunctionPassManager, false);
774       if (!activeFunctionPassManager->addPass(FP))
775         assert(0 && "Unable to add pass");
776     }
777     return true;
778   }
779
780   ModulePass *MP = dynamic_cast<ModulePass *>(P);
781   if (!MP)
782     return false;
783
784   // If this pass does not preserve anlysis that is used by other passes
785   // managed by this manager than it is not a suiable pass for this manager.
786   if (!manageablePass(P))
787     return false;
788
789   addPassToManager(MP);
790   activeFunctionPassManager = NULL;
791   return true;
792 }
793
794
795 /// Execute all of the passes scheduled for execution by invoking 
796 /// runOnModule method.  Keep track of whether any of the passes modifies 
797 /// the module, and if so, return true.
798 bool
799 ModulePassManager_New::runOnModule(Module &M) {
800   bool Changed = false;
801   initializeAnalysisInfo();
802
803   for (std::vector<Pass *>::iterator itr = passVectorBegin(),
804          e = passVectorEnd(); itr != e; ++itr) {
805     Pass *P = *itr;
806
807     recordAvailableAnalysis(P);
808     ModulePass *MP = dynamic_cast<ModulePass*>(P);
809     Changed |= MP->runOnModule(M);
810     removeNotPreservedAnalysis(P);
811     removeDeadPasses(P);
812   }
813   return Changed;
814 }
815
816 /// Return true IFF AnalysisID AID is currently available.
817 Pass *ModulePassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
818
819   
820   Pass *P = getAnalysisPass(AID);
821   if (P)
822     return P;
823
824   if (activeFunctionPassManager && 
825       activeFunctionPassManager->getAnalysisPass(AID) != 0)
826     return activeFunctionPassManager->getAnalysisPass(AID);
827
828   // TODO : Check inactive managers
829   return NULL;
830 }
831
832 //===----------------------------------------------------------------------===//
833 // PassManagerImpl implementation
834
835 /// Return true IFF AnalysisID AID is currently available.
836 Pass *PassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
837
838   Pass *P = NULL;
839   for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
840          e = PassManagers.end(); !P && itr != e; ++itr)
841     P  = (*itr)->getAnalysisPassFromManager(AID);
842   return P;
843 }
844
845 /// Schedule pass P for execution. Make sure that passes required by
846 /// P are run before P is run. Update analysis info maintained by
847 /// the manager. Remove dead passes. This is a recursive function.
848 void PassManagerImpl_New::schedulePass(Pass *P) {
849
850   AnalysisUsage AnUsage;
851   P->getAnalysisUsage(AnUsage);
852   const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
853   for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
854          E = RequiredSet.end(); I != E; ++I) {
855
856     Pass *AnalysisPass = getAnalysisPassFromManager(*I);
857     if (!AnalysisPass) {
858       // Schedule this analysis run first.
859       AnalysisPass = (*I)->createPass();
860       schedulePass(AnalysisPass);
861     }
862     setLastUser (AnalysisPass, P);
863
864     // Prolong live range of analyses that are needed after an analysis pass
865     // is destroyed, for querying by subsequent passes
866     const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
867     for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
868            E = IDs.end(); I != E; ++I) {
869       Pass *AP = getAnalysisPassFromManager(*I);
870       assert (AP && "Analysis pass is not available");
871       setLastUser(AP, P);
872     }
873   }
874   addPass(P);
875 }
876
877 /// Schedule all passes from the queue by adding them in their
878 /// respective manager's queue. 
879 void PassManagerImpl_New::schedulePasses() {
880   for (std::vector<Pass *>::iterator I = passVectorBegin(),
881          E = passVectorEnd(); I != E; ++I)
882     schedulePass (*I);
883 }
884
885 /// Add pass P to the queue of passes to run.
886 void PassManagerImpl_New::add(Pass *P) {
887   // Do not process Analysis now. Analysis is process while scheduling
888   // the pass vector.
889   addPassToManager(P, false);
890 }
891
892 // PassManager_New implementation
893 /// Add P into active pass manager or use new module pass manager to
894 /// manage it.
895 bool PassManagerImpl_New::addPass(Pass *P) {
896
897   if (!activeManager || !activeManager->addPass(P)) {
898     activeManager = new ModulePassManager_New();
899     PassManagers.push_back(activeManager);
900   }
901
902   return activeManager->addPass(P);
903 }
904
905 /// run - Execute all of the passes scheduled for execution.  Keep track of
906 /// whether any of the passes modifies the module, and if so, return true.
907 bool PassManagerImpl_New::run(Module &M) {
908
909   schedulePasses();
910   bool Changed = false;
911   for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
912          e = PassManagers.end(); itr != e; ++itr) {
913     ModulePassManager_New *pm = *itr;
914     Changed |= pm->runOnModule(M);
915   }
916   return Changed;
917 }
918
919 //===----------------------------------------------------------------------===//
920 // PassManager implementation
921
922 /// Create new pass manager
923 PassManager_New::PassManager_New() {
924   PM = new PassManagerImpl_New();
925 }
926
927 /// add - Add a pass to the queue of passes to run.  This passes ownership of
928 /// the Pass to the PassManager.  When the PassManager is destroyed, the pass
929 /// will be destroyed as well, so there is no need to delete the pass.  This
930 /// implies that all passes MUST be allocated with 'new'.
931 void 
932 PassManager_New::add(Pass *P) {
933   PM->add(P);
934 }
935
936 /// run - Execute all of the passes scheduled for execution.  Keep track of
937 /// whether any of the passes modifies the module, and if so, return true.
938 bool
939 PassManager_New::run(Module &M) {
940   return PM->run(M);
941 }
942