blob: 83061deac16a2cc306b6153bfef3b06ea68760cd [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 Lattner395c27a2002-07-31 18:04:17 +000021#include <iostream>
Chris Lattnera454b5b2002-04-28 05:14:06 +000022class Annotable;
Chris Lattner67d25652002-01-30 23:20:39 +000023
Chris Lattner67d25652002-01-30 23:20:39 +000024//===----------------------------------------------------------------------===//
Chris Lattner198cf422002-07-30 16:27:02 +000025// Pass debugging information. Often it is useful to find out what pass is
26// running when a crash occurs in a utility. When this library is compiled with
27// debugging on, a command line option (--debug-pass) is enabled that causes the
28// pass name to be printed before it executes.
29//
30
31// Different debug levels that can be enabled...
32enum PassDebugLevel {
Chris Lattner1e4867f2002-07-30 19:51:02 +000033 None, Arguments, Structure, Executions, Details
Chris Lattner198cf422002-07-30 16:27:02 +000034};
35
36static cl::opt<enum PassDebugLevel>
37PassDebugging("debug-pass", cl::Hidden,
38 cl::desc("Print PassManager debugging information"),
39 cl::values(
40 clEnumVal(None , "disable debug output"),
Chris Lattner1e4867f2002-07-30 19:51:02 +000041 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
Chris Lattner198cf422002-07-30 16:27:02 +000042 clEnumVal(Structure , "print pass structure before run()"),
43 clEnumVal(Executions, "print pass name before it is executed"),
44 clEnumVal(Details , "print pass details when it is executed"),
45 0));
46
47//===----------------------------------------------------------------------===//
Chris Lattner57d2ba32002-04-04 19:35:24 +000048// PMDebug class - a set of debugging functions, that are not to be
49// instantiated by the template.
Chris Lattner67d25652002-01-30 23:20:39 +000050//
51struct PMDebug {
Chris Lattner1e4867f2002-07-30 19:51:02 +000052 static void PerformPassStartupStuff(Pass *P) {
53 // If debugging is enabled, print out argument information...
54 if (PassDebugging >= Arguments) {
55 std::cerr << "Pass Arguments: ";
56 PrintArgumentInformation(P);
57 std::cerr << "\n";
58
59 // Print the pass execution structure
60 if (PassDebugging >= Structure)
61 P->dumpPassStructure();
62 }
Chris Lattner198cf422002-07-30 16:27:02 +000063 }
Chris Lattner1e4867f2002-07-30 19:51:02 +000064
65 static void PrintArgumentInformation(const Pass *P);
Chris Lattnera454b5b2002-04-28 05:14:06 +000066 static void PrintPassInformation(unsigned,const char*,Pass *, Annotable *);
Chris Lattnerac3e0602002-01-31 18:32:27 +000067 static void PrintAnalysisSetInfo(unsigned,const char*,Pass *P,
Chris Lattnerc8e66542002-04-27 06:56:12 +000068 const std::vector<AnalysisID> &);
Chris Lattner67d25652002-01-30 23:20:39 +000069};
70
71
Chris Lattnere2eb99e2002-04-29 04:04:29 +000072//===----------------------------------------------------------------------===//
73// TimingInfo Class - This class is used to calculate information about the
74// amount of time each pass takes to execute. This only happens when
75// -time-passes is enabled on the command line.
76//
Chris Lattner6a33d6f2002-08-01 19:33:09 +000077struct TimeRecord { // TimeRecord - Data we collect and print for each pass
78 double Elapsed; // Wall clock time elapsed in seconds
79 double UserTime; // User time elapsed
80 double SystemTime; // System time elapsed
81 unsigned long MaxRSS; // Maximum resident set size (in bytes)
82 unsigned long RSSTemp; // Temp for calculating maxrss
83
84 TimeRecord() : Elapsed(0), UserTime(0), SystemTime(0), MaxRSS(0) {}
85 void passStart(const TimeRecord &T);
86 void passEnd(const TimeRecord &T);
87 void sum(const TimeRecord &TR);
88 bool operator<(const TimeRecord &TR) const {
89 return UserTime+SystemTime < TR.UserTime+TR.SystemTime;
90 }
91
92 void print(const char *PassName, const TimeRecord &TotalTime) const;
93};
94
Chris Lattnere2eb99e2002-04-29 04:04:29 +000095class TimingInfo {
Chris Lattner6a33d6f2002-08-01 19:33:09 +000096 std::map<Pass*, TimeRecord> TimingData;
Chris Lattnere2eb99e2002-04-29 04:04:29 +000097 TimingInfo() {} // Private ctor, must use create member
98public:
99 // Create method. If Timing is enabled, this creates and returns a new timing
100 // object, otherwise it returns null.
101 //
102 static TimingInfo *create();
103
104 // TimingDtor - Print out information about timing information
105 ~TimingInfo();
106
107 void passStarted(Pass *P);
108 void passEnded(Pass *P);
109};
110
111
Chris Lattner67d25652002-01-30 23:20:39 +0000112
113//===----------------------------------------------------------------------===//
114// Declare the PassManagerTraits which will be specialized...
115//
116template<class UnitType> class PassManagerTraits; // Do not define.
117
118
119//===----------------------------------------------------------------------===//
120// PassManagerT - Container object for passes. The PassManagerT destructor
121// deletes all passes contained inside of the PassManagerT, so you shouldn't
122// delete passes manually, and all passes should be dynamically allocated.
123//
124template<typename UnitType>
125class PassManagerT : public PassManagerTraits<UnitType>,public AnalysisResolver{
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000126 typedef PassManagerTraits<UnitType> Traits;
127 typedef typename Traits::PassClass PassClass;
128 typedef typename Traits::SubPassClass SubPassClass;
129 typedef typename Traits::BatcherClass BatcherClass;
130 typedef typename Traits::ParentClass ParentClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000131
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000132 friend typename Traits::PassClass;
133 friend typename Traits::SubPassClass;
134 friend class Traits;
Chris Lattner67d25652002-01-30 23:20:39 +0000135
Chris Lattner73503172002-07-29 21:03:38 +0000136 std::vector<PassClass*> Passes; // List of passes to run
Chris Lattner67d25652002-01-30 23:20:39 +0000137
138 // The parent of this pass manager...
Chris Lattnerac3e0602002-01-31 18:32:27 +0000139 ParentClass * const Parent;
Chris Lattner67d25652002-01-30 23:20:39 +0000140
141 // The current batcher if one is in use, or null
142 BatcherClass *Batcher;
143
144 // CurrentAnalyses - As the passes are being run, this map contains the
145 // analyses that are available to the current pass for use. This is accessed
146 // through the getAnalysis() function in this class and in Pass.
147 //
148 std::map<AnalysisID, Pass*> CurrentAnalyses;
149
Chris Lattnerac3e0602002-01-31 18:32:27 +0000150 // LastUseOf - This map keeps track of the last usage in our pipeline of a
151 // particular pass. When executing passes, the memory for .first is free'd
152 // after .second is run.
153 //
154 std::map<Pass*, Pass*> LastUseOf;
155
Chris Lattner67d25652002-01-30 23:20:39 +0000156public:
157 PassManagerT(ParentClass *Par = 0) : Parent(Par), Batcher(0) {}
158 ~PassManagerT() {
159 // Delete all of the contained passes...
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000160 for (typename std::vector<PassClass*>::iterator
161 I = Passes.begin(), E = Passes.end(); I != E; ++I)
Chris Lattner67d25652002-01-30 23:20:39 +0000162 delete *I;
163 }
164
165 // run - Run all of the queued passes on the specified module in an optimal
166 // way.
167 virtual bool runOnUnit(UnitType *M) {
168 bool MadeChanges = false;
169 closeBatcher();
170 CurrentAnalyses.clear();
171
Chris Lattnerac3e0602002-01-31 18:32:27 +0000172 // LastUserOf - This contains the inverted LastUseOfMap...
173 std::map<Pass *, std::vector<Pass*> > LastUserOf;
174 for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(),
175 E = LastUseOf.end(); I != E; ++I)
176 LastUserOf[I->second].push_back(I->first);
177
178
Chris Lattner67d25652002-01-30 23:20:39 +0000179 // Output debug information...
Chris Lattner1e4867f2002-07-30 19:51:02 +0000180 if (Parent == 0) PMDebug::PerformPassStartupStuff(this);
Chris Lattner67d25652002-01-30 23:20:39 +0000181
182 // Run all of the passes
183 for (unsigned i = 0, e = Passes.size(); i < e; ++i) {
184 PassClass *P = Passes[i];
185
Chris Lattnera454b5b2002-04-28 05:14:06 +0000186 PMDebug::PrintPassInformation(getDepth(), "Executing Pass", P,
187 (Annotable*)M);
Chris Lattner67d25652002-01-30 23:20:39 +0000188
189 // Get information about what analyses the pass uses...
Chris Lattnerc8e66542002-04-27 06:56:12 +0000190 AnalysisUsage AnUsage;
191 P->getAnalysisUsage(AnUsage);
192 PMDebug::PrintAnalysisSetInfo(getDepth(), "Required", P,
193 AnUsage.getRequiredSet());
Chris Lattner67d25652002-01-30 23:20:39 +0000194
195#ifndef NDEBUG
196 // All Required analyses should be available to the pass as it runs!
Anand Shukla8c377892002-06-25 22:07:38 +0000197 for (std::vector<AnalysisID>::const_iterator
Chris Lattnerc8e66542002-04-27 06:56:12 +0000198 I = AnUsage.getRequiredSet().begin(),
199 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Chris Lattner67d25652002-01-30 23:20:39 +0000200 assert(getAnalysisOrNullUp(*I) && "Analysis used but not available!");
201 }
202#endif
203
204 // Run the sub pass!
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000205 startPass(P);
206 bool Changed = runPass(P, M);
207 endPass(P);
Chris Lattnerf2f31bd2002-02-01 04:53:36 +0000208 MadeChanges |= Changed;
Chris Lattner67d25652002-01-30 23:20:39 +0000209
Chris Lattnerf2f31bd2002-02-01 04:53:36 +0000210 if (Changed)
211 PMDebug::PrintPassInformation(getDepth()+1, "Made Modification", P,
Chris Lattnera454b5b2002-04-28 05:14:06 +0000212 (Annotable*)M);
Chris Lattnerc8e66542002-04-27 06:56:12 +0000213 PMDebug::PrintAnalysisSetInfo(getDepth(), "Preserved", P,
214 AnUsage.getPreservedSet());
Chris Lattner67d25652002-01-30 23:20:39 +0000215
Chris Lattnerc8e66542002-04-27 06:56:12 +0000216
217 // Erase all analyses not in the preserved set...
218 if (!AnUsage.preservesAll()) {
219 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
220 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(),
221 E = CurrentAnalyses.end(); I != E; )
222 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) !=
223 PreservedSet.end())
224 ++I; // This analysis is preserved, leave it in the available set...
225 else {
226#if MAP_DOESNT_HAVE_BROKEN_ERASE_MEMBER
227 I = CurrentAnalyses.erase(I); // Analysis not preserved!
228#else
229 // GCC 2.95.3 STL doesn't have correct erase member!
230 CurrentAnalyses.erase(I);
231 I = CurrentAnalyses.begin();
232#endif
233 }
234 }
235
Chris Lattner73503172002-07-29 21:03:38 +0000236 // Add the current pass to the set of passes that have been run, and are
237 // thus available to users.
238 //
239 if (const PassInfo *PI = P->getPassInfo())
240 CurrentAnalyses[PI] = P;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000241
242 // Free memory for any passes that we are the last use of...
243 std::vector<Pass*> &DeadPass = LastUserOf[P];
244 for (std::vector<Pass*>::iterator I = DeadPass.begin(),E = DeadPass.end();
245 I != E; ++I) {
246 PMDebug::PrintPassInformation(getDepth()+1, "Freeing Pass", *I,
Chris Lattnera454b5b2002-04-28 05:14:06 +0000247 (Annotable*)M);
Chris Lattnerac3e0602002-01-31 18:32:27 +0000248 (*I)->releaseMemory();
249 }
Chris Lattner67d25652002-01-30 23:20:39 +0000250 }
251 return MadeChanges;
252 }
253
Chris Lattnerac3e0602002-01-31 18:32:27 +0000254 // dumpPassStructure - Implement the -debug-passes=PassStructure option
255 virtual void dumpPassStructure(unsigned Offset = 0) {
256 std::cerr << std::string(Offset*2, ' ') << Traits::getPMName()
257 << " Pass Manager\n";
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000258 for (typename std::vector<PassClass*>::iterator
259 I = Passes.begin(), E = Passes.end(); I != E; ++I) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000260 PassClass *P = *I;
261 P->dumpPassStructure(Offset+1);
262
263 // Loop through and see which classes are destroyed after this one...
264 for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(),
265 E = LastUseOf.end(); I != E; ++I) {
266 if (P == I->second) {
Chris Lattner73503172002-07-29 21:03:38 +0000267 std::cerr << "--" << std::string(Offset*2, ' ');
Chris Lattnerac3e0602002-01-31 18:32:27 +0000268 I->first->dumpPassStructure(0);
269 }
270 }
271 }
272 }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000273
274 Pass *getAnalysisOrNullDown(AnalysisID ID) const {
275 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID);
276 if (I == CurrentAnalyses.end()) {
277 if (Batcher)
278 return ((AnalysisResolver*)Batcher)->getAnalysisOrNullDown(ID);
279 return 0;
280 }
281 return I->second;
282 }
283
284 Pass *getAnalysisOrNullUp(AnalysisID ID) const {
285 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID);
286 if (I == CurrentAnalyses.end()) {
287 if (Parent)
288 return Parent->getAnalysisOrNullUp(ID);
289 return 0;
290 }
291 return I->second;
292 }
293
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000294 // {start/end}Pass - Called when a pass is started, it just propogates
295 // information up to the top level PassManagerT object to tell it that a pass
296 // has started or ended. This is used to gather timing information about
297 // passes.
298 //
299 void startPass(Pass *P) {
300 if (Parent) Parent->startPass(P);
301 else PassStarted(P);
302 }
303 void endPass(Pass *P) {
304 if (Parent) Parent->endPass(P);
305 else PassEnded(P);
306 }
307
Chris Lattnerac3e0602002-01-31 18:32:27 +0000308 // markPassUsed - Inform higher level pass managers (and ourselves)
309 // that these analyses are being used by this pass. This is used to
310 // make sure that analyses are not free'd before we have to use
311 // them...
312 //
313 void markPassUsed(AnalysisID P, Pass *User) {
314 std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.find(P);
315 if (I != CurrentAnalyses.end()) {
316 LastUseOf[I->second] = User; // Local pass, extend the lifetime
317 } else {
318 // Pass not in current available set, must be a higher level pass
319 // available to us, propogate to parent pass manager... We tell the
320 // parent that we (the passmanager) are using the analysis so that it
321 // frees the analysis AFTER this pass manager runs.
322 //
Chris Lattnerde421a72002-03-17 21:16:01 +0000323 assert(Parent != 0 && "Pass available but not found! "
324 "Did your analysis pass 'Provide' itself?");
Chris Lattnerac3e0602002-01-31 18:32:27 +0000325 Parent->markPassUsed(P, this);
326 }
327 }
328
329 // Return the number of parent PassManagers that exist
330 virtual unsigned getDepth() const {
331 if (Parent == 0) return 0;
332 return 1 + Parent->getDepth();
333 }
334
Chris Lattner1e4867f2002-07-30 19:51:02 +0000335 virtual unsigned getNumContainedPasses() const { return Passes.size(); }
336 virtual const Pass *getContainedPass(unsigned N) const {
337 assert(N < Passes.size() && "Pass number out of range!");
338 return Passes[N];
339 }
340
Chris Lattner67d25652002-01-30 23:20:39 +0000341 // add - Add a pass to the queue of passes to run. This passes ownership of
342 // the Pass to the PassManager. When the PassManager is destroyed, the pass
Chris Lattnerac3e0602002-01-31 18:32:27 +0000343 // will be destroyed as well, so there is no need to delete the pass. This
344 // implies that all passes MUST be new'd.
Chris Lattner67d25652002-01-30 23:20:39 +0000345 //
346 void add(PassClass *P) {
347 // Get information about what analyses the pass uses...
Chris Lattnerc8e66542002-04-27 06:56:12 +0000348 AnalysisUsage AnUsage;
349 P->getAnalysisUsage(AnUsage);
350 const std::vector<AnalysisID> &Required = AnUsage.getRequiredSet();
Chris Lattner67d25652002-01-30 23:20:39 +0000351
352 // Loop over all of the analyses used by this pass,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000353 for (std::vector<AnalysisID>::const_iterator I = Required.begin(),
354 E = Required.end(); I != E; ++I) {
Chris Lattner67d25652002-01-30 23:20:39 +0000355 if (getAnalysisOrNullDown(*I) == 0)
Chris Lattner26750072002-07-27 01:12:17 +0000356 add((PassClass*)(*I)->createPass());
Chris Lattner67d25652002-01-30 23:20:39 +0000357 }
358
359 // Tell the pass to add itself to this PassManager... the way it does so
360 // depends on the class of the pass, and is critical to laying out passes in
361 // an optimal order..
362 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000363 P->addToPassManager(this, AnUsage);
Chris Lattner67d25652002-01-30 23:20:39 +0000364 }
365
366private:
367
368 // addPass - These functions are used to implement the subclass specific
369 // behaviors present in PassManager. Basically the add(Pass*) method ends up
370 // reflecting its behavior into a Pass::addToPassManager call. Subclasses of
371 // Pass override it specifically so that they can reflect the type
372 // information inherent in "this" back to the PassManager.
373 //
374 // For generic Pass subclasses (which are interprocedural passes), we simply
375 // add the pass to the end of the pass list and terminate any accumulation of
Chris Lattnerc8e66542002-04-27 06:56:12 +0000376 // FunctionPass's that are present.
Chris Lattner67d25652002-01-30 23:20:39 +0000377 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000378 void addPass(PassClass *P, AnalysisUsage &AnUsage) {
379 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
Chris Lattnerc8e66542002-04-27 06:56:12 +0000380
Chris Lattner73503172002-07-29 21:03:38 +0000381 // FIXME: If this pass being added isn't killed by any of the passes in the
382 // batcher class then we can reorder to pass to execute before the batcher
383 // does, which will potentially allow us to batch more passes!
Chris Lattner67d25652002-01-30 23:20:39 +0000384 //
Chris Lattner73503172002-07-29 21:03:38 +0000385 //const std::vector<AnalysisID> &ProvidedSet = AnUsage.getProvidedSet();
386 if (Batcher /*&& ProvidedSet.empty()*/)
Chris Lattner67d25652002-01-30 23:20:39 +0000387 closeBatcher(); // This pass cannot be batched!
388
389 // Set the Resolver instance variable in the Pass so that it knows where to
390 // find this object...
391 //
392 setAnalysisResolver(P, this);
393 Passes.push_back(P);
394
Chris Lattnerac3e0602002-01-31 18:32:27 +0000395 // Inform higher level pass managers (and ourselves) that these analyses are
396 // being used by this pass. This is used to make sure that analyses are not
397 // free'd before we have to use them...
398 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000399 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
400 E = RequiredSet.end(); I != E; ++I)
Chris Lattnerac3e0602002-01-31 18:32:27 +0000401 markPassUsed(*I, P); // Mark *I as used by P
402
Chris Lattnerc8e66542002-04-27 06:56:12 +0000403 // Erase all analyses not in the preserved set...
404 if (!AnUsage.preservesAll()) {
405 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
406 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(),
407 E = CurrentAnalyses.end(); I != E; )
408 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) !=
409 PreservedSet.end())
410 ++I; // This analysis is preserved, leave it in the available set...
411 else {
412#if MAP_DOESNT_HAVE_BROKEN_ERASE_MEMBER
413 I = CurrentAnalyses.erase(I); // Analysis not preserved!
414#else
415 CurrentAnalyses.erase(I);// GCC 2.95.3 STL doesn't have correct erase!
416 I = CurrentAnalyses.begin();
417#endif
418 }
419 }
Chris Lattner67d25652002-01-30 23:20:39 +0000420
Chris Lattner73503172002-07-29 21:03:38 +0000421 // Add this pass to the currently available set...
422 if (const PassInfo *PI = P->getPassInfo())
423 CurrentAnalyses[PI] = P;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000424
425 // For now assume that our results are never used...
426 LastUseOf[P] = P;
Chris Lattner67d25652002-01-30 23:20:39 +0000427 }
428
Chris Lattnerc8e66542002-04-27 06:56:12 +0000429 // For FunctionPass subclasses, we must be sure to batch the FunctionPass's
430 // together in a BatcherClass object so that all of the analyses are run
431 // together a function at a time.
Chris Lattner67d25652002-01-30 23:20:39 +0000432 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000433 void addPass(SubPassClass *MP, AnalysisUsage &AnUsage) {
Chris Lattner67d25652002-01-30 23:20:39 +0000434 if (Batcher == 0) // If we don't have a batcher yet, make one now.
435 Batcher = new BatcherClass(this);
Chris Lattner73503172002-07-29 21:03:38 +0000436 // The Batcher will queue the passes up
Chris Lattnerc8e66542002-04-27 06:56:12 +0000437 MP->addToPassManager(Batcher, AnUsage);
Chris Lattner67d25652002-01-30 23:20:39 +0000438 }
439
440 // closeBatcher - Terminate the batcher that is being worked on.
441 void closeBatcher() {
442 if (Batcher) {
443 Passes.push_back(Batcher);
444 Batcher = 0;
445 }
446 }
447};
448
449
450
451//===----------------------------------------------------------------------===//
452// PassManagerTraits<BasicBlock> Specialization
453//
454// This pass manager is used to group together all of the BasicBlockPass's
455// into a single unit.
456//
457template<> struct PassManagerTraits<BasicBlock> : public BasicBlockPass {
458 // PassClass - The type of passes tracked by this PassManager
459 typedef BasicBlockPass PassClass;
460
461 // SubPassClass - The types of classes that should be collated together
462 // This is impossible to match, so BasicBlock instantiations of PassManagerT
463 // do not collate.
464 //
465 typedef PassManagerT<Module> SubPassClass;
466
467 // BatcherClass - The type to use for collation of subtypes... This class is
468 // never instantiated for the PassManager<BasicBlock>, but it must be an
469 // instance of PassClass to typecheck.
470 //
471 typedef PassClass BatcherClass;
472
473 // ParentClass - The type of the parent PassManager...
Chris Lattner4e8c4872002-03-23 22:51:58 +0000474 typedef PassManagerT<Function> ParentClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000475
Chris Lattner2eaac392002-01-31 00:40:44 +0000476 // PMType - The type of the passmanager that subclasses this class
477 typedef PassManagerT<BasicBlock> PMType;
478
Chris Lattner67d25652002-01-30 23:20:39 +0000479 // runPass - Specify how the pass should be run on the UnitType
480 static bool runPass(PassClass *P, BasicBlock *M) {
481 // todo, init and finalize
Chris Lattner113f4f42002-06-25 16:13:24 +0000482 return P->runOnBasicBlock(*M);
Chris Lattner67d25652002-01-30 23:20:39 +0000483 }
484
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000485 // Dummy implementation of PassStarted/PassEnded
486 static void PassStarted(Pass *P) {}
487 static void PassEnded(Pass *P) {}
488
Chris Lattnerac3e0602002-01-31 18:32:27 +0000489 // getPMName() - Return the name of the unit the PassManager operates on for
490 // debugging.
491 const char *getPMName() const { return "BasicBlock"; }
Chris Lattner37104aa2002-04-29 14:57:45 +0000492 virtual const char *getPassName() const { return "BasicBlock Pass Manager"; }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000493
Chris Lattner2eaac392002-01-31 00:40:44 +0000494 // Implement the BasicBlockPass interface...
Chris Lattner113f4f42002-06-25 16:13:24 +0000495 virtual bool doInitialization(Module &M);
496 virtual bool runOnBasicBlock(BasicBlock &BB);
497 virtual bool doFinalization(Module &M);
Chris Lattnercb429062002-04-30 18:50:17 +0000498
499 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
500 AU.setPreservesAll();
501 }
Chris Lattner67d25652002-01-30 23:20:39 +0000502};
503
504
505
506//===----------------------------------------------------------------------===//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000507// PassManagerTraits<Function> Specialization
Chris Lattner67d25652002-01-30 23:20:39 +0000508//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000509// This pass manager is used to group together all of the FunctionPass's
Chris Lattner67d25652002-01-30 23:20:39 +0000510// into a single unit.
511//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000512template<> struct PassManagerTraits<Function> : public FunctionPass {
Chris Lattner67d25652002-01-30 23:20:39 +0000513 // PassClass - The type of passes tracked by this PassManager
Chris Lattnerc8e66542002-04-27 06:56:12 +0000514 typedef FunctionPass PassClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000515
516 // SubPassClass - The types of classes that should be collated together
517 typedef BasicBlockPass SubPassClass;
518
519 // BatcherClass - The type to use for collation of subtypes...
520 typedef PassManagerT<BasicBlock> BatcherClass;
521
522 // ParentClass - The type of the parent PassManager...
523 typedef PassManagerT<Module> ParentClass;
524
525 // PMType - The type of the passmanager that subclasses this class
Chris Lattner4e8c4872002-03-23 22:51:58 +0000526 typedef PassManagerT<Function> PMType;
Chris Lattner67d25652002-01-30 23:20:39 +0000527
528 // runPass - Specify how the pass should be run on the UnitType
Chris Lattnerc8e66542002-04-27 06:56:12 +0000529 static bool runPass(PassClass *P, Function *F) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000530 return P->runOnFunction(*F);
Chris Lattner67d25652002-01-30 23:20:39 +0000531 }
532
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000533 // Dummy implementation of PassStarted/PassEnded
534 static void PassStarted(Pass *P) {}
535 static void PassEnded(Pass *P) {}
536
Chris Lattnerac3e0602002-01-31 18:32:27 +0000537 // getPMName() - Return the name of the unit the PassManager operates on for
538 // debugging.
Chris Lattner4e8c4872002-03-23 22:51:58 +0000539 const char *getPMName() const { return "Function"; }
Chris Lattner37104aa2002-04-29 14:57:45 +0000540 virtual const char *getPassName() const { return "Function Pass Manager"; }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000541
Chris Lattnerc8e66542002-04-27 06:56:12 +0000542 // Implement the FunctionPass interface...
Chris Lattner113f4f42002-06-25 16:13:24 +0000543 virtual bool doInitialization(Module &M);
544 virtual bool runOnFunction(Function &F);
545 virtual bool doFinalization(Module &M);
Chris Lattnercb429062002-04-30 18:50:17 +0000546
547 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
548 AU.setPreservesAll();
549 }
Chris Lattner67d25652002-01-30 23:20:39 +0000550};
551
552
553
554//===----------------------------------------------------------------------===//
555// PassManagerTraits<Module> Specialization
556//
557// This is the top level PassManager implementation that holds generic passes.
558//
559template<> struct PassManagerTraits<Module> : public Pass {
560 // PassClass - The type of passes tracked by this PassManager
561 typedef Pass PassClass;
562
563 // SubPassClass - The types of classes that should be collated together
Chris Lattnerc8e66542002-04-27 06:56:12 +0000564 typedef FunctionPass SubPassClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000565
566 // BatcherClass - The type to use for collation of subtypes...
Chris Lattner4e8c4872002-03-23 22:51:58 +0000567 typedef PassManagerT<Function> BatcherClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000568
569 // ParentClass - The type of the parent PassManager...
Chris Lattnerac3e0602002-01-31 18:32:27 +0000570 typedef AnalysisResolver ParentClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000571
572 // runPass - Specify how the pass should be run on the UnitType
Chris Lattner113f4f42002-06-25 16:13:24 +0000573 static bool runPass(PassClass *P, Module *M) { return P->run(*M); }
Chris Lattner67d25652002-01-30 23:20:39 +0000574
Chris Lattnerac3e0602002-01-31 18:32:27 +0000575 // getPMName() - Return the name of the unit the PassManager operates on for
576 // debugging.
577 const char *getPMName() const { return "Module"; }
Chris Lattner37104aa2002-04-29 14:57:45 +0000578 virtual const char *getPassName() const { return "Module Pass Manager"; }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000579
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000580 // TimingInformation - This data member maintains timing information for each
581 // of the passes that is executed.
582 //
583 TimingInfo *TimeInfo;
584
585 // PassStarted/Ended - This callback is notified any time a pass is started
586 // or stops. This is used to collect timing information about the different
587 // passes being executed.
588 //
589 void PassStarted(Pass *P) {
590 if (TimeInfo) TimeInfo->passStarted(P);
Chris Lattner67d25652002-01-30 23:20:39 +0000591 }
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000592 void PassEnded(Pass *P) {
593 if (TimeInfo) TimeInfo->passEnded(P);
594 }
595
596 // run - Implement the PassManager interface...
Chris Lattner113f4f42002-06-25 16:13:24 +0000597 bool run(Module &M) {
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000598 TimeInfo = TimingInfo::create();
Chris Lattner113f4f42002-06-25 16:13:24 +0000599 bool Result = ((PassManagerT<Module>*)this)->runOnUnit(&M);
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000600 if (TimeInfo) {
601 delete TimeInfo;
602 TimeInfo = 0;
603 }
604 return Result;
605 }
606
607 // PassManagerTraits constructor - Create a timing info object if the user
608 // specified timing info should be collected on the command line.
609 //
610 PassManagerTraits() : TimeInfo(0) {}
Chris Lattner67d25652002-01-30 23:20:39 +0000611};
612
613
614
615//===----------------------------------------------------------------------===//
616// PassManagerTraits Method Implementations
617//
618
619// PassManagerTraits<BasicBlock> Implementations
620//
Chris Lattner113f4f42002-06-25 16:13:24 +0000621inline bool PassManagerTraits<BasicBlock>::doInitialization(Module &M) {
Chris Lattner2eaac392002-01-31 00:40:44 +0000622 bool Changed = false;
623 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
624 ((PMType*)this)->Passes[i]->doInitialization(M);
625 return Changed;
626}
627
Chris Lattner113f4f42002-06-25 16:13:24 +0000628inline bool PassManagerTraits<BasicBlock>::runOnBasicBlock(BasicBlock &BB) {
629 return ((PMType*)this)->runOnUnit(&BB);
Chris Lattner2eaac392002-01-31 00:40:44 +0000630}
631
Chris Lattner113f4f42002-06-25 16:13:24 +0000632inline bool PassManagerTraits<BasicBlock>::doFinalization(Module &M) {
Chris Lattner2eaac392002-01-31 00:40:44 +0000633 bool Changed = false;
634 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
635 ((PMType*)this)->Passes[i]->doFinalization(M);
636 return Changed;
Chris Lattner67d25652002-01-30 23:20:39 +0000637}
638
639
Chris Lattner4e8c4872002-03-23 22:51:58 +0000640// PassManagerTraits<Function> Implementations
Chris Lattner67d25652002-01-30 23:20:39 +0000641//
Chris Lattner113f4f42002-06-25 16:13:24 +0000642inline bool PassManagerTraits<Function>::doInitialization(Module &M) {
Chris Lattner67d25652002-01-30 23:20:39 +0000643 bool Changed = false;
644 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
645 ((PMType*)this)->Passes[i]->doInitialization(M);
646 return Changed;
647}
648
Chris Lattner113f4f42002-06-25 16:13:24 +0000649inline bool PassManagerTraits<Function>::runOnFunction(Function &F) {
650 return ((PMType*)this)->runOnUnit(&F);
Chris Lattner67d25652002-01-30 23:20:39 +0000651}
652
Chris Lattner113f4f42002-06-25 16:13:24 +0000653inline bool PassManagerTraits<Function>::doFinalization(Module &M) {
Chris Lattner67d25652002-01-30 23:20:39 +0000654 bool Changed = false;
655 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
656 ((PMType*)this)->Passes[i]->doFinalization(M);
657 return Changed;
658}
659
660#endif