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 // TODO : Make it a reference.
337 PMTopLevelManager *TPM;
340 // Set of available Analysis. This information is used while scheduling
341 // pass. If a pass requires an analysis which is not not available then
342 // equired analysis pass is scheduled to run before the pass itself is
344 std::map<AnalysisID, Pass*> AvailableAnalysis;
346 // Collection of pass that are managed by this manager
347 std::vector<Pass *> PassVector;
352 /// BasicBlockPassManager_New manages BasicBlockPass. It batches all the
353 /// pass together and sequence them to process one basic block before
354 /// processing next basic block.
355 class BasicBlockPassManager_New : public PMDataManager,
356 public FunctionPass {
359 BasicBlockPassManager_New(int D) : PMDataManager(D) { }
361 /// Add a pass into a passmanager queue.
362 bool addPass(Pass *p);
364 /// Execute all of the passes scheduled for execution. Keep track of
365 /// whether any of the passes modifies the function, and if so, return true.
366 bool runOnFunction(Function &F);
368 /// Pass Manager itself does not invalidate any analysis info.
369 void getAnalysisUsage(AnalysisUsage &Info) const {
370 Info.setPreservesAll();
373 bool doInitialization(Module &M);
374 bool doInitialization(Function &F);
375 bool doFinalization(Module &M);
376 bool doFinalization(Function &F);
380 /// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
381 /// It batches all function passes and basic block pass managers together and
382 /// sequence them to process one function at a time before processing next
384 class FunctionPassManagerImpl_New : public ModulePass,
385 public PMDataManager,
386 public PMTopLevelManager {
388 FunctionPassManagerImpl_New(ModuleProvider *P, int D) :
389 PMDataManager(D) { /* TODO */ }
390 FunctionPassManagerImpl_New(int D) : PMDataManager(D) {
391 activeBBPassManager = NULL;
393 ~FunctionPassManagerImpl_New() { /* TODO */ };
395 inline void addTopLevelPass(Pass *P) {
397 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
399 // P is a immutable pass then it will be managed by this
400 // top level manager. Set up analysis resolver to connect them.
401 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
403 addImmutablePass(IP);
409 /// add - Add a pass to the queue of passes to run. This passes
410 /// ownership of the Pass to the PassManager. When the
411 /// PassManager_X is destroyed, the pass will be destroyed as well, so
412 /// there is no need to delete the pass. (TODO delete passes.)
413 /// This implies that all passes MUST be allocated with 'new'.
418 /// Add pass into the pass manager queue.
419 bool addPass(Pass *P);
421 /// Execute all of the passes scheduled for execution. Keep
422 /// track of whether any of the passes modifies the function, and if
424 bool runOnModule(Module &M);
425 bool runOnFunction(Function &F);
426 bool run(Function &F);
428 /// doInitialization - Run all of the initializers for the function passes.
430 bool doInitialization(Module &M);
432 /// doFinalization - Run all of the initializers for the function passes.
434 bool doFinalization(Module &M);
436 /// Pass Manager itself does not invalidate any analysis info.
437 void getAnalysisUsage(AnalysisUsage &Info) const {
438 Info.setPreservesAll();
442 // Active Pass Managers
443 BasicBlockPassManager_New *activeBBPassManager;
446 /// ModulePassManager_New manages ModulePasses and function pass managers.
447 /// It batches all Module passes passes and function pass managers together and
448 /// sequence them to process one module.
449 class ModulePassManager_New : public Pass,
450 public PMDataManager {
453 ModulePassManager_New(int D) : PMDataManager(D) {
454 activeFunctionPassManager = NULL;
457 /// Add a pass into a passmanager queue.
458 bool addPass(Pass *p);
460 /// run - Execute all of the passes scheduled for execution. Keep track of
461 /// whether any of the passes modifies the module, and if so, return true.
462 bool runOnModule(Module &M);
464 /// Pass Manager itself does not invalidate any analysis info.
465 void getAnalysisUsage(AnalysisUsage &Info) const {
466 Info.setPreservesAll();
470 // Active Pass Manager
471 FunctionPassManagerImpl_New *activeFunctionPassManager;
474 /// PassManager_New manages ModulePassManagers
475 class PassManagerImpl_New : public Pass,
476 public PMDataManager,
477 public PMTopLevelManager {
481 PassManagerImpl_New(int D) : PMDataManager(D) {}
483 /// add - Add a pass to the queue of passes to run. This passes ownership of
484 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
485 /// will be destroyed as well, so there is no need to delete the pass. This
486 /// implies that all passes MUST be allocated with 'new'.
491 /// run - Execute all of the passes scheduled for execution. Keep track of
492 /// whether any of the passes modifies the module, and if so, return true.
495 /// Pass Manager itself does not invalidate any analysis info.
496 void getAnalysisUsage(AnalysisUsage &Info) const {
497 Info.setPreservesAll();
500 inline void addTopLevelPass(Pass *P) {
502 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
504 // P is a immutable pass and it will be managed by this
505 // top level manager. Set up analysis resolver to connect them.
506 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
508 addImmutablePass(IP);
516 /// Add a pass into a passmanager queue.
517 bool addPass(Pass *p);
519 // Active Pass Manager
520 ModulePassManager_New *activeManager;
523 } // End of llvm namespace
525 //===----------------------------------------------------------------------===//
526 // PMDataManager implementation
528 /// Return true IFF pass P's required analysis set does not required new
530 bool PMDataManager::manageablePass(Pass *P) {
533 // If this pass is not preserving information that is required by a
534 // pass maintained by higher level pass manager then do not insert
535 // this pass into current manager. Use new manager. For example,
536 // For example, If FunctionPass F is not preserving ModulePass Info M1
537 // that is used by another ModulePass M2 then do not insert F in
538 // current function pass manager.
542 /// Augement AvailableAnalysis by adding analysis made available by pass P.
543 void PMDataManager::recordAvailableAnalysis(Pass *P) {
545 if (const PassInfo *PI = P->getPassInfo()) {
546 AvailableAnalysis[PI] = P;
548 //This pass is the current implementation of all of the interfaces it
549 //implements as well.
550 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
551 for (unsigned i = 0, e = II.size(); i != e; ++i)
552 AvailableAnalysis[II[i]] = P;
556 /// Remove Analyss not preserved by Pass P
557 void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
558 AnalysisUsage AnUsage;
559 P->getAnalysisUsage(AnUsage);
561 if (AnUsage.getPreservesAll())
564 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
565 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
566 E = AvailableAnalysis.end(); I != E; ++I ) {
567 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
568 PreservedSet.end()) {
569 // Remove this analysis
570 std::map<AnalysisID, Pass*>::iterator J = I++;
571 AvailableAnalysis.erase(J);
576 /// Remove analysis passes that are not used any longer
577 void PMDataManager::removeDeadPasses(Pass *P) {
579 std::vector<Pass *> DeadPasses;
580 TPM->collectLastUses(DeadPasses, P);
582 for (std::vector<Pass *>::iterator I = DeadPasses.begin(),
583 E = DeadPasses.end(); I != E; ++I) {
584 (*I)->releaseMemory();
586 std::map<AnalysisID, Pass*>::iterator Pos =
587 AvailableAnalysis.find((*I)->getPassInfo());
589 // It is possible that pass is already removed from the AvailableAnalysis
590 if (Pos != AvailableAnalysis.end())
591 AvailableAnalysis.erase(Pos);
595 /// Add pass P into the PassVector. Update
596 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
597 void PMDataManager::addPassToManager(Pass *P,
598 bool ProcessAnalysis) {
600 // This manager is going to manage pass P. Set up analysis resolver
602 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
605 if (ProcessAnalysis) {
607 // At the moment, this pass is the last user of all required passes.
608 std::vector<Pass *> LastUses;
609 std::vector<Pass *> RequiredPasses;
610 unsigned PDepth = this->getDepth();
612 collectRequiredAnalysisPasses(RequiredPasses, P);
613 for (std::vector<Pass *>::iterator I = RequiredPasses.begin(),
614 E = RequiredPasses.end(); I != E; ++I) {
615 Pass *PRequired = *I;
618 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
619 RDepth = DM.getDepth();
621 if (PDepth == RDepth)
622 LastUses.push_back(PRequired);
623 else if (PDepth > RDepth) {
624 // Let the parent claim responsibility of last use
625 ForcedLastUses.push_back(PRequired);
627 // Note : This feature is not yet implemented
629 "Unable to handle Pass that requires lower level Analysis pass");
633 if (!LastUses.empty())
634 TPM->setLastUser(LastUses, P);
636 // Take a note of analysis required and made available by this pass.
637 // Remove the analysis not preserved by this pass
638 initializeAnalysisImpl(P);
639 removeNotPreservedAnalysis(P);
640 recordAvailableAnalysis(P);
644 PassVector.push_back(P);
647 /// Populate RequiredPasses with the analysis pass that are required by
649 void PMDataManager::collectRequiredAnalysisPasses(std::vector<Pass *> &RP,
651 AnalysisUsage AnUsage;
652 P->getAnalysisUsage(AnUsage);
653 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
654 for (std::vector<AnalysisID>::const_iterator
655 I = RequiredSet.begin(), E = RequiredSet.end();
657 Pass *AnalysisPass = findAnalysisPass(*I, true);
658 assert (AnalysisPass && "Analysis pass is not available");
659 RP.push_back(AnalysisPass);
663 // All Required analyses should be available to the pass as it runs! Here
664 // we fill in the AnalysisImpls member of the pass so that it can
665 // successfully use the getAnalysis() method to retrieve the
666 // implementations it needs.
668 void PMDataManager::initializeAnalysisImpl(Pass *P) {
669 AnalysisUsage AnUsage;
670 P->getAnalysisUsage(AnUsage);
672 for (std::vector<const PassInfo *>::const_iterator
673 I = AnUsage.getRequiredSet().begin(),
674 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
675 Pass *Impl = findAnalysisPass(*I, true);
677 assert(0 && "Analysis used but not available!");
678 AnalysisResolver_New *AR = P->getResolver();
679 AR->addAnalysisImplsPair(*I, Impl);
683 /// Find the pass that implements Analysis AID. If desired pass is not found
684 /// then return NULL.
685 Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
687 // Check if AvailableAnalysis map has one entry.
688 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
690 if (I != AvailableAnalysis.end())
693 // Search Parents through TopLevelManager
695 return TPM->findAnalysisPass(AID);
701 //===----------------------------------------------------------------------===//
702 // NOTE: Is this the right place to define this method ?
703 // getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
704 Pass *AnalysisResolver_New::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
705 return PM.findAnalysisPass(ID, dir);
708 //===----------------------------------------------------------------------===//
709 // BasicBlockPassManager_New implementation
711 /// Add pass P into PassVector and return true. If this pass is not
712 /// manageable by this manager then return false.
714 BasicBlockPassManager_New::addPass(Pass *P) {
716 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
720 // If this pass does not preserve anlysis that is used by other passes
721 // managed by this manager than it is not a suiable pass for this manager.
722 if (!manageablePass(P))
725 addPassToManager (BP);
730 /// Execute all of the passes scheduled for execution by invoking
731 /// runOnBasicBlock method. Keep track of whether any of the passes modifies
732 /// the function, and if so, return true.
734 BasicBlockPassManager_New::runOnFunction(Function &F) {
736 bool Changed = doInitialization(F);
737 initializeAnalysisInfo();
739 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
740 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
741 e = passVectorEnd(); itr != e; ++itr) {
744 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
745 Changed |= BP->runOnBasicBlock(*I);
746 removeNotPreservedAnalysis(P);
747 recordAvailableAnalysis(P);
750 return Changed | doFinalization(F);
753 // Implement doInitialization and doFinalization
754 inline bool BasicBlockPassManager_New::doInitialization(Module &M) {
755 bool Changed = false;
757 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
758 e = passVectorEnd(); itr != e; ++itr) {
760 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
761 Changed |= BP->doInitialization(M);
767 inline bool BasicBlockPassManager_New::doFinalization(Module &M) {
768 bool Changed = false;
770 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
771 e = passVectorEnd(); itr != e; ++itr) {
773 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
774 Changed |= BP->doFinalization(M);
780 inline bool BasicBlockPassManager_New::doInitialization(Function &F) {
781 bool Changed = false;
783 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
784 e = passVectorEnd(); itr != e; ++itr) {
786 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
787 Changed |= BP->doInitialization(F);
793 inline bool BasicBlockPassManager_New::doFinalization(Function &F) {
794 bool Changed = false;
796 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
797 e = passVectorEnd(); itr != e; ++itr) {
799 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
800 Changed |= BP->doFinalization(F);
807 //===----------------------------------------------------------------------===//
808 // FunctionPassManager_New implementation
810 /// Create new Function pass manager
811 FunctionPassManager_New::FunctionPassManager_New() {
812 FPM = new FunctionPassManagerImpl_New(0);
815 FunctionPassManager_New::FunctionPassManager_New(ModuleProvider *P) {
816 FPM = new FunctionPassManagerImpl_New(0);
820 /// add - Add a pass to the queue of passes to run. This passes
821 /// ownership of the Pass to the PassManager. When the
822 /// PassManager_X is destroyed, the pass will be destroyed as well, so
823 /// there is no need to delete the pass. (TODO delete passes.)
824 /// This implies that all passes MUST be allocated with 'new'.
825 void FunctionPassManager_New::add(Pass *P) {
829 /// Execute all of the passes scheduled for execution. Keep
830 /// track of whether any of the passes modifies the function, and if
832 bool FunctionPassManager_New::runOnModule(Module &M) {
833 return FPM->runOnModule(M);
836 /// run - Execute all of the passes scheduled for execution. Keep
837 /// track of whether any of the passes modifies the function, and if
840 bool FunctionPassManager_New::run(Function &F) {
842 if (MP->materializeFunction(&F, &errstr)) {
843 cerr << "Error reading bytecode file: " << errstr << "\n";
850 /// doInitialization - Run all of the initializers for the function passes.
852 bool FunctionPassManager_New::doInitialization() {
853 return FPM->doInitialization(*MP->getModule());
856 /// doFinalization - Run all of the initializers for the function passes.
858 bool FunctionPassManager_New::doFinalization() {
859 return FPM->doFinalization(*MP->getModule());
862 //===----------------------------------------------------------------------===//
863 // FunctionPassManagerImpl_New implementation
865 /// Add pass P into the pass manager queue. If P is a BasicBlockPass then
866 /// either use it into active basic block pass manager or create new basic
867 /// block pass manager to handle pass P.
869 FunctionPassManagerImpl_New::addPass(Pass *P) {
871 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
872 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
874 if (!activeBBPassManager || !activeBBPassManager->addPass(BP)) {
876 // If active manager exists then clear its analysis info.
877 if (activeBBPassManager)
878 activeBBPassManager->initializeAnalysisInfo();
880 // Create and add new manager
881 activeBBPassManager =
882 new BasicBlockPassManager_New(getDepth() + 1);
883 addPassToManager(activeBBPassManager, false);
884 TPM->addOtherPassManager(activeBBPassManager);
886 // Add pass into new manager. This time it must succeed.
887 if (!activeBBPassManager->addPass(BP))
888 assert(0 && "Unable to add Pass");
891 if (!ForcedLastUses.empty())
892 TPM->setLastUser(ForcedLastUses, this);
897 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
901 // If this pass does not preserve anlysis that is used by other passes
902 // managed by this manager than it is not a suiable pass for this manager.
903 if (!manageablePass(P))
906 addPassToManager (FP);
908 // If active manager exists then clear its analysis info.
909 if (activeBBPassManager) {
910 activeBBPassManager->initializeAnalysisInfo();
911 activeBBPassManager = NULL;
917 /// Execute all of the passes scheduled for execution by invoking
918 /// runOnFunction method. Keep track of whether any of the passes modifies
919 /// the function, and if so, return true.
920 bool FunctionPassManagerImpl_New::runOnModule(Module &M) {
922 bool Changed = doInitialization(M);
923 initializeAnalysisInfo();
925 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
926 this->runOnFunction(*I);
928 return Changed | doFinalization(M);
931 /// Execute all of the passes scheduled for execution by invoking
932 /// runOnFunction method. Keep track of whether any of the passes modifies
933 /// the function, and if so, return true.
934 bool FunctionPassManagerImpl_New::runOnFunction(Function &F) {
936 bool Changed = false;
937 initializeAnalysisInfo();
939 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
940 e = passVectorEnd(); itr != e; ++itr) {
943 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
944 Changed |= FP->runOnFunction(F);
945 removeNotPreservedAnalysis(P);
946 recordAvailableAnalysis(P);
953 inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
954 bool Changed = false;
956 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
957 e = passVectorEnd(); itr != e; ++itr) {
960 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
961 Changed |= FP->doInitialization(M);
967 inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
968 bool Changed = false;
970 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
971 e = passVectorEnd(); itr != e; ++itr) {
974 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
975 Changed |= FP->doFinalization(M);
981 // Execute all the passes managed by this top level manager.
982 // Return true if any function is modified by a pass.
983 bool FunctionPassManagerImpl_New::run(Function &F) {
985 bool Changed = false;
986 for (std::vector<Pass *>::iterator I = passManagersBegin(),
987 E = passManagersEnd(); I != E; ++I) {
988 FunctionPass *FP = dynamic_cast<FunctionPass *>(*I);
989 Changed |= FP->runOnFunction(F);
994 //===----------------------------------------------------------------------===//
995 // ModulePassManager implementation
997 /// Add P into pass vector if it is manageble. If P is a FunctionPass
998 /// then use FunctionPassManagerImpl_New to manage it. Return false if P
999 /// is not manageable by this manager.
1001 ModulePassManager_New::addPass(Pass *P) {
1003 // If P is FunctionPass then use function pass maanager.
1004 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
1006 if (!activeFunctionPassManager || !activeFunctionPassManager->addPass(P)) {
1008 // If active manager exists then clear its analysis info.
1009 if (activeFunctionPassManager)
1010 activeFunctionPassManager->initializeAnalysisInfo();
1012 // Create and add new manager
1013 activeFunctionPassManager =
1014 new FunctionPassManagerImpl_New(getDepth() + 1);
1015 addPassToManager(activeFunctionPassManager, false);
1016 TPM->addOtherPassManager(activeFunctionPassManager);
1018 // Add pass into new manager. This time it must succeed.
1019 if (!activeFunctionPassManager->addPass(FP))
1020 assert(0 && "Unable to add pass");
1023 if (!ForcedLastUses.empty())
1024 TPM->setLastUser(ForcedLastUses, this);
1029 ModulePass *MP = dynamic_cast<ModulePass *>(P);
1033 // If this pass does not preserve anlysis that is used by other passes
1034 // managed by this manager than it is not a suiable pass for this manager.
1035 if (!manageablePass(P))
1038 addPassToManager(MP);
1039 // If active manager exists then clear its analysis info.
1040 if (activeFunctionPassManager) {
1041 activeFunctionPassManager->initializeAnalysisInfo();
1042 activeFunctionPassManager = NULL;
1049 /// Execute all of the passes scheduled for execution by invoking
1050 /// runOnModule method. Keep track of whether any of the passes modifies
1051 /// the module, and if so, return true.
1053 ModulePassManager_New::runOnModule(Module &M) {
1054 bool Changed = false;
1055 initializeAnalysisInfo();
1057 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1058 e = passVectorEnd(); itr != e; ++itr) {
1061 ModulePass *MP = dynamic_cast<ModulePass*>(P);
1062 Changed |= MP->runOnModule(M);
1063 removeNotPreservedAnalysis(P);
1064 recordAvailableAnalysis(P);
1065 removeDeadPasses(P);
1070 //===----------------------------------------------------------------------===//
1071 // PassManagerImpl implementation
1073 // PassManager_New implementation
1074 /// Add P into active pass manager or use new module pass manager to
1076 bool PassManagerImpl_New::addPass(Pass *P) {
1078 if (!activeManager || !activeManager->addPass(P)) {
1079 activeManager = new ModulePassManager_New(getDepth() + 1);
1081 // This top level manager is going to manage activeManager.
1082 // Set up analysis resolver to connect them.
1083 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
1084 activeManager->setResolver(AR);
1086 addPassManager(activeManager);
1087 return activeManager->addPass(P);
1092 /// run - Execute all of the passes scheduled for execution. Keep track of
1093 /// whether any of the passes modifies the module, and if so, return true.
1094 bool PassManagerImpl_New::run(Module &M) {
1096 bool Changed = false;
1097 for (std::vector<Pass *>::iterator I = passManagersBegin(),
1098 E = passManagersEnd(); I != E; ++I) {
1099 ModulePassManager_New *MP = dynamic_cast<ModulePassManager_New *>(*I);
1100 Changed |= MP->runOnModule(M);
1105 //===----------------------------------------------------------------------===//
1106 // PassManager implementation
1108 /// Create new pass manager
1109 PassManager_New::PassManager_New() {
1110 PM = new PassManagerImpl_New(0);
1113 /// add - Add a pass to the queue of passes to run. This passes ownership of
1114 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
1115 /// will be destroyed as well, so there is no need to delete the pass. This
1116 /// implies that all passes MUST be allocated with 'new'.
1118 PassManager_New::add(Pass *P) {
1122 /// run - Execute all of the passes scheduled for execution. Keep track of
1123 /// whether any of the passes modifies the module, and if so, return true.
1125 PassManager_New::run(Module &M) {