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 (DenseMap<Pass *, Pass *>::iterator LUI = LastUser.begin(),
408 LUE = LastUser.end(); LUI != LUE; ++LUI) {
409 if (LUI->second == AP)
410 // DenseMap iterator is not invalidated here because
411 // this is just updating exisitng entry.
412 LastUser[LUI->first] = P;
417 /// Collect passes whose last user is P
418 void PMTopLevelManager::collectLastUses(SmallVector<Pass *, 12> &LastUses,
420 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
421 InversedLastUser.find(P);
422 if (DMI == InversedLastUser.end())
425 SmallPtrSet<Pass *, 8> &LU = DMI->second;
426 for (SmallPtrSet<Pass *, 8>::iterator I = LU.begin(),
427 E = LU.end(); I != E; ++I) {
428 LastUses.push_back(*I);
433 AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
434 AnalysisUsage *AnUsage = NULL;
435 DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.find(P);
436 if (DMI != AnUsageMap.end())
437 AnUsage = DMI->second;
439 AnUsage = new AnalysisUsage();
440 P->getAnalysisUsage(*AnUsage);
441 AnUsageMap[P] = AnUsage;
446 /// Schedule pass P for execution. Make sure that passes required by
447 /// P are run before P is run. Update analysis info maintained by
448 /// the manager. Remove dead passes. This is a recursive function.
449 void PMTopLevelManager::schedulePass(Pass *P) {
451 // TODO : Allocate function manager for this pass, other wise required set
452 // may be inserted into previous function manager
454 // Give pass a chance to prepare the stage.
455 P->preparePassManager(activeStack);
457 // If P is an analysis pass and it is available then do not
458 // generate the analysis again. Stale analysis info should not be
459 // available at this point.
460 if (P->getPassInfo() &&
461 P->getPassInfo()->isAnalysis() && findAnalysisPass(P->getPassInfo()))
464 AnalysisUsage *AnUsage = findAnalysisUsage(P);
466 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
467 for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(),
468 E = RequiredSet.end(); I != E; ++I) {
470 Pass *AnalysisPass = findAnalysisPass(*I);
472 AnalysisPass = (*I)->createPass();
473 // Schedule this analysis run first only if it is not a lower level
474 // analysis pass. Lower level analsyis passes are run on the fly.
475 if (P->getPotentialPassManagerType () >=
476 AnalysisPass->getPotentialPassManagerType())
477 schedulePass(AnalysisPass);
483 // Now all required passes are available.
487 /// Find the pass that implements Analysis AID. Search immutable
488 /// passes and all pass managers. If desired pass is not found
489 /// then return NULL.
490 Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
493 // Check pass managers
494 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
495 E = PassManagers.end(); P == NULL && I != E; ++I) {
496 PMDataManager *PMD = *I;
497 P = PMD->findAnalysisPass(AID, false);
500 // Check other pass managers
501 for (SmallVector<PMDataManager *, 8>::iterator I = IndirectPassManagers.begin(),
502 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
503 P = (*I)->findAnalysisPass(AID, false);
505 for (SmallVector<ImmutablePass *, 8>::iterator I = ImmutablePasses.begin(),
506 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
507 const PassInfo *PI = (*I)->getPassInfo();
511 // If Pass not found then check the interfaces implemented by Immutable Pass
513 const std::vector<const PassInfo*> &ImmPI =
514 PI->getInterfacesImplemented();
515 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
523 // Print passes managed by this top level manager.
524 void PMTopLevelManager::dumpPasses() const {
526 if (PassDebugging < Structure)
529 // Print out the immutable passes
530 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
531 ImmutablePasses[i]->dumpPassStructure(0);
534 // Every class that derives from PMDataManager also derives from Pass
535 // (sometimes indirectly), but there's no inheritance relationship
536 // between PMDataManager and Pass, so we have to dynamic_cast to get
537 // from a PMDataManager* to a Pass*.
538 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
539 E = PassManagers.end(); I != E; ++I)
540 dynamic_cast<Pass *>(*I)->dumpPassStructure(1);
543 void PMTopLevelManager::dumpArguments() const {
545 if (PassDebugging < Arguments)
548 cerr << "Pass Arguments: ";
549 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
550 E = PassManagers.end(); I != E; ++I) {
551 PMDataManager *PMD = *I;
552 PMD->dumpPassArguments();
557 void PMTopLevelManager::initializeAllAnalysisInfo() {
559 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
560 E = PassManagers.end(); I != E; ++I) {
561 PMDataManager *PMD = *I;
562 PMD->initializeAnalysisInfo();
565 // Initailize other pass managers
566 for (SmallVector<PMDataManager *, 8>::iterator I = IndirectPassManagers.begin(),
567 E = IndirectPassManagers.end(); I != E; ++I)
568 (*I)->initializeAnalysisInfo();
570 for(DenseMap<Pass *, Pass *>::iterator DMI = LastUser.begin(),
571 DME = LastUser.end(); DMI != DME; ++DMI) {
572 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator InvDMI =
573 InversedLastUser.find(DMI->second);
574 if (InvDMI != InversedLastUser.end()) {
575 SmallPtrSet<Pass *, 8> &L = InvDMI->second;
576 L.insert(DMI->first);
578 SmallPtrSet<Pass *, 8> L; L.insert(DMI->first);
579 InversedLastUser[DMI->second] = L;
585 PMTopLevelManager::~PMTopLevelManager() {
586 for (SmallVector<PMDataManager *, 8>::iterator I = PassManagers.begin(),
587 E = PassManagers.end(); I != E; ++I)
590 for (SmallVector<ImmutablePass *, 8>::iterator
591 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
594 for (DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.begin(),
595 DME = AnUsageMap.end(); DMI != DME; ++DMI) {
596 AnalysisUsage *AU = DMI->second;
602 //===----------------------------------------------------------------------===//
603 // PMDataManager implementation
605 /// Augement AvailableAnalysis by adding analysis made available by pass P.
606 void PMDataManager::recordAvailableAnalysis(Pass *P) {
608 if (const PassInfo *PI = P->getPassInfo()) {
609 AvailableAnalysis[PI] = P;
611 //This pass is the current implementation of all of the interfaces it
612 //implements as well.
613 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
614 for (unsigned i = 0, e = II.size(); i != e; ++i)
615 AvailableAnalysis[II[i]] = P;
619 // Return true if P preserves high level analysis used by other
620 // passes managed by this manager
621 bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
623 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
625 if (AnUsage->getPreservesAll())
628 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
629 for (SmallVector<Pass *, 8>::iterator I = HigherLevelAnalysis.begin(),
630 E = HigherLevelAnalysis.end(); I != E; ++I) {
632 if (!dynamic_cast<ImmutablePass*>(P1) &&
633 std::find(PreservedSet.begin(), PreservedSet.end(),
634 P1->getPassInfo()) ==
642 /// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
643 void PMDataManager::verifyPreservedAnalysis(Pass *P) {
644 // Don't do this unless assertions are enabled.
648 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
649 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
651 // Verify preserved analysis
652 for (AnalysisUsage::VectorType::const_iterator I = PreservedSet.begin(),
653 E = PreservedSet.end(); I != E; ++I) {
655 if (Pass *AP = findAnalysisPass(AID, true))
656 AP->verifyAnalysis();
660 /// verifyDomInfo - Verify dominator information if it is available.
661 void PMDataManager::verifyDomInfo(Pass &P, Function &F) {
663 if (!VerifyDomInfo || !P.getResolver())
666 DominatorTree *DT = P.getAnalysisToUpdate<DominatorTree>();
670 DominatorTree OtherDT;
671 OtherDT.getBase().recalculate(F);
672 if (DT->compare(OtherDT)) {
673 cerr << "Dominator Information for " << F.getNameStart() << "\n";
674 cerr << "Pass '" << P.getPassName() << "'\n";
675 cerr << "----- Valid -----\n";
677 cerr << "----- Invalid -----\n";
679 assert (0 && "Invalid dominator info");
682 DominanceFrontier *DF = P.getAnalysisToUpdate<DominanceFrontier>();
686 DominanceFrontier OtherDF;
687 std::vector<BasicBlock*> DTRoots = DT->getRoots();
688 OtherDF.calculate(*DT, DT->getNode(DTRoots[0]));
689 if (DF->compare(OtherDF)) {
690 cerr << "Dominator Information for " << F.getNameStart() << "\n";
691 cerr << "Pass '" << P.getPassName() << "'\n";
692 cerr << "----- Valid -----\n";
694 cerr << "----- Invalid -----\n";
696 assert (0 && "Invalid dominator info");
700 /// Remove Analysis not preserved by Pass P
701 void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
702 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
703 if (AnUsage->getPreservesAll())
706 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
707 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
708 E = AvailableAnalysis.end(); I != E; ) {
709 std::map<AnalysisID, Pass*>::iterator Info = I++;
710 if (!dynamic_cast<ImmutablePass*>(Info->second)
711 && std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
712 PreservedSet.end()) {
713 // Remove this analysis
714 AvailableAnalysis.erase(Info);
715 if (PassDebugging >= Details) {
716 Pass *S = Info->second;
717 cerr << " -- '" << P->getPassName() << "' is not preserving '";
718 cerr << S->getPassName() << "'\n";
723 // Check inherited analysis also. If P is not preserving analysis
724 // provided by parent manager then remove it here.
725 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
727 if (!InheritedAnalysis[Index])
730 for (std::map<AnalysisID, Pass*>::iterator
731 I = InheritedAnalysis[Index]->begin(),
732 E = InheritedAnalysis[Index]->end(); I != E; ) {
733 std::map<AnalysisID, Pass *>::iterator Info = I++;
734 if (!dynamic_cast<ImmutablePass*>(Info->second) &&
735 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
737 // Remove this analysis
738 InheritedAnalysis[Index]->erase(Info);
743 /// Remove analysis passes that are not used any longer
744 void PMDataManager::removeDeadPasses(Pass *P, const char *Msg,
745 enum PassDebuggingString DBG_STR) {
747 SmallVector<Pass *, 12> DeadPasses;
749 // If this is a on the fly manager then it does not have TPM.
753 TPM->collectLastUses(DeadPasses, P);
755 if (PassDebugging >= Details && !DeadPasses.empty()) {
756 cerr << " -*- '" << P->getPassName();
757 cerr << "' is the last user of following pass instances.";
758 cerr << " Free these instances\n";
761 for (SmallVector<Pass *, 12>::iterator I = DeadPasses.begin(),
762 E = DeadPasses.end(); I != E; ++I) {
764 dumpPassInfo(*I, FREEING_MSG, DBG_STR, Msg);
766 if (TheTimeInfo) TheTimeInfo->passStarted(*I);
767 (*I)->releaseMemory();
768 if (TheTimeInfo) TheTimeInfo->passEnded(*I);
770 std::map<AnalysisID, Pass*>::iterator Pos =
771 AvailableAnalysis.find((*I)->getPassInfo());
773 // It is possible that pass is already removed from the AvailableAnalysis
774 if (Pos != AvailableAnalysis.end())
775 AvailableAnalysis.erase(Pos);
779 /// Add pass P into the PassVector. Update
780 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
781 void PMDataManager::add(Pass *P,
782 bool ProcessAnalysis) {
784 // This manager is going to manage pass P. Set up analysis resolver
786 AnalysisResolver *AR = new AnalysisResolver(*this);
789 // If a FunctionPass F is the last user of ModulePass info M
790 // then the F's manager, not F, records itself as a last user of M.
791 SmallVector<Pass *, 12> TransferLastUses;
793 if (ProcessAnalysis) {
795 // At the moment, this pass is the last user of all required passes.
796 SmallVector<Pass *, 12> LastUses;
797 SmallVector<Pass *, 8> RequiredPasses;
798 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
800 unsigned PDepth = this->getDepth();
802 collectRequiredAnalysis(RequiredPasses,
803 ReqAnalysisNotAvailable, P);
804 for (SmallVector<Pass *, 8>::iterator I = RequiredPasses.begin(),
805 E = RequiredPasses.end(); I != E; ++I) {
806 Pass *PRequired = *I;
809 assert (PRequired->getResolver() && "Analysis Resolver is not set");
810 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
811 RDepth = DM.getDepth();
813 if (PDepth == RDepth)
814 LastUses.push_back(PRequired);
815 else if (PDepth > RDepth) {
816 // Let the parent claim responsibility of last use
817 TransferLastUses.push_back(PRequired);
818 // Keep track of higher level analysis used by this manager.
819 HigherLevelAnalysis.push_back(PRequired);
821 assert (0 && "Unable to accomodate Required Pass");
824 // Set P as P's last user until someone starts using P.
825 // However, if P is a Pass Manager then it does not need
826 // to record its last user.
827 if (!dynamic_cast<PMDataManager *>(P))
828 LastUses.push_back(P);
829 TPM->setLastUser(LastUses, P);
831 if (!TransferLastUses.empty()) {
832 Pass *My_PM = dynamic_cast<Pass *>(this);
833 TPM->setLastUser(TransferLastUses, My_PM);
834 TransferLastUses.clear();
837 // Now, take care of required analysises that are not available.
838 for (SmallVector<AnalysisID, 8>::iterator
839 I = ReqAnalysisNotAvailable.begin(),
840 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
841 Pass *AnalysisPass = (*I)->createPass();
842 this->addLowerLevelRequiredPass(P, AnalysisPass);
845 // Take a note of analysis required and made available by this pass.
846 // Remove the analysis not preserved by this pass
847 removeNotPreservedAnalysis(P);
848 recordAvailableAnalysis(P);
852 PassVector.push_back(P);
856 /// Populate RP with analysis pass that are required by
857 /// pass P and are available. Populate RP_NotAvail with analysis
858 /// pass that are required by pass P but are not available.
859 void PMDataManager::collectRequiredAnalysis(SmallVector<Pass *, 8>&RP,
860 SmallVector<AnalysisID, 8> &RP_NotAvail,
862 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
863 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
864 for (AnalysisUsage::VectorType::const_iterator
865 I = RequiredSet.begin(), E = RequiredSet.end();
868 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
869 RP.push_back(AnalysisPass);
871 RP_NotAvail.push_back(AID);
874 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
875 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
876 E = IDs.end(); I != E; ++I) {
878 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
879 RP.push_back(AnalysisPass);
881 RP_NotAvail.push_back(AID);
885 // All Required analyses should be available to the pass as it runs! Here
886 // we fill in the AnalysisImpls member of the pass so that it can
887 // successfully use the getAnalysis() method to retrieve the
888 // implementations it needs.
890 void PMDataManager::initializeAnalysisImpl(Pass *P) {
891 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
893 for (AnalysisUsage::VectorType::const_iterator
894 I = AnUsage->getRequiredSet().begin(),
895 E = AnUsage->getRequiredSet().end(); I != E; ++I) {
896 Pass *Impl = findAnalysisPass(*I, true);
898 // This may be analysis pass that is initialized on the fly.
899 // If that is not the case then it will raise an assert when it is used.
901 AnalysisResolver *AR = P->getResolver();
902 assert (AR && "Analysis Resolver is not set");
903 AR->addAnalysisImplsPair(*I, Impl);
907 /// Find the pass that implements Analysis AID. If desired pass is not found
908 /// then return NULL.
909 Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
911 // Check if AvailableAnalysis map has one entry.
912 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
914 if (I != AvailableAnalysis.end())
917 // Search Parents through TopLevelManager
919 return TPM->findAnalysisPass(AID);
924 // Print list of passes that are last used by P.
925 void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
927 SmallVector<Pass *, 12> LUses;
929 // If this is a on the fly manager then it does not have TPM.
933 TPM->collectLastUses(LUses, P);
935 for (SmallVector<Pass *, 12>::iterator I = LUses.begin(),
936 E = LUses.end(); I != E; ++I) {
937 llvm::cerr << "--" << std::string(Offset*2, ' ');
938 (*I)->dumpPassStructure(0);
942 void PMDataManager::dumpPassArguments() const {
943 for(SmallVector<Pass *, 8>::const_iterator I = PassVector.begin(),
944 E = PassVector.end(); I != E; ++I) {
945 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
946 PMD->dumpPassArguments();
948 if (const PassInfo *PI = (*I)->getPassInfo())
949 if (!PI->isAnalysisGroup())
950 cerr << " -" << PI->getPassArgument();
954 void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
955 enum PassDebuggingString S2,
957 if (PassDebugging < Executions)
959 cerr << (void*)this << std::string(getDepth()*2+1, ' ');
962 cerr << "Executing Pass '" << P->getPassName();
964 case MODIFICATION_MSG:
965 cerr << "Made Modification '" << P->getPassName();
968 cerr << " Freeing Pass '" << P->getPassName();
974 case ON_BASICBLOCK_MSG:
975 cerr << "' on BasicBlock '" << Msg << "'...\n";
977 case ON_FUNCTION_MSG:
978 cerr << "' on Function '" << Msg << "'...\n";
981 cerr << "' on Module '" << Msg << "'...\n";
984 cerr << "' on Loop " << Msg << "'...\n";
987 cerr << "' on Call Graph " << Msg << "'...\n";
994 void PMDataManager::dumpRequiredSet(const Pass *P)
996 if (PassDebugging < Details)
999 AnalysisUsage analysisUsage;
1000 P->getAnalysisUsage(analysisUsage);
1001 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1004 void PMDataManager::dumpPreservedSet(const Pass *P)
1006 if (PassDebugging < Details)
1009 AnalysisUsage analysisUsage;
1010 P->getAnalysisUsage(analysisUsage);
1011 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1014 void PMDataManager::dumpAnalysisUsage(const char *Msg, const Pass *P,
1015 const AnalysisUsage::VectorType &Set)
1017 assert(PassDebugging >= Details);
1020 cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
1021 for (unsigned i = 0; i != Set.size(); ++i) {
1023 cerr << " " << Set[i]->getPassName();
1028 /// Add RequiredPass into list of lower level passes required by pass P.
1029 /// RequiredPass is run on the fly by Pass Manager when P requests it
1030 /// through getAnalysis interface.
1031 /// This should be handled by specific pass manager.
1032 void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1034 TPM->dumpArguments();
1038 // Module Level pass may required Function Level analysis info
1039 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1040 // to provide this on demand. In that case, in Pass manager terminology,
1041 // module level pass is requiring lower level analysis info managed by
1042 // lower level pass manager.
1044 // When Pass manager is not able to order required analysis info, Pass manager
1045 // checks whether any lower level manager will be able to provide this
1046 // analysis info on demand or not.
1048 cerr << "Unable to schedule '" << RequiredPass->getPassName();
1049 cerr << "' required by '" << P->getPassName() << "'\n";
1051 assert (0 && "Unable to schedule pass");
1055 PMDataManager::~PMDataManager() {
1057 for (SmallVector<Pass *, 8>::iterator I = PassVector.begin(),
1058 E = PassVector.end(); I != E; ++I)
1063 //===----------------------------------------------------------------------===//
1064 // NOTE: Is this the right place to define this method ?
1065 // getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
1066 Pass *AnalysisResolver::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
1067 return PM.findAnalysisPass(ID, dir);
1070 Pass *AnalysisResolver::findImplPass(Pass *P, const PassInfo *AnalysisPI,
1072 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1075 //===----------------------------------------------------------------------===//
1076 // BBPassManager implementation
1078 /// Execute all of the passes scheduled for execution by invoking
1079 /// runOnBasicBlock method. Keep track of whether any of the passes modifies
1080 /// the function, and if so, return true.
1082 BBPassManager::runOnFunction(Function &F) {
1084 if (F.isDeclaration())
1087 bool Changed = doInitialization(F);
1089 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
1090 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1091 BasicBlockPass *BP = getContainedPass(Index);
1093 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getNameStart());
1094 dumpRequiredSet(BP);
1096 initializeAnalysisImpl(BP);
1098 if (TheTimeInfo) TheTimeInfo->passStarted(BP);
1099 Changed |= BP->runOnBasicBlock(*I);
1100 if (TheTimeInfo) TheTimeInfo->passEnded(BP);
1103 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
1105 dumpPreservedSet(BP);
1107 verifyPreservedAnalysis(BP);
1108 removeNotPreservedAnalysis(BP);
1109 recordAvailableAnalysis(BP);
1110 removeDeadPasses(BP, I->getNameStart(), ON_BASICBLOCK_MSG);
1113 return Changed |= doFinalization(F);
1116 // Implement doInitialization and doFinalization
1117 inline bool BBPassManager::doInitialization(Module &M) {
1118 bool Changed = false;
1120 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1121 BasicBlockPass *BP = getContainedPass(Index);
1122 Changed |= BP->doInitialization(M);
1128 inline bool BBPassManager::doFinalization(Module &M) {
1129 bool Changed = false;
1131 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1132 BasicBlockPass *BP = getContainedPass(Index);
1133 Changed |= BP->doFinalization(M);
1139 inline bool BBPassManager::doInitialization(Function &F) {
1140 bool Changed = false;
1142 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1143 BasicBlockPass *BP = getContainedPass(Index);
1144 Changed |= BP->doInitialization(F);
1150 inline bool BBPassManager::doFinalization(Function &F) {
1151 bool Changed = false;
1153 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1154 BasicBlockPass *BP = getContainedPass(Index);
1155 Changed |= BP->doFinalization(F);
1162 //===----------------------------------------------------------------------===//
1163 // FunctionPassManager implementation
1165 /// Create new Function pass manager
1166 FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
1167 FPM = new FunctionPassManagerImpl(0);
1168 // FPM is the top level manager.
1169 FPM->setTopLevelManager(FPM);
1171 AnalysisResolver *AR = new AnalysisResolver(*FPM);
1172 FPM->setResolver(AR);
1177 FunctionPassManager::~FunctionPassManager() {
1181 /// add - Add a pass to the queue of passes to run. This passes
1182 /// ownership of the Pass to the PassManager. When the
1183 /// PassManager_X is destroyed, the pass will be destroyed as well, so
1184 /// there is no need to delete the pass. (TODO delete passes.)
1185 /// This implies that all passes MUST be allocated with 'new'.
1186 void FunctionPassManager::add(Pass *P) {
1190 /// run - Execute all of the passes scheduled for execution. Keep
1191 /// track of whether any of the passes modifies the function, and if
1192 /// so, return true.
1194 bool FunctionPassManager::run(Function &F) {
1196 if (MP->materializeFunction(&F, &errstr)) {
1197 cerr << "Error reading bitcode file: " << errstr << "\n";
1204 /// doInitialization - Run all of the initializers for the function passes.
1206 bool FunctionPassManager::doInitialization() {
1207 return FPM->doInitialization(*MP->getModule());
1210 /// doFinalization - Run all of the finalizers for the function passes.
1212 bool FunctionPassManager::doFinalization() {
1213 return FPM->doFinalization(*MP->getModule());
1216 //===----------------------------------------------------------------------===//
1217 // FunctionPassManagerImpl implementation
1219 inline bool FunctionPassManagerImpl::doInitialization(Module &M) {
1220 bool Changed = false;
1222 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1223 FPPassManager *FP = getContainedManager(Index);
1224 Changed |= FP->doInitialization(M);
1230 inline bool FunctionPassManagerImpl::doFinalization(Module &M) {
1231 bool Changed = false;
1233 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1234 FPPassManager *FP = getContainedManager(Index);
1235 Changed |= FP->doFinalization(M);
1241 // Execute all the passes managed by this top level manager.
1242 // Return true if any function is modified by a pass.
1243 bool FunctionPassManagerImpl::run(Function &F) {
1245 bool Changed = false;
1247 TimingInfo::createTheTimeInfo();
1252 initializeAllAnalysisInfo();
1253 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1254 FPPassManager *FP = getContainedManager(Index);
1255 Changed |= FP->runOnFunction(F);
1260 //===----------------------------------------------------------------------===//
1261 // FPPassManager implementation
1263 char FPPassManager::ID = 0;
1264 /// Print passes managed by this manager
1265 void FPPassManager::dumpPassStructure(unsigned Offset) {
1266 llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
1267 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1268 FunctionPass *FP = getContainedPass(Index);
1269 FP->dumpPassStructure(Offset + 1);
1270 dumpLastUses(FP, Offset+1);
1275 /// Execute all of the passes scheduled for execution by invoking
1276 /// runOnFunction method. Keep track of whether any of the passes modifies
1277 /// the function, and if so, return true.
1278 bool FPPassManager::runOnFunction(Function &F) {
1280 bool Changed = false;
1282 if (F.isDeclaration())
1285 // Collect inherited analysis from Module level pass manager.
1286 populateInheritedAnalysis(TPM->activeStack);
1288 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1289 FunctionPass *FP = getContainedPass(Index);
1291 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getNameStart());
1292 dumpRequiredSet(FP);
1294 initializeAnalysisImpl(FP);
1296 if (TheTimeInfo) TheTimeInfo->passStarted(FP);
1297 Changed |= FP->runOnFunction(F);
1298 if (TheTimeInfo) TheTimeInfo->passEnded(FP);
1301 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getNameStart());
1302 dumpPreservedSet(FP);
1304 verifyPreservedAnalysis(FP);
1305 removeNotPreservedAnalysis(FP);
1306 recordAvailableAnalysis(FP);
1307 removeDeadPasses(FP, F.getNameStart(), ON_FUNCTION_MSG);
1309 // If dominator information is available then verify the info if requested.
1310 verifyDomInfo(*FP, F);
1315 bool FPPassManager::runOnModule(Module &M) {
1317 bool Changed = doInitialization(M);
1319 for(Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1320 this->runOnFunction(*I);
1322 return Changed |= doFinalization(M);
1325 inline bool FPPassManager::doInitialization(Module &M) {
1326 bool Changed = false;
1328 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1329 FunctionPass *FP = getContainedPass(Index);
1330 Changed |= FP->doInitialization(M);
1336 inline bool FPPassManager::doFinalization(Module &M) {
1337 bool Changed = false;
1339 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1340 FunctionPass *FP = getContainedPass(Index);
1341 Changed |= FP->doFinalization(M);
1347 //===----------------------------------------------------------------------===//
1348 // MPPassManager implementation
1350 /// Execute all of the passes scheduled for execution by invoking
1351 /// runOnModule method. Keep track of whether any of the passes modifies
1352 /// the module, and if so, return true.
1354 MPPassManager::runOnModule(Module &M) {
1355 bool Changed = false;
1357 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1358 ModulePass *MP = getContainedPass(Index);
1360 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG,
1361 M.getModuleIdentifier().c_str());
1362 dumpRequiredSet(MP);
1364 initializeAnalysisImpl(MP);
1366 if (TheTimeInfo) TheTimeInfo->passStarted(MP);
1367 Changed |= MP->runOnModule(M);
1368 if (TheTimeInfo) TheTimeInfo->passEnded(MP);
1371 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
1372 M.getModuleIdentifier().c_str());
1373 dumpPreservedSet(MP);
1375 verifyPreservedAnalysis(MP);
1376 removeNotPreservedAnalysis(MP);
1377 recordAvailableAnalysis(MP);
1378 removeDeadPasses(MP, M.getModuleIdentifier().c_str(), ON_MODULE_MSG);
1383 /// Add RequiredPass into list of lower level passes required by pass P.
1384 /// RequiredPass is run on the fly by Pass Manager when P requests it
1385 /// through getAnalysis interface.
1386 void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1388 assert (P->getPotentialPassManagerType() == PMT_ModulePassManager
1389 && "Unable to handle Pass that requires lower level Analysis pass");
1390 assert ((P->getPotentialPassManagerType() <
1391 RequiredPass->getPotentialPassManagerType())
1392 && "Unable to handle Pass that requires lower level Analysis pass");
1394 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
1396 FPP = new FunctionPassManagerImpl(0);
1397 // FPP is the top level manager.
1398 FPP->setTopLevelManager(FPP);
1400 OnTheFlyManagers[P] = FPP;
1402 FPP->add(RequiredPass);
1404 // Register P as the last user of RequiredPass.
1405 SmallVector<Pass *, 12> LU;
1406 LU.push_back(RequiredPass);
1407 FPP->setLastUser(LU, P);
1410 /// Return function pass corresponding to PassInfo PI, that is
1411 /// required by module pass MP. Instantiate analysis pass, by using
1412 /// its runOnFunction() for function F.
1413 Pass* MPPassManager::getOnTheFlyPass(Pass *MP, const PassInfo *PI,
1415 AnalysisID AID = PI;
1416 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
1417 assert (FPP && "Unable to find on the fly pass");
1420 return (dynamic_cast<PMTopLevelManager *>(FPP))->findAnalysisPass(AID);
1424 //===----------------------------------------------------------------------===//
1425 // PassManagerImpl implementation
1427 /// run - Execute all of the passes scheduled for execution. Keep track of
1428 /// whether any of the passes modifies the module, and if so, return true.
1429 bool PassManagerImpl::run(Module &M) {
1431 bool Changed = false;
1433 TimingInfo::createTheTimeInfo();
1438 initializeAllAnalysisInfo();
1439 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1440 MPPassManager *MP = getContainedManager(Index);
1441 Changed |= MP->runOnModule(M);
1446 //===----------------------------------------------------------------------===//
1447 // PassManager implementation
1449 /// Create new pass manager
1450 PassManager::PassManager() {
1451 PM = new PassManagerImpl(0);
1452 // PM is the top level manager
1453 PM->setTopLevelManager(PM);
1456 PassManager::~PassManager() {
1460 /// add - Add a pass to the queue of passes to run. This passes ownership of
1461 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
1462 /// will be destroyed as well, so there is no need to delete the pass. This
1463 /// implies that all passes MUST be allocated with 'new'.
1465 PassManager::add(Pass *P) {
1469 /// run - Execute all of the passes scheduled for execution. Keep track of
1470 /// whether any of the passes modifies the module, and if so, return true.
1472 PassManager::run(Module &M) {
1476 //===----------------------------------------------------------------------===//
1477 // TimingInfo Class - This class is used to calculate information about the
1478 // amount of time each pass takes to execute. This only happens with
1479 // -time-passes is enabled on the command line.
1481 bool llvm::TimePassesIsEnabled = false;
1482 static cl::opt<bool,true>
1483 EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1484 cl::desc("Time each pass, printing elapsed time for each on exit"));
1486 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1487 // a non null value (if the -time-passes option is enabled) or it leaves it
1488 // null. It may be called multiple times.
1489 void TimingInfo::createTheTimeInfo() {
1490 if (!TimePassesIsEnabled || TheTimeInfo) return;
1492 // Constructed the first time this is called, iff -time-passes is enabled.
1493 // This guarantees that the object will be constructed before static globals,
1494 // thus it will be destroyed before them.
1495 static ManagedStatic<TimingInfo> TTI;
1496 TheTimeInfo = &*TTI;
1499 /// If TimingInfo is enabled then start pass timer.
1500 void StartPassTimer(Pass *P) {
1502 TheTimeInfo->passStarted(P);
1505 /// If TimingInfo is enabled then stop pass timer.
1506 void StopPassTimer(Pass *P) {
1508 TheTimeInfo->passEnded(P);
1511 //===----------------------------------------------------------------------===//
1512 // PMStack implementation
1515 // Pop Pass Manager from the stack and clear its analysis info.
1516 void PMStack::pop() {
1518 PMDataManager *Top = this->top();
1519 Top->initializeAnalysisInfo();
1524 // Push PM on the stack and set its top level manager.
1525 void PMStack::push(PMDataManager *PM) {
1527 PMDataManager *Top = NULL;
1528 assert (PM && "Unable to push. Pass Manager expected");
1530 if (this->empty()) {
1535 PMTopLevelManager *TPM = Top->getTopLevelManager();
1537 assert (TPM && "Unable to find top level manager");
1538 TPM->addIndirectPassManager(PM);
1539 PM->setTopLevelManager(TPM);
1545 // Dump content of the pass manager stack.
1546 void PMStack::dump() {
1547 for(std::deque<PMDataManager *>::iterator I = S.begin(),
1548 E = S.end(); I != E; ++I) {
1549 Pass *P = dynamic_cast<Pass *>(*I);
1550 printf("%s ", P->getPassName());
1556 /// Find appropriate Module Pass Manager in the PM Stack and
1557 /// add self into that manager.
1558 void ModulePass::assignPassManager(PMStack &PMS,
1559 PassManagerType PreferredType) {
1561 // Find Module Pass Manager
1562 while(!PMS.empty()) {
1563 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1564 if (TopPMType == PreferredType)
1565 break; // We found desired pass manager
1566 else if (TopPMType > PMT_ModulePassManager)
1567 PMS.pop(); // Pop children pass managers
1572 PMS.top()->add(this);
1575 /// Find appropriate Function Pass Manager or Call Graph Pass Manager
1576 /// in the PM Stack and add self into that manager.
1577 void FunctionPass::assignPassManager(PMStack &PMS,
1578 PassManagerType PreferredType) {
1580 // Find Module Pass Manager (TODO : Or Call Graph Pass Manager)
1581 while(!PMS.empty()) {
1582 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1587 FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top());
1589 // Create new Function Pass Manager
1591 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1592 PMDataManager *PMD = PMS.top();
1594 // [1] Create new Function Pass Manager
1595 FPP = new FPPassManager(PMD->getDepth() + 1);
1596 FPP->populateInheritedAnalysis(PMS);
1598 // [2] Set up new manager's top level manager
1599 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1600 TPM->addIndirectPassManager(FPP);
1602 // [3] Assign manager to manage this new manager. This may create
1603 // and push new managers into PMS
1605 // If Call Graph Pass Manager is active then use it to manage
1606 // this new Function Pass manager.
1607 if (PMD->getPassManagerType() == PMT_CallGraphPassManager)
1608 FPP->assignPassManager(PMS, PMT_CallGraphPassManager);
1610 FPP->assignPassManager(PMS);
1612 // [4] Push new manager into PMS
1616 // Assign FPP as the manager of this pass.
1620 /// Find appropriate Basic Pass Manager or Call Graph Pass Manager
1621 /// in the PM Stack and add self into that manager.
1622 void BasicBlockPass::assignPassManager(PMStack &PMS,
1623 PassManagerType PreferredType) {
1625 BBPassManager *BBP = NULL;
1627 // Basic Pass Manager is a leaf pass manager. It does not handle
1628 // any other pass manager.
1630 BBP = dynamic_cast<BBPassManager *>(PMS.top());
1632 // If leaf manager is not Basic Block Pass manager then create new
1633 // basic Block Pass manager.
1636 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1637 PMDataManager *PMD = PMS.top();
1639 // [1] Create new Basic Block Manager
1640 BBP = new BBPassManager(PMD->getDepth() + 1);
1642 // [2] Set up new manager's top level manager
1643 // Basic Block Pass Manager does not live by itself
1644 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1645 TPM->addIndirectPassManager(BBP);
1647 // [3] Assign manager to manage this new manager. This may create
1648 // and push new managers into PMS
1649 BBP->assignPassManager(PMS);
1651 // [4] Push new manager into PMS
1655 // Assign BBP as the manager of this pass.
1659 PassManagerBase::~PassManagerBase() {}
1661 /*===-- C Bindings --------------------------------------------------------===*/
1663 LLVMPassManagerRef LLVMCreatePassManager() {
1664 return wrap(new PassManager());
1667 LLVMPassManagerRef LLVMCreateFunctionPassManager(LLVMModuleProviderRef P) {
1668 return wrap(new FunctionPassManager(unwrap(P)));
1671 int LLVMRunPassManager(LLVMPassManagerRef PM, LLVMModuleRef M) {
1672 return unwrap<PassManager>(PM)->run(*unwrap(M));
1675 int LLVMInitializeFunctionPassManager(LLVMPassManagerRef FPM) {
1676 return unwrap<FunctionPassManager>(FPM)->doInitialization();
1679 int LLVMRunFunctionPassManager(LLVMPassManagerRef FPM, LLVMValueRef F) {
1680 return unwrap<FunctionPassManager>(FPM)->run(*unwrap<Function>(F));
1683 int LLVMFinalizeFunctionPassManager(LLVMPassManagerRef FPM) {
1684 return unwrap<FunctionPassManager>(FPM)->doFinalization();
1687 void LLVMDisposePassManager(LLVMPassManagerRef PM) {