1 //===- PassManager.cpp - LLVM Pass Infrastructure Implementation ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Devang Patel and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the LLVM Pass Manager infrastructure.
12 //===----------------------------------------------------------------------===//
15 #include "llvm/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"
25 // See PassManagers.h for Pass Manager infrastructure overview.
29 //===----------------------------------------------------------------------===//
30 // Pass debugging information. Often it is useful to find out what pass is
31 // running when a crash occurs in a utility. When this library is compiled with
32 // debugging on, a command line option (--debug-pass) is enabled that causes the
33 // pass name to be printed before it executes.
36 // Different debug levels that can be enabled...
38 None, Arguments, Structure, Executions, Details
41 static cl::opt<enum PassDebugLevel>
42 PassDebugging("debug-pass", cl::Hidden,
43 cl::desc("Print PassManager debugging information"),
45 clEnumVal(None , "disable debug output"),
46 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
47 clEnumVal(Structure , "print pass structure before run()"),
48 clEnumVal(Executions, "print pass name before it is executed"),
49 clEnumVal(Details , "print pass details when it is executed"),
51 } // End of llvm namespace
55 //===----------------------------------------------------------------------===//
58 /// BBPassManager manages BasicBlockPass. It batches all the
59 /// pass together and sequence them to process one basic block before
60 /// processing next basic block.
61 class VISIBILITY_HIDDEN BBPassManager : public PMDataManager,
65 BBPassManager(int Depth) : PMDataManager(Depth) { }
67 /// Execute all of the passes scheduled for execution. Keep track of
68 /// whether any of the passes modifies the function, and if so, return true.
69 bool runOnFunction(Function &F);
71 /// Pass Manager itself does not invalidate any analysis info.
72 void getAnalysisUsage(AnalysisUsage &Info) const {
73 Info.setPreservesAll();
76 bool doInitialization(Module &M);
77 bool doInitialization(Function &F);
78 bool doFinalization(Module &M);
79 bool doFinalization(Function &F);
81 virtual const char *getPassName() const {
82 return "BasicBlock Pass Manager";
85 // Print passes managed by this manager
86 void dumpPassStructure(unsigned Offset) {
87 llvm::cerr << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n";
88 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
89 BasicBlockPass *BP = getContainedPass(Index);
90 BP->dumpPassStructure(Offset + 1);
91 dumpLastUses(BP, Offset+1);
95 BasicBlockPass *getContainedPass(unsigned N) {
96 assert ( N < PassVector.size() && "Pass number out of range!");
97 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
101 virtual PassManagerType getPassManagerType() {
102 return PMT_BasicBlockPassManager;
110 //===----------------------------------------------------------------------===//
111 // FunctionPassManagerImpl
113 /// FunctionPassManagerImpl manages FPPassManagers
114 class FunctionPassManagerImpl : public Pass,
115 public PMDataManager,
116 public PMTopLevelManager {
119 FunctionPassManagerImpl(int Depth) : PMDataManager(Depth),
120 PMTopLevelManager(TLM_Function) { }
122 /// add - Add a pass to the queue of passes to run. This passes ownership of
123 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
124 /// will be destroyed as well, so there is no need to delete the pass. This
125 /// implies that all passes MUST be allocated with 'new'.
130 /// run - Execute all of the passes scheduled for execution. Keep track of
131 /// whether any of the passes modifies the module, and if so, return true.
132 bool run(Function &F);
134 /// doInitialization - Run all of the initializers for the function passes.
136 bool doInitialization(Module &M);
138 /// doFinalization - Run all of the initializers for the function passes.
140 bool doFinalization(Module &M);
142 /// Pass Manager itself does not invalidate any analysis info.
143 void getAnalysisUsage(AnalysisUsage &Info) const {
144 Info.setPreservesAll();
147 inline void addTopLevelPass(Pass *P) {
149 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
151 // P is a immutable pass and it will be managed by this
152 // top level manager. Set up analysis resolver to connect them.
153 AnalysisResolver *AR = new AnalysisResolver(*this);
155 initializeAnalysisImpl(P);
156 addImmutablePass(IP);
157 recordAvailableAnalysis(IP);
159 P->assignPassManager(activeStack);
164 FPPassManager *getContainedManager(unsigned N) {
165 assert ( N < PassManagers.size() && "Pass number out of range!");
166 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
172 //===----------------------------------------------------------------------===//
175 /// MPPassManager manages ModulePasses and function pass managers.
176 /// It batches all Module passes passes and function pass managers together and
177 /// sequence them to process one module.
178 class MPPassManager : public Pass, public PMDataManager {
181 MPPassManager(int Depth) : PMDataManager(Depth) { }
183 /// run - Execute all of the passes scheduled for execution. Keep track of
184 /// whether any of the passes modifies the module, and if so, return true.
185 bool runOnModule(Module &M);
187 /// Pass Manager itself does not invalidate any analysis info.
188 void getAnalysisUsage(AnalysisUsage &Info) const {
189 Info.setPreservesAll();
192 virtual const char *getPassName() const {
193 return "Module Pass Manager";
196 // Print passes managed by this manager
197 void dumpPassStructure(unsigned Offset) {
198 llvm::cerr << std::string(Offset*2, ' ') << "ModulePass Manager\n";
199 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
200 ModulePass *MP = getContainedPass(Index);
201 MP->dumpPassStructure(Offset + 1);
202 dumpLastUses(MP, Offset+1);
206 ModulePass *getContainedPass(unsigned N) {
207 assert ( N < PassVector.size() && "Pass number out of range!");
208 ModulePass *MP = static_cast<ModulePass *>(PassVector[N]);
212 virtual PassManagerType getPassManagerType() { return PMT_ModulePassManager; }
215 //===----------------------------------------------------------------------===//
218 /// PassManagerImpl manages MPPassManagers
219 class PassManagerImpl : public Pass,
220 public PMDataManager,
221 public PMTopLevelManager {
225 PassManagerImpl(int Depth) : PMDataManager(Depth),
226 PMTopLevelManager(TLM_Pass) { }
228 /// add - Add a pass to the queue of passes to run. This passes ownership of
229 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
230 /// will be destroyed as well, so there is no need to delete the pass. This
231 /// implies that all passes MUST be allocated with 'new'.
236 /// run - Execute all of the passes scheduled for execution. Keep track of
237 /// whether any of the passes modifies the module, and if so, return true.
240 /// Pass Manager itself does not invalidate any analysis info.
241 void getAnalysisUsage(AnalysisUsage &Info) const {
242 Info.setPreservesAll();
245 inline void addTopLevelPass(Pass *P) {
247 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) {
249 // P is a immutable pass and it will be managed by this
250 // top level manager. Set up analysis resolver to connect them.
251 AnalysisResolver *AR = new AnalysisResolver(*this);
253 initializeAnalysisImpl(P);
254 addImmutablePass(IP);
255 recordAvailableAnalysis(IP);
257 P->assignPassManager(activeStack);
262 MPPassManager *getContainedManager(unsigned N) {
263 assert ( N < PassManagers.size() && "Pass number out of range!");
264 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
270 } // End of llvm namespace
274 //===----------------------------------------------------------------------===//
275 // TimingInfo Class - This class is used to calculate information about the
276 // amount of time each pass takes to execute. This only happens when
277 // -time-passes is enabled on the command line.
280 class VISIBILITY_HIDDEN TimingInfo {
281 std::map<Pass*, Timer> TimingData;
285 // Use 'create' member to get this.
286 TimingInfo() : TG("... Pass execution timing report ...") {}
288 // TimingDtor - Print out information about timing information
290 // Delete all of the timers...
292 // TimerGroup is deleted next, printing the report.
295 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
296 // to a non null value (if the -time-passes option is enabled) or it leaves it
297 // null. It may be called multiple times.
298 static void createTheTimeInfo();
300 void passStarted(Pass *P) {
302 if (dynamic_cast<PMDataManager *>(P))
305 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
306 if (I == TimingData.end())
307 I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
308 I->second.startTimer();
310 void passEnded(Pass *P) {
312 if (dynamic_cast<PMDataManager *>(P))
315 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
316 assert (I != TimingData.end() && "passStarted/passEnded not nested right!");
317 I->second.stopTimer();
321 static TimingInfo *TheTimeInfo;
323 } // End of anon namespace
325 //===----------------------------------------------------------------------===//
326 // PMTopLevelManager implementation
328 /// Initialize top level manager. Create first pass manager.
329 PMTopLevelManager::PMTopLevelManager (enum TopLevelManagerType t) {
332 MPPassManager *MPP = new MPPassManager(1);
333 MPP->setTopLevelManager(this);
335 activeStack.push(MPP);
337 else if (t == TLM_Function) {
338 FPPassManager *FPP = new FPPassManager(1);
339 FPP->setTopLevelManager(this);
341 activeStack.push(FPP);
345 /// Set pass P as the last user of the given analysis passes.
346 void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses,
349 for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(),
350 E = AnalysisPasses.end(); I != E; ++I) {
353 // If AP is the last user of other passes then make P last user of
355 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
356 LUE = LastUser.end(); LUI != LUE; ++LUI) {
357 if (LUI->second == AP)
358 LastUser[LUI->first] = P;
363 /// Collect passes whose last user is P
364 void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses,
366 for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
367 LUE = LastUser.end(); LUI != LUE; ++LUI)
368 if (LUI->second == P)
369 LastUses.push_back(LUI->first);
372 /// Schedule pass P for execution. Make sure that passes required by
373 /// P are run before P is run. Update analysis info maintained by
374 /// the manager. Remove dead passes. This is a recursive function.
375 void PMTopLevelManager::schedulePass(Pass *P) {
377 // TODO : Allocate function manager for this pass, other wise required set
378 // may be inserted into previous function manager
380 AnalysisUsage AnUsage;
381 P->getAnalysisUsage(AnUsage);
382 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
383 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
384 E = RequiredSet.end(); I != E; ++I) {
386 Pass *AnalysisPass = findAnalysisPass(*I);
388 // Schedule this analysis run first.
389 AnalysisPass = (*I)->createPass();
390 schedulePass(AnalysisPass);
394 // Now all required passes are available.
398 /// Find the pass that implements Analysis AID. Search immutable
399 /// passes and all pass managers. If desired pass is not found
400 /// then return NULL.
401 Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
404 // Check pass managers
405 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
406 E = PassManagers.end(); P == NULL && I != E; ++I) {
407 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
408 assert(PMD && "This is not a PassManager");
409 P = PMD->findAnalysisPass(AID, false);
412 // Check other pass managers
413 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
414 E = IndirectPassManagers.end(); P == NULL && I != E; ++I)
415 P = (*I)->findAnalysisPass(AID, false);
417 for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(),
418 E = ImmutablePasses.end(); P == NULL && I != E; ++I) {
419 const PassInfo *PI = (*I)->getPassInfo();
423 // If Pass not found then check the interfaces implemented by Immutable Pass
425 const std::vector<const PassInfo*> &ImmPI = PI->getInterfacesImplemented();
426 if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end())
434 // Print passes managed by this top level manager.
435 void PMTopLevelManager::dumpPasses() const {
437 if (PassDebugging < Structure)
440 // Print out the immutable passes
441 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
442 ImmutablePasses[i]->dumpPassStructure(0);
445 for (std::vector<Pass *>::const_iterator I = PassManagers.begin(),
446 E = PassManagers.end(); I != E; ++I)
447 (*I)->dumpPassStructure(1);
450 void PMTopLevelManager::dumpArguments() const {
452 if (PassDebugging < Arguments)
455 cerr << "Pass Arguments: ";
456 for (std::vector<Pass *>::const_iterator I = PassManagers.begin(),
457 E = PassManagers.end(); I != E; ++I) {
458 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
459 assert(PMD && "This is not a PassManager");
460 PMD->dumpPassArguments();
465 void PMTopLevelManager::initializeAllAnalysisInfo() {
467 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
468 E = PassManagers.end(); I != E; ++I) {
469 PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I);
470 assert(PMD && "This is not a PassManager");
471 PMD->initializeAnalysisInfo();
474 // Initailize other pass managers
475 for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
476 E = IndirectPassManagers.end(); I != E; ++I)
477 (*I)->initializeAnalysisInfo();
481 PMTopLevelManager::~PMTopLevelManager() {
482 for (std::vector<Pass *>::iterator I = PassManagers.begin(),
483 E = PassManagers.end(); I != E; ++I)
486 for (std::vector<ImmutablePass *>::iterator
487 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
490 PassManagers.clear();
493 //===----------------------------------------------------------------------===//
494 // PMDataManager implementation
496 /// Return true IFF pass P's required analysis set does not required new
498 bool PMDataManager::manageablePass(Pass *P) {
501 // If this pass is not preserving information that is required by a
502 // pass maintained by higher level pass manager then do not insert
503 // this pass into current manager. Use new manager. For example,
504 // For example, If FunctionPass F is not preserving ModulePass Info M1
505 // that is used by another ModulePass M2 then do not insert F in
506 // current function pass manager.
510 /// Augement AvailableAnalysis by adding analysis made available by pass P.
511 void PMDataManager::recordAvailableAnalysis(Pass *P) {
513 if (const PassInfo *PI = P->getPassInfo()) {
514 AvailableAnalysis[PI] = P;
516 //This pass is the current implementation of all of the interfaces it
517 //implements as well.
518 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
519 for (unsigned i = 0, e = II.size(); i != e; ++i)
520 AvailableAnalysis[II[i]] = P;
524 /// Remove Analyss not preserved by Pass P
525 void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
526 AnalysisUsage AnUsage;
527 P->getAnalysisUsage(AnUsage);
529 if (AnUsage.getPreservesAll())
532 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
533 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
534 E = AvailableAnalysis.end(); I != E; ) {
535 std::map<AnalysisID, Pass*>::iterator Info = I++;
536 if (std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
537 PreservedSet.end()) {
538 // Remove this analysis
539 if (!dynamic_cast<ImmutablePass*>(Info->second))
540 AvailableAnalysis.erase(Info);
545 /// Remove analysis passes that are not used any longer
546 void PMDataManager::removeDeadPasses(Pass *P, std::string &Msg) {
548 std::vector<Pass *> DeadPasses;
549 TPM->collectLastUses(DeadPasses, P);
551 for (std::vector<Pass *>::iterator I = DeadPasses.begin(),
552 E = DeadPasses.end(); I != E; ++I) {
554 std::string Msg1 = " Freeing Pass '";
555 dumpPassInfo(*I, Msg1, Msg);
557 if (TheTimeInfo) TheTimeInfo->passStarted(P);
558 (*I)->releaseMemory();
559 if (TheTimeInfo) TheTimeInfo->passEnded(P);
561 std::map<AnalysisID, Pass*>::iterator Pos =
562 AvailableAnalysis.find((*I)->getPassInfo());
564 // It is possible that pass is already removed from the AvailableAnalysis
565 if (Pos != AvailableAnalysis.end())
566 AvailableAnalysis.erase(Pos);
570 /// Add pass P into the PassVector. Update
571 /// AvailableAnalysis appropriately if ProcessAnalysis is true.
572 void PMDataManager::add(Pass *P,
573 bool ProcessAnalysis) {
575 // This manager is going to manage pass P. Set up analysis resolver
577 AnalysisResolver *AR = new AnalysisResolver(*this);
580 if (ProcessAnalysis) {
582 // At the moment, this pass is the last user of all required passes.
583 std::vector<Pass *> LastUses;
584 std::vector<Pass *> RequiredPasses;
585 unsigned PDepth = this->getDepth();
587 collectRequiredAnalysisPasses(RequiredPasses, P);
588 for (std::vector<Pass *>::iterator I = RequiredPasses.begin(),
589 E = RequiredPasses.end(); I != E; ++I) {
590 Pass *PRequired = *I;
593 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
594 RDepth = DM.getDepth();
596 if (PDepth == RDepth)
597 LastUses.push_back(PRequired);
598 else if (PDepth > RDepth) {
599 // Let the parent claim responsibility of last use
600 TransferLastUses.push_back(PRequired);
602 // Note : This feature is not yet implemented
604 "Unable to handle Pass that requires lower level Analysis pass");
608 // Set P as P's last user until someone starts using P.
609 // However, if P is a Pass Manager then it does not need
610 // to record its last user.
611 if (!dynamic_cast<PMDataManager *>(P))
612 LastUses.push_back(P);
613 TPM->setLastUser(LastUses, P);
615 // Take a note of analysis required and made available by this pass.
616 // Remove the analysis not preserved by this pass
617 removeNotPreservedAnalysis(P);
618 recordAvailableAnalysis(P);
622 PassVector.push_back(P);
625 /// Populate RequiredPasses with the analysis pass that are required by
627 void PMDataManager::collectRequiredAnalysisPasses(std::vector<Pass *> &RP,
629 AnalysisUsage AnUsage;
630 P->getAnalysisUsage(AnUsage);
631 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
632 for (std::vector<AnalysisID>::const_iterator
633 I = RequiredSet.begin(), E = RequiredSet.end();
635 Pass *AnalysisPass = findAnalysisPass(*I, true);
636 assert (AnalysisPass && "Analysis pass is not available");
637 RP.push_back(AnalysisPass);
640 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
641 for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
642 E = IDs.end(); I != E; ++I) {
643 Pass *AnalysisPass = findAnalysisPass(*I, true);
644 assert (AnalysisPass && "Analysis pass is not available");
645 RP.push_back(AnalysisPass);
649 // All Required analyses should be available to the pass as it runs! Here
650 // we fill in the AnalysisImpls member of the pass so that it can
651 // successfully use the getAnalysis() method to retrieve the
652 // implementations it needs.
654 void PMDataManager::initializeAnalysisImpl(Pass *P) {
655 AnalysisUsage AnUsage;
656 P->getAnalysisUsage(AnUsage);
658 for (std::vector<const PassInfo *>::const_iterator
659 I = AnUsage.getRequiredSet().begin(),
660 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
661 Pass *Impl = findAnalysisPass(*I, true);
663 assert(0 && "Analysis used but not available!");
664 AnalysisResolver *AR = P->getResolver();
665 AR->addAnalysisImplsPair(*I, Impl);
669 /// Find the pass that implements Analysis AID. If desired pass is not found
670 /// then return NULL.
671 Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
673 // Check if AvailableAnalysis map has one entry.
674 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
676 if (I != AvailableAnalysis.end())
679 // Search Parents through TopLevelManager
681 return TPM->findAnalysisPass(AID);
686 // Print list of passes that are last used by P.
687 void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
689 std::vector<Pass *> LUses;
691 assert (TPM && "Top Level Manager is missing");
692 TPM->collectLastUses(LUses, P);
694 for (std::vector<Pass *>::iterator I = LUses.begin(),
695 E = LUses.end(); I != E; ++I) {
696 llvm::cerr << "--" << std::string(Offset*2, ' ');
697 (*I)->dumpPassStructure(0);
701 void PMDataManager::dumpPassArguments() const {
702 for(std::vector<Pass *>::const_iterator I = PassVector.begin(),
703 E = PassVector.end(); I != E; ++I) {
704 if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I))
705 PMD->dumpPassArguments();
707 if (const PassInfo *PI = (*I)->getPassInfo())
708 if (!PI->isAnalysisGroup())
709 cerr << " -" << PI->getPassArgument();
713 void PMDataManager:: dumpPassInfo(Pass *P, std::string &Msg1,
714 std::string &Msg2) const {
715 if (PassDebugging < Executions)
717 cerr << (void*)this << std::string(getDepth()*2+1, ' ');
719 cerr << P->getPassName();
723 void PMDataManager::dumpAnalysisSetInfo(const char *Msg, Pass *P,
724 const std::vector<AnalysisID> &Set)
726 if (PassDebugging >= Details && !Set.empty()) {
727 cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
728 for (unsigned i = 0; i != Set.size(); ++i) {
730 cerr << " " << Set[i]->getPassName();
737 PMDataManager::~PMDataManager() {
739 for (std::vector<Pass *>::iterator I = PassVector.begin(),
740 E = PassVector.end(); I != E; ++I)
746 //===----------------------------------------------------------------------===//
747 // NOTE: Is this the right place to define this method ?
748 // getAnalysisToUpdate - Return an analysis result or null if it doesn't exist
749 Pass *AnalysisResolver::getAnalysisToUpdate(AnalysisID ID, bool dir) const {
750 return PM.findAnalysisPass(ID, dir);
753 //===----------------------------------------------------------------------===//
754 // BBPassManager implementation
756 /// Execute all of the passes scheduled for execution by invoking
757 /// runOnBasicBlock method. Keep track of whether any of the passes modifies
758 /// the function, and if so, return true.
760 BBPassManager::runOnFunction(Function &F) {
762 if (F.isDeclaration())
765 bool Changed = doInitialization(F);
767 std::string Msg1 = "Executing Pass '";
768 std::string Msg3 = "' Made Modification '";
770 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
771 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
772 BasicBlockPass *BP = getContainedPass(Index);
773 AnalysisUsage AnUsage;
774 BP->getAnalysisUsage(AnUsage);
776 std::string Msg2 = "' on BasicBlock '" + (*I).getName() + "'...\n";
777 dumpPassInfo(BP, Msg1, Msg2);
778 dumpAnalysisSetInfo("Required", BP, AnUsage.getRequiredSet());
780 initializeAnalysisImpl(BP);
782 if (TheTimeInfo) TheTimeInfo->passStarted(BP);
783 Changed |= BP->runOnBasicBlock(*I);
784 if (TheTimeInfo) TheTimeInfo->passEnded(BP);
787 dumpPassInfo(BP, Msg3, Msg2);
788 dumpAnalysisSetInfo("Preserved", BP, AnUsage.getPreservedSet());
790 removeNotPreservedAnalysis(BP);
791 recordAvailableAnalysis(BP);
792 removeDeadPasses(BP, Msg2);
794 return Changed |= doFinalization(F);
797 // Implement doInitialization and doFinalization
798 inline bool BBPassManager::doInitialization(Module &M) {
799 bool Changed = false;
801 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
802 BasicBlockPass *BP = getContainedPass(Index);
803 Changed |= BP->doInitialization(M);
809 inline bool BBPassManager::doFinalization(Module &M) {
810 bool Changed = false;
812 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
813 BasicBlockPass *BP = getContainedPass(Index);
814 Changed |= BP->doFinalization(M);
820 inline bool BBPassManager::doInitialization(Function &F) {
821 bool Changed = false;
823 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
824 BasicBlockPass *BP = getContainedPass(Index);
825 Changed |= BP->doInitialization(F);
831 inline bool BBPassManager::doFinalization(Function &F) {
832 bool Changed = false;
834 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
835 BasicBlockPass *BP = getContainedPass(Index);
836 Changed |= BP->doFinalization(F);
843 //===----------------------------------------------------------------------===//
844 // FunctionPassManager implementation
846 /// Create new Function pass manager
847 FunctionPassManager::FunctionPassManager(ModuleProvider *P) {
848 FPM = new FunctionPassManagerImpl(0);
849 // FPM is the top level manager.
850 FPM->setTopLevelManager(FPM);
852 PMDataManager *PMD = dynamic_cast<PMDataManager *>(FPM);
853 AnalysisResolver *AR = new AnalysisResolver(*PMD);
854 FPM->setResolver(AR);
859 FunctionPassManager::~FunctionPassManager() {
863 /// add - Add a pass to the queue of passes to run. This passes
864 /// ownership of the Pass to the PassManager. When the
865 /// PassManager_X is destroyed, the pass will be destroyed as well, so
866 /// there is no need to delete the pass. (TODO delete passes.)
867 /// This implies that all passes MUST be allocated with 'new'.
868 void FunctionPassManager::add(Pass *P) {
872 /// run - Execute all of the passes scheduled for execution. Keep
873 /// track of whether any of the passes modifies the function, and if
876 bool FunctionPassManager::run(Function &F) {
878 if (MP->materializeFunction(&F, &errstr)) {
879 cerr << "Error reading bytecode file: " << errstr << "\n";
886 /// doInitialization - Run all of the initializers for the function passes.
888 bool FunctionPassManager::doInitialization() {
889 return FPM->doInitialization(*MP->getModule());
892 /// doFinalization - Run all of the initializers for the function passes.
894 bool FunctionPassManager::doFinalization() {
895 return FPM->doFinalization(*MP->getModule());
898 //===----------------------------------------------------------------------===//
899 // FunctionPassManagerImpl implementation
901 inline bool FunctionPassManagerImpl::doInitialization(Module &M) {
902 bool Changed = false;
904 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
905 FPPassManager *FP = getContainedManager(Index);
906 Changed |= FP->doInitialization(M);
912 inline bool FunctionPassManagerImpl::doFinalization(Module &M) {
913 bool Changed = false;
915 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
916 FPPassManager *FP = getContainedManager(Index);
917 Changed |= FP->doFinalization(M);
923 // Execute all the passes managed by this top level manager.
924 // Return true if any function is modified by a pass.
925 bool FunctionPassManagerImpl::run(Function &F) {
927 bool Changed = false;
929 TimingInfo::createTheTimeInfo();
934 initializeAllAnalysisInfo();
935 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
936 FPPassManager *FP = getContainedManager(Index);
937 Changed |= FP->runOnFunction(F);
942 //===----------------------------------------------------------------------===//
943 // FPPassManager implementation
945 /// Print passes managed by this manager
946 void FPPassManager::dumpPassStructure(unsigned Offset) {
947 llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n";
948 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
949 FunctionPass *FP = getContainedPass(Index);
950 FP->dumpPassStructure(Offset + 1);
951 dumpLastUses(FP, Offset+1);
956 /// Execute all of the passes scheduled for execution by invoking
957 /// runOnFunction method. Keep track of whether any of the passes modifies
958 /// the function, and if so, return true.
959 bool FPPassManager::runOnFunction(Function &F) {
961 bool Changed = false;
963 if (F.isDeclaration())
966 std::string Msg1 = "Executing Pass '";
967 std::string Msg3 = "' Made Modification '";
969 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
970 FunctionPass *FP = getContainedPass(Index);
972 AnalysisUsage AnUsage;
973 FP->getAnalysisUsage(AnUsage);
975 std::string Msg2 = "' on Function '" + F.getName() + "'...\n";
976 dumpPassInfo(FP, Msg1, Msg2);
977 dumpAnalysisSetInfo("Required", FP, AnUsage.getRequiredSet());
979 initializeAnalysisImpl(FP);
981 if (TheTimeInfo) TheTimeInfo->passStarted(FP);
982 Changed |= FP->runOnFunction(F);
983 if (TheTimeInfo) TheTimeInfo->passEnded(FP);
986 dumpPassInfo(FP, Msg3, Msg2);
987 dumpAnalysisSetInfo("Preserved", FP, AnUsage.getPreservedSet());
989 removeNotPreservedAnalysis(FP);
990 recordAvailableAnalysis(FP);
991 removeDeadPasses(FP, Msg2);
996 bool FPPassManager::runOnModule(Module &M) {
998 bool Changed = doInitialization(M);
1000 for(Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1001 this->runOnFunction(*I);
1003 return Changed |= doFinalization(M);
1006 inline bool FPPassManager::doInitialization(Module &M) {
1007 bool Changed = false;
1009 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1010 FunctionPass *FP = getContainedPass(Index);
1011 Changed |= FP->doInitialization(M);
1017 inline bool FPPassManager::doFinalization(Module &M) {
1018 bool Changed = false;
1020 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1021 FunctionPass *FP = getContainedPass(Index);
1022 Changed |= FP->doFinalization(M);
1028 //===----------------------------------------------------------------------===//
1029 // MPPassManager implementation
1031 /// Execute all of the passes scheduled for execution by invoking
1032 /// runOnModule method. Keep track of whether any of the passes modifies
1033 /// the module, and if so, return true.
1035 MPPassManager::runOnModule(Module &M) {
1036 bool Changed = false;
1038 std::string Msg1 = "Executing Pass '";
1039 std::string Msg3 = "' Made Modification '";
1041 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1042 ModulePass *MP = getContainedPass(Index);
1044 AnalysisUsage AnUsage;
1045 MP->getAnalysisUsage(AnUsage);
1047 std::string Msg2 = "' on Module '" + M.getModuleIdentifier() + "'...\n";
1048 dumpPassInfo(MP, Msg1, Msg2);
1049 dumpAnalysisSetInfo("Required", MP, AnUsage.getRequiredSet());
1051 initializeAnalysisImpl(MP);
1053 if (TheTimeInfo) TheTimeInfo->passStarted(MP);
1054 Changed |= MP->runOnModule(M);
1055 if (TheTimeInfo) TheTimeInfo->passEnded(MP);
1058 dumpPassInfo(MP, Msg3, Msg2);
1059 dumpAnalysisSetInfo("Preserved", MP, AnUsage.getPreservedSet());
1061 removeNotPreservedAnalysis(MP);
1062 recordAvailableAnalysis(MP);
1063 removeDeadPasses(MP, Msg2);
1068 //===----------------------------------------------------------------------===//
1069 // PassManagerImpl implementation
1071 /// run - Execute all of the passes scheduled for execution. Keep track of
1072 /// whether any of the passes modifies the module, and if so, return true.
1073 bool PassManagerImpl::run(Module &M) {
1075 bool Changed = false;
1077 TimingInfo::createTheTimeInfo();
1082 initializeAllAnalysisInfo();
1083 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1084 MPPassManager *MP = getContainedManager(Index);
1085 Changed |= MP->runOnModule(M);
1090 //===----------------------------------------------------------------------===//
1091 // PassManager implementation
1093 /// Create new pass manager
1094 PassManager::PassManager() {
1095 PM = new PassManagerImpl(0);
1096 // PM is the top level manager
1097 PM->setTopLevelManager(PM);
1100 PassManager::~PassManager() {
1104 /// add - Add a pass to the queue of passes to run. This passes ownership of
1105 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
1106 /// will be destroyed as well, so there is no need to delete the pass. This
1107 /// implies that all passes MUST be allocated with 'new'.
1109 PassManager::add(Pass *P) {
1113 /// run - Execute all of the passes scheduled for execution. Keep track of
1114 /// whether any of the passes modifies the module, and if so, return true.
1116 PassManager::run(Module &M) {
1120 //===----------------------------------------------------------------------===//
1121 // TimingInfo Class - This class is used to calculate information about the
1122 // amount of time each pass takes to execute. This only happens with
1123 // -time-passes is enabled on the command line.
1125 bool llvm::TimePassesIsEnabled = false;
1126 static cl::opt<bool,true>
1127 EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1128 cl::desc("Time each pass, printing elapsed time for each on exit"));
1130 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1131 // a non null value (if the -time-passes option is enabled) or it leaves it
1132 // null. It may be called multiple times.
1133 void TimingInfo::createTheTimeInfo() {
1134 if (!TimePassesIsEnabled || TheTimeInfo) return;
1136 // Constructed the first time this is called, iff -time-passes is enabled.
1137 // This guarantees that the object will be constructed before static globals,
1138 // thus it will be destroyed before them.
1139 static ManagedStatic<TimingInfo> TTI;
1140 TheTimeInfo = &*TTI;
1143 /// If TimingInfo is enabled then start pass timer.
1144 void StartPassTimer(Pass *P) {
1146 TheTimeInfo->passStarted(P);
1149 /// If TimingInfo is enabled then stop pass timer.
1150 void StopPassTimer(Pass *P) {
1152 TheTimeInfo->passEnded(P);
1155 //===----------------------------------------------------------------------===//
1156 // PMStack implementation
1159 // Pop Pass Manager from the stack and clear its analysis info.
1160 void PMStack::pop() {
1162 PMDataManager *Top = this->top();
1163 Top->initializeAnalysisInfo();
1168 // Push PM on the stack and set its top level manager.
1169 void PMStack::push(Pass *P) {
1171 PMDataManager *Top = NULL;
1172 PMDataManager *PM = dynamic_cast<PMDataManager *>(P);
1173 assert (PM && "Unable to push. Pass Manager expected");
1175 if (this->empty()) {
1180 PMTopLevelManager *TPM = Top->getTopLevelManager();
1182 assert (TPM && "Unable to find top level manager");
1183 TPM->addIndirectPassManager(PM);
1184 PM->setTopLevelManager(TPM);
1187 AnalysisResolver *AR = new AnalysisResolver(*Top);
1193 // Dump content of the pass manager stack.
1194 void PMStack::dump() {
1195 for(std::deque<PMDataManager *>::iterator I = S.begin(),
1196 E = S.end(); I != E; ++I) {
1197 Pass *P = dynamic_cast<Pass *>(*I);
1198 printf ("%s ", P->getPassName());
1204 // Walk Pass Manager stack and set LastUse markers if any
1205 // manager is transfering this priviledge to its parent manager
1206 void PMStack::handleLastUserOverflow() {
1208 for(PMStack::iterator I = this->begin(), E = this->end(); I != E;) {
1210 PMDataManager *Child = *I++;
1212 PMDataManager *Parent = *I++;
1213 PMTopLevelManager *TPM = Parent->getTopLevelManager();
1214 std::vector<Pass *> &TLU = Child->getTransferredLastUses();
1216 Pass *P = dynamic_cast<Pass *>(Parent);
1217 TPM->setLastUser(TLU, P);
1223 /// Find appropriate Module Pass Manager in the PM Stack and
1224 /// add self into that manager.
1225 void ModulePass::assignPassManager(PMStack &PMS,
1226 PassManagerType PreferredType) {
1228 // Find Module Pass Manager
1229 while(!PMS.empty()) {
1230 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1231 if (TopPMType == PreferredType)
1232 break; // We found desired pass manager
1233 else if (TopPMType > PMT_ModulePassManager)
1234 PMS.pop(); // Pop children pass managers
1239 PMS.top()->add(this);
1242 /// Find appropriate Function Pass Manager or Call Graph Pass Manager
1243 /// in the PM Stack and add self into that manager.
1244 void FunctionPass::assignPassManager(PMStack &PMS,
1245 PassManagerType PreferredType) {
1247 // Find Module Pass Manager (TODO : Or Call Graph Pass Manager)
1248 while(!PMS.empty()) {
1249 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1254 FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top());
1256 // Create new Function Pass Manager
1258 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1259 PMDataManager *PMD = PMS.top();
1261 // [1] Create new Function Pass Manager
1262 FPP = new FPPassManager(PMD->getDepth() + 1);
1264 // [2] Set up new manager's top level manager
1265 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1266 TPM->addIndirectPassManager(FPP);
1268 // [3] Assign manager to manage this new manager. This may create
1269 // and push new managers into PMS
1270 Pass *P = dynamic_cast<Pass *>(FPP);
1272 // If Call Graph Pass Manager is active then use it to manage
1273 // this new Function Pass manager.
1274 if (PMD->getPassManagerType() == PMT_CallGraphPassManager)
1275 P->assignPassManager(PMS, PMT_CallGraphPassManager);
1277 P->assignPassManager(PMS);
1279 // [4] Push new manager into PMS
1283 // Assign FPP as the manager of this pass.
1287 /// Find appropriate Basic Pass Manager or Call Graph Pass Manager
1288 /// in the PM Stack and add self into that manager.
1289 void BasicBlockPass::assignPassManager(PMStack &PMS,
1290 PassManagerType PreferredType) {
1292 BBPassManager *BBP = NULL;
1294 // Basic Pass Manager is a leaf pass manager. It does not handle
1295 // any other pass manager.
1297 BBP = dynamic_cast<BBPassManager *>(PMS.top());
1300 // If leaf manager is not Basic Block Pass manager then create new
1301 // basic Block Pass manager.
1304 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1305 PMDataManager *PMD = PMS.top();
1307 // [1] Create new Basic Block Manager
1308 BBP = new BBPassManager(PMD->getDepth() + 1);
1310 // [2] Set up new manager's top level manager
1311 // Basic Block Pass Manager does not live by itself
1312 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1313 TPM->addIndirectPassManager(BBP);
1315 // [3] Assign manager to manage this new manager. This may create
1316 // and push new managers into PMS
1317 Pass *P = dynamic_cast<Pass *>(BBP);
1318 P->assignPassManager(PMS);
1320 // [4] Push new manager into PMS
1324 // Assign BBP as the manager of this pass.