1 //===- PassManagerT.h - Container for Passes --------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the PassManagerT class. This class is used to hold,
11 // maintain, and optimize execution of Pass's. The PassManager class ensures
12 // that analysis results are available before a pass runs, and that Pass's are
13 // destroyed when the PassManager is destroyed.
15 // The PassManagerT template is instantiated three times to do its job. The
16 // public PassManager class is a Pimpl around the PassManagerT<Module> interface
17 // to avoid having all of the PassManager clients being exposed to the
18 // implementation details herein.
20 //===----------------------------------------------------------------------===//
22 #ifndef LLVM_PASSMANAGER_T_H
23 #define LLVM_PASSMANAGER_T_H
25 #include "llvm/Pass.h"
26 #include "Support/CommandLine.h"
27 #include "Support/LeakDetector.h"
28 #include "Support/Timer.h"
34 //===----------------------------------------------------------------------===//
35 // Pass debugging information. Often it is useful to find out what pass is
36 // running when a crash occurs in a utility. When this library is compiled with
37 // debugging on, a command line option (--debug-pass) is enabled that causes the
38 // pass name to be printed before it executes.
41 // Different debug levels that can be enabled...
43 None, Arguments, Structure, Executions, Details
46 static cl::opt<enum PassDebugLevel>
47 PassDebugging("debug-pass", cl::Hidden,
48 cl::desc("Print PassManager debugging information"),
50 clEnumVal(None , "disable debug output"),
51 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
52 clEnumVal(Structure , "print pass structure before run()"),
53 clEnumVal(Executions, "print pass name before it is executed"),
54 clEnumVal(Details , "print pass details when it is executed"),
57 //===----------------------------------------------------------------------===//
58 // PMDebug class - a set of debugging functions, that are not to be
59 // instantiated by the template.
62 static void PerformPassStartupStuff(Pass *P) {
63 // If debugging is enabled, print out argument information...
64 if (PassDebugging >= Arguments) {
65 std::cerr << "Pass Arguments: ";
66 PrintArgumentInformation(P);
69 // Print the pass execution structure
70 if (PassDebugging >= Structure)
71 P->dumpPassStructure();
75 static void PrintArgumentInformation(const Pass *P);
76 static void PrintPassInformation(unsigned,const char*,Pass *, Module *);
77 static void PrintPassInformation(unsigned,const char*,Pass *, Function *);
78 static void PrintPassInformation(unsigned,const char*,Pass *, BasicBlock *);
79 static void PrintAnalysisSetInfo(unsigned,const char*,Pass *P,
80 const std::vector<AnalysisID> &);
84 //===----------------------------------------------------------------------===//
85 // TimingInfo Class - This class is used to calculate information about the
86 // amount of time each pass takes to execute. This only happens when
87 // -time-passes is enabled on the command line.
91 std::map<Pass*, Timer> TimingData;
94 // Private ctor, must use 'create' member
95 TimingInfo() : TG("... Pass execution timing report ...") {}
97 // TimingDtor - Print out information about timing information
99 // Delete all of the timers...
101 // TimerGroup is deleted next, printing the report.
104 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
105 // to a non null value (if the -time-passes option is enabled) or it leaves it
106 // null. It may be called multiple times.
107 static void createTheTimeInfo();
109 void passStarted(Pass *P) {
110 if (dynamic_cast<AnalysisResolver*>(P)) return;
111 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
112 if (I == TimingData.end())
113 I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
114 I->second.startTimer();
116 void passEnded(Pass *P) {
117 if (dynamic_cast<AnalysisResolver*>(P)) return;
118 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
119 assert (I != TimingData.end() && "passStarted/passEnded not nested right!");
120 I->second.stopTimer();
124 static TimingInfo *TheTimeInfo;
126 //===----------------------------------------------------------------------===//
127 // Declare the PassManagerTraits which will be specialized...
129 template<class UnitType> class PassManagerTraits; // Do not define.
132 //===----------------------------------------------------------------------===//
133 // PassManagerT - Container object for passes. The PassManagerT destructor
134 // deletes all passes contained inside of the PassManagerT, so you shouldn't
135 // delete passes manually, and all passes should be dynamically allocated.
137 template<typename UnitType>
138 class PassManagerT : public PassManagerTraits<UnitType>,public AnalysisResolver{
139 typedef PassManagerTraits<UnitType> Traits;
140 typedef typename Traits::PassClass PassClass;
141 typedef typename Traits::SubPassClass SubPassClass;
142 typedef typename Traits::BatcherClass BatcherClass;
143 typedef typename Traits::ParentClass ParentClass;
145 friend class PassManagerTraits<UnitType>::PassClass;
146 friend class PassManagerTraits<UnitType>::SubPassClass;
147 friend class PassManagerTraits<UnitType>;
148 friend class ImmutablePass;
150 std::vector<PassClass*> Passes; // List of passes to run
151 std::vector<ImmutablePass*> ImmutablePasses; // List of immutable passes
153 // The parent of this pass manager...
154 ParentClass * const Parent;
156 // The current batcher if one is in use, or null
157 BatcherClass *Batcher;
159 // CurrentAnalyses - As the passes are being run, this map contains the
160 // analyses that are available to the current pass for use. This is accessed
161 // through the getAnalysis() function in this class and in Pass.
163 std::map<AnalysisID, Pass*> CurrentAnalyses;
165 // LastUseOf - This map keeps track of the last usage in our pipeline of a
166 // particular pass. When executing passes, the memory for .first is free'd
167 // after .second is run.
169 std::map<Pass*, Pass*> LastUseOf;
172 PassManagerT(ParentClass *Par = 0) : Parent(Par), Batcher(0) {}
174 // Delete all of the contained passes...
175 for (typename std::vector<PassClass*>::iterator
176 I = Passes.begin(), E = Passes.end(); I != E; ++I)
179 for (std::vector<ImmutablePass*>::iterator
180 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
184 // run - Run all of the queued passes on the specified module in an optimal
186 virtual bool runOnUnit(UnitType *M) {
187 bool MadeChanges = false;
189 CurrentAnalyses.clear();
191 TimingInfo::createTheTimeInfo();
193 // Add any immutable passes to the CurrentAnalyses set...
194 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
195 ImmutablePass *IPass = ImmutablePasses[i];
196 if (const PassInfo *PI = IPass->getPassInfo()) {
197 CurrentAnalyses[PI] = IPass;
199 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
200 for (unsigned i = 0, e = II.size(); i != e; ++i)
201 CurrentAnalyses[II[i]] = IPass;
205 // LastUserOf - This contains the inverted LastUseOfMap...
206 std::map<Pass *, std::vector<Pass*> > LastUserOf;
207 for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(),
208 E = LastUseOf.end(); I != E; ++I)
209 LastUserOf[I->second].push_back(I->first);
212 // Output debug information...
213 if (Parent == 0) PMDebug::PerformPassStartupStuff(this);
215 // Run all of the passes
216 for (unsigned i = 0, e = Passes.size(); i < e; ++i) {
217 PassClass *P = Passes[i];
219 PMDebug::PrintPassInformation(getDepth(), "Executing Pass", P, M);
221 // Get information about what analyses the pass uses...
222 AnalysisUsage AnUsage;
223 P->getAnalysisUsage(AnUsage);
224 PMDebug::PrintAnalysisSetInfo(getDepth(), "Required", P,
225 AnUsage.getRequiredSet());
227 // All Required analyses should be available to the pass as it runs! Here
228 // we fill in the AnalysisImpls member of the pass so that it can
229 // successfully use the getAnalysis() method to retrieve the
230 // implementations it needs.
232 P->AnalysisImpls.clear();
233 P->AnalysisImpls.reserve(AnUsage.getRequiredSet().size());
234 for (std::vector<const PassInfo *>::const_iterator
235 I = AnUsage.getRequiredSet().begin(),
236 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
237 Pass *Impl = getAnalysisOrNullUp(*I);
239 std::cerr << "Analysis '" << (*I)->getPassName()
240 << "' used but not available!";
241 assert(0 && "Analysis used but not available!");
242 } else if (PassDebugging == Details) {
243 if ((*I)->getPassName() != std::string(Impl->getPassName()))
244 std::cerr << " Interface '" << (*I)->getPassName()
245 << "' implemented by '" << Impl->getPassName() << "'\n";
247 P->AnalysisImpls.push_back(std::make_pair(*I, Impl));
251 if (TheTimeInfo) TheTimeInfo->passStarted(P);
252 bool Changed = runPass(P, M);
253 if (TheTimeInfo) TheTimeInfo->passEnded(P);
254 MadeChanges |= Changed;
256 // Check for memory leaks by the pass...
257 LeakDetector::checkForGarbage(std::string("after running pass '") +
258 P->getPassName() + "'");
261 PMDebug::PrintPassInformation(getDepth()+1, "Made Modification", P, M);
262 PMDebug::PrintAnalysisSetInfo(getDepth(), "Preserved", P,
263 AnUsage.getPreservedSet());
266 // Erase all analyses not in the preserved set...
267 if (!AnUsage.getPreservesAll()) {
268 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
269 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(),
270 E = CurrentAnalyses.end(); I != E; )
271 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) !=
273 ++I; // This analysis is preserved, leave it in the available set...
275 if (!dynamic_cast<ImmutablePass*>(I->second)) {
276 std::map<AnalysisID, Pass*>::iterator J = I++;
277 CurrentAnalyses.erase(J); // Analysis not preserved!
284 // Add the current pass to the set of passes that have been run, and are
285 // thus available to users.
287 if (const PassInfo *PI = P->getPassInfo()) {
288 CurrentAnalyses[PI] = P;
290 // This pass is the current implementation of all of the interfaces it
291 // implements as well.
293 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
294 for (unsigned i = 0, e = II.size(); i != e; ++i)
295 CurrentAnalyses[II[i]] = P;
298 // Free memory for any passes that we are the last use of...
299 std::vector<Pass*> &DeadPass = LastUserOf[P];
300 for (std::vector<Pass*>::iterator I = DeadPass.begin(),E = DeadPass.end();
302 PMDebug::PrintPassInformation(getDepth()+1, "Freeing Pass", *I, M);
303 (*I)->releaseMemory();
306 // Make sure to remove dead passes from the CurrentAnalyses list...
307 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin();
308 I != CurrentAnalyses.end(); ) {
309 std::vector<Pass*>::iterator DPI = std::find(DeadPass.begin(),
310 DeadPass.end(), I->second);
311 if (DPI != DeadPass.end()) { // This pass is dead now... remove it
312 std::map<AnalysisID, Pass*>::iterator IDead = I++;
313 CurrentAnalyses.erase(IDead);
315 ++I; // Move on to the next element...
323 // dumpPassStructure - Implement the -debug-passes=PassStructure option
324 virtual void dumpPassStructure(unsigned Offset = 0) {
325 // Print out the immutable passes...
326 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i)
327 ImmutablePasses[i]->dumpPassStructure(0);
329 std::cerr << std::string(Offset*2, ' ') << Traits::getPMName()
330 << " Pass Manager\n";
331 for (typename std::vector<PassClass*>::iterator
332 I = Passes.begin(), E = Passes.end(); I != E; ++I) {
334 P->dumpPassStructure(Offset+1);
336 // Loop through and see which classes are destroyed after this one...
337 for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(),
338 E = LastUseOf.end(); I != E; ++I) {
339 if (P == I->second) {
340 std::cerr << "--" << std::string(Offset*2, ' ');
341 I->first->dumpPassStructure(0);
347 Pass *getImmutablePassOrNull(const PassInfo *ID) const {
348 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
349 const PassInfo *IPID = ImmutablePasses[i]->getPassInfo();
351 return ImmutablePasses[i];
353 // This pass is the current implementation of all of the interfaces it
354 // implements as well.
356 const std::vector<const PassInfo*> &II =
357 IPID->getInterfacesImplemented();
358 for (unsigned j = 0, e = II.size(); j != e; ++j)
359 if (II[j] == ID) return ImmutablePasses[i];
364 Pass *getAnalysisOrNullDown(const PassInfo *ID) const {
365 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID);
367 if (I != CurrentAnalyses.end())
368 return I->second; // Found it.
370 if (Pass *P = getImmutablePassOrNull(ID))
374 return ((AnalysisResolver*)Batcher)->getAnalysisOrNullDown(ID);
378 Pass *getAnalysisOrNullUp(const PassInfo *ID) const {
379 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID);
380 if (I != CurrentAnalyses.end())
381 return I->second; // Found it.
383 if (Parent) // Try scanning...
384 return Parent->getAnalysisOrNullUp(ID);
385 else if (!ImmutablePasses.empty())
386 return getImmutablePassOrNull(ID);
390 // markPassUsed - Inform higher level pass managers (and ourselves)
391 // that these analyses are being used by this pass. This is used to
392 // make sure that analyses are not free'd before we have to use
395 void markPassUsed(const PassInfo *P, Pass *User) {
396 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(P);
398 if (I != CurrentAnalyses.end()) {
399 LastUseOf[I->second] = User; // Local pass, extend the lifetime
401 // Pass not in current available set, must be a higher level pass
402 // available to us, propagate to parent pass manager... We tell the
403 // parent that we (the passmanager) are using the analysis so that it
404 // frees the analysis AFTER this pass manager runs.
407 Parent->markPassUsed(P, this);
409 assert(getAnalysisOrNullUp(P) &&
410 dynamic_cast<ImmutablePass*>(getAnalysisOrNullUp(P)) &&
411 "Pass available but not found! "
412 "Perhaps this is a module pass requiring a function pass?");
417 // Return the number of parent PassManagers that exist
418 virtual unsigned getDepth() const {
419 if (Parent == 0) return 0;
420 return 1 + Parent->getDepth();
423 virtual unsigned getNumContainedPasses() const { return Passes.size(); }
424 virtual const Pass *getContainedPass(unsigned N) const {
425 assert(N < Passes.size() && "Pass number out of range!");
429 // add - Add a pass to the queue of passes to run. This gives ownership of
430 // the Pass to the PassManager. When the PassManager is destroyed, the pass
431 // will be destroyed as well, so there is no need to delete the pass. This
432 // implies that all passes MUST be new'd.
434 void add(PassClass *P) {
435 // Get information about what analyses the pass uses...
436 AnalysisUsage AnUsage;
437 P->getAnalysisUsage(AnUsage);
438 const std::vector<AnalysisID> &Required = AnUsage.getRequiredSet();
440 // Loop over all of the analyses used by this pass,
441 for (std::vector<AnalysisID>::const_iterator I = Required.begin(),
442 E = Required.end(); I != E; ++I) {
443 if (getAnalysisOrNullDown(*I) == 0)
444 add((PassClass*)(*I)->createPass());
447 // Tell the pass to add itself to this PassManager... the way it does so
448 // depends on the class of the pass, and is critical to laying out passes in
449 // an optimal order..
451 P->addToPassManager(this, AnUsage);
454 // add - H4x0r an ImmutablePass into a PassManager that might not be
457 void add(ImmutablePass *P) {
458 // Get information about what analyses the pass uses...
459 AnalysisUsage AnUsage;
460 P->getAnalysisUsage(AnUsage);
461 const std::vector<AnalysisID> &Required = AnUsage.getRequiredSet();
463 // Loop over all of the analyses used by this pass,
464 for (std::vector<AnalysisID>::const_iterator I = Required.begin(),
465 E = Required.end(); I != E; ++I) {
466 if (getAnalysisOrNullDown(*I) == 0)
467 add((PassClass*)(*I)->createPass());
470 // Add the ImmutablePass to this PassManager.
475 // addPass - These functions are used to implement the subclass specific
476 // behaviors present in PassManager. Basically the add(Pass*) method ends up
477 // reflecting its behavior into a Pass::addToPassManager call. Subclasses of
478 // Pass override it specifically so that they can reflect the type
479 // information inherent in "this" back to the PassManager.
481 // For generic Pass subclasses (which are interprocedural passes), we simply
482 // add the pass to the end of the pass list and terminate any accumulation of
483 // FunctionPass's that are present.
485 void addPass(PassClass *P, AnalysisUsage &AnUsage) {
486 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
488 // FIXME: If this pass being added isn't killed by any of the passes in the
489 // batcher class then we can reorder to pass to execute before the batcher
490 // does, which will potentially allow us to batch more passes!
492 //const std::vector<AnalysisID> &ProvidedSet = AnUsage.getProvidedSet();
493 if (Batcher /*&& ProvidedSet.empty()*/)
494 closeBatcher(); // This pass cannot be batched!
496 // Set the Resolver instance variable in the Pass so that it knows where to
497 // find this object...
499 setAnalysisResolver(P, this);
502 // Inform higher level pass managers (and ourselves) that these analyses are
503 // being used by this pass. This is used to make sure that analyses are not
504 // free'd before we have to use them...
506 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
507 E = RequiredSet.end(); I != E; ++I)
508 markPassUsed(*I, P); // Mark *I as used by P
510 // Erase all analyses not in the preserved set...
511 if (!AnUsage.getPreservesAll()) {
512 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
513 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(),
514 E = CurrentAnalyses.end(); I != E; ) {
515 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
516 PreservedSet.end()) { // Analysis not preserved!
517 CurrentAnalyses.erase(I); // Remove from available analyses
518 I = CurrentAnalyses.begin();
525 // Add this pass to the currently available set...
526 if (const PassInfo *PI = P->getPassInfo()) {
527 CurrentAnalyses[PI] = P;
529 // This pass is the current implementation of all of the interfaces it
530 // implements as well.
532 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
533 for (unsigned i = 0, e = II.size(); i != e; ++i)
534 CurrentAnalyses[II[i]] = P;
537 // For now assume that our results are never used...
541 // For FunctionPass subclasses, we must be sure to batch the FunctionPass's
542 // together in a BatcherClass object so that all of the analyses are run
543 // together a function at a time.
545 void addPass(SubPassClass *MP, AnalysisUsage &AnUsage) {
546 if (Batcher == 0) // If we don't have a batcher yet, make one now.
547 Batcher = new BatcherClass(this);
548 // The Batcher will queue the passes up
549 MP->addToPassManager(Batcher, AnUsage);
552 // closeBatcher - Terminate the batcher that is being worked on.
553 void closeBatcher() {
555 Passes.push_back(Batcher);
561 // When an ImmutablePass is added, it gets added to the top level pass
563 void addPass(ImmutablePass *IP, AnalysisUsage &AU) {
564 if (Parent) { // Make sure this request goes to the top level passmanager...
565 Parent->addPass(IP, AU);
569 // Set the Resolver instance variable in the Pass so that it knows where to
570 // find this object...
572 setAnalysisResolver(IP, this);
573 ImmutablePasses.push_back(IP);
575 // All Required analyses should be available to the pass as it initializes!
576 // Here we fill in the AnalysisImpls member of the pass so that it can
577 // successfully use the getAnalysis() method to retrieve the implementations
580 IP->AnalysisImpls.clear();
581 IP->AnalysisImpls.reserve(AU.getRequiredSet().size());
582 for (std::vector<const PassInfo *>::const_iterator
583 I = AU.getRequiredSet().begin(),
584 E = AU.getRequiredSet().end(); I != E; ++I) {
585 Pass *Impl = getAnalysisOrNullUp(*I);
587 std::cerr << "Analysis '" << (*I)->getPassName()
588 << "' used but not available!";
589 assert(0 && "Analysis used but not available!");
590 } else if (PassDebugging == Details) {
591 if ((*I)->getPassName() != std::string(Impl->getPassName()))
592 std::cerr << " Interface '" << (*I)->getPassName()
593 << "' implemented by '" << Impl->getPassName() << "'\n";
595 IP->AnalysisImpls.push_back(std::make_pair(*I, Impl));
598 // Initialize the immutable pass...
599 IP->initializePass();
605 //===----------------------------------------------------------------------===//
606 // PassManagerTraits<BasicBlock> Specialization
608 // This pass manager is used to group together all of the BasicBlockPass's
609 // into a single unit.
611 template<> struct PassManagerTraits<BasicBlock> : public BasicBlockPass {
612 // PassClass - The type of passes tracked by this PassManager
613 typedef BasicBlockPass PassClass;
615 // SubPassClass - The types of classes that should be collated together
616 // This is impossible to match, so BasicBlock instantiations of PassManagerT
619 typedef PassManagerT<Module> SubPassClass;
621 // BatcherClass - The type to use for collation of subtypes... This class is
622 // never instantiated for the PassManager<BasicBlock>, but it must be an
623 // instance of PassClass to typecheck.
625 typedef PassClass BatcherClass;
627 // ParentClass - The type of the parent PassManager...
628 typedef PassManagerT<Function> ParentClass;
630 // PMType - The type of the passmanager that subclasses this class
631 typedef PassManagerT<BasicBlock> PMType;
633 // runPass - Specify how the pass should be run on the UnitType
634 static bool runPass(PassClass *P, BasicBlock *M) {
635 // todo, init and finalize
636 return P->runOnBasicBlock(*M);
639 // getPMName() - Return the name of the unit the PassManager operates on for
641 const char *getPMName() const { return "BasicBlock"; }
642 virtual const char *getPassName() const { return "BasicBlock Pass Manager"; }
644 // Implement the BasicBlockPass interface...
645 virtual bool doInitialization(Module &M);
646 virtual bool doInitialization(Function &F);
647 virtual bool runOnBasicBlock(BasicBlock &BB);
648 virtual bool doFinalization(Function &F);
649 virtual bool doFinalization(Module &M);
651 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
652 AU.setPreservesAll();
658 //===----------------------------------------------------------------------===//
659 // PassManagerTraits<Function> Specialization
661 // This pass manager is used to group together all of the FunctionPass's
662 // into a single unit.
664 template<> struct PassManagerTraits<Function> : public FunctionPass {
665 // PassClass - The type of passes tracked by this PassManager
666 typedef FunctionPass PassClass;
668 // SubPassClass - The types of classes that should be collated together
669 typedef BasicBlockPass SubPassClass;
671 // BatcherClass - The type to use for collation of subtypes...
672 typedef PassManagerT<BasicBlock> BatcherClass;
674 // ParentClass - The type of the parent PassManager...
675 typedef PassManagerT<Module> ParentClass;
677 // PMType - The type of the passmanager that subclasses this class
678 typedef PassManagerT<Function> PMType;
680 // runPass - Specify how the pass should be run on the UnitType
681 static bool runPass(PassClass *P, Function *F) {
682 return P->runOnFunction(*F);
685 // getPMName() - Return the name of the unit the PassManager operates on for
687 const char *getPMName() const { return "Function"; }
688 virtual const char *getPassName() const { return "Function Pass Manager"; }
690 // Implement the FunctionPass interface...
691 virtual bool doInitialization(Module &M);
692 virtual bool runOnFunction(Function &F);
693 virtual bool doFinalization(Module &M);
695 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
696 AU.setPreservesAll();
702 //===----------------------------------------------------------------------===//
703 // PassManagerTraits<Module> Specialization
705 // This is the top level PassManager implementation that holds generic passes.
707 template<> struct PassManagerTraits<Module> : public Pass {
708 // PassClass - The type of passes tracked by this PassManager
709 typedef Pass PassClass;
711 // SubPassClass - The types of classes that should be collated together
712 typedef FunctionPass SubPassClass;
714 // BatcherClass - The type to use for collation of subtypes...
715 typedef PassManagerT<Function> BatcherClass;
717 // ParentClass - The type of the parent PassManager...
718 typedef AnalysisResolver ParentClass;
720 // runPass - Specify how the pass should be run on the UnitType
721 static bool runPass(PassClass *P, Module *M) { return P->run(*M); }
723 // getPMName() - Return the name of the unit the PassManager operates on for
725 const char *getPMName() const { return "Module"; }
726 virtual const char *getPassName() const { return "Module Pass Manager"; }
728 // run - Implement the PassManager interface...
729 bool run(Module &M) {
730 return ((PassManagerT<Module>*)this)->runOnUnit(&M);
736 //===----------------------------------------------------------------------===//
737 // PassManagerTraits Method Implementations
740 // PassManagerTraits<BasicBlock> Implementations
742 inline bool PassManagerTraits<BasicBlock>::doInitialization(Module &M) {
743 bool Changed = false;
744 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
745 ((PMType*)this)->Passes[i]->doInitialization(M);
749 inline bool PassManagerTraits<BasicBlock>::doInitialization(Function &F) {
750 bool Changed = false;
751 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
752 ((PMType*)this)->Passes[i]->doInitialization(F);
756 inline bool PassManagerTraits<BasicBlock>::runOnBasicBlock(BasicBlock &BB) {
757 return ((PMType*)this)->runOnUnit(&BB);
760 inline bool PassManagerTraits<BasicBlock>::doFinalization(Function &F) {
761 bool Changed = false;
762 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
763 ((PMType*)this)->Passes[i]->doFinalization(F);
767 inline bool PassManagerTraits<BasicBlock>::doFinalization(Module &M) {
768 bool Changed = false;
769 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
770 ((PMType*)this)->Passes[i]->doFinalization(M);
775 // PassManagerTraits<Function> Implementations
777 inline bool PassManagerTraits<Function>::doInitialization(Module &M) {
778 bool Changed = false;
779 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
780 ((PMType*)this)->Passes[i]->doInitialization(M);
784 inline bool PassManagerTraits<Function>::runOnFunction(Function &F) {
785 return ((PMType*)this)->runOnUnit(&F);
788 inline bool PassManagerTraits<Function>::doFinalization(Module &M) {
789 bool Changed = false;
790 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
791 ((PMType*)this)->Passes[i]->doFinalization(M);
795 } // End llvm namespace