blob: 7bd9b3488b4ca21b085887f17110bea684d9eaef [file] [log] [blame]
Chris Lattnerc7bc3bb2003-10-13 05:34:24 +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:
Chris Lattnere2eb99e2002-04-29 04:04:29 +000087 // TimingDtor - Print out information about timing information
Chris Lattneraf751b82002-10-01 19:54:07 +000088 ~TimingInfo() {
89 // Delete all of the timers...
90 TimingData.clear();
91 // TimerGroup is deleted next, printing the report.
92 }
Chris Lattnere2eb99e2002-04-29 04:04:29 +000093
Chris Lattnere7e09442003-08-14 05:20:28 +000094 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
95 // to a non null value (if the -time-passes option is enabled) or it leaves it
96 // null. It may be called multiple times.
97 static void createTheTimeInfo();
98
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 Lattnere7e09442003-08-14 05:20:28 +0000114static TimingInfo *TheTimeInfo;
115
Chris Lattner6e041bd2002-08-21 22:17:09 +0000116//===----------------------------------------------------------------------===//
Chris Lattner67d25652002-01-30 23:20:39 +0000117// Declare the PassManagerTraits which will be specialized...
118//
119template<class UnitType> class PassManagerTraits; // Do not define.
120
121
122//===----------------------------------------------------------------------===//
123// PassManagerT - Container object for passes. The PassManagerT destructor
124// deletes all passes contained inside of the PassManagerT, so you shouldn't
125// delete passes manually, and all passes should be dynamically allocated.
126//
127template<typename UnitType>
128class PassManagerT : public PassManagerTraits<UnitType>,public AnalysisResolver{
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000129 typedef PassManagerTraits<UnitType> Traits;
130 typedef typename Traits::PassClass PassClass;
131 typedef typename Traits::SubPassClass SubPassClass;
132 typedef typename Traits::BatcherClass BatcherClass;
133 typedef typename Traits::ParentClass ParentClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000134
Chris Lattner4c9cd822003-06-23 19:16:20 +0000135 friend class PassManagerTraits<UnitType>::PassClass;
136 friend class PassManagerTraits<UnitType>::SubPassClass;
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000137 friend class Traits;
Chris Lattneree0788d2002-09-25 21:59:11 +0000138 friend class ImmutablePass;
Chris Lattner67d25652002-01-30 23:20:39 +0000139
Chris Lattner73503172002-07-29 21:03:38 +0000140 std::vector<PassClass*> Passes; // List of passes to run
Chris Lattneree0788d2002-09-25 21:59:11 +0000141 std::vector<ImmutablePass*> ImmutablePasses; // List of immutable passes
Chris Lattner67d25652002-01-30 23:20:39 +0000142
143 // The parent of this pass manager...
Chris Lattnerac3e0602002-01-31 18:32:27 +0000144 ParentClass * const Parent;
Chris Lattner67d25652002-01-30 23:20:39 +0000145
146 // The current batcher if one is in use, or null
147 BatcherClass *Batcher;
148
149 // CurrentAnalyses - As the passes are being run, this map contains the
150 // analyses that are available to the current pass for use. This is accessed
151 // through the getAnalysis() function in this class and in Pass.
152 //
153 std::map<AnalysisID, Pass*> CurrentAnalyses;
154
Chris Lattnerac3e0602002-01-31 18:32:27 +0000155 // LastUseOf - This map keeps track of the last usage in our pipeline of a
156 // particular pass. When executing passes, the memory for .first is free'd
157 // after .second is run.
158 //
159 std::map<Pass*, Pass*> LastUseOf;
160
Chris Lattner67d25652002-01-30 23:20:39 +0000161public:
162 PassManagerT(ParentClass *Par = 0) : Parent(Par), Batcher(0) {}
163 ~PassManagerT() {
164 // Delete all of the contained passes...
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000165 for (typename std::vector<PassClass*>::iterator
166 I = Passes.begin(), E = Passes.end(); I != E; ++I)
Chris Lattner67d25652002-01-30 23:20:39 +0000167 delete *I;
Chris Lattneree0788d2002-09-25 21:59:11 +0000168
169 for (std::vector<ImmutablePass*>::iterator
170 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
171 delete *I;
Chris Lattner67d25652002-01-30 23:20:39 +0000172 }
173
174 // run - Run all of the queued passes on the specified module in an optimal
175 // way.
176 virtual bool runOnUnit(UnitType *M) {
177 bool MadeChanges = false;
178 closeBatcher();
179 CurrentAnalyses.clear();
180
Chris Lattnere7e09442003-08-14 05:20:28 +0000181 TimingInfo::createTheTimeInfo();
182
Chris Lattneree0788d2002-09-25 21:59:11 +0000183 // Add any immutable passes to the CurrentAnalyses set...
Chris Lattner480b37d2002-09-25 22:26:52 +0000184 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
185 ImmutablePass *IPass = ImmutablePasses[i];
186 if (const PassInfo *PI = IPass->getPassInfo()) {
187 CurrentAnalyses[PI] = IPass;
Chris Lattneree0788d2002-09-25 21:59:11 +0000188
189 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
190 for (unsigned i = 0, e = II.size(); i != e; ++i)
Chris Lattner480b37d2002-09-25 22:26:52 +0000191 CurrentAnalyses[II[i]] = IPass;
Chris Lattneree0788d2002-09-25 21:59:11 +0000192 }
Chris Lattner480b37d2002-09-25 22:26:52 +0000193 }
Chris Lattneree0788d2002-09-25 21:59:11 +0000194
Chris Lattnerac3e0602002-01-31 18:32:27 +0000195 // LastUserOf - This contains the inverted LastUseOfMap...
196 std::map<Pass *, std::vector<Pass*> > LastUserOf;
197 for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(),
198 E = LastUseOf.end(); I != E; ++I)
199 LastUserOf[I->second].push_back(I->first);
200
201
Chris Lattner67d25652002-01-30 23:20:39 +0000202 // Output debug information...
Chris Lattner1e4867f2002-07-30 19:51:02 +0000203 if (Parent == 0) PMDebug::PerformPassStartupStuff(this);
Chris Lattner67d25652002-01-30 23:20:39 +0000204
205 // Run all of the passes
206 for (unsigned i = 0, e = Passes.size(); i < e; ++i) {
207 PassClass *P = Passes[i];
208
Chris Lattnera454b5b2002-04-28 05:14:06 +0000209 PMDebug::PrintPassInformation(getDepth(), "Executing Pass", P,
210 (Annotable*)M);
Chris Lattner67d25652002-01-30 23:20:39 +0000211
212 // Get information about what analyses the pass uses...
Chris Lattnerc8e66542002-04-27 06:56:12 +0000213 AnalysisUsage AnUsage;
214 P->getAnalysisUsage(AnUsage);
215 PMDebug::PrintAnalysisSetInfo(getDepth(), "Required", P,
216 AnUsage.getRequiredSet());
Chris Lattner67d25652002-01-30 23:20:39 +0000217
Chris Lattner216edd42002-08-30 20:25:01 +0000218 // All Required analyses should be available to the pass as it runs! Here
219 // we fill in the AnalysisImpls member of the pass so that it can
220 // successfully use the getAnalysis() method to retrieve the
221 // implementations it needs.
222 //
223 P->AnalysisImpls.clear();
224 P->AnalysisImpls.reserve(AnUsage.getRequiredSet().size());
225 for (std::vector<const PassInfo *>::const_iterator
Chris Lattnerc8e66542002-04-27 06:56:12 +0000226 I = AnUsage.getRequiredSet().begin(),
227 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Chris Lattner216edd42002-08-30 20:25:01 +0000228 Pass *Impl = getAnalysisOrNullUp(*I);
229 if (Impl == 0) {
230 std::cerr << "Analysis '" << (*I)->getPassName()
231 << "' used but not available!";
232 assert(0 && "Analysis used but not available!");
233 } else if (PassDebugging == Details) {
234 if ((*I)->getPassName() != std::string(Impl->getPassName()))
235 std::cerr << " Interface '" << (*I)->getPassName()
236 << "' implemented by '" << Impl->getPassName() << "'\n";
237 }
238 P->AnalysisImpls.push_back(std::make_pair(*I, Impl));
Chris Lattner67d25652002-01-30 23:20:39 +0000239 }
Chris Lattner67d25652002-01-30 23:20:39 +0000240
241 // Run the sub pass!
Chris Lattnere7e09442003-08-14 05:20:28 +0000242 if (TheTimeInfo) TheTimeInfo->passStarted(P);
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000243 bool Changed = runPass(P, M);
Chris Lattnere7e09442003-08-14 05:20:28 +0000244 if (TheTimeInfo) TheTimeInfo->passEnded(P);
Chris Lattnerf2f31bd2002-02-01 04:53:36 +0000245 MadeChanges |= Changed;
Chris Lattner67d25652002-01-30 23:20:39 +0000246
Chris Lattnered6504b2002-09-08 19:00:07 +0000247 // Check for memory leaks by the pass...
248 LeakDetector::checkForGarbage(std::string("after running pass '") +
249 P->getPassName() + "'");
250
Chris Lattnerf2f31bd2002-02-01 04:53:36 +0000251 if (Changed)
252 PMDebug::PrintPassInformation(getDepth()+1, "Made Modification", P,
Chris Lattnera454b5b2002-04-28 05:14:06 +0000253 (Annotable*)M);
Chris Lattnerc8e66542002-04-27 06:56:12 +0000254 PMDebug::PrintAnalysisSetInfo(getDepth(), "Preserved", P,
255 AnUsage.getPreservedSet());
Chris Lattner67d25652002-01-30 23:20:39 +0000256
Chris Lattnerc8e66542002-04-27 06:56:12 +0000257
258 // Erase all analyses not in the preserved set...
Chris Lattner820d9712002-10-21 20:00:28 +0000259 if (!AnUsage.getPreservesAll()) {
Chris Lattnerc8e66542002-04-27 06:56:12 +0000260 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
261 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(),
262 E = CurrentAnalyses.end(); I != E; )
263 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) !=
264 PreservedSet.end())
265 ++I; // This analysis is preserved, leave it in the available set...
266 else {
Chris Lattneree0788d2002-09-25 21:59:11 +0000267 if (!dynamic_cast<ImmutablePass*>(I->second)) {
Chris Lattnerade85ec2003-02-14 05:34:36 +0000268 std::map<AnalysisID, Pass*>::iterator J = I++;
269 CurrentAnalyses.erase(J); // Analysis not preserved!
Chris Lattneree0788d2002-09-25 21:59:11 +0000270 } else {
271 ++I;
272 }
Chris Lattnerc8e66542002-04-27 06:56:12 +0000273 }
274 }
275
Chris Lattner73503172002-07-29 21:03:38 +0000276 // Add the current pass to the set of passes that have been run, and are
277 // thus available to users.
278 //
Chris Lattner216edd42002-08-30 20:25:01 +0000279 if (const PassInfo *PI = P->getPassInfo()) {
Chris Lattner73503172002-07-29 21:03:38 +0000280 CurrentAnalyses[PI] = P;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000281
Chris Lattner216edd42002-08-30 20:25:01 +0000282 // This pass is the current implementation of all of the interfaces it
283 // implements as well.
284 //
285 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
286 for (unsigned i = 0, e = II.size(); i != e; ++i)
287 CurrentAnalyses[II[i]] = P;
288 }
289
Chris Lattnerac3e0602002-01-31 18:32:27 +0000290 // Free memory for any passes that we are the last use of...
291 std::vector<Pass*> &DeadPass = LastUserOf[P];
292 for (std::vector<Pass*>::iterator I = DeadPass.begin(),E = DeadPass.end();
293 I != E; ++I) {
294 PMDebug::PrintPassInformation(getDepth()+1, "Freeing Pass", *I,
Chris Lattnera454b5b2002-04-28 05:14:06 +0000295 (Annotable*)M);
Chris Lattnerac3e0602002-01-31 18:32:27 +0000296 (*I)->releaseMemory();
297 }
Chris Lattner6b6f5402002-09-29 22:50:22 +0000298
299 // Make sure to remove dead passes from the CurrentAnalyses list...
300 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin();
301 I != CurrentAnalyses.end(); ) {
302 std::vector<Pass*>::iterator DPI = std::find(DeadPass.begin(),
303 DeadPass.end(), I->second);
304 if (DPI != DeadPass.end()) { // This pass is dead now... remove it
305 std::map<AnalysisID, Pass*>::iterator IDead = I++;
306 CurrentAnalyses.erase(IDead);
307 } else {
308 ++I; // Move on to the next element...
309 }
310 }
Chris Lattner67d25652002-01-30 23:20:39 +0000311 }
Chris Lattnere7e09442003-08-14 05:20:28 +0000312
Chris Lattner67d25652002-01-30 23:20:39 +0000313 return MadeChanges;
314 }
315
Chris Lattnerac3e0602002-01-31 18:32:27 +0000316 // dumpPassStructure - Implement the -debug-passes=PassStructure option
317 virtual void dumpPassStructure(unsigned Offset = 0) {
Chris Lattner480b37d2002-09-25 22:26:52 +0000318 // Print out the immutable passes...
319 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i)
320 ImmutablePasses[i]->dumpPassStructure(0);
321
Chris Lattnerac3e0602002-01-31 18:32:27 +0000322 std::cerr << std::string(Offset*2, ' ') << Traits::getPMName()
323 << " Pass Manager\n";
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000324 for (typename std::vector<PassClass*>::iterator
325 I = Passes.begin(), E = Passes.end(); I != E; ++I) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000326 PassClass *P = *I;
327 P->dumpPassStructure(Offset+1);
328
329 // Loop through and see which classes are destroyed after this one...
330 for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(),
331 E = LastUseOf.end(); I != E; ++I) {
332 if (P == I->second) {
Chris Lattner73503172002-07-29 21:03:38 +0000333 std::cerr << "--" << std::string(Offset*2, ' ');
Chris Lattnerac3e0602002-01-31 18:32:27 +0000334 I->first->dumpPassStructure(0);
335 }
336 }
337 }
338 }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000339
Chris Lattner60430422003-04-24 20:07:38 +0000340 Pass *getImmutablePassOrNull(const PassInfo *ID) const {
341 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
342 const PassInfo *IPID = ImmutablePasses[i]->getPassInfo();
343 if (IPID == ID)
344 return ImmutablePasses[i];
345
346 // This pass is the current implementation of all of the interfaces it
347 // implements as well.
348 //
349 const std::vector<const PassInfo*> &II =
350 IPID->getInterfacesImplemented();
351 for (unsigned j = 0, e = II.size(); j != e; ++j)
352 if (II[j] == ID) return ImmutablePasses[i];
353 }
354 return 0;
355 }
356
Chris Lattner6e041bd2002-08-21 22:17:09 +0000357 Pass *getAnalysisOrNullDown(const PassInfo *ID) const {
Chris Lattner216edd42002-08-30 20:25:01 +0000358 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000359
360 if (I != CurrentAnalyses.end())
361 return I->second; // Found it.
362
Chris Lattner60430422003-04-24 20:07:38 +0000363 if (Pass *P = getImmutablePassOrNull(ID))
364 return P;
365
Chris Lattner6e041bd2002-08-21 22:17:09 +0000366 if (Batcher)
367 return ((AnalysisResolver*)Batcher)->getAnalysisOrNullDown(ID);
368 return 0;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000369 }
370
Chris Lattner6e041bd2002-08-21 22:17:09 +0000371 Pass *getAnalysisOrNullUp(const PassInfo *ID) const {
Chris Lattner216edd42002-08-30 20:25:01 +0000372 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000373 if (I != CurrentAnalyses.end())
374 return I->second; // Found it.
375
376 if (Parent) // Try scanning...
377 return Parent->getAnalysisOrNullUp(ID);
Chris Lattner60430422003-04-24 20:07:38 +0000378 else if (!ImmutablePasses.empty())
379 return getImmutablePassOrNull(ID);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000380 return 0;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000381 }
382
383 // markPassUsed - Inform higher level pass managers (and ourselves)
384 // that these analyses are being used by this pass. This is used to
385 // make sure that analyses are not free'd before we have to use
386 // them...
387 //
Chris Lattner6e041bd2002-08-21 22:17:09 +0000388 void markPassUsed(const PassInfo *P, Pass *User) {
Chris Lattner216edd42002-08-30 20:25:01 +0000389 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(P);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000390
Chris Lattnerac3e0602002-01-31 18:32:27 +0000391 if (I != CurrentAnalyses.end()) {
392 LastUseOf[I->second] = User; // Local pass, extend the lifetime
393 } else {
394 // Pass not in current available set, must be a higher level pass
Misha Brukman632df282002-10-29 23:06:16 +0000395 // available to us, propagate to parent pass manager... We tell the
Chris Lattnerac3e0602002-01-31 18:32:27 +0000396 // parent that we (the passmanager) are using the analysis so that it
397 // frees the analysis AFTER this pass manager runs.
398 //
Chris Lattneree0788d2002-09-25 21:59:11 +0000399 if (Parent) {
400 Parent->markPassUsed(P, this);
401 } else {
Chris Lattner60430422003-04-24 20:07:38 +0000402 assert(getAnalysisOrNullUp(P) &&
403 dynamic_cast<ImmutablePass*>(getAnalysisOrNullUp(P)) &&
404 "Pass available but not found! "
Chris Lattneree0788d2002-09-25 21:59:11 +0000405 "Perhaps this is a module pass requiring a function pass?");
406 }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000407 }
408 }
Chris Lattneree0788d2002-09-25 21:59:11 +0000409
Chris Lattnerac3e0602002-01-31 18:32:27 +0000410 // Return the number of parent PassManagers that exist
411 virtual unsigned getDepth() const {
412 if (Parent == 0) return 0;
413 return 1 + Parent->getDepth();
414 }
415
Chris Lattner1e4867f2002-07-30 19:51:02 +0000416 virtual unsigned getNumContainedPasses() const { return Passes.size(); }
417 virtual const Pass *getContainedPass(unsigned N) const {
418 assert(N < Passes.size() && "Pass number out of range!");
419 return Passes[N];
420 }
421
Chris Lattner6e041bd2002-08-21 22:17:09 +0000422 // add - Add a pass to the queue of passes to run. This gives ownership of
Chris Lattner67d25652002-01-30 23:20:39 +0000423 // the Pass to the PassManager. When the PassManager is destroyed, the pass
Chris Lattnerac3e0602002-01-31 18:32:27 +0000424 // will be destroyed as well, so there is no need to delete the pass. This
425 // implies that all passes MUST be new'd.
Chris Lattner67d25652002-01-30 23:20:39 +0000426 //
427 void add(PassClass *P) {
428 // Get information about what analyses the pass uses...
Chris Lattnerc8e66542002-04-27 06:56:12 +0000429 AnalysisUsage AnUsage;
430 P->getAnalysisUsage(AnUsage);
431 const std::vector<AnalysisID> &Required = AnUsage.getRequiredSet();
Chris Lattner67d25652002-01-30 23:20:39 +0000432
433 // Loop over all of the analyses used by this pass,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000434 for (std::vector<AnalysisID>::const_iterator I = Required.begin(),
435 E = Required.end(); I != E; ++I) {
Chris Lattner67d25652002-01-30 23:20:39 +0000436 if (getAnalysisOrNullDown(*I) == 0)
Chris Lattner26750072002-07-27 01:12:17 +0000437 add((PassClass*)(*I)->createPass());
Chris Lattner67d25652002-01-30 23:20:39 +0000438 }
439
440 // Tell the pass to add itself to this PassManager... the way it does so
441 // depends on the class of the pass, and is critical to laying out passes in
442 // an optimal order..
443 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000444 P->addToPassManager(this, AnUsage);
Chris Lattner67d25652002-01-30 23:20:39 +0000445 }
446
Brian Gaeke2cc4b9d2003-08-14 06:07:57 +0000447 // add - H4x0r an ImmutablePass into a PassManager that might not be
448 // expecting one.
449 //
450 void add(ImmutablePass *P) {
451 // Get information about what analyses the pass uses...
452 AnalysisUsage AnUsage;
453 P->getAnalysisUsage(AnUsage);
454 const std::vector<AnalysisID> &Required = AnUsage.getRequiredSet();
Chris Lattner67d25652002-01-30 23:20:39 +0000455
Brian Gaeke2cc4b9d2003-08-14 06:07:57 +0000456 // Loop over all of the analyses used by this pass,
457 for (std::vector<AnalysisID>::const_iterator I = Required.begin(),
458 E = Required.end(); I != E; ++I) {
459 if (getAnalysisOrNullDown(*I) == 0)
460 add((PassClass*)(*I)->createPass());
461 }
462
463 // Add the ImmutablePass to this PassManager.
464 addPass(P, AnUsage);
465 }
466
467private:
Chris Lattner67d25652002-01-30 23:20:39 +0000468 // addPass - These functions are used to implement the subclass specific
469 // behaviors present in PassManager. Basically the add(Pass*) method ends up
470 // reflecting its behavior into a Pass::addToPassManager call. Subclasses of
471 // Pass override it specifically so that they can reflect the type
472 // information inherent in "this" back to the PassManager.
473 //
474 // For generic Pass subclasses (which are interprocedural passes), we simply
475 // add the pass to the end of the pass list and terminate any accumulation of
Chris Lattnerc8e66542002-04-27 06:56:12 +0000476 // FunctionPass's that are present.
Chris Lattner67d25652002-01-30 23:20:39 +0000477 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000478 void addPass(PassClass *P, AnalysisUsage &AnUsage) {
479 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
Chris Lattnerc8e66542002-04-27 06:56:12 +0000480
Chris Lattner73503172002-07-29 21:03:38 +0000481 // FIXME: If this pass being added isn't killed by any of the passes in the
482 // batcher class then we can reorder to pass to execute before the batcher
483 // does, which will potentially allow us to batch more passes!
Chris Lattner67d25652002-01-30 23:20:39 +0000484 //
Chris Lattner73503172002-07-29 21:03:38 +0000485 //const std::vector<AnalysisID> &ProvidedSet = AnUsage.getProvidedSet();
486 if (Batcher /*&& ProvidedSet.empty()*/)
Chris Lattner67d25652002-01-30 23:20:39 +0000487 closeBatcher(); // This pass cannot be batched!
488
489 // Set the Resolver instance variable in the Pass so that it knows where to
490 // find this object...
491 //
492 setAnalysisResolver(P, this);
493 Passes.push_back(P);
494
Chris Lattnerac3e0602002-01-31 18:32:27 +0000495 // Inform higher level pass managers (and ourselves) that these analyses are
496 // being used by this pass. This is used to make sure that analyses are not
497 // free'd before we have to use them...
498 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000499 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
500 E = RequiredSet.end(); I != E; ++I)
Chris Lattnerac3e0602002-01-31 18:32:27 +0000501 markPassUsed(*I, P); // Mark *I as used by P
502
Chris Lattnerc8e66542002-04-27 06:56:12 +0000503 // Erase all analyses not in the preserved set...
Chris Lattner820d9712002-10-21 20:00:28 +0000504 if (!AnUsage.getPreservesAll()) {
Chris Lattnerc8e66542002-04-27 06:56:12 +0000505 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
506 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(),
Chris Lattneree0788d2002-09-25 21:59:11 +0000507 E = CurrentAnalyses.end(); I != E; ) {
508 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
509 PreservedSet.end()) { // Analysis not preserved!
510 CurrentAnalyses.erase(I); // Remove from available analyses
Chris Lattnerc8e66542002-04-27 06:56:12 +0000511 I = CurrentAnalyses.begin();
Chris Lattneree0788d2002-09-25 21:59:11 +0000512 } else {
513 ++I;
Chris Lattnerc8e66542002-04-27 06:56:12 +0000514 }
Chris Lattneree0788d2002-09-25 21:59:11 +0000515 }
Chris Lattnerc8e66542002-04-27 06:56:12 +0000516 }
Chris Lattner67d25652002-01-30 23:20:39 +0000517
Chris Lattner73503172002-07-29 21:03:38 +0000518 // Add this pass to the currently available set...
Chris Lattner216edd42002-08-30 20:25:01 +0000519 if (const PassInfo *PI = P->getPassInfo()) {
Chris Lattner73503172002-07-29 21:03:38 +0000520 CurrentAnalyses[PI] = P;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000521
Chris Lattner216edd42002-08-30 20:25:01 +0000522 // This pass is the current implementation of all of the interfaces it
523 // implements as well.
524 //
525 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
526 for (unsigned i = 0, e = II.size(); i != e; ++i)
527 CurrentAnalyses[II[i]] = P;
528 }
529
Chris Lattnerac3e0602002-01-31 18:32:27 +0000530 // For now assume that our results are never used...
531 LastUseOf[P] = P;
Chris Lattner67d25652002-01-30 23:20:39 +0000532 }
533
Chris Lattnerc8e66542002-04-27 06:56:12 +0000534 // For FunctionPass subclasses, we must be sure to batch the FunctionPass's
535 // together in a BatcherClass object so that all of the analyses are run
536 // together a function at a time.
Chris Lattner67d25652002-01-30 23:20:39 +0000537 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000538 void addPass(SubPassClass *MP, AnalysisUsage &AnUsage) {
Chris Lattner67d25652002-01-30 23:20:39 +0000539 if (Batcher == 0) // If we don't have a batcher yet, make one now.
540 Batcher = new BatcherClass(this);
Chris Lattner73503172002-07-29 21:03:38 +0000541 // The Batcher will queue the passes up
Chris Lattnerc8e66542002-04-27 06:56:12 +0000542 MP->addToPassManager(Batcher, AnUsage);
Chris Lattner67d25652002-01-30 23:20:39 +0000543 }
544
545 // closeBatcher - Terminate the batcher that is being worked on.
546 void closeBatcher() {
547 if (Batcher) {
548 Passes.push_back(Batcher);
549 Batcher = 0;
550 }
551 }
Chris Lattneree0788d2002-09-25 21:59:11 +0000552
553public:
554 // When an ImmutablePass is added, it gets added to the top level pass
555 // manager.
556 void addPass(ImmutablePass *IP, AnalysisUsage &AU) {
557 if (Parent) { // Make sure this request goes to the top level passmanager...
558 Parent->addPass(IP, AU);
559 return;
560 }
561
562 // Set the Resolver instance variable in the Pass so that it knows where to
563 // find this object...
564 //
565 setAnalysisResolver(IP, this);
566 ImmutablePasses.push_back(IP);
Chris Lattnerd4ed5c62003-02-26 19:10:57 +0000567
568 // All Required analyses should be available to the pass as it initializes!
569 // Here we fill in the AnalysisImpls member of the pass so that it can
570 // successfully use the getAnalysis() method to retrieve the implementations
571 // it needs.
572 //
573 IP->AnalysisImpls.clear();
574 IP->AnalysisImpls.reserve(AU.getRequiredSet().size());
575 for (std::vector<const PassInfo *>::const_iterator
576 I = AU.getRequiredSet().begin(),
577 E = AU.getRequiredSet().end(); I != E; ++I) {
578 Pass *Impl = getAnalysisOrNullUp(*I);
579 if (Impl == 0) {
580 std::cerr << "Analysis '" << (*I)->getPassName()
581 << "' used but not available!";
582 assert(0 && "Analysis used but not available!");
583 } else if (PassDebugging == Details) {
584 if ((*I)->getPassName() != std::string(Impl->getPassName()))
585 std::cerr << " Interface '" << (*I)->getPassName()
586 << "' implemented by '" << Impl->getPassName() << "'\n";
587 }
588 IP->AnalysisImpls.push_back(std::make_pair(*I, Impl));
589 }
Chris Lattneree0788d2002-09-25 21:59:11 +0000590
Chris Lattnerd4ed5c62003-02-26 19:10:57 +0000591 // Initialize the immutable pass...
592 IP->initializePass();
Chris Lattneree0788d2002-09-25 21:59:11 +0000593 }
Chris Lattner67d25652002-01-30 23:20:39 +0000594};
595
596
597
598//===----------------------------------------------------------------------===//
599// PassManagerTraits<BasicBlock> Specialization
600//
601// This pass manager is used to group together all of the BasicBlockPass's
602// into a single unit.
603//
604template<> struct PassManagerTraits<BasicBlock> : public BasicBlockPass {
605 // PassClass - The type of passes tracked by this PassManager
606 typedef BasicBlockPass PassClass;
607
608 // SubPassClass - The types of classes that should be collated together
609 // This is impossible to match, so BasicBlock instantiations of PassManagerT
610 // do not collate.
611 //
612 typedef PassManagerT<Module> SubPassClass;
613
614 // BatcherClass - The type to use for collation of subtypes... This class is
615 // never instantiated for the PassManager<BasicBlock>, but it must be an
616 // instance of PassClass to typecheck.
617 //
618 typedef PassClass BatcherClass;
619
620 // ParentClass - The type of the parent PassManager...
Chris Lattner4e8c4872002-03-23 22:51:58 +0000621 typedef PassManagerT<Function> ParentClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000622
Chris Lattner2eaac392002-01-31 00:40:44 +0000623 // PMType - The type of the passmanager that subclasses this class
624 typedef PassManagerT<BasicBlock> PMType;
625
Chris Lattner67d25652002-01-30 23:20:39 +0000626 // runPass - Specify how the pass should be run on the UnitType
627 static bool runPass(PassClass *P, BasicBlock *M) {
628 // todo, init and finalize
Chris Lattner113f4f42002-06-25 16:13:24 +0000629 return P->runOnBasicBlock(*M);
Chris Lattner67d25652002-01-30 23:20:39 +0000630 }
631
Chris Lattnerac3e0602002-01-31 18:32:27 +0000632 // getPMName() - Return the name of the unit the PassManager operates on for
633 // debugging.
634 const char *getPMName() const { return "BasicBlock"; }
Chris Lattner37104aa2002-04-29 14:57:45 +0000635 virtual const char *getPassName() const { return "BasicBlock Pass Manager"; }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000636
Chris Lattner2eaac392002-01-31 00:40:44 +0000637 // Implement the BasicBlockPass interface...
Chris Lattner113f4f42002-06-25 16:13:24 +0000638 virtual bool doInitialization(Module &M);
Chris Lattnerbae3c672002-09-12 17:06:40 +0000639 virtual bool doInitialization(Function &F);
Chris Lattner113f4f42002-06-25 16:13:24 +0000640 virtual bool runOnBasicBlock(BasicBlock &BB);
Chris Lattnerbae3c672002-09-12 17:06:40 +0000641 virtual bool doFinalization(Function &F);
Chris Lattner113f4f42002-06-25 16:13:24 +0000642 virtual bool doFinalization(Module &M);
Chris Lattnercb429062002-04-30 18:50:17 +0000643
644 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
645 AU.setPreservesAll();
646 }
Chris Lattner67d25652002-01-30 23:20:39 +0000647};
648
649
650
651//===----------------------------------------------------------------------===//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000652// PassManagerTraits<Function> Specialization
Chris Lattner67d25652002-01-30 23:20:39 +0000653//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000654// This pass manager is used to group together all of the FunctionPass's
Chris Lattner67d25652002-01-30 23:20:39 +0000655// into a single unit.
656//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000657template<> struct PassManagerTraits<Function> : public FunctionPass {
Chris Lattner67d25652002-01-30 23:20:39 +0000658 // PassClass - The type of passes tracked by this PassManager
Chris Lattnerc8e66542002-04-27 06:56:12 +0000659 typedef FunctionPass PassClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000660
661 // SubPassClass - The types of classes that should be collated together
662 typedef BasicBlockPass SubPassClass;
663
664 // BatcherClass - The type to use for collation of subtypes...
665 typedef PassManagerT<BasicBlock> BatcherClass;
666
667 // ParentClass - The type of the parent PassManager...
668 typedef PassManagerT<Module> ParentClass;
669
670 // PMType - The type of the passmanager that subclasses this class
Chris Lattner4e8c4872002-03-23 22:51:58 +0000671 typedef PassManagerT<Function> PMType;
Chris Lattner67d25652002-01-30 23:20:39 +0000672
673 // runPass - Specify how the pass should be run on the UnitType
Chris Lattnerc8e66542002-04-27 06:56:12 +0000674 static bool runPass(PassClass *P, Function *F) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000675 return P->runOnFunction(*F);
Chris Lattner67d25652002-01-30 23:20:39 +0000676 }
677
Chris Lattnerac3e0602002-01-31 18:32:27 +0000678 // getPMName() - Return the name of the unit the PassManager operates on for
679 // debugging.
Chris Lattner4e8c4872002-03-23 22:51:58 +0000680 const char *getPMName() const { return "Function"; }
Chris Lattner37104aa2002-04-29 14:57:45 +0000681 virtual const char *getPassName() const { return "Function Pass Manager"; }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000682
Chris Lattnerc8e66542002-04-27 06:56:12 +0000683 // Implement the FunctionPass interface...
Chris Lattner113f4f42002-06-25 16:13:24 +0000684 virtual bool doInitialization(Module &M);
685 virtual bool runOnFunction(Function &F);
686 virtual bool doFinalization(Module &M);
Chris Lattnercb429062002-04-30 18:50:17 +0000687
688 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
689 AU.setPreservesAll();
690 }
Chris Lattner67d25652002-01-30 23:20:39 +0000691};
692
693
694
695//===----------------------------------------------------------------------===//
696// PassManagerTraits<Module> Specialization
697//
698// This is the top level PassManager implementation that holds generic passes.
699//
700template<> struct PassManagerTraits<Module> : public Pass {
701 // PassClass - The type of passes tracked by this PassManager
702 typedef Pass PassClass;
703
704 // SubPassClass - The types of classes that should be collated together
Chris Lattnerc8e66542002-04-27 06:56:12 +0000705 typedef FunctionPass SubPassClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000706
707 // BatcherClass - The type to use for collation of subtypes...
Chris Lattner4e8c4872002-03-23 22:51:58 +0000708 typedef PassManagerT<Function> BatcherClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000709
710 // ParentClass - The type of the parent PassManager...
Chris Lattnerac3e0602002-01-31 18:32:27 +0000711 typedef AnalysisResolver ParentClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000712
713 // runPass - Specify how the pass should be run on the UnitType
Chris Lattner113f4f42002-06-25 16:13:24 +0000714 static bool runPass(PassClass *P, Module *M) { return P->run(*M); }
Chris Lattner67d25652002-01-30 23:20:39 +0000715
Chris Lattnerac3e0602002-01-31 18:32:27 +0000716 // getPMName() - Return the name of the unit the PassManager operates on for
717 // debugging.
718 const char *getPMName() const { return "Module"; }
Chris Lattner37104aa2002-04-29 14:57:45 +0000719 virtual const char *getPassName() const { return "Module Pass Manager"; }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000720
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000721 // run - Implement the PassManager interface...
Chris Lattner113f4f42002-06-25 16:13:24 +0000722 bool run(Module &M) {
Chris Lattnere7e09442003-08-14 05:20:28 +0000723 return ((PassManagerT<Module>*)this)->runOnUnit(&M);
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000724 }
Chris Lattner67d25652002-01-30 23:20:39 +0000725};
726
727
728
729//===----------------------------------------------------------------------===//
730// PassManagerTraits Method Implementations
731//
732
733// PassManagerTraits<BasicBlock> Implementations
734//
Chris Lattner113f4f42002-06-25 16:13:24 +0000735inline bool PassManagerTraits<BasicBlock>::doInitialization(Module &M) {
Chris Lattner2eaac392002-01-31 00:40:44 +0000736 bool Changed = false;
737 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
738 ((PMType*)this)->Passes[i]->doInitialization(M);
739 return Changed;
740}
741
Chris Lattnerbae3c672002-09-12 17:06:40 +0000742inline bool PassManagerTraits<BasicBlock>::doInitialization(Function &F) {
743 bool Changed = false;
744 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
745 ((PMType*)this)->Passes[i]->doInitialization(F);
746 return Changed;
747}
748
Chris Lattner113f4f42002-06-25 16:13:24 +0000749inline bool PassManagerTraits<BasicBlock>::runOnBasicBlock(BasicBlock &BB) {
750 return ((PMType*)this)->runOnUnit(&BB);
Chris Lattner2eaac392002-01-31 00:40:44 +0000751}
752
Chris Lattnerbae3c672002-09-12 17:06:40 +0000753inline bool PassManagerTraits<BasicBlock>::doFinalization(Function &F) {
754 bool Changed = false;
755 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
756 ((PMType*)this)->Passes[i]->doFinalization(F);
757 return Changed;
758}
759
Chris Lattner113f4f42002-06-25 16:13:24 +0000760inline bool PassManagerTraits<BasicBlock>::doFinalization(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]->doFinalization(M);
764 return Changed;
Chris Lattner67d25652002-01-30 23:20:39 +0000765}
766
767
Chris Lattner4e8c4872002-03-23 22:51:58 +0000768// PassManagerTraits<Function> Implementations
Chris Lattner67d25652002-01-30 23:20:39 +0000769//
Chris Lattner113f4f42002-06-25 16:13:24 +0000770inline bool PassManagerTraits<Function>::doInitialization(Module &M) {
Chris Lattner67d25652002-01-30 23:20:39 +0000771 bool Changed = false;
772 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
773 ((PMType*)this)->Passes[i]->doInitialization(M);
774 return Changed;
775}
776
Chris Lattner113f4f42002-06-25 16:13:24 +0000777inline bool PassManagerTraits<Function>::runOnFunction(Function &F) {
778 return ((PMType*)this)->runOnUnit(&F);
Chris Lattner67d25652002-01-30 23:20:39 +0000779}
780
Chris Lattner113f4f42002-06-25 16:13:24 +0000781inline bool PassManagerTraits<Function>::doFinalization(Module &M) {
Chris Lattner67d25652002-01-30 23:20:39 +0000782 bool Changed = false;
783 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
784 ((PMType*)this)->Passes[i]->doFinalization(M);
785 return Changed;
786}
787
788#endif