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/Support/CommandLine.h"
17 #include "llvm/Module.h"
18 #include "llvm/ModuleProvider.h"
19 #include "llvm/Support/Streams.h"
24 //===----------------------------------------------------------------------===//
26 // The Pass Manager Infrastructure manages passes. It's responsibilities are:
28 // o Manage optimization pass execution order
29 // o Make required Analysis information available before pass P is run
30 // o Release memory occupied by dead passes
31 // o If Analysis information is dirtied by a pass then regenerate Analysis
32 // information before it is consumed by another pass.
34 // Pass Manager Infrastructure uses multipe pass managers. They are PassManager,
35 // FunctionPassManager, ModulePassManager, BasicBlockPassManager. This class
36 // hierarcy uses multiple inheritance but pass managers do not derive from
37 // another pass manager.
39 // PassManager and FunctionPassManager are two top level pass manager that
40 // represents the external interface of this entire pass manager infrastucture.
42 // Important classes :
44 // [o] class PMTopLevelManager;
46 // Two top level managers, PassManager and FunctionPassManager, derive from
47 // PMTopLevelManager. PMTopLevelManager manages information used by top level
48 // managers such as last user info.
50 // [o] class PMDataManager;
52 // PMDataManager manages information, e.g. list of available analysis info,
53 // used by a pass manager to manage execution order of passes. It also provides
54 // a place to implement common pass manager APIs. All pass managers derive from
57 // [o] class BasicBlockPassManager : public FunctionPass, public PMDataManager;
59 // BasicBlockPassManager manages BasicBlockPasses.
61 // [o] class FunctionPassManager;
63 // This is a external interface used by JIT to manage FunctionPasses. This
64 // interface relies on FunctionPassManagerImpl to do all the tasks.
66 // [o] class FunctionPassManagerImpl : public ModulePass, PMDataManager,
67 // public PMTopLevelManager;
69 // FunctionPassManagerImpl is a top level manager. It manages FunctionPasses
70 // and BasicBlockPassManagers.
72 // [o] class ModulePassManager : public Pass, public PMDataManager;
74 // ModulePassManager manages ModulePasses and FunctionPassManagerImpls.
76 // [o] class PassManager;
78 // This is a external interface used by various tools to manages passes. It
79 // relies on PassManagerImpl to do all the tasks.
81 // [o] class PassManagerImpl : public Pass, public PMDataManager,
82 // public PMDTopLevelManager
84 // PassManagerImpl is a top level pass manager responsible for managing
85 // ModulePassManagers.
86 //===----------------------------------------------------------------------===//
90 //===----------------------------------------------------------------------===//
91 // Pass debugging information. Often it is useful to find out what pass is
92 // running when a crash occurs in a utility. When this library is compiled with
93 // debugging on, a command line option (--debug-pass) is enabled that causes the
94 // pass name to be printed before it executes.
97 static cl::opt<enum PassDebugLevel>
98 PassDebugging_New("debug-pass", cl::Hidden,
99 cl::desc("Print PassManager debugging information"),
101 clEnumVal(PDLNone , "disable debug output"),
102 clEnumVal(PDLArguments , "print pass arguments to pass to 'opt'"),
103 clEnumVal(PDLStructure , "print pass structure before run()"),
104 clEnumVal(PDLExecutions, "print pass name before it is executed"),
105 clEnumVal(PDLDetails , "print pass details when it is executed"),
107 } // End of llvm namespace
109 #ifndef USE_OLD_PASSMANAGER
114 //===----------------------------------------------------------------------===//
117 /// PMTopLevelManager manages LastUser info and collects common APIs used by
118 /// top level pass managers.
119 class PMTopLevelManager {
123 inline std::vector<Pass *>::iterator passManagersBegin() {
124 return PassManagers.begin();
127 inline std::vector<Pass *>::iterator passManagersEnd() {
128 return PassManagers.end();
131 /// Schedule pass P for execution. Make sure that passes required by
132 /// P are run before P is run. Update analysis info maintained by
133 /// the manager. Remove dead passes. This is a recursive function.
134 void schedulePass(Pass *P);
136 /// This is implemented by top level pass manager and used by
137 /// schedulePass() to add analysis info passes that are not available.
138 virtual void addTopLevelPass(Pass *P) = 0;
140 /// Set pass P as the last user of the given analysis passes.
141 void setLastUser(std::vector<Pass *> &AnalysisPasses, Pass *P);
143 /// Collect passes whose last user is P
144 void collectLastUses(std::vector<Pass *> &LastUses, Pass *P);
146 /// Find the pass that implements Analysis AID. Search immutable
147 /// passes and all pass managers. If desired pass is not found
148 /// then return NULL.
149 Pass *findAnalysisPass(AnalysisID AID);
151 inline void clearManagers() {
152 PassManagers.clear();
155 virtual ~PMTopLevelManager() {
157 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
158 E = PassManagers.end(); I != E; ++I)
161 for (std::vector<ImmutablePass *>::iterator
162 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
165 PassManagers.clear();
168 /// Add immutable pass and initialize it.
169 inline void addImmutablePass(ImmutablePass *P) {
171 ImmutablePasses.push_back(P);
174 inline std::vector<ImmutablePass *>& getImmutablePasses() {
175 return ImmutablePasses;
178 void addPassManager(Pass *Manager) {
179 PassManagers.push_back(Manager);
182 // Add Manager into the list of managers that are not directly
183 // maintained by this top level pass manager
184 inline void addIndirectPassManager(PMDataManager *Manager) {
185 IndirectPassManagers.push_back(Manager);
188 // Print passes managed by this top level manager.
193 /// Collection of pass managers
194 std::vector<Pass *> PassManagers;
196 /// Collection of pass managers that are not directly maintained
197 /// by this pass manager
198 std::vector<PMDataManager *> IndirectPassManagers;
200 // Map to keep track of last user of the analysis pass.
201 // LastUser->second is the last user of Lastuser->first.
202 std::map<Pass *, Pass *> LastUser;
204 /// Immutable passes are managed by top level manager.
205 std::vector<ImmutablePass *> ImmutablePasses;
208 //===----------------------------------------------------------------------===//
211 /// PMDataManager provides the common place to manage the analysis data
212 /// used by pass managers.
213 class PMDataManager {
217 PMDataManager(int D) : TPM(NULL), Depth(D) {
218 initializeAnalysisInfo();
221 virtual ~PMDataManager() {
223 for (std::vector<Pass *>::iterator I = PassVector.begin(),
224 E = PassVector.end(); I != E; ++I)
230 /// Return true IFF pass P's required analysis set does not required new
232 bool manageablePass(Pass *P);
234 /// Augment AvailableAnalysis by adding analysis made available by pass P.
235 void recordAvailableAnalysis(Pass *P);
237 /// Remove Analysis that is not preserved by the pass
238 void removeNotPreservedAnalysis(Pass *P);
240 /// Remove dead passes
241 void removeDeadPasses(Pass *P);
243 /// Add pass P into the PassVector. Update
244 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
245 void addPassToManager (Pass *P, bool ProcessAnalysis = true);
247 /// Initialize available analysis information.
248 void initializeAnalysisInfo() {
249 ForcedLastUses.clear();
250 AvailableAnalysis.clear();
253 /// Populate RequiredPasses with the analysis pass that are required by
255 void collectRequiredAnalysisPasses(std::vector<Pass *> &RequiredPasses,
258 /// All Required analyses should be available to the pass as it runs! Here
259 /// we fill in the AnalysisImpls member of the pass so that it can
260 /// successfully use the getAnalysis() method to retrieve the
261 /// implementations it needs.
262 void initializeAnalysisImpl(Pass *P);
264 /// Find the pass that implements Analysis AID. If desired pass is not found
265 /// then return NULL.
266 Pass *findAnalysisPass(AnalysisID AID, bool Direction);
268 inline std::vector<Pass *>::iterator passVectorBegin() {
269 return PassVector.begin();
272 inline std::vector<Pass *>::iterator passVectorEnd() {
273 return PassVector.end();
276 // Access toplevel manager
277 PMTopLevelManager *getTopLevelManager() { return TPM; }
278 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
280 unsigned getDepth() { return Depth; }
282 // Print list of passes that are last used by P.
283 void dumpLastUses(Pass *P, unsigned Offset) {
285 std::vector<Pass *> LUses;
287 assert (TPM && "Top Level Manager is missing");
288 TPM->collectLastUses(LUses, P);
290 for (std::vector<Pass *>::iterator I = LUses.begin(),
291 E = LUses.end(); I != E; ++I) {
292 llvm::cerr << "--" << std::string(Offset*2, ' ');
293 (*I)->dumpPassStructure(0);
299 // Collection of pass whose last user asked this manager to claim
300 // last use. If a FunctionPass F is the last user of ModulePass info M
301 // then the F's manager, not F, records itself as a last user of M.
302 std::vector<Pass *> ForcedLastUses;
304 // Top level manager.
305 PMTopLevelManager *TPM;
308 // Set of available Analysis. This information is used while scheduling
309 // pass. If a pass requires an analysis which is not not available then
310 // equired analysis pass is scheduled to run before the pass itself is
312 std::map<AnalysisID, Pass*> AvailableAnalysis;
314 // Collection of pass that are managed by this manager
315 std::vector<Pass *> PassVector;
320 //===----------------------------------------------------------------------===//
321 // BasicBlockPassManager
323 /// BasicBlockPassManager manages BasicBlockPass. It batches all the
324 /// pass together and sequence them to process one basic block before
325 /// processing next basic block.
326 class BasicBlockPassManager : public PMDataManager,
327 public FunctionPass {
330 BasicBlockPassManager(int D) : PMDataManager(D) { }
332 /// Add a pass into a passmanager queue.
333 bool addPass(Pass *p);
335 /// Execute all of the passes scheduled for execution. Keep track of
336 /// whether any of the passes modifies the function, and if so, return true.
337 bool runOnFunction(Function &F);
339 /// Pass Manager itself does not invalidate any analysis info.
340 void getAnalysisUsage(AnalysisUsage &Info) const {
341 Info.setPreservesAll();
344 bool doInitialization(Module &M);
345 bool doInitialization(Function &F);
346 bool doFinalization(Module &M);
347 bool doFinalization(Function &F);
349 // Print passes managed by this manager
350 void dumpPassStructure(unsigned Offset) {
351 llvm::cerr << std::string(Offset*2, ' ') << "BasicBLockPass Manager\n";
352 for (std::vector<Pass *>::iterator I = passVectorBegin(),
353 E = passVectorEnd(); I != E; ++I) {
354 (*I)->dumpPassStructure(Offset + 1);
355 dumpLastUses(*I, Offset+1);
361 //===----------------------------------------------------------------------===//
362 // FunctionPassManagerImpl_New
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(int D) : PMDataManager(D) {
373 activeBBPassManager = NULL;
375 ~FunctionPassManagerImpl_New() { /* TODO */ };
377 inline void addTopLevelPass(Pass *P) {
379 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
381 // P is a immutable pass then it will be managed by this
382 // top level manager. Set up analysis resolver to connect them.
383 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
385 initializeAnalysisImpl(P);
386 addImmutablePass(IP);
387 recordAvailableAnalysis(IP);
393 /// add - Add a pass to the queue of passes to run. This passes
394 /// ownership of the Pass to the PassManager. When the
395 /// PassManager_X is destroyed, the pass will be destroyed as well, so
396 /// there is no need to delete the pass. (TODO delete passes.)
397 /// This implies that all passes MUST be allocated with 'new'.
402 /// Add pass into the pass manager queue.
403 bool addPass(Pass *P);
405 /// Execute all of the passes scheduled for execution. Keep
406 /// track of whether any of the passes modifies the function, and if
408 bool runOnModule(Module &M);
409 bool runOnFunction(Function &F);
410 bool run(Function &F);
412 /// doInitialization - Run all of the initializers for the function passes.
414 bool doInitialization(Module &M);
416 /// doFinalization - Run all of the initializers for the function passes.
418 bool doFinalization(Module &M);
420 /// Pass Manager itself does not invalidate any analysis info.
421 void getAnalysisUsage(AnalysisUsage &Info) const {
422 Info.setPreservesAll();
425 // Print passes managed by this manager
426 void dumpPassStructure(unsigned Offset) {
427 llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
428 for (std::vector<Pass *>::iterator I = passVectorBegin(),
429 E = passVectorEnd(); I != E; ++I) {
430 (*I)->dumpPassStructure(Offset + 1);
431 dumpLastUses(*I, Offset+1);
436 // Active Pass Managers
437 BasicBlockPassManager *activeBBPassManager;
440 //===----------------------------------------------------------------------===//
443 /// ModulePassManager manages ModulePasses and function pass managers.
444 /// It batches all Module passes passes and function pass managers together and
445 /// sequence them to process one module.
446 class ModulePassManager : public Pass,
447 public PMDataManager {
450 ModulePassManager(int D) : PMDataManager(D) {
451 activeFunctionPassManager = NULL;
454 /// Add a pass into a passmanager queue.
455 bool addPass(Pass *p);
457 /// run - Execute all of the passes scheduled for execution. Keep track of
458 /// whether any of the passes modifies the module, and if so, return true.
459 bool runOnModule(Module &M);
461 /// Pass Manager itself does not invalidate any analysis info.
462 void getAnalysisUsage(AnalysisUsage &Info) const {
463 Info.setPreservesAll();
466 // Print passes managed by this manager
467 void dumpPassStructure(unsigned Offset) {
468 llvm::cerr << std::string(Offset*2, ' ') << "ModulePass Manager\n";
469 for (std::vector<Pass *>::iterator I = passVectorBegin(),
470 E = passVectorEnd(); I != E; ++I) {
471 (*I)->dumpPassStructure(Offset + 1);
472 dumpLastUses(*I, Offset+1);
477 // Active Pass Manager
478 FunctionPassManagerImpl_New *activeFunctionPassManager;
481 //===----------------------------------------------------------------------===//
482 // PassManagerImpl_New
484 /// PassManagerImpl_New manages ModulePassManagers
485 class PassManagerImpl_New : public Pass,
486 public PMDataManager,
487 public PMTopLevelManager {
491 PassManagerImpl_New(int D) : PMDataManager(D) {
492 activeManager = NULL;
495 /// add - Add a pass to the queue of passes to run. This passes ownership of
496 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
497 /// will be destroyed as well, so there is no need to delete the pass. This
498 /// implies that all passes MUST be allocated with 'new'.
503 /// run - Execute all of the passes scheduled for execution. Keep track of
504 /// whether any of the passes modifies the module, and if so, return true.
507 /// Pass Manager itself does not invalidate any analysis info.
508 void getAnalysisUsage(AnalysisUsage &Info) const {
509 Info.setPreservesAll();
512 inline void addTopLevelPass(Pass *P) {
514 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
516 // P is a immutable pass and it will be managed by this
517 // top level manager. Set up analysis resolver to connect them.
518 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
520 initializeAnalysisImpl(P);
521 addImmutablePass(IP);
522 recordAvailableAnalysis(IP);
530 /// Add a pass into a passmanager queue.
531 bool addPass(Pass *p);
533 // Active Pass Manager
534 ModulePassManager *activeManager;
537 } // End of llvm namespace
539 //===----------------------------------------------------------------------===//
540 // PMTopLevelManager implementation
542 /// Set pass P as the last user of the given analysis passes.
543 void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses,
546 for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(),
547 E = AnalysisPasses.end(); I != E; ++I) {
550 // If AP is the last user of other passes then make P last user of
552 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
553 LUE = LastUser.end(); LUI != LUE; ++LUI) {
554 if (LUI->second == AP)
555 LastUser[LUI->first] = P;
561 /// Collect passes whose last user is P
562 void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses,
564 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
565 LUE = LastUser.end(); LUI != LUE; ++LUI)
566 if (LUI->second == P)
567 LastUses.push_back(LUI->first);
570 /// Schedule pass P for execution. Make sure that passes required by
571 /// P are run before P is run. Update analysis info maintained by
572 /// the manager. Remove dead passes. This is a recursive function.
573 void PMTopLevelManager::schedulePass(Pass *P) {
575 // TODO : Allocate function manager for this pass, other wise required set
576 // may be inserted into previous function manager
578 AnalysisUsage AnUsage;
579 P->getAnalysisUsage(AnUsage);
580 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
581 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
582 E = RequiredSet.end(); I != E; ++I) {
584 Pass *AnalysisPass = findAnalysisPass(*I);
586 // Schedule this analysis run first.
587 AnalysisPass = (*I)->createPass();
588 schedulePass(AnalysisPass);
592 // Now all required passes are available.
596 /// Find the pass that implements Analysis AID. Search immutable
597 /// passes and all pass managers. If desired pass is not found
598 /// then return NULL.
599 Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
602 // Check pass managers
603 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
604 E = PassManagers.end(); P == NULL && I != E; ++I) {
605 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
606 assert(PMD && "This is not a PassManager");
607 P = PMD->findAnalysisPass(AID, false);
610 // Check other pass managers
611 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
612 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
613 P = (*I)->findAnalysisPass(AID, false);
615 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
616 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
617 const PassInfo *PI = (*I)->getPassInfo();
621 // If Pass not found then check the interfaces implemented by Immutable Pass
623 const std::vector<const PassInfo*> &ImmPI =
624 PI->getInterfacesImplemented();
625 for (unsigned Index = 0, End = ImmPI.size();
626 P == NULL && Index != End; ++Index)
627 if (ImmPI[Index] == AID)
635 // Print passes managed by this top level manager.
636 void PMTopLevelManager::dumpPasses() {
638 // Print out the immutable passes
639 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
640 ImmutablePasses[i]->dumpPassStructure(0);
643 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
644 E = PassManagers.end(); I != E; ++I)
645 (*I)->dumpPassStructure(1);
649 //===----------------------------------------------------------------------===//
650 // PMDataManager implementation
652 /// Return true IFF pass P's required analysis set does not required new
654 bool PMDataManager::manageablePass(Pass *P) {
657 // If this pass is not preserving information that is required by a
658 // pass maintained by higher level pass manager then do not insert
659 // this pass into current manager. Use new manager. For example,
660 // For example, If FunctionPass F is not preserving ModulePass Info M1
661 // that is used by another ModulePass M2 then do not insert F in
662 // current function pass manager.
666 /// Augement AvailableAnalysis by adding analysis made available by pass P.
667 void PMDataManager::recordAvailableAnalysis(Pass *P) {
669 if (const PassInfo *PI = P->getPassInfo()) {
670 AvailableAnalysis[PI] = P;
672 //This pass is the current implementation of all of the interfaces it
673 //implements as well.
674 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
675 for (unsigned i = 0, e = II.size(); i != e; ++i)
676 AvailableAnalysis[II[i]] = P;
680 /// Remove Analyss not preserved by Pass P
681 void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
682 AnalysisUsage AnUsage;
683 P->getAnalysisUsage(AnUsage);
685 if (AnUsage.getPreservesAll())
688 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
689 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
690 E = AvailableAnalysis.end(); I != E; ) {
691 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
692 PreservedSet.end()) {
693 // Remove this analysis
694 if (!dynamic_cast<ImmutablePass*>(I->second)) {
695 std::map<AnalysisID, Pass*>::iterator J = I++;
696 AvailableAnalysis.erase(J);
704 /// Remove analysis passes that are not used any longer
705 void PMDataManager::removeDeadPasses(Pass *P) {
707 std::vector<Pass *> DeadPasses;
708 TPM->collectLastUses(DeadPasses, P);
710 for (std::vector<Pass *>::iterator I = DeadPasses.begin(),
711 E = DeadPasses.end(); I != E; ++I) {
712 (*I)->releaseMemory();
714 std::map<AnalysisID, Pass*>::iterator Pos =
715 AvailableAnalysis.find((*I)->getPassInfo());
717 // It is possible that pass is already removed from the AvailableAnalysis
718 if (Pos != AvailableAnalysis.end())
719 AvailableAnalysis.erase(Pos);
723 /// Add pass P into the PassVector. Update
724 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
725 void PMDataManager::addPassToManager(Pass *P,
726 bool ProcessAnalysis) {
728 // This manager is going to manage pass P. Set up analysis resolver
730 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
733 if (ProcessAnalysis) {
735 // At the moment, this pass is the last user of all required passes.
736 std::vector<Pass *> LastUses;
737 std::vector<Pass *> RequiredPasses;
738 unsigned PDepth = this->getDepth();
740 collectRequiredAnalysisPasses(RequiredPasses, P);
741 for (std::vector<Pass *>::iterator I = RequiredPasses.begin(),
742 E = RequiredPasses.end(); I != E; ++I) {
743 Pass *PRequired = *I;
746 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
747 RDepth = DM.getDepth();
749 if (PDepth == RDepth)
750 LastUses.push_back(PRequired);
751 else if (PDepth > RDepth) {
752 // Let the parent claim responsibility of last use
753 ForcedLastUses.push_back(PRequired);
755 // Note : This feature is not yet implemented
757 "Unable to handle Pass that requires lower level Analysis pass");
761 if (!LastUses.empty())
762 TPM->setLastUser(LastUses, P);
764 // Take a note of analysis required and made available by this pass.
765 // Remove the analysis not preserved by this pass
766 removeNotPreservedAnalysis(P);
767 recordAvailableAnalysis(P);
771 PassVector.push_back(P);
774 /// Populate RequiredPasses with the analysis pass that are required by
776 void PMDataManager::collectRequiredAnalysisPasses(std::vector<Pass *> &RP,
778 AnalysisUsage AnUsage;
779 P->getAnalysisUsage(AnUsage);
780 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
781 for (std::vector<AnalysisID>::const_iterator
782 I = RequiredSet.begin(), E = RequiredSet.end();
784 Pass *AnalysisPass = findAnalysisPass(*I, true);
785 assert (AnalysisPass && "Analysis pass is not available");
786 RP.push_back(AnalysisPass);
789 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
790 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
791 E = IDs.end(); I != E; ++I) {
792 Pass *AnalysisPass = findAnalysisPass(*I, true);
793 assert (AnalysisPass && "Analysis pass is not available");
794 RP.push_back(AnalysisPass);
798 // All Required analyses should be available to the pass as it runs! Here
799 // we fill in the AnalysisImpls member of the pass so that it can
800 // successfully use the getAnalysis() method to retrieve the
801 // implementations it needs.
803 void PMDataManager::initializeAnalysisImpl(Pass *P) {
804 AnalysisUsage AnUsage;
805 P->getAnalysisUsage(AnUsage);
807 for (std::vector<const PassInfo *>::const_iterator
808 I = AnUsage.getRequiredSet().begin(),
809 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
810 Pass *Impl = findAnalysisPass(*I, true);
812 assert(0 && "Analysis used but not available!");
813 AnalysisResolver_New *AR = P->getResolver();
814 AR->addAnalysisImplsPair(*I, Impl);
818 /// Find the pass that implements Analysis AID. If desired pass is not found
819 /// then return NULL.
820 Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
822 // Check if AvailableAnalysis map has one entry.
823 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
825 if (I != AvailableAnalysis.end())
828 // Search Parents through TopLevelManager
830 return TPM->findAnalysisPass(AID);
836 //===----------------------------------------------------------------------===//
837 // NOTE: Is this the right place to define this method ?
838 // getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
839 Pass *AnalysisResolver_New::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
840 return PM.findAnalysisPass(ID, dir);
843 //===----------------------------------------------------------------------===//
844 // BasicBlockPassManager implementation
846 /// Add pass P into PassVector and return true. If this pass is not
847 /// manageable by this manager then return false.
849 BasicBlockPassManager::addPass(Pass *P) {
851 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
855 // If this pass does not preserve anlysis that is used by other passes
856 // managed by this manager than it is not a suiable pass for this manager.
857 if (!manageablePass(P))
860 addPassToManager (BP);
865 /// Execute all of the passes scheduled for execution by invoking
866 /// runOnBasicBlock method. Keep track of whether any of the passes modifies
867 /// the function, and if so, return true.
869 BasicBlockPassManager::runOnFunction(Function &F) {
874 bool Changed = doInitialization(F);
875 initializeAnalysisInfo();
877 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
878 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
879 e = passVectorEnd(); itr != e; ++itr) {
881 initializeAnalysisImpl(P);
882 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
883 Changed |= BP->runOnBasicBlock(*I);
884 removeNotPreservedAnalysis(P);
885 recordAvailableAnalysis(P);
888 return Changed | doFinalization(F);
891 // Implement doInitialization and doFinalization
892 inline bool BasicBlockPassManager::doInitialization(Module &M) {
893 bool Changed = false;
895 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
896 e = passVectorEnd(); itr != e; ++itr) {
898 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
899 Changed |= BP->doInitialization(M);
905 inline bool BasicBlockPassManager::doFinalization(Module &M) {
906 bool Changed = false;
908 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
909 e = passVectorEnd(); itr != e; ++itr) {
911 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
912 Changed |= BP->doFinalization(M);
918 inline bool BasicBlockPassManager::doInitialization(Function &F) {
919 bool Changed = false;
921 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
922 e = passVectorEnd(); itr != e; ++itr) {
924 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
925 Changed |= BP->doInitialization(F);
931 inline bool BasicBlockPassManager::doFinalization(Function &F) {
932 bool Changed = false;
934 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
935 e = passVectorEnd(); itr != e; ++itr) {
937 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
938 Changed |= BP->doFinalization(F);
945 //===----------------------------------------------------------------------===//
946 // FunctionPassManager implementation
948 /// Create new Function pass manager
949 FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
950 FPM = new FunctionPassManagerImpl_New(0);
951 // FPM is the top level manager.
952 FPM->setTopLevelManager(FPM);
954 PMDataManager *PMD = dynamic_cast<PMDataManager *>(FPM);
955 AnalysisResolver_New *AR = new AnalysisResolver_New(*PMD);
956 FPM->setResolver(AR);
958 FPM->addPassManager(FPM);
962 FunctionPassManager::~FunctionPassManager() {
963 // Note : FPM maintains one entry in PassManagers vector.
964 // This one entry is FPM itself. This is not ideal. One
965 // alternative is have one additional layer between
966 // FunctionPassManager and FunctionPassManagerImpl.
967 // Meanwhile, to avoid going into infinte loop, first
968 // remove FPM from its PassMangers vector.
969 FPM->clearManagers();
973 /// add - Add a pass to the queue of passes to run. This passes
974 /// ownership of the Pass to the PassManager. When the
975 /// PassManager_X is destroyed, the pass will be destroyed as well, so
976 /// there is no need to delete the pass. (TODO delete passes.)
977 /// This implies that all passes MUST be allocated with 'new'.
978 void FunctionPassManager::add(Pass *P) {
982 /// run - Execute all of the passes scheduled for execution. Keep
983 /// track of whether any of the passes modifies the function, and if
986 bool FunctionPassManager::run(Function &F) {
988 if (MP->materializeFunction(&F, &errstr)) {
989 cerr << "Error reading bytecode file: " << errstr << "\n";
996 /// doInitialization - Run all of the initializers for the function passes.
998 bool FunctionPassManager::doInitialization() {
999 return FPM->doInitialization(*MP->getModule());
1002 /// doFinalization - Run all of the initializers for the function passes.
1004 bool FunctionPassManager::doFinalization() {
1005 return FPM->doFinalization(*MP->getModule());
1008 //===----------------------------------------------------------------------===//
1009 // FunctionPassManagerImpl_New implementation
1011 /// Add pass P into the pass manager queue. If P is a BasicBlockPass then
1012 /// either use it into active basic block pass manager or create new basic
1013 /// block pass manager to handle pass P.
1015 FunctionPassManagerImpl_New::addPass(Pass *P) {
1017 // If P is a BasicBlockPass then use BasicBlockPassManager.
1018 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
1020 if (!activeBBPassManager || !activeBBPassManager->addPass(BP)) {
1022 // If active manager exists then clear its analysis info.
1023 if (activeBBPassManager)
1024 activeBBPassManager->initializeAnalysisInfo();
1026 // Create and add new manager
1027 activeBBPassManager =
1028 new BasicBlockPassManager(getDepth() + 1);
1029 // Inherit top level manager
1030 activeBBPassManager->setTopLevelManager(this->getTopLevelManager());
1032 // Add new manager into current manager's list.
1033 addPassToManager(activeBBPassManager, false);
1035 // Add new manager into top level manager's indirect passes list
1036 PMDataManager *PMD = dynamic_cast<PMDataManager *>(activeBBPassManager);
1037 assert (PMD && "Manager is not Pass Manager");
1038 TPM->addIndirectPassManager(PMD);
1040 // Add pass into new manager. This time it must succeed.
1041 if (!activeBBPassManager->addPass(BP))
1042 assert(0 && "Unable to add Pass");
1045 if (!ForcedLastUses.empty())
1046 TPM->setLastUser(ForcedLastUses, this);
1051 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
1055 // If this pass does not preserve anlysis that is used by other passes
1056 // managed by this manager than it is not a suiable pass for this manager.
1057 if (!manageablePass(P))
1060 addPassToManager (FP);
1062 // If active manager exists then clear its analysis info.
1063 if (activeBBPassManager) {
1064 activeBBPassManager->initializeAnalysisInfo();
1065 activeBBPassManager = NULL;
1071 /// Execute all of the passes scheduled for execution by invoking
1072 /// runOnFunction method. Keep track of whether any of the passes modifies
1073 /// the function, and if so, return true.
1074 bool FunctionPassManagerImpl_New::runOnModule(Module &M) {
1076 bool Changed = doInitialization(M);
1077 initializeAnalysisInfo();
1079 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1080 this->runOnFunction(*I);
1082 return Changed | doFinalization(M);
1085 /// Execute all of the passes scheduled for execution by invoking
1086 /// runOnFunction method. Keep track of whether any of the passes modifies
1087 /// the function, and if so, return true.
1088 bool FunctionPassManagerImpl_New::runOnFunction(Function &F) {
1090 bool Changed = false;
1095 initializeAnalysisInfo();
1097 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1098 e = passVectorEnd(); itr != e; ++itr) {
1100 initializeAnalysisImpl(P);
1101 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
1102 Changed |= FP->runOnFunction(F);
1103 removeNotPreservedAnalysis(P);
1104 recordAvailableAnalysis(P);
1105 removeDeadPasses(P);
1111 inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
1112 bool Changed = false;
1114 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1115 e = passVectorEnd(); itr != e; ++itr) {
1118 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
1119 Changed |= FP->doInitialization(M);
1125 inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
1126 bool Changed = false;
1128 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1129 e = passVectorEnd(); itr != e; ++itr) {
1132 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
1133 Changed |= FP->doFinalization(M);
1139 // Execute all the passes managed by this top level manager.
1140 // Return true if any function is modified by a pass.
1141 bool FunctionPassManagerImpl_New::run(Function &F) {
1143 bool Changed = false;
1144 for (std::vector<Pass *>::iterator I = passManagersBegin(),
1145 E = passManagersEnd(); I != E; ++I) {
1146 FunctionPassManagerImpl_New *FP =
1147 dynamic_cast<FunctionPassManagerImpl_New *>(*I);
1148 Changed |= FP->runOnFunction(F);
1153 //===----------------------------------------------------------------------===//
1154 // ModulePassManager implementation
1156 /// Add P into pass vector if it is manageble. If P is a FunctionPass
1157 /// then use FunctionPassManagerImpl_New to manage it. Return false if P
1158 /// is not manageable by this manager.
1160 ModulePassManager::addPass(Pass *P) {
1162 // If P is FunctionPass then use function pass maanager.
1163 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
1165 if (!activeFunctionPassManager || !activeFunctionPassManager->addPass(P)) {
1167 // If active manager exists then clear its analysis info.
1168 if (activeFunctionPassManager)
1169 activeFunctionPassManager->initializeAnalysisInfo();
1171 // Create and add new manager
1172 activeFunctionPassManager =
1173 new FunctionPassManagerImpl_New(getDepth() + 1);
1175 // Add new manager into current manager's list
1176 addPassToManager(activeFunctionPassManager, false);
1178 // Inherit top level manager
1179 activeFunctionPassManager->setTopLevelManager(this->getTopLevelManager());
1181 // Add new manager into top level manager's indirect passes list
1182 PMDataManager *PMD = dynamic_cast<PMDataManager *>(activeFunctionPassManager);
1183 assert (PMD && "Manager is not Pass Manager");
1184 TPM->addIndirectPassManager(PMD);
1186 // Add pass into new manager. This time it must succeed.
1187 if (!activeFunctionPassManager->addPass(FP))
1188 assert(0 && "Unable to add pass");
1191 if (!ForcedLastUses.empty())
1192 TPM->setLastUser(ForcedLastUses, this);
1197 ModulePass *MP = dynamic_cast<ModulePass *>(P);
1201 // If this pass does not preserve anlysis that is used by other passes
1202 // managed by this manager than it is not a suiable pass for this manager.
1203 if (!manageablePass(P))
1206 addPassToManager(MP);
1207 // If active manager exists then clear its analysis info.
1208 if (activeFunctionPassManager) {
1209 activeFunctionPassManager->initializeAnalysisInfo();
1210 activeFunctionPassManager = NULL;
1217 /// Execute all of the passes scheduled for execution by invoking
1218 /// runOnModule method. Keep track of whether any of the passes modifies
1219 /// the module, and if so, return true.
1221 ModulePassManager::runOnModule(Module &M) {
1222 bool Changed = false;
1223 initializeAnalysisInfo();
1225 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1226 e = passVectorEnd(); itr != e; ++itr) {
1228 initializeAnalysisImpl(P);
1229 ModulePass *MP = dynamic_cast<ModulePass*>(P);
1230 Changed |= MP->runOnModule(M);
1231 removeNotPreservedAnalysis(P);
1232 recordAvailableAnalysis(P);
1233 removeDeadPasses(P);
1238 //===----------------------------------------------------------------------===//
1239 // PassManagerImpl implementation
1241 /// Add P into active pass manager or use new module pass manager to
1243 bool PassManagerImpl_New::addPass(Pass *P) {
1245 if (!activeManager || !activeManager->addPass(P)) {
1246 activeManager = new ModulePassManager(getDepth() + 1);
1247 // Inherit top level manager
1248 activeManager->setTopLevelManager(this->getTopLevelManager());
1250 // This top level manager is going to manage activeManager.
1251 // Set up analysis resolver to connect them.
1252 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
1253 activeManager->setResolver(AR);
1255 addPassManager(activeManager);
1256 return activeManager->addPass(P);
1261 /// run - Execute all of the passes scheduled for execution. Keep track of
1262 /// whether any of the passes modifies the module, and if so, return true.
1263 bool PassManagerImpl_New::run(Module &M) {
1265 bool Changed = false;
1267 if (PassDebugging_New >= PDLStructure)
1270 for (std::vector<Pass *>::iterator I = passManagersBegin(),
1271 E = passManagersEnd(); I != E; ++I) {
1272 ModulePassManager *MP = dynamic_cast<ModulePassManager *>(*I);
1273 Changed |= MP->runOnModule(M);
1278 //===----------------------------------------------------------------------===//
1279 // PassManager implementation
1281 /// Create new pass manager
1282 PassManager::PassManager() {
1283 PM = new PassManagerImpl_New(0);
1284 // PM is the top level manager
1285 PM->setTopLevelManager(PM);
1288 PassManager::~PassManager() {
1292 /// add - Add a pass to the queue of passes to run. This passes ownership of
1293 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
1294 /// will be destroyed as well, so there is no need to delete the pass. This
1295 /// implies that all passes MUST be allocated with 'new'.
1297 PassManager::add(Pass *P) {
1301 /// run - Execute all of the passes scheduled for execution. Keep track of
1302 /// whether any of the passes modifies the module, and if so, return true.
1304 PassManager::run(Module &M) {