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 /// Populate RequiredPasses with the analysis pass that are required by
257 void collectRequiredAnalysisPasses(std::vector<Pass *> &RequiredPasses,
260 /// All Required analyses should be available to the pass as it runs! Here
261 /// we fill in the AnalysisImpls member of the pass so that it can
262 /// successfully use the getAnalysis() method to retrieve the
263 /// implementations it needs.
264 void initializeAnalysisImpl(Pass *P);
266 inline std::vector<Pass *>::iterator passVectorBegin() {
267 return PassVector.begin();
270 inline std::vector<Pass *>::iterator passVectorEnd() {
271 return PassVector.end();
274 // Access toplevel manager
275 PMTopLevelManager *getTopLevelManager() { return TPM; }
276 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
279 // Set of available Analysis. This information is used while scheduling
280 // pass. If a pass requires an analysis which is not not available then
281 // equired analysis pass is scheduled to run before the pass itself is
283 std::map<AnalysisID, Pass*> AvailableAnalysis;
285 // Collection of pass that are managed by this manager
286 std::vector<Pass *> PassVector;
288 // Top level manager.
289 // TODO : Make it a reference.
290 PMTopLevelManager *TPM;
293 /// BasicBlockPassManager_New manages BasicBlockPass. It batches all the
294 /// pass together and sequence them to process one basic block before
295 /// processing next basic block.
296 class BasicBlockPassManager_New : public PMDataManager,
297 public FunctionPass {
300 BasicBlockPassManager_New() { }
302 /// Add a pass into a passmanager queue.
303 bool addPass(Pass *p);
305 /// Execute all of the passes scheduled for execution. Keep track of
306 /// whether any of the passes modifies the function, and if so, return true.
307 bool runOnFunction(Function &F);
309 /// Return true IFF AnalysisID AID is currently available.
310 /// TODO : Replace this method with getAnalysisPass()
311 Pass *getAnalysisPassFromManager(AnalysisID AID);
313 /// Pass Manager itself does not invalidate any analysis info.
314 void getAnalysisUsage(AnalysisUsage &Info) const {
315 Info.setPreservesAll();
321 /// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
322 /// It batches all function passes and basic block pass managers together and
323 /// sequence them to process one function at a time before processing next
325 class FunctionPassManagerImpl_New : public ModulePass,
326 public PMDataManager,
327 public PMTopLevelManager {
329 FunctionPassManagerImpl_New(ModuleProvider *P) { /* TODO */ }
330 FunctionPassManagerImpl_New() {
331 activeBBPassManager = NULL;
333 ~FunctionPassManagerImpl_New() { /* TODO */ };
335 inline void addTopLevelPass(Pass *P) {
339 /// add - Add a pass to the queue of passes to run. This passes
340 /// ownership of the Pass to the PassManager. When the
341 /// PassManager_X is destroyed, the pass will be destroyed as well, so
342 /// there is no need to delete the pass. (TODO delete passes.)
343 /// This implies that all passes MUST be allocated with 'new'.
345 schedulePass(P, this);
348 /// Add pass into the pass manager queue.
349 bool addPass(Pass *P);
351 /// Execute all of the passes scheduled for execution. Keep
352 /// track of whether any of the passes modifies the function, and if
354 bool runOnModule(Module &M);
355 bool runOnFunction(Function &F);
357 /// Return true IFF AnalysisID AID is currently available.
358 /// TODO : Replace this method with getAnalysisPass()
359 Pass *getAnalysisPassFromManager(AnalysisID AID);
361 /// doInitialization - Run all of the initializers for the function passes.
363 bool doInitialization(Module &M);
365 /// doFinalization - Run all of the initializers for the function passes.
367 bool doFinalization(Module &M);
369 /// Pass Manager itself does not invalidate any analysis info.
370 void getAnalysisUsage(AnalysisUsage &Info) const {
371 Info.setPreservesAll();
375 // Active Pass Managers
376 BasicBlockPassManager_New *activeBBPassManager;
379 /// ModulePassManager_New manages ModulePasses and function pass managers.
380 /// It batches all Module passes passes and function pass managers together and
381 /// sequence them to process one module.
382 class ModulePassManager_New : public PMDataManager {
385 ModulePassManager_New() { activeFunctionPassManager = NULL; }
387 /// Add a pass into a passmanager queue.
388 bool addPass(Pass *p);
390 /// run - Execute all of the passes scheduled for execution. Keep track of
391 /// whether any of the passes modifies the module, and if so, return true.
392 bool runOnModule(Module &M);
394 /// Return true IFF AnalysisID AID is currently available.
395 /// TODO : Replace this method with getAnalysisPass()
396 Pass *getAnalysisPassFromManager(AnalysisID AID);
398 /// Pass Manager itself does not invalidate any analysis info.
399 void getAnalysisUsage(AnalysisUsage &Info) const {
400 Info.setPreservesAll();
404 // Active Pass Manager
405 FunctionPassManagerImpl_New *activeFunctionPassManager;
408 /// PassManager_New manages ModulePassManagers
409 class PassManagerImpl_New : public Pass,
410 public PMDataManager,
411 public PMTopLevelManager {
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'.
420 schedulePass(P, this);
423 /// run - Execute all of the passes scheduled for execution. Keep track of
424 /// whether any of the passes modifies the module, and if so, return true.
427 /// Return true IFF AnalysisID AID is currently available.
428 /// TODO : Replace this method with getAnalysisPass()
429 Pass *getAnalysisPassFromManager(AnalysisID AID);
431 /// Pass Manager itself does not invalidate any analysis info.
432 void getAnalysisUsage(AnalysisUsage &Info) const {
433 Info.setPreservesAll();
436 inline void addTopLevelPass(Pass *P) {
442 /// Add a pass into a passmanager queue.
443 bool addPass(Pass *p);
445 // Collection of pass managers
446 std::vector<ModulePassManager_New *> PassManagers;
448 // Active Pass Manager
449 ModulePassManager_New *activeManager;
452 } // End of llvm namespace
454 //===----------------------------------------------------------------------===//
455 // PMDataManager implementation
457 /// Return true IFF pass P's required analysis set does not required new
459 bool PMDataManager::manageablePass(Pass *P) {
462 // If this pass is not preserving information that is required by a
463 // pass maintained by higher level pass manager then do not insert
464 // this pass into current manager. Use new manager. For example,
465 // For example, If FunctionPass F is not preserving ModulePass Info M1
466 // that is used by another ModulePass M2 then do not insert F in
467 // current function pass manager.
471 /// Augement AvailableAnalysis by adding analysis made available by pass P.
472 void PMDataManager::recordAvailableAnalysis(Pass *P) {
474 if (const PassInfo *PI = P->getPassInfo()) {
475 AvailableAnalysis[PI] = P;
477 //This pass is the current implementation of all of the interfaces it
478 //implements as well.
479 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
480 for (unsigned i = 0, e = II.size(); i != e; ++i)
481 AvailableAnalysis[II[i]] = P;
485 /// Remove Analyss not preserved by Pass P
486 void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
487 AnalysisUsage AnUsage;
488 P->getAnalysisUsage(AnUsage);
490 if (AnUsage.getPreservesAll())
493 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
494 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
495 E = AvailableAnalysis.end(); I != E; ++I ) {
496 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
497 PreservedSet.end()) {
498 // Remove this analysis
499 std::map<AnalysisID, Pass*>::iterator J = I++;
500 AvailableAnalysis.erase(J);
505 /// Remove analysis passes that are not used any longer
506 void PMDataManager::removeDeadPasses(Pass *P) {
507 // TODO : reimplement
510 /// Add pass P into the PassVector. Update
511 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
512 void PMDataManager::addPassToManager(Pass *P,
513 bool ProcessAnalysis) {
515 if (ProcessAnalysis) {
516 // Take a note of analysis required and made available by this pass.
517 // Remove the analysis not preserved by this pass
518 initializeAnalysisImpl(P);
519 removeNotPreservedAnalysis(P);
520 recordAvailableAnalysis(P);
524 PassVector.push_back(P);
527 /// Populate RequiredPasses with the analysis pass that are required by
529 void PMDataManager::collectRequiredAnalysisPasses(std::vector<Pass *> &RP,
531 AnalysisUsage AnUsage;
532 P->getAnalysisUsage(AnUsage);
533 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
534 for (std::vector<AnalysisID>::const_iterator
535 I = RequiredSet.begin(), E = RequiredSet.end();
537 Pass *AnalysisPass = NULL; //FIXME findAnalysisPass(*I,true);
538 assert (AnalysisPass && "Analysis pass is not available");
539 RP.push_back(AnalysisPass);
543 // All Required analyses should be available to the pass as it runs! Here
544 // we fill in the AnalysisImpls member of the pass so that it can
545 // successfully use the getAnalysis() method to retrieve the
546 // implementations it needs.
548 void PMDataManager::initializeAnalysisImpl(Pass *P) {
549 AnalysisUsage AnUsage;
550 P->getAnalysisUsage(AnUsage);
552 for (std::vector<const PassInfo *>::const_iterator
553 I = AnUsage.getRequiredSet().begin(),
554 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
555 Pass *Impl = getAnalysisPass(*I);
557 assert(0 && "Analysis used but not available!");
558 // TODO: P->AnalysisImpls.push_back(std::make_pair(*I, Impl));
562 //===----------------------------------------------------------------------===//
563 // BasicBlockPassManager_New implementation
565 /// Add pass P into PassVector and return true. If this pass is not
566 /// manageable by this manager then return false.
568 BasicBlockPassManager_New::addPass(Pass *P) {
570 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
574 // If this pass does not preserve anlysis that is used by other passes
575 // managed by this manager than it is not a suiable pass for this manager.
576 if (!manageablePass(P))
579 addPassToManager (BP);
584 /// Execute all of the passes scheduled for execution by invoking
585 /// runOnBasicBlock method. Keep track of whether any of the passes modifies
586 /// the function, and if so, return true.
588 BasicBlockPassManager_New::runOnFunction(Function &F) {
590 bool Changed = false;
591 initializeAnalysisInfo();
593 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
594 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
595 e = passVectorEnd(); itr != e; ++itr) {
598 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
599 Changed |= BP->runOnBasicBlock(*I);
600 removeNotPreservedAnalysis(P);
601 recordAvailableAnalysis(P);
607 /// Return true IFF AnalysisID AID is currently available.
608 /// TODO : Replace this method with getAnalysisPass()
609 Pass * BasicBlockPassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
610 return getAnalysisPass(AID);
613 //===----------------------------------------------------------------------===//
614 // FunctionPassManager_New implementation
616 /// Create new Function pass manager
617 FunctionPassManager_New::FunctionPassManager_New() {
618 FPM = new FunctionPassManagerImpl_New();
621 /// add - Add a pass to the queue of passes to run. This passes
622 /// ownership of the Pass to the PassManager. When the
623 /// PassManager_X is destroyed, the pass will be destroyed as well, so
624 /// there is no need to delete the pass. (TODO delete passes.)
625 /// This implies that all passes MUST be allocated with 'new'.
626 void FunctionPassManager_New::add(Pass *P) {
630 /// Execute all of the passes scheduled for execution. Keep
631 /// track of whether any of the passes modifies the function, and if
633 bool FunctionPassManager_New::runOnModule(Module &M) {
634 return FPM->runOnModule(M);
637 /// run - Execute all of the passes scheduled for execution. Keep
638 /// track of whether any of the passes modifies the function, and if
641 bool FunctionPassManager_New::run(Function &F) {
643 if (MP->materializeFunction(&F, &errstr)) {
644 cerr << "Error reading bytecode file: " << errstr << "\n";
647 return FPM->runOnFunction(F);
651 /// doInitialization - Run all of the initializers for the function passes.
653 bool FunctionPassManager_New::doInitialization() {
654 return FPM->doInitialization(*MP->getModule());
657 /// doFinalization - Run all of the initializers for the function passes.
659 bool FunctionPassManager_New::doFinalization() {
660 return FPM->doFinalization(*MP->getModule());
663 //===----------------------------------------------------------------------===//
664 // FunctionPassManagerImpl_New implementation
666 /// Add pass P into the pass manager queue. If P is a BasicBlockPass then
667 /// either use it into active basic block pass manager or create new basic
668 /// block pass manager to handle pass P.
670 FunctionPassManagerImpl_New::addPass(Pass *P) {
672 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
673 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
675 if (!activeBBPassManager || !activeBBPassManager->addPass(BP)) {
677 // If active manager exists then clear its analysis info.
678 if (activeBBPassManager)
679 activeBBPassManager->initializeAnalysisInfo();
681 // Create and add new manager
682 activeBBPassManager = new BasicBlockPassManager_New();
683 addPassToManager(activeBBPassManager, false);
685 // Add pass into new manager. This time it must succeed.
686 if (!activeBBPassManager->addPass(BP))
687 assert(0 && "Unable to add Pass");
692 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
696 // If this pass does not preserve anlysis that is used by other passes
697 // managed by this manager than it is not a suiable pass for this manager.
698 if (!manageablePass(P))
701 addPassToManager (FP);
703 // If active manager exists then clear its analysis info.
704 if (activeBBPassManager) {
705 activeBBPassManager->initializeAnalysisInfo();
706 activeBBPassManager = NULL;
712 /// Execute all of the passes scheduled for execution by invoking
713 /// runOnFunction method. Keep track of whether any of the passes modifies
714 /// the function, and if so, return true.
715 bool FunctionPassManagerImpl_New::runOnModule(Module &M) {
717 bool Changed = false;
718 initializeAnalysisInfo();
720 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
721 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
722 e = passVectorEnd(); itr != e; ++itr) {
725 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
726 Changed |= FP->runOnFunction(*I);
727 removeNotPreservedAnalysis(P);
728 recordAvailableAnalysis(P);
734 /// Execute all of the passes scheduled for execution by invoking
735 /// runOnFunction method. Keep track of whether any of the passes modifies
736 /// the function, and if so, return true.
737 bool FunctionPassManagerImpl_New::runOnFunction(Function &F) {
739 bool Changed = false;
740 initializeAnalysisInfo();
742 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
743 e = passVectorEnd(); itr != e; ++itr) {
746 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
747 Changed |= FP->runOnFunction(F);
748 removeNotPreservedAnalysis(P);
749 recordAvailableAnalysis(P);
756 /// Return true IFF AnalysisID AID is currently available.
757 /// TODO : Replace this method with getAnalysisPass()
758 Pass *FunctionPassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
760 Pass *P = getAnalysisPass(AID);
764 if (activeBBPassManager &&
765 activeBBPassManager->getAnalysisPass(AID) != 0)
766 return activeBBPassManager->getAnalysisPass(AID);
768 // TODO : Check inactive managers
772 inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
773 bool Changed = false;
775 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
776 e = passVectorEnd(); itr != e; ++itr) {
779 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
780 Changed |= FP->doInitialization(M);
786 inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
787 bool Changed = false;
789 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
790 e = passVectorEnd(); itr != e; ++itr) {
793 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
794 Changed |= FP->doFinalization(M);
801 //===----------------------------------------------------------------------===//
802 // ModulePassManager implementation
804 /// Add P into pass vector if it is manageble. If P is a FunctionPass
805 /// then use FunctionPassManagerImpl_New to manage it. Return false if P
806 /// is not manageable by this manager.
808 ModulePassManager_New::addPass(Pass *P) {
810 // If P is FunctionPass then use function pass maanager.
811 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
813 if (!activeFunctionPassManager
814 || !activeFunctionPassManager->addPass(P)) {
816 // If active manager exists then clear its analysis info.
817 if (activeFunctionPassManager)
818 activeFunctionPassManager->initializeAnalysisInfo();
820 // Create and add new manager
821 activeFunctionPassManager = new FunctionPassManagerImpl_New();
822 addPassToManager(activeFunctionPassManager, false);
824 // Add pass into new manager. This time it must succeed.
825 if (!activeFunctionPassManager->addPass(FP))
826 assert(0 && "Unable to add pass");
831 ModulePass *MP = dynamic_cast<ModulePass *>(P);
835 // If this pass does not preserve anlysis that is used by other passes
836 // managed by this manager than it is not a suiable pass for this manager.
837 if (!manageablePass(P))
840 addPassToManager(MP);
841 // If active manager exists then clear its analysis info.
842 if (activeFunctionPassManager) {
843 activeFunctionPassManager->initializeAnalysisInfo();
844 activeFunctionPassManager = NULL;
851 /// Execute all of the passes scheduled for execution by invoking
852 /// runOnModule method. Keep track of whether any of the passes modifies
853 /// the module, and if so, return true.
855 ModulePassManager_New::runOnModule(Module &M) {
856 bool Changed = false;
857 initializeAnalysisInfo();
859 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
860 e = passVectorEnd(); itr != e; ++itr) {
863 ModulePass *MP = dynamic_cast<ModulePass*>(P);
864 Changed |= MP->runOnModule(M);
865 removeNotPreservedAnalysis(P);
866 recordAvailableAnalysis(P);
872 /// Return true IFF AnalysisID AID is currently available.
873 /// TODO : Replace this method with getAnalysisPass()
874 Pass *ModulePassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
876 Pass *P = getAnalysisPass(AID);
880 if (activeFunctionPassManager &&
881 activeFunctionPassManager->getAnalysisPass(AID) != 0)
882 return activeFunctionPassManager->getAnalysisPass(AID);
884 // TODO : Check inactive managers
888 //===----------------------------------------------------------------------===//
889 // PassManagerImpl implementation
891 /// Return true IFF AnalysisID AID is currently available.
892 /// TODO : Replace this method with getAnalysisPass()
893 Pass *PassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
896 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
897 e = PassManagers.end(); !P && itr != e; ++itr)
898 P = (*itr)->getAnalysisPassFromManager(AID);
902 // PassManager_New implementation
903 /// Add P into active pass manager or use new module pass manager to
905 bool PassManagerImpl_New::addPass(Pass *P) {
907 if (!activeManager || !activeManager->addPass(P)) {
908 activeManager = new ModulePassManager_New();
909 PassManagers.push_back(activeManager);
910 return activeManager->addPass(P);
915 /// run - Execute all of the passes scheduled for execution. Keep track of
916 /// whether any of the passes modifies the module, and if so, return true.
917 bool PassManagerImpl_New::run(Module &M) {
919 bool Changed = false;
920 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
921 e = PassManagers.end(); itr != e; ++itr) {
922 ModulePassManager_New *pm = *itr;
923 Changed |= pm->runOnModule(M);
928 //===----------------------------------------------------------------------===//
929 // PassManager implementation
931 /// Create new pass manager
932 PassManager_New::PassManager_New() {
933 PM = new PassManagerImpl_New();
936 /// add - Add a pass to the queue of passes to run. This passes ownership of
937 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
938 /// will be destroyed as well, so there is no need to delete the pass. This
939 /// implies that all passes MUST be allocated with 'new'.
941 PassManager_New::add(Pass *P) {
945 /// run - Execute all of the passes scheduled for execution. Keep track of
946 /// whether any of the passes modifies the module, and if so, return true.
948 PassManager_New::run(Module &M) {