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 "llvm/Support/CommandLine.h"
27 #include "llvm/Support/LeakDetector.h"
28 #include "llvm/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 #if defined(_MSC_VER) || defined(__INTEL_COMPILER) || defined(__HP_aCC)
149 friend class PassManagerTraits<UnitType>::PassClass;
150 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 Pass *AP = (*I)->createPass();
459 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (AP)) { add(IP); }
460 else if (PassClass *RP = dynamic_cast<PassClass *> (AP)) { add(RP); }
461 else { assert (0 && "Wrong kind of pass for this PassManager"); }
465 // Tell the pass to add itself to this PassManager... the way it does so
466 // depends on the class of the pass, and is critical to laying out passes in
467 // an optimal order..
469 P->addToPassManager(this, AnUsage);
472 // add - H4x0r an ImmutablePass into a PassManager that might not be
475 void add(ImmutablePass *P) {
476 // Get information about what analyses the pass uses...
477 AnalysisUsage AnUsage;
478 P->getAnalysisUsage(AnUsage);
479 const std::vector<AnalysisID> &Required = AnUsage.getRequiredSet();
481 // Loop over all of the analyses used by this pass,
482 for (std::vector<AnalysisID>::const_iterator I = Required.begin(),
483 E = Required.end(); I != E; ++I) {
484 if (getAnalysisOrNullDown(*I) == 0) {
485 Pass *AP = (*I)->createPass();
486 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (AP)) add(IP);
487 else if (PassClass *RP = dynamic_cast<PassClass *> (AP)) add(RP);
488 else assert (0 && "Wrong kind of pass for this PassManager");
492 // Add the ImmutablePass to this PassManager.
497 // addPass - These functions are used to implement the subclass specific
498 // behaviors present in PassManager. Basically the add(Pass*) method ends up
499 // reflecting its behavior into a Pass::addToPassManager call. Subclasses of
500 // Pass override it specifically so that they can reflect the type
501 // information inherent in "this" back to the PassManager.
503 // For generic Pass subclasses (which are interprocedural passes), we simply
504 // add the pass to the end of the pass list and terminate any accumulation of
505 // FunctionPass's that are present.
507 void addPass(PassClass *P, AnalysisUsage &AnUsage) {
508 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
510 // FIXME: If this pass being added isn't killed by any of the passes in the
511 // batcher class then we can reorder to pass to execute before the batcher
512 // does, which will potentially allow us to batch more passes!
514 //const std::vector<AnalysisID> &ProvidedSet = AnUsage.getProvidedSet();
515 if (Batcher /*&& ProvidedSet.empty()*/)
516 closeBatcher(); // This pass cannot be batched!
518 // Set the Resolver instance variable in the Pass so that it knows where to
519 // find this object...
521 setAnalysisResolver(P, this);
524 // Inform higher level pass managers (and ourselves) that these analyses are
525 // being used by this pass. This is used to make sure that analyses are not
526 // free'd before we have to use them...
528 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
529 E = RequiredSet.end(); I != E; ++I)
530 markPassUsed(*I, P); // Mark *I as used by P
532 // Erase all analyses not in the preserved set...
533 if (!AnUsage.getPreservesAll()) {
534 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
535 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(),
536 E = CurrentAnalyses.end(); I != E; ) {
537 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
538 PreservedSet.end()) { // Analysis not preserved!
539 CurrentAnalyses.erase(I); // Remove from available analyses
540 I = CurrentAnalyses.begin();
547 // Add this pass to the currently available set...
548 if (const PassInfo *PI = P->getPassInfo()) {
549 CurrentAnalyses[PI] = P;
551 // This pass is the current implementation of all of the interfaces it
552 // implements as well.
554 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
555 for (unsigned i = 0, e = II.size(); i != e; ++i)
556 CurrentAnalyses[II[i]] = P;
559 // For now assume that our results are never used...
563 // For FunctionPass subclasses, we must be sure to batch the FunctionPass's
564 // together in a BatcherClass object so that all of the analyses are run
565 // together a function at a time.
567 void addPass(SubPassClass *MP, AnalysisUsage &AnUsage) {
568 if (Batcher == 0) // If we don't have a batcher yet, make one now.
569 Batcher = new BatcherClass(this);
570 // The Batcher will queue the passes up
571 MP->addToPassManager(Batcher, AnUsage);
574 // closeBatcher - Terminate the batcher that is being worked on.
575 void closeBatcher() {
577 Passes.push_back(Batcher);
583 // When an ImmutablePass is added, it gets added to the top level pass
585 void addPass(ImmutablePass *IP, AnalysisUsage &AU) {
586 if (Parent) { // Make sure this request goes to the top level passmanager...
587 Parent->addPass(IP, AU);
591 // Set the Resolver instance variable in the Pass so that it knows where to
592 // find this object...
594 setAnalysisResolver(IP, this);
595 ImmutablePasses.push_back(IP);
597 // All Required analyses should be available to the pass as it initializes!
598 // Here we fill in the AnalysisImpls member of the pass so that it can
599 // successfully use the getAnalysis() method to retrieve the implementations
602 IP->AnalysisImpls.clear();
603 IP->AnalysisImpls.reserve(AU.getRequiredSet().size());
604 for (std::vector<const PassInfo *>::const_iterator
605 I = AU.getRequiredSet().begin(),
606 E = AU.getRequiredSet().end(); I != E; ++I) {
607 Pass *Impl = getAnalysisOrNullUp(*I);
609 std::cerr << "Analysis '" << (*I)->getPassName()
610 << "' used but not available!";
611 assert(0 && "Analysis used but not available!");
612 } else if (PassDebugging == Details) {
613 if ((*I)->getPassName() != std::string(Impl->getPassName()))
614 std::cerr << " Interface '" << (*I)->getPassName()
615 << "' implemented by '" << Impl->getPassName() << "'\n";
617 IP->AnalysisImpls.push_back(std::make_pair(*I, Impl));
620 // Initialize the immutable pass...
621 IP->initializePass();
627 //===----------------------------------------------------------------------===//
628 // PassManagerTraits<BasicBlock> Specialization
630 // This pass manager is used to group together all of the BasicBlockPass's
631 // into a single unit.
633 template<> class PassManagerTraits<BasicBlock> : public BasicBlockPass {
635 // PassClass - The type of passes tracked by this PassManager
636 typedef BasicBlockPass PassClass;
638 // SubPassClass - The types of classes that should be collated together
639 // This is impossible to match, so BasicBlock instantiations of PassManagerT
642 typedef PassManagerT<Module> SubPassClass;
644 // BatcherClass - The type to use for collation of subtypes... This class is
645 // never instantiated for the PassManager<BasicBlock>, but it must be an
646 // instance of PassClass to typecheck.
648 typedef PassClass BatcherClass;
650 // ParentClass - The type of the parent PassManager...
651 typedef PassManagerT<Function> ParentClass;
653 // PMType - The type of the passmanager that subclasses this class
654 typedef PassManagerT<BasicBlock> PMType;
656 // runPass - Specify how the pass should be run on the UnitType
657 static bool runPass(PassClass *P, BasicBlock *M) {
658 // todo, init and finalize
659 return P->runOnBasicBlock(*M);
662 // getPMName() - Return the name of the unit the PassManager operates on for
664 const char *getPMName() const { return "BasicBlock"; }
665 virtual const char *getPassName() const { return "BasicBlock Pass Manager"; }
667 // Implement the BasicBlockPass interface...
668 virtual bool doInitialization(Module &M);
669 virtual bool doInitialization(Function &F);
670 virtual bool runOnBasicBlock(BasicBlock &BB);
671 virtual bool doFinalization(Function &F);
672 virtual bool doFinalization(Module &M);
674 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
675 AU.setPreservesAll();
681 //===----------------------------------------------------------------------===//
682 // PassManagerTraits<Function> Specialization
684 // This pass manager is used to group together all of the FunctionPass's
685 // into a single unit.
687 template<> class PassManagerTraits<Function> : public FunctionPass {
689 // PassClass - The type of passes tracked by this PassManager
690 typedef FunctionPass PassClass;
692 // SubPassClass - The types of classes that should be collated together
693 typedef BasicBlockPass SubPassClass;
695 // BatcherClass - The type to use for collation of subtypes...
696 typedef PassManagerT<BasicBlock> BatcherClass;
698 // ParentClass - The type of the parent PassManager...
699 typedef PassManagerT<Module> ParentClass;
701 // PMType - The type of the passmanager that subclasses this class
702 typedef PassManagerT<Function> PMType;
704 // runPass - Specify how the pass should be run on the UnitType
705 static bool runPass(PassClass *P, Function *F) {
706 return P->runOnFunction(*F);
709 // getPMName() - Return the name of the unit the PassManager operates on for
711 const char *getPMName() const { return "Function"; }
712 virtual const char *getPassName() const { return "Function Pass Manager"; }
714 // Implement the FunctionPass interface...
715 virtual bool doInitialization(Module &M);
716 virtual bool runOnFunction(Function &F);
717 virtual bool doFinalization(Module &M);
719 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
720 AU.setPreservesAll();
726 //===----------------------------------------------------------------------===//
727 // PassManagerTraits<Module> Specialization
729 // This is the top level PassManager implementation that holds generic passes.
731 template<> class PassManagerTraits<Module> : public ModulePass {
733 // PassClass - The type of passes tracked by this PassManager
734 typedef ModulePass PassClass;
736 // SubPassClass - The types of classes that should be collated together
737 typedef FunctionPass SubPassClass;
739 // BatcherClass - The type to use for collation of subtypes...
740 typedef PassManagerT<Function> BatcherClass;
742 // ParentClass - The type of the parent PassManager...
743 typedef AnalysisResolver ParentClass;
745 // runPass - Specify how the pass should be run on the UnitType
746 static bool runPass(PassClass *P, Module *M) { return P->runOnModule(*M); }
748 // getPMName() - Return the name of the unit the PassManager operates on for
750 const char *getPMName() const { return "Module"; }
751 virtual const char *getPassName() const { return "Module Pass Manager"; }
753 // runOnModule - Implement the PassManager interface.
754 bool runOnModule(Module &M) {
755 return ((PassManagerT<Module>*)this)->runOnUnit(&M);
761 //===----------------------------------------------------------------------===//
762 // PassManagerTraits Method Implementations
765 // PassManagerTraits<BasicBlock> Implementations
767 inline bool PassManagerTraits<BasicBlock>::doInitialization(Module &M) {
768 bool Changed = false;
769 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
770 ((PMType*)this)->Passes[i]->doInitialization(M);
774 inline bool PassManagerTraits<BasicBlock>::doInitialization(Function &F) {
775 bool Changed = false;
776 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
777 ((PMType*)this)->Passes[i]->doInitialization(F);
781 inline bool PassManagerTraits<BasicBlock>::runOnBasicBlock(BasicBlock &BB) {
782 return ((PMType*)this)->runOnUnit(&BB);
785 inline bool PassManagerTraits<BasicBlock>::doFinalization(Function &F) {
786 bool Changed = false;
787 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
788 ((PMType*)this)->Passes[i]->doFinalization(F);
792 inline bool PassManagerTraits<BasicBlock>::doFinalization(Module &M) {
793 bool Changed = false;
794 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
795 ((PMType*)this)->Passes[i]->doFinalization(M);
800 // PassManagerTraits<Function> Implementations
802 inline bool PassManagerTraits<Function>::doInitialization(Module &M) {
803 bool Changed = false;
804 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
805 ((PMType*)this)->Passes[i]->doInitialization(M);
809 inline bool PassManagerTraits<Function>::runOnFunction(Function &F) {
810 return ((PMType*)this)->runOnUnit(&F);
813 inline bool PassManagerTraits<Function>::doFinalization(Module &M) {
814 bool Changed = false;
815 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
816 ((PMType*)this)->Passes[i]->doFinalization(M);
820 } // End llvm namespace