blob: afff535391b952ffd17a08ca9c037833896eab13 [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 Lattnered6504b2002-09-08 19:00:07 +000020#include "Support/LeakDetector.h"
Chris Lattnerc8e66542002-04-27 06:56:12 +000021#include <algorithm>
Chris Lattner395c27a2002-07-31 18:04:17 +000022#include <iostream>
Chris Lattnera454b5b2002-04-28 05:14:06 +000023class Annotable;
Chris Lattner67d25652002-01-30 23:20:39 +000024
Chris Lattner67d25652002-01-30 23:20:39 +000025//===----------------------------------------------------------------------===//
Chris Lattner198cf422002-07-30 16:27:02 +000026// Pass debugging information. Often it is useful to find out what pass is
27// running when a crash occurs in a utility. When this library is compiled with
28// debugging on, a command line option (--debug-pass) is enabled that causes the
29// pass name to be printed before it executes.
30//
31
32// Different debug levels that can be enabled...
33enum PassDebugLevel {
Chris Lattner1e4867f2002-07-30 19:51:02 +000034 None, Arguments, Structure, Executions, Details
Chris Lattner198cf422002-07-30 16:27:02 +000035};
36
37static cl::opt<enum PassDebugLevel>
38PassDebugging("debug-pass", cl::Hidden,
39 cl::desc("Print PassManager debugging information"),
40 cl::values(
41 clEnumVal(None , "disable debug output"),
Chris Lattner1e4867f2002-07-30 19:51:02 +000042 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
Chris Lattner198cf422002-07-30 16:27:02 +000043 clEnumVal(Structure , "print pass structure before run()"),
44 clEnumVal(Executions, "print pass name before it is executed"),
45 clEnumVal(Details , "print pass details when it is executed"),
46 0));
47
48//===----------------------------------------------------------------------===//
Chris Lattner57d2ba32002-04-04 19:35:24 +000049// PMDebug class - a set of debugging functions, that are not to be
50// instantiated by the template.
Chris Lattner67d25652002-01-30 23:20:39 +000051//
52struct PMDebug {
Chris Lattner1e4867f2002-07-30 19:51:02 +000053 static void PerformPassStartupStuff(Pass *P) {
54 // If debugging is enabled, print out argument information...
55 if (PassDebugging >= Arguments) {
56 std::cerr << "Pass Arguments: ";
57 PrintArgumentInformation(P);
58 std::cerr << "\n";
59
60 // Print the pass execution structure
61 if (PassDebugging >= Structure)
62 P->dumpPassStructure();
63 }
Chris Lattner198cf422002-07-30 16:27:02 +000064 }
Chris Lattner1e4867f2002-07-30 19:51:02 +000065
66 static void PrintArgumentInformation(const Pass *P);
Chris Lattnera454b5b2002-04-28 05:14:06 +000067 static void PrintPassInformation(unsigned,const char*,Pass *, Annotable *);
Chris Lattnerac3e0602002-01-31 18:32:27 +000068 static void PrintAnalysisSetInfo(unsigned,const char*,Pass *P,
Chris Lattnerc8e66542002-04-27 06:56:12 +000069 const std::vector<AnalysisID> &);
Chris Lattner67d25652002-01-30 23:20:39 +000070};
71
72
Chris Lattnere2eb99e2002-04-29 04:04:29 +000073//===----------------------------------------------------------------------===//
74// TimingInfo Class - This class is used to calculate information about the
75// amount of time each pass takes to execute. This only happens when
76// -time-passes is enabled on the command line.
77//
Chris Lattner6a33d6f2002-08-01 19:33:09 +000078struct TimeRecord { // TimeRecord - Data we collect and print for each pass
79 double Elapsed; // Wall clock time elapsed in seconds
80 double UserTime; // User time elapsed
81 double SystemTime; // System time elapsed
82 unsigned long MaxRSS; // Maximum resident set size (in bytes)
83 unsigned long RSSTemp; // Temp for calculating maxrss
84
85 TimeRecord() : Elapsed(0), UserTime(0), SystemTime(0), MaxRSS(0) {}
86 void passStart(const TimeRecord &T);
87 void passEnd(const TimeRecord &T);
88 void sum(const TimeRecord &TR);
Chris Lattnere821d782002-08-20 18:47:53 +000089 bool operator<(const TimeRecord &TR) const;
Chris Lattner6a33d6f2002-08-01 19:33:09 +000090
91 void print(const char *PassName, const TimeRecord &TotalTime) const;
92};
93
Chris Lattnere2eb99e2002-04-29 04:04:29 +000094class TimingInfo {
Chris Lattner6a33d6f2002-08-01 19:33:09 +000095 std::map<Pass*, TimeRecord> TimingData;
Chris Lattnere2eb99e2002-04-29 04:04:29 +000096 TimingInfo() {} // Private ctor, must use create member
97public:
98 // Create method. If Timing is enabled, this creates and returns a new timing
99 // object, otherwise it returns null.
100 //
101 static TimingInfo *create();
102
103 // TimingDtor - Print out information about timing information
104 ~TimingInfo();
105
106 void passStarted(Pass *P);
107 void passEnded(Pass *P);
108};
109
Chris Lattner6e041bd2002-08-21 22:17:09 +0000110//===----------------------------------------------------------------------===//
Chris Lattner67d25652002-01-30 23:20:39 +0000111// Declare the PassManagerTraits which will be specialized...
112//
113template<class UnitType> class PassManagerTraits; // Do not define.
114
115
116//===----------------------------------------------------------------------===//
117// PassManagerT - Container object for passes. The PassManagerT destructor
118// deletes all passes contained inside of the PassManagerT, so you shouldn't
119// delete passes manually, and all passes should be dynamically allocated.
120//
121template<typename UnitType>
122class PassManagerT : public PassManagerTraits<UnitType>,public AnalysisResolver{
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000123 typedef PassManagerTraits<UnitType> Traits;
124 typedef typename Traits::PassClass PassClass;
125 typedef typename Traits::SubPassClass SubPassClass;
126 typedef typename Traits::BatcherClass BatcherClass;
127 typedef typename Traits::ParentClass ParentClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000128
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000129 friend typename Traits::PassClass;
130 friend typename Traits::SubPassClass;
131 friend class Traits;
Chris Lattner67d25652002-01-30 23:20:39 +0000132
Chris Lattner73503172002-07-29 21:03:38 +0000133 std::vector<PassClass*> Passes; // List of passes to run
Chris Lattner67d25652002-01-30 23:20:39 +0000134
135 // The parent of this pass manager...
Chris Lattnerac3e0602002-01-31 18:32:27 +0000136 ParentClass * const Parent;
Chris Lattner67d25652002-01-30 23:20:39 +0000137
138 // The current batcher if one is in use, or null
139 BatcherClass *Batcher;
140
141 // CurrentAnalyses - As the passes are being run, this map contains the
142 // analyses that are available to the current pass for use. This is accessed
143 // through the getAnalysis() function in this class and in Pass.
144 //
145 std::map<AnalysisID, Pass*> CurrentAnalyses;
146
Chris Lattnerac3e0602002-01-31 18:32:27 +0000147 // LastUseOf - This map keeps track of the last usage in our pipeline of a
148 // particular pass. When executing passes, the memory for .first is free'd
149 // after .second is run.
150 //
151 std::map<Pass*, Pass*> LastUseOf;
152
Chris Lattner67d25652002-01-30 23:20:39 +0000153public:
154 PassManagerT(ParentClass *Par = 0) : Parent(Par), Batcher(0) {}
155 ~PassManagerT() {
156 // Delete all of the contained passes...
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000157 for (typename std::vector<PassClass*>::iterator
158 I = Passes.begin(), E = Passes.end(); I != E; ++I)
Chris Lattner67d25652002-01-30 23:20:39 +0000159 delete *I;
160 }
161
162 // run - Run all of the queued passes on the specified module in an optimal
163 // way.
164 virtual bool runOnUnit(UnitType *M) {
165 bool MadeChanges = false;
166 closeBatcher();
167 CurrentAnalyses.clear();
168
Chris Lattnerac3e0602002-01-31 18:32:27 +0000169 // LastUserOf - This contains the inverted LastUseOfMap...
170 std::map<Pass *, std::vector<Pass*> > LastUserOf;
171 for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(),
172 E = LastUseOf.end(); I != E; ++I)
173 LastUserOf[I->second].push_back(I->first);
174
175
Chris Lattner67d25652002-01-30 23:20:39 +0000176 // Output debug information...
Chris Lattner1e4867f2002-07-30 19:51:02 +0000177 if (Parent == 0) PMDebug::PerformPassStartupStuff(this);
Chris Lattner67d25652002-01-30 23:20:39 +0000178
179 // Run all of the passes
180 for (unsigned i = 0, e = Passes.size(); i < e; ++i) {
181 PassClass *P = Passes[i];
182
Chris Lattnera454b5b2002-04-28 05:14:06 +0000183 PMDebug::PrintPassInformation(getDepth(), "Executing Pass", P,
184 (Annotable*)M);
Chris Lattner67d25652002-01-30 23:20:39 +0000185
186 // Get information about what analyses the pass uses...
Chris Lattnerc8e66542002-04-27 06:56:12 +0000187 AnalysisUsage AnUsage;
188 P->getAnalysisUsage(AnUsage);
189 PMDebug::PrintAnalysisSetInfo(getDepth(), "Required", P,
190 AnUsage.getRequiredSet());
Chris Lattner67d25652002-01-30 23:20:39 +0000191
Chris Lattner216edd42002-08-30 20:25:01 +0000192 // All Required analyses should be available to the pass as it runs! Here
193 // we fill in the AnalysisImpls member of the pass so that it can
194 // successfully use the getAnalysis() method to retrieve the
195 // implementations it needs.
196 //
197 P->AnalysisImpls.clear();
198 P->AnalysisImpls.reserve(AnUsage.getRequiredSet().size());
199 for (std::vector<const PassInfo *>::const_iterator
Chris Lattnerc8e66542002-04-27 06:56:12 +0000200 I = AnUsage.getRequiredSet().begin(),
201 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Chris Lattner216edd42002-08-30 20:25:01 +0000202 Pass *Impl = getAnalysisOrNullUp(*I);
203 if (Impl == 0) {
204 std::cerr << "Analysis '" << (*I)->getPassName()
205 << "' used but not available!";
206 assert(0 && "Analysis used but not available!");
207 } else if (PassDebugging == Details) {
208 if ((*I)->getPassName() != std::string(Impl->getPassName()))
209 std::cerr << " Interface '" << (*I)->getPassName()
210 << "' implemented by '" << Impl->getPassName() << "'\n";
211 }
212 P->AnalysisImpls.push_back(std::make_pair(*I, Impl));
Chris Lattner67d25652002-01-30 23:20:39 +0000213 }
Chris Lattner67d25652002-01-30 23:20:39 +0000214
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 Lattnered6504b2002-09-08 19:00:07 +0000221 // Check for memory leaks by the pass...
222 LeakDetector::checkForGarbage(std::string("after running pass '") +
223 P->getPassName() + "'");
224
Chris Lattnerf2f31bd2002-02-01 04:53:36 +0000225 if (Changed)
226 PMDebug::PrintPassInformation(getDepth()+1, "Made Modification", P,
Chris Lattnera454b5b2002-04-28 05:14:06 +0000227 (Annotable*)M);
Chris Lattnerc8e66542002-04-27 06:56:12 +0000228 PMDebug::PrintAnalysisSetInfo(getDepth(), "Preserved", P,
229 AnUsage.getPreservedSet());
Chris Lattner67d25652002-01-30 23:20:39 +0000230
Chris Lattnerc8e66542002-04-27 06:56:12 +0000231
232 // Erase all analyses not in the preserved set...
233 if (!AnUsage.preservesAll()) {
234 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
235 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(),
236 E = CurrentAnalyses.end(); I != E; )
237 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) !=
238 PreservedSet.end())
239 ++I; // This analysis is preserved, leave it in the available set...
240 else {
241#if MAP_DOESNT_HAVE_BROKEN_ERASE_MEMBER
242 I = CurrentAnalyses.erase(I); // Analysis not preserved!
243#else
244 // GCC 2.95.3 STL doesn't have correct erase member!
245 CurrentAnalyses.erase(I);
246 I = CurrentAnalyses.begin();
247#endif
248 }
249 }
250
Chris Lattner73503172002-07-29 21:03:38 +0000251 // Add the current pass to the set of passes that have been run, and are
252 // thus available to users.
253 //
Chris Lattner216edd42002-08-30 20:25:01 +0000254 if (const PassInfo *PI = P->getPassInfo()) {
Chris Lattner73503172002-07-29 21:03:38 +0000255 CurrentAnalyses[PI] = P;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000256
Chris Lattner216edd42002-08-30 20:25:01 +0000257 // This pass is the current implementation of all of the interfaces it
258 // implements as well.
259 //
260 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
261 for (unsigned i = 0, e = II.size(); i != e; ++i)
262 CurrentAnalyses[II[i]] = P;
263 }
264
Chris Lattnerac3e0602002-01-31 18:32:27 +0000265 // Free memory for any passes that we are the last use of...
266 std::vector<Pass*> &DeadPass = LastUserOf[P];
267 for (std::vector<Pass*>::iterator I = DeadPass.begin(),E = DeadPass.end();
268 I != E; ++I) {
269 PMDebug::PrintPassInformation(getDepth()+1, "Freeing Pass", *I,
Chris Lattnera454b5b2002-04-28 05:14:06 +0000270 (Annotable*)M);
Chris Lattnerac3e0602002-01-31 18:32:27 +0000271 (*I)->releaseMemory();
272 }
Chris Lattner67d25652002-01-30 23:20:39 +0000273 }
274 return MadeChanges;
275 }
276
Chris Lattnerac3e0602002-01-31 18:32:27 +0000277 // dumpPassStructure - Implement the -debug-passes=PassStructure option
278 virtual void dumpPassStructure(unsigned Offset = 0) {
279 std::cerr << std::string(Offset*2, ' ') << Traits::getPMName()
280 << " Pass Manager\n";
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000281 for (typename std::vector<PassClass*>::iterator
282 I = Passes.begin(), E = Passes.end(); I != E; ++I) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000283 PassClass *P = *I;
284 P->dumpPassStructure(Offset+1);
285
286 // Loop through and see which classes are destroyed after this one...
287 for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(),
288 E = LastUseOf.end(); I != E; ++I) {
289 if (P == I->second) {
Chris Lattner73503172002-07-29 21:03:38 +0000290 std::cerr << "--" << std::string(Offset*2, ' ');
Chris Lattnerac3e0602002-01-31 18:32:27 +0000291 I->first->dumpPassStructure(0);
292 }
293 }
294 }
295 }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000296
Chris Lattner6e041bd2002-08-21 22:17:09 +0000297 Pass *getAnalysisOrNullDown(const PassInfo *ID) const {
Chris Lattner216edd42002-08-30 20:25:01 +0000298 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000299
300 if (I != CurrentAnalyses.end())
301 return I->second; // Found it.
302
303 if (Batcher)
304 return ((AnalysisResolver*)Batcher)->getAnalysisOrNullDown(ID);
305 return 0;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000306 }
307
Chris Lattner6e041bd2002-08-21 22:17:09 +0000308 Pass *getAnalysisOrNullUp(const PassInfo *ID) const {
Chris Lattner216edd42002-08-30 20:25:01 +0000309 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000310 if (I != CurrentAnalyses.end())
311 return I->second; // Found it.
312
313 if (Parent) // Try scanning...
314 return Parent->getAnalysisOrNullUp(ID);
315 return 0;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000316 }
317
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000318 // {start/end}Pass - Called when a pass is started, it just propogates
319 // information up to the top level PassManagerT object to tell it that a pass
320 // has started or ended. This is used to gather timing information about
321 // passes.
322 //
323 void startPass(Pass *P) {
324 if (Parent) Parent->startPass(P);
325 else PassStarted(P);
326 }
327 void endPass(Pass *P) {
328 if (Parent) Parent->endPass(P);
329 else PassEnded(P);
330 }
331
Chris Lattnerac3e0602002-01-31 18:32:27 +0000332 // markPassUsed - Inform higher level pass managers (and ourselves)
333 // that these analyses are being used by this pass. This is used to
334 // make sure that analyses are not free'd before we have to use
335 // them...
336 //
Chris Lattner6e041bd2002-08-21 22:17:09 +0000337 void markPassUsed(const PassInfo *P, Pass *User) {
Chris Lattner216edd42002-08-30 20:25:01 +0000338 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(P);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000339
Chris Lattnerac3e0602002-01-31 18:32:27 +0000340 if (I != CurrentAnalyses.end()) {
341 LastUseOf[I->second] = User; // Local pass, extend the lifetime
342 } else {
343 // Pass not in current available set, must be a higher level pass
344 // available to us, propogate to parent pass manager... We tell the
345 // parent that we (the passmanager) are using the analysis so that it
346 // frees the analysis AFTER this pass manager runs.
347 //
Chris Lattner6e041bd2002-08-21 22:17:09 +0000348 assert(Parent != 0 && "Pass available but not found!");
Chris Lattnerac3e0602002-01-31 18:32:27 +0000349 Parent->markPassUsed(P, this);
350 }
351 }
352
353 // Return the number of parent PassManagers that exist
354 virtual unsigned getDepth() const {
355 if (Parent == 0) return 0;
356 return 1 + Parent->getDepth();
357 }
358
Chris Lattner1e4867f2002-07-30 19:51:02 +0000359 virtual unsigned getNumContainedPasses() const { return Passes.size(); }
360 virtual const Pass *getContainedPass(unsigned N) const {
361 assert(N < Passes.size() && "Pass number out of range!");
362 return Passes[N];
363 }
364
Chris Lattner6e041bd2002-08-21 22:17:09 +0000365 // add - Add a pass to the queue of passes to run. This gives ownership of
Chris Lattner67d25652002-01-30 23:20:39 +0000366 // the Pass to the PassManager. When the PassManager is destroyed, the pass
Chris Lattnerac3e0602002-01-31 18:32:27 +0000367 // will be destroyed as well, so there is no need to delete the pass. This
368 // implies that all passes MUST be new'd.
Chris Lattner67d25652002-01-30 23:20:39 +0000369 //
370 void add(PassClass *P) {
371 // Get information about what analyses the pass uses...
Chris Lattnerc8e66542002-04-27 06:56:12 +0000372 AnalysisUsage AnUsage;
373 P->getAnalysisUsage(AnUsage);
374 const std::vector<AnalysisID> &Required = AnUsage.getRequiredSet();
Chris Lattner67d25652002-01-30 23:20:39 +0000375
376 // Loop over all of the analyses used by this pass,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000377 for (std::vector<AnalysisID>::const_iterator I = Required.begin(),
378 E = Required.end(); I != E; ++I) {
Chris Lattner67d25652002-01-30 23:20:39 +0000379 if (getAnalysisOrNullDown(*I) == 0)
Chris Lattner26750072002-07-27 01:12:17 +0000380 add((PassClass*)(*I)->createPass());
Chris Lattner67d25652002-01-30 23:20:39 +0000381 }
382
383 // Tell the pass to add itself to this PassManager... the way it does so
384 // depends on the class of the pass, and is critical to laying out passes in
385 // an optimal order..
386 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000387 P->addToPassManager(this, AnUsage);
Chris Lattner67d25652002-01-30 23:20:39 +0000388 }
389
390private:
391
392 // addPass - These functions are used to implement the subclass specific
393 // behaviors present in PassManager. Basically the add(Pass*) method ends up
394 // reflecting its behavior into a Pass::addToPassManager call. Subclasses of
395 // Pass override it specifically so that they can reflect the type
396 // information inherent in "this" back to the PassManager.
397 //
398 // For generic Pass subclasses (which are interprocedural passes), we simply
399 // add the pass to the end of the pass list and terminate any accumulation of
Chris Lattnerc8e66542002-04-27 06:56:12 +0000400 // FunctionPass's that are present.
Chris Lattner67d25652002-01-30 23:20:39 +0000401 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000402 void addPass(PassClass *P, AnalysisUsage &AnUsage) {
403 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
Chris Lattnerc8e66542002-04-27 06:56:12 +0000404
Chris Lattner73503172002-07-29 21:03:38 +0000405 // FIXME: If this pass being added isn't killed by any of the passes in the
406 // batcher class then we can reorder to pass to execute before the batcher
407 // does, which will potentially allow us to batch more passes!
Chris Lattner67d25652002-01-30 23:20:39 +0000408 //
Chris Lattner73503172002-07-29 21:03:38 +0000409 //const std::vector<AnalysisID> &ProvidedSet = AnUsage.getProvidedSet();
410 if (Batcher /*&& ProvidedSet.empty()*/)
Chris Lattner67d25652002-01-30 23:20:39 +0000411 closeBatcher(); // This pass cannot be batched!
412
413 // Set the Resolver instance variable in the Pass so that it knows where to
414 // find this object...
415 //
416 setAnalysisResolver(P, this);
417 Passes.push_back(P);
418
Chris Lattnerac3e0602002-01-31 18:32:27 +0000419 // Inform higher level pass managers (and ourselves) that these analyses are
420 // being used by this pass. This is used to make sure that analyses are not
421 // free'd before we have to use them...
422 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000423 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
424 E = RequiredSet.end(); I != E; ++I)
Chris Lattnerac3e0602002-01-31 18:32:27 +0000425 markPassUsed(*I, P); // Mark *I as used by P
426
Chris Lattnerc8e66542002-04-27 06:56:12 +0000427 // Erase all analyses not in the preserved set...
428 if (!AnUsage.preservesAll()) {
429 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
430 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(),
431 E = CurrentAnalyses.end(); I != E; )
432 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) !=
433 PreservedSet.end())
434 ++I; // This analysis is preserved, leave it in the available set...
435 else {
436#if MAP_DOESNT_HAVE_BROKEN_ERASE_MEMBER
437 I = CurrentAnalyses.erase(I); // Analysis not preserved!
438#else
439 CurrentAnalyses.erase(I);// GCC 2.95.3 STL doesn't have correct erase!
440 I = CurrentAnalyses.begin();
441#endif
442 }
443 }
Chris Lattner67d25652002-01-30 23:20:39 +0000444
Chris Lattner73503172002-07-29 21:03:38 +0000445 // Add this pass to the currently available set...
Chris Lattner216edd42002-08-30 20:25:01 +0000446 if (const PassInfo *PI = P->getPassInfo()) {
Chris Lattner73503172002-07-29 21:03:38 +0000447 CurrentAnalyses[PI] = P;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000448
Chris Lattner216edd42002-08-30 20:25:01 +0000449 // This pass is the current implementation of all of the interfaces it
450 // implements as well.
451 //
452 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
453 for (unsigned i = 0, e = II.size(); i != e; ++i)
454 CurrentAnalyses[II[i]] = P;
455 }
456
Chris Lattnerac3e0602002-01-31 18:32:27 +0000457 // For now assume that our results are never used...
458 LastUseOf[P] = P;
Chris Lattner67d25652002-01-30 23:20:39 +0000459 }
460
Chris Lattnerc8e66542002-04-27 06:56:12 +0000461 // For FunctionPass subclasses, we must be sure to batch the FunctionPass's
462 // together in a BatcherClass object so that all of the analyses are run
463 // together a function at a time.
Chris Lattner67d25652002-01-30 23:20:39 +0000464 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000465 void addPass(SubPassClass *MP, AnalysisUsage &AnUsage) {
Chris Lattner67d25652002-01-30 23:20:39 +0000466 if (Batcher == 0) // If we don't have a batcher yet, make one now.
467 Batcher = new BatcherClass(this);
Chris Lattner73503172002-07-29 21:03:38 +0000468 // The Batcher will queue the passes up
Chris Lattnerc8e66542002-04-27 06:56:12 +0000469 MP->addToPassManager(Batcher, AnUsage);
Chris Lattner67d25652002-01-30 23:20:39 +0000470 }
471
472 // closeBatcher - Terminate the batcher that is being worked on.
473 void closeBatcher() {
474 if (Batcher) {
475 Passes.push_back(Batcher);
476 Batcher = 0;
477 }
478 }
479};
480
481
482
483//===----------------------------------------------------------------------===//
484// PassManagerTraits<BasicBlock> Specialization
485//
486// This pass manager is used to group together all of the BasicBlockPass's
487// into a single unit.
488//
489template<> struct PassManagerTraits<BasicBlock> : public BasicBlockPass {
490 // PassClass - The type of passes tracked by this PassManager
491 typedef BasicBlockPass PassClass;
492
493 // SubPassClass - The types of classes that should be collated together
494 // This is impossible to match, so BasicBlock instantiations of PassManagerT
495 // do not collate.
496 //
497 typedef PassManagerT<Module> SubPassClass;
498
499 // BatcherClass - The type to use for collation of subtypes... This class is
500 // never instantiated for the PassManager<BasicBlock>, but it must be an
501 // instance of PassClass to typecheck.
502 //
503 typedef PassClass BatcherClass;
504
505 // ParentClass - The type of the parent PassManager...
Chris Lattner4e8c4872002-03-23 22:51:58 +0000506 typedef PassManagerT<Function> ParentClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000507
Chris Lattner2eaac392002-01-31 00:40:44 +0000508 // PMType - The type of the passmanager that subclasses this class
509 typedef PassManagerT<BasicBlock> PMType;
510
Chris Lattner67d25652002-01-30 23:20:39 +0000511 // runPass - Specify how the pass should be run on the UnitType
512 static bool runPass(PassClass *P, BasicBlock *M) {
513 // todo, init and finalize
Chris Lattner113f4f42002-06-25 16:13:24 +0000514 return P->runOnBasicBlock(*M);
Chris Lattner67d25652002-01-30 23:20:39 +0000515 }
516
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000517 // Dummy implementation of PassStarted/PassEnded
518 static void PassStarted(Pass *P) {}
519 static void PassEnded(Pass *P) {}
520
Chris Lattnerac3e0602002-01-31 18:32:27 +0000521 // getPMName() - Return the name of the unit the PassManager operates on for
522 // debugging.
523 const char *getPMName() const { return "BasicBlock"; }
Chris Lattner37104aa2002-04-29 14:57:45 +0000524 virtual const char *getPassName() const { return "BasicBlock Pass Manager"; }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000525
Chris Lattner2eaac392002-01-31 00:40:44 +0000526 // Implement the BasicBlockPass interface...
Chris Lattner113f4f42002-06-25 16:13:24 +0000527 virtual bool doInitialization(Module &M);
Chris Lattnerbae3c672002-09-12 17:06:40 +0000528 virtual bool doInitialization(Function &F);
Chris Lattner113f4f42002-06-25 16:13:24 +0000529 virtual bool runOnBasicBlock(BasicBlock &BB);
Chris Lattnerbae3c672002-09-12 17:06:40 +0000530 virtual bool doFinalization(Function &F);
Chris Lattner113f4f42002-06-25 16:13:24 +0000531 virtual bool doFinalization(Module &M);
Chris Lattnercb429062002-04-30 18:50:17 +0000532
533 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
534 AU.setPreservesAll();
535 }
Chris Lattner67d25652002-01-30 23:20:39 +0000536};
537
538
539
540//===----------------------------------------------------------------------===//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000541// PassManagerTraits<Function> Specialization
Chris Lattner67d25652002-01-30 23:20:39 +0000542//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000543// This pass manager is used to group together all of the FunctionPass's
Chris Lattner67d25652002-01-30 23:20:39 +0000544// into a single unit.
545//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000546template<> struct PassManagerTraits<Function> : public FunctionPass {
Chris Lattner67d25652002-01-30 23:20:39 +0000547 // PassClass - The type of passes tracked by this PassManager
Chris Lattnerc8e66542002-04-27 06:56:12 +0000548 typedef FunctionPass PassClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000549
550 // SubPassClass - The types of classes that should be collated together
551 typedef BasicBlockPass SubPassClass;
552
553 // BatcherClass - The type to use for collation of subtypes...
554 typedef PassManagerT<BasicBlock> BatcherClass;
555
556 // ParentClass - The type of the parent PassManager...
557 typedef PassManagerT<Module> ParentClass;
558
559 // PMType - The type of the passmanager that subclasses this class
Chris Lattner4e8c4872002-03-23 22:51:58 +0000560 typedef PassManagerT<Function> PMType;
Chris Lattner67d25652002-01-30 23:20:39 +0000561
562 // runPass - Specify how the pass should be run on the UnitType
Chris Lattnerc8e66542002-04-27 06:56:12 +0000563 static bool runPass(PassClass *P, Function *F) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000564 return P->runOnFunction(*F);
Chris Lattner67d25652002-01-30 23:20:39 +0000565 }
566
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000567 // Dummy implementation of PassStarted/PassEnded
568 static void PassStarted(Pass *P) {}
569 static void PassEnded(Pass *P) {}
570
Chris Lattnerac3e0602002-01-31 18:32:27 +0000571 // getPMName() - Return the name of the unit the PassManager operates on for
572 // debugging.
Chris Lattner4e8c4872002-03-23 22:51:58 +0000573 const char *getPMName() const { return "Function"; }
Chris Lattner37104aa2002-04-29 14:57:45 +0000574 virtual const char *getPassName() const { return "Function Pass Manager"; }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000575
Chris Lattnerc8e66542002-04-27 06:56:12 +0000576 // Implement the FunctionPass interface...
Chris Lattner113f4f42002-06-25 16:13:24 +0000577 virtual bool doInitialization(Module &M);
578 virtual bool runOnFunction(Function &F);
579 virtual bool doFinalization(Module &M);
Chris Lattnercb429062002-04-30 18:50:17 +0000580
581 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
582 AU.setPreservesAll();
583 }
Chris Lattner67d25652002-01-30 23:20:39 +0000584};
585
586
587
588//===----------------------------------------------------------------------===//
589// PassManagerTraits<Module> Specialization
590//
591// This is the top level PassManager implementation that holds generic passes.
592//
593template<> struct PassManagerTraits<Module> : public Pass {
594 // PassClass - The type of passes tracked by this PassManager
595 typedef Pass PassClass;
596
597 // SubPassClass - The types of classes that should be collated together
Chris Lattnerc8e66542002-04-27 06:56:12 +0000598 typedef FunctionPass SubPassClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000599
600 // BatcherClass - The type to use for collation of subtypes...
Chris Lattner4e8c4872002-03-23 22:51:58 +0000601 typedef PassManagerT<Function> BatcherClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000602
603 // ParentClass - The type of the parent PassManager...
Chris Lattnerac3e0602002-01-31 18:32:27 +0000604 typedef AnalysisResolver ParentClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000605
606 // runPass - Specify how the pass should be run on the UnitType
Chris Lattner113f4f42002-06-25 16:13:24 +0000607 static bool runPass(PassClass *P, Module *M) { return P->run(*M); }
Chris Lattner67d25652002-01-30 23:20:39 +0000608
Chris Lattnerac3e0602002-01-31 18:32:27 +0000609 // getPMName() - Return the name of the unit the PassManager operates on for
610 // debugging.
611 const char *getPMName() const { return "Module"; }
Chris Lattner37104aa2002-04-29 14:57:45 +0000612 virtual const char *getPassName() const { return "Module Pass Manager"; }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000613
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000614 // TimingInformation - This data member maintains timing information for each
615 // of the passes that is executed.
616 //
617 TimingInfo *TimeInfo;
618
619 // PassStarted/Ended - This callback is notified any time a pass is started
620 // or stops. This is used to collect timing information about the different
621 // passes being executed.
622 //
623 void PassStarted(Pass *P) {
624 if (TimeInfo) TimeInfo->passStarted(P);
Chris Lattner67d25652002-01-30 23:20:39 +0000625 }
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000626 void PassEnded(Pass *P) {
627 if (TimeInfo) TimeInfo->passEnded(P);
628 }
629
630 // run - Implement the PassManager interface...
Chris Lattner113f4f42002-06-25 16:13:24 +0000631 bool run(Module &M) {
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000632 TimeInfo = TimingInfo::create();
Chris Lattner113f4f42002-06-25 16:13:24 +0000633 bool Result = ((PassManagerT<Module>*)this)->runOnUnit(&M);
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000634 if (TimeInfo) {
635 delete TimeInfo;
636 TimeInfo = 0;
637 }
638 return Result;
639 }
640
641 // PassManagerTraits constructor - Create a timing info object if the user
642 // specified timing info should be collected on the command line.
643 //
644 PassManagerTraits() : TimeInfo(0) {}
Chris Lattner67d25652002-01-30 23:20:39 +0000645};
646
647
648
649//===----------------------------------------------------------------------===//
650// PassManagerTraits Method Implementations
651//
652
653// PassManagerTraits<BasicBlock> Implementations
654//
Chris Lattner113f4f42002-06-25 16:13:24 +0000655inline bool PassManagerTraits<BasicBlock>::doInitialization(Module &M) {
Chris Lattner2eaac392002-01-31 00:40:44 +0000656 bool Changed = false;
657 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
658 ((PMType*)this)->Passes[i]->doInitialization(M);
659 return Changed;
660}
661
Chris Lattnerbae3c672002-09-12 17:06:40 +0000662inline bool PassManagerTraits<BasicBlock>::doInitialization(Function &F) {
663 bool Changed = false;
664 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
665 ((PMType*)this)->Passes[i]->doInitialization(F);
666 return Changed;
667}
668
Chris Lattner113f4f42002-06-25 16:13:24 +0000669inline bool PassManagerTraits<BasicBlock>::runOnBasicBlock(BasicBlock &BB) {
670 return ((PMType*)this)->runOnUnit(&BB);
Chris Lattner2eaac392002-01-31 00:40:44 +0000671}
672
Chris Lattnerbae3c672002-09-12 17:06:40 +0000673inline bool PassManagerTraits<BasicBlock>::doFinalization(Function &F) {
674 bool Changed = false;
675 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
676 ((PMType*)this)->Passes[i]->doFinalization(F);
677 return Changed;
678}
679
Chris Lattner113f4f42002-06-25 16:13:24 +0000680inline bool PassManagerTraits<BasicBlock>::doFinalization(Module &M) {
Chris Lattner2eaac392002-01-31 00:40:44 +0000681 bool Changed = false;
682 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
683 ((PMType*)this)->Passes[i]->doFinalization(M);
684 return Changed;
Chris Lattner67d25652002-01-30 23:20:39 +0000685}
686
687
Chris Lattner4e8c4872002-03-23 22:51:58 +0000688// PassManagerTraits<Function> Implementations
Chris Lattner67d25652002-01-30 23:20:39 +0000689//
Chris Lattner113f4f42002-06-25 16:13:24 +0000690inline bool PassManagerTraits<Function>::doInitialization(Module &M) {
Chris Lattner67d25652002-01-30 23:20:39 +0000691 bool Changed = false;
692 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
693 ((PMType*)this)->Passes[i]->doInitialization(M);
694 return Changed;
695}
696
Chris Lattner113f4f42002-06-25 16:13:24 +0000697inline bool PassManagerTraits<Function>::runOnFunction(Function &F) {
698 return ((PMType*)this)->runOnUnit(&F);
Chris Lattner67d25652002-01-30 23:20:39 +0000699}
700
Chris Lattner113f4f42002-06-25 16:13:24 +0000701inline bool PassManagerTraits<Function>::doFinalization(Module &M) {
Chris Lattner67d25652002-01-30 23:20:39 +0000702 bool Changed = false;
703 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
704 ((PMType*)this)->Passes[i]->doFinalization(M);
705 return Changed;
706}
707
708#endif