1 //===- PassManager.cpp - LLVM Pass Infrastructure Implementation ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the LLVM Pass Manager infrastructure.
12 //===----------------------------------------------------------------------===//
15 #include "llvm/PassManagers.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"
22 #include "llvm-c/Core.h"
28 // See PassManagers.h for Pass Manager infrastructure overview.
32 //===----------------------------------------------------------------------===//
33 // Pass debugging information. Often it is useful to find out what pass is
34 // running when a crash occurs in a utility. When this library is compiled with
35 // debugging on, a command line option (--debug-pass) is enabled that causes the
36 // pass name to be printed before it executes.
39 // Different debug levels that can be enabled...
41 None, Arguments, Structure, Executions, Details
44 static cl::opt<enum PassDebugLevel>
45 PassDebugging("debug-pass", cl::Hidden,
46 cl::desc("Print PassManager debugging information"),
48 clEnumVal(None , "disable debug output"),
49 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
50 clEnumVal(Structure , "print pass structure before run()"),
51 clEnumVal(Executions, "print pass name before it is executed"),
52 clEnumVal(Details , "print pass details when it is executed"),
54 } // End of llvm namespace
58 //===----------------------------------------------------------------------===//
61 /// BBPassManager manages BasicBlockPass. It batches all the
62 /// pass together and sequence them to process one basic block before
63 /// processing next basic block.
64 class VISIBILITY_HIDDEN BBPassManager : public PMDataManager,
69 explicit BBPassManager(int Depth)
70 : PMDataManager(Depth), FunctionPass((intptr_t)&ID) {}
72 /// Execute all of the passes scheduled for execution. Keep track of
73 /// whether any of the passes modifies the function, and if so, return true.
74 bool runOnFunction(Function &F);
76 /// Pass Manager itself does not invalidate any analysis info.
77 void getAnalysisUsage(AnalysisUsage &Info) const {
78 Info.setPreservesAll();
81 bool doInitialization(Module &M);
82 bool doInitialization(Function &F);
83 bool doFinalization(Module &M);
84 bool doFinalization(Function &F);
86 virtual const char *getPassName() const {
87 return "BasicBlock Pass Manager";
90 // Print passes managed by this manager
91 void dumpPassStructure(unsigned Offset) {
92 llvm::cerr << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n";
93 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
94 BasicBlockPass *BP = getContainedPass(Index);
95 BP->dumpPassStructure(Offset + 1);
96 dumpLastUses(BP, Offset+1);
100 BasicBlockPass *getContainedPass(unsigned N) {
101 assert ( N < PassVector.size() && "Pass number out of range!");
102 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
106 virtual PassManagerType getPassManagerType() const {
107 return PMT_BasicBlockPassManager;
111 char BBPassManager::ID = 0;
116 //===----------------------------------------------------------------------===//
117 // FunctionPassManagerImpl
119 /// FunctionPassManagerImpl manages FPPassManagers
120 class FunctionPassManagerImpl : public Pass,
121 public PMDataManager,
122 public PMTopLevelManager {
125 explicit FunctionPassManagerImpl(int Depth) :
126 Pass((intptr_t)&ID), PMDataManager(Depth),
127 PMTopLevelManager(TLM_Function) { }
129 /// add - Add a pass to the queue of passes to run. This passes ownership of
130 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
131 /// will be destroyed as well, so there is no need to delete the pass. This
132 /// implies that all passes MUST be allocated with 'new'.
137 /// run - Execute all of the passes scheduled for execution. Keep track of
138 /// whether any of the passes modifies the module, and if so, return true.
139 bool run(Function &F);
141 /// doInitialization - Run all of the initializers for the function passes.
143 bool doInitialization(Module &M);
145 /// doFinalization - Run all of the finalizers for the function passes.
147 bool doFinalization(Module &M);
149 /// Pass Manager itself does not invalidate any analysis info.
150 void getAnalysisUsage(AnalysisUsage &Info) const {
151 Info.setPreservesAll();
154 inline void addTopLevelPass(Pass *P) {
156 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
158 // P is a immutable pass and it will be managed by this
159 // top level manager. Set up analysis resolver to connect them.
160 AnalysisResolver *AR = new AnalysisResolver(*this);
162 initializeAnalysisImpl(P);
163 addImmutablePass(IP);
164 recordAvailableAnalysis(IP);
166 P->assignPassManager(activeStack);
171 FPPassManager *getContainedManager(unsigned N) {
172 assert ( N < PassManagers.size() && "Pass number out of range!");
173 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
178 char FunctionPassManagerImpl::ID = 0;
179 //===----------------------------------------------------------------------===//
182 /// MPPassManager manages ModulePasses and function pass managers.
183 /// It batches all Module passes and function pass managers together and
184 /// sequences them to process one module.
185 class MPPassManager : public Pass, public PMDataManager {
189 explicit MPPassManager(int Depth) :
190 Pass((intptr_t)&ID), PMDataManager(Depth) { }
192 // Delete on the fly managers.
193 virtual ~MPPassManager() {
194 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
195 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
197 FunctionPassManagerImpl *FPP = I->second;
202 /// run - Execute all of the passes scheduled for execution. Keep track of
203 /// whether any of the passes modifies the module, and if so, return true.
204 bool runOnModule(Module &M);
206 /// Pass Manager itself does not invalidate any analysis info.
207 void getAnalysisUsage(AnalysisUsage &Info) const {
208 Info.setPreservesAll();
211 /// Add RequiredPass into list of lower level passes required by pass P.
212 /// RequiredPass is run on the fly by Pass Manager when P requests it
213 /// through getAnalysis interface.
214 virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
216 /// Return function pass corresponding to PassInfo PI, that is
217 /// required by module pass MP. Instantiate analysis pass, by using
218 /// its runOnFunction() for function F.
219 virtual Pass* getOnTheFlyPass(Pass *MP, const PassInfo *PI, Function &F);
221 virtual const char *getPassName() const {
222 return "Module Pass Manager";
225 // Print passes managed by this manager
226 void dumpPassStructure(unsigned Offset) {
227 llvm::cerr << std::string(Offset*2, ' ') << "ModulePass Manager\n";
228 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
229 ModulePass *MP = getContainedPass(Index);
230 MP->dumpPassStructure(Offset + 1);
231 if (FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP])
232 FPP->dumpPassStructure(Offset + 2);
233 dumpLastUses(MP, Offset+1);
237 ModulePass *getContainedPass(unsigned N) {
238 assert ( N < PassVector.size() && "Pass number out of range!");
239 ModulePass *MP = static_cast<ModulePass *>(PassVector[N]);
243 virtual PassManagerType getPassManagerType() const {
244 return PMT_ModulePassManager;
248 /// Collection of on the fly FPPassManagers. These managers manage
249 /// function passes that are required by module passes.
250 std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
253 char MPPassManager::ID = 0;
254 //===----------------------------------------------------------------------===//
258 /// PassManagerImpl manages MPPassManagers
259 class PassManagerImpl : public Pass,
260 public PMDataManager,
261 public PMTopLevelManager {
265 explicit PassManagerImpl(int Depth) :
266 Pass((intptr_t)&ID), PMDataManager(Depth),
267 PMTopLevelManager(TLM_Pass) { }
269 /// add - Add a pass to the queue of passes to run. This passes ownership of
270 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
271 /// will be destroyed as well, so there is no need to delete the pass. This
272 /// implies that all passes MUST be allocated with 'new'.
277 /// run - Execute all of the passes scheduled for execution. Keep track of
278 /// whether any of the passes modifies the module, and if so, return true.
281 /// Pass Manager itself does not invalidate any analysis info.
282 void getAnalysisUsage(AnalysisUsage &Info) const {
283 Info.setPreservesAll();
286 inline void addTopLevelPass(Pass *P) {
288 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
290 // P is a immutable pass and it will be managed by this
291 // top level manager. Set up analysis resolver to connect them.
292 AnalysisResolver *AR = new AnalysisResolver(*this);
294 initializeAnalysisImpl(P);
295 addImmutablePass(IP);
296 recordAvailableAnalysis(IP);
298 P->assignPassManager(activeStack);
303 MPPassManager *getContainedManager(unsigned N) {
304 assert ( N < PassManagers.size() && "Pass number out of range!");
305 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
311 char PassManagerImpl::ID = 0;
312 } // End of llvm namespace
316 //===----------------------------------------------------------------------===//
317 // TimingInfo Class - This class is used to calculate information about the
318 // amount of time each pass takes to execute. This only happens when
319 // -time-passes is enabled on the command line.
322 class VISIBILITY_HIDDEN TimingInfo {
323 std::map<Pass*, Timer> TimingData;
327 // Use 'create' member to get this.
328 TimingInfo() : TG("... Pass execution timing report ...") {}
330 // TimingDtor - Print out information about timing information
332 // Delete all of the timers...
334 // TimerGroup is deleted next, printing the report.
337 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
338 // to a non null value (if the -time-passes option is enabled) or it leaves it
339 // null. It may be called multiple times.
340 static void createTheTimeInfo();
342 void passStarted(Pass *P) {
344 if (dynamic_cast<PMDataManager *>(P))
347 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
348 if (I == TimingData.end())
349 I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
350 I->second.startTimer();
352 void passEnded(Pass *P) {
354 if (dynamic_cast<PMDataManager *>(P))
357 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
358 assert (I != TimingData.end() && "passStarted/passEnded not nested right!");
359 I->second.stopTimer();
363 } // End of anon namespace
365 static TimingInfo *TheTimeInfo;
367 //===----------------------------------------------------------------------===//
368 // PMTopLevelManager implementation
370 /// Initialize top level manager. Create first pass manager.
371 PMTopLevelManager::PMTopLevelManager (enum TopLevelManagerType t) {
374 MPPassManager *MPP = new MPPassManager(1);
375 MPP->setTopLevelManager(this);
377 activeStack.push(MPP);
379 else if (t == TLM_Function) {
380 FPPassManager *FPP = new FPPassManager(1);
381 FPP->setTopLevelManager(this);
383 activeStack.push(FPP);
387 /// Set pass P as the last user of the given analysis passes.
388 void PMTopLevelManager::setLastUser(SmallVector<Pass *, 12> &AnalysisPasses,
391 for (SmallVector<Pass *, 12>::iterator I = AnalysisPasses.begin(),
392 E = AnalysisPasses.end(); I != E; ++I) {
399 // If AP is the last user of other passes then make P last user of
401 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
402 LUE = LastUser.end(); LUI != LUE; ++LUI) {
403 if (LUI->second == AP)
404 LastUser[LUI->first] = P;
409 /// Collect passes whose last user is P
410 void PMTopLevelManager::collectLastUses(SmallVector<Pass *, 12> &LastUses,
412 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
413 LUE = LastUser.end(); LUI != LUE; ++LUI)
414 if (LUI->second == P)
415 LastUses.push_back(LUI->first);
418 /// Schedule pass P for execution. Make sure that passes required by
419 /// P are run before P is run. Update analysis info maintained by
420 /// the manager. Remove dead passes. This is a recursive function.
421 void PMTopLevelManager::schedulePass(Pass *P) {
423 // TODO : Allocate function manager for this pass, other wise required set
424 // may be inserted into previous function manager
426 // Give pass a chance to prepare the stage.
427 P->preparePassManager(activeStack);
429 // If P is an analysis pass and it is available then do not
430 // generate the analysis again. Stale analysis info should not be
431 // available at this point.
432 if (P->getPassInfo() &&
433 P->getPassInfo()->isAnalysis() && findAnalysisPass(P->getPassInfo()))
436 AnalysisUsage AnUsage;
437 P->getAnalysisUsage(AnUsage);
438 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
439 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
440 E = RequiredSet.end(); I != E; ++I) {
442 Pass *AnalysisPass = findAnalysisPass(*I);
444 AnalysisPass = (*I)->createPass();
445 // Schedule this analysis run first only if it is not a lower level
446 // analysis pass. Lower level analsyis passes are run on the fly.
447 if (P->getPotentialPassManagerType () >=
448 AnalysisPass->getPotentialPassManagerType())
449 schedulePass(AnalysisPass);
455 // Now all required passes are available.
459 /// Find the pass that implements Analysis AID. Search immutable
460 /// passes and all pass managers. If desired pass is not found
461 /// then return NULL.
462 Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
465 // Check pass managers
466 for (std::vector<PMDataManager *>::iterator I = PassManagers.begin(),
467 E = PassManagers.end(); P == NULL && I != E; ++I) {
468 PMDataManager *PMD = *I;
469 P = PMD->findAnalysisPass(AID, false);
472 // Check other pass managers
473 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
474 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
475 P = (*I)->findAnalysisPass(AID, false);
477 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
478 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
479 const PassInfo *PI = (*I)->getPassInfo();
483 // If Pass not found then check the interfaces implemented by Immutable Pass
485 const std::vector<const PassInfo*> &ImmPI =
486 PI->getInterfacesImplemented();
487 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
495 // Print passes managed by this top level manager.
496 void PMTopLevelManager::dumpPasses() const {
498 if (PassDebugging < Structure)
501 // Print out the immutable passes
502 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
503 ImmutablePasses[i]->dumpPassStructure(0);
506 // Every class that derives from PMDataManager also derives from Pass
507 // (sometimes indirectly), but there's no inheritance relationship
508 // between PMDataManager and Pass, so we have to dynamic_cast to get
509 // from a PMDataManager* to a Pass*.
510 for (std::vector<PMDataManager *>::const_iterator I = PassManagers.begin(),
511 E = PassManagers.end(); I != E; ++I)
512 dynamic_cast<Pass *>(*I)->dumpPassStructure(1);
515 void PMTopLevelManager::dumpArguments() const {
517 if (PassDebugging < Arguments)
520 cerr << "Pass Arguments: ";
521 for (std::vector<PMDataManager *>::const_iterator I = PassManagers.begin(),
522 E = PassManagers.end(); I != E; ++I) {
523 PMDataManager *PMD = *I;
524 PMD->dumpPassArguments();
529 void PMTopLevelManager::initializeAllAnalysisInfo() {
531 for (std::vector<PMDataManager *>::iterator I = PassManagers.begin(),
532 E = PassManagers.end(); I != E; ++I) {
533 PMDataManager *PMD = *I;
534 PMD->initializeAnalysisInfo();
537 // Initailize other pass managers
538 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
539 E = IndirectPassManagers.end(); I != E; ++I)
540 (*I)->initializeAnalysisInfo();
544 PMTopLevelManager::~PMTopLevelManager() {
545 for (std::vector<PMDataManager *>::iterator I = PassManagers.begin(),
546 E = PassManagers.end(); I != E; ++I)
549 for (std::vector<ImmutablePass *>::iterator
550 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
554 //===----------------------------------------------------------------------===//
555 // PMDataManager implementation
557 /// Augement AvailableAnalysis by adding analysis made available by pass P.
558 void PMDataManager::recordAvailableAnalysis(Pass *P) {
560 if (const PassInfo *PI = P->getPassInfo()) {
561 AvailableAnalysis[PI] = P;
563 //This pass is the current implementation of all of the interfaces it
564 //implements as well.
565 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
566 for (unsigned i = 0, e = II.size(); i != e; ++i)
567 AvailableAnalysis[II[i]] = P;
571 // Return true if P preserves high level analysis used by other
572 // passes managed by this manager
573 bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
575 AnalysisUsage AnUsage;
576 P->getAnalysisUsage(AnUsage);
578 if (AnUsage.getPreservesAll())
581 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
582 for (std::vector<Pass *>::iterator I = HigherLevelAnalysis.begin(),
583 E = HigherLevelAnalysis.end(); I != E; ++I) {
585 if (!dynamic_cast<ImmutablePass*>(P1) &&
586 std::find(PreservedSet.begin(), PreservedSet.end(),
587 P1->getPassInfo()) ==
595 /// verifyPreservedAnalysis -- Verify analysis presreved by pass P.
596 void PMDataManager::verifyPreservedAnalysis(Pass *P) {
597 AnalysisUsage AnUsage;
598 P->getAnalysisUsage(AnUsage);
599 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
601 // Verify preserved analysis
602 for (std::vector<AnalysisID>::const_iterator I = PreservedSet.begin(),
603 E = PreservedSet.end(); I != E; ++I) {
605 Pass *AP = findAnalysisPass(AID, true);
607 AP->verifyAnalysis();
611 /// Remove Analyss not preserved by Pass P
612 void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
613 AnalysisUsage AnUsage;
614 P->getAnalysisUsage(AnUsage);
615 if (AnUsage.getPreservesAll())
618 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
619 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
620 E = AvailableAnalysis.end(); I != E; ) {
621 std::map<AnalysisID, Pass*>::iterator Info = I++;
622 if (!dynamic_cast<ImmutablePass*>(Info->second)
623 && std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
624 PreservedSet.end()) {
625 // Remove this analysis
626 AvailableAnalysis.erase(Info);
627 if (PassDebugging >= Details) {
628 Pass *S = Info->second;
629 cerr << " -- " << P->getPassName() << " is not preserving ";
630 cerr << S->getPassName() << "\n";
635 // Check inherited analysis also. If P is not preserving analysis
636 // provided by parent manager then remove it here.
637 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
639 if (!InheritedAnalysis[Index])
642 for (std::map<AnalysisID, Pass*>::iterator
643 I = InheritedAnalysis[Index]->begin(),
644 E = InheritedAnalysis[Index]->end(); I != E; ) {
645 std::map<AnalysisID, Pass *>::iterator Info = I++;
646 if (!dynamic_cast<ImmutablePass*>(Info->second) &&
647 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
649 // Remove this analysis
650 InheritedAnalysis[Index]->erase(Info);
656 /// Remove analysis passes that are not used any longer
657 void PMDataManager::removeDeadPasses(Pass *P, const char *Msg,
658 enum PassDebuggingString DBG_STR) {
660 SmallVector<Pass *, 12> DeadPasses;
662 // If this is a on the fly manager then it does not have TPM.
666 TPM->collectLastUses(DeadPasses, P);
668 if (PassDebugging >= Details) {
669 cerr << " -*- " << P->getPassName();
670 cerr << " is the last users of following passes. Free them\n";
673 for (SmallVector<Pass *, 12>::iterator I = DeadPasses.begin(),
674 E = DeadPasses.end(); I != E; ++I) {
676 dumpPassInfo(*I, FREEING_MSG, DBG_STR, Msg);
678 if (TheTimeInfo) TheTimeInfo->passStarted(*I);
679 (*I)->releaseMemory();
680 if (TheTimeInfo) TheTimeInfo->passEnded(*I);
682 std::map<AnalysisID, Pass*>::iterator Pos =
683 AvailableAnalysis.find((*I)->getPassInfo());
685 // It is possible that pass is already removed from the AvailableAnalysis
686 if (Pos != AvailableAnalysis.end())
687 AvailableAnalysis.erase(Pos);
691 /// Add pass P into the PassVector. Update
692 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
693 void PMDataManager::add(Pass *P,
694 bool ProcessAnalysis) {
696 // This manager is going to manage pass P. Set up analysis resolver
698 AnalysisResolver *AR = new AnalysisResolver(*this);
701 // If a FunctionPass F is the last user of ModulePass info M
702 // then the F's manager, not F, records itself as a last user of M.
703 SmallVector<Pass *, 12> TransferLastUses;
705 if (ProcessAnalysis) {
707 // At the moment, this pass is the last user of all required passes.
708 SmallVector<Pass *, 12> LastUses;
709 SmallVector<Pass *, 8> RequiredPasses;
710 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
712 unsigned PDepth = this->getDepth();
714 collectRequiredAnalysis(RequiredPasses,
715 ReqAnalysisNotAvailable, P);
716 for (SmallVector<Pass *, 8>::iterator I = RequiredPasses.begin(),
717 E = RequiredPasses.end(); I != E; ++I) {
718 Pass *PRequired = *I;
721 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
722 RDepth = DM.getDepth();
724 if (PDepth == RDepth)
725 LastUses.push_back(PRequired);
726 else if (PDepth > RDepth) {
727 // Let the parent claim responsibility of last use
728 TransferLastUses.push_back(PRequired);
729 // Keep track of higher level analysis used by this manager.
730 HigherLevelAnalysis.push_back(PRequired);
732 assert (0 && "Unable to accomodate Required Pass");
735 // Set P as P's last user until someone starts using P.
736 // However, if P is a Pass Manager then it does not need
737 // to record its last user.
738 if (!dynamic_cast<PMDataManager *>(P))
739 LastUses.push_back(P);
740 TPM->setLastUser(LastUses, P);
742 if (!TransferLastUses.empty()) {
743 Pass *My_PM = dynamic_cast<Pass *>(this);
744 TPM->setLastUser(TransferLastUses, My_PM);
745 TransferLastUses.clear();
748 // Now, take care of required analysises that are not available.
749 for (SmallVector<AnalysisID, 8>::iterator
750 I = ReqAnalysisNotAvailable.begin(),
751 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
752 Pass *AnalysisPass = (*I)->createPass();
753 this->addLowerLevelRequiredPass(P, AnalysisPass);
756 // Take a note of analysis required and made available by this pass.
757 // Remove the analysis not preserved by this pass
758 removeNotPreservedAnalysis(P);
759 recordAvailableAnalysis(P);
763 PassVector.push_back(P);
767 /// Populate RP with analysis pass that are required by
768 /// pass P and are available. Populate RP_NotAvail with analysis
769 /// pass that are required by pass P but are not available.
770 void PMDataManager::collectRequiredAnalysis(SmallVector<Pass *, 8>&RP,
771 SmallVector<AnalysisID, 8> &RP_NotAvail,
773 AnalysisUsage AnUsage;
774 P->getAnalysisUsage(AnUsage);
775 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
776 for (std::vector<AnalysisID>::const_iterator
777 I = RequiredSet.begin(), E = RequiredSet.end();
780 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
781 RP.push_back(AnalysisPass);
783 RP_NotAvail.push_back(AID);
786 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
787 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
788 E = IDs.end(); I != E; ++I) {
790 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
791 RP.push_back(AnalysisPass);
793 RP_NotAvail.push_back(AID);
797 // All Required analyses should be available to the pass as it runs! Here
798 // we fill in the AnalysisImpls member of the pass so that it can
799 // successfully use the getAnalysis() method to retrieve the
800 // implementations it needs.
802 void PMDataManager::initializeAnalysisImpl(Pass *P) {
803 AnalysisUsage AnUsage;
804 P->getAnalysisUsage(AnUsage);
806 for (std::vector<const PassInfo *>::const_iterator
807 I = AnUsage.getRequiredSet().begin(),
808 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
809 Pass *Impl = findAnalysisPass(*I, true);
811 // This may be analysis pass that is initialized on the fly.
812 // If that is not the case then it will raise an assert when it is used.
814 AnalysisResolver *AR = P->getResolver();
815 AR->addAnalysisImplsPair(*I, Impl);
819 /// Find the pass that implements Analysis AID. If desired pass is not found
820 /// then return NULL.
821 Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
823 // Check if AvailableAnalysis map has one entry.
824 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
826 if (I != AvailableAnalysis.end())
829 // Search Parents through TopLevelManager
831 return TPM->findAnalysisPass(AID);
836 // Print list of passes that are last used by P.
837 void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
839 SmallVector<Pass *, 12> LUses;
841 // If this is a on the fly manager then it does not have TPM.
845 TPM->collectLastUses(LUses, P);
847 for (SmallVector<Pass *, 12>::iterator I = LUses.begin(),
848 E = LUses.end(); I != E; ++I) {
849 llvm::cerr << "--" << std::string(Offset*2, ' ');
850 (*I)->dumpPassStructure(0);
854 void PMDataManager::dumpPassArguments() const {
855 for(std::vector<Pass *>::const_iterator I = PassVector.begin(),
856 E = PassVector.end(); I != E; ++I) {
857 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
858 PMD->dumpPassArguments();
860 if (const PassInfo *PI = (*I)->getPassInfo())
861 if (!PI->isAnalysisGroup())
862 cerr << " -" << PI->getPassArgument();
866 void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
867 enum PassDebuggingString S2,
869 if (PassDebugging < Executions)
871 cerr << (void*)this << std::string(getDepth()*2+1, ' ');
874 cerr << "Executing Pass '" << P->getPassName();
876 case MODIFICATION_MSG:
877 cerr << "Made Modification '" << P->getPassName();
880 cerr << " Freeing Pass '" << P->getPassName();
886 case ON_BASICBLOCK_MSG:
887 cerr << "' on BasicBlock '" << Msg << "'...\n";
889 case ON_FUNCTION_MSG:
890 cerr << "' on Function '" << Msg << "'...\n";
893 cerr << "' on Module '" << Msg << "'...\n";
896 cerr << "' on Loop " << Msg << "'...\n";
899 cerr << "' on Call Graph " << Msg << "'...\n";
906 void PMDataManager::dumpAnalysisSetInfo(const char *Msg, Pass *P,
907 const std::vector<AnalysisID> &Set)
909 if (PassDebugging >= Details && !Set.empty()) {
910 cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
911 for (unsigned i = 0; i != Set.size(); ++i) {
913 cerr << " " << Set[i]->getPassName();
919 /// Add RequiredPass into list of lower level passes required by pass P.
920 /// RequiredPass is run on the fly by Pass Manager when P requests it
921 /// through getAnalysis interface.
922 /// This should be handled by specific pass manager.
923 void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
925 TPM->dumpArguments();
929 // Module Level pass may required Function Level analysis info
930 // (e.g. dominator info). Pass manager uses on the fly function pass manager
931 // to provide this on demand. In that case, in Pass manager terminology,
932 // module level pass is requiring lower level analysis info managed by
933 // lower level pass manager.
935 // When Pass manager is not able to order required analysis info, Pass manager
936 // checks whether any lower level manager will be able to provide this
937 // analysis info on demand or not.
939 cerr << "Unable to schedule " << RequiredPass->getPassName();
940 cerr << " required by " << P->getPassName() << "\n";
942 assert (0 && "Unable to schedule pass");
946 PMDataManager::~PMDataManager() {
948 for (std::vector<Pass *>::iterator I = PassVector.begin(),
949 E = PassVector.end(); I != E; ++I)
954 //===----------------------------------------------------------------------===//
955 // NOTE: Is this the right place to define this method ?
956 // getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
957 Pass *AnalysisResolver::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
958 return PM.findAnalysisPass(ID, dir);
961 Pass *AnalysisResolver::findImplPass(Pass *P, const PassInfo *AnalysisPI,
963 return PM.getOnTheFlyPass(P, AnalysisPI, F);
966 //===----------------------------------------------------------------------===//
967 // BBPassManager implementation
969 /// Execute all of the passes scheduled for execution by invoking
970 /// runOnBasicBlock method. Keep track of whether any of the passes modifies
971 /// the function, and if so, return true.
973 BBPassManager::runOnFunction(Function &F) {
975 if (F.isDeclaration())
978 bool Changed = doInitialization(F);
980 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
981 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
982 BasicBlockPass *BP = getContainedPass(Index);
983 AnalysisUsage AnUsage;
984 BP->getAnalysisUsage(AnUsage);
986 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getNameStart());
987 dumpAnalysisSetInfo("Required", BP, AnUsage.getRequiredSet());
989 initializeAnalysisImpl(BP);
991 if (TheTimeInfo) TheTimeInfo->passStarted(BP);
992 Changed |= BP->runOnBasicBlock(*I);
993 if (TheTimeInfo) TheTimeInfo->passEnded(BP);
996 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
998 dumpAnalysisSetInfo("Preserved", BP, AnUsage.getPreservedSet());
1000 verifyPreservedAnalysis(BP);
1001 removeNotPreservedAnalysis(BP);
1002 recordAvailableAnalysis(BP);
1003 removeDeadPasses(BP, I->getNameStart(), ON_BASICBLOCK_MSG);
1006 return Changed |= doFinalization(F);
1009 // Implement doInitialization and doFinalization
1010 inline bool BBPassManager::doInitialization(Module &M) {
1011 bool Changed = false;
1013 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1014 BasicBlockPass *BP = getContainedPass(Index);
1015 Changed |= BP->doInitialization(M);
1021 inline bool BBPassManager::doFinalization(Module &M) {
1022 bool Changed = false;
1024 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1025 BasicBlockPass *BP = getContainedPass(Index);
1026 Changed |= BP->doFinalization(M);
1032 inline bool BBPassManager::doInitialization(Function &F) {
1033 bool Changed = false;
1035 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1036 BasicBlockPass *BP = getContainedPass(Index);
1037 Changed |= BP->doInitialization(F);
1043 inline bool BBPassManager::doFinalization(Function &F) {
1044 bool Changed = false;
1046 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1047 BasicBlockPass *BP = getContainedPass(Index);
1048 Changed |= BP->doFinalization(F);
1055 //===----------------------------------------------------------------------===//
1056 // FunctionPassManager implementation
1058 /// Create new Function pass manager
1059 FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
1060 FPM = new FunctionPassManagerImpl(0);
1061 // FPM is the top level manager.
1062 FPM->setTopLevelManager(FPM);
1064 AnalysisResolver *AR = new AnalysisResolver(*FPM);
1065 FPM->setResolver(AR);
1070 FunctionPassManager::~FunctionPassManager() {
1074 /// add - Add a pass to the queue of passes to run. This passes
1075 /// ownership of the Pass to the PassManager. When the
1076 /// PassManager_X is destroyed, the pass will be destroyed as well, so
1077 /// there is no need to delete the pass. (TODO delete passes.)
1078 /// This implies that all passes MUST be allocated with 'new'.
1079 void FunctionPassManager::add(Pass *P) {
1083 /// run - Execute all of the passes scheduled for execution. Keep
1084 /// track of whether any of the passes modifies the function, and if
1085 /// so, return true.
1087 bool FunctionPassManager::run(Function &F) {
1089 if (MP->materializeFunction(&F, &errstr)) {
1090 cerr << "Error reading bitcode file: " << errstr << "\n";
1097 /// doInitialization - Run all of the initializers for the function passes.
1099 bool FunctionPassManager::doInitialization() {
1100 return FPM->doInitialization(*MP->getModule());
1103 /// doFinalization - Run all of the finalizers for the function passes.
1105 bool FunctionPassManager::doFinalization() {
1106 return FPM->doFinalization(*MP->getModule());
1109 //===----------------------------------------------------------------------===//
1110 // FunctionPassManagerImpl implementation
1112 inline bool FunctionPassManagerImpl::doInitialization(Module &M) {
1113 bool Changed = false;
1115 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1116 FPPassManager *FP = getContainedManager(Index);
1117 Changed |= FP->doInitialization(M);
1123 inline bool FunctionPassManagerImpl::doFinalization(Module &M) {
1124 bool Changed = false;
1126 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1127 FPPassManager *FP = getContainedManager(Index);
1128 Changed |= FP->doFinalization(M);
1134 // Execute all the passes managed by this top level manager.
1135 // Return true if any function is modified by a pass.
1136 bool FunctionPassManagerImpl::run(Function &F) {
1138 bool Changed = false;
1140 TimingInfo::createTheTimeInfo();
1145 initializeAllAnalysisInfo();
1146 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1147 FPPassManager *FP = getContainedManager(Index);
1148 Changed |= FP->runOnFunction(F);
1153 //===----------------------------------------------------------------------===//
1154 // FPPassManager implementation
1156 char FPPassManager::ID = 0;
1157 /// Print passes managed by this manager
1158 void FPPassManager::dumpPassStructure(unsigned Offset) {
1159 llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
1160 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1161 FunctionPass *FP = getContainedPass(Index);
1162 FP->dumpPassStructure(Offset + 1);
1163 dumpLastUses(FP, Offset+1);
1168 /// Execute all of the passes scheduled for execution by invoking
1169 /// runOnFunction method. Keep track of whether any of the passes modifies
1170 /// the function, and if so, return true.
1171 bool FPPassManager::runOnFunction(Function &F) {
1173 bool Changed = false;
1175 if (F.isDeclaration())
1178 // Collect inherited analysis from Module level pass manager.
1179 populateInheritedAnalysis(TPM->activeStack);
1181 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1182 FunctionPass *FP = getContainedPass(Index);
1184 AnalysisUsage AnUsage;
1185 FP->getAnalysisUsage(AnUsage);
1187 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getNameStart());
1188 dumpAnalysisSetInfo("Required", FP, AnUsage.getRequiredSet());
1190 initializeAnalysisImpl(FP);
1192 if (TheTimeInfo) TheTimeInfo->passStarted(FP);
1193 Changed |= FP->runOnFunction(F);
1194 if (TheTimeInfo) TheTimeInfo->passEnded(FP);
1197 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getNameStart());
1198 dumpAnalysisSetInfo("Preserved", FP, AnUsage.getPreservedSet());
1200 verifyPreservedAnalysis(FP);
1201 removeNotPreservedAnalysis(FP);
1202 recordAvailableAnalysis(FP);
1203 removeDeadPasses(FP, F.getNameStart(), ON_FUNCTION_MSG);
1208 bool FPPassManager::runOnModule(Module &M) {
1210 bool Changed = doInitialization(M);
1212 for(Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1213 this->runOnFunction(*I);
1215 return Changed |= doFinalization(M);
1218 inline bool FPPassManager::doInitialization(Module &M) {
1219 bool Changed = false;
1221 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1222 FunctionPass *FP = getContainedPass(Index);
1223 Changed |= FP->doInitialization(M);
1229 inline bool FPPassManager::doFinalization(Module &M) {
1230 bool Changed = false;
1232 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1233 FunctionPass *FP = getContainedPass(Index);
1234 Changed |= FP->doFinalization(M);
1240 //===----------------------------------------------------------------------===//
1241 // MPPassManager implementation
1243 /// Execute all of the passes scheduled for execution by invoking
1244 /// runOnModule method. Keep track of whether any of the passes modifies
1245 /// the module, and if so, return true.
1247 MPPassManager::runOnModule(Module &M) {
1248 bool Changed = false;
1250 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1251 ModulePass *MP = getContainedPass(Index);
1253 AnalysisUsage AnUsage;
1254 MP->getAnalysisUsage(AnUsage);
1256 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG,
1257 M.getModuleIdentifier().c_str());
1258 dumpAnalysisSetInfo("Required", MP, AnUsage.getRequiredSet());
1260 initializeAnalysisImpl(MP);
1262 if (TheTimeInfo) TheTimeInfo->passStarted(MP);
1263 Changed |= MP->runOnModule(M);
1264 if (TheTimeInfo) TheTimeInfo->passEnded(MP);
1267 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
1268 M.getModuleIdentifier().c_str());
1269 dumpAnalysisSetInfo("Preserved", MP, AnUsage.getPreservedSet());
1271 verifyPreservedAnalysis(MP);
1272 removeNotPreservedAnalysis(MP);
1273 recordAvailableAnalysis(MP);
1274 removeDeadPasses(MP, M.getModuleIdentifier().c_str(), ON_MODULE_MSG);
1279 /// Add RequiredPass into list of lower level passes required by pass P.
1280 /// RequiredPass is run on the fly by Pass Manager when P requests it
1281 /// through getAnalysis interface.
1282 void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1284 assert (P->getPotentialPassManagerType() == PMT_ModulePassManager
1285 && "Unable to handle Pass that requires lower level Analysis pass");
1286 assert ((P->getPotentialPassManagerType() <
1287 RequiredPass->getPotentialPassManagerType())
1288 && "Unable to handle Pass that requires lower level Analysis pass");
1290 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
1292 FPP = new FunctionPassManagerImpl(0);
1293 // FPP is the top level manager.
1294 FPP->setTopLevelManager(FPP);
1296 OnTheFlyManagers[P] = FPP;
1298 FPP->add(RequiredPass);
1300 // Register P as the last user of RequiredPass.
1301 SmallVector<Pass *, 12> LU;
1302 LU.push_back(RequiredPass);
1303 FPP->setLastUser(LU, P);
1306 /// Return function pass corresponding to PassInfo PI, that is
1307 /// required by module pass MP. Instantiate analysis pass, by using
1308 /// its runOnFunction() for function F.
1309 Pass* MPPassManager::getOnTheFlyPass(Pass *MP, const PassInfo *PI,
1311 AnalysisID AID = PI;
1312 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
1313 assert (FPP && "Unable to find on the fly pass");
1316 return (dynamic_cast<PMTopLevelManager *>(FPP))->findAnalysisPass(AID);
1320 //===----------------------------------------------------------------------===//
1321 // PassManagerImpl implementation
1323 /// run - Execute all of the passes scheduled for execution. Keep track of
1324 /// whether any of the passes modifies the module, and if so, return true.
1325 bool PassManagerImpl::run(Module &M) {
1327 bool Changed = false;
1329 TimingInfo::createTheTimeInfo();
1334 initializeAllAnalysisInfo();
1335 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1336 MPPassManager *MP = getContainedManager(Index);
1337 Changed |= MP->runOnModule(M);
1342 //===----------------------------------------------------------------------===//
1343 // PassManager implementation
1345 /// Create new pass manager
1346 PassManager::PassManager() {
1347 PM = new PassManagerImpl(0);
1348 // PM is the top level manager
1349 PM->setTopLevelManager(PM);
1352 PassManager::~PassManager() {
1356 /// add - Add a pass to the queue of passes to run. This passes ownership of
1357 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
1358 /// will be destroyed as well, so there is no need to delete the pass. This
1359 /// implies that all passes MUST be allocated with 'new'.
1361 PassManager::add(Pass *P) {
1365 /// run - Execute all of the passes scheduled for execution. Keep track of
1366 /// whether any of the passes modifies the module, and if so, return true.
1368 PassManager::run(Module &M) {
1372 //===----------------------------------------------------------------------===//
1373 // TimingInfo Class - This class is used to calculate information about the
1374 // amount of time each pass takes to execute. This only happens with
1375 // -time-passes is enabled on the command line.
1377 bool llvm::TimePassesIsEnabled = false;
1378 static cl::opt<bool,true>
1379 EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1380 cl::desc("Time each pass, printing elapsed time for each on exit"));
1382 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1383 // a non null value (if the -time-passes option is enabled) or it leaves it
1384 // null. It may be called multiple times.
1385 void TimingInfo::createTheTimeInfo() {
1386 if (!TimePassesIsEnabled || TheTimeInfo) return;
1388 // Constructed the first time this is called, iff -time-passes is enabled.
1389 // This guarantees that the object will be constructed before static globals,
1390 // thus it will be destroyed before them.
1391 static ManagedStatic<TimingInfo> TTI;
1392 TheTimeInfo = &*TTI;
1395 /// If TimingInfo is enabled then start pass timer.
1396 void StartPassTimer(Pass *P) {
1398 TheTimeInfo->passStarted(P);
1401 /// If TimingInfo is enabled then stop pass timer.
1402 void StopPassTimer(Pass *P) {
1404 TheTimeInfo->passEnded(P);
1407 //===----------------------------------------------------------------------===//
1408 // PMStack implementation
1411 // Pop Pass Manager from the stack and clear its analysis info.
1412 void PMStack::pop() {
1414 PMDataManager *Top = this->top();
1415 Top->initializeAnalysisInfo();
1420 // Push PM on the stack and set its top level manager.
1421 void PMStack::push(PMDataManager *PM) {
1423 PMDataManager *Top = NULL;
1424 assert (PM && "Unable to push. Pass Manager expected");
1426 if (this->empty()) {
1431 PMTopLevelManager *TPM = Top->getTopLevelManager();
1433 assert (TPM && "Unable to find top level manager");
1434 TPM->addIndirectPassManager(PM);
1435 PM->setTopLevelManager(TPM);
1441 // Dump content of the pass manager stack.
1442 void PMStack::dump() {
1443 for(std::deque<PMDataManager *>::iterator I = S.begin(),
1444 E = S.end(); I != E; ++I) {
1445 Pass *P = dynamic_cast<Pass *>(*I);
1446 printf("%s ", P->getPassName());
1452 /// Find appropriate Module Pass Manager in the PM Stack and
1453 /// add self into that manager.
1454 void ModulePass::assignPassManager(PMStack &PMS,
1455 PassManagerType PreferredType) {
1457 // Find Module Pass Manager
1458 while(!PMS.empty()) {
1459 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1460 if (TopPMType == PreferredType)
1461 break; // We found desired pass manager
1462 else if (TopPMType > PMT_ModulePassManager)
1463 PMS.pop(); // Pop children pass managers
1468 PMS.top()->add(this);
1471 /// Find appropriate Function Pass Manager or Call Graph Pass Manager
1472 /// in the PM Stack and add self into that manager.
1473 void FunctionPass::assignPassManager(PMStack &PMS,
1474 PassManagerType PreferredType) {
1476 // Find Module Pass Manager (TODO : Or Call Graph Pass Manager)
1477 while(!PMS.empty()) {
1478 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1483 FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top());
1485 // Create new Function Pass Manager
1487 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1488 PMDataManager *PMD = PMS.top();
1490 // [1] Create new Function Pass Manager
1491 FPP = new FPPassManager(PMD->getDepth() + 1);
1492 FPP->populateInheritedAnalysis(PMS);
1494 // [2] Set up new manager's top level manager
1495 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1496 TPM->addIndirectPassManager(FPP);
1498 // [3] Assign manager to manage this new manager. This may create
1499 // and push new managers into PMS
1501 // If Call Graph Pass Manager is active then use it to manage
1502 // this new Function Pass manager.
1503 if (PMD->getPassManagerType() == PMT_CallGraphPassManager)
1504 FPP->assignPassManager(PMS, PMT_CallGraphPassManager);
1506 FPP->assignPassManager(PMS);
1508 // [4] Push new manager into PMS
1512 // Assign FPP as the manager of this pass.
1516 /// Find appropriate Basic Pass Manager or Call Graph Pass Manager
1517 /// in the PM Stack and add self into that manager.
1518 void BasicBlockPass::assignPassManager(PMStack &PMS,
1519 PassManagerType PreferredType) {
1521 BBPassManager *BBP = NULL;
1523 // Basic Pass Manager is a leaf pass manager. It does not handle
1524 // any other pass manager.
1526 BBP = dynamic_cast<BBPassManager *>(PMS.top());
1528 // If leaf manager is not Basic Block Pass manager then create new
1529 // basic Block Pass manager.
1532 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1533 PMDataManager *PMD = PMS.top();
1535 // [1] Create new Basic Block Manager
1536 BBP = new BBPassManager(PMD->getDepth() + 1);
1538 // [2] Set up new manager's top level manager
1539 // Basic Block Pass Manager does not live by itself
1540 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1541 TPM->addIndirectPassManager(BBP);
1543 // [3] Assign manager to manage this new manager. This may create
1544 // and push new managers into PMS
1545 BBP->assignPassManager(PMS);
1547 // [4] Push new manager into PMS
1551 // Assign BBP as the manager of this pass.
1555 PassManagerBase::~PassManagerBase() {}
1557 /*===-- C Bindings --------------------------------------------------------===*/
1559 LLVMPassManagerRef LLVMCreatePassManager() {
1560 return wrap(new PassManager());
1563 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
1564 return wrap(new FunctionPassManager(unwrap(P)));
1567 int LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
1568 return unwrap<PassManager>(PM)->run(*unwrap(M));
1571 int LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
1572 return unwrap<FunctionPassManager>(FPM)->doInitialization();
1575 int LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
1576 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
1579 int LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
1580 return unwrap<FunctionPassManager>(FPM)->doFinalization();
1583 void LLVMDisposePassManager(LLVMPassManagerRef PM) {