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);
272 /// Pass Manager itself does not invalidate any analysis info.
273 void getAnalysisUsage(AnalysisUsage &Info) const {
274 Info.setPreservesAll();
280 /// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
281 /// It batches all function passes and basic block pass managers together and
282 /// sequence them to process one function at a time before processing next
284 class FunctionPassManagerImpl_New : public PMDataManager,
287 FunctionPassManagerImpl_New(ModuleProvider *P) { /* TODO */ }
288 FunctionPassManagerImpl_New() {
289 activeBBPassManager = NULL;
291 ~FunctionPassManagerImpl_New() { /* TODO */ };
293 /// add - Add a pass to the queue of passes to run. This passes
294 /// ownership of the Pass to the PassManager. When the
295 /// PassManager_X is destroyed, the pass will be destroyed as well, so
296 /// there is no need to delete the pass. (TODO delete passes.)
297 /// This implies that all passes MUST be allocated with 'new'.
298 void add(Pass *P) { /* TODO*/ }
300 /// Add pass into the pass manager queue.
301 bool addPass(Pass *P);
303 /// Execute all of the passes scheduled for execution. Keep
304 /// track of whether any of the passes modifies the function, and if
306 bool runOnModule(Module &M);
307 bool runOnFunction(Function &F);
309 /// Return true IFF AnalysisID AID is currently available.
310 Pass *getAnalysisPassFromManager(AnalysisID AID);
312 /// doInitialization - Run all of the initializers for the function passes.
314 bool doInitialization(Module &M);
316 /// doFinalization - Run all of the initializers for the function passes.
318 bool doFinalization(Module &M);
320 /// Pass Manager itself does not invalidate any analysis info.
321 void getAnalysisUsage(AnalysisUsage &Info) const {
322 Info.setPreservesAll();
326 // Active Pass Managers
327 BasicBlockPassManager_New *activeBBPassManager;
330 /// ModulePassManager_New manages ModulePasses and function pass managers.
331 /// It batches all Module passes passes and function pass managers together and
332 /// sequence them to process one module.
333 class ModulePassManager_New : public PMDataManager {
336 ModulePassManager_New() { activeFunctionPassManager = NULL; }
338 /// Add a pass into a passmanager queue.
339 bool addPass(Pass *p);
341 /// run - Execute all of the passes scheduled for execution. Keep track of
342 /// whether any of the passes modifies the module, and if so, return true.
343 bool runOnModule(Module &M);
345 /// Return true IFF AnalysisID AID is currently available.
346 Pass *getAnalysisPassFromManager(AnalysisID AID);
348 /// Pass Manager itself does not invalidate any analysis info.
349 void getAnalysisUsage(AnalysisUsage &Info) const {
350 Info.setPreservesAll();
354 // Active Pass Manager
355 FunctionPassManagerImpl_New *activeFunctionPassManager;
358 /// PassManager_New manages ModulePassManagers
359 class PassManagerImpl_New : public PMDataManager {
363 /// add - Add a pass to the queue of passes to run. This passes ownership of
364 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
365 /// will be destroyed as well, so there is no need to delete the pass. This
366 /// implies that all passes MUST be allocated with 'new'.
369 /// run - Execute all of the passes scheduled for execution. Keep track of
370 /// whether any of the passes modifies the module, and if so, return true.
373 /// Return true IFF AnalysisID AID is currently available.
374 Pass *getAnalysisPassFromManager(AnalysisID AID);
376 /// Pass Manager itself does not invalidate any analysis info.
377 void getAnalysisUsage(AnalysisUsage &Info) const {
378 Info.setPreservesAll();
383 /// Add a pass into a passmanager queue. This is used by schedulePasses
384 bool addPass(Pass *p);
386 /// Schedule pass P for execution. Make sure that passes required by
387 /// P are run before P is run. Update analysis info maintained by
388 /// the manager. Remove dead passes. This is a recursive function.
389 void schedulePass(Pass *P);
391 /// Schedule all passes collected in pass queue using add(). Add all the
392 /// schedule passes into various manager's queue using addPass().
393 void schedulePasses();
395 // Collection of pass managers
396 std::vector<ModulePassManager_New *> PassManagers;
398 // Active Pass Manager
399 ModulePassManager_New *activeManager;
402 } // End of llvm namespace
404 //===----------------------------------------------------------------------===//
405 // PMDataManager implementation
407 /// Return true IFF pass P's required analysis set does not required new
409 bool PMDataManager::manageablePass(Pass *P) {
412 // If this pass is not preserving information that is required by a
413 // pass maintained by higher level pass manager then do not insert
414 // this pass into current manager. Use new manager. For example,
415 // For example, If FunctionPass F is not preserving ModulePass Info M1
416 // that is used by another ModulePass M2 then do not insert F in
417 // current function pass manager.
421 /// Augement AvailableAnalysis by adding analysis made available by pass P.
422 void PMDataManager::recordAvailableAnalysis(Pass *P) {
424 if (const PassInfo *PI = P->getPassInfo()) {
425 AvailableAnalysis[PI] = P;
427 //This pass is the current implementation of all of the interfaces it
428 //implements as well.
429 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
430 for (unsigned i = 0, e = II.size(); i != e; ++i)
431 AvailableAnalysis[II[i]] = P;
435 /// Remove Analyss not preserved by Pass P
436 void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
437 AnalysisUsage AnUsage;
438 P->getAnalysisUsage(AnUsage);
440 if (AnUsage.getPreservesAll())
443 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
444 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
445 E = AvailableAnalysis.end(); I != E; ++I ) {
446 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
447 PreservedSet.end()) {
448 // Remove this analysis
449 std::map<AnalysisID, Pass*>::iterator J = I++;
450 AvailableAnalysis.erase(J);
455 /// Remove analysis passes that are not used any longer
456 void PMDataManager::removeDeadPasses(Pass *P) {
458 for (std::map<Pass *, Pass *>::iterator I = LastUser.begin(),
459 E = LastUser.end(); I !=E; ++I) {
460 if (I->second == P) {
461 Pass *deadPass = I->first;
462 deadPass->releaseMemory();
464 std::map<AnalysisID, Pass*>::iterator Pos =
465 AvailableAnalysis.find(deadPass->getPassInfo());
467 assert (Pos != AvailableAnalysis.end() &&
468 "Pass is not available");
469 AvailableAnalysis.erase(Pos);
474 /// Add pass P into the PassVector. Update
475 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
476 void PMDataManager::addPassToManager(Pass *P,
477 bool ProcessAnalysis) {
479 if (ProcessAnalysis) {
480 // Take a note of analysis required and made available by this pass
481 initializeAnalysisImpl(P);
482 recordAvailableAnalysis(P);
484 // Remove the analysis not preserved by this pass
485 removeNotPreservedAnalysis(P);
489 PassVector.push_back(P);
492 // All Required analyses should be available to the pass as it runs! Here
493 // we fill in the AnalysisImpls member of the pass so that it can
494 // successfully use the getAnalysis() method to retrieve the
495 // implementations it needs.
497 void PMDataManager::initializeAnalysisImpl(Pass *P) {
498 AnalysisUsage AnUsage;
499 P->getAnalysisUsage(AnUsage);
501 for (std::vector<const PassInfo *>::const_iterator
502 I = AnUsage.getRequiredSet().begin(),
503 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
504 Pass *Impl = getAnalysisPass(*I);
506 assert(0 && "Analysis used but not available!");
507 // TODO: P->AnalysisImpls.push_back(std::make_pair(*I, Impl));
511 //===----------------------------------------------------------------------===//
512 // BasicBlockPassManager_New implementation
514 /// Add pass P into PassVector and return true. If this pass is not
515 /// manageable by this manager then return false.
517 BasicBlockPassManager_New::addPass(Pass *P) {
519 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
523 // If this pass does not preserve anlysis that is used by other passes
524 // managed by this manager than it is not a suiable pass for this manager.
525 if (!manageablePass(P))
528 addPassToManager (BP);
533 /// Execute all of the passes scheduled for execution by invoking
534 /// runOnBasicBlock method. Keep track of whether any of the passes modifies
535 /// the function, and if so, return true.
537 BasicBlockPassManager_New::runOnFunction(Function &F) {
539 bool Changed = false;
540 initializeAnalysisInfo();
542 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
543 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
544 e = passVectorEnd(); itr != e; ++itr) {
547 recordAvailableAnalysis(P);
548 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
549 Changed |= BP->runOnBasicBlock(*I);
550 removeNotPreservedAnalysis(P);
556 /// Return true IFF AnalysisID AID is currently available.
557 Pass * BasicBlockPassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
558 return getAnalysisPass(AID);
561 //===----------------------------------------------------------------------===//
562 // FunctionPassManager_New implementation
564 /// Create new Function pass manager
565 FunctionPassManager_New::FunctionPassManager_New() {
566 FPM = new FunctionPassManagerImpl_New();
569 /// add - Add a pass to the queue of passes to run. This passes
570 /// ownership of the Pass to the PassManager. When the
571 /// PassManager_X is destroyed, the pass will be destroyed as well, so
572 /// there is no need to delete the pass. (TODO delete passes.)
573 /// This implies that all passes MUST be allocated with 'new'.
574 void FunctionPassManager_New::add(Pass *P) {
578 /// Execute all of the passes scheduled for execution. Keep
579 /// track of whether any of the passes modifies the function, and if
581 bool FunctionPassManager_New::runOnModule(Module &M) {
582 return FPM->runOnModule(M);
585 /// run - Execute all of the passes scheduled for execution. Keep
586 /// track of whether any of the passes modifies the function, and if
589 bool FunctionPassManager_New::run(Function &F) {
591 if (MP->materializeFunction(&F, &errstr)) {
592 cerr << "Error reading bytecode file: " << errstr << "\n";
595 return FPM->runOnFunction(F);
599 /// doInitialization - Run all of the initializers for the function passes.
601 bool FunctionPassManager_New::doInitialization() {
602 return FPM->doInitialization(*MP->getModule());
605 /// doFinalization - Run all of the initializers for the function passes.
607 bool FunctionPassManager_New::doFinalization() {
608 return FPM->doFinalization(*MP->getModule());
611 //===----------------------------------------------------------------------===//
612 // FunctionPassManagerImpl_New implementation
614 /// Add pass P into the pass manager queue. If P is a BasicBlockPass then
615 /// either use it into active basic block pass manager or create new basic
616 /// block pass manager to handle pass P.
618 FunctionPassManagerImpl_New::addPass(Pass *P) {
620 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
621 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
623 if (!activeBBPassManager
624 || !activeBBPassManager->addPass(BP)) {
626 activeBBPassManager = new BasicBlockPassManager_New();
627 addPassToManager(activeBBPassManager, false);
628 if (!activeBBPassManager->addPass(BP))
629 assert(0 && "Unable to add Pass");
634 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
638 // If this pass does not preserve anlysis that is used by other passes
639 // managed by this manager than it is not a suiable pass for this manager.
640 if (!manageablePass(P))
643 addPassToManager (FP);
644 activeBBPassManager = NULL;
648 /// Execute all of the passes scheduled for execution by invoking
649 /// runOnFunction method. Keep track of whether any of the passes modifies
650 /// the function, and if so, return true.
651 bool FunctionPassManagerImpl_New::runOnModule(Module &M) {
653 bool Changed = false;
654 initializeAnalysisInfo();
656 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
657 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
658 e = passVectorEnd(); itr != e; ++itr) {
661 recordAvailableAnalysis(P);
662 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
663 Changed |= FP->runOnFunction(*I);
664 removeNotPreservedAnalysis(P);
670 /// Execute all of the passes scheduled for execution by invoking
671 /// runOnFunction method. Keep track of whether any of the passes modifies
672 /// the function, and if so, return true.
673 bool FunctionPassManagerImpl_New::runOnFunction(Function &F) {
675 bool Changed = false;
676 initializeAnalysisInfo();
678 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
679 e = passVectorEnd(); itr != e; ++itr) {
682 recordAvailableAnalysis(P);
683 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
684 Changed |= FP->runOnFunction(F);
685 removeNotPreservedAnalysis(P);
692 /// Return true IFF AnalysisID AID is currently available.
693 Pass *FunctionPassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
695 Pass *P = getAnalysisPass(AID);
699 if (activeBBPassManager &&
700 activeBBPassManager->getAnalysisPass(AID) != 0)
701 return activeBBPassManager->getAnalysisPass(AID);
703 // TODO : Check inactive managers
707 inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
708 bool Changed = false;
710 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
711 e = passVectorEnd(); itr != e; ++itr) {
714 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
715 Changed |= FP->doInitialization(M);
721 inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
722 bool Changed = false;
724 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
725 e = passVectorEnd(); itr != e; ++itr) {
728 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
729 Changed |= FP->doFinalization(M);
736 //===----------------------------------------------------------------------===//
737 // ModulePassManager implementation
739 /// Add P into pass vector if it is manageble. If P is a FunctionPass
740 /// then use FunctionPassManagerImpl_New to manage it. Return false if P
741 /// is not manageable by this manager.
743 ModulePassManager_New::addPass(Pass *P) {
745 // If P is FunctionPass then use function pass maanager.
746 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
748 activeFunctionPassManager = NULL;
750 if (!activeFunctionPassManager
751 || !activeFunctionPassManager->addPass(P)) {
753 activeFunctionPassManager = new FunctionPassManagerImpl_New();
754 addPassToManager(activeFunctionPassManager, false);
755 if (!activeFunctionPassManager->addPass(FP))
756 assert(0 && "Unable to add pass");
761 ModulePass *MP = dynamic_cast<ModulePass *>(P);
765 // If this pass does not preserve anlysis that is used by other passes
766 // managed by this manager than it is not a suiable pass for this manager.
767 if (!manageablePass(P))
770 addPassToManager(MP);
771 activeFunctionPassManager = NULL;
776 /// Execute all of the passes scheduled for execution by invoking
777 /// runOnModule method. Keep track of whether any of the passes modifies
778 /// the module, and if so, return true.
780 ModulePassManager_New::runOnModule(Module &M) {
781 bool Changed = false;
782 initializeAnalysisInfo();
784 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
785 e = passVectorEnd(); itr != e; ++itr) {
788 recordAvailableAnalysis(P);
789 ModulePass *MP = dynamic_cast<ModulePass*>(P);
790 Changed |= MP->runOnModule(M);
791 removeNotPreservedAnalysis(P);
797 /// Return true IFF AnalysisID AID is currently available.
798 Pass *ModulePassManager_New::getAnalysisPassFromManager(AnalysisID AID) {
801 Pass *P = getAnalysisPass(AID);
805 if (activeFunctionPassManager &&
806 activeFunctionPassManager->getAnalysisPass(AID) != 0)
807 return activeFunctionPassManager->getAnalysisPass(AID);
809 // TODO : Check inactive managers
813 //===----------------------------------------------------------------------===//
814 // PassManagerImpl implementation
816 /// Return true IFF AnalysisID AID is currently available.
817 Pass *PassManagerImpl_New::getAnalysisPassFromManager(AnalysisID AID) {
820 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
821 e = PassManagers.end(); !P && itr != e; ++itr)
822 P = (*itr)->getAnalysisPassFromManager(AID);
826 /// Schedule pass P for execution. Make sure that passes required by
827 /// P are run before P is run. Update analysis info maintained by
828 /// the manager. Remove dead passes. This is a recursive function.
829 void PassManagerImpl_New::schedulePass(Pass *P) {
831 AnalysisUsage AnUsage;
832 P->getAnalysisUsage(AnUsage);
833 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
834 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
835 E = RequiredSet.end(); I != E; ++I) {
837 Pass *AnalysisPass = getAnalysisPassFromManager(*I);
839 // Schedule this analysis run first.
840 AnalysisPass = (*I)->createPass();
841 schedulePass(AnalysisPass);
843 setLastUser (AnalysisPass, P);
845 // Prolong live range of analyses that are needed after an analysis pass
846 // is destroyed, for querying by subsequent passes
847 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
848 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
849 E = IDs.end(); I != E; ++I) {
850 Pass *AP = getAnalysisPassFromManager(*I);
851 assert (AP && "Analysis pass is not available");
858 /// Schedule all passes from the queue by adding them in their
859 /// respective manager's queue.
860 void PassManagerImpl_New::schedulePasses() {
861 for (std::vector<Pass *>::iterator I = passVectorBegin(),
862 E = passVectorEnd(); I != E; ++I)
866 /// Add pass P to the queue of passes to run.
867 void PassManagerImpl_New::add(Pass *P) {
868 // Do not process Analysis now. Analysis is process while scheduling
870 addPassToManager(P, false);
873 // PassManager_New implementation
874 /// Add P into active pass manager or use new module pass manager to
876 bool PassManagerImpl_New::addPass(Pass *P) {
878 if (!activeManager || !activeManager->addPass(P)) {
879 activeManager = new ModulePassManager_New();
880 PassManagers.push_back(activeManager);
883 return activeManager->addPass(P);
886 /// run - Execute all of the passes scheduled for execution. Keep track of
887 /// whether any of the passes modifies the module, and if so, return true.
888 bool PassManagerImpl_New::run(Module &M) {
891 bool Changed = false;
892 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
893 e = PassManagers.end(); itr != e; ++itr) {
894 ModulePassManager_New *pm = *itr;
895 Changed |= pm->runOnModule(M);
900 //===----------------------------------------------------------------------===//
901 // PassManager implementation
903 /// Create new pass manager
904 PassManager_New::PassManager_New() {
905 PM = new PassManagerImpl_New();
908 /// add - Add a pass to the queue of passes to run. This passes ownership of
909 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
910 /// will be destroyed as well, so there is no need to delete the pass. This
911 /// implies that all passes MUST be allocated with 'new'.
913 PassManager_New::add(Pass *P) {
917 /// run - Execute all of the passes scheduled for execution. Keep track of
918 /// whether any of the passes modifies the module, and if so, return true.
920 PassManager_New::run(Module &M) {