blob: 555bcaae1062ff60e41df5b3b1c278405d59bc6d [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"
Chris Lattner198cf422002-07-30 16:27:02 +000019#include "Support/CommandLine.h"
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 Lattner198cf422002-07-30 16:27:02 +000024// Pass debugging information. Often it is useful to find out what pass is
25// running when a crash occurs in a utility. When this library is compiled with
26// debugging on, a command line option (--debug-pass) is enabled that causes the
27// pass name to be printed before it executes.
28//
29
30// Different debug levels that can be enabled...
31enum PassDebugLevel {
32 None, Structure, Executions, Details
33};
34
35static cl::opt<enum PassDebugLevel>
36PassDebugging("debug-pass", cl::Hidden,
37 cl::desc("Print PassManager debugging information"),
38 cl::values(
39 clEnumVal(None , "disable debug output"),
40 // TODO: add option to print out pass names "PassOptions"
41 clEnumVal(Structure , "print pass structure before run()"),
42 clEnumVal(Executions, "print pass name before it is executed"),
43 clEnumVal(Details , "print pass details when it is executed"),
44 0));
45
46//===----------------------------------------------------------------------===//
Chris Lattner57d2ba32002-04-04 19:35:24 +000047// PMDebug class - a set of debugging functions, that are not to be
48// instantiated by the template.
Chris Lattner67d25652002-01-30 23:20:39 +000049//
50struct PMDebug {
Chris Lattner67d25652002-01-30 23:20:39 +000051 // If compiled in debug mode, these functions can be enabled by setting
52 // -debug-pass on the command line of the tool being used.
53 //
Chris Lattner198cf422002-07-30 16:27:02 +000054 static void PrintPassStructure(Pass *P) {
55 if (PassDebugging >= Structure)
56 P->dumpPassStructure();
57 }
Chris Lattnera454b5b2002-04-28 05:14:06 +000058 static void PrintPassInformation(unsigned,const char*,Pass *, Annotable *);
Chris Lattnerac3e0602002-01-31 18:32:27 +000059 static void PrintAnalysisSetInfo(unsigned,const char*,Pass *P,
Chris Lattnerc8e66542002-04-27 06:56:12 +000060 const std::vector<AnalysisID> &);
Chris Lattner67d25652002-01-30 23:20:39 +000061};
62
63
Chris Lattnere2eb99e2002-04-29 04:04:29 +000064//===----------------------------------------------------------------------===//
65// TimingInfo Class - This class is used to calculate information about the
66// amount of time each pass takes to execute. This only happens when
67// -time-passes is enabled on the command line.
68//
69class TimingInfo {
70 std::map<Pass*, double> TimingData;
71 TimingInfo() {} // Private ctor, must use create member
72public:
73 // Create method. If Timing is enabled, this creates and returns a new timing
74 // object, otherwise it returns null.
75 //
76 static TimingInfo *create();
77
78 // TimingDtor - Print out information about timing information
79 ~TimingInfo();
80
81 void passStarted(Pass *P);
82 void passEnded(Pass *P);
83};
84
85
Chris Lattner67d25652002-01-30 23:20:39 +000086
87//===----------------------------------------------------------------------===//
88// Declare the PassManagerTraits which will be specialized...
89//
90template<class UnitType> class PassManagerTraits; // Do not define.
91
92
93//===----------------------------------------------------------------------===//
94// PassManagerT - Container object for passes. The PassManagerT destructor
95// deletes all passes contained inside of the PassManagerT, so you shouldn't
96// delete passes manually, and all passes should be dynamically allocated.
97//
98template<typename UnitType>
99class PassManagerT : public PassManagerTraits<UnitType>,public AnalysisResolver{
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000100 typedef PassManagerTraits<UnitType> Traits;
101 typedef typename Traits::PassClass PassClass;
102 typedef typename Traits::SubPassClass SubPassClass;
103 typedef typename Traits::BatcherClass BatcherClass;
104 typedef typename Traits::ParentClass ParentClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000105
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000106 friend typename Traits::PassClass;
107 friend typename Traits::SubPassClass;
108 friend class Traits;
Chris Lattner67d25652002-01-30 23:20:39 +0000109
Chris Lattner73503172002-07-29 21:03:38 +0000110 std::vector<PassClass*> Passes; // List of passes to run
Chris Lattner67d25652002-01-30 23:20:39 +0000111
112 // The parent of this pass manager...
Chris Lattnerac3e0602002-01-31 18:32:27 +0000113 ParentClass * const Parent;
Chris Lattner67d25652002-01-30 23:20:39 +0000114
115 // The current batcher if one is in use, or null
116 BatcherClass *Batcher;
117
118 // CurrentAnalyses - As the passes are being run, this map contains the
119 // analyses that are available to the current pass for use. This is accessed
120 // through the getAnalysis() function in this class and in Pass.
121 //
122 std::map<AnalysisID, Pass*> CurrentAnalyses;
123
Chris Lattnerac3e0602002-01-31 18:32:27 +0000124 // LastUseOf - This map keeps track of the last usage in our pipeline of a
125 // particular pass. When executing passes, the memory for .first is free'd
126 // after .second is run.
127 //
128 std::map<Pass*, Pass*> LastUseOf;
129
Chris Lattner67d25652002-01-30 23:20:39 +0000130public:
131 PassManagerT(ParentClass *Par = 0) : Parent(Par), Batcher(0) {}
132 ~PassManagerT() {
133 // Delete all of the contained passes...
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000134 for (typename std::vector<PassClass*>::iterator
135 I = Passes.begin(), E = Passes.end(); I != E; ++I)
Chris Lattner67d25652002-01-30 23:20:39 +0000136 delete *I;
137 }
138
139 // run - Run all of the queued passes on the specified module in an optimal
140 // way.
141 virtual bool runOnUnit(UnitType *M) {
142 bool MadeChanges = false;
143 closeBatcher();
144 CurrentAnalyses.clear();
145
Chris Lattnerac3e0602002-01-31 18:32:27 +0000146 // LastUserOf - This contains the inverted LastUseOfMap...
147 std::map<Pass *, std::vector<Pass*> > LastUserOf;
148 for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(),
149 E = LastUseOf.end(); I != E; ++I)
150 LastUserOf[I->second].push_back(I->first);
151
152
Chris Lattner67d25652002-01-30 23:20:39 +0000153 // Output debug information...
154 if (Parent == 0) PMDebug::PrintPassStructure(this);
155
156 // Run all of the passes
157 for (unsigned i = 0, e = Passes.size(); i < e; ++i) {
158 PassClass *P = Passes[i];
159
Chris Lattnera454b5b2002-04-28 05:14:06 +0000160 PMDebug::PrintPassInformation(getDepth(), "Executing Pass", P,
161 (Annotable*)M);
Chris Lattner67d25652002-01-30 23:20:39 +0000162
163 // Get information about what analyses the pass uses...
Chris Lattnerc8e66542002-04-27 06:56:12 +0000164 AnalysisUsage AnUsage;
165 P->getAnalysisUsage(AnUsage);
166 PMDebug::PrintAnalysisSetInfo(getDepth(), "Required", P,
167 AnUsage.getRequiredSet());
Chris Lattner67d25652002-01-30 23:20:39 +0000168
169#ifndef NDEBUG
170 // All Required analyses should be available to the pass as it runs!
Anand Shukla8c377892002-06-25 22:07:38 +0000171 for (std::vector<AnalysisID>::const_iterator
Chris Lattnerc8e66542002-04-27 06:56:12 +0000172 I = AnUsage.getRequiredSet().begin(),
173 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Chris Lattner67d25652002-01-30 23:20:39 +0000174 assert(getAnalysisOrNullUp(*I) && "Analysis used but not available!");
175 }
176#endif
177
178 // Run the sub pass!
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000179 startPass(P);
180 bool Changed = runPass(P, M);
181 endPass(P);
Chris Lattnerf2f31bd2002-02-01 04:53:36 +0000182 MadeChanges |= Changed;
Chris Lattner67d25652002-01-30 23:20:39 +0000183
Chris Lattnerf2f31bd2002-02-01 04:53:36 +0000184 if (Changed)
185 PMDebug::PrintPassInformation(getDepth()+1, "Made Modification", P,
Chris Lattnera454b5b2002-04-28 05:14:06 +0000186 (Annotable*)M);
Chris Lattnerc8e66542002-04-27 06:56:12 +0000187 PMDebug::PrintAnalysisSetInfo(getDepth(), "Preserved", P,
188 AnUsage.getPreservedSet());
Chris Lattner67d25652002-01-30 23:20:39 +0000189
Chris Lattnerc8e66542002-04-27 06:56:12 +0000190
191 // Erase all analyses not in the preserved set...
192 if (!AnUsage.preservesAll()) {
193 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
194 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(),
195 E = CurrentAnalyses.end(); I != E; )
196 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) !=
197 PreservedSet.end())
198 ++I; // This analysis is preserved, leave it in the available set...
199 else {
200#if MAP_DOESNT_HAVE_BROKEN_ERASE_MEMBER
201 I = CurrentAnalyses.erase(I); // Analysis not preserved!
202#else
203 // GCC 2.95.3 STL doesn't have correct erase member!
204 CurrentAnalyses.erase(I);
205 I = CurrentAnalyses.begin();
206#endif
207 }
208 }
209
Chris Lattner73503172002-07-29 21:03:38 +0000210 // Add the current pass to the set of passes that have been run, and are
211 // thus available to users.
212 //
213 if (const PassInfo *PI = P->getPassInfo())
214 CurrentAnalyses[PI] = P;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000215
216 // Free memory for any passes that we are the last use of...
217 std::vector<Pass*> &DeadPass = LastUserOf[P];
218 for (std::vector<Pass*>::iterator I = DeadPass.begin(),E = DeadPass.end();
219 I != E; ++I) {
220 PMDebug::PrintPassInformation(getDepth()+1, "Freeing Pass", *I,
Chris Lattnera454b5b2002-04-28 05:14:06 +0000221 (Annotable*)M);
Chris Lattnerac3e0602002-01-31 18:32:27 +0000222 (*I)->releaseMemory();
223 }
Chris Lattner67d25652002-01-30 23:20:39 +0000224 }
225 return MadeChanges;
226 }
227
Chris Lattnerac3e0602002-01-31 18:32:27 +0000228 // dumpPassStructure - Implement the -debug-passes=PassStructure option
229 virtual void dumpPassStructure(unsigned Offset = 0) {
230 std::cerr << std::string(Offset*2, ' ') << Traits::getPMName()
231 << " Pass Manager\n";
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000232 for (typename std::vector<PassClass*>::iterator
233 I = Passes.begin(), E = Passes.end(); I != E; ++I) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000234 PassClass *P = *I;
235 P->dumpPassStructure(Offset+1);
236
237 // Loop through and see which classes are destroyed after this one...
238 for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(),
239 E = LastUseOf.end(); I != E; ++I) {
240 if (P == I->second) {
Chris Lattner73503172002-07-29 21:03:38 +0000241 std::cerr << "--" << std::string(Offset*2, ' ');
Chris Lattnerac3e0602002-01-31 18:32:27 +0000242 I->first->dumpPassStructure(0);
243 }
244 }
245 }
246 }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000247
248 Pass *getAnalysisOrNullDown(AnalysisID ID) const {
249 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID);
250 if (I == CurrentAnalyses.end()) {
251 if (Batcher)
252 return ((AnalysisResolver*)Batcher)->getAnalysisOrNullDown(ID);
253 return 0;
254 }
255 return I->second;
256 }
257
258 Pass *getAnalysisOrNullUp(AnalysisID ID) const {
259 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID);
260 if (I == CurrentAnalyses.end()) {
261 if (Parent)
262 return Parent->getAnalysisOrNullUp(ID);
263 return 0;
264 }
265 return I->second;
266 }
267
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000268 // {start/end}Pass - Called when a pass is started, it just propogates
269 // information up to the top level PassManagerT object to tell it that a pass
270 // has started or ended. This is used to gather timing information about
271 // passes.
272 //
273 void startPass(Pass *P) {
274 if (Parent) Parent->startPass(P);
275 else PassStarted(P);
276 }
277 void endPass(Pass *P) {
278 if (Parent) Parent->endPass(P);
279 else PassEnded(P);
280 }
281
Chris Lattnerac3e0602002-01-31 18:32:27 +0000282 // markPassUsed - Inform higher level pass managers (and ourselves)
283 // that these analyses are being used by this pass. This is used to
284 // make sure that analyses are not free'd before we have to use
285 // them...
286 //
287 void markPassUsed(AnalysisID P, Pass *User) {
288 std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.find(P);
289 if (I != CurrentAnalyses.end()) {
290 LastUseOf[I->second] = User; // Local pass, extend the lifetime
291 } else {
292 // Pass not in current available set, must be a higher level pass
293 // available to us, propogate to parent pass manager... We tell the
294 // parent that we (the passmanager) are using the analysis so that it
295 // frees the analysis AFTER this pass manager runs.
296 //
Chris Lattnerde421a72002-03-17 21:16:01 +0000297 assert(Parent != 0 && "Pass available but not found! "
298 "Did your analysis pass 'Provide' itself?");
Chris Lattnerac3e0602002-01-31 18:32:27 +0000299 Parent->markPassUsed(P, this);
300 }
301 }
302
303 // Return the number of parent PassManagers that exist
304 virtual unsigned getDepth() const {
305 if (Parent == 0) return 0;
306 return 1 + Parent->getDepth();
307 }
308
Chris Lattner67d25652002-01-30 23:20:39 +0000309 // add - Add a pass to the queue of passes to run. This passes ownership of
310 // the Pass to the PassManager. When the PassManager is destroyed, the pass
Chris Lattnerac3e0602002-01-31 18:32:27 +0000311 // will be destroyed as well, so there is no need to delete the pass. This
312 // implies that all passes MUST be new'd.
Chris Lattner67d25652002-01-30 23:20:39 +0000313 //
314 void add(PassClass *P) {
315 // Get information about what analyses the pass uses...
Chris Lattnerc8e66542002-04-27 06:56:12 +0000316 AnalysisUsage AnUsage;
317 P->getAnalysisUsage(AnUsage);
318 const std::vector<AnalysisID> &Required = AnUsage.getRequiredSet();
Chris Lattner67d25652002-01-30 23:20:39 +0000319
320 // Loop over all of the analyses used by this pass,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000321 for (std::vector<AnalysisID>::const_iterator I = Required.begin(),
322 E = Required.end(); I != E; ++I) {
Chris Lattner67d25652002-01-30 23:20:39 +0000323 if (getAnalysisOrNullDown(*I) == 0)
Chris Lattner26750072002-07-27 01:12:17 +0000324 add((PassClass*)(*I)->createPass());
Chris Lattner67d25652002-01-30 23:20:39 +0000325 }
326
327 // Tell the pass to add itself to this PassManager... the way it does so
328 // depends on the class of the pass, and is critical to laying out passes in
329 // an optimal order..
330 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000331 P->addToPassManager(this, AnUsage);
Chris Lattner67d25652002-01-30 23:20:39 +0000332 }
333
334private:
335
336 // addPass - These functions are used to implement the subclass specific
337 // behaviors present in PassManager. Basically the add(Pass*) method ends up
338 // reflecting its behavior into a Pass::addToPassManager call. Subclasses of
339 // Pass override it specifically so that they can reflect the type
340 // information inherent in "this" back to the PassManager.
341 //
342 // For generic Pass subclasses (which are interprocedural passes), we simply
343 // add the pass to the end of the pass list and terminate any accumulation of
Chris Lattnerc8e66542002-04-27 06:56:12 +0000344 // FunctionPass's that are present.
Chris Lattner67d25652002-01-30 23:20:39 +0000345 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000346 void addPass(PassClass *P, AnalysisUsage &AnUsage) {
347 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
Chris Lattnerc8e66542002-04-27 06:56:12 +0000348
Chris Lattner73503172002-07-29 21:03:38 +0000349 // FIXME: If this pass being added isn't killed by any of the passes in the
350 // batcher class then we can reorder to pass to execute before the batcher
351 // does, which will potentially allow us to batch more passes!
Chris Lattner67d25652002-01-30 23:20:39 +0000352 //
Chris Lattner73503172002-07-29 21:03:38 +0000353 //const std::vector<AnalysisID> &ProvidedSet = AnUsage.getProvidedSet();
354 if (Batcher /*&& ProvidedSet.empty()*/)
Chris Lattner67d25652002-01-30 23:20:39 +0000355 closeBatcher(); // This pass cannot be batched!
356
357 // Set the Resolver instance variable in the Pass so that it knows where to
358 // find this object...
359 //
360 setAnalysisResolver(P, this);
361 Passes.push_back(P);
362
Chris Lattnerac3e0602002-01-31 18:32:27 +0000363 // Inform higher level pass managers (and ourselves) that these analyses are
364 // being used by this pass. This is used to make sure that analyses are not
365 // free'd before we have to use them...
366 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000367 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
368 E = RequiredSet.end(); I != E; ++I)
Chris Lattnerac3e0602002-01-31 18:32:27 +0000369 markPassUsed(*I, P); // Mark *I as used by P
370
Chris Lattnerc8e66542002-04-27 06:56:12 +0000371 // Erase all analyses not in the preserved set...
372 if (!AnUsage.preservesAll()) {
373 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
374 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(),
375 E = CurrentAnalyses.end(); I != E; )
376 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) !=
377 PreservedSet.end())
378 ++I; // This analysis is preserved, leave it in the available set...
379 else {
380#if MAP_DOESNT_HAVE_BROKEN_ERASE_MEMBER
381 I = CurrentAnalyses.erase(I); // Analysis not preserved!
382#else
383 CurrentAnalyses.erase(I);// GCC 2.95.3 STL doesn't have correct erase!
384 I = CurrentAnalyses.begin();
385#endif
386 }
387 }
Chris Lattner67d25652002-01-30 23:20:39 +0000388
Chris Lattner73503172002-07-29 21:03:38 +0000389 // Add this pass to the currently available set...
390 if (const PassInfo *PI = P->getPassInfo())
391 CurrentAnalyses[PI] = P;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000392
393 // For now assume that our results are never used...
394 LastUseOf[P] = P;
Chris Lattner67d25652002-01-30 23:20:39 +0000395 }
396
Chris Lattnerc8e66542002-04-27 06:56:12 +0000397 // For FunctionPass subclasses, we must be sure to batch the FunctionPass's
398 // together in a BatcherClass object so that all of the analyses are run
399 // together a function at a time.
Chris Lattner67d25652002-01-30 23:20:39 +0000400 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000401 void addPass(SubPassClass *MP, AnalysisUsage &AnUsage) {
Chris Lattner67d25652002-01-30 23:20:39 +0000402 if (Batcher == 0) // If we don't have a batcher yet, make one now.
403 Batcher = new BatcherClass(this);
Chris Lattner73503172002-07-29 21:03:38 +0000404 // The Batcher will queue the passes up
Chris Lattnerc8e66542002-04-27 06:56:12 +0000405 MP->addToPassManager(Batcher, AnUsage);
Chris Lattner67d25652002-01-30 23:20:39 +0000406 }
407
408 // closeBatcher - Terminate the batcher that is being worked on.
409 void closeBatcher() {
410 if (Batcher) {
411 Passes.push_back(Batcher);
412 Batcher = 0;
413 }
414 }
415};
416
417
418
419//===----------------------------------------------------------------------===//
420// PassManagerTraits<BasicBlock> Specialization
421//
422// This pass manager is used to group together all of the BasicBlockPass's
423// into a single unit.
424//
425template<> struct PassManagerTraits<BasicBlock> : public BasicBlockPass {
426 // PassClass - The type of passes tracked by this PassManager
427 typedef BasicBlockPass PassClass;
428
429 // SubPassClass - The types of classes that should be collated together
430 // This is impossible to match, so BasicBlock instantiations of PassManagerT
431 // do not collate.
432 //
433 typedef PassManagerT<Module> SubPassClass;
434
435 // BatcherClass - The type to use for collation of subtypes... This class is
436 // never instantiated for the PassManager<BasicBlock>, but it must be an
437 // instance of PassClass to typecheck.
438 //
439 typedef PassClass BatcherClass;
440
441 // ParentClass - The type of the parent PassManager...
Chris Lattner4e8c4872002-03-23 22:51:58 +0000442 typedef PassManagerT<Function> ParentClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000443
Chris Lattner2eaac392002-01-31 00:40:44 +0000444 // PMType - The type of the passmanager that subclasses this class
445 typedef PassManagerT<BasicBlock> PMType;
446
Chris Lattner67d25652002-01-30 23:20:39 +0000447 // runPass - Specify how the pass should be run on the UnitType
448 static bool runPass(PassClass *P, BasicBlock *M) {
449 // todo, init and finalize
Chris Lattner113f4f42002-06-25 16:13:24 +0000450 return P->runOnBasicBlock(*M);
Chris Lattner67d25652002-01-30 23:20:39 +0000451 }
452
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000453 // Dummy implementation of PassStarted/PassEnded
454 static void PassStarted(Pass *P) {}
455 static void PassEnded(Pass *P) {}
456
Chris Lattnerac3e0602002-01-31 18:32:27 +0000457 // getPMName() - Return the name of the unit the PassManager operates on for
458 // debugging.
459 const char *getPMName() const { return "BasicBlock"; }
Chris Lattner37104aa2002-04-29 14:57:45 +0000460 virtual const char *getPassName() const { return "BasicBlock Pass Manager"; }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000461
Chris Lattner2eaac392002-01-31 00:40:44 +0000462 // Implement the BasicBlockPass interface...
Chris Lattner113f4f42002-06-25 16:13:24 +0000463 virtual bool doInitialization(Module &M);
464 virtual bool runOnBasicBlock(BasicBlock &BB);
465 virtual bool doFinalization(Module &M);
Chris Lattnercb429062002-04-30 18:50:17 +0000466
467 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
468 AU.setPreservesAll();
469 }
Chris Lattner67d25652002-01-30 23:20:39 +0000470};
471
472
473
474//===----------------------------------------------------------------------===//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000475// PassManagerTraits<Function> Specialization
Chris Lattner67d25652002-01-30 23:20:39 +0000476//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000477// This pass manager is used to group together all of the FunctionPass's
Chris Lattner67d25652002-01-30 23:20:39 +0000478// into a single unit.
479//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000480template<> struct PassManagerTraits<Function> : public FunctionPass {
Chris Lattner67d25652002-01-30 23:20:39 +0000481 // PassClass - The type of passes tracked by this PassManager
Chris Lattnerc8e66542002-04-27 06:56:12 +0000482 typedef FunctionPass PassClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000483
484 // SubPassClass - The types of classes that should be collated together
485 typedef BasicBlockPass SubPassClass;
486
487 // BatcherClass - The type to use for collation of subtypes...
488 typedef PassManagerT<BasicBlock> BatcherClass;
489
490 // ParentClass - The type of the parent PassManager...
491 typedef PassManagerT<Module> ParentClass;
492
493 // PMType - The type of the passmanager that subclasses this class
Chris Lattner4e8c4872002-03-23 22:51:58 +0000494 typedef PassManagerT<Function> PMType;
Chris Lattner67d25652002-01-30 23:20:39 +0000495
496 // runPass - Specify how the pass should be run on the UnitType
Chris Lattnerc8e66542002-04-27 06:56:12 +0000497 static bool runPass(PassClass *P, Function *F) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000498 return P->runOnFunction(*F);
Chris Lattner67d25652002-01-30 23:20:39 +0000499 }
500
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000501 // Dummy implementation of PassStarted/PassEnded
502 static void PassStarted(Pass *P) {}
503 static void PassEnded(Pass *P) {}
504
Chris Lattnerac3e0602002-01-31 18:32:27 +0000505 // getPMName() - Return the name of the unit the PassManager operates on for
506 // debugging.
Chris Lattner4e8c4872002-03-23 22:51:58 +0000507 const char *getPMName() const { return "Function"; }
Chris Lattner37104aa2002-04-29 14:57:45 +0000508 virtual const char *getPassName() const { return "Function Pass Manager"; }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000509
Chris Lattnerc8e66542002-04-27 06:56:12 +0000510 // Implement the FunctionPass interface...
Chris Lattner113f4f42002-06-25 16:13:24 +0000511 virtual bool doInitialization(Module &M);
512 virtual bool runOnFunction(Function &F);
513 virtual bool doFinalization(Module &M);
Chris Lattnercb429062002-04-30 18:50:17 +0000514
515 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
516 AU.setPreservesAll();
517 }
Chris Lattner67d25652002-01-30 23:20:39 +0000518};
519
520
521
522//===----------------------------------------------------------------------===//
523// PassManagerTraits<Module> Specialization
524//
525// This is the top level PassManager implementation that holds generic passes.
526//
527template<> struct PassManagerTraits<Module> : public Pass {
528 // PassClass - The type of passes tracked by this PassManager
529 typedef Pass PassClass;
530
531 // SubPassClass - The types of classes that should be collated together
Chris Lattnerc8e66542002-04-27 06:56:12 +0000532 typedef FunctionPass SubPassClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000533
534 // BatcherClass - The type to use for collation of subtypes...
Chris Lattner4e8c4872002-03-23 22:51:58 +0000535 typedef PassManagerT<Function> BatcherClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000536
537 // ParentClass - The type of the parent PassManager...
Chris Lattnerac3e0602002-01-31 18:32:27 +0000538 typedef AnalysisResolver ParentClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000539
540 // runPass - Specify how the pass should be run on the UnitType
Chris Lattner113f4f42002-06-25 16:13:24 +0000541 static bool runPass(PassClass *P, Module *M) { return P->run(*M); }
Chris Lattner67d25652002-01-30 23:20:39 +0000542
Chris Lattnerac3e0602002-01-31 18:32:27 +0000543 // getPMName() - Return the name of the unit the PassManager operates on for
544 // debugging.
545 const char *getPMName() const { return "Module"; }
Chris Lattner37104aa2002-04-29 14:57:45 +0000546 virtual const char *getPassName() const { return "Module Pass Manager"; }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000547
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000548 // TimingInformation - This data member maintains timing information for each
549 // of the passes that is executed.
550 //
551 TimingInfo *TimeInfo;
552
553 // PassStarted/Ended - This callback is notified any time a pass is started
554 // or stops. This is used to collect timing information about the different
555 // passes being executed.
556 //
557 void PassStarted(Pass *P) {
558 if (TimeInfo) TimeInfo->passStarted(P);
Chris Lattner67d25652002-01-30 23:20:39 +0000559 }
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000560 void PassEnded(Pass *P) {
561 if (TimeInfo) TimeInfo->passEnded(P);
562 }
563
564 // run - Implement the PassManager interface...
Chris Lattner113f4f42002-06-25 16:13:24 +0000565 bool run(Module &M) {
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000566 TimeInfo = TimingInfo::create();
Chris Lattner113f4f42002-06-25 16:13:24 +0000567 bool Result = ((PassManagerT<Module>*)this)->runOnUnit(&M);
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000568 if (TimeInfo) {
569 delete TimeInfo;
570 TimeInfo = 0;
571 }
572 return Result;
573 }
574
575 // PassManagerTraits constructor - Create a timing info object if the user
576 // specified timing info should be collected on the command line.
577 //
578 PassManagerTraits() : TimeInfo(0) {}
Chris Lattner67d25652002-01-30 23:20:39 +0000579};
580
581
582
583//===----------------------------------------------------------------------===//
584// PassManagerTraits Method Implementations
585//
586
587// PassManagerTraits<BasicBlock> Implementations
588//
Chris Lattner113f4f42002-06-25 16:13:24 +0000589inline bool PassManagerTraits<BasicBlock>::doInitialization(Module &M) {
Chris Lattner2eaac392002-01-31 00:40:44 +0000590 bool Changed = false;
591 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
592 ((PMType*)this)->Passes[i]->doInitialization(M);
593 return Changed;
594}
595
Chris Lattner113f4f42002-06-25 16:13:24 +0000596inline bool PassManagerTraits<BasicBlock>::runOnBasicBlock(BasicBlock &BB) {
597 return ((PMType*)this)->runOnUnit(&BB);
Chris Lattner2eaac392002-01-31 00:40:44 +0000598}
599
Chris Lattner113f4f42002-06-25 16:13:24 +0000600inline bool PassManagerTraits<BasicBlock>::doFinalization(Module &M) {
Chris Lattner2eaac392002-01-31 00:40:44 +0000601 bool Changed = false;
602 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
603 ((PMType*)this)->Passes[i]->doFinalization(M);
604 return Changed;
Chris Lattner67d25652002-01-30 23:20:39 +0000605}
606
607
Chris Lattner4e8c4872002-03-23 22:51:58 +0000608// PassManagerTraits<Function> Implementations
Chris Lattner67d25652002-01-30 23:20:39 +0000609//
Chris Lattner113f4f42002-06-25 16:13:24 +0000610inline bool PassManagerTraits<Function>::doInitialization(Module &M) {
Chris Lattner67d25652002-01-30 23:20:39 +0000611 bool Changed = false;
612 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
613 ((PMType*)this)->Passes[i]->doInitialization(M);
614 return Changed;
615}
616
Chris Lattner113f4f42002-06-25 16:13:24 +0000617inline bool PassManagerTraits<Function>::runOnFunction(Function &F) {
618 return ((PMType*)this)->runOnUnit(&F);
Chris Lattner67d25652002-01-30 23:20:39 +0000619}
620
Chris Lattner113f4f42002-06-25 16:13:24 +0000621inline bool PassManagerTraits<Function>::doFinalization(Module &M) {
Chris Lattner67d25652002-01-30 23:20:39 +0000622 bool Changed = false;
623 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
624 ((PMType*)this)->Passes[i]->doFinalization(M);
625 return Changed;
626}
627
628#endif