blob: 157f806651a392af8fbb92df59808dc9c0f6c64d [file] [log] [blame]
Chris Lattnerc7bc3bb2003-10-13 05:34:24 +00001//===- PassManagerT.h - Container for Passes --------------------*- C++ -*-===//
John Criswell29265fe2003-10-21 15:17:13 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner67d25652002-01-30 23:20:39 +00009//
Chris Lattnerd0e9bd12002-04-28 20:42:50 +000010// This file defines the PassManagerT class. This class is used to hold,
Chris Lattner67d25652002-01-30 23:20:39 +000011// maintain, and optimize execution of Pass's. The PassManager class ensures
12// that analysis results are available before a pass runs, and that Pass's are
13// destroyed when the PassManager is destroyed.
14//
Chris Lattnerd0e9bd12002-04-28 20:42:50 +000015// The PassManagerT template is instantiated three times to do its job. The
16// public PassManager class is a Pimpl around the PassManagerT<Module> interface
17// to avoid having all of the PassManager clients being exposed to the
18// implementation details herein.
Chris Lattner67d25652002-01-30 23:20:39 +000019//
20//===----------------------------------------------------------------------===//
21
Chris Lattnerd0e9bd12002-04-28 20:42:50 +000022#ifndef LLVM_PASSMANAGER_T_H
23#define LLVM_PASSMANAGER_T_H
Chris Lattner67d25652002-01-30 23:20:39 +000024
25#include "llvm/Pass.h"
Chris Lattner198cf422002-07-30 16:27:02 +000026#include "Support/CommandLine.h"
Chris Lattnered6504b2002-09-08 19:00:07 +000027#include "Support/LeakDetector.h"
Chris Lattneraf751b82002-10-01 19:54:07 +000028#include "Support/Timer.h"
Chris Lattnerc8e66542002-04-27 06:56:12 +000029#include <algorithm>
Chris Lattner395c27a2002-07-31 18:04:17 +000030#include <iostream>
Chris Lattnera454b5b2002-04-28 05:14:06 +000031class Annotable;
Chris Lattner67d25652002-01-30 23:20:39 +000032
Chris Lattner67d25652002-01-30 23:20:39 +000033//===----------------------------------------------------------------------===//
Chris Lattner198cf422002-07-30 16:27:02 +000034// Pass debugging information. Often it is useful to find out what pass is
35// running when a crash occurs in a utility. When this library is compiled with
36// debugging on, a command line option (--debug-pass) is enabled that causes the
37// pass name to be printed before it executes.
38//
39
40// Different debug levels that can be enabled...
41enum PassDebugLevel {
Chris Lattner1e4867f2002-07-30 19:51:02 +000042 None, Arguments, Structure, Executions, Details
Chris Lattner198cf422002-07-30 16:27:02 +000043};
44
45static cl::opt<enum PassDebugLevel>
46PassDebugging("debug-pass", cl::Hidden,
47 cl::desc("Print PassManager debugging information"),
48 cl::values(
49 clEnumVal(None , "disable debug output"),
Chris Lattner1e4867f2002-07-30 19:51:02 +000050 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
Chris Lattner198cf422002-07-30 16:27:02 +000051 clEnumVal(Structure , "print pass structure before run()"),
52 clEnumVal(Executions, "print pass name before it is executed"),
53 clEnumVal(Details , "print pass details when it is executed"),
54 0));
55
56//===----------------------------------------------------------------------===//
Chris Lattner57d2ba32002-04-04 19:35:24 +000057// PMDebug class - a set of debugging functions, that are not to be
58// instantiated by the template.
Chris Lattner67d25652002-01-30 23:20:39 +000059//
60struct PMDebug {
Chris Lattner1e4867f2002-07-30 19:51:02 +000061 static void PerformPassStartupStuff(Pass *P) {
62 // If debugging is enabled, print out argument information...
63 if (PassDebugging >= Arguments) {
64 std::cerr << "Pass Arguments: ";
65 PrintArgumentInformation(P);
66 std::cerr << "\n";
67
68 // Print the pass execution structure
69 if (PassDebugging >= Structure)
70 P->dumpPassStructure();
71 }
Chris Lattner198cf422002-07-30 16:27:02 +000072 }
Chris Lattner1e4867f2002-07-30 19:51:02 +000073
74 static void PrintArgumentInformation(const Pass *P);
Chris Lattnera454b5b2002-04-28 05:14:06 +000075 static void PrintPassInformation(unsigned,const char*,Pass *, Annotable *);
Chris Lattnerac3e0602002-01-31 18:32:27 +000076 static void PrintAnalysisSetInfo(unsigned,const char*,Pass *P,
Chris Lattnerc8e66542002-04-27 06:56:12 +000077 const std::vector<AnalysisID> &);
Chris Lattner67d25652002-01-30 23:20:39 +000078};
79
80
Chris Lattnere2eb99e2002-04-29 04:04:29 +000081//===----------------------------------------------------------------------===//
82// TimingInfo Class - This class is used to calculate information about the
83// amount of time each pass takes to execute. This only happens when
84// -time-passes is enabled on the command line.
85//
Chris Lattner6a33d6f2002-08-01 19:33:09 +000086
Chris Lattnere2eb99e2002-04-29 04:04:29 +000087class TimingInfo {
Chris Lattneraf751b82002-10-01 19:54:07 +000088 std::map<Pass*, Timer> TimingData;
89 TimerGroup TG;
90
91 // Private ctor, must use 'create' member
92 TimingInfo() : TG("... Pass execution timing report ...") {}
Chris Lattnere2eb99e2002-04-29 04:04:29 +000093public:
Chris Lattnere2eb99e2002-04-29 04:04:29 +000094 // TimingDtor - Print out information about timing information
Chris Lattneraf751b82002-10-01 19:54:07 +000095 ~TimingInfo() {
96 // Delete all of the timers...
97 TimingData.clear();
98 // TimerGroup is deleted next, printing the report.
99 }
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000100
Chris Lattnere7e09442003-08-14 05:20:28 +0000101 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
102 // to a non null value (if the -time-passes option is enabled) or it leaves it
103 // null. It may be called multiple times.
104 static void createTheTimeInfo();
105
Chris Lattneraf751b82002-10-01 19:54:07 +0000106 void passStarted(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 if (I == TimingData.end())
Chris Lattner52db2712002-10-01 20:12:06 +0000110 I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first;
Chris Lattneraf751b82002-10-01 19:54:07 +0000111 I->second.startTimer();
112 }
113 void passEnded(Pass *P) {
Chris Lattnerd5fc9022002-10-01 20:08:11 +0000114 if (dynamic_cast<AnalysisResolver*>(P)) return;
Chris Lattneraf751b82002-10-01 19:54:07 +0000115 std::map<Pass*, Timer>::iterator I = TimingData.find(P);
116 assert (I != TimingData.end() && "passStarted/passEnded not nested right!");
117 I->second.stopTimer();
118 }
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000119};
120
Chris Lattnere7e09442003-08-14 05:20:28 +0000121static TimingInfo *TheTimeInfo;
122
Chris Lattner6e041bd2002-08-21 22:17:09 +0000123//===----------------------------------------------------------------------===//
Chris Lattner67d25652002-01-30 23:20:39 +0000124// Declare the PassManagerTraits which will be specialized...
125//
126template<class UnitType> class PassManagerTraits; // Do not define.
127
128
129//===----------------------------------------------------------------------===//
130// PassManagerT - Container object for passes. The PassManagerT destructor
131// deletes all passes contained inside of the PassManagerT, so you shouldn't
132// delete passes manually, and all passes should be dynamically allocated.
133//
134template<typename UnitType>
135class PassManagerT : public PassManagerTraits<UnitType>,public AnalysisResolver{
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000136 typedef PassManagerTraits<UnitType> Traits;
137 typedef typename Traits::PassClass PassClass;
138 typedef typename Traits::SubPassClass SubPassClass;
139 typedef typename Traits::BatcherClass BatcherClass;
140 typedef typename Traits::ParentClass ParentClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000141
Chris Lattner4c9cd822003-06-23 19:16:20 +0000142 friend class PassManagerTraits<UnitType>::PassClass;
143 friend class PassManagerTraits<UnitType>::SubPassClass;
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000144 friend class Traits;
Chris Lattneree0788d2002-09-25 21:59:11 +0000145 friend class ImmutablePass;
Chris Lattner67d25652002-01-30 23:20:39 +0000146
Chris Lattner73503172002-07-29 21:03:38 +0000147 std::vector<PassClass*> Passes; // List of passes to run
Chris Lattneree0788d2002-09-25 21:59:11 +0000148 std::vector<ImmutablePass*> ImmutablePasses; // List of immutable passes
Chris Lattner67d25652002-01-30 23:20:39 +0000149
150 // The parent of this pass manager...
Chris Lattnerac3e0602002-01-31 18:32:27 +0000151 ParentClass * const Parent;
Chris Lattner67d25652002-01-30 23:20:39 +0000152
153 // The current batcher if one is in use, or null
154 BatcherClass *Batcher;
155
156 // CurrentAnalyses - As the passes are being run, this map contains the
157 // analyses that are available to the current pass for use. This is accessed
158 // through the getAnalysis() function in this class and in Pass.
159 //
160 std::map<AnalysisID, Pass*> CurrentAnalyses;
161
Chris Lattnerac3e0602002-01-31 18:32:27 +0000162 // LastUseOf - This map keeps track of the last usage in our pipeline of a
163 // particular pass. When executing passes, the memory for .first is free'd
164 // after .second is run.
165 //
166 std::map<Pass*, Pass*> LastUseOf;
167
Chris Lattner67d25652002-01-30 23:20:39 +0000168public:
169 PassManagerT(ParentClass *Par = 0) : Parent(Par), Batcher(0) {}
170 ~PassManagerT() {
171 // Delete all of the contained passes...
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000172 for (typename std::vector<PassClass*>::iterator
173 I = Passes.begin(), E = Passes.end(); I != E; ++I)
Chris Lattner67d25652002-01-30 23:20:39 +0000174 delete *I;
Chris Lattneree0788d2002-09-25 21:59:11 +0000175
176 for (std::vector<ImmutablePass*>::iterator
177 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
178 delete *I;
Chris Lattner67d25652002-01-30 23:20:39 +0000179 }
180
181 // run - Run all of the queued passes on the specified module in an optimal
182 // way.
183 virtual bool runOnUnit(UnitType *M) {
184 bool MadeChanges = false;
185 closeBatcher();
186 CurrentAnalyses.clear();
187
Chris Lattnere7e09442003-08-14 05:20:28 +0000188 TimingInfo::createTheTimeInfo();
189
Chris Lattneree0788d2002-09-25 21:59:11 +0000190 // Add any immutable passes to the CurrentAnalyses set...
Chris Lattner480b37d2002-09-25 22:26:52 +0000191 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
192 ImmutablePass *IPass = ImmutablePasses[i];
193 if (const PassInfo *PI = IPass->getPassInfo()) {
194 CurrentAnalyses[PI] = IPass;
Chris Lattneree0788d2002-09-25 21:59:11 +0000195
196 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
197 for (unsigned i = 0, e = II.size(); i != e; ++i)
Chris Lattner480b37d2002-09-25 22:26:52 +0000198 CurrentAnalyses[II[i]] = IPass;
Chris Lattneree0788d2002-09-25 21:59:11 +0000199 }
Chris Lattner480b37d2002-09-25 22:26:52 +0000200 }
Chris Lattneree0788d2002-09-25 21:59:11 +0000201
Chris Lattnerac3e0602002-01-31 18:32:27 +0000202 // LastUserOf - This contains the inverted LastUseOfMap...
203 std::map<Pass *, std::vector<Pass*> > LastUserOf;
204 for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(),
205 E = LastUseOf.end(); I != E; ++I)
206 LastUserOf[I->second].push_back(I->first);
207
208
Chris Lattner67d25652002-01-30 23:20:39 +0000209 // Output debug information...
Chris Lattner1e4867f2002-07-30 19:51:02 +0000210 if (Parent == 0) PMDebug::PerformPassStartupStuff(this);
Chris Lattner67d25652002-01-30 23:20:39 +0000211
212 // Run all of the passes
213 for (unsigned i = 0, e = Passes.size(); i < e; ++i) {
214 PassClass *P = Passes[i];
215
Chris Lattnera454b5b2002-04-28 05:14:06 +0000216 PMDebug::PrintPassInformation(getDepth(), "Executing Pass", P,
217 (Annotable*)M);
Chris Lattner67d25652002-01-30 23:20:39 +0000218
219 // Get information about what analyses the pass uses...
Chris Lattnerc8e66542002-04-27 06:56:12 +0000220 AnalysisUsage AnUsage;
221 P->getAnalysisUsage(AnUsage);
222 PMDebug::PrintAnalysisSetInfo(getDepth(), "Required", P,
223 AnUsage.getRequiredSet());
Chris Lattner67d25652002-01-30 23:20:39 +0000224
Chris Lattner216edd42002-08-30 20:25:01 +0000225 // All Required analyses should be available to the pass as it runs! Here
226 // we fill in the AnalysisImpls member of the pass so that it can
227 // successfully use the getAnalysis() method to retrieve the
228 // implementations it needs.
229 //
230 P->AnalysisImpls.clear();
231 P->AnalysisImpls.reserve(AnUsage.getRequiredSet().size());
232 for (std::vector<const PassInfo *>::const_iterator
Chris Lattnerc8e66542002-04-27 06:56:12 +0000233 I = AnUsage.getRequiredSet().begin(),
234 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Chris Lattner216edd42002-08-30 20:25:01 +0000235 Pass *Impl = getAnalysisOrNullUp(*I);
236 if (Impl == 0) {
237 std::cerr << "Analysis '" << (*I)->getPassName()
238 << "' used but not available!";
239 assert(0 && "Analysis used but not available!");
240 } else if (PassDebugging == Details) {
241 if ((*I)->getPassName() != std::string(Impl->getPassName()))
242 std::cerr << " Interface '" << (*I)->getPassName()
243 << "' implemented by '" << Impl->getPassName() << "'\n";
244 }
245 P->AnalysisImpls.push_back(std::make_pair(*I, Impl));
Chris Lattner67d25652002-01-30 23:20:39 +0000246 }
Chris Lattner67d25652002-01-30 23:20:39 +0000247
248 // Run the sub pass!
Chris Lattnere7e09442003-08-14 05:20:28 +0000249 if (TheTimeInfo) TheTimeInfo->passStarted(P);
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000250 bool Changed = runPass(P, M);
Chris Lattnere7e09442003-08-14 05:20:28 +0000251 if (TheTimeInfo) TheTimeInfo->passEnded(P);
Chris Lattnerf2f31bd2002-02-01 04:53:36 +0000252 MadeChanges |= Changed;
Chris Lattner67d25652002-01-30 23:20:39 +0000253
Chris Lattnered6504b2002-09-08 19:00:07 +0000254 // Check for memory leaks by the pass...
255 LeakDetector::checkForGarbage(std::string("after running pass '") +
256 P->getPassName() + "'");
257
Chris Lattnerf2f31bd2002-02-01 04:53:36 +0000258 if (Changed)
259 PMDebug::PrintPassInformation(getDepth()+1, "Made Modification", P,
Chris Lattnera454b5b2002-04-28 05:14:06 +0000260 (Annotable*)M);
Chris Lattnerc8e66542002-04-27 06:56:12 +0000261 PMDebug::PrintAnalysisSetInfo(getDepth(), "Preserved", P,
262 AnUsage.getPreservedSet());
Chris Lattner67d25652002-01-30 23:20:39 +0000263
Chris Lattnerc8e66542002-04-27 06:56:12 +0000264
265 // Erase all analyses not in the preserved set...
Chris Lattner820d9712002-10-21 20:00:28 +0000266 if (!AnUsage.getPreservesAll()) {
Chris Lattnerc8e66542002-04-27 06:56:12 +0000267 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
268 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(),
269 E = CurrentAnalyses.end(); I != E; )
270 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) !=
271 PreservedSet.end())
272 ++I; // This analysis is preserved, leave it in the available set...
273 else {
Chris Lattneree0788d2002-09-25 21:59:11 +0000274 if (!dynamic_cast<ImmutablePass*>(I->second)) {
Chris Lattnerade85ec2003-02-14 05:34:36 +0000275 std::map<AnalysisID, Pass*>::iterator J = I++;
276 CurrentAnalyses.erase(J); // Analysis not preserved!
Chris Lattneree0788d2002-09-25 21:59:11 +0000277 } else {
278 ++I;
279 }
Chris Lattnerc8e66542002-04-27 06:56:12 +0000280 }
281 }
282
Chris Lattner73503172002-07-29 21:03:38 +0000283 // Add the current pass to the set of passes that have been run, and are
284 // thus available to users.
285 //
Chris Lattner216edd42002-08-30 20:25:01 +0000286 if (const PassInfo *PI = P->getPassInfo()) {
Chris Lattner73503172002-07-29 21:03:38 +0000287 CurrentAnalyses[PI] = P;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000288
Chris Lattner216edd42002-08-30 20:25:01 +0000289 // This pass is the current implementation of all of the interfaces it
290 // implements as well.
291 //
292 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
293 for (unsigned i = 0, e = II.size(); i != e; ++i)
294 CurrentAnalyses[II[i]] = P;
295 }
296
Chris Lattnerac3e0602002-01-31 18:32:27 +0000297 // Free memory for any passes that we are the last use of...
298 std::vector<Pass*> &DeadPass = LastUserOf[P];
299 for (std::vector<Pass*>::iterator I = DeadPass.begin(),E = DeadPass.end();
300 I != E; ++I) {
301 PMDebug::PrintPassInformation(getDepth()+1, "Freeing Pass", *I,
Chris Lattnera454b5b2002-04-28 05:14:06 +0000302 (Annotable*)M);
Chris Lattnerac3e0602002-01-31 18:32:27 +0000303 (*I)->releaseMemory();
304 }
Chris Lattner6b6f5402002-09-29 22:50:22 +0000305
306 // Make sure to remove dead passes from the CurrentAnalyses list...
307 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin();
308 I != CurrentAnalyses.end(); ) {
309 std::vector<Pass*>::iterator DPI = std::find(DeadPass.begin(),
310 DeadPass.end(), I->second);
311 if (DPI != DeadPass.end()) { // This pass is dead now... remove it
312 std::map<AnalysisID, Pass*>::iterator IDead = I++;
313 CurrentAnalyses.erase(IDead);
314 } else {
315 ++I; // Move on to the next element...
316 }
317 }
Chris Lattner67d25652002-01-30 23:20:39 +0000318 }
Chris Lattnere7e09442003-08-14 05:20:28 +0000319
Chris Lattner67d25652002-01-30 23:20:39 +0000320 return MadeChanges;
321 }
322
Chris Lattnerac3e0602002-01-31 18:32:27 +0000323 // dumpPassStructure - Implement the -debug-passes=PassStructure option
324 virtual void dumpPassStructure(unsigned Offset = 0) {
Chris Lattner480b37d2002-09-25 22:26:52 +0000325 // Print out the immutable passes...
326 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i)
327 ImmutablePasses[i]->dumpPassStructure(0);
328
Chris Lattnerac3e0602002-01-31 18:32:27 +0000329 std::cerr << std::string(Offset*2, ' ') << Traits::getPMName()
330 << " Pass Manager\n";
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000331 for (typename std::vector<PassClass*>::iterator
332 I = Passes.begin(), E = Passes.end(); I != E; ++I) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000333 PassClass *P = *I;
334 P->dumpPassStructure(Offset+1);
335
336 // Loop through and see which classes are destroyed after this one...
337 for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(),
338 E = LastUseOf.end(); I != E; ++I) {
339 if (P == I->second) {
Chris Lattner73503172002-07-29 21:03:38 +0000340 std::cerr << "--" << std::string(Offset*2, ' ');
Chris Lattnerac3e0602002-01-31 18:32:27 +0000341 I->first->dumpPassStructure(0);
342 }
343 }
344 }
345 }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000346
Chris Lattner60430422003-04-24 20:07:38 +0000347 Pass *getImmutablePassOrNull(const PassInfo *ID) const {
348 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
349 const PassInfo *IPID = ImmutablePasses[i]->getPassInfo();
350 if (IPID == ID)
351 return ImmutablePasses[i];
352
353 // This pass is the current implementation of all of the interfaces it
354 // implements as well.
355 //
356 const std::vector<const PassInfo*> &II =
357 IPID->getInterfacesImplemented();
358 for (unsigned j = 0, e = II.size(); j != e; ++j)
359 if (II[j] == ID) return ImmutablePasses[i];
360 }
361 return 0;
362 }
363
Chris Lattner6e041bd2002-08-21 22:17:09 +0000364 Pass *getAnalysisOrNullDown(const PassInfo *ID) const {
Chris Lattner216edd42002-08-30 20:25:01 +0000365 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000366
367 if (I != CurrentAnalyses.end())
368 return I->second; // Found it.
369
Chris Lattner60430422003-04-24 20:07:38 +0000370 if (Pass *P = getImmutablePassOrNull(ID))
371 return P;
372
Chris Lattner6e041bd2002-08-21 22:17:09 +0000373 if (Batcher)
374 return ((AnalysisResolver*)Batcher)->getAnalysisOrNullDown(ID);
375 return 0;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000376 }
377
Chris Lattner6e041bd2002-08-21 22:17:09 +0000378 Pass *getAnalysisOrNullUp(const PassInfo *ID) const {
Chris Lattner216edd42002-08-30 20:25:01 +0000379 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000380 if (I != CurrentAnalyses.end())
381 return I->second; // Found it.
382
383 if (Parent) // Try scanning...
384 return Parent->getAnalysisOrNullUp(ID);
Chris Lattner60430422003-04-24 20:07:38 +0000385 else if (!ImmutablePasses.empty())
386 return getImmutablePassOrNull(ID);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000387 return 0;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000388 }
389
390 // markPassUsed - Inform higher level pass managers (and ourselves)
391 // that these analyses are being used by this pass. This is used to
392 // make sure that analyses are not free'd before we have to use
393 // them...
394 //
Chris Lattner6e041bd2002-08-21 22:17:09 +0000395 void markPassUsed(const PassInfo *P, Pass *User) {
Chris Lattner216edd42002-08-30 20:25:01 +0000396 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(P);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000397
Chris Lattnerac3e0602002-01-31 18:32:27 +0000398 if (I != CurrentAnalyses.end()) {
399 LastUseOf[I->second] = User; // Local pass, extend the lifetime
400 } else {
401 // Pass not in current available set, must be a higher level pass
Misha Brukman632df282002-10-29 23:06:16 +0000402 // available to us, propagate to parent pass manager... We tell the
Chris Lattnerac3e0602002-01-31 18:32:27 +0000403 // parent that we (the passmanager) are using the analysis so that it
404 // frees the analysis AFTER this pass manager runs.
405 //
Chris Lattneree0788d2002-09-25 21:59:11 +0000406 if (Parent) {
407 Parent->markPassUsed(P, this);
408 } else {
Chris Lattner60430422003-04-24 20:07:38 +0000409 assert(getAnalysisOrNullUp(P) &&
410 dynamic_cast<ImmutablePass*>(getAnalysisOrNullUp(P)) &&
411 "Pass available but not found! "
Chris Lattneree0788d2002-09-25 21:59:11 +0000412 "Perhaps this is a module pass requiring a function pass?");
413 }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000414 }
415 }
Chris Lattneree0788d2002-09-25 21:59:11 +0000416
Chris Lattnerac3e0602002-01-31 18:32:27 +0000417 // Return the number of parent PassManagers that exist
418 virtual unsigned getDepth() const {
419 if (Parent == 0) return 0;
420 return 1 + Parent->getDepth();
421 }
422
Chris Lattner1e4867f2002-07-30 19:51:02 +0000423 virtual unsigned getNumContainedPasses() const { return Passes.size(); }
424 virtual const Pass *getContainedPass(unsigned N) const {
425 assert(N < Passes.size() && "Pass number out of range!");
426 return Passes[N];
427 }
428
Chris Lattner6e041bd2002-08-21 22:17:09 +0000429 // add - Add a pass to the queue of passes to run. This gives ownership of
Chris Lattner67d25652002-01-30 23:20:39 +0000430 // the Pass to the PassManager. When the PassManager is destroyed, the pass
Chris Lattnerac3e0602002-01-31 18:32:27 +0000431 // will be destroyed as well, so there is no need to delete the pass. This
432 // implies that all passes MUST be new'd.
Chris Lattner67d25652002-01-30 23:20:39 +0000433 //
434 void add(PassClass *P) {
435 // Get information about what analyses the pass uses...
Chris Lattnerc8e66542002-04-27 06:56:12 +0000436 AnalysisUsage AnUsage;
437 P->getAnalysisUsage(AnUsage);
438 const std::vector<AnalysisID> &Required = AnUsage.getRequiredSet();
Chris Lattner67d25652002-01-30 23:20:39 +0000439
440 // Loop over all of the analyses used by this pass,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000441 for (std::vector<AnalysisID>::const_iterator I = Required.begin(),
442 E = Required.end(); I != E; ++I) {
Chris Lattner67d25652002-01-30 23:20:39 +0000443 if (getAnalysisOrNullDown(*I) == 0)
Chris Lattner26750072002-07-27 01:12:17 +0000444 add((PassClass*)(*I)->createPass());
Chris Lattner67d25652002-01-30 23:20:39 +0000445 }
446
447 // Tell the pass to add itself to this PassManager... the way it does so
448 // depends on the class of the pass, and is critical to laying out passes in
449 // an optimal order..
450 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000451 P->addToPassManager(this, AnUsage);
Chris Lattner67d25652002-01-30 23:20:39 +0000452 }
453
Brian Gaeke2cc4b9d2003-08-14 06:07:57 +0000454 // add - H4x0r an ImmutablePass into a PassManager that might not be
455 // expecting one.
456 //
457 void add(ImmutablePass *P) {
458 // Get information about what analyses the pass uses...
459 AnalysisUsage AnUsage;
460 P->getAnalysisUsage(AnUsage);
461 const std::vector<AnalysisID> &Required = AnUsage.getRequiredSet();
Chris Lattner67d25652002-01-30 23:20:39 +0000462
Brian Gaeke2cc4b9d2003-08-14 06:07:57 +0000463 // Loop over all of the analyses used by this pass,
464 for (std::vector<AnalysisID>::const_iterator I = Required.begin(),
465 E = Required.end(); I != E; ++I) {
466 if (getAnalysisOrNullDown(*I) == 0)
467 add((PassClass*)(*I)->createPass());
468 }
469
470 // Add the ImmutablePass to this PassManager.
471 addPass(P, AnUsage);
472 }
473
474private:
Chris Lattner67d25652002-01-30 23:20:39 +0000475 // addPass - These functions are used to implement the subclass specific
476 // behaviors present in PassManager. Basically the add(Pass*) method ends up
477 // reflecting its behavior into a Pass::addToPassManager call. Subclasses of
478 // Pass override it specifically so that they can reflect the type
479 // information inherent in "this" back to the PassManager.
480 //
481 // For generic Pass subclasses (which are interprocedural passes), we simply
482 // add the pass to the end of the pass list and terminate any accumulation of
Chris Lattnerc8e66542002-04-27 06:56:12 +0000483 // FunctionPass's that are present.
Chris Lattner67d25652002-01-30 23:20:39 +0000484 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000485 void addPass(PassClass *P, AnalysisUsage &AnUsage) {
486 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
Chris Lattnerc8e66542002-04-27 06:56:12 +0000487
Chris Lattner73503172002-07-29 21:03:38 +0000488 // FIXME: If this pass being added isn't killed by any of the passes in the
489 // batcher class then we can reorder to pass to execute before the batcher
490 // does, which will potentially allow us to batch more passes!
Chris Lattner67d25652002-01-30 23:20:39 +0000491 //
Chris Lattner73503172002-07-29 21:03:38 +0000492 //const std::vector<AnalysisID> &ProvidedSet = AnUsage.getProvidedSet();
493 if (Batcher /*&& ProvidedSet.empty()*/)
Chris Lattner67d25652002-01-30 23:20:39 +0000494 closeBatcher(); // This pass cannot be batched!
495
496 // Set the Resolver instance variable in the Pass so that it knows where to
497 // find this object...
498 //
499 setAnalysisResolver(P, this);
500 Passes.push_back(P);
501
Chris Lattnerac3e0602002-01-31 18:32:27 +0000502 // Inform higher level pass managers (and ourselves) that these analyses are
503 // being used by this pass. This is used to make sure that analyses are not
504 // free'd before we have to use them...
505 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000506 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
507 E = RequiredSet.end(); I != E; ++I)
Chris Lattnerac3e0602002-01-31 18:32:27 +0000508 markPassUsed(*I, P); // Mark *I as used by P
509
Chris Lattnerc8e66542002-04-27 06:56:12 +0000510 // Erase all analyses not in the preserved set...
Chris Lattner820d9712002-10-21 20:00:28 +0000511 if (!AnUsage.getPreservesAll()) {
Chris Lattnerc8e66542002-04-27 06:56:12 +0000512 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
513 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(),
Chris Lattneree0788d2002-09-25 21:59:11 +0000514 E = CurrentAnalyses.end(); I != E; ) {
515 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) ==
516 PreservedSet.end()) { // Analysis not preserved!
517 CurrentAnalyses.erase(I); // Remove from available analyses
Chris Lattnerc8e66542002-04-27 06:56:12 +0000518 I = CurrentAnalyses.begin();
Chris Lattneree0788d2002-09-25 21:59:11 +0000519 } else {
520 ++I;
Chris Lattnerc8e66542002-04-27 06:56:12 +0000521 }
Chris Lattneree0788d2002-09-25 21:59:11 +0000522 }
Chris Lattnerc8e66542002-04-27 06:56:12 +0000523 }
Chris Lattner67d25652002-01-30 23:20:39 +0000524
Chris Lattner73503172002-07-29 21:03:38 +0000525 // Add this pass to the currently available set...
Chris Lattner216edd42002-08-30 20:25:01 +0000526 if (const PassInfo *PI = P->getPassInfo()) {
Chris Lattner73503172002-07-29 21:03:38 +0000527 CurrentAnalyses[PI] = P;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000528
Chris Lattner216edd42002-08-30 20:25:01 +0000529 // This pass is the current implementation of all of the interfaces it
530 // implements as well.
531 //
532 const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented();
533 for (unsigned i = 0, e = II.size(); i != e; ++i)
534 CurrentAnalyses[II[i]] = P;
535 }
536
Chris Lattnerac3e0602002-01-31 18:32:27 +0000537 // For now assume that our results are never used...
538 LastUseOf[P] = P;
Chris Lattner67d25652002-01-30 23:20:39 +0000539 }
540
Chris Lattnerc8e66542002-04-27 06:56:12 +0000541 // For FunctionPass subclasses, we must be sure to batch the FunctionPass's
542 // together in a BatcherClass object so that all of the analyses are run
543 // together a function at a time.
Chris Lattner67d25652002-01-30 23:20:39 +0000544 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000545 void addPass(SubPassClass *MP, AnalysisUsage &AnUsage) {
Chris Lattner67d25652002-01-30 23:20:39 +0000546 if (Batcher == 0) // If we don't have a batcher yet, make one now.
547 Batcher = new BatcherClass(this);
Chris Lattner73503172002-07-29 21:03:38 +0000548 // The Batcher will queue the passes up
Chris Lattnerc8e66542002-04-27 06:56:12 +0000549 MP->addToPassManager(Batcher, AnUsage);
Chris Lattner67d25652002-01-30 23:20:39 +0000550 }
551
552 // closeBatcher - Terminate the batcher that is being worked on.
553 void closeBatcher() {
554 if (Batcher) {
555 Passes.push_back(Batcher);
556 Batcher = 0;
557 }
558 }
Chris Lattneree0788d2002-09-25 21:59:11 +0000559
560public:
561 // When an ImmutablePass is added, it gets added to the top level pass
562 // manager.
563 void addPass(ImmutablePass *IP, AnalysisUsage &AU) {
564 if (Parent) { // Make sure this request goes to the top level passmanager...
565 Parent->addPass(IP, AU);
566 return;
567 }
568
569 // Set the Resolver instance variable in the Pass so that it knows where to
570 // find this object...
571 //
572 setAnalysisResolver(IP, this);
573 ImmutablePasses.push_back(IP);
Chris Lattnerd4ed5c62003-02-26 19:10:57 +0000574
575 // All Required analyses should be available to the pass as it initializes!
576 // Here we fill in the AnalysisImpls member of the pass so that it can
577 // successfully use the getAnalysis() method to retrieve the implementations
578 // it needs.
579 //
580 IP->AnalysisImpls.clear();
581 IP->AnalysisImpls.reserve(AU.getRequiredSet().size());
582 for (std::vector<const PassInfo *>::const_iterator
583 I = AU.getRequiredSet().begin(),
584 E = AU.getRequiredSet().end(); I != E; ++I) {
585 Pass *Impl = getAnalysisOrNullUp(*I);
586 if (Impl == 0) {
587 std::cerr << "Analysis '" << (*I)->getPassName()
588 << "' used but not available!";
589 assert(0 && "Analysis used but not available!");
590 } else if (PassDebugging == Details) {
591 if ((*I)->getPassName() != std::string(Impl->getPassName()))
592 std::cerr << " Interface '" << (*I)->getPassName()
593 << "' implemented by '" << Impl->getPassName() << "'\n";
594 }
595 IP->AnalysisImpls.push_back(std::make_pair(*I, Impl));
596 }
Chris Lattneree0788d2002-09-25 21:59:11 +0000597
Chris Lattnerd4ed5c62003-02-26 19:10:57 +0000598 // Initialize the immutable pass...
599 IP->initializePass();
Chris Lattneree0788d2002-09-25 21:59:11 +0000600 }
Chris Lattner67d25652002-01-30 23:20:39 +0000601};
602
603
604
605//===----------------------------------------------------------------------===//
606// PassManagerTraits<BasicBlock> Specialization
607//
608// This pass manager is used to group together all of the BasicBlockPass's
609// into a single unit.
610//
611template<> struct PassManagerTraits<BasicBlock> : public BasicBlockPass {
612 // PassClass - The type of passes tracked by this PassManager
613 typedef BasicBlockPass PassClass;
614
615 // SubPassClass - The types of classes that should be collated together
616 // This is impossible to match, so BasicBlock instantiations of PassManagerT
617 // do not collate.
618 //
619 typedef PassManagerT<Module> SubPassClass;
620
621 // BatcherClass - The type to use for collation of subtypes... This class is
622 // never instantiated for the PassManager<BasicBlock>, but it must be an
623 // instance of PassClass to typecheck.
624 //
625 typedef PassClass BatcherClass;
626
627 // ParentClass - The type of the parent PassManager...
Chris Lattner4e8c4872002-03-23 22:51:58 +0000628 typedef PassManagerT<Function> ParentClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000629
Chris Lattner2eaac392002-01-31 00:40:44 +0000630 // PMType - The type of the passmanager that subclasses this class
631 typedef PassManagerT<BasicBlock> PMType;
632
Chris Lattner67d25652002-01-30 23:20:39 +0000633 // runPass - Specify how the pass should be run on the UnitType
634 static bool runPass(PassClass *P, BasicBlock *M) {
635 // todo, init and finalize
Chris Lattner113f4f42002-06-25 16:13:24 +0000636 return P->runOnBasicBlock(*M);
Chris Lattner67d25652002-01-30 23:20:39 +0000637 }
638
Chris Lattnerac3e0602002-01-31 18:32:27 +0000639 // getPMName() - Return the name of the unit the PassManager operates on for
640 // debugging.
641 const char *getPMName() const { return "BasicBlock"; }
Chris Lattner37104aa2002-04-29 14:57:45 +0000642 virtual const char *getPassName() const { return "BasicBlock Pass Manager"; }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000643
Chris Lattner2eaac392002-01-31 00:40:44 +0000644 // Implement the BasicBlockPass interface...
Chris Lattner113f4f42002-06-25 16:13:24 +0000645 virtual bool doInitialization(Module &M);
Chris Lattnerbae3c672002-09-12 17:06:40 +0000646 virtual bool doInitialization(Function &F);
Chris Lattner113f4f42002-06-25 16:13:24 +0000647 virtual bool runOnBasicBlock(BasicBlock &BB);
Chris Lattnerbae3c672002-09-12 17:06:40 +0000648 virtual bool doFinalization(Function &F);
Chris Lattner113f4f42002-06-25 16:13:24 +0000649 virtual bool doFinalization(Module &M);
Chris Lattnercb429062002-04-30 18:50:17 +0000650
651 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
652 AU.setPreservesAll();
653 }
Chris Lattner67d25652002-01-30 23:20:39 +0000654};
655
656
657
658//===----------------------------------------------------------------------===//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000659// PassManagerTraits<Function> Specialization
Chris Lattner67d25652002-01-30 23:20:39 +0000660//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000661// This pass manager is used to group together all of the FunctionPass's
Chris Lattner67d25652002-01-30 23:20:39 +0000662// into a single unit.
663//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000664template<> struct PassManagerTraits<Function> : public FunctionPass {
Chris Lattner67d25652002-01-30 23:20:39 +0000665 // PassClass - The type of passes tracked by this PassManager
Chris Lattnerc8e66542002-04-27 06:56:12 +0000666 typedef FunctionPass PassClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000667
668 // SubPassClass - The types of classes that should be collated together
669 typedef BasicBlockPass SubPassClass;
670
671 // BatcherClass - The type to use for collation of subtypes...
672 typedef PassManagerT<BasicBlock> BatcherClass;
673
674 // ParentClass - The type of the parent PassManager...
675 typedef PassManagerT<Module> ParentClass;
676
677 // PMType - The type of the passmanager that subclasses this class
Chris Lattner4e8c4872002-03-23 22:51:58 +0000678 typedef PassManagerT<Function> PMType;
Chris Lattner67d25652002-01-30 23:20:39 +0000679
680 // runPass - Specify how the pass should be run on the UnitType
Chris Lattnerc8e66542002-04-27 06:56:12 +0000681 static bool runPass(PassClass *P, Function *F) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000682 return P->runOnFunction(*F);
Chris Lattner67d25652002-01-30 23:20:39 +0000683 }
684
Chris Lattnerac3e0602002-01-31 18:32:27 +0000685 // getPMName() - Return the name of the unit the PassManager operates on for
686 // debugging.
Chris Lattner4e8c4872002-03-23 22:51:58 +0000687 const char *getPMName() const { return "Function"; }
Chris Lattner37104aa2002-04-29 14:57:45 +0000688 virtual const char *getPassName() const { return "Function Pass Manager"; }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000689
Chris Lattnerc8e66542002-04-27 06:56:12 +0000690 // Implement the FunctionPass interface...
Chris Lattner113f4f42002-06-25 16:13:24 +0000691 virtual bool doInitialization(Module &M);
692 virtual bool runOnFunction(Function &F);
693 virtual bool doFinalization(Module &M);
Chris Lattnercb429062002-04-30 18:50:17 +0000694
695 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
696 AU.setPreservesAll();
697 }
Chris Lattner67d25652002-01-30 23:20:39 +0000698};
699
700
701
702//===----------------------------------------------------------------------===//
703// PassManagerTraits<Module> Specialization
704//
705// This is the top level PassManager implementation that holds generic passes.
706//
707template<> struct PassManagerTraits<Module> : public Pass {
708 // PassClass - The type of passes tracked by this PassManager
709 typedef Pass PassClass;
710
711 // SubPassClass - The types of classes that should be collated together
Chris Lattnerc8e66542002-04-27 06:56:12 +0000712 typedef FunctionPass SubPassClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000713
714 // BatcherClass - The type to use for collation of subtypes...
Chris Lattner4e8c4872002-03-23 22:51:58 +0000715 typedef PassManagerT<Function> BatcherClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000716
717 // ParentClass - The type of the parent PassManager...
Chris Lattnerac3e0602002-01-31 18:32:27 +0000718 typedef AnalysisResolver ParentClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000719
720 // runPass - Specify how the pass should be run on the UnitType
Chris Lattner113f4f42002-06-25 16:13:24 +0000721 static bool runPass(PassClass *P, Module *M) { return P->run(*M); }
Chris Lattner67d25652002-01-30 23:20:39 +0000722
Chris Lattnerac3e0602002-01-31 18:32:27 +0000723 // getPMName() - Return the name of the unit the PassManager operates on for
724 // debugging.
725 const char *getPMName() const { return "Module"; }
Chris Lattner37104aa2002-04-29 14:57:45 +0000726 virtual const char *getPassName() const { return "Module Pass Manager"; }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000727
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000728 // run - Implement the PassManager interface...
Chris Lattner113f4f42002-06-25 16:13:24 +0000729 bool run(Module &M) {
Chris Lattnere7e09442003-08-14 05:20:28 +0000730 return ((PassManagerT<Module>*)this)->runOnUnit(&M);
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000731 }
Chris Lattner67d25652002-01-30 23:20:39 +0000732};
733
734
735
736//===----------------------------------------------------------------------===//
737// PassManagerTraits Method Implementations
738//
739
740// PassManagerTraits<BasicBlock> Implementations
741//
Chris Lattner113f4f42002-06-25 16:13:24 +0000742inline bool PassManagerTraits<BasicBlock>::doInitialization(Module &M) {
Chris Lattner2eaac392002-01-31 00:40:44 +0000743 bool Changed = false;
744 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
745 ((PMType*)this)->Passes[i]->doInitialization(M);
746 return Changed;
747}
748
Chris Lattnerbae3c672002-09-12 17:06:40 +0000749inline bool PassManagerTraits<BasicBlock>::doInitialization(Function &F) {
750 bool Changed = false;
751 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
752 ((PMType*)this)->Passes[i]->doInitialization(F);
753 return Changed;
754}
755
Chris Lattner113f4f42002-06-25 16:13:24 +0000756inline bool PassManagerTraits<BasicBlock>::runOnBasicBlock(BasicBlock &BB) {
757 return ((PMType*)this)->runOnUnit(&BB);
Chris Lattner2eaac392002-01-31 00:40:44 +0000758}
759
Chris Lattnerbae3c672002-09-12 17:06:40 +0000760inline bool PassManagerTraits<BasicBlock>::doFinalization(Function &F) {
761 bool Changed = false;
762 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
763 ((PMType*)this)->Passes[i]->doFinalization(F);
764 return Changed;
765}
766
Chris Lattner113f4f42002-06-25 16:13:24 +0000767inline bool PassManagerTraits<BasicBlock>::doFinalization(Module &M) {
Chris Lattner2eaac392002-01-31 00:40:44 +0000768 bool Changed = false;
769 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
770 ((PMType*)this)->Passes[i]->doFinalization(M);
771 return Changed;
Chris Lattner67d25652002-01-30 23:20:39 +0000772}
773
774
Chris Lattner4e8c4872002-03-23 22:51:58 +0000775// PassManagerTraits<Function> Implementations
Chris Lattner67d25652002-01-30 23:20:39 +0000776//
Chris Lattner113f4f42002-06-25 16:13:24 +0000777inline bool PassManagerTraits<Function>::doInitialization(Module &M) {
Chris Lattner67d25652002-01-30 23:20:39 +0000778 bool Changed = false;
779 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
780 ((PMType*)this)->Passes[i]->doInitialization(M);
781 return Changed;
782}
783
Chris Lattner113f4f42002-06-25 16:13:24 +0000784inline bool PassManagerTraits<Function>::runOnFunction(Function &F) {
785 return ((PMType*)this)->runOnUnit(&F);
Chris Lattner67d25652002-01-30 23:20:39 +0000786}
787
Chris Lattner113f4f42002-06-25 16:13:24 +0000788inline bool PassManagerTraits<Function>::doFinalization(Module &M) {
Chris Lattner67d25652002-01-30 23:20:39 +0000789 bool Changed = false;
790 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
791 ((PMType*)this)->Passes[i]->doFinalization(M);
792 return Changed;
793}
794
795#endif