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