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/Support/Timer.h"
18 #include "llvm/Module.h"
19 #include "llvm/ModuleProvider.h"
20 #include "llvm/Support/Streams.h"
21 #include "llvm/Support/ManagedStatic.h"
26 class llvm::PMDataManager;
28 //===----------------------------------------------------------------------===//
30 // The Pass Manager Infrastructure manages passes. It's responsibilities are:
32 // o Manage optimization pass execution order
33 // o Make required Analysis information available before pass P is run
34 // o Release memory occupied by dead passes
35 // o If Analysis information is dirtied by a pass then regenerate Analysis
36 // information before it is consumed by another pass.
38 // Pass Manager Infrastructure uses multiple pass managers. They are
39 // PassManager, FunctionPassManager, MPPassManager, FPPassManager, BBPassManager.
40 // This class hierarcy uses multiple inheritance but pass managers do not derive
41 // from another pass manager.
43 // PassManager and FunctionPassManager are two top-level pass manager that
44 // represents the external interface of this entire pass manager infrastucture.
46 // Important classes :
48 // [o] class PMTopLevelManager;
50 // Two top level managers, PassManager and FunctionPassManager, derive from
51 // PMTopLevelManager. PMTopLevelManager manages information used by top level
52 // managers such as last user info.
54 // [o] class PMDataManager;
56 // PMDataManager manages information, e.g. list of available analysis info,
57 // used by a pass manager to manage execution order of passes. It also provides
58 // a place to implement common pass manager APIs. All pass managers derive from
61 // [o] class BBPassManager : public FunctionPass, public PMDataManager;
63 // BBPassManager manages BasicBlockPasses.
65 // [o] class FunctionPassManager;
67 // This is a external interface used by JIT to manage FunctionPasses. This
68 // interface relies on FunctionPassManagerImpl to do all the tasks.
70 // [o] class FunctionPassManagerImpl : public ModulePass, PMDataManager,
71 // public PMTopLevelManager;
73 // FunctionPassManagerImpl is a top level manager. It manages FPPassManagers
75 // [o] class FPPassManager : public ModulePass, public PMDataManager;
77 // FPPassManager manages FunctionPasses and BBPassManagers
79 // [o] class MPPassManager : public Pass, public PMDataManager;
81 // MPPassManager manages ModulePasses and FPPassManagers
83 // [o] class PassManager;
85 // This is a external interface used by various tools to manages passes. It
86 // relies on PassManagerImpl to do all the tasks.
88 // [o] class PassManagerImpl : public Pass, public PMDataManager,
89 // public PMDTopLevelManager
91 // PassManagerImpl is a top level pass manager responsible for managing
93 //===----------------------------------------------------------------------===//
97 //===----------------------------------------------------------------------===//
98 // Pass debugging information. Often it is useful to find out what pass is
99 // running when a crash occurs in a utility. When this library is compiled with
100 // debugging on, a command line option (--debug-pass) is enabled that causes the
101 // pass name to be printed before it executes.
104 // Different debug levels that can be enabled...
105 enum PassDebugLevel {
106 None, Arguments, Structure, Executions, Details
109 static cl::opt<enum PassDebugLevel>
110 PassDebugging_New("debug-pass", cl::Hidden,
111 cl::desc("Print PassManager debugging information"),
113 clEnumVal(None , "disable debug output"),
114 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
115 clEnumVal(Structure , "print pass structure before run()"),
116 clEnumVal(Executions, "print pass name before it is executed"),
117 clEnumVal(Details , "print pass details when it is executed"),
119 } // End of llvm namespace
123 //===----------------------------------------------------------------------===//
126 /// PMTopLevelManager manages LastUser info and collects common APIs used by
127 /// top level pass managers.
128 class VISIBILITY_HIDDEN PMTopLevelManager {
131 virtual unsigned getNumContainedManagers() {
132 return PassManagers.size();
135 /// Schedule pass P for execution. Make sure that passes required by
136 /// P are run before P is run. Update analysis info maintained by
137 /// the manager. Remove dead passes. This is a recursive function.
138 void schedulePass(Pass *P);
140 /// This is implemented by top level pass manager and used by
141 /// schedulePass() to add analysis info passes that are not available.
142 virtual void addTopLevelPass(Pass *P) = 0;
144 /// Set pass P as the last user of the given analysis passes.
145 void setLastUser(std::vector<Pass *> &AnalysisPasses, Pass *P);
147 /// Collect passes whose last user is P
148 void collectLastUses(std::vector<Pass *> &LastUses, Pass *P);
150 /// Find the pass that implements Analysis AID. Search immutable
151 /// passes and all pass managers. If desired pass is not found
152 /// then return NULL.
153 Pass *findAnalysisPass(AnalysisID AID);
155 virtual ~PMTopLevelManager() {
156 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
157 E = PassManagers.end(); I != E; ++I)
160 for (std::vector<ImmutablePass *>::iterator
161 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
164 PassManagers.clear();
167 /// Add immutable pass and initialize it.
168 inline void addImmutablePass(ImmutablePass *P) {
170 ImmutablePasses.push_back(P);
173 inline std::vector<ImmutablePass *>& getImmutablePasses() {
174 return ImmutablePasses;
177 void addPassManager(Pass *Manager) {
178 PassManagers.push_back(Manager);
181 // Add Manager into the list of managers that are not directly
182 // maintained by this top level pass manager
183 inline void addIndirectPassManager(PMDataManager *Manager) {
184 IndirectPassManagers.push_back(Manager);
187 // Print passes managed by this top level manager.
188 void dumpPasses() const;
189 void dumpArguments() const;
191 void initializeAllAnalysisInfo();
195 /// Collection of pass managers
196 std::vector<Pass *> PassManagers;
200 /// Collection of pass managers that are not directly maintained
201 /// by this pass manager
202 std::vector<PMDataManager *> IndirectPassManagers;
204 // Map to keep track of last user of the analysis pass.
205 // LastUser->second is the last user of Lastuser->first.
206 std::map<Pass *, Pass *> LastUser;
208 /// Immutable passes are managed by top level manager.
209 std::vector<ImmutablePass *> ImmutablePasses;
212 } // End of anon namespace
214 //===----------------------------------------------------------------------===//
218 /// PMDataManager provides the common place to manage the analysis data
219 /// used by pass managers.
220 class PMDataManager {
222 PMDataManager(int Depth) : TPM(NULL), Depth(Depth) {
223 initializeAnalysisInfo();
226 virtual ~PMDataManager() {
228 for (std::vector<Pass *>::iterator I = PassVector.begin(),
229 E = PassVector.end(); I != E; ++I)
235 /// Return true IFF pass P's required analysis set does not required new
237 bool manageablePass(Pass *P);
239 /// Augment AvailableAnalysis by adding analysis made available by pass P.
240 void recordAvailableAnalysis(Pass *P);
242 /// Remove Analysis that is not preserved by the pass
243 void removeNotPreservedAnalysis(Pass *P);
245 /// Remove dead passes
246 void removeDeadPasses(Pass *P, std::string &Msg);
248 /// Add pass P into the PassVector. Update
249 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
250 void addPassToManager(Pass *P, bool ProcessAnalysis = true);
252 /// Initialize available analysis information.
253 void initializeAnalysisInfo() {
254 TransferLastUses.clear();
255 AvailableAnalysis.clear();
258 /// Populate RequiredPasses with the analysis pass that are required by
260 void collectRequiredAnalysisPasses(std::vector<Pass *> &RequiredPasses,
263 /// All Required analyses should be available to the pass as it runs! Here
264 /// we fill in the AnalysisImpls member of the pass so that it can
265 /// successfully use the getAnalysis() method to retrieve the
266 /// implementations it needs.
267 void initializeAnalysisImpl(Pass *P);
269 /// Find the pass that implements Analysis AID. If desired pass is not found
270 /// then return NULL.
271 Pass *findAnalysisPass(AnalysisID AID, bool Direction);
273 // Access toplevel manager
274 PMTopLevelManager *getTopLevelManager() { return TPM; }
275 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
277 unsigned getDepth() const { return Depth; }
279 // Print routines used by debug-pass
280 void dumpLastUses(Pass *P, unsigned Offset) const;
281 void dumpPassArguments() const;
282 void dumpPassInfo(Pass *P, std::string &Msg1, std::string &Msg2) const;
283 void dumpAnalysisSetInfo(const char *Msg, Pass *P,
284 const std::vector<AnalysisID> &Set) const;
286 std::vector<Pass *>& getTransferredLastUses() {
287 return TransferLastUses;
290 virtual unsigned getNumContainedPasses() {
291 return PassVector.size();
294 virtual PassManagerType getPassManagerType() {
295 assert ( 0 && "Invalid use of getPassManagerType");
300 // 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 // Current pass manage is requesting parent manager to record parent
303 // manager as the last user of these TrransferLastUses passes.
304 std::vector<Pass *> TransferLastUses;
306 // Top level manager.
307 PMTopLevelManager *TPM;
309 // Collection of pass that are managed by this manager
310 std::vector<Pass *> PassVector;
313 // Set of available Analysis. This information is used while scheduling
314 // pass. If a pass requires an analysis which is not not available then
315 // equired analysis pass is scheduled to run before the pass itself is
317 std::map<AnalysisID, Pass*> AvailableAnalysis;
322 //===----------------------------------------------------------------------===//
325 /// BBPassManager manages BasicBlockPass. It batches all the
326 /// pass together and sequence them to process one basic block before
327 /// processing next basic block.
328 class VISIBILITY_HIDDEN BBPassManager : public PMDataManager,
329 public FunctionPass {
332 BBPassManager(int Depth) : PMDataManager(Depth) { }
334 /// Add a pass into a passmanager queue.
335 bool addPass(Pass *p);
337 /// Execute all of the passes scheduled for execution. Keep track of
338 /// whether any of the passes modifies the function, and if so, return true.
339 bool runOnFunction(Function &F);
341 /// Pass Manager itself does not invalidate any analysis info.
342 void getAnalysisUsage(AnalysisUsage &Info) const {
343 Info.setPreservesAll();
346 bool doInitialization(Module &M);
347 bool doInitialization(Function &F);
348 bool doFinalization(Module &M);
349 bool doFinalization(Function &F);
351 // Print passes managed by this manager
352 void dumpPassStructure(unsigned Offset) {
353 llvm::cerr << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n";
354 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
355 BasicBlockPass *BP = getContainedPass(Index);
356 BP->dumpPassStructure(Offset + 1);
357 dumpLastUses(BP, Offset+1);
361 BasicBlockPass *getContainedPass(unsigned N) {
362 assert ( N < PassVector.size() && "Pass number out of range!");
363 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
367 virtual PassManagerType getPassManagerType() {
368 return PMT_BasicBlockPassManager;
372 //===----------------------------------------------------------------------===//
375 /// FPPassManager manages BBPassManagers and FunctionPasses.
376 /// It batches all function passes and basic block pass managers together and
377 /// sequence them to process one function at a time before processing next
380 class FPPassManager : public ModulePass, public PMDataManager {
383 FPPassManager(int Depth) : PMDataManager(Depth) {
384 activeBBPassManager = NULL;
387 /// Add a pass into a passmanager queue.
388 bool addPass(Pass *p);
390 /// run - Execute all of the passes scheduled for execution. Keep track of
391 /// whether any of the passes modifies the module, and if so, return true.
392 bool runOnFunction(Function &F);
393 bool runOnModule(Module &M);
395 /// doInitialization - Run all of the initializers for the function passes.
397 bool doInitialization(Module &M);
399 /// doFinalization - Run all of the initializers for the function passes.
401 bool doFinalization(Module &M);
403 /// Pass Manager itself does not invalidate any analysis info.
404 void getAnalysisUsage(AnalysisUsage &Info) const {
405 Info.setPreservesAll();
408 // Print passes managed by this manager
409 void dumpPassStructure(unsigned Offset) {
410 llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
411 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
412 FunctionPass *FP = getContainedPass(Index);
413 FP->dumpPassStructure(Offset + 1);
414 dumpLastUses(FP, Offset+1);
418 FunctionPass *getContainedPass(unsigned N) {
419 assert ( N < PassVector.size() && "Pass number out of range!");
420 FunctionPass *FP = static_cast<FunctionPass *>(PassVector[N]);
424 virtual PassManagerType getPassManagerType() {
425 return PMT_FunctionPassManager;
428 // Active Pass Manager
429 BBPassManager *activeBBPassManager;
432 //===----------------------------------------------------------------------===//
433 // FunctionPassManagerImpl
435 /// FunctionPassManagerImpl manages FPPassManagers
436 class FunctionPassManagerImpl : public Pass,
437 public PMDataManager,
438 public PMTopLevelManager {
442 FunctionPassManagerImpl(int Depth) : PMDataManager(Depth) {
443 activeManager = NULL;
446 /// add - Add a pass to the queue of passes to run. This passes ownership of
447 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
448 /// will be destroyed as well, so there is no need to delete the pass. This
449 /// implies that all passes MUST be allocated with 'new'.
454 /// run - Execute all of the passes scheduled for execution. Keep track of
455 /// whether any of the passes modifies the module, and if so, return true.
456 bool run(Function &F);
458 /// doInitialization - Run all of the initializers for the function passes.
460 bool doInitialization(Module &M);
462 /// doFinalization - Run all of the initializers for the function passes.
464 bool doFinalization(Module &M);
466 /// Pass Manager itself does not invalidate any analysis info.
467 void getAnalysisUsage(AnalysisUsage &Info) const {
468 Info.setPreservesAll();
471 inline void addTopLevelPass(Pass *P) {
473 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
475 // P is a immutable pass and it will be managed by this
476 // top level manager. Set up analysis resolver to connect them.
477 AnalysisResolver *AR = new AnalysisResolver(*this);
479 initializeAnalysisImpl(P);
480 addImmutablePass(IP);
481 recordAvailableAnalysis(IP);
487 FPPassManager *getContainedManager(unsigned N) {
488 assert ( N < PassManagers.size() && "Pass number out of range!");
489 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
493 /// Add a pass into a passmanager queue.
494 bool addPass(Pass *p);
498 // Active Pass Manager
499 FPPassManager *activeManager;
502 //===----------------------------------------------------------------------===//
505 /// MPPassManager manages ModulePasses and function pass managers.
506 /// It batches all Module passes passes and function pass managers together and
507 /// sequence them to process one module.
508 class MPPassManager : public Pass, public PMDataManager {
511 MPPassManager(int Depth) : PMDataManager(Depth) {
512 activeFunctionPassManager = NULL;
515 /// Add a pass into a passmanager queue.
516 bool addPass(Pass *p);
518 /// run - Execute all of the passes scheduled for execution. Keep track of
519 /// whether any of the passes modifies the module, and if so, return true.
520 bool runOnModule(Module &M);
522 /// Pass Manager itself does not invalidate any analysis info.
523 void getAnalysisUsage(AnalysisUsage &Info) const {
524 Info.setPreservesAll();
527 // Print passes managed by this manager
528 void dumpPassStructure(unsigned Offset) {
529 llvm::cerr << std::string(Offset*2, ' ') << "ModulePass Manager\n";
530 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
531 ModulePass *MP = getContainedPass(Index);
532 MP->dumpPassStructure(Offset + 1);
533 dumpLastUses(MP, Offset+1);
537 ModulePass *getContainedPass(unsigned N) {
538 assert ( N < PassVector.size() && "Pass number out of range!");
539 ModulePass *MP = static_cast<ModulePass *>(PassVector[N]);
543 virtual PassManagerType getPassManagerType() { return PMT_ModulePassManager; }
545 // Active Pass Manager
546 FPPassManager *activeFunctionPassManager;
549 //===----------------------------------------------------------------------===//
552 /// PassManagerImpl manages MPPassManagers
553 class PassManagerImpl : public Pass,
554 public PMDataManager,
555 public PMTopLevelManager {
559 PassManagerImpl(int Depth) : PMDataManager(Depth) {
560 activeManager = NULL;
563 /// add - Add a pass to the queue of passes to run. This passes ownership of
564 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
565 /// will be destroyed as well, so there is no need to delete the pass. This
566 /// implies that all passes MUST be allocated with 'new'.
571 /// run - Execute all of the passes scheduled for execution. Keep track of
572 /// whether any of the passes modifies the module, and if so, return true.
575 /// Pass Manager itself does not invalidate any analysis info.
576 void getAnalysisUsage(AnalysisUsage &Info) const {
577 Info.setPreservesAll();
580 inline void addTopLevelPass(Pass *P) {
582 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
584 // P is a immutable pass and it will be managed by this
585 // top level manager. Set up analysis resolver to connect them.
586 AnalysisResolver *AR = new AnalysisResolver(*this);
588 initializeAnalysisImpl(P);
589 addImmutablePass(IP);
590 recordAvailableAnalysis(IP);
596 MPPassManager *getContainedManager(unsigned N) {
597 assert ( N < PassManagers.size() && "Pass number out of range!");
598 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
604 /// Add a pass into a passmanager queue.
605 bool addPass(Pass *p);
607 // Active Pass Manager
608 MPPassManager *activeManager;
611 } // End of llvm namespace
615 //===----------------------------------------------------------------------===//
616 // TimingInfo Class - This class is used to calculate information about the
617 // amount of time each pass takes to execute. This only happens when
618 // -time-passes is enabled on the command line.
621 class VISIBILITY_HIDDEN TimingInfo {
622 std::map<Pass*, Timer> TimingData;
626 // Use 'create' member to get this.
627 TimingInfo() : TG("... Pass execution timing report ...") {}
629 // TimingDtor - Print out information about timing information
631 // Delete all of the timers...
633 // TimerGroup is deleted next, printing the report.
636 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
637 // to a non null value (if the -time-passes option is enabled) or it leaves it
638 // null. It may be called multiple times.
639 static void createTheTimeInfo();
641 void passStarted(Pass *P) {
643 if (dynamic_cast<PMDataManager *>(P))
646 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
647 if (I == TimingData.end())
648 I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
649 I->second.startTimer();
651 void passEnded(Pass *P) {
653 if (dynamic_cast<PMDataManager *>(P))
656 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
657 assert (I != TimingData.end() && "passStarted/passEnded not nested right!");
658 I->second.stopTimer();
662 static TimingInfo *TheTimeInfo;
664 } // End of anon namespace
666 //===----------------------------------------------------------------------===//
667 // PMTopLevelManager implementation
669 /// Set pass P as the last user of the given analysis passes.
670 void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses,
673 for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(),
674 E = AnalysisPasses.end(); I != E; ++I) {
677 // If AP is the last user of other passes then make P last user of
679 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
680 LUE = LastUser.end(); LUI != LUE; ++LUI) {
681 if (LUI->second == AP)
682 LastUser[LUI->first] = P;
687 /// Collect passes whose last user is P
688 void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses,
690 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
691 LUE = LastUser.end(); LUI != LUE; ++LUI)
692 if (LUI->second == P)
693 LastUses.push_back(LUI->first);
696 /// Schedule pass P for execution. Make sure that passes required by
697 /// P are run before P is run. Update analysis info maintained by
698 /// the manager. Remove dead passes. This is a recursive function.
699 void PMTopLevelManager::schedulePass(Pass *P) {
701 // TODO : Allocate function manager for this pass, other wise required set
702 // may be inserted into previous function manager
704 AnalysisUsage AnUsage;
705 P->getAnalysisUsage(AnUsage);
706 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
707 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
708 E = RequiredSet.end(); I != E; ++I) {
710 Pass *AnalysisPass = findAnalysisPass(*I);
712 // Schedule this analysis run first.
713 AnalysisPass = (*I)->createPass();
714 schedulePass(AnalysisPass);
718 // Now all required passes are available.
722 /// Find the pass that implements Analysis AID. Search immutable
723 /// passes and all pass managers. If desired pass is not found
724 /// then return NULL.
725 Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
728 // Check pass managers
729 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
730 E = PassManagers.end(); P == NULL && I != E; ++I) {
731 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
732 assert(PMD && "This is not a PassManager");
733 P = PMD->findAnalysisPass(AID, false);
736 // Check other pass managers
737 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
738 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
739 P = (*I)->findAnalysisPass(AID, false);
741 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
742 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
743 const PassInfo *PI = (*I)->getPassInfo();
747 // If Pass not found then check the interfaces implemented by Immutable Pass
749 const std::vector<const PassInfo*> &ImmPI = PI->getInterfacesImplemented();
750 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
758 // Print passes managed by this top level manager.
759 void PMTopLevelManager::dumpPasses() const {
761 if (PassDebugging_New < Structure)
764 // Print out the immutable passes
765 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
766 ImmutablePasses[i]->dumpPassStructure(0);
769 for (std::vector<Pass *>::const_iterator I = PassManagers.begin(),
770 E = PassManagers.end(); I != E; ++I)
771 (*I)->dumpPassStructure(1);
774 void PMTopLevelManager::dumpArguments() const {
776 if (PassDebugging_New < Arguments)
779 cerr << "Pass Arguments: ";
780 for (std::vector<Pass *>::const_iterator I = PassManagers.begin(),
781 E = PassManagers.end(); I != E; ++I) {
782 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
783 assert(PMD && "This is not a PassManager");
784 PMD->dumpPassArguments();
789 void PMTopLevelManager::initializeAllAnalysisInfo() {
791 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
792 E = PassManagers.end(); I != E; ++I) {
793 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
794 assert(PMD && "This is not a PassManager");
795 PMD->initializeAnalysisInfo();
798 // Initailize other pass managers
799 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
800 E = IndirectPassManagers.end(); I != E; ++I)
801 (*I)->initializeAnalysisInfo();
804 //===----------------------------------------------------------------------===//
805 // PMDataManager implementation
807 /// Return true IFF pass P's required analysis set does not required new
809 bool PMDataManager::manageablePass(Pass *P) {
812 // If this pass is not preserving information that is required by a
813 // pass maintained by higher level pass manager then do not insert
814 // this pass into current manager. Use new manager. For example,
815 // For example, If FunctionPass F is not preserving ModulePass Info M1
816 // that is used by another ModulePass M2 then do not insert F in
817 // current function pass manager.
821 /// Augement AvailableAnalysis by adding analysis made available by pass P.
822 void PMDataManager::recordAvailableAnalysis(Pass *P) {
824 if (const PassInfo *PI = P->getPassInfo()) {
825 AvailableAnalysis[PI] = P;
827 //This pass is the current implementation of all of the interfaces it
828 //implements as well.
829 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
830 for (unsigned i = 0, e = II.size(); i != e; ++i)
831 AvailableAnalysis[II[i]] = P;
835 /// Remove Analyss not preserved by Pass P
836 void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
837 AnalysisUsage AnUsage;
838 P->getAnalysisUsage(AnUsage);
840 if (AnUsage.getPreservesAll())
843 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
844 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
845 E = AvailableAnalysis.end(); I != E; ) {
846 std::map<AnalysisID, Pass*>::iterator Info = I++;
847 if (std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
848 PreservedSet.end()) {
849 // Remove this analysis
850 if (!dynamic_cast<ImmutablePass*>(Info->second))
851 AvailableAnalysis.erase(Info);
856 /// Remove analysis passes that are not used any longer
857 void PMDataManager::removeDeadPasses(Pass *P, std::string &Msg) {
859 std::vector<Pass *> DeadPasses;
860 TPM->collectLastUses(DeadPasses, P);
862 for (std::vector<Pass *>::iterator I = DeadPasses.begin(),
863 E = DeadPasses.end(); I != E; ++I) {
865 std::string Msg1 = " Freeing Pass '";
866 dumpPassInfo(*I, Msg1, Msg);
868 if (TheTimeInfo) TheTimeInfo->passStarted(P);
869 (*I)->releaseMemory();
870 if (TheTimeInfo) TheTimeInfo->passEnded(P);
872 std::map<AnalysisID, Pass*>::iterator Pos =
873 AvailableAnalysis.find((*I)->getPassInfo());
875 // It is possible that pass is already removed from the AvailableAnalysis
876 if (Pos != AvailableAnalysis.end())
877 AvailableAnalysis.erase(Pos);
881 /// Add pass P into the PassVector. Update
882 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
883 void PMDataManager::addPassToManager(Pass *P,
884 bool ProcessAnalysis) {
886 // This manager is going to manage pass P. Set up analysis resolver
888 AnalysisResolver *AR = new AnalysisResolver(*this);
891 if (ProcessAnalysis) {
893 // At the moment, this pass is the last user of all required passes.
894 std::vector<Pass *> LastUses;
895 std::vector<Pass *> RequiredPasses;
896 unsigned PDepth = this->getDepth();
898 collectRequiredAnalysisPasses(RequiredPasses, P);
899 for (std::vector<Pass *>::iterator I = RequiredPasses.begin(),
900 E = RequiredPasses.end(); I != E; ++I) {
901 Pass *PRequired = *I;
904 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
905 RDepth = DM.getDepth();
907 if (PDepth == RDepth)
908 LastUses.push_back(PRequired);
909 else if (PDepth > RDepth) {
910 // Let the parent claim responsibility of last use
911 TransferLastUses.push_back(PRequired);
913 // Note : This feature is not yet implemented
915 "Unable to handle Pass that requires lower level Analysis pass");
919 LastUses.push_back(P);
920 TPM->setLastUser(LastUses, P);
922 // Take a note of analysis required and made available by this pass.
923 // Remove the analysis not preserved by this pass
924 removeNotPreservedAnalysis(P);
925 recordAvailableAnalysis(P);
929 PassVector.push_back(P);
932 /// Populate RequiredPasses with the analysis pass that are required by
934 void PMDataManager::collectRequiredAnalysisPasses(std::vector<Pass *> &RP,
936 AnalysisUsage AnUsage;
937 P->getAnalysisUsage(AnUsage);
938 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
939 for (std::vector<AnalysisID>::const_iterator
940 I = RequiredSet.begin(), E = RequiredSet.end();
942 Pass *AnalysisPass = findAnalysisPass(*I, true);
943 assert (AnalysisPass && "Analysis pass is not available");
944 RP.push_back(AnalysisPass);
947 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
948 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
949 E = IDs.end(); I != E; ++I) {
950 Pass *AnalysisPass = findAnalysisPass(*I, true);
951 assert (AnalysisPass && "Analysis pass is not available");
952 RP.push_back(AnalysisPass);
956 // All Required analyses should be available to the pass as it runs! Here
957 // we fill in the AnalysisImpls member of the pass so that it can
958 // successfully use the getAnalysis() method to retrieve the
959 // implementations it needs.
961 void PMDataManager::initializeAnalysisImpl(Pass *P) {
962 AnalysisUsage AnUsage;
963 P->getAnalysisUsage(AnUsage);
965 for (std::vector<const PassInfo *>::const_iterator
966 I = AnUsage.getRequiredSet().begin(),
967 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
968 Pass *Impl = findAnalysisPass(*I, true);
970 assert(0 && "Analysis used but not available!");
971 AnalysisResolver *AR = P->getResolver();
972 AR->addAnalysisImplsPair(*I, Impl);
976 /// Find the pass that implements Analysis AID. If desired pass is not found
977 /// then return NULL.
978 Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
980 // Check if AvailableAnalysis map has one entry.
981 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
983 if (I != AvailableAnalysis.end())
986 // Search Parents through TopLevelManager
988 return TPM->findAnalysisPass(AID);
993 // Print list of passes that are last used by P.
994 void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
996 std::vector<Pass *> LUses;
998 assert (TPM && "Top Level Manager is missing");
999 TPM->collectLastUses(LUses, P);
1001 for (std::vector<Pass *>::iterator I = LUses.begin(),
1002 E = LUses.end(); I != E; ++I) {
1003 llvm::cerr << "--" << std::string(Offset*2, ' ');
1004 (*I)->dumpPassStructure(0);
1008 void PMDataManager::dumpPassArguments() const {
1009 for(std::vector<Pass *>::const_iterator I = PassVector.begin(),
1010 E = PassVector.end(); I != E; ++I) {
1011 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
1012 PMD->dumpPassArguments();
1014 if (const PassInfo *PI = (*I)->getPassInfo())
1015 if (!PI->isAnalysisGroup())
1016 cerr << " -" << PI->getPassArgument();
1020 void PMDataManager:: dumpPassInfo(Pass *P, std::string &Msg1,
1021 std::string &Msg2) const {
1022 if (PassDebugging_New < Executions)
1024 cerr << (void*)this << std::string(getDepth()*2+1, ' ');
1026 cerr << P->getPassName();
1030 void PMDataManager::dumpAnalysisSetInfo(const char *Msg, Pass *P,
1031 const std::vector<AnalysisID> &Set)
1033 if (PassDebugging_New >= Details && !Set.empty()) {
1034 cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
1035 for (unsigned i = 0; i != Set.size(); ++i) {
1037 cerr << " " << Set[i]->getPassName();
1043 //===----------------------------------------------------------------------===//
1044 // NOTE: Is this the right place to define this method ?
1045 // getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
1046 Pass *AnalysisResolver::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
1047 return PM.findAnalysisPass(ID, dir);
1050 //===----------------------------------------------------------------------===//
1051 // BBPassManager implementation
1053 /// Add pass P into PassVector and return true. If this pass is not
1054 /// manageable by this manager then return false.
1056 BBPassManager::addPass(Pass *P) {
1058 BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P);
1062 // If this pass does not preserve analysis that is used by other passes
1063 // managed by this manager than it is not a suitable pass for this manager.
1064 if (!manageablePass(P))
1067 addPassToManager(BP);
1072 /// Execute all of the passes scheduled for execution by invoking
1073 /// runOnBasicBlock method. Keep track of whether any of the passes modifies
1074 /// the function, and if so, return true.
1076 BBPassManager::runOnFunction(Function &F) {
1081 bool Changed = doInitialization(F);
1083 std::string Msg1 = "Executing Pass '";
1084 std::string Msg3 = "' Made Modification '";
1086 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
1087 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1088 BasicBlockPass *BP = getContainedPass(Index);
1089 AnalysisUsage AnUsage;
1090 BP->getAnalysisUsage(AnUsage);
1092 std::string Msg2 = "' on BasicBlock '" + (*I).getName() + "'...\n";
1093 dumpPassInfo(BP, Msg1, Msg2);
1094 dumpAnalysisSetInfo("Required", BP, AnUsage.getRequiredSet());
1096 initializeAnalysisImpl(BP);
1098 if (TheTimeInfo) TheTimeInfo->passStarted(BP);
1099 Changed |= BP->runOnBasicBlock(*I);
1100 if (TheTimeInfo) TheTimeInfo->passEnded(BP);
1103 dumpPassInfo(BP, Msg3, Msg2);
1104 dumpAnalysisSetInfo("Preserved", BP, AnUsage.getPreservedSet());
1106 removeNotPreservedAnalysis(BP);
1107 recordAvailableAnalysis(BP);
1108 removeDeadPasses(BP, Msg2);
1110 return Changed |= doFinalization(F);
1113 // Implement doInitialization and doFinalization
1114 inline bool BBPassManager::doInitialization(Module &M) {
1115 bool Changed = false;
1117 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1118 BasicBlockPass *BP = getContainedPass(Index);
1119 Changed |= BP->doInitialization(M);
1125 inline bool BBPassManager::doFinalization(Module &M) {
1126 bool Changed = false;
1128 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1129 BasicBlockPass *BP = getContainedPass(Index);
1130 Changed |= BP->doFinalization(M);
1136 inline bool BBPassManager::doInitialization(Function &F) {
1137 bool Changed = false;
1139 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1140 BasicBlockPass *BP = getContainedPass(Index);
1141 Changed |= BP->doInitialization(F);
1147 inline bool BBPassManager::doFinalization(Function &F) {
1148 bool Changed = false;
1150 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1151 BasicBlockPass *BP = getContainedPass(Index);
1152 Changed |= BP->doFinalization(F);
1159 //===----------------------------------------------------------------------===//
1160 // FunctionPassManager implementation
1162 /// Create new Function pass manager
1163 FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
1164 FPM = new FunctionPassManagerImpl(0);
1165 // FPM is the top level manager.
1166 FPM->setTopLevelManager(FPM);
1168 PMDataManager *PMD = dynamic_cast<PMDataManager *>(FPM);
1169 AnalysisResolver *AR = new AnalysisResolver(*PMD);
1170 FPM->setResolver(AR);
1175 FunctionPassManager::~FunctionPassManager() {
1179 /// add - Add a pass to the queue of passes to run. This passes
1180 /// ownership of the Pass to the PassManager. When the
1181 /// PassManager_X is destroyed, the pass will be destroyed as well, so
1182 /// there is no need to delete the pass. (TODO delete passes.)
1183 /// This implies that all passes MUST be allocated with 'new'.
1184 void FunctionPassManager::add(Pass *P) {
1188 /// run - Execute all of the passes scheduled for execution. Keep
1189 /// track of whether any of the passes modifies the function, and if
1190 /// so, return true.
1192 bool FunctionPassManager::run(Function &F) {
1194 if (MP->materializeFunction(&F, &errstr)) {
1195 cerr << "Error reading bytecode file: " << errstr << "\n";
1202 /// doInitialization - Run all of the initializers for the function passes.
1204 bool FunctionPassManager::doInitialization() {
1205 return FPM->doInitialization(*MP->getModule());
1208 /// doFinalization - Run all of the initializers for the function passes.
1210 bool FunctionPassManager::doFinalization() {
1211 return FPM->doFinalization(*MP->getModule());
1214 //===----------------------------------------------------------------------===//
1215 // FunctionPassManagerImpl implementation
1217 /// Add P into active pass manager or use new module pass manager to
1219 bool FunctionPassManagerImpl::addPass(Pass *P) {
1221 if (!activeManager || !activeManager->addPass(P)) {
1222 activeManager = new FPPassManager(getDepth() + 1);
1223 // Inherit top level manager
1224 activeManager->setTopLevelManager(this->getTopLevelManager());
1226 // This top level manager is going to manage activeManager.
1227 // Set up analysis resolver to connect them.
1228 AnalysisResolver *AR = new AnalysisResolver(*this);
1229 activeManager->setResolver(AR);
1231 addPassManager(activeManager);
1232 return activeManager->addPass(P);
1237 inline bool FunctionPassManagerImpl::doInitialization(Module &M) {
1238 bool Changed = false;
1240 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1241 FPPassManager *FP = getContainedManager(Index);
1242 Changed |= FP->doInitialization(M);
1248 inline bool FunctionPassManagerImpl::doFinalization(Module &M) {
1249 bool Changed = false;
1251 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1252 FPPassManager *FP = getContainedManager(Index);
1253 Changed |= FP->doFinalization(M);
1259 // Execute all the passes managed by this top level manager.
1260 // Return true if any function is modified by a pass.
1261 bool FunctionPassManagerImpl::run(Function &F) {
1263 bool Changed = false;
1265 TimingInfo::createTheTimeInfo();
1270 initializeAllAnalysisInfo();
1271 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1272 FPPassManager *FP = getContainedManager(Index);
1273 Changed |= FP->runOnFunction(F);
1278 //===----------------------------------------------------------------------===//
1279 // FPPassManager implementation
1281 /// Add pass P into the pass manager queue. If P is a BasicBlockPass then
1282 /// either use it into active basic block pass manager or create new basic
1283 /// block pass manager to handle pass P.
1285 FPPassManager::addPass(Pass *P) {
1287 // If P is a BasicBlockPass then use BBPassManager.
1288 if (BasicBlockPass *BP = dynamic_cast<BasicBlockPass*>(P)) {
1290 if (!activeBBPassManager || !activeBBPassManager->addPass(BP)) {
1292 // If active manager exists then clear its analysis info.
1293 if (activeBBPassManager)
1294 activeBBPassManager->initializeAnalysisInfo();
1296 // Create and add new manager
1297 activeBBPassManager = new BBPassManager(getDepth() + 1);
1298 // Inherit top level manager
1299 activeBBPassManager->setTopLevelManager(this->getTopLevelManager());
1301 // Add new manager into current manager's list.
1302 addPassToManager(activeBBPassManager, false);
1304 // Add new manager into top level manager's indirect passes list
1305 PMDataManager *PMD = dynamic_cast<PMDataManager *>(activeBBPassManager);
1306 assert (PMD && "Manager is not Pass Manager");
1307 TPM->addIndirectPassManager(PMD);
1309 // Add pass into new manager. This time it must succeed.
1310 if (!activeBBPassManager->addPass(BP))
1311 assert(0 && "Unable to add Pass");
1313 // If activeBBPassManager transfered any Last Uses then handle them here.
1314 std::vector<Pass *> &TLU = activeBBPassManager->getTransferredLastUses();
1316 TPM->setLastUser(TLU, this);
1323 FunctionPass *FP = dynamic_cast<FunctionPass *>(P);
1327 // If this pass does not preserve analysis that is used by other passes
1328 // managed by this manager than it is not a suitable pass for this manager.
1329 if (!manageablePass(P))
1332 addPassToManager (FP);
1334 // If active manager exists then clear its analysis info.
1335 if (activeBBPassManager) {
1336 activeBBPassManager->initializeAnalysisInfo();
1337 activeBBPassManager = NULL;
1343 /// Execute all of the passes scheduled for execution by invoking
1344 /// runOnFunction method. Keep track of whether any of the passes modifies
1345 /// the function, and if so, return true.
1346 bool FPPassManager::runOnFunction(Function &F) {
1348 bool Changed = false;
1353 std::string Msg1 = "Executing Pass '";
1354 std::string Msg3 = "' Made Modification '";
1356 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1357 FunctionPass *FP = getContainedPass(Index);
1359 AnalysisUsage AnUsage;
1360 FP->getAnalysisUsage(AnUsage);
1362 std::string Msg2 = "' on Function '" + F.getName() + "'...\n";
1363 dumpPassInfo(FP, Msg1, Msg2);
1364 dumpAnalysisSetInfo("Required", FP, AnUsage.getRequiredSet());
1366 initializeAnalysisImpl(FP);
1368 if (TheTimeInfo) TheTimeInfo->passStarted(FP);
1369 Changed |= FP->runOnFunction(F);
1370 if (TheTimeInfo) TheTimeInfo->passEnded(FP);
1373 dumpPassInfo(FP, Msg3, Msg2);
1374 dumpAnalysisSetInfo("Preserved", FP, AnUsage.getPreservedSet());
1376 removeNotPreservedAnalysis(FP);
1377 recordAvailableAnalysis(FP);
1378 removeDeadPasses(FP, Msg2);
1383 bool FPPassManager::runOnModule(Module &M) {
1385 bool Changed = doInitialization(M);
1387 for(Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1388 this->runOnFunction(*I);
1390 return Changed |= doFinalization(M);
1393 inline bool FPPassManager::doInitialization(Module &M) {
1394 bool Changed = false;
1396 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1397 FunctionPass *FP = getContainedPass(Index);
1398 Changed |= FP->doInitialization(M);
1404 inline bool FPPassManager::doFinalization(Module &M) {
1405 bool Changed = false;
1407 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1408 FunctionPass *FP = getContainedPass(Index);
1409 Changed |= FP->doFinalization(M);
1415 //===----------------------------------------------------------------------===//
1416 // MPPassManager implementation
1418 /// Add P into pass vector if it is manageble. If P is a FunctionPass
1419 /// then use FPPassManager to manage it. Return false if P
1420 /// is not manageable by this manager.
1422 MPPassManager::addPass(Pass *P) {
1424 // If P is FunctionPass then use function pass maanager.
1425 if (FunctionPass *FP = dynamic_cast<FunctionPass*>(P)) {
1427 if (!activeFunctionPassManager || !activeFunctionPassManager->addPass(P)) {
1429 // If active manager exists then clear its analysis info.
1430 if (activeFunctionPassManager)
1431 activeFunctionPassManager->initializeAnalysisInfo();
1433 // Create and add new manager
1434 activeFunctionPassManager =
1435 new FPPassManager(getDepth() + 1);
1437 // Add new manager into current manager's list
1438 addPassToManager(activeFunctionPassManager, false);
1440 // Inherit top level manager
1441 activeFunctionPassManager->setTopLevelManager(this->getTopLevelManager());
1443 // Add new manager into top level manager's indirect passes list
1444 PMDataManager *PMD =
1445 dynamic_cast<PMDataManager *>(activeFunctionPassManager);
1446 assert(PMD && "Manager is not Pass Manager");
1447 TPM->addIndirectPassManager(PMD);
1449 // Add pass into new manager. This time it must succeed.
1450 if (!activeFunctionPassManager->addPass(FP))
1451 assert(0 && "Unable to add pass");
1453 // If activeFunctionPassManager transfered any Last Uses then
1454 // handle them here.
1455 std::vector<Pass *> &TLU =
1456 activeFunctionPassManager->getTransferredLastUses();
1458 TPM->setLastUser(TLU, this);
1464 ModulePass *MP = dynamic_cast<ModulePass *>(P);
1468 // If this pass does not preserve analysis that is used by other passes
1469 // managed by this manager than it is not a suitable pass for this manager.
1470 if (!manageablePass(P))
1473 addPassToManager(MP);
1474 // If active manager exists then clear its analysis info.
1475 if (activeFunctionPassManager) {
1476 activeFunctionPassManager->initializeAnalysisInfo();
1477 activeFunctionPassManager = NULL;
1484 /// Execute all of the passes scheduled for execution by invoking
1485 /// runOnModule method. Keep track of whether any of the passes modifies
1486 /// the module, and if so, return true.
1488 MPPassManager::runOnModule(Module &M) {
1489 bool Changed = false;
1491 std::string Msg1 = "Executing Pass '";
1492 std::string Msg3 = "' Made Modification '";
1494 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1495 ModulePass *MP = getContainedPass(Index);
1497 AnalysisUsage AnUsage;
1498 MP->getAnalysisUsage(AnUsage);
1500 std::string Msg2 = "' on Module '" + M.getModuleIdentifier() + "'...\n";
1501 dumpPassInfo(MP, Msg1, Msg2);
1502 dumpAnalysisSetInfo("Required", MP, AnUsage.getRequiredSet());
1504 initializeAnalysisImpl(MP);
1506 if (TheTimeInfo) TheTimeInfo->passStarted(MP);
1507 Changed |= MP->runOnModule(M);
1508 if (TheTimeInfo) TheTimeInfo->passEnded(MP);
1511 dumpPassInfo(MP, Msg3, Msg2);
1512 dumpAnalysisSetInfo("Preserved", MP, AnUsage.getPreservedSet());
1514 removeNotPreservedAnalysis(MP);
1515 recordAvailableAnalysis(MP);
1516 removeDeadPasses(MP, Msg2);
1521 //===----------------------------------------------------------------------===//
1522 // PassManagerImpl implementation
1524 /// Add P into active pass manager or use new module pass manager to
1526 bool PassManagerImpl::addPass(Pass *P) {
1528 if (!activeManager || !activeManager->addPass(P)) {
1530 activeManager = new MPPassManager(getDepth() + 1);
1532 // Inherit top level manager
1533 activeManager->setTopLevelManager(this->getTopLevelManager());
1535 // This top level manager is going to manage activeManager.
1536 // Set up analysis resolver to connect them.
1537 AnalysisResolver *AR = new AnalysisResolver(*this);
1538 activeManager->setResolver(AR);
1540 addPassManager(activeManager);
1541 return activeManager->addPass(P);
1546 /// run - Execute all of the passes scheduled for execution. Keep track of
1547 /// whether any of the passes modifies the module, and if so, return true.
1548 bool PassManagerImpl::run(Module &M) {
1550 bool Changed = false;
1552 TimingInfo::createTheTimeInfo();
1557 initializeAllAnalysisInfo();
1558 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1559 MPPassManager *MP = getContainedManager(Index);
1560 Changed |= MP->runOnModule(M);
1565 //===----------------------------------------------------------------------===//
1566 // PassManager implementation
1568 /// Create new pass manager
1569 PassManager::PassManager() {
1570 PM = new PassManagerImpl(0);
1571 // PM is the top level manager
1572 PM->setTopLevelManager(PM);
1575 PassManager::~PassManager() {
1579 /// add - Add a pass to the queue of passes to run. This passes ownership of
1580 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
1581 /// will be destroyed as well, so there is no need to delete the pass. This
1582 /// implies that all passes MUST be allocated with 'new'.
1584 PassManager::add(Pass *P) {
1588 /// run - Execute all of the passes scheduled for execution. Keep track of
1589 /// whether any of the passes modifies the module, and if so, return true.
1591 PassManager::run(Module &M) {
1595 //===----------------------------------------------------------------------===//
1596 // TimingInfo Class - This class is used to calculate information about the
1597 // amount of time each pass takes to execute. This only happens with
1598 // -time-passes is enabled on the command line.
1600 bool llvm::TimePassesIsEnabled = false;
1601 static cl::opt<bool,true>
1602 EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1603 cl::desc("Time each pass, printing elapsed time for each on exit"));
1605 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1606 // a non null value (if the -time-passes option is enabled) or it leaves it
1607 // null. It may be called multiple times.
1608 void TimingInfo::createTheTimeInfo() {
1609 if (!TimePassesIsEnabled || TheTimeInfo) return;
1611 // Constructed the first time this is called, iff -time-passes is enabled.
1612 // This guarantees that the object will be constructed before static globals,
1613 // thus it will be destroyed before them.
1614 static ManagedStatic<TimingInfo> TTI;
1615 TheTimeInfo = &*TTI;
1618 //===----------------------------------------------------------------------===//
1619 // PMStack implementation
1621 // Pop Pass Manager from the stack and clear its analysis info.
1622 void PMStack::pop() {
1624 PMDataManager *Top = this->top();
1625 Top->initializeAnalysisInfo();
1630 // Push PM on the stack and set its top level manager.
1631 void PMStack::push(Pass *P) {
1633 PMDataManager *Top = NULL;
1634 PMDataManager *PM = dynamic_cast<PMDataManager *>(P);
1635 assert (PM && "Unable to push. Pass Manager expected");
1637 if (this->empty()) {
1642 PMTopLevelManager *TPM = Top->getTopLevelManager();
1644 assert (TPM && "Unable to find top level manager");
1645 TPM->addIndirectPassManager(PM);
1646 PM->setTopLevelManager(TPM);
1649 AnalysisResolver *AR = new AnalysisResolver(*Top);
1655 // Dump content of the pass manager stack.
1656 void PMStack::dump() {
1657 for(std::deque<PMDataManager *>::iterator I = S.begin(),
1658 E = S.end(); I != E; ++I) {
1659 Pass *P = dynamic_cast<Pass *>(*I);
1660 printf ("%s ", P->getPassName());
1666 // Walk Pass Manager stack and set LastUse markers if any
1667 // manager is transfering this priviledge to its parent manager
1668 void PMStack::handleLastUserOverflow() {
1670 for(PMStack::iterator I = this->begin(), E = this->end(); I != E;) {
1672 PMDataManager *Child = *I++;
1674 PMDataManager *Parent = *I++;
1675 PMTopLevelManager *TPM = Parent->getTopLevelManager();
1676 std::vector<Pass *> &TLU = Child->getTransferredLastUses();
1678 Pass *P = dynamic_cast<Pass *>(Parent);
1679 TPM->setLastUser(TLU, P);
1685 /// Find appropriate Module Pass Manager in the PM Stack and
1686 /// add self into that manager.
1687 void ModulePass::assignPassManager(PMStack &PMS) {
1689 MPPassManager *MPP = NULL;
1691 // Find Module Pass Manager
1692 while(!PMS.empty()) {
1694 MPP = dynamic_cast<MPPassManager *>(PMS.top());
1698 PMS.pop(); // Pop children pass managers
1701 assert(MPP && "Unable to find Module Pass Manager");
1702 MPP->addPassToManager(this);
1705 /// Find appropriate Function Pass Manager or Call Graph Pass Manager
1706 /// in the PM Stack and add self into that manager.
1707 void FunctionPass::assignPassManager(PMStack &PMS) {
1709 FPPassManager *FPP = NULL;
1711 // Find Module Pass Manager (TODO : Or Call Graph Pass Manager)
1712 while(!PMS.empty()) {
1714 FPP = dynamic_cast<FPPassManager *>(PMS.top());
1716 break; // Found Function Pass Manager
1717 else if (dynamic_cast<BBPassManager *>(PMS.top()))
1718 PMS.pop(); // Pop Basic Block Pass Manager
1719 // TODO : else if Pop Loop Pass Manager
1721 break; // PMS.top() is either Module Pass Manager or Call Graph
1725 // Create new Function Pass Manager
1727 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1728 PMDataManager *PMD = PMS.top();
1730 // [1] Create new Function Pass Manager
1731 FPP = new FPPassManager(PMD->getDepth() + 1);
1733 // [2] Set up new manager's top level manager
1734 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1735 TPM->addIndirectPassManager(FPP);
1737 // [3] Assign manager to manage this new manager. This may create
1738 // and push new managers into PMS
1739 Pass *P = dynamic_cast<Pass *>(FPP);
1740 P->assignPassManager(PMS);
1742 // [4] Push new manager into PMS
1746 // Assign FPP as the manager of this pass.
1747 FPP->addPassToManager(this);
1750 /// Find appropriate Basic Pass Manager or Call Graph Pass Manager
1751 /// in the PM Stack and add self into that manager.
1752 void BasicBlockPass::assignPassManager(PMStack &PMS) {
1754 BBPassManager *BBP = NULL;
1756 // Basic Pass Manager is a leaf pass manager. It does not handle
1757 // any other pass manager.
1759 BBP = dynamic_cast<BBPassManager *>(PMS.top());
1762 // If leaf manager is not Basic Block Pass manager then create new
1763 // basic Block Pass manager.
1766 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1767 PMDataManager *PMD = PMS.top();
1769 // [1] Create new Basic Block Manager
1770 BBP = new BBPassManager(PMD->getDepth() + 1);
1772 // [2] Set up new manager's top level manager
1773 // Basic Block Pass Manager does not live by itself
1774 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1775 TPM->addIndirectPassManager(BBP);
1777 // [3] Assign manager to manage this new manager. This may create
1778 // and push new managers into PMS
1779 Pass *P = dynamic_cast<Pass *>(BBP);
1780 P->assignPassManager(PMS);
1782 // [4] Push new manager into PMS
1786 // Assign BBP as the manager of this pass.
1787 BBP->addPassToManager(this);