blob: e88693fbfff23cda8d266364da38fae4c7169e5f [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//===----------------------------------------------------------------------===//
Chris Lattner67d25652002-01-30 23:20:39 +0000110// Declare the PassManagerTraits which will be specialized...
111//
112template<class UnitType> class PassManagerTraits; // Do not define.
113
114
115//===----------------------------------------------------------------------===//
116// PassManagerT - Container object for passes. The PassManagerT destructor
117// deletes all passes contained inside of the PassManagerT, so you shouldn't
118// delete passes manually, and all passes should be dynamically allocated.
119//
120template<typename UnitType>
121class PassManagerT : public PassManagerTraits<UnitType>,public AnalysisResolver{
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000122 typedef PassManagerTraits<UnitType> Traits;
123 typedef typename Traits::PassClass PassClass;
124 typedef typename Traits::SubPassClass SubPassClass;
125 typedef typename Traits::BatcherClass BatcherClass;
126 typedef typename Traits::ParentClass ParentClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000127
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000128 friend typename Traits::PassClass;
129 friend typename Traits::SubPassClass;
130 friend class Traits;
Chris Lattner67d25652002-01-30 23:20:39 +0000131
Chris Lattner73503172002-07-29 21:03:38 +0000132 std::vector<PassClass*> Passes; // List of passes to run
Chris Lattner67d25652002-01-30 23:20:39 +0000133
134 // The parent of this pass manager...
Chris Lattnerac3e0602002-01-31 18:32:27 +0000135 ParentClass * const Parent;
Chris Lattner67d25652002-01-30 23:20:39 +0000136
137 // The current batcher if one is in use, or null
138 BatcherClass *Batcher;
139
140 // CurrentAnalyses - As the passes are being run, this map contains the
141 // analyses that are available to the current pass for use. This is accessed
142 // through the getAnalysis() function in this class and in Pass.
143 //
144 std::map<AnalysisID, Pass*> CurrentAnalyses;
145
Chris Lattnerac3e0602002-01-31 18:32:27 +0000146 // LastUseOf - This map keeps track of the last usage in our pipeline of a
147 // particular pass. When executing passes, the memory for .first is free'd
148 // after .second is run.
149 //
150 std::map<Pass*, Pass*> LastUseOf;
151
Chris Lattner67d25652002-01-30 23:20:39 +0000152public:
153 PassManagerT(ParentClass *Par = 0) : Parent(Par), Batcher(0) {}
154 ~PassManagerT() {
155 // Delete all of the contained passes...
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000156 for (typename std::vector<PassClass*>::iterator
157 I = Passes.begin(), E = Passes.end(); I != E; ++I)
Chris Lattner67d25652002-01-30 23:20:39 +0000158 delete *I;
159 }
160
161 // run - Run all of the queued passes on the specified module in an optimal
162 // way.
163 virtual bool runOnUnit(UnitType *M) {
164 bool MadeChanges = false;
165 closeBatcher();
166 CurrentAnalyses.clear();
167
Chris Lattnerac3e0602002-01-31 18:32:27 +0000168 // LastUserOf - This contains the inverted LastUseOfMap...
169 std::map<Pass *, std::vector<Pass*> > LastUserOf;
170 for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(),
171 E = LastUseOf.end(); I != E; ++I)
172 LastUserOf[I->second].push_back(I->first);
173
174
Chris Lattner67d25652002-01-30 23:20:39 +0000175 // Output debug information...
Chris Lattner1e4867f2002-07-30 19:51:02 +0000176 if (Parent == 0) PMDebug::PerformPassStartupStuff(this);
Chris Lattner67d25652002-01-30 23:20:39 +0000177
178 // Run all of the passes
179 for (unsigned i = 0, e = Passes.size(); i < e; ++i) {
180 PassClass *P = Passes[i];
181
Chris Lattnera454b5b2002-04-28 05:14:06 +0000182 PMDebug::PrintPassInformation(getDepth(), "Executing Pass", P,
183 (Annotable*)M);
Chris Lattner67d25652002-01-30 23:20:39 +0000184
185 // Get information about what analyses the pass uses...
Chris Lattnerc8e66542002-04-27 06:56:12 +0000186 AnalysisUsage AnUsage;
187 P->getAnalysisUsage(AnUsage);
188 PMDebug::PrintAnalysisSetInfo(getDepth(), "Required", P,
189 AnUsage.getRequiredSet());
Chris Lattner67d25652002-01-30 23:20:39 +0000190
Chris Lattner216edd42002-08-30 20:25:01 +0000191 // All Required analyses should be available to the pass as it runs! Here
192 // we fill in the AnalysisImpls member of the pass so that it can
193 // successfully use the getAnalysis() method to retrieve the
194 // implementations it needs.
195 //
196 P->AnalysisImpls.clear();
197 P->AnalysisImpls.reserve(AnUsage.getRequiredSet().size());
198 for (std::vector<const PassInfo *>::const_iterator
Chris Lattnerc8e66542002-04-27 06:56:12 +0000199 I = AnUsage.getRequiredSet().begin(),
200 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Chris Lattner216edd42002-08-30 20:25:01 +0000201 Pass *Impl = getAnalysisOrNullUp(*I);
202 if (Impl == 0) {
203 std::cerr << "Analysis '" << (*I)->getPassName()
204 << "' used but not available!";
205 assert(0 && "Analysis used but not available!");
206 } else if (PassDebugging == Details) {
207 if ((*I)->getPassName() != std::string(Impl->getPassName()))
208 std::cerr << " Interface '" << (*I)->getPassName()
209 << "' implemented by '" << Impl->getPassName() << "'\n";
210 }
211 P->AnalysisImpls.push_back(std::make_pair(*I, Impl));
Chris Lattner67d25652002-01-30 23:20:39 +0000212 }
Chris Lattner67d25652002-01-30 23:20:39 +0000213
214 // Run the sub pass!
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000215 startPass(P);
216 bool Changed = runPass(P, M);
217 endPass(P);
Chris Lattnerf2f31bd2002-02-01 04:53:36 +0000218 MadeChanges |= Changed;
Chris Lattner67d25652002-01-30 23:20:39 +0000219
Chris Lattnerf2f31bd2002-02-01 04:53:36 +0000220 if (Changed)
221 PMDebug::PrintPassInformation(getDepth()+1, "Made Modification", P,
Chris Lattnera454b5b2002-04-28 05:14:06 +0000222 (Annotable*)M);
Chris Lattnerc8e66542002-04-27 06:56:12 +0000223 PMDebug::PrintAnalysisSetInfo(getDepth(), "Preserved", P,
224 AnUsage.getPreservedSet());
Chris Lattner67d25652002-01-30 23:20:39 +0000225
Chris Lattnerc8e66542002-04-27 06:56:12 +0000226
227 // Erase all analyses not in the preserved set...
228 if (!AnUsage.preservesAll()) {
229 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
230 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(),
231 E = CurrentAnalyses.end(); I != E; )
232 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) !=
233 PreservedSet.end())
234 ++I; // This analysis is preserved, leave it in the available set...
235 else {
236#if MAP_DOESNT_HAVE_BROKEN_ERASE_MEMBER
237 I = CurrentAnalyses.erase(I); // Analysis not preserved!
238#else
239 // GCC 2.95.3 STL doesn't have correct erase member!
240 CurrentAnalyses.erase(I);
241 I = CurrentAnalyses.begin();
242#endif
243 }
244 }
245
Chris Lattner73503172002-07-29 21:03:38 +0000246 // Add the current pass to the set of passes that have been run, and are
247 // thus available to users.
248 //
Chris Lattner216edd42002-08-30 20:25:01 +0000249 if (const PassInfo *PI = P->getPassInfo()) {
Chris Lattner73503172002-07-29 21:03:38 +0000250 CurrentAnalyses[PI] = P;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000251
Chris Lattner216edd42002-08-30 20:25:01 +0000252 // This pass is the current implementation of all of the interfaces it
253 // implements as well.
254 //
255 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
256 for (unsigned i = 0, e = II.size(); i != e; ++i)
257 CurrentAnalyses[II[i]] = P;
258 }
259
Chris Lattnerac3e0602002-01-31 18:32:27 +0000260 // Free memory for any passes that we are the last use of...
261 std::vector<Pass*> &DeadPass = LastUserOf[P];
262 for (std::vector<Pass*>::iterator I = DeadPass.begin(),E = DeadPass.end();
263 I != E; ++I) {
264 PMDebug::PrintPassInformation(getDepth()+1, "Freeing Pass", *I,
Chris Lattnera454b5b2002-04-28 05:14:06 +0000265 (Annotable*)M);
Chris Lattnerac3e0602002-01-31 18:32:27 +0000266 (*I)->releaseMemory();
267 }
Chris Lattner67d25652002-01-30 23:20:39 +0000268 }
269 return MadeChanges;
270 }
271
Chris Lattnerac3e0602002-01-31 18:32:27 +0000272 // dumpPassStructure - Implement the -debug-passes=PassStructure option
273 virtual void dumpPassStructure(unsigned Offset = 0) {
274 std::cerr << std::string(Offset*2, ' ') << Traits::getPMName()
275 << " Pass Manager\n";
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000276 for (typename std::vector<PassClass*>::iterator
277 I = Passes.begin(), E = Passes.end(); I != E; ++I) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000278 PassClass *P = *I;
279 P->dumpPassStructure(Offset+1);
280
281 // Loop through and see which classes are destroyed after this one...
282 for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(),
283 E = LastUseOf.end(); I != E; ++I) {
284 if (P == I->second) {
Chris Lattner73503172002-07-29 21:03:38 +0000285 std::cerr << "--" << std::string(Offset*2, ' ');
Chris Lattnerac3e0602002-01-31 18:32:27 +0000286 I->first->dumpPassStructure(0);
287 }
288 }
289 }
290 }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000291
Chris Lattner6e041bd2002-08-21 22:17:09 +0000292 Pass *getAnalysisOrNullDown(const PassInfo *ID) const {
Chris Lattner216edd42002-08-30 20:25:01 +0000293 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000294
295 if (I != CurrentAnalyses.end())
296 return I->second; // Found it.
297
298 if (Batcher)
299 return ((AnalysisResolver*)Batcher)->getAnalysisOrNullDown(ID);
300 return 0;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000301 }
302
Chris Lattner6e041bd2002-08-21 22:17:09 +0000303 Pass *getAnalysisOrNullUp(const PassInfo *ID) const {
Chris Lattner216edd42002-08-30 20:25:01 +0000304 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000305 if (I != CurrentAnalyses.end())
306 return I->second; // Found it.
307
308 if (Parent) // Try scanning...
309 return Parent->getAnalysisOrNullUp(ID);
310 return 0;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000311 }
312
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000313 // {start/end}Pass - Called when a pass is started, it just propogates
314 // information up to the top level PassManagerT object to tell it that a pass
315 // has started or ended. This is used to gather timing information about
316 // passes.
317 //
318 void startPass(Pass *P) {
319 if (Parent) Parent->startPass(P);
320 else PassStarted(P);
321 }
322 void endPass(Pass *P) {
323 if (Parent) Parent->endPass(P);
324 else PassEnded(P);
325 }
326
Chris Lattnerac3e0602002-01-31 18:32:27 +0000327 // markPassUsed - Inform higher level pass managers (and ourselves)
328 // that these analyses are being used by this pass. This is used to
329 // make sure that analyses are not free'd before we have to use
330 // them...
331 //
Chris Lattner6e041bd2002-08-21 22:17:09 +0000332 void markPassUsed(const PassInfo *P, Pass *User) {
Chris Lattner216edd42002-08-30 20:25:01 +0000333 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(P);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000334
Chris Lattnerac3e0602002-01-31 18:32:27 +0000335 if (I != CurrentAnalyses.end()) {
336 LastUseOf[I->second] = User; // Local pass, extend the lifetime
337 } else {
338 // Pass not in current available set, must be a higher level pass
339 // available to us, propogate to parent pass manager... We tell the
340 // parent that we (the passmanager) are using the analysis so that it
341 // frees the analysis AFTER this pass manager runs.
342 //
Chris Lattner6e041bd2002-08-21 22:17:09 +0000343 assert(Parent != 0 && "Pass available but not found!");
Chris Lattnerac3e0602002-01-31 18:32:27 +0000344 Parent->markPassUsed(P, this);
345 }
346 }
347
348 // Return the number of parent PassManagers that exist
349 virtual unsigned getDepth() const {
350 if (Parent == 0) return 0;
351 return 1 + Parent->getDepth();
352 }
353
Chris Lattner1e4867f2002-07-30 19:51:02 +0000354 virtual unsigned getNumContainedPasses() const { return Passes.size(); }
355 virtual const Pass *getContainedPass(unsigned N) const {
356 assert(N < Passes.size() && "Pass number out of range!");
357 return Passes[N];
358 }
359
Chris Lattner6e041bd2002-08-21 22:17:09 +0000360 // add - Add a pass to the queue of passes to run. This gives ownership of
Chris Lattner67d25652002-01-30 23:20:39 +0000361 // the Pass to the PassManager. When the PassManager is destroyed, the pass
Chris Lattnerac3e0602002-01-31 18:32:27 +0000362 // will be destroyed as well, so there is no need to delete the pass. This
363 // implies that all passes MUST be new'd.
Chris Lattner67d25652002-01-30 23:20:39 +0000364 //
365 void add(PassClass *P) {
366 // Get information about what analyses the pass uses...
Chris Lattnerc8e66542002-04-27 06:56:12 +0000367 AnalysisUsage AnUsage;
368 P->getAnalysisUsage(AnUsage);
369 const std::vector<AnalysisID> &Required = AnUsage.getRequiredSet();
Chris Lattner67d25652002-01-30 23:20:39 +0000370
371 // Loop over all of the analyses used by this pass,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000372 for (std::vector<AnalysisID>::const_iterator I = Required.begin(),
373 E = Required.end(); I != E; ++I) {
Chris Lattner67d25652002-01-30 23:20:39 +0000374 if (getAnalysisOrNullDown(*I) == 0)
Chris Lattner26750072002-07-27 01:12:17 +0000375 add((PassClass*)(*I)->createPass());
Chris Lattner67d25652002-01-30 23:20:39 +0000376 }
377
378 // Tell the pass to add itself to this PassManager... the way it does so
379 // depends on the class of the pass, and is critical to laying out passes in
380 // an optimal order..
381 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000382 P->addToPassManager(this, AnUsage);
Chris Lattner67d25652002-01-30 23:20:39 +0000383 }
384
385private:
386
387 // addPass - These functions are used to implement the subclass specific
388 // behaviors present in PassManager. Basically the add(Pass*) method ends up
389 // reflecting its behavior into a Pass::addToPassManager call. Subclasses of
390 // Pass override it specifically so that they can reflect the type
391 // information inherent in "this" back to the PassManager.
392 //
393 // For generic Pass subclasses (which are interprocedural passes), we simply
394 // add the pass to the end of the pass list and terminate any accumulation of
Chris Lattnerc8e66542002-04-27 06:56:12 +0000395 // FunctionPass's that are present.
Chris Lattner67d25652002-01-30 23:20:39 +0000396 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000397 void addPass(PassClass *P, AnalysisUsage &AnUsage) {
398 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
Chris Lattnerc8e66542002-04-27 06:56:12 +0000399
Chris Lattner73503172002-07-29 21:03:38 +0000400 // FIXME: If this pass being added isn't killed by any of the passes in the
401 // batcher class then we can reorder to pass to execute before the batcher
402 // does, which will potentially allow us to batch more passes!
Chris Lattner67d25652002-01-30 23:20:39 +0000403 //
Chris Lattner73503172002-07-29 21:03:38 +0000404 //const std::vector<AnalysisID> &ProvidedSet = AnUsage.getProvidedSet();
405 if (Batcher /*&& ProvidedSet.empty()*/)
Chris Lattner67d25652002-01-30 23:20:39 +0000406 closeBatcher(); // This pass cannot be batched!
407
408 // Set the Resolver instance variable in the Pass so that it knows where to
409 // find this object...
410 //
411 setAnalysisResolver(P, this);
412 Passes.push_back(P);
413
Chris Lattnerac3e0602002-01-31 18:32:27 +0000414 // Inform higher level pass managers (and ourselves) that these analyses are
415 // being used by this pass. This is used to make sure that analyses are not
416 // free'd before we have to use them...
417 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000418 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
419 E = RequiredSet.end(); I != E; ++I)
Chris Lattnerac3e0602002-01-31 18:32:27 +0000420 markPassUsed(*I, P); // Mark *I as used by P
421
Chris Lattnerc8e66542002-04-27 06:56:12 +0000422 // Erase all analyses not in the preserved set...
423 if (!AnUsage.preservesAll()) {
424 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
425 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(),
426 E = CurrentAnalyses.end(); I != E; )
427 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) !=
428 PreservedSet.end())
429 ++I; // This analysis is preserved, leave it in the available set...
430 else {
431#if MAP_DOESNT_HAVE_BROKEN_ERASE_MEMBER
432 I = CurrentAnalyses.erase(I); // Analysis not preserved!
433#else
434 CurrentAnalyses.erase(I);// GCC 2.95.3 STL doesn't have correct erase!
435 I = CurrentAnalyses.begin();
436#endif
437 }
438 }
Chris Lattner67d25652002-01-30 23:20:39 +0000439
Chris Lattner73503172002-07-29 21:03:38 +0000440 // Add this pass to the currently available set...
Chris Lattner216edd42002-08-30 20:25:01 +0000441 if (const PassInfo *PI = P->getPassInfo()) {
Chris Lattner73503172002-07-29 21:03:38 +0000442 CurrentAnalyses[PI] = P;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000443
Chris Lattner216edd42002-08-30 20:25:01 +0000444 // This pass is the current implementation of all of the interfaces it
445 // implements as well.
446 //
447 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
448 for (unsigned i = 0, e = II.size(); i != e; ++i)
449 CurrentAnalyses[II[i]] = P;
450 }
451
Chris Lattnerac3e0602002-01-31 18:32:27 +0000452 // For now assume that our results are never used...
453 LastUseOf[P] = P;
Chris Lattner67d25652002-01-30 23:20:39 +0000454 }
455
Chris Lattnerc8e66542002-04-27 06:56:12 +0000456 // For FunctionPass subclasses, we must be sure to batch the FunctionPass's
457 // together in a BatcherClass object so that all of the analyses are run
458 // together a function at a time.
Chris Lattner67d25652002-01-30 23:20:39 +0000459 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000460 void addPass(SubPassClass *MP, AnalysisUsage &AnUsage) {
Chris Lattner67d25652002-01-30 23:20:39 +0000461 if (Batcher == 0) // If we don't have a batcher yet, make one now.
462 Batcher = new BatcherClass(this);
Chris Lattner73503172002-07-29 21:03:38 +0000463 // The Batcher will queue the passes up
Chris Lattnerc8e66542002-04-27 06:56:12 +0000464 MP->addToPassManager(Batcher, AnUsage);
Chris Lattner67d25652002-01-30 23:20:39 +0000465 }
466
467 // closeBatcher - Terminate the batcher that is being worked on.
468 void closeBatcher() {
469 if (Batcher) {
470 Passes.push_back(Batcher);
471 Batcher = 0;
472 }
473 }
474};
475
476
477
478//===----------------------------------------------------------------------===//
479// PassManagerTraits<BasicBlock> Specialization
480//
481// This pass manager is used to group together all of the BasicBlockPass's
482// into a single unit.
483//
484template<> struct PassManagerTraits<BasicBlock> : public BasicBlockPass {
485 // PassClass - The type of passes tracked by this PassManager
486 typedef BasicBlockPass PassClass;
487
488 // SubPassClass - The types of classes that should be collated together
489 // This is impossible to match, so BasicBlock instantiations of PassManagerT
490 // do not collate.
491 //
492 typedef PassManagerT<Module> SubPassClass;
493
494 // BatcherClass - The type to use for collation of subtypes... This class is
495 // never instantiated for the PassManager<BasicBlock>, but it must be an
496 // instance of PassClass to typecheck.
497 //
498 typedef PassClass BatcherClass;
499
500 // ParentClass - The type of the parent PassManager...
Chris Lattner4e8c4872002-03-23 22:51:58 +0000501 typedef PassManagerT<Function> ParentClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000502
Chris Lattner2eaac392002-01-31 00:40:44 +0000503 // PMType - The type of the passmanager that subclasses this class
504 typedef PassManagerT<BasicBlock> PMType;
505
Chris Lattner67d25652002-01-30 23:20:39 +0000506 // runPass - Specify how the pass should be run on the UnitType
507 static bool runPass(PassClass *P, BasicBlock *M) {
508 // todo, init and finalize
Chris Lattner113f4f42002-06-25 16:13:24 +0000509 return P->runOnBasicBlock(*M);
Chris Lattner67d25652002-01-30 23:20:39 +0000510 }
511
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000512 // Dummy implementation of PassStarted/PassEnded
513 static void PassStarted(Pass *P) {}
514 static void PassEnded(Pass *P) {}
515
Chris Lattnerac3e0602002-01-31 18:32:27 +0000516 // getPMName() - Return the name of the unit the PassManager operates on for
517 // debugging.
518 const char *getPMName() const { return "BasicBlock"; }
Chris Lattner37104aa2002-04-29 14:57:45 +0000519 virtual const char *getPassName() const { return "BasicBlock Pass Manager"; }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000520
Chris Lattner2eaac392002-01-31 00:40:44 +0000521 // Implement the BasicBlockPass interface...
Chris Lattner113f4f42002-06-25 16:13:24 +0000522 virtual bool doInitialization(Module &M);
523 virtual bool runOnBasicBlock(BasicBlock &BB);
524 virtual bool doFinalization(Module &M);
Chris Lattnercb429062002-04-30 18:50:17 +0000525
526 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
527 AU.setPreservesAll();
528 }
Chris Lattner67d25652002-01-30 23:20:39 +0000529};
530
531
532
533//===----------------------------------------------------------------------===//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000534// PassManagerTraits<Function> Specialization
Chris Lattner67d25652002-01-30 23:20:39 +0000535//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000536// This pass manager is used to group together all of the FunctionPass's
Chris Lattner67d25652002-01-30 23:20:39 +0000537// into a single unit.
538//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000539template<> struct PassManagerTraits<Function> : public FunctionPass {
Chris Lattner67d25652002-01-30 23:20:39 +0000540 // PassClass - The type of passes tracked by this PassManager
Chris Lattnerc8e66542002-04-27 06:56:12 +0000541 typedef FunctionPass PassClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000542
543 // SubPassClass - The types of classes that should be collated together
544 typedef BasicBlockPass SubPassClass;
545
546 // BatcherClass - The type to use for collation of subtypes...
547 typedef PassManagerT<BasicBlock> BatcherClass;
548
549 // ParentClass - The type of the parent PassManager...
550 typedef PassManagerT<Module> ParentClass;
551
552 // PMType - The type of the passmanager that subclasses this class
Chris Lattner4e8c4872002-03-23 22:51:58 +0000553 typedef PassManagerT<Function> PMType;
Chris Lattner67d25652002-01-30 23:20:39 +0000554
555 // runPass - Specify how the pass should be run on the UnitType
Chris Lattnerc8e66542002-04-27 06:56:12 +0000556 static bool runPass(PassClass *P, Function *F) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000557 return P->runOnFunction(*F);
Chris Lattner67d25652002-01-30 23:20:39 +0000558 }
559
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000560 // Dummy implementation of PassStarted/PassEnded
561 static void PassStarted(Pass *P) {}
562 static void PassEnded(Pass *P) {}
563
Chris Lattnerac3e0602002-01-31 18:32:27 +0000564 // getPMName() - Return the name of the unit the PassManager operates on for
565 // debugging.
Chris Lattner4e8c4872002-03-23 22:51:58 +0000566 const char *getPMName() const { return "Function"; }
Chris Lattner37104aa2002-04-29 14:57:45 +0000567 virtual const char *getPassName() const { return "Function Pass Manager"; }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000568
Chris Lattnerc8e66542002-04-27 06:56:12 +0000569 // Implement the FunctionPass interface...
Chris Lattner113f4f42002-06-25 16:13:24 +0000570 virtual bool doInitialization(Module &M);
571 virtual bool runOnFunction(Function &F);
572 virtual bool doFinalization(Module &M);
Chris Lattnercb429062002-04-30 18:50:17 +0000573
574 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
575 AU.setPreservesAll();
576 }
Chris Lattner67d25652002-01-30 23:20:39 +0000577};
578
579
580
581//===----------------------------------------------------------------------===//
582// PassManagerTraits<Module> Specialization
583//
584// This is the top level PassManager implementation that holds generic passes.
585//
586template<> struct PassManagerTraits<Module> : public Pass {
587 // PassClass - The type of passes tracked by this PassManager
588 typedef Pass PassClass;
589
590 // SubPassClass - The types of classes that should be collated together
Chris Lattnerc8e66542002-04-27 06:56:12 +0000591 typedef FunctionPass SubPassClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000592
593 // BatcherClass - The type to use for collation of subtypes...
Chris Lattner4e8c4872002-03-23 22:51:58 +0000594 typedef PassManagerT<Function> BatcherClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000595
596 // ParentClass - The type of the parent PassManager...
Chris Lattnerac3e0602002-01-31 18:32:27 +0000597 typedef AnalysisResolver ParentClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000598
599 // runPass - Specify how the pass should be run on the UnitType
Chris Lattner113f4f42002-06-25 16:13:24 +0000600 static bool runPass(PassClass *P, Module *M) { return P->run(*M); }
Chris Lattner67d25652002-01-30 23:20:39 +0000601
Chris Lattnerac3e0602002-01-31 18:32:27 +0000602 // getPMName() - Return the name of the unit the PassManager operates on for
603 // debugging.
604 const char *getPMName() const { return "Module"; }
Chris Lattner37104aa2002-04-29 14:57:45 +0000605 virtual const char *getPassName() const { return "Module Pass Manager"; }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000606
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000607 // TimingInformation - This data member maintains timing information for each
608 // of the passes that is executed.
609 //
610 TimingInfo *TimeInfo;
611
612 // PassStarted/Ended - This callback is notified any time a pass is started
613 // or stops. This is used to collect timing information about the different
614 // passes being executed.
615 //
616 void PassStarted(Pass *P) {
617 if (TimeInfo) TimeInfo->passStarted(P);
Chris Lattner67d25652002-01-30 23:20:39 +0000618 }
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000619 void PassEnded(Pass *P) {
620 if (TimeInfo) TimeInfo->passEnded(P);
621 }
622
623 // run - Implement the PassManager interface...
Chris Lattner113f4f42002-06-25 16:13:24 +0000624 bool run(Module &M) {
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000625 TimeInfo = TimingInfo::create();
Chris Lattner113f4f42002-06-25 16:13:24 +0000626 bool Result = ((PassManagerT<Module>*)this)->runOnUnit(&M);
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000627 if (TimeInfo) {
628 delete TimeInfo;
629 TimeInfo = 0;
630 }
631 return Result;
632 }
633
634 // PassManagerTraits constructor - Create a timing info object if the user
635 // specified timing info should be collected on the command line.
636 //
637 PassManagerTraits() : TimeInfo(0) {}
Chris Lattner67d25652002-01-30 23:20:39 +0000638};
639
640
641
642//===----------------------------------------------------------------------===//
643// PassManagerTraits Method Implementations
644//
645
646// PassManagerTraits<BasicBlock> Implementations
647//
Chris Lattner113f4f42002-06-25 16:13:24 +0000648inline bool PassManagerTraits<BasicBlock>::doInitialization(Module &M) {
Chris Lattner2eaac392002-01-31 00:40:44 +0000649 bool Changed = false;
650 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
651 ((PMType*)this)->Passes[i]->doInitialization(M);
652 return Changed;
653}
654
Chris Lattner113f4f42002-06-25 16:13:24 +0000655inline bool PassManagerTraits<BasicBlock>::runOnBasicBlock(BasicBlock &BB) {
656 return ((PMType*)this)->runOnUnit(&BB);
Chris Lattner2eaac392002-01-31 00:40:44 +0000657}
658
Chris Lattner113f4f42002-06-25 16:13:24 +0000659inline bool PassManagerTraits<BasicBlock>::doFinalization(Module &M) {
Chris Lattner2eaac392002-01-31 00:40:44 +0000660 bool Changed = false;
661 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
662 ((PMType*)this)->Passes[i]->doFinalization(M);
663 return Changed;
Chris Lattner67d25652002-01-30 23:20:39 +0000664}
665
666
Chris Lattner4e8c4872002-03-23 22:51:58 +0000667// PassManagerTraits<Function> Implementations
Chris Lattner67d25652002-01-30 23:20:39 +0000668//
Chris Lattner113f4f42002-06-25 16:13:24 +0000669inline bool PassManagerTraits<Function>::doInitialization(Module &M) {
Chris Lattner67d25652002-01-30 23:20:39 +0000670 bool Changed = false;
671 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
672 ((PMType*)this)->Passes[i]->doInitialization(M);
673 return Changed;
674}
675
Chris Lattner113f4f42002-06-25 16:13:24 +0000676inline bool PassManagerTraits<Function>::runOnFunction(Function &F) {
677 return ((PMType*)this)->runOnUnit(&F);
Chris Lattner67d25652002-01-30 23:20:39 +0000678}
679
Chris Lattner113f4f42002-06-25 16:13:24 +0000680inline bool PassManagerTraits<Function>::doFinalization(Module &M) {
Chris Lattner67d25652002-01-30 23:20:39 +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;
685}
686
687#endif