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);
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 /// Find the pass that implements Analysis AID. Search immutable
122 /// passes and all pass managers. If desired pass is not found
123 /// then return NULL.
124 Pass *findAnalysisPass(AnalysisID AID);
126 virtual ~PMTopLevelManager() {
127 PassManagers.clear();
130 /// Add immutable pass and initialize it.
131 inline void addImmutablePass(ImmutablePass *P) {
133 ImmutablePasses.push_back(P);
136 inline std::vector<ImmutablePass *>& getImmutablePasses() {
137 return ImmutablePasses;
140 void addPassManager(Pass *Manager) {
141 PassManagers.push_back(Manager);
144 // Add Manager into the list of managers that are not directly
145 // maintained by this top level pass manager
146 void addOtherPassManager(Pass *Manager) {
147 OtherPassManagers.push_back(Manager);
152 /// Collection of pass managers
153 std::vector<Pass *> PassManagers;
155 /// Collection of pass managers that are not directly maintained
156 /// by this pass manager
157 std::vector<Pass *> OtherPassManagers;
159 // Map to keep track of last user of the analysis pass.
160 // LastUser->second is the last user of Lastuser->first.
161 std::map<Pass *, Pass *> LastUser;
163 /// Immutable passes are managed by top level manager.
164 std::vector<ImmutablePass *> ImmutablePasses;
167 /// Set pass P as the last user of the given analysis passes.
168 void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses,
171 for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(),
172 E = AnalysisPasses.end(); I != E; ++I) {
175 // If AP is the last user of other passes then make P last user of
177 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
178 LUE = LastUser.end(); LUI != LUE; ++LUI) {
179 if (LUI->second == AP)
180 LastUser[LUI->first] = P;
186 /// Collect passes whose last user is P
187 void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses,
189 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
190 LUE = LastUser.end(); LUI != LUE; ++LUI)
191 if (LUI->second == P)
192 LastUses.push_back(LUI->first);
195 /// Schedule pass P for execution. Make sure that passes required by
196 /// P are run before P is run. Update analysis info maintained by
197 /// the manager. Remove dead passes. This is a recursive function.
198 void PMTopLevelManager::schedulePass(Pass *P) {
200 // TODO : Allocate function manager for this pass, other wise required set
201 // may be inserted into previous function manager
203 AnalysisUsage AnUsage;
204 P->getAnalysisUsage(AnUsage);
205 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
206 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
207 E = RequiredSet.end(); I != E; ++I) {
209 Pass *AnalysisPass = findAnalysisPass(*I);
211 // Schedule this analysis run first.
212 AnalysisPass = (*I)->createPass();
213 schedulePass(AnalysisPass);
217 // Now all required passes are available.
221 /// Find the pass that implements Analysis AID. Search immutable
222 /// passes and all pass managers. If desired pass is not found
223 /// then return NULL.
224 Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
227 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
228 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
229 const PassInfo *PI = (*I)->getPassInfo();
233 // If Pass not found then check the interfaces implemented by Immutable Pass
235 const std::vector<const PassInfo*> &ImmPI =
236 PI->getInterfacesImplemented();
237 for (unsigned Index = 0, End = ImmPI.size();
238 P == NULL && Index != End; ++Index)
239 if (ImmPI[Index] == AID)
244 // Check pass managers
245 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
246 E = PassManagers.end(); P == NULL && I != E; ++I)
247 P = (*I)->getResolver()->getAnalysisToUpdate(AID, false);
249 // Check other pass managers
250 for (std::vector<Pass *>::iterator I = OtherPassManagers.begin(),
251 E = OtherPassManagers.end(); P == NULL && I != E; ++I)
252 P = (*I)->getResolver()->getAnalysisToUpdate(AID, false);
257 //===----------------------------------------------------------------------===//
260 /// PMDataManager provides the common place to manage the analysis data
261 /// used by pass managers.
262 class PMDataManager {
266 PMDataManager(int D) : TPM(NULL), Depth(D) {
267 initializeAnalysisInfo();
270 /// Return true IFF pass P's required analysis set does not required new
272 bool manageablePass(Pass *P);
274 /// Augment AvailableAnalysis by adding analysis made available by pass P.
275 void recordAvailableAnalysis(Pass *P);
277 /// Remove Analysis that is not preserved by the pass
278 void removeNotPreservedAnalysis(Pass *P);
280 /// Remove dead passes
281 void removeDeadPasses(Pass *P);
283 /// Add pass P into the PassVector. Update
284 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
285 void addPassToManager (Pass *P, bool ProcessAnalysis = true);
287 /// Initialize available analysis information.
288 void initializeAnalysisInfo() {
289 ForcedLastUses.clear();
290 AvailableAnalysis.clear();
292 // Include immutable passes into AvailableAnalysis vector.
293 std::vector<ImmutablePass *> &ImmutablePasses = TPM->getImmutablePasses();
294 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
295 E = ImmutablePasses.end(); I != E; ++I)
296 recordAvailableAnalysis(*I);
299 /// Populate RequiredPasses with the analysis pass that are required by
301 void collectRequiredAnalysisPasses(std::vector<Pass *> &RequiredPasses,
304 /// All Required analyses should be available to the pass as it runs! Here
305 /// we fill in the AnalysisImpls member of the pass so that it can
306 /// successfully use the getAnalysis() method to retrieve the
307 /// implementations it needs.
308 void initializeAnalysisImpl(Pass *P);
310 /// Find the pass that implements Analysis AID. If desired pass is not found
311 /// then return NULL.
312 Pass *findAnalysisPass(AnalysisID AID, bool Direction);
314 inline std::vector<Pass *>::iterator passVectorBegin() {
315 return PassVector.begin();
318 inline std::vector<Pass *>::iterator passVectorEnd() {
319 return PassVector.end();
322 // Access toplevel manager
323 PMTopLevelManager *getTopLevelManager() { return TPM; }
324 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
326 unsigned getDepth() { return Depth; }
330 // Collection of pass whose last user asked this manager to claim
331 // last use. If a FunctionPass F is the last user of ModulePass info M
332 // then the F's manager, not F, records itself as a last user of M.
333 std::vector<Pass *> ForcedLastUses;
335 // Top level manager.
336 PMTopLevelManager *TPM;
339 // Set of available Analysis. This information is used while scheduling
340 // pass. If a pass requires an analysis which is not not available then
341 // equired analysis pass is scheduled to run before the pass itself is
343 std::map<AnalysisID, Pass*> AvailableAnalysis;
345 // Collection of pass that are managed by this manager
346 std::vector<Pass *> PassVector;
351 /// BasicBlockPassManager_New manages BasicBlockPass. It batches all the
352 /// pass together and sequence them to process one basic block before
353 /// processing next basic block.
354 class BasicBlockPassManager_New : public PMDataManager,
355 public FunctionPass {
358 BasicBlockPassManager_New(int D) : PMDataManager(D) { }
360 /// Add a pass into a passmanager queue.
361 bool addPass(Pass *p);
363 /// Execute all of the passes scheduled for execution. Keep track of
364 /// whether any of the passes modifies the function, and if so, return true.
365 bool runOnFunction(Function &F);
367 /// Pass Manager itself does not invalidate any analysis info.
368 void getAnalysisUsage(AnalysisUsage &Info) const {
369 Info.setPreservesAll();
372 bool doInitialization(Module &M);
373 bool doInitialization(Function &F);
374 bool doFinalization(Module &M);
375 bool doFinalization(Function &F);
379 /// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
380 /// It batches all function passes and basic block pass managers together and
381 /// sequence them to process one function at a time before processing next
383 class FunctionPassManagerImpl_New : public ModulePass,
384 public PMDataManager,
385 public PMTopLevelManager {
387 FunctionPassManagerImpl_New(ModuleProvider *P, int D) :
388 PMDataManager(D) { /* TODO */ }
389 FunctionPassManagerImpl_New(int D) : PMDataManager(D) {
390 activeBBPassManager = NULL;
392 ~FunctionPassManagerImpl_New() { /* TODO */ };
394 inline void addTopLevelPass(Pass *P) {
396 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
398 // P is a immutable pass then it will be managed by this
399 // top level manager. Set up analysis resolver to connect them.
400 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
402 addImmutablePass(IP);
408 /// add - Add a pass to the queue of passes to run. This passes
409 /// ownership of the Pass to the PassManager. When the
410 /// PassManager_X is destroyed, the pass will be destroyed as well, so
411 /// there is no need to delete the pass. (TODO delete passes.)
412 /// This implies that all passes MUST be allocated with 'new'.
417 /// Add pass into the pass manager queue.
418 bool addPass(Pass *P);
420 /// Execute all of the passes scheduled for execution. Keep
421 /// track of whether any of the passes modifies the function, and if
423 bool runOnModule(Module &M);
424 bool runOnFunction(Function &F);
425 bool run(Function &F);
427 /// doInitialization - Run all of the initializers for the function passes.
429 bool doInitialization(Module &M);
431 /// doFinalization - Run all of the initializers for the function passes.
433 bool doFinalization(Module &M);
435 /// Pass Manager itself does not invalidate any analysis info.
436 void getAnalysisUsage(AnalysisUsage &Info) const {
437 Info.setPreservesAll();
441 // Active Pass Managers
442 BasicBlockPassManager_New *activeBBPassManager;
445 /// ModulePassManager_New manages ModulePasses and function pass managers.
446 /// It batches all Module passes passes and function pass managers together and
447 /// sequence them to process one module.
448 class ModulePassManager_New : public Pass,
449 public PMDataManager {
452 ModulePassManager_New(int D) : PMDataManager(D) {
453 activeFunctionPassManager = NULL;
456 /// Add a pass into a passmanager queue.
457 bool addPass(Pass *p);
459 /// run - Execute all of the passes scheduled for execution. Keep track of
460 /// whether any of the passes modifies the module, and if so, return true.
461 bool runOnModule(Module &M);
463 /// Pass Manager itself does not invalidate any analysis info.
464 void getAnalysisUsage(AnalysisUsage &Info) const {
465 Info.setPreservesAll();
469 // Active Pass Manager
470 FunctionPassManagerImpl_New *activeFunctionPassManager;
473 /// PassManager_New manages ModulePassManagers
474 class PassManagerImpl_New : public Pass,
475 public PMDataManager,
476 public PMTopLevelManager {
480 PassManagerImpl_New(int D) : PMDataManager(D) {}
482 /// add - Add a pass to the queue of passes to run. This passes ownership of
483 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
484 /// will be destroyed as well, so there is no need to delete the pass. This
485 /// implies that all passes MUST be allocated with 'new'.
490 /// run - Execute all of the passes scheduled for execution. Keep track of
491 /// whether any of the passes modifies the module, and if so, return true.
494 /// Pass Manager itself does not invalidate any analysis info.
495 void getAnalysisUsage(AnalysisUsage &Info) const {
496 Info.setPreservesAll();
499 inline void addTopLevelPass(Pass *P) {
501 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
503 // P is a immutable pass and it will be managed by this
504 // top level manager. Set up analysis resolver to connect them.
505 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
507 addImmutablePass(IP);
515 /// Add a pass into a passmanager queue.
516 bool addPass(Pass *p);
518 // Active Pass Manager
519 ModulePassManager_New *activeManager;
522 } // End of llvm namespace
524 //===----------------------------------------------------------------------===//
525 // PMDataManager implementation
527 /// Return true IFF pass P's required analysis set does not required new
529 bool PMDataManager::manageablePass(Pass *P) {
532 // If this pass is not preserving information that is required by a
533 // pass maintained by higher level pass manager then do not insert
534 // this pass into current manager. Use new manager. For example,
535 // For example, If FunctionPass F is not preserving ModulePass Info M1
536 // that is used by another ModulePass M2 then do not insert F in
537 // current function pass manager.
541 /// Augement AvailableAnalysis by adding analysis made available by pass P.
542 void PMDataManager::recordAvailableAnalysis(Pass *P) {
544 if (const PassInfo *PI = P->getPassInfo()) {
545 AvailableAnalysis[PI] = P;
547 //This pass is the current implementation of all of the interfaces it
548 //implements as well.
549 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
550 for (unsigned i = 0, e = II.size(); i != e; ++i)
551 AvailableAnalysis[II[i]] = P;
555 /// Remove Analyss not preserved by Pass P
556 void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
557 AnalysisUsage AnUsage;
558 P->getAnalysisUsage(AnUsage);
560 if (AnUsage.getPreservesAll())
563 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
564 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
565 E = AvailableAnalysis.end(); I != E; ++I ) {
566 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
567 PreservedSet.end()) {
568 // Remove this analysis
569 std::map<AnalysisID, Pass*>::iterator J = I++;
570 AvailableAnalysis.erase(J);
575 /// Remove analysis passes that are not used any longer
576 void PMDataManager::removeDeadPasses(Pass *P) {
578 std::vector<Pass *> DeadPasses;
579 TPM->collectLastUses(DeadPasses, P);
581 for (std::vector<Pass *>::iterator I = DeadPasses.begin(),
582 E = DeadPasses.end(); I != E; ++I) {
583 (*I)->releaseMemory();
585 std::map<AnalysisID, Pass*>::iterator Pos =
586 AvailableAnalysis.find((*I)->getPassInfo());
588 // It is possible that pass is already removed from the AvailableAnalysis
589 if (Pos != AvailableAnalysis.end())
590 AvailableAnalysis.erase(Pos);
594 /// Add pass P into the PassVector. Update
595 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
596 void PMDataManager::addPassToManager(Pass *P,
597 bool ProcessAnalysis) {
599 // This manager is going to manage pass P. Set up analysis resolver
601 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
604 if (ProcessAnalysis) {
606 // At the moment, this pass is the last user of all required passes.
607 std::vector<Pass *> LastUses;
608 std::vector<Pass *> RequiredPasses;
609 unsigned PDepth = this->getDepth();
611 collectRequiredAnalysisPasses(RequiredPasses, P);
612 for (std::vector<Pass *>::iterator I = RequiredPasses.begin(),
613 E = RequiredPasses.end(); I != E; ++I) {
614 Pass *PRequired = *I;
617 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
618 RDepth = DM.getDepth();
620 if (PDepth == RDepth)
621 LastUses.push_back(PRequired);
622 else if (PDepth > RDepth) {
623 // Let the parent claim responsibility of last use
624 ForcedLastUses.push_back(PRequired);
626 // Note : This feature is not yet implemented
628 "Unable to handle Pass that requires lower level Analysis pass");
632 if (!LastUses.empty())
633 TPM->setLastUser(LastUses, P);
635 // Take a note of analysis required and made available by this pass.
636 // Remove the analysis not preserved by this pass
637 initializeAnalysisImpl(P);
638 removeNotPreservedAnalysis(P);
639 recordAvailableAnalysis(P);
643 PassVector.push_back(P);
646 /// Populate RequiredPasses with the analysis pass that are required by
648 void PMDataManager::collectRequiredAnalysisPasses(std::vector<Pass *> &RP,
650 AnalysisUsage AnUsage;
651 P->getAnalysisUsage(AnUsage);
652 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
653 for (std::vector<AnalysisID>::const_iterator
654 I = RequiredSet.begin(), E = RequiredSet.end();
656 Pass *AnalysisPass = findAnalysisPass(*I, true);
657 assert (AnalysisPass && "Analysis pass is not available");
658 RP.push_back(AnalysisPass);
662 // All Required analyses should be available to the pass as it runs! Here
663 // we fill in the AnalysisImpls member of the pass so that it can
664 // successfully use the getAnalysis() method to retrieve the
665 // implementations it needs.
667 void PMDataManager::initializeAnalysisImpl(Pass *P) {
668 AnalysisUsage AnUsage;
669 P->getAnalysisUsage(AnUsage);
671 for (std::vector<const PassInfo *>::const_iterator
672 I = AnUsage.getRequiredSet().begin(),
673 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
674 Pass *Impl = findAnalysisPass(*I, true);
676 assert(0 && "Analysis used but not available!");
677 AnalysisResolver_New *AR = P->getResolver();
678 AR->addAnalysisImplsPair(*I, Impl);
682 /// Find the pass that implements Analysis AID. If desired pass is not found
683 /// then return NULL.
684 Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
686 // Check if AvailableAnalysis map has one entry.
687 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
689 if (I != AvailableAnalysis.end())
692 // Search Parents through TopLevelManager
694 return TPM->findAnalysisPass(AID);
700 //===----------------------------------------------------------------------===//
701 // NOTE: Is this the right place to define this method ?
702 // getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
703 Pass *AnalysisResolver_New::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
704 return PM.findAnalysisPass(ID, dir);
707 //===----------------------------------------------------------------------===//
708 // BasicBlockPassManager_New implementation
710 /// Add pass P into PassVector and return true. If this pass is not
711 /// manageable by this manager then return false.
713 BasicBlockPassManager_New::addPass(Pass *P) {
715 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
719 // If this pass does not preserve anlysis that is used by other passes
720 // managed by this manager than it is not a suiable pass for this manager.
721 if (!manageablePass(P))
724 addPassToManager (BP);
729 /// Execute all of the passes scheduled for execution by invoking
730 /// runOnBasicBlock method. Keep track of whether any of the passes modifies
731 /// the function, and if so, return true.
733 BasicBlockPassManager_New::runOnFunction(Function &F) {
735 bool Changed = doInitialization(F);
736 initializeAnalysisInfo();
738 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
739 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
740 e = passVectorEnd(); itr != e; ++itr) {
743 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
744 Changed |= BP->runOnBasicBlock(*I);
745 removeNotPreservedAnalysis(P);
746 recordAvailableAnalysis(P);
749 return Changed | doFinalization(F);
752 // Implement doInitialization and doFinalization
753 inline bool BasicBlockPassManager_New::doInitialization(Module &M) {
754 bool Changed = false;
756 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
757 e = passVectorEnd(); itr != e; ++itr) {
759 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
760 Changed |= BP->doInitialization(M);
766 inline bool BasicBlockPassManager_New::doFinalization(Module &M) {
767 bool Changed = false;
769 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
770 e = passVectorEnd(); itr != e; ++itr) {
772 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
773 Changed |= BP->doFinalization(M);
779 inline bool BasicBlockPassManager_New::doInitialization(Function &F) {
780 bool Changed = false;
782 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
783 e = passVectorEnd(); itr != e; ++itr) {
785 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
786 Changed |= BP->doInitialization(F);
792 inline bool BasicBlockPassManager_New::doFinalization(Function &F) {
793 bool Changed = false;
795 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
796 e = passVectorEnd(); itr != e; ++itr) {
798 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
799 Changed |= BP->doFinalization(F);
806 //===----------------------------------------------------------------------===//
807 // FunctionPassManager_New implementation
809 /// Create new Function pass manager
810 FunctionPassManager_New::FunctionPassManager_New() {
811 FPM = new FunctionPassManagerImpl_New(0);
814 FunctionPassManager_New::FunctionPassManager_New(ModuleProvider *P) {
815 FPM = new FunctionPassManagerImpl_New(0);
816 // FPM is the top level manager.
817 FPM->setTopLevelManager(FPM);
821 /// add - Add a pass to the queue of passes to run. This passes
822 /// ownership of the Pass to the PassManager. When the
823 /// PassManager_X is destroyed, the pass will be destroyed as well, so
824 /// there is no need to delete the pass. (TODO delete passes.)
825 /// This implies that all passes MUST be allocated with 'new'.
826 void FunctionPassManager_New::add(Pass *P) {
830 /// Execute all of the passes scheduled for execution. Keep
831 /// track of whether any of the passes modifies the function, and if
833 bool FunctionPassManager_New::runOnModule(Module &M) {
834 return FPM->runOnModule(M);
837 /// run - Execute all of the passes scheduled for execution. Keep
838 /// track of whether any of the passes modifies the function, and if
841 bool FunctionPassManager_New::run(Function &F) {
843 if (MP->materializeFunction(&F, &errstr)) {
844 cerr << "Error reading bytecode file: " << errstr << "\n";
851 /// doInitialization - Run all of the initializers for the function passes.
853 bool FunctionPassManager_New::doInitialization() {
854 return FPM->doInitialization(*MP->getModule());
857 /// doFinalization - Run all of the initializers for the function passes.
859 bool FunctionPassManager_New::doFinalization() {
860 return FPM->doFinalization(*MP->getModule());
863 //===----------------------------------------------------------------------===//
864 // FunctionPassManagerImpl_New implementation
866 /// Add pass P into the pass manager queue. If P is a BasicBlockPass then
867 /// either use it into active basic block pass manager or create new basic
868 /// block pass manager to handle pass P.
870 FunctionPassManagerImpl_New::addPass(Pass *P) {
872 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
873 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
875 if (!activeBBPassManager || !activeBBPassManager->addPass(BP)) {
877 // If active manager exists then clear its analysis info.
878 if (activeBBPassManager)
879 activeBBPassManager->initializeAnalysisInfo();
881 // Create and add new manager
882 activeBBPassManager =
883 new BasicBlockPassManager_New(getDepth() + 1);
884 // Inherit top level manager
885 activeBBPassManager->setTopLevelManager(this->getTopLevelManager());
886 addPassToManager(activeBBPassManager, false);
887 TPM->addOtherPassManager(activeBBPassManager);
889 // Add pass into new manager. This time it must succeed.
890 if (!activeBBPassManager->addPass(BP))
891 assert(0 && "Unable to add Pass");
894 if (!ForcedLastUses.empty())
895 TPM->setLastUser(ForcedLastUses, this);
900 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
904 // If this pass does not preserve anlysis that is used by other passes
905 // managed by this manager than it is not a suiable pass for this manager.
906 if (!manageablePass(P))
909 addPassToManager (FP);
911 // If active manager exists then clear its analysis info.
912 if (activeBBPassManager) {
913 activeBBPassManager->initializeAnalysisInfo();
914 activeBBPassManager = NULL;
920 /// Execute all of the passes scheduled for execution by invoking
921 /// runOnFunction method. Keep track of whether any of the passes modifies
922 /// the function, and if so, return true.
923 bool FunctionPassManagerImpl_New::runOnModule(Module &M) {
925 bool Changed = doInitialization(M);
926 initializeAnalysisInfo();
928 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
929 this->runOnFunction(*I);
931 return Changed | doFinalization(M);
934 /// Execute all of the passes scheduled for execution by invoking
935 /// runOnFunction method. Keep track of whether any of the passes modifies
936 /// the function, and if so, return true.
937 bool FunctionPassManagerImpl_New::runOnFunction(Function &F) {
939 bool Changed = false;
940 initializeAnalysisInfo();
942 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
943 e = passVectorEnd(); itr != e; ++itr) {
946 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
947 Changed |= FP->runOnFunction(F);
948 removeNotPreservedAnalysis(P);
949 recordAvailableAnalysis(P);
956 inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
957 bool Changed = false;
959 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
960 e = passVectorEnd(); itr != e; ++itr) {
963 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
964 Changed |= FP->doInitialization(M);
970 inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
971 bool Changed = false;
973 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
974 e = passVectorEnd(); itr != e; ++itr) {
977 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
978 Changed |= FP->doFinalization(M);
984 // Execute all the passes managed by this top level manager.
985 // Return true if any function is modified by a pass.
986 bool FunctionPassManagerImpl_New::run(Function &F) {
988 bool Changed = false;
989 for (std::vector<Pass *>::iterator I = passManagersBegin(),
990 E = passManagersEnd(); I != E; ++I) {
991 FunctionPass *FP = dynamic_cast<FunctionPass *>(*I);
992 Changed |= FP->runOnFunction(F);
997 //===----------------------------------------------------------------------===//
998 // ModulePassManager implementation
1000 /// Add P into pass vector if it is manageble. If P is a FunctionPass
1001 /// then use FunctionPassManagerImpl_New to manage it. Return false if P
1002 /// is not manageable by this manager.
1004 ModulePassManager_New::addPass(Pass *P) {
1006 // If P is FunctionPass then use function pass maanager.
1007 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
1009 if (!activeFunctionPassManager || !activeFunctionPassManager->addPass(P)) {
1011 // If active manager exists then clear its analysis info.
1012 if (activeFunctionPassManager)
1013 activeFunctionPassManager->initializeAnalysisInfo();
1015 // Create and add new manager
1016 activeFunctionPassManager =
1017 new FunctionPassManagerImpl_New(getDepth() + 1);
1018 addPassToManager(activeFunctionPassManager, false);
1019 // Inherit top level manager
1020 activeFunctionPassManager->setTopLevelManager(this->getTopLevelManager());
1021 TPM->addOtherPassManager(activeFunctionPassManager);
1023 // Add pass into new manager. This time it must succeed.
1024 if (!activeFunctionPassManager->addPass(FP))
1025 assert(0 && "Unable to add pass");
1028 if (!ForcedLastUses.empty())
1029 TPM->setLastUser(ForcedLastUses, this);
1034 ModulePass *MP = dynamic_cast<ModulePass *>(P);
1038 // If this pass does not preserve anlysis that is used by other passes
1039 // managed by this manager than it is not a suiable pass for this manager.
1040 if (!manageablePass(P))
1043 addPassToManager(MP);
1044 // If active manager exists then clear its analysis info.
1045 if (activeFunctionPassManager) {
1046 activeFunctionPassManager->initializeAnalysisInfo();
1047 activeFunctionPassManager = NULL;
1054 /// Execute all of the passes scheduled for execution by invoking
1055 /// runOnModule method. Keep track of whether any of the passes modifies
1056 /// the module, and if so, return true.
1058 ModulePassManager_New::runOnModule(Module &M) {
1059 bool Changed = false;
1060 initializeAnalysisInfo();
1062 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1063 e = passVectorEnd(); itr != e; ++itr) {
1066 ModulePass *MP = dynamic_cast<ModulePass*>(P);
1067 Changed |= MP->runOnModule(M);
1068 removeNotPreservedAnalysis(P);
1069 recordAvailableAnalysis(P);
1070 removeDeadPasses(P);
1075 //===----------------------------------------------------------------------===//
1076 // PassManagerImpl implementation
1078 // PassManager_New implementation
1079 /// Add P into active pass manager or use new module pass manager to
1081 bool PassManagerImpl_New::addPass(Pass *P) {
1083 if (!activeManager || !activeManager->addPass(P)) {
1084 activeManager = new ModulePassManager_New(getDepth() + 1);
1085 // Inherit top level manager
1086 activeManager->setTopLevelManager(this->getTopLevelManager());
1088 // This top level manager is going to manage activeManager.
1089 // Set up analysis resolver to connect them.
1090 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
1091 activeManager->setResolver(AR);
1093 addPassManager(activeManager);
1094 return activeManager->addPass(P);
1099 /// run - Execute all of the passes scheduled for execution. Keep track of
1100 /// whether any of the passes modifies the module, and if so, return true.
1101 bool PassManagerImpl_New::run(Module &M) {
1103 bool Changed = false;
1104 for (std::vector<Pass *>::iterator I = passManagersBegin(),
1105 E = passManagersEnd(); I != E; ++I) {
1106 ModulePassManager_New *MP = dynamic_cast<ModulePassManager_New *>(*I);
1107 Changed |= MP->runOnModule(M);
1112 //===----------------------------------------------------------------------===//
1113 // PassManager implementation
1115 /// Create new pass manager
1116 PassManager_New::PassManager_New() {
1117 PM = new PassManagerImpl_New(0);
1118 // PM is the top level manager
1119 PM->setTopLevelManager(PM);
1122 /// add - Add a pass to the queue of passes to run. This passes ownership of
1123 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
1124 /// will be destroyed as well, so there is no need to delete the pass. This
1125 /// implies that all passes MUST be allocated with 'new'.
1127 PassManager_New::add(Pass *P) {
1131 /// run - Execute all of the passes scheduled for execution. Keep track of
1132 /// whether any of the passes modifies the module, and if so, return true.
1134 PassManager_New::run(Module &M) {