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 //===----------------------------------------------------------------------===//
179 /// PMDataManager provides the common place to manage the analysis data
180 /// used by pass managers.
181 class PMDataManager {
185 PMDataManager() : TPM(NULL) {
186 initializeAnalysisInfo();
189 /// Return true IFF pass P's required analysis set does not required new
191 bool manageablePass(Pass *P);
193 Pass *getAnalysisPass(AnalysisID AID) const {
195 std::map<AnalysisID, Pass*>::const_iterator I =
196 AvailableAnalysis.find(AID);
198 if (I != AvailableAnalysis.end())
204 /// Augment AvailableAnalysis by adding analysis made available by pass P.
205 void recordAvailableAnalysis(Pass *P);
207 /// Remove Analysis that is not preserved by the pass
208 void removeNotPreservedAnalysis(Pass *P);
210 /// Remove dead passes
211 void removeDeadPasses(Pass *P);
213 /// Add pass P into the PassVector. Update
214 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
215 void addPassToManager (Pass *P, bool ProcessAnalysis = true);
217 // Initialize available analysis information.
218 void initializeAnalysisInfo() {
219 AvailableAnalysis.clear();
222 // Include immutable passes into AvailableAnalysis vector.
223 std::vector<ImmutablePass *> &ImmutablePasses = TPM->getImmutablePasses();
224 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
225 E = ImmutablePasses.end(); I != E; ++I)
226 recordAvailableAnalysis(*I);
229 // All Required analyses should be available to the pass as it runs! Here
230 // we fill in the AnalysisImpls member of the pass so that it can
231 // successfully use the getAnalysis() method to retrieve the
232 // implementations it needs.
234 void initializeAnalysisImpl(Pass *P);
236 inline std::vector<Pass *>::iterator passVectorBegin() {
237 return PassVector.begin();
240 inline std::vector<Pass *>::iterator passVectorEnd() {
241 return PassVector.end();
244 inline void setLastUser(Pass *P, Pass *LU) {
246 // TODO : Check if pass P is available.
249 // Access toplevel manager
250 PMTopLevelManager *getTopLevelManager() { return TPM; }
251 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
254 // Set of available Analysis. This information is used while scheduling
255 // pass. If a pass requires an analysis which is not not available then
256 // equired analysis pass is scheduled to run before the pass itself is
258 std::map<AnalysisID, Pass*> AvailableAnalysis;
260 // Map to keep track of last user of the analysis pass.
261 // LastUser->second is the last user of Lastuser->first.
262 std::map<Pass *, Pass *> LastUser;
264 // Collection of pass that are managed by this manager
265 std::vector<Pass *> PassVector;
267 // Top level manager.
268 // TODO : Make it a reference.
269 PMTopLevelManager *TPM;
272 /// BasicBlockPassManager_New manages BasicBlockPass. It batches all the
273 /// pass together and sequence them to process one basic block before
274 /// processing next basic block.
275 class BasicBlockPassManager_New : public PMDataManager,
276 public FunctionPass {
279 BasicBlockPassManager_New() { }
281 /// Add a pass into a passmanager queue.
282 bool addPass(Pass *p);
284 /// Execute all of the passes scheduled for execution. Keep track of
285 /// whether any of the passes modifies the function, and if so, return true.
286 bool runOnFunction(Function &F);
288 /// Return true IFF AnalysisID AID is currently available.
289 Pass *getAnalysisPassFromManager(AnalysisID AID);
291 /// Pass Manager itself does not invalidate any analysis info.
292 void getAnalysisUsage(AnalysisUsage &Info) const {
293 Info.setPreservesAll();
299 /// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
300 /// It batches all function passes and basic block pass managers together and
301 /// sequence them to process one function at a time before processing next
303 class FunctionPassManagerImpl_New : public PMDataManager,
306 FunctionPassManagerImpl_New(ModuleProvider *P) { /* TODO */ }
307 FunctionPassManagerImpl_New() {
308 activeBBPassManager = NULL;
310 ~FunctionPassManagerImpl_New() { /* TODO */ };
312 /// add - Add a pass to the queue of passes to run. This passes
313 /// ownership of the Pass to the PassManager. When the
314 /// PassManager_X is destroyed, the pass will be destroyed as well, so
315 /// there is no need to delete the pass. (TODO delete passes.)
316 /// This implies that all passes MUST be allocated with 'new'.
317 void add(Pass *P) { /* TODO*/ }
319 /// Add pass into the pass manager queue.
320 bool addPass(Pass *P);
322 /// Execute all of the passes scheduled for execution. Keep
323 /// track of whether any of the passes modifies the function, and if
325 bool runOnModule(Module &M);
326 bool runOnFunction(Function &F);
328 /// Return true IFF AnalysisID AID is currently available.
329 Pass *getAnalysisPassFromManager(AnalysisID AID);
331 /// doInitialization - Run all of the initializers for the function passes.
333 bool doInitialization(Module &M);
335 /// doFinalization - Run all of the initializers for the function passes.
337 bool doFinalization(Module &M);
339 /// Pass Manager itself does not invalidate any analysis info.
340 void getAnalysisUsage(AnalysisUsage &Info) const {
341 Info.setPreservesAll();
345 // Active Pass Managers
346 BasicBlockPassManager_New *activeBBPassManager;
349 /// ModulePassManager_New manages ModulePasses and function pass managers.
350 /// It batches all Module passes passes and function pass managers together and
351 /// sequence them to process one module.
352 class ModulePassManager_New : public PMDataManager {
355 ModulePassManager_New() { activeFunctionPassManager = NULL; }
357 /// Add a pass into a passmanager queue.
358 bool addPass(Pass *p);
360 /// run - Execute all of the passes scheduled for execution. Keep track of
361 /// whether any of the passes modifies the module, and if so, return true.
362 bool runOnModule(Module &M);
364 /// Return true IFF AnalysisID AID is currently available.
365 Pass *getAnalysisPassFromManager(AnalysisID AID);
367 /// Pass Manager itself does not invalidate any analysis info.
368 void getAnalysisUsage(AnalysisUsage &Info) const {
369 Info.setPreservesAll();
373 // Active Pass Manager
374 FunctionPassManagerImpl_New *activeFunctionPassManager;
377 /// PassManager_New manages ModulePassManagers
378 class PassManagerImpl_New : public PMDataManager {
382 /// add - Add a pass to the queue of passes to run. This passes ownership of
383 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
384 /// will be destroyed as well, so there is no need to delete the pass. This
385 /// implies that all passes MUST be allocated with 'new'.
388 /// run - Execute all of the passes scheduled for execution. Keep track of
389 /// whether any of the passes modifies the module, and if so, return true.
392 /// Return true IFF AnalysisID AID is currently available.
393 Pass *getAnalysisPassFromManager(AnalysisID AID);
395 /// Pass Manager itself does not invalidate any analysis info.
396 void getAnalysisUsage(AnalysisUsage &Info) const {
397 Info.setPreservesAll();
402 /// Add a pass into a passmanager queue. This is used by schedulePasses
403 bool addPass(Pass *p);
405 /// Schedule pass P for execution. Make sure that passes required by
406 /// P are run before P is run. Update analysis info maintained by
407 /// the manager. Remove dead passes. This is a recursive function.
408 void schedulePass(Pass *P);
410 /// Schedule all passes collected in pass queue using add(). Add all the
411 /// schedule passes into various manager's queue using addPass().
412 void schedulePasses();
414 // Collection of pass managers
415 std::vector<ModulePassManager_New *> PassManagers;
417 // Active Pass Manager
418 ModulePassManager_New *activeManager;
421 } // End of llvm namespace
423 //===----------------------------------------------------------------------===//
424 // PMDataManager implementation
426 /// Return true IFF pass P's required analysis set does not required new
428 bool PMDataManager::manageablePass(Pass *P) {
431 // If this pass is not preserving information that is required by a
432 // pass maintained by higher level pass manager then do not insert
433 // this pass into current manager. Use new manager. For example,
434 // For example, If FunctionPass F is not preserving ModulePass Info M1
435 // that is used by another ModulePass M2 then do not insert F in
436 // current function pass manager.
440 /// Augement AvailableAnalysis by adding analysis made available by pass P.
441 void PMDataManager::recordAvailableAnalysis(Pass *P) {
443 if (const PassInfo *PI = P->getPassInfo()) {
444 AvailableAnalysis[PI] = P;
446 //This pass is the current implementation of all of the interfaces it
447 //implements as well.
448 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
449 for (unsigned i = 0, e = II.size(); i != e; ++i)
450 AvailableAnalysis[II[i]] = P;
454 /// Remove Analyss not preserved by Pass P
455 void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
456 AnalysisUsage AnUsage;
457 P->getAnalysisUsage(AnUsage);
459 if (AnUsage.getPreservesAll())
462 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
463 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
464 E = AvailableAnalysis.end(); I != E; ++I ) {
465 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
466 PreservedSet.end()) {
467 // Remove this analysis
468 std::map<AnalysisID, Pass*>::iterator J = I++;
469 AvailableAnalysis.erase(J);
474 /// Remove analysis passes that are not used any longer
475 void PMDataManager::removeDeadPasses(Pass *P) {
477 for (std::map<Pass *, Pass *>::iterator I = LastUser.begin(),
478 E = LastUser.end(); I !=E; ++I) {
479 if (I->second == P) {
480 Pass *deadPass = I->first;
481 deadPass->releaseMemory();
483 std::map<AnalysisID, Pass*>::iterator Pos =
484 AvailableAnalysis.find(deadPass->getPassInfo());
486 assert (Pos != AvailableAnalysis.end() &&
487 "Pass is not available");
488 AvailableAnalysis.erase(Pos);
493 /// Add pass P into the PassVector. Update
494 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
495 void PMDataManager::addPassToManager(Pass *P,
496 bool ProcessAnalysis) {
498 if (ProcessAnalysis) {
499 // Take a note of analysis required and made available by this pass
500 initializeAnalysisImpl(P);
501 recordAvailableAnalysis(P);
503 // Remove the analysis not preserved by this pass
504 removeNotPreservedAnalysis(P);
508 PassVector.push_back(P);
511 // All Required analyses should be available to the pass as it runs! Here
512 // we fill in the AnalysisImpls member of the pass so that it can
513 // successfully use the getAnalysis() method to retrieve the
514 // implementations it needs.
516 void PMDataManager::initializeAnalysisImpl(Pass *P) {
517 AnalysisUsage AnUsage;
518 P->getAnalysisUsage(AnUsage);
520 for (std::vector<const PassInfo *>::const_iterator
521 I = AnUsage.getRequiredSet().begin(),
522 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
523 Pass *Impl = getAnalysisPass(*I);
525 assert(0 && "Analysis used but not available!");
526 // TODO: P->AnalysisImpls.push_back(std::make_pair(*I, Impl));
530 //===----------------------------------------------------------------------===//
531 // BasicBlockPassManager_New implementation
533 /// Add pass P into PassVector and return true. If this pass is not
534 /// manageable by this manager then return false.
536 BasicBlockPassManager_New::addPass(Pass *P) {
538 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
542 // If this pass does not preserve anlysis that is used by other passes
543 // managed by this manager than it is not a suiable pass for this manager.
544 if (!manageablePass(P))
547 addPassToManager (BP);
552 /// Execute all of the passes scheduled for execution by invoking
553 /// runOnBasicBlock method. Keep track of whether any of the passes modifies
554 /// the function, and if so, return true.
556 BasicBlockPassManager_New::runOnFunction(Function &F) {
558 bool Changed = false;
559 initializeAnalysisInfo();
561 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
562 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
563 e = passVectorEnd(); itr != e; ++itr) {
566 recordAvailableAnalysis(P);
567 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
568 Changed |= BP->runOnBasicBlock(*I);
569 removeNotPreservedAnalysis(P);
575 /// Return true IFF AnalysisID AID is currently available.
576 Pass * BasicBlockPassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
577 return getAnalysisPass(AID);
580 //===----------------------------------------------------------------------===//
581 // FunctionPassManager_New implementation
583 /// Create new Function pass manager
584 FunctionPassManager_New::FunctionPassManager_New() {
585 FPM = new FunctionPassManagerImpl_New();
588 /// add - Add a pass to the queue of passes to run. This passes
589 /// ownership of the Pass to the PassManager. When the
590 /// PassManager_X is destroyed, the pass will be destroyed as well, so
591 /// there is no need to delete the pass. (TODO delete passes.)
592 /// This implies that all passes MUST be allocated with 'new'.
593 void FunctionPassManager_New::add(Pass *P) {
597 /// Execute all of the passes scheduled for execution. Keep
598 /// track of whether any of the passes modifies the function, and if
600 bool FunctionPassManager_New::runOnModule(Module &M) {
601 return FPM->runOnModule(M);
604 /// run - Execute all of the passes scheduled for execution. Keep
605 /// track of whether any of the passes modifies the function, and if
608 bool FunctionPassManager_New::run(Function &F) {
610 if (MP->materializeFunction(&F, &errstr)) {
611 cerr << "Error reading bytecode file: " << errstr << "\n";
614 return FPM->runOnFunction(F);
618 /// doInitialization - Run all of the initializers for the function passes.
620 bool FunctionPassManager_New::doInitialization() {
621 return FPM->doInitialization(*MP->getModule());
624 /// doFinalization - Run all of the initializers for the function passes.
626 bool FunctionPassManager_New::doFinalization() {
627 return FPM->doFinalization(*MP->getModule());
630 //===----------------------------------------------------------------------===//
631 // FunctionPassManagerImpl_New implementation
633 /// Add pass P into the pass manager queue. If P is a BasicBlockPass then
634 /// either use it into active basic block pass manager or create new basic
635 /// block pass manager to handle pass P.
637 FunctionPassManagerImpl_New::addPass(Pass *P) {
639 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
640 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
642 if (!activeBBPassManager
643 || !activeBBPassManager->addPass(BP)) {
645 activeBBPassManager = new BasicBlockPassManager_New();
646 addPassToManager(activeBBPassManager, false);
647 if (!activeBBPassManager->addPass(BP))
648 assert(0 && "Unable to add Pass");
653 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
657 // If this pass does not preserve anlysis that is used by other passes
658 // managed by this manager than it is not a suiable pass for this manager.
659 if (!manageablePass(P))
662 addPassToManager (FP);
663 activeBBPassManager = NULL;
667 /// Execute all of the passes scheduled for execution by invoking
668 /// runOnFunction method. Keep track of whether any of the passes modifies
669 /// the function, and if so, return true.
670 bool FunctionPassManagerImpl_New::runOnModule(Module &M) {
672 bool Changed = false;
673 initializeAnalysisInfo();
675 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
676 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
677 e = passVectorEnd(); itr != e; ++itr) {
680 recordAvailableAnalysis(P);
681 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
682 Changed |= FP->runOnFunction(*I);
683 removeNotPreservedAnalysis(P);
689 /// Execute all of the passes scheduled for execution by invoking
690 /// runOnFunction method. Keep track of whether any of the passes modifies
691 /// the function, and if so, return true.
692 bool FunctionPassManagerImpl_New::runOnFunction(Function &F) {
694 bool Changed = false;
695 initializeAnalysisInfo();
697 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
698 e = passVectorEnd(); itr != e; ++itr) {
701 recordAvailableAnalysis(P);
702 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
703 Changed |= FP->runOnFunction(F);
704 removeNotPreservedAnalysis(P);
711 /// Return true IFF AnalysisID AID is currently available.
712 Pass *FunctionPassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
714 Pass *P = getAnalysisPass(AID);
718 if (activeBBPassManager &&
719 activeBBPassManager->getAnalysisPass(AID) != 0)
720 return activeBBPassManager->getAnalysisPass(AID);
722 // TODO : Check inactive managers
726 inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
727 bool Changed = false;
729 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
730 e = passVectorEnd(); itr != e; ++itr) {
733 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
734 Changed |= FP->doInitialization(M);
740 inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
741 bool Changed = false;
743 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
744 e = passVectorEnd(); itr != e; ++itr) {
747 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
748 Changed |= FP->doFinalization(M);
755 //===----------------------------------------------------------------------===//
756 // ModulePassManager implementation
758 /// Add P into pass vector if it is manageble. If P is a FunctionPass
759 /// then use FunctionPassManagerImpl_New to manage it. Return false if P
760 /// is not manageable by this manager.
762 ModulePassManager_New::addPass(Pass *P) {
764 // If P is FunctionPass then use function pass maanager.
765 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
767 activeFunctionPassManager = NULL;
769 if (!activeFunctionPassManager
770 || !activeFunctionPassManager->addPass(P)) {
772 activeFunctionPassManager = new FunctionPassManagerImpl_New();
773 addPassToManager(activeFunctionPassManager, false);
774 if (!activeFunctionPassManager->addPass(FP))
775 assert(0 && "Unable to add pass");
780 ModulePass *MP = dynamic_cast<ModulePass *>(P);
784 // If this pass does not preserve anlysis that is used by other passes
785 // managed by this manager than it is not a suiable pass for this manager.
786 if (!manageablePass(P))
789 addPassToManager(MP);
790 activeFunctionPassManager = NULL;
795 /// Execute all of the passes scheduled for execution by invoking
796 /// runOnModule method. Keep track of whether any of the passes modifies
797 /// the module, and if so, return true.
799 ModulePassManager_New::runOnModule(Module &M) {
800 bool Changed = false;
801 initializeAnalysisInfo();
803 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
804 e = passVectorEnd(); itr != e; ++itr) {
807 recordAvailableAnalysis(P);
808 ModulePass *MP = dynamic_cast<ModulePass*>(P);
809 Changed |= MP->runOnModule(M);
810 removeNotPreservedAnalysis(P);
816 /// Return true IFF AnalysisID AID is currently available.
817 Pass *ModulePassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
820 Pass *P = getAnalysisPass(AID);
824 if (activeFunctionPassManager &&
825 activeFunctionPassManager->getAnalysisPass(AID) != 0)
826 return activeFunctionPassManager->getAnalysisPass(AID);
828 // TODO : Check inactive managers
832 //===----------------------------------------------------------------------===//
833 // PassManagerImpl implementation
835 /// Return true IFF AnalysisID AID is currently available.
836 Pass *PassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
839 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
840 e = PassManagers.end(); !P && itr != e; ++itr)
841 P = (*itr)->getAnalysisPassFromManager(AID);
845 /// Schedule pass P for execution. Make sure that passes required by
846 /// P are run before P is run. Update analysis info maintained by
847 /// the manager. Remove dead passes. This is a recursive function.
848 void PassManagerImpl_New::schedulePass(Pass *P) {
850 AnalysisUsage AnUsage;
851 P->getAnalysisUsage(AnUsage);
852 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
853 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
854 E = RequiredSet.end(); I != E; ++I) {
856 Pass *AnalysisPass = getAnalysisPassFromManager(*I);
858 // Schedule this analysis run first.
859 AnalysisPass = (*I)->createPass();
860 schedulePass(AnalysisPass);
862 setLastUser (AnalysisPass, P);
864 // Prolong live range of analyses that are needed after an analysis pass
865 // is destroyed, for querying by subsequent passes
866 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
867 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
868 E = IDs.end(); I != E; ++I) {
869 Pass *AP = getAnalysisPassFromManager(*I);
870 assert (AP && "Analysis pass is not available");
877 /// Schedule all passes from the queue by adding them in their
878 /// respective manager's queue.
879 void PassManagerImpl_New::schedulePasses() {
880 for (std::vector<Pass *>::iterator I = passVectorBegin(),
881 E = passVectorEnd(); I != E; ++I)
885 /// Add pass P to the queue of passes to run.
886 void PassManagerImpl_New::add(Pass *P) {
887 // Do not process Analysis now. Analysis is process while scheduling
889 addPassToManager(P, false);
892 // PassManager_New implementation
893 /// Add P into active pass manager or use new module pass manager to
895 bool PassManagerImpl_New::addPass(Pass *P) {
897 if (!activeManager || !activeManager->addPass(P)) {
898 activeManager = new ModulePassManager_New();
899 PassManagers.push_back(activeManager);
902 return activeManager->addPass(P);
905 /// run - Execute all of the passes scheduled for execution. Keep track of
906 /// whether any of the passes modifies the module, and if so, return true.
907 bool PassManagerImpl_New::run(Module &M) {
910 bool Changed = false;
911 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
912 e = PassManagers.end(); itr != e; ++itr) {
913 ModulePassManager_New *pm = *itr;
914 Changed |= pm->runOnModule(M);
919 //===----------------------------------------------------------------------===//
920 // PassManager implementation
922 /// Create new pass manager
923 PassManager_New::PassManager_New() {
924 PM = new PassManagerImpl_New();
927 /// add - Add a pass to the queue of passes to run. This passes ownership of
928 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
929 /// will be destroyed as well, so there is no need to delete the pass. This
930 /// implies that all passes MUST be allocated with 'new'.
932 PassManager_New::add(Pass *P) {
936 /// run - Execute all of the passes scheduled for execution. Keep track of
937 /// whether any of the passes modifies the module, and if so, return true.
939 PassManager_New::run(Module &M) {