1 //===- PassManagerT.h - Container for Passes ---------------------*- C++ -*--=//
3 // This file defines the PassManagerT class. This class is used to hold,
4 // maintain, and optimize execution of Pass's. The PassManager class ensures
5 // that analysis results are available before a pass runs, and that Pass's are
6 // destroyed when the PassManager is destroyed.
8 // The PassManagerT template is instantiated three times to do its job. The
9 // public PassManager class is a Pimpl around the PassManagerT<Module> interface
10 // to avoid having all of the PassManager clients being exposed to the
11 // implementation details herein.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_PASSMANAGER_T_H
16 #define LLVM_PASSMANAGER_T_H
18 #include "llvm/Pass.h"
23 //===----------------------------------------------------------------------===//
24 // PMDebug class - a set of debugging functions, that are not to be
25 // instantiated by the template.
28 // If compiled in debug mode, these functions can be enabled by setting
29 // -debug-pass on the command line of the tool being used.
31 static void PrintPassStructure(Pass *P);
32 static void PrintPassInformation(unsigned,const char*,Pass *, Annotable *);
33 static void PrintAnalysisSetInfo(unsigned,const char*,Pass *P,
34 const std::vector<AnalysisID> &);
39 //===----------------------------------------------------------------------===//
40 // Declare the PassManagerTraits which will be specialized...
42 template<class UnitType> class PassManagerTraits; // Do not define.
45 //===----------------------------------------------------------------------===//
46 // PassManagerT - Container object for passes. The PassManagerT destructor
47 // deletes all passes contained inside of the PassManagerT, so you shouldn't
48 // delete passes manually, and all passes should be dynamically allocated.
50 template<typename UnitType>
51 class PassManagerT : public PassManagerTraits<UnitType>,public AnalysisResolver{
52 typedef typename PassManagerTraits<UnitType>::PassClass PassClass;
53 typedef typename PassManagerTraits<UnitType>::SubPassClass SubPassClass;
54 typedef typename PassManagerTraits<UnitType>::BatcherClass BatcherClass;
55 typedef typename PassManagerTraits<UnitType>::ParentClass ParentClass;
56 typedef PassManagerTraits<UnitType> Traits;
58 friend typename PassManagerTraits<UnitType>::PassClass;
59 friend typename PassManagerTraits<UnitType>::SubPassClass;
60 friend class PassManagerTraits<UnitType>;
62 std::vector<PassClass*> Passes; // List of pass's to run
64 // The parent of this pass manager...
65 ParentClass * const Parent;
67 // The current batcher if one is in use, or null
68 BatcherClass *Batcher;
70 // CurrentAnalyses - As the passes are being run, this map contains the
71 // analyses that are available to the current pass for use. This is accessed
72 // through the getAnalysis() function in this class and in Pass.
74 std::map<AnalysisID, Pass*> CurrentAnalyses;
76 // LastUseOf - This map keeps track of the last usage in our pipeline of a
77 // particular pass. When executing passes, the memory for .first is free'd
78 // after .second is run.
80 std::map<Pass*, Pass*> LastUseOf;
83 PassManagerT(ParentClass *Par = 0) : Parent(Par), Batcher(0) {}
85 // Delete all of the contained passes...
86 for (std::vector<PassClass*>::iterator I = Passes.begin(), E = Passes.end();
91 // run - Run all of the queued passes on the specified module in an optimal
93 virtual bool runOnUnit(UnitType *M) {
94 bool MadeChanges = false;
96 CurrentAnalyses.clear();
98 // LastUserOf - This contains the inverted LastUseOfMap...
99 std::map<Pass *, std::vector<Pass*> > LastUserOf;
100 for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(),
101 E = LastUseOf.end(); I != E; ++I)
102 LastUserOf[I->second].push_back(I->first);
105 // Output debug information...
106 if (Parent == 0) PMDebug::PrintPassStructure(this);
108 // Run all of the passes
109 for (unsigned i = 0, e = Passes.size(); i < e; ++i) {
110 PassClass *P = Passes[i];
112 PMDebug::PrintPassInformation(getDepth(), "Executing Pass", P,
115 // Get information about what analyses the pass uses...
116 AnalysisUsage AnUsage;
117 P->getAnalysisUsage(AnUsage);
118 PMDebug::PrintAnalysisSetInfo(getDepth(), "Required", P,
119 AnUsage.getRequiredSet());
122 // All Required analyses should be available to the pass as it runs!
123 for (vector<AnalysisID>::const_iterator
124 I = AnUsage.getRequiredSet().begin(),
125 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
126 assert(getAnalysisOrNullUp(*I) && "Analysis used but not available!");
131 bool Changed = Traits::runPass(P, M);
132 MadeChanges |= Changed;
135 PMDebug::PrintPassInformation(getDepth()+1, "Made Modification", P,
137 PMDebug::PrintAnalysisSetInfo(getDepth(), "Preserved", P,
138 AnUsage.getPreservedSet());
139 PMDebug::PrintAnalysisSetInfo(getDepth(), "Provided", P,
140 AnUsage.getProvidedSet());
143 // Erase all analyses not in the preserved set...
144 if (!AnUsage.preservesAll()) {
145 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
146 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(),
147 E = CurrentAnalyses.end(); I != E; )
148 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) !=
150 ++I; // This analysis is preserved, leave it in the available set...
152 #if MAP_DOESNT_HAVE_BROKEN_ERASE_MEMBER
153 I = CurrentAnalyses.erase(I); // Analysis not preserved!
155 // GCC 2.95.3 STL doesn't have correct erase member!
156 CurrentAnalyses.erase(I);
157 I = CurrentAnalyses.begin();
162 // Add all analyses in the provided set...
163 for (std::vector<AnalysisID>::const_iterator
164 I = AnUsage.getProvidedSet().begin(),
165 E = AnUsage.getProvidedSet().end(); I != E; ++I)
166 CurrentAnalyses[*I] = P;
168 // Free memory for any passes that we are the last use of...
169 std::vector<Pass*> &DeadPass = LastUserOf[P];
170 for (std::vector<Pass*>::iterator I = DeadPass.begin(),E = DeadPass.end();
172 PMDebug::PrintPassInformation(getDepth()+1, "Freeing Pass", *I,
174 (*I)->releaseMemory();
180 // dumpPassStructure - Implement the -debug-passes=PassStructure option
181 virtual void dumpPassStructure(unsigned Offset = 0) {
182 std::cerr << std::string(Offset*2, ' ') << Traits::getPMName()
183 << " Pass Manager\n";
184 for (std::vector<PassClass*>::iterator I = Passes.begin(), E = Passes.end();
187 P->dumpPassStructure(Offset+1);
189 // Loop through and see which classes are destroyed after this one...
190 for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(),
191 E = LastUseOf.end(); I != E; ++I) {
192 if (P == I->second) {
193 std::cerr << "Fr" << std::string(Offset*2, ' ');
194 I->first->dumpPassStructure(0);
200 Pass *getAnalysisOrNullDown(AnalysisID ID) const {
201 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID);
202 if (I == CurrentAnalyses.end()) {
204 return ((AnalysisResolver*)Batcher)->getAnalysisOrNullDown(ID);
210 Pass *getAnalysisOrNullUp(AnalysisID ID) const {
211 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID);
212 if (I == CurrentAnalyses.end()) {
214 return Parent->getAnalysisOrNullUp(ID);
220 // markPassUsed - Inform higher level pass managers (and ourselves)
221 // that these analyses are being used by this pass. This is used to
222 // make sure that analyses are not free'd before we have to use
225 void markPassUsed(AnalysisID P, Pass *User) {
226 std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.find(P);
227 if (I != CurrentAnalyses.end()) {
228 LastUseOf[I->second] = User; // Local pass, extend the lifetime
230 // Pass not in current available set, must be a higher level pass
231 // available to us, propogate to parent pass manager... We tell the
232 // parent that we (the passmanager) are using the analysis so that it
233 // frees the analysis AFTER this pass manager runs.
235 assert(Parent != 0 && "Pass available but not found! "
236 "Did your analysis pass 'Provide' itself?");
237 Parent->markPassUsed(P, this);
241 // Return the number of parent PassManagers that exist
242 virtual unsigned getDepth() const {
243 if (Parent == 0) return 0;
244 return 1 + Parent->getDepth();
247 // add - Add a pass to the queue of passes to run. This passes ownership of
248 // the Pass to the PassManager. When the PassManager is destroyed, the pass
249 // will be destroyed as well, so there is no need to delete the pass. This
250 // implies that all passes MUST be new'd.
252 void add(PassClass *P) {
253 // Get information about what analyses the pass uses...
254 AnalysisUsage AnUsage;
255 P->getAnalysisUsage(AnUsage);
256 const std::vector<AnalysisID> &Required = AnUsage.getRequiredSet();
258 // Loop over all of the analyses used by this pass,
259 for (std::vector<AnalysisID>::const_iterator I = Required.begin(),
260 E = Required.end(); I != E; ++I) {
261 if (getAnalysisOrNullDown(*I) == 0)
262 add((PassClass*)I->createPass());
265 // Tell the pass to add itself to this PassManager... the way it does so
266 // depends on the class of the pass, and is critical to laying out passes in
267 // an optimal order..
269 P->addToPassManager(this, AnUsage);
274 // addPass - These functions are used to implement the subclass specific
275 // behaviors present in PassManager. Basically the add(Pass*) method ends up
276 // reflecting its behavior into a Pass::addToPassManager call. Subclasses of
277 // Pass override it specifically so that they can reflect the type
278 // information inherent in "this" back to the PassManager.
280 // For generic Pass subclasses (which are interprocedural passes), we simply
281 // add the pass to the end of the pass list and terminate any accumulation of
282 // FunctionPass's that are present.
284 void addPass(PassClass *P, AnalysisUsage &AnUsage) {
285 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
286 const std::vector<AnalysisID> &ProvidedSet = AnUsage.getProvidedSet();
288 // Providers are analysis classes which are forbidden to modify the module
289 // they are operating on, so they are allowed to be reordered to before the
292 if (Batcher && ProvidedSet.empty())
293 closeBatcher(); // This pass cannot be batched!
295 // Set the Resolver instance variable in the Pass so that it knows where to
296 // find this object...
298 setAnalysisResolver(P, this);
301 // Inform higher level pass managers (and ourselves) that these analyses are
302 // being used by this pass. This is used to make sure that analyses are not
303 // free'd before we have to use them...
305 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
306 E = RequiredSet.end(); I != E; ++I)
307 markPassUsed(*I, P); // Mark *I as used by P
309 // Erase all analyses not in the preserved set...
310 if (!AnUsage.preservesAll()) {
311 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
312 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(),
313 E = CurrentAnalyses.end(); I != E; )
314 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) !=
316 ++I; // This analysis is preserved, leave it in the available set...
318 #if MAP_DOESNT_HAVE_BROKEN_ERASE_MEMBER
319 I = CurrentAnalyses.erase(I); // Analysis not preserved!
321 CurrentAnalyses.erase(I);// GCC 2.95.3 STL doesn't have correct erase!
322 I = CurrentAnalyses.begin();
327 // Add all analyses in the provided set...
328 for (std::vector<AnalysisID>::const_iterator I = ProvidedSet.begin(),
329 E = ProvidedSet.end(); I != E; ++I)
330 CurrentAnalyses[*I] = P;
332 // For now assume that our results are never used...
336 // For FunctionPass subclasses, we must be sure to batch the FunctionPass's
337 // together in a BatcherClass object so that all of the analyses are run
338 // together a function at a time.
340 void addPass(SubPassClass *MP, AnalysisUsage &AnUsage) {
341 if (Batcher == 0) // If we don't have a batcher yet, make one now.
342 Batcher = new BatcherClass(this);
343 // The Batcher will queue them passes up
344 MP->addToPassManager(Batcher, AnUsage);
347 // closeBatcher - Terminate the batcher that is being worked on.
348 void closeBatcher() {
350 Passes.push_back(Batcher);
358 //===----------------------------------------------------------------------===//
359 // PassManagerTraits<BasicBlock> Specialization
361 // This pass manager is used to group together all of the BasicBlockPass's
362 // into a single unit.
364 template<> struct PassManagerTraits<BasicBlock> : public BasicBlockPass {
365 // PassClass - The type of passes tracked by this PassManager
366 typedef BasicBlockPass PassClass;
368 // SubPassClass - The types of classes that should be collated together
369 // This is impossible to match, so BasicBlock instantiations of PassManagerT
372 typedef PassManagerT<Module> SubPassClass;
374 // BatcherClass - The type to use for collation of subtypes... This class is
375 // never instantiated for the PassManager<BasicBlock>, but it must be an
376 // instance of PassClass to typecheck.
378 typedef PassClass BatcherClass;
380 // ParentClass - The type of the parent PassManager...
381 typedef PassManagerT<Function> ParentClass;
383 // PMType - The type of the passmanager that subclasses this class
384 typedef PassManagerT<BasicBlock> PMType;
386 // runPass - Specify how the pass should be run on the UnitType
387 static bool runPass(PassClass *P, BasicBlock *M) {
388 // todo, init and finalize
389 return P->runOnBasicBlock(M);
392 // getPMName() - Return the name of the unit the PassManager operates on for
394 const char *getPMName() const { return "BasicBlock"; }
396 // Implement the BasicBlockPass interface...
397 virtual bool doInitialization(Module *M);
398 virtual bool runOnBasicBlock(BasicBlock *BB);
399 virtual bool doFinalization(Module *M);
404 //===----------------------------------------------------------------------===//
405 // PassManagerTraits<Function> Specialization
407 // This pass manager is used to group together all of the FunctionPass's
408 // into a single unit.
410 template<> struct PassManagerTraits<Function> : public FunctionPass {
411 // PassClass - The type of passes tracked by this PassManager
412 typedef FunctionPass PassClass;
414 // SubPassClass - The types of classes that should be collated together
415 typedef BasicBlockPass SubPassClass;
417 // BatcherClass - The type to use for collation of subtypes...
418 typedef PassManagerT<BasicBlock> BatcherClass;
420 // ParentClass - The type of the parent PassManager...
421 typedef PassManagerT<Module> ParentClass;
423 // PMType - The type of the passmanager that subclasses this class
424 typedef PassManagerT<Function> PMType;
426 // runPass - Specify how the pass should be run on the UnitType
427 static bool runPass(PassClass *P, Function *F) {
428 return P->runOnFunction(F);
431 // getPMName() - Return the name of the unit the PassManager operates on for
433 const char *getPMName() const { return "Function"; }
435 // Implement the FunctionPass interface...
436 virtual bool doInitialization(Module *M);
437 virtual bool runOnFunction(Function *F);
438 virtual bool doFinalization(Module *M);
443 //===----------------------------------------------------------------------===//
444 // PassManagerTraits<Module> Specialization
446 // This is the top level PassManager implementation that holds generic passes.
448 template<> struct PassManagerTraits<Module> : public Pass {
449 // PassClass - The type of passes tracked by this PassManager
450 typedef Pass PassClass;
452 // SubPassClass - The types of classes that should be collated together
453 typedef FunctionPass SubPassClass;
455 // BatcherClass - The type to use for collation of subtypes...
456 typedef PassManagerT<Function> BatcherClass;
458 // ParentClass - The type of the parent PassManager...
459 typedef AnalysisResolver ParentClass;
461 // runPass - Specify how the pass should be run on the UnitType
462 static bool runPass(PassClass *P, Module *M) { return P->run(M); }
464 // getPMName() - Return the name of the unit the PassManager operates on for
466 const char *getPMName() const { return "Module"; }
468 // run - Implement the Pass interface...
469 bool run(Module *M) {
470 return ((PassManagerT<Module>*)this)->runOnUnit(M);
476 //===----------------------------------------------------------------------===//
477 // PassManagerTraits Method Implementations
480 // PassManagerTraits<BasicBlock> Implementations
482 inline bool PassManagerTraits<BasicBlock>::doInitialization(Module *M) {
483 bool Changed = false;
484 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
485 ((PMType*)this)->Passes[i]->doInitialization(M);
489 inline bool PassManagerTraits<BasicBlock>::runOnBasicBlock(BasicBlock *BB) {
490 return ((PMType*)this)->runOnUnit(BB);
493 inline bool PassManagerTraits<BasicBlock>::doFinalization(Module *M) {
494 bool Changed = false;
495 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
496 ((PMType*)this)->Passes[i]->doFinalization(M);
501 // PassManagerTraits<Function> Implementations
503 inline bool PassManagerTraits<Function>::doInitialization(Module *M) {
504 bool Changed = false;
505 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
506 ((PMType*)this)->Passes[i]->doInitialization(M);
510 inline bool PassManagerTraits<Function>::runOnFunction(Function *F) {
511 return ((PMType*)this)->runOnUnit(F);
514 inline bool PassManagerTraits<Function>::doFinalization(Module *M) {
515 bool Changed = false;
516 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
517 ((PMType*)this)->Passes[i]->doFinalization(M);