blob: b4155e42a49b6061021e25e2629294fd47043c57 [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 {
Chris Lattner1e4867f2002-07-30 19:51:02 +000032 None, Arguments, Structure, Executions, Details
Chris Lattner198cf422002-07-30 16:27:02 +000033};
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"),
Chris Lattner1e4867f2002-07-30 19:51:02 +000040 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
Chris Lattner198cf422002-07-30 16:27:02 +000041 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 Lattner1e4867f2002-07-30 19:51:02 +000051 static void PerformPassStartupStuff(Pass *P) {
52 // If debugging is enabled, print out argument information...
53 if (PassDebugging >= Arguments) {
54 std::cerr << "Pass Arguments: ";
55 PrintArgumentInformation(P);
56 std::cerr << "\n";
57
58 // Print the pass execution structure
59 if (PassDebugging >= Structure)
60 P->dumpPassStructure();
61 }
Chris Lattner198cf422002-07-30 16:27:02 +000062 }
Chris Lattner1e4867f2002-07-30 19:51:02 +000063
64 static void PrintArgumentInformation(const Pass *P);
Chris Lattnera454b5b2002-04-28 05:14:06 +000065 static void PrintPassInformation(unsigned,const char*,Pass *, Annotable *);
Chris Lattnerac3e0602002-01-31 18:32:27 +000066 static void PrintAnalysisSetInfo(unsigned,const char*,Pass *P,
Chris Lattnerc8e66542002-04-27 06:56:12 +000067 const std::vector<AnalysisID> &);
Chris Lattner67d25652002-01-30 23:20:39 +000068};
69
70
Chris Lattnere2eb99e2002-04-29 04:04:29 +000071//===----------------------------------------------------------------------===//
72// TimingInfo Class - This class is used to calculate information about the
73// amount of time each pass takes to execute. This only happens when
74// -time-passes is enabled on the command line.
75//
76class TimingInfo {
77 std::map<Pass*, double> TimingData;
78 TimingInfo() {} // Private ctor, must use create member
79public:
80 // Create method. If Timing is enabled, this creates and returns a new timing
81 // object, otherwise it returns null.
82 //
83 static TimingInfo *create();
84
85 // TimingDtor - Print out information about timing information
86 ~TimingInfo();
87
88 void passStarted(Pass *P);
89 void passEnded(Pass *P);
90};
91
92
Chris Lattner67d25652002-01-30 23:20:39 +000093
94//===----------------------------------------------------------------------===//
95// Declare the PassManagerTraits which will be specialized...
96//
97template<class UnitType> class PassManagerTraits; // Do not define.
98
99
100//===----------------------------------------------------------------------===//
101// PassManagerT - Container object for passes. The PassManagerT destructor
102// deletes all passes contained inside of the PassManagerT, so you shouldn't
103// delete passes manually, and all passes should be dynamically allocated.
104//
105template<typename UnitType>
106class PassManagerT : public PassManagerTraits<UnitType>,public AnalysisResolver{
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000107 typedef PassManagerTraits<UnitType> Traits;
108 typedef typename Traits::PassClass PassClass;
109 typedef typename Traits::SubPassClass SubPassClass;
110 typedef typename Traits::BatcherClass BatcherClass;
111 typedef typename Traits::ParentClass ParentClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000112
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000113 friend typename Traits::PassClass;
114 friend typename Traits::SubPassClass;
115 friend class Traits;
Chris Lattner67d25652002-01-30 23:20:39 +0000116
Chris Lattner73503172002-07-29 21:03:38 +0000117 std::vector<PassClass*> Passes; // List of passes to run
Chris Lattner67d25652002-01-30 23:20:39 +0000118
119 // The parent of this pass manager...
Chris Lattnerac3e0602002-01-31 18:32:27 +0000120 ParentClass * const Parent;
Chris Lattner67d25652002-01-30 23:20:39 +0000121
122 // The current batcher if one is in use, or null
123 BatcherClass *Batcher;
124
125 // CurrentAnalyses - As the passes are being run, this map contains the
126 // analyses that are available to the current pass for use. This is accessed
127 // through the getAnalysis() function in this class and in Pass.
128 //
129 std::map<AnalysisID, Pass*> CurrentAnalyses;
130
Chris Lattnerac3e0602002-01-31 18:32:27 +0000131 // LastUseOf - This map keeps track of the last usage in our pipeline of a
132 // particular pass. When executing passes, the memory for .first is free'd
133 // after .second is run.
134 //
135 std::map<Pass*, Pass*> LastUseOf;
136
Chris Lattner67d25652002-01-30 23:20:39 +0000137public:
138 PassManagerT(ParentClass *Par = 0) : Parent(Par), Batcher(0) {}
139 ~PassManagerT() {
140 // Delete all of the contained passes...
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000141 for (typename std::vector<PassClass*>::iterator
142 I = Passes.begin(), E = Passes.end(); I != E; ++I)
Chris Lattner67d25652002-01-30 23:20:39 +0000143 delete *I;
144 }
145
146 // run - Run all of the queued passes on the specified module in an optimal
147 // way.
148 virtual bool runOnUnit(UnitType *M) {
149 bool MadeChanges = false;
150 closeBatcher();
151 CurrentAnalyses.clear();
152
Chris Lattnerac3e0602002-01-31 18:32:27 +0000153 // LastUserOf - This contains the inverted LastUseOfMap...
154 std::map<Pass *, std::vector<Pass*> > LastUserOf;
155 for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(),
156 E = LastUseOf.end(); I != E; ++I)
157 LastUserOf[I->second].push_back(I->first);
158
159
Chris Lattner67d25652002-01-30 23:20:39 +0000160 // Output debug information...
Chris Lattner1e4867f2002-07-30 19:51:02 +0000161 if (Parent == 0) PMDebug::PerformPassStartupStuff(this);
Chris Lattner67d25652002-01-30 23:20:39 +0000162
163 // Run all of the passes
164 for (unsigned i = 0, e = Passes.size(); i < e; ++i) {
165 PassClass *P = Passes[i];
166
Chris Lattnera454b5b2002-04-28 05:14:06 +0000167 PMDebug::PrintPassInformation(getDepth(), "Executing Pass", P,
168 (Annotable*)M);
Chris Lattner67d25652002-01-30 23:20:39 +0000169
170 // Get information about what analyses the pass uses...
Chris Lattnerc8e66542002-04-27 06:56:12 +0000171 AnalysisUsage AnUsage;
172 P->getAnalysisUsage(AnUsage);
173 PMDebug::PrintAnalysisSetInfo(getDepth(), "Required", P,
174 AnUsage.getRequiredSet());
Chris Lattner67d25652002-01-30 23:20:39 +0000175
176#ifndef NDEBUG
177 // All Required analyses should be available to the pass as it runs!
Anand Shukla8c377892002-06-25 22:07:38 +0000178 for (std::vector<AnalysisID>::const_iterator
Chris Lattnerc8e66542002-04-27 06:56:12 +0000179 I = AnUsage.getRequiredSet().begin(),
180 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Chris Lattner67d25652002-01-30 23:20:39 +0000181 assert(getAnalysisOrNullUp(*I) && "Analysis used but not available!");
182 }
183#endif
184
185 // Run the sub pass!
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000186 startPass(P);
187 bool Changed = runPass(P, M);
188 endPass(P);
Chris Lattnerf2f31bd2002-02-01 04:53:36 +0000189 MadeChanges |= Changed;
Chris Lattner67d25652002-01-30 23:20:39 +0000190
Chris Lattnerf2f31bd2002-02-01 04:53:36 +0000191 if (Changed)
192 PMDebug::PrintPassInformation(getDepth()+1, "Made Modification", P,
Chris Lattnera454b5b2002-04-28 05:14:06 +0000193 (Annotable*)M);
Chris Lattnerc8e66542002-04-27 06:56:12 +0000194 PMDebug::PrintAnalysisSetInfo(getDepth(), "Preserved", P,
195 AnUsage.getPreservedSet());
Chris Lattner67d25652002-01-30 23:20:39 +0000196
Chris Lattnerc8e66542002-04-27 06:56:12 +0000197
198 // Erase all analyses not in the preserved set...
199 if (!AnUsage.preservesAll()) {
200 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
201 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(),
202 E = CurrentAnalyses.end(); I != E; )
203 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) !=
204 PreservedSet.end())
205 ++I; // This analysis is preserved, leave it in the available set...
206 else {
207#if MAP_DOESNT_HAVE_BROKEN_ERASE_MEMBER
208 I = CurrentAnalyses.erase(I); // Analysis not preserved!
209#else
210 // GCC 2.95.3 STL doesn't have correct erase member!
211 CurrentAnalyses.erase(I);
212 I = CurrentAnalyses.begin();
213#endif
214 }
215 }
216
Chris Lattner73503172002-07-29 21:03:38 +0000217 // Add the current pass to the set of passes that have been run, and are
218 // thus available to users.
219 //
220 if (const PassInfo *PI = P->getPassInfo())
221 CurrentAnalyses[PI] = P;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000222
223 // Free memory for any passes that we are the last use of...
224 std::vector<Pass*> &DeadPass = LastUserOf[P];
225 for (std::vector<Pass*>::iterator I = DeadPass.begin(),E = DeadPass.end();
226 I != E; ++I) {
227 PMDebug::PrintPassInformation(getDepth()+1, "Freeing Pass", *I,
Chris Lattnera454b5b2002-04-28 05:14:06 +0000228 (Annotable*)M);
Chris Lattnerac3e0602002-01-31 18:32:27 +0000229 (*I)->releaseMemory();
230 }
Chris Lattner67d25652002-01-30 23:20:39 +0000231 }
232 return MadeChanges;
233 }
234
Chris Lattnerac3e0602002-01-31 18:32:27 +0000235 // dumpPassStructure - Implement the -debug-passes=PassStructure option
236 virtual void dumpPassStructure(unsigned Offset = 0) {
237 std::cerr << std::string(Offset*2, ' ') << Traits::getPMName()
238 << " Pass Manager\n";
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000239 for (typename std::vector<PassClass*>::iterator
240 I = Passes.begin(), E = Passes.end(); I != E; ++I) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000241 PassClass *P = *I;
242 P->dumpPassStructure(Offset+1);
243
244 // Loop through and see which classes are destroyed after this one...
245 for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(),
246 E = LastUseOf.end(); I != E; ++I) {
247 if (P == I->second) {
Chris Lattner73503172002-07-29 21:03:38 +0000248 std::cerr << "--" << std::string(Offset*2, ' ');
Chris Lattnerac3e0602002-01-31 18:32:27 +0000249 I->first->dumpPassStructure(0);
250 }
251 }
252 }
253 }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000254
255 Pass *getAnalysisOrNullDown(AnalysisID ID) const {
256 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID);
257 if (I == CurrentAnalyses.end()) {
258 if (Batcher)
259 return ((AnalysisResolver*)Batcher)->getAnalysisOrNullDown(ID);
260 return 0;
261 }
262 return I->second;
263 }
264
265 Pass *getAnalysisOrNullUp(AnalysisID ID) const {
266 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID);
267 if (I == CurrentAnalyses.end()) {
268 if (Parent)
269 return Parent->getAnalysisOrNullUp(ID);
270 return 0;
271 }
272 return I->second;
273 }
274
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000275 // {start/end}Pass - Called when a pass is started, it just propogates
276 // information up to the top level PassManagerT object to tell it that a pass
277 // has started or ended. This is used to gather timing information about
278 // passes.
279 //
280 void startPass(Pass *P) {
281 if (Parent) Parent->startPass(P);
282 else PassStarted(P);
283 }
284 void endPass(Pass *P) {
285 if (Parent) Parent->endPass(P);
286 else PassEnded(P);
287 }
288
Chris Lattnerac3e0602002-01-31 18:32:27 +0000289 // markPassUsed - Inform higher level pass managers (and ourselves)
290 // that these analyses are being used by this pass. This is used to
291 // make sure that analyses are not free'd before we have to use
292 // them...
293 //
294 void markPassUsed(AnalysisID P, Pass *User) {
295 std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.find(P);
296 if (I != CurrentAnalyses.end()) {
297 LastUseOf[I->second] = User; // Local pass, extend the lifetime
298 } else {
299 // Pass not in current available set, must be a higher level pass
300 // available to us, propogate to parent pass manager... We tell the
301 // parent that we (the passmanager) are using the analysis so that it
302 // frees the analysis AFTER this pass manager runs.
303 //
Chris Lattnerde421a72002-03-17 21:16:01 +0000304 assert(Parent != 0 && "Pass available but not found! "
305 "Did your analysis pass 'Provide' itself?");
Chris Lattnerac3e0602002-01-31 18:32:27 +0000306 Parent->markPassUsed(P, this);
307 }
308 }
309
310 // Return the number of parent PassManagers that exist
311 virtual unsigned getDepth() const {
312 if (Parent == 0) return 0;
313 return 1 + Parent->getDepth();
314 }
315
Chris Lattner1e4867f2002-07-30 19:51:02 +0000316 virtual unsigned getNumContainedPasses() const { return Passes.size(); }
317 virtual const Pass *getContainedPass(unsigned N) const {
318 assert(N < Passes.size() && "Pass number out of range!");
319 return Passes[N];
320 }
321
Chris Lattner67d25652002-01-30 23:20:39 +0000322 // add - Add a pass to the queue of passes to run. This passes ownership of
323 // the Pass to the PassManager. When the PassManager is destroyed, the pass
Chris Lattnerac3e0602002-01-31 18:32:27 +0000324 // will be destroyed as well, so there is no need to delete the pass. This
325 // implies that all passes MUST be new'd.
Chris Lattner67d25652002-01-30 23:20:39 +0000326 //
327 void add(PassClass *P) {
328 // Get information about what analyses the pass uses...
Chris Lattnerc8e66542002-04-27 06:56:12 +0000329 AnalysisUsage AnUsage;
330 P->getAnalysisUsage(AnUsage);
331 const std::vector<AnalysisID> &Required = AnUsage.getRequiredSet();
Chris Lattner67d25652002-01-30 23:20:39 +0000332
333 // Loop over all of the analyses used by this pass,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000334 for (std::vector<AnalysisID>::const_iterator I = Required.begin(),
335 E = Required.end(); I != E; ++I) {
Chris Lattner67d25652002-01-30 23:20:39 +0000336 if (getAnalysisOrNullDown(*I) == 0)
Chris Lattner26750072002-07-27 01:12:17 +0000337 add((PassClass*)(*I)->createPass());
Chris Lattner67d25652002-01-30 23:20:39 +0000338 }
339
340 // Tell the pass to add itself to this PassManager... the way it does so
341 // depends on the class of the pass, and is critical to laying out passes in
342 // an optimal order..
343 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000344 P->addToPassManager(this, AnUsage);
Chris Lattner67d25652002-01-30 23:20:39 +0000345 }
346
347private:
348
349 // addPass - These functions are used to implement the subclass specific
350 // behaviors present in PassManager. Basically the add(Pass*) method ends up
351 // reflecting its behavior into a Pass::addToPassManager call. Subclasses of
352 // Pass override it specifically so that they can reflect the type
353 // information inherent in "this" back to the PassManager.
354 //
355 // For generic Pass subclasses (which are interprocedural passes), we simply
356 // add the pass to the end of the pass list and terminate any accumulation of
Chris Lattnerc8e66542002-04-27 06:56:12 +0000357 // FunctionPass's that are present.
Chris Lattner67d25652002-01-30 23:20:39 +0000358 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000359 void addPass(PassClass *P, AnalysisUsage &AnUsage) {
360 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
Chris Lattnerc8e66542002-04-27 06:56:12 +0000361
Chris Lattner73503172002-07-29 21:03:38 +0000362 // FIXME: If this pass being added isn't killed by any of the passes in the
363 // batcher class then we can reorder to pass to execute before the batcher
364 // does, which will potentially allow us to batch more passes!
Chris Lattner67d25652002-01-30 23:20:39 +0000365 //
Chris Lattner73503172002-07-29 21:03:38 +0000366 //const std::vector<AnalysisID> &ProvidedSet = AnUsage.getProvidedSet();
367 if (Batcher /*&& ProvidedSet.empty()*/)
Chris Lattner67d25652002-01-30 23:20:39 +0000368 closeBatcher(); // This pass cannot be batched!
369
370 // Set the Resolver instance variable in the Pass so that it knows where to
371 // find this object...
372 //
373 setAnalysisResolver(P, this);
374 Passes.push_back(P);
375
Chris Lattnerac3e0602002-01-31 18:32:27 +0000376 // Inform higher level pass managers (and ourselves) that these analyses are
377 // being used by this pass. This is used to make sure that analyses are not
378 // free'd before we have to use them...
379 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000380 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
381 E = RequiredSet.end(); I != E; ++I)
Chris Lattnerac3e0602002-01-31 18:32:27 +0000382 markPassUsed(*I, P); // Mark *I as used by P
383
Chris Lattnerc8e66542002-04-27 06:56:12 +0000384 // Erase all analyses not in the preserved set...
385 if (!AnUsage.preservesAll()) {
386 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
387 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(),
388 E = CurrentAnalyses.end(); I != E; )
389 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) !=
390 PreservedSet.end())
391 ++I; // This analysis is preserved, leave it in the available set...
392 else {
393#if MAP_DOESNT_HAVE_BROKEN_ERASE_MEMBER
394 I = CurrentAnalyses.erase(I); // Analysis not preserved!
395#else
396 CurrentAnalyses.erase(I);// GCC 2.95.3 STL doesn't have correct erase!
397 I = CurrentAnalyses.begin();
398#endif
399 }
400 }
Chris Lattner67d25652002-01-30 23:20:39 +0000401
Chris Lattner73503172002-07-29 21:03:38 +0000402 // Add this pass to the currently available set...
403 if (const PassInfo *PI = P->getPassInfo())
404 CurrentAnalyses[PI] = P;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000405
406 // For now assume that our results are never used...
407 LastUseOf[P] = P;
Chris Lattner67d25652002-01-30 23:20:39 +0000408 }
409
Chris Lattnerc8e66542002-04-27 06:56:12 +0000410 // For FunctionPass subclasses, we must be sure to batch the FunctionPass's
411 // together in a BatcherClass object so that all of the analyses are run
412 // together a function at a time.
Chris Lattner67d25652002-01-30 23:20:39 +0000413 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000414 void addPass(SubPassClass *MP, AnalysisUsage &AnUsage) {
Chris Lattner67d25652002-01-30 23:20:39 +0000415 if (Batcher == 0) // If we don't have a batcher yet, make one now.
416 Batcher = new BatcherClass(this);
Chris Lattner73503172002-07-29 21:03:38 +0000417 // The Batcher will queue the passes up
Chris Lattnerc8e66542002-04-27 06:56:12 +0000418 MP->addToPassManager(Batcher, AnUsage);
Chris Lattner67d25652002-01-30 23:20:39 +0000419 }
420
421 // closeBatcher - Terminate the batcher that is being worked on.
422 void closeBatcher() {
423 if (Batcher) {
424 Passes.push_back(Batcher);
425 Batcher = 0;
426 }
427 }
428};
429
430
431
432//===----------------------------------------------------------------------===//
433// PassManagerTraits<BasicBlock> Specialization
434//
435// This pass manager is used to group together all of the BasicBlockPass's
436// into a single unit.
437//
438template<> struct PassManagerTraits<BasicBlock> : public BasicBlockPass {
439 // PassClass - The type of passes tracked by this PassManager
440 typedef BasicBlockPass PassClass;
441
442 // SubPassClass - The types of classes that should be collated together
443 // This is impossible to match, so BasicBlock instantiations of PassManagerT
444 // do not collate.
445 //
446 typedef PassManagerT<Module> SubPassClass;
447
448 // BatcherClass - The type to use for collation of subtypes... This class is
449 // never instantiated for the PassManager<BasicBlock>, but it must be an
450 // instance of PassClass to typecheck.
451 //
452 typedef PassClass BatcherClass;
453
454 // ParentClass - The type of the parent PassManager...
Chris Lattner4e8c4872002-03-23 22:51:58 +0000455 typedef PassManagerT<Function> ParentClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000456
Chris Lattner2eaac392002-01-31 00:40:44 +0000457 // PMType - The type of the passmanager that subclasses this class
458 typedef PassManagerT<BasicBlock> PMType;
459
Chris Lattner67d25652002-01-30 23:20:39 +0000460 // runPass - Specify how the pass should be run on the UnitType
461 static bool runPass(PassClass *P, BasicBlock *M) {
462 // todo, init and finalize
Chris Lattner113f4f42002-06-25 16:13:24 +0000463 return P->runOnBasicBlock(*M);
Chris Lattner67d25652002-01-30 23:20:39 +0000464 }
465
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000466 // Dummy implementation of PassStarted/PassEnded
467 static void PassStarted(Pass *P) {}
468 static void PassEnded(Pass *P) {}
469
Chris Lattnerac3e0602002-01-31 18:32:27 +0000470 // getPMName() - Return the name of the unit the PassManager operates on for
471 // debugging.
472 const char *getPMName() const { return "BasicBlock"; }
Chris Lattner37104aa2002-04-29 14:57:45 +0000473 virtual const char *getPassName() const { return "BasicBlock Pass Manager"; }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000474
Chris Lattner2eaac392002-01-31 00:40:44 +0000475 // Implement the BasicBlockPass interface...
Chris Lattner113f4f42002-06-25 16:13:24 +0000476 virtual bool doInitialization(Module &M);
477 virtual bool runOnBasicBlock(BasicBlock &BB);
478 virtual bool doFinalization(Module &M);
Chris Lattnercb429062002-04-30 18:50:17 +0000479
480 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
481 AU.setPreservesAll();
482 }
Chris Lattner67d25652002-01-30 23:20:39 +0000483};
484
485
486
487//===----------------------------------------------------------------------===//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000488// PassManagerTraits<Function> Specialization
Chris Lattner67d25652002-01-30 23:20:39 +0000489//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000490// This pass manager is used to group together all of the FunctionPass's
Chris Lattner67d25652002-01-30 23:20:39 +0000491// into a single unit.
492//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000493template<> struct PassManagerTraits<Function> : public FunctionPass {
Chris Lattner67d25652002-01-30 23:20:39 +0000494 // PassClass - The type of passes tracked by this PassManager
Chris Lattnerc8e66542002-04-27 06:56:12 +0000495 typedef FunctionPass PassClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000496
497 // SubPassClass - The types of classes that should be collated together
498 typedef BasicBlockPass SubPassClass;
499
500 // BatcherClass - The type to use for collation of subtypes...
501 typedef PassManagerT<BasicBlock> BatcherClass;
502
503 // ParentClass - The type of the parent PassManager...
504 typedef PassManagerT<Module> ParentClass;
505
506 // PMType - The type of the passmanager that subclasses this class
Chris Lattner4e8c4872002-03-23 22:51:58 +0000507 typedef PassManagerT<Function> PMType;
Chris Lattner67d25652002-01-30 23:20:39 +0000508
509 // runPass - Specify how the pass should be run on the UnitType
Chris Lattnerc8e66542002-04-27 06:56:12 +0000510 static bool runPass(PassClass *P, Function *F) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000511 return P->runOnFunction(*F);
Chris Lattner67d25652002-01-30 23:20:39 +0000512 }
513
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000514 // Dummy implementation of PassStarted/PassEnded
515 static void PassStarted(Pass *P) {}
516 static void PassEnded(Pass *P) {}
517
Chris Lattnerac3e0602002-01-31 18:32:27 +0000518 // getPMName() - Return the name of the unit the PassManager operates on for
519 // debugging.
Chris Lattner4e8c4872002-03-23 22:51:58 +0000520 const char *getPMName() const { return "Function"; }
Chris Lattner37104aa2002-04-29 14:57:45 +0000521 virtual const char *getPassName() const { return "Function Pass Manager"; }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000522
Chris Lattnerc8e66542002-04-27 06:56:12 +0000523 // Implement the FunctionPass interface...
Chris Lattner113f4f42002-06-25 16:13:24 +0000524 virtual bool doInitialization(Module &M);
525 virtual bool runOnFunction(Function &F);
526 virtual bool doFinalization(Module &M);
Chris Lattnercb429062002-04-30 18:50:17 +0000527
528 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
529 AU.setPreservesAll();
530 }
Chris Lattner67d25652002-01-30 23:20:39 +0000531};
532
533
534
535//===----------------------------------------------------------------------===//
536// PassManagerTraits<Module> Specialization
537//
538// This is the top level PassManager implementation that holds generic passes.
539//
540template<> struct PassManagerTraits<Module> : public Pass {
541 // PassClass - The type of passes tracked by this PassManager
542 typedef Pass PassClass;
543
544 // SubPassClass - The types of classes that should be collated together
Chris Lattnerc8e66542002-04-27 06:56:12 +0000545 typedef FunctionPass SubPassClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000546
547 // BatcherClass - The type to use for collation of subtypes...
Chris Lattner4e8c4872002-03-23 22:51:58 +0000548 typedef PassManagerT<Function> BatcherClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000549
550 // ParentClass - The type of the parent PassManager...
Chris Lattnerac3e0602002-01-31 18:32:27 +0000551 typedef AnalysisResolver ParentClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000552
553 // runPass - Specify how the pass should be run on the UnitType
Chris Lattner113f4f42002-06-25 16:13:24 +0000554 static bool runPass(PassClass *P, Module *M) { return P->run(*M); }
Chris Lattner67d25652002-01-30 23:20:39 +0000555
Chris Lattnerac3e0602002-01-31 18:32:27 +0000556 // getPMName() - Return the name of the unit the PassManager operates on for
557 // debugging.
558 const char *getPMName() const { return "Module"; }
Chris Lattner37104aa2002-04-29 14:57:45 +0000559 virtual const char *getPassName() const { return "Module Pass Manager"; }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000560
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000561 // TimingInformation - This data member maintains timing information for each
562 // of the passes that is executed.
563 //
564 TimingInfo *TimeInfo;
565
566 // PassStarted/Ended - This callback is notified any time a pass is started
567 // or stops. This is used to collect timing information about the different
568 // passes being executed.
569 //
570 void PassStarted(Pass *P) {
571 if (TimeInfo) TimeInfo->passStarted(P);
Chris Lattner67d25652002-01-30 23:20:39 +0000572 }
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000573 void PassEnded(Pass *P) {
574 if (TimeInfo) TimeInfo->passEnded(P);
575 }
576
577 // run - Implement the PassManager interface...
Chris Lattner113f4f42002-06-25 16:13:24 +0000578 bool run(Module &M) {
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000579 TimeInfo = TimingInfo::create();
Chris Lattner113f4f42002-06-25 16:13:24 +0000580 bool Result = ((PassManagerT<Module>*)this)->runOnUnit(&M);
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000581 if (TimeInfo) {
582 delete TimeInfo;
583 TimeInfo = 0;
584 }
585 return Result;
586 }
587
588 // PassManagerTraits constructor - Create a timing info object if the user
589 // specified timing info should be collected on the command line.
590 //
591 PassManagerTraits() : TimeInfo(0) {}
Chris Lattner67d25652002-01-30 23:20:39 +0000592};
593
594
595
596//===----------------------------------------------------------------------===//
597// PassManagerTraits Method Implementations
598//
599
600// PassManagerTraits<BasicBlock> Implementations
601//
Chris Lattner113f4f42002-06-25 16:13:24 +0000602inline bool PassManagerTraits<BasicBlock>::doInitialization(Module &M) {
Chris Lattner2eaac392002-01-31 00:40:44 +0000603 bool Changed = false;
604 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
605 ((PMType*)this)->Passes[i]->doInitialization(M);
606 return Changed;
607}
608
Chris Lattner113f4f42002-06-25 16:13:24 +0000609inline bool PassManagerTraits<BasicBlock>::runOnBasicBlock(BasicBlock &BB) {
610 return ((PMType*)this)->runOnUnit(&BB);
Chris Lattner2eaac392002-01-31 00:40:44 +0000611}
612
Chris Lattner113f4f42002-06-25 16:13:24 +0000613inline bool PassManagerTraits<BasicBlock>::doFinalization(Module &M) {
Chris Lattner2eaac392002-01-31 00:40:44 +0000614 bool Changed = false;
615 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
616 ((PMType*)this)->Passes[i]->doFinalization(M);
617 return Changed;
Chris Lattner67d25652002-01-30 23:20:39 +0000618}
619
620
Chris Lattner4e8c4872002-03-23 22:51:58 +0000621// PassManagerTraits<Function> Implementations
Chris Lattner67d25652002-01-30 23:20:39 +0000622//
Chris Lattner113f4f42002-06-25 16:13:24 +0000623inline bool PassManagerTraits<Function>::doInitialization(Module &M) {
Chris Lattner67d25652002-01-30 23:20:39 +0000624 bool Changed = false;
625 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
626 ((PMType*)this)->Passes[i]->doInitialization(M);
627 return Changed;
628}
629
Chris Lattner113f4f42002-06-25 16:13:24 +0000630inline bool PassManagerTraits<Function>::runOnFunction(Function &F) {
631 return ((PMType*)this)->runOnUnit(&F);
Chris Lattner67d25652002-01-30 23:20:39 +0000632}
633
Chris Lattner113f4f42002-06-25 16:13:24 +0000634inline bool PassManagerTraits<Function>::doFinalization(Module &M) {
Chris Lattner67d25652002-01-30 23:20:39 +0000635 bool Changed = false;
636 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
637 ((PMType*)this)->Passes[i]->doFinalization(M);
638 return Changed;
639}
640
641#endif