Reverting back to original 1.8 version so I can manually merge in patch.
[llvm-complete.git] / lib / VMCore / PassManagerT.h
blobde0fc46f7e40496ba6f58b9e7fd080ea8e334dcd
1 //===- PassManagerT.h - Container for Passes --------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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"
29 #include <algorithm>
30 #include <iostream>
32 namespace llvm {
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...
42 enum PassDebugLevel {
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"),
49 cl::values(
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"),
55 clEnumValEnd));
57 //===----------------------------------------------------------------------===//
58 // PMDebug class - a set of debugging functions, that are not to be
59 // instantiated by the template.
61 struct PMDebug {
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);
67 std::cerr << "\n";
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.
90 class TimingInfo {
91 std::map<Pass*, Timer> TimingData;
92 TimerGroup TG;
94 // Private ctor, must use 'create' member
95 TimingInfo() : TG("... Pass execution timing report ...") {}
96 public:
97 // TimingDtor - Print out information about timing information
98 ~TimingInfo() {
99 // Delete all of the timers...
100 TimingData.clear();
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 struct BBTraits {
127 typedef BasicBlock UnitType;
129 // PassClass - The type of passes tracked by this PassManager
130 typedef BasicBlockPass PassClass;
132 // SubPassClass - The types of classes that should be collated together
133 // This is impossible to match, so BasicBlock instantiations of PassManagerT
134 // do not collate.
136 typedef BasicBlockPassManager SubPassClass;
138 // BatcherClass - The type to use for collation of subtypes... This class is
139 // never instantiated for the BasicBlockPassManager, but it must be an
140 // instance of PassClass to typecheck.
142 typedef PassClass BatcherClass;
144 // ParentClass - The type of the parent PassManager...
145 typedef FunctionPassManagerT ParentClass;
147 // PMType - The type of this passmanager
148 typedef BasicBlockPassManager PMType;
151 struct FTraits {
152 typedef Function UnitType;
154 // PassClass - The type of passes tracked by this PassManager
155 typedef FunctionPass PassClass;
157 // SubPassClass - The types of classes that should be collated together
158 typedef BasicBlockPass SubPassClass;
160 // BatcherClass - The type to use for collation of subtypes...
161 typedef BasicBlockPassManager BatcherClass;
163 // ParentClass - The type of the parent PassManager...
164 typedef ModulePassManager ParentClass;
166 // PMType - The type of this passmanager
167 typedef FunctionPassManagerT PMType;
170 struct MTraits {
171 typedef Module UnitType;
173 // PassClass - The type of passes tracked by this PassManager
174 typedef ModulePass PassClass;
176 // SubPassClass - The types of classes that should be collated together
177 typedef FunctionPass SubPassClass;
179 // BatcherClass - The type to use for collation of subtypes...
180 typedef FunctionPassManagerT BatcherClass;
182 // ParentClass - The type of the parent PassManager...
183 typedef AnalysisResolver ParentClass;
185 // PMType - The type of this passmanager
186 typedef ModulePassManager PMType;
190 //===----------------------------------------------------------------------===//
191 // PassManagerT - Container object for passes. The PassManagerT destructor
192 // deletes all passes contained inside of the PassManagerT, so you shouldn't
193 // delete passes manually, and all passes should be dynamically allocated.
195 template<typename Trait> class PassManagerT : public AnalysisResolver {
197 typedef typename Trait::PassClass PassClass;
198 typedef typename Trait::UnitType UnitType;
199 typedef typename Trait::ParentClass ParentClass;
200 typedef typename Trait::SubPassClass SubPassClass;
201 typedef typename Trait::BatcherClass BatcherClass;
202 typedef typename Trait::PMType PMType;
204 friend class ModulePass;
205 friend class FunctionPass;
206 friend class BasicBlockPass;
208 friend class ImmutablePass;
210 friend class BasicBlockPassManager;
211 friend class FunctionPassManagerT;
212 friend class ModulePassManager;
214 std::vector<PassClass*> Passes; // List of passes to run
215 std::vector<ImmutablePass*> ImmutablePasses; // List of immutable passes
217 // The parent of this pass manager...
218 ParentClass * const Parent;
220 // The current batcher if one is in use, or null
221 BatcherClass *Batcher;
223 // CurrentAnalyses - As the passes are being run, this map contains the
224 // analyses that are available to the current pass for use. This is accessed
225 // through the getAnalysis() function in this class and in Pass.
227 std::map<AnalysisID, Pass*> CurrentAnalyses;
229 // LastUseOf - This map keeps track of the last usage in our pipeline of a
230 // particular pass. When executing passes, the memory for .first is free'd
231 // after .second is run.
233 std::map<Pass*, Pass*> LastUseOf;
235 public:
237 // getPMName() - Return the name of the unit the PassManager operates on for
238 // debugging.
239 virtual const char *getPMName() const =0;
241 virtual const char *getPassName() const =0;
243 virtual bool runPass(PassClass *P, UnitType *M) =0;
245 // TODO:Figure out what pure virtuals remain.
248 PassManagerT(ParentClass *Par = 0) : Parent(Par), Batcher(0) {}
249 virtual ~PassManagerT() {
250 // Delete all of the contained passes...
251 for (typename std::vector<PassClass*>::iterator
252 I = Passes.begin(), E = Passes.end(); I != E; ++I)
253 delete *I;
255 for (std::vector<ImmutablePass*>::iterator
256 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
257 delete *I;
260 // run - Run all of the queued passes on the specified module in an optimal
261 // way.
262 virtual bool runOnUnit(UnitType *M) {
263 closeBatcher();
264 CurrentAnalyses.clear();
266 TimingInfo::createTheTimeInfo();
268 addImmutablePasses();
270 // LastUserOf - This contains the inverted LastUseOfMap...
271 std::map<Pass *, std::vector<Pass*> > LastUserOf;
272 for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(),
273 E = LastUseOf.end(); I != E; ++I)
274 LastUserOf[I->second].push_back(I->first);
276 // Output debug information...
277 assert(dynamic_cast<PassClass*>(this) &&
278 "It wasn't the PassClass I thought it was");
279 if (Parent == 0)
280 PMDebug::PerformPassStartupStuff((dynamic_cast<PMType*>(this)));
282 return runPasses(M, LastUserOf);
285 // dumpPassStructure - Implement the -debug-passes=PassStructure option
286 inline void dumpPassStructure(unsigned Offset = 0) {
287 // Print out the immutable passes...
289 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i)
290 ImmutablePasses[i]->dumpPassStructure(0);
292 std::cerr << std::string(Offset*2, ' ') << this->getPMName()
293 << " Pass Manager\n";
294 for (typename std::vector<PassClass*>::iterator
295 I = Passes.begin(), E = Passes.end(); I != E; ++I) {
296 PassClass *P = *I;
297 P->dumpPassStructure(Offset+1);
299 // Loop through and see which classes are destroyed after this one...
300 for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(),
301 E = LastUseOf.end(); I != E; ++I) {
302 if (P == I->second) {
303 std::cerr << "--" << std::string(Offset*2, ' ');
304 I->first->dumpPassStructure(0);
310 Pass *getImmutablePassOrNull(const PassInfo *ID) const {
311 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
312 const PassInfo *IPID = ImmutablePasses[i]->getPassInfo();
313 if (IPID == ID)
314 return ImmutablePasses[i];
316 // This pass is the current implementation of all of the interfaces it
317 // implements as well.
319 const std::vector<const PassInfo*> &II =
320 IPID->getInterfacesImplemented();
321 for (unsigned j = 0, e = II.size(); j != e; ++j)
322 if (II[j] == ID) return ImmutablePasses[i];
324 return 0;
327 Pass *getAnalysisOrNullDown(const PassInfo *ID) const {
328 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID);
330 if (I != CurrentAnalyses.end())
331 return I->second; // Found it.
333 if (Pass *P = getImmutablePassOrNull(ID))
334 return P;
336 if (Batcher)
337 return ((AnalysisResolver*)Batcher)->getAnalysisOrNullDown(ID);
338 return 0;
341 Pass *getAnalysisOrNullUp(const PassInfo *ID) const {
342 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID);
343 if (I != CurrentAnalyses.end())
344 return I->second; // Found it.
346 if (Parent) // Try scanning...
347 return Parent->getAnalysisOrNullUp(ID);
348 else if (!ImmutablePasses.empty())
349 return getImmutablePassOrNull(ID);
350 return 0;
353 // markPassUsed - Inform higher level pass managers (and ourselves)
354 // that these analyses are being used by this pass. This is used to
355 // make sure that analyses are not free'd before we have to use
356 // them...
358 void markPassUsed(const PassInfo *P, Pass *User) {
359 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(P);
361 if (I != CurrentAnalyses.end()) {
362 LastUseOf[I->second] = User; // Local pass, extend the lifetime
364 // Prolong live range of analyses that are needed after an analysis pass
365 // is destroyed, for querying by subsequent passes
366 AnalysisUsage AnUsage;
367 I->second->getAnalysisUsage(AnUsage);
368 const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
369 for (std::vector<AnalysisID>::const_iterator i = IDs.begin(),
370 e = IDs.end(); i != e; ++i)
371 markPassUsed(*i, User);
373 } else {
374 // Pass not in current available set, must be a higher level pass
375 // available to us, propagate to parent pass manager... We tell the
376 // parent that we (the passmanager) are using the analysis so that it
377 // frees the analysis AFTER this pass manager runs.
379 if (Parent) {
380 assert(dynamic_cast<Pass*>(this) &&
381 "It wasn't the Pass type I thought it was.");
382 Parent->markPassUsed(P, dynamic_cast<Pass*>(this));
383 } else {
384 assert(getAnalysisOrNullUp(P) &&
385 dynamic_cast<ImmutablePass*>(getAnalysisOrNullUp(P)) &&
386 "Pass available but not found! "
387 "Perhaps this is a module pass requiring a function pass?");
392 // Return the number of parent PassManagers that exist
393 virtual unsigned getDepth() const {
394 if (Parent == 0) return 0;
395 return 1 + Parent->getDepth();
398 virtual unsigned getNumContainedPasses() const { return Passes.size(); }
400 virtual const Pass *getContainedPass(unsigned N) const {
401 assert(N < Passes.size() && "Pass number out of range!");
402 return Passes[N];
405 // add - Add a pass to the queue of passes to run. This gives ownership of
406 // the Pass to the PassManager. When the PassManager is destroyed, the pass
407 // will be destroyed as well, so there is no need to delete the pass. This
408 // implies that all passes MUST be new'd.
410 void add(PassClass *P) {
411 // Get information about what analyses the pass uses...
412 AnalysisUsage AnUsage;
413 P->getAnalysisUsage(AnUsage);
415 addRequiredPasses(AnUsage.getRequiredSet());
417 // Tell the pass to add itself to this PassManager... the way it does so
418 // depends on the class of the pass, and is critical to laying out passes in
419 // an optimal order..
421 assert(dynamic_cast<PMType*>(this) &&
422 "It wasn't the right passmanager type.");
423 P->addToPassManager(static_cast<PMType*>(this), AnUsage);
426 // add - H4x0r an ImmutablePass into a PassManager that might not be
427 // expecting one.
429 void add(ImmutablePass *P) {
430 // Get information about what analyses the pass uses...
431 AnalysisUsage AnUsage;
432 P->getAnalysisUsage(AnUsage);
434 addRequiredPasses(AnUsage.getRequiredSet());
436 // Add the ImmutablePass to this PassManager.
437 addPass(P, AnUsage);
440 private:
441 // addPass - These functions are used to implement the subclass specific
442 // behaviors present in PassManager. Basically the add(Pass*) method ends up
443 // reflecting its behavior into a Pass::addToPassManager call. Subclasses of
444 // Pass override it specifically so that they can reflect the type
445 // information inherent in "this" back to the PassManager.
447 // For generic Pass subclasses (which are interprocedural passes), we simply
448 // add the pass to the end of the pass list and terminate any accumulation of
449 // FunctionPass's that are present.
451 void addPass(PassClass *P, AnalysisUsage &AnUsage) {
452 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
454 // FIXME: If this pass being added isn't killed by any of the passes in the
455 // batcher class then we can reorder the pass to execute before the batcher
456 // does, which will potentially allow us to batch more passes!
458 if (Batcher)
459 closeBatcher(); // This pass cannot be batched!
461 // Set the Resolver instance variable in the Pass so that it knows where to
462 // find this object...
464 setAnalysisResolver(P, this);
465 Passes.push_back(P);
467 // Inform higher level pass managers (and ourselves) that these analyses are
468 // being used by this pass. This is used to make sure that analyses are not
469 // free'd before we have to use them...
471 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
472 E = RequiredSet.end(); I != E; ++I)
473 markPassUsed(*I, P); // Mark *I as used by P
475 removeNonPreservedAnalyses(AnUsage);
477 makeCurrentlyAvailable(P);
479 // For now assume that our results are never used...
480 LastUseOf[P] = P;
483 // For FunctionPass subclasses, we must be sure to batch the FunctionPass's
484 // together in a BatcherClass object so that all of the analyses are run
485 // together a function at a time.
487 void addPass(SubPassClass *MP, AnalysisUsage &AnUsage) {
489 if (Batcher == 0) { // If we don't have a batcher yet, make one now.
490 assert(dynamic_cast<PMType*>(this) &&
491 "It wasn't the PassManager type I thought it was");
492 Batcher = new BatcherClass((static_cast<PMType*>(this)));
495 // The Batcher will queue the passes up
496 MP->addToPassManager(Batcher, AnUsage);
499 // closeBatcher - Terminate the batcher that is being worked on.
500 void closeBatcher() {
501 if (Batcher) {
502 Passes.push_back(Batcher);
503 Batcher = 0;
507 void addRequiredPasses(const std::vector<AnalysisID> &Required) {
508 for (std::vector<AnalysisID>::const_iterator I = Required.begin(),
509 E = Required.end(); I != E; ++I) {
510 if (getAnalysisOrNullDown(*I) == 0) {
511 Pass *AP = (*I)->createPass();
512 if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (AP)) add(IP);
513 else if (PassClass *RP = dynamic_cast<PassClass *> (AP)) add(RP);
514 else assert (0 && "Wrong kind of pass for this PassManager");
519 public:
520 // When an ImmutablePass is added, it gets added to the top level pass
521 // manager.
522 void addPass(ImmutablePass *IP, AnalysisUsage &AU) {
523 if (Parent) { // Make sure this request goes to the top level passmanager...
524 Parent->addPass(IP, AU);
525 return;
528 // Set the Resolver instance variable in the Pass so that it knows where to
529 // find this object...
531 setAnalysisResolver(IP, this);
532 ImmutablePasses.push_back(IP);
534 // All Required analyses should be available to the pass as it initializes!
535 // Here we fill in the AnalysisImpls member of the pass so that it can
536 // successfully use the getAnalysis() method to retrieve the implementations
537 // it needs.
539 IP->AnalysisImpls.clear();
540 IP->AnalysisImpls.reserve(AU.getRequiredSet().size());
541 for (std::vector<const PassInfo *>::const_iterator
542 I = AU.getRequiredSet().begin(),
543 E = AU.getRequiredSet().end(); I != E; ++I) {
544 Pass *Impl = getAnalysisOrNullUp(*I);
545 if (Impl == 0) {
546 std::cerr << "Analysis '" << (*I)->getPassName()
547 << "' used but not available!";
548 assert(0 && "Analysis used but not available!");
549 } else if (PassDebugging == Details) {
550 if ((*I)->getPassName() != std::string(Impl->getPassName()))
551 std::cerr << " Interface '" << (*I)->getPassName()
552 << "' implemented by '" << Impl->getPassName() << "'\n";
554 IP->AnalysisImpls.push_back(std::make_pair(*I, Impl));
557 // Initialize the immutable pass...
558 IP->initializePass();
560 private:
562 // Add any immutable passes to the CurrentAnalyses set...
563 inline void addImmutablePasses() {
564 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
565 ImmutablePass *IPass = ImmutablePasses[i];
566 if (const PassInfo *PI = IPass->getPassInfo()) {
567 CurrentAnalyses[PI] = IPass;
569 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
570 for (unsigned i = 0, e = II.size(); i != e; ++i)
571 CurrentAnalyses[II[i]] = IPass;
576 // Run all of the passes
577 inline bool runPasses(UnitType *M,
578 std::map<Pass *, std::vector<Pass*> > &LastUserOf) {
579 bool MadeChanges = false;
581 for (unsigned i = 0, e = Passes.size(); i < e; ++i) {
582 PassClass *P = Passes[i];
584 PMDebug::PrintPassInformation(getDepth(), "Executing Pass", P, M);
586 // Get information about what analyses the pass uses...
587 AnalysisUsage AnUsage;
588 P->getAnalysisUsage(AnUsage);
589 PMDebug::PrintAnalysisSetInfo(getDepth(), "Required", P,
590 AnUsage.getRequiredSet());
592 initialiseAnalysisImpl(P, AnUsage);
594 // Run the sub pass!
595 if (TheTimeInfo) TheTimeInfo->passStarted(P);
596 bool Changed = runPass(P, M);
597 if (TheTimeInfo) TheTimeInfo->passEnded(P);
598 MadeChanges |= Changed;
600 // Check for memory leaks by the pass...
601 LeakDetector::checkForGarbage(std::string("after running pass '") +
602 P->getPassName() + "'");
604 if (Changed)
605 PMDebug::PrintPassInformation(getDepth()+1, "Made Modification", P, M);
606 PMDebug::PrintAnalysisSetInfo(getDepth(), "Preserved", P,
607 AnUsage.getPreservedSet());
609 // Erase all analyses not in the preserved set
610 removeNonPreservedAnalyses(AnUsage);
612 makeCurrentlyAvailable(P);
614 // free memory and remove dead passes from the CurrentAnalyses list...
615 removeDeadPasses(P, M, LastUserOf);
618 return MadeChanges;
621 // All Required analyses should be available to the pass as it runs! Here
622 // we fill in the AnalysisImpls member of the pass so that it can
623 // successfully use the getAnalysis() method to retrieve the
624 // implementations it needs.
626 inline void initialiseAnalysisImpl(PassClass *P, AnalysisUsage &AnUsage) {
627 P->AnalysisImpls.clear();
628 P->AnalysisImpls.reserve(AnUsage.getRequiredSet().size());
630 for (std::vector<const PassInfo *>::const_iterator
631 I = AnUsage.getRequiredSet().begin(),
632 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
633 Pass *Impl = getAnalysisOrNullUp(*I);
634 if (Impl == 0) {
635 std::cerr << "Analysis '" << (*I)->getPassName()
636 << "' used but not available!";
637 assert(0 && "Analysis used but not available!");
638 } else if (PassDebugging == Details) {
639 if ((*I)->getPassName() != std::string(Impl->getPassName()))
640 std::cerr << " Interface '" << (*I)->getPassName()
641 << "' implemented by '" << Impl->getPassName() << "'\n";
644 P->AnalysisImpls.push_back(std::make_pair(*I, Impl));
648 inline void removeNonPreservedAnalyses(AnalysisUsage &AnUsage) {
649 if (!AnUsage.getPreservesAll()) {
650 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
651 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(),
652 E = CurrentAnalyses.end(); I != E; )
653 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) !=
654 PreservedSet.end())
655 ++I; // This analysis is preserved, leave it in the available set...
656 else {
657 if (!dynamic_cast<ImmutablePass*>(I->second)) {
658 std::map<AnalysisID, Pass*>::iterator J = I++;
659 CurrentAnalyses.erase(J); // Analysis not preserved!
660 } else {
661 ++I;
667 inline void removeDeadPasses(Pass* P, UnitType *M,
668 std::map<Pass *, std::vector<Pass*> > &LastUserOf) {
669 std::vector<Pass*> &DeadPass = LastUserOf[P];
670 for (std::vector<Pass*>::iterator I = DeadPass.begin(),E = DeadPass.end();
671 I != E; ++I) {
672 PMDebug::PrintPassInformation(getDepth()+1, "Freeing Pass", *I, M);
673 (*I)->releaseMemory();
676 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin();
677 I != CurrentAnalyses.end(); ) {
678 std::vector<Pass*>::iterator DPI = std::find(DeadPass.begin(),
679 DeadPass.end(), I->second);
680 if (DPI != DeadPass.end()) { // This pass is dead now... remove it
681 std::map<AnalysisID, Pass*>::iterator IDead = I++;
682 CurrentAnalyses.erase(IDead);
683 } else {
684 ++I; // Move on to the next element...
689 inline void makeCurrentlyAvailable(Pass* P) {
690 if (const PassInfo *PI = P->getPassInfo()) {
691 CurrentAnalyses[PI] = P;
693 // This pass is the current implementation of all of the interfaces it
694 // implements as well.
696 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
697 for (unsigned i = 0, e = II.size(); i != e; ++i)
698 CurrentAnalyses[II[i]] = P;
705 //===----------------------------------------------------------------------===//
706 // BasicBlockPassManager
708 // This pass manager is used to group together all of the BasicBlockPass's
709 // into a single unit.
711 class BasicBlockPassManager : public BasicBlockPass,
712 public BBTraits,
713 public PassManagerT<BBTraits> {
714 public:
715 BasicBlockPassManager(BBTraits::ParentClass* PC) :
716 PassManagerT<BBTraits>(PC) {
719 BasicBlockPassManager(BasicBlockPassManager* BBPM) :
720 PassManagerT<BBTraits>(BBPM->Parent) {
723 // runPass - Specify how the pass should be run on the UnitType
724 virtual bool runPass(BBTraits::PassClass *P, BasicBlock *M) {
725 // TODO: init and finalize
726 return P->runOnBasicBlock(*M);
729 virtual ~BasicBlockPassManager() {}
731 virtual void dumpPassStructure(unsigned Offset = 0) {
732 PassManagerT<BBTraits>::dumpPassStructure(Offset);
735 // getPMName() - Return the name of the unit the PassManager operates on for
736 // debugging.
737 virtual const char *getPMName() const { return "BasicBlock"; }
739 virtual const char *getPassName() const { return "BasicBlock Pass Manager"; }
741 virtual bool doInitialization(Module &M);
742 virtual bool doInitialization(Function &F);
743 virtual bool runOnBasicBlock(BasicBlock &BB);
744 virtual bool doFinalization(Function &F);
745 virtual bool doFinalization(Module &M);
747 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
748 AU.setPreservesAll();
752 //===----------------------------------------------------------------------===//
753 // FunctionPassManager
755 // This pass manager is used to group together all of the FunctionPass's
756 // into a single unit.
758 class FunctionPassManagerT : public FunctionPass,
759 public FTraits,
760 public PassManagerT<FTraits> {
761 public:
762 FunctionPassManagerT() : PassManagerT<FTraits>(0) {}
764 // Parent constructor
765 FunctionPassManagerT(FTraits::ParentClass* PC) : PassManagerT<FTraits>(PC) {}
767 FunctionPassManagerT(FunctionPassManagerT* FPM) :
768 PassManagerT<FTraits>(FPM->Parent) {
771 virtual ~FunctionPassManagerT() {}
773 virtual void dumpPassStructure(unsigned Offset = 0) {
774 PassManagerT<FTraits>::dumpPassStructure(Offset);
777 // getPMName() - Return the name of the unit the PassManager operates on for
778 // debugging.
779 virtual const char *getPMName() const { return "Function"; }
781 virtual const char *getPassName() const { return "Function Pass Manager"; }
783 virtual bool runOnFunction(Function &F);
785 virtual bool doInitialization(Module &M);
787 virtual bool doFinalization(Module &M);
789 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
790 AU.setPreservesAll();
793 // runPass - Specify how the pass should be run on the UnitType
794 virtual bool runPass(FTraits::PassClass *P, Function *F) {
795 return P->runOnFunction(*F);
800 //===----------------------------------------------------------------------===//
801 // ModulePassManager
803 // This is the top level PassManager implementation that holds generic passes.
805 class ModulePassManager : public ModulePass,
806 public MTraits,
807 public PassManagerT<MTraits> {
808 public:
809 ModulePassManager() : PassManagerT<MTraits>(0) {}
811 // Batcher Constructor
812 ModulePassManager(MTraits::ParentClass* PC) : PassManagerT<MTraits>(PC) {}
814 ModulePassManager(ModulePassManager* MPM) :
815 PassManagerT<MTraits>((MPM->Parent)) {
818 virtual ~ModulePassManager() {}
820 virtual void dumpPassStructure(unsigned Offset = 0) {
821 PassManagerT<MTraits>::dumpPassStructure(Offset);
824 // getPMName() - Return the name of the unit the PassManager operates on for
825 // debugging.
826 virtual const char *getPassName() const { return "Module Pass Manager"; }
828 // getPMName() - Return the name of the unit the PassManager operates on for
829 // debugging.
830 virtual const char *getPMName() const { return "Module"; }
832 // runOnModule - Implement the PassManager interface.
833 virtual bool runOnModule(Module &M);
835 // runPass - Specify how the pass should be run on the UnitType
836 virtual bool runPass(MTraits::PassClass *P, Module *M) {
837 return P->runOnModule(*M);
841 //===----------------------------------------------------------------------===//
842 // PassManager Method Implementations
845 // BasicBlockPassManager Implementations
848 inline bool BasicBlockPassManager::runOnBasicBlock(BasicBlock &BB) {
849 return ((BBTraits::PMType*)this)->runOnUnit(&BB);
852 inline bool BasicBlockPassManager::doInitialization(Module &M) {
853 bool Changed = false;
854 for (unsigned i = 0, e =((BBTraits::PMType*)this)->Passes.size(); i != e; ++i)
855 ((BBTraits::PMType*)this)->Passes[i]->doInitialization(M);
856 return Changed;
859 inline bool BasicBlockPassManager::doInitialization(Function &F) {
860 bool Changed = false;
861 for (unsigned i = 0, e =((BBTraits::PMType*)this)->Passes.size(); i != e; ++i)
862 ((BBTraits::PMType*)this)->Passes[i]->doInitialization(F);
863 return Changed;
866 inline bool BasicBlockPassManager::doFinalization(Function &F) {
867 bool Changed = false;
868 for (unsigned i = 0, e =((BBTraits::PMType*)this)->Passes.size(); i != e; ++i)
869 ((BBTraits::PMType*)this)->Passes[i]->doFinalization(F);
870 return Changed;
873 inline bool BasicBlockPassManager::doFinalization(Module &M) {
874 bool Changed = false;
875 for (unsigned i=0, e = ((BBTraits::PMType*)this)->Passes.size(); i != e; ++i)
876 ((BBTraits::PMType*)this)->Passes[i]->doFinalization(M);
877 return Changed;
880 // FunctionPassManagerT Implementations
883 inline bool FunctionPassManagerT::runOnFunction(Function &F) {
884 return ((FTraits::PMType*)this)->runOnUnit(&F);
887 inline bool FunctionPassManagerT::doInitialization(Module &M) {
888 bool Changed = false;
889 for (unsigned i=0, e = ((FTraits::PMType*)this)->Passes.size(); i != e; ++i)
890 ((FTraits::PMType*)this)->Passes[i]->doInitialization(M);
891 return Changed;
894 inline bool FunctionPassManagerT::doFinalization(Module &M) {
895 bool Changed = false;
896 for (unsigned i=0, e = ((FTraits::PMType*)this)->Passes.size(); i != e; ++i)
897 ((FTraits::PMType*)this)->Passes[i]->doFinalization(M);
898 return Changed;
901 // ModulePassManager Implementations
904 bool ModulePassManager::runOnModule(Module &M) {
905 return ((PassManagerT<MTraits>*)this)->runOnUnit(&M);
908 } // End llvm namespace
910 #endif