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 // Different debug levels that can be enabled...
99 None, Arguments, Structure, Executions, Details
102 static cl::opt<enum PassDebugLevel>
103 PassDebugging_New("debug-pass", cl::Hidden,
104 cl::desc("Print PassManager debugging information"),
106 clEnumVal(None , "disable debug output"),
107 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
108 clEnumVal(Structure , "print pass structure before run()"),
109 clEnumVal(Executions, "print pass name before it is executed"),
110 clEnumVal(Details , "print pass details when it is executed"),
112 } // End of llvm namespace
114 #ifndef USE_OLD_PASSMANAGER
119 //===----------------------------------------------------------------------===//
122 /// PMTopLevelManager manages LastUser info and collects common APIs used by
123 /// top level pass managers.
124 class PMTopLevelManager {
128 inline std::vector<Pass *>::iterator passManagersBegin() {
129 return PassManagers.begin();
132 inline std::vector<Pass *>::iterator passManagersEnd() {
133 return PassManagers.end();
136 /// Schedule pass P for execution. Make sure that passes required by
137 /// P are run before P is run. Update analysis info maintained by
138 /// the manager. Remove dead passes. This is a recursive function.
139 void schedulePass(Pass *P);
141 /// This is implemented by top level pass manager and used by
142 /// schedulePass() to add analysis info passes that are not available.
143 virtual void addTopLevelPass(Pass *P) = 0;
145 /// Set pass P as the last user of the given analysis passes.
146 void setLastUser(std::vector<Pass *> &AnalysisPasses, Pass *P);
148 /// Collect passes whose last user is P
149 void collectLastUses(std::vector<Pass *> &LastUses, Pass *P);
151 /// Find the pass that implements Analysis AID. Search immutable
152 /// passes and all pass managers. If desired pass is not found
153 /// then return NULL.
154 Pass *findAnalysisPass(AnalysisID AID);
156 inline void clearManagers() {
157 PassManagers.clear();
160 virtual ~PMTopLevelManager() {
162 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
163 E = PassManagers.end(); I != E; ++I)
166 for (std::vector<ImmutablePass *>::iterator
167 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
170 PassManagers.clear();
173 /// Add immutable pass and initialize it.
174 inline void addImmutablePass(ImmutablePass *P) {
176 ImmutablePasses.push_back(P);
179 inline std::vector<ImmutablePass *>& getImmutablePasses() {
180 return ImmutablePasses;
183 void addPassManager(Pass *Manager) {
184 PassManagers.push_back(Manager);
187 // Add Manager into the list of managers that are not directly
188 // maintained by this top level pass manager
189 inline void addIndirectPassManager(PMDataManager *Manager) {
190 IndirectPassManagers.push_back(Manager);
193 // Print passes managed by this top level manager.
195 void dumpArguments();
199 /// Collection of pass managers
200 std::vector<Pass *> PassManagers;
202 /// Collection of pass managers that are not directly maintained
203 /// by this pass manager
204 std::vector<PMDataManager *> IndirectPassManagers;
206 // Map to keep track of last user of the analysis pass.
207 // LastUser->second is the last user of Lastuser->first.
208 std::map<Pass *, Pass *> LastUser;
210 /// Immutable passes are managed by top level manager.
211 std::vector<ImmutablePass *> ImmutablePasses;
214 //===----------------------------------------------------------------------===//
217 /// PMDataManager provides the common place to manage the analysis data
218 /// used by pass managers.
219 class PMDataManager {
223 PMDataManager(int D) : TPM(NULL), Depth(D) {
224 initializeAnalysisInfo();
227 virtual ~PMDataManager() {
229 for (std::vector<Pass *>::iterator I = PassVector.begin(),
230 E = PassVector.end(); I != E; ++I)
236 /// Return true IFF pass P's required analysis set does not required new
238 bool manageablePass(Pass *P);
240 /// Augment AvailableAnalysis by adding analysis made available by pass P.
241 void recordAvailableAnalysis(Pass *P);
243 /// Remove Analysis that is not preserved by the pass
244 void removeNotPreservedAnalysis(Pass *P);
246 /// Remove dead passes
247 void removeDeadPasses(Pass *P, std::string &Msg);
249 /// Add pass P into the PassVector. Update
250 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
251 void addPassToManager (Pass *P, bool ProcessAnalysis = true);
253 /// Initialize available analysis information.
254 void initializeAnalysisInfo() {
255 ForcedLastUses.clear();
256 AvailableAnalysis.clear();
259 /// Populate RequiredPasses with the analysis pass that are required by
261 void collectRequiredAnalysisPasses(std::vector<Pass *> &RequiredPasses,
264 /// All Required analyses should be available to the pass as it runs! Here
265 /// we fill in the AnalysisImpls member of the pass so that it can
266 /// successfully use the getAnalysis() method to retrieve the
267 /// implementations it needs.
268 void initializeAnalysisImpl(Pass *P);
270 /// Find the pass that implements Analysis AID. If desired pass is not found
271 /// then return NULL.
272 Pass *findAnalysisPass(AnalysisID AID, bool Direction);
274 inline std::vector<Pass *>::iterator passVectorBegin() {
275 return PassVector.begin();
278 inline std::vector<Pass *>::iterator passVectorEnd() {
279 return PassVector.end();
282 // Access toplevel manager
283 PMTopLevelManager *getTopLevelManager() { return TPM; }
284 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
286 unsigned getDepth() { return Depth; }
288 // Print list of passes that are last used by P.
289 void dumpLastUses(Pass *P, unsigned Offset) {
291 std::vector<Pass *> LUses;
293 assert (TPM && "Top Level Manager is missing");
294 TPM->collectLastUses(LUses, P);
296 for (std::vector<Pass *>::iterator I = LUses.begin(),
297 E = LUses.end(); I != E; ++I) {
298 llvm::cerr << "--" << std::string(Offset*2, ' ');
299 (*I)->dumpPassStructure(0);
303 void dumpPassArguments() {
304 for(std::vector<Pass *>::iterator I = PassVector.begin(),
305 E = PassVector.end(); I != E; ++I) {
306 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
307 PMD->dumpPassArguments();
309 if (const PassInfo *PI = (*I)->getPassInfo())
310 if (!PI->isAnalysisGroup())
311 cerr << " -" << PI->getPassArgument();
315 void dumpPassInfo(Pass *P, std::string &Msg1, std::string &Msg2) {
316 if (PassDebugging_New < Executions)
318 cerr << (void*)this << std::string(getDepth()*2+1, ' ');
320 cerr << P->getPassName();
326 // Collection of pass whose last user asked this manager to claim
327 // last use. If a FunctionPass F is the last user of ModulePass info M
328 // then the F's manager, not F, records itself as a last user of M.
329 std::vector<Pass *> ForcedLastUses;
331 // Top level manager.
332 PMTopLevelManager *TPM;
335 // Set of available Analysis. This information is used while scheduling
336 // pass. If a pass requires an analysis which is not not available then
337 // equired analysis pass is scheduled to run before the pass itself is
339 std::map<AnalysisID, Pass*> AvailableAnalysis;
341 // Collection of pass that are managed by this manager
342 std::vector<Pass *> PassVector;
347 //===----------------------------------------------------------------------===//
348 // BasicBlockPassManager
350 /// BasicBlockPassManager manages BasicBlockPass. It batches all the
351 /// pass together and sequence them to process one basic block before
352 /// processing next basic block.
353 class BasicBlockPassManager : public PMDataManager,
354 public FunctionPass {
357 BasicBlockPassManager(int D) : PMDataManager(D) { }
359 /// Add a pass into a passmanager queue.
360 bool addPass(Pass *p);
362 /// Execute all of the passes scheduled for execution. Keep track of
363 /// whether any of the passes modifies the function, and if so, return true.
364 bool runOnFunction(Function &F);
366 /// Pass Manager itself does not invalidate any analysis info.
367 void getAnalysisUsage(AnalysisUsage &Info) const {
368 Info.setPreservesAll();
371 bool doInitialization(Module &M);
372 bool doInitialization(Function &F);
373 bool doFinalization(Module &M);
374 bool doFinalization(Function &F);
376 // Print passes managed by this manager
377 void dumpPassStructure(unsigned Offset) {
378 llvm::cerr << std::string(Offset*2, ' ') << "BasicBLockPass Manager\n";
379 for (std::vector<Pass *>::iterator I = passVectorBegin(),
380 E = passVectorEnd(); I != E; ++I) {
381 (*I)->dumpPassStructure(Offset + 1);
382 dumpLastUses(*I, Offset+1);
387 //===----------------------------------------------------------------------===//
388 // FunctionPassManagerImpl_New
390 /// FunctionPassManagerImpl_New manages FunctionPasses and
391 /// BasicBlockPassManagers. It batches all function passes and basic block pass
392 /// managers together and sequence them to process one function at a time before
393 /// processing next function.
394 class FunctionPassManagerImpl_New : public ModulePass,
395 public PMDataManager,
396 public PMTopLevelManager {
398 FunctionPassManagerImpl_New(int D) : PMDataManager(D) {
399 activeBBPassManager = NULL;
401 ~FunctionPassManagerImpl_New() { /* TODO */ };
403 inline void addTopLevelPass(Pass *P) {
405 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
407 // P is a immutable pass then it will be managed by this
408 // top level manager. Set up analysis resolver to connect them.
409 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
411 initializeAnalysisImpl(P);
412 addImmutablePass(IP);
413 recordAvailableAnalysis(IP);
419 /// add - Add a pass to the queue of passes to run. This passes
420 /// ownership of the Pass to the PassManager. When the
421 /// PassManager_X is destroyed, the pass will be destroyed as well, so
422 /// there is no need to delete the pass. (TODO delete passes.)
423 /// This implies that all passes MUST be allocated with 'new'.
428 /// Add pass into the pass manager queue.
429 bool addPass(Pass *P);
431 /// Execute all of the passes scheduled for execution. Keep
432 /// track of whether any of the passes modifies the function, and if
434 bool runOnModule(Module &M);
435 bool runOnFunction(Function &F);
436 bool run(Function &F);
438 /// doInitialization - Run all of the initializers for the function passes.
440 bool doInitialization(Module &M);
442 /// doFinalization - Run all of the initializers for the function passes.
444 bool doFinalization(Module &M);
446 /// Pass Manager itself does not invalidate any analysis info.
447 void getAnalysisUsage(AnalysisUsage &Info) const {
448 Info.setPreservesAll();
451 // Print passes managed by this manager
452 void dumpPassStructure(unsigned Offset) {
453 llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
454 for (std::vector<Pass *>::iterator I = passVectorBegin(),
455 E = passVectorEnd(); I != E; ++I) {
456 (*I)->dumpPassStructure(Offset + 1);
457 dumpLastUses(*I, Offset+1);
462 // Active Pass Managers
463 BasicBlockPassManager *activeBBPassManager;
466 //===----------------------------------------------------------------------===//
469 /// ModulePassManager manages ModulePasses and function pass managers.
470 /// It batches all Module passes passes and function pass managers together and
471 /// sequence them to process one module.
472 class ModulePassManager : public Pass,
473 public PMDataManager {
476 ModulePassManager(int D) : PMDataManager(D) {
477 activeFunctionPassManager = NULL;
480 /// Add a pass into a passmanager queue.
481 bool addPass(Pass *p);
483 /// run - Execute all of the passes scheduled for execution. Keep track of
484 /// whether any of the passes modifies the module, and if so, return true.
485 bool runOnModule(Module &M);
487 /// Pass Manager itself does not invalidate any analysis info.
488 void getAnalysisUsage(AnalysisUsage &Info) const {
489 Info.setPreservesAll();
492 // Print passes managed by this manager
493 void dumpPassStructure(unsigned Offset) {
494 llvm::cerr << std::string(Offset*2, ' ') << "ModulePass Manager\n";
495 for (std::vector<Pass *>::iterator I = passVectorBegin(),
496 E = passVectorEnd(); I != E; ++I) {
497 (*I)->dumpPassStructure(Offset + 1);
498 dumpLastUses(*I, Offset+1);
503 // Active Pass Manager
504 FunctionPassManagerImpl_New *activeFunctionPassManager;
507 //===----------------------------------------------------------------------===//
508 // PassManagerImpl_New
510 /// PassManagerImpl_New manages ModulePassManagers
511 class PassManagerImpl_New : public Pass,
512 public PMDataManager,
513 public PMTopLevelManager {
517 PassManagerImpl_New(int D) : PMDataManager(D) {
518 activeManager = NULL;
521 /// add - Add a pass to the queue of passes to run. This passes ownership of
522 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
523 /// will be destroyed as well, so there is no need to delete the pass. This
524 /// implies that all passes MUST be allocated with 'new'.
529 /// run - Execute all of the passes scheduled for execution. Keep track of
530 /// whether any of the passes modifies the module, and if so, return true.
533 /// Pass Manager itself does not invalidate any analysis info.
534 void getAnalysisUsage(AnalysisUsage &Info) const {
535 Info.setPreservesAll();
538 inline void addTopLevelPass(Pass *P) {
540 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
542 // P is a immutable pass and it will be managed by this
543 // top level manager. Set up analysis resolver to connect them.
544 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
546 initializeAnalysisImpl(P);
547 addImmutablePass(IP);
548 recordAvailableAnalysis(IP);
556 /// Add a pass into a passmanager queue.
557 bool addPass(Pass *p);
559 // Active Pass Manager
560 ModulePassManager *activeManager;
563 } // End of llvm namespace
565 //===----------------------------------------------------------------------===//
566 // PMTopLevelManager implementation
568 /// Set pass P as the last user of the given analysis passes.
569 void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses,
572 for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(),
573 E = AnalysisPasses.end(); I != E; ++I) {
576 // If AP is the last user of other passes then make P last user of
578 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
579 LUE = LastUser.end(); LUI != LUE; ++LUI) {
580 if (LUI->second == AP)
581 LastUser[LUI->first] = P;
586 /// Collect passes whose last user is P
587 void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses,
589 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
590 LUE = LastUser.end(); LUI != LUE; ++LUI)
591 if (LUI->second == P)
592 LastUses.push_back(LUI->first);
595 /// Schedule pass P for execution. Make sure that passes required by
596 /// P are run before P is run. Update analysis info maintained by
597 /// the manager. Remove dead passes. This is a recursive function.
598 void PMTopLevelManager::schedulePass(Pass *P) {
600 // TODO : Allocate function manager for this pass, other wise required set
601 // may be inserted into previous function manager
603 AnalysisUsage AnUsage;
604 P->getAnalysisUsage(AnUsage);
605 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
606 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
607 E = RequiredSet.end(); I != E; ++I) {
609 Pass *AnalysisPass = findAnalysisPass(*I);
611 // Schedule this analysis run first.
612 AnalysisPass = (*I)->createPass();
613 schedulePass(AnalysisPass);
617 // Now all required passes are available.
621 /// Find the pass that implements Analysis AID. Search immutable
622 /// passes and all pass managers. If desired pass is not found
623 /// then return NULL.
624 Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
627 // Check pass managers
628 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
629 E = PassManagers.end(); P == NULL && I != E; ++I) {
630 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
631 assert(PMD && "This is not a PassManager");
632 P = PMD->findAnalysisPass(AID, false);
635 // Check other pass managers
636 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
637 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
638 P = (*I)->findAnalysisPass(AID, false);
640 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
641 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
642 const PassInfo *PI = (*I)->getPassInfo();
646 // If Pass not found then check the interfaces implemented by Immutable Pass
648 const std::vector<const PassInfo*> &ImmPI =
649 PI->getInterfacesImplemented();
650 for (unsigned Index = 0, End = ImmPI.size();
651 P == NULL && Index != End; ++Index)
652 if (ImmPI[Index] == AID)
660 // Print passes managed by this top level manager.
661 void PMTopLevelManager::dumpPasses() {
663 // Print out the immutable passes
664 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
665 ImmutablePasses[i]->dumpPassStructure(0);
668 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
669 E = PassManagers.end(); I != E; ++I)
670 (*I)->dumpPassStructure(1);
674 void PMTopLevelManager::dumpArguments() {
676 if (PassDebugging_New < Arguments)
679 cerr << "Pass Arguments: ";
680 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
681 E = PassManagers.end(); I != E; ++I) {
682 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
683 assert(PMD && "This is not a PassManager");
684 PMD->dumpPassArguments();
689 //===----------------------------------------------------------------------===//
690 // PMDataManager implementation
692 /// Return true IFF pass P's required analysis set does not required new
694 bool PMDataManager::manageablePass(Pass *P) {
697 // If this pass is not preserving information that is required by a
698 // pass maintained by higher level pass manager then do not insert
699 // this pass into current manager. Use new manager. For example,
700 // For example, If FunctionPass F is not preserving ModulePass Info M1
701 // that is used by another ModulePass M2 then do not insert F in
702 // current function pass manager.
706 /// Augement AvailableAnalysis by adding analysis made available by pass P.
707 void PMDataManager::recordAvailableAnalysis(Pass *P) {
709 if (const PassInfo *PI = P->getPassInfo()) {
710 AvailableAnalysis[PI] = P;
712 //This pass is the current implementation of all of the interfaces it
713 //implements as well.
714 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
715 for (unsigned i = 0, e = II.size(); i != e; ++i)
716 AvailableAnalysis[II[i]] = P;
720 /// Remove Analyss not preserved by Pass P
721 void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
722 AnalysisUsage AnUsage;
723 P->getAnalysisUsage(AnUsage);
725 if (AnUsage.getPreservesAll())
728 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
729 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
730 E = AvailableAnalysis.end(); I != E; ) {
731 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
732 PreservedSet.end()) {
733 // Remove this analysis
734 if (!dynamic_cast<ImmutablePass*>(I->second)) {
735 std::map<AnalysisID, Pass*>::iterator J = I++;
736 AvailableAnalysis.erase(J);
744 /// Remove analysis passes that are not used any longer
745 void PMDataManager::removeDeadPasses(Pass *P, std::string &Msg) {
747 std::vector<Pass *> DeadPasses;
748 TPM->collectLastUses(DeadPasses, P);
750 for (std::vector<Pass *>::iterator I = DeadPasses.begin(),
751 E = DeadPasses.end(); I != E; ++I) {
753 std::string Msg1 = " Freeing Pass '";
754 dumpPassInfo(*I, Msg1, Msg);
756 (*I)->releaseMemory();
758 std::map<AnalysisID, Pass*>::iterator Pos =
759 AvailableAnalysis.find((*I)->getPassInfo());
761 // It is possible that pass is already removed from the AvailableAnalysis
762 if (Pos != AvailableAnalysis.end())
763 AvailableAnalysis.erase(Pos);
767 /// Add pass P into the PassVector. Update
768 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
769 void PMDataManager::addPassToManager(Pass *P,
770 bool ProcessAnalysis) {
772 // This manager is going to manage pass P. Set up analysis resolver
774 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
777 if (ProcessAnalysis) {
779 // At the moment, this pass is the last user of all required passes.
780 std::vector<Pass *> LastUses;
781 std::vector<Pass *> RequiredPasses;
782 unsigned PDepth = this->getDepth();
784 collectRequiredAnalysisPasses(RequiredPasses, P);
785 for (std::vector<Pass *>::iterator I = RequiredPasses.begin(),
786 E = RequiredPasses.end(); I != E; ++I) {
787 Pass *PRequired = *I;
790 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
791 RDepth = DM.getDepth();
793 if (PDepth == RDepth)
794 LastUses.push_back(PRequired);
795 else if (PDepth > RDepth) {
796 // Let the parent claim responsibility of last use
797 ForcedLastUses.push_back(PRequired);
799 // Note : This feature is not yet implemented
801 "Unable to handle Pass that requires lower level Analysis pass");
805 if (!LastUses.empty())
806 TPM->setLastUser(LastUses, P);
808 // Take a note of analysis required and made available by this pass.
809 // Remove the analysis not preserved by this pass
810 removeNotPreservedAnalysis(P);
811 recordAvailableAnalysis(P);
815 PassVector.push_back(P);
818 /// Populate RequiredPasses with the analysis pass that are required by
820 void PMDataManager::collectRequiredAnalysisPasses(std::vector<Pass *> &RP,
822 AnalysisUsage AnUsage;
823 P->getAnalysisUsage(AnUsage);
824 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
825 for (std::vector<AnalysisID>::const_iterator
826 I = RequiredSet.begin(), E = RequiredSet.end();
828 Pass *AnalysisPass = findAnalysisPass(*I, true);
829 assert (AnalysisPass && "Analysis pass is not available");
830 RP.push_back(AnalysisPass);
833 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
834 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
835 E = IDs.end(); I != E; ++I) {
836 Pass *AnalysisPass = findAnalysisPass(*I, true);
837 assert (AnalysisPass && "Analysis pass is not available");
838 RP.push_back(AnalysisPass);
842 // All Required analyses should be available to the pass as it runs! Here
843 // we fill in the AnalysisImpls member of the pass so that it can
844 // successfully use the getAnalysis() method to retrieve the
845 // implementations it needs.
847 void PMDataManager::initializeAnalysisImpl(Pass *P) {
848 AnalysisUsage AnUsage;
849 P->getAnalysisUsage(AnUsage);
851 for (std::vector<const PassInfo *>::const_iterator
852 I = AnUsage.getRequiredSet().begin(),
853 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
854 Pass *Impl = findAnalysisPass(*I, true);
856 assert(0 && "Analysis used but not available!");
857 AnalysisResolver_New *AR = P->getResolver();
858 AR->addAnalysisImplsPair(*I, Impl);
862 /// Find the pass that implements Analysis AID. If desired pass is not found
863 /// then return NULL.
864 Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
866 // Check if AvailableAnalysis map has one entry.
867 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
869 if (I != AvailableAnalysis.end())
872 // Search Parents through TopLevelManager
874 return TPM->findAnalysisPass(AID);
880 //===----------------------------------------------------------------------===//
881 // NOTE: Is this the right place to define this method ?
882 // getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
883 Pass *AnalysisResolver_New::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
884 return PM.findAnalysisPass(ID, dir);
887 //===----------------------------------------------------------------------===//
888 // BasicBlockPassManager implementation
890 /// Add pass P into PassVector and return true. If this pass is not
891 /// manageable by this manager then return false.
893 BasicBlockPassManager::addPass(Pass *P) {
895 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
899 // If this pass does not preserve anlysis that is used by other passes
900 // managed by this manager than it is not a suiable pass for this manager.
901 if (!manageablePass(P))
904 addPassToManager (BP);
909 /// Execute all of the passes scheduled for execution by invoking
910 /// runOnBasicBlock method. Keep track of whether any of the passes modifies
911 /// the function, and if so, return true.
913 BasicBlockPassManager::runOnFunction(Function &F) {
918 bool Changed = doInitialization(F);
919 initializeAnalysisInfo();
921 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
922 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
923 e = passVectorEnd(); itr != e; ++itr) {
925 std::string Msg1 = "Executing Pass '";
926 std::string Msg2 = "' on BasicBlock '" + (*I).getName() + "'...\n";
927 dumpPassInfo(P, Msg1, Msg2);
928 initializeAnalysisImpl(P);
929 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
930 Changed |= BP->runOnBasicBlock(*I);
931 removeNotPreservedAnalysis(P);
932 recordAvailableAnalysis(P);
933 removeDeadPasses(P, Msg2);
935 return Changed | doFinalization(F);
938 // Implement doInitialization and doFinalization
939 inline bool BasicBlockPassManager::doInitialization(Module &M) {
940 bool Changed = false;
942 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
943 e = passVectorEnd(); itr != e; ++itr) {
945 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
946 Changed |= BP->doInitialization(M);
952 inline bool BasicBlockPassManager::doFinalization(Module &M) {
953 bool Changed = false;
955 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
956 e = passVectorEnd(); itr != e; ++itr) {
958 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
959 Changed |= BP->doFinalization(M);
965 inline bool BasicBlockPassManager::doInitialization(Function &F) {
966 bool Changed = false;
968 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
969 e = passVectorEnd(); itr != e; ++itr) {
971 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
972 Changed |= BP->doInitialization(F);
978 inline bool BasicBlockPassManager::doFinalization(Function &F) {
979 bool Changed = false;
981 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
982 e = passVectorEnd(); itr != e; ++itr) {
984 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
985 Changed |= BP->doFinalization(F);
992 //===----------------------------------------------------------------------===//
993 // FunctionPassManager implementation
995 /// Create new Function pass manager
996 FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
997 FPM = new FunctionPassManagerImpl_New(0);
998 // FPM is the top level manager.
999 FPM->setTopLevelManager(FPM);
1001 PMDataManager *PMD = dynamic_cast<PMDataManager *>(FPM);
1002 AnalysisResolver_New *AR = new AnalysisResolver_New(*PMD);
1003 FPM->setResolver(AR);
1005 FPM->addPassManager(FPM);
1009 FunctionPassManager::~FunctionPassManager() {
1010 // Note : FPM maintains one entry in PassManagers vector.
1011 // This one entry is FPM itself. This is not ideal. One
1012 // alternative is have one additional layer between
1013 // FunctionPassManager and FunctionPassManagerImpl.
1014 // Meanwhile, to avoid going into infinte loop, first
1015 // remove FPM from its PassMangers vector.
1016 FPM->clearManagers();
1020 /// add - Add a pass to the queue of passes to run. This passes
1021 /// ownership of the Pass to the PassManager. When the
1022 /// PassManager_X is destroyed, the pass will be destroyed as well, so
1023 /// there is no need to delete the pass. (TODO delete passes.)
1024 /// This implies that all passes MUST be allocated with 'new'.
1025 void FunctionPassManager::add(Pass *P) {
1029 /// run - Execute all of the passes scheduled for execution. Keep
1030 /// track of whether any of the passes modifies the function, and if
1031 /// so, return true.
1033 bool FunctionPassManager::run(Function &F) {
1035 if (MP->materializeFunction(&F, &errstr)) {
1036 cerr << "Error reading bytecode file: " << errstr << "\n";
1043 /// doInitialization - Run all of the initializers for the function passes.
1045 bool FunctionPassManager::doInitialization() {
1046 return FPM->doInitialization(*MP->getModule());
1049 /// doFinalization - Run all of the initializers for the function passes.
1051 bool FunctionPassManager::doFinalization() {
1052 return FPM->doFinalization(*MP->getModule());
1055 //===----------------------------------------------------------------------===//
1056 // FunctionPassManagerImpl_New implementation
1058 /// Add pass P into the pass manager queue. If P is a BasicBlockPass then
1059 /// either use it into active basic block pass manager or create new basic
1060 /// block pass manager to handle pass P.
1062 FunctionPassManagerImpl_New::addPass(Pass *P) {
1064 // If P is a BasicBlockPass then use BasicBlockPassManager.
1065 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
1067 if (!activeBBPassManager || !activeBBPassManager->addPass(BP)) {
1069 // If active manager exists then clear its analysis info.
1070 if (activeBBPassManager)
1071 activeBBPassManager->initializeAnalysisInfo();
1073 // Create and add new manager
1074 activeBBPassManager =
1075 new BasicBlockPassManager(getDepth() + 1);
1076 // Inherit top level manager
1077 activeBBPassManager->setTopLevelManager(this->getTopLevelManager());
1079 // Add new manager into current manager's list.
1080 addPassToManager(activeBBPassManager, false);
1082 // Add new manager into top level manager's indirect passes list
1083 PMDataManager *PMD = dynamic_cast<PMDataManager *>(activeBBPassManager);
1084 assert (PMD && "Manager is not Pass Manager");
1085 TPM->addIndirectPassManager(PMD);
1087 // Add pass into new manager. This time it must succeed.
1088 if (!activeBBPassManager->addPass(BP))
1089 assert(0 && "Unable to add Pass");
1092 if (!ForcedLastUses.empty())
1093 TPM->setLastUser(ForcedLastUses, this);
1098 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
1102 // If this pass does not preserve anlysis that is used by other passes
1103 // managed by this manager than it is not a suiable pass for this manager.
1104 if (!manageablePass(P))
1107 addPassToManager (FP);
1109 // If active manager exists then clear its analysis info.
1110 if (activeBBPassManager) {
1111 activeBBPassManager->initializeAnalysisInfo();
1112 activeBBPassManager = NULL;
1118 /// Execute all of the passes scheduled for execution by invoking
1119 /// runOnFunction method. Keep track of whether any of the passes modifies
1120 /// the function, and if so, return true.
1121 bool FunctionPassManagerImpl_New::runOnModule(Module &M) {
1123 bool Changed = doInitialization(M);
1124 initializeAnalysisInfo();
1126 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1127 this->runOnFunction(*I);
1129 return Changed | doFinalization(M);
1132 /// Execute all of the passes scheduled for execution by invoking
1133 /// runOnFunction method. Keep track of whether any of the passes modifies
1134 /// the function, and if so, return true.
1135 bool FunctionPassManagerImpl_New::runOnFunction(Function &F) {
1137 bool Changed = false;
1142 initializeAnalysisInfo();
1144 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1145 e = passVectorEnd(); itr != e; ++itr) {
1147 std::string Msg1 = "Executing Pass '";
1148 std::string Msg2 = "' on Function '" + F.getName() + "'...\n";
1149 dumpPassInfo(P, Msg1, Msg2);
1150 initializeAnalysisImpl(P);
1151 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
1152 Changed |= FP->runOnFunction(F);
1153 removeNotPreservedAnalysis(P);
1154 recordAvailableAnalysis(P);
1155 removeDeadPasses(P, Msg2);
1161 inline bool FunctionPassManagerImpl_New::doInitialization(Module &M) {
1162 bool Changed = false;
1164 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1165 e = passVectorEnd(); itr != e; ++itr) {
1168 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
1169 Changed |= FP->doInitialization(M);
1175 inline bool FunctionPassManagerImpl_New::doFinalization(Module &M) {
1176 bool Changed = false;
1178 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1179 e = passVectorEnd(); itr != e; ++itr) {
1182 FunctionPass *FP = dynamic_cast<FunctionPass*>(P);
1183 Changed |= FP->doFinalization(M);
1189 // Execute all the passes managed by this top level manager.
1190 // Return true if any function is modified by a pass.
1191 bool FunctionPassManagerImpl_New::run(Function &F) {
1193 bool Changed = false;
1194 for (std::vector<Pass *>::iterator I = passManagersBegin(),
1195 E = passManagersEnd(); I != E; ++I) {
1196 FunctionPassManagerImpl_New *FP =
1197 dynamic_cast<FunctionPassManagerImpl_New *>(*I);
1198 Changed |= FP->runOnFunction(F);
1203 //===----------------------------------------------------------------------===//
1204 // ModulePassManager implementation
1206 /// Add P into pass vector if it is manageble. If P is a FunctionPass
1207 /// then use FunctionPassManagerImpl_New to manage it. Return false if P
1208 /// is not manageable by this manager.
1210 ModulePassManager::addPass(Pass *P) {
1212 // If P is FunctionPass then use function pass maanager.
1213 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
1215 if (!activeFunctionPassManager || !activeFunctionPassManager->addPass(P)) {
1217 // If active manager exists then clear its analysis info.
1218 if (activeFunctionPassManager)
1219 activeFunctionPassManager->initializeAnalysisInfo();
1221 // Create and add new manager
1222 activeFunctionPassManager =
1223 new FunctionPassManagerImpl_New(getDepth() + 1);
1225 // Add new manager into current manager's list
1226 addPassToManager(activeFunctionPassManager, false);
1228 // Inherit top level manager
1229 activeFunctionPassManager->setTopLevelManager(this->getTopLevelManager());
1231 // Add new manager into top level manager's indirect passes list
1232 PMDataManager *PMD =
1233 dynamic_cast<PMDataManager *>(activeFunctionPassManager);
1234 assert(PMD && "Manager is not Pass Manager");
1235 TPM->addIndirectPassManager(PMD);
1237 // Add pass into new manager. This time it must succeed.
1238 if (!activeFunctionPassManager->addPass(FP))
1239 assert(0 && "Unable to add pass");
1242 if (!ForcedLastUses.empty())
1243 TPM->setLastUser(ForcedLastUses, this);
1248 ModulePass *MP = dynamic_cast<ModulePass *>(P);
1252 // If this pass does not preserve anlysis that is used by other passes
1253 // managed by this manager than it is not a suiable pass for this manager.
1254 if (!manageablePass(P))
1257 addPassToManager(MP);
1258 // If active manager exists then clear its analysis info.
1259 if (activeFunctionPassManager) {
1260 activeFunctionPassManager->initializeAnalysisInfo();
1261 activeFunctionPassManager = NULL;
1268 /// Execute all of the passes scheduled for execution by invoking
1269 /// runOnModule method. Keep track of whether any of the passes modifies
1270 /// the module, and if so, return true.
1272 ModulePassManager::runOnModule(Module &M) {
1273 bool Changed = false;
1274 initializeAnalysisInfo();
1276 for (std::vector<Pass *>::iterator itr = passVectorBegin(),
1277 e = passVectorEnd(); itr != e; ++itr) {
1279 std::string Msg1 = "Executing Pass '";
1280 std::string Msg2 = "' on Module '" + M.getModuleIdentifier() + "'...\n";
1281 dumpPassInfo(P, Msg1, Msg2);
1282 initializeAnalysisImpl(P);
1283 ModulePass *MP = dynamic_cast<ModulePass*>(P);
1284 Changed |= MP->runOnModule(M);
1285 removeNotPreservedAnalysis(P);
1286 recordAvailableAnalysis(P);
1287 removeDeadPasses(P, Msg2);
1292 //===----------------------------------------------------------------------===//
1293 // PassManagerImpl implementation
1295 /// Add P into active pass manager or use new module pass manager to
1297 bool PassManagerImpl_New::addPass(Pass *P) {
1299 if (!activeManager || !activeManager->addPass(P)) {
1300 activeManager = new ModulePassManager(getDepth() + 1);
1301 // Inherit top level manager
1302 activeManager->setTopLevelManager(this->getTopLevelManager());
1304 // This top level manager is going to manage activeManager.
1305 // Set up analysis resolver to connect them.
1306 AnalysisResolver_New *AR = new AnalysisResolver_New(*this);
1307 activeManager->setResolver(AR);
1309 addPassManager(activeManager);
1310 return activeManager->addPass(P);
1315 /// run - Execute all of the passes scheduled for execution. Keep track of
1316 /// whether any of the passes modifies the module, and if so, return true.
1317 bool PassManagerImpl_New::run(Module &M) {
1319 bool Changed = false;
1322 if (PassDebugging_New >= Structure)
1325 for (std::vector<Pass *>::iterator I = passManagersBegin(),
1326 E = passManagersEnd(); I != E; ++I) {
1327 ModulePassManager *MP = dynamic_cast<ModulePassManager *>(*I);
1328 Changed |= MP->runOnModule(M);
1333 //===----------------------------------------------------------------------===//
1334 // PassManager implementation
1336 /// Create new pass manager
1337 PassManager::PassManager() {
1338 PM = new PassManagerImpl_New(0);
1339 // PM is the top level manager
1340 PM->setTopLevelManager(PM);
1343 PassManager::~PassManager() {
1347 /// add - Add a pass to the queue of passes to run. This passes ownership of
1348 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
1349 /// will be destroyed as well, so there is no need to delete the pass. This
1350 /// implies that all passes MUST be allocated with 'new'.
1352 PassManager::add(Pass *P) {
1356 /// run - Execute all of the passes scheduled for execution. Keep track of
1357 /// whether any of the passes modifies the module, and if so, return true.
1359 PassManager::run(Module &M) {