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 || !activeBBPassManager->addPass(BP)) {
657 // If active manager exists then clear its analysis info.
658 if (activeBBPassManager)
659 activeBBPassManager->initializeAnalysisInfo();
661 // Create and add new manager
662 activeBBPassManager = new BasicBlockPassManager_New();
663 addPassToManager(activeBBPassManager, false);
665 // Add pass into new manager. This time it must succeed.
666 if (!activeBBPassManager->addPass(BP))
667 assert(0 && "Unable to add Pass");
672 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
676 // If this pass does not preserve anlysis that is used by other passes
677 // managed by this manager than it is not a suiable pass for this manager.
678 if (!manageablePass(P))
681 addPassToManager (FP);
683 // If active manager exists then clear its analysis info.
684 if (activeBBPassManager) {
685 activeBBPassManager->initializeAnalysisInfo();
686 activeBBPassManager = NULL;
692 /// Execute all of the passes scheduled for execution by invoking
693 /// runOnFunction method. Keep track of whether any of the passes modifies
694 /// the function, and if so, return true.
695 bool FunctionPassManagerImpl_New::runOnModule(Module &M) {
697 bool Changed = false;
698 initializeAnalysisInfo();
700 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
701 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
702 e = passVectorEnd(); itr != e; ++itr) {
705 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
706 Changed |= FP->runOnFunction(*I);
707 removeNotPreservedAnalysis(P);
708 recordAvailableAnalysis(P);
714 /// Execute all of the passes scheduled for execution by invoking
715 /// runOnFunction method. Keep track of whether any of the passes modifies
716 /// the function, and if so, return true.
717 bool FunctionPassManagerImpl_New::runOnFunction(Function &F) {
719 bool Changed = false;
720 initializeAnalysisInfo();
722 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
723 e = passVectorEnd(); itr != e; ++itr) {
726 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
727 Changed |= FP->runOnFunction(F);
728 removeNotPreservedAnalysis(P);
729 recordAvailableAnalysis(P);
736 /// Return true IFF AnalysisID AID is currently available.
737 /// TODO : Replace this method with getAnalysisPass()
738 Pass *FunctionPassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
740 Pass *P = getAnalysisPass(AID);
744 if (activeBBPassManager &&
745 activeBBPassManager->getAnalysisPass(AID) != 0)
746 return activeBBPassManager->getAnalysisPass(AID);
748 // TODO : Check inactive managers
752 inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
753 bool Changed = false;
755 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
756 e = passVectorEnd(); itr != e; ++itr) {
759 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
760 Changed |= FP->doInitialization(M);
766 inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
767 bool Changed = false;
769 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
770 e = passVectorEnd(); itr != e; ++itr) {
773 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
774 Changed |= FP->doFinalization(M);
781 //===----------------------------------------------------------------------===//
782 // ModulePassManager implementation
784 /// Add P into pass vector if it is manageble. If P is a FunctionPass
785 /// then use FunctionPassManagerImpl_New to manage it. Return false if P
786 /// is not manageable by this manager.
788 ModulePassManager_New::addPass(Pass *P) {
790 // If P is FunctionPass then use function pass maanager.
791 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
793 if (!activeFunctionPassManager
794 || !activeFunctionPassManager->addPass(P)) {
796 // If active manager exists then clear its analysis info.
797 if (activeFunctionPassManager)
798 activeFunctionPassManager->initializeAnalysisInfo();
800 // Create and add new manager
801 activeFunctionPassManager = new FunctionPassManagerImpl_New();
802 addPassToManager(activeFunctionPassManager, false);
804 // Add pass into new manager. This time it must succeed.
805 if (!activeFunctionPassManager->addPass(FP))
806 assert(0 && "Unable to add pass");
811 ModulePass *MP = dynamic_cast<ModulePass *>(P);
815 // If this pass does not preserve anlysis that is used by other passes
816 // managed by this manager than it is not a suiable pass for this manager.
817 if (!manageablePass(P))
820 addPassToManager(MP);
821 // If active manager exists then clear its analysis info.
822 if (activeFunctionPassManager) {
823 activeFunctionPassManager->initializeAnalysisInfo();
824 activeFunctionPassManager = NULL;
831 /// Execute all of the passes scheduled for execution by invoking
832 /// runOnModule method. Keep track of whether any of the passes modifies
833 /// the module, and if so, return true.
835 ModulePassManager_New::runOnModule(Module &M) {
836 bool Changed = false;
837 initializeAnalysisInfo();
839 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
840 e = passVectorEnd(); itr != e; ++itr) {
843 ModulePass *MP = dynamic_cast<ModulePass*>(P);
844 Changed |= MP->runOnModule(M);
845 removeNotPreservedAnalysis(P);
846 recordAvailableAnalysis(P);
852 /// Return true IFF AnalysisID AID is currently available.
853 /// TODO : Replace this method with getAnalysisPass()
854 Pass *ModulePassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
856 Pass *P = getAnalysisPass(AID);
860 if (activeFunctionPassManager &&
861 activeFunctionPassManager->getAnalysisPass(AID) != 0)
862 return activeFunctionPassManager->getAnalysisPass(AID);
864 // TODO : Check inactive managers
868 //===----------------------------------------------------------------------===//
869 // PassManagerImpl implementation
871 /// Return true IFF AnalysisID AID is currently available.
872 /// TODO : Replace this method with getAnalysisPass()
873 Pass *PassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
876 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
877 e = PassManagers.end(); !P && itr != e; ++itr)
878 P = (*itr)->getAnalysisPassFromManager(AID);
882 // PassManager_New implementation
883 /// Add P into active pass manager or use new module pass manager to
885 bool PassManagerImpl_New::addPass(Pass *P) {
887 if (!activeManager || !activeManager->addPass(P)) {
888 activeManager = new ModulePassManager_New();
889 PassManagers.push_back(activeManager);
890 return activeManager->addPass(P);
895 /// run - Execute all of the passes scheduled for execution. Keep track of
896 /// whether any of the passes modifies the module, and if so, return true.
897 bool PassManagerImpl_New::run(Module &M) {
899 bool Changed = false;
900 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
901 e = PassManagers.end(); itr != e; ++itr) {
902 ModulePassManager_New *pm = *itr;
903 Changed |= pm->runOnModule(M);
908 //===----------------------------------------------------------------------===//
909 // PassManager implementation
911 /// Create new pass manager
912 PassManager_New::PassManager_New() {
913 PM = new PassManagerImpl_New();
916 /// add - Add a pass to the queue of passes to run. This passes ownership of
917 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
918 /// will be destroyed as well, so there is no need to delete the pass. This
919 /// implies that all passes MUST be allocated with 'new'.
921 PassManager_New::add(Pass *P) {
925 /// run - Execute all of the passes scheduled for execution. Keep track of
926 /// whether any of the passes modifies the module, and if so, return true.
928 PassManager_New::run(Module &M) {