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);
146 /// Collection of pass managers
147 std::vector<Pass *> PassManagers;
149 // Map to keep track of last user of the analysis pass.
150 // LastUser->second is the last user of Lastuser->first.
151 std::map<Pass *, Pass *> LastUser;
153 /// Immutable passes are managed by top level manager.
154 std::vector<ImmutablePass *> ImmutablePasses;
157 /// Set pass P as the last user of the given analysis passes.
158 void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses,
161 for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(),
162 E = AnalysisPasses.end(); I != E; ++I) {
165 // If AP is the last user of other passes then make P last user of
167 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
168 LUE = LastUser.end(); LUI != LUE; ++LUI) {
169 if (LUI->second == AP)
170 LastUser[LUI->first] = P;
176 /// Collect passes whose last user is P
177 void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses,
179 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
180 LUE = LastUser.end(); LUI != LUE; ++LUI)
181 if (LUI->second == P)
182 LastUses.push_back(LUI->first);
185 /// Schedule pass P for execution. Make sure that passes required by
186 /// P are run before P is run. Update analysis info maintained by
187 /// the manager. Remove dead passes. This is a recursive function.
188 void PMTopLevelManager::schedulePass(Pass *P) {
190 // TODO : Allocate function manager for this pass, other wise required set
191 // may be inserted into previous function manager
193 AnalysisUsage AnUsage;
194 P->getAnalysisUsage(AnUsage);
195 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
196 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
197 E = RequiredSet.end(); I != E; ++I) {
199 Pass *AnalysisPass = findAnalysisPass(*I);
201 // Schedule this analysis run first.
202 AnalysisPass = (*I)->createPass();
203 schedulePass(AnalysisPass);
207 // Now all required passes are available.
211 /// Find the pass that implements Analysis AID. Search immutable
212 /// passes and all pass managers. If desired pass is not found
213 /// then return NULL.
214 Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
217 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
218 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
219 const PassInfo *PI = (*I)->getPassInfo();
223 // If Pass not found then check the interfaces implemented by Immutable Pass
225 const std::vector<const PassInfo*> &ImmPI =
226 PI->getInterfacesImplemented();
227 for (unsigned Index = 0, End = ImmPI.size();
228 P == NULL && Index != End; ++Index)
229 if (ImmPI[Index] == AID)
237 // Check pass managers;
238 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
239 E = PassManagers.end(); P == NULL && I != E; ++I)
240 P = NULL; // FIXME: (*I)->findAnalysisPass(AID, false /* Search downward */);
245 //===----------------------------------------------------------------------===//
248 /// PMDataManager provides the common place to manage the analysis data
249 /// used by pass managers.
250 class PMDataManager {
254 PMDataManager(int D) : TPM(NULL), Depth(D) {
255 initializeAnalysisInfo();
258 /// Return true IFF pass P's required analysis set does not required new
260 bool manageablePass(Pass *P);
262 /// Augment AvailableAnalysis by adding analysis made available by pass P.
263 void recordAvailableAnalysis(Pass *P);
265 /// Remove Analysis that is not preserved by the pass
266 void removeNotPreservedAnalysis(Pass *P);
268 /// Remove dead passes
269 void removeDeadPasses(Pass *P);
271 /// Add pass P into the PassVector. Update
272 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
273 void addPassToManager (Pass *P, bool ProcessAnalysis = true);
275 /// Initialize available analysis information.
276 void initializeAnalysisInfo() {
277 ForcedLastUses.clear();
278 AvailableAnalysis.clear();
280 // Include immutable passes into AvailableAnalysis vector.
281 std::vector<ImmutablePass *> &ImmutablePasses = TPM->getImmutablePasses();
282 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
283 E = ImmutablePasses.end(); I != E; ++I)
284 recordAvailableAnalysis(*I);
287 /// Populate RequiredPasses with the analysis pass that are required by
289 void collectRequiredAnalysisPasses(std::vector<Pass *> &RequiredPasses,
292 /// All Required analyses should be available to the pass as it runs! Here
293 /// we fill in the AnalysisImpls member of the pass so that it can
294 /// successfully use the getAnalysis() method to retrieve the
295 /// implementations it needs.
296 void initializeAnalysisImpl(Pass *P);
298 /// Find the pass that implements Analysis AID. If desired pass is not found
299 /// then return NULL.
300 Pass *findAnalysisPass(AnalysisID AID, bool Direction);
302 inline std::vector<Pass *>::iterator passVectorBegin() {
303 return PassVector.begin();
306 inline std::vector<Pass *>::iterator passVectorEnd() {
307 return PassVector.end();
310 // Access toplevel manager
311 PMTopLevelManager *getTopLevelManager() { return TPM; }
312 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
314 unsigned getDepth() { return Depth; }
318 // Collection of pass whose last user asked this manager to claim
319 // last use. If a FunctionPass F is the last user of ModulePass info M
320 // then the F's manager, not F, records itself as a last user of M.
321 std::vector<Pass *> ForcedLastUses;
323 // Top level manager.
324 // TODO : Make it a reference.
325 PMTopLevelManager *TPM;
328 // Set of available Analysis. This information is used while scheduling
329 // pass. If a pass requires an analysis which is not not available then
330 // equired analysis pass is scheduled to run before the pass itself is
332 std::map<AnalysisID, Pass*> AvailableAnalysis;
334 // Collection of pass that are managed by this manager
335 std::vector<Pass *> PassVector;
340 /// BasicBlockPassManager_New manages BasicBlockPass. It batches all the
341 /// pass together and sequence them to process one basic block before
342 /// processing next basic block.
343 class BasicBlockPassManager_New : public PMDataManager,
344 public FunctionPass {
347 BasicBlockPassManager_New(int D) : PMDataManager(D) { }
349 /// Add a pass into a passmanager queue.
350 bool addPass(Pass *p);
352 /// Execute all of the passes scheduled for execution. Keep track of
353 /// whether any of the passes modifies the function, and if so, return true.
354 bool runOnFunction(Function &F);
356 /// Pass Manager itself does not invalidate any analysis info.
357 void getAnalysisUsage(AnalysisUsage &Info) const {
358 Info.setPreservesAll();
361 bool doInitialization(Module &M);
362 bool doInitialization(Function &F);
363 bool doFinalization(Module &M);
364 bool doFinalization(Function &F);
368 /// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
369 /// It batches all function passes and basic block pass managers together and
370 /// sequence them to process one function at a time before processing next
372 class FunctionPassManagerImpl_New : public ModulePass,
373 public PMDataManager,
374 public PMTopLevelManager {
376 FunctionPassManagerImpl_New(ModuleProvider *P, int D) :
377 PMDataManager(D) { /* TODO */ }
378 FunctionPassManagerImpl_New(int D) : PMDataManager(D) {
379 activeBBPassManager = NULL;
381 ~FunctionPassManagerImpl_New() { /* TODO */ };
383 inline void addTopLevelPass(Pass *P) {
387 /// add - Add a pass to the queue of passes to run. This passes
388 /// ownership of the Pass to the PassManager. When the
389 /// PassManager_X is destroyed, the pass will be destroyed as well, so
390 /// there is no need to delete the pass. (TODO delete passes.)
391 /// This implies that all passes MUST be allocated with 'new'.
396 /// Add pass into the pass manager queue.
397 bool addPass(Pass *P);
399 /// Execute all of the passes scheduled for execution. Keep
400 /// track of whether any of the passes modifies the function, and if
402 bool runOnModule(Module &M);
403 bool runOnFunction(Function &F);
404 bool run(Function &F);
406 /// doInitialization - Run all of the initializers for the function passes.
408 bool doInitialization(Module &M);
410 /// doFinalization - Run all of the initializers for the function passes.
412 bool doFinalization(Module &M);
414 /// Pass Manager itself does not invalidate any analysis info.
415 void getAnalysisUsage(AnalysisUsage &Info) const {
416 Info.setPreservesAll();
420 // Active Pass Managers
421 BasicBlockPassManager_New *activeBBPassManager;
424 /// ModulePassManager_New manages ModulePasses and function pass managers.
425 /// It batches all Module passes passes and function pass managers together and
426 /// sequence them to process one module.
427 class ModulePassManager_New : public Pass,
428 public PMDataManager {
431 ModulePassManager_New(int D) : PMDataManager(D) {
432 activeFunctionPassManager = NULL;
435 /// Add a pass into a passmanager queue.
436 bool addPass(Pass *p);
438 /// run - Execute all of the passes scheduled for execution. Keep track of
439 /// whether any of the passes modifies the module, and if so, return true.
440 bool runOnModule(Module &M);
442 /// Pass Manager itself does not invalidate any analysis info.
443 void getAnalysisUsage(AnalysisUsage &Info) const {
444 Info.setPreservesAll();
448 // Active Pass Manager
449 FunctionPassManagerImpl_New *activeFunctionPassManager;
452 /// PassManager_New manages ModulePassManagers
453 class PassManagerImpl_New : public Pass,
454 public PMDataManager,
455 public PMTopLevelManager {
459 PassManagerImpl_New(int D) : PMDataManager(D) {}
461 /// add - Add a pass to the queue of passes to run. This passes ownership of
462 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
463 /// will be destroyed as well, so there is no need to delete the pass. This
464 /// implies that all passes MUST be allocated with 'new'.
469 /// run - Execute all of the passes scheduled for execution. Keep track of
470 /// whether any of the passes modifies the module, and if so, return true.
473 /// Pass Manager itself does not invalidate any analysis info.
474 void getAnalysisUsage(AnalysisUsage &Info) const {
475 Info.setPreservesAll();
478 inline void addTopLevelPass(Pass *P) {
484 /// Add a pass into a passmanager queue.
485 bool addPass(Pass *p);
487 // Active Pass Manager
488 ModulePassManager_New *activeManager;
491 } // End of llvm namespace
493 //===----------------------------------------------------------------------===//
494 // PMDataManager implementation
496 /// Return true IFF pass P's required analysis set does not required new
498 bool PMDataManager::manageablePass(Pass *P) {
501 // If this pass is not preserving information that is required by a
502 // pass maintained by higher level pass manager then do not insert
503 // this pass into current manager. Use new manager. For example,
504 // For example, If FunctionPass F is not preserving ModulePass Info M1
505 // that is used by another ModulePass M2 then do not insert F in
506 // current function pass manager.
510 /// Augement AvailableAnalysis by adding analysis made available by pass P.
511 void PMDataManager::recordAvailableAnalysis(Pass *P) {
513 if (const PassInfo *PI = P->getPassInfo()) {
514 AvailableAnalysis[PI] = P;
516 //This pass is the current implementation of all of the interfaces it
517 //implements as well.
518 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
519 for (unsigned i = 0, e = II.size(); i != e; ++i)
520 AvailableAnalysis[II[i]] = P;
524 /// Remove Analyss not preserved by Pass P
525 void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
526 AnalysisUsage AnUsage;
527 P->getAnalysisUsage(AnUsage);
529 if (AnUsage.getPreservesAll())
532 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
533 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
534 E = AvailableAnalysis.end(); I != E; ++I ) {
535 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
536 PreservedSet.end()) {
537 // Remove this analysis
538 std::map<AnalysisID, Pass*>::iterator J = I++;
539 AvailableAnalysis.erase(J);
544 /// Remove analysis passes that are not used any longer
545 void PMDataManager::removeDeadPasses(Pass *P) {
547 std::vector<Pass *> DeadPasses;
548 TPM->collectLastUses(DeadPasses, P);
550 for (std::vector<Pass *>::iterator I = DeadPasses.begin(),
551 E = DeadPasses.end(); I != E; ++I) {
552 (*I)->releaseMemory();
554 std::map<AnalysisID, Pass*>::iterator Pos =
555 AvailableAnalysis.find((*I)->getPassInfo());
557 // It is possible that pass is already removed from the AvailableAnalysis
558 if (Pos != AvailableAnalysis.end())
559 AvailableAnalysis.erase(Pos);
563 /// Add pass P into the PassVector. Update
564 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
565 void PMDataManager::addPassToManager(Pass *P,
566 bool ProcessAnalysis) {
568 if (ProcessAnalysis) {
570 // At the moment, this pass is the last user of all required passes.
571 std::vector<Pass *> LastUses;
572 std::vector<Pass *> RequiredPasses;
573 unsigned PDepth = this->getDepth();
575 collectRequiredAnalysisPasses(RequiredPasses, P);
576 for (std::vector<Pass *>::iterator I = RequiredPasses.begin(),
577 E = RequiredPasses.end(); I != E; ++I) {
578 Pass *PRequired = *I;
580 //FIXME: RDepth = PRequired->getResolver()->getDepth();
581 if (PDepth == RDepth)
582 LastUses.push_back(PRequired);
583 else if (PDepth > RDepth) {
584 // Let the parent claim responsibility of last use
585 ForcedLastUses.push_back(PRequired);
587 // Note : This feature is not yet implemented
589 "Unable to handle Pass that requires lower level Analysis pass");
593 if (!LastUses.empty())
594 TPM->setLastUser(LastUses, P);
596 // Take a note of analysis required and made available by this pass.
597 // Remove the analysis not preserved by this pass
598 initializeAnalysisImpl(P);
599 removeNotPreservedAnalysis(P);
600 recordAvailableAnalysis(P);
604 PassVector.push_back(P);
607 /// Populate RequiredPasses with the analysis pass that are required by
609 void PMDataManager::collectRequiredAnalysisPasses(std::vector<Pass *> &RP,
611 AnalysisUsage AnUsage;
612 P->getAnalysisUsage(AnUsage);
613 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
614 for (std::vector<AnalysisID>::const_iterator
615 I = RequiredSet.begin(), E = RequiredSet.end();
617 Pass *AnalysisPass = findAnalysisPass(*I, true);
618 assert (AnalysisPass && "Analysis pass is not available");
619 RP.push_back(AnalysisPass);
623 // All Required analyses should be available to the pass as it runs! Here
624 // we fill in the AnalysisImpls member of the pass so that it can
625 // successfully use the getAnalysis() method to retrieve the
626 // implementations it needs.
628 void PMDataManager::initializeAnalysisImpl(Pass *P) {
629 AnalysisUsage AnUsage;
630 P->getAnalysisUsage(AnUsage);
632 for (std::vector<const PassInfo *>::const_iterator
633 I = AnUsage.getRequiredSet().begin(),
634 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
635 Pass *Impl = findAnalysisPass(*I, true);
637 assert(0 && "Analysis used but not available!");
638 // TODO: P->AnalysisImpls.push_back(std::make_pair(*I, Impl));
642 /// Find the pass that implements Analysis AID. If desired pass is not found
643 /// then return NULL.
644 Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
646 // Check if AvailableAnalysis map has one entry.
647 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
649 if (I != AvailableAnalysis.end())
652 // Search Parents through TopLevelManager
654 return TPM->findAnalysisPass(AID);
656 // FIXME : This is expensive and requires. Need to check only managers not all passes.
657 // One solution is to collect managers in advance at TPM level.
659 for(std::vector<Pass *>::iterator I = passVectorBegin(),
660 E = passVectorEnd(); P == NULL && I!= E; ++I )
661 P = NULL; // FIXME : P = (*I)->getResolver()->getAnalysisToUpdate(AID, false /* Do not search parents again */);
666 //===----------------------------------------------------------------------===//
667 // BasicBlockPassManager_New implementation
669 /// Add pass P into PassVector and return true. If this pass is not
670 /// manageable by this manager then return false.
672 BasicBlockPassManager_New::addPass(Pass *P) {
674 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
678 // If this pass does not preserve anlysis that is used by other passes
679 // managed by this manager than it is not a suiable pass for this manager.
680 if (!manageablePass(P))
683 addPassToManager (BP);
688 /// Execute all of the passes scheduled for execution by invoking
689 /// runOnBasicBlock method. Keep track of whether any of the passes modifies
690 /// the function, and if so, return true.
692 BasicBlockPassManager_New::runOnFunction(Function &F) {
694 bool Changed = doInitialization(F);
695 initializeAnalysisInfo();
697 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
698 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
699 e = passVectorEnd(); itr != e; ++itr) {
702 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
703 Changed |= BP->runOnBasicBlock(*I);
704 removeNotPreservedAnalysis(P);
705 recordAvailableAnalysis(P);
708 return Changed | doFinalization(F);
711 // Implement doInitialization and doFinalization
712 inline bool BasicBlockPassManager_New::doInitialization(Module &M) {
713 bool Changed = false;
715 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
716 e = passVectorEnd(); itr != e; ++itr) {
718 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
719 Changed |= BP->doInitialization(M);
725 inline bool BasicBlockPassManager_New::doFinalization(Module &M) {
726 bool Changed = false;
728 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
729 e = passVectorEnd(); itr != e; ++itr) {
731 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
732 Changed |= BP->doFinalization(M);
738 inline bool BasicBlockPassManager_New::doInitialization(Function &F) {
739 bool Changed = false;
741 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
742 e = passVectorEnd(); itr != e; ++itr) {
744 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
745 Changed |= BP->doInitialization(F);
751 inline bool BasicBlockPassManager_New::doFinalization(Function &F) {
752 bool Changed = false;
754 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
755 e = passVectorEnd(); itr != e; ++itr) {
757 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
758 Changed |= BP->doFinalization(F);
765 //===----------------------------------------------------------------------===//
766 // FunctionPassManager_New implementation
768 /// Create new Function pass manager
769 FunctionPassManager_New::FunctionPassManager_New() {
770 FPM = new FunctionPassManagerImpl_New(0);
773 FunctionPassManager_New::FunctionPassManager_New(ModuleProvider *P) {
774 FPM = new FunctionPassManagerImpl_New(0);
778 /// add - Add a pass to the queue of passes to run. This passes
779 /// ownership of the Pass to the PassManager. When the
780 /// PassManager_X is destroyed, the pass will be destroyed as well, so
781 /// there is no need to delete the pass. (TODO delete passes.)
782 /// This implies that all passes MUST be allocated with 'new'.
783 void FunctionPassManager_New::add(Pass *P) {
787 /// Execute all of the passes scheduled for execution. Keep
788 /// track of whether any of the passes modifies the function, and if
790 bool FunctionPassManager_New::runOnModule(Module &M) {
791 return FPM->runOnModule(M);
794 /// run - Execute all of the passes scheduled for execution. Keep
795 /// track of whether any of the passes modifies the function, and if
798 bool FunctionPassManager_New::run(Function &F) {
800 if (MP->materializeFunction(&F, &errstr)) {
801 cerr << "Error reading bytecode file: " << errstr << "\n";
808 /// doInitialization - Run all of the initializers for the function passes.
810 bool FunctionPassManager_New::doInitialization() {
811 return FPM->doInitialization(*MP->getModule());
814 /// doFinalization - Run all of the initializers for the function passes.
816 bool FunctionPassManager_New::doFinalization() {
817 return FPM->doFinalization(*MP->getModule());
820 //===----------------------------------------------------------------------===//
821 // FunctionPassManagerImpl_New implementation
823 /// Add pass P into the pass manager queue. If P is a BasicBlockPass then
824 /// either use it into active basic block pass manager or create new basic
825 /// block pass manager to handle pass P.
827 FunctionPassManagerImpl_New::addPass(Pass *P) {
829 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
830 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
832 if (!activeBBPassManager || !activeBBPassManager->addPass(BP)) {
834 // If active manager exists then clear its analysis info.
835 if (activeBBPassManager)
836 activeBBPassManager->initializeAnalysisInfo();
838 // Create and add new manager
839 activeBBPassManager =
840 new BasicBlockPassManager_New(getDepth() + 1);
841 addPassToManager(activeBBPassManager, false);
843 // Add pass into new manager. This time it must succeed.
844 if (!activeBBPassManager->addPass(BP))
845 assert(0 && "Unable to add Pass");
848 if (!ForcedLastUses.empty())
849 TPM->setLastUser(ForcedLastUses, this);
854 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
858 // If this pass does not preserve anlysis that is used by other passes
859 // managed by this manager than it is not a suiable pass for this manager.
860 if (!manageablePass(P))
863 addPassToManager (FP);
865 // If active manager exists then clear its analysis info.
866 if (activeBBPassManager) {
867 activeBBPassManager->initializeAnalysisInfo();
868 activeBBPassManager = NULL;
874 /// Execute all of the passes scheduled for execution by invoking
875 /// runOnFunction method. Keep track of whether any of the passes modifies
876 /// the function, and if so, return true.
877 bool FunctionPassManagerImpl_New::runOnModule(Module &M) {
879 bool Changed = doInitialization(M);
880 initializeAnalysisInfo();
882 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
883 this->runOnFunction(*I);
885 return Changed | doFinalization(M);
888 /// Execute all of the passes scheduled for execution by invoking
889 /// runOnFunction method. Keep track of whether any of the passes modifies
890 /// the function, and if so, return true.
891 bool FunctionPassManagerImpl_New::runOnFunction(Function &F) {
893 bool Changed = false;
894 initializeAnalysisInfo();
896 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
897 e = passVectorEnd(); itr != e; ++itr) {
900 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
901 Changed |= FP->runOnFunction(F);
902 removeNotPreservedAnalysis(P);
903 recordAvailableAnalysis(P);
910 inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
911 bool Changed = false;
913 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
914 e = passVectorEnd(); itr != e; ++itr) {
917 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
918 Changed |= FP->doInitialization(M);
924 inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
925 bool Changed = false;
927 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
928 e = passVectorEnd(); itr != e; ++itr) {
931 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
932 Changed |= FP->doFinalization(M);
938 // Execute all the passes managed by this top level manager.
939 // Return true if any function is modified by a pass.
940 bool FunctionPassManagerImpl_New::run(Function &F) {
942 bool Changed = false;
943 for (std::vector<Pass *>::iterator I = passManagersBegin(),
944 E = passManagersEnd(); I != E; ++I) {
945 FunctionPass *FP = dynamic_cast<FunctionPass *>(*I);
946 Changed |= FP->runOnFunction(F);
951 //===----------------------------------------------------------------------===//
952 // ModulePassManager implementation
954 /// Add P into pass vector if it is manageble. If P is a FunctionPass
955 /// then use FunctionPassManagerImpl_New to manage it. Return false if P
956 /// is not manageable by this manager.
958 ModulePassManager_New::addPass(Pass *P) {
960 // If P is FunctionPass then use function pass maanager.
961 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
963 if (!activeFunctionPassManager || !activeFunctionPassManager->addPass(P)) {
965 // If active manager exists then clear its analysis info.
966 if (activeFunctionPassManager)
967 activeFunctionPassManager->initializeAnalysisInfo();
969 // Create and add new manager
970 activeFunctionPassManager =
971 new FunctionPassManagerImpl_New(getDepth() + 1);
972 addPassToManager(activeFunctionPassManager, false);
974 // Add pass into new manager. This time it must succeed.
975 if (!activeFunctionPassManager->addPass(FP))
976 assert(0 && "Unable to add pass");
979 if (!ForcedLastUses.empty())
980 TPM->setLastUser(ForcedLastUses, this);
985 ModulePass *MP = dynamic_cast<ModulePass *>(P);
989 // If this pass does not preserve anlysis that is used by other passes
990 // managed by this manager than it is not a suiable pass for this manager.
991 if (!manageablePass(P))
994 addPassToManager(MP);
995 // If active manager exists then clear its analysis info.
996 if (activeFunctionPassManager) {
997 activeFunctionPassManager->initializeAnalysisInfo();
998 activeFunctionPassManager = NULL;
1005 /// Execute all of the passes scheduled for execution by invoking
1006 /// runOnModule method. Keep track of whether any of the passes modifies
1007 /// the module, and if so, return true.
1009 ModulePassManager_New::runOnModule(Module &M) {
1010 bool Changed = false;
1011 initializeAnalysisInfo();
1013 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1014 e = passVectorEnd(); itr != e; ++itr) {
1017 ModulePass *MP = dynamic_cast<ModulePass*>(P);
1018 Changed |= MP->runOnModule(M);
1019 removeNotPreservedAnalysis(P);
1020 recordAvailableAnalysis(P);
1021 removeDeadPasses(P);
1026 //===----------------------------------------------------------------------===//
1027 // PassManagerImpl implementation
1029 // PassManager_New implementation
1030 /// Add P into active pass manager or use new module pass manager to
1032 bool PassManagerImpl_New::addPass(Pass *P) {
1034 if (!activeManager || !activeManager->addPass(P)) {
1035 activeManager = new ModulePassManager_New(getDepth() + 1);
1036 addPassManager(activeManager);
1037 return activeManager->addPass(P);
1042 /// run - Execute all of the passes scheduled for execution. Keep track of
1043 /// whether any of the passes modifies the module, and if so, return true.
1044 bool PassManagerImpl_New::run(Module &M) {
1046 bool Changed = false;
1047 for (std::vector<Pass *>::iterator I = passManagersBegin(),
1048 E = passManagersEnd(); I != E; ++I) {
1049 ModulePassManager_New *MP = dynamic_cast<ModulePassManager_New *>(*I);
1050 Changed |= MP->runOnModule(M);
1055 //===----------------------------------------------------------------------===//
1056 // PassManager implementation
1058 /// Create new pass manager
1059 PassManager_New::PassManager_New() {
1060 PM = new PassManagerImpl_New(0);
1063 /// add - Add a pass to the queue of passes to run. This passes ownership of
1064 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
1065 /// will be destroyed as well, so there is no need to delete the pass. This
1066 /// implies that all passes MUST be allocated with 'new'.
1068 PassManager_New::add(Pass *P) {
1072 /// run - Execute all of the passes scheduled for execution. Keep track of
1073 /// whether any of the passes modifies the module, and if so, return true.
1075 PassManager_New::run(Module &M) {