blob: cf8624105da987fb323e3c2707ab3a69eb09656c [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 Lattneree0788d2002-09-25 21:59:11 +0000132 friend class ImmutablePass;
Chris Lattner67d25652002-01-30 23:20:39 +0000133
Chris Lattner73503172002-07-29 21:03:38 +0000134 std::vector<PassClass*> Passes; // List of passes to run
Chris Lattneree0788d2002-09-25 21:59:11 +0000135 std::vector<ImmutablePass*> ImmutablePasses; // List of immutable passes
Chris Lattner67d25652002-01-30 23:20:39 +0000136
137 // The parent of this pass manager...
Chris Lattnerac3e0602002-01-31 18:32:27 +0000138 ParentClass * const Parent;
Chris Lattner67d25652002-01-30 23:20:39 +0000139
140 // The current batcher if one is in use, or null
141 BatcherClass *Batcher;
142
143 // CurrentAnalyses - As the passes are being run, this map contains the
144 // analyses that are available to the current pass for use. This is accessed
145 // through the getAnalysis() function in this class and in Pass.
146 //
147 std::map<AnalysisID, Pass*> CurrentAnalyses;
148
Chris Lattnerac3e0602002-01-31 18:32:27 +0000149 // LastUseOf - This map keeps track of the last usage in our pipeline of a
150 // particular pass. When executing passes, the memory for .first is free'd
151 // after .second is run.
152 //
153 std::map<Pass*, Pass*> LastUseOf;
154
Chris Lattner67d25652002-01-30 23:20:39 +0000155public:
156 PassManagerT(ParentClass *Par = 0) : Parent(Par), Batcher(0) {}
157 ~PassManagerT() {
158 // Delete all of the contained passes...
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000159 for (typename std::vector<PassClass*>::iterator
160 I = Passes.begin(), E = Passes.end(); I != E; ++I)
Chris Lattner67d25652002-01-30 23:20:39 +0000161 delete *I;
Chris Lattneree0788d2002-09-25 21:59:11 +0000162
163 for (std::vector<ImmutablePass*>::iterator
164 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
165 delete *I;
Chris Lattner67d25652002-01-30 23:20:39 +0000166 }
167
168 // run - Run all of the queued passes on the specified module in an optimal
169 // way.
170 virtual bool runOnUnit(UnitType *M) {
171 bool MadeChanges = false;
172 closeBatcher();
173 CurrentAnalyses.clear();
174
Chris Lattneree0788d2002-09-25 21:59:11 +0000175 // Add any immutable passes to the CurrentAnalyses set...
Chris Lattner480b37d2002-09-25 22:26:52 +0000176 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
177 ImmutablePass *IPass = ImmutablePasses[i];
178 if (const PassInfo *PI = IPass->getPassInfo()) {
179 CurrentAnalyses[PI] = IPass;
Chris Lattneree0788d2002-09-25 21:59:11 +0000180
181 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
182 for (unsigned i = 0, e = II.size(); i != e; ++i)
Chris Lattner480b37d2002-09-25 22:26:52 +0000183 CurrentAnalyses[II[i]] = IPass;
Chris Lattneree0788d2002-09-25 21:59:11 +0000184 }
Chris Lattner480b37d2002-09-25 22:26:52 +0000185 }
Chris Lattneree0788d2002-09-25 21:59:11 +0000186
Chris Lattnerac3e0602002-01-31 18:32:27 +0000187 // LastUserOf - This contains the inverted LastUseOfMap...
188 std::map<Pass *, std::vector<Pass*> > LastUserOf;
189 for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(),
190 E = LastUseOf.end(); I != E; ++I)
191 LastUserOf[I->second].push_back(I->first);
192
193
Chris Lattner67d25652002-01-30 23:20:39 +0000194 // Output debug information...
Chris Lattner1e4867f2002-07-30 19:51:02 +0000195 if (Parent == 0) PMDebug::PerformPassStartupStuff(this);
Chris Lattner67d25652002-01-30 23:20:39 +0000196
197 // Run all of the passes
198 for (unsigned i = 0, e = Passes.size(); i < e; ++i) {
199 PassClass *P = Passes[i];
200
Chris Lattnera454b5b2002-04-28 05:14:06 +0000201 PMDebug::PrintPassInformation(getDepth(), "Executing Pass", P,
202 (Annotable*)M);
Chris Lattner67d25652002-01-30 23:20:39 +0000203
204 // Get information about what analyses the pass uses...
Chris Lattnerc8e66542002-04-27 06:56:12 +0000205 AnalysisUsage AnUsage;
206 P->getAnalysisUsage(AnUsage);
207 PMDebug::PrintAnalysisSetInfo(getDepth(), "Required", P,
208 AnUsage.getRequiredSet());
Chris Lattner67d25652002-01-30 23:20:39 +0000209
Chris Lattner216edd42002-08-30 20:25:01 +0000210 // All Required analyses should be available to the pass as it runs! Here
211 // we fill in the AnalysisImpls member of the pass so that it can
212 // successfully use the getAnalysis() method to retrieve the
213 // implementations it needs.
214 //
215 P->AnalysisImpls.clear();
216 P->AnalysisImpls.reserve(AnUsage.getRequiredSet().size());
217 for (std::vector<const PassInfo *>::const_iterator
Chris Lattnerc8e66542002-04-27 06:56:12 +0000218 I = AnUsage.getRequiredSet().begin(),
219 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Chris Lattner216edd42002-08-30 20:25:01 +0000220 Pass *Impl = getAnalysisOrNullUp(*I);
221 if (Impl == 0) {
222 std::cerr << "Analysis '" << (*I)->getPassName()
223 << "' used but not available!";
224 assert(0 && "Analysis used but not available!");
225 } else if (PassDebugging == Details) {
226 if ((*I)->getPassName() != std::string(Impl->getPassName()))
227 std::cerr << " Interface '" << (*I)->getPassName()
228 << "' implemented by '" << Impl->getPassName() << "'\n";
229 }
230 P->AnalysisImpls.push_back(std::make_pair(*I, Impl));
Chris Lattner67d25652002-01-30 23:20:39 +0000231 }
Chris Lattner67d25652002-01-30 23:20:39 +0000232
233 // Run the sub pass!
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000234 startPass(P);
235 bool Changed = runPass(P, M);
236 endPass(P);
Chris Lattnerf2f31bd2002-02-01 04:53:36 +0000237 MadeChanges |= Changed;
Chris Lattner67d25652002-01-30 23:20:39 +0000238
Chris Lattnered6504b2002-09-08 19:00:07 +0000239 // Check for memory leaks by the pass...
240 LeakDetector::checkForGarbage(std::string("after running pass '") +
241 P->getPassName() + "'");
242
Chris Lattnerf2f31bd2002-02-01 04:53:36 +0000243 if (Changed)
244 PMDebug::PrintPassInformation(getDepth()+1, "Made Modification", P,
Chris Lattnera454b5b2002-04-28 05:14:06 +0000245 (Annotable*)M);
Chris Lattnerc8e66542002-04-27 06:56:12 +0000246 PMDebug::PrintAnalysisSetInfo(getDepth(), "Preserved", P,
247 AnUsage.getPreservedSet());
Chris Lattner67d25652002-01-30 23:20:39 +0000248
Chris Lattnerc8e66542002-04-27 06:56:12 +0000249
250 // Erase all analyses not in the preserved set...
251 if (!AnUsage.preservesAll()) {
252 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
253 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(),
254 E = CurrentAnalyses.end(); I != E; )
255 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) !=
256 PreservedSet.end())
257 ++I; // This analysis is preserved, leave it in the available set...
258 else {
Chris Lattneree0788d2002-09-25 21:59:11 +0000259 if (!dynamic_cast<ImmutablePass*>(I->second)) {
Chris Lattnerc8e66542002-04-27 06:56:12 +0000260#if MAP_DOESNT_HAVE_BROKEN_ERASE_MEMBER
Chris Lattneree0788d2002-09-25 21:59:11 +0000261 I = CurrentAnalyses.erase(I); // Analysis not preserved!
Chris Lattnerc8e66542002-04-27 06:56:12 +0000262#else
Chris Lattneree0788d2002-09-25 21:59:11 +0000263 // GCC 2.95.3 STL doesn't have correct erase member!
264 CurrentAnalyses.erase(I);
265 I = CurrentAnalyses.begin();
Chris Lattnerc8e66542002-04-27 06:56:12 +0000266#endif
Chris Lattneree0788d2002-09-25 21:59:11 +0000267 } else {
268 ++I;
269 }
Chris Lattnerc8e66542002-04-27 06:56:12 +0000270 }
271 }
272
Chris Lattner73503172002-07-29 21:03:38 +0000273 // Add the current pass to the set of passes that have been run, and are
274 // thus available to users.
275 //
Chris Lattner216edd42002-08-30 20:25:01 +0000276 if (const PassInfo *PI = P->getPassInfo()) {
Chris Lattner73503172002-07-29 21:03:38 +0000277 CurrentAnalyses[PI] = P;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000278
Chris Lattner216edd42002-08-30 20:25:01 +0000279 // This pass is the current implementation of all of the interfaces it
280 // implements as well.
281 //
282 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
283 for (unsigned i = 0, e = II.size(); i != e; ++i)
284 CurrentAnalyses[II[i]] = P;
285 }
286
Chris Lattnerac3e0602002-01-31 18:32:27 +0000287 // Free memory for any passes that we are the last use of...
288 std::vector<Pass*> &DeadPass = LastUserOf[P];
289 for (std::vector<Pass*>::iterator I = DeadPass.begin(),E = DeadPass.end();
290 I != E; ++I) {
291 PMDebug::PrintPassInformation(getDepth()+1, "Freeing Pass", *I,
Chris Lattnera454b5b2002-04-28 05:14:06 +0000292 (Annotable*)M);
Chris Lattnerac3e0602002-01-31 18:32:27 +0000293 (*I)->releaseMemory();
294 }
Chris Lattner6b6f5402002-09-29 22:50:22 +0000295
296 // Make sure to remove dead passes from the CurrentAnalyses list...
297 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin();
298 I != CurrentAnalyses.end(); ) {
299 std::vector<Pass*>::iterator DPI = std::find(DeadPass.begin(),
300 DeadPass.end(), I->second);
301 if (DPI != DeadPass.end()) { // This pass is dead now... remove it
302 std::map<AnalysisID, Pass*>::iterator IDead = I++;
303 CurrentAnalyses.erase(IDead);
304 } else {
305 ++I; // Move on to the next element...
306 }
307 }
Chris Lattner67d25652002-01-30 23:20:39 +0000308 }
309 return MadeChanges;
310 }
311
Chris Lattnerac3e0602002-01-31 18:32:27 +0000312 // dumpPassStructure - Implement the -debug-passes=PassStructure option
313 virtual void dumpPassStructure(unsigned Offset = 0) {
Chris Lattner480b37d2002-09-25 22:26:52 +0000314 // Print out the immutable passes...
315 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i)
316 ImmutablePasses[i]->dumpPassStructure(0);
317
Chris Lattnerac3e0602002-01-31 18:32:27 +0000318 std::cerr << std::string(Offset*2, ' ') << Traits::getPMName()
319 << " Pass Manager\n";
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000320 for (typename std::vector<PassClass*>::iterator
321 I = Passes.begin(), E = Passes.end(); I != E; ++I) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000322 PassClass *P = *I;
323 P->dumpPassStructure(Offset+1);
324
325 // Loop through and see which classes are destroyed after this one...
326 for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(),
327 E = LastUseOf.end(); I != E; ++I) {
328 if (P == I->second) {
Chris Lattner73503172002-07-29 21:03:38 +0000329 std::cerr << "--" << std::string(Offset*2, ' ');
Chris Lattnerac3e0602002-01-31 18:32:27 +0000330 I->first->dumpPassStructure(0);
331 }
332 }
333 }
334 }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000335
Chris Lattner6e041bd2002-08-21 22:17:09 +0000336 Pass *getAnalysisOrNullDown(const PassInfo *ID) const {
Chris Lattner216edd42002-08-30 20:25:01 +0000337 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000338
339 if (I != CurrentAnalyses.end())
340 return I->second; // Found it.
341
342 if (Batcher)
343 return ((AnalysisResolver*)Batcher)->getAnalysisOrNullDown(ID);
344 return 0;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000345 }
346
Chris Lattner6e041bd2002-08-21 22:17:09 +0000347 Pass *getAnalysisOrNullUp(const PassInfo *ID) const {
Chris Lattner216edd42002-08-30 20:25:01 +0000348 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000349 if (I != CurrentAnalyses.end())
350 return I->second; // Found it.
351
352 if (Parent) // Try scanning...
353 return Parent->getAnalysisOrNullUp(ID);
354 return 0;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000355 }
356
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000357 // {start/end}Pass - Called when a pass is started, it just propogates
358 // information up to the top level PassManagerT object to tell it that a pass
359 // has started or ended. This is used to gather timing information about
360 // passes.
361 //
362 void startPass(Pass *P) {
363 if (Parent) Parent->startPass(P);
364 else PassStarted(P);
365 }
366 void endPass(Pass *P) {
367 if (Parent) Parent->endPass(P);
368 else PassEnded(P);
369 }
370
Chris Lattnerac3e0602002-01-31 18:32:27 +0000371 // markPassUsed - Inform higher level pass managers (and ourselves)
372 // that these analyses are being used by this pass. This is used to
373 // make sure that analyses are not free'd before we have to use
374 // them...
375 //
Chris Lattner6e041bd2002-08-21 22:17:09 +0000376 void markPassUsed(const PassInfo *P, Pass *User) {
Chris Lattner216edd42002-08-30 20:25:01 +0000377 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(P);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000378
Chris Lattnerac3e0602002-01-31 18:32:27 +0000379 if (I != CurrentAnalyses.end()) {
380 LastUseOf[I->second] = User; // Local pass, extend the lifetime
381 } else {
382 // Pass not in current available set, must be a higher level pass
383 // available to us, propogate to parent pass manager... We tell the
384 // parent that we (the passmanager) are using the analysis so that it
385 // frees the analysis AFTER this pass manager runs.
386 //
Chris Lattneree0788d2002-09-25 21:59:11 +0000387 if (Parent) {
388 Parent->markPassUsed(P, this);
389 } else {
390 assert(0 && "Pass available but not found! "
391 "Perhaps this is a module pass requiring a function pass?");
392 }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000393 }
394 }
Chris Lattneree0788d2002-09-25 21:59:11 +0000395
Chris Lattnerac3e0602002-01-31 18:32:27 +0000396 // Return the number of parent PassManagers that exist
397 virtual unsigned getDepth() const {
398 if (Parent == 0) return 0;
399 return 1 + Parent->getDepth();
400 }
401
Chris Lattner1e4867f2002-07-30 19:51:02 +0000402 virtual unsigned getNumContainedPasses() const { return Passes.size(); }
403 virtual const Pass *getContainedPass(unsigned N) const {
404 assert(N < Passes.size() && "Pass number out of range!");
405 return Passes[N];
406 }
407
Chris Lattner6e041bd2002-08-21 22:17:09 +0000408 // add - Add a pass to the queue of passes to run. This gives ownership of
Chris Lattner67d25652002-01-30 23:20:39 +0000409 // the Pass to the PassManager. When the PassManager is destroyed, the pass
Chris Lattnerac3e0602002-01-31 18:32:27 +0000410 // will be destroyed as well, so there is no need to delete the pass. This
411 // implies that all passes MUST be new'd.
Chris Lattner67d25652002-01-30 23:20:39 +0000412 //
413 void add(PassClass *P) {
414 // Get information about what analyses the pass uses...
Chris Lattnerc8e66542002-04-27 06:56:12 +0000415 AnalysisUsage AnUsage;
416 P->getAnalysisUsage(AnUsage);
417 const std::vector<AnalysisID> &Required = AnUsage.getRequiredSet();
Chris Lattner67d25652002-01-30 23:20:39 +0000418
419 // Loop over all of the analyses used by this pass,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000420 for (std::vector<AnalysisID>::const_iterator I = Required.begin(),
421 E = Required.end(); I != E; ++I) {
Chris Lattner67d25652002-01-30 23:20:39 +0000422 if (getAnalysisOrNullDown(*I) == 0)
Chris Lattner26750072002-07-27 01:12:17 +0000423 add((PassClass*)(*I)->createPass());
Chris Lattner67d25652002-01-30 23:20:39 +0000424 }
425
426 // Tell the pass to add itself to this PassManager... the way it does so
427 // depends on the class of the pass, and is critical to laying out passes in
428 // an optimal order..
429 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000430 P->addToPassManager(this, AnUsage);
Chris Lattner67d25652002-01-30 23:20:39 +0000431 }
432
433private:
434
435 // addPass - These functions are used to implement the subclass specific
436 // behaviors present in PassManager. Basically the add(Pass*) method ends up
437 // reflecting its behavior into a Pass::addToPassManager call. Subclasses of
438 // Pass override it specifically so that they can reflect the type
439 // information inherent in "this" back to the PassManager.
440 //
441 // For generic Pass subclasses (which are interprocedural passes), we simply
442 // add the pass to the end of the pass list and terminate any accumulation of
Chris Lattnerc8e66542002-04-27 06:56:12 +0000443 // FunctionPass's that are present.
Chris Lattner67d25652002-01-30 23:20:39 +0000444 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000445 void addPass(PassClass *P, AnalysisUsage &AnUsage) {
446 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
Chris Lattnerc8e66542002-04-27 06:56:12 +0000447
Chris Lattner73503172002-07-29 21:03:38 +0000448 // FIXME: If this pass being added isn't killed by any of the passes in the
449 // batcher class then we can reorder to pass to execute before the batcher
450 // does, which will potentially allow us to batch more passes!
Chris Lattner67d25652002-01-30 23:20:39 +0000451 //
Chris Lattner73503172002-07-29 21:03:38 +0000452 //const std::vector<AnalysisID> &ProvidedSet = AnUsage.getProvidedSet();
453 if (Batcher /*&& ProvidedSet.empty()*/)
Chris Lattner67d25652002-01-30 23:20:39 +0000454 closeBatcher(); // This pass cannot be batched!
455
456 // Set the Resolver instance variable in the Pass so that it knows where to
457 // find this object...
458 //
459 setAnalysisResolver(P, this);
460 Passes.push_back(P);
461
Chris Lattnerac3e0602002-01-31 18:32:27 +0000462 // Inform higher level pass managers (and ourselves) that these analyses are
463 // being used by this pass. This is used to make sure that analyses are not
464 // free'd before we have to use them...
465 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000466 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
467 E = RequiredSet.end(); I != E; ++I)
Chris Lattnerac3e0602002-01-31 18:32:27 +0000468 markPassUsed(*I, P); // Mark *I as used by P
469
Chris Lattnerc8e66542002-04-27 06:56:12 +0000470 // Erase all analyses not in the preserved set...
471 if (!AnUsage.preservesAll()) {
472 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
473 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(),
Chris Lattneree0788d2002-09-25 21:59:11 +0000474 E = CurrentAnalyses.end(); I != E; ) {
475 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
476 PreservedSet.end()) { // Analysis not preserved!
477 CurrentAnalyses.erase(I); // Remove from available analyses
Chris Lattnerc8e66542002-04-27 06:56:12 +0000478 I = CurrentAnalyses.begin();
Chris Lattneree0788d2002-09-25 21:59:11 +0000479 } else {
480 ++I;
Chris Lattnerc8e66542002-04-27 06:56:12 +0000481 }
Chris Lattneree0788d2002-09-25 21:59:11 +0000482 }
Chris Lattnerc8e66542002-04-27 06:56:12 +0000483 }
Chris Lattner67d25652002-01-30 23:20:39 +0000484
Chris Lattner73503172002-07-29 21:03:38 +0000485 // Add this pass to the currently available set...
Chris Lattner216edd42002-08-30 20:25:01 +0000486 if (const PassInfo *PI = P->getPassInfo()) {
Chris Lattner73503172002-07-29 21:03:38 +0000487 CurrentAnalyses[PI] = P;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000488
Chris Lattner216edd42002-08-30 20:25:01 +0000489 // This pass is the current implementation of all of the interfaces it
490 // implements as well.
491 //
492 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
493 for (unsigned i = 0, e = II.size(); i != e; ++i)
494 CurrentAnalyses[II[i]] = P;
495 }
496
Chris Lattnerac3e0602002-01-31 18:32:27 +0000497 // For now assume that our results are never used...
498 LastUseOf[P] = P;
Chris Lattner67d25652002-01-30 23:20:39 +0000499 }
500
Chris Lattnerc8e66542002-04-27 06:56:12 +0000501 // For FunctionPass subclasses, we must be sure to batch the FunctionPass's
502 // together in a BatcherClass object so that all of the analyses are run
503 // together a function at a time.
Chris Lattner67d25652002-01-30 23:20:39 +0000504 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000505 void addPass(SubPassClass *MP, AnalysisUsage &AnUsage) {
Chris Lattner67d25652002-01-30 23:20:39 +0000506 if (Batcher == 0) // If we don't have a batcher yet, make one now.
507 Batcher = new BatcherClass(this);
Chris Lattner73503172002-07-29 21:03:38 +0000508 // The Batcher will queue the passes up
Chris Lattnerc8e66542002-04-27 06:56:12 +0000509 MP->addToPassManager(Batcher, AnUsage);
Chris Lattner67d25652002-01-30 23:20:39 +0000510 }
511
512 // closeBatcher - Terminate the batcher that is being worked on.
513 void closeBatcher() {
514 if (Batcher) {
515 Passes.push_back(Batcher);
516 Batcher = 0;
517 }
518 }
Chris Lattneree0788d2002-09-25 21:59:11 +0000519
520public:
521 // When an ImmutablePass is added, it gets added to the top level pass
522 // manager.
523 void addPass(ImmutablePass *IP, AnalysisUsage &AU) {
524 if (Parent) { // Make sure this request goes to the top level passmanager...
525 Parent->addPass(IP, AU);
526 return;
527 }
528
529 // Set the Resolver instance variable in the Pass so that it knows where to
530 // find this object...
531 //
532 setAnalysisResolver(IP, this);
533 ImmutablePasses.push_back(IP);
534
535 // Add this pass to the currently available set...
536 if (const PassInfo *PI = IP->getPassInfo()) {
537 CurrentAnalyses[PI] = IP;
538
539 // This pass is the current implementation of all of the interfaces it
540 // implements as well.
541 //
542 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
543 for (unsigned i = 0, e = II.size(); i != e; ++i)
544 CurrentAnalyses[II[i]] = IP;
545 }
546 }
Chris Lattner67d25652002-01-30 23:20:39 +0000547};
548
549
550
551//===----------------------------------------------------------------------===//
552// PassManagerTraits<BasicBlock> Specialization
553//
554// This pass manager is used to group together all of the BasicBlockPass's
555// into a single unit.
556//
557template<> struct PassManagerTraits<BasicBlock> : public BasicBlockPass {
558 // PassClass - The type of passes tracked by this PassManager
559 typedef BasicBlockPass PassClass;
560
561 // SubPassClass - The types of classes that should be collated together
562 // This is impossible to match, so BasicBlock instantiations of PassManagerT
563 // do not collate.
564 //
565 typedef PassManagerT<Module> SubPassClass;
566
567 // BatcherClass - The type to use for collation of subtypes... This class is
568 // never instantiated for the PassManager<BasicBlock>, but it must be an
569 // instance of PassClass to typecheck.
570 //
571 typedef PassClass BatcherClass;
572
573 // ParentClass - The type of the parent PassManager...
Chris Lattner4e8c4872002-03-23 22:51:58 +0000574 typedef PassManagerT<Function> ParentClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000575
Chris Lattner2eaac392002-01-31 00:40:44 +0000576 // PMType - The type of the passmanager that subclasses this class
577 typedef PassManagerT<BasicBlock> PMType;
578
Chris Lattner67d25652002-01-30 23:20:39 +0000579 // runPass - Specify how the pass should be run on the UnitType
580 static bool runPass(PassClass *P, BasicBlock *M) {
581 // todo, init and finalize
Chris Lattner113f4f42002-06-25 16:13:24 +0000582 return P->runOnBasicBlock(*M);
Chris Lattner67d25652002-01-30 23:20:39 +0000583 }
584
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000585 // Dummy implementation of PassStarted/PassEnded
586 static void PassStarted(Pass *P) {}
587 static void PassEnded(Pass *P) {}
588
Chris Lattnerac3e0602002-01-31 18:32:27 +0000589 // getPMName() - Return the name of the unit the PassManager operates on for
590 // debugging.
591 const char *getPMName() const { return "BasicBlock"; }
Chris Lattner37104aa2002-04-29 14:57:45 +0000592 virtual const char *getPassName() const { return "BasicBlock Pass Manager"; }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000593
Chris Lattner2eaac392002-01-31 00:40:44 +0000594 // Implement the BasicBlockPass interface...
Chris Lattner113f4f42002-06-25 16:13:24 +0000595 virtual bool doInitialization(Module &M);
Chris Lattnerbae3c672002-09-12 17:06:40 +0000596 virtual bool doInitialization(Function &F);
Chris Lattner113f4f42002-06-25 16:13:24 +0000597 virtual bool runOnBasicBlock(BasicBlock &BB);
Chris Lattnerbae3c672002-09-12 17:06:40 +0000598 virtual bool doFinalization(Function &F);
Chris Lattner113f4f42002-06-25 16:13:24 +0000599 virtual bool doFinalization(Module &M);
Chris Lattnercb429062002-04-30 18:50:17 +0000600
601 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
602 AU.setPreservesAll();
603 }
Chris Lattner67d25652002-01-30 23:20:39 +0000604};
605
606
607
608//===----------------------------------------------------------------------===//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000609// PassManagerTraits<Function> Specialization
Chris Lattner67d25652002-01-30 23:20:39 +0000610//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000611// This pass manager is used to group together all of the FunctionPass's
Chris Lattner67d25652002-01-30 23:20:39 +0000612// into a single unit.
613//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000614template<> struct PassManagerTraits<Function> : public FunctionPass {
Chris Lattner67d25652002-01-30 23:20:39 +0000615 // PassClass - The type of passes tracked by this PassManager
Chris Lattnerc8e66542002-04-27 06:56:12 +0000616 typedef FunctionPass PassClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000617
618 // SubPassClass - The types of classes that should be collated together
619 typedef BasicBlockPass SubPassClass;
620
621 // BatcherClass - The type to use for collation of subtypes...
622 typedef PassManagerT<BasicBlock> BatcherClass;
623
624 // ParentClass - The type of the parent PassManager...
625 typedef PassManagerT<Module> ParentClass;
626
627 // PMType - The type of the passmanager that subclasses this class
Chris Lattner4e8c4872002-03-23 22:51:58 +0000628 typedef PassManagerT<Function> PMType;
Chris Lattner67d25652002-01-30 23:20:39 +0000629
630 // runPass - Specify how the pass should be run on the UnitType
Chris Lattnerc8e66542002-04-27 06:56:12 +0000631 static bool runPass(PassClass *P, Function *F) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000632 return P->runOnFunction(*F);
Chris Lattner67d25652002-01-30 23:20:39 +0000633 }
634
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000635 // Dummy implementation of PassStarted/PassEnded
636 static void PassStarted(Pass *P) {}
637 static void PassEnded(Pass *P) {}
638
Chris Lattnerac3e0602002-01-31 18:32:27 +0000639 // getPMName() - Return the name of the unit the PassManager operates on for
640 // debugging.
Chris Lattner4e8c4872002-03-23 22:51:58 +0000641 const char *getPMName() const { return "Function"; }
Chris Lattner37104aa2002-04-29 14:57:45 +0000642 virtual const char *getPassName() const { return "Function Pass Manager"; }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000643
Chris Lattnerc8e66542002-04-27 06:56:12 +0000644 // Implement the FunctionPass interface...
Chris Lattner113f4f42002-06-25 16:13:24 +0000645 virtual bool doInitialization(Module &M);
646 virtual bool runOnFunction(Function &F);
647 virtual bool doFinalization(Module &M);
Chris Lattnercb429062002-04-30 18:50:17 +0000648
649 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
650 AU.setPreservesAll();
651 }
Chris Lattner67d25652002-01-30 23:20:39 +0000652};
653
654
655
656//===----------------------------------------------------------------------===//
657// PassManagerTraits<Module> Specialization
658//
659// This is the top level PassManager implementation that holds generic passes.
660//
661template<> struct PassManagerTraits<Module> : public Pass {
662 // PassClass - The type of passes tracked by this PassManager
663 typedef Pass PassClass;
664
665 // SubPassClass - The types of classes that should be collated together
Chris Lattnerc8e66542002-04-27 06:56:12 +0000666 typedef FunctionPass SubPassClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000667
668 // BatcherClass - The type to use for collation of subtypes...
Chris Lattner4e8c4872002-03-23 22:51:58 +0000669 typedef PassManagerT<Function> BatcherClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000670
671 // ParentClass - The type of the parent PassManager...
Chris Lattnerac3e0602002-01-31 18:32:27 +0000672 typedef AnalysisResolver ParentClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000673
674 // runPass - Specify how the pass should be run on the UnitType
Chris Lattner113f4f42002-06-25 16:13:24 +0000675 static bool runPass(PassClass *P, Module *M) { return P->run(*M); }
Chris Lattner67d25652002-01-30 23:20:39 +0000676
Chris Lattnerac3e0602002-01-31 18:32:27 +0000677 // getPMName() - Return the name of the unit the PassManager operates on for
678 // debugging.
679 const char *getPMName() const { return "Module"; }
Chris Lattner37104aa2002-04-29 14:57:45 +0000680 virtual const char *getPassName() const { return "Module Pass Manager"; }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000681
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000682 // TimingInformation - This data member maintains timing information for each
683 // of the passes that is executed.
684 //
685 TimingInfo *TimeInfo;
686
687 // PassStarted/Ended - This callback is notified any time a pass is started
688 // or stops. This is used to collect timing information about the different
689 // passes being executed.
690 //
691 void PassStarted(Pass *P) {
692 if (TimeInfo) TimeInfo->passStarted(P);
Chris Lattner67d25652002-01-30 23:20:39 +0000693 }
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000694 void PassEnded(Pass *P) {
695 if (TimeInfo) TimeInfo->passEnded(P);
696 }
697
698 // run - Implement the PassManager interface...
Chris Lattner113f4f42002-06-25 16:13:24 +0000699 bool run(Module &M) {
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000700 TimeInfo = TimingInfo::create();
Chris Lattner113f4f42002-06-25 16:13:24 +0000701 bool Result = ((PassManagerT<Module>*)this)->runOnUnit(&M);
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000702 if (TimeInfo) {
703 delete TimeInfo;
704 TimeInfo = 0;
705 }
706 return Result;
707 }
708
709 // PassManagerTraits constructor - Create a timing info object if the user
710 // specified timing info should be collected on the command line.
711 //
712 PassManagerTraits() : TimeInfo(0) {}
Chris Lattner67d25652002-01-30 23:20:39 +0000713};
714
715
716
717//===----------------------------------------------------------------------===//
718// PassManagerTraits Method Implementations
719//
720
721// PassManagerTraits<BasicBlock> Implementations
722//
Chris Lattner113f4f42002-06-25 16:13:24 +0000723inline bool PassManagerTraits<BasicBlock>::doInitialization(Module &M) {
Chris Lattner2eaac392002-01-31 00:40:44 +0000724 bool Changed = false;
725 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
726 ((PMType*)this)->Passes[i]->doInitialization(M);
727 return Changed;
728}
729
Chris Lattnerbae3c672002-09-12 17:06:40 +0000730inline bool PassManagerTraits<BasicBlock>::doInitialization(Function &F) {
731 bool Changed = false;
732 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
733 ((PMType*)this)->Passes[i]->doInitialization(F);
734 return Changed;
735}
736
Chris Lattner113f4f42002-06-25 16:13:24 +0000737inline bool PassManagerTraits<BasicBlock>::runOnBasicBlock(BasicBlock &BB) {
738 return ((PMType*)this)->runOnUnit(&BB);
Chris Lattner2eaac392002-01-31 00:40:44 +0000739}
740
Chris Lattnerbae3c672002-09-12 17:06:40 +0000741inline bool PassManagerTraits<BasicBlock>::doFinalization(Function &F) {
742 bool Changed = false;
743 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
744 ((PMType*)this)->Passes[i]->doFinalization(F);
745 return Changed;
746}
747
Chris Lattner113f4f42002-06-25 16:13:24 +0000748inline bool PassManagerTraits<BasicBlock>::doFinalization(Module &M) {
Chris Lattner2eaac392002-01-31 00:40:44 +0000749 bool Changed = false;
750 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
751 ((PMType*)this)->Passes[i]->doFinalization(M);
752 return Changed;
Chris Lattner67d25652002-01-30 23:20:39 +0000753}
754
755
Chris Lattner4e8c4872002-03-23 22:51:58 +0000756// PassManagerTraits<Function> Implementations
Chris Lattner67d25652002-01-30 23:20:39 +0000757//
Chris Lattner113f4f42002-06-25 16:13:24 +0000758inline bool PassManagerTraits<Function>::doInitialization(Module &M) {
Chris Lattner67d25652002-01-30 23:20:39 +0000759 bool Changed = false;
760 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
761 ((PMType*)this)->Passes[i]->doInitialization(M);
762 return Changed;
763}
764
Chris Lattner113f4f42002-06-25 16:13:24 +0000765inline bool PassManagerTraits<Function>::runOnFunction(Function &F) {
766 return ((PMType*)this)->runOnUnit(&F);
Chris Lattner67d25652002-01-30 23:20:39 +0000767}
768
Chris Lattner113f4f42002-06-25 16:13:24 +0000769inline bool PassManagerTraits<Function>::doFinalization(Module &M) {
Chris Lattner67d25652002-01-30 23:20:39 +0000770 bool Changed = false;
771 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
772 ((PMType*)this)->Passes[i]->doFinalization(M);
773 return Changed;
774}
775
776#endif