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(&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(&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(&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(&ID), PMDataManager(Depth), PMTopLevelManager(TLM_Pass) { }
274 /// add - Add a pass to the queue of passes to run. This passes ownership of
275 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
276 /// will be destroyed as well, so there is no need to delete the pass. This
277 /// implies that all passes MUST be allocated with 'new'.
282 /// run - Execute all of the passes scheduled for execution. Keep track of
283 /// whether any of the passes modifies the module, and if so, return true.
286 /// Pass Manager itself does not invalidate any analysis info.
287 void getAnalysisUsage(AnalysisUsage &Info) const {
288 Info.setPreservesAll();
291 inline void addTopLevelPass(Pass *P) {
293 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
295 // P is a immutable pass and it will be managed by this
296 // top level manager. Set up analysis resolver to connect them.
297 AnalysisResolver *AR = new AnalysisResolver(*this);
299 initializeAnalysisImpl(P);
300 addImmutablePass(IP);
301 recordAvailableAnalysis(IP);
303 P->assignPassManager(activeStack);
308 MPPassManager *getContainedManager(unsigned N) {
309 assert ( N < PassManagers.size() && "Pass number out of range!");
310 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
316 char PassManagerImpl::ID = 0;
317 } // End of llvm namespace
321 //===----------------------------------------------------------------------===//
322 // TimingInfo Class - This class is used to calculate information about the
323 // amount of time each pass takes to execute. This only happens when
324 // -time-passes is enabled on the command line.
327 class VISIBILITY_HIDDEN TimingInfo {
328 std::map<Pass*, Timer> TimingData;
332 // Use 'create' member to get this.
333 TimingInfo() : TG("... Pass execution timing report ...") {}
335 // TimingDtor - Print out information about timing information
337 // Delete all of the timers...
339 // TimerGroup is deleted next, printing the report.
342 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
343 // to a non null value (if the -time-passes option is enabled) or it leaves it
344 // null. It may be called multiple times.
345 static void createTheTimeInfo();
347 void passStarted(Pass *P) {
349 if (dynamic_cast<PMDataManager *>(P))
352 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
353 if (I == TimingData.end())
354 I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
355 I->second.startTimer();
357 void passEnded(Pass *P) {
359 if (dynamic_cast<PMDataManager *>(P))
362 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
363 assert (I != TimingData.end() && "passStarted/passEnded not nested right!");
364 I->second.stopTimer();
368 } // End of anon namespace
370 static TimingInfo *TheTimeInfo;
372 //===----------------------------------------------------------------------===//
373 // PMTopLevelManager implementation
375 /// Initialize top level manager. Create first pass manager.
376 PMTopLevelManager::PMTopLevelManager (enum TopLevelManagerType t) {
379 MPPassManager *MPP = new MPPassManager(1);
380 MPP->setTopLevelManager(this);
382 activeStack.push(MPP);
384 else if (t == TLM_Function) {
385 FPPassManager *FPP = new FPPassManager(1);
386 FPP->setTopLevelManager(this);
388 activeStack.push(FPP);
392 /// Set pass P as the last user of the given analysis passes.
393 void PMTopLevelManager::setLastUser(SmallVector<Pass *, 12> &AnalysisPasses,
396 for (SmallVector<Pass *, 12>::iterator I = AnalysisPasses.begin(),
397 E = AnalysisPasses.end(); I != E; ++I) {
404 // If AP is the last user of other passes then make P last user of
406 for (DenseMap<Pass *, Pass *>::iterator LUI = LastUser.begin(),
407 LUE = LastUser.end(); LUI != LUE; ++LUI) {
408 if (LUI->second == AP)
409 // DenseMap iterator is not invalidated here because
410 // this is just updating exisitng entry.
411 LastUser[LUI->first] = P;
416 /// Collect passes whose last user is P
417 void PMTopLevelManager::collectLastUses(SmallVector<Pass *, 12> &LastUses,
419 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
420 InversedLastUser.find(P);
421 if (DMI == InversedLastUser.end())
424 SmallPtrSet<Pass *, 8> &LU = DMI->second;
425 for (SmallPtrSet<Pass *, 8>::iterator I = LU.begin(),
426 E = LU.end(); I != E; ++I) {
427 LastUses.push_back(*I);
432 AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
433 AnalysisUsage *AnUsage = NULL;
434 DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.find(P);
435 if (DMI != AnUsageMap.end())
436 AnUsage = DMI->second;
438 AnUsage = new AnalysisUsage();
439 P->getAnalysisUsage(*AnUsage);
440 AnUsageMap[P] = AnUsage;
445 /// Schedule pass P for execution. Make sure that passes required by
446 /// P are run before P is run. Update analysis info maintained by
447 /// the manager. Remove dead passes. This is a recursive function.
448 void PMTopLevelManager::schedulePass(Pass *P) {
450 // TODO : Allocate function manager for this pass, other wise required set
451 // may be inserted into previous function manager
453 // Give pass a chance to prepare the stage.
454 P->preparePassManager(activeStack);
456 // If P is an analysis pass and it is available then do not
457 // generate the analysis again. Stale analysis info should not be
458 // available at this point.
459 if (P->getPassInfo() &&
460 P->getPassInfo()->isAnalysis() && findAnalysisPass(P->getPassInfo()))
463 AnalysisUsage *AnUsage = findAnalysisUsage(P);
465 bool checkAnalysis = true;
466 while (checkAnalysis) {
467 checkAnalysis = false;
469 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
470 for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(),
471 E = RequiredSet.end(); I != E; ++I) {
473 Pass *AnalysisPass = findAnalysisPass(*I);
475 AnalysisPass = (*I)->createPass();
476 if (P->getPotentialPassManagerType () ==
477 AnalysisPass->getPotentialPassManagerType())
478 // Schedule analysis pass that is managed by the same pass manager.
479 schedulePass(AnalysisPass);
480 else if (P->getPotentialPassManagerType () >
481 AnalysisPass->getPotentialPassManagerType()) {
482 // Schedule analysis pass that is managed by a new manager.
483 schedulePass(AnalysisPass);
484 // Recheck analysis passes to ensure that required analysises that
485 // are already checked are still available.
486 checkAnalysis = true;
489 // Do not schedule this analysis. Lower level analsyis
490 // passes are run on the fly.
496 // Now all required passes are available.
500 /// Find the pass that implements Analysis AID. Search immutable
501 /// passes and all pass managers. If desired pass is not found
502 /// then return NULL.
503 Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
506 // Check pass managers
507 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
508 E = PassManagers.end(); P == NULL && I != E; ++I) {
509 PMDataManager *PMD = *I;
510 P = PMD->findAnalysisPass(AID, false);
513 // Check other pass managers
514 for (SmallVector<PMDataManager *, 8>::iterator I = IndirectPassManagers.begin(),
515 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
516 P = (*I)->findAnalysisPass(AID, false);
518 for (SmallVector<ImmutablePass *, 8>::iterator I = ImmutablePasses.begin(),
519 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
520 const PassInfo *PI = (*I)->getPassInfo();
524 // If Pass not found then check the interfaces implemented by Immutable Pass
526 const std::vector<const PassInfo*> &ImmPI =
527 PI->getInterfacesImplemented();
528 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
536 // Print passes managed by this top level manager.
537 void PMTopLevelManager::dumpPasses() const {
539 if (PassDebugging < Structure)
542 // Print out the immutable passes
543 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
544 ImmutablePasses[i]->dumpPassStructure(0);
547 // Every class that derives from PMDataManager also derives from Pass
548 // (sometimes indirectly), but there's no inheritance relationship
549 // between PMDataManager and Pass, so we have to dynamic_cast to get
550 // from a PMDataManager* to a Pass*.
551 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
552 E = PassManagers.end(); I != E; ++I)
553 dynamic_cast<Pass *>(*I)->dumpPassStructure(1);
556 void PMTopLevelManager::dumpArguments() const {
558 if (PassDebugging < Arguments)
561 cerr << "Pass Arguments: ";
562 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
563 E = PassManagers.end(); I != E; ++I) {
564 PMDataManager *PMD = *I;
565 PMD->dumpPassArguments();
570 void PMTopLevelManager::initializeAllAnalysisInfo() {
572 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
573 E = PassManagers.end(); I != E; ++I) {
574 PMDataManager *PMD = *I;
575 PMD->initializeAnalysisInfo();
578 // Initailize other pass managers
579 for (SmallVector<PMDataManager *, 8>::iterator I = IndirectPassManagers.begin(),
580 E = IndirectPassManagers.end(); I != E; ++I)
581 (*I)->initializeAnalysisInfo();
583 for(DenseMap<Pass *, Pass *>::iterator DMI = LastUser.begin(),
584 DME = LastUser.end(); DMI != DME; ++DMI) {
585 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator InvDMI =
586 InversedLastUser.find(DMI->second);
587 if (InvDMI != InversedLastUser.end()) {
588 SmallPtrSet<Pass *, 8> &L = InvDMI->second;
589 L.insert(DMI->first);
591 SmallPtrSet<Pass *, 8> L; L.insert(DMI->first);
592 InversedLastUser[DMI->second] = L;
598 PMTopLevelManager::~PMTopLevelManager() {
599 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
600 E = PassManagers.end(); I != E; ++I)
603 for (SmallVector<ImmutablePass *, 8>::iterator
604 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
607 for (DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.begin(),
608 DME = AnUsageMap.end(); DMI != DME; ++DMI) {
609 AnalysisUsage *AU = DMI->second;
615 //===----------------------------------------------------------------------===//
616 // PMDataManager implementation
618 /// Augement AvailableAnalysis by adding analysis made available by pass P.
619 void PMDataManager::recordAvailableAnalysis(Pass *P) {
621 if (const PassInfo *PI = P->getPassInfo()) {
622 AvailableAnalysis[PI] = P;
624 //This pass is the current implementation of all of the interfaces it
625 //implements as well.
626 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
627 for (unsigned i = 0, e = II.size(); i != e; ++i)
628 AvailableAnalysis[II[i]] = P;
632 // Return true if P preserves high level analysis used by other
633 // passes managed by this manager
634 bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
636 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
638 if (AnUsage->getPreservesAll())
641 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
642 for (SmallVector<Pass *, 8>::iterator I = HigherLevelAnalysis.begin(),
643 E = HigherLevelAnalysis.end(); I != E; ++I) {
645 if (!dynamic_cast<ImmutablePass*>(P1) &&
646 std::find(PreservedSet.begin(), PreservedSet.end(),
647 P1->getPassInfo()) ==
655 /// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
656 void PMDataManager::verifyPreservedAnalysis(Pass *P) {
657 // Don't do this unless assertions are enabled.
661 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
662 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
664 // Verify preserved analysis
665 for (AnalysisUsage::VectorType::const_iterator I = PreservedSet.begin(),
666 E = PreservedSet.end(); I != E; ++I) {
668 if (Pass *AP = findAnalysisPass(AID, true))
669 AP->verifyAnalysis();
673 /// verifyDomInfo - Verify dominator information if it is available.
674 void PMDataManager::verifyDomInfo(Pass &P, Function &F) {
676 if (!VerifyDomInfo || !P.getResolver())
679 DominatorTree *DT = P.getAnalysisToUpdate<DominatorTree>();
683 DominatorTree OtherDT;
684 OtherDT.getBase().recalculate(F);
685 if (DT->compare(OtherDT)) {
686 cerr << "Dominator Information for " << F.getNameStart() << "\n";
687 cerr << "Pass '" << P.getPassName() << "'\n";
688 cerr << "----- Valid -----\n";
690 cerr << "----- Invalid -----\n";
692 assert (0 && "Invalid dominator info");
695 DominanceFrontier *DF = P.getAnalysisToUpdate<DominanceFrontier>();
699 DominanceFrontier OtherDF;
700 std::vector<BasicBlock*> DTRoots = DT->getRoots();
701 OtherDF.calculate(*DT, DT->getNode(DTRoots[0]));
702 if (DF->compare(OtherDF)) {
703 cerr << "Dominator Information for " << F.getNameStart() << "\n";
704 cerr << "Pass '" << P.getPassName() << "'\n";
705 cerr << "----- Valid -----\n";
707 cerr << "----- Invalid -----\n";
709 assert (0 && "Invalid dominator info");
713 /// Remove Analysis not preserved by Pass P
714 void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
715 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
716 if (AnUsage->getPreservesAll())
719 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
720 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
721 E = AvailableAnalysis.end(); I != E; ) {
722 std::map<AnalysisID, Pass*>::iterator Info = I++;
723 if (!dynamic_cast<ImmutablePass*>(Info->second)
724 && std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
725 PreservedSet.end()) {
726 // Remove this analysis
727 AvailableAnalysis.erase(Info);
728 if (PassDebugging >= Details) {
729 Pass *S = Info->second;
730 cerr << " -- '" << P->getPassName() << "' is not preserving '";
731 cerr << S->getPassName() << "'\n";
736 // Check inherited analysis also. If P is not preserving analysis
737 // provided by parent manager then remove it here.
738 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
740 if (!InheritedAnalysis[Index])
743 for (std::map<AnalysisID, Pass*>::iterator
744 I = InheritedAnalysis[Index]->begin(),
745 E = InheritedAnalysis[Index]->end(); I != E; ) {
746 std::map<AnalysisID, Pass *>::iterator Info = I++;
747 if (!dynamic_cast<ImmutablePass*>(Info->second) &&
748 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
750 // Remove this analysis
751 InheritedAnalysis[Index]->erase(Info);
756 /// Remove analysis passes that are not used any longer
757 void PMDataManager::removeDeadPasses(Pass *P, const char *Msg,
758 enum PassDebuggingString DBG_STR) {
760 SmallVector<Pass *, 12> DeadPasses;
762 // If this is a on the fly manager then it does not have TPM.
766 TPM->collectLastUses(DeadPasses, P);
768 if (PassDebugging >= Details && !DeadPasses.empty()) {
769 cerr << " -*- '" << P->getPassName();
770 cerr << "' is the last user of following pass instances.";
771 cerr << " Free these instances\n";
774 for (SmallVector<Pass *, 12>::iterator I = DeadPasses.begin(),
775 E = DeadPasses.end(); I != E; ++I) {
777 dumpPassInfo(*I, FREEING_MSG, DBG_STR, Msg);
779 if (TheTimeInfo) TheTimeInfo->passStarted(*I);
780 (*I)->releaseMemory();
781 if (TheTimeInfo) TheTimeInfo->passEnded(*I);
783 std::map<AnalysisID, Pass*>::iterator Pos =
784 AvailableAnalysis.find((*I)->getPassInfo());
786 // It is possible that pass is already removed from the AvailableAnalysis
787 if (Pos != AvailableAnalysis.end())
788 AvailableAnalysis.erase(Pos);
792 /// Add pass P into the PassVector. Update
793 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
794 void PMDataManager::add(Pass *P,
795 bool ProcessAnalysis) {
797 // This manager is going to manage pass P. Set up analysis resolver
799 AnalysisResolver *AR = new AnalysisResolver(*this);
802 // If a FunctionPass F is the last user of ModulePass info M
803 // then the F's manager, not F, records itself as a last user of M.
804 SmallVector<Pass *, 12> TransferLastUses;
806 if (ProcessAnalysis) {
808 // At the moment, this pass is the last user of all required passes.
809 SmallVector<Pass *, 12> LastUses;
810 SmallVector<Pass *, 8> RequiredPasses;
811 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
813 unsigned PDepth = this->getDepth();
815 collectRequiredAnalysis(RequiredPasses,
816 ReqAnalysisNotAvailable, P);
817 for (SmallVector<Pass *, 8>::iterator I = RequiredPasses.begin(),
818 E = RequiredPasses.end(); I != E; ++I) {
819 Pass *PRequired = *I;
822 assert (PRequired->getResolver() && "Analysis Resolver is not set");
823 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
824 RDepth = DM.getDepth();
826 if (PDepth == RDepth)
827 LastUses.push_back(PRequired);
828 else if (PDepth > RDepth) {
829 // Let the parent claim responsibility of last use
830 TransferLastUses.push_back(PRequired);
831 // Keep track of higher level analysis used by this manager.
832 HigherLevelAnalysis.push_back(PRequired);
834 assert (0 && "Unable to accomodate Required Pass");
837 // Set P as P's last user until someone starts using P.
838 // However, if P is a Pass Manager then it does not need
839 // to record its last user.
840 if (!dynamic_cast<PMDataManager *>(P))
841 LastUses.push_back(P);
842 TPM->setLastUser(LastUses, P);
844 if (!TransferLastUses.empty()) {
845 Pass *My_PM = dynamic_cast<Pass *>(this);
846 TPM->setLastUser(TransferLastUses, My_PM);
847 TransferLastUses.clear();
850 // Now, take care of required analysises that are not available.
851 for (SmallVector<AnalysisID, 8>::iterator
852 I = ReqAnalysisNotAvailable.begin(),
853 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
854 Pass *AnalysisPass = (*I)->createPass();
855 this->addLowerLevelRequiredPass(P, AnalysisPass);
858 // Take a note of analysis required and made available by this pass.
859 // Remove the analysis not preserved by this pass
860 removeNotPreservedAnalysis(P);
861 recordAvailableAnalysis(P);
865 PassVector.push_back(P);
869 /// Populate RP with analysis pass that are required by
870 /// pass P and are available. Populate RP_NotAvail with analysis
871 /// pass that are required by pass P but are not available.
872 void PMDataManager::collectRequiredAnalysis(SmallVector<Pass *, 8>&RP,
873 SmallVector<AnalysisID, 8> &RP_NotAvail,
875 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
876 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
877 for (AnalysisUsage::VectorType::const_iterator
878 I = RequiredSet.begin(), E = RequiredSet.end();
881 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
882 RP.push_back(AnalysisPass);
884 RP_NotAvail.push_back(AID);
887 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
888 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
889 E = IDs.end(); I != E; ++I) {
891 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
892 RP.push_back(AnalysisPass);
894 RP_NotAvail.push_back(AID);
898 // All Required analyses should be available to the pass as it runs! Here
899 // we fill in the AnalysisImpls member of the pass so that it can
900 // successfully use the getAnalysis() method to retrieve the
901 // implementations it needs.
903 void PMDataManager::initializeAnalysisImpl(Pass *P) {
904 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
906 for (AnalysisUsage::VectorType::const_iterator
907 I = AnUsage->getRequiredSet().begin(),
908 E = AnUsage->getRequiredSet().end(); I != E; ++I) {
909 Pass *Impl = findAnalysisPass(*I, true);
911 // This may be analysis pass that is initialized on the fly.
912 // If that is not the case then it will raise an assert when it is used.
914 AnalysisResolver *AR = P->getResolver();
915 assert (AR && "Analysis Resolver is not set");
916 AR->addAnalysisImplsPair(*I, Impl);
920 /// Find the pass that implements Analysis AID. If desired pass is not found
921 /// then return NULL.
922 Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
924 // Check if AvailableAnalysis map has one entry.
925 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
927 if (I != AvailableAnalysis.end())
930 // Search Parents through TopLevelManager
932 return TPM->findAnalysisPass(AID);
937 // Print list of passes that are last used by P.
938 void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
940 SmallVector<Pass *, 12> LUses;
942 // If this is a on the fly manager then it does not have TPM.
946 TPM->collectLastUses(LUses, P);
948 for (SmallVector<Pass *, 12>::iterator I = LUses.begin(),
949 E = LUses.end(); I != E; ++I) {
950 llvm::cerr << "--" << std::string(Offset*2, ' ');
951 (*I)->dumpPassStructure(0);
955 void PMDataManager::dumpPassArguments() const {
956 for(SmallVector<Pass *, 8>::const_iterator I = PassVector.begin(),
957 E = PassVector.end(); I != E; ++I) {
958 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
959 PMD->dumpPassArguments();
961 if (const PassInfo *PI = (*I)->getPassInfo())
962 if (!PI->isAnalysisGroup())
963 cerr << " -" << PI->getPassArgument();
967 void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
968 enum PassDebuggingString S2,
970 if (PassDebugging < Executions)
972 cerr << (void*)this << std::string(getDepth()*2+1, ' ');
975 cerr << "Executing Pass '" << P->getPassName();
977 case MODIFICATION_MSG:
978 cerr << "Made Modification '" << P->getPassName();
981 cerr << " Freeing Pass '" << P->getPassName();
987 case ON_BASICBLOCK_MSG:
988 cerr << "' on BasicBlock '" << Msg << "'...\n";
990 case ON_FUNCTION_MSG:
991 cerr << "' on Function '" << Msg << "'...\n";
994 cerr << "' on Module '" << Msg << "'...\n";
997 cerr << "' on Loop " << Msg << "'...\n";
1000 cerr << "' on Call Graph " << Msg << "'...\n";
1007 void PMDataManager::dumpRequiredSet(const Pass *P)
1009 if (PassDebugging < Details)
1012 AnalysisUsage analysisUsage;
1013 P->getAnalysisUsage(analysisUsage);
1014 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1017 void PMDataManager::dumpPreservedSet(const Pass *P)
1019 if (PassDebugging < Details)
1022 AnalysisUsage analysisUsage;
1023 P->getAnalysisUsage(analysisUsage);
1024 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1027 void PMDataManager::dumpAnalysisUsage(const char *Msg, const Pass *P,
1028 const AnalysisUsage::VectorType &Set)
1030 assert(PassDebugging >= Details);
1033 cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
1034 for (unsigned i = 0; i != Set.size(); ++i) {
1036 cerr << " " << Set[i]->getPassName();
1041 /// Add RequiredPass into list of lower level passes required by pass P.
1042 /// RequiredPass is run on the fly by Pass Manager when P requests it
1043 /// through getAnalysis interface.
1044 /// This should be handled by specific pass manager.
1045 void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1047 TPM->dumpArguments();
1051 // Module Level pass may required Function Level analysis info
1052 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1053 // to provide this on demand. In that case, in Pass manager terminology,
1054 // module level pass is requiring lower level analysis info managed by
1055 // lower level pass manager.
1057 // When Pass manager is not able to order required analysis info, Pass manager
1058 // checks whether any lower level manager will be able to provide this
1059 // analysis info on demand or not.
1061 cerr << "Unable to schedule '" << RequiredPass->getPassName();
1062 cerr << "' required by '" << P->getPassName() << "'\n";
1064 assert (0 && "Unable to schedule pass");
1068 PMDataManager::~PMDataManager() {
1070 for (SmallVector<Pass *, 8>::iterator I = PassVector.begin(),
1071 E = PassVector.end(); I != E; ++I)
1076 //===----------------------------------------------------------------------===//
1077 // NOTE: Is this the right place to define this method ?
1078 // getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
1079 Pass *AnalysisResolver::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
1080 return PM.findAnalysisPass(ID, dir);
1083 Pass *AnalysisResolver::findImplPass(Pass *P, const PassInfo *AnalysisPI,
1085 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1088 //===----------------------------------------------------------------------===//
1089 // BBPassManager implementation
1091 /// Execute all of the passes scheduled for execution by invoking
1092 /// runOnBasicBlock method. Keep track of whether any of the passes modifies
1093 /// the function, and if so, return true.
1095 BBPassManager::runOnFunction(Function &F) {
1097 if (F.isDeclaration())
1100 bool Changed = doInitialization(F);
1102 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
1103 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1104 BasicBlockPass *BP = getContainedPass(Index);
1106 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getNameStart());
1107 dumpRequiredSet(BP);
1109 initializeAnalysisImpl(BP);
1111 if (TheTimeInfo) TheTimeInfo->passStarted(BP);
1112 Changed |= BP->runOnBasicBlock(*I);
1113 if (TheTimeInfo) TheTimeInfo->passEnded(BP);
1116 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
1118 dumpPreservedSet(BP);
1120 verifyPreservedAnalysis(BP);
1121 removeNotPreservedAnalysis(BP);
1122 recordAvailableAnalysis(BP);
1123 removeDeadPasses(BP, I->getNameStart(), ON_BASICBLOCK_MSG);
1126 return Changed |= doFinalization(F);
1129 // Implement doInitialization and doFinalization
1130 inline bool BBPassManager::doInitialization(Module &M) {
1131 bool Changed = false;
1133 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1134 BasicBlockPass *BP = getContainedPass(Index);
1135 Changed |= BP->doInitialization(M);
1141 inline bool BBPassManager::doFinalization(Module &M) {
1142 bool Changed = false;
1144 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1145 BasicBlockPass *BP = getContainedPass(Index);
1146 Changed |= BP->doFinalization(M);
1152 inline bool BBPassManager::doInitialization(Function &F) {
1153 bool Changed = false;
1155 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1156 BasicBlockPass *BP = getContainedPass(Index);
1157 Changed |= BP->doInitialization(F);
1163 inline bool BBPassManager::doFinalization(Function &F) {
1164 bool Changed = false;
1166 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1167 BasicBlockPass *BP = getContainedPass(Index);
1168 Changed |= BP->doFinalization(F);
1175 //===----------------------------------------------------------------------===//
1176 // FunctionPassManager implementation
1178 /// Create new Function pass manager
1179 FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
1180 FPM = new FunctionPassManagerImpl(0);
1181 // FPM is the top level manager.
1182 FPM->setTopLevelManager(FPM);
1184 AnalysisResolver *AR = new AnalysisResolver(*FPM);
1185 FPM->setResolver(AR);
1190 FunctionPassManager::~FunctionPassManager() {
1194 /// add - Add a pass to the queue of passes to run. This passes
1195 /// ownership of the Pass to the PassManager. When the
1196 /// PassManager_X is destroyed, the pass will be destroyed as well, so
1197 /// there is no need to delete the pass. (TODO delete passes.)
1198 /// This implies that all passes MUST be allocated with 'new'.
1199 void FunctionPassManager::add(Pass *P) {
1203 /// run - Execute all of the passes scheduled for execution. Keep
1204 /// track of whether any of the passes modifies the function, and if
1205 /// so, return true.
1207 bool FunctionPassManager::run(Function &F) {
1209 if (MP->materializeFunction(&F, &errstr)) {
1210 cerr << "Error reading bitcode file: " << errstr << "\n";
1217 /// doInitialization - Run all of the initializers for the function passes.
1219 bool FunctionPassManager::doInitialization() {
1220 return FPM->doInitialization(*MP->getModule());
1223 /// doFinalization - Run all of the finalizers for the function passes.
1225 bool FunctionPassManager::doFinalization() {
1226 return FPM->doFinalization(*MP->getModule());
1229 //===----------------------------------------------------------------------===//
1230 // FunctionPassManagerImpl implementation
1232 inline bool FunctionPassManagerImpl::doInitialization(Module &M) {
1233 bool Changed = false;
1235 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1236 FPPassManager *FP = getContainedManager(Index);
1237 Changed |= FP->doInitialization(M);
1243 inline bool FunctionPassManagerImpl::doFinalization(Module &M) {
1244 bool Changed = false;
1246 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1247 FPPassManager *FP = getContainedManager(Index);
1248 Changed |= FP->doFinalization(M);
1254 // Execute all the passes managed by this top level manager.
1255 // Return true if any function is modified by a pass.
1256 bool FunctionPassManagerImpl::run(Function &F) {
1258 bool Changed = false;
1260 TimingInfo::createTheTimeInfo();
1265 initializeAllAnalysisInfo();
1266 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1267 FPPassManager *FP = getContainedManager(Index);
1268 Changed |= FP->runOnFunction(F);
1273 //===----------------------------------------------------------------------===//
1274 // FPPassManager implementation
1276 char FPPassManager::ID = 0;
1277 /// Print passes managed by this manager
1278 void FPPassManager::dumpPassStructure(unsigned Offset) {
1279 llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
1280 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1281 FunctionPass *FP = getContainedPass(Index);
1282 FP->dumpPassStructure(Offset + 1);
1283 dumpLastUses(FP, Offset+1);
1288 /// Execute all of the passes scheduled for execution by invoking
1289 /// runOnFunction method. Keep track of whether any of the passes modifies
1290 /// the function, and if so, return true.
1291 bool FPPassManager::runOnFunction(Function &F) {
1293 bool Changed = false;
1295 if (F.isDeclaration())
1298 // Collect inherited analysis from Module level pass manager.
1299 populateInheritedAnalysis(TPM->activeStack);
1301 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1302 FunctionPass *FP = getContainedPass(Index);
1304 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getNameStart());
1305 dumpRequiredSet(FP);
1307 initializeAnalysisImpl(FP);
1309 if (TheTimeInfo) TheTimeInfo->passStarted(FP);
1310 Changed |= FP->runOnFunction(F);
1311 if (TheTimeInfo) TheTimeInfo->passEnded(FP);
1314 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getNameStart());
1315 dumpPreservedSet(FP);
1317 verifyPreservedAnalysis(FP);
1318 removeNotPreservedAnalysis(FP);
1319 recordAvailableAnalysis(FP);
1320 removeDeadPasses(FP, F.getNameStart(), ON_FUNCTION_MSG);
1322 // If dominator information is available then verify the info if requested.
1323 verifyDomInfo(*FP, F);
1328 bool FPPassManager::runOnModule(Module &M) {
1330 bool Changed = doInitialization(M);
1332 for(Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1333 this->runOnFunction(*I);
1335 return Changed |= doFinalization(M);
1338 inline bool FPPassManager::doInitialization(Module &M) {
1339 bool Changed = false;
1341 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1342 FunctionPass *FP = getContainedPass(Index);
1343 Changed |= FP->doInitialization(M);
1349 inline bool FPPassManager::doFinalization(Module &M) {
1350 bool Changed = false;
1352 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1353 FunctionPass *FP = getContainedPass(Index);
1354 Changed |= FP->doFinalization(M);
1360 //===----------------------------------------------------------------------===//
1361 // MPPassManager implementation
1363 /// Execute all of the passes scheduled for execution by invoking
1364 /// runOnModule method. Keep track of whether any of the passes modifies
1365 /// the module, and if so, return true.
1367 MPPassManager::runOnModule(Module &M) {
1368 bool Changed = false;
1370 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1371 ModulePass *MP = getContainedPass(Index);
1373 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG,
1374 M.getModuleIdentifier().c_str());
1375 dumpRequiredSet(MP);
1377 initializeAnalysisImpl(MP);
1379 if (TheTimeInfo) TheTimeInfo->passStarted(MP);
1380 Changed |= MP->runOnModule(M);
1381 if (TheTimeInfo) TheTimeInfo->passEnded(MP);
1384 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
1385 M.getModuleIdentifier().c_str());
1386 dumpPreservedSet(MP);
1388 verifyPreservedAnalysis(MP);
1389 removeNotPreservedAnalysis(MP);
1390 recordAvailableAnalysis(MP);
1391 removeDeadPasses(MP, M.getModuleIdentifier().c_str(), ON_MODULE_MSG);
1396 /// Add RequiredPass into list of lower level passes required by pass P.
1397 /// RequiredPass is run on the fly by Pass Manager when P requests it
1398 /// through getAnalysis interface.
1399 void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1401 assert (P->getPotentialPassManagerType() == PMT_ModulePassManager
1402 && "Unable to handle Pass that requires lower level Analysis pass");
1403 assert ((P->getPotentialPassManagerType() <
1404 RequiredPass->getPotentialPassManagerType())
1405 && "Unable to handle Pass that requires lower level Analysis pass");
1407 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
1409 FPP = new FunctionPassManagerImpl(0);
1410 // FPP is the top level manager.
1411 FPP->setTopLevelManager(FPP);
1413 OnTheFlyManagers[P] = FPP;
1415 FPP->add(RequiredPass);
1417 // Register P as the last user of RequiredPass.
1418 SmallVector<Pass *, 12> LU;
1419 LU.push_back(RequiredPass);
1420 FPP->setLastUser(LU, P);
1423 /// Return function pass corresponding to PassInfo PI, that is
1424 /// required by module pass MP. Instantiate analysis pass, by using
1425 /// its runOnFunction() for function F.
1426 Pass* MPPassManager::getOnTheFlyPass(Pass *MP, const PassInfo *PI,
1428 AnalysisID AID = PI;
1429 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
1430 assert (FPP && "Unable to find on the fly pass");
1433 return (dynamic_cast<PMTopLevelManager *>(FPP))->findAnalysisPass(AID);
1437 //===----------------------------------------------------------------------===//
1438 // PassManagerImpl implementation
1440 /// run - Execute all of the passes scheduled for execution. Keep track of
1441 /// whether any of the passes modifies the module, and if so, return true.
1442 bool PassManagerImpl::run(Module &M) {
1444 bool Changed = false;
1446 TimingInfo::createTheTimeInfo();
1451 initializeAllAnalysisInfo();
1452 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1453 MPPassManager *MP = getContainedManager(Index);
1454 Changed |= MP->runOnModule(M);
1459 //===----------------------------------------------------------------------===//
1460 // PassManager implementation
1462 /// Create new pass manager
1463 PassManager::PassManager() {
1464 PM = new PassManagerImpl(0);
1465 // PM is the top level manager
1466 PM->setTopLevelManager(PM);
1469 PassManager::~PassManager() {
1473 /// add - Add a pass to the queue of passes to run. This passes ownership of
1474 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
1475 /// will be destroyed as well, so there is no need to delete the pass. This
1476 /// implies that all passes MUST be allocated with 'new'.
1478 PassManager::add(Pass *P) {
1482 /// run - Execute all of the passes scheduled for execution. Keep track of
1483 /// whether any of the passes modifies the module, and if so, return true.
1485 PassManager::run(Module &M) {
1489 //===----------------------------------------------------------------------===//
1490 // TimingInfo Class - This class is used to calculate information about the
1491 // amount of time each pass takes to execute. This only happens with
1492 // -time-passes is enabled on the command line.
1494 bool llvm::TimePassesIsEnabled = false;
1495 static cl::opt<bool,true>
1496 EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1497 cl::desc("Time each pass, printing elapsed time for each on exit"));
1499 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1500 // a non null value (if the -time-passes option is enabled) or it leaves it
1501 // null. It may be called multiple times.
1502 void TimingInfo::createTheTimeInfo() {
1503 if (!TimePassesIsEnabled || TheTimeInfo) return;
1505 // Constructed the first time this is called, iff -time-passes is enabled.
1506 // This guarantees that the object will be constructed before static globals,
1507 // thus it will be destroyed before them.
1508 static ManagedStatic<TimingInfo> TTI;
1509 TheTimeInfo = &*TTI;
1512 /// If TimingInfo is enabled then start pass timer.
1513 void StartPassTimer(Pass *P) {
1515 TheTimeInfo->passStarted(P);
1518 /// If TimingInfo is enabled then stop pass timer.
1519 void StopPassTimer(Pass *P) {
1521 TheTimeInfo->passEnded(P);
1524 //===----------------------------------------------------------------------===//
1525 // PMStack implementation
1528 // Pop Pass Manager from the stack and clear its analysis info.
1529 void PMStack::pop() {
1531 PMDataManager *Top = this->top();
1532 Top->initializeAnalysisInfo();
1537 // Push PM on the stack and set its top level manager.
1538 void PMStack::push(PMDataManager *PM) {
1540 PMDataManager *Top = NULL;
1541 assert (PM && "Unable to push. Pass Manager expected");
1543 if (this->empty()) {
1548 PMTopLevelManager *TPM = Top->getTopLevelManager();
1550 assert (TPM && "Unable to find top level manager");
1551 TPM->addIndirectPassManager(PM);
1552 PM->setTopLevelManager(TPM);
1558 // Dump content of the pass manager stack.
1559 void PMStack::dump() {
1560 for(std::deque<PMDataManager *>::iterator I = S.begin(),
1561 E = S.end(); I != E; ++I) {
1562 Pass *P = dynamic_cast<Pass *>(*I);
1563 printf("%s ", P->getPassName());
1569 /// Find appropriate Module Pass Manager in the PM Stack and
1570 /// add self into that manager.
1571 void ModulePass::assignPassManager(PMStack &PMS,
1572 PassManagerType PreferredType) {
1574 // Find Module Pass Manager
1575 while(!PMS.empty()) {
1576 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1577 if (TopPMType == PreferredType)
1578 break; // We found desired pass manager
1579 else if (TopPMType > PMT_ModulePassManager)
1580 PMS.pop(); // Pop children pass managers
1585 PMS.top()->add(this);
1588 /// Find appropriate Function Pass Manager or Call Graph Pass Manager
1589 /// in the PM Stack and add self into that manager.
1590 void FunctionPass::assignPassManager(PMStack &PMS,
1591 PassManagerType PreferredType) {
1593 // Find Module Pass Manager
1594 while(!PMS.empty()) {
1595 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1600 FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top());
1602 // Create new Function Pass Manager
1604 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1605 PMDataManager *PMD = PMS.top();
1607 // [1] Create new Function Pass Manager
1608 FPP = new FPPassManager(PMD->getDepth() + 1);
1609 FPP->populateInheritedAnalysis(PMS);
1611 // [2] Set up new manager's top level manager
1612 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1613 TPM->addIndirectPassManager(FPP);
1615 // [3] Assign manager to manage this new manager. This may create
1616 // and push new managers into PMS
1617 FPP->assignPassManager(PMS, PMD->getPassManagerType());
1619 // [4] Push new manager into PMS
1623 // Assign FPP as the manager of this pass.
1627 /// Find appropriate Basic Pass Manager or Call Graph Pass Manager
1628 /// in the PM Stack and add self into that manager.
1629 void BasicBlockPass::assignPassManager(PMStack &PMS,
1630 PassManagerType PreferredType) {
1632 BBPassManager *BBP = NULL;
1634 // Basic Pass Manager is a leaf pass manager. It does not handle
1635 // any other pass manager.
1637 BBP = dynamic_cast<BBPassManager *>(PMS.top());
1639 // If leaf manager is not Basic Block Pass manager then create new
1640 // basic Block Pass manager.
1643 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1644 PMDataManager *PMD = PMS.top();
1646 // [1] Create new Basic Block Manager
1647 BBP = new BBPassManager(PMD->getDepth() + 1);
1649 // [2] Set up new manager's top level manager
1650 // Basic Block Pass Manager does not live by itself
1651 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1652 TPM->addIndirectPassManager(BBP);
1654 // [3] Assign manager to manage this new manager. This may create
1655 // and push new managers into PMS
1656 BBP->assignPassManager(PMS);
1658 // [4] Push new manager into PMS
1662 // Assign BBP as the manager of this pass.
1666 PassManagerBase::~PassManagerBase() {}
1668 /*===-- C Bindings --------------------------------------------------------===*/
1670 LLVMPassManagerRef LLVMCreatePassManager() {
1671 return wrap(new PassManager());
1674 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
1675 return wrap(new FunctionPassManager(unwrap(P)));
1678 int LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
1679 return unwrap<PassManager>(PM)->run(*unwrap(M));
1682 int LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
1683 return unwrap<FunctionPassManager>(FPM)->doInitialization();
1686 int LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
1687 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
1690 int LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
1691 return unwrap<FunctionPassManager>(FPM)->doFinalization();
1694 void LLVMDisposePassManager(LLVMPassManagerRef PM) {