1 //===- PassManager.cpp - LLVM Pass Infrastructure Implementation ----------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the LLVM Pass Manager infrastructure.
12 //===----------------------------------------------------------------------===//
15 #include "llvm/PassManager.h"
16 #include "llvm/Module.h"
17 #include "llvm/ModuleProvider.h"
18 #include "llvm/Support/Streams.h"
23 //===----------------------------------------------------------------------===//
25 // The Pass Manager Infrastructure manages passes. It's responsibilities are:
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.
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.
38 // PassManager and FunctionPassManager are two top level pass manager that
39 // represents the external interface of this entire pass manager infrastucture.
41 // Important classes :
43 // [o] class PMTopLevelManager;
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.
49 // [o] class PMDataManager;
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
56 // [o] class BasicBlockPassManager : public FunctionPass, public PMDataManager;
58 // BasicBlockPassManager manages BasicBlockPasses.
60 // [o] class FunctionPassManager;
62 // This is a external interface used by JIT to manage FunctionPasses. This
63 // interface relies on FunctionPassManagerImpl to do all the tasks.
65 // [o] class FunctionPassManagerImpl : public ModulePass, PMDataManager,
66 // public PMTopLevelManager;
68 // FunctionPassManagerImpl is a top level manager. It manages FunctionPasses
69 // and BasicBlockPassManagers.
71 // [o] class ModulePassManager : public Pass, public PMDataManager;
73 // ModulePassManager manages ModulePasses and FunctionPassManagerImpls.
75 // [o] class PassManager;
77 // This is a external interface used by various tools to manages passes. It
78 // relies on PassManagerImpl to do all the tasks.
80 // [o] class PassManagerImpl : public Pass, public PMDataManager,
81 // public PMDTopLevelManager
83 // PassManagerImpl is a top level pass manager responsible for managing
84 // ModulePassManagers.
85 //===----------------------------------------------------------------------===//
89 //===----------------------------------------------------------------------===//
92 /// PMTopLevelManager manages LastUser info and collects common APIs used by
93 /// top level pass managers.
94 class PMTopLevelManager {
98 inline std::vector<Pass *>::iterator passManagersBegin() {
99 return PassManagers.begin();
102 inline std::vector<Pass *>::iterator passManagersEnd() {
103 return PassManagers.end();
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);
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;
115 /// Set pass P as the last user of the given analysis passes.
116 void setLastUser(std::vector<Pass *> &AnalysisPasses, Pass *P);
118 /// Collect passes whose last user is P
119 void collectLastUses(std::vector<Pass *> &LastUses, Pass *P);
121 virtual ~PMTopLevelManager() {
122 PassManagers.clear();
125 /// Add immutable pass and initialize it.
126 inline void addImmutablePass(ImmutablePass *P) {
128 ImmutablePasses.push_back(P);
131 inline std::vector<ImmutablePass *>& getImmutablePasses() {
132 return ImmutablePasses;
137 /// Collection of pass managers
138 std::vector<Pass *> PassManagers;
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;
144 /// Immutable passes are managed by top level manager.
145 std::vector<ImmutablePass *> ImmutablePasses;
148 /// Set pass P as the last user of the given analysis passes.
149 void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses,
152 for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(),
153 E = AnalysisPasses.end(); I != E; ++I) {
156 // If AP is the last user of other passes then make P last user of
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;
167 /// Collect passes whose last user is P
168 void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses,
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);
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) {
181 // TODO : Allocate function manager for this pass, other wise required set
182 // may be inserted into previous function manager
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) {
190 Pass *AnalysisPass = NULL; // FIXME PM->getAnalysisPass(*I, true);
192 // Schedule this analysis run first.
193 AnalysisPass = (*I)->createPass();
194 schedulePass(AnalysisPass, PM);
198 // Now all required passes are available.
203 //===----------------------------------------------------------------------===//
206 /// PMDataManager provides the common place to manage the analysis data
207 /// used by pass managers.
208 class PMDataManager {
212 PMDataManager() : TPM(NULL) {
213 initializeAnalysisInfo();
216 /// Return true IFF pass P's required analysis set does not required new
218 bool manageablePass(Pass *P);
220 Pass *getAnalysisPass(AnalysisID AID) const {
222 std::map<AnalysisID, Pass*>::const_iterator I =
223 AvailableAnalysis.find(AID);
225 if (I != AvailableAnalysis.end())
231 /// Augment AvailableAnalysis by adding analysis made available by pass P.
232 void recordAvailableAnalysis(Pass *P);
234 /// Remove Analysis that is not preserved by the pass
235 void removeNotPreservedAnalysis(Pass *P);
237 /// Remove dead passes
238 void removeDeadPasses(Pass *P);
240 /// Add pass P into the PassVector. Update
241 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
242 void addPassToManager (Pass *P, bool ProcessAnalysis = true);
244 // Initialize available analysis information.
245 void initializeAnalysisInfo() {
246 AvailableAnalysis.clear();
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);
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.
260 void initializeAnalysisImpl(Pass *P);
262 inline std::vector<Pass *>::iterator passVectorBegin() {
263 return PassVector.begin();
266 inline std::vector<Pass *>::iterator passVectorEnd() {
267 return PassVector.end();
270 // Access toplevel manager
271 PMTopLevelManager *getTopLevelManager() { return TPM; }
272 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
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
279 std::map<AnalysisID, Pass*> AvailableAnalysis;
281 // Collection of pass that are managed by this manager
282 std::vector<Pass *> PassVector;
284 // Top level manager.
285 // TODO : Make it a reference.
286 PMTopLevelManager *TPM;
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 {
296 BasicBlockPassManager_New() { }
298 /// Add a pass into a passmanager queue.
299 bool addPass(Pass *p);
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);
305 /// Return true IFF AnalysisID AID is currently available.
306 /// TODO : Replace this method with getAnalysisPass()
307 Pass *getAnalysisPassFromManager(AnalysisID AID);
309 /// Pass Manager itself does not invalidate any analysis info.
310 void getAnalysisUsage(AnalysisUsage &Info) const {
311 Info.setPreservesAll();
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
321 class FunctionPassManagerImpl_New : public ModulePass,
322 public PMDataManager,
323 public PMTopLevelManager {
325 FunctionPassManagerImpl_New(ModuleProvider *P) { /* TODO */ }
326 FunctionPassManagerImpl_New() {
327 activeBBPassManager = NULL;
329 ~FunctionPassManagerImpl_New() { /* TODO */ };
331 inline void addTopLevelPass(Pass *P) {
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'.
341 schedulePass(P, this);
344 /// Add pass into the pass manager queue.
345 bool addPass(Pass *P);
347 /// Execute all of the passes scheduled for execution. Keep
348 /// track of whether any of the passes modifies the function, and if
350 bool runOnModule(Module &M);
351 bool runOnFunction(Function &F);
353 /// Return true IFF AnalysisID AID is currently available.
354 /// TODO : Replace this method with getAnalysisPass()
355 Pass *getAnalysisPassFromManager(AnalysisID AID);
357 /// doInitialization - Run all of the initializers for the function passes.
359 bool doInitialization(Module &M);
361 /// doFinalization - Run all of the initializers for the function passes.
363 bool doFinalization(Module &M);
365 /// Pass Manager itself does not invalidate any analysis info.
366 void getAnalysisUsage(AnalysisUsage &Info) const {
367 Info.setPreservesAll();
371 // Active Pass Managers
372 BasicBlockPassManager_New *activeBBPassManager;
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 {
381 ModulePassManager_New() { activeFunctionPassManager = NULL; }
383 /// Add a pass into a passmanager queue.
384 bool addPass(Pass *p);
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);
390 /// Return true IFF AnalysisID AID is currently available.
391 /// TODO : Replace this method with getAnalysisPass()
392 Pass *getAnalysisPassFromManager(AnalysisID AID);
394 /// Pass Manager itself does not invalidate any analysis info.
395 void getAnalysisUsage(AnalysisUsage &Info) const {
396 Info.setPreservesAll();
400 // Active Pass Manager
401 FunctionPassManagerImpl_New *activeFunctionPassManager;
404 /// PassManager_New manages ModulePassManagers
405 class PassManagerImpl_New : public Pass,
406 public PMDataManager,
407 public PMTopLevelManager {
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'.
416 schedulePass(P, this);
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.
423 /// Return true IFF AnalysisID AID is currently available.
424 /// TODO : Replace this method with getAnalysisPass()
425 Pass *getAnalysisPassFromManager(AnalysisID AID);
427 /// Pass Manager itself does not invalidate any analysis info.
428 void getAnalysisUsage(AnalysisUsage &Info) const {
429 Info.setPreservesAll();
432 inline void addTopLevelPass(Pass *P) {
438 /// Add a pass into a passmanager queue.
439 bool addPass(Pass *p);
441 // Collection of pass managers
442 std::vector<ModulePassManager_New *> PassManagers;
444 // Active Pass Manager
445 ModulePassManager_New *activeManager;
448 } // End of llvm namespace
450 //===----------------------------------------------------------------------===//
451 // PMDataManager implementation
453 /// Return true IFF pass P's required analysis set does not required new
455 bool PMDataManager::manageablePass(Pass *P) {
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.
467 /// Augement AvailableAnalysis by adding analysis made available by pass P.
468 void PMDataManager::recordAvailableAnalysis(Pass *P) {
470 if (const PassInfo *PI = P->getPassInfo()) {
471 AvailableAnalysis[PI] = P;
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;
481 /// Remove Analyss not preserved by Pass P
482 void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
483 AnalysisUsage AnUsage;
484 P->getAnalysisUsage(AnUsage);
486 if (AnUsage.getPreservesAll())
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);
501 /// Remove analysis passes that are not used any longer
502 void PMDataManager::removeDeadPasses(Pass *P) {
503 // TODO : reimplement
506 /// Add pass P into the PassVector. Update
507 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
508 void PMDataManager::addPassToManager(Pass *P,
509 bool ProcessAnalysis) {
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);
520 PassVector.push_back(P);
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.
528 void PMDataManager::initializeAnalysisImpl(Pass *P) {
529 AnalysisUsage AnUsage;
530 P->getAnalysisUsage(AnUsage);
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);
537 assert(0 && "Analysis used but not available!");
538 // TODO: P->AnalysisImpls.push_back(std::make_pair(*I, Impl));
542 //===----------------------------------------------------------------------===//
543 // BasicBlockPassManager_New implementation
545 /// Add pass P into PassVector and return true. If this pass is not
546 /// manageable by this manager then return false.
548 BasicBlockPassManager_New::addPass(Pass *P) {
550 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
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))
559 addPassToManager (BP);
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.
568 BasicBlockPassManager_New::runOnFunction(Function &F) {
570 bool Changed = false;
571 initializeAnalysisInfo();
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) {
578 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
579 Changed |= BP->runOnBasicBlock(*I);
580 removeNotPreservedAnalysis(P);
581 recordAvailableAnalysis(P);
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);
593 //===----------------------------------------------------------------------===//
594 // FunctionPassManager_New implementation
596 /// Create new Function pass manager
597 FunctionPassManager_New::FunctionPassManager_New() {
598 FPM = new FunctionPassManagerImpl_New();
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) {
610 /// Execute all of the passes scheduled for execution. Keep
611 /// track of whether any of the passes modifies the function, and if
613 bool FunctionPassManager_New::runOnModule(Module &M) {
614 return FPM->runOnModule(M);
617 /// run - Execute all of the passes scheduled for execution. Keep
618 /// track of whether any of the passes modifies the function, and if
621 bool FunctionPassManager_New::run(Function &F) {
623 if (MP->materializeFunction(&F, &errstr)) {
624 cerr << "Error reading bytecode file: " << errstr << "\n";
627 return FPM->runOnFunction(F);
631 /// doInitialization - Run all of the initializers for the function passes.
633 bool FunctionPassManager_New::doInitialization() {
634 return FPM->doInitialization(*MP->getModule());
637 /// doFinalization - Run all of the initializers for the function passes.
639 bool FunctionPassManager_New::doFinalization() {
640 return FPM->doFinalization(*MP->getModule());
643 //===----------------------------------------------------------------------===//
644 // FunctionPassManagerImpl_New implementation
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.
650 FunctionPassManagerImpl_New::addPass(Pass *P) {
652 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
653 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
655 if (!activeBBPassManager
656 || !activeBBPassManager->addPass(BP)) {
658 // TODO : intialize AvailableAnalysis info
660 activeBBPassManager = new BasicBlockPassManager_New();
661 addPassToManager(activeBBPassManager, false);
662 if (!activeBBPassManager->addPass(BP))
663 assert(0 && "Unable to add Pass");
668 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
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))
677 addPassToManager (FP);
678 // TODO : intialize AvailableAnalysis info
679 activeBBPassManager = NULL;
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) {
688 bool Changed = false;
689 initializeAnalysisInfo();
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) {
696 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
697 Changed |= FP->runOnFunction(*I);
698 removeNotPreservedAnalysis(P);
699 recordAvailableAnalysis(P);
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) {
710 bool Changed = false;
711 initializeAnalysisInfo();
713 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
714 e = passVectorEnd(); itr != e; ++itr) {
717 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
718 Changed |= FP->runOnFunction(F);
719 removeNotPreservedAnalysis(P);
720 recordAvailableAnalysis(P);
727 /// Return true IFF AnalysisID AID is currently available.
728 /// TODO : Replace this method with getAnalysisPass()
729 Pass *FunctionPassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
731 Pass *P = getAnalysisPass(AID);
735 if (activeBBPassManager &&
736 activeBBPassManager->getAnalysisPass(AID) != 0)
737 return activeBBPassManager->getAnalysisPass(AID);
739 // TODO : Check inactive managers
743 inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
744 bool Changed = false;
746 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
747 e = passVectorEnd(); itr != e; ++itr) {
750 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
751 Changed |= FP->doInitialization(M);
757 inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
758 bool Changed = false;
760 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
761 e = passVectorEnd(); itr != e; ++itr) {
764 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
765 Changed |= FP->doFinalization(M);
772 //===----------------------------------------------------------------------===//
773 // ModulePassManager implementation
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.
779 ModulePassManager_New::addPass(Pass *P) {
781 // If P is FunctionPass then use function pass maanager.
782 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
784 if (!activeFunctionPassManager
785 || !activeFunctionPassManager->addPass(P)) {
787 // TODO : intialize AvailableAnalysis info
788 activeFunctionPassManager = NULL;
790 activeFunctionPassManager = new FunctionPassManagerImpl_New();
791 addPassToManager(activeFunctionPassManager, false);
792 if (!activeFunctionPassManager->addPass(FP))
793 assert(0 && "Unable to add pass");
798 ModulePass *MP = dynamic_cast<ModulePass *>(P);
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))
807 addPassToManager(MP);
808 // TODO : intialize AvailableAnalysis info
809 activeFunctionPassManager = NULL;
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.
818 ModulePassManager_New::runOnModule(Module &M) {
819 bool Changed = false;
820 initializeAnalysisInfo();
822 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
823 e = passVectorEnd(); itr != e; ++itr) {
826 ModulePass *MP = dynamic_cast<ModulePass*>(P);
827 Changed |= MP->runOnModule(M);
828 removeNotPreservedAnalysis(P);
829 recordAvailableAnalysis(P);
835 /// Return true IFF AnalysisID AID is currently available.
836 /// TODO : Replace this method with getAnalysisPass()
837 Pass *ModulePassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
839 Pass *P = getAnalysisPass(AID);
843 if (activeFunctionPassManager &&
844 activeFunctionPassManager->getAnalysisPass(AID) != 0)
845 return activeFunctionPassManager->getAnalysisPass(AID);
847 // TODO : Check inactive managers
851 //===----------------------------------------------------------------------===//
852 // PassManagerImpl implementation
854 /// Return true IFF AnalysisID AID is currently available.
855 /// TODO : Replace this method with getAnalysisPass()
856 Pass *PassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
859 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
860 e = PassManagers.end(); !P && itr != e; ++itr)
861 P = (*itr)->getAnalysisPassFromManager(AID);
865 // PassManager_New implementation
866 /// Add P into active pass manager or use new module pass manager to
868 bool PassManagerImpl_New::addPass(Pass *P) {
870 if (!activeManager || !activeManager->addPass(P)) {
871 activeManager = new ModulePassManager_New();
872 PassManagers.push_back(activeManager);
873 return activeManager->addPass(P);
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) {
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);
891 //===----------------------------------------------------------------------===//
892 // PassManager implementation
894 /// Create new pass manager
895 PassManager_New::PassManager_New() {
896 PM = new PassManagerImpl_New();
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'.
904 PassManager_New::add(Pass *P) {
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.
911 PassManager_New::run(Module &M) {