blob: d5c483f7524d133f60bd9df39b6a3323c6f65a5e [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!
Chris Lattnerf2f31bd2002-02-01 04:53:36 +0000131 bool Changed = Traits::runPass(P, M);
132 MadeChanges |= Changed;
Chris Lattner67d25652002-01-30 23:20:39 +0000133
Chris Lattnerf2f31bd2002-02-01 04:53:36 +0000134 if (Changed)
135 PMDebug::PrintPassInformation(getDepth()+1, "Made Modification", P,
136 (Value*)M);
Chris Lattnerac3e0602002-01-31 18:32:27 +0000137 PMDebug::PrintAnalysisSetInfo(getDepth(), "Destroyed", P, Destroyed);
138 PMDebug::PrintAnalysisSetInfo(getDepth(), "Provided", P, Provided);
Chris Lattner67d25652002-01-30 23:20:39 +0000139
140 // Erase all analyses in the destroyed set...
141 for (Pass::AnalysisSet::iterator I = Destroyed.begin(),
142 E = Destroyed.end(); I != E; ++I)
143 CurrentAnalyses.erase(*I);
144
145 // Add all analyses in the provided set...
146 for (Pass::AnalysisSet::iterator I = Provided.begin(),
147 E = Provided.end(); I != E; ++I)
148 CurrentAnalyses[*I] = P;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000149
150 // Free memory for any passes that we are the last use of...
151 std::vector<Pass*> &DeadPass = LastUserOf[P];
152 for (std::vector<Pass*>::iterator I = DeadPass.begin(),E = DeadPass.end();
153 I != E; ++I) {
154 PMDebug::PrintPassInformation(getDepth()+1, "Freeing Pass", *I,
155 (Value*)M);
156 (*I)->releaseMemory();
157 }
Chris Lattner67d25652002-01-30 23:20:39 +0000158 }
159 return MadeChanges;
160 }
161
Chris Lattnerac3e0602002-01-31 18:32:27 +0000162#ifndef NDEBUG
163 // dumpPassStructure - Implement the -debug-passes=PassStructure option
164 virtual void dumpPassStructure(unsigned Offset = 0) {
165 std::cerr << std::string(Offset*2, ' ') << Traits::getPMName()
166 << " Pass Manager\n";
167 for (std::vector<PassClass*>::iterator I = Passes.begin(), E = Passes.end();
168 I != E; ++I) {
169 PassClass *P = *I;
170 P->dumpPassStructure(Offset+1);
171
172 // Loop through and see which classes are destroyed after this one...
173 for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(),
174 E = LastUseOf.end(); I != E; ++I) {
175 if (P == I->second) {
176 std::cerr << "Fr" << std::string(Offset*2, ' ');
177 I->first->dumpPassStructure(0);
178 }
179 }
180 }
181 }
182#endif
183
184 Pass *getAnalysisOrNullDown(AnalysisID ID) const {
185 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID);
186 if (I == CurrentAnalyses.end()) {
187 if (Batcher)
188 return ((AnalysisResolver*)Batcher)->getAnalysisOrNullDown(ID);
189 return 0;
190 }
191 return I->second;
192 }
193
194 Pass *getAnalysisOrNullUp(AnalysisID ID) const {
195 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID);
196 if (I == CurrentAnalyses.end()) {
197 if (Parent)
198 return Parent->getAnalysisOrNullUp(ID);
199 return 0;
200 }
201 return I->second;
202 }
203
204 // markPassUsed - Inform higher level pass managers (and ourselves)
205 // that these analyses are being used by this pass. This is used to
206 // make sure that analyses are not free'd before we have to use
207 // them...
208 //
209 void markPassUsed(AnalysisID P, Pass *User) {
210 std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.find(P);
211 if (I != CurrentAnalyses.end()) {
212 LastUseOf[I->second] = User; // Local pass, extend the lifetime
213 } else {
214 // Pass not in current available set, must be a higher level pass
215 // available to us, propogate to parent pass manager... We tell the
216 // parent that we (the passmanager) are using the analysis so that it
217 // frees the analysis AFTER this pass manager runs.
218 //
219 assert(Parent != 0 && "Pass available but not found!");
220 Parent->markPassUsed(P, this);
221 }
222 }
223
224 // Return the number of parent PassManagers that exist
225 virtual unsigned getDepth() const {
226 if (Parent == 0) return 0;
227 return 1 + Parent->getDepth();
228 }
229
Chris Lattner67d25652002-01-30 23:20:39 +0000230 // add - Add a pass to the queue of passes to run. This passes ownership of
231 // the Pass to the PassManager. When the PassManager is destroyed, the pass
Chris Lattnerac3e0602002-01-31 18:32:27 +0000232 // will be destroyed as well, so there is no need to delete the pass. This
233 // implies that all passes MUST be new'd.
Chris Lattner67d25652002-01-30 23:20:39 +0000234 //
235 void add(PassClass *P) {
236 // Get information about what analyses the pass uses...
237 std::vector<AnalysisID> Required, Destroyed, Provided;
238 P->getAnalysisUsageInfo(Required, Destroyed, Provided);
239
240 // Loop over all of the analyses used by this pass,
241 for (std::vector<AnalysisID>::iterator I = Required.begin(),
242 E = Required.end(); I != E; ++I) {
243 if (getAnalysisOrNullDown(*I) == 0)
Chris Lattner2eaac392002-01-31 00:40:44 +0000244 add((PassClass*)I->createPass());
Chris Lattner67d25652002-01-30 23:20:39 +0000245 }
246
247 // Tell the pass to add itself to this PassManager... the way it does so
248 // depends on the class of the pass, and is critical to laying out passes in
249 // an optimal order..
250 //
Chris Lattnerac3e0602002-01-31 18:32:27 +0000251 P->addToPassManager(this, Required, Destroyed, Provided);
Chris Lattner67d25652002-01-30 23:20:39 +0000252 }
253
254private:
255
256 // addPass - These functions are used to implement the subclass specific
257 // behaviors present in PassManager. Basically the add(Pass*) method ends up
258 // reflecting its behavior into a Pass::addToPassManager call. Subclasses of
259 // Pass override it specifically so that they can reflect the type
260 // information inherent in "this" back to the PassManager.
261 //
262 // For generic Pass subclasses (which are interprocedural passes), we simply
263 // add the pass to the end of the pass list and terminate any accumulation of
264 // MethodPasses that are present.
265 //
Chris Lattnerac3e0602002-01-31 18:32:27 +0000266 void addPass(PassClass *P, Pass::AnalysisSet &Required,
267 Pass::AnalysisSet &Destroyed, Pass::AnalysisSet &Provided) {
Chris Lattner67d25652002-01-30 23:20:39 +0000268 // Providers are analysis classes which are forbidden to modify the module
269 // they are operating on, so they are allowed to be reordered to before the
270 // batcher...
271 //
272 if (Batcher && Provided.empty())
273 closeBatcher(); // This pass cannot be batched!
274
275 // Set the Resolver instance variable in the Pass so that it knows where to
276 // find this object...
277 //
278 setAnalysisResolver(P, this);
279 Passes.push_back(P);
280
Chris Lattnerac3e0602002-01-31 18:32:27 +0000281 // Inform higher level pass managers (and ourselves) that these analyses are
282 // being used by this pass. This is used to make sure that analyses are not
283 // free'd before we have to use them...
284 //
285 for (std::vector<AnalysisID>::iterator I = Required.begin(),
286 E = Required.end(); I != E; ++I)
287 markPassUsed(*I, P); // Mark *I as used by P
288
Chris Lattner67d25652002-01-30 23:20:39 +0000289 // Erase all analyses in the destroyed set...
290 for (std::vector<AnalysisID>::iterator I = Destroyed.begin(),
291 E = Destroyed.end(); I != E; ++I)
292 CurrentAnalyses.erase(*I);
293
294 // Add all analyses in the provided set...
295 for (std::vector<AnalysisID>::iterator I = Provided.begin(),
296 E = Provided.end(); I != E; ++I)
297 CurrentAnalyses[*I] = P;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000298
299 // For now assume that our results are never used...
300 LastUseOf[P] = P;
Chris Lattner67d25652002-01-30 23:20:39 +0000301 }
302
303 // For MethodPass subclasses, we must be sure to batch the MethodPasses
304 // together in a MethodPassBatcher object so that all of the analyses are run
305 // together a method at a time.
306 //
Chris Lattnerac3e0602002-01-31 18:32:27 +0000307 void addPass(SubPassClass *MP, Pass::AnalysisSet &Required,
308 Pass::AnalysisSet &Destroyed, Pass::AnalysisSet &Provided) {
Chris Lattner67d25652002-01-30 23:20:39 +0000309 if (Batcher == 0) // If we don't have a batcher yet, make one now.
310 Batcher = new BatcherClass(this);
311 // The Batcher will queue them passes up
Chris Lattnerac3e0602002-01-31 18:32:27 +0000312 MP->addToPassManager(Batcher, Required, Destroyed, Provided);
Chris Lattner67d25652002-01-30 23:20:39 +0000313 }
314
315 // closeBatcher - Terminate the batcher that is being worked on.
316 void closeBatcher() {
317 if (Batcher) {
318 Passes.push_back(Batcher);
319 Batcher = 0;
320 }
321 }
322};
323
324
325
326//===----------------------------------------------------------------------===//
327// PassManagerTraits<BasicBlock> Specialization
328//
329// This pass manager is used to group together all of the BasicBlockPass's
330// into a single unit.
331//
332template<> struct PassManagerTraits<BasicBlock> : public BasicBlockPass {
333 // PassClass - The type of passes tracked by this PassManager
334 typedef BasicBlockPass PassClass;
335
336 // SubPassClass - The types of classes that should be collated together
337 // This is impossible to match, so BasicBlock instantiations of PassManagerT
338 // do not collate.
339 //
340 typedef PassManagerT<Module> SubPassClass;
341
342 // BatcherClass - The type to use for collation of subtypes... This class is
343 // never instantiated for the PassManager<BasicBlock>, but it must be an
344 // instance of PassClass to typecheck.
345 //
346 typedef PassClass BatcherClass;
347
348 // ParentClass - The type of the parent PassManager...
349 typedef PassManagerT<Method> ParentClass;
350
Chris Lattner2eaac392002-01-31 00:40:44 +0000351 // PMType - The type of the passmanager that subclasses this class
352 typedef PassManagerT<BasicBlock> PMType;
353
Chris Lattner67d25652002-01-30 23:20:39 +0000354 // runPass - Specify how the pass should be run on the UnitType
355 static bool runPass(PassClass *P, BasicBlock *M) {
356 // todo, init and finalize
357 return P->runOnBasicBlock(M);
358 }
359
Chris Lattnerac3e0602002-01-31 18:32:27 +0000360 // getPMName() - Return the name of the unit the PassManager operates on for
361 // debugging.
362 const char *getPMName() const { return "BasicBlock"; }
363
Chris Lattner2eaac392002-01-31 00:40:44 +0000364 // Implement the BasicBlockPass interface...
365 virtual bool doInitialization(Module *M);
Chris Lattner67d25652002-01-30 23:20:39 +0000366 virtual bool runOnBasicBlock(BasicBlock *BB);
Chris Lattner2eaac392002-01-31 00:40:44 +0000367 virtual bool doFinalization(Module *M);
Chris Lattner67d25652002-01-30 23:20:39 +0000368};
369
370
371
372//===----------------------------------------------------------------------===//
373// PassManagerTraits<Method> Specialization
374//
375// This pass manager is used to group together all of the MethodPass's
376// into a single unit.
377//
378template<> struct PassManagerTraits<Method> : public MethodPass {
379 // PassClass - The type of passes tracked by this PassManager
380 typedef MethodPass PassClass;
381
382 // SubPassClass - The types of classes that should be collated together
383 typedef BasicBlockPass SubPassClass;
384
385 // BatcherClass - The type to use for collation of subtypes...
386 typedef PassManagerT<BasicBlock> BatcherClass;
387
388 // ParentClass - The type of the parent PassManager...
389 typedef PassManagerT<Module> ParentClass;
390
391 // PMType - The type of the passmanager that subclasses this class
392 typedef PassManagerT<Method> PMType;
393
394 // runPass - Specify how the pass should be run on the UnitType
395 static bool runPass(PassClass *P, Method *M) {
396 return P->runOnMethod(M);
397 }
398
Chris Lattnerac3e0602002-01-31 18:32:27 +0000399 // getPMName() - Return the name of the unit the PassManager operates on for
400 // debugging.
401 const char *getPMName() const { return "Method"; }
402
Chris Lattner67d25652002-01-30 23:20:39 +0000403 // Implement the MethodPass interface...
404 virtual bool doInitialization(Module *M);
405 virtual bool runOnMethod(Method *M);
406 virtual bool doFinalization(Module *M);
407};
408
409
410
411//===----------------------------------------------------------------------===//
412// PassManagerTraits<Module> Specialization
413//
414// This is the top level PassManager implementation that holds generic passes.
415//
416template<> struct PassManagerTraits<Module> : public Pass {
417 // PassClass - The type of passes tracked by this PassManager
418 typedef Pass PassClass;
419
420 // SubPassClass - The types of classes that should be collated together
421 typedef MethodPass SubPassClass;
422
423 // BatcherClass - The type to use for collation of subtypes...
424 typedef PassManagerT<Method> BatcherClass;
425
426 // ParentClass - The type of the parent PassManager...
Chris Lattnerac3e0602002-01-31 18:32:27 +0000427 typedef AnalysisResolver ParentClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000428
429 // runPass - Specify how the pass should be run on the UnitType
430 static bool runPass(PassClass *P, Module *M) { return P->run(M); }
431
Chris Lattnerac3e0602002-01-31 18:32:27 +0000432 // getPMName() - Return the name of the unit the PassManager operates on for
433 // debugging.
434 const char *getPMName() const { return "Module"; }
435
Chris Lattner67d25652002-01-30 23:20:39 +0000436 // run - Implement the Pass interface...
437 virtual bool run(Module *M) {
438 return ((PassManagerT<Module>*)this)->runOnUnit(M);
439 }
440};
441
442
443
444//===----------------------------------------------------------------------===//
445// PassManagerTraits Method Implementations
446//
447
448// PassManagerTraits<BasicBlock> Implementations
449//
Chris Lattner2eaac392002-01-31 00:40:44 +0000450inline bool PassManagerTraits<BasicBlock>::doInitialization(Module *M) {
451 bool Changed = false;
452 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
453 ((PMType*)this)->Passes[i]->doInitialization(M);
454 return Changed;
455}
456
Chris Lattner67d25652002-01-30 23:20:39 +0000457inline bool PassManagerTraits<BasicBlock>::runOnBasicBlock(BasicBlock *BB) {
Chris Lattner2eaac392002-01-31 00:40:44 +0000458 return ((PMType*)this)->runOnUnit(BB);
459}
460
461inline bool PassManagerTraits<BasicBlock>::doFinalization(Module *M) {
462 bool Changed = false;
463 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
464 ((PMType*)this)->Passes[i]->doFinalization(M);
465 return Changed;
Chris Lattner67d25652002-01-30 23:20:39 +0000466}
467
468
469// PassManagerTraits<Method> Implementations
470//
471inline bool PassManagerTraits<Method>::doInitialization(Module *M) {
472 bool Changed = false;
473 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
474 ((PMType*)this)->Passes[i]->doInitialization(M);
475 return Changed;
476}
477
478inline bool PassManagerTraits<Method>::runOnMethod(Method *M) {
479 return ((PMType*)this)->runOnUnit(M);
480}
481
Chris Lattner67d25652002-01-30 23:20:39 +0000482inline bool PassManagerTraits<Method>::doFinalization(Module *M) {
483 bool Changed = false;
484 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
485 ((PMType*)this)->Passes[i]->doFinalization(M);
486 return Changed;
487}
488
489#endif