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/Analysis/Dominators.h"
23 #include "llvm-c/Core.h"
29 // See PassManagers.h for Pass Manager infrastructure overview.
33 //===----------------------------------------------------------------------===//
34 // Pass debugging information. Often it is useful to find out what pass is
35 // running when a crash occurs in a utility. When this library is compiled with
36 // debugging on, a command line option (--debug-pass) is enabled that causes the
37 // pass name to be printed before it executes.
40 // Different debug levels that can be enabled...
42 None, Arguments, Structure, Executions, Details
45 bool VerifyDomInfo = false;
46 static cl::opt<bool,true>
47 VerifyDomInfoX("verify-dom-info", cl::location(VerifyDomInfo),
48 cl::desc("Verify dominator info (time consuming)"));
50 static cl::opt<enum PassDebugLevel>
51 PassDebugging("debug-pass", cl::Hidden,
52 cl::desc("Print PassManager debugging information"),
54 clEnumVal(None , "disable debug output"),
55 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
56 clEnumVal(Structure , "print pass structure before run()"),
57 clEnumVal(Executions, "print pass name before it is executed"),
58 clEnumVal(Details , "print pass details when it is executed"),
60 } // End of llvm namespace
64 //===----------------------------------------------------------------------===//
67 /// BBPassManager manages BasicBlockPass. It batches all the
68 /// pass together and sequence them to process one basic block before
69 /// processing next basic block.
70 class VISIBILITY_HIDDEN BBPassManager : public PMDataManager,
75 explicit BBPassManager(int Depth)
76 : PMDataManager(Depth), FunctionPass((intptr_t)&ID) {}
78 /// Execute all of the passes scheduled for execution. Keep track of
79 /// whether any of the passes modifies the function, and if so, return true.
80 bool runOnFunction(Function &F);
82 /// Pass Manager itself does not invalidate any analysis info.
83 void getAnalysisUsage(AnalysisUsage &Info) const {
84 Info.setPreservesAll();
87 bool doInitialization(Module &M);
88 bool doInitialization(Function &F);
89 bool doFinalization(Module &M);
90 bool doFinalization(Function &F);
92 virtual const char *getPassName() const {
93 return "BasicBlock Pass Manager";
96 // Print passes managed by this manager
97 void dumpPassStructure(unsigned Offset) {
98 llvm::cerr << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n";
99 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
100 BasicBlockPass *BP = getContainedPass(Index);
101 BP->dumpPassStructure(Offset + 1);
102 dumpLastUses(BP, Offset+1);
106 BasicBlockPass *getContainedPass(unsigned N) {
107 assert ( N < PassVector.size() && "Pass number out of range!");
108 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
112 virtual PassManagerType getPassManagerType() const {
113 return PMT_BasicBlockPassManager;
117 char BBPassManager::ID = 0;
122 //===----------------------------------------------------------------------===//
123 // FunctionPassManagerImpl
125 /// FunctionPassManagerImpl manages FPPassManagers
126 class FunctionPassManagerImpl : public Pass,
127 public PMDataManager,
128 public PMTopLevelManager {
131 explicit FunctionPassManagerImpl(int Depth) :
132 Pass((intptr_t)&ID), PMDataManager(Depth),
133 PMTopLevelManager(TLM_Function) { }
135 /// add - Add a pass to the queue of passes to run. This passes ownership of
136 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
137 /// will be destroyed as well, so there is no need to delete the pass. This
138 /// implies that all passes MUST be allocated with 'new'.
143 /// run - Execute all of the passes scheduled for execution. Keep track of
144 /// whether any of the passes modifies the module, and if so, return true.
145 bool run(Function &F);
147 /// doInitialization - Run all of the initializers for the function passes.
149 bool doInitialization(Module &M);
151 /// doFinalization - Run all of the finalizers for the function passes.
153 bool doFinalization(Module &M);
155 /// Pass Manager itself does not invalidate any analysis info.
156 void getAnalysisUsage(AnalysisUsage &Info) const {
157 Info.setPreservesAll();
160 inline void addTopLevelPass(Pass *P) {
162 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
164 // P is a immutable pass and it will be managed by this
165 // top level manager. Set up analysis resolver to connect them.
166 AnalysisResolver *AR = new AnalysisResolver(*this);
168 initializeAnalysisImpl(P);
169 addImmutablePass(IP);
170 recordAvailableAnalysis(IP);
172 P->assignPassManager(activeStack);
177 FPPassManager *getContainedManager(unsigned N) {
178 assert ( N < PassManagers.size() && "Pass number out of range!");
179 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
184 char FunctionPassManagerImpl::ID = 0;
185 //===----------------------------------------------------------------------===//
188 /// MPPassManager manages ModulePasses and function pass managers.
189 /// It batches all Module passes and function pass managers together and
190 /// sequences them to process one module.
191 class MPPassManager : public Pass, public PMDataManager {
195 explicit MPPassManager(int Depth) :
196 Pass((intptr_t)&ID), PMDataManager(Depth) { }
198 // Delete on the fly managers.
199 virtual ~MPPassManager() {
200 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
201 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
203 FunctionPassManagerImpl *FPP = I->second;
208 /// run - Execute all of the passes scheduled for execution. Keep track of
209 /// whether any of the passes modifies the module, and if so, return true.
210 bool runOnModule(Module &M);
212 /// Pass Manager itself does not invalidate any analysis info.
213 void getAnalysisUsage(AnalysisUsage &Info) const {
214 Info.setPreservesAll();
217 /// Add RequiredPass into list of lower level passes required by pass P.
218 /// RequiredPass is run on the fly by Pass Manager when P requests it
219 /// through getAnalysis interface.
220 virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
222 /// Return function pass corresponding to PassInfo PI, that is
223 /// required by module pass MP. Instantiate analysis pass, by using
224 /// its runOnFunction() for function F.
225 virtual Pass* getOnTheFlyPass(Pass *MP, const PassInfo *PI, Function &F);
227 virtual const char *getPassName() const {
228 return "Module Pass Manager";
231 // Print passes managed by this manager
232 void dumpPassStructure(unsigned Offset) {
233 llvm::cerr << std::string(Offset*2, ' ') << "ModulePass Manager\n";
234 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
235 ModulePass *MP = getContainedPass(Index);
236 MP->dumpPassStructure(Offset + 1);
237 if (FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP])
238 FPP->dumpPassStructure(Offset + 2);
239 dumpLastUses(MP, Offset+1);
243 ModulePass *getContainedPass(unsigned N) {
244 assert ( N < PassVector.size() && "Pass number out of range!");
245 ModulePass *MP = static_cast<ModulePass *>(PassVector[N]);
249 virtual PassManagerType getPassManagerType() const {
250 return PMT_ModulePassManager;
254 /// Collection of on the fly FPPassManagers. These managers manage
255 /// function passes that are required by module passes.
256 std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
259 char MPPassManager::ID = 0;
260 //===----------------------------------------------------------------------===//
264 /// PassManagerImpl manages MPPassManagers
265 class PassManagerImpl : public Pass,
266 public PMDataManager,
267 public PMTopLevelManager {
271 explicit PassManagerImpl(int Depth) :
272 Pass((intptr_t)&ID), PMDataManager(Depth),
273 PMTopLevelManager(TLM_Pass) { }
275 /// add - Add a pass to the queue of passes to run. This passes ownership of
276 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
277 /// will be destroyed as well, so there is no need to delete the pass. This
278 /// implies that all passes MUST be allocated with 'new'.
283 /// run - Execute all of the passes scheduled for execution. Keep track of
284 /// whether any of the passes modifies the module, and if so, return true.
287 /// Pass Manager itself does not invalidate any analysis info.
288 void getAnalysisUsage(AnalysisUsage &Info) const {
289 Info.setPreservesAll();
292 inline void addTopLevelPass(Pass *P) {
294 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
296 // P is a immutable pass and it will be managed by this
297 // top level manager. Set up analysis resolver to connect them.
298 AnalysisResolver *AR = new AnalysisResolver(*this);
300 initializeAnalysisImpl(P);
301 addImmutablePass(IP);
302 recordAvailableAnalysis(IP);
304 P->assignPassManager(activeStack);
309 MPPassManager *getContainedManager(unsigned N) {
310 assert ( N < PassManagers.size() && "Pass number out of range!");
311 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
317 char PassManagerImpl::ID = 0;
318 } // End of llvm namespace
322 //===----------------------------------------------------------------------===//
323 // TimingInfo Class - This class is used to calculate information about the
324 // amount of time each pass takes to execute. This only happens when
325 // -time-passes is enabled on the command line.
328 class VISIBILITY_HIDDEN TimingInfo {
329 std::map<Pass*, Timer> TimingData;
333 // Use 'create' member to get this.
334 TimingInfo() : TG("... Pass execution timing report ...") {}
336 // TimingDtor - Print out information about timing information
338 // Delete all of the timers...
340 // TimerGroup is deleted next, printing the report.
343 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
344 // to a non null value (if the -time-passes option is enabled) or it leaves it
345 // null. It may be called multiple times.
346 static void createTheTimeInfo();
348 void passStarted(Pass *P) {
350 if (dynamic_cast<PMDataManager *>(P))
353 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
354 if (I == TimingData.end())
355 I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
356 I->second.startTimer();
358 void passEnded(Pass *P) {
360 if (dynamic_cast<PMDataManager *>(P))
363 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
364 assert (I != TimingData.end() && "passStarted/passEnded not nested right!");
365 I->second.stopTimer();
369 } // End of anon namespace
371 static TimingInfo *TheTimeInfo;
373 //===----------------------------------------------------------------------===//
374 // PMTopLevelManager implementation
376 /// Initialize top level manager. Create first pass manager.
377 PMTopLevelManager::PMTopLevelManager (enum TopLevelManagerType t) {
380 MPPassManager *MPP = new MPPassManager(1);
381 MPP->setTopLevelManager(this);
383 activeStack.push(MPP);
385 else if (t == TLM_Function) {
386 FPPassManager *FPP = new FPPassManager(1);
387 FPP->setTopLevelManager(this);
389 activeStack.push(FPP);
393 /// Set pass P as the last user of the given analysis passes.
394 void PMTopLevelManager::setLastUser(SmallVector<Pass *, 12> &AnalysisPasses,
397 for (SmallVector<Pass *, 12>::iterator I = AnalysisPasses.begin(),
398 E = AnalysisPasses.end(); I != E; ++I) {
405 // If AP is the last user of other passes then make P last user of
407 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
408 LUE = LastUser.end(); LUI != LUE; ++LUI) {
409 if (LUI->second == AP)
410 LastUser[LUI->first] = P;
415 /// Collect passes whose last user is P
416 void PMTopLevelManager::collectLastUses(SmallVector<Pass *, 12> &LastUses,
418 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
419 LUE = LastUser.end(); LUI != LUE; ++LUI)
420 if (LUI->second == P)
421 LastUses.push_back(LUI->first);
424 /// Schedule pass P for execution. Make sure that passes required by
425 /// P are run before P is run. Update analysis info maintained by
426 /// the manager. Remove dead passes. This is a recursive function.
427 void PMTopLevelManager::schedulePass(Pass *P) {
429 // TODO : Allocate function manager for this pass, other wise required set
430 // may be inserted into previous function manager
432 // Give pass a chance to prepare the stage.
433 P->preparePassManager(activeStack);
435 // If P is an analysis pass and it is available then do not
436 // generate the analysis again. Stale analysis info should not be
437 // available at this point.
438 if (P->getPassInfo() &&
439 P->getPassInfo()->isAnalysis() && findAnalysisPass(P->getPassInfo()))
442 AnalysisUsage AnUsage;
443 P->getAnalysisUsage(AnUsage);
444 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
445 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
446 E = RequiredSet.end(); I != E; ++I) {
448 Pass *AnalysisPass = findAnalysisPass(*I);
450 AnalysisPass = (*I)->createPass();
451 // Schedule this analysis run first only if it is not a lower level
452 // analysis pass. Lower level analsyis passes are run on the fly.
453 if (P->getPotentialPassManagerType () >=
454 AnalysisPass->getPotentialPassManagerType())
455 schedulePass(AnalysisPass);
461 // Now all required passes are available.
465 /// Find the pass that implements Analysis AID. Search immutable
466 /// passes and all pass managers. If desired pass is not found
467 /// then return NULL.
468 Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
471 // Check pass managers
472 for (std::vector<PMDataManager *>::iterator I = PassManagers.begin(),
473 E = PassManagers.end(); P == NULL && I != E; ++I) {
474 PMDataManager *PMD = *I;
475 P = PMD->findAnalysisPass(AID, false);
478 // Check other pass managers
479 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
480 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
481 P = (*I)->findAnalysisPass(AID, false);
483 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
484 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
485 const PassInfo *PI = (*I)->getPassInfo();
489 // If Pass not found then check the interfaces implemented by Immutable Pass
491 const std::vector<const PassInfo*> &ImmPI =
492 PI->getInterfacesImplemented();
493 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
501 // Print passes managed by this top level manager.
502 void PMTopLevelManager::dumpPasses() const {
504 if (PassDebugging < Structure)
507 // Print out the immutable passes
508 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
509 ImmutablePasses[i]->dumpPassStructure(0);
512 // Every class that derives from PMDataManager also derives from Pass
513 // (sometimes indirectly), but there's no inheritance relationship
514 // between PMDataManager and Pass, so we have to dynamic_cast to get
515 // from a PMDataManager* to a Pass*.
516 for (std::vector<PMDataManager *>::const_iterator I = PassManagers.begin(),
517 E = PassManagers.end(); I != E; ++I)
518 dynamic_cast<Pass *>(*I)->dumpPassStructure(1);
521 void PMTopLevelManager::dumpArguments() const {
523 if (PassDebugging < Arguments)
526 cerr << "Pass Arguments: ";
527 for (std::vector<PMDataManager *>::const_iterator I = PassManagers.begin(),
528 E = PassManagers.end(); I != E; ++I) {
529 PMDataManager *PMD = *I;
530 PMD->dumpPassArguments();
535 void PMTopLevelManager::initializeAllAnalysisInfo() {
537 for (std::vector<PMDataManager *>::iterator I = PassManagers.begin(),
538 E = PassManagers.end(); I != E; ++I) {
539 PMDataManager *PMD = *I;
540 PMD->initializeAnalysisInfo();
543 // Initailize other pass managers
544 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
545 E = IndirectPassManagers.end(); I != E; ++I)
546 (*I)->initializeAnalysisInfo();
550 PMTopLevelManager::~PMTopLevelManager() {
551 for (std::vector<PMDataManager *>::iterator I = PassManagers.begin(),
552 E = PassManagers.end(); I != E; ++I)
555 for (std::vector<ImmutablePass *>::iterator
556 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
560 //===----------------------------------------------------------------------===//
561 // PMDataManager implementation
563 /// Augement AvailableAnalysis by adding analysis made available by pass P.
564 void PMDataManager::recordAvailableAnalysis(Pass *P) {
566 if (const PassInfo *PI = P->getPassInfo()) {
567 AvailableAnalysis[PI] = P;
569 //This pass is the current implementation of all of the interfaces it
570 //implements as well.
571 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
572 for (unsigned i = 0, e = II.size(); i != e; ++i)
573 AvailableAnalysis[II[i]] = P;
577 // Return true if P preserves high level analysis used by other
578 // passes managed by this manager
579 bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
581 AnalysisUsage AnUsage;
582 P->getAnalysisUsage(AnUsage);
584 if (AnUsage.getPreservesAll())
587 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
588 for (std::vector<Pass *>::iterator I = HigherLevelAnalysis.begin(),
589 E = HigherLevelAnalysis.end(); I != E; ++I) {
591 if (!dynamic_cast<ImmutablePass*>(P1) &&
592 std::find(PreservedSet.begin(), PreservedSet.end(),
593 P1->getPassInfo()) ==
601 /// verifyPreservedAnalysis -- Verify analysis presreved by pass P.
602 void PMDataManager::verifyPreservedAnalysis(Pass *P) {
603 AnalysisUsage AnUsage;
604 P->getAnalysisUsage(AnUsage);
605 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
607 // Verify preserved analysis
608 for (std::vector<AnalysisID>::const_iterator I = PreservedSet.begin(),
609 E = PreservedSet.end(); I != E; ++I) {
611 Pass *AP = findAnalysisPass(AID, true);
613 AP->verifyAnalysis();
617 /// verifyDomInfo - Verify dominator information if it is available.
618 void PMDataManager::verifyDomInfo(Pass &P, Function &F) {
620 if (!VerifyDomInfo || !P.getResolver())
623 DominatorTree *DT = P.getAnalysisToUpdate<DominatorTree>();
627 DominatorTree OtherDT;
628 OtherDT.getBase().recalculate(F);
629 if (DT->compare(OtherDT)) {
630 cerr << "Dominator Information for " << F.getNameStart() << "\n";
631 cerr << "Pass '" << P.getPassName() << "'\n";
632 cerr << "----- Valid -----\n";
634 cerr << "----- Invalid -----\n";
636 assert (0 && "Invalid dominator info");
639 DominanceFrontier *DF = P.getAnalysisToUpdate<DominanceFrontier>();
643 DominanceFrontier OtherDF;
644 std::vector<BasicBlock*> DTRoots = DT->getRoots();
645 OtherDF.calculate(*DT, DT->getNode(DTRoots[0]));
646 if (DF->compare(OtherDF)) {
647 cerr << "Dominator Information for " << F.getNameStart() << "\n";
648 cerr << "Pass '" << P.getPassName() << "'\n";
649 cerr << "----- Valid -----\n";
651 cerr << "----- Invalid -----\n";
653 assert (0 && "Invalid dominator info");
657 /// Remove Analysis not preserved by Pass P
658 void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
659 AnalysisUsage AnUsage;
660 P->getAnalysisUsage(AnUsage);
661 if (AnUsage.getPreservesAll())
664 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
665 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
666 E = AvailableAnalysis.end(); I != E; ) {
667 std::map<AnalysisID, Pass*>::iterator Info = I++;
668 if (!dynamic_cast<ImmutablePass*>(Info->second)
669 && std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
670 PreservedSet.end()) {
671 // Remove this analysis
672 AvailableAnalysis.erase(Info);
673 if (PassDebugging >= Details) {
674 Pass *S = Info->second;
675 cerr << " -- '" << P->getPassName() << "' is not preserving '";
676 cerr << S->getPassName() << "'\n";
681 // Check inherited analysis also. If P is not preserving analysis
682 // provided by parent manager then remove it here.
683 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
685 if (!InheritedAnalysis[Index])
688 for (std::map<AnalysisID, Pass*>::iterator
689 I = InheritedAnalysis[Index]->begin(),
690 E = InheritedAnalysis[Index]->end(); I != E; ) {
691 std::map<AnalysisID, Pass *>::iterator Info = I++;
692 if (!dynamic_cast<ImmutablePass*>(Info->second) &&
693 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
695 // Remove this analysis
696 InheritedAnalysis[Index]->erase(Info);
701 /// Remove analysis passes that are not used any longer
702 void PMDataManager::removeDeadPasses(Pass *P, const char *Msg,
703 enum PassDebuggingString DBG_STR) {
705 SmallVector<Pass *, 12> DeadPasses;
707 // If this is a on the fly manager then it does not have TPM.
711 TPM->collectLastUses(DeadPasses, P);
713 if (PassDebugging >= Details && !DeadPasses.empty()) {
714 cerr << " -*- '" << P->getPassName();
715 cerr << "' is the last user of following pass instances.";
716 cerr << " Free these instances\n";
719 for (SmallVector<Pass *, 12>::iterator I = DeadPasses.begin(),
720 E = DeadPasses.end(); I != E; ++I) {
722 dumpPassInfo(*I, FREEING_MSG, DBG_STR, Msg);
724 if (TheTimeInfo) TheTimeInfo->passStarted(*I);
725 (*I)->releaseMemory();
726 if (TheTimeInfo) TheTimeInfo->passEnded(*I);
728 std::map<AnalysisID, Pass*>::iterator Pos =
729 AvailableAnalysis.find((*I)->getPassInfo());
731 // It is possible that pass is already removed from the AvailableAnalysis
732 if (Pos != AvailableAnalysis.end())
733 AvailableAnalysis.erase(Pos);
737 /// Add pass P into the PassVector. Update
738 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
739 void PMDataManager::add(Pass *P,
740 bool ProcessAnalysis) {
742 // This manager is going to manage pass P. Set up analysis resolver
744 AnalysisResolver *AR = new AnalysisResolver(*this);
747 // If a FunctionPass F is the last user of ModulePass info M
748 // then the F's manager, not F, records itself as a last user of M.
749 SmallVector<Pass *, 12> TransferLastUses;
751 if (ProcessAnalysis) {
753 // At the moment, this pass is the last user of all required passes.
754 SmallVector<Pass *, 12> LastUses;
755 SmallVector<Pass *, 8> RequiredPasses;
756 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
758 unsigned PDepth = this->getDepth();
760 collectRequiredAnalysis(RequiredPasses,
761 ReqAnalysisNotAvailable, P);
762 for (SmallVector<Pass *, 8>::iterator I = RequiredPasses.begin(),
763 E = RequiredPasses.end(); I != E; ++I) {
764 Pass *PRequired = *I;
767 assert (PRequired->getResolver() && "Analysis Resolver is not set");
768 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
769 RDepth = DM.getDepth();
771 if (PDepth == RDepth)
772 LastUses.push_back(PRequired);
773 else if (PDepth > RDepth) {
774 // Let the parent claim responsibility of last use
775 TransferLastUses.push_back(PRequired);
776 // Keep track of higher level analysis used by this manager.
777 HigherLevelAnalysis.push_back(PRequired);
779 assert (0 && "Unable to accomodate Required Pass");
782 // Set P as P's last user until someone starts using P.
783 // However, if P is a Pass Manager then it does not need
784 // to record its last user.
785 if (!dynamic_cast<PMDataManager *>(P))
786 LastUses.push_back(P);
787 TPM->setLastUser(LastUses, P);
789 if (!TransferLastUses.empty()) {
790 Pass *My_PM = dynamic_cast<Pass *>(this);
791 TPM->setLastUser(TransferLastUses, My_PM);
792 TransferLastUses.clear();
795 // Now, take care of required analysises that are not available.
796 for (SmallVector<AnalysisID, 8>::iterator
797 I = ReqAnalysisNotAvailable.begin(),
798 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
799 Pass *AnalysisPass = (*I)->createPass();
800 this->addLowerLevelRequiredPass(P, AnalysisPass);
803 // Take a note of analysis required and made available by this pass.
804 // Remove the analysis not preserved by this pass
805 removeNotPreservedAnalysis(P);
806 recordAvailableAnalysis(P);
810 PassVector.push_back(P);
814 /// Populate RP with analysis pass that are required by
815 /// pass P and are available. Populate RP_NotAvail with analysis
816 /// pass that are required by pass P but are not available.
817 void PMDataManager::collectRequiredAnalysis(SmallVector<Pass *, 8>&RP,
818 SmallVector<AnalysisID, 8> &RP_NotAvail,
820 AnalysisUsage AnUsage;
821 P->getAnalysisUsage(AnUsage);
822 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
823 for (std::vector<AnalysisID>::const_iterator
824 I = RequiredSet.begin(), E = RequiredSet.end();
827 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
828 RP.push_back(AnalysisPass);
830 RP_NotAvail.push_back(AID);
833 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
834 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
835 E = IDs.end(); I != E; ++I) {
837 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
838 RP.push_back(AnalysisPass);
840 RP_NotAvail.push_back(AID);
844 // All Required analyses should be available to the pass as it runs! Here
845 // we fill in the AnalysisImpls member of the pass so that it can
846 // successfully use the getAnalysis() method to retrieve the
847 // implementations it needs.
849 void PMDataManager::initializeAnalysisImpl(Pass *P) {
850 AnalysisUsage AnUsage;
851 P->getAnalysisUsage(AnUsage);
853 for (std::vector<const PassInfo *>::const_iterator
854 I = AnUsage.getRequiredSet().begin(),
855 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
856 Pass *Impl = findAnalysisPass(*I, true);
858 // This may be analysis pass that is initialized on the fly.
859 // If that is not the case then it will raise an assert when it is used.
861 AnalysisResolver *AR = P->getResolver();
862 assert (AR && "Analysis Resolver is not set");
863 AR->addAnalysisImplsPair(*I, Impl);
867 /// Find the pass that implements Analysis AID. If desired pass is not found
868 /// then return NULL.
869 Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
871 // Check if AvailableAnalysis map has one entry.
872 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
874 if (I != AvailableAnalysis.end())
877 // Search Parents through TopLevelManager
879 return TPM->findAnalysisPass(AID);
884 // Print list of passes that are last used by P.
885 void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
887 SmallVector<Pass *, 12> LUses;
889 // If this is a on the fly manager then it does not have TPM.
893 TPM->collectLastUses(LUses, P);
895 for (SmallVector<Pass *, 12>::iterator I = LUses.begin(),
896 E = LUses.end(); I != E; ++I) {
897 llvm::cerr << "--" << std::string(Offset*2, ' ');
898 (*I)->dumpPassStructure(0);
902 void PMDataManager::dumpPassArguments() const {
903 for(std::vector<Pass *>::const_iterator I = PassVector.begin(),
904 E = PassVector.end(); I != E; ++I) {
905 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
906 PMD->dumpPassArguments();
908 if (const PassInfo *PI = (*I)->getPassInfo())
909 if (!PI->isAnalysisGroup())
910 cerr << " -" << PI->getPassArgument();
914 void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
915 enum PassDebuggingString S2,
917 if (PassDebugging < Executions)
919 cerr << (void*)this << std::string(getDepth()*2+1, ' ');
922 cerr << "Executing Pass '" << P->getPassName();
924 case MODIFICATION_MSG:
925 cerr << "Made Modification '" << P->getPassName();
928 cerr << " Freeing Pass '" << P->getPassName();
934 case ON_BASICBLOCK_MSG:
935 cerr << "' on BasicBlock '" << Msg << "'...\n";
937 case ON_FUNCTION_MSG:
938 cerr << "' on Function '" << Msg << "'...\n";
941 cerr << "' on Module '" << Msg << "'...\n";
944 cerr << "' on Loop " << Msg << "'...\n";
947 cerr << "' on Call Graph " << Msg << "'...\n";
954 void PMDataManager::dumpAnalysisSetInfo(const char *Msg, Pass *P,
955 const std::vector<AnalysisID> &Set)
957 if (PassDebugging >= Details && !Set.empty()) {
958 cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
959 for (unsigned i = 0; i != Set.size(); ++i) {
961 cerr << " " << Set[i]->getPassName();
967 /// Add RequiredPass into list of lower level passes required by pass P.
968 /// RequiredPass is run on the fly by Pass Manager when P requests it
969 /// through getAnalysis interface.
970 /// This should be handled by specific pass manager.
971 void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
973 TPM->dumpArguments();
977 // Module Level pass may required Function Level analysis info
978 // (e.g. dominator info). Pass manager uses on the fly function pass manager
979 // to provide this on demand. In that case, in Pass manager terminology,
980 // module level pass is requiring lower level analysis info managed by
981 // lower level pass manager.
983 // When Pass manager is not able to order required analysis info, Pass manager
984 // checks whether any lower level manager will be able to provide this
985 // analysis info on demand or not.
987 cerr << "Unable to schedule '" << RequiredPass->getPassName();
988 cerr << "' required by '" << P->getPassName() << "'\n";
990 assert (0 && "Unable to schedule pass");
994 PMDataManager::~PMDataManager() {
996 for (std::vector<Pass *>::iterator I = PassVector.begin(),
997 E = PassVector.end(); I != E; ++I)
1002 //===----------------------------------------------------------------------===//
1003 // NOTE: Is this the right place to define this method ?
1004 // getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
1005 Pass *AnalysisResolver::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
1006 return PM.findAnalysisPass(ID, dir);
1009 Pass *AnalysisResolver::findImplPass(Pass *P, const PassInfo *AnalysisPI,
1011 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1014 //===----------------------------------------------------------------------===//
1015 // BBPassManager implementation
1017 /// Execute all of the passes scheduled for execution by invoking
1018 /// runOnBasicBlock method. Keep track of whether any of the passes modifies
1019 /// the function, and if so, return true.
1021 BBPassManager::runOnFunction(Function &F) {
1023 if (F.isDeclaration())
1026 bool Changed = doInitialization(F);
1028 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
1029 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1030 BasicBlockPass *BP = getContainedPass(Index);
1031 AnalysisUsage AnUsage;
1032 BP->getAnalysisUsage(AnUsage);
1034 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getNameStart());
1035 dumpAnalysisSetInfo("Required", BP, AnUsage.getRequiredSet());
1037 initializeAnalysisImpl(BP);
1039 if (TheTimeInfo) TheTimeInfo->passStarted(BP);
1040 Changed |= BP->runOnBasicBlock(*I);
1041 if (TheTimeInfo) TheTimeInfo->passEnded(BP);
1044 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
1046 dumpAnalysisSetInfo("Preserved", BP, AnUsage.getPreservedSet());
1048 verifyPreservedAnalysis(BP);
1049 removeNotPreservedAnalysis(BP);
1050 recordAvailableAnalysis(BP);
1051 removeDeadPasses(BP, I->getNameStart(), ON_BASICBLOCK_MSG);
1054 return Changed |= doFinalization(F);
1057 // Implement doInitialization and doFinalization
1058 inline bool BBPassManager::doInitialization(Module &M) {
1059 bool Changed = false;
1061 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1062 BasicBlockPass *BP = getContainedPass(Index);
1063 Changed |= BP->doInitialization(M);
1069 inline bool BBPassManager::doFinalization(Module &M) {
1070 bool Changed = false;
1072 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1073 BasicBlockPass *BP = getContainedPass(Index);
1074 Changed |= BP->doFinalization(M);
1080 inline bool BBPassManager::doInitialization(Function &F) {
1081 bool Changed = false;
1083 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1084 BasicBlockPass *BP = getContainedPass(Index);
1085 Changed |= BP->doInitialization(F);
1091 inline bool BBPassManager::doFinalization(Function &F) {
1092 bool Changed = false;
1094 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1095 BasicBlockPass *BP = getContainedPass(Index);
1096 Changed |= BP->doFinalization(F);
1103 //===----------------------------------------------------------------------===//
1104 // FunctionPassManager implementation
1106 /// Create new Function pass manager
1107 FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
1108 FPM = new FunctionPassManagerImpl(0);
1109 // FPM is the top level manager.
1110 FPM->setTopLevelManager(FPM);
1112 AnalysisResolver *AR = new AnalysisResolver(*FPM);
1113 FPM->setResolver(AR);
1118 FunctionPassManager::~FunctionPassManager() {
1122 /// add - Add a pass to the queue of passes to run. This passes
1123 /// ownership of the Pass to the PassManager. When the
1124 /// PassManager_X is destroyed, the pass will be destroyed as well, so
1125 /// there is no need to delete the pass. (TODO delete passes.)
1126 /// This implies that all passes MUST be allocated with 'new'.
1127 void FunctionPassManager::add(Pass *P) {
1131 /// run - Execute all of the passes scheduled for execution. Keep
1132 /// track of whether any of the passes modifies the function, and if
1133 /// so, return true.
1135 bool FunctionPassManager::run(Function &F) {
1137 if (MP->materializeFunction(&F, &errstr)) {
1138 cerr << "Error reading bitcode file: " << errstr << "\n";
1145 /// doInitialization - Run all of the initializers for the function passes.
1147 bool FunctionPassManager::doInitialization() {
1148 return FPM->doInitialization(*MP->getModule());
1151 /// doFinalization - Run all of the finalizers for the function passes.
1153 bool FunctionPassManager::doFinalization() {
1154 return FPM->doFinalization(*MP->getModule());
1157 //===----------------------------------------------------------------------===//
1158 // FunctionPassManagerImpl implementation
1160 inline bool FunctionPassManagerImpl::doInitialization(Module &M) {
1161 bool Changed = false;
1163 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1164 FPPassManager *FP = getContainedManager(Index);
1165 Changed |= FP->doInitialization(M);
1171 inline bool FunctionPassManagerImpl::doFinalization(Module &M) {
1172 bool Changed = false;
1174 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1175 FPPassManager *FP = getContainedManager(Index);
1176 Changed |= FP->doFinalization(M);
1182 // Execute all the passes managed by this top level manager.
1183 // Return true if any function is modified by a pass.
1184 bool FunctionPassManagerImpl::run(Function &F) {
1186 bool Changed = false;
1188 TimingInfo::createTheTimeInfo();
1193 initializeAllAnalysisInfo();
1194 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1195 FPPassManager *FP = getContainedManager(Index);
1196 Changed |= FP->runOnFunction(F);
1201 //===----------------------------------------------------------------------===//
1202 // FPPassManager implementation
1204 char FPPassManager::ID = 0;
1205 /// Print passes managed by this manager
1206 void FPPassManager::dumpPassStructure(unsigned Offset) {
1207 llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
1208 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1209 FunctionPass *FP = getContainedPass(Index);
1210 FP->dumpPassStructure(Offset + 1);
1211 dumpLastUses(FP, Offset+1);
1216 /// Execute all of the passes scheduled for execution by invoking
1217 /// runOnFunction method. Keep track of whether any of the passes modifies
1218 /// the function, and if so, return true.
1219 bool FPPassManager::runOnFunction(Function &F) {
1221 bool Changed = false;
1223 if (F.isDeclaration())
1226 // Collect inherited analysis from Module level pass manager.
1227 populateInheritedAnalysis(TPM->activeStack);
1229 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1230 FunctionPass *FP = getContainedPass(Index);
1232 AnalysisUsage AnUsage;
1233 FP->getAnalysisUsage(AnUsage);
1235 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getNameStart());
1236 dumpAnalysisSetInfo("Required", FP, AnUsage.getRequiredSet());
1238 initializeAnalysisImpl(FP);
1240 if (TheTimeInfo) TheTimeInfo->passStarted(FP);
1241 Changed |= FP->runOnFunction(F);
1242 if (TheTimeInfo) TheTimeInfo->passEnded(FP);
1245 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getNameStart());
1246 dumpAnalysisSetInfo("Preserved", FP, AnUsage.getPreservedSet());
1248 verifyPreservedAnalysis(FP);
1249 removeNotPreservedAnalysis(FP);
1250 recordAvailableAnalysis(FP);
1251 removeDeadPasses(FP, F.getNameStart(), ON_FUNCTION_MSG);
1253 // If dominator information is available then verify the info if requested.
1254 verifyDomInfo(*FP, F);
1259 bool FPPassManager::runOnModule(Module &M) {
1261 bool Changed = doInitialization(M);
1263 for(Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1264 this->runOnFunction(*I);
1266 return Changed |= doFinalization(M);
1269 inline bool FPPassManager::doInitialization(Module &M) {
1270 bool Changed = false;
1272 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1273 FunctionPass *FP = getContainedPass(Index);
1274 Changed |= FP->doInitialization(M);
1280 inline bool FPPassManager::doFinalization(Module &M) {
1281 bool Changed = false;
1283 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1284 FunctionPass *FP = getContainedPass(Index);
1285 Changed |= FP->doFinalization(M);
1291 //===----------------------------------------------------------------------===//
1292 // MPPassManager implementation
1294 /// Execute all of the passes scheduled for execution by invoking
1295 /// runOnModule method. Keep track of whether any of the passes modifies
1296 /// the module, and if so, return true.
1298 MPPassManager::runOnModule(Module &M) {
1299 bool Changed = false;
1301 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1302 ModulePass *MP = getContainedPass(Index);
1304 AnalysisUsage AnUsage;
1305 MP->getAnalysisUsage(AnUsage);
1307 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG,
1308 M.getModuleIdentifier().c_str());
1309 dumpAnalysisSetInfo("Required", MP, AnUsage.getRequiredSet());
1311 initializeAnalysisImpl(MP);
1313 if (TheTimeInfo) TheTimeInfo->passStarted(MP);
1314 Changed |= MP->runOnModule(M);
1315 if (TheTimeInfo) TheTimeInfo->passEnded(MP);
1318 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
1319 M.getModuleIdentifier().c_str());
1320 dumpAnalysisSetInfo("Preserved", MP, AnUsage.getPreservedSet());
1322 verifyPreservedAnalysis(MP);
1323 removeNotPreservedAnalysis(MP);
1324 recordAvailableAnalysis(MP);
1325 removeDeadPasses(MP, M.getModuleIdentifier().c_str(), ON_MODULE_MSG);
1330 /// Add RequiredPass into list of lower level passes required by pass P.
1331 /// RequiredPass is run on the fly by Pass Manager when P requests it
1332 /// through getAnalysis interface.
1333 void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1335 assert (P->getPotentialPassManagerType() == PMT_ModulePassManager
1336 && "Unable to handle Pass that requires lower level Analysis pass");
1337 assert ((P->getPotentialPassManagerType() <
1338 RequiredPass->getPotentialPassManagerType())
1339 && "Unable to handle Pass that requires lower level Analysis pass");
1341 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
1343 FPP = new FunctionPassManagerImpl(0);
1344 // FPP is the top level manager.
1345 FPP->setTopLevelManager(FPP);
1347 OnTheFlyManagers[P] = FPP;
1349 FPP->add(RequiredPass);
1351 // Register P as the last user of RequiredPass.
1352 SmallVector<Pass *, 12> LU;
1353 LU.push_back(RequiredPass);
1354 FPP->setLastUser(LU, P);
1357 /// Return function pass corresponding to PassInfo PI, that is
1358 /// required by module pass MP. Instantiate analysis pass, by using
1359 /// its runOnFunction() for function F.
1360 Pass* MPPassManager::getOnTheFlyPass(Pass *MP, const PassInfo *PI,
1362 AnalysisID AID = PI;
1363 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
1364 assert (FPP && "Unable to find on the fly pass");
1367 return (dynamic_cast<PMTopLevelManager *>(FPP))->findAnalysisPass(AID);
1371 //===----------------------------------------------------------------------===//
1372 // PassManagerImpl implementation
1374 /// run - Execute all of the passes scheduled for execution. Keep track of
1375 /// whether any of the passes modifies the module, and if so, return true.
1376 bool PassManagerImpl::run(Module &M) {
1378 bool Changed = false;
1380 TimingInfo::createTheTimeInfo();
1385 initializeAllAnalysisInfo();
1386 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1387 MPPassManager *MP = getContainedManager(Index);
1388 Changed |= MP->runOnModule(M);
1393 //===----------------------------------------------------------------------===//
1394 // PassManager implementation
1396 /// Create new pass manager
1397 PassManager::PassManager() {
1398 PM = new PassManagerImpl(0);
1399 // PM is the top level manager
1400 PM->setTopLevelManager(PM);
1403 PassManager::~PassManager() {
1407 /// add - Add a pass to the queue of passes to run. This passes ownership of
1408 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
1409 /// will be destroyed as well, so there is no need to delete the pass. This
1410 /// implies that all passes MUST be allocated with 'new'.
1412 PassManager::add(Pass *P) {
1416 /// run - Execute all of the passes scheduled for execution. Keep track of
1417 /// whether any of the passes modifies the module, and if so, return true.
1419 PassManager::run(Module &M) {
1423 //===----------------------------------------------------------------------===//
1424 // TimingInfo Class - This class is used to calculate information about the
1425 // amount of time each pass takes to execute. This only happens with
1426 // -time-passes is enabled on the command line.
1428 bool llvm::TimePassesIsEnabled = false;
1429 static cl::opt<bool,true>
1430 EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1431 cl::desc("Time each pass, printing elapsed time for each on exit"));
1433 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1434 // a non null value (if the -time-passes option is enabled) or it leaves it
1435 // null. It may be called multiple times.
1436 void TimingInfo::createTheTimeInfo() {
1437 if (!TimePassesIsEnabled || TheTimeInfo) return;
1439 // Constructed the first time this is called, iff -time-passes is enabled.
1440 // This guarantees that the object will be constructed before static globals,
1441 // thus it will be destroyed before them.
1442 static ManagedStatic<TimingInfo> TTI;
1443 TheTimeInfo = &*TTI;
1446 /// If TimingInfo is enabled then start pass timer.
1447 void StartPassTimer(Pass *P) {
1449 TheTimeInfo->passStarted(P);
1452 /// If TimingInfo is enabled then stop pass timer.
1453 void StopPassTimer(Pass *P) {
1455 TheTimeInfo->passEnded(P);
1458 //===----------------------------------------------------------------------===//
1459 // PMStack implementation
1462 // Pop Pass Manager from the stack and clear its analysis info.
1463 void PMStack::pop() {
1465 PMDataManager *Top = this->top();
1466 Top->initializeAnalysisInfo();
1471 // Push PM on the stack and set its top level manager.
1472 void PMStack::push(PMDataManager *PM) {
1474 PMDataManager *Top = NULL;
1475 assert (PM && "Unable to push. Pass Manager expected");
1477 if (this->empty()) {
1482 PMTopLevelManager *TPM = Top->getTopLevelManager();
1484 assert (TPM && "Unable to find top level manager");
1485 TPM->addIndirectPassManager(PM);
1486 PM->setTopLevelManager(TPM);
1492 // Dump content of the pass manager stack.
1493 void PMStack::dump() {
1494 for(std::deque<PMDataManager *>::iterator I = S.begin(),
1495 E = S.end(); I != E; ++I) {
1496 Pass *P = dynamic_cast<Pass *>(*I);
1497 printf("%s ", P->getPassName());
1503 /// Find appropriate Module Pass Manager in the PM Stack and
1504 /// add self into that manager.
1505 void ModulePass::assignPassManager(PMStack &PMS,
1506 PassManagerType PreferredType) {
1508 // Find Module Pass Manager
1509 while(!PMS.empty()) {
1510 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1511 if (TopPMType == PreferredType)
1512 break; // We found desired pass manager
1513 else if (TopPMType > PMT_ModulePassManager)
1514 PMS.pop(); // Pop children pass managers
1519 PMS.top()->add(this);
1522 /// Find appropriate Function Pass Manager or Call Graph Pass Manager
1523 /// in the PM Stack and add self into that manager.
1524 void FunctionPass::assignPassManager(PMStack &PMS,
1525 PassManagerType PreferredType) {
1527 // Find Module Pass Manager (TODO : Or Call Graph Pass Manager)
1528 while(!PMS.empty()) {
1529 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1534 FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top());
1536 // Create new Function Pass Manager
1538 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1539 PMDataManager *PMD = PMS.top();
1541 // [1] Create new Function Pass Manager
1542 FPP = new FPPassManager(PMD->getDepth() + 1);
1543 FPP->populateInheritedAnalysis(PMS);
1545 // [2] Set up new manager's top level manager
1546 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1547 TPM->addIndirectPassManager(FPP);
1549 // [3] Assign manager to manage this new manager. This may create
1550 // and push new managers into PMS
1552 // If Call Graph Pass Manager is active then use it to manage
1553 // this new Function Pass manager.
1554 if (PMD->getPassManagerType() == PMT_CallGraphPassManager)
1555 FPP->assignPassManager(PMS, PMT_CallGraphPassManager);
1557 FPP->assignPassManager(PMS);
1559 // [4] Push new manager into PMS
1563 // Assign FPP as the manager of this pass.
1567 /// Find appropriate Basic Pass Manager or Call Graph Pass Manager
1568 /// in the PM Stack and add self into that manager.
1569 void BasicBlockPass::assignPassManager(PMStack &PMS,
1570 PassManagerType PreferredType) {
1572 BBPassManager *BBP = NULL;
1574 // Basic Pass Manager is a leaf pass manager. It does not handle
1575 // any other pass manager.
1577 BBP = dynamic_cast<BBPassManager *>(PMS.top());
1579 // If leaf manager is not Basic Block Pass manager then create new
1580 // basic Block Pass manager.
1583 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1584 PMDataManager *PMD = PMS.top();
1586 // [1] Create new Basic Block Manager
1587 BBP = new BBPassManager(PMD->getDepth() + 1);
1589 // [2] Set up new manager's top level manager
1590 // Basic Block Pass Manager does not live by itself
1591 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1592 TPM->addIndirectPassManager(BBP);
1594 // [3] Assign manager to manage this new manager. This may create
1595 // and push new managers into PMS
1596 BBP->assignPassManager(PMS);
1598 // [4] Push new manager into PMS
1602 // Assign BBP as the manager of this pass.
1606 PassManagerBase::~PassManagerBase() {}
1608 /*===-- C Bindings --------------------------------------------------------===*/
1610 LLVMPassManagerRef LLVMCreatePassManager() {
1611 return wrap(new PassManager());
1614 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
1615 return wrap(new FunctionPassManager(unwrap(P)));
1618 int LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
1619 return unwrap<PassManager>(PM)->run(*unwrap(M));
1622 int LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
1623 return unwrap<FunctionPassManager>(FPM)->doInitialization();
1626 int LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
1627 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
1630 int LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
1631 return unwrap<FunctionPassManager>(FPM)->doFinalization();
1634 void LLVMDisposePassManager(LLVMPassManagerRef PM) {