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 //===----------------------------------------------------------------------===//
91 //===----------------------------------------------------------------------===//
94 /// PMTopLevelManager manages LastUser info and collects common APIs used by
95 /// top level pass managers.
96 class PMTopLevelManager {
100 inline std::vector<Pass *>::iterator passManagersBegin() {
101 return PassManagers.begin();
104 inline std::vector<Pass *>::iterator passManagersEnd() {
105 return PassManagers.end();
108 /// Schedule pass P for execution. Make sure that passes required by
109 /// P are run before P is run. Update analysis info maintained by
110 /// the manager. Remove dead passes. This is a recursive function.
111 void schedulePass(Pass *P);
113 /// This is implemented by top level pass manager and used by
114 /// schedulePass() to add analysis info passes that are not available.
115 virtual void addTopLevelPass(Pass *P) = 0;
117 /// Set pass P as the last user of the given analysis passes.
118 void setLastUser(std::vector<Pass *> &AnalysisPasses, Pass *P);
120 /// Collect passes whose last user is P
121 void collectLastUses(std::vector<Pass *> &LastUses, Pass *P);
123 /// Find the pass that implements Analysis AID. Search immutable
124 /// passes and all pass managers. If desired pass is not found
125 /// then return NULL.
126 Pass *findAnalysisPass(AnalysisID AID);
128 virtual ~PMTopLevelManager() {
129 PassManagers.clear();
132 /// Add immutable pass and initialize it.
133 inline void addImmutablePass(ImmutablePass *P) {
135 ImmutablePasses.push_back(P);
138 inline std::vector<ImmutablePass *>& getImmutablePasses() {
139 return ImmutablePasses;
142 void addPassManager(Pass *Manager) {
143 PassManagers.push_back(Manager);
146 // Add Manager into the list of managers that are not directly
147 // maintained by this top level pass manager
148 inline void addIndirectPassManager(PMDataManager *Manager) {
149 IndirectPassManagers.push_back(Manager);
154 /// Collection of pass managers
155 std::vector<Pass *> PassManagers;
157 /// Collection of pass managers that are not directly maintained
158 /// by this pass manager
159 std::vector<PMDataManager *> IndirectPassManagers;
161 // Map to keep track of last user of the analysis pass.
162 // LastUser->second is the last user of Lastuser->first.
163 std::map<Pass *, Pass *> LastUser;
165 /// Immutable passes are managed by top level manager.
166 std::vector<ImmutablePass *> ImmutablePasses;
169 //===----------------------------------------------------------------------===//
172 /// PMDataManager provides the common place to manage the analysis data
173 /// used by pass managers.
174 class PMDataManager {
178 PMDataManager(int D) : TPM(NULL), Depth(D) {
179 initializeAnalysisInfo();
182 /// Return true IFF pass P's required analysis set does not required new
184 bool manageablePass(Pass *P);
186 /// Augment AvailableAnalysis by adding analysis made available by pass P.
187 void recordAvailableAnalysis(Pass *P);
189 /// Remove Analysis that is not preserved by the pass
190 void removeNotPreservedAnalysis(Pass *P);
192 /// Remove dead passes
193 void removeDeadPasses(Pass *P);
195 /// Add pass P into the PassVector. Update
196 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
197 void addPassToManager (Pass *P, bool ProcessAnalysis = true);
199 /// Initialize available analysis information.
200 void initializeAnalysisInfo() {
201 ForcedLastUses.clear();
202 AvailableAnalysis.clear();
205 /// Populate RequiredPasses with the analysis pass that are required by
207 void collectRequiredAnalysisPasses(std::vector<Pass *> &RequiredPasses,
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.
214 void initializeAnalysisImpl(Pass *P);
216 /// Find the pass that implements Analysis AID. If desired pass is not found
217 /// then return NULL.
218 Pass *findAnalysisPass(AnalysisID AID, bool Direction);
220 inline std::vector<Pass *>::iterator passVectorBegin() {
221 return PassVector.begin();
224 inline std::vector<Pass *>::iterator passVectorEnd() {
225 return PassVector.end();
228 // Access toplevel manager
229 PMTopLevelManager *getTopLevelManager() { return TPM; }
230 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
232 unsigned getDepth() { return Depth; }
236 // Collection of pass whose last user asked this manager to claim
237 // last use. If a FunctionPass F is the last user of ModulePass info M
238 // then the F's manager, not F, records itself as a last user of M.
239 std::vector<Pass *> ForcedLastUses;
241 // Top level manager.
242 PMTopLevelManager *TPM;
245 // Set of available Analysis. This information is used while scheduling
246 // pass. If a pass requires an analysis which is not not available then
247 // equired analysis pass is scheduled to run before the pass itself is
249 std::map<AnalysisID, Pass*> AvailableAnalysis;
251 // Collection of pass that are managed by this manager
252 std::vector<Pass *> PassVector;
257 //===----------------------------------------------------------------------===//
258 // BasicBlockPassManager_New
260 /// BasicBlockPassManager_New manages BasicBlockPass. It batches all the
261 /// pass together and sequence them to process one basic block before
262 /// processing next basic block.
263 class BasicBlockPassManager_New : public PMDataManager,
264 public FunctionPass {
267 BasicBlockPassManager_New(int D) : PMDataManager(D) { }
269 /// Add a pass into a passmanager queue.
270 bool addPass(Pass *p);
272 /// Execute all of the passes scheduled for execution. Keep track of
273 /// whether any of the passes modifies the function, and if so, return true.
274 bool runOnFunction(Function &F);
276 /// Pass Manager itself does not invalidate any analysis info.
277 void getAnalysisUsage(AnalysisUsage &Info) const {
278 Info.setPreservesAll();
281 bool doInitialization(Module &M);
282 bool doInitialization(Function &F);
283 bool doFinalization(Module &M);
284 bool doFinalization(Function &F);
288 //===----------------------------------------------------------------------===//
289 // FunctionPassManagerImpl_New
291 /// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
292 /// It batches all function passes and basic block pass managers together and
293 /// sequence them to process one function at a time before processing next
295 class FunctionPassManagerImpl_New : public ModulePass,
296 public PMDataManager,
297 public PMTopLevelManager {
299 FunctionPassManagerImpl_New(int D) : PMDataManager(D) {
300 activeBBPassManager = NULL;
302 ~FunctionPassManagerImpl_New() { /* TODO */ };
304 inline void addTopLevelPass(Pass *P) {
306 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
308 // P is a immutable pass then it will be managed by this
309 // top level manager. Set up analysis resolver to connect them.
310 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
312 initializeAnalysisImpl(P);
313 addImmutablePass(IP);
314 recordAvailableAnalysis(IP);
320 /// add - Add a pass to the queue of passes to run. This passes
321 /// ownership of the Pass to the PassManager. When the
322 /// PassManager_X is destroyed, the pass will be destroyed as well, so
323 /// there is no need to delete the pass. (TODO delete passes.)
324 /// This implies that all passes MUST be allocated with 'new'.
329 /// Add pass into the pass manager queue.
330 bool addPass(Pass *P);
332 /// Execute all of the passes scheduled for execution. Keep
333 /// track of whether any of the passes modifies the function, and if
335 bool runOnModule(Module &M);
336 bool runOnFunction(Function &F);
337 bool run(Function &F);
339 /// doInitialization - Run all of the initializers for the function passes.
341 bool doInitialization(Module &M);
343 /// doFinalization - Run all of the initializers for the function passes.
345 bool doFinalization(Module &M);
347 /// Pass Manager itself does not invalidate any analysis info.
348 void getAnalysisUsage(AnalysisUsage &Info) const {
349 Info.setPreservesAll();
353 // Active Pass Managers
354 BasicBlockPassManager_New *activeBBPassManager;
357 //===----------------------------------------------------------------------===//
358 // ModulePassManager_New
360 /// ModulePassManager_New manages ModulePasses and function pass managers.
361 /// It batches all Module passes passes and function pass managers together and
362 /// sequence them to process one module.
363 class ModulePassManager_New : public Pass,
364 public PMDataManager {
367 ModulePassManager_New(int D) : PMDataManager(D) {
368 activeFunctionPassManager = NULL;
371 /// Add a pass into a passmanager queue.
372 bool addPass(Pass *p);
374 /// run - Execute all of the passes scheduled for execution. Keep track of
375 /// whether any of the passes modifies the module, and if so, return true.
376 bool runOnModule(Module &M);
378 /// Pass Manager itself does not invalidate any analysis info.
379 void getAnalysisUsage(AnalysisUsage &Info) const {
380 Info.setPreservesAll();
384 // Active Pass Manager
385 FunctionPassManagerImpl_New *activeFunctionPassManager;
388 //===----------------------------------------------------------------------===//
389 // PassManagerImpl_New
391 /// PassManagerImpl_New manages ModulePassManagers
392 class PassManagerImpl_New : public Pass,
393 public PMDataManager,
394 public PMTopLevelManager {
398 PassManagerImpl_New(int D) : PMDataManager(D) {
399 activeManager = NULL;
402 /// add - Add a pass to the queue of passes to run. This passes ownership of
403 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
404 /// will be destroyed as well, so there is no need to delete the pass. This
405 /// implies that all passes MUST be allocated with 'new'.
410 /// run - Execute all of the passes scheduled for execution. Keep track of
411 /// whether any of the passes modifies the module, and if so, return true.
414 /// Pass Manager itself does not invalidate any analysis info.
415 void getAnalysisUsage(AnalysisUsage &Info) const {
416 Info.setPreservesAll();
419 inline void addTopLevelPass(Pass *P) {
421 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
423 // P is a immutable pass and it will be managed by this
424 // top level manager. Set up analysis resolver to connect them.
425 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
427 initializeAnalysisImpl(P);
428 addImmutablePass(IP);
429 recordAvailableAnalysis(IP);
437 /// Add a pass into a passmanager queue.
438 bool addPass(Pass *p);
440 // Active Pass Manager
441 ModulePassManager_New *activeManager;
444 } // End of llvm namespace
446 //===----------------------------------------------------------------------===//
447 // PMTopLevelManager implementation
449 /// Set pass P as the last user of the given analysis passes.
450 void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses,
453 for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(),
454 E = AnalysisPasses.end(); I != E; ++I) {
457 // If AP is the last user of other passes then make P last user of
459 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
460 LUE = LastUser.end(); LUI != LUE; ++LUI) {
461 if (LUI->second == AP)
462 LastUser[LUI->first] = P;
468 /// Collect passes whose last user is P
469 void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses,
471 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
472 LUE = LastUser.end(); LUI != LUE; ++LUI)
473 if (LUI->second == P)
474 LastUses.push_back(LUI->first);
477 /// Schedule pass P for execution. Make sure that passes required by
478 /// P are run before P is run. Update analysis info maintained by
479 /// the manager. Remove dead passes. This is a recursive function.
480 void PMTopLevelManager::schedulePass(Pass *P) {
482 // TODO : Allocate function manager for this pass, other wise required set
483 // may be inserted into previous function manager
485 AnalysisUsage AnUsage;
486 P->getAnalysisUsage(AnUsage);
487 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
488 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
489 E = RequiredSet.end(); I != E; ++I) {
491 Pass *AnalysisPass = findAnalysisPass(*I);
493 // Schedule this analysis run first.
494 AnalysisPass = (*I)->createPass();
495 schedulePass(AnalysisPass);
499 // Now all required passes are available.
503 /// Find the pass that implements Analysis AID. Search immutable
504 /// passes and all pass managers. If desired pass is not found
505 /// then return NULL.
506 Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
509 // Check pass managers
510 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
511 E = PassManagers.end(); P == NULL && I != E; ++I) {
512 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
513 assert(PMD && "This is not a PassManager");
514 P = PMD->findAnalysisPass(AID, false);
517 // Check other pass managers
518 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
519 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
520 P = (*I)->findAnalysisPass(AID, false);
522 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
523 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
524 const PassInfo *PI = (*I)->getPassInfo();
528 // If Pass not found then check the interfaces implemented by Immutable Pass
530 const std::vector<const PassInfo*> &ImmPI =
531 PI->getInterfacesImplemented();
532 for (unsigned Index = 0, End = ImmPI.size();
533 P == NULL && Index != End; ++Index)
534 if (ImmPI[Index] == AID)
542 //===----------------------------------------------------------------------===//
543 // PMDataManager implementation
545 /// Return true IFF pass P's required analysis set does not required new
547 bool PMDataManager::manageablePass(Pass *P) {
550 // If this pass is not preserving information that is required by a
551 // pass maintained by higher level pass manager then do not insert
552 // this pass into current manager. Use new manager. For example,
553 // For example, If FunctionPass F is not preserving ModulePass Info M1
554 // that is used by another ModulePass M2 then do not insert F in
555 // current function pass manager.
559 /// Augement AvailableAnalysis by adding analysis made available by pass P.
560 void PMDataManager::recordAvailableAnalysis(Pass *P) {
562 if (const PassInfo *PI = P->getPassInfo()) {
563 AvailableAnalysis[PI] = P;
565 //This pass is the current implementation of all of the interfaces it
566 //implements as well.
567 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
568 for (unsigned i = 0, e = II.size(); i != e; ++i)
569 AvailableAnalysis[II[i]] = P;
573 /// Remove Analyss not preserved by Pass P
574 void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
575 AnalysisUsage AnUsage;
576 P->getAnalysisUsage(AnUsage);
578 if (AnUsage.getPreservesAll())
581 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
582 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
583 E = AvailableAnalysis.end(); I != E; ) {
584 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
585 PreservedSet.end()) {
586 // Remove this analysis
587 if (!dynamic_cast<ImmutablePass*>(I->second)) {
588 std::map<AnalysisID, Pass*>::iterator J = I++;
589 AvailableAnalysis.erase(J);
597 /// Remove analysis passes that are not used any longer
598 void PMDataManager::removeDeadPasses(Pass *P) {
600 std::vector<Pass *> DeadPasses;
601 TPM->collectLastUses(DeadPasses, P);
603 for (std::vector<Pass *>::iterator I = DeadPasses.begin(),
604 E = DeadPasses.end(); I != E; ++I) {
605 (*I)->releaseMemory();
607 std::map<AnalysisID, Pass*>::iterator Pos =
608 AvailableAnalysis.find((*I)->getPassInfo());
610 // It is possible that pass is already removed from the AvailableAnalysis
611 if (Pos != AvailableAnalysis.end())
612 AvailableAnalysis.erase(Pos);
616 /// Add pass P into the PassVector. Update
617 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
618 void PMDataManager::addPassToManager(Pass *P,
619 bool ProcessAnalysis) {
621 // This manager is going to manage pass P. Set up analysis resolver
623 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
626 if (ProcessAnalysis) {
628 // At the moment, this pass is the last user of all required passes.
629 std::vector<Pass *> LastUses;
630 std::vector<Pass *> RequiredPasses;
631 unsigned PDepth = this->getDepth();
633 collectRequiredAnalysisPasses(RequiredPasses, P);
634 for (std::vector<Pass *>::iterator I = RequiredPasses.begin(),
635 E = RequiredPasses.end(); I != E; ++I) {
636 Pass *PRequired = *I;
639 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
640 RDepth = DM.getDepth();
642 if (PDepth == RDepth)
643 LastUses.push_back(PRequired);
644 else if (PDepth > RDepth) {
645 // Let the parent claim responsibility of last use
646 ForcedLastUses.push_back(PRequired);
648 // Note : This feature is not yet implemented
650 "Unable to handle Pass that requires lower level Analysis pass");
654 if (!LastUses.empty())
655 TPM->setLastUser(LastUses, P);
657 // Take a note of analysis required and made available by this pass.
658 // Remove the analysis not preserved by this pass
659 removeNotPreservedAnalysis(P);
660 recordAvailableAnalysis(P);
664 PassVector.push_back(P);
667 /// Populate RequiredPasses with the analysis pass that are required by
669 void PMDataManager::collectRequiredAnalysisPasses(std::vector<Pass *> &RP,
671 AnalysisUsage AnUsage;
672 P->getAnalysisUsage(AnUsage);
673 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
674 for (std::vector<AnalysisID>::const_iterator
675 I = RequiredSet.begin(), E = RequiredSet.end();
677 Pass *AnalysisPass = findAnalysisPass(*I, true);
678 assert (AnalysisPass && "Analysis pass is not available");
679 RP.push_back(AnalysisPass);
682 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
683 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
684 E = IDs.end(); I != E; ++I) {
685 Pass *AnalysisPass = findAnalysisPass(*I, true);
686 assert (AnalysisPass && "Analysis pass is not available");
687 RP.push_back(AnalysisPass);
691 // All Required analyses should be available to the pass as it runs! Here
692 // we fill in the AnalysisImpls member of the pass so that it can
693 // successfully use the getAnalysis() method to retrieve the
694 // implementations it needs.
696 void PMDataManager::initializeAnalysisImpl(Pass *P) {
697 AnalysisUsage AnUsage;
698 P->getAnalysisUsage(AnUsage);
700 for (std::vector<const PassInfo *>::const_iterator
701 I = AnUsage.getRequiredSet().begin(),
702 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
703 Pass *Impl = findAnalysisPass(*I, true);
705 assert(0 && "Analysis used but not available!");
706 AnalysisResolver_New *AR = P->getResolver();
707 AR->addAnalysisImplsPair(*I, Impl);
711 /// Find the pass that implements Analysis AID. If desired pass is not found
712 /// then return NULL.
713 Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
715 // Check if AvailableAnalysis map has one entry.
716 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
718 if (I != AvailableAnalysis.end())
721 // Search Parents through TopLevelManager
723 return TPM->findAnalysisPass(AID);
729 //===----------------------------------------------------------------------===//
730 // NOTE: Is this the right place to define this method ?
731 // getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
732 Pass *AnalysisResolver_New::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
733 return PM.findAnalysisPass(ID, dir);
736 //===----------------------------------------------------------------------===//
737 // BasicBlockPassManager_New implementation
739 /// Add pass P into PassVector and return true. If this pass is not
740 /// manageable by this manager then return false.
742 BasicBlockPassManager_New::addPass(Pass *P) {
744 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
748 // If this pass does not preserve anlysis that is used by other passes
749 // managed by this manager than it is not a suiable pass for this manager.
750 if (!manageablePass(P))
753 addPassToManager (BP);
758 /// Execute all of the passes scheduled for execution by invoking
759 /// runOnBasicBlock method. Keep track of whether any of the passes modifies
760 /// the function, and if so, return true.
762 BasicBlockPassManager_New::runOnFunction(Function &F) {
767 bool Changed = doInitialization(F);
768 initializeAnalysisInfo();
770 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
771 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
772 e = passVectorEnd(); itr != e; ++itr) {
774 initializeAnalysisImpl(P);
775 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
776 Changed |= BP->runOnBasicBlock(*I);
777 removeNotPreservedAnalysis(P);
778 recordAvailableAnalysis(P);
781 return Changed | doFinalization(F);
784 // Implement doInitialization and doFinalization
785 inline bool BasicBlockPassManager_New::doInitialization(Module &M) {
786 bool Changed = false;
788 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
789 e = passVectorEnd(); itr != e; ++itr) {
791 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
792 Changed |= BP->doInitialization(M);
798 inline bool BasicBlockPassManager_New::doFinalization(Module &M) {
799 bool Changed = false;
801 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
802 e = passVectorEnd(); itr != e; ++itr) {
804 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
805 Changed |= BP->doFinalization(M);
811 inline bool BasicBlockPassManager_New::doInitialization(Function &F) {
812 bool Changed = false;
814 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
815 e = passVectorEnd(); itr != e; ++itr) {
817 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
818 Changed |= BP->doInitialization(F);
824 inline bool BasicBlockPassManager_New::doFinalization(Function &F) {
825 bool Changed = false;
827 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
828 e = passVectorEnd(); itr != e; ++itr) {
830 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
831 Changed |= BP->doFinalization(F);
838 //===----------------------------------------------------------------------===//
839 // FunctionPassManager_New implementation
841 /// Create new Function pass manager
842 FunctionPassManager_New::FunctionPassManager_New() {
843 FPM = new FunctionPassManagerImpl_New(0);
846 FunctionPassManager_New::FunctionPassManager_New(ModuleProvider *P) {
847 FPM = new FunctionPassManagerImpl_New(0);
848 // FPM is the top level manager.
849 FPM->setTopLevelManager(FPM);
851 PMDataManager *PMD = dynamic_cast<PMDataManager *>(FPM);
852 AnalysisResolver_New *AR = new AnalysisResolver_New(*PMD);
853 FPM->setResolver(AR);
855 FPM->addPassManager(FPM);
859 /// add - Add a pass to the queue of passes to run. This passes
860 /// ownership of the Pass to the PassManager. When the
861 /// PassManager_X is destroyed, the pass will be destroyed as well, so
862 /// there is no need to delete the pass. (TODO delete passes.)
863 /// This implies that all passes MUST be allocated with 'new'.
864 void FunctionPassManager_New::add(Pass *P) {
868 /// Execute all of the passes scheduled for execution. Keep
869 /// track of whether any of the passes modifies the function, and if
871 bool FunctionPassManager_New::runOnModule(Module &M) {
872 return FPM->runOnModule(M);
875 /// run - Execute all of the passes scheduled for execution. Keep
876 /// track of whether any of the passes modifies the function, and if
879 bool FunctionPassManager_New::run(Function &F) {
881 if (MP->materializeFunction(&F, &errstr)) {
882 cerr << "Error reading bytecode file: " << errstr << "\n";
889 /// doInitialization - Run all of the initializers for the function passes.
891 bool FunctionPassManager_New::doInitialization() {
892 return FPM->doInitialization(*MP->getModule());
895 /// doFinalization - Run all of the initializers for the function passes.
897 bool FunctionPassManager_New::doFinalization() {
898 return FPM->doFinalization(*MP->getModule());
901 //===----------------------------------------------------------------------===//
902 // FunctionPassManagerImpl_New implementation
904 /// Add pass P into the pass manager queue. If P is a BasicBlockPass then
905 /// either use it into active basic block pass manager or create new basic
906 /// block pass manager to handle pass P.
908 FunctionPassManagerImpl_New::addPass(Pass *P) {
910 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
911 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
913 if (!activeBBPassManager || !activeBBPassManager->addPass(BP)) {
915 // If active manager exists then clear its analysis info.
916 if (activeBBPassManager)
917 activeBBPassManager->initializeAnalysisInfo();
919 // Create and add new manager
920 activeBBPassManager =
921 new BasicBlockPassManager_New(getDepth() + 1);
922 // Inherit top level manager
923 activeBBPassManager->setTopLevelManager(this->getTopLevelManager());
925 // Add new manager into current manager's list.
926 addPassToManager(activeBBPassManager, false);
928 // Add new manager into top level manager's indirect passes list
929 PMDataManager *PMD = dynamic_cast<PMDataManager *>(activeBBPassManager);
930 assert (PMD && "Manager is not Pass Manager");
931 TPM->addIndirectPassManager(PMD);
933 // Add pass into new manager. This time it must succeed.
934 if (!activeBBPassManager->addPass(BP))
935 assert(0 && "Unable to add Pass");
938 if (!ForcedLastUses.empty())
939 TPM->setLastUser(ForcedLastUses, this);
944 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
948 // If this pass does not preserve anlysis that is used by other passes
949 // managed by this manager than it is not a suiable pass for this manager.
950 if (!manageablePass(P))
953 addPassToManager (FP);
955 // If active manager exists then clear its analysis info.
956 if (activeBBPassManager) {
957 activeBBPassManager->initializeAnalysisInfo();
958 activeBBPassManager = NULL;
964 /// Execute all of the passes scheduled for execution by invoking
965 /// runOnFunction method. Keep track of whether any of the passes modifies
966 /// the function, and if so, return true.
967 bool FunctionPassManagerImpl_New::runOnModule(Module &M) {
969 bool Changed = doInitialization(M);
970 initializeAnalysisInfo();
972 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
973 this->runOnFunction(*I);
975 return Changed | doFinalization(M);
978 /// Execute all of the passes scheduled for execution by invoking
979 /// runOnFunction method. Keep track of whether any of the passes modifies
980 /// the function, and if so, return true.
981 bool FunctionPassManagerImpl_New::runOnFunction(Function &F) {
983 bool Changed = false;
988 initializeAnalysisInfo();
990 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
991 e = passVectorEnd(); itr != e; ++itr) {
993 initializeAnalysisImpl(P);
994 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
995 Changed |= FP->runOnFunction(F);
996 removeNotPreservedAnalysis(P);
997 recordAvailableAnalysis(P);
1004 inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
1005 bool Changed = false;
1007 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1008 e = passVectorEnd(); itr != e; ++itr) {
1011 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
1012 Changed |= FP->doInitialization(M);
1018 inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
1019 bool Changed = false;
1021 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1022 e = passVectorEnd(); itr != e; ++itr) {
1025 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
1026 Changed |= FP->doFinalization(M);
1032 // Execute all the passes managed by this top level manager.
1033 // Return true if any function is modified by a pass.
1034 bool FunctionPassManagerImpl_New::run(Function &F) {
1036 bool Changed = false;
1037 for (std::vector<Pass *>::iterator I = passManagersBegin(),
1038 E = passManagersEnd(); I != E; ++I) {
1039 FunctionPass *FP = dynamic_cast<FunctionPass *>(*I);
1040 Changed |= FP->runOnFunction(F);
1045 //===----------------------------------------------------------------------===//
1046 // ModulePassManager implementation
1048 /// Add P into pass vector if it is manageble. If P is a FunctionPass
1049 /// then use FunctionPassManagerImpl_New to manage it. Return false if P
1050 /// is not manageable by this manager.
1052 ModulePassManager_New::addPass(Pass *P) {
1054 // If P is FunctionPass then use function pass maanager.
1055 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
1057 if (!activeFunctionPassManager || !activeFunctionPassManager->addPass(P)) {
1059 // If active manager exists then clear its analysis info.
1060 if (activeFunctionPassManager)
1061 activeFunctionPassManager->initializeAnalysisInfo();
1063 // Create and add new manager
1064 activeFunctionPassManager =
1065 new FunctionPassManagerImpl_New(getDepth() + 1);
1067 // Add new manager into current manager's list
1068 addPassToManager(activeFunctionPassManager, false);
1070 // Inherit top level manager
1071 activeFunctionPassManager->setTopLevelManager(this->getTopLevelManager());
1073 // Add new manager into top level manager's indirect passes list
1074 PMDataManager *PMD = dynamic_cast<PMDataManager *>(activeFunctionPassManager);
1075 assert (PMD && "Manager is not Pass Manager");
1076 TPM->addIndirectPassManager(PMD);
1078 // Add pass into new manager. This time it must succeed.
1079 if (!activeFunctionPassManager->addPass(FP))
1080 assert(0 && "Unable to add pass");
1083 if (!ForcedLastUses.empty())
1084 TPM->setLastUser(ForcedLastUses, this);
1089 ModulePass *MP = dynamic_cast<ModulePass *>(P);
1093 // If this pass does not preserve anlysis that is used by other passes
1094 // managed by this manager than it is not a suiable pass for this manager.
1095 if (!manageablePass(P))
1098 addPassToManager(MP);
1099 // If active manager exists then clear its analysis info.
1100 if (activeFunctionPassManager) {
1101 activeFunctionPassManager->initializeAnalysisInfo();
1102 activeFunctionPassManager = NULL;
1109 /// Execute all of the passes scheduled for execution by invoking
1110 /// runOnModule method. Keep track of whether any of the passes modifies
1111 /// the module, and if so, return true.
1113 ModulePassManager_New::runOnModule(Module &M) {
1114 bool Changed = false;
1115 initializeAnalysisInfo();
1117 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1118 e = passVectorEnd(); itr != e; ++itr) {
1120 initializeAnalysisImpl(P);
1121 ModulePass *MP = dynamic_cast<ModulePass*>(P);
1122 Changed |= MP->runOnModule(M);
1123 removeNotPreservedAnalysis(P);
1124 recordAvailableAnalysis(P);
1125 removeDeadPasses(P);
1130 //===----------------------------------------------------------------------===//
1131 // PassManagerImpl implementation
1133 // PassManager_New implementation
1134 /// Add P into active pass manager or use new module pass manager to
1136 bool PassManagerImpl_New::addPass(Pass *P) {
1138 if (!activeManager || !activeManager->addPass(P)) {
1139 activeManager = new ModulePassManager_New(getDepth() + 1);
1140 // Inherit top level manager
1141 activeManager->setTopLevelManager(this->getTopLevelManager());
1143 // This top level manager is going to manage activeManager.
1144 // Set up analysis resolver to connect them.
1145 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
1146 activeManager->setResolver(AR);
1148 addPassManager(activeManager);
1149 return activeManager->addPass(P);
1154 /// run - Execute all of the passes scheduled for execution. Keep track of
1155 /// whether any of the passes modifies the module, and if so, return true.
1156 bool PassManagerImpl_New::run(Module &M) {
1158 bool Changed = false;
1159 for (std::vector<Pass *>::iterator I = passManagersBegin(),
1160 E = passManagersEnd(); I != E; ++I) {
1161 ModulePassManager_New *MP = dynamic_cast<ModulePassManager_New *>(*I);
1162 Changed |= MP->runOnModule(M);
1167 //===----------------------------------------------------------------------===//
1168 // PassManager implementation
1170 /// Create new pass manager
1171 PassManager_New::PassManager_New() {
1172 PM = new PassManagerImpl_New(0);
1173 // PM is the top level manager
1174 PM->setTopLevelManager(PM);
1177 /// add - Add a pass to the queue of passes to run. This passes ownership of
1178 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
1179 /// will be destroyed as well, so there is no need to delete the pass. This
1180 /// implies that all passes MUST be allocated with 'new'.
1182 PassManager_New::add(Pass *P) {
1186 /// run - Execute all of the passes scheduled for execution. Keep track of
1187 /// whether any of the passes modifies the module, and if so, return true.
1189 PassManager_New::run(Module &M) {