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(int D) : TPM(NULL), Depth(D) {
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; }
278 unsigned getDepth() { return Depth; }
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 // Collection of pass that are managed by this manager
288 std::vector<Pass *> PassVector;
290 // Top level manager.
291 // TODO : Make it a reference.
292 PMTopLevelManager *TPM;
297 /// BasicBlockPassManager_New manages BasicBlockPass. It batches all the
298 /// pass together and sequence them to process one basic block before
299 /// processing next basic block.
300 class BasicBlockPassManager_New : public PMDataManager,
301 public FunctionPass {
304 BasicBlockPassManager_New(int D) : PMDataManager(D) { }
306 /// Add a pass into a passmanager queue.
307 bool addPass(Pass *p);
309 /// Execute all of the passes scheduled for execution. Keep track of
310 /// whether any of the passes modifies the function, and if so, return true.
311 bool runOnFunction(Function &F);
313 /// Return true IFF AnalysisID AID is currently available.
314 /// TODO : Replace this method with getAnalysisPass()
315 Pass *getAnalysisPassFromManager(AnalysisID AID);
317 /// Pass Manager itself does not invalidate any analysis info.
318 void getAnalysisUsage(AnalysisUsage &Info) const {
319 Info.setPreservesAll();
325 /// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
326 /// It batches all function passes and basic block pass managers together and
327 /// sequence them to process one function at a time before processing next
329 class FunctionPassManagerImpl_New : public ModulePass,
330 public PMDataManager,
331 public PMTopLevelManager {
333 FunctionPassManagerImpl_New(ModuleProvider *P, int D) :
334 PMDataManager(D) { /* TODO */ }
335 FunctionPassManagerImpl_New(int D) : PMDataManager(D) {
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 /// TODO : Replace this method with getAnalysisPass()
364 Pass *getAnalysisPassFromManager(AnalysisID AID);
366 /// doInitialization - Run all of the initializers for the function passes.
368 bool doInitialization(Module &M);
370 /// doFinalization - Run all of the initializers for the function passes.
372 bool doFinalization(Module &M);
374 /// Pass Manager itself does not invalidate any analysis info.
375 void getAnalysisUsage(AnalysisUsage &Info) const {
376 Info.setPreservesAll();
380 // Active Pass Managers
381 BasicBlockPassManager_New *activeBBPassManager;
384 /// ModulePassManager_New manages ModulePasses and function pass managers.
385 /// It batches all Module passes passes and function pass managers together and
386 /// sequence them to process one module.
387 class ModulePassManager_New : public PMDataManager {
390 ModulePassManager_New(int D) : PMDataManager(D) {
391 activeFunctionPassManager = NULL;
394 /// Add a pass into a passmanager queue.
395 bool addPass(Pass *p);
397 /// run - Execute all of the passes scheduled for execution. Keep track of
398 /// whether any of the passes modifies the module, and if so, return true.
399 bool runOnModule(Module &M);
401 /// Return true IFF AnalysisID AID is currently available.
402 /// TODO : Replace this method with getAnalysisPass()
403 Pass *getAnalysisPassFromManager(AnalysisID AID);
405 /// Pass Manager itself does not invalidate any analysis info.
406 void getAnalysisUsage(AnalysisUsage &Info) const {
407 Info.setPreservesAll();
411 // Active Pass Manager
412 FunctionPassManagerImpl_New *activeFunctionPassManager;
415 /// PassManager_New manages ModulePassManagers
416 class PassManagerImpl_New : public Pass,
417 public PMDataManager,
418 public PMTopLevelManager {
422 PassManagerImpl_New(int D) : PMDataManager(D) {}
424 /// add - Add a pass to the queue of passes to run. This passes ownership of
425 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
426 /// will be destroyed as well, so there is no need to delete the pass. This
427 /// implies that all passes MUST be allocated with 'new'.
429 schedulePass(P, this);
432 /// run - Execute all of the passes scheduled for execution. Keep track of
433 /// whether any of the passes modifies the module, and if so, return true.
436 /// Return true IFF AnalysisID AID is currently available.
437 /// TODO : Replace this method with getAnalysisPass()
438 Pass *getAnalysisPassFromManager(AnalysisID AID);
440 /// Pass Manager itself does not invalidate any analysis info.
441 void getAnalysisUsage(AnalysisUsage &Info) const {
442 Info.setPreservesAll();
445 inline void addTopLevelPass(Pass *P) {
451 /// Add a pass into a passmanager queue.
452 bool addPass(Pass *p);
454 // Collection of pass managers
455 std::vector<ModulePassManager_New *> PassManagers;
457 // Active Pass Manager
458 ModulePassManager_New *activeManager;
461 } // End of llvm namespace
463 //===----------------------------------------------------------------------===//
464 // PMDataManager implementation
466 /// Return true IFF pass P's required analysis set does not required new
468 bool PMDataManager::manageablePass(Pass *P) {
471 // If this pass is not preserving information that is required by a
472 // pass maintained by higher level pass manager then do not insert
473 // this pass into current manager. Use new manager. For example,
474 // For example, If FunctionPass F is not preserving ModulePass Info M1
475 // that is used by another ModulePass M2 then do not insert F in
476 // current function pass manager.
480 /// Augement AvailableAnalysis by adding analysis made available by pass P.
481 void PMDataManager::recordAvailableAnalysis(Pass *P) {
483 if (const PassInfo *PI = P->getPassInfo()) {
484 AvailableAnalysis[PI] = P;
486 //This pass is the current implementation of all of the interfaces it
487 //implements as well.
488 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
489 for (unsigned i = 0, e = II.size(); i != e; ++i)
490 AvailableAnalysis[II[i]] = P;
494 /// Remove Analyss not preserved by Pass P
495 void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
496 AnalysisUsage AnUsage;
497 P->getAnalysisUsage(AnUsage);
499 if (AnUsage.getPreservesAll())
502 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
503 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
504 E = AvailableAnalysis.end(); I != E; ++I ) {
505 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
506 PreservedSet.end()) {
507 // Remove this analysis
508 std::map<AnalysisID, Pass*>::iterator J = I++;
509 AvailableAnalysis.erase(J);
514 /// Remove analysis passes that are not used any longer
515 void PMDataManager::removeDeadPasses(Pass *P) {
516 // TODO : reimplement
519 /// Add pass P into the PassVector. Update
520 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
521 void PMDataManager::addPassToManager(Pass *P,
522 bool ProcessAnalysis) {
524 if (ProcessAnalysis) {
525 // Take a note of analysis required and made available by this pass.
526 // Remove the analysis not preserved by this pass
527 initializeAnalysisImpl(P);
528 removeNotPreservedAnalysis(P);
529 recordAvailableAnalysis(P);
533 PassVector.push_back(P);
536 /// Populate RequiredPasses with the analysis pass that are required by
538 void PMDataManager::collectRequiredAnalysisPasses(std::vector<Pass *> &RP,
540 AnalysisUsage AnUsage;
541 P->getAnalysisUsage(AnUsage);
542 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
543 for (std::vector<AnalysisID>::const_iterator
544 I = RequiredSet.begin(), E = RequiredSet.end();
546 Pass *AnalysisPass = NULL; //FIXME findAnalysisPass(*I,true);
547 assert (AnalysisPass && "Analysis pass is not available");
548 RP.push_back(AnalysisPass);
552 // All Required analyses should be available to the pass as it runs! Here
553 // we fill in the AnalysisImpls member of the pass so that it can
554 // successfully use the getAnalysis() method to retrieve the
555 // implementations it needs.
557 void PMDataManager::initializeAnalysisImpl(Pass *P) {
558 AnalysisUsage AnUsage;
559 P->getAnalysisUsage(AnUsage);
561 for (std::vector<const PassInfo *>::const_iterator
562 I = AnUsage.getRequiredSet().begin(),
563 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
564 Pass *Impl = getAnalysisPass(*I);
566 assert(0 && "Analysis used but not available!");
567 // TODO: P->AnalysisImpls.push_back(std::make_pair(*I, Impl));
571 //===----------------------------------------------------------------------===//
572 // BasicBlockPassManager_New implementation
574 /// Add pass P into PassVector and return true. If this pass is not
575 /// manageable by this manager then return false.
577 BasicBlockPassManager_New::addPass(Pass *P) {
579 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
583 // If this pass does not preserve anlysis that is used by other passes
584 // managed by this manager than it is not a suiable pass for this manager.
585 if (!manageablePass(P))
588 addPassToManager (BP);
593 /// Execute all of the passes scheduled for execution by invoking
594 /// runOnBasicBlock method. Keep track of whether any of the passes modifies
595 /// the function, and if so, return true.
597 BasicBlockPassManager_New::runOnFunction(Function &F) {
599 bool Changed = false;
600 initializeAnalysisInfo();
602 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
603 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
604 e = passVectorEnd(); itr != e; ++itr) {
607 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
608 Changed |= BP->runOnBasicBlock(*I);
609 removeNotPreservedAnalysis(P);
610 recordAvailableAnalysis(P);
616 /// Return true IFF AnalysisID AID is currently available.
617 /// TODO : Replace this method with getAnalysisPass()
618 Pass * BasicBlockPassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
619 return getAnalysisPass(AID);
622 //===----------------------------------------------------------------------===//
623 // FunctionPassManager_New implementation
625 /// Create new Function pass manager
626 FunctionPassManager_New::FunctionPassManager_New() {
627 FPM = new FunctionPassManagerImpl_New(0);
630 /// add - Add a pass to the queue of passes to run. This passes
631 /// ownership of the Pass to the PassManager. When the
632 /// PassManager_X is destroyed, the pass will be destroyed as well, so
633 /// there is no need to delete the pass. (TODO delete passes.)
634 /// This implies that all passes MUST be allocated with 'new'.
635 void FunctionPassManager_New::add(Pass *P) {
639 /// Execute all of the passes scheduled for execution. Keep
640 /// track of whether any of the passes modifies the function, and if
642 bool FunctionPassManager_New::runOnModule(Module &M) {
643 return FPM->runOnModule(M);
646 /// run - Execute all of the passes scheduled for execution. Keep
647 /// track of whether any of the passes modifies the function, and if
650 bool FunctionPassManager_New::run(Function &F) {
652 if (MP->materializeFunction(&F, &errstr)) {
653 cerr << "Error reading bytecode file: " << errstr << "\n";
656 return FPM->runOnFunction(F);
660 /// doInitialization - Run all of the initializers for the function passes.
662 bool FunctionPassManager_New::doInitialization() {
663 return FPM->doInitialization(*MP->getModule());
666 /// doFinalization - Run all of the initializers for the function passes.
668 bool FunctionPassManager_New::doFinalization() {
669 return FPM->doFinalization(*MP->getModule());
672 //===----------------------------------------------------------------------===//
673 // FunctionPassManagerImpl_New implementation
675 /// Add pass P into the pass manager queue. If P is a BasicBlockPass then
676 /// either use it into active basic block pass manager or create new basic
677 /// block pass manager to handle pass P.
679 FunctionPassManagerImpl_New::addPass(Pass *P) {
681 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
682 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
684 if (!activeBBPassManager || !activeBBPassManager->addPass(BP)) {
686 // If active manager exists then clear its analysis info.
687 if (activeBBPassManager)
688 activeBBPassManager->initializeAnalysisInfo();
690 // Create and add new manager
691 activeBBPassManager =
692 new BasicBlockPassManager_New(getDepth() + 1);
693 addPassToManager(activeBBPassManager, false);
695 // Add pass into new manager. This time it must succeed.
696 if (!activeBBPassManager->addPass(BP))
697 assert(0 && "Unable to add Pass");
702 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
706 // If this pass does not preserve anlysis that is used by other passes
707 // managed by this manager than it is not a suiable pass for this manager.
708 if (!manageablePass(P))
711 addPassToManager (FP);
713 // If active manager exists then clear its analysis info.
714 if (activeBBPassManager) {
715 activeBBPassManager->initializeAnalysisInfo();
716 activeBBPassManager = NULL;
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::runOnModule(Module &M) {
727 bool Changed = false;
728 initializeAnalysisInfo();
730 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
731 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
732 e = passVectorEnd(); itr != e; ++itr) {
735 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
736 Changed |= FP->runOnFunction(*I);
737 removeNotPreservedAnalysis(P);
738 recordAvailableAnalysis(P);
744 /// Execute all of the passes scheduled for execution by invoking
745 /// runOnFunction method. Keep track of whether any of the passes modifies
746 /// the function, and if so, return true.
747 bool FunctionPassManagerImpl_New::runOnFunction(Function &F) {
749 bool Changed = false;
750 initializeAnalysisInfo();
752 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
753 e = passVectorEnd(); itr != e; ++itr) {
756 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
757 Changed |= FP->runOnFunction(F);
758 removeNotPreservedAnalysis(P);
759 recordAvailableAnalysis(P);
766 /// Return true IFF AnalysisID AID is currently available.
767 /// TODO : Replace this method with getAnalysisPass()
768 Pass *FunctionPassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
770 Pass *P = getAnalysisPass(AID);
774 if (activeBBPassManager &&
775 activeBBPassManager->getAnalysisPass(AID) != 0)
776 return activeBBPassManager->getAnalysisPass(AID);
778 // TODO : Check inactive managers
782 inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
783 bool Changed = false;
785 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
786 e = passVectorEnd(); itr != e; ++itr) {
789 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
790 Changed |= FP->doInitialization(M);
796 inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
797 bool Changed = false;
799 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
800 e = passVectorEnd(); itr != e; ++itr) {
803 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
804 Changed |= FP->doFinalization(M);
811 //===----------------------------------------------------------------------===//
812 // ModulePassManager implementation
814 /// Add P into pass vector if it is manageble. If P is a FunctionPass
815 /// then use FunctionPassManagerImpl_New to manage it. Return false if P
816 /// is not manageable by this manager.
818 ModulePassManager_New::addPass(Pass *P) {
820 // If P is FunctionPass then use function pass maanager.
821 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
823 if (!activeFunctionPassManager
824 || !activeFunctionPassManager->addPass(P)) {
826 // If active manager exists then clear its analysis info.
827 if (activeFunctionPassManager)
828 activeFunctionPassManager->initializeAnalysisInfo();
830 // Create and add new manager
831 activeFunctionPassManager =
832 new FunctionPassManagerImpl_New(getDepth() + 1);
833 addPassToManager(activeFunctionPassManager, false);
835 // Add pass into new manager. This time it must succeed.
836 if (!activeFunctionPassManager->addPass(FP))
837 assert(0 && "Unable to add pass");
842 ModulePass *MP = dynamic_cast<ModulePass *>(P);
846 // If this pass does not preserve anlysis that is used by other passes
847 // managed by this manager than it is not a suiable pass for this manager.
848 if (!manageablePass(P))
851 addPassToManager(MP);
852 // If active manager exists then clear its analysis info.
853 if (activeFunctionPassManager) {
854 activeFunctionPassManager->initializeAnalysisInfo();
855 activeFunctionPassManager = NULL;
862 /// Execute all of the passes scheduled for execution by invoking
863 /// runOnModule method. Keep track of whether any of the passes modifies
864 /// the module, and if so, return true.
866 ModulePassManager_New::runOnModule(Module &M) {
867 bool Changed = false;
868 initializeAnalysisInfo();
870 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
871 e = passVectorEnd(); itr != e; ++itr) {
874 ModulePass *MP = dynamic_cast<ModulePass*>(P);
875 Changed |= MP->runOnModule(M);
876 removeNotPreservedAnalysis(P);
877 recordAvailableAnalysis(P);
883 /// Return true IFF AnalysisID AID is currently available.
884 /// TODO : Replace this method with getAnalysisPass()
885 Pass *ModulePassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
887 Pass *P = getAnalysisPass(AID);
891 if (activeFunctionPassManager &&
892 activeFunctionPassManager->getAnalysisPass(AID) != 0)
893 return activeFunctionPassManager->getAnalysisPass(AID);
895 // TODO : Check inactive managers
899 //===----------------------------------------------------------------------===//
900 // PassManagerImpl implementation
902 /// Return true IFF AnalysisID AID is currently available.
903 /// TODO : Replace this method with getAnalysisPass()
904 Pass *PassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
907 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
908 e = PassManagers.end(); !P && itr != e; ++itr)
909 P = (*itr)->getAnalysisPassFromManager(AID);
913 // PassManager_New implementation
914 /// Add P into active pass manager or use new module pass manager to
916 bool PassManagerImpl_New::addPass(Pass *P) {
918 if (!activeManager || !activeManager->addPass(P)) {
919 activeManager = new ModulePassManager_New(getDepth() + 1);
920 PassManagers.push_back(activeManager);
921 return activeManager->addPass(P);
926 /// run - Execute all of the passes scheduled for execution. Keep track of
927 /// whether any of the passes modifies the module, and if so, return true.
928 bool PassManagerImpl_New::run(Module &M) {
930 bool Changed = false;
931 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
932 e = PassManagers.end(); itr != e; ++itr) {
933 ModulePassManager_New *pm = *itr;
934 Changed |= pm->runOnModule(M);
939 //===----------------------------------------------------------------------===//
940 // PassManager implementation
942 /// Create new pass manager
943 PassManager_New::PassManager_New() {
944 PM = new PassManagerImpl_New(0);
947 /// add - Add a pass to the queue of passes to run. This passes ownership of
948 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
949 /// will be destroyed as well, so there is no need to delete the pass. This
950 /// implies that all passes MUST be allocated with 'new'.
952 PassManager_New::add(Pass *P) {
956 /// run - Execute all of the passes scheduled for execution. Keep track of
957 /// whether any of the passes modifies the module, and if so, return true.
959 PassManager_New::run(Module &M) {