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();
127 /// Collection of pass managers
128 std::vector<Pass *> PassManagers;
130 // Map to keep track of last user of the analysis pass.
131 // LastUser->second is the last user of Lastuser->first.
132 std::map<Pass *, Pass *> LastUser;
135 /// Set pass P as the last user of the given analysis passes.
136 void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses,
139 for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(),
140 E = AnalysisPasses.end(); I != E; ++I) {
143 // If AP is the last user of other passes then make P last user of
145 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
146 LUE = LastUser.end(); LUI != LUE; ++LUI) {
147 if (LUI->second == AP)
148 LastUser[LUI->first] = P;
154 /// Collect passes whose last user is P
155 void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses,
157 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
158 LUE = LastUser.end(); LUI != LUE; ++LUI)
159 if (LUI->second == P)
160 LastUses.push_back(LUI->first);
163 //===----------------------------------------------------------------------===//
166 /// PMDataManager provides the common place to manage the analysis data
167 /// used by pass managers.
168 class PMDataManager {
172 PMDataManager() : TPM(NULL) {
173 initializeAnalysisInfo();
176 /// Return true IFF pass P's required analysis set does not required new
178 bool manageablePass(Pass *P);
180 Pass *getAnalysisPass(AnalysisID AID) const {
182 std::map<AnalysisID, Pass*>::const_iterator I =
183 AvailableAnalysis.find(AID);
185 if (I != AvailableAnalysis.end())
191 /// Augment AvailableAnalysis by adding analysis made available by pass P.
192 void recordAvailableAnalysis(Pass *P);
194 /// Remove Analysis that is not preserved by the pass
195 void removeNotPreservedAnalysis(Pass *P);
197 /// Remove dead passes
198 void removeDeadPasses(Pass *P);
200 /// Add pass P into the PassVector. Update
201 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
202 void addPassToManager (Pass *P, bool ProcessAnalysis = true);
204 // Initialize available analysis information.
205 void initializeAnalysisInfo() {
206 AvailableAnalysis.clear();
210 // All Required analyses should be available to the pass as it runs! Here
211 // we fill in the AnalysisImpls member of the pass so that it can
212 // successfully use the getAnalysis() method to retrieve the
213 // implementations it needs.
215 void initializeAnalysisImpl(Pass *P);
217 inline std::vector<Pass *>::iterator passVectorBegin() {
218 return PassVector.begin();
221 inline std::vector<Pass *>::iterator passVectorEnd() {
222 return PassVector.end();
225 inline void setLastUser(Pass *P, Pass *LU) {
227 // TODO : Check if pass P is available.
230 // Access toplevel manager
231 PMTopLevelManager *getTopLevelManager() { return TPM; }
232 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
235 // Set of available Analysis. This information is used while scheduling
236 // pass. If a pass requires an analysis which is not not available then
237 // equired analysis pass is scheduled to run before the pass itself is
239 std::map<AnalysisID, Pass*> AvailableAnalysis;
241 // Map to keep track of last user of the analysis pass.
242 // LastUser->second is the last user of Lastuser->first.
243 std::map<Pass *, Pass *> LastUser;
245 // Collection of pass that are managed by this manager
246 std::vector<Pass *> PassVector;
248 // Top level manager.
249 // TODO : Make it a reference.
250 PMTopLevelManager *TPM;
253 /// BasicBlockPassManager_New manages BasicBlockPass. It batches all the
254 /// pass together and sequence them to process one basic block before
255 /// processing next basic block.
256 class BasicBlockPassManager_New : public PMDataManager,
257 public FunctionPass {
260 BasicBlockPassManager_New() { }
262 /// Add a pass into a passmanager queue.
263 bool addPass(Pass *p);
265 /// Execute all of the passes scheduled for execution. Keep track of
266 /// whether any of the passes modifies the function, and if so, return true.
267 bool runOnFunction(Function &F);
269 /// Return true IFF AnalysisID AID is currently available.
270 Pass *getAnalysisPassFromManager(AnalysisID AID);
275 /// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
276 /// It batches all function passes and basic block pass managers together and
277 /// sequence them to process one function at a time before processing next
279 class FunctionPassManagerImpl_New : public PMDataManager,
282 FunctionPassManagerImpl_New(ModuleProvider *P) { /* TODO */ }
283 FunctionPassManagerImpl_New() {
284 activeBBPassManager = NULL;
286 ~FunctionPassManagerImpl_New() { /* TODO */ };
288 /// add - Add a pass to the queue of passes to run. This passes
289 /// ownership of the Pass to the PassManager. When the
290 /// PassManager_X is destroyed, the pass will be destroyed as well, so
291 /// there is no need to delete the pass. (TODO delete passes.)
292 /// This implies that all passes MUST be allocated with 'new'.
293 void add(Pass *P) { /* TODO*/ }
295 /// Add pass into the pass manager queue.
296 bool addPass(Pass *P);
298 /// Execute all of the passes scheduled for execution. Keep
299 /// track of whether any of the passes modifies the function, and if
301 bool runOnModule(Module &M);
302 bool runOnFunction(Function &F);
304 /// Return true IFF AnalysisID AID is currently available.
305 Pass *getAnalysisPassFromManager(AnalysisID AID);
307 /// doInitialization - Run all of the initializers for the function passes.
309 bool doInitialization(Module &M);
311 /// doFinalization - Run all of the initializers for the function passes.
313 bool doFinalization(Module &M);
315 // Active Pass Managers
316 BasicBlockPassManager_New *activeBBPassManager;
319 /// ModulePassManager_New manages ModulePasses and function pass managers.
320 /// It batches all Module passes passes and function pass managers together and
321 /// sequence them to process one module.
322 class ModulePassManager_New : public PMDataManager {
325 ModulePassManager_New() { activeFunctionPassManager = NULL; }
327 /// Add a pass into a passmanager queue.
328 bool addPass(Pass *p);
330 /// run - Execute all of the passes scheduled for execution. Keep track of
331 /// whether any of the passes modifies the module, and if so, return true.
332 bool runOnModule(Module &M);
334 /// Return true IFF AnalysisID AID is currently available.
335 Pass *getAnalysisPassFromManager(AnalysisID AID);
338 // Active Pass Manager
339 FunctionPassManagerImpl_New *activeFunctionPassManager;
342 /// PassManager_New manages ModulePassManagers
343 class PassManagerImpl_New : public PMDataManager {
347 /// add - Add a pass to the queue of passes to run. This passes ownership of
348 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
349 /// will be destroyed as well, so there is no need to delete the pass. This
350 /// implies that all passes MUST be allocated with 'new'.
353 /// run - Execute all of the passes scheduled for execution. Keep track of
354 /// whether any of the passes modifies the module, and if so, return true.
357 /// Return true IFF AnalysisID AID is currently available.
358 Pass *getAnalysisPassFromManager(AnalysisID AID);
362 /// Add a pass into a passmanager queue. This is used by schedulePasses
363 bool addPass(Pass *p);
365 /// Schedule pass P for execution. Make sure that passes required by
366 /// P are run before P is run. Update analysis info maintained by
367 /// the manager. Remove dead passes. This is a recursive function.
368 void schedulePass(Pass *P);
370 /// Schedule all passes collected in pass queue using add(). Add all the
371 /// schedule passes into various manager's queue using addPass().
372 void schedulePasses();
374 // Collection of pass managers
375 std::vector<ModulePassManager_New *> PassManagers;
377 // Active Pass Manager
378 ModulePassManager_New *activeManager;
381 } // End of llvm namespace
383 //===----------------------------------------------------------------------===//
384 // PMDataManager implementation
386 /// Return true IFF pass P's required analysis set does not required new
388 bool PMDataManager::manageablePass(Pass *P) {
391 // If this pass is not preserving information that is required by a
392 // pass maintained by higher level pass manager then do not insert
393 // this pass into current manager. Use new manager. For example,
394 // For example, If FunctionPass F is not preserving ModulePass Info M1
395 // that is used by another ModulePass M2 then do not insert F in
396 // current function pass manager.
400 /// Augement AvailableAnalysis by adding analysis made available by pass P.
401 void PMDataManager::recordAvailableAnalysis(Pass *P) {
403 if (const PassInfo *PI = P->getPassInfo()) {
404 AvailableAnalysis[PI] = P;
406 //This pass is the current implementation of all of the interfaces it
407 //implements as well.
408 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
409 for (unsigned i = 0, e = II.size(); i != e; ++i)
410 AvailableAnalysis[II[i]] = P;
414 /// Remove Analyss not preserved by Pass P
415 void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
416 AnalysisUsage AnUsage;
417 P->getAnalysisUsage(AnUsage);
418 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
420 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
421 E = AvailableAnalysis.end(); I != E; ++I ) {
422 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
423 PreservedSet.end()) {
424 // Remove this analysis
425 std::map<AnalysisID, Pass*>::iterator J = I++;
426 AvailableAnalysis.erase(J);
431 /// Remove analysis passes that are not used any longer
432 void PMDataManager::removeDeadPasses(Pass *P) {
434 for (std::map<Pass *, Pass *>::iterator I = LastUser.begin(),
435 E = LastUser.end(); I !=E; ++I) {
436 if (I->second == P) {
437 Pass *deadPass = I->first;
438 deadPass->releaseMemory();
440 std::map<AnalysisID, Pass*>::iterator Pos =
441 AvailableAnalysis.find(deadPass->getPassInfo());
443 assert (Pos != AvailableAnalysis.end() &&
444 "Pass is not available");
445 AvailableAnalysis.erase(Pos);
450 /// Add pass P into the PassVector. Update
451 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
452 void PMDataManager::addPassToManager (Pass *P,
453 bool ProcessAnalysis) {
455 if (ProcessAnalysis) {
456 // Take a note of analysis required and made available by this pass
457 initializeAnalysisImpl(P);
458 recordAvailableAnalysis(P);
460 // Remove the analysis not preserved by this pass
461 removeNotPreservedAnalysis(P);
465 PassVector.push_back(P);
468 // All Required analyses should be available to the pass as it runs! Here
469 // we fill in the AnalysisImpls member of the pass so that it can
470 // successfully use the getAnalysis() method to retrieve the
471 // implementations it needs.
473 void PMDataManager::initializeAnalysisImpl(Pass *P) {
474 AnalysisUsage AnUsage;
475 P->getAnalysisUsage(AnUsage);
477 for (std::vector<const PassInfo *>::const_iterator
478 I = AnUsage.getRequiredSet().begin(),
479 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
480 Pass *Impl = getAnalysisPass(*I);
482 assert(0 && "Analysis used but not available!");
483 // TODO: P->AnalysisImpls.push_back(std::make_pair(*I, Impl));
487 //===----------------------------------------------------------------------===//
488 // BasicBlockPassManager_New implementation
490 /// Add pass P into PassVector and return true. If this pass is not
491 /// manageable by this manager then return false.
493 BasicBlockPassManager_New::addPass(Pass *P) {
495 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
499 // If this pass does not preserve anlysis that is used by other passes
500 // managed by this manager than it is not a suiable pass for this manager.
501 if (!manageablePass(P))
504 addPassToManager (BP);
509 /// Execute all of the passes scheduled for execution by invoking
510 /// runOnBasicBlock method. Keep track of whether any of the passes modifies
511 /// the function, and if so, return true.
513 BasicBlockPassManager_New::runOnFunction(Function &F) {
515 bool Changed = false;
516 initializeAnalysisInfo();
518 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
519 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
520 e = passVectorEnd(); itr != e; ++itr) {
523 recordAvailableAnalysis(P);
524 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
525 Changed |= BP->runOnBasicBlock(*I);
526 removeNotPreservedAnalysis(P);
532 /// Return true IFF AnalysisID AID is currently available.
533 Pass * BasicBlockPassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
534 return getAnalysisPass(AID);
537 //===----------------------------------------------------------------------===//
538 // FunctionPassManager_New implementation
540 /// Create new Function pass manager
541 FunctionPassManager_New::FunctionPassManager_New() {
542 FPM = new FunctionPassManagerImpl_New();
545 /// add - Add a pass to the queue of passes to run. This passes
546 /// ownership of the Pass to the PassManager. When the
547 /// PassManager_X is destroyed, the pass will be destroyed as well, so
548 /// there is no need to delete the pass. (TODO delete passes.)
549 /// This implies that all passes MUST be allocated with 'new'.
550 void FunctionPassManager_New::add(Pass *P) {
554 /// Execute all of the passes scheduled for execution. Keep
555 /// track of whether any of the passes modifies the function, and if
557 bool FunctionPassManager_New::runOnModule(Module &M) {
558 return FPM->runOnModule(M);
561 /// run - Execute all of the passes scheduled for execution. Keep
562 /// track of whether any of the passes modifies the function, and if
565 bool FunctionPassManager_New::run(Function &F) {
567 if (MP->materializeFunction(&F, &errstr)) {
568 cerr << "Error reading bytecode file: " << errstr << "\n";
571 return FPM->runOnFunction(F);
575 /// doInitialization - Run all of the initializers for the function passes.
577 bool FunctionPassManager_New::doInitialization() {
578 return FPM->doInitialization(*MP->getModule());
581 /// doFinalization - Run all of the initializers for the function passes.
583 bool FunctionPassManager_New::doFinalization() {
584 return FPM->doFinalization(*MP->getModule());
587 //===----------------------------------------------------------------------===//
588 // FunctionPassManagerImpl_New implementation
590 /// Add pass P into the pass manager queue. If P is a BasicBlockPass then
591 /// either use it into active basic block pass manager or create new basic
592 /// block pass manager to handle pass P.
594 FunctionPassManagerImpl_New::addPass(Pass *P) {
596 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
597 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
599 if (!activeBBPassManager
600 || !activeBBPassManager->addPass(BP)) {
602 activeBBPassManager = new BasicBlockPassManager_New();
603 addPassToManager(activeBBPassManager, false);
604 if (!activeBBPassManager->addPass(BP))
605 assert(0 && "Unable to add Pass");
610 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
614 // If this pass does not preserve anlysis that is used by other passes
615 // managed by this manager than it is not a suiable pass for this manager.
616 if (!manageablePass(P))
619 addPassToManager (FP);
620 activeBBPassManager = NULL;
624 /// Execute all of the passes scheduled for execution by invoking
625 /// runOnFunction method. Keep track of whether any of the passes modifies
626 /// the function, and if so, return true.
627 bool FunctionPassManagerImpl_New::runOnModule(Module &M) {
629 bool Changed = false;
630 initializeAnalysisInfo();
632 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
633 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
634 e = passVectorEnd(); itr != e; ++itr) {
637 recordAvailableAnalysis(P);
638 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
639 Changed |= FP->runOnFunction(*I);
640 removeNotPreservedAnalysis(P);
646 /// Execute all of the passes scheduled for execution by invoking
647 /// runOnFunction method. Keep track of whether any of the passes modifies
648 /// the function, and if so, return true.
649 bool FunctionPassManagerImpl_New::runOnFunction(Function &F) {
651 bool Changed = false;
652 initializeAnalysisInfo();
654 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
655 e = passVectorEnd(); itr != e; ++itr) {
658 recordAvailableAnalysis(P);
659 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
660 Changed |= FP->runOnFunction(F);
661 removeNotPreservedAnalysis(P);
668 /// Return true IFF AnalysisID AID is currently available.
669 Pass *FunctionPassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
671 Pass *P = getAnalysisPass(AID);
675 if (activeBBPassManager &&
676 activeBBPassManager->getAnalysisPass(AID) != 0)
677 return activeBBPassManager->getAnalysisPass(AID);
679 // TODO : Check inactive managers
683 inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
684 bool Changed = false;
686 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
687 e = passVectorEnd(); itr != e; ++itr) {
690 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
691 Changed |= FP->doInitialization(M);
697 inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
698 bool Changed = false;
700 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
701 e = passVectorEnd(); itr != e; ++itr) {
704 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
705 Changed |= FP->doFinalization(M);
712 //===----------------------------------------------------------------------===//
713 // ModulePassManager implementation
715 /// Add P into pass vector if it is manageble. If P is a FunctionPass
716 /// then use FunctionPassManagerImpl_New to manage it. Return false if P
717 /// is not manageable by this manager.
719 ModulePassManager_New::addPass(Pass *P) {
721 // If P is FunctionPass then use function pass maanager.
722 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
724 activeFunctionPassManager = NULL;
726 if (!activeFunctionPassManager
727 || !activeFunctionPassManager->addPass(P)) {
729 activeFunctionPassManager = new FunctionPassManagerImpl_New();
730 addPassToManager(activeFunctionPassManager, false);
731 if (!activeFunctionPassManager->addPass(FP))
732 assert(0 && "Unable to add pass");
737 ModulePass *MP = dynamic_cast<ModulePass *>(P);
741 // If this pass does not preserve anlysis that is used by other passes
742 // managed by this manager than it is not a suiable pass for this manager.
743 if (!manageablePass(P))
746 addPassToManager(MP);
747 activeFunctionPassManager = NULL;
752 /// Execute all of the passes scheduled for execution by invoking
753 /// runOnModule method. Keep track of whether any of the passes modifies
754 /// the module, and if so, return true.
756 ModulePassManager_New::runOnModule(Module &M) {
757 bool Changed = false;
758 initializeAnalysisInfo();
760 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
761 e = passVectorEnd(); itr != e; ++itr) {
764 recordAvailableAnalysis(P);
765 ModulePass *MP = dynamic_cast<ModulePass*>(P);
766 Changed |= MP->runOnModule(M);
767 removeNotPreservedAnalysis(P);
773 /// Return true IFF AnalysisID AID is currently available.
774 Pass *ModulePassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
777 Pass *P = getAnalysisPass(AID);
781 if (activeFunctionPassManager &&
782 activeFunctionPassManager->getAnalysisPass(AID) != 0)
783 return activeFunctionPassManager->getAnalysisPass(AID);
785 // TODO : Check inactive managers
789 //===----------------------------------------------------------------------===//
790 // PassManagerImpl implementation
792 /// Return true IFF AnalysisID AID is currently available.
793 Pass *PassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
796 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
797 e = PassManagers.end(); !P && itr != e; ++itr)
798 P = (*itr)->getAnalysisPassFromManager(AID);
802 /// Schedule pass P for execution. Make sure that passes required by
803 /// P are run before P is run. Update analysis info maintained by
804 /// the manager. Remove dead passes. This is a recursive function.
805 void PassManagerImpl_New::schedulePass(Pass *P) {
807 AnalysisUsage AnUsage;
808 P->getAnalysisUsage(AnUsage);
809 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
810 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
811 E = RequiredSet.end(); I != E; ++I) {
813 Pass *AnalysisPass = getAnalysisPassFromManager(*I);
815 // Schedule this analysis run first.
816 AnalysisPass = (*I)->createPass();
817 schedulePass(AnalysisPass);
819 setLastUser (AnalysisPass, P);
821 // Prolong live range of analyses that are needed after an analysis pass
822 // is destroyed, for querying by subsequent passes
823 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
824 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
825 E = IDs.end(); I != E; ++I) {
826 Pass *AP = getAnalysisPassFromManager(*I);
827 assert (AP && "Analysis pass is not available");
834 /// Schedule all passes from the queue by adding them in their
835 /// respective manager's queue.
836 void PassManagerImpl_New::schedulePasses() {
837 for (std::vector<Pass *>::iterator I = passVectorBegin(),
838 E = passVectorEnd(); I != E; ++I)
842 /// Add pass P to the queue of passes to run.
843 void PassManagerImpl_New::add(Pass *P) {
844 // Do not process Analysis now. Analysis is process while scheduling
846 addPassToManager(P, false);
849 // PassManager_New implementation
850 /// Add P into active pass manager or use new module pass manager to
852 bool PassManagerImpl_New::addPass(Pass *P) {
854 if (!activeManager || !activeManager->addPass(P)) {
855 activeManager = new ModulePassManager_New();
856 PassManagers.push_back(activeManager);
859 return activeManager->addPass(P);
862 /// run - Execute all of the passes scheduled for execution. Keep track of
863 /// whether any of the passes modifies the module, and if so, return true.
864 bool PassManagerImpl_New::run(Module &M) {
867 bool Changed = false;
868 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
869 e = PassManagers.end(); itr != e; ++itr) {
870 ModulePassManager_New *pm = *itr;
871 Changed |= pm->runOnModule(M);
876 //===----------------------------------------------------------------------===//
877 // PassManager implementation
879 /// Create new pass manager
880 PassManager_New::PassManager_New() {
881 PM = new PassManagerImpl_New();
884 /// add - Add a pass to the queue of passes to run. This passes ownership of
885 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
886 /// will be destroyed as well, so there is no need to delete the pass. This
887 /// implies that all passes MUST be allocated with 'new'.
889 PassManager_New::add(Pass *P) {
893 /// run - Execute all of the passes scheduled for execution. Keep track of
894 /// whether any of the passes modifies the module, and if so, return true.
896 PassManager_New::run(Module &M) {