blob: 55beaa457a482b52f8217290bf3ba6331f02503a [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 Lattneraf751b82002-10-01 19:54:07 +000021#include "Support/Timer.h"
Chris Lattnerc8e66542002-04-27 06:56:12 +000022#include <algorithm>
Chris Lattner395c27a2002-07-31 18:04:17 +000023#include <iostream>
Chris Lattnera454b5b2002-04-28 05:14:06 +000024class Annotable;
Chris Lattner67d25652002-01-30 23:20:39 +000025
Chris Lattner67d25652002-01-30 23:20:39 +000026//===----------------------------------------------------------------------===//
Chris Lattner198cf422002-07-30 16:27:02 +000027// Pass debugging information. Often it is useful to find out what pass is
28// running when a crash occurs in a utility. When this library is compiled with
29// debugging on, a command line option (--debug-pass) is enabled that causes the
30// pass name to be printed before it executes.
31//
32
33// Different debug levels that can be enabled...
34enum PassDebugLevel {
Chris Lattner1e4867f2002-07-30 19:51:02 +000035 None, Arguments, Structure, Executions, Details
Chris Lattner198cf422002-07-30 16:27:02 +000036};
37
38static cl::opt<enum PassDebugLevel>
39PassDebugging("debug-pass", cl::Hidden,
40 cl::desc("Print PassManager debugging information"),
41 cl::values(
42 clEnumVal(None , "disable debug output"),
Chris Lattner1e4867f2002-07-30 19:51:02 +000043 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
Chris Lattner198cf422002-07-30 16:27:02 +000044 clEnumVal(Structure , "print pass structure before run()"),
45 clEnumVal(Executions, "print pass name before it is executed"),
46 clEnumVal(Details , "print pass details when it is executed"),
47 0));
48
49//===----------------------------------------------------------------------===//
Chris Lattner57d2ba32002-04-04 19:35:24 +000050// PMDebug class - a set of debugging functions, that are not to be
51// instantiated by the template.
Chris Lattner67d25652002-01-30 23:20:39 +000052//
53struct PMDebug {
Chris Lattner1e4867f2002-07-30 19:51:02 +000054 static void PerformPassStartupStuff(Pass *P) {
55 // If debugging is enabled, print out argument information...
56 if (PassDebugging >= Arguments) {
57 std::cerr << "Pass Arguments: ";
58 PrintArgumentInformation(P);
59 std::cerr << "\n";
60
61 // Print the pass execution structure
62 if (PassDebugging >= Structure)
63 P->dumpPassStructure();
64 }
Chris Lattner198cf422002-07-30 16:27:02 +000065 }
Chris Lattner1e4867f2002-07-30 19:51:02 +000066
67 static void PrintArgumentInformation(const Pass *P);
Chris Lattnera454b5b2002-04-28 05:14:06 +000068 static void PrintPassInformation(unsigned,const char*,Pass *, Annotable *);
Chris Lattnerac3e0602002-01-31 18:32:27 +000069 static void PrintAnalysisSetInfo(unsigned,const char*,Pass *P,
Chris Lattnerc8e66542002-04-27 06:56:12 +000070 const std::vector<AnalysisID> &);
Chris Lattner67d25652002-01-30 23:20:39 +000071};
72
73
Chris Lattnere2eb99e2002-04-29 04:04:29 +000074//===----------------------------------------------------------------------===//
75// TimingInfo Class - This class is used to calculate information about the
76// amount of time each pass takes to execute. This only happens when
77// -time-passes is enabled on the command line.
78//
Chris Lattner6a33d6f2002-08-01 19:33:09 +000079
Chris Lattnere2eb99e2002-04-29 04:04:29 +000080class TimingInfo {
Chris Lattneraf751b82002-10-01 19:54:07 +000081 std::map<Pass*, Timer> TimingData;
82 TimerGroup TG;
83
84 // Private ctor, must use 'create' member
85 TimingInfo() : TG("... Pass execution timing report ...") {}
Chris Lattnere2eb99e2002-04-29 04:04:29 +000086public:
87 // Create method. If Timing is enabled, this creates and returns a new timing
88 // object, otherwise it returns null.
89 //
90 static TimingInfo *create();
91
92 // TimingDtor - Print out information about timing information
Chris Lattneraf751b82002-10-01 19:54:07 +000093 ~TimingInfo() {
94 // Delete all of the timers...
95 TimingData.clear();
96 // TimerGroup is deleted next, printing the report.
97 }
Chris Lattnere2eb99e2002-04-29 04:04:29 +000098
Chris Lattneraf751b82002-10-01 19:54:07 +000099 void passStarted(Pass *P) {
Chris Lattnerd5fc9022002-10-01 20:08:11 +0000100 if (dynamic_cast<AnalysisResolver*>(P)) return;
Chris Lattneraf751b82002-10-01 19:54:07 +0000101 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
102 if (I == TimingData.end())
Chris Lattner52db2712002-10-01 20:12:06 +0000103 I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
Chris Lattneraf751b82002-10-01 19:54:07 +0000104 I->second.startTimer();
105 }
106 void passEnded(Pass *P) {
Chris Lattnerd5fc9022002-10-01 20:08:11 +0000107 if (dynamic_cast<AnalysisResolver*>(P)) return;
Chris Lattneraf751b82002-10-01 19:54:07 +0000108 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
109 assert (I != TimingData.end() && "passStarted/passEnded not nested right!");
110 I->second.stopTimer();
111 }
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000112};
113
Chris Lattner6e041bd2002-08-21 22:17:09 +0000114//===----------------------------------------------------------------------===//
Chris Lattner67d25652002-01-30 23:20:39 +0000115// Declare the PassManagerTraits which will be specialized...
116//
117template<class UnitType> class PassManagerTraits; // Do not define.
118
119
120//===----------------------------------------------------------------------===//
121// PassManagerT - Container object for passes. The PassManagerT destructor
122// deletes all passes contained inside of the PassManagerT, so you shouldn't
123// delete passes manually, and all passes should be dynamically allocated.
124//
125template<typename UnitType>
126class PassManagerT : public PassManagerTraits<UnitType>,public AnalysisResolver{
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000127 typedef PassManagerTraits<UnitType> Traits;
128 typedef typename Traits::PassClass PassClass;
129 typedef typename Traits::SubPassClass SubPassClass;
130 typedef typename Traits::BatcherClass BatcherClass;
131 typedef typename Traits::ParentClass ParentClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000132
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000133 friend typename Traits::PassClass;
134 friend typename Traits::SubPassClass;
135 friend class Traits;
Chris Lattneree0788d2002-09-25 21:59:11 +0000136 friend class ImmutablePass;
Chris Lattner67d25652002-01-30 23:20:39 +0000137
Chris Lattner73503172002-07-29 21:03:38 +0000138 std::vector<PassClass*> Passes; // List of passes to run
Chris Lattneree0788d2002-09-25 21:59:11 +0000139 std::vector<ImmutablePass*> ImmutablePasses; // List of immutable passes
Chris Lattner67d25652002-01-30 23:20:39 +0000140
141 // The parent of this pass manager...
Chris Lattnerac3e0602002-01-31 18:32:27 +0000142 ParentClass * const Parent;
Chris Lattner67d25652002-01-30 23:20:39 +0000143
144 // The current batcher if one is in use, or null
145 BatcherClass *Batcher;
146
147 // CurrentAnalyses - As the passes are being run, this map contains the
148 // analyses that are available to the current pass for use. This is accessed
149 // through the getAnalysis() function in this class and in Pass.
150 //
151 std::map<AnalysisID, Pass*> CurrentAnalyses;
152
Chris Lattnerac3e0602002-01-31 18:32:27 +0000153 // LastUseOf - This map keeps track of the last usage in our pipeline of a
154 // particular pass. When executing passes, the memory for .first is free'd
155 // after .second is run.
156 //
157 std::map<Pass*, Pass*> LastUseOf;
158
Chris Lattner67d25652002-01-30 23:20:39 +0000159public:
160 PassManagerT(ParentClass *Par = 0) : Parent(Par), Batcher(0) {}
161 ~PassManagerT() {
162 // Delete all of the contained passes...
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000163 for (typename std::vector<PassClass*>::iterator
164 I = Passes.begin(), E = Passes.end(); I != E; ++I)
Chris Lattner67d25652002-01-30 23:20:39 +0000165 delete *I;
Chris Lattneree0788d2002-09-25 21:59:11 +0000166
167 for (std::vector<ImmutablePass*>::iterator
168 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
169 delete *I;
Chris Lattner67d25652002-01-30 23:20:39 +0000170 }
171
172 // run - Run all of the queued passes on the specified module in an optimal
173 // way.
174 virtual bool runOnUnit(UnitType *M) {
175 bool MadeChanges = false;
176 closeBatcher();
177 CurrentAnalyses.clear();
178
Chris Lattneree0788d2002-09-25 21:59:11 +0000179 // Add any immutable passes to the CurrentAnalyses set...
Chris Lattner480b37d2002-09-25 22:26:52 +0000180 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
181 ImmutablePass *IPass = ImmutablePasses[i];
182 if (const PassInfo *PI = IPass->getPassInfo()) {
183 CurrentAnalyses[PI] = IPass;
Chris Lattneree0788d2002-09-25 21:59:11 +0000184
185 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
186 for (unsigned i = 0, e = II.size(); i != e; ++i)
Chris Lattner480b37d2002-09-25 22:26:52 +0000187 CurrentAnalyses[II[i]] = IPass;
Chris Lattneree0788d2002-09-25 21:59:11 +0000188 }
Chris Lattner480b37d2002-09-25 22:26:52 +0000189 }
Chris Lattneree0788d2002-09-25 21:59:11 +0000190
Chris Lattnerac3e0602002-01-31 18:32:27 +0000191 // LastUserOf - This contains the inverted LastUseOfMap...
192 std::map<Pass *, std::vector<Pass*> > LastUserOf;
193 for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(),
194 E = LastUseOf.end(); I != E; ++I)
195 LastUserOf[I->second].push_back(I->first);
196
197
Chris Lattner67d25652002-01-30 23:20:39 +0000198 // Output debug information...
Chris Lattner1e4867f2002-07-30 19:51:02 +0000199 if (Parent == 0) PMDebug::PerformPassStartupStuff(this);
Chris Lattner67d25652002-01-30 23:20:39 +0000200
201 // Run all of the passes
202 for (unsigned i = 0, e = Passes.size(); i < e; ++i) {
203 PassClass *P = Passes[i];
204
Chris Lattnera454b5b2002-04-28 05:14:06 +0000205 PMDebug::PrintPassInformation(getDepth(), "Executing Pass", P,
206 (Annotable*)M);
Chris Lattner67d25652002-01-30 23:20:39 +0000207
208 // Get information about what analyses the pass uses...
Chris Lattnerc8e66542002-04-27 06:56:12 +0000209 AnalysisUsage AnUsage;
210 P->getAnalysisUsage(AnUsage);
211 PMDebug::PrintAnalysisSetInfo(getDepth(), "Required", P,
212 AnUsage.getRequiredSet());
Chris Lattner67d25652002-01-30 23:20:39 +0000213
Chris Lattner216edd42002-08-30 20:25:01 +0000214 // All Required analyses should be available to the pass as it runs! Here
215 // we fill in the AnalysisImpls member of the pass so that it can
216 // successfully use the getAnalysis() method to retrieve the
217 // implementations it needs.
218 //
219 P->AnalysisImpls.clear();
220 P->AnalysisImpls.reserve(AnUsage.getRequiredSet().size());
221 for (std::vector<const PassInfo *>::const_iterator
Chris Lattnerc8e66542002-04-27 06:56:12 +0000222 I = AnUsage.getRequiredSet().begin(),
223 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Chris Lattner216edd42002-08-30 20:25:01 +0000224 Pass *Impl = getAnalysisOrNullUp(*I);
225 if (Impl == 0) {
226 std::cerr << "Analysis '" << (*I)->getPassName()
227 << "' used but not available!";
228 assert(0 && "Analysis used but not available!");
229 } else if (PassDebugging == Details) {
230 if ((*I)->getPassName() != std::string(Impl->getPassName()))
231 std::cerr << " Interface '" << (*I)->getPassName()
232 << "' implemented by '" << Impl->getPassName() << "'\n";
233 }
234 P->AnalysisImpls.push_back(std::make_pair(*I, Impl));
Chris Lattner67d25652002-01-30 23:20:39 +0000235 }
Chris Lattner67d25652002-01-30 23:20:39 +0000236
237 // Run the sub pass!
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000238 startPass(P);
239 bool Changed = runPass(P, M);
240 endPass(P);
Chris Lattnerf2f31bd2002-02-01 04:53:36 +0000241 MadeChanges |= Changed;
Chris Lattner67d25652002-01-30 23:20:39 +0000242
Chris Lattnered6504b2002-09-08 19:00:07 +0000243 // Check for memory leaks by the pass...
244 LeakDetector::checkForGarbage(std::string("after running pass '") +
245 P->getPassName() + "'");
246
Chris Lattnerf2f31bd2002-02-01 04:53:36 +0000247 if (Changed)
248 PMDebug::PrintPassInformation(getDepth()+1, "Made Modification", P,
Chris Lattnera454b5b2002-04-28 05:14:06 +0000249 (Annotable*)M);
Chris Lattnerc8e66542002-04-27 06:56:12 +0000250 PMDebug::PrintAnalysisSetInfo(getDepth(), "Preserved", P,
251 AnUsage.getPreservedSet());
Chris Lattner67d25652002-01-30 23:20:39 +0000252
Chris Lattnerc8e66542002-04-27 06:56:12 +0000253
254 // Erase all analyses not in the preserved set...
Chris Lattner820d9712002-10-21 20:00:28 +0000255 if (!AnUsage.getPreservesAll()) {
Chris Lattnerc8e66542002-04-27 06:56:12 +0000256 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
257 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(),
258 E = CurrentAnalyses.end(); I != E; )
259 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) !=
260 PreservedSet.end())
261 ++I; // This analysis is preserved, leave it in the available set...
262 else {
Chris Lattneree0788d2002-09-25 21:59:11 +0000263 if (!dynamic_cast<ImmutablePass*>(I->second)) {
Chris Lattnerade85ec2003-02-14 05:34:36 +0000264 std::map<AnalysisID, Pass*>::iterator J = I++;
265 CurrentAnalyses.erase(J); // Analysis not preserved!
Chris Lattneree0788d2002-09-25 21:59:11 +0000266 } else {
267 ++I;
268 }
Chris Lattnerc8e66542002-04-27 06:56:12 +0000269 }
270 }
271
Chris Lattner73503172002-07-29 21:03:38 +0000272 // Add the current pass to the set of passes that have been run, and are
273 // thus available to users.
274 //
Chris Lattner216edd42002-08-30 20:25:01 +0000275 if (const PassInfo *PI = P->getPassInfo()) {
Chris Lattner73503172002-07-29 21:03:38 +0000276 CurrentAnalyses[PI] = P;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000277
Chris Lattner216edd42002-08-30 20:25:01 +0000278 // This pass is the current implementation of all of the interfaces it
279 // implements as well.
280 //
281 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
282 for (unsigned i = 0, e = II.size(); i != e; ++i)
283 CurrentAnalyses[II[i]] = P;
284 }
285
Chris Lattnerac3e0602002-01-31 18:32:27 +0000286 // Free memory for any passes that we are the last use of...
287 std::vector<Pass*> &DeadPass = LastUserOf[P];
288 for (std::vector<Pass*>::iterator I = DeadPass.begin(),E = DeadPass.end();
289 I != E; ++I) {
290 PMDebug::PrintPassInformation(getDepth()+1, "Freeing Pass", *I,
Chris Lattnera454b5b2002-04-28 05:14:06 +0000291 (Annotable*)M);
Chris Lattnerac3e0602002-01-31 18:32:27 +0000292 (*I)->releaseMemory();
293 }
Chris Lattner6b6f5402002-09-29 22:50:22 +0000294
295 // Make sure to remove dead passes from the CurrentAnalyses list...
296 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin();
297 I != CurrentAnalyses.end(); ) {
298 std::vector<Pass*>::iterator DPI = std::find(DeadPass.begin(),
299 DeadPass.end(), I->second);
300 if (DPI != DeadPass.end()) { // This pass is dead now... remove it
301 std::map<AnalysisID, Pass*>::iterator IDead = I++;
302 CurrentAnalyses.erase(IDead);
303 } else {
304 ++I; // Move on to the next element...
305 }
306 }
Chris Lattner67d25652002-01-30 23:20:39 +0000307 }
308 return MadeChanges;
309 }
310
Chris Lattnerac3e0602002-01-31 18:32:27 +0000311 // dumpPassStructure - Implement the -debug-passes=PassStructure option
312 virtual void dumpPassStructure(unsigned Offset = 0) {
Chris Lattner480b37d2002-09-25 22:26:52 +0000313 // Print out the immutable passes...
314 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i)
315 ImmutablePasses[i]->dumpPassStructure(0);
316
Chris Lattnerac3e0602002-01-31 18:32:27 +0000317 std::cerr << std::string(Offset*2, ' ') << Traits::getPMName()
318 << " Pass Manager\n";
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000319 for (typename std::vector<PassClass*>::iterator
320 I = Passes.begin(), E = Passes.end(); I != E; ++I) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000321 PassClass *P = *I;
322 P->dumpPassStructure(Offset+1);
323
324 // Loop through and see which classes are destroyed after this one...
325 for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(),
326 E = LastUseOf.end(); I != E; ++I) {
327 if (P == I->second) {
Chris Lattner73503172002-07-29 21:03:38 +0000328 std::cerr << "--" << std::string(Offset*2, ' ');
Chris Lattnerac3e0602002-01-31 18:32:27 +0000329 I->first->dumpPassStructure(0);
330 }
331 }
332 }
333 }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000334
Chris Lattner60430422003-04-24 20:07:38 +0000335 Pass *getImmutablePassOrNull(const PassInfo *ID) const {
336 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
337 const PassInfo *IPID = ImmutablePasses[i]->getPassInfo();
338 if (IPID == ID)
339 return ImmutablePasses[i];
340
341 // This pass is the current implementation of all of the interfaces it
342 // implements as well.
343 //
344 const std::vector<const PassInfo*> &II =
345 IPID->getInterfacesImplemented();
346 for (unsigned j = 0, e = II.size(); j != e; ++j)
347 if (II[j] == ID) return ImmutablePasses[i];
348 }
349 return 0;
350 }
351
Chris Lattner6e041bd2002-08-21 22:17:09 +0000352 Pass *getAnalysisOrNullDown(const PassInfo *ID) const {
Chris Lattner216edd42002-08-30 20:25:01 +0000353 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000354
355 if (I != CurrentAnalyses.end())
356 return I->second; // Found it.
357
Chris Lattner60430422003-04-24 20:07:38 +0000358 if (Pass *P = getImmutablePassOrNull(ID))
359 return P;
360
Chris Lattner6e041bd2002-08-21 22:17:09 +0000361 if (Batcher)
362 return ((AnalysisResolver*)Batcher)->getAnalysisOrNullDown(ID);
363 return 0;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000364 }
365
Chris Lattner6e041bd2002-08-21 22:17:09 +0000366 Pass *getAnalysisOrNullUp(const PassInfo *ID) const {
Chris Lattner216edd42002-08-30 20:25:01 +0000367 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000368 if (I != CurrentAnalyses.end())
369 return I->second; // Found it.
370
371 if (Parent) // Try scanning...
372 return Parent->getAnalysisOrNullUp(ID);
Chris Lattner60430422003-04-24 20:07:38 +0000373 else if (!ImmutablePasses.empty())
374 return getImmutablePassOrNull(ID);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000375 return 0;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000376 }
377
Misha Brukman632df282002-10-29 23:06:16 +0000378 // {start/end}Pass - Called when a pass is started, it just propagates
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000379 // information up to the top level PassManagerT object to tell it that a pass
380 // has started or ended. This is used to gather timing information about
381 // passes.
382 //
383 void startPass(Pass *P) {
384 if (Parent) Parent->startPass(P);
385 else PassStarted(P);
386 }
387 void endPass(Pass *P) {
388 if (Parent) Parent->endPass(P);
389 else PassEnded(P);
390 }
391
Chris Lattnerac3e0602002-01-31 18:32:27 +0000392 // markPassUsed - Inform higher level pass managers (and ourselves)
393 // that these analyses are being used by this pass. This is used to
394 // make sure that analyses are not free'd before we have to use
395 // them...
396 //
Chris Lattner6e041bd2002-08-21 22:17:09 +0000397 void markPassUsed(const PassInfo *P, Pass *User) {
Chris Lattner216edd42002-08-30 20:25:01 +0000398 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(P);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000399
Chris Lattnerac3e0602002-01-31 18:32:27 +0000400 if (I != CurrentAnalyses.end()) {
401 LastUseOf[I->second] = User; // Local pass, extend the lifetime
402 } else {
403 // Pass not in current available set, must be a higher level pass
Misha Brukman632df282002-10-29 23:06:16 +0000404 // available to us, propagate to parent pass manager... We tell the
Chris Lattnerac3e0602002-01-31 18:32:27 +0000405 // parent that we (the passmanager) are using the analysis so that it
406 // frees the analysis AFTER this pass manager runs.
407 //
Chris Lattneree0788d2002-09-25 21:59:11 +0000408 if (Parent) {
409 Parent->markPassUsed(P, this);
410 } else {
Chris Lattner60430422003-04-24 20:07:38 +0000411 assert(getAnalysisOrNullUp(P) &&
412 dynamic_cast<ImmutablePass*>(getAnalysisOrNullUp(P)) &&
413 "Pass available but not found! "
Chris Lattneree0788d2002-09-25 21:59:11 +0000414 "Perhaps this is a module pass requiring a function pass?");
415 }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000416 }
417 }
Chris Lattneree0788d2002-09-25 21:59:11 +0000418
Chris Lattnerac3e0602002-01-31 18:32:27 +0000419 // Return the number of parent PassManagers that exist
420 virtual unsigned getDepth() const {
421 if (Parent == 0) return 0;
422 return 1 + Parent->getDepth();
423 }
424
Chris Lattner1e4867f2002-07-30 19:51:02 +0000425 virtual unsigned getNumContainedPasses() const { return Passes.size(); }
426 virtual const Pass *getContainedPass(unsigned N) const {
427 assert(N < Passes.size() && "Pass number out of range!");
428 return Passes[N];
429 }
430
Chris Lattner6e041bd2002-08-21 22:17:09 +0000431 // add - Add a pass to the queue of passes to run. This gives ownership of
Chris Lattner67d25652002-01-30 23:20:39 +0000432 // the Pass to the PassManager. When the PassManager is destroyed, the pass
Chris Lattnerac3e0602002-01-31 18:32:27 +0000433 // will be destroyed as well, so there is no need to delete the pass. This
434 // implies that all passes MUST be new'd.
Chris Lattner67d25652002-01-30 23:20:39 +0000435 //
436 void add(PassClass *P) {
437 // Get information about what analyses the pass uses...
Chris Lattnerc8e66542002-04-27 06:56:12 +0000438 AnalysisUsage AnUsage;
439 P->getAnalysisUsage(AnUsage);
440 const std::vector<AnalysisID> &Required = AnUsage.getRequiredSet();
Chris Lattner67d25652002-01-30 23:20:39 +0000441
442 // Loop over all of the analyses used by this pass,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000443 for (std::vector<AnalysisID>::const_iterator I = Required.begin(),
444 E = Required.end(); I != E; ++I) {
Chris Lattner67d25652002-01-30 23:20:39 +0000445 if (getAnalysisOrNullDown(*I) == 0)
Chris Lattner26750072002-07-27 01:12:17 +0000446 add((PassClass*)(*I)->createPass());
Chris Lattner67d25652002-01-30 23:20:39 +0000447 }
448
449 // Tell the pass to add itself to this PassManager... the way it does so
450 // depends on the class of the pass, and is critical to laying out passes in
451 // an optimal order..
452 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000453 P->addToPassManager(this, AnUsage);
Chris Lattner67d25652002-01-30 23:20:39 +0000454 }
455
456private:
457
458 // addPass - These functions are used to implement the subclass specific
459 // behaviors present in PassManager. Basically the add(Pass*) method ends up
460 // reflecting its behavior into a Pass::addToPassManager call. Subclasses of
461 // Pass override it specifically so that they can reflect the type
462 // information inherent in "this" back to the PassManager.
463 //
464 // For generic Pass subclasses (which are interprocedural passes), we simply
465 // add the pass to the end of the pass list and terminate any accumulation of
Chris Lattnerc8e66542002-04-27 06:56:12 +0000466 // FunctionPass's that are present.
Chris Lattner67d25652002-01-30 23:20:39 +0000467 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000468 void addPass(PassClass *P, AnalysisUsage &AnUsage) {
469 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
Chris Lattnerc8e66542002-04-27 06:56:12 +0000470
Chris Lattner73503172002-07-29 21:03:38 +0000471 // FIXME: If this pass being added isn't killed by any of the passes in the
472 // batcher class then we can reorder to pass to execute before the batcher
473 // does, which will potentially allow us to batch more passes!
Chris Lattner67d25652002-01-30 23:20:39 +0000474 //
Chris Lattner73503172002-07-29 21:03:38 +0000475 //const std::vector<AnalysisID> &ProvidedSet = AnUsage.getProvidedSet();
476 if (Batcher /*&& ProvidedSet.empty()*/)
Chris Lattner67d25652002-01-30 23:20:39 +0000477 closeBatcher(); // This pass cannot be batched!
478
479 // Set the Resolver instance variable in the Pass so that it knows where to
480 // find this object...
481 //
482 setAnalysisResolver(P, this);
483 Passes.push_back(P);
484
Chris Lattnerac3e0602002-01-31 18:32:27 +0000485 // Inform higher level pass managers (and ourselves) that these analyses are
486 // being used by this pass. This is used to make sure that analyses are not
487 // free'd before we have to use them...
488 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000489 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
490 E = RequiredSet.end(); I != E; ++I)
Chris Lattnerac3e0602002-01-31 18:32:27 +0000491 markPassUsed(*I, P); // Mark *I as used by P
492
Chris Lattnerc8e66542002-04-27 06:56:12 +0000493 // Erase all analyses not in the preserved set...
Chris Lattner820d9712002-10-21 20:00:28 +0000494 if (!AnUsage.getPreservesAll()) {
Chris Lattnerc8e66542002-04-27 06:56:12 +0000495 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
496 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(),
Chris Lattneree0788d2002-09-25 21:59:11 +0000497 E = CurrentAnalyses.end(); I != E; ) {
498 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
499 PreservedSet.end()) { // Analysis not preserved!
500 CurrentAnalyses.erase(I); // Remove from available analyses
Chris Lattnerc8e66542002-04-27 06:56:12 +0000501 I = CurrentAnalyses.begin();
Chris Lattneree0788d2002-09-25 21:59:11 +0000502 } else {
503 ++I;
Chris Lattnerc8e66542002-04-27 06:56:12 +0000504 }
Chris Lattneree0788d2002-09-25 21:59:11 +0000505 }
Chris Lattnerc8e66542002-04-27 06:56:12 +0000506 }
Chris Lattner67d25652002-01-30 23:20:39 +0000507
Chris Lattner73503172002-07-29 21:03:38 +0000508 // Add this pass to the currently available set...
Chris Lattner216edd42002-08-30 20:25:01 +0000509 if (const PassInfo *PI = P->getPassInfo()) {
Chris Lattner73503172002-07-29 21:03:38 +0000510 CurrentAnalyses[PI] = P;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000511
Chris Lattner216edd42002-08-30 20:25:01 +0000512 // This pass is the current implementation of all of the interfaces it
513 // implements as well.
514 //
515 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
516 for (unsigned i = 0, e = II.size(); i != e; ++i)
517 CurrentAnalyses[II[i]] = P;
518 }
519
Chris Lattnerac3e0602002-01-31 18:32:27 +0000520 // For now assume that our results are never used...
521 LastUseOf[P] = P;
Chris Lattner67d25652002-01-30 23:20:39 +0000522 }
523
Chris Lattnerc8e66542002-04-27 06:56:12 +0000524 // For FunctionPass subclasses, we must be sure to batch the FunctionPass's
525 // together in a BatcherClass object so that all of the analyses are run
526 // together a function at a time.
Chris Lattner67d25652002-01-30 23:20:39 +0000527 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000528 void addPass(SubPassClass *MP, AnalysisUsage &AnUsage) {
Chris Lattner67d25652002-01-30 23:20:39 +0000529 if (Batcher == 0) // If we don't have a batcher yet, make one now.
530 Batcher = new BatcherClass(this);
Chris Lattner73503172002-07-29 21:03:38 +0000531 // The Batcher will queue the passes up
Chris Lattnerc8e66542002-04-27 06:56:12 +0000532 MP->addToPassManager(Batcher, AnUsage);
Chris Lattner67d25652002-01-30 23:20:39 +0000533 }
534
535 // closeBatcher - Terminate the batcher that is being worked on.
536 void closeBatcher() {
537 if (Batcher) {
538 Passes.push_back(Batcher);
539 Batcher = 0;
540 }
541 }
Chris Lattneree0788d2002-09-25 21:59:11 +0000542
543public:
544 // When an ImmutablePass is added, it gets added to the top level pass
545 // manager.
546 void addPass(ImmutablePass *IP, AnalysisUsage &AU) {
547 if (Parent) { // Make sure this request goes to the top level passmanager...
548 Parent->addPass(IP, AU);
549 return;
550 }
551
552 // Set the Resolver instance variable in the Pass so that it knows where to
553 // find this object...
554 //
555 setAnalysisResolver(IP, this);
556 ImmutablePasses.push_back(IP);
Chris Lattnerd4ed5c62003-02-26 19:10:57 +0000557
558 // All Required analyses should be available to the pass as it initializes!
559 // Here we fill in the AnalysisImpls member of the pass so that it can
560 // successfully use the getAnalysis() method to retrieve the implementations
561 // it needs.
562 //
563 IP->AnalysisImpls.clear();
564 IP->AnalysisImpls.reserve(AU.getRequiredSet().size());
565 for (std::vector<const PassInfo *>::const_iterator
566 I = AU.getRequiredSet().begin(),
567 E = AU.getRequiredSet().end(); I != E; ++I) {
568 Pass *Impl = getAnalysisOrNullUp(*I);
569 if (Impl == 0) {
570 std::cerr << "Analysis '" << (*I)->getPassName()
571 << "' used but not available!";
572 assert(0 && "Analysis used but not available!");
573 } else if (PassDebugging == Details) {
574 if ((*I)->getPassName() != std::string(Impl->getPassName()))
575 std::cerr << " Interface '" << (*I)->getPassName()
576 << "' implemented by '" << Impl->getPassName() << "'\n";
577 }
578 IP->AnalysisImpls.push_back(std::make_pair(*I, Impl));
579 }
Chris Lattneree0788d2002-09-25 21:59:11 +0000580
Chris Lattnerd4ed5c62003-02-26 19:10:57 +0000581 // Initialize the immutable pass...
582 IP->initializePass();
Chris Lattneree0788d2002-09-25 21:59:11 +0000583 }
Chris Lattner67d25652002-01-30 23:20:39 +0000584};
585
586
587
588//===----------------------------------------------------------------------===//
589// PassManagerTraits<BasicBlock> Specialization
590//
591// This pass manager is used to group together all of the BasicBlockPass's
592// into a single unit.
593//
594template<> struct PassManagerTraits<BasicBlock> : public BasicBlockPass {
595 // PassClass - The type of passes tracked by this PassManager
596 typedef BasicBlockPass PassClass;
597
598 // SubPassClass - The types of classes that should be collated together
599 // This is impossible to match, so BasicBlock instantiations of PassManagerT
600 // do not collate.
601 //
602 typedef PassManagerT<Module> SubPassClass;
603
604 // BatcherClass - The type to use for collation of subtypes... This class is
605 // never instantiated for the PassManager<BasicBlock>, but it must be an
606 // instance of PassClass to typecheck.
607 //
608 typedef PassClass BatcherClass;
609
610 // ParentClass - The type of the parent PassManager...
Chris Lattner4e8c4872002-03-23 22:51:58 +0000611 typedef PassManagerT<Function> ParentClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000612
Chris Lattner2eaac392002-01-31 00:40:44 +0000613 // PMType - The type of the passmanager that subclasses this class
614 typedef PassManagerT<BasicBlock> PMType;
615
Chris Lattner67d25652002-01-30 23:20:39 +0000616 // runPass - Specify how the pass should be run on the UnitType
617 static bool runPass(PassClass *P, BasicBlock *M) {
618 // todo, init and finalize
Chris Lattner113f4f42002-06-25 16:13:24 +0000619 return P->runOnBasicBlock(*M);
Chris Lattner67d25652002-01-30 23:20:39 +0000620 }
621
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000622 // Dummy implementation of PassStarted/PassEnded
623 static void PassStarted(Pass *P) {}
624 static void PassEnded(Pass *P) {}
625
Chris Lattnerac3e0602002-01-31 18:32:27 +0000626 // getPMName() - Return the name of the unit the PassManager operates on for
627 // debugging.
628 const char *getPMName() const { return "BasicBlock"; }
Chris Lattner37104aa2002-04-29 14:57:45 +0000629 virtual const char *getPassName() const { return "BasicBlock Pass Manager"; }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000630
Chris Lattner2eaac392002-01-31 00:40:44 +0000631 // Implement the BasicBlockPass interface...
Chris Lattner113f4f42002-06-25 16:13:24 +0000632 virtual bool doInitialization(Module &M);
Chris Lattnerbae3c672002-09-12 17:06:40 +0000633 virtual bool doInitialization(Function &F);
Chris Lattner113f4f42002-06-25 16:13:24 +0000634 virtual bool runOnBasicBlock(BasicBlock &BB);
Chris Lattnerbae3c672002-09-12 17:06:40 +0000635 virtual bool doFinalization(Function &F);
Chris Lattner113f4f42002-06-25 16:13:24 +0000636 virtual bool doFinalization(Module &M);
Chris Lattnercb429062002-04-30 18:50:17 +0000637
638 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
639 AU.setPreservesAll();
640 }
Chris Lattner67d25652002-01-30 23:20:39 +0000641};
642
643
644
645//===----------------------------------------------------------------------===//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000646// PassManagerTraits<Function> Specialization
Chris Lattner67d25652002-01-30 23:20:39 +0000647//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000648// This pass manager is used to group together all of the FunctionPass's
Chris Lattner67d25652002-01-30 23:20:39 +0000649// into a single unit.
650//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000651template<> struct PassManagerTraits<Function> : public FunctionPass {
Chris Lattner67d25652002-01-30 23:20:39 +0000652 // PassClass - The type of passes tracked by this PassManager
Chris Lattnerc8e66542002-04-27 06:56:12 +0000653 typedef FunctionPass PassClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000654
655 // SubPassClass - The types of classes that should be collated together
656 typedef BasicBlockPass SubPassClass;
657
658 // BatcherClass - The type to use for collation of subtypes...
659 typedef PassManagerT<BasicBlock> BatcherClass;
660
661 // ParentClass - The type of the parent PassManager...
662 typedef PassManagerT<Module> ParentClass;
663
664 // PMType - The type of the passmanager that subclasses this class
Chris Lattner4e8c4872002-03-23 22:51:58 +0000665 typedef PassManagerT<Function> PMType;
Chris Lattner67d25652002-01-30 23:20:39 +0000666
667 // runPass - Specify how the pass should be run on the UnitType
Chris Lattnerc8e66542002-04-27 06:56:12 +0000668 static bool runPass(PassClass *P, Function *F) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000669 return P->runOnFunction(*F);
Chris Lattner67d25652002-01-30 23:20:39 +0000670 }
671
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000672 // Dummy implementation of PassStarted/PassEnded
673 static void PassStarted(Pass *P) {}
674 static void PassEnded(Pass *P) {}
675
Chris Lattnerac3e0602002-01-31 18:32:27 +0000676 // getPMName() - Return the name of the unit the PassManager operates on for
677 // debugging.
Chris Lattner4e8c4872002-03-23 22:51:58 +0000678 const char *getPMName() const { return "Function"; }
Chris Lattner37104aa2002-04-29 14:57:45 +0000679 virtual const char *getPassName() const { return "Function Pass Manager"; }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000680
Chris Lattnerc8e66542002-04-27 06:56:12 +0000681 // Implement the FunctionPass interface...
Chris Lattner113f4f42002-06-25 16:13:24 +0000682 virtual bool doInitialization(Module &M);
683 virtual bool runOnFunction(Function &F);
684 virtual bool doFinalization(Module &M);
Chris Lattnercb429062002-04-30 18:50:17 +0000685
686 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
687 AU.setPreservesAll();
688 }
Chris Lattner67d25652002-01-30 23:20:39 +0000689};
690
691
692
693//===----------------------------------------------------------------------===//
694// PassManagerTraits<Module> Specialization
695//
696// This is the top level PassManager implementation that holds generic passes.
697//
698template<> struct PassManagerTraits<Module> : public Pass {
699 // PassClass - The type of passes tracked by this PassManager
700 typedef Pass PassClass;
701
702 // SubPassClass - The types of classes that should be collated together
Chris Lattnerc8e66542002-04-27 06:56:12 +0000703 typedef FunctionPass SubPassClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000704
705 // BatcherClass - The type to use for collation of subtypes...
Chris Lattner4e8c4872002-03-23 22:51:58 +0000706 typedef PassManagerT<Function> BatcherClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000707
708 // ParentClass - The type of the parent PassManager...
Chris Lattnerac3e0602002-01-31 18:32:27 +0000709 typedef AnalysisResolver ParentClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000710
711 // runPass - Specify how the pass should be run on the UnitType
Chris Lattner113f4f42002-06-25 16:13:24 +0000712 static bool runPass(PassClass *P, Module *M) { return P->run(*M); }
Chris Lattner67d25652002-01-30 23:20:39 +0000713
Chris Lattnerac3e0602002-01-31 18:32:27 +0000714 // getPMName() - Return the name of the unit the PassManager operates on for
715 // debugging.
716 const char *getPMName() const { return "Module"; }
Chris Lattner37104aa2002-04-29 14:57:45 +0000717 virtual const char *getPassName() const { return "Module Pass Manager"; }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000718
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000719 // TimingInformation - This data member maintains timing information for each
720 // of the passes that is executed.
721 //
722 TimingInfo *TimeInfo;
723
724 // PassStarted/Ended - This callback is notified any time a pass is started
725 // or stops. This is used to collect timing information about the different
726 // passes being executed.
727 //
728 void PassStarted(Pass *P) {
729 if (TimeInfo) TimeInfo->passStarted(P);
Chris Lattner67d25652002-01-30 23:20:39 +0000730 }
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000731 void PassEnded(Pass *P) {
732 if (TimeInfo) TimeInfo->passEnded(P);
733 }
734
735 // run - Implement the PassManager interface...
Chris Lattner113f4f42002-06-25 16:13:24 +0000736 bool run(Module &M) {
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000737 TimeInfo = TimingInfo::create();
Chris Lattner113f4f42002-06-25 16:13:24 +0000738 bool Result = ((PassManagerT<Module>*)this)->runOnUnit(&M);
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000739 if (TimeInfo) {
740 delete TimeInfo;
741 TimeInfo = 0;
742 }
743 return Result;
744 }
745
746 // PassManagerTraits constructor - Create a timing info object if the user
747 // specified timing info should be collected on the command line.
748 //
749 PassManagerTraits() : TimeInfo(0) {}
Chris Lattner67d25652002-01-30 23:20:39 +0000750};
751
752
753
754//===----------------------------------------------------------------------===//
755// PassManagerTraits Method Implementations
756//
757
758// PassManagerTraits<BasicBlock> Implementations
759//
Chris Lattner113f4f42002-06-25 16:13:24 +0000760inline bool PassManagerTraits<BasicBlock>::doInitialization(Module &M) {
Chris Lattner2eaac392002-01-31 00:40:44 +0000761 bool Changed = false;
762 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
763 ((PMType*)this)->Passes[i]->doInitialization(M);
764 return Changed;
765}
766
Chris Lattnerbae3c672002-09-12 17:06:40 +0000767inline bool PassManagerTraits<BasicBlock>::doInitialization(Function &F) {
768 bool Changed = false;
769 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
770 ((PMType*)this)->Passes[i]->doInitialization(F);
771 return Changed;
772}
773
Chris Lattner113f4f42002-06-25 16:13:24 +0000774inline bool PassManagerTraits<BasicBlock>::runOnBasicBlock(BasicBlock &BB) {
775 return ((PMType*)this)->runOnUnit(&BB);
Chris Lattner2eaac392002-01-31 00:40:44 +0000776}
777
Chris Lattnerbae3c672002-09-12 17:06:40 +0000778inline bool PassManagerTraits<BasicBlock>::doFinalization(Function &F) {
779 bool Changed = false;
780 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
781 ((PMType*)this)->Passes[i]->doFinalization(F);
782 return Changed;
783}
784
Chris Lattner113f4f42002-06-25 16:13:24 +0000785inline bool PassManagerTraits<BasicBlock>::doFinalization(Module &M) {
Chris Lattner2eaac392002-01-31 00:40:44 +0000786 bool Changed = false;
787 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
788 ((PMType*)this)->Passes[i]->doFinalization(M);
789 return Changed;
Chris Lattner67d25652002-01-30 23:20:39 +0000790}
791
792
Chris Lattner4e8c4872002-03-23 22:51:58 +0000793// PassManagerTraits<Function> Implementations
Chris Lattner67d25652002-01-30 23:20:39 +0000794//
Chris Lattner113f4f42002-06-25 16:13:24 +0000795inline bool PassManagerTraits<Function>::doInitialization(Module &M) {
Chris Lattner67d25652002-01-30 23:20:39 +0000796 bool Changed = false;
797 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
798 ((PMType*)this)->Passes[i]->doInitialization(M);
799 return Changed;
800}
801
Chris Lattner113f4f42002-06-25 16:13:24 +0000802inline bool PassManagerTraits<Function>::runOnFunction(Function &F) {
803 return ((PMType*)this)->runOnUnit(&F);
Chris Lattner67d25652002-01-30 23:20:39 +0000804}
805
Chris Lattner113f4f42002-06-25 16:13:24 +0000806inline bool PassManagerTraits<Function>::doFinalization(Module &M) {
Chris Lattner67d25652002-01-30 23:20:39 +0000807 bool Changed = false;
808 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
809 ((PMType*)this)->Passes[i]->doFinalization(M);
810 return Changed;
811}
812
813#endif