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();
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);
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.
261 void initializeAnalysisImpl(Pass *P);
263 inline std::vector<Pass *>::iterator passVectorBegin() {
264 return PassVector.begin();
267 inline std::vector<Pass *>::iterator passVectorEnd() {
268 return PassVector.end();
271 inline void setLastUser(Pass *P, Pass *LU) {
273 // TODO : Check if pass P is available.
276 // Access toplevel manager
277 PMTopLevelManager *getTopLevelManager() { return TPM; }
278 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
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
285 std::map<AnalysisID, Pass*> AvailableAnalysis;
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;
291 // Collection of pass that are managed by this manager
292 std::vector<Pass *> PassVector;
294 // Top level manager.
295 // TODO : Make it a reference.
296 PMTopLevelManager *TPM;
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 {
306 BasicBlockPassManager_New() { }
308 /// Add a pass into a passmanager queue.
309 bool addPass(Pass *p);
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);
315 /// Return true IFF AnalysisID AID is currently available.
316 Pass *getAnalysisPassFromManager(AnalysisID AID);
318 /// Pass Manager itself does not invalidate any analysis info.
319 void getAnalysisUsage(AnalysisUsage &Info) const {
320 Info.setPreservesAll();
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
330 class FunctionPassManagerImpl_New : public ModulePass,
331 public PMDataManager,
332 public PMTopLevelManager {
334 FunctionPassManagerImpl_New(ModuleProvider *P) { /* TODO */ }
335 FunctionPassManagerImpl_New() {
336 activeBBPassManager = NULL;
338 ~FunctionPassManagerImpl_New() { /* TODO */ };
340 inline void addTopLevelPass(Pass *P) {
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'.
350 schedulePass(P, this);
353 /// Add pass into the pass manager queue.
354 bool addPass(Pass *P);
356 /// Execute all of the passes scheduled for execution. Keep
357 /// track of whether any of the passes modifies the function, and if
359 bool runOnModule(Module &M);
360 bool runOnFunction(Function &F);
362 /// Return true IFF AnalysisID AID is currently available.
363 Pass *getAnalysisPassFromManager(AnalysisID AID);
365 /// doInitialization - Run all of the initializers for the function passes.
367 bool doInitialization(Module &M);
369 /// doFinalization - Run all of the initializers for the function passes.
371 bool doFinalization(Module &M);
373 /// Pass Manager itself does not invalidate any analysis info.
374 void getAnalysisUsage(AnalysisUsage &Info) const {
375 Info.setPreservesAll();
379 // Active Pass Managers
380 BasicBlockPassManager_New *activeBBPassManager;
383 /// ModulePassManager_New manages ModulePasses and function pass managers.
384 /// It batches all Module passes passes and function pass managers together and
385 /// sequence them to process one module.
386 class ModulePassManager_New : public PMDataManager {
389 ModulePassManager_New() { activeFunctionPassManager = NULL; }
391 /// Add a pass into a passmanager queue.
392 bool addPass(Pass *p);
394 /// run - Execute all of the passes scheduled for execution. Keep track of
395 /// whether any of the passes modifies the module, and if so, return true.
396 bool runOnModule(Module &M);
398 /// Return true IFF AnalysisID AID is currently available.
399 Pass *getAnalysisPassFromManager(AnalysisID AID);
401 /// Pass Manager itself does not invalidate any analysis info.
402 void getAnalysisUsage(AnalysisUsage &Info) const {
403 Info.setPreservesAll();
407 // Active Pass Manager
408 FunctionPassManagerImpl_New *activeFunctionPassManager;
411 /// PassManager_New manages ModulePassManagers
412 class PassManagerImpl_New : public Pass,
413 public PMDataManager,
414 public PMTopLevelManager {
418 /// add - Add a pass to the queue of passes to run. This passes ownership of
419 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
420 /// will be destroyed as well, so there is no need to delete the pass. This
421 /// implies that all passes MUST be allocated with 'new'.
423 schedulePass(P, this);
426 /// run - Execute all of the passes scheduled for execution. Keep track of
427 /// whether any of the passes modifies the module, and if so, return true.
430 /// Return true IFF AnalysisID AID is currently available.
431 Pass *getAnalysisPassFromManager(AnalysisID AID);
433 /// Pass Manager itself does not invalidate any analysis info.
434 void getAnalysisUsage(AnalysisUsage &Info) const {
435 Info.setPreservesAll();
438 inline void addTopLevelPass(Pass *P) {
444 /// Add a pass into a passmanager queue.
445 bool addPass(Pass *p);
447 // Collection of pass managers
448 std::vector<ModulePassManager_New *> PassManagers;
450 // Active Pass Manager
451 ModulePassManager_New *activeManager;
454 } // End of llvm namespace
456 //===----------------------------------------------------------------------===//
457 // PMDataManager implementation
459 /// Return true IFF pass P's required analysis set does not required new
461 bool PMDataManager::manageablePass(Pass *P) {
464 // If this pass is not preserving information that is required by a
465 // pass maintained by higher level pass manager then do not insert
466 // this pass into current manager. Use new manager. For example,
467 // For example, If FunctionPass F is not preserving ModulePass Info M1
468 // that is used by another ModulePass M2 then do not insert F in
469 // current function pass manager.
473 /// Augement AvailableAnalysis by adding analysis made available by pass P.
474 void PMDataManager::recordAvailableAnalysis(Pass *P) {
476 if (const PassInfo *PI = P->getPassInfo()) {
477 AvailableAnalysis[PI] = P;
479 //This pass is the current implementation of all of the interfaces it
480 //implements as well.
481 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
482 for (unsigned i = 0, e = II.size(); i != e; ++i)
483 AvailableAnalysis[II[i]] = P;
487 /// Remove Analyss not preserved by Pass P
488 void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
489 AnalysisUsage AnUsage;
490 P->getAnalysisUsage(AnUsage);
492 if (AnUsage.getPreservesAll())
495 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
496 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
497 E = AvailableAnalysis.end(); I != E; ++I ) {
498 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
499 PreservedSet.end()) {
500 // Remove this analysis
501 std::map<AnalysisID, Pass*>::iterator J = I++;
502 AvailableAnalysis.erase(J);
507 /// Remove analysis passes that are not used any longer
508 void PMDataManager::removeDeadPasses(Pass *P) {
510 for (std::map<Pass *, Pass *>::iterator I = LastUser.begin(),
511 E = LastUser.end(); I !=E; ++I) {
512 if (I->second == P) {
513 Pass *deadPass = I->first;
514 deadPass->releaseMemory();
516 std::map<AnalysisID, Pass*>::iterator Pos =
517 AvailableAnalysis.find(deadPass->getPassInfo());
519 assert (Pos != AvailableAnalysis.end() &&
520 "Pass is not available");
521 AvailableAnalysis.erase(Pos);
526 /// Add pass P into the PassVector. Update
527 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
528 void PMDataManager::addPassToManager(Pass *P,
529 bool ProcessAnalysis) {
531 if (ProcessAnalysis) {
532 // Take a note of analysis required and made available by this pass
533 initializeAnalysisImpl(P);
534 recordAvailableAnalysis(P);
536 // Remove the analysis not preserved by this pass
537 removeNotPreservedAnalysis(P);
541 PassVector.push_back(P);
544 // All Required analyses should be available to the pass as it runs! Here
545 // we fill in the AnalysisImpls member of the pass so that it can
546 // successfully use the getAnalysis() method to retrieve the
547 // implementations it needs.
549 void PMDataManager::initializeAnalysisImpl(Pass *P) {
550 AnalysisUsage AnUsage;
551 P->getAnalysisUsage(AnUsage);
553 for (std::vector<const PassInfo *>::const_iterator
554 I = AnUsage.getRequiredSet().begin(),
555 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
556 Pass *Impl = getAnalysisPass(*I);
558 assert(0 && "Analysis used but not available!");
559 // TODO: P->AnalysisImpls.push_back(std::make_pair(*I, Impl));
563 //===----------------------------------------------------------------------===//
564 // BasicBlockPassManager_New implementation
566 /// Add pass P into PassVector and return true. If this pass is not
567 /// manageable by this manager then return false.
569 BasicBlockPassManager_New::addPass(Pass *P) {
571 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
575 // If this pass does not preserve anlysis that is used by other passes
576 // managed by this manager than it is not a suiable pass for this manager.
577 if (!manageablePass(P))
580 addPassToManager (BP);
585 /// Execute all of the passes scheduled for execution by invoking
586 /// runOnBasicBlock method. Keep track of whether any of the passes modifies
587 /// the function, and if so, return true.
589 BasicBlockPassManager_New::runOnFunction(Function &F) {
591 bool Changed = false;
592 initializeAnalysisInfo();
594 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
595 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
596 e = passVectorEnd(); itr != e; ++itr) {
599 recordAvailableAnalysis(P);
600 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
601 Changed |= BP->runOnBasicBlock(*I);
602 removeNotPreservedAnalysis(P);
608 /// Return true IFF AnalysisID AID is currently available.
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
676 || !activeBBPassManager->addPass(BP)) {
678 activeBBPassManager = new BasicBlockPassManager_New();
679 addPassToManager(activeBBPassManager, false);
680 if (!activeBBPassManager->addPass(BP))
681 assert(0 && "Unable to add Pass");
686 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
690 // If this pass does not preserve anlysis that is used by other passes
691 // managed by this manager than it is not a suiable pass for this manager.
692 if (!manageablePass(P))
695 addPassToManager (FP);
696 activeBBPassManager = NULL;
700 /// Execute all of the passes scheduled for execution by invoking
701 /// runOnFunction method. Keep track of whether any of the passes modifies
702 /// the function, and if so, return true.
703 bool FunctionPassManagerImpl_New::runOnModule(Module &M) {
705 bool Changed = false;
706 initializeAnalysisInfo();
708 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
709 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
710 e = passVectorEnd(); itr != e; ++itr) {
713 recordAvailableAnalysis(P);
714 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
715 Changed |= FP->runOnFunction(*I);
716 removeNotPreservedAnalysis(P);
722 /// Execute all of the passes scheduled for execution by invoking
723 /// runOnFunction method. Keep track of whether any of the passes modifies
724 /// the function, and if so, return true.
725 bool FunctionPassManagerImpl_New::runOnFunction(Function &F) {
727 bool Changed = false;
728 initializeAnalysisInfo();
730 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
731 e = passVectorEnd(); itr != e; ++itr) {
734 recordAvailableAnalysis(P);
735 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
736 Changed |= FP->runOnFunction(F);
737 removeNotPreservedAnalysis(P);
744 /// Return true IFF AnalysisID AID is currently available.
745 Pass *FunctionPassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
747 Pass *P = getAnalysisPass(AID);
751 if (activeBBPassManager &&
752 activeBBPassManager->getAnalysisPass(AID) != 0)
753 return activeBBPassManager->getAnalysisPass(AID);
755 // TODO : Check inactive managers
759 inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
760 bool Changed = false;
762 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
763 e = passVectorEnd(); itr != e; ++itr) {
766 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
767 Changed |= FP->doInitialization(M);
773 inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
774 bool Changed = false;
776 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
777 e = passVectorEnd(); itr != e; ++itr) {
780 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
781 Changed |= FP->doFinalization(M);
788 //===----------------------------------------------------------------------===//
789 // ModulePassManager implementation
791 /// Add P into pass vector if it is manageble. If P is a FunctionPass
792 /// then use FunctionPassManagerImpl_New to manage it. Return false if P
793 /// is not manageable by this manager.
795 ModulePassManager_New::addPass(Pass *P) {
797 // If P is FunctionPass then use function pass maanager.
798 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
800 activeFunctionPassManager = NULL;
802 if (!activeFunctionPassManager
803 || !activeFunctionPassManager->addPass(P)) {
805 activeFunctionPassManager = new FunctionPassManagerImpl_New();
806 addPassToManager(activeFunctionPassManager, false);
807 if (!activeFunctionPassManager->addPass(FP))
808 assert(0 && "Unable to add pass");
813 ModulePass *MP = dynamic_cast<ModulePass *>(P);
817 // If this pass does not preserve anlysis that is used by other passes
818 // managed by this manager than it is not a suiable pass for this manager.
819 if (!manageablePass(P))
822 addPassToManager(MP);
823 activeFunctionPassManager = NULL;
828 /// Execute all of the passes scheduled for execution by invoking
829 /// runOnModule method. Keep track of whether any of the passes modifies
830 /// the module, and if so, return true.
832 ModulePassManager_New::runOnModule(Module &M) {
833 bool Changed = false;
834 initializeAnalysisInfo();
836 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
837 e = passVectorEnd(); itr != e; ++itr) {
840 recordAvailableAnalysis(P);
841 ModulePass *MP = dynamic_cast<ModulePass*>(P);
842 Changed |= MP->runOnModule(M);
843 removeNotPreservedAnalysis(P);
849 /// Return true IFF AnalysisID AID is currently available.
850 Pass *ModulePassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
853 Pass *P = getAnalysisPass(AID);
857 if (activeFunctionPassManager &&
858 activeFunctionPassManager->getAnalysisPass(AID) != 0)
859 return activeFunctionPassManager->getAnalysisPass(AID);
861 // TODO : Check inactive managers
865 //===----------------------------------------------------------------------===//
866 // PassManagerImpl implementation
868 /// Return true IFF AnalysisID AID is currently available.
869 Pass *PassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
872 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
873 e = PassManagers.end(); !P && itr != e; ++itr)
874 P = (*itr)->getAnalysisPassFromManager(AID);
878 // PassManager_New implementation
879 /// Add P into active pass manager or use new module pass manager to
881 bool PassManagerImpl_New::addPass(Pass *P) {
883 if (!activeManager || !activeManager->addPass(P)) {
884 activeManager = new ModulePassManager_New();
885 PassManagers.push_back(activeManager);
886 return activeManager->addPass(P);
891 /// run - Execute all of the passes scheduled for execution. Keep track of
892 /// whether any of the passes modifies the module, and if so, return true.
893 bool PassManagerImpl_New::run(Module &M) {
895 bool Changed = false;
896 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
897 e = PassManagers.end(); itr != e; ++itr) {
898 ModulePassManager_New *pm = *itr;
899 Changed |= pm->runOnModule(M);
904 //===----------------------------------------------------------------------===//
905 // PassManager implementation
907 /// Create new pass manager
908 PassManager_New::PassManager_New() {
909 PM = new PassManagerImpl_New();
912 /// add - Add a pass to the queue of passes to run. This passes ownership of
913 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
914 /// will be destroyed as well, so there is no need to delete the pass. This
915 /// implies that all passes MUST be allocated with 'new'.
917 PassManager_New::add(Pass *P) {
921 /// run - Execute all of the passes scheduled for execution. Keep track of
922 /// whether any of the passes modifies the module, and if so, return true.
924 PassManager_New::run(Module &M) {