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;
142 /// Collection of pass managers
143 std::vector<Pass *> PassManagers;
145 // Map to keep track of last user of the analysis pass.
146 // LastUser->second is the last user of Lastuser->first.
147 std::map<Pass *, Pass *> LastUser;
149 /// Immutable passes are managed by top level manager.
150 std::vector<ImmutablePass *> ImmutablePasses;
153 /// Set pass P as the last user of the given analysis passes.
154 void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses,
157 for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(),
158 E = AnalysisPasses.end(); I != E; ++I) {
161 // If AP is the last user of other passes then make P last user of
163 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
164 LUE = LastUser.end(); LUI != LUE; ++LUI) {
165 if (LUI->second == AP)
166 LastUser[LUI->first] = P;
172 /// Collect passes whose last user is P
173 void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses,
175 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
176 LUE = LastUser.end(); LUI != LUE; ++LUI)
177 if (LUI->second == P)
178 LastUses.push_back(LUI->first);
181 /// Schedule pass P for execution. Make sure that passes required by
182 /// P are run before P is run. Update analysis info maintained by
183 /// the manager. Remove dead passes. This is a recursive function.
184 void PMTopLevelManager::schedulePass(Pass *P) {
186 // TODO : Allocate function manager for this pass, other wise required set
187 // may be inserted into previous function manager
189 AnalysisUsage AnUsage;
190 P->getAnalysisUsage(AnUsage);
191 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
192 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
193 E = RequiredSet.end(); I != E; ++I) {
195 Pass *AnalysisPass = findAnalysisPass(*I);
197 // Schedule this analysis run first.
198 AnalysisPass = (*I)->createPass();
199 schedulePass(AnalysisPass);
203 // Now all required passes are available.
207 /// Find the pass that implements Analysis AID. Search immutable
208 /// passes and all pass managers. If desired pass is not found
209 /// then return NULL.
210 Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
213 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
214 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
215 const PassInfo *PI = (*I)->getPassInfo();
219 // If Pass not found then check the interfaces implemented by Immutable Pass
221 const std::vector<const PassInfo*> &ImmPI =
222 PI->getInterfacesImplemented();
223 for (unsigned Index = 0, End = ImmPI.size();
224 P == NULL && Index != End; ++Index)
225 if (ImmPI[Index] == AID)
233 // Check pass managers;
234 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
235 E = PassManagers.end(); P == NULL && I != E; ++I)
236 P = NULL; // FIXME: (*I)->findAnalysisPass(AID, false /* Search downward */);
241 //===----------------------------------------------------------------------===//
244 /// PMDataManager provides the common place to manage the analysis data
245 /// used by pass managers.
246 class PMDataManager {
250 PMDataManager(int D) : TPM(NULL), Depth(D) {
251 initializeAnalysisInfo();
254 /// Return true IFF pass P's required analysis set does not required new
256 bool manageablePass(Pass *P);
258 /// Augment AvailableAnalysis by adding analysis made available by pass P.
259 void recordAvailableAnalysis(Pass *P);
261 /// Remove Analysis that is not preserved by the pass
262 void removeNotPreservedAnalysis(Pass *P);
264 /// Remove dead passes
265 void removeDeadPasses(Pass *P);
267 /// Add pass P into the PassVector. Update
268 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
269 void addPassToManager (Pass *P, bool ProcessAnalysis = true);
271 /// Initialize available analysis information.
272 void initializeAnalysisInfo() {
273 ForcedLastUses.clear();
274 AvailableAnalysis.clear();
276 // Include immutable passes into AvailableAnalysis vector.
277 std::vector<ImmutablePass *> &ImmutablePasses = TPM->getImmutablePasses();
278 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
279 E = ImmutablePasses.end(); I != E; ++I)
280 recordAvailableAnalysis(*I);
283 /// Populate RequiredPasses with the analysis pass that are required by
285 void collectRequiredAnalysisPasses(std::vector<Pass *> &RequiredPasses,
288 /// All Required analyses should be available to the pass as it runs! Here
289 /// we fill in the AnalysisImpls member of the pass so that it can
290 /// successfully use the getAnalysis() method to retrieve the
291 /// implementations it needs.
292 void initializeAnalysisImpl(Pass *P);
294 /// Find the pass that implements Analysis AID. If desired pass is not found
295 /// then return NULL.
296 Pass *findAnalysisPass(AnalysisID AID, bool Direction);
298 inline std::vector<Pass *>::iterator passVectorBegin() {
299 return PassVector.begin();
302 inline std::vector<Pass *>::iterator passVectorEnd() {
303 return PassVector.end();
306 // Access toplevel manager
307 PMTopLevelManager *getTopLevelManager() { return TPM; }
308 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
310 unsigned getDepth() { return Depth; }
314 // Collection of pass whose last user asked this manager to claim
315 // last use. If a FunctionPass F is the last user of ModulePass info M
316 // then the F's manager, not F, records itself as a last user of M.
317 std::vector<Pass *> ForcedLastUses;
319 // Top level manager.
320 // TODO : Make it a reference.
321 PMTopLevelManager *TPM;
324 // Set of available Analysis. This information is used while scheduling
325 // pass. If a pass requires an analysis which is not not available then
326 // equired analysis pass is scheduled to run before the pass itself is
328 std::map<AnalysisID, Pass*> AvailableAnalysis;
330 // Collection of pass that are managed by this manager
331 std::vector<Pass *> PassVector;
336 /// BasicBlockPassManager_New manages BasicBlockPass. It batches all the
337 /// pass together and sequence them to process one basic block before
338 /// processing next basic block.
339 class BasicBlockPassManager_New : public PMDataManager,
340 public FunctionPass {
343 BasicBlockPassManager_New(int D) : PMDataManager(D) { }
345 /// Add a pass into a passmanager queue.
346 bool addPass(Pass *p);
348 /// Execute all of the passes scheduled for execution. Keep track of
349 /// whether any of the passes modifies the function, and if so, return true.
350 bool runOnFunction(Function &F);
352 /// Pass Manager itself does not invalidate any analysis info.
353 void getAnalysisUsage(AnalysisUsage &Info) const {
354 Info.setPreservesAll();
357 bool doInitialization(Module &M);
358 bool doInitialization(Function &F);
359 bool doFinalization(Module &M);
360 bool doFinalization(Function &F);
364 /// FunctionPassManagerImpl_New manages FunctionPasses and BasicBlockPassManagers.
365 /// It batches all function passes and basic block pass managers together and
366 /// sequence them to process one function at a time before processing next
368 class FunctionPassManagerImpl_New : public ModulePass,
369 public PMDataManager,
370 public PMTopLevelManager {
372 FunctionPassManagerImpl_New(ModuleProvider *P, int D) :
373 PMDataManager(D) { /* TODO */ }
374 FunctionPassManagerImpl_New(int D) : PMDataManager(D) {
375 activeBBPassManager = NULL;
377 ~FunctionPassManagerImpl_New() { /* TODO */ };
379 inline void addTopLevelPass(Pass *P) {
383 /// add - Add a pass to the queue of passes to run. This passes
384 /// ownership of the Pass to the PassManager. When the
385 /// PassManager_X is destroyed, the pass will be destroyed as well, so
386 /// there is no need to delete the pass. (TODO delete passes.)
387 /// This implies that all passes MUST be allocated with 'new'.
392 /// Add pass into the pass manager queue.
393 bool addPass(Pass *P);
395 /// Execute all of the passes scheduled for execution. Keep
396 /// track of whether any of the passes modifies the function, and if
398 bool runOnModule(Module &M);
399 bool runOnFunction(Function &F);
401 /// doInitialization - Run all of the initializers for the function passes.
403 bool doInitialization(Module &M);
405 /// doFinalization - Run all of the initializers for the function passes.
407 bool doFinalization(Module &M);
409 /// Pass Manager itself does not invalidate any analysis info.
410 void getAnalysisUsage(AnalysisUsage &Info) const {
411 Info.setPreservesAll();
415 // Active Pass Managers
416 BasicBlockPassManager_New *activeBBPassManager;
419 /// ModulePassManager_New manages ModulePasses and function pass managers.
420 /// It batches all Module passes passes and function pass managers together and
421 /// sequence them to process one module.
422 class ModulePassManager_New : public Pass,
423 public PMDataManager {
426 ModulePassManager_New(int D) : PMDataManager(D) {
427 activeFunctionPassManager = NULL;
430 /// Add a pass into a passmanager queue.
431 bool addPass(Pass *p);
433 /// run - Execute all of the passes scheduled for execution. Keep track of
434 /// whether any of the passes modifies the module, and if so, return true.
435 bool runOnModule(Module &M);
437 /// Pass Manager itself does not invalidate any analysis info.
438 void getAnalysisUsage(AnalysisUsage &Info) const {
439 Info.setPreservesAll();
443 // Active Pass Manager
444 FunctionPassManagerImpl_New *activeFunctionPassManager;
447 /// PassManager_New manages ModulePassManagers
448 class PassManagerImpl_New : public Pass,
449 public PMDataManager,
450 public PMTopLevelManager {
454 PassManagerImpl_New(int D) : PMDataManager(D) {}
456 /// add - Add a pass to the queue of passes to run. This passes ownership of
457 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
458 /// will be destroyed as well, so there is no need to delete the pass. This
459 /// implies that all passes MUST be allocated with 'new'.
464 /// run - Execute all of the passes scheduled for execution. Keep track of
465 /// whether any of the passes modifies the module, and if so, return true.
468 /// Pass Manager itself does not invalidate any analysis info.
469 void getAnalysisUsage(AnalysisUsage &Info) const {
470 Info.setPreservesAll();
473 inline void addTopLevelPass(Pass *P) {
479 /// Add a pass into a passmanager queue.
480 bool addPass(Pass *p);
482 // Collection of pass managers
483 std::vector<ModulePassManager_New *> PassManagers;
485 // Active Pass Manager
486 ModulePassManager_New *activeManager;
489 } // End of llvm namespace
491 //===----------------------------------------------------------------------===//
492 // PMDataManager implementation
494 /// Return true IFF pass P's required analysis set does not required new
496 bool PMDataManager::manageablePass(Pass *P) {
499 // If this pass is not preserving information that is required by a
500 // pass maintained by higher level pass manager then do not insert
501 // this pass into current manager. Use new manager. For example,
502 // For example, If FunctionPass F is not preserving ModulePass Info M1
503 // that is used by another ModulePass M2 then do not insert F in
504 // current function pass manager.
508 /// Augement AvailableAnalysis by adding analysis made available by pass P.
509 void PMDataManager::recordAvailableAnalysis(Pass *P) {
511 if (const PassInfo *PI = P->getPassInfo()) {
512 AvailableAnalysis[PI] = P;
514 //This pass is the current implementation of all of the interfaces it
515 //implements as well.
516 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
517 for (unsigned i = 0, e = II.size(); i != e; ++i)
518 AvailableAnalysis[II[i]] = P;
522 /// Remove Analyss not preserved by Pass P
523 void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
524 AnalysisUsage AnUsage;
525 P->getAnalysisUsage(AnUsage);
527 if (AnUsage.getPreservesAll())
530 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
531 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
532 E = AvailableAnalysis.end(); I != E; ++I ) {
533 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
534 PreservedSet.end()) {
535 // Remove this analysis
536 std::map<AnalysisID, Pass*>::iterator J = I++;
537 AvailableAnalysis.erase(J);
542 /// Remove analysis passes that are not used any longer
543 void PMDataManager::removeDeadPasses(Pass *P) {
545 std::vector<Pass *> DeadPasses;
546 TPM->collectLastUses(DeadPasses, P);
548 for (std::vector<Pass *>::iterator I = DeadPasses.begin(),
549 E = DeadPasses.end(); I != E; ++I) {
550 (*I)->releaseMemory();
552 std::map<AnalysisID, Pass*>::iterator Pos =
553 AvailableAnalysis.find((*I)->getPassInfo());
555 // It is possible that pass is already removed from the AvailableAnalysis
556 if (Pos != AvailableAnalysis.end())
557 AvailableAnalysis.erase(Pos);
561 /// Add pass P into the PassVector. Update
562 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
563 void PMDataManager::addPassToManager(Pass *P,
564 bool ProcessAnalysis) {
566 if (ProcessAnalysis) {
568 // At the moment, this pass is the last user of all required passes.
569 std::vector<Pass *> LastUses;
570 std::vector<Pass *> RequiredPasses;
571 unsigned PDepth = this->getDepth();
573 collectRequiredAnalysisPasses(RequiredPasses, P);
574 for (std::vector<Pass *>::iterator I = RequiredPasses.begin(),
575 E = RequiredPasses.end(); I != E; ++I) {
576 Pass *PRequired = *I;
578 //FIXME: RDepth = PRequired->getResolver()->getDepth();
579 if (PDepth == RDepth)
580 LastUses.push_back(PRequired);
581 else if (PDepth > RDepth) {
582 // Let the parent claim responsibility of last use
583 ForcedLastUses.push_back(PRequired);
585 // Note : This feature is not yet implemented
587 "Unable to handle Pass that requires lower level Analysis pass");
591 if (!LastUses.empty())
592 TPM->setLastUser(LastUses, P);
594 // Take a note of analysis required and made available by this pass.
595 // Remove the analysis not preserved by this pass
596 initializeAnalysisImpl(P);
597 removeNotPreservedAnalysis(P);
598 recordAvailableAnalysis(P);
602 PassVector.push_back(P);
605 /// Populate RequiredPasses with the analysis pass that are required by
607 void PMDataManager::collectRequiredAnalysisPasses(std::vector<Pass *> &RP,
609 AnalysisUsage AnUsage;
610 P->getAnalysisUsage(AnUsage);
611 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
612 for (std::vector<AnalysisID>::const_iterator
613 I = RequiredSet.begin(), E = RequiredSet.end();
615 Pass *AnalysisPass = findAnalysisPass(*I, true);
616 assert (AnalysisPass && "Analysis pass is not available");
617 RP.push_back(AnalysisPass);
621 // All Required analyses should be available to the pass as it runs! Here
622 // we fill in the AnalysisImpls member of the pass so that it can
623 // successfully use the getAnalysis() method to retrieve the
624 // implementations it needs.
626 void PMDataManager::initializeAnalysisImpl(Pass *P) {
627 AnalysisUsage AnUsage;
628 P->getAnalysisUsage(AnUsage);
630 for (std::vector<const PassInfo *>::const_iterator
631 I = AnUsage.getRequiredSet().begin(),
632 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
633 Pass *Impl = findAnalysisPass(*I, true);
635 assert(0 && "Analysis used but not available!");
636 // TODO: P->AnalysisImpls.push_back(std::make_pair(*I, Impl));
640 /// Find the pass that implements Analysis AID. If desired pass is not found
641 /// then return NULL.
642 Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
644 // Check if AvailableAnalysis map has one entry.
645 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
647 if (I != AvailableAnalysis.end())
650 // Search Parents through TopLevelManager
652 return TPM->findAnalysisPass(AID);
654 // FIXME : This is expensive and requires. Need to check only managers not all passes.
655 // One solution is to collect managers in advance at TPM level.
657 for(std::vector<Pass *>::iterator I = passVectorBegin(),
658 E = passVectorEnd(); P == NULL && I!= E; ++I )
659 P = NULL; // FIXME : P = (*I)->getResolver()->getAnalysisToUpdate(AID, false /* Do not search parents again */);
664 //===----------------------------------------------------------------------===//
665 // BasicBlockPassManager_New implementation
667 /// Add pass P into PassVector and return true. If this pass is not
668 /// manageable by this manager then return false.
670 BasicBlockPassManager_New::addPass(Pass *P) {
672 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
676 // If this pass does not preserve anlysis that is used by other passes
677 // managed by this manager than it is not a suiable pass for this manager.
678 if (!manageablePass(P))
681 addPassToManager (BP);
686 /// Execute all of the passes scheduled for execution by invoking
687 /// runOnBasicBlock method. Keep track of whether any of the passes modifies
688 /// the function, and if so, return true.
690 BasicBlockPassManager_New::runOnFunction(Function &F) {
692 bool Changed = doInitialization(F);
693 initializeAnalysisInfo();
695 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
696 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
697 e = passVectorEnd(); itr != e; ++itr) {
700 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
701 Changed |= BP->runOnBasicBlock(*I);
702 removeNotPreservedAnalysis(P);
703 recordAvailableAnalysis(P);
706 return Changed | doFinalization(F);
709 // Implement doInitialization and doFinalization
710 inline bool BasicBlockPassManager_New::doInitialization(Module &M) {
711 bool Changed = false;
713 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
714 e = passVectorEnd(); itr != e; ++itr) {
716 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
717 Changed |= BP->doInitialization(M);
723 inline bool BasicBlockPassManager_New::doFinalization(Module &M) {
724 bool Changed = false;
726 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
727 e = passVectorEnd(); itr != e; ++itr) {
729 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
730 Changed |= BP->doFinalization(M);
736 inline bool BasicBlockPassManager_New::doInitialization(Function &F) {
737 bool Changed = false;
739 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
740 e = passVectorEnd(); itr != e; ++itr) {
742 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
743 Changed |= BP->doInitialization(F);
749 inline bool BasicBlockPassManager_New::doFinalization(Function &F) {
750 bool Changed = false;
752 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
753 e = passVectorEnd(); itr != e; ++itr) {
755 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
756 Changed |= BP->doFinalization(F);
763 //===----------------------------------------------------------------------===//
764 // FunctionPassManager_New implementation
766 /// Create new Function pass manager
767 FunctionPassManager_New::FunctionPassManager_New() {
768 FPM = new FunctionPassManagerImpl_New(0);
771 FunctionPassManager_New::FunctionPassManager_New(ModuleProvider *P) {
772 FPM = new FunctionPassManagerImpl_New(0);
776 /// add - Add a pass to the queue of passes to run. This passes
777 /// ownership of the Pass to the PassManager. When the
778 /// PassManager_X is destroyed, the pass will be destroyed as well, so
779 /// there is no need to delete the pass. (TODO delete passes.)
780 /// This implies that all passes MUST be allocated with 'new'.
781 void FunctionPassManager_New::add(Pass *P) {
785 /// Execute all of the passes scheduled for execution. Keep
786 /// track of whether any of the passes modifies the function, and if
788 bool FunctionPassManager_New::runOnModule(Module &M) {
789 return FPM->runOnModule(M);
792 /// run - Execute all of the passes scheduled for execution. Keep
793 /// track of whether any of the passes modifies the function, and if
796 bool FunctionPassManager_New::run(Function &F) {
798 if (MP->materializeFunction(&F, &errstr)) {
799 cerr << "Error reading bytecode file: " << errstr << "\n";
802 return FPM->runOnFunction(F);
806 /// doInitialization - Run all of the initializers for the function passes.
808 bool FunctionPassManager_New::doInitialization() {
809 return FPM->doInitialization(*MP->getModule());
812 /// doFinalization - Run all of the initializers for the function passes.
814 bool FunctionPassManager_New::doFinalization() {
815 return FPM->doFinalization(*MP->getModule());
818 //===----------------------------------------------------------------------===//
819 // FunctionPassManagerImpl_New implementation
821 /// Add pass P into the pass manager queue. If P is a BasicBlockPass then
822 /// either use it into active basic block pass manager or create new basic
823 /// block pass manager to handle pass P.
825 FunctionPassManagerImpl_New::addPass(Pass *P) {
827 // If P is a BasicBlockPass then use BasicBlockPassManager_New.
828 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
830 if (!activeBBPassManager || !activeBBPassManager->addPass(BP)) {
832 // If active manager exists then clear its analysis info.
833 if (activeBBPassManager)
834 activeBBPassManager->initializeAnalysisInfo();
836 // Create and add new manager
837 activeBBPassManager =
838 new BasicBlockPassManager_New(getDepth() + 1);
839 addPassToManager(activeBBPassManager, false);
841 // Add pass into new manager. This time it must succeed.
842 if (!activeBBPassManager->addPass(BP))
843 assert(0 && "Unable to add Pass");
846 if (!ForcedLastUses.empty())
847 TPM->setLastUser(ForcedLastUses, this);
852 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
856 // If this pass does not preserve anlysis that is used by other passes
857 // managed by this manager than it is not a suiable pass for this manager.
858 if (!manageablePass(P))
861 addPassToManager (FP);
863 // If active manager exists then clear its analysis info.
864 if (activeBBPassManager) {
865 activeBBPassManager->initializeAnalysisInfo();
866 activeBBPassManager = NULL;
872 /// Execute all of the passes scheduled for execution by invoking
873 /// runOnFunction method. Keep track of whether any of the passes modifies
874 /// the function, and if so, return true.
875 bool FunctionPassManagerImpl_New::runOnModule(Module &M) {
877 bool Changed = doInitialization(M);
878 initializeAnalysisInfo();
880 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
881 this->runOnFunction(*I);
883 return Changed | doFinalization(M);
886 /// Execute all of the passes scheduled for execution by invoking
887 /// runOnFunction method. Keep track of whether any of the passes modifies
888 /// the function, and if so, return true.
889 bool FunctionPassManagerImpl_New::runOnFunction(Function &F) {
891 bool Changed = false;
892 initializeAnalysisInfo();
894 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
895 e = passVectorEnd(); itr != e; ++itr) {
898 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
899 Changed |= FP->runOnFunction(F);
900 removeNotPreservedAnalysis(P);
901 recordAvailableAnalysis(P);
908 inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
909 bool Changed = false;
911 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
912 e = passVectorEnd(); itr != e; ++itr) {
915 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
916 Changed |= FP->doInitialization(M);
922 inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
923 bool Changed = false;
925 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
926 e = passVectorEnd(); itr != e; ++itr) {
929 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
930 Changed |= FP->doFinalization(M);
936 //===----------------------------------------------------------------------===//
937 // ModulePassManager implementation
939 /// Add P into pass vector if it is manageble. If P is a FunctionPass
940 /// then use FunctionPassManagerImpl_New to manage it. Return false if P
941 /// is not manageable by this manager.
943 ModulePassManager_New::addPass(Pass *P) {
945 // If P is FunctionPass then use function pass maanager.
946 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
948 if (!activeFunctionPassManager || !activeFunctionPassManager->addPass(P)) {
950 // If active manager exists then clear its analysis info.
951 if (activeFunctionPassManager)
952 activeFunctionPassManager->initializeAnalysisInfo();
954 // Create and add new manager
955 activeFunctionPassManager =
956 new FunctionPassManagerImpl_New(getDepth() + 1);
957 addPassToManager(activeFunctionPassManager, false);
959 // Add pass into new manager. This time it must succeed.
960 if (!activeFunctionPassManager->addPass(FP))
961 assert(0 && "Unable to add pass");
964 if (!ForcedLastUses.empty())
965 TPM->setLastUser(ForcedLastUses, this);
970 ModulePass *MP = dynamic_cast<ModulePass *>(P);
974 // If this pass does not preserve anlysis that is used by other passes
975 // managed by this manager than it is not a suiable pass for this manager.
976 if (!manageablePass(P))
979 addPassToManager(MP);
980 // If active manager exists then clear its analysis info.
981 if (activeFunctionPassManager) {
982 activeFunctionPassManager->initializeAnalysisInfo();
983 activeFunctionPassManager = NULL;
990 /// Execute all of the passes scheduled for execution by invoking
991 /// runOnModule method. Keep track of whether any of the passes modifies
992 /// the module, and if so, return true.
994 ModulePassManager_New::runOnModule(Module &M) {
995 bool Changed = false;
996 initializeAnalysisInfo();
998 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
999 e = passVectorEnd(); itr != e; ++itr) {
1002 ModulePass *MP = dynamic_cast<ModulePass*>(P);
1003 Changed |= MP->runOnModule(M);
1004 removeNotPreservedAnalysis(P);
1005 recordAvailableAnalysis(P);
1006 removeDeadPasses(P);
1011 //===----------------------------------------------------------------------===//
1012 // PassManagerImpl implementation
1014 // PassManager_New implementation
1015 /// Add P into active pass manager or use new module pass manager to
1017 bool PassManagerImpl_New::addPass(Pass *P) {
1019 if (!activeManager || !activeManager->addPass(P)) {
1020 activeManager = new ModulePassManager_New(getDepth() + 1);
1021 PassManagers.push_back(activeManager);
1022 return activeManager->addPass(P);
1027 /// run - Execute all of the passes scheduled for execution. Keep track of
1028 /// whether any of the passes modifies the module, and if so, return true.
1029 bool PassManagerImpl_New::run(Module &M) {
1031 bool Changed = false;
1032 for (std::vector<ModulePassManager_New *>::iterator itr = PassManagers.begin(),
1033 e = PassManagers.end(); itr != e; ++itr) {
1034 ModulePassManager_New *pm = *itr;
1035 Changed |= pm->runOnModule(M);
1040 //===----------------------------------------------------------------------===//
1041 // PassManager implementation
1043 /// Create new pass manager
1044 PassManager_New::PassManager_New() {
1045 PM = new PassManagerImpl_New(0);
1048 /// add - Add a pass to the queue of passes to run. This passes ownership of
1049 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
1050 /// will be destroyed as well, so there is no need to delete the pass. This
1051 /// implies that all passes MUST be allocated with 'new'.
1053 PassManager_New::add(Pass *P) {
1057 /// run - Execute all of the passes scheduled for execution. Keep track of
1058 /// whether any of the passes modifies the module, and if so, return true.
1060 PassManager_New::run(Module &M) {