blob: 4e0d6b24bf30e72f1608122cdb40f973d4ec154d [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
Chris Lattner6e041bd2002-08-21 22:17:09 +0000109//===----------------------------------------------------------------------===//
110// Forward declarations of global functions defined in Pass.cpp. These are
111// defined to be static functions because this header is *ONLY* included by
112// Pass.cpp.
113//
114
115// findAnalysisGroupMember - Return an iterator pointing to one of the elements
116// of Map if there is a pass in Map that is a member of the analysis group for
117// the specified AnalysisGroupID.
118//
119static std::map<const PassInfo*, Pass*>::const_iterator
120findAnalysisGroupMember(const PassInfo *AnalysisGroupID,
121 const std::map<const PassInfo*, Pass*> &Map);
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000122
Chris Lattner67d25652002-01-30 23:20:39 +0000123
124//===----------------------------------------------------------------------===//
125// Declare the PassManagerTraits which will be specialized...
126//
127template<class UnitType> class PassManagerTraits; // Do not define.
128
129
130//===----------------------------------------------------------------------===//
131// PassManagerT - Container object for passes. The PassManagerT destructor
132// deletes all passes contained inside of the PassManagerT, so you shouldn't
133// delete passes manually, and all passes should be dynamically allocated.
134//
135template<typename UnitType>
136class PassManagerT : public PassManagerTraits<UnitType>,public AnalysisResolver{
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000137 typedef PassManagerTraits<UnitType> Traits;
138 typedef typename Traits::PassClass PassClass;
139 typedef typename Traits::SubPassClass SubPassClass;
140 typedef typename Traits::BatcherClass BatcherClass;
141 typedef typename Traits::ParentClass ParentClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000142
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000143 friend typename Traits::PassClass;
144 friend typename Traits::SubPassClass;
145 friend class Traits;
Chris Lattner67d25652002-01-30 23:20:39 +0000146
Chris Lattner73503172002-07-29 21:03:38 +0000147 std::vector<PassClass*> Passes; // List of passes to run
Chris Lattner67d25652002-01-30 23:20:39 +0000148
149 // The parent of this pass manager...
Chris Lattnerac3e0602002-01-31 18:32:27 +0000150 ParentClass * const Parent;
Chris Lattner67d25652002-01-30 23:20:39 +0000151
152 // The current batcher if one is in use, or null
153 BatcherClass *Batcher;
154
155 // CurrentAnalyses - As the passes are being run, this map contains the
156 // analyses that are available to the current pass for use. This is accessed
157 // through the getAnalysis() function in this class and in Pass.
158 //
159 std::map<AnalysisID, Pass*> CurrentAnalyses;
160
Chris Lattnerac3e0602002-01-31 18:32:27 +0000161 // LastUseOf - This map keeps track of the last usage in our pipeline of a
162 // particular pass. When executing passes, the memory for .first is free'd
163 // after .second is run.
164 //
165 std::map<Pass*, Pass*> LastUseOf;
166
Chris Lattner67d25652002-01-30 23:20:39 +0000167public:
168 PassManagerT(ParentClass *Par = 0) : Parent(Par), Batcher(0) {}
169 ~PassManagerT() {
170 // Delete all of the contained passes...
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000171 for (typename std::vector<PassClass*>::iterator
172 I = Passes.begin(), E = Passes.end(); I != E; ++I)
Chris Lattner67d25652002-01-30 23:20:39 +0000173 delete *I;
174 }
175
176 // run - Run all of the queued passes on the specified module in an optimal
177 // way.
178 virtual bool runOnUnit(UnitType *M) {
179 bool MadeChanges = false;
180 closeBatcher();
181 CurrentAnalyses.clear();
182
Chris Lattnerac3e0602002-01-31 18:32:27 +0000183 // LastUserOf - This contains the inverted LastUseOfMap...
184 std::map<Pass *, std::vector<Pass*> > LastUserOf;
185 for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(),
186 E = LastUseOf.end(); I != E; ++I)
187 LastUserOf[I->second].push_back(I->first);
188
189
Chris Lattner67d25652002-01-30 23:20:39 +0000190 // Output debug information...
Chris Lattner1e4867f2002-07-30 19:51:02 +0000191 if (Parent == 0) PMDebug::PerformPassStartupStuff(this);
Chris Lattner67d25652002-01-30 23:20:39 +0000192
193 // Run all of the passes
194 for (unsigned i = 0, e = Passes.size(); i < e; ++i) {
195 PassClass *P = Passes[i];
196
Chris Lattnera454b5b2002-04-28 05:14:06 +0000197 PMDebug::PrintPassInformation(getDepth(), "Executing Pass", P,
198 (Annotable*)M);
Chris Lattner67d25652002-01-30 23:20:39 +0000199
200 // Get information about what analyses the pass uses...
Chris Lattnerc8e66542002-04-27 06:56:12 +0000201 AnalysisUsage AnUsage;
202 P->getAnalysisUsage(AnUsage);
203 PMDebug::PrintAnalysisSetInfo(getDepth(), "Required", P,
204 AnUsage.getRequiredSet());
Chris Lattner67d25652002-01-30 23:20:39 +0000205
206#ifndef NDEBUG
207 // All Required analyses should be available to the pass as it runs!
Anand Shukla8c377892002-06-25 22:07:38 +0000208 for (std::vector<AnalysisID>::const_iterator
Chris Lattnerc8e66542002-04-27 06:56:12 +0000209 I = AnUsage.getRequiredSet().begin(),
210 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Chris Lattner67d25652002-01-30 23:20:39 +0000211 assert(getAnalysisOrNullUp(*I) && "Analysis used but not available!");
212 }
213#endif
214
215 // Run the sub pass!
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000216 startPass(P);
217 bool Changed = runPass(P, M);
218 endPass(P);
Chris Lattnerf2f31bd2002-02-01 04:53:36 +0000219 MadeChanges |= Changed;
Chris Lattner67d25652002-01-30 23:20:39 +0000220
Chris Lattnerf2f31bd2002-02-01 04:53:36 +0000221 if (Changed)
222 PMDebug::PrintPassInformation(getDepth()+1, "Made Modification", P,
Chris Lattnera454b5b2002-04-28 05:14:06 +0000223 (Annotable*)M);
Chris Lattnerc8e66542002-04-27 06:56:12 +0000224 PMDebug::PrintAnalysisSetInfo(getDepth(), "Preserved", P,
225 AnUsage.getPreservedSet());
Chris Lattner67d25652002-01-30 23:20:39 +0000226
Chris Lattnerc8e66542002-04-27 06:56:12 +0000227
228 // Erase all analyses not in the preserved set...
229 if (!AnUsage.preservesAll()) {
230 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
231 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(),
232 E = CurrentAnalyses.end(); I != E; )
233 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) !=
234 PreservedSet.end())
235 ++I; // This analysis is preserved, leave it in the available set...
236 else {
237#if MAP_DOESNT_HAVE_BROKEN_ERASE_MEMBER
238 I = CurrentAnalyses.erase(I); // Analysis not preserved!
239#else
240 // GCC 2.95.3 STL doesn't have correct erase member!
241 CurrentAnalyses.erase(I);
242 I = CurrentAnalyses.begin();
243#endif
244 }
245 }
246
Chris Lattner73503172002-07-29 21:03:38 +0000247 // Add the current pass to the set of passes that have been run, and are
248 // thus available to users.
249 //
250 if (const PassInfo *PI = P->getPassInfo())
251 CurrentAnalyses[PI] = P;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000252
253 // Free memory for any passes that we are the last use of...
254 std::vector<Pass*> &DeadPass = LastUserOf[P];
255 for (std::vector<Pass*>::iterator I = DeadPass.begin(),E = DeadPass.end();
256 I != E; ++I) {
257 PMDebug::PrintPassInformation(getDepth()+1, "Freeing Pass", *I,
Chris Lattnera454b5b2002-04-28 05:14:06 +0000258 (Annotable*)M);
Chris Lattnerac3e0602002-01-31 18:32:27 +0000259 (*I)->releaseMemory();
260 }
Chris Lattner67d25652002-01-30 23:20:39 +0000261 }
262 return MadeChanges;
263 }
264
Chris Lattnerac3e0602002-01-31 18:32:27 +0000265 // dumpPassStructure - Implement the -debug-passes=PassStructure option
266 virtual void dumpPassStructure(unsigned Offset = 0) {
267 std::cerr << std::string(Offset*2, ' ') << Traits::getPMName()
268 << " Pass Manager\n";
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000269 for (typename std::vector<PassClass*>::iterator
270 I = Passes.begin(), E = Passes.end(); I != E; ++I) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000271 PassClass *P = *I;
272 P->dumpPassStructure(Offset+1);
273
274 // Loop through and see which classes are destroyed after this one...
275 for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(),
276 E = LastUseOf.end(); I != E; ++I) {
277 if (P == I->second) {
Chris Lattner73503172002-07-29 21:03:38 +0000278 std::cerr << "--" << std::string(Offset*2, ' ');
Chris Lattnerac3e0602002-01-31 18:32:27 +0000279 I->first->dumpPassStructure(0);
280 }
281 }
282 }
283 }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000284
Chris Lattner6e041bd2002-08-21 22:17:09 +0000285 Pass *getAnalysisOrNullDown(const PassInfo *ID) const {
286 std::map<const PassInfo*, Pass*>::const_iterator I;
287
288 if (ID->getPassType() == PassInfo::AnalysisGroup) {
289 I = findAnalysisGroupMember(ID, CurrentAnalyses);
290 } else {
291 I = CurrentAnalyses.find(ID);
Chris Lattnerac3e0602002-01-31 18:32:27 +0000292 }
Chris Lattner6e041bd2002-08-21 22:17:09 +0000293
294 if (I != CurrentAnalyses.end())
295 return I->second; // Found it.
296
297 if (Batcher)
298 return ((AnalysisResolver*)Batcher)->getAnalysisOrNullDown(ID);
299 return 0;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000300 }
301
Chris Lattner6e041bd2002-08-21 22:17:09 +0000302 Pass *getAnalysisOrNullUp(const PassInfo *ID) const {
303 std::map<AnalysisID, Pass*>::const_iterator I;
304 if (ID->getPassType() == PassInfo::AnalysisGroup) {
305 I = findAnalysisGroupMember(ID, CurrentAnalyses);
306 } else {
307 I = CurrentAnalyses.find(ID);
Chris Lattnerac3e0602002-01-31 18:32:27 +0000308 }
Chris Lattner6e041bd2002-08-21 22:17:09 +0000309 if (I != CurrentAnalyses.end())
310 return I->second; // Found it.
311
312 if (Parent) // Try scanning...
313 return Parent->getAnalysisOrNullUp(ID);
314 return 0;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000315 }
316
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000317 // {start/end}Pass - Called when a pass is started, it just propogates
318 // information up to the top level PassManagerT object to tell it that a pass
319 // has started or ended. This is used to gather timing information about
320 // passes.
321 //
322 void startPass(Pass *P) {
323 if (Parent) Parent->startPass(P);
324 else PassStarted(P);
325 }
326 void endPass(Pass *P) {
327 if (Parent) Parent->endPass(P);
328 else PassEnded(P);
329 }
330
Chris Lattnerac3e0602002-01-31 18:32:27 +0000331 // markPassUsed - Inform higher level pass managers (and ourselves)
332 // that these analyses are being used by this pass. This is used to
333 // make sure that analyses are not free'd before we have to use
334 // them...
335 //
Chris Lattner6e041bd2002-08-21 22:17:09 +0000336 void markPassUsed(const PassInfo *P, Pass *User) {
337 std::map<const PassInfo *, Pass*>::const_iterator I;
338
339 if (P->getPassType() == PassInfo::AnalysisGroup) {
340 I = findAnalysisGroupMember(P, CurrentAnalyses);
341 } else {
342 I = CurrentAnalyses.find(P);
343 }
344
Chris Lattnerac3e0602002-01-31 18:32:27 +0000345 if (I != CurrentAnalyses.end()) {
346 LastUseOf[I->second] = User; // Local pass, extend the lifetime
347 } else {
348 // Pass not in current available set, must be a higher level pass
349 // available to us, propogate to parent pass manager... We tell the
350 // parent that we (the passmanager) are using the analysis so that it
351 // frees the analysis AFTER this pass manager runs.
352 //
Chris Lattner6e041bd2002-08-21 22:17:09 +0000353 assert(Parent != 0 && "Pass available but not found!");
Chris Lattnerac3e0602002-01-31 18:32:27 +0000354 Parent->markPassUsed(P, this);
355 }
356 }
357
358 // Return the number of parent PassManagers that exist
359 virtual unsigned getDepth() const {
360 if (Parent == 0) return 0;
361 return 1 + Parent->getDepth();
362 }
363
Chris Lattner1e4867f2002-07-30 19:51:02 +0000364 virtual unsigned getNumContainedPasses() const { return Passes.size(); }
365 virtual const Pass *getContainedPass(unsigned N) const {
366 assert(N < Passes.size() && "Pass number out of range!");
367 return Passes[N];
368 }
369
Chris Lattner6e041bd2002-08-21 22:17:09 +0000370 // add - Add a pass to the queue of passes to run. This gives ownership of
Chris Lattner67d25652002-01-30 23:20:39 +0000371 // the Pass to the PassManager. When the PassManager is destroyed, the pass
Chris Lattnerac3e0602002-01-31 18:32:27 +0000372 // will be destroyed as well, so there is no need to delete the pass. This
373 // implies that all passes MUST be new'd.
Chris Lattner67d25652002-01-30 23:20:39 +0000374 //
375 void add(PassClass *P) {
376 // Get information about what analyses the pass uses...
Chris Lattnerc8e66542002-04-27 06:56:12 +0000377 AnalysisUsage AnUsage;
378 P->getAnalysisUsage(AnUsage);
379 const std::vector<AnalysisID> &Required = AnUsage.getRequiredSet();
Chris Lattner67d25652002-01-30 23:20:39 +0000380
381 // Loop over all of the analyses used by this pass,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000382 for (std::vector<AnalysisID>::const_iterator I = Required.begin(),
383 E = Required.end(); I != E; ++I) {
Chris Lattner67d25652002-01-30 23:20:39 +0000384 if (getAnalysisOrNullDown(*I) == 0)
Chris Lattner26750072002-07-27 01:12:17 +0000385 add((PassClass*)(*I)->createPass());
Chris Lattner67d25652002-01-30 23:20:39 +0000386 }
387
388 // Tell the pass to add itself to this PassManager... the way it does so
389 // depends on the class of the pass, and is critical to laying out passes in
390 // an optimal order..
391 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000392 P->addToPassManager(this, AnUsage);
Chris Lattner67d25652002-01-30 23:20:39 +0000393 }
394
395private:
396
397 // addPass - These functions are used to implement the subclass specific
398 // behaviors present in PassManager. Basically the add(Pass*) method ends up
399 // reflecting its behavior into a Pass::addToPassManager call. Subclasses of
400 // Pass override it specifically so that they can reflect the type
401 // information inherent in "this" back to the PassManager.
402 //
403 // For generic Pass subclasses (which are interprocedural passes), we simply
404 // add the pass to the end of the pass list and terminate any accumulation of
Chris Lattnerc8e66542002-04-27 06:56:12 +0000405 // FunctionPass's that are present.
Chris Lattner67d25652002-01-30 23:20:39 +0000406 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000407 void addPass(PassClass *P, AnalysisUsage &AnUsage) {
408 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
Chris Lattnerc8e66542002-04-27 06:56:12 +0000409
Chris Lattner73503172002-07-29 21:03:38 +0000410 // FIXME: If this pass being added isn't killed by any of the passes in the
411 // batcher class then we can reorder to pass to execute before the batcher
412 // does, which will potentially allow us to batch more passes!
Chris Lattner67d25652002-01-30 23:20:39 +0000413 //
Chris Lattner73503172002-07-29 21:03:38 +0000414 //const std::vector<AnalysisID> &ProvidedSet = AnUsage.getProvidedSet();
415 if (Batcher /*&& ProvidedSet.empty()*/)
Chris Lattner67d25652002-01-30 23:20:39 +0000416 closeBatcher(); // This pass cannot be batched!
417
418 // Set the Resolver instance variable in the Pass so that it knows where to
419 // find this object...
420 //
421 setAnalysisResolver(P, this);
422 Passes.push_back(P);
423
Chris Lattnerac3e0602002-01-31 18:32:27 +0000424 // Inform higher level pass managers (and ourselves) that these analyses are
425 // being used by this pass. This is used to make sure that analyses are not
426 // free'd before we have to use them...
427 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000428 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
429 E = RequiredSet.end(); I != E; ++I)
Chris Lattnerac3e0602002-01-31 18:32:27 +0000430 markPassUsed(*I, P); // Mark *I as used by P
431
Chris Lattnerc8e66542002-04-27 06:56:12 +0000432 // Erase all analyses not in the preserved set...
433 if (!AnUsage.preservesAll()) {
434 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
435 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(),
436 E = CurrentAnalyses.end(); I != E; )
437 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) !=
438 PreservedSet.end())
439 ++I; // This analysis is preserved, leave it in the available set...
440 else {
441#if MAP_DOESNT_HAVE_BROKEN_ERASE_MEMBER
442 I = CurrentAnalyses.erase(I); // Analysis not preserved!
443#else
444 CurrentAnalyses.erase(I);// GCC 2.95.3 STL doesn't have correct erase!
445 I = CurrentAnalyses.begin();
446#endif
447 }
448 }
Chris Lattner67d25652002-01-30 23:20:39 +0000449
Chris Lattner73503172002-07-29 21:03:38 +0000450 // Add this pass to the currently available set...
451 if (const PassInfo *PI = P->getPassInfo())
452 CurrentAnalyses[PI] = P;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000453
454 // For now assume that our results are never used...
455 LastUseOf[P] = P;
Chris Lattner67d25652002-01-30 23:20:39 +0000456 }
457
Chris Lattnerc8e66542002-04-27 06:56:12 +0000458 // For FunctionPass subclasses, we must be sure to batch the FunctionPass's
459 // together in a BatcherClass object so that all of the analyses are run
460 // together a function at a time.
Chris Lattner67d25652002-01-30 23:20:39 +0000461 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000462 void addPass(SubPassClass *MP, AnalysisUsage &AnUsage) {
Chris Lattner67d25652002-01-30 23:20:39 +0000463 if (Batcher == 0) // If we don't have a batcher yet, make one now.
464 Batcher = new BatcherClass(this);
Chris Lattner73503172002-07-29 21:03:38 +0000465 // The Batcher will queue the passes up
Chris Lattnerc8e66542002-04-27 06:56:12 +0000466 MP->addToPassManager(Batcher, AnUsage);
Chris Lattner67d25652002-01-30 23:20:39 +0000467 }
468
469 // closeBatcher - Terminate the batcher that is being worked on.
470 void closeBatcher() {
471 if (Batcher) {
472 Passes.push_back(Batcher);
473 Batcher = 0;
474 }
475 }
476};
477
478
479
480//===----------------------------------------------------------------------===//
481// PassManagerTraits<BasicBlock> Specialization
482//
483// This pass manager is used to group together all of the BasicBlockPass's
484// into a single unit.
485//
486template<> struct PassManagerTraits<BasicBlock> : public BasicBlockPass {
487 // PassClass - The type of passes tracked by this PassManager
488 typedef BasicBlockPass PassClass;
489
490 // SubPassClass - The types of classes that should be collated together
491 // This is impossible to match, so BasicBlock instantiations of PassManagerT
492 // do not collate.
493 //
494 typedef PassManagerT<Module> SubPassClass;
495
496 // BatcherClass - The type to use for collation of subtypes... This class is
497 // never instantiated for the PassManager<BasicBlock>, but it must be an
498 // instance of PassClass to typecheck.
499 //
500 typedef PassClass BatcherClass;
501
502 // ParentClass - The type of the parent PassManager...
Chris Lattner4e8c4872002-03-23 22:51:58 +0000503 typedef PassManagerT<Function> ParentClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000504
Chris Lattner2eaac392002-01-31 00:40:44 +0000505 // PMType - The type of the passmanager that subclasses this class
506 typedef PassManagerT<BasicBlock> PMType;
507
Chris Lattner67d25652002-01-30 23:20:39 +0000508 // runPass - Specify how the pass should be run on the UnitType
509 static bool runPass(PassClass *P, BasicBlock *M) {
510 // todo, init and finalize
Chris Lattner113f4f42002-06-25 16:13:24 +0000511 return P->runOnBasicBlock(*M);
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.
520 const char *getPMName() const { return "BasicBlock"; }
Chris Lattner37104aa2002-04-29 14:57:45 +0000521 virtual const char *getPassName() const { return "BasicBlock Pass Manager"; }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000522
Chris Lattner2eaac392002-01-31 00:40:44 +0000523 // Implement the BasicBlockPass interface...
Chris Lattner113f4f42002-06-25 16:13:24 +0000524 virtual bool doInitialization(Module &M);
525 virtual bool runOnBasicBlock(BasicBlock &BB);
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//===----------------------------------------------------------------------===//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000536// PassManagerTraits<Function> Specialization
Chris Lattner67d25652002-01-30 23:20:39 +0000537//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000538// This pass manager is used to group together all of the FunctionPass's
Chris Lattner67d25652002-01-30 23:20:39 +0000539// into a single unit.
540//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000541template<> struct PassManagerTraits<Function> : public FunctionPass {
Chris Lattner67d25652002-01-30 23:20:39 +0000542 // PassClass - The type of passes tracked by this PassManager
Chris Lattnerc8e66542002-04-27 06:56:12 +0000543 typedef FunctionPass PassClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000544
545 // SubPassClass - The types of classes that should be collated together
546 typedef BasicBlockPass SubPassClass;
547
548 // BatcherClass - The type to use for collation of subtypes...
549 typedef PassManagerT<BasicBlock> BatcherClass;
550
551 // ParentClass - The type of the parent PassManager...
552 typedef PassManagerT<Module> ParentClass;
553
554 // PMType - The type of the passmanager that subclasses this class
Chris Lattner4e8c4872002-03-23 22:51:58 +0000555 typedef PassManagerT<Function> PMType;
Chris Lattner67d25652002-01-30 23:20:39 +0000556
557 // runPass - Specify how the pass should be run on the UnitType
Chris Lattnerc8e66542002-04-27 06:56:12 +0000558 static bool runPass(PassClass *P, Function *F) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000559 return P->runOnFunction(*F);
Chris Lattner67d25652002-01-30 23:20:39 +0000560 }
561
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000562 // Dummy implementation of PassStarted/PassEnded
563 static void PassStarted(Pass *P) {}
564 static void PassEnded(Pass *P) {}
565
Chris Lattnerac3e0602002-01-31 18:32:27 +0000566 // getPMName() - Return the name of the unit the PassManager operates on for
567 // debugging.
Chris Lattner4e8c4872002-03-23 22:51:58 +0000568 const char *getPMName() const { return "Function"; }
Chris Lattner37104aa2002-04-29 14:57:45 +0000569 virtual const char *getPassName() const { return "Function Pass Manager"; }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000570
Chris Lattnerc8e66542002-04-27 06:56:12 +0000571 // Implement the FunctionPass interface...
Chris Lattner113f4f42002-06-25 16:13:24 +0000572 virtual bool doInitialization(Module &M);
573 virtual bool runOnFunction(Function &F);
574 virtual bool doFinalization(Module &M);
Chris Lattnercb429062002-04-30 18:50:17 +0000575
576 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
577 AU.setPreservesAll();
578 }
Chris Lattner67d25652002-01-30 23:20:39 +0000579};
580
581
582
583//===----------------------------------------------------------------------===//
584// PassManagerTraits<Module> Specialization
585//
586// This is the top level PassManager implementation that holds generic passes.
587//
588template<> struct PassManagerTraits<Module> : public Pass {
589 // PassClass - The type of passes tracked by this PassManager
590 typedef Pass PassClass;
591
592 // SubPassClass - The types of classes that should be collated together
Chris Lattnerc8e66542002-04-27 06:56:12 +0000593 typedef FunctionPass SubPassClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000594
595 // BatcherClass - The type to use for collation of subtypes...
Chris Lattner4e8c4872002-03-23 22:51:58 +0000596 typedef PassManagerT<Function> BatcherClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000597
598 // ParentClass - The type of the parent PassManager...
Chris Lattnerac3e0602002-01-31 18:32:27 +0000599 typedef AnalysisResolver ParentClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000600
601 // runPass - Specify how the pass should be run on the UnitType
Chris Lattner113f4f42002-06-25 16:13:24 +0000602 static bool runPass(PassClass *P, Module *M) { return P->run(*M); }
Chris Lattner67d25652002-01-30 23:20:39 +0000603
Chris Lattnerac3e0602002-01-31 18:32:27 +0000604 // getPMName() - Return the name of the unit the PassManager operates on for
605 // debugging.
606 const char *getPMName() const { return "Module"; }
Chris Lattner37104aa2002-04-29 14:57:45 +0000607 virtual const char *getPassName() const { return "Module Pass Manager"; }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000608
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000609 // TimingInformation - This data member maintains timing information for each
610 // of the passes that is executed.
611 //
612 TimingInfo *TimeInfo;
613
614 // PassStarted/Ended - This callback is notified any time a pass is started
615 // or stops. This is used to collect timing information about the different
616 // passes being executed.
617 //
618 void PassStarted(Pass *P) {
619 if (TimeInfo) TimeInfo->passStarted(P);
Chris Lattner67d25652002-01-30 23:20:39 +0000620 }
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000621 void PassEnded(Pass *P) {
622 if (TimeInfo) TimeInfo->passEnded(P);
623 }
624
625 // run - Implement the PassManager interface...
Chris Lattner113f4f42002-06-25 16:13:24 +0000626 bool run(Module &M) {
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000627 TimeInfo = TimingInfo::create();
Chris Lattner113f4f42002-06-25 16:13:24 +0000628 bool Result = ((PassManagerT<Module>*)this)->runOnUnit(&M);
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000629 if (TimeInfo) {
630 delete TimeInfo;
631 TimeInfo = 0;
632 }
633 return Result;
634 }
635
636 // PassManagerTraits constructor - Create a timing info object if the user
637 // specified timing info should be collected on the command line.
638 //
639 PassManagerTraits() : TimeInfo(0) {}
Chris Lattner67d25652002-01-30 23:20:39 +0000640};
641
642
643
644//===----------------------------------------------------------------------===//
645// PassManagerTraits Method Implementations
646//
647
648// PassManagerTraits<BasicBlock> Implementations
649//
Chris Lattner113f4f42002-06-25 16:13:24 +0000650inline bool PassManagerTraits<BasicBlock>::doInitialization(Module &M) {
Chris Lattner2eaac392002-01-31 00:40:44 +0000651 bool Changed = false;
652 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
653 ((PMType*)this)->Passes[i]->doInitialization(M);
654 return Changed;
655}
656
Chris Lattner113f4f42002-06-25 16:13:24 +0000657inline bool PassManagerTraits<BasicBlock>::runOnBasicBlock(BasicBlock &BB) {
658 return ((PMType*)this)->runOnUnit(&BB);
Chris Lattner2eaac392002-01-31 00:40:44 +0000659}
660
Chris Lattner113f4f42002-06-25 16:13:24 +0000661inline bool PassManagerTraits<BasicBlock>::doFinalization(Module &M) {
Chris Lattner2eaac392002-01-31 00:40:44 +0000662 bool Changed = false;
663 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
664 ((PMType*)this)->Passes[i]->doFinalization(M);
665 return Changed;
Chris Lattner67d25652002-01-30 23:20:39 +0000666}
667
668
Chris Lattner4e8c4872002-03-23 22:51:58 +0000669// PassManagerTraits<Function> Implementations
Chris Lattner67d25652002-01-30 23:20:39 +0000670//
Chris Lattner113f4f42002-06-25 16:13:24 +0000671inline bool PassManagerTraits<Function>::doInitialization(Module &M) {
Chris Lattner67d25652002-01-30 23:20:39 +0000672 bool Changed = false;
673 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
674 ((PMType*)this)->Passes[i]->doInitialization(M);
675 return Changed;
676}
677
Chris Lattner113f4f42002-06-25 16:13:24 +0000678inline bool PassManagerTraits<Function>::runOnFunction(Function &F) {
679 return ((PMType*)this)->runOnUnit(&F);
Chris Lattner67d25652002-01-30 23:20:39 +0000680}
681
Chris Lattner113f4f42002-06-25 16:13:24 +0000682inline bool PassManagerTraits<Function>::doFinalization(Module &M) {
Chris Lattner67d25652002-01-30 23:20:39 +0000683 bool Changed = false;
684 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
685 ((PMType*)this)->Passes[i]->doFinalization(M);
686 return Changed;
687}
688
689#endif