blob: 5583b9b9187f27ca2176368982b95fbb714fe15f [file] [log] [blame]
Chris Lattner67d25652002-01-30 23:20:39 +00001//===- llvm/PassManager.h - Container for Passes -----------------*- C++ -*--=//
2//
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.
7//
8// The PassManagerT template is instantiated three times to do its job.
9//
10//===----------------------------------------------------------------------===//
11
12#ifndef LLVM_PASSMANAGER_H
13#define LLVM_PASSMANAGER_H
14
15#include "llvm/Pass.h"
16#include <string>
17
Chris Lattner67d25652002-01-30 23:20:39 +000018//===----------------------------------------------------------------------===//
19// PMDebug class - a set of debugging functions that are enabled when compiling
20// with -g on. If compiling at -O, all functions are inlined noops.
21//
22struct PMDebug {
23#ifdef NDEBUG
24 inline static void PrintPassStructure(Pass *) {}
25 inline static void PrintPassInformation(unsigned,const char*,Pass*,Value*) {}
Chris Lattnerac3e0602002-01-31 18:32:27 +000026 inline static void PrintAnalysisSetInfo(unsigned,const char*,Pass *P,
Chris Lattner67d25652002-01-30 23:20:39 +000027 const Pass::AnalysisSet &) {}
28#else
29 // If compiled in debug mode, these functions can be enabled by setting
30 // -debug-pass on the command line of the tool being used.
31 //
32 static void PrintPassStructure(Pass *P);
33 static void PrintPassInformation(unsigned,const char*,Pass *, Value *);
Chris Lattnerac3e0602002-01-31 18:32:27 +000034 static void PrintAnalysisSetInfo(unsigned,const char*,Pass *P,
35 const Pass::AnalysisSet&);
Chris Lattner67d25652002-01-30 23:20:39 +000036#endif
37};
38
39
40
41//===----------------------------------------------------------------------===//
42// Declare the PassManagerTraits which will be specialized...
43//
44template<class UnitType> class PassManagerTraits; // Do not define.
45
46
47//===----------------------------------------------------------------------===//
48// PassManagerT - Container object for passes. The PassManagerT destructor
49// deletes all passes contained inside of the PassManagerT, so you shouldn't
50// delete passes manually, and all passes should be dynamically allocated.
51//
52template<typename UnitType>
53class PassManagerT : public PassManagerTraits<UnitType>,public AnalysisResolver{
54 typedef typename PassManagerTraits<UnitType>::PassClass PassClass;
55 typedef typename PassManagerTraits<UnitType>::SubPassClass SubPassClass;
56 typedef typename PassManagerTraits<UnitType>::BatcherClass BatcherClass;
57 typedef typename PassManagerTraits<UnitType>::ParentClass ParentClass;
58 typedef PassManagerTraits<UnitType> Traits;
59
60 friend typename PassManagerTraits<UnitType>::PassClass;
61 friend typename PassManagerTraits<UnitType>::SubPassClass;
62 friend class PassManagerTraits<UnitType>;
63
64 std::vector<PassClass*> Passes; // List of pass's to run
65
66 // The parent of this pass manager...
Chris Lattnerac3e0602002-01-31 18:32:27 +000067 ParentClass * const Parent;
Chris Lattner67d25652002-01-30 23:20:39 +000068
69 // The current batcher if one is in use, or null
70 BatcherClass *Batcher;
71
72 // CurrentAnalyses - As the passes are being run, this map contains the
73 // analyses that are available to the current pass for use. This is accessed
74 // through the getAnalysis() function in this class and in Pass.
75 //
76 std::map<AnalysisID, Pass*> CurrentAnalyses;
77
Chris Lattnerac3e0602002-01-31 18:32:27 +000078 // LastUseOf - This map keeps track of the last usage in our pipeline of a
79 // particular pass. When executing passes, the memory for .first is free'd
80 // after .second is run.
81 //
82 std::map<Pass*, Pass*> LastUseOf;
83
Chris Lattner67d25652002-01-30 23:20:39 +000084public:
85 PassManagerT(ParentClass *Par = 0) : Parent(Par), Batcher(0) {}
86 ~PassManagerT() {
87 // Delete all of the contained passes...
88 for (std::vector<PassClass*>::iterator I = Passes.begin(), E = Passes.end();
89 I != E; ++I)
90 delete *I;
91 }
92
93 // run - Run all of the queued passes on the specified module in an optimal
94 // way.
95 virtual bool runOnUnit(UnitType *M) {
96 bool MadeChanges = false;
97 closeBatcher();
98 CurrentAnalyses.clear();
99
Chris Lattnerac3e0602002-01-31 18:32:27 +0000100 // LastUserOf - This contains the inverted LastUseOfMap...
101 std::map<Pass *, std::vector<Pass*> > LastUserOf;
102 for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(),
103 E = LastUseOf.end(); I != E; ++I)
104 LastUserOf[I->second].push_back(I->first);
105
106
Chris Lattner67d25652002-01-30 23:20:39 +0000107 // Output debug information...
108 if (Parent == 0) PMDebug::PrintPassStructure(this);
109
110 // Run all of the passes
111 for (unsigned i = 0, e = Passes.size(); i < e; ++i) {
112 PassClass *P = Passes[i];
113
114 PMDebug::PrintPassInformation(getDepth(), "Executing Pass", P, (Value*)M);
115
116 // Get information about what analyses the pass uses...
117 std::vector<AnalysisID> Required, Destroyed, Provided;
118 P->getAnalysisUsageInfo(Required, Destroyed, Provided);
119
Chris Lattnerac3e0602002-01-31 18:32:27 +0000120 PMDebug::PrintAnalysisSetInfo(getDepth(), "Required", P, Required);
Chris Lattner67d25652002-01-30 23:20:39 +0000121
122#ifndef NDEBUG
123 // All Required analyses should be available to the pass as it runs!
124 for (Pass::AnalysisSet::iterator I = Required.begin(),
125 E = Required.end(); I != E; ++I) {
126 assert(getAnalysisOrNullUp(*I) && "Analysis used but not available!");
127 }
128#endif
129
130 // Run the sub pass!
131 MadeChanges |= Traits::runPass(P, M);
132
Chris Lattnerac3e0602002-01-31 18:32:27 +0000133 PMDebug::PrintAnalysisSetInfo(getDepth(), "Destroyed", P, Destroyed);
134 PMDebug::PrintAnalysisSetInfo(getDepth(), "Provided", P, Provided);
Chris Lattner67d25652002-01-30 23:20:39 +0000135
136 // Erase all analyses in the destroyed set...
137 for (Pass::AnalysisSet::iterator I = Destroyed.begin(),
138 E = Destroyed.end(); I != E; ++I)
139 CurrentAnalyses.erase(*I);
140
141 // Add all analyses in the provided set...
142 for (Pass::AnalysisSet::iterator I = Provided.begin(),
143 E = Provided.end(); I != E; ++I)
144 CurrentAnalyses[*I] = P;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000145
146 // Free memory for any passes that we are the last use of...
147 std::vector<Pass*> &DeadPass = LastUserOf[P];
148 for (std::vector<Pass*>::iterator I = DeadPass.begin(),E = DeadPass.end();
149 I != E; ++I) {
150 PMDebug::PrintPassInformation(getDepth()+1, "Freeing Pass", *I,
151 (Value*)M);
152 (*I)->releaseMemory();
153 }
Chris Lattner67d25652002-01-30 23:20:39 +0000154 }
155 return MadeChanges;
156 }
157
Chris Lattnerac3e0602002-01-31 18:32:27 +0000158#ifndef NDEBUG
159 // dumpPassStructure - Implement the -debug-passes=PassStructure option
160 virtual void dumpPassStructure(unsigned Offset = 0) {
161 std::cerr << std::string(Offset*2, ' ') << Traits::getPMName()
162 << " Pass Manager\n";
163 for (std::vector<PassClass*>::iterator I = Passes.begin(), E = Passes.end();
164 I != E; ++I) {
165 PassClass *P = *I;
166 P->dumpPassStructure(Offset+1);
167
168 // Loop through and see which classes are destroyed after this one...
169 for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(),
170 E = LastUseOf.end(); I != E; ++I) {
171 if (P == I->second) {
172 std::cerr << "Fr" << std::string(Offset*2, ' ');
173 I->first->dumpPassStructure(0);
174 }
175 }
176 }
177 }
178#endif
179
180 Pass *getAnalysisOrNullDown(AnalysisID ID) const {
181 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID);
182 if (I == CurrentAnalyses.end()) {
183 if (Batcher)
184 return ((AnalysisResolver*)Batcher)->getAnalysisOrNullDown(ID);
185 return 0;
186 }
187 return I->second;
188 }
189
190 Pass *getAnalysisOrNullUp(AnalysisID ID) const {
191 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID);
192 if (I == CurrentAnalyses.end()) {
193 if (Parent)
194 return Parent->getAnalysisOrNullUp(ID);
195 return 0;
196 }
197 return I->second;
198 }
199
200 // markPassUsed - Inform higher level pass managers (and ourselves)
201 // that these analyses are being used by this pass. This is used to
202 // make sure that analyses are not free'd before we have to use
203 // them...
204 //
205 void markPassUsed(AnalysisID P, Pass *User) {
206 std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.find(P);
207 if (I != CurrentAnalyses.end()) {
208 LastUseOf[I->second] = User; // Local pass, extend the lifetime
209 } else {
210 // Pass not in current available set, must be a higher level pass
211 // available to us, propogate to parent pass manager... We tell the
212 // parent that we (the passmanager) are using the analysis so that it
213 // frees the analysis AFTER this pass manager runs.
214 //
215 assert(Parent != 0 && "Pass available but not found!");
216 Parent->markPassUsed(P, this);
217 }
218 }
219
220 // Return the number of parent PassManagers that exist
221 virtual unsigned getDepth() const {
222 if (Parent == 0) return 0;
223 return 1 + Parent->getDepth();
224 }
225
Chris Lattner67d25652002-01-30 23:20:39 +0000226 // add - Add a pass to the queue of passes to run. This passes ownership of
227 // the Pass to the PassManager. When the PassManager is destroyed, the pass
Chris Lattnerac3e0602002-01-31 18:32:27 +0000228 // will be destroyed as well, so there is no need to delete the pass. This
229 // implies that all passes MUST be new'd.
Chris Lattner67d25652002-01-30 23:20:39 +0000230 //
231 void add(PassClass *P) {
232 // Get information about what analyses the pass uses...
233 std::vector<AnalysisID> Required, Destroyed, Provided;
234 P->getAnalysisUsageInfo(Required, Destroyed, Provided);
235
236 // Loop over all of the analyses used by this pass,
237 for (std::vector<AnalysisID>::iterator I = Required.begin(),
238 E = Required.end(); I != E; ++I) {
239 if (getAnalysisOrNullDown(*I) == 0)
Chris Lattner2eaac392002-01-31 00:40:44 +0000240 add((PassClass*)I->createPass());
Chris Lattner67d25652002-01-30 23:20:39 +0000241 }
242
243 // Tell the pass to add itself to this PassManager... the way it does so
244 // depends on the class of the pass, and is critical to laying out passes in
245 // an optimal order..
246 //
Chris Lattnerac3e0602002-01-31 18:32:27 +0000247 P->addToPassManager(this, Required, Destroyed, Provided);
Chris Lattner67d25652002-01-30 23:20:39 +0000248 }
249
250private:
251
252 // addPass - These functions are used to implement the subclass specific
253 // behaviors present in PassManager. Basically the add(Pass*) method ends up
254 // reflecting its behavior into a Pass::addToPassManager call. Subclasses of
255 // Pass override it specifically so that they can reflect the type
256 // information inherent in "this" back to the PassManager.
257 //
258 // For generic Pass subclasses (which are interprocedural passes), we simply
259 // add the pass to the end of the pass list and terminate any accumulation of
260 // MethodPasses that are present.
261 //
Chris Lattnerac3e0602002-01-31 18:32:27 +0000262 void addPass(PassClass *P, Pass::AnalysisSet &Required,
263 Pass::AnalysisSet &Destroyed, Pass::AnalysisSet &Provided) {
Chris Lattner67d25652002-01-30 23:20:39 +0000264 // Providers are analysis classes which are forbidden to modify the module
265 // they are operating on, so they are allowed to be reordered to before the
266 // batcher...
267 //
268 if (Batcher && Provided.empty())
269 closeBatcher(); // This pass cannot be batched!
270
271 // Set the Resolver instance variable in the Pass so that it knows where to
272 // find this object...
273 //
274 setAnalysisResolver(P, this);
275 Passes.push_back(P);
276
Chris Lattnerac3e0602002-01-31 18:32:27 +0000277 // Inform higher level pass managers (and ourselves) that these analyses are
278 // being used by this pass. This is used to make sure that analyses are not
279 // free'd before we have to use them...
280 //
281 for (std::vector<AnalysisID>::iterator I = Required.begin(),
282 E = Required.end(); I != E; ++I)
283 markPassUsed(*I, P); // Mark *I as used by P
284
Chris Lattner67d25652002-01-30 23:20:39 +0000285 // Erase all analyses in the destroyed set...
286 for (std::vector<AnalysisID>::iterator I = Destroyed.begin(),
287 E = Destroyed.end(); I != E; ++I)
288 CurrentAnalyses.erase(*I);
289
290 // Add all analyses in the provided set...
291 for (std::vector<AnalysisID>::iterator I = Provided.begin(),
292 E = Provided.end(); I != E; ++I)
293 CurrentAnalyses[*I] = P;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000294
295 // For now assume that our results are never used...
296 LastUseOf[P] = P;
Chris Lattner67d25652002-01-30 23:20:39 +0000297 }
298
299 // For MethodPass subclasses, we must be sure to batch the MethodPasses
300 // together in a MethodPassBatcher object so that all of the analyses are run
301 // together a method at a time.
302 //
Chris Lattnerac3e0602002-01-31 18:32:27 +0000303 void addPass(SubPassClass *MP, Pass::AnalysisSet &Required,
304 Pass::AnalysisSet &Destroyed, Pass::AnalysisSet &Provided) {
Chris Lattner67d25652002-01-30 23:20:39 +0000305 if (Batcher == 0) // If we don't have a batcher yet, make one now.
306 Batcher = new BatcherClass(this);
307 // The Batcher will queue them passes up
Chris Lattnerac3e0602002-01-31 18:32:27 +0000308 MP->addToPassManager(Batcher, Required, Destroyed, Provided);
Chris Lattner67d25652002-01-30 23:20:39 +0000309 }
310
311 // closeBatcher - Terminate the batcher that is being worked on.
312 void closeBatcher() {
313 if (Batcher) {
314 Passes.push_back(Batcher);
315 Batcher = 0;
316 }
317 }
318};
319
320
321
322//===----------------------------------------------------------------------===//
323// PassManagerTraits<BasicBlock> Specialization
324//
325// This pass manager is used to group together all of the BasicBlockPass's
326// into a single unit.
327//
328template<> struct PassManagerTraits<BasicBlock> : public BasicBlockPass {
329 // PassClass - The type of passes tracked by this PassManager
330 typedef BasicBlockPass PassClass;
331
332 // SubPassClass - The types of classes that should be collated together
333 // This is impossible to match, so BasicBlock instantiations of PassManagerT
334 // do not collate.
335 //
336 typedef PassManagerT<Module> SubPassClass;
337
338 // BatcherClass - The type to use for collation of subtypes... This class is
339 // never instantiated for the PassManager<BasicBlock>, but it must be an
340 // instance of PassClass to typecheck.
341 //
342 typedef PassClass BatcherClass;
343
344 // ParentClass - The type of the parent PassManager...
345 typedef PassManagerT<Method> ParentClass;
346
Chris Lattner2eaac392002-01-31 00:40:44 +0000347 // PMType - The type of the passmanager that subclasses this class
348 typedef PassManagerT<BasicBlock> PMType;
349
Chris Lattner67d25652002-01-30 23:20:39 +0000350 // runPass - Specify how the pass should be run on the UnitType
351 static bool runPass(PassClass *P, BasicBlock *M) {
352 // todo, init and finalize
353 return P->runOnBasicBlock(M);
354 }
355
Chris Lattnerac3e0602002-01-31 18:32:27 +0000356 // getPMName() - Return the name of the unit the PassManager operates on for
357 // debugging.
358 const char *getPMName() const { return "BasicBlock"; }
359
Chris Lattner2eaac392002-01-31 00:40:44 +0000360 // Implement the BasicBlockPass interface...
361 virtual bool doInitialization(Module *M);
Chris Lattner67d25652002-01-30 23:20:39 +0000362 virtual bool runOnBasicBlock(BasicBlock *BB);
Chris Lattner2eaac392002-01-31 00:40:44 +0000363 virtual bool doFinalization(Module *M);
Chris Lattner67d25652002-01-30 23:20:39 +0000364};
365
366
367
368//===----------------------------------------------------------------------===//
369// PassManagerTraits<Method> Specialization
370//
371// This pass manager is used to group together all of the MethodPass's
372// into a single unit.
373//
374template<> struct PassManagerTraits<Method> : public MethodPass {
375 // PassClass - The type of passes tracked by this PassManager
376 typedef MethodPass PassClass;
377
378 // SubPassClass - The types of classes that should be collated together
379 typedef BasicBlockPass SubPassClass;
380
381 // BatcherClass - The type to use for collation of subtypes...
382 typedef PassManagerT<BasicBlock> BatcherClass;
383
384 // ParentClass - The type of the parent PassManager...
385 typedef PassManagerT<Module> ParentClass;
386
387 // PMType - The type of the passmanager that subclasses this class
388 typedef PassManagerT<Method> PMType;
389
390 // runPass - Specify how the pass should be run on the UnitType
391 static bool runPass(PassClass *P, Method *M) {
392 return P->runOnMethod(M);
393 }
394
Chris Lattnerac3e0602002-01-31 18:32:27 +0000395 // getPMName() - Return the name of the unit the PassManager operates on for
396 // debugging.
397 const char *getPMName() const { return "Method"; }
398
Chris Lattner67d25652002-01-30 23:20:39 +0000399 // Implement the MethodPass interface...
400 virtual bool doInitialization(Module *M);
401 virtual bool runOnMethod(Method *M);
402 virtual bool doFinalization(Module *M);
403};
404
405
406
407//===----------------------------------------------------------------------===//
408// PassManagerTraits<Module> Specialization
409//
410// This is the top level PassManager implementation that holds generic passes.
411//
412template<> struct PassManagerTraits<Module> : public Pass {
413 // PassClass - The type of passes tracked by this PassManager
414 typedef Pass PassClass;
415
416 // SubPassClass - The types of classes that should be collated together
417 typedef MethodPass SubPassClass;
418
419 // BatcherClass - The type to use for collation of subtypes...
420 typedef PassManagerT<Method> BatcherClass;
421
422 // ParentClass - The type of the parent PassManager...
Chris Lattnerac3e0602002-01-31 18:32:27 +0000423 typedef AnalysisResolver ParentClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000424
425 // runPass - Specify how the pass should be run on the UnitType
426 static bool runPass(PassClass *P, Module *M) { return P->run(M); }
427
Chris Lattnerac3e0602002-01-31 18:32:27 +0000428 // getPMName() - Return the name of the unit the PassManager operates on for
429 // debugging.
430 const char *getPMName() const { return "Module"; }
431
Chris Lattner67d25652002-01-30 23:20:39 +0000432 // run - Implement the Pass interface...
433 virtual bool run(Module *M) {
434 return ((PassManagerT<Module>*)this)->runOnUnit(M);
435 }
436};
437
438
439
440//===----------------------------------------------------------------------===//
441// PassManagerTraits Method Implementations
442//
443
444// PassManagerTraits<BasicBlock> Implementations
445//
Chris Lattner2eaac392002-01-31 00:40:44 +0000446inline bool PassManagerTraits<BasicBlock>::doInitialization(Module *M) {
447 bool Changed = false;
448 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
449 ((PMType*)this)->Passes[i]->doInitialization(M);
450 return Changed;
451}
452
Chris Lattner67d25652002-01-30 23:20:39 +0000453inline bool PassManagerTraits<BasicBlock>::runOnBasicBlock(BasicBlock *BB) {
Chris Lattner2eaac392002-01-31 00:40:44 +0000454 return ((PMType*)this)->runOnUnit(BB);
455}
456
457inline bool PassManagerTraits<BasicBlock>::doFinalization(Module *M) {
458 bool Changed = false;
459 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
460 ((PMType*)this)->Passes[i]->doFinalization(M);
461 return Changed;
Chris Lattner67d25652002-01-30 23:20:39 +0000462}
463
464
465// PassManagerTraits<Method> Implementations
466//
467inline bool PassManagerTraits<Method>::doInitialization(Module *M) {
468 bool Changed = false;
469 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
470 ((PMType*)this)->Passes[i]->doInitialization(M);
471 return Changed;
472}
473
474inline bool PassManagerTraits<Method>::runOnMethod(Method *M) {
475 return ((PMType*)this)->runOnUnit(M);
476}
477
Chris Lattner67d25652002-01-30 23:20:39 +0000478inline bool PassManagerTraits<Method>::doFinalization(Module *M) {
479 bool Changed = false;
480 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
481 ((PMType*)this)->Passes[i]->doFinalization(M);
482 return Changed;
483}
484
485#endif