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;
29 //===----------------------------------------------------------------------===//
31 // The Pass Manager Infrastructure manages passes. It's responsibilities are:
33 // o Manage optimization pass execution order
34 // o Make required Analysis information available before pass P is run
35 // o Release memory occupied by dead passes
36 // o If Analysis information is dirtied by a pass then regenerate Analysis
37 // information before it is consumed by another pass.
39 // Pass Manager Infrastructure uses multiple pass managers. They are
40 // PassManager, FunctionPassManager, MPPassManager, FPPassManager, BBPassManager.
41 // This class hierarcy uses multiple inheritance but pass managers do not derive
42 // from another pass manager.
44 // PassManager and FunctionPassManager are two top-level pass manager that
45 // represents the external interface of this entire pass manager infrastucture.
47 // Important classes :
49 // [o] class PMTopLevelManager;
51 // Two top level managers, PassManager and FunctionPassManager, derive from
52 // PMTopLevelManager. PMTopLevelManager manages information used by top level
53 // managers such as last user info.
55 // [o] class PMDataManager;
57 // PMDataManager manages information, e.g. list of available analysis info,
58 // used by a pass manager to manage execution order of passes. It also provides
59 // a place to implement common pass manager APIs. All pass managers derive from
62 // [o] class BBPassManager : public FunctionPass, public PMDataManager;
64 // BBPassManager manages BasicBlockPasses.
66 // [o] class FunctionPassManager;
68 // This is a external interface used by JIT to manage FunctionPasses. This
69 // interface relies on FunctionPassManagerImpl to do all the tasks.
71 // [o] class FunctionPassManagerImpl : public ModulePass, PMDataManager,
72 // public PMTopLevelManager;
74 // FunctionPassManagerImpl is a top level manager. It manages FPPassManagers
76 // [o] class FPPassManager : public ModulePass, public PMDataManager;
78 // FPPassManager manages FunctionPasses and BBPassManagers
80 // [o] class MPPassManager : public Pass, public PMDataManager;
82 // MPPassManager manages ModulePasses and FPPassManagers
84 // [o] class PassManager;
86 // This is a external interface used by various tools to manages passes. It
87 // relies on PassManagerImpl to do all the tasks.
89 // [o] class PassManagerImpl : public Pass, public PMDataManager,
90 // public PMDTopLevelManager
92 // PassManagerImpl is a top level pass manager responsible for managing
94 //===----------------------------------------------------------------------===//
98 //===----------------------------------------------------------------------===//
99 // Pass debugging information. Often it is useful to find out what pass is
100 // running when a crash occurs in a utility. When this library is compiled with
101 // debugging on, a command line option (--debug-pass) is enabled that causes the
102 // pass name to be printed before it executes.
105 // Different debug levels that can be enabled...
106 enum PassDebugLevel {
107 None, Arguments, Structure, Executions, Details
110 static cl::opt<enum PassDebugLevel>
111 PassDebugging_New("debug-pass", cl::Hidden,
112 cl::desc("Print PassManager debugging information"),
114 clEnumVal(None , "disable debug output"),
115 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
116 clEnumVal(Structure , "print pass structure before run()"),
117 clEnumVal(Executions, "print pass name before it is executed"),
118 clEnumVal(Details , "print pass details when it is executed"),
120 } // End of llvm namespace
124 //===----------------------------------------------------------------------===//
127 /// PMTopLevelManager manages LastUser info and collects common APIs used by
128 /// top level pass managers.
129 class VISIBILITY_HIDDEN PMTopLevelManager {
132 virtual unsigned getNumContainedManagers() {
133 return PassManagers.size();
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 virtual ~PMTopLevelManager() {
157 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
158 E = PassManagers.end(); I != E; ++I)
161 for (std::vector<ImmutablePass *>::iterator
162 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
165 PassManagers.clear();
168 /// Add immutable pass and initialize it.
169 inline void addImmutablePass(ImmutablePass *P) {
171 ImmutablePasses.push_back(P);
174 inline std::vector<ImmutablePass *>& getImmutablePasses() {
175 return ImmutablePasses;
178 void addPassManager(Pass *Manager) {
179 PassManagers.push_back(Manager);
182 // Add Manager into the list of managers that are not directly
183 // maintained by this top level pass manager
184 inline void addIndirectPassManager(PMDataManager *Manager) {
185 IndirectPassManagers.push_back(Manager);
188 // Print passes managed by this top level manager.
189 void dumpPasses() const;
190 void dumpArguments() const;
192 void initializeAllAnalysisInfo();
194 // Active Pass Managers
199 /// Collection of pass managers
200 std::vector<Pass *> PassManagers;
204 /// Collection of pass managers that are not directly maintained
205 /// by this pass manager
206 std::vector<PMDataManager *> IndirectPassManagers;
208 // Map to keep track of last user of the analysis pass.
209 // LastUser->second is the last user of Lastuser->first.
210 std::map<Pass *, Pass *> LastUser;
212 /// Immutable passes are managed by top level manager.
213 std::vector<ImmutablePass *> ImmutablePasses;
216 } // End of anon namespace
218 //===----------------------------------------------------------------------===//
222 /// PMDataManager provides the common place to manage the analysis data
223 /// used by pass managers.
224 class PMDataManager {
226 PMDataManager(int Depth) : TPM(NULL), Depth(Depth) {
227 initializeAnalysisInfo();
230 virtual ~PMDataManager() {
232 for (std::vector<Pass *>::iterator I = PassVector.begin(),
233 E = PassVector.end(); I != E; ++I)
239 /// Return true IFF pass P's required analysis set does not required new
241 bool manageablePass(Pass *P);
243 /// Augment AvailableAnalysis by adding analysis made available by pass P.
244 void recordAvailableAnalysis(Pass *P);
246 /// Remove Analysis that is not preserved by the pass
247 void removeNotPreservedAnalysis(Pass *P);
249 /// Remove dead passes
250 void removeDeadPasses(Pass *P, std::string &Msg);
252 /// Add pass P into the PassVector. Update
253 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
254 void addPassToManager(Pass *P, bool ProcessAnalysis = true);
256 /// Initialize available analysis information.
257 void initializeAnalysisInfo() {
258 TransferLastUses.clear();
259 AvailableAnalysis.clear();
262 /// Populate RequiredPasses with the analysis pass that are required by
264 void collectRequiredAnalysisPasses(std::vector<Pass *> &RequiredPasses,
267 /// All Required analyses should be available to the pass as it runs! Here
268 /// we fill in the AnalysisImpls member of the pass so that it can
269 /// successfully use the getAnalysis() method to retrieve the
270 /// implementations it needs.
271 void initializeAnalysisImpl(Pass *P);
273 /// Find the pass that implements Analysis AID. If desired pass is not found
274 /// then return NULL.
275 Pass *findAnalysisPass(AnalysisID AID, bool Direction);
277 // Access toplevel manager
278 PMTopLevelManager *getTopLevelManager() { return TPM; }
279 void setTopLevelManager(PMTopLevelManager *T) { TPM = T; }
281 unsigned getDepth() const { return Depth; }
283 // Print routines used by debug-pass
284 void dumpLastUses(Pass *P, unsigned Offset) const;
285 void dumpPassArguments() const;
286 void dumpPassInfo(Pass *P, std::string &Msg1, std::string &Msg2) const;
287 void dumpAnalysisSetInfo(const char *Msg, Pass *P,
288 const std::vector<AnalysisID> &Set) const;
290 std::vector<Pass *>& getTransferredLastUses() {
291 return TransferLastUses;
294 virtual unsigned getNumContainedPasses() {
295 return PassVector.size();
298 virtual PassManagerType getPassManagerType() {
299 assert ( 0 && "Invalid use of getPassManagerType");
304 // If a FunctionPass F is the last user of ModulePass info M
305 // then the F's manager, not F, records itself as a last user of M.
306 // Current pass manage is requesting parent manager to record parent
307 // manager as the last user of these TrransferLastUses passes.
308 std::vector<Pass *> TransferLastUses;
310 // Top level manager.
311 PMTopLevelManager *TPM;
313 // Collection of pass that are managed by this manager
314 std::vector<Pass *> PassVector;
317 // Set of available Analysis. This information is used while scheduling
318 // pass. If a pass requires an analysis which is not not available then
319 // equired analysis pass is scheduled to run before the pass itself is
321 std::map<AnalysisID, Pass*> AvailableAnalysis;
326 //===----------------------------------------------------------------------===//
329 /// BBPassManager manages BasicBlockPass. It batches all the
330 /// pass together and sequence them to process one basic block before
331 /// processing next basic block.
332 class VISIBILITY_HIDDEN BBPassManager : public PMDataManager,
333 public FunctionPass {
336 BBPassManager(int Depth) : PMDataManager(Depth) { }
338 /// Execute all of the passes scheduled for execution. Keep track of
339 /// whether any of the passes modifies the function, and if so, return true.
340 bool runOnFunction(Function &F);
342 /// Pass Manager itself does not invalidate any analysis info.
343 void getAnalysisUsage(AnalysisUsage &Info) const {
344 Info.setPreservesAll();
347 bool doInitialization(Module &M);
348 bool doInitialization(Function &F);
349 bool doFinalization(Module &M);
350 bool doFinalization(Function &F);
352 // Print passes managed by this manager
353 void dumpPassStructure(unsigned Offset) {
354 llvm::cerr << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n";
355 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
356 BasicBlockPass *BP = getContainedPass(Index);
357 BP->dumpPassStructure(Offset + 1);
358 dumpLastUses(BP, Offset+1);
362 BasicBlockPass *getContainedPass(unsigned N) {
363 assert ( N < PassVector.size() && "Pass number out of range!");
364 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
368 virtual PassManagerType getPassManagerType() {
369 return PMT_BasicBlockPassManager;
373 //===----------------------------------------------------------------------===//
376 /// FPPassManager manages BBPassManagers and FunctionPasses.
377 /// It batches all function passes and basic block pass managers together and
378 /// sequence them to process one function at a time before processing next
381 class FPPassManager : public ModulePass, public PMDataManager {
384 FPPassManager(int Depth) : PMDataManager(Depth) { }
386 /// run - Execute all of the passes scheduled for execution. Keep track of
387 /// whether any of the passes modifies the module, and if so, return true.
388 bool runOnFunction(Function &F);
389 bool runOnModule(Module &M);
391 /// doInitialization - Run all of the initializers for the function passes.
393 bool doInitialization(Module &M);
395 /// doFinalization - Run all of the initializers for the function passes.
397 bool doFinalization(Module &M);
399 /// Pass Manager itself does not invalidate any analysis info.
400 void getAnalysisUsage(AnalysisUsage &Info) const {
401 Info.setPreservesAll();
404 // Print passes managed by this manager
405 void dumpPassStructure(unsigned Offset) {
406 llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
407 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
408 FunctionPass *FP = getContainedPass(Index);
409 FP->dumpPassStructure(Offset + 1);
410 dumpLastUses(FP, Offset+1);
414 FunctionPass *getContainedPass(unsigned N) {
415 assert ( N < PassVector.size() && "Pass number out of range!");
416 FunctionPass *FP = static_cast<FunctionPass *>(PassVector[N]);
420 virtual PassManagerType getPassManagerType() {
421 return PMT_FunctionPassManager;
425 //===----------------------------------------------------------------------===//
426 // FunctionPassManagerImpl
428 /// FunctionPassManagerImpl manages FPPassManagers
429 class FunctionPassManagerImpl : public Pass,
430 public PMDataManager,
431 public PMTopLevelManager {
434 FunctionPassManagerImpl(int Depth) : PMDataManager(Depth) { }
436 /// add - Add a pass to the queue of passes to run. This passes ownership of
437 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
438 /// will be destroyed as well, so there is no need to delete the pass. This
439 /// implies that all passes MUST be allocated with 'new'.
444 /// run - Execute all of the passes scheduled for execution. Keep track of
445 /// whether any of the passes modifies the module, and if so, return true.
446 bool run(Function &F);
448 /// doInitialization - Run all of the initializers for the function passes.
450 bool doInitialization(Module &M);
452 /// doFinalization - Run all of the initializers for the function passes.
454 bool doFinalization(Module &M);
456 /// Pass Manager itself does not invalidate any analysis info.
457 void getAnalysisUsage(AnalysisUsage &Info) const {
458 Info.setPreservesAll();
461 inline void addTopLevelPass(Pass *P) {
463 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
465 // P is a immutable pass and it will be managed by this
466 // top level manager. Set up analysis resolver to connect them.
467 AnalysisResolver *AR = new AnalysisResolver(*this);
469 initializeAnalysisImpl(P);
470 addImmutablePass(IP);
471 recordAvailableAnalysis(IP);
474 if (activeStack.empty()) {
475 FPPassManager *FPP = new FPPassManager(getDepth() + 1);
476 FPP->setTopLevelManager(this->getTopLevelManager());
478 activeStack.push(FPP);
480 P->assignPassManager(activeStack);
485 FPPassManager *getContainedManager(unsigned N) {
486 assert ( N < PassManagers.size() && "Pass number out of range!");
487 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
493 //===----------------------------------------------------------------------===//
496 /// MPPassManager manages ModulePasses and function pass managers.
497 /// It batches all Module passes passes and function pass managers together and
498 /// sequence them to process one module.
499 class MPPassManager : public Pass, public PMDataManager {
502 MPPassManager(int Depth) : PMDataManager(Depth) { }
504 /// run - Execute all of the passes scheduled for execution. Keep track of
505 /// whether any of the passes modifies the module, and if so, return true.
506 bool runOnModule(Module &M);
508 /// Pass Manager itself does not invalidate any analysis info.
509 void getAnalysisUsage(AnalysisUsage &Info) const {
510 Info.setPreservesAll();
513 // Print passes managed by this manager
514 void dumpPassStructure(unsigned Offset) {
515 llvm::cerr << std::string(Offset*2, ' ') << "ModulePass Manager\n";
516 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
517 ModulePass *MP = getContainedPass(Index);
518 MP->dumpPassStructure(Offset + 1);
519 dumpLastUses(MP, Offset+1);
523 ModulePass *getContainedPass(unsigned N) {
524 assert ( N < PassVector.size() && "Pass number out of range!");
525 ModulePass *MP = static_cast<ModulePass *>(PassVector[N]);
529 virtual PassManagerType getPassManagerType() { return PMT_ModulePassManager; }
532 //===----------------------------------------------------------------------===//
535 /// PassManagerImpl manages MPPassManagers
536 class PassManagerImpl : public Pass,
537 public PMDataManager,
538 public PMTopLevelManager {
542 PassManagerImpl(int Depth) : PMDataManager(Depth) { }
544 /// add - Add a pass to the queue of passes to run. This passes ownership of
545 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
546 /// will be destroyed as well, so there is no need to delete the pass. This
547 /// implies that all passes MUST be allocated with 'new'.
552 /// run - Execute all of the passes scheduled for execution. Keep track of
553 /// whether any of the passes modifies the module, and if so, return true.
556 /// Pass Manager itself does not invalidate any analysis info.
557 void getAnalysisUsage(AnalysisUsage &Info) const {
558 Info.setPreservesAll();
561 inline void addTopLevelPass(Pass *P) {
563 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
565 // P is a immutable pass and it will be managed by this
566 // top level manager. Set up analysis resolver to connect them.
567 AnalysisResolver *AR = new AnalysisResolver(*this);
569 initializeAnalysisImpl(P);
570 addImmutablePass(IP);
571 recordAvailableAnalysis(IP);
575 if (activeStack.empty()) {
576 MPPassManager *MPP = new MPPassManager(getDepth() + 1);
577 MPP->setTopLevelManager(this->getTopLevelManager());
579 activeStack.push(MPP);
582 P->assignPassManager(activeStack);
587 MPPassManager *getContainedManager(unsigned N) {
588 assert ( N < PassManagers.size() && "Pass number out of range!");
589 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
595 } // End of llvm namespace
599 //===----------------------------------------------------------------------===//
600 // TimingInfo Class - This class is used to calculate information about the
601 // amount of time each pass takes to execute. This only happens when
602 // -time-passes is enabled on the command line.
605 class VISIBILITY_HIDDEN TimingInfo {
606 std::map<Pass*, Timer> TimingData;
610 // Use 'create' member to get this.
611 TimingInfo() : TG("... Pass execution timing report ...") {}
613 // TimingDtor - Print out information about timing information
615 // Delete all of the timers...
617 // TimerGroup is deleted next, printing the report.
620 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
621 // to a non null value (if the -time-passes option is enabled) or it leaves it
622 // null. It may be called multiple times.
623 static void createTheTimeInfo();
625 void passStarted(Pass *P) {
627 if (dynamic_cast<PMDataManager *>(P))
630 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
631 if (I == TimingData.end())
632 I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
633 I->second.startTimer();
635 void passEnded(Pass *P) {
637 if (dynamic_cast<PMDataManager *>(P))
640 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
641 assert (I != TimingData.end() && "passStarted/passEnded not nested right!");
642 I->second.stopTimer();
646 static TimingInfo *TheTimeInfo;
648 } // End of anon namespace
650 //===----------------------------------------------------------------------===//
651 // PMTopLevelManager implementation
653 /// Set pass P as the last user of the given analysis passes.
654 void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses,
657 for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(),
658 E = AnalysisPasses.end(); I != E; ++I) {
661 // If AP is the last user of other passes then make P last user of
663 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
664 LUE = LastUser.end(); LUI != LUE; ++LUI) {
665 if (LUI->second == AP)
666 LastUser[LUI->first] = P;
671 /// Collect passes whose last user is P
672 void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses,
674 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
675 LUE = LastUser.end(); LUI != LUE; ++LUI)
676 if (LUI->second == P)
677 LastUses.push_back(LUI->first);
680 /// Schedule pass P for execution. Make sure that passes required by
681 /// P are run before P is run. Update analysis info maintained by
682 /// the manager. Remove dead passes. This is a recursive function.
683 void PMTopLevelManager::schedulePass(Pass *P) {
685 // TODO : Allocate function manager for this pass, other wise required set
686 // may be inserted into previous function manager
688 AnalysisUsage AnUsage;
689 P->getAnalysisUsage(AnUsage);
690 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
691 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
692 E = RequiredSet.end(); I != E; ++I) {
694 Pass *AnalysisPass = findAnalysisPass(*I);
696 // Schedule this analysis run first.
697 AnalysisPass = (*I)->createPass();
698 schedulePass(AnalysisPass);
702 // Now all required passes are available.
706 /// Find the pass that implements Analysis AID. Search immutable
707 /// passes and all pass managers. If desired pass is not found
708 /// then return NULL.
709 Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
712 // Check pass managers
713 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
714 E = PassManagers.end(); P == NULL && I != E; ++I) {
715 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
716 assert(PMD && "This is not a PassManager");
717 P = PMD->findAnalysisPass(AID, false);
720 // Check other pass managers
721 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
722 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
723 P = (*I)->findAnalysisPass(AID, false);
725 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
726 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
727 const PassInfo *PI = (*I)->getPassInfo();
731 // If Pass not found then check the interfaces implemented by Immutable Pass
733 const std::vector<const PassInfo*> &ImmPI = PI->getInterfacesImplemented();
734 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
742 // Print passes managed by this top level manager.
743 void PMTopLevelManager::dumpPasses() const {
745 if (PassDebugging_New < Structure)
748 // Print out the immutable passes
749 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
750 ImmutablePasses[i]->dumpPassStructure(0);
753 for (std::vector<Pass *>::const_iterator I = PassManagers.begin(),
754 E = PassManagers.end(); I != E; ++I)
755 (*I)->dumpPassStructure(1);
758 void PMTopLevelManager::dumpArguments() const {
760 if (PassDebugging_New < Arguments)
763 cerr << "Pass Arguments: ";
764 for (std::vector<Pass *>::const_iterator I = PassManagers.begin(),
765 E = PassManagers.end(); I != E; ++I) {
766 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
767 assert(PMD && "This is not a PassManager");
768 PMD->dumpPassArguments();
773 void PMTopLevelManager::initializeAllAnalysisInfo() {
775 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
776 E = PassManagers.end(); I != E; ++I) {
777 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
778 assert(PMD && "This is not a PassManager");
779 PMD->initializeAnalysisInfo();
782 // Initailize other pass managers
783 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
784 E = IndirectPassManagers.end(); I != E; ++I)
785 (*I)->initializeAnalysisInfo();
788 //===----------------------------------------------------------------------===//
789 // PMDataManager implementation
791 /// Return true IFF pass P's required analysis set does not required new
793 bool PMDataManager::manageablePass(Pass *P) {
796 // If this pass is not preserving information that is required by a
797 // pass maintained by higher level pass manager then do not insert
798 // this pass into current manager. Use new manager. For example,
799 // For example, If FunctionPass F is not preserving ModulePass Info M1
800 // that is used by another ModulePass M2 then do not insert F in
801 // current function pass manager.
805 /// Augement AvailableAnalysis by adding analysis made available by pass P.
806 void PMDataManager::recordAvailableAnalysis(Pass *P) {
808 if (const PassInfo *PI = P->getPassInfo()) {
809 AvailableAnalysis[PI] = P;
811 //This pass is the current implementation of all of the interfaces it
812 //implements as well.
813 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
814 for (unsigned i = 0, e = II.size(); i != e; ++i)
815 AvailableAnalysis[II[i]] = P;
819 /// Remove Analyss not preserved by Pass P
820 void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
821 AnalysisUsage AnUsage;
822 P->getAnalysisUsage(AnUsage);
824 if (AnUsage.getPreservesAll())
827 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
828 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
829 E = AvailableAnalysis.end(); I != E; ) {
830 std::map<AnalysisID, Pass*>::iterator Info = I++;
831 if (std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
832 PreservedSet.end()) {
833 // Remove this analysis
834 if (!dynamic_cast<ImmutablePass*>(Info->second))
835 AvailableAnalysis.erase(Info);
840 /// Remove analysis passes that are not used any longer
841 void PMDataManager::removeDeadPasses(Pass *P, std::string &Msg) {
843 std::vector<Pass *> DeadPasses;
844 TPM->collectLastUses(DeadPasses, P);
846 for (std::vector<Pass *>::iterator I = DeadPasses.begin(),
847 E = DeadPasses.end(); I != E; ++I) {
849 std::string Msg1 = " Freeing Pass '";
850 dumpPassInfo(*I, Msg1, Msg);
852 if (TheTimeInfo) TheTimeInfo->passStarted(P);
853 (*I)->releaseMemory();
854 if (TheTimeInfo) TheTimeInfo->passEnded(P);
856 std::map<AnalysisID, Pass*>::iterator Pos =
857 AvailableAnalysis.find((*I)->getPassInfo());
859 // It is possible that pass is already removed from the AvailableAnalysis
860 if (Pos != AvailableAnalysis.end())
861 AvailableAnalysis.erase(Pos);
865 /// Add pass P into the PassVector. Update
866 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
867 void PMDataManager::addPassToManager(Pass *P,
868 bool ProcessAnalysis) {
870 // This manager is going to manage pass P. Set up analysis resolver
872 AnalysisResolver *AR = new AnalysisResolver(*this);
875 if (ProcessAnalysis) {
877 // At the moment, this pass is the last user of all required passes.
878 std::vector<Pass *> LastUses;
879 std::vector<Pass *> RequiredPasses;
880 unsigned PDepth = this->getDepth();
882 collectRequiredAnalysisPasses(RequiredPasses, P);
883 for (std::vector<Pass *>::iterator I = RequiredPasses.begin(),
884 E = RequiredPasses.end(); I != E; ++I) {
885 Pass *PRequired = *I;
888 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
889 RDepth = DM.getDepth();
891 if (PDepth == RDepth)
892 LastUses.push_back(PRequired);
893 else if (PDepth > RDepth) {
894 // Let the parent claim responsibility of last use
895 TransferLastUses.push_back(PRequired);
897 // Note : This feature is not yet implemented
899 "Unable to handle Pass that requires lower level Analysis pass");
903 LastUses.push_back(P);
904 TPM->setLastUser(LastUses, P);
906 // Take a note of analysis required and made available by this pass.
907 // Remove the analysis not preserved by this pass
908 removeNotPreservedAnalysis(P);
909 recordAvailableAnalysis(P);
913 PassVector.push_back(P);
916 /// Populate RequiredPasses with the analysis pass that are required by
918 void PMDataManager::collectRequiredAnalysisPasses(std::vector<Pass *> &RP,
920 AnalysisUsage AnUsage;
921 P->getAnalysisUsage(AnUsage);
922 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
923 for (std::vector<AnalysisID>::const_iterator
924 I = RequiredSet.begin(), E = RequiredSet.end();
926 Pass *AnalysisPass = findAnalysisPass(*I, true);
927 assert (AnalysisPass && "Analysis pass is not available");
928 RP.push_back(AnalysisPass);
931 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
932 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
933 E = IDs.end(); I != E; ++I) {
934 Pass *AnalysisPass = findAnalysisPass(*I, true);
935 assert (AnalysisPass && "Analysis pass is not available");
936 RP.push_back(AnalysisPass);
940 // All Required analyses should be available to the pass as it runs! Here
941 // we fill in the AnalysisImpls member of the pass so that it can
942 // successfully use the getAnalysis() method to retrieve the
943 // implementations it needs.
945 void PMDataManager::initializeAnalysisImpl(Pass *P) {
946 AnalysisUsage AnUsage;
947 P->getAnalysisUsage(AnUsage);
949 for (std::vector<const PassInfo *>::const_iterator
950 I = AnUsage.getRequiredSet().begin(),
951 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
952 Pass *Impl = findAnalysisPass(*I, true);
954 assert(0 && "Analysis used but not available!");
955 AnalysisResolver *AR = P->getResolver();
956 AR->addAnalysisImplsPair(*I, Impl);
960 /// Find the pass that implements Analysis AID. If desired pass is not found
961 /// then return NULL.
962 Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
964 // Check if AvailableAnalysis map has one entry.
965 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
967 if (I != AvailableAnalysis.end())
970 // Search Parents through TopLevelManager
972 return TPM->findAnalysisPass(AID);
977 // Print list of passes that are last used by P.
978 void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
980 std::vector<Pass *> LUses;
982 assert (TPM && "Top Level Manager is missing");
983 TPM->collectLastUses(LUses, P);
985 for (std::vector<Pass *>::iterator I = LUses.begin(),
986 E = LUses.end(); I != E; ++I) {
987 llvm::cerr << "--" << std::string(Offset*2, ' ');
988 (*I)->dumpPassStructure(0);
992 void PMDataManager::dumpPassArguments() const {
993 for(std::vector<Pass *>::const_iterator I = PassVector.begin(),
994 E = PassVector.end(); I != E; ++I) {
995 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
996 PMD->dumpPassArguments();
998 if (const PassInfo *PI = (*I)->getPassInfo())
999 if (!PI->isAnalysisGroup())
1000 cerr << " -" << PI->getPassArgument();
1004 void PMDataManager:: dumpPassInfo(Pass *P, std::string &Msg1,
1005 std::string &Msg2) const {
1006 if (PassDebugging_New < Executions)
1008 cerr << (void*)this << std::string(getDepth()*2+1, ' ');
1010 cerr << P->getPassName();
1014 void PMDataManager::dumpAnalysisSetInfo(const char *Msg, Pass *P,
1015 const std::vector<AnalysisID> &Set)
1017 if (PassDebugging_New >= Details && !Set.empty()) {
1018 cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
1019 for (unsigned i = 0; i != Set.size(); ++i) {
1021 cerr << " " << Set[i]->getPassName();
1027 //===----------------------------------------------------------------------===//
1028 // NOTE: Is this the right place to define this method ?
1029 // getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
1030 Pass *AnalysisResolver::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
1031 return PM.findAnalysisPass(ID, dir);
1034 //===----------------------------------------------------------------------===//
1035 // BBPassManager implementation
1037 /// Execute all of the passes scheduled for execution by invoking
1038 /// runOnBasicBlock method. Keep track of whether any of the passes modifies
1039 /// the function, and if so, return true.
1041 BBPassManager::runOnFunction(Function &F) {
1046 bool Changed = doInitialization(F);
1048 std::string Msg1 = "Executing Pass '";
1049 std::string Msg3 = "' Made Modification '";
1051 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
1052 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1053 BasicBlockPass *BP = getContainedPass(Index);
1054 AnalysisUsage AnUsage;
1055 BP->getAnalysisUsage(AnUsage);
1057 std::string Msg2 = "' on BasicBlock '" + (*I).getName() + "'...\n";
1058 dumpPassInfo(BP, Msg1, Msg2);
1059 dumpAnalysisSetInfo("Required", BP, AnUsage.getRequiredSet());
1061 initializeAnalysisImpl(BP);
1063 if (TheTimeInfo) TheTimeInfo->passStarted(BP);
1064 Changed |= BP->runOnBasicBlock(*I);
1065 if (TheTimeInfo) TheTimeInfo->passEnded(BP);
1068 dumpPassInfo(BP, Msg3, Msg2);
1069 dumpAnalysisSetInfo("Preserved", BP, AnUsage.getPreservedSet());
1071 removeNotPreservedAnalysis(BP);
1072 recordAvailableAnalysis(BP);
1073 removeDeadPasses(BP, Msg2);
1075 return Changed |= doFinalization(F);
1078 // Implement doInitialization and doFinalization
1079 inline bool BBPassManager::doInitialization(Module &M) {
1080 bool Changed = false;
1082 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1083 BasicBlockPass *BP = getContainedPass(Index);
1084 Changed |= BP->doInitialization(M);
1090 inline bool BBPassManager::doFinalization(Module &M) {
1091 bool Changed = false;
1093 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1094 BasicBlockPass *BP = getContainedPass(Index);
1095 Changed |= BP->doFinalization(M);
1101 inline bool BBPassManager::doInitialization(Function &F) {
1102 bool Changed = false;
1104 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1105 BasicBlockPass *BP = getContainedPass(Index);
1106 Changed |= BP->doInitialization(F);
1112 inline bool BBPassManager::doFinalization(Function &F) {
1113 bool Changed = false;
1115 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1116 BasicBlockPass *BP = getContainedPass(Index);
1117 Changed |= BP->doFinalization(F);
1124 //===----------------------------------------------------------------------===//
1125 // FunctionPassManager implementation
1127 /// Create new Function pass manager
1128 FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
1129 FPM = new FunctionPassManagerImpl(0);
1130 // FPM is the top level manager.
1131 FPM->setTopLevelManager(FPM);
1133 PMDataManager *PMD = dynamic_cast<PMDataManager *>(FPM);
1134 AnalysisResolver *AR = new AnalysisResolver(*PMD);
1135 FPM->setResolver(AR);
1140 FunctionPassManager::~FunctionPassManager() {
1144 /// add - Add a pass to the queue of passes to run. This passes
1145 /// ownership of the Pass to the PassManager. When the
1146 /// PassManager_X is destroyed, the pass will be destroyed as well, so
1147 /// there is no need to delete the pass. (TODO delete passes.)
1148 /// This implies that all passes MUST be allocated with 'new'.
1149 void FunctionPassManager::add(Pass *P) {
1153 /// run - Execute all of the passes scheduled for execution. Keep
1154 /// track of whether any of the passes modifies the function, and if
1155 /// so, return true.
1157 bool FunctionPassManager::run(Function &F) {
1159 if (MP->materializeFunction(&F, &errstr)) {
1160 cerr << "Error reading bytecode file: " << errstr << "\n";
1167 /// doInitialization - Run all of the initializers for the function passes.
1169 bool FunctionPassManager::doInitialization() {
1170 return FPM->doInitialization(*MP->getModule());
1173 /// doFinalization - Run all of the initializers for the function passes.
1175 bool FunctionPassManager::doFinalization() {
1176 return FPM->doFinalization(*MP->getModule());
1179 //===----------------------------------------------------------------------===//
1180 // FunctionPassManagerImpl implementation
1182 inline bool FunctionPassManagerImpl::doInitialization(Module &M) {
1183 bool Changed = false;
1185 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1186 FPPassManager *FP = getContainedManager(Index);
1187 Changed |= FP->doInitialization(M);
1193 inline bool FunctionPassManagerImpl::doFinalization(Module &M) {
1194 bool Changed = false;
1196 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1197 FPPassManager *FP = getContainedManager(Index);
1198 Changed |= FP->doFinalization(M);
1204 // Execute all the passes managed by this top level manager.
1205 // Return true if any function is modified by a pass.
1206 bool FunctionPassManagerImpl::run(Function &F) {
1208 bool Changed = false;
1210 TimingInfo::createTheTimeInfo();
1215 initializeAllAnalysisInfo();
1216 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1217 FPPassManager *FP = getContainedManager(Index);
1218 Changed |= FP->runOnFunction(F);
1223 //===----------------------------------------------------------------------===//
1224 // FPPassManager implementation
1226 /// Execute all of the passes scheduled for execution by invoking
1227 /// runOnFunction method. Keep track of whether any of the passes modifies
1228 /// the function, and if so, return true.
1229 bool FPPassManager::runOnFunction(Function &F) {
1231 bool Changed = false;
1236 std::string Msg1 = "Executing Pass '";
1237 std::string Msg3 = "' Made Modification '";
1239 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1240 FunctionPass *FP = getContainedPass(Index);
1242 AnalysisUsage AnUsage;
1243 FP->getAnalysisUsage(AnUsage);
1245 std::string Msg2 = "' on Function '" + F.getName() + "'...\n";
1246 dumpPassInfo(FP, Msg1, Msg2);
1247 dumpAnalysisSetInfo("Required", FP, AnUsage.getRequiredSet());
1249 initializeAnalysisImpl(FP);
1251 if (TheTimeInfo) TheTimeInfo->passStarted(FP);
1252 Changed |= FP->runOnFunction(F);
1253 if (TheTimeInfo) TheTimeInfo->passEnded(FP);
1256 dumpPassInfo(FP, Msg3, Msg2);
1257 dumpAnalysisSetInfo("Preserved", FP, AnUsage.getPreservedSet());
1259 removeNotPreservedAnalysis(FP);
1260 recordAvailableAnalysis(FP);
1261 removeDeadPasses(FP, Msg2);
1266 bool FPPassManager::runOnModule(Module &M) {
1268 bool Changed = doInitialization(M);
1270 for(Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1271 this->runOnFunction(*I);
1273 return Changed |= doFinalization(M);
1276 inline bool FPPassManager::doInitialization(Module &M) {
1277 bool Changed = false;
1279 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1280 FunctionPass *FP = getContainedPass(Index);
1281 Changed |= FP->doInitialization(M);
1287 inline bool FPPassManager::doFinalization(Module &M) {
1288 bool Changed = false;
1290 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1291 FunctionPass *FP = getContainedPass(Index);
1292 Changed |= FP->doFinalization(M);
1298 //===----------------------------------------------------------------------===//
1299 // MPPassManager implementation
1301 /// Execute all of the passes scheduled for execution by invoking
1302 /// runOnModule method. Keep track of whether any of the passes modifies
1303 /// the module, and if so, return true.
1305 MPPassManager::runOnModule(Module &M) {
1306 bool Changed = false;
1308 std::string Msg1 = "Executing Pass '";
1309 std::string Msg3 = "' Made Modification '";
1311 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1312 ModulePass *MP = getContainedPass(Index);
1314 AnalysisUsage AnUsage;
1315 MP->getAnalysisUsage(AnUsage);
1317 std::string Msg2 = "' on Module '" + M.getModuleIdentifier() + "'...\n";
1318 dumpPassInfo(MP, Msg1, Msg2);
1319 dumpAnalysisSetInfo("Required", MP, AnUsage.getRequiredSet());
1321 initializeAnalysisImpl(MP);
1323 if (TheTimeInfo) TheTimeInfo->passStarted(MP);
1324 Changed |= MP->runOnModule(M);
1325 if (TheTimeInfo) TheTimeInfo->passEnded(MP);
1328 dumpPassInfo(MP, Msg3, Msg2);
1329 dumpAnalysisSetInfo("Preserved", MP, AnUsage.getPreservedSet());
1331 removeNotPreservedAnalysis(MP);
1332 recordAvailableAnalysis(MP);
1333 removeDeadPasses(MP, Msg2);
1338 //===----------------------------------------------------------------------===//
1339 // PassManagerImpl implementation
1341 /// run - Execute all of the passes scheduled for execution. Keep track of
1342 /// whether any of the passes modifies the module, and if so, return true.
1343 bool PassManagerImpl::run(Module &M) {
1345 bool Changed = false;
1347 TimingInfo::createTheTimeInfo();
1352 initializeAllAnalysisInfo();
1353 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1354 MPPassManager *MP = getContainedManager(Index);
1355 Changed |= MP->runOnModule(M);
1360 //===----------------------------------------------------------------------===//
1361 // PassManager implementation
1363 /// Create new pass manager
1364 PassManager::PassManager() {
1365 PM = new PassManagerImpl(0);
1366 // PM is the top level manager
1367 PM->setTopLevelManager(PM);
1370 PassManager::~PassManager() {
1374 /// add - Add a pass to the queue of passes to run. This passes ownership of
1375 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
1376 /// will be destroyed as well, so there is no need to delete the pass. This
1377 /// implies that all passes MUST be allocated with 'new'.
1379 PassManager::add(Pass *P) {
1383 /// run - Execute all of the passes scheduled for execution. Keep track of
1384 /// whether any of the passes modifies the module, and if so, return true.
1386 PassManager::run(Module &M) {
1390 //===----------------------------------------------------------------------===//
1391 // TimingInfo Class - This class is used to calculate information about the
1392 // amount of time each pass takes to execute. This only happens with
1393 // -time-passes is enabled on the command line.
1395 bool llvm::TimePassesIsEnabled = false;
1396 static cl::opt<bool,true>
1397 EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1398 cl::desc("Time each pass, printing elapsed time for each on exit"));
1400 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1401 // a non null value (if the -time-passes option is enabled) or it leaves it
1402 // null. It may be called multiple times.
1403 void TimingInfo::createTheTimeInfo() {
1404 if (!TimePassesIsEnabled || TheTimeInfo) return;
1406 // Constructed the first time this is called, iff -time-passes is enabled.
1407 // This guarantees that the object will be constructed before static globals,
1408 // thus it will be destroyed before them.
1409 static ManagedStatic<TimingInfo> TTI;
1410 TheTimeInfo = &*TTI;
1413 //===----------------------------------------------------------------------===//
1414 // PMStack implementation
1417 // Pop Pass Manager from the stack and clear its analysis info.
1418 void PMStack::pop() {
1420 PMDataManager *Top = this->top();
1421 Top->initializeAnalysisInfo();
1426 // Push PM on the stack and set its top level manager.
1427 void PMStack::push(Pass *P) {
1429 PMDataManager *Top = NULL;
1430 PMDataManager *PM = dynamic_cast<PMDataManager *>(P);
1431 assert (PM && "Unable to push. Pass Manager expected");
1433 if (this->empty()) {
1438 PMTopLevelManager *TPM = Top->getTopLevelManager();
1440 assert (TPM && "Unable to find top level manager");
1441 TPM->addIndirectPassManager(PM);
1442 PM->setTopLevelManager(TPM);
1445 AnalysisResolver *AR = new AnalysisResolver(*Top);
1451 // Dump content of the pass manager stack.
1452 void PMStack::dump() {
1453 for(std::deque<PMDataManager *>::iterator I = S.begin(),
1454 E = S.end(); I != E; ++I) {
1455 Pass *P = dynamic_cast<Pass *>(*I);
1456 printf ("%s ", P->getPassName());
1462 // Walk Pass Manager stack and set LastUse markers if any
1463 // manager is transfering this priviledge to its parent manager
1464 void PMStack::handleLastUserOverflow() {
1466 for(PMStack::iterator I = this->begin(), E = this->end(); I != E;) {
1468 PMDataManager *Child = *I++;
1470 PMDataManager *Parent = *I++;
1471 PMTopLevelManager *TPM = Parent->getTopLevelManager();
1472 std::vector<Pass *> &TLU = Child->getTransferredLastUses();
1474 Pass *P = dynamic_cast<Pass *>(Parent);
1475 TPM->setLastUser(TLU, P);
1481 /// Find appropriate Module Pass Manager in the PM Stack and
1482 /// add self into that manager.
1483 void ModulePass::assignPassManager(PMStack &PMS) {
1485 // Find Module Pass Manager
1486 while(!PMS.empty()) {
1487 if (PMS.top()->getPassManagerType() > PMT_ModulePassManager)
1488 PMS.pop(); // Pop children pass managers
1492 MPPassManager *MPP = dynamic_cast<MPPassManager *>(PMS.top());
1494 assert(MPP && "Unable to find Module Pass Manager");
1495 MPP->addPassToManager(this);
1498 /// Find appropriate Function Pass Manager or Call Graph Pass Manager
1499 /// in the PM Stack and add self into that manager.
1500 void FunctionPass::assignPassManager(PMStack &PMS) {
1502 // Find Module Pass Manager (TODO : Or Call Graph Pass Manager)
1503 while(!PMS.empty()) {
1504 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1509 FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top());
1511 // Create new Function Pass Manager
1513 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1514 PMDataManager *PMD = PMS.top();
1516 // [1] Create new Function Pass Manager
1517 FPP = new FPPassManager(PMD->getDepth() + 1);
1519 // [2] Set up new manager's top level manager
1520 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1521 TPM->addIndirectPassManager(FPP);
1523 // [3] Assign manager to manage this new manager. This may create
1524 // and push new managers into PMS
1525 Pass *P = dynamic_cast<Pass *>(FPP);
1526 P->assignPassManager(PMS);
1528 // [4] Push new manager into PMS
1532 // Assign FPP as the manager of this pass.
1533 FPP->addPassToManager(this);
1536 /// Find appropriate Basic Pass Manager or Call Graph Pass Manager
1537 /// in the PM Stack and add self into that manager.
1538 void BasicBlockPass::assignPassManager(PMStack &PMS) {
1540 BBPassManager *BBP = NULL;
1542 // Basic Pass Manager is a leaf pass manager. It does not handle
1543 // any other pass manager.
1545 BBP = dynamic_cast<BBPassManager *>(PMS.top());
1548 // If leaf manager is not Basic Block Pass manager then create new
1549 // basic Block Pass manager.
1552 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1553 PMDataManager *PMD = PMS.top();
1555 // [1] Create new Basic Block Manager
1556 BBP = new BBPassManager(PMD->getDepth() + 1);
1558 // [2] Set up new manager's top level manager
1559 // Basic Block Pass Manager does not live by itself
1560 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1561 TPM->addIndirectPassManager(BBP);
1563 // [3] Assign manager to manage this new manager. This may create
1564 // and push new managers into PMS
1565 Pass *P = dynamic_cast<Pass *>(BBP);
1566 P->assignPassManager(PMS);
1568 // [4] Push new manager into PMS
1572 // Assign BBP as the manager of this pass.
1573 BBP->addPassToManager(this);