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;
146 friend class PassManagerTraits<UnitType>::PassClass;
147 friend class PassManagerTraits<UnitType>::SubPassClass;
152 friend class PassManagerTraits<UnitType>;
153 friend class ImmutablePass;
155 std::vector<PassClass*> Passes; // List of passes to run
156 std::vector<ImmutablePass*> ImmutablePasses; // List of immutable passes
158 // The parent of this pass manager...
159 ParentClass * const Parent;
161 // The current batcher if one is in use, or null
162 BatcherClass *Batcher;
164 // CurrentAnalyses - As the passes are being run, this map contains the
165 // analyses that are available to the current pass for use. This is accessed
166 // through the getAnalysis() function in this class and in Pass.
168 std::map<AnalysisID, Pass*> CurrentAnalyses;
170 // LastUseOf - This map keeps track of the last usage in our pipeline of a
171 // particular pass. When executing passes, the memory for .first is free'd
172 // after .second is run.
174 std::map<Pass*, Pass*> LastUseOf;
177 PassManagerT(ParentClass *Par = 0) : Parent(Par), Batcher(0) {}
179 // Delete all of the contained passes...
180 for (typename std::vector<PassClass*>::iterator
181 I = Passes.begin(), E = Passes.end(); I != E; ++I)
184 for (std::vector<ImmutablePass*>::iterator
185 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
189 // run - Run all of the queued passes on the specified module in an optimal
191 virtual bool runOnUnit(UnitType *M) {
192 bool MadeChanges = false;
194 CurrentAnalyses.clear();
196 TimingInfo::createTheTimeInfo();
198 // Add any immutable passes to the CurrentAnalyses set...
199 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
200 ImmutablePass *IPass = ImmutablePasses[i];
201 if (const PassInfo *PI = IPass->getPassInfo()) {
202 CurrentAnalyses[PI] = IPass;
204 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
205 for (unsigned i = 0, e = II.size(); i != e; ++i)
206 CurrentAnalyses[II[i]] = IPass;
210 // LastUserOf - This contains the inverted LastUseOfMap...
211 std::map<Pass *, std::vector<Pass*> > LastUserOf;
212 for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(),
213 E = LastUseOf.end(); I != E; ++I)
214 LastUserOf[I->second].push_back(I->first);
216 // Output debug information...
217 if (Parent == 0) PMDebug::PerformPassStartupStuff(this);
219 // Run all of the passes
220 for (unsigned i = 0, e = Passes.size(); i < e; ++i) {
221 PassClass *P = Passes[i];
223 PMDebug::PrintPassInformation(getDepth(), "Executing Pass", P, M);
225 // Get information about what analyses the pass uses...
226 AnalysisUsage AnUsage;
227 P->getAnalysisUsage(AnUsage);
228 PMDebug::PrintAnalysisSetInfo(getDepth(), "Required", P,
229 AnUsage.getRequiredSet());
231 // All Required analyses should be available to the pass as it runs! Here
232 // we fill in the AnalysisImpls member of the pass so that it can
233 // successfully use the getAnalysis() method to retrieve the
234 // implementations it needs.
236 P->AnalysisImpls.clear();
237 P->AnalysisImpls.reserve(AnUsage.getRequiredSet().size());
238 for (std::vector<const PassInfo *>::const_iterator
239 I = AnUsage.getRequiredSet().begin(),
240 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
241 Pass *Impl = getAnalysisOrNullUp(*I);
243 std::cerr << "Analysis '" << (*I)->getPassName()
244 << "' used but not available!";
245 assert(0 && "Analysis used but not available!");
246 } else if (PassDebugging == Details) {
247 if ((*I)->getPassName() != std::string(Impl->getPassName()))
248 std::cerr << " Interface '" << (*I)->getPassName()
249 << "' implemented by '" << Impl->getPassName() << "'\n";
251 P->AnalysisImpls.push_back(std::make_pair(*I, Impl));
255 if (TheTimeInfo) TheTimeInfo->passStarted(P);
256 bool Changed = runPass(P, M);
257 if (TheTimeInfo) TheTimeInfo->passEnded(P);
258 MadeChanges |= Changed;
260 // Check for memory leaks by the pass...
261 LeakDetector::checkForGarbage(std::string("after running pass '") +
262 P->getPassName() + "'");
265 PMDebug::PrintPassInformation(getDepth()+1, "Made Modification", P, M);
266 PMDebug::PrintAnalysisSetInfo(getDepth(), "Preserved", P,
267 AnUsage.getPreservedSet());
270 // Erase all analyses not in the preserved set...
271 if (!AnUsage.getPreservesAll()) {
272 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
273 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(),
274 E = CurrentAnalyses.end(); I != E; )
275 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) !=
277 ++I; // This analysis is preserved, leave it in the available set...
279 if (!dynamic_cast<ImmutablePass*>(I->second)) {
280 std::map<AnalysisID, Pass*>::iterator J = I++;
281 CurrentAnalyses.erase(J); // Analysis not preserved!
288 // Add the current pass to the set of passes that have been run, and are
289 // thus available to users.
291 if (const PassInfo *PI = P->getPassInfo()) {
292 CurrentAnalyses[PI] = P;
294 // This pass is the current implementation of all of the interfaces it
295 // implements as well.
297 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
298 for (unsigned i = 0, e = II.size(); i != e; ++i)
299 CurrentAnalyses[II[i]] = P;
302 // Free memory for any passes that we are the last use of...
303 std::vector<Pass*> &DeadPass = LastUserOf[P];
304 for (std::vector<Pass*>::iterator I = DeadPass.begin(),E = DeadPass.end();
306 PMDebug::PrintPassInformation(getDepth()+1, "Freeing Pass", *I, M);
307 (*I)->releaseMemory();
310 // Make sure to remove dead passes from the CurrentAnalyses list...
311 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin();
312 I != CurrentAnalyses.end(); ) {
313 std::vector<Pass*>::iterator DPI = std::find(DeadPass.begin(),
314 DeadPass.end(), I->second);
315 if (DPI != DeadPass.end()) { // This pass is dead now... remove it
316 std::map<AnalysisID, Pass*>::iterator IDead = I++;
317 CurrentAnalyses.erase(IDead);
319 ++I; // Move on to the next element...
327 // dumpPassStructure - Implement the -debug-passes=PassStructure option
328 virtual void dumpPassStructure(unsigned Offset = 0) {
329 // Print out the immutable passes...
330 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i)
331 ImmutablePasses[i]->dumpPassStructure(0);
333 std::cerr << std::string(Offset*2, ' ') << Traits::getPMName()
334 << " Pass Manager\n";
335 for (typename std::vector<PassClass*>::iterator
336 I = Passes.begin(), E = Passes.end(); I != E; ++I) {
338 P->dumpPassStructure(Offset+1);
340 // Loop through and see which classes are destroyed after this one...
341 for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(),
342 E = LastUseOf.end(); I != E; ++I) {
343 if (P == I->second) {
344 std::cerr << "--" << std::string(Offset*2, ' ');
345 I->first->dumpPassStructure(0);
351 Pass *getImmutablePassOrNull(const PassInfo *ID) const {
352 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
353 const PassInfo *IPID = ImmutablePasses[i]->getPassInfo();
355 return ImmutablePasses[i];
357 // This pass is the current implementation of all of the interfaces it
358 // implements as well.
360 const std::vector<const PassInfo*> &II =
361 IPID->getInterfacesImplemented();
362 for (unsigned j = 0, e = II.size(); j != e; ++j)
363 if (II[j] == ID) return ImmutablePasses[i];
368 Pass *getAnalysisOrNullDown(const PassInfo *ID) const {
369 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID);
371 if (I != CurrentAnalyses.end())
372 return I->second; // Found it.
374 if (Pass *P = getImmutablePassOrNull(ID))
378 return ((AnalysisResolver*)Batcher)->getAnalysisOrNullDown(ID);
382 Pass *getAnalysisOrNullUp(const PassInfo *ID) const {
383 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID);
384 if (I != CurrentAnalyses.end())
385 return I->second; // Found it.
387 if (Parent) // Try scanning...
388 return Parent->getAnalysisOrNullUp(ID);
389 else if (!ImmutablePasses.empty())
390 return getImmutablePassOrNull(ID);
394 // markPassUsed - Inform higher level pass managers (and ourselves)
395 // that these analyses are being used by this pass. This is used to
396 // make sure that analyses are not free'd before we have to use
399 void markPassUsed(const PassInfo *P, Pass *User) {
400 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(P);
402 if (I != CurrentAnalyses.end()) {
403 LastUseOf[I->second] = User; // Local pass, extend the lifetime
405 // Prolong live range of analyses that are needed after an analysis pass
406 // is destroyed, for querying by subsequent passes
407 AnalysisUsage AnUsage;
408 I->second->getAnalysisUsage(AnUsage);
409 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
410 for (std::vector<AnalysisID>::const_iterator i = IDs.begin(),
411 e = IDs.end(); i != e; ++i)
412 markPassUsed(*i, User);
415 // Pass not in current available set, must be a higher level pass
416 // available to us, propagate to parent pass manager... We tell the
417 // parent that we (the passmanager) are using the analysis so that it
418 // frees the analysis AFTER this pass manager runs.
421 Parent->markPassUsed(P, this);
423 assert(getAnalysisOrNullUp(P) &&
424 dynamic_cast<ImmutablePass*>(getAnalysisOrNullUp(P)) &&
425 "Pass available but not found! "
426 "Perhaps this is a module pass requiring a function pass?");
431 // Return the number of parent PassManagers that exist
432 virtual unsigned getDepth() const {
433 if (Parent == 0) return 0;
434 return 1 + Parent->getDepth();
437 virtual unsigned getNumContainedPasses() const { return Passes.size(); }
438 virtual const Pass *getContainedPass(unsigned N) const {
439 assert(N < Passes.size() && "Pass number out of range!");
443 // add - Add a pass to the queue of passes to run. This gives ownership of
444 // the Pass to the PassManager. When the PassManager is destroyed, the pass
445 // will be destroyed as well, so there is no need to delete the pass. This
446 // implies that all passes MUST be new'd.
448 void add(PassClass *P) {
449 // Get information about what analyses the pass uses...
450 AnalysisUsage AnUsage;
451 P->getAnalysisUsage(AnUsage);
452 const std::vector<AnalysisID> &Required = AnUsage.getRequiredSet();
454 // Loop over all of the analyses used by this pass,
455 for (std::vector<AnalysisID>::const_iterator I = Required.begin(),
456 E = Required.end(); I != E; ++I) {
457 if (getAnalysisOrNullDown(*I) == 0)
458 add((PassClass*)(*I)->createPass());
461 // Tell the pass to add itself to this PassManager... the way it does so
462 // depends on the class of the pass, and is critical to laying out passes in
463 // an optimal order..
465 P->addToPassManager(this, AnUsage);
468 // add - H4x0r an ImmutablePass into a PassManager that might not be
471 void add(ImmutablePass *P) {
472 // Get information about what analyses the pass uses...
473 AnalysisUsage AnUsage;
474 P->getAnalysisUsage(AnUsage);
475 const std::vector<AnalysisID> &Required = AnUsage.getRequiredSet();
477 // Loop over all of the analyses used by this pass,
478 for (std::vector<AnalysisID>::const_iterator I = Required.begin(),
479 E = Required.end(); I != E; ++I) {
480 if (getAnalysisOrNullDown(*I) == 0)
481 add((PassClass*)(*I)->createPass());
484 // Add the ImmutablePass to this PassManager.
489 // addPass - These functions are used to implement the subclass specific
490 // behaviors present in PassManager. Basically the add(Pass*) method ends up
491 // reflecting its behavior into a Pass::addToPassManager call. Subclasses of
492 // Pass override it specifically so that they can reflect the type
493 // information inherent in "this" back to the PassManager.
495 // For generic Pass subclasses (which are interprocedural passes), we simply
496 // add the pass to the end of the pass list and terminate any accumulation of
497 // FunctionPass's that are present.
499 void addPass(PassClass *P, AnalysisUsage &AnUsage) {
500 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
502 // FIXME: If this pass being added isn't killed by any of the passes in the
503 // batcher class then we can reorder to pass to execute before the batcher
504 // does, which will potentially allow us to batch more passes!
506 //const std::vector<AnalysisID> &ProvidedSet = AnUsage.getProvidedSet();
507 if (Batcher /*&& ProvidedSet.empty()*/)
508 closeBatcher(); // This pass cannot be batched!
510 // Set the Resolver instance variable in the Pass so that it knows where to
511 // find this object...
513 setAnalysisResolver(P, this);
516 // Inform higher level pass managers (and ourselves) that these analyses are
517 // being used by this pass. This is used to make sure that analyses are not
518 // free'd before we have to use them...
520 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
521 E = RequiredSet.end(); I != E; ++I)
522 markPassUsed(*I, P); // Mark *I as used by P
524 // Erase all analyses not in the preserved set...
525 if (!AnUsage.getPreservesAll()) {
526 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
527 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(),
528 E = CurrentAnalyses.end(); I != E; ) {
529 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
530 PreservedSet.end()) { // Analysis not preserved!
531 CurrentAnalyses.erase(I); // Remove from available analyses
532 I = CurrentAnalyses.begin();
539 // Add this pass to the currently available set...
540 if (const PassInfo *PI = P->getPassInfo()) {
541 CurrentAnalyses[PI] = P;
543 // This pass is the current implementation of all of the interfaces it
544 // implements as well.
546 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
547 for (unsigned i = 0, e = II.size(); i != e; ++i)
548 CurrentAnalyses[II[i]] = P;
551 // For now assume that our results are never used...
555 // For FunctionPass subclasses, we must be sure to batch the FunctionPass's
556 // together in a BatcherClass object so that all of the analyses are run
557 // together a function at a time.
559 void addPass(SubPassClass *MP, AnalysisUsage &AnUsage) {
560 if (Batcher == 0) // If we don't have a batcher yet, make one now.
561 Batcher = new BatcherClass(this);
562 // The Batcher will queue the passes up
563 MP->addToPassManager(Batcher, AnUsage);
566 // closeBatcher - Terminate the batcher that is being worked on.
567 void closeBatcher() {
569 Passes.push_back(Batcher);
575 // When an ImmutablePass is added, it gets added to the top level pass
577 void addPass(ImmutablePass *IP, AnalysisUsage &AU) {
578 if (Parent) { // Make sure this request goes to the top level passmanager...
579 Parent->addPass(IP, AU);
583 // Set the Resolver instance variable in the Pass so that it knows where to
584 // find this object...
586 setAnalysisResolver(IP, this);
587 ImmutablePasses.push_back(IP);
589 // All Required analyses should be available to the pass as it initializes!
590 // Here we fill in the AnalysisImpls member of the pass so that it can
591 // successfully use the getAnalysis() method to retrieve the implementations
594 IP->AnalysisImpls.clear();
595 IP->AnalysisImpls.reserve(AU.getRequiredSet().size());
596 for (std::vector<const PassInfo *>::const_iterator
597 I = AU.getRequiredSet().begin(),
598 E = AU.getRequiredSet().end(); I != E; ++I) {
599 Pass *Impl = getAnalysisOrNullUp(*I);
601 std::cerr << "Analysis '" << (*I)->getPassName()
602 << "' used but not available!";
603 assert(0 && "Analysis used but not available!");
604 } else if (PassDebugging == Details) {
605 if ((*I)->getPassName() != std::string(Impl->getPassName()))
606 std::cerr << " Interface '" << (*I)->getPassName()
607 << "' implemented by '" << Impl->getPassName() << "'\n";
609 IP->AnalysisImpls.push_back(std::make_pair(*I, Impl));
612 // Initialize the immutable pass...
613 IP->initializePass();
619 //===----------------------------------------------------------------------===//
620 // PassManagerTraits<BasicBlock> Specialization
622 // This pass manager is used to group together all of the BasicBlockPass's
623 // into a single unit.
625 template<> struct PassManagerTraits<BasicBlock> : public BasicBlockPass {
626 // PassClass - The type of passes tracked by this PassManager
627 typedef BasicBlockPass PassClass;
629 // SubPassClass - The types of classes that should be collated together
630 // This is impossible to match, so BasicBlock instantiations of PassManagerT
633 typedef PassManagerT<Module> SubPassClass;
635 // BatcherClass - The type to use for collation of subtypes... This class is
636 // never instantiated for the PassManager<BasicBlock>, but it must be an
637 // instance of PassClass to typecheck.
639 typedef PassClass BatcherClass;
641 // ParentClass - The type of the parent PassManager...
642 typedef PassManagerT<Function> ParentClass;
644 // PMType - The type of the passmanager that subclasses this class
645 typedef PassManagerT<BasicBlock> PMType;
647 // runPass - Specify how the pass should be run on the UnitType
648 static bool runPass(PassClass *P, BasicBlock *M) {
649 // todo, init and finalize
650 return P->runOnBasicBlock(*M);
653 // getPMName() - Return the name of the unit the PassManager operates on for
655 const char *getPMName() const { return "BasicBlock"; }
656 virtual const char *getPassName() const { return "BasicBlock Pass Manager"; }
658 // Implement the BasicBlockPass interface...
659 virtual bool doInitialization(Module &M);
660 virtual bool doInitialization(Function &F);
661 virtual bool runOnBasicBlock(BasicBlock &BB);
662 virtual bool doFinalization(Function &F);
663 virtual bool doFinalization(Module &M);
665 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
666 AU.setPreservesAll();
672 //===----------------------------------------------------------------------===//
673 // PassManagerTraits<Function> Specialization
675 // This pass manager is used to group together all of the FunctionPass's
676 // into a single unit.
678 template<> struct PassManagerTraits<Function> : public FunctionPass {
679 // PassClass - The type of passes tracked by this PassManager
680 typedef FunctionPass PassClass;
682 // SubPassClass - The types of classes that should be collated together
683 typedef BasicBlockPass SubPassClass;
685 // BatcherClass - The type to use for collation of subtypes...
686 typedef PassManagerT<BasicBlock> BatcherClass;
688 // ParentClass - The type of the parent PassManager...
689 typedef PassManagerT<Module> ParentClass;
691 // PMType - The type of the passmanager that subclasses this class
692 typedef PassManagerT<Function> PMType;
694 // runPass - Specify how the pass should be run on the UnitType
695 static bool runPass(PassClass *P, Function *F) {
696 return P->runOnFunction(*F);
699 // getPMName() - Return the name of the unit the PassManager operates on for
701 const char *getPMName() const { return "Function"; }
702 virtual const char *getPassName() const { return "Function Pass Manager"; }
704 // Implement the FunctionPass interface...
705 virtual bool doInitialization(Module &M);
706 virtual bool runOnFunction(Function &F);
707 virtual bool doFinalization(Module &M);
709 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
710 AU.setPreservesAll();
716 //===----------------------------------------------------------------------===//
717 // PassManagerTraits<Module> Specialization
719 // This is the top level PassManager implementation that holds generic passes.
721 template<> struct PassManagerTraits<Module> : public Pass {
722 // PassClass - The type of passes tracked by this PassManager
723 typedef Pass PassClass;
725 // SubPassClass - The types of classes that should be collated together
726 typedef FunctionPass SubPassClass;
728 // BatcherClass - The type to use for collation of subtypes...
729 typedef PassManagerT<Function> BatcherClass;
731 // ParentClass - The type of the parent PassManager...
732 typedef AnalysisResolver ParentClass;
734 // runPass - Specify how the pass should be run on the UnitType
735 static bool runPass(PassClass *P, Module *M) { return P->run(*M); }
737 // getPMName() - Return the name of the unit the PassManager operates on for
739 const char *getPMName() const { return "Module"; }
740 virtual const char *getPassName() const { return "Module Pass Manager"; }
742 // run - Implement the PassManager interface...
743 bool run(Module &M) {
744 return ((PassManagerT<Module>*)this)->runOnUnit(&M);
750 //===----------------------------------------------------------------------===//
751 // PassManagerTraits Method Implementations
754 // PassManagerTraits<BasicBlock> Implementations
756 inline bool PassManagerTraits<BasicBlock>::doInitialization(Module &M) {
757 bool Changed = false;
758 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
759 ((PMType*)this)->Passes[i]->doInitialization(M);
763 inline bool PassManagerTraits<BasicBlock>::doInitialization(Function &F) {
764 bool Changed = false;
765 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
766 ((PMType*)this)->Passes[i]->doInitialization(F);
770 inline bool PassManagerTraits<BasicBlock>::runOnBasicBlock(BasicBlock &BB) {
771 return ((PMType*)this)->runOnUnit(&BB);
774 inline bool PassManagerTraits<BasicBlock>::doFinalization(Function &F) {
775 bool Changed = false;
776 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
777 ((PMType*)this)->Passes[i]->doFinalization(F);
781 inline bool PassManagerTraits<BasicBlock>::doFinalization(Module &M) {
782 bool Changed = false;
783 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
784 ((PMType*)this)->Passes[i]->doFinalization(M);
789 // PassManagerTraits<Function> Implementations
791 inline bool PassManagerTraits<Function>::doInitialization(Module &M) {
792 bool Changed = false;
793 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
794 ((PMType*)this)->Passes[i]->doInitialization(M);
798 inline bool PassManagerTraits<Function>::runOnFunction(Function &F) {
799 return ((PMType*)this)->runOnUnit(&F);
802 inline bool PassManagerTraits<Function>::doFinalization(Module &M) {
803 bool Changed = false;
804 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
805 ((PMType*)this)->Passes[i]->doFinalization(M);
809 } // End llvm namespace