blob: c72af14ce2fb1e51e67d8351b41019778dd3039b [file] [log] [blame]
Chris Lattnerd0e9bd12002-04-28 20:42:50 +00001//===- PassManagerT.h - Container for Passes ---------------------*- C++ -*--=//
Chris Lattner67d25652002-01-30 23:20:39 +00002//
Chris Lattnerd0e9bd12002-04-28 20:42:50 +00003// This file defines the PassManagerT class. This class is used to hold,
Chris Lattner67d25652002-01-30 23:20:39 +00004// 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//
Chris Lattnerd0e9bd12002-04-28 20:42:50 +00008// 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.
Chris Lattner67d25652002-01-30 23:20:39 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattnerd0e9bd12002-04-28 20:42:50 +000015#ifndef LLVM_PASSMANAGER_T_H
16#define LLVM_PASSMANAGER_T_H
Chris Lattner67d25652002-01-30 23:20:39 +000017
18#include "llvm/Pass.h"
19#include <string>
Chris Lattnerc8e66542002-04-27 06:56:12 +000020#include <algorithm>
Chris Lattnera454b5b2002-04-28 05:14:06 +000021class Annotable;
Chris Lattner67d25652002-01-30 23:20:39 +000022
Chris Lattner67d25652002-01-30 23:20:39 +000023//===----------------------------------------------------------------------===//
Chris Lattner57d2ba32002-04-04 19:35:24 +000024// PMDebug class - a set of debugging functions, that are not to be
25// instantiated by the template.
Chris Lattner67d25652002-01-30 23:20:39 +000026//
27struct PMDebug {
Chris Lattner67d25652002-01-30 23:20:39 +000028 // If compiled in debug mode, these functions can be enabled by setting
29 // -debug-pass on the command line of the tool being used.
30 //
31 static void PrintPassStructure(Pass *P);
Chris Lattnera454b5b2002-04-28 05:14:06 +000032 static void PrintPassInformation(unsigned,const char*,Pass *, Annotable *);
Chris Lattnerac3e0602002-01-31 18:32:27 +000033 static void PrintAnalysisSetInfo(unsigned,const char*,Pass *P,
Chris Lattnerc8e66542002-04-27 06:56:12 +000034 const std::vector<AnalysisID> &);
Chris Lattner67d25652002-01-30 23:20:39 +000035};
36
37
Chris Lattnere2eb99e2002-04-29 04:04:29 +000038//===----------------------------------------------------------------------===//
39// TimingInfo Class - This class is used to calculate information about the
40// amount of time each pass takes to execute. This only happens when
41// -time-passes is enabled on the command line.
42//
43class TimingInfo {
44 std::map<Pass*, double> TimingData;
45 TimingInfo() {} // Private ctor, must use create member
46public:
47 // Create method. If Timing is enabled, this creates and returns a new timing
48 // object, otherwise it returns null.
49 //
50 static TimingInfo *create();
51
52 // TimingDtor - Print out information about timing information
53 ~TimingInfo();
54
55 void passStarted(Pass *P);
56 void passEnded(Pass *P);
57};
58
59
Chris Lattner67d25652002-01-30 23:20:39 +000060
61//===----------------------------------------------------------------------===//
62// Declare the PassManagerTraits which will be specialized...
63//
64template<class UnitType> class PassManagerTraits; // Do not define.
65
66
67//===----------------------------------------------------------------------===//
68// PassManagerT - Container object for passes. The PassManagerT destructor
69// deletes all passes contained inside of the PassManagerT, so you shouldn't
70// delete passes manually, and all passes should be dynamically allocated.
71//
72template<typename UnitType>
73class PassManagerT : public PassManagerTraits<UnitType>,public AnalysisResolver{
Chris Lattnere2eb99e2002-04-29 04:04:29 +000074 typedef PassManagerTraits<UnitType> Traits;
75 typedef typename Traits::PassClass PassClass;
76 typedef typename Traits::SubPassClass SubPassClass;
77 typedef typename Traits::BatcherClass BatcherClass;
78 typedef typename Traits::ParentClass ParentClass;
Chris Lattner67d25652002-01-30 23:20:39 +000079
Chris Lattnere2eb99e2002-04-29 04:04:29 +000080 friend typename Traits::PassClass;
81 friend typename Traits::SubPassClass;
82 friend class Traits;
Chris Lattner67d25652002-01-30 23:20:39 +000083
84 std::vector<PassClass*> Passes; // List of pass's to run
85
86 // The parent of this pass manager...
Chris Lattnerac3e0602002-01-31 18:32:27 +000087 ParentClass * const Parent;
Chris Lattner67d25652002-01-30 23:20:39 +000088
89 // The current batcher if one is in use, or null
90 BatcherClass *Batcher;
91
92 // CurrentAnalyses - As the passes are being run, this map contains the
93 // analyses that are available to the current pass for use. This is accessed
94 // through the getAnalysis() function in this class and in Pass.
95 //
96 std::map<AnalysisID, Pass*> CurrentAnalyses;
97
Chris Lattnerac3e0602002-01-31 18:32:27 +000098 // LastUseOf - This map keeps track of the last usage in our pipeline of a
99 // particular pass. When executing passes, the memory for .first is free'd
100 // after .second is run.
101 //
102 std::map<Pass*, Pass*> LastUseOf;
103
Chris Lattner67d25652002-01-30 23:20:39 +0000104public:
105 PassManagerT(ParentClass *Par = 0) : Parent(Par), Batcher(0) {}
106 ~PassManagerT() {
107 // Delete all of the contained passes...
108 for (std::vector<PassClass*>::iterator I = Passes.begin(), E = Passes.end();
109 I != E; ++I)
110 delete *I;
111 }
112
113 // run - Run all of the queued passes on the specified module in an optimal
114 // way.
115 virtual bool runOnUnit(UnitType *M) {
116 bool MadeChanges = false;
117 closeBatcher();
118 CurrentAnalyses.clear();
119
Chris Lattnerac3e0602002-01-31 18:32:27 +0000120 // LastUserOf - This contains the inverted LastUseOfMap...
121 std::map<Pass *, std::vector<Pass*> > LastUserOf;
122 for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(),
123 E = LastUseOf.end(); I != E; ++I)
124 LastUserOf[I->second].push_back(I->first);
125
126
Chris Lattner67d25652002-01-30 23:20:39 +0000127 // Output debug information...
128 if (Parent == 0) PMDebug::PrintPassStructure(this);
129
130 // Run all of the passes
131 for (unsigned i = 0, e = Passes.size(); i < e; ++i) {
132 PassClass *P = Passes[i];
133
Chris Lattnera454b5b2002-04-28 05:14:06 +0000134 PMDebug::PrintPassInformation(getDepth(), "Executing Pass", P,
135 (Annotable*)M);
Chris Lattner67d25652002-01-30 23:20:39 +0000136
137 // Get information about what analyses the pass uses...
Chris Lattnerc8e66542002-04-27 06:56:12 +0000138 AnalysisUsage AnUsage;
139 P->getAnalysisUsage(AnUsage);
140 PMDebug::PrintAnalysisSetInfo(getDepth(), "Required", P,
141 AnUsage.getRequiredSet());
Chris Lattner67d25652002-01-30 23:20:39 +0000142
143#ifndef NDEBUG
144 // All Required analyses should be available to the pass as it runs!
Chris Lattnerc8e66542002-04-27 06:56:12 +0000145 for (vector<AnalysisID>::const_iterator
146 I = AnUsage.getRequiredSet().begin(),
147 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Chris Lattner67d25652002-01-30 23:20:39 +0000148 assert(getAnalysisOrNullUp(*I) && "Analysis used but not available!");
149 }
150#endif
151
152 // Run the sub pass!
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000153 startPass(P);
154 bool Changed = runPass(P, M);
155 endPass(P);
Chris Lattnerf2f31bd2002-02-01 04:53:36 +0000156 MadeChanges |= Changed;
Chris Lattner67d25652002-01-30 23:20:39 +0000157
Chris Lattnerf2f31bd2002-02-01 04:53:36 +0000158 if (Changed)
159 PMDebug::PrintPassInformation(getDepth()+1, "Made Modification", P,
Chris Lattnera454b5b2002-04-28 05:14:06 +0000160 (Annotable*)M);
Chris Lattnerc8e66542002-04-27 06:56:12 +0000161 PMDebug::PrintAnalysisSetInfo(getDepth(), "Preserved", P,
162 AnUsage.getPreservedSet());
163 PMDebug::PrintAnalysisSetInfo(getDepth(), "Provided", P,
164 AnUsage.getProvidedSet());
Chris Lattner67d25652002-01-30 23:20:39 +0000165
Chris Lattnerc8e66542002-04-27 06:56:12 +0000166
167 // Erase all analyses not in the preserved set...
168 if (!AnUsage.preservesAll()) {
169 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
170 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(),
171 E = CurrentAnalyses.end(); I != E; )
172 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) !=
173 PreservedSet.end())
174 ++I; // This analysis is preserved, leave it in the available set...
175 else {
176#if MAP_DOESNT_HAVE_BROKEN_ERASE_MEMBER
177 I = CurrentAnalyses.erase(I); // Analysis not preserved!
178#else
179 // GCC 2.95.3 STL doesn't have correct erase member!
180 CurrentAnalyses.erase(I);
181 I = CurrentAnalyses.begin();
182#endif
183 }
184 }
185
Chris Lattner67d25652002-01-30 23:20:39 +0000186 // Add all analyses in the provided set...
Chris Lattnerc8e66542002-04-27 06:56:12 +0000187 for (std::vector<AnalysisID>::const_iterator
188 I = AnUsage.getProvidedSet().begin(),
189 E = AnUsage.getProvidedSet().end(); I != E; ++I)
Chris Lattner67d25652002-01-30 23:20:39 +0000190 CurrentAnalyses[*I] = P;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000191
192 // Free memory for any passes that we are the last use of...
193 std::vector<Pass*> &DeadPass = LastUserOf[P];
194 for (std::vector<Pass*>::iterator I = DeadPass.begin(),E = DeadPass.end();
195 I != E; ++I) {
196 PMDebug::PrintPassInformation(getDepth()+1, "Freeing Pass", *I,
Chris Lattnera454b5b2002-04-28 05:14:06 +0000197 (Annotable*)M);
Chris Lattnerac3e0602002-01-31 18:32:27 +0000198 (*I)->releaseMemory();
199 }
Chris Lattner67d25652002-01-30 23:20:39 +0000200 }
201 return MadeChanges;
202 }
203
Chris Lattnerac3e0602002-01-31 18:32:27 +0000204 // dumpPassStructure - Implement the -debug-passes=PassStructure option
205 virtual void dumpPassStructure(unsigned Offset = 0) {
206 std::cerr << std::string(Offset*2, ' ') << Traits::getPMName()
207 << " Pass Manager\n";
208 for (std::vector<PassClass*>::iterator I = Passes.begin(), E = Passes.end();
209 I != E; ++I) {
210 PassClass *P = *I;
211 P->dumpPassStructure(Offset+1);
212
213 // Loop through and see which classes are destroyed after this one...
214 for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(),
215 E = LastUseOf.end(); I != E; ++I) {
216 if (P == I->second) {
217 std::cerr << "Fr" << std::string(Offset*2, ' ');
218 I->first->dumpPassStructure(0);
219 }
220 }
221 }
222 }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000223
224 Pass *getAnalysisOrNullDown(AnalysisID ID) const {
225 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID);
226 if (I == CurrentAnalyses.end()) {
227 if (Batcher)
228 return ((AnalysisResolver*)Batcher)->getAnalysisOrNullDown(ID);
229 return 0;
230 }
231 return I->second;
232 }
233
234 Pass *getAnalysisOrNullUp(AnalysisID ID) const {
235 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID);
236 if (I == CurrentAnalyses.end()) {
237 if (Parent)
238 return Parent->getAnalysisOrNullUp(ID);
239 return 0;
240 }
241 return I->second;
242 }
243
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000244 // {start/end}Pass - Called when a pass is started, it just propogates
245 // information up to the top level PassManagerT object to tell it that a pass
246 // has started or ended. This is used to gather timing information about
247 // passes.
248 //
249 void startPass(Pass *P) {
250 if (Parent) Parent->startPass(P);
251 else PassStarted(P);
252 }
253 void endPass(Pass *P) {
254 if (Parent) Parent->endPass(P);
255 else PassEnded(P);
256 }
257
Chris Lattnerac3e0602002-01-31 18:32:27 +0000258 // markPassUsed - Inform higher level pass managers (and ourselves)
259 // that these analyses are being used by this pass. This is used to
260 // make sure that analyses are not free'd before we have to use
261 // them...
262 //
263 void markPassUsed(AnalysisID P, Pass *User) {
264 std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.find(P);
265 if (I != CurrentAnalyses.end()) {
266 LastUseOf[I->second] = User; // Local pass, extend the lifetime
267 } else {
268 // Pass not in current available set, must be a higher level pass
269 // available to us, propogate to parent pass manager... We tell the
270 // parent that we (the passmanager) are using the analysis so that it
271 // frees the analysis AFTER this pass manager runs.
272 //
Chris Lattnerde421a72002-03-17 21:16:01 +0000273 assert(Parent != 0 && "Pass available but not found! "
274 "Did your analysis pass 'Provide' itself?");
Chris Lattnerac3e0602002-01-31 18:32:27 +0000275 Parent->markPassUsed(P, this);
276 }
277 }
278
279 // Return the number of parent PassManagers that exist
280 virtual unsigned getDepth() const {
281 if (Parent == 0) return 0;
282 return 1 + Parent->getDepth();
283 }
284
Chris Lattner67d25652002-01-30 23:20:39 +0000285 // add - Add a pass to the queue of passes to run. This passes ownership of
286 // the Pass to the PassManager. When the PassManager is destroyed, the pass
Chris Lattnerac3e0602002-01-31 18:32:27 +0000287 // will be destroyed as well, so there is no need to delete the pass. This
288 // implies that all passes MUST be new'd.
Chris Lattner67d25652002-01-30 23:20:39 +0000289 //
290 void add(PassClass *P) {
291 // Get information about what analyses the pass uses...
Chris Lattnerc8e66542002-04-27 06:56:12 +0000292 AnalysisUsage AnUsage;
293 P->getAnalysisUsage(AnUsage);
294 const std::vector<AnalysisID> &Required = AnUsage.getRequiredSet();
Chris Lattner67d25652002-01-30 23:20:39 +0000295
296 // Loop over all of the analyses used by this pass,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000297 for (std::vector<AnalysisID>::const_iterator I = Required.begin(),
298 E = Required.end(); I != E; ++I) {
Chris Lattner67d25652002-01-30 23:20:39 +0000299 if (getAnalysisOrNullDown(*I) == 0)
Chris Lattner2eaac392002-01-31 00:40:44 +0000300 add((PassClass*)I->createPass());
Chris Lattner67d25652002-01-30 23:20:39 +0000301 }
302
303 // Tell the pass to add itself to this PassManager... the way it does so
304 // depends on the class of the pass, and is critical to laying out passes in
305 // an optimal order..
306 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000307 P->addToPassManager(this, AnUsage);
Chris Lattner67d25652002-01-30 23:20:39 +0000308 }
309
310private:
311
312 // addPass - These functions are used to implement the subclass specific
313 // behaviors present in PassManager. Basically the add(Pass*) method ends up
314 // reflecting its behavior into a Pass::addToPassManager call. Subclasses of
315 // Pass override it specifically so that they can reflect the type
316 // information inherent in "this" back to the PassManager.
317 //
318 // For generic Pass subclasses (which are interprocedural passes), we simply
319 // add the pass to the end of the pass list and terminate any accumulation of
Chris Lattnerc8e66542002-04-27 06:56:12 +0000320 // FunctionPass's that are present.
Chris Lattner67d25652002-01-30 23:20:39 +0000321 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000322 void addPass(PassClass *P, AnalysisUsage &AnUsage) {
323 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
324 const std::vector<AnalysisID> &ProvidedSet = AnUsage.getProvidedSet();
325
Chris Lattner67d25652002-01-30 23:20:39 +0000326 // Providers are analysis classes which are forbidden to modify the module
327 // they are operating on, so they are allowed to be reordered to before the
328 // batcher...
329 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000330 if (Batcher && ProvidedSet.empty())
Chris Lattner67d25652002-01-30 23:20:39 +0000331 closeBatcher(); // This pass cannot be batched!
332
333 // Set the Resolver instance variable in the Pass so that it knows where to
334 // find this object...
335 //
336 setAnalysisResolver(P, this);
337 Passes.push_back(P);
338
Chris Lattnerac3e0602002-01-31 18:32:27 +0000339 // Inform higher level pass managers (and ourselves) that these analyses are
340 // being used by this pass. This is used to make sure that analyses are not
341 // free'd before we have to use them...
342 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000343 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
344 E = RequiredSet.end(); I != E; ++I)
Chris Lattnerac3e0602002-01-31 18:32:27 +0000345 markPassUsed(*I, P); // Mark *I as used by P
346
Chris Lattnerc8e66542002-04-27 06:56:12 +0000347 // Erase all analyses not in the preserved set...
348 if (!AnUsage.preservesAll()) {
349 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
350 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(),
351 E = CurrentAnalyses.end(); I != E; )
352 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) !=
353 PreservedSet.end())
354 ++I; // This analysis is preserved, leave it in the available set...
355 else {
356#if MAP_DOESNT_HAVE_BROKEN_ERASE_MEMBER
357 I = CurrentAnalyses.erase(I); // Analysis not preserved!
358#else
359 CurrentAnalyses.erase(I);// GCC 2.95.3 STL doesn't have correct erase!
360 I = CurrentAnalyses.begin();
361#endif
362 }
363 }
Chris Lattner67d25652002-01-30 23:20:39 +0000364
365 // Add all analyses in the provided set...
Chris Lattnerc8e66542002-04-27 06:56:12 +0000366 for (std::vector<AnalysisID>::const_iterator I = ProvidedSet.begin(),
367 E = ProvidedSet.end(); I != E; ++I)
Chris Lattner67d25652002-01-30 23:20:39 +0000368 CurrentAnalyses[*I] = P;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000369
370 // For now assume that our results are never used...
371 LastUseOf[P] = P;
Chris Lattner67d25652002-01-30 23:20:39 +0000372 }
373
Chris Lattnerc8e66542002-04-27 06:56:12 +0000374 // For FunctionPass subclasses, we must be sure to batch the FunctionPass's
375 // together in a BatcherClass object so that all of the analyses are run
376 // together a function at a time.
Chris Lattner67d25652002-01-30 23:20:39 +0000377 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000378 void addPass(SubPassClass *MP, AnalysisUsage &AnUsage) {
Chris Lattner67d25652002-01-30 23:20:39 +0000379 if (Batcher == 0) // If we don't have a batcher yet, make one now.
380 Batcher = new BatcherClass(this);
381 // The Batcher will queue them passes up
Chris Lattnerc8e66542002-04-27 06:56:12 +0000382 MP->addToPassManager(Batcher, AnUsage);
Chris Lattner67d25652002-01-30 23:20:39 +0000383 }
384
385 // closeBatcher - Terminate the batcher that is being worked on.
386 void closeBatcher() {
387 if (Batcher) {
388 Passes.push_back(Batcher);
389 Batcher = 0;
390 }
391 }
392};
393
394
395
396//===----------------------------------------------------------------------===//
397// PassManagerTraits<BasicBlock> Specialization
398//
399// This pass manager is used to group together all of the BasicBlockPass's
400// into a single unit.
401//
402template<> struct PassManagerTraits<BasicBlock> : public BasicBlockPass {
403 // PassClass - The type of passes tracked by this PassManager
404 typedef BasicBlockPass PassClass;
405
406 // SubPassClass - The types of classes that should be collated together
407 // This is impossible to match, so BasicBlock instantiations of PassManagerT
408 // do not collate.
409 //
410 typedef PassManagerT<Module> SubPassClass;
411
412 // BatcherClass - The type to use for collation of subtypes... This class is
413 // never instantiated for the PassManager<BasicBlock>, but it must be an
414 // instance of PassClass to typecheck.
415 //
416 typedef PassClass BatcherClass;
417
418 // ParentClass - The type of the parent PassManager...
Chris Lattner4e8c4872002-03-23 22:51:58 +0000419 typedef PassManagerT<Function> ParentClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000420
Chris Lattner2eaac392002-01-31 00:40:44 +0000421 // PMType - The type of the passmanager that subclasses this class
422 typedef PassManagerT<BasicBlock> PMType;
423
Chris Lattner67d25652002-01-30 23:20:39 +0000424 // runPass - Specify how the pass should be run on the UnitType
425 static bool runPass(PassClass *P, BasicBlock *M) {
426 // todo, init and finalize
427 return P->runOnBasicBlock(M);
428 }
429
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000430 // Dummy implementation of PassStarted/PassEnded
431 static void PassStarted(Pass *P) {}
432 static void PassEnded(Pass *P) {}
433
Chris Lattnerac3e0602002-01-31 18:32:27 +0000434 // getPMName() - Return the name of the unit the PassManager operates on for
435 // debugging.
436 const char *getPMName() const { return "BasicBlock"; }
Chris Lattner37104aa2002-04-29 14:57:45 +0000437 virtual const char *getPassName() const { return "BasicBlock Pass Manager"; }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000438
Chris Lattner2eaac392002-01-31 00:40:44 +0000439 // Implement the BasicBlockPass interface...
440 virtual bool doInitialization(Module *M);
Chris Lattner67d25652002-01-30 23:20:39 +0000441 virtual bool runOnBasicBlock(BasicBlock *BB);
Chris Lattner2eaac392002-01-31 00:40:44 +0000442 virtual bool doFinalization(Module *M);
Chris Lattner67d25652002-01-30 23:20:39 +0000443};
444
445
446
447//===----------------------------------------------------------------------===//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000448// PassManagerTraits<Function> Specialization
Chris Lattner67d25652002-01-30 23:20:39 +0000449//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000450// This pass manager is used to group together all of the FunctionPass's
Chris Lattner67d25652002-01-30 23:20:39 +0000451// into a single unit.
452//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000453template<> struct PassManagerTraits<Function> : public FunctionPass {
Chris Lattner67d25652002-01-30 23:20:39 +0000454 // PassClass - The type of passes tracked by this PassManager
Chris Lattnerc8e66542002-04-27 06:56:12 +0000455 typedef FunctionPass PassClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000456
457 // SubPassClass - The types of classes that should be collated together
458 typedef BasicBlockPass SubPassClass;
459
460 // BatcherClass - The type to use for collation of subtypes...
461 typedef PassManagerT<BasicBlock> BatcherClass;
462
463 // ParentClass - The type of the parent PassManager...
464 typedef PassManagerT<Module> ParentClass;
465
466 // PMType - The type of the passmanager that subclasses this class
Chris Lattner4e8c4872002-03-23 22:51:58 +0000467 typedef PassManagerT<Function> PMType;
Chris Lattner67d25652002-01-30 23:20:39 +0000468
469 // runPass - Specify how the pass should be run on the UnitType
Chris Lattnerc8e66542002-04-27 06:56:12 +0000470 static bool runPass(PassClass *P, Function *F) {
471 return P->runOnFunction(F);
Chris Lattner67d25652002-01-30 23:20:39 +0000472 }
473
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000474 // Dummy implementation of PassStarted/PassEnded
475 static void PassStarted(Pass *P) {}
476 static void PassEnded(Pass *P) {}
477
Chris Lattnerac3e0602002-01-31 18:32:27 +0000478 // getPMName() - Return the name of the unit the PassManager operates on for
479 // debugging.
Chris Lattner4e8c4872002-03-23 22:51:58 +0000480 const char *getPMName() const { return "Function"; }
Chris Lattner37104aa2002-04-29 14:57:45 +0000481 virtual const char *getPassName() const { return "Function Pass Manager"; }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000482
Chris Lattnerc8e66542002-04-27 06:56:12 +0000483 // Implement the FunctionPass interface...
Chris Lattner67d25652002-01-30 23:20:39 +0000484 virtual bool doInitialization(Module *M);
Chris Lattnerc8e66542002-04-27 06:56:12 +0000485 virtual bool runOnFunction(Function *F);
Chris Lattner67d25652002-01-30 23:20:39 +0000486 virtual bool doFinalization(Module *M);
487};
488
489
490
491//===----------------------------------------------------------------------===//
492// PassManagerTraits<Module> Specialization
493//
494// This is the top level PassManager implementation that holds generic passes.
495//
496template<> struct PassManagerTraits<Module> : public Pass {
497 // PassClass - The type of passes tracked by this PassManager
498 typedef Pass PassClass;
499
500 // SubPassClass - The types of classes that should be collated together
Chris Lattnerc8e66542002-04-27 06:56:12 +0000501 typedef FunctionPass SubPassClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000502
503 // BatcherClass - The type to use for collation of subtypes...
Chris Lattner4e8c4872002-03-23 22:51:58 +0000504 typedef PassManagerT<Function> BatcherClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000505
506 // ParentClass - The type of the parent PassManager...
Chris Lattnerac3e0602002-01-31 18:32:27 +0000507 typedef AnalysisResolver ParentClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000508
509 // runPass - Specify how the pass should be run on the UnitType
510 static bool runPass(PassClass *P, Module *M) { return P->run(M); }
511
Chris Lattnerac3e0602002-01-31 18:32:27 +0000512 // getPMName() - Return the name of the unit the PassManager operates on for
513 // debugging.
514 const char *getPMName() const { return "Module"; }
Chris Lattner37104aa2002-04-29 14:57:45 +0000515 virtual const char *getPassName() const { return "Module Pass Manager"; }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000516
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000517 // TimingInformation - This data member maintains timing information for each
518 // of the passes that is executed.
519 //
520 TimingInfo *TimeInfo;
521
522 // PassStarted/Ended - This callback is notified any time a pass is started
523 // or stops. This is used to collect timing information about the different
524 // passes being executed.
525 //
526 void PassStarted(Pass *P) {
527 if (TimeInfo) TimeInfo->passStarted(P);
Chris Lattner67d25652002-01-30 23:20:39 +0000528 }
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000529 void PassEnded(Pass *P) {
530 if (TimeInfo) TimeInfo->passEnded(P);
531 }
532
533 // run - Implement the PassManager interface...
534 bool run(Module *M) {
535 TimeInfo = TimingInfo::create();
536 bool Result = ((PassManagerT<Module>*)this)->runOnUnit(M);
537 if (TimeInfo) {
538 delete TimeInfo;
539 TimeInfo = 0;
540 }
541 return Result;
542 }
543
544 // PassManagerTraits constructor - Create a timing info object if the user
545 // specified timing info should be collected on the command line.
546 //
547 PassManagerTraits() : TimeInfo(0) {}
Chris Lattner67d25652002-01-30 23:20:39 +0000548};
549
550
551
552//===----------------------------------------------------------------------===//
553// PassManagerTraits Method Implementations
554//
555
556// PassManagerTraits<BasicBlock> Implementations
557//
Chris Lattner2eaac392002-01-31 00:40:44 +0000558inline bool PassManagerTraits<BasicBlock>::doInitialization(Module *M) {
559 bool Changed = false;
560 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
561 ((PMType*)this)->Passes[i]->doInitialization(M);
562 return Changed;
563}
564
Chris Lattner67d25652002-01-30 23:20:39 +0000565inline bool PassManagerTraits<BasicBlock>::runOnBasicBlock(BasicBlock *BB) {
Chris Lattner2eaac392002-01-31 00:40:44 +0000566 return ((PMType*)this)->runOnUnit(BB);
567}
568
569inline bool PassManagerTraits<BasicBlock>::doFinalization(Module *M) {
570 bool Changed = false;
571 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
572 ((PMType*)this)->Passes[i]->doFinalization(M);
573 return Changed;
Chris Lattner67d25652002-01-30 23:20:39 +0000574}
575
576
Chris Lattner4e8c4872002-03-23 22:51:58 +0000577// PassManagerTraits<Function> Implementations
Chris Lattner67d25652002-01-30 23:20:39 +0000578//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000579inline bool PassManagerTraits<Function>::doInitialization(Module *M) {
Chris Lattner67d25652002-01-30 23:20:39 +0000580 bool Changed = false;
581 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
582 ((PMType*)this)->Passes[i]->doInitialization(M);
583 return Changed;
584}
585
Chris Lattnerc8e66542002-04-27 06:56:12 +0000586inline bool PassManagerTraits<Function>::runOnFunction(Function *F) {
587 return ((PMType*)this)->runOnUnit(F);
Chris Lattner67d25652002-01-30 23:20:39 +0000588}
589
Chris Lattner4e8c4872002-03-23 22:51:58 +0000590inline bool PassManagerTraits<Function>::doFinalization(Module *M) {
Chris Lattner67d25652002-01-30 23:20:39 +0000591 bool Changed = false;
592 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
593 ((PMType*)this)->Passes[i]->doFinalization(M);
594 return Changed;
595}
596
597#endif