1 //===- llvm/PassManager.h - Container for Passes -----------------*- C++ -*--=//
3 // This file defines the PassManager 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.
10 //===----------------------------------------------------------------------===//
12 #ifndef LLVM_PASSMANAGER_H
13 #define LLVM_PASSMANAGER_H
15 #include "llvm/Pass.h"
18 // PassManager - Top level PassManagerT instantiation intended to be used.
19 typedef PassManagerT<Module> PassManager;
22 //===----------------------------------------------------------------------===//
23 // PMDebug class - a set of debugging functions that are enabled when compiling
24 // with -g on. If compiling at -O, all functions are inlined noops.
28 inline static void PrintPassStructure(Pass *) {}
29 inline static void PrintPassInformation(unsigned,const char*,Pass*,Value*) {}
30 inline static void PrintAnalysisSetInfo(unsigned,const char*,
31 const Pass::AnalysisSet &) {}
33 // If compiled in debug mode, these functions can be enabled by setting
34 // -debug-pass on the command line of the tool being used.
36 static void PrintPassStructure(Pass *P);
37 static void PrintPassInformation(unsigned,const char*,Pass *, Value *);
38 static void PrintAnalysisSetInfo(unsigned,const char*,const Pass::AnalysisSet&);
44 //===----------------------------------------------------------------------===//
45 // Declare the PassManagerTraits which will be specialized...
47 template<class UnitType> class PassManagerTraits; // Do not define.
50 //===----------------------------------------------------------------------===//
51 // PassManagerT - Container object for passes. The PassManagerT destructor
52 // deletes all passes contained inside of the PassManagerT, so you shouldn't
53 // delete passes manually, and all passes should be dynamically allocated.
55 template<typename UnitType>
56 class PassManagerT : public PassManagerTraits<UnitType>,public AnalysisResolver{
57 typedef typename PassManagerTraits<UnitType>::PassClass PassClass;
58 typedef typename PassManagerTraits<UnitType>::SubPassClass SubPassClass;
59 typedef typename PassManagerTraits<UnitType>::BatcherClass BatcherClass;
60 typedef typename PassManagerTraits<UnitType>::ParentClass ParentClass;
61 typedef PassManagerTraits<UnitType> Traits;
63 friend typename PassManagerTraits<UnitType>::PassClass;
64 friend typename PassManagerTraits<UnitType>::SubPassClass;
65 friend class PassManagerTraits<UnitType>;
67 std::vector<PassClass*> Passes; // List of pass's to run
69 // The parent of this pass manager...
70 const ParentClass *Parent;
72 // The current batcher if one is in use, or null
73 BatcherClass *Batcher;
75 // CurrentAnalyses - As the passes are being run, this map contains the
76 // analyses that are available to the current pass for use. This is accessed
77 // through the getAnalysis() function in this class and in Pass.
79 std::map<AnalysisID, Pass*> CurrentAnalyses;
82 PassManagerT(ParentClass *Par = 0) : Parent(Par), Batcher(0) {}
84 // Delete all of the contained passes...
85 for (std::vector<PassClass*>::iterator I = Passes.begin(), E = Passes.end();
90 // run - Run all of the queued passes on the specified module in an optimal
92 virtual bool runOnUnit(UnitType *M) {
93 bool MadeChanges = false;
95 CurrentAnalyses.clear();
97 // Output debug information...
98 if (Parent == 0) PMDebug::PrintPassStructure(this);
100 // Run all of the passes
101 for (unsigned i = 0, e = Passes.size(); i < e; ++i) {
102 PassClass *P = Passes[i];
104 PMDebug::PrintPassInformation(getDepth(), "Executing Pass", P, (Value*)M);
106 // Get information about what analyses the pass uses...
107 std::vector<AnalysisID> Required, Destroyed, Provided;
108 P->getAnalysisUsageInfo(Required, Destroyed, Provided);
110 PMDebug::PrintAnalysisSetInfo(getDepth(), "Required", Required);
113 // All Required analyses should be available to the pass as it runs!
114 for (Pass::AnalysisSet::iterator I = Required.begin(),
115 E = Required.end(); I != E; ++I) {
116 assert(getAnalysisOrNullUp(*I) && "Analysis used but not available!");
121 MadeChanges |= Traits::runPass(P, M);
123 PMDebug::PrintAnalysisSetInfo(getDepth(), "Destroyed", Destroyed);
124 PMDebug::PrintAnalysisSetInfo(getDepth(), "Provided", Provided);
126 // Erase all analyses in the destroyed set...
127 for (Pass::AnalysisSet::iterator I = Destroyed.begin(),
128 E = Destroyed.end(); I != E; ++I)
129 CurrentAnalyses.erase(*I);
131 // Add all analyses in the provided set...
132 for (Pass::AnalysisSet::iterator I = Provided.begin(),
133 E = Provided.end(); I != E; ++I)
134 CurrentAnalyses[*I] = P;
139 // add - Add a pass to the queue of passes to run. This passes ownership of
140 // the Pass to the PassManager. When the PassManager is destroyed, the pass
141 // will be destroyed as well, so there is no need to delete the pass. Also,
142 // all passes MUST be new'd.
144 void add(PassClass *P) {
145 // Get information about what analyses the pass uses...
146 std::vector<AnalysisID> Required, Destroyed, Provided;
147 P->getAnalysisUsageInfo(Required, Destroyed, Provided);
149 // Loop over all of the analyses used by this pass,
150 for (std::vector<AnalysisID>::iterator I = Required.begin(),
151 E = Required.end(); I != E; ++I) {
152 if (getAnalysisOrNullDown(*I) == 0)
153 add(I->createPass());
156 // Tell the pass to add itself to this PassManager... the way it does so
157 // depends on the class of the pass, and is critical to laying out passes in
158 // an optimal order..
160 P->addToPassManager(this, Destroyed, Provided);
164 // dumpPassStructure - Implement the -debug-passes=PassStructure option
165 virtual void dumpPassStructure(unsigned Offset = 0) {
166 std::cerr << std::string(Offset*2, ' ') << "Pass Manager\n";
167 for (std::vector<PassClass*>::iterator I = Passes.begin(), E = Passes.end();
169 (*I)->dumpPassStructure(Offset+1);
174 Pass *getAnalysisOrNullDown(AnalysisID ID) {
175 std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.find(ID);
176 if (I == CurrentAnalyses.end()) {
178 return ((AnalysisResolver*)Batcher)->getAnalysisOrNullDown(ID);
184 Pass *getAnalysisOrNullUp(AnalysisID ID) {
185 std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.find(ID);
186 if (I == CurrentAnalyses.end()) {
188 return ((AnalysisResolver*)Parent)->getAnalysisOrNullUp(ID);
194 virtual unsigned getDepth() const {
195 if (Parent == 0) return 0;
196 return 1 + ((AnalysisResolver*)Parent)->getDepth();
201 // addPass - These functions are used to implement the subclass specific
202 // behaviors present in PassManager. Basically the add(Pass*) method ends up
203 // reflecting its behavior into a Pass::addToPassManager call. Subclasses of
204 // Pass override it specifically so that they can reflect the type
205 // information inherent in "this" back to the PassManager.
207 // For generic Pass subclasses (which are interprocedural passes), we simply
208 // add the pass to the end of the pass list and terminate any accumulation of
209 // MethodPasses that are present.
211 void addPass(PassClass *P, Pass::AnalysisSet &Destroyed,
212 Pass::AnalysisSet &Provided) {
213 // Providers are analysis classes which are forbidden to modify the module
214 // they are operating on, so they are allowed to be reordered to before the
217 if (Batcher && Provided.empty())
218 closeBatcher(); // This pass cannot be batched!
220 // Set the Resolver instance variable in the Pass so that it knows where to
221 // find this object...
223 setAnalysisResolver(P, this);
226 // Erase all analyses in the destroyed set...
227 for (std::vector<AnalysisID>::iterator I = Destroyed.begin(),
228 E = Destroyed.end(); I != E; ++I)
229 CurrentAnalyses.erase(*I);
231 // Add all analyses in the provided set...
232 for (std::vector<AnalysisID>::iterator I = Provided.begin(),
233 E = Provided.end(); I != E; ++I)
234 CurrentAnalyses[*I] = P;
237 // For MethodPass subclasses, we must be sure to batch the MethodPasses
238 // together in a MethodPassBatcher object so that all of the analyses are run
239 // together a method at a time.
241 void addPass(SubPassClass *MP, Pass::AnalysisSet &Destroyed,
242 Pass::AnalysisSet &Provided) {
243 if (Batcher == 0) // If we don't have a batcher yet, make one now.
244 Batcher = new BatcherClass(this);
245 // The Batcher will queue them passes up
246 MP->addToPassManager(Batcher, Destroyed, Provided);
249 // closeBatcher - Terminate the batcher that is being worked on.
250 void closeBatcher() {
252 Passes.push_back(Batcher);
260 //===----------------------------------------------------------------------===//
261 // PassManagerTraits<BasicBlock> Specialization
263 // This pass manager is used to group together all of the BasicBlockPass's
264 // into a single unit.
266 template<> struct PassManagerTraits<BasicBlock> : public BasicBlockPass {
267 // PassClass - The type of passes tracked by this PassManager
268 typedef BasicBlockPass PassClass;
270 // SubPassClass - The types of classes that should be collated together
271 // This is impossible to match, so BasicBlock instantiations of PassManagerT
274 typedef PassManagerT<Module> SubPassClass;
276 // BatcherClass - The type to use for collation of subtypes... This class is
277 // never instantiated for the PassManager<BasicBlock>, but it must be an
278 // instance of PassClass to typecheck.
280 typedef PassClass BatcherClass;
282 // ParentClass - The type of the parent PassManager...
283 typedef PassManagerT<Method> ParentClass;
285 // runPass - Specify how the pass should be run on the UnitType
286 static bool runPass(PassClass *P, BasicBlock *M) {
287 // todo, init and finalize
288 return P->runOnBasicBlock(M);
291 // run - Implement the Pass interface...
292 virtual bool runOnBasicBlock(BasicBlock *BB);
297 //===----------------------------------------------------------------------===//
298 // PassManagerTraits<Method> Specialization
300 // This pass manager is used to group together all of the MethodPass's
301 // into a single unit.
303 template<> struct PassManagerTraits<Method> : public MethodPass {
304 // PassClass - The type of passes tracked by this PassManager
305 typedef MethodPass PassClass;
307 // SubPassClass - The types of classes that should be collated together
308 typedef BasicBlockPass SubPassClass;
310 // BatcherClass - The type to use for collation of subtypes...
311 typedef PassManagerT<BasicBlock> BatcherClass;
313 // ParentClass - The type of the parent PassManager...
314 typedef PassManagerT<Module> ParentClass;
316 // PMType - The type of the passmanager that subclasses this class
317 typedef PassManagerT<Method> PMType;
319 // runPass - Specify how the pass should be run on the UnitType
320 static bool runPass(PassClass *P, Method *M) {
321 return P->runOnMethod(M);
324 // Implement the MethodPass interface...
325 virtual bool doInitialization(Module *M);
326 virtual bool runOnMethod(Method *M);
327 virtual bool doFinalization(Module *M);
332 //===----------------------------------------------------------------------===//
333 // PassManagerTraits<Module> Specialization
335 // This is the top level PassManager implementation that holds generic passes.
337 template<> struct PassManagerTraits<Module> : public Pass {
338 // PassClass - The type of passes tracked by this PassManager
339 typedef Pass PassClass;
341 // SubPassClass - The types of classes that should be collated together
342 typedef MethodPass SubPassClass;
344 // BatcherClass - The type to use for collation of subtypes...
345 typedef PassManagerT<Method> BatcherClass;
347 // ParentClass - The type of the parent PassManager...
348 typedef void ParentClass;
350 // runPass - Specify how the pass should be run on the UnitType
351 static bool runPass(PassClass *P, Module *M) { return P->run(M); }
353 // run - Implement the Pass interface...
354 virtual bool run(Module *M) {
355 return ((PassManagerT<Module>*)this)->runOnUnit(M);
361 //===----------------------------------------------------------------------===//
362 // PassManagerTraits Method Implementations
365 // PassManagerTraits<BasicBlock> Implementations
367 inline bool PassManagerTraits<BasicBlock>::runOnBasicBlock(BasicBlock *BB) {
368 return ((PassManagerT<BasicBlock>*)this)->runOnUnit(BB);
372 // PassManagerTraits<Method> Implementations
374 inline bool PassManagerTraits<Method>::doInitialization(Module *M) {
375 bool Changed = false;
376 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
377 ((PMType*)this)->Passes[i]->doInitialization(M);
381 inline bool PassManagerTraits<Method>::runOnMethod(Method *M) {
382 return ((PMType*)this)->runOnUnit(M);
386 // PassManagerTraits<Module> Implementations
388 inline bool PassManagerTraits<Method>::doFinalization(Module *M) {
389 bool Changed = false;
390 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
391 ((PMType*)this)->Passes[i]->doFinalization(M);