blob: 9f34f15cca5911c2024e209d48eacaf2ac11b120 [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);
Chris Lattnere821d782002-08-20 18:47:53 +000088 bool operator<(const TimeRecord &TR) const;
Chris Lattner6a33d6f2002-08-01 19:33:09 +000089
90 void print(const char *PassName, const TimeRecord &TotalTime) const;
91};
92
Chris Lattnere2eb99e2002-04-29 04:04:29 +000093class TimingInfo {
Chris Lattner6a33d6f2002-08-01 19:33:09 +000094 std::map<Pass*, TimeRecord> TimingData;
Chris Lattnere2eb99e2002-04-29 04:04:29 +000095 TimingInfo() {} // Private ctor, must use create member
96public:
97 // Create method. If Timing is enabled, this creates and returns a new timing
98 // object, otherwise it returns null.
99 //
100 static TimingInfo *create();
101
102 // TimingDtor - Print out information about timing information
103 ~TimingInfo();
104
105 void passStarted(Pass *P);
106 void passEnded(Pass *P);
107};
108
109
Chris Lattner67d25652002-01-30 23:20:39 +0000110
111//===----------------------------------------------------------------------===//
112// Declare the PassManagerTraits which will be specialized...
113//
114template<class UnitType> class PassManagerTraits; // Do not define.
115
116
117//===----------------------------------------------------------------------===//
118// PassManagerT - Container object for passes. The PassManagerT destructor
119// deletes all passes contained inside of the PassManagerT, so you shouldn't
120// delete passes manually, and all passes should be dynamically allocated.
121//
122template<typename UnitType>
123class PassManagerT : public PassManagerTraits<UnitType>,public AnalysisResolver{
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000124 typedef PassManagerTraits<UnitType> Traits;
125 typedef typename Traits::PassClass PassClass;
126 typedef typename Traits::SubPassClass SubPassClass;
127 typedef typename Traits::BatcherClass BatcherClass;
128 typedef typename Traits::ParentClass ParentClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000129
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000130 friend typename Traits::PassClass;
131 friend typename Traits::SubPassClass;
132 friend class Traits;
Chris Lattner67d25652002-01-30 23:20:39 +0000133
Chris Lattner73503172002-07-29 21:03:38 +0000134 std::vector<PassClass*> Passes; // List of passes to run
Chris Lattner67d25652002-01-30 23:20:39 +0000135
136 // The parent of this pass manager...
Chris Lattnerac3e0602002-01-31 18:32:27 +0000137 ParentClass * const Parent;
Chris Lattner67d25652002-01-30 23:20:39 +0000138
139 // The current batcher if one is in use, or null
140 BatcherClass *Batcher;
141
142 // CurrentAnalyses - As the passes are being run, this map contains the
143 // analyses that are available to the current pass for use. This is accessed
144 // through the getAnalysis() function in this class and in Pass.
145 //
146 std::map<AnalysisID, Pass*> CurrentAnalyses;
147
Chris Lattnerac3e0602002-01-31 18:32:27 +0000148 // LastUseOf - This map keeps track of the last usage in our pipeline of a
149 // particular pass. When executing passes, the memory for .first is free'd
150 // after .second is run.
151 //
152 std::map<Pass*, Pass*> LastUseOf;
153
Chris Lattner67d25652002-01-30 23:20:39 +0000154public:
155 PassManagerT(ParentClass *Par = 0) : Parent(Par), Batcher(0) {}
156 ~PassManagerT() {
157 // Delete all of the contained passes...
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000158 for (typename std::vector<PassClass*>::iterator
159 I = Passes.begin(), E = Passes.end(); I != E; ++I)
Chris Lattner67d25652002-01-30 23:20:39 +0000160 delete *I;
161 }
162
163 // run - Run all of the queued passes on the specified module in an optimal
164 // way.
165 virtual bool runOnUnit(UnitType *M) {
166 bool MadeChanges = false;
167 closeBatcher();
168 CurrentAnalyses.clear();
169
Chris Lattnerac3e0602002-01-31 18:32:27 +0000170 // LastUserOf - This contains the inverted LastUseOfMap...
171 std::map<Pass *, std::vector<Pass*> > LastUserOf;
172 for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(),
173 E = LastUseOf.end(); I != E; ++I)
174 LastUserOf[I->second].push_back(I->first);
175
176
Chris Lattner67d25652002-01-30 23:20:39 +0000177 // Output debug information...
Chris Lattner1e4867f2002-07-30 19:51:02 +0000178 if (Parent == 0) PMDebug::PerformPassStartupStuff(this);
Chris Lattner67d25652002-01-30 23:20:39 +0000179
180 // Run all of the passes
181 for (unsigned i = 0, e = Passes.size(); i < e; ++i) {
182 PassClass *P = Passes[i];
183
Chris Lattnera454b5b2002-04-28 05:14:06 +0000184 PMDebug::PrintPassInformation(getDepth(), "Executing Pass", P,
185 (Annotable*)M);
Chris Lattner67d25652002-01-30 23:20:39 +0000186
187 // Get information about what analyses the pass uses...
Chris Lattnerc8e66542002-04-27 06:56:12 +0000188 AnalysisUsage AnUsage;
189 P->getAnalysisUsage(AnUsage);
190 PMDebug::PrintAnalysisSetInfo(getDepth(), "Required", P,
191 AnUsage.getRequiredSet());
Chris Lattner67d25652002-01-30 23:20:39 +0000192
193#ifndef NDEBUG
194 // All Required analyses should be available to the pass as it runs!
Anand Shukla8c377892002-06-25 22:07:38 +0000195 for (std::vector<AnalysisID>::const_iterator
Chris Lattnerc8e66542002-04-27 06:56:12 +0000196 I = AnUsage.getRequiredSet().begin(),
197 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Chris Lattner67d25652002-01-30 23:20:39 +0000198 assert(getAnalysisOrNullUp(*I) && "Analysis used but not available!");
199 }
200#endif
201
202 // Run the sub pass!
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000203 startPass(P);
204 bool Changed = runPass(P, M);
205 endPass(P);
Chris Lattnerf2f31bd2002-02-01 04:53:36 +0000206 MadeChanges |= Changed;
Chris Lattner67d25652002-01-30 23:20:39 +0000207
Chris Lattnerf2f31bd2002-02-01 04:53:36 +0000208 if (Changed)
209 PMDebug::PrintPassInformation(getDepth()+1, "Made Modification", P,
Chris Lattnera454b5b2002-04-28 05:14:06 +0000210 (Annotable*)M);
Chris Lattnerc8e66542002-04-27 06:56:12 +0000211 PMDebug::PrintAnalysisSetInfo(getDepth(), "Preserved", P,
212 AnUsage.getPreservedSet());
Chris Lattner67d25652002-01-30 23:20:39 +0000213
Chris Lattnerc8e66542002-04-27 06:56:12 +0000214
215 // Erase all analyses not in the preserved set...
216 if (!AnUsage.preservesAll()) {
217 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
218 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(),
219 E = CurrentAnalyses.end(); I != E; )
220 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) !=
221 PreservedSet.end())
222 ++I; // This analysis is preserved, leave it in the available set...
223 else {
224#if MAP_DOESNT_HAVE_BROKEN_ERASE_MEMBER
225 I = CurrentAnalyses.erase(I); // Analysis not preserved!
226#else
227 // GCC 2.95.3 STL doesn't have correct erase member!
228 CurrentAnalyses.erase(I);
229 I = CurrentAnalyses.begin();
230#endif
231 }
232 }
233
Chris Lattner73503172002-07-29 21:03:38 +0000234 // Add the current pass to the set of passes that have been run, and are
235 // thus available to users.
236 //
237 if (const PassInfo *PI = P->getPassInfo())
238 CurrentAnalyses[PI] = P;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000239
240 // Free memory for any passes that we are the last use of...
241 std::vector<Pass*> &DeadPass = LastUserOf[P];
242 for (std::vector<Pass*>::iterator I = DeadPass.begin(),E = DeadPass.end();
243 I != E; ++I) {
244 PMDebug::PrintPassInformation(getDepth()+1, "Freeing Pass", *I,
Chris Lattnera454b5b2002-04-28 05:14:06 +0000245 (Annotable*)M);
Chris Lattnerac3e0602002-01-31 18:32:27 +0000246 (*I)->releaseMemory();
247 }
Chris Lattner67d25652002-01-30 23:20:39 +0000248 }
249 return MadeChanges;
250 }
251
Chris Lattnerac3e0602002-01-31 18:32:27 +0000252 // dumpPassStructure - Implement the -debug-passes=PassStructure option
253 virtual void dumpPassStructure(unsigned Offset = 0) {
254 std::cerr << std::string(Offset*2, ' ') << Traits::getPMName()
255 << " Pass Manager\n";
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000256 for (typename std::vector<PassClass*>::iterator
257 I = Passes.begin(), E = Passes.end(); I != E; ++I) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000258 PassClass *P = *I;
259 P->dumpPassStructure(Offset+1);
260
261 // Loop through and see which classes are destroyed after this one...
262 for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(),
263 E = LastUseOf.end(); I != E; ++I) {
264 if (P == I->second) {
Chris Lattner73503172002-07-29 21:03:38 +0000265 std::cerr << "--" << std::string(Offset*2, ' ');
Chris Lattnerac3e0602002-01-31 18:32:27 +0000266 I->first->dumpPassStructure(0);
267 }
268 }
269 }
270 }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000271
272 Pass *getAnalysisOrNullDown(AnalysisID ID) const {
273 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID);
274 if (I == CurrentAnalyses.end()) {
275 if (Batcher)
276 return ((AnalysisResolver*)Batcher)->getAnalysisOrNullDown(ID);
277 return 0;
278 }
279 return I->second;
280 }
281
282 Pass *getAnalysisOrNullUp(AnalysisID ID) const {
283 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID);
284 if (I == CurrentAnalyses.end()) {
285 if (Parent)
286 return Parent->getAnalysisOrNullUp(ID);
287 return 0;
288 }
289 return I->second;
290 }
291
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000292 // {start/end}Pass - Called when a pass is started, it just propogates
293 // information up to the top level PassManagerT object to tell it that a pass
294 // has started or ended. This is used to gather timing information about
295 // passes.
296 //
297 void startPass(Pass *P) {
298 if (Parent) Parent->startPass(P);
299 else PassStarted(P);
300 }
301 void endPass(Pass *P) {
302 if (Parent) Parent->endPass(P);
303 else PassEnded(P);
304 }
305
Chris Lattnerac3e0602002-01-31 18:32:27 +0000306 // markPassUsed - Inform higher level pass managers (and ourselves)
307 // that these analyses are being used by this pass. This is used to
308 // make sure that analyses are not free'd before we have to use
309 // them...
310 //
311 void markPassUsed(AnalysisID P, Pass *User) {
312 std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.find(P);
313 if (I != CurrentAnalyses.end()) {
314 LastUseOf[I->second] = User; // Local pass, extend the lifetime
315 } else {
316 // Pass not in current available set, must be a higher level pass
317 // available to us, propogate to parent pass manager... We tell the
318 // parent that we (the passmanager) are using the analysis so that it
319 // frees the analysis AFTER this pass manager runs.
320 //
Chris Lattnerde421a72002-03-17 21:16:01 +0000321 assert(Parent != 0 && "Pass available but not found! "
322 "Did your analysis pass 'Provide' itself?");
Chris Lattnerac3e0602002-01-31 18:32:27 +0000323 Parent->markPassUsed(P, this);
324 }
325 }
326
327 // Return the number of parent PassManagers that exist
328 virtual unsigned getDepth() const {
329 if (Parent == 0) return 0;
330 return 1 + Parent->getDepth();
331 }
332
Chris Lattner1e4867f2002-07-30 19:51:02 +0000333 virtual unsigned getNumContainedPasses() const { return Passes.size(); }
334 virtual const Pass *getContainedPass(unsigned N) const {
335 assert(N < Passes.size() && "Pass number out of range!");
336 return Passes[N];
337 }
338
Chris Lattner67d25652002-01-30 23:20:39 +0000339 // add - Add a pass to the queue of passes to run. This passes ownership of
340 // the Pass to the PassManager. When the PassManager is destroyed, the pass
Chris Lattnerac3e0602002-01-31 18:32:27 +0000341 // will be destroyed as well, so there is no need to delete the pass. This
342 // implies that all passes MUST be new'd.
Chris Lattner67d25652002-01-30 23:20:39 +0000343 //
344 void add(PassClass *P) {
345 // Get information about what analyses the pass uses...
Chris Lattnerc8e66542002-04-27 06:56:12 +0000346 AnalysisUsage AnUsage;
347 P->getAnalysisUsage(AnUsage);
348 const std::vector<AnalysisID> &Required = AnUsage.getRequiredSet();
Chris Lattner67d25652002-01-30 23:20:39 +0000349
350 // Loop over all of the analyses used by this pass,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000351 for (std::vector<AnalysisID>::const_iterator I = Required.begin(),
352 E = Required.end(); I != E; ++I) {
Chris Lattner67d25652002-01-30 23:20:39 +0000353 if (getAnalysisOrNullDown(*I) == 0)
Chris Lattner26750072002-07-27 01:12:17 +0000354 add((PassClass*)(*I)->createPass());
Chris Lattner67d25652002-01-30 23:20:39 +0000355 }
356
357 // Tell the pass to add itself to this PassManager... the way it does so
358 // depends on the class of the pass, and is critical to laying out passes in
359 // an optimal order..
360 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000361 P->addToPassManager(this, AnUsage);
Chris Lattner67d25652002-01-30 23:20:39 +0000362 }
363
364private:
365
366 // addPass - These functions are used to implement the subclass specific
367 // behaviors present in PassManager. Basically the add(Pass*) method ends up
368 // reflecting its behavior into a Pass::addToPassManager call. Subclasses of
369 // Pass override it specifically so that they can reflect the type
370 // information inherent in "this" back to the PassManager.
371 //
372 // For generic Pass subclasses (which are interprocedural passes), we simply
373 // add the pass to the end of the pass list and terminate any accumulation of
Chris Lattnerc8e66542002-04-27 06:56:12 +0000374 // FunctionPass's that are present.
Chris Lattner67d25652002-01-30 23:20:39 +0000375 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000376 void addPass(PassClass *P, AnalysisUsage &AnUsage) {
377 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
Chris Lattnerc8e66542002-04-27 06:56:12 +0000378
Chris Lattner73503172002-07-29 21:03:38 +0000379 // FIXME: If this pass being added isn't killed by any of the passes in the
380 // batcher class then we can reorder to pass to execute before the batcher
381 // does, which will potentially allow us to batch more passes!
Chris Lattner67d25652002-01-30 23:20:39 +0000382 //
Chris Lattner73503172002-07-29 21:03:38 +0000383 //const std::vector<AnalysisID> &ProvidedSet = AnUsage.getProvidedSet();
384 if (Batcher /*&& ProvidedSet.empty()*/)
Chris Lattner67d25652002-01-30 23:20:39 +0000385 closeBatcher(); // This pass cannot be batched!
386
387 // Set the Resolver instance variable in the Pass so that it knows where to
388 // find this object...
389 //
390 setAnalysisResolver(P, this);
391 Passes.push_back(P);
392
Chris Lattnerac3e0602002-01-31 18:32:27 +0000393 // Inform higher level pass managers (and ourselves) that these analyses are
394 // being used by this pass. This is used to make sure that analyses are not
395 // free'd before we have to use them...
396 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000397 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
398 E = RequiredSet.end(); I != E; ++I)
Chris Lattnerac3e0602002-01-31 18:32:27 +0000399 markPassUsed(*I, P); // Mark *I as used by P
400
Chris Lattnerc8e66542002-04-27 06:56:12 +0000401 // Erase all analyses not in the preserved set...
402 if (!AnUsage.preservesAll()) {
403 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
404 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(),
405 E = CurrentAnalyses.end(); I != E; )
406 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) !=
407 PreservedSet.end())
408 ++I; // This analysis is preserved, leave it in the available set...
409 else {
410#if MAP_DOESNT_HAVE_BROKEN_ERASE_MEMBER
411 I = CurrentAnalyses.erase(I); // Analysis not preserved!
412#else
413 CurrentAnalyses.erase(I);// GCC 2.95.3 STL doesn't have correct erase!
414 I = CurrentAnalyses.begin();
415#endif
416 }
417 }
Chris Lattner67d25652002-01-30 23:20:39 +0000418
Chris Lattner73503172002-07-29 21:03:38 +0000419 // Add this pass to the currently available set...
420 if (const PassInfo *PI = P->getPassInfo())
421 CurrentAnalyses[PI] = P;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000422
423 // For now assume that our results are never used...
424 LastUseOf[P] = P;
Chris Lattner67d25652002-01-30 23:20:39 +0000425 }
426
Chris Lattnerc8e66542002-04-27 06:56:12 +0000427 // For FunctionPass subclasses, we must be sure to batch the FunctionPass's
428 // together in a BatcherClass object so that all of the analyses are run
429 // together a function at a time.
Chris Lattner67d25652002-01-30 23:20:39 +0000430 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000431 void addPass(SubPassClass *MP, AnalysisUsage &AnUsage) {
Chris Lattner67d25652002-01-30 23:20:39 +0000432 if (Batcher == 0) // If we don't have a batcher yet, make one now.
433 Batcher = new BatcherClass(this);
Chris Lattner73503172002-07-29 21:03:38 +0000434 // The Batcher will queue the passes up
Chris Lattnerc8e66542002-04-27 06:56:12 +0000435 MP->addToPassManager(Batcher, AnUsage);
Chris Lattner67d25652002-01-30 23:20:39 +0000436 }
437
438 // closeBatcher - Terminate the batcher that is being worked on.
439 void closeBatcher() {
440 if (Batcher) {
441 Passes.push_back(Batcher);
442 Batcher = 0;
443 }
444 }
445};
446
447
448
449//===----------------------------------------------------------------------===//
450// PassManagerTraits<BasicBlock> Specialization
451//
452// This pass manager is used to group together all of the BasicBlockPass's
453// into a single unit.
454//
455template<> struct PassManagerTraits<BasicBlock> : public BasicBlockPass {
456 // PassClass - The type of passes tracked by this PassManager
457 typedef BasicBlockPass PassClass;
458
459 // SubPassClass - The types of classes that should be collated together
460 // This is impossible to match, so BasicBlock instantiations of PassManagerT
461 // do not collate.
462 //
463 typedef PassManagerT<Module> SubPassClass;
464
465 // BatcherClass - The type to use for collation of subtypes... This class is
466 // never instantiated for the PassManager<BasicBlock>, but it must be an
467 // instance of PassClass to typecheck.
468 //
469 typedef PassClass BatcherClass;
470
471 // ParentClass - The type of the parent PassManager...
Chris Lattner4e8c4872002-03-23 22:51:58 +0000472 typedef PassManagerT<Function> ParentClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000473
Chris Lattner2eaac392002-01-31 00:40:44 +0000474 // PMType - The type of the passmanager that subclasses this class
475 typedef PassManagerT<BasicBlock> PMType;
476
Chris Lattner67d25652002-01-30 23:20:39 +0000477 // runPass - Specify how the pass should be run on the UnitType
478 static bool runPass(PassClass *P, BasicBlock *M) {
479 // todo, init and finalize
Chris Lattner113f4f42002-06-25 16:13:24 +0000480 return P->runOnBasicBlock(*M);
Chris Lattner67d25652002-01-30 23:20:39 +0000481 }
482
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000483 // Dummy implementation of PassStarted/PassEnded
484 static void PassStarted(Pass *P) {}
485 static void PassEnded(Pass *P) {}
486
Chris Lattnerac3e0602002-01-31 18:32:27 +0000487 // getPMName() - Return the name of the unit the PassManager operates on for
488 // debugging.
489 const char *getPMName() const { return "BasicBlock"; }
Chris Lattner37104aa2002-04-29 14:57:45 +0000490 virtual const char *getPassName() const { return "BasicBlock Pass Manager"; }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000491
Chris Lattner2eaac392002-01-31 00:40:44 +0000492 // Implement the BasicBlockPass interface...
Chris Lattner113f4f42002-06-25 16:13:24 +0000493 virtual bool doInitialization(Module &M);
494 virtual bool runOnBasicBlock(BasicBlock &BB);
495 virtual bool doFinalization(Module &M);
Chris Lattnercb429062002-04-30 18:50:17 +0000496
497 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
498 AU.setPreservesAll();
499 }
Chris Lattner67d25652002-01-30 23:20:39 +0000500};
501
502
503
504//===----------------------------------------------------------------------===//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000505// PassManagerTraits<Function> Specialization
Chris Lattner67d25652002-01-30 23:20:39 +0000506//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000507// This pass manager is used to group together all of the FunctionPass's
Chris Lattner67d25652002-01-30 23:20:39 +0000508// into a single unit.
509//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000510template<> struct PassManagerTraits<Function> : public FunctionPass {
Chris Lattner67d25652002-01-30 23:20:39 +0000511 // PassClass - The type of passes tracked by this PassManager
Chris Lattnerc8e66542002-04-27 06:56:12 +0000512 typedef FunctionPass PassClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000513
514 // SubPassClass - The types of classes that should be collated together
515 typedef BasicBlockPass SubPassClass;
516
517 // BatcherClass - The type to use for collation of subtypes...
518 typedef PassManagerT<BasicBlock> BatcherClass;
519
520 // ParentClass - The type of the parent PassManager...
521 typedef PassManagerT<Module> ParentClass;
522
523 // PMType - The type of the passmanager that subclasses this class
Chris Lattner4e8c4872002-03-23 22:51:58 +0000524 typedef PassManagerT<Function> PMType;
Chris Lattner67d25652002-01-30 23:20:39 +0000525
526 // runPass - Specify how the pass should be run on the UnitType
Chris Lattnerc8e66542002-04-27 06:56:12 +0000527 static bool runPass(PassClass *P, Function *F) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000528 return P->runOnFunction(*F);
Chris Lattner67d25652002-01-30 23:20:39 +0000529 }
530
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000531 // Dummy implementation of PassStarted/PassEnded
532 static void PassStarted(Pass *P) {}
533 static void PassEnded(Pass *P) {}
534
Chris Lattnerac3e0602002-01-31 18:32:27 +0000535 // getPMName() - Return the name of the unit the PassManager operates on for
536 // debugging.
Chris Lattner4e8c4872002-03-23 22:51:58 +0000537 const char *getPMName() const { return "Function"; }
Chris Lattner37104aa2002-04-29 14:57:45 +0000538 virtual const char *getPassName() const { return "Function Pass Manager"; }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000539
Chris Lattnerc8e66542002-04-27 06:56:12 +0000540 // Implement the FunctionPass interface...
Chris Lattner113f4f42002-06-25 16:13:24 +0000541 virtual bool doInitialization(Module &M);
542 virtual bool runOnFunction(Function &F);
543 virtual bool doFinalization(Module &M);
Chris Lattnercb429062002-04-30 18:50:17 +0000544
545 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
546 AU.setPreservesAll();
547 }
Chris Lattner67d25652002-01-30 23:20:39 +0000548};
549
550
551
552//===----------------------------------------------------------------------===//
553// PassManagerTraits<Module> Specialization
554//
555// This is the top level PassManager implementation that holds generic passes.
556//
557template<> struct PassManagerTraits<Module> : public Pass {
558 // PassClass - The type of passes tracked by this PassManager
559 typedef Pass PassClass;
560
561 // SubPassClass - The types of classes that should be collated together
Chris Lattnerc8e66542002-04-27 06:56:12 +0000562 typedef FunctionPass SubPassClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000563
564 // BatcherClass - The type to use for collation of subtypes...
Chris Lattner4e8c4872002-03-23 22:51:58 +0000565 typedef PassManagerT<Function> BatcherClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000566
567 // ParentClass - The type of the parent PassManager...
Chris Lattnerac3e0602002-01-31 18:32:27 +0000568 typedef AnalysisResolver ParentClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000569
570 // runPass - Specify how the pass should be run on the UnitType
Chris Lattner113f4f42002-06-25 16:13:24 +0000571 static bool runPass(PassClass *P, Module *M) { return P->run(*M); }
Chris Lattner67d25652002-01-30 23:20:39 +0000572
Chris Lattnerac3e0602002-01-31 18:32:27 +0000573 // getPMName() - Return the name of the unit the PassManager operates on for
574 // debugging.
575 const char *getPMName() const { return "Module"; }
Chris Lattner37104aa2002-04-29 14:57:45 +0000576 virtual const char *getPassName() const { return "Module Pass Manager"; }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000577
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000578 // TimingInformation - This data member maintains timing information for each
579 // of the passes that is executed.
580 //
581 TimingInfo *TimeInfo;
582
583 // PassStarted/Ended - This callback is notified any time a pass is started
584 // or stops. This is used to collect timing information about the different
585 // passes being executed.
586 //
587 void PassStarted(Pass *P) {
588 if (TimeInfo) TimeInfo->passStarted(P);
Chris Lattner67d25652002-01-30 23:20:39 +0000589 }
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000590 void PassEnded(Pass *P) {
591 if (TimeInfo) TimeInfo->passEnded(P);
592 }
593
594 // run - Implement the PassManager interface...
Chris Lattner113f4f42002-06-25 16:13:24 +0000595 bool run(Module &M) {
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000596 TimeInfo = TimingInfo::create();
Chris Lattner113f4f42002-06-25 16:13:24 +0000597 bool Result = ((PassManagerT<Module>*)this)->runOnUnit(&M);
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000598 if (TimeInfo) {
599 delete TimeInfo;
600 TimeInfo = 0;
601 }
602 return Result;
603 }
604
605 // PassManagerTraits constructor - Create a timing info object if the user
606 // specified timing info should be collected on the command line.
607 //
608 PassManagerTraits() : TimeInfo(0) {}
Chris Lattner67d25652002-01-30 23:20:39 +0000609};
610
611
612
613//===----------------------------------------------------------------------===//
614// PassManagerTraits Method Implementations
615//
616
617// PassManagerTraits<BasicBlock> Implementations
618//
Chris Lattner113f4f42002-06-25 16:13:24 +0000619inline bool PassManagerTraits<BasicBlock>::doInitialization(Module &M) {
Chris Lattner2eaac392002-01-31 00:40:44 +0000620 bool Changed = false;
621 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
622 ((PMType*)this)->Passes[i]->doInitialization(M);
623 return Changed;
624}
625
Chris Lattner113f4f42002-06-25 16:13:24 +0000626inline bool PassManagerTraits<BasicBlock>::runOnBasicBlock(BasicBlock &BB) {
627 return ((PMType*)this)->runOnUnit(&BB);
Chris Lattner2eaac392002-01-31 00:40:44 +0000628}
629
Chris Lattner113f4f42002-06-25 16:13:24 +0000630inline bool PassManagerTraits<BasicBlock>::doFinalization(Module &M) {
Chris Lattner2eaac392002-01-31 00:40:44 +0000631 bool Changed = false;
632 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
633 ((PMType*)this)->Passes[i]->doFinalization(M);
634 return Changed;
Chris Lattner67d25652002-01-30 23:20:39 +0000635}
636
637
Chris Lattner4e8c4872002-03-23 22:51:58 +0000638// PassManagerTraits<Function> Implementations
Chris Lattner67d25652002-01-30 23:20:39 +0000639//
Chris Lattner113f4f42002-06-25 16:13:24 +0000640inline bool PassManagerTraits<Function>::doInitialization(Module &M) {
Chris Lattner67d25652002-01-30 23:20:39 +0000641 bool Changed = false;
642 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
643 ((PMType*)this)->Passes[i]->doInitialization(M);
644 return Changed;
645}
646
Chris Lattner113f4f42002-06-25 16:13:24 +0000647inline bool PassManagerTraits<Function>::runOnFunction(Function &F) {
648 return ((PMType*)this)->runOnUnit(&F);
Chris Lattner67d25652002-01-30 23:20:39 +0000649}
650
Chris Lattner113f4f42002-06-25 16:13:24 +0000651inline bool PassManagerTraits<Function>::doFinalization(Module &M) {
Chris Lattner67d25652002-01-30 23:20:39 +0000652 bool Changed = false;
653 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
654 ((PMType*)this)->Passes[i]->doFinalization(M);
655 return Changed;
656}
657
658#endif