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 /// TODO : Replace this method with getAnalysisPass()
317 Pass *getAnalysisPassFromManager(AnalysisID AID);
319 /// Pass Manager itself does not invalidate any analysis info.
320 void getAnalysisUsage(AnalysisUsage &Info) const {
321 Info.setPreservesAll();
327 /// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
328 /// It batches all function passes and basic block pass managers together and
329 /// sequence them to process one function at a time before processing next
331 class FunctionPassManagerImpl_New : public ModulePass,
332 public PMDataManager,
333 public PMTopLevelManager {
335 FunctionPassManagerImpl_New(ModuleProvider *P) { /* TODO */ }
336 FunctionPassManagerImpl_New() {
337 activeBBPassManager = NULL;
339 ~FunctionPassManagerImpl_New() { /* TODO */ };
341 inline void addTopLevelPass(Pass *P) {
345 /// add - Add a pass to the queue of passes to run. This passes
346 /// ownership of the Pass to the PassManager. When the
347 /// PassManager_X is destroyed, the pass will be destroyed as well, so
348 /// there is no need to delete the pass. (TODO delete passes.)
349 /// This implies that all passes MUST be allocated with 'new'.
351 schedulePass(P, this);
354 /// Add pass into the pass manager queue.
355 bool addPass(Pass *P);
357 /// Execute all of the passes scheduled for execution. Keep
358 /// track of whether any of the passes modifies the function, and if
360 bool runOnModule(Module &M);
361 bool runOnFunction(Function &F);
363 /// Return true IFF AnalysisID AID is currently available.
364 /// TODO : Replace this method with getAnalysisPass()
365 Pass *getAnalysisPassFromManager(AnalysisID AID);
367 /// doInitialization - Run all of the initializers for the function passes.
369 bool doInitialization(Module &M);
371 /// doFinalization - Run all of the initializers for the function passes.
373 bool doFinalization(Module &M);
375 /// Pass Manager itself does not invalidate any analysis info.
376 void getAnalysisUsage(AnalysisUsage &Info) const {
377 Info.setPreservesAll();
381 // Active Pass Managers
382 BasicBlockPassManager_New *activeBBPassManager;
385 /// ModulePassManager_New manages ModulePasses and function pass managers.
386 /// It batches all Module passes passes and function pass managers together and
387 /// sequence them to process one module.
388 class ModulePassManager_New : public PMDataManager {
391 ModulePassManager_New() { activeFunctionPassManager = NULL; }
393 /// Add a pass into a passmanager queue.
394 bool addPass(Pass *p);
396 /// run - Execute all of the passes scheduled for execution. Keep track of
397 /// whether any of the passes modifies the module, and if so, return true.
398 bool runOnModule(Module &M);
400 /// Return true IFF AnalysisID AID is currently available.
401 /// TODO : Replace this method with getAnalysisPass()
402 Pass *getAnalysisPassFromManager(AnalysisID AID);
404 /// Pass Manager itself does not invalidate any analysis info.
405 void getAnalysisUsage(AnalysisUsage &Info) const {
406 Info.setPreservesAll();
410 // Active Pass Manager
411 FunctionPassManagerImpl_New *activeFunctionPassManager;
414 /// PassManager_New manages ModulePassManagers
415 class PassManagerImpl_New : public Pass,
416 public PMDataManager,
417 public PMTopLevelManager {
421 /// add - Add a pass to the queue of passes to run. This passes ownership of
422 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
423 /// will be destroyed as well, so there is no need to delete the pass. This
424 /// implies that all passes MUST be allocated with 'new'.
426 schedulePass(P, this);
429 /// run - Execute all of the passes scheduled for execution. Keep track of
430 /// whether any of the passes modifies the module, and if so, return true.
433 /// Return true IFF AnalysisID AID is currently available.
434 /// TODO : Replace this method with getAnalysisPass()
435 Pass *getAnalysisPassFromManager(AnalysisID AID);
437 /// Pass Manager itself does not invalidate any analysis info.
438 void getAnalysisUsage(AnalysisUsage &Info) const {
439 Info.setPreservesAll();
442 inline void addTopLevelPass(Pass *P) {
448 /// Add a pass into a passmanager queue.
449 bool addPass(Pass *p);
451 // Collection of pass managers
452 std::vector<ModulePassManager_New *> PassManagers;
454 // Active Pass Manager
455 ModulePassManager_New *activeManager;
458 } // End of llvm namespace
460 //===----------------------------------------------------------------------===//
461 // PMDataManager implementation
463 /// Return true IFF pass P's required analysis set does not required new
465 bool PMDataManager::manageablePass(Pass *P) {
468 // If this pass is not preserving information that is required by a
469 // pass maintained by higher level pass manager then do not insert
470 // this pass into current manager. Use new manager. For example,
471 // For example, If FunctionPass F is not preserving ModulePass Info M1
472 // that is used by another ModulePass M2 then do not insert F in
473 // current function pass manager.
477 /// Augement AvailableAnalysis by adding analysis made available by pass P.
478 void PMDataManager::recordAvailableAnalysis(Pass *P) {
480 if (const PassInfo *PI = P->getPassInfo()) {
481 AvailableAnalysis[PI] = P;
483 //This pass is the current implementation of all of the interfaces it
484 //implements as well.
485 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
486 for (unsigned i = 0, e = II.size(); i != e; ++i)
487 AvailableAnalysis[II[i]] = P;
491 /// Remove Analyss not preserved by Pass P
492 void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
493 AnalysisUsage AnUsage;
494 P->getAnalysisUsage(AnUsage);
496 if (AnUsage.getPreservesAll())
499 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
500 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
501 E = AvailableAnalysis.end(); I != E; ++I ) {
502 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
503 PreservedSet.end()) {
504 // Remove this analysis
505 std::map<AnalysisID, Pass*>::iterator J = I++;
506 AvailableAnalysis.erase(J);
511 /// Remove analysis passes that are not used any longer
512 void PMDataManager::removeDeadPasses(Pass *P) {
514 for (std::map<Pass *, Pass *>::iterator I = LastUser.begin(),
515 E = LastUser.end(); I !=E; ++I) {
516 if (I->second == P) {
517 Pass *deadPass = I->first;
518 deadPass->releaseMemory();
520 std::map<AnalysisID, Pass*>::iterator Pos =
521 AvailableAnalysis.find(deadPass->getPassInfo());
523 assert (Pos != AvailableAnalysis.end() &&
524 "Pass is not available");
525 AvailableAnalysis.erase(Pos);
530 /// Add pass P into the PassVector. Update
531 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
532 void PMDataManager::addPassToManager(Pass *P,
533 bool ProcessAnalysis) {
535 if (ProcessAnalysis) {
536 // Take a note of analysis required and made available by this pass.
537 // Remove the analysis not preserved by this pass
538 initializeAnalysisImpl(P);
539 removeNotPreservedAnalysis(P);
540 recordAvailableAnalysis(P);
544 PassVector.push_back(P);
547 // All Required analyses should be available to the pass as it runs! Here
548 // we fill in the AnalysisImpls member of the pass so that it can
549 // successfully use the getAnalysis() method to retrieve the
550 // implementations it needs.
552 void PMDataManager::initializeAnalysisImpl(Pass *P) {
553 AnalysisUsage AnUsage;
554 P->getAnalysisUsage(AnUsage);
556 for (std::vector<const PassInfo *>::const_iterator
557 I = AnUsage.getRequiredSet().begin(),
558 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
559 Pass *Impl = getAnalysisPass(*I);
561 assert(0 && "Analysis used but not available!");
562 // TODO: P->AnalysisImpls.push_back(std::make_pair(*I, Impl));
566 //===----------------------------------------------------------------------===//
567 // BasicBlockPassManager_New implementation
569 /// Add pass P into PassVector and return true. If this pass is not
570 /// manageable by this manager then return false.
572 BasicBlockPassManager_New::addPass(Pass *P) {
574 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
578 // If this pass does not preserve anlysis that is used by other passes
579 // managed by this manager than it is not a suiable pass for this manager.
580 if (!manageablePass(P))
583 addPassToManager (BP);
588 /// Execute all of the passes scheduled for execution by invoking
589 /// runOnBasicBlock method. Keep track of whether any of the passes modifies
590 /// the function, and if so, return true.
592 BasicBlockPassManager_New::runOnFunction(Function &F) {
594 bool Changed = false;
595 initializeAnalysisInfo();
597 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
598 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
599 e = passVectorEnd(); itr != e; ++itr) {
602 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
603 Changed |= BP->runOnBasicBlock(*I);
604 removeNotPreservedAnalysis(P);
605 recordAvailableAnalysis(P);
611 /// Return true IFF AnalysisID AID is currently available.
612 /// TODO : Replace this method with getAnalysisPass()
613 Pass * BasicBlockPassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
614 return getAnalysisPass(AID);
617 //===----------------------------------------------------------------------===//
618 // FunctionPassManager_New implementation
620 /// Create new Function pass manager
621 FunctionPassManager_New::FunctionPassManager_New() {
622 FPM = new FunctionPassManagerImpl_New();
625 /// add - Add a pass to the queue of passes to run. This passes
626 /// ownership of the Pass to the PassManager. When the
627 /// PassManager_X is destroyed, the pass will be destroyed as well, so
628 /// there is no need to delete the pass. (TODO delete passes.)
629 /// This implies that all passes MUST be allocated with 'new'.
630 void FunctionPassManager_New::add(Pass *P) {
634 /// Execute all of the passes scheduled for execution. Keep
635 /// track of whether any of the passes modifies the function, and if
637 bool FunctionPassManager_New::runOnModule(Module &M) {
638 return FPM->runOnModule(M);
641 /// run - Execute all of the passes scheduled for execution. Keep
642 /// track of whether any of the passes modifies the function, and if
645 bool FunctionPassManager_New::run(Function &F) {
647 if (MP->materializeFunction(&F, &errstr)) {
648 cerr << "Error reading bytecode file: " << errstr << "\n";
651 return FPM->runOnFunction(F);
655 /// doInitialization - Run all of the initializers for the function passes.
657 bool FunctionPassManager_New::doInitialization() {
658 return FPM->doInitialization(*MP->getModule());
661 /// doFinalization - Run all of the initializers for the function passes.
663 bool FunctionPassManager_New::doFinalization() {
664 return FPM->doFinalization(*MP->getModule());
667 //===----------------------------------------------------------------------===//
668 // FunctionPassManagerImpl_New implementation
670 /// Add pass P into the pass manager queue. If P is a BasicBlockPass then
671 /// either use it into active basic block pass manager or create new basic
672 /// block pass manager to handle pass P.
674 FunctionPassManagerImpl_New::addPass(Pass *P) {
676 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
677 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
679 if (!activeBBPassManager
680 || !activeBBPassManager->addPass(BP)) {
682 // TODO : intialize AvailableAnalysis info
684 activeBBPassManager = new BasicBlockPassManager_New();
685 addPassToManager(activeBBPassManager, false);
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);
702 // TODO : intialize AvailableAnalysis info
703 activeBBPassManager = NULL;
707 /// Execute all of the passes scheduled for execution by invoking
708 /// runOnFunction method. Keep track of whether any of the passes modifies
709 /// the function, and if so, return true.
710 bool FunctionPassManagerImpl_New::runOnModule(Module &M) {
712 bool Changed = false;
713 initializeAnalysisInfo();
715 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
716 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
717 e = passVectorEnd(); itr != e; ++itr) {
720 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
721 Changed |= FP->runOnFunction(*I);
722 removeNotPreservedAnalysis(P);
723 recordAvailableAnalysis(P);
729 /// Execute all of the passes scheduled for execution by invoking
730 /// runOnFunction method. Keep track of whether any of the passes modifies
731 /// the function, and if so, return true.
732 bool FunctionPassManagerImpl_New::runOnFunction(Function &F) {
734 bool Changed = false;
735 initializeAnalysisInfo();
737 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
738 e = passVectorEnd(); itr != e; ++itr) {
741 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
742 Changed |= FP->runOnFunction(F);
743 removeNotPreservedAnalysis(P);
744 recordAvailableAnalysis(P);
751 /// Return true IFF AnalysisID AID is currently available.
752 /// TODO : Replace this method with getAnalysisPass()
753 Pass *FunctionPassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
755 Pass *P = getAnalysisPass(AID);
759 if (activeBBPassManager &&
760 activeBBPassManager->getAnalysisPass(AID) != 0)
761 return activeBBPassManager->getAnalysisPass(AID);
763 // TODO : Check inactive managers
767 inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
768 bool Changed = false;
770 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
771 e = passVectorEnd(); itr != e; ++itr) {
774 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
775 Changed |= FP->doInitialization(M);
781 inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
782 bool Changed = false;
784 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
785 e = passVectorEnd(); itr != e; ++itr) {
788 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
789 Changed |= FP->doFinalization(M);
796 //===----------------------------------------------------------------------===//
797 // ModulePassManager implementation
799 /// Add P into pass vector if it is manageble. If P is a FunctionPass
800 /// then use FunctionPassManagerImpl_New to manage it. Return false if P
801 /// is not manageable by this manager.
803 ModulePassManager_New::addPass(Pass *P) {
805 // If P is FunctionPass then use function pass maanager.
806 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
808 if (!activeFunctionPassManager
809 || !activeFunctionPassManager->addPass(P)) {
811 // TODO : intialize AvailableAnalysis info
812 activeFunctionPassManager = NULL;
814 activeFunctionPassManager = new FunctionPassManagerImpl_New();
815 addPassToManager(activeFunctionPassManager, false);
816 if (!activeFunctionPassManager->addPass(FP))
817 assert(0 && "Unable to add pass");
822 ModulePass *MP = dynamic_cast<ModulePass *>(P);
826 // If this pass does not preserve anlysis that is used by other passes
827 // managed by this manager than it is not a suiable pass for this manager.
828 if (!manageablePass(P))
831 addPassToManager(MP);
832 // TODO : intialize AvailableAnalysis info
833 activeFunctionPassManager = NULL;
838 /// Execute all of the passes scheduled for execution by invoking
839 /// runOnModule method. Keep track of whether any of the passes modifies
840 /// the module, and if so, return true.
842 ModulePassManager_New::runOnModule(Module &M) {
843 bool Changed = false;
844 initializeAnalysisInfo();
846 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
847 e = passVectorEnd(); itr != e; ++itr) {
850 ModulePass *MP = dynamic_cast<ModulePass*>(P);
851 Changed |= MP->runOnModule(M);
852 removeNotPreservedAnalysis(P);
853 recordAvailableAnalysis(P);
859 /// Return true IFF AnalysisID AID is currently available.
860 /// TODO : Replace this method with getAnalysisPass()
861 Pass *ModulePassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
863 Pass *P = getAnalysisPass(AID);
867 if (activeFunctionPassManager &&
868 activeFunctionPassManager->getAnalysisPass(AID) != 0)
869 return activeFunctionPassManager->getAnalysisPass(AID);
871 // TODO : Check inactive managers
875 //===----------------------------------------------------------------------===//
876 // PassManagerImpl implementation
878 /// Return true IFF AnalysisID AID is currently available.
879 /// TODO : Replace this method with getAnalysisPass()
880 Pass *PassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
883 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
884 e = PassManagers.end(); !P && itr != e; ++itr)
885 P = (*itr)->getAnalysisPassFromManager(AID);
889 // PassManager_New implementation
890 /// Add P into active pass manager or use new module pass manager to
892 bool PassManagerImpl_New::addPass(Pass *P) {
894 if (!activeManager || !activeManager->addPass(P)) {
895 activeManager = new ModulePassManager_New();
896 PassManagers.push_back(activeManager);
897 return activeManager->addPass(P);
902 /// run - Execute all of the passes scheduled for execution. Keep track of
903 /// whether any of the passes modifies the module, and if so, return true.
904 bool PassManagerImpl_New::run(Module &M) {
906 bool Changed = false;
907 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
908 e = PassManagers.end(); itr != e; ++itr) {
909 ModulePassManager_New *pm = *itr;
910 Changed |= pm->runOnModule(M);
915 //===----------------------------------------------------------------------===//
916 // PassManager implementation
918 /// Create new pass manager
919 PassManager_New::PassManager_New() {
920 PM = new PassManagerImpl_New();
923 /// add - Add a pass to the queue of passes to run. This passes ownership of
924 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
925 /// will be destroyed as well, so there is no need to delete the pass. This
926 /// implies that all passes MUST be allocated with 'new'.
928 PassManager_New::add(Pass *P) {
932 /// run - Execute all of the passes scheduled for execution. Keep track of
933 /// whether any of the passes modifies the module, and if so, return true.
935 PassManager_New::run(Module &M) {