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 = NULL; // FIXME: (*I)->findAnalysisPass(AID, false /* Search downward */);
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 = NULL; // FIXME: (*I)->findAnalysisPass(AID, false /* Search downward */);
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) {
399 /// add - Add a pass to the queue of passes to run. This passes
400 /// ownership of the Pass to the PassManager. When the
401 /// PassManager_X is destroyed, the pass will be destroyed as well, so
402 /// there is no need to delete the pass. (TODO delete passes.)
403 /// This implies that all passes MUST be allocated with 'new'.
408 /// Add pass into the pass manager queue.
409 bool addPass(Pass *P);
411 /// Execute all of the passes scheduled for execution. Keep
412 /// track of whether any of the passes modifies the function, and if
414 bool runOnModule(Module &M);
415 bool runOnFunction(Function &F);
416 bool run(Function &F);
418 /// doInitialization - Run all of the initializers for the function passes.
420 bool doInitialization(Module &M);
422 /// doFinalization - Run all of the initializers for the function passes.
424 bool doFinalization(Module &M);
426 /// Pass Manager itself does not invalidate any analysis info.
427 void getAnalysisUsage(AnalysisUsage &Info) const {
428 Info.setPreservesAll();
432 // Active Pass Managers
433 BasicBlockPassManager_New *activeBBPassManager;
436 /// ModulePassManager_New manages ModulePasses and function pass managers.
437 /// It batches all Module passes passes and function pass managers together and
438 /// sequence them to process one module.
439 class ModulePassManager_New : public Pass,
440 public PMDataManager {
443 ModulePassManager_New(int D) : PMDataManager(D) {
444 activeFunctionPassManager = NULL;
447 /// Add a pass into a passmanager queue.
448 bool addPass(Pass *p);
450 /// run - Execute all of the passes scheduled for execution. Keep track of
451 /// whether any of the passes modifies the module, and if so, return true.
452 bool runOnModule(Module &M);
454 /// Pass Manager itself does not invalidate any analysis info.
455 void getAnalysisUsage(AnalysisUsage &Info) const {
456 Info.setPreservesAll();
460 // Active Pass Manager
461 FunctionPassManagerImpl_New *activeFunctionPassManager;
464 /// PassManager_New manages ModulePassManagers
465 class PassManagerImpl_New : public Pass,
466 public PMDataManager,
467 public PMTopLevelManager {
471 PassManagerImpl_New(int D) : PMDataManager(D) {}
473 /// add - Add a pass to the queue of passes to run. This passes ownership of
474 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
475 /// will be destroyed as well, so there is no need to delete the pass. This
476 /// implies that all passes MUST be allocated with 'new'.
481 /// run - Execute all of the passes scheduled for execution. Keep track of
482 /// whether any of the passes modifies the module, and if so, return true.
485 /// Pass Manager itself does not invalidate any analysis info.
486 void getAnalysisUsage(AnalysisUsage &Info) const {
487 Info.setPreservesAll();
490 inline void addTopLevelPass(Pass *P) {
496 /// Add a pass into a passmanager queue.
497 bool addPass(Pass *p);
499 // Active Pass Manager
500 ModulePassManager_New *activeManager;
503 } // End of llvm namespace
505 //===----------------------------------------------------------------------===//
506 // PMDataManager implementation
508 /// Return true IFF pass P's required analysis set does not required new
510 bool PMDataManager::manageablePass(Pass *P) {
513 // If this pass is not preserving information that is required by a
514 // pass maintained by higher level pass manager then do not insert
515 // this pass into current manager. Use new manager. For example,
516 // For example, If FunctionPass F is not preserving ModulePass Info M1
517 // that is used by another ModulePass M2 then do not insert F in
518 // current function pass manager.
522 /// Augement AvailableAnalysis by adding analysis made available by pass P.
523 void PMDataManager::recordAvailableAnalysis(Pass *P) {
525 if (const PassInfo *PI = P->getPassInfo()) {
526 AvailableAnalysis[PI] = P;
528 //This pass is the current implementation of all of the interfaces it
529 //implements as well.
530 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
531 for (unsigned i = 0, e = II.size(); i != e; ++i)
532 AvailableAnalysis[II[i]] = P;
536 /// Remove Analyss not preserved by Pass P
537 void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
538 AnalysisUsage AnUsage;
539 P->getAnalysisUsage(AnUsage);
541 if (AnUsage.getPreservesAll())
544 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
545 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
546 E = AvailableAnalysis.end(); I != E; ++I ) {
547 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
548 PreservedSet.end()) {
549 // Remove this analysis
550 std::map<AnalysisID, Pass*>::iterator J = I++;
551 AvailableAnalysis.erase(J);
556 /// Remove analysis passes that are not used any longer
557 void PMDataManager::removeDeadPasses(Pass *P) {
559 std::vector<Pass *> DeadPasses;
560 TPM->collectLastUses(DeadPasses, P);
562 for (std::vector<Pass *>::iterator I = DeadPasses.begin(),
563 E = DeadPasses.end(); I != E; ++I) {
564 (*I)->releaseMemory();
566 std::map<AnalysisID, Pass*>::iterator Pos =
567 AvailableAnalysis.find((*I)->getPassInfo());
569 // It is possible that pass is already removed from the AvailableAnalysis
570 if (Pos != AvailableAnalysis.end())
571 AvailableAnalysis.erase(Pos);
575 /// Add pass P into the PassVector. Update
576 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
577 void PMDataManager::addPassToManager(Pass *P,
578 bool ProcessAnalysis) {
580 if (ProcessAnalysis) {
582 // At the moment, this pass is the last user of all required passes.
583 std::vector<Pass *> LastUses;
584 std::vector<Pass *> RequiredPasses;
585 unsigned PDepth = this->getDepth();
587 collectRequiredAnalysisPasses(RequiredPasses, P);
588 for (std::vector<Pass *>::iterator I = RequiredPasses.begin(),
589 E = RequiredPasses.end(); I != E; ++I) {
590 Pass *PRequired = *I;
592 //FIXME: RDepth = PRequired->getResolver()->getDepth();
593 if (PDepth == RDepth)
594 LastUses.push_back(PRequired);
595 else if (PDepth > RDepth) {
596 // Let the parent claim responsibility of last use
597 ForcedLastUses.push_back(PRequired);
599 // Note : This feature is not yet implemented
601 "Unable to handle Pass that requires lower level Analysis pass");
605 if (!LastUses.empty())
606 TPM->setLastUser(LastUses, P);
608 // Take a note of analysis required and made available by this pass.
609 // Remove the analysis not preserved by this pass
610 initializeAnalysisImpl(P);
611 removeNotPreservedAnalysis(P);
612 recordAvailableAnalysis(P);
616 PassVector.push_back(P);
619 /// Populate RequiredPasses with the analysis pass that are required by
621 void PMDataManager::collectRequiredAnalysisPasses(std::vector<Pass *> &RP,
623 AnalysisUsage AnUsage;
624 P->getAnalysisUsage(AnUsage);
625 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
626 for (std::vector<AnalysisID>::const_iterator
627 I = RequiredSet.begin(), E = RequiredSet.end();
629 Pass *AnalysisPass = findAnalysisPass(*I, true);
630 assert (AnalysisPass && "Analysis pass is not available");
631 RP.push_back(AnalysisPass);
635 // All Required analyses should be available to the pass as it runs! Here
636 // we fill in the AnalysisImpls member of the pass so that it can
637 // successfully use the getAnalysis() method to retrieve the
638 // implementations it needs.
640 void PMDataManager::initializeAnalysisImpl(Pass *P) {
641 AnalysisUsage AnUsage;
642 P->getAnalysisUsage(AnUsage);
644 for (std::vector<const PassInfo *>::const_iterator
645 I = AnUsage.getRequiredSet().begin(),
646 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
647 Pass *Impl = findAnalysisPass(*I, true);
649 assert(0 && "Analysis used but not available!");
650 // TODO: P->AnalysisImpls.push_back(std::make_pair(*I, Impl));
654 /// Find the pass that implements Analysis AID. If desired pass is not found
655 /// then return NULL.
656 Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
658 // Check if AvailableAnalysis map has one entry.
659 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
661 if (I != AvailableAnalysis.end())
664 // Search Parents through TopLevelManager
666 return TPM->findAnalysisPass(AID);
668 // FIXME : This is expensive and requires. Need to check only managers not all passes.
669 // One solution is to collect managers in advance at TPM level.
671 for(std::vector<Pass *>::iterator I = passVectorBegin(),
672 E = passVectorEnd(); P == NULL && I!= E; ++I )
673 P = NULL; // FIXME : P = (*I)->getResolver()->getAnalysisToUpdate(AID, false /* Do not search parents again */);
678 //===----------------------------------------------------------------------===//
679 // BasicBlockPassManager_New implementation
681 /// Add pass P into PassVector and return true. If this pass is not
682 /// manageable by this manager then return false.
684 BasicBlockPassManager_New::addPass(Pass *P) {
686 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
690 // If this pass does not preserve anlysis that is used by other passes
691 // managed by this manager than it is not a suiable pass for this manager.
692 if (!manageablePass(P))
695 addPassToManager (BP);
700 /// Execute all of the passes scheduled for execution by invoking
701 /// runOnBasicBlock method. Keep track of whether any of the passes modifies
702 /// the function, and if so, return true.
704 BasicBlockPassManager_New::runOnFunction(Function &F) {
706 bool Changed = doInitialization(F);
707 initializeAnalysisInfo();
709 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
710 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
711 e = passVectorEnd(); itr != e; ++itr) {
714 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
715 Changed |= BP->runOnBasicBlock(*I);
716 removeNotPreservedAnalysis(P);
717 recordAvailableAnalysis(P);
720 return Changed | doFinalization(F);
723 // Implement doInitialization and doFinalization
724 inline bool BasicBlockPassManager_New::doInitialization(Module &M) {
725 bool Changed = false;
727 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
728 e = passVectorEnd(); itr != e; ++itr) {
730 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
731 Changed |= BP->doInitialization(M);
737 inline bool BasicBlockPassManager_New::doFinalization(Module &M) {
738 bool Changed = false;
740 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
741 e = passVectorEnd(); itr != e; ++itr) {
743 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
744 Changed |= BP->doFinalization(M);
750 inline bool BasicBlockPassManager_New::doInitialization(Function &F) {
751 bool Changed = false;
753 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
754 e = passVectorEnd(); itr != e; ++itr) {
756 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
757 Changed |= BP->doInitialization(F);
763 inline bool BasicBlockPassManager_New::doFinalization(Function &F) {
764 bool Changed = false;
766 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
767 e = passVectorEnd(); itr != e; ++itr) {
769 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
770 Changed |= BP->doFinalization(F);
777 //===----------------------------------------------------------------------===//
778 // FunctionPassManager_New implementation
780 /// Create new Function pass manager
781 FunctionPassManager_New::FunctionPassManager_New() {
782 FPM = new FunctionPassManagerImpl_New(0);
785 FunctionPassManager_New::FunctionPassManager_New(ModuleProvider *P) {
786 FPM = new FunctionPassManagerImpl_New(0);
790 /// add - Add a pass to the queue of passes to run. This passes
791 /// ownership of the Pass to the PassManager. When the
792 /// PassManager_X is destroyed, the pass will be destroyed as well, so
793 /// there is no need to delete the pass. (TODO delete passes.)
794 /// This implies that all passes MUST be allocated with 'new'.
795 void FunctionPassManager_New::add(Pass *P) {
799 /// Execute all of the passes scheduled for execution. Keep
800 /// track of whether any of the passes modifies the function, and if
802 bool FunctionPassManager_New::runOnModule(Module &M) {
803 return FPM->runOnModule(M);
806 /// run - Execute all of the passes scheduled for execution. Keep
807 /// track of whether any of the passes modifies the function, and if
810 bool FunctionPassManager_New::run(Function &F) {
812 if (MP->materializeFunction(&F, &errstr)) {
813 cerr << "Error reading bytecode file: " << errstr << "\n";
820 /// doInitialization - Run all of the initializers for the function passes.
822 bool FunctionPassManager_New::doInitialization() {
823 return FPM->doInitialization(*MP->getModule());
826 /// doFinalization - Run all of the initializers for the function passes.
828 bool FunctionPassManager_New::doFinalization() {
829 return FPM->doFinalization(*MP->getModule());
832 //===----------------------------------------------------------------------===//
833 // FunctionPassManagerImpl_New implementation
835 /// Add pass P into the pass manager queue. If P is a BasicBlockPass then
836 /// either use it into active basic block pass manager or create new basic
837 /// block pass manager to handle pass P.
839 FunctionPassManagerImpl_New::addPass(Pass *P) {
841 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
842 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
844 if (!activeBBPassManager || !activeBBPassManager->addPass(BP)) {
846 // If active manager exists then clear its analysis info.
847 if (activeBBPassManager)
848 activeBBPassManager->initializeAnalysisInfo();
850 // Create and add new manager
851 activeBBPassManager =
852 new BasicBlockPassManager_New(getDepth() + 1);
853 addPassToManager(activeBBPassManager, false);
854 TPM->addOtherPassManager(activeBBPassManager);
856 // Add pass into new manager. This time it must succeed.
857 if (!activeBBPassManager->addPass(BP))
858 assert(0 && "Unable to add Pass");
861 if (!ForcedLastUses.empty())
862 TPM->setLastUser(ForcedLastUses, this);
867 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
871 // If this pass does not preserve anlysis that is used by other passes
872 // managed by this manager than it is not a suiable pass for this manager.
873 if (!manageablePass(P))
876 addPassToManager (FP);
878 // If active manager exists then clear its analysis info.
879 if (activeBBPassManager) {
880 activeBBPassManager->initializeAnalysisInfo();
881 activeBBPassManager = NULL;
887 /// Execute all of the passes scheduled for execution by invoking
888 /// runOnFunction method. Keep track of whether any of the passes modifies
889 /// the function, and if so, return true.
890 bool FunctionPassManagerImpl_New::runOnModule(Module &M) {
892 bool Changed = doInitialization(M);
893 initializeAnalysisInfo();
895 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
896 this->runOnFunction(*I);
898 return Changed | doFinalization(M);
901 /// Execute all of the passes scheduled for execution by invoking
902 /// runOnFunction method. Keep track of whether any of the passes modifies
903 /// the function, and if so, return true.
904 bool FunctionPassManagerImpl_New::runOnFunction(Function &F) {
906 bool Changed = false;
907 initializeAnalysisInfo();
909 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
910 e = passVectorEnd(); itr != e; ++itr) {
913 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
914 Changed |= FP->runOnFunction(F);
915 removeNotPreservedAnalysis(P);
916 recordAvailableAnalysis(P);
923 inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
924 bool Changed = false;
926 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
927 e = passVectorEnd(); itr != e; ++itr) {
930 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
931 Changed |= FP->doInitialization(M);
937 inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
938 bool Changed = false;
940 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
941 e = passVectorEnd(); itr != e; ++itr) {
944 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
945 Changed |= FP->doFinalization(M);
951 // Execute all the passes managed by this top level manager.
952 // Return true if any function is modified by a pass.
953 bool FunctionPassManagerImpl_New::run(Function &F) {
955 bool Changed = false;
956 for (std::vector<Pass *>::iterator I = passManagersBegin(),
957 E = passManagersEnd(); I != E; ++I) {
958 FunctionPass *FP = dynamic_cast<FunctionPass *>(*I);
959 Changed |= FP->runOnFunction(F);
964 //===----------------------------------------------------------------------===//
965 // ModulePassManager implementation
967 /// Add P into pass vector if it is manageble. If P is a FunctionPass
968 /// then use FunctionPassManagerImpl_New to manage it. Return false if P
969 /// is not manageable by this manager.
971 ModulePassManager_New::addPass(Pass *P) {
973 // If P is FunctionPass then use function pass maanager.
974 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
976 if (!activeFunctionPassManager || !activeFunctionPassManager->addPass(P)) {
978 // If active manager exists then clear its analysis info.
979 if (activeFunctionPassManager)
980 activeFunctionPassManager->initializeAnalysisInfo();
982 // Create and add new manager
983 activeFunctionPassManager =
984 new FunctionPassManagerImpl_New(getDepth() + 1);
985 addPassToManager(activeFunctionPassManager, false);
986 TPM->addOtherPassManager(activeFunctionPassManager);
988 // Add pass into new manager. This time it must succeed.
989 if (!activeFunctionPassManager->addPass(FP))
990 assert(0 && "Unable to add pass");
993 if (!ForcedLastUses.empty())
994 TPM->setLastUser(ForcedLastUses, this);
999 ModulePass *MP = dynamic_cast<ModulePass *>(P);
1003 // If this pass does not preserve anlysis that is used by other passes
1004 // managed by this manager than it is not a suiable pass for this manager.
1005 if (!manageablePass(P))
1008 addPassToManager(MP);
1009 // If active manager exists then clear its analysis info.
1010 if (activeFunctionPassManager) {
1011 activeFunctionPassManager->initializeAnalysisInfo();
1012 activeFunctionPassManager = NULL;
1019 /// Execute all of the passes scheduled for execution by invoking
1020 /// runOnModule method. Keep track of whether any of the passes modifies
1021 /// the module, and if so, return true.
1023 ModulePassManager_New::runOnModule(Module &M) {
1024 bool Changed = false;
1025 initializeAnalysisInfo();
1027 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1028 e = passVectorEnd(); itr != e; ++itr) {
1031 ModulePass *MP = dynamic_cast<ModulePass*>(P);
1032 Changed |= MP->runOnModule(M);
1033 removeNotPreservedAnalysis(P);
1034 recordAvailableAnalysis(P);
1035 removeDeadPasses(P);
1040 //===----------------------------------------------------------------------===//
1041 // PassManagerImpl implementation
1043 // PassManager_New implementation
1044 /// Add P into active pass manager or use new module pass manager to
1046 bool PassManagerImpl_New::addPass(Pass *P) {
1048 if (!activeManager || !activeManager->addPass(P)) {
1049 activeManager = new ModulePassManager_New(getDepth() + 1);
1050 addPassManager(activeManager);
1051 return activeManager->addPass(P);
1056 /// run - Execute all of the passes scheduled for execution. Keep track of
1057 /// whether any of the passes modifies the module, and if so, return true.
1058 bool PassManagerImpl_New::run(Module &M) {
1060 bool Changed = false;
1061 for (std::vector<Pass *>::iterator I = passManagersBegin(),
1062 E = passManagersEnd(); I != E; ++I) {
1063 ModulePassManager_New *MP = dynamic_cast<ModulePassManager_New *>(*I);
1064 Changed |= MP->runOnModule(M);
1069 //===----------------------------------------------------------------------===//
1070 // PassManager implementation
1072 /// Create new pass manager
1073 PassManager_New::PassManager_New() {
1074 PM = new PassManagerImpl_New(0);
1077 /// add - Add a pass to the queue of passes to run. This passes ownership of
1078 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
1079 /// will be destroyed as well, so there is no need to delete the pass. This
1080 /// implies that all passes MUST be allocated with 'new'.
1082 PassManager_New::add(Pass *P) {
1086 /// run - Execute all of the passes scheduled for execution. Keep track of
1087 /// whether any of the passes modifies the module, and if so, return true.
1089 PassManager_New::run(Module &M) {