blob: 6e5fcbdd3e282887347d0112eb9a8c4f221cf578 [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"
19#include <string>
Chris Lattnerc8e66542002-04-27 06:56:12 +000020#include <algorithm>
Chris Lattnera454b5b2002-04-28 05:14:06 +000021class Annotable;
Chris Lattner67d25652002-01-30 23:20:39 +000022
Chris Lattner67d25652002-01-30 23:20:39 +000023//===----------------------------------------------------------------------===//
Chris Lattner57d2ba32002-04-04 19:35:24 +000024// PMDebug class - a set of debugging functions, that are not to be
25// instantiated by the template.
Chris Lattner67d25652002-01-30 23:20:39 +000026//
27struct PMDebug {
Chris Lattner67d25652002-01-30 23:20:39 +000028 // If compiled in debug mode, these functions can be enabled by setting
29 // -debug-pass on the command line of the tool being used.
30 //
31 static void PrintPassStructure(Pass *P);
Chris Lattnera454b5b2002-04-28 05:14:06 +000032 static void PrintPassInformation(unsigned,const char*,Pass *, Annotable *);
Chris Lattnerac3e0602002-01-31 18:32:27 +000033 static void PrintAnalysisSetInfo(unsigned,const char*,Pass *P,
Chris Lattnerc8e66542002-04-27 06:56:12 +000034 const std::vector<AnalysisID> &);
Chris Lattner67d25652002-01-30 23:20:39 +000035};
36
37
Chris Lattnere2eb99e2002-04-29 04:04:29 +000038//===----------------------------------------------------------------------===//
39// TimingInfo Class - This class is used to calculate information about the
40// amount of time each pass takes to execute. This only happens when
41// -time-passes is enabled on the command line.
42//
43class TimingInfo {
44 std::map<Pass*, double> TimingData;
45 TimingInfo() {} // Private ctor, must use create member
46public:
47 // Create method. If Timing is enabled, this creates and returns a new timing
48 // object, otherwise it returns null.
49 //
50 static TimingInfo *create();
51
52 // TimingDtor - Print out information about timing information
53 ~TimingInfo();
54
55 void passStarted(Pass *P);
56 void passEnded(Pass *P);
57};
58
59
Chris Lattner67d25652002-01-30 23:20:39 +000060
61//===----------------------------------------------------------------------===//
62// Declare the PassManagerTraits which will be specialized...
63//
64template<class UnitType> class PassManagerTraits; // Do not define.
65
66
67//===----------------------------------------------------------------------===//
68// PassManagerT - Container object for passes. The PassManagerT destructor
69// deletes all passes contained inside of the PassManagerT, so you shouldn't
70// delete passes manually, and all passes should be dynamically allocated.
71//
72template<typename UnitType>
73class PassManagerT : public PassManagerTraits<UnitType>,public AnalysisResolver{
Chris Lattnere2eb99e2002-04-29 04:04:29 +000074 typedef PassManagerTraits<UnitType> Traits;
75 typedef typename Traits::PassClass PassClass;
76 typedef typename Traits::SubPassClass SubPassClass;
77 typedef typename Traits::BatcherClass BatcherClass;
78 typedef typename Traits::ParentClass ParentClass;
Chris Lattner67d25652002-01-30 23:20:39 +000079
Chris Lattnere2eb99e2002-04-29 04:04:29 +000080 friend typename Traits::PassClass;
81 friend typename Traits::SubPassClass;
82 friend class Traits;
Chris Lattner67d25652002-01-30 23:20:39 +000083
Chris Lattner73503172002-07-29 21:03:38 +000084 std::vector<PassClass*> Passes; // List of passes to run
Chris Lattner67d25652002-01-30 23:20:39 +000085
86 // The parent of this pass manager...
Chris Lattnerac3e0602002-01-31 18:32:27 +000087 ParentClass * const Parent;
Chris Lattner67d25652002-01-30 23:20:39 +000088
89 // The current batcher if one is in use, or null
90 BatcherClass *Batcher;
91
92 // CurrentAnalyses - As the passes are being run, this map contains the
93 // analyses that are available to the current pass for use. This is accessed
94 // through the getAnalysis() function in this class and in Pass.
95 //
96 std::map<AnalysisID, Pass*> CurrentAnalyses;
97
Chris Lattnerac3e0602002-01-31 18:32:27 +000098 // LastUseOf - This map keeps track of the last usage in our pipeline of a
99 // particular pass. When executing passes, the memory for .first is free'd
100 // after .second is run.
101 //
102 std::map<Pass*, Pass*> LastUseOf;
103
Chris Lattner67d25652002-01-30 23:20:39 +0000104public:
105 PassManagerT(ParentClass *Par = 0) : Parent(Par), Batcher(0) {}
106 ~PassManagerT() {
107 // Delete all of the contained passes...
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000108 for (typename std::vector<PassClass*>::iterator
109 I = Passes.begin(), E = Passes.end(); I != E; ++I)
Chris Lattner67d25652002-01-30 23:20:39 +0000110 delete *I;
111 }
112
113 // run - Run all of the queued passes on the specified module in an optimal
114 // way.
115 virtual bool runOnUnit(UnitType *M) {
116 bool MadeChanges = false;
117 closeBatcher();
118 CurrentAnalyses.clear();
119
Chris Lattnerac3e0602002-01-31 18:32:27 +0000120 // LastUserOf - This contains the inverted LastUseOfMap...
121 std::map<Pass *, std::vector<Pass*> > LastUserOf;
122 for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(),
123 E = LastUseOf.end(); I != E; ++I)
124 LastUserOf[I->second].push_back(I->first);
125
126
Chris Lattner67d25652002-01-30 23:20:39 +0000127 // Output debug information...
128 if (Parent == 0) PMDebug::PrintPassStructure(this);
129
130 // Run all of the passes
131 for (unsigned i = 0, e = Passes.size(); i < e; ++i) {
132 PassClass *P = Passes[i];
133
Chris Lattnera454b5b2002-04-28 05:14:06 +0000134 PMDebug::PrintPassInformation(getDepth(), "Executing Pass", P,
135 (Annotable*)M);
Chris Lattner67d25652002-01-30 23:20:39 +0000136
137 // Get information about what analyses the pass uses...
Chris Lattnerc8e66542002-04-27 06:56:12 +0000138 AnalysisUsage AnUsage;
139 P->getAnalysisUsage(AnUsage);
140 PMDebug::PrintAnalysisSetInfo(getDepth(), "Required", P,
141 AnUsage.getRequiredSet());
Chris Lattner67d25652002-01-30 23:20:39 +0000142
143#ifndef NDEBUG
144 // All Required analyses should be available to the pass as it runs!
Anand Shukla8c377892002-06-25 22:07:38 +0000145 for (std::vector<AnalysisID>::const_iterator
Chris Lattnerc8e66542002-04-27 06:56:12 +0000146 I = AnUsage.getRequiredSet().begin(),
147 E = AnUsage.getRequiredSet().end(); I != E; ++I) {
Chris Lattner67d25652002-01-30 23:20:39 +0000148 assert(getAnalysisOrNullUp(*I) && "Analysis used but not available!");
149 }
150#endif
151
152 // Run the sub pass!
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000153 startPass(P);
154 bool Changed = runPass(P, M);
155 endPass(P);
Chris Lattnerf2f31bd2002-02-01 04:53:36 +0000156 MadeChanges |= Changed;
Chris Lattner67d25652002-01-30 23:20:39 +0000157
Chris Lattnerf2f31bd2002-02-01 04:53:36 +0000158 if (Changed)
159 PMDebug::PrintPassInformation(getDepth()+1, "Made Modification", P,
Chris Lattnera454b5b2002-04-28 05:14:06 +0000160 (Annotable*)M);
Chris Lattnerc8e66542002-04-27 06:56:12 +0000161 PMDebug::PrintAnalysisSetInfo(getDepth(), "Preserved", P,
162 AnUsage.getPreservedSet());
Chris Lattner67d25652002-01-30 23:20:39 +0000163
Chris Lattnerc8e66542002-04-27 06:56:12 +0000164
165 // Erase all analyses not in the preserved set...
166 if (!AnUsage.preservesAll()) {
167 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
168 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(),
169 E = CurrentAnalyses.end(); I != E; )
170 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) !=
171 PreservedSet.end())
172 ++I; // This analysis is preserved, leave it in the available set...
173 else {
174#if MAP_DOESNT_HAVE_BROKEN_ERASE_MEMBER
175 I = CurrentAnalyses.erase(I); // Analysis not preserved!
176#else
177 // GCC 2.95.3 STL doesn't have correct erase member!
178 CurrentAnalyses.erase(I);
179 I = CurrentAnalyses.begin();
180#endif
181 }
182 }
183
Chris Lattner73503172002-07-29 21:03:38 +0000184 // Add the current pass to the set of passes that have been run, and are
185 // thus available to users.
186 //
187 if (const PassInfo *PI = P->getPassInfo())
188 CurrentAnalyses[PI] = P;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000189
190 // Free memory for any passes that we are the last use of...
191 std::vector<Pass*> &DeadPass = LastUserOf[P];
192 for (std::vector<Pass*>::iterator I = DeadPass.begin(),E = DeadPass.end();
193 I != E; ++I) {
194 PMDebug::PrintPassInformation(getDepth()+1, "Freeing Pass", *I,
Chris Lattnera454b5b2002-04-28 05:14:06 +0000195 (Annotable*)M);
Chris Lattnerac3e0602002-01-31 18:32:27 +0000196 (*I)->releaseMemory();
197 }
Chris Lattner67d25652002-01-30 23:20:39 +0000198 }
199 return MadeChanges;
200 }
201
Chris Lattnerac3e0602002-01-31 18:32:27 +0000202 // dumpPassStructure - Implement the -debug-passes=PassStructure option
203 virtual void dumpPassStructure(unsigned Offset = 0) {
204 std::cerr << std::string(Offset*2, ' ') << Traits::getPMName()
205 << " Pass Manager\n";
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000206 for (typename std::vector<PassClass*>::iterator
207 I = Passes.begin(), E = Passes.end(); I != E; ++I) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000208 PassClass *P = *I;
209 P->dumpPassStructure(Offset+1);
210
211 // Loop through and see which classes are destroyed after this one...
212 for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(),
213 E = LastUseOf.end(); I != E; ++I) {
214 if (P == I->second) {
Chris Lattner73503172002-07-29 21:03:38 +0000215 std::cerr << "--" << std::string(Offset*2, ' ');
Chris Lattnerac3e0602002-01-31 18:32:27 +0000216 I->first->dumpPassStructure(0);
217 }
218 }
219 }
220 }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000221
222 Pass *getAnalysisOrNullDown(AnalysisID ID) const {
223 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID);
224 if (I == CurrentAnalyses.end()) {
225 if (Batcher)
226 return ((AnalysisResolver*)Batcher)->getAnalysisOrNullDown(ID);
227 return 0;
228 }
229 return I->second;
230 }
231
232 Pass *getAnalysisOrNullUp(AnalysisID ID) const {
233 std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID);
234 if (I == CurrentAnalyses.end()) {
235 if (Parent)
236 return Parent->getAnalysisOrNullUp(ID);
237 return 0;
238 }
239 return I->second;
240 }
241
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000242 // {start/end}Pass - Called when a pass is started, it just propogates
243 // information up to the top level PassManagerT object to tell it that a pass
244 // has started or ended. This is used to gather timing information about
245 // passes.
246 //
247 void startPass(Pass *P) {
248 if (Parent) Parent->startPass(P);
249 else PassStarted(P);
250 }
251 void endPass(Pass *P) {
252 if (Parent) Parent->endPass(P);
253 else PassEnded(P);
254 }
255
Chris Lattnerac3e0602002-01-31 18:32:27 +0000256 // markPassUsed - Inform higher level pass managers (and ourselves)
257 // that these analyses are being used by this pass. This is used to
258 // make sure that analyses are not free'd before we have to use
259 // them...
260 //
261 void markPassUsed(AnalysisID P, Pass *User) {
262 std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.find(P);
263 if (I != CurrentAnalyses.end()) {
264 LastUseOf[I->second] = User; // Local pass, extend the lifetime
265 } else {
266 // Pass not in current available set, must be a higher level pass
267 // available to us, propogate to parent pass manager... We tell the
268 // parent that we (the passmanager) are using the analysis so that it
269 // frees the analysis AFTER this pass manager runs.
270 //
Chris Lattnerde421a72002-03-17 21:16:01 +0000271 assert(Parent != 0 && "Pass available but not found! "
272 "Did your analysis pass 'Provide' itself?");
Chris Lattnerac3e0602002-01-31 18:32:27 +0000273 Parent->markPassUsed(P, this);
274 }
275 }
276
277 // Return the number of parent PassManagers that exist
278 virtual unsigned getDepth() const {
279 if (Parent == 0) return 0;
280 return 1 + Parent->getDepth();
281 }
282
Chris Lattner67d25652002-01-30 23:20:39 +0000283 // add - Add a pass to the queue of passes to run. This passes ownership of
284 // the Pass to the PassManager. When the PassManager is destroyed, the pass
Chris Lattnerac3e0602002-01-31 18:32:27 +0000285 // will be destroyed as well, so there is no need to delete the pass. This
286 // implies that all passes MUST be new'd.
Chris Lattner67d25652002-01-30 23:20:39 +0000287 //
288 void add(PassClass *P) {
289 // Get information about what analyses the pass uses...
Chris Lattnerc8e66542002-04-27 06:56:12 +0000290 AnalysisUsage AnUsage;
291 P->getAnalysisUsage(AnUsage);
292 const std::vector<AnalysisID> &Required = AnUsage.getRequiredSet();
Chris Lattner67d25652002-01-30 23:20:39 +0000293
294 // Loop over all of the analyses used by this pass,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000295 for (std::vector<AnalysisID>::const_iterator I = Required.begin(),
296 E = Required.end(); I != E; ++I) {
Chris Lattner67d25652002-01-30 23:20:39 +0000297 if (getAnalysisOrNullDown(*I) == 0)
Chris Lattner26750072002-07-27 01:12:17 +0000298 add((PassClass*)(*I)->createPass());
Chris Lattner67d25652002-01-30 23:20:39 +0000299 }
300
301 // Tell the pass to add itself to this PassManager... the way it does so
302 // depends on the class of the pass, and is critical to laying out passes in
303 // an optimal order..
304 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000305 P->addToPassManager(this, AnUsage);
Chris Lattner67d25652002-01-30 23:20:39 +0000306 }
307
308private:
309
310 // addPass - These functions are used to implement the subclass specific
311 // behaviors present in PassManager. Basically the add(Pass*) method ends up
312 // reflecting its behavior into a Pass::addToPassManager call. Subclasses of
313 // Pass override it specifically so that they can reflect the type
314 // information inherent in "this" back to the PassManager.
315 //
316 // For generic Pass subclasses (which are interprocedural passes), we simply
317 // add the pass to the end of the pass list and terminate any accumulation of
Chris Lattnerc8e66542002-04-27 06:56:12 +0000318 // FunctionPass's that are present.
Chris Lattner67d25652002-01-30 23:20:39 +0000319 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000320 void addPass(PassClass *P, AnalysisUsage &AnUsage) {
321 const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
Chris Lattnerc8e66542002-04-27 06:56:12 +0000322
Chris Lattner73503172002-07-29 21:03:38 +0000323 // FIXME: If this pass being added isn't killed by any of the passes in the
324 // batcher class then we can reorder to pass to execute before the batcher
325 // does, which will potentially allow us to batch more passes!
Chris Lattner67d25652002-01-30 23:20:39 +0000326 //
Chris Lattner73503172002-07-29 21:03:38 +0000327 //const std::vector<AnalysisID> &ProvidedSet = AnUsage.getProvidedSet();
328 if (Batcher /*&& ProvidedSet.empty()*/)
Chris Lattner67d25652002-01-30 23:20:39 +0000329 closeBatcher(); // This pass cannot be batched!
330
331 // Set the Resolver instance variable in the Pass so that it knows where to
332 // find this object...
333 //
334 setAnalysisResolver(P, this);
335 Passes.push_back(P);
336
Chris Lattnerac3e0602002-01-31 18:32:27 +0000337 // Inform higher level pass managers (and ourselves) that these analyses are
338 // being used by this pass. This is used to make sure that analyses are not
339 // free'd before we have to use them...
340 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000341 for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
342 E = RequiredSet.end(); I != E; ++I)
Chris Lattnerac3e0602002-01-31 18:32:27 +0000343 markPassUsed(*I, P); // Mark *I as used by P
344
Chris Lattnerc8e66542002-04-27 06:56:12 +0000345 // Erase all analyses not in the preserved set...
346 if (!AnUsage.preservesAll()) {
347 const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
348 for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(),
349 E = CurrentAnalyses.end(); I != E; )
350 if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) !=
351 PreservedSet.end())
352 ++I; // This analysis is preserved, leave it in the available set...
353 else {
354#if MAP_DOESNT_HAVE_BROKEN_ERASE_MEMBER
355 I = CurrentAnalyses.erase(I); // Analysis not preserved!
356#else
357 CurrentAnalyses.erase(I);// GCC 2.95.3 STL doesn't have correct erase!
358 I = CurrentAnalyses.begin();
359#endif
360 }
361 }
Chris Lattner67d25652002-01-30 23:20:39 +0000362
Chris Lattner73503172002-07-29 21:03:38 +0000363 // Add this pass to the currently available set...
364 if (const PassInfo *PI = P->getPassInfo())
365 CurrentAnalyses[PI] = P;
Chris Lattnerac3e0602002-01-31 18:32:27 +0000366
367 // For now assume that our results are never used...
368 LastUseOf[P] = P;
Chris Lattner67d25652002-01-30 23:20:39 +0000369 }
370
Chris Lattnerc8e66542002-04-27 06:56:12 +0000371 // For FunctionPass subclasses, we must be sure to batch the FunctionPass's
372 // together in a BatcherClass object so that all of the analyses are run
373 // together a function at a time.
Chris Lattner67d25652002-01-30 23:20:39 +0000374 //
Chris Lattnerc8e66542002-04-27 06:56:12 +0000375 void addPass(SubPassClass *MP, AnalysisUsage &AnUsage) {
Chris Lattner67d25652002-01-30 23:20:39 +0000376 if (Batcher == 0) // If we don't have a batcher yet, make one now.
377 Batcher = new BatcherClass(this);
Chris Lattner73503172002-07-29 21:03:38 +0000378 // The Batcher will queue the passes up
Chris Lattnerc8e66542002-04-27 06:56:12 +0000379 MP->addToPassManager(Batcher, AnUsage);
Chris Lattner67d25652002-01-30 23:20:39 +0000380 }
381
382 // closeBatcher - Terminate the batcher that is being worked on.
383 void closeBatcher() {
384 if (Batcher) {
385 Passes.push_back(Batcher);
386 Batcher = 0;
387 }
388 }
389};
390
391
392
393//===----------------------------------------------------------------------===//
394// PassManagerTraits<BasicBlock> Specialization
395//
396// This pass manager is used to group together all of the BasicBlockPass's
397// into a single unit.
398//
399template<> struct PassManagerTraits<BasicBlock> : public BasicBlockPass {
400 // PassClass - The type of passes tracked by this PassManager
401 typedef BasicBlockPass PassClass;
402
403 // SubPassClass - The types of classes that should be collated together
404 // This is impossible to match, so BasicBlock instantiations of PassManagerT
405 // do not collate.
406 //
407 typedef PassManagerT<Module> SubPassClass;
408
409 // BatcherClass - The type to use for collation of subtypes... This class is
410 // never instantiated for the PassManager<BasicBlock>, but it must be an
411 // instance of PassClass to typecheck.
412 //
413 typedef PassClass BatcherClass;
414
415 // ParentClass - The type of the parent PassManager...
Chris Lattner4e8c4872002-03-23 22:51:58 +0000416 typedef PassManagerT<Function> ParentClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000417
Chris Lattner2eaac392002-01-31 00:40:44 +0000418 // PMType - The type of the passmanager that subclasses this class
419 typedef PassManagerT<BasicBlock> PMType;
420
Chris Lattner67d25652002-01-30 23:20:39 +0000421 // runPass - Specify how the pass should be run on the UnitType
422 static bool runPass(PassClass *P, BasicBlock *M) {
423 // todo, init and finalize
Chris Lattner113f4f42002-06-25 16:13:24 +0000424 return P->runOnBasicBlock(*M);
Chris Lattner67d25652002-01-30 23:20:39 +0000425 }
426
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000427 // Dummy implementation of PassStarted/PassEnded
428 static void PassStarted(Pass *P) {}
429 static void PassEnded(Pass *P) {}
430
Chris Lattnerac3e0602002-01-31 18:32:27 +0000431 // getPMName() - Return the name of the unit the PassManager operates on for
432 // debugging.
433 const char *getPMName() const { return "BasicBlock"; }
Chris Lattner37104aa2002-04-29 14:57:45 +0000434 virtual const char *getPassName() const { return "BasicBlock Pass Manager"; }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000435
Chris Lattner2eaac392002-01-31 00:40:44 +0000436 // Implement the BasicBlockPass interface...
Chris Lattner113f4f42002-06-25 16:13:24 +0000437 virtual bool doInitialization(Module &M);
438 virtual bool runOnBasicBlock(BasicBlock &BB);
439 virtual bool doFinalization(Module &M);
Chris Lattnercb429062002-04-30 18:50:17 +0000440
441 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
442 AU.setPreservesAll();
443 }
Chris Lattner67d25652002-01-30 23:20:39 +0000444};
445
446
447
448//===----------------------------------------------------------------------===//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000449// PassManagerTraits<Function> Specialization
Chris Lattner67d25652002-01-30 23:20:39 +0000450//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000451// This pass manager is used to group together all of the FunctionPass's
Chris Lattner67d25652002-01-30 23:20:39 +0000452// into a single unit.
453//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000454template<> struct PassManagerTraits<Function> : public FunctionPass {
Chris Lattner67d25652002-01-30 23:20:39 +0000455 // PassClass - The type of passes tracked by this PassManager
Chris Lattnerc8e66542002-04-27 06:56:12 +0000456 typedef FunctionPass PassClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000457
458 // SubPassClass - The types of classes that should be collated together
459 typedef BasicBlockPass SubPassClass;
460
461 // BatcherClass - The type to use for collation of subtypes...
462 typedef PassManagerT<BasicBlock> BatcherClass;
463
464 // ParentClass - The type of the parent PassManager...
465 typedef PassManagerT<Module> ParentClass;
466
467 // PMType - The type of the passmanager that subclasses this class
Chris Lattner4e8c4872002-03-23 22:51:58 +0000468 typedef PassManagerT<Function> PMType;
Chris Lattner67d25652002-01-30 23:20:39 +0000469
470 // runPass - Specify how the pass should be run on the UnitType
Chris Lattnerc8e66542002-04-27 06:56:12 +0000471 static bool runPass(PassClass *P, Function *F) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000472 return P->runOnFunction(*F);
Chris Lattner67d25652002-01-30 23:20:39 +0000473 }
474
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000475 // Dummy implementation of PassStarted/PassEnded
476 static void PassStarted(Pass *P) {}
477 static void PassEnded(Pass *P) {}
478
Chris Lattnerac3e0602002-01-31 18:32:27 +0000479 // getPMName() - Return the name of the unit the PassManager operates on for
480 // debugging.
Chris Lattner4e8c4872002-03-23 22:51:58 +0000481 const char *getPMName() const { return "Function"; }
Chris Lattner37104aa2002-04-29 14:57:45 +0000482 virtual const char *getPassName() const { return "Function Pass Manager"; }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000483
Chris Lattnerc8e66542002-04-27 06:56:12 +0000484 // Implement the FunctionPass interface...
Chris Lattner113f4f42002-06-25 16:13:24 +0000485 virtual bool doInitialization(Module &M);
486 virtual bool runOnFunction(Function &F);
487 virtual bool doFinalization(Module &M);
Chris Lattnercb429062002-04-30 18:50:17 +0000488
489 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
490 AU.setPreservesAll();
491 }
Chris Lattner67d25652002-01-30 23:20:39 +0000492};
493
494
495
496//===----------------------------------------------------------------------===//
497// PassManagerTraits<Module> Specialization
498//
499// This is the top level PassManager implementation that holds generic passes.
500//
501template<> struct PassManagerTraits<Module> : public Pass {
502 // PassClass - The type of passes tracked by this PassManager
503 typedef Pass PassClass;
504
505 // SubPassClass - The types of classes that should be collated together
Chris Lattnerc8e66542002-04-27 06:56:12 +0000506 typedef FunctionPass SubPassClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000507
508 // BatcherClass - The type to use for collation of subtypes...
Chris Lattner4e8c4872002-03-23 22:51:58 +0000509 typedef PassManagerT<Function> BatcherClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000510
511 // ParentClass - The type of the parent PassManager...
Chris Lattnerac3e0602002-01-31 18:32:27 +0000512 typedef AnalysisResolver ParentClass;
Chris Lattner67d25652002-01-30 23:20:39 +0000513
514 // runPass - Specify how the pass should be run on the UnitType
Chris Lattner113f4f42002-06-25 16:13:24 +0000515 static bool runPass(PassClass *P, Module *M) { return P->run(*M); }
Chris Lattner67d25652002-01-30 23:20:39 +0000516
Chris Lattnerac3e0602002-01-31 18:32:27 +0000517 // getPMName() - Return the name of the unit the PassManager operates on for
518 // debugging.
519 const char *getPMName() const { return "Module"; }
Chris Lattner37104aa2002-04-29 14:57:45 +0000520 virtual const char *getPassName() const { return "Module Pass Manager"; }
Chris Lattnerac3e0602002-01-31 18:32:27 +0000521
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000522 // TimingInformation - This data member maintains timing information for each
523 // of the passes that is executed.
524 //
525 TimingInfo *TimeInfo;
526
527 // PassStarted/Ended - This callback is notified any time a pass is started
528 // or stops. This is used to collect timing information about the different
529 // passes being executed.
530 //
531 void PassStarted(Pass *P) {
532 if (TimeInfo) TimeInfo->passStarted(P);
Chris Lattner67d25652002-01-30 23:20:39 +0000533 }
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000534 void PassEnded(Pass *P) {
535 if (TimeInfo) TimeInfo->passEnded(P);
536 }
537
538 // run - Implement the PassManager interface...
Chris Lattner113f4f42002-06-25 16:13:24 +0000539 bool run(Module &M) {
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000540 TimeInfo = TimingInfo::create();
Chris Lattner113f4f42002-06-25 16:13:24 +0000541 bool Result = ((PassManagerT<Module>*)this)->runOnUnit(&M);
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000542 if (TimeInfo) {
543 delete TimeInfo;
544 TimeInfo = 0;
545 }
546 return Result;
547 }
548
549 // PassManagerTraits constructor - Create a timing info object if the user
550 // specified timing info should be collected on the command line.
551 //
552 PassManagerTraits() : TimeInfo(0) {}
Chris Lattner67d25652002-01-30 23:20:39 +0000553};
554
555
556
557//===----------------------------------------------------------------------===//
558// PassManagerTraits Method Implementations
559//
560
561// PassManagerTraits<BasicBlock> Implementations
562//
Chris Lattner113f4f42002-06-25 16:13:24 +0000563inline bool PassManagerTraits<BasicBlock>::doInitialization(Module &M) {
Chris Lattner2eaac392002-01-31 00:40:44 +0000564 bool Changed = false;
565 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
566 ((PMType*)this)->Passes[i]->doInitialization(M);
567 return Changed;
568}
569
Chris Lattner113f4f42002-06-25 16:13:24 +0000570inline bool PassManagerTraits<BasicBlock>::runOnBasicBlock(BasicBlock &BB) {
571 return ((PMType*)this)->runOnUnit(&BB);
Chris Lattner2eaac392002-01-31 00:40:44 +0000572}
573
Chris Lattner113f4f42002-06-25 16:13:24 +0000574inline bool PassManagerTraits<BasicBlock>::doFinalization(Module &M) {
Chris Lattner2eaac392002-01-31 00:40:44 +0000575 bool Changed = false;
576 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
577 ((PMType*)this)->Passes[i]->doFinalization(M);
578 return Changed;
Chris Lattner67d25652002-01-30 23:20:39 +0000579}
580
581
Chris Lattner4e8c4872002-03-23 22:51:58 +0000582// PassManagerTraits<Function> Implementations
Chris Lattner67d25652002-01-30 23:20:39 +0000583//
Chris Lattner113f4f42002-06-25 16:13:24 +0000584inline bool PassManagerTraits<Function>::doInitialization(Module &M) {
Chris Lattner67d25652002-01-30 23:20:39 +0000585 bool Changed = false;
586 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
587 ((PMType*)this)->Passes[i]->doInitialization(M);
588 return Changed;
589}
590
Chris Lattner113f4f42002-06-25 16:13:24 +0000591inline bool PassManagerTraits<Function>::runOnFunction(Function &F) {
592 return ((PMType*)this)->runOnUnit(&F);
Chris Lattner67d25652002-01-30 23:20:39 +0000593}
594
Chris Lattner113f4f42002-06-25 16:13:24 +0000595inline bool PassManagerTraits<Function>::doFinalization(Module &M) {
Chris Lattner67d25652002-01-30 23:20:39 +0000596 bool Changed = false;
597 for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i)
598 ((PMType*)this)->Passes[i]->doFinalization(M);
599 return Changed;
600}
601
602#endif