blob: 3d09d9d7e1c8b1d78724f00ab8ccf193db48439f [file] [log] [blame]
Chris Lattner26e4f892002-01-21 07:37:31 +00001//===- Pass.cpp - LLVM Pass Infrastructure Impementation ------------------===//
2//
3// This file implements the LLVM Pass infrastructure. It is primarily
4// responsible with ensuring that passes are executed and batched together
5// optimally.
6//
7//===----------------------------------------------------------------------===//
8
Chris Lattnercdd09c22002-01-31 00:45:31 +00009#include "llvm/PassManager.h"
Chris Lattner37c86672002-04-28 20:46:05 +000010#include "PassManagerT.h" // PassManagerT implementation
Chris Lattnercdd09c22002-01-31 00:45:31 +000011#include "llvm/Module.h"
Chris Lattner26e4f892002-01-21 07:37:31 +000012#include "Support/STLExtras.h"
Chris Lattner37d3c952002-07-23 18:08:00 +000013#include "Support/TypeInfo.h"
Chris Lattnere2eb99e2002-04-29 04:04:29 +000014#include <stdio.h>
Chris Lattner6a33d6f2002-08-01 19:33:09 +000015#include <sys/resource.h>
16#include <sys/unistd.h>
Chris Lattner6e041bd2002-08-21 22:17:09 +000017#include <set>
Chris Lattnerd013ba92002-01-23 05:49:41 +000018
Chris Lattner7e0dbe62002-05-06 19:31:52 +000019//===----------------------------------------------------------------------===//
20// AnalysisID Class Implementation
21//
22
Chris Lattner198cf422002-07-30 16:27:02 +000023static std::vector<const PassInfo*> CFGOnlyAnalyses;
Chris Lattnercdd09c22002-01-31 00:45:31 +000024
Chris Lattner198cf422002-07-30 16:27:02 +000025void RegisterPassBase::setPreservesCFG() {
26 CFGOnlyAnalyses.push_back(PIObj);
Chris Lattner7e0dbe62002-05-06 19:31:52 +000027}
28
29//===----------------------------------------------------------------------===//
30// AnalysisResolver Class Implementation
31//
32
Chris Lattnercdd09c22002-01-31 00:45:31 +000033void AnalysisResolver::setAnalysisResolver(Pass *P, AnalysisResolver *AR) {
34 assert(P->Resolver == 0 && "Pass already in a PassManager!");
35 P->Resolver = AR;
36}
37
Chris Lattner7e0dbe62002-05-06 19:31:52 +000038//===----------------------------------------------------------------------===//
39// AnalysisUsage Class Implementation
40//
Chris Lattneree2ff5d2002-04-28 21:25:41 +000041
42// preservesCFG - This function should be called to by the pass, iff they do
43// not:
44//
45// 1. Add or remove basic blocks from the function
46// 2. Modify terminator instructions in any way.
47//
48// This function annotates the AnalysisUsage info object to say that analyses
49// that only depend on the CFG are preserved by this pass.
50//
51void AnalysisUsage::preservesCFG() {
Chris Lattner7e0dbe62002-05-06 19:31:52 +000052 // Since this transformation doesn't modify the CFG, it preserves all analyses
53 // that only depend on the CFG (like dominators, loop info, etc...)
54 //
55 Preserved.insert(Preserved.end(),
56 CFGOnlyAnalyses.begin(), CFGOnlyAnalyses.end());
Chris Lattneree2ff5d2002-04-28 21:25:41 +000057}
58
59
Chris Lattner37c86672002-04-28 20:46:05 +000060//===----------------------------------------------------------------------===//
61// PassManager implementation - The PassManager class is a simple Pimpl class
62// that wraps the PassManagerT template.
63//
64PassManager::PassManager() : PM(new PassManagerT<Module>()) {}
65PassManager::~PassManager() { delete PM; }
66void PassManager::add(Pass *P) { PM->add(P); }
Chris Lattner113f4f42002-06-25 16:13:24 +000067bool PassManager::run(Module &M) { return PM->run(M); }
Chris Lattnercdd09c22002-01-31 00:45:31 +000068
Chris Lattner37c86672002-04-28 20:46:05 +000069
70//===----------------------------------------------------------------------===//
Chris Lattnere2eb99e2002-04-29 04:04:29 +000071// TimingInfo Class - This class is used to calculate information about the
72// amount of time each pass takes to execute. This only happens with
73// -time-passes is enabled on the command line.
74//
Chris Lattnerf5cad152002-07-22 02:10:13 +000075static cl::opt<bool>
76EnableTiming("time-passes",
77 cl::desc("Time each pass, printing elapsed time for each on exit"));
Chris Lattnere2eb99e2002-04-29 04:04:29 +000078
Chris Lattner6a33d6f2002-08-01 19:33:09 +000079static TimeRecord getTimeRecord() {
80 static unsigned long PageSize = 0;
81
82 if (PageSize == 0) {
83#ifdef _SC_PAGE_SIZE
84 PageSize = sysconf(_SC_PAGE_SIZE);
85#else
86#ifdef _SC_PAGESIZE
87 PageSize = sysconf(_SC_PAGESIZE);
88#else
89 PageSize = getpagesize();
90#endif
91#endif
92 }
93
94 struct rusage RU;
Chris Lattnere2eb99e2002-04-29 04:04:29 +000095 struct timeval T;
96 gettimeofday(&T, 0);
Chris Lattner6a33d6f2002-08-01 19:33:09 +000097 if (getrusage(RUSAGE_SELF, &RU)) {
98 perror("getrusage call failed: -time-passes info incorrect!");
99 }
100
101 TimeRecord Result;
102 Result.Elapsed = T.tv_sec + T.tv_usec/1000000.0;
103 Result.UserTime = RU.ru_utime.tv_sec + RU.ru_utime.tv_usec/1000000.0;
104 Result.SystemTime = RU.ru_stime.tv_sec + RU.ru_stime.tv_usec/1000000.0;
105 Result.MaxRSS = RU.ru_maxrss*PageSize;
106
107 return Result;
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000108}
109
Chris Lattnere821d782002-08-20 18:47:53 +0000110bool TimeRecord::operator<(const TimeRecord &TR) const {
111 // Primary sort key is User+System time
112 if (UserTime+SystemTime < TR.UserTime+TR.SystemTime)
113 return true;
114 if (UserTime+SystemTime > TR.UserTime+TR.SystemTime)
115 return false;
116
117 // Secondary sort key is Wall Time
118 return Elapsed < TR.Elapsed;
119}
120
Chris Lattner6a33d6f2002-08-01 19:33:09 +0000121void TimeRecord::passStart(const TimeRecord &T) {
122 Elapsed -= T.Elapsed;
123 UserTime -= T.UserTime;
124 SystemTime -= T.SystemTime;
125 RSSTemp = T.MaxRSS;
126}
127
128void TimeRecord::passEnd(const TimeRecord &T) {
129 Elapsed += T.Elapsed;
130 UserTime += T.UserTime;
131 SystemTime += T.SystemTime;
132 RSSTemp = T.MaxRSS - RSSTemp;
133 MaxRSS = std::max(MaxRSS, RSSTemp);
134}
135
Chris Lattner5ec216b2002-08-19 15:43:33 +0000136static void printVal(double Val, double Total) {
137 if (Total < 1e-7) // Avoid dividing by zero...
Chris Lattnerca5afe72002-08-19 20:42:12 +0000138 fprintf(stderr, " ----- ");
Chris Lattner5ec216b2002-08-19 15:43:33 +0000139 else
140 fprintf(stderr, " %7.4f (%5.1f%%)", Val, Val*100/Total);
141}
142
Chris Lattner6a33d6f2002-08-01 19:33:09 +0000143void TimeRecord::print(const char *PassName, const TimeRecord &Total) const {
Chris Lattner5ec216b2002-08-19 15:43:33 +0000144 printVal(UserTime, Total.UserTime);
145 printVal(SystemTime, Total.SystemTime);
146 printVal(UserTime+SystemTime, Total.UserTime+Total.SystemTime);
147 printVal(Elapsed, Total.Elapsed);
148
149 fprintf(stderr, " ");
Chris Lattner6a33d6f2002-08-01 19:33:09 +0000150
151 if (Total.MaxRSS)
152 std::cerr << MaxRSS << "\t";
153 std::cerr << PassName << "\n";
154}
155
156
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000157// Create method. If Timing is enabled, this creates and returns a new timing
158// object, otherwise it returns null.
159//
160TimingInfo *TimingInfo::create() {
161 return EnableTiming ? new TimingInfo() : 0;
162}
163
Chris Lattner6a33d6f2002-08-01 19:33:09 +0000164void TimingInfo::passStarted(Pass *P) {
165 TimingData[P].passStart(getTimeRecord());
166}
167void TimingInfo::passEnded(Pass *P) {
168 TimingData[P].passEnd(getTimeRecord());
169}
170void TimeRecord::sum(const TimeRecord &TR) {
171 Elapsed += TR.Elapsed;
172 UserTime += TR.UserTime;
173 SystemTime += TR.SystemTime;
174 MaxRSS += TR.MaxRSS;
175}
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000176
177// TimingDtor - Print out information about timing information
178TimingInfo::~TimingInfo() {
179 // Iterate over all of the data, converting it into the dual of the data map,
180 // so that the data is sorted by amount of time taken, instead of pointer.
181 //
Chris Lattner6a33d6f2002-08-01 19:33:09 +0000182 std::vector<std::pair<TimeRecord, Pass*> > Data;
183 TimeRecord Total;
184 for (std::map<Pass*, TimeRecord>::iterator I = TimingData.begin(),
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000185 E = TimingData.end(); I != E; ++I)
186 // Throw out results for "grouping" pass managers...
187 if (!dynamic_cast<AnalysisResolver*>(I->first)) {
188 Data.push_back(std::make_pair(I->second, I->first));
Chris Lattner6a33d6f2002-08-01 19:33:09 +0000189 Total.sum(I->second);
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000190 }
191
192 // Sort the data by time as the primary key, in reverse order...
Chris Lattner6a33d6f2002-08-01 19:33:09 +0000193 std::sort(Data.begin(), Data.end(),
194 std::greater<std::pair<TimeRecord, Pass*> >());
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000195
196 // Print out timing header...
Anand Shukla8c377892002-06-25 22:07:38 +0000197 std::cerr << std::string(79, '=') << "\n"
Chris Lattner6a33d6f2002-08-01 19:33:09 +0000198 << " ... Pass execution timing report ...\n"
199 << std::string(79, '=') << "\n Total Execution Time: "
200 << (Total.UserTime+Total.SystemTime) << " seconds ("
201 << Total.Elapsed << " wall clock)\n\n ---User Time--- "
202 << "--System Time-- --User+System-- ---Wall Time---";
203
204 if (Total.MaxRSS)
205 std::cerr << " ---Mem---";
206 std::cerr << " --- Pass Name ---\n";
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000207
208 // Loop through all of the timing data, printing it out...
Chris Lattner6a33d6f2002-08-01 19:33:09 +0000209 for (unsigned i = 0, e = Data.size(); i != e; ++i)
210 Data[i].first.print(Data[i].second->getPassName(), Total);
211
212 Total.print("TOTAL", Total);
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000213}
214
215
Chris Lattner1e4867f2002-07-30 19:51:02 +0000216void PMDebug::PrintArgumentInformation(const Pass *P) {
217 // Print out passes in pass manager...
218 if (const AnalysisResolver *PM = dynamic_cast<const AnalysisResolver*>(P)) {
219 for (unsigned i = 0, e = PM->getNumContainedPasses(); i != e; ++i)
220 PrintArgumentInformation(PM->getContainedPass(i));
221
222 } else { // Normal pass. Print argument information...
223 // Print out arguments for registered passes that are _optimizations_
224 if (const PassInfo *PI = P->getPassInfo())
225 if (PI->getPassType() & PassInfo::Optimization)
226 std::cerr << " -" << PI->getPassArgument();
227 }
228}
Chris Lattnercdd09c22002-01-31 00:45:31 +0000229
230void PMDebug::PrintPassInformation(unsigned Depth, const char *Action,
Chris Lattnera454b5b2002-04-28 05:14:06 +0000231 Pass *P, Annotable *V) {
Chris Lattnerf5cad152002-07-22 02:10:13 +0000232 if (PassDebugging >= Executions) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000233 std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '"
Chris Lattner37104aa2002-04-29 14:57:45 +0000234 << P->getPassName();
Chris Lattnercdd09c22002-01-31 00:45:31 +0000235 if (V) {
236 std::cerr << "' on ";
Chris Lattnera454b5b2002-04-28 05:14:06 +0000237
238 if (dynamic_cast<Module*>(V)) {
Chris Lattnercdd09c22002-01-31 00:45:31 +0000239 std::cerr << "Module\n"; return;
Chris Lattnera454b5b2002-04-28 05:14:06 +0000240 } else if (Function *F = dynamic_cast<Function*>(V))
241 std::cerr << "Function '" << F->getName();
242 else if (BasicBlock *BB = dynamic_cast<BasicBlock*>(V))
243 std::cerr << "BasicBlock '" << BB->getName();
244 else if (Value *Val = dynamic_cast<Value*>(V))
245 std::cerr << typeid(*Val).name() << " '" << Val->getName();
Chris Lattnercdd09c22002-01-31 00:45:31 +0000246 }
247 std::cerr << "'...\n";
248 }
249}
250
251void PMDebug::PrintAnalysisSetInfo(unsigned Depth, const char *Msg,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000252 Pass *P, const std::vector<AnalysisID> &Set){
Chris Lattnerf5cad152002-07-22 02:10:13 +0000253 if (PassDebugging >= Details && !Set.empty()) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000254 std::cerr << (void*)P << std::string(Depth*2+3, ' ') << Msg << " Analyses:";
Chris Lattner198cf422002-07-30 16:27:02 +0000255 for (unsigned i = 0; i != Set.size(); ++i)
256 std::cerr << " " << Set[i]->getPassName();
Chris Lattnercdd09c22002-01-31 00:45:31 +0000257 std::cerr << "\n";
258 }
259}
260
Chris Lattnercdd09c22002-01-31 00:45:31 +0000261//===----------------------------------------------------------------------===//
262// Pass Implementation
Chris Lattner654b5bc2002-01-22 00:17:48 +0000263//
Chris Lattnercdd09c22002-01-31 00:45:31 +0000264
Chris Lattnerc8e66542002-04-27 06:56:12 +0000265void Pass::addToPassManager(PassManagerT<Module> *PM, AnalysisUsage &AU) {
266 PM->addPass(this, AU);
Chris Lattner654b5bc2002-01-22 00:17:48 +0000267}
Chris Lattner26e4f892002-01-21 07:37:31 +0000268
Chris Lattner198cf422002-07-30 16:27:02 +0000269// dumpPassStructure - Implement the -debug-passes=Structure option
270void Pass::dumpPassStructure(unsigned Offset) {
271 std::cerr << std::string(Offset*2, ' ') << getPassName() << "\n";
272}
Chris Lattner37104aa2002-04-29 14:57:45 +0000273
274// getPassName - Use C++ RTTI to get a SOMEWHAT intelligable name for the pass.
275//
Chris Lattner071577d2002-07-29 21:02:31 +0000276const char *Pass::getPassName() const {
277 if (const PassInfo *PI = getPassInfo())
278 return PI->getPassName();
279 return typeid(*this).name();
280}
Chris Lattner37104aa2002-04-29 14:57:45 +0000281
Chris Lattner26750072002-07-27 01:12:17 +0000282// print - Print out the internal state of the pass. This is called by Analyse
283// to print out the contents of an analysis. Otherwise it is not neccesary to
284// implement this method.
285//
286void Pass::print(std::ostream &O) const {
287 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
288}
289
290// dump - call print(std::cerr);
291void Pass::dump() const {
292 print(std::cerr, 0);
293}
294
Chris Lattnercdd09c22002-01-31 00:45:31 +0000295//===----------------------------------------------------------------------===//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000296// FunctionPass Implementation
Chris Lattner26e4f892002-01-21 07:37:31 +0000297//
Chris Lattnercdd09c22002-01-31 00:45:31 +0000298
Chris Lattnerc8e66542002-04-27 06:56:12 +0000299// run - On a module, we run this pass by initializing, runOnFunction'ing once
300// for every function in the module, then by finalizing.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000301//
Chris Lattner113f4f42002-06-25 16:13:24 +0000302bool FunctionPass::run(Module &M) {
Chris Lattnercdd09c22002-01-31 00:45:31 +0000303 bool Changed = doInitialization(M);
304
Chris Lattner113f4f42002-06-25 16:13:24 +0000305 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
306 if (!I->isExternal()) // Passes are not run on external functions!
Chris Lattnerc8e66542002-04-27 06:56:12 +0000307 Changed |= runOnFunction(*I);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000308
309 return Changed | doFinalization(M);
Chris Lattner26e4f892002-01-21 07:37:31 +0000310}
311
Chris Lattnerc8e66542002-04-27 06:56:12 +0000312// run - On a function, we simply initialize, run the function, then finalize.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000313//
Chris Lattner113f4f42002-06-25 16:13:24 +0000314bool FunctionPass::run(Function &F) {
315 if (F.isExternal()) return false;// Passes are not run on external functions!
Chris Lattnercdd09c22002-01-31 00:45:31 +0000316
Chris Lattner113f4f42002-06-25 16:13:24 +0000317 return doInitialization(*F.getParent()) | runOnFunction(F)
318 | doFinalization(*F.getParent());
Chris Lattner26e4f892002-01-21 07:37:31 +0000319}
Chris Lattnerd013ba92002-01-23 05:49:41 +0000320
Chris Lattnerc8e66542002-04-27 06:56:12 +0000321void FunctionPass::addToPassManager(PassManagerT<Module> *PM,
322 AnalysisUsage &AU) {
323 PM->addPass(this, AU);
Chris Lattnerd013ba92002-01-23 05:49:41 +0000324}
Chris Lattnercdd09c22002-01-31 00:45:31 +0000325
Chris Lattnerc8e66542002-04-27 06:56:12 +0000326void FunctionPass::addToPassManager(PassManagerT<Function> *PM,
327 AnalysisUsage &AU) {
328 PM->addPass(this, AU);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000329}
330
331//===----------------------------------------------------------------------===//
332// BasicBlockPass Implementation
333//
334
Chris Lattnerc8e66542002-04-27 06:56:12 +0000335// To run this pass on a function, we simply call runOnBasicBlock once for each
336// function.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000337//
Chris Lattner113f4f42002-06-25 16:13:24 +0000338bool BasicBlockPass::runOnFunction(Function &F) {
Chris Lattnercdd09c22002-01-31 00:45:31 +0000339 bool Changed = false;
Chris Lattner113f4f42002-06-25 16:13:24 +0000340 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Chris Lattnercdd09c22002-01-31 00:45:31 +0000341 Changed |= runOnBasicBlock(*I);
342 return Changed;
343}
344
345// To run directly on the basic block, we initialize, runOnBasicBlock, then
346// finalize.
347//
Chris Lattner113f4f42002-06-25 16:13:24 +0000348bool BasicBlockPass::run(BasicBlock &BB) {
349 Module &M = *BB.getParent()->getParent();
Chris Lattnercdd09c22002-01-31 00:45:31 +0000350 return doInitialization(M) | runOnBasicBlock(BB) | doFinalization(M);
351}
352
Chris Lattner57698e22002-03-26 18:01:55 +0000353void BasicBlockPass::addToPassManager(PassManagerT<Function> *PM,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000354 AnalysisUsage &AU) {
355 PM->addPass(this, AU);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000356}
357
358void BasicBlockPass::addToPassManager(PassManagerT<BasicBlock> *PM,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000359 AnalysisUsage &AU) {
360 PM->addPass(this, AU);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000361}
362
Chris Lattner37d3c952002-07-23 18:08:00 +0000363
364//===----------------------------------------------------------------------===//
365// Pass Registration mechanism
366//
367static std::map<TypeInfo, PassInfo*> *PassInfoMap = 0;
368static std::vector<PassRegistrationListener*> *Listeners = 0;
369
370// getPassInfo - Return the PassInfo data structure that corresponds to this
371// pass...
372const PassInfo *Pass::getPassInfo() const {
Chris Lattner071577d2002-07-29 21:02:31 +0000373 if (PassInfoCache) return PassInfoCache;
Chris Lattner4b169632002-08-21 17:08:37 +0000374 return lookupPassInfo(typeid(*this));
375}
376
377const PassInfo *Pass::lookupPassInfo(const std::type_info &TI) {
Chris Lattner071577d2002-07-29 21:02:31 +0000378 if (PassInfoMap == 0) return 0;
Chris Lattner4b169632002-08-21 17:08:37 +0000379 std::map<TypeInfo, PassInfo*>::iterator I = PassInfoMap->find(TI);
Chris Lattner071577d2002-07-29 21:02:31 +0000380 return (I != PassInfoMap->end()) ? I->second : 0;
Chris Lattner37d3c952002-07-23 18:08:00 +0000381}
382
383void RegisterPassBase::registerPass(PassInfo *PI) {
384 if (PassInfoMap == 0)
385 PassInfoMap = new std::map<TypeInfo, PassInfo*>();
386
387 assert(PassInfoMap->find(PI->getTypeInfo()) == PassInfoMap->end() &&
388 "Pass already registered!");
389 PIObj = PI;
390 PassInfoMap->insert(std::make_pair(TypeInfo(PI->getTypeInfo()), PI));
391
392 // Notify any listeners...
393 if (Listeners)
394 for (std::vector<PassRegistrationListener*>::iterator
395 I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
396 (*I)->passRegistered(PI);
397}
398
Chris Lattner6e041bd2002-08-21 22:17:09 +0000399void RegisterPassBase::unregisterPass(PassInfo *PI) {
Chris Lattner37d3c952002-07-23 18:08:00 +0000400 assert(PassInfoMap && "Pass registered but not in map!");
401 std::map<TypeInfo, PassInfo*>::iterator I =
Chris Lattner6e041bd2002-08-21 22:17:09 +0000402 PassInfoMap->find(PI->getTypeInfo());
Chris Lattner37d3c952002-07-23 18:08:00 +0000403 assert(I != PassInfoMap->end() && "Pass registered but not in map!");
404
405 // Remove pass from the map...
406 PassInfoMap->erase(I);
407 if (PassInfoMap->empty()) {
408 delete PassInfoMap;
409 PassInfoMap = 0;
410 }
411
412 // Notify any listeners...
413 if (Listeners)
414 for (std::vector<PassRegistrationListener*>::iterator
415 I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
Chris Lattner6e041bd2002-08-21 22:17:09 +0000416 (*I)->passUnregistered(PI);
Chris Lattner37d3c952002-07-23 18:08:00 +0000417
418 // Delete the PassInfo object itself...
Chris Lattner6e041bd2002-08-21 22:17:09 +0000419 delete PI;
Chris Lattner37d3c952002-07-23 18:08:00 +0000420}
421
Chris Lattner6e041bd2002-08-21 22:17:09 +0000422//===----------------------------------------------------------------------===//
423// Analysis Group Implementation Code
424//===----------------------------------------------------------------------===//
425
426struct AnalysisGroupInfo {
427 const PassInfo *DefaultImpl;
428 std::set<const PassInfo *> Implementations;
429 AnalysisGroupInfo() : DefaultImpl(0) {}
430};
431
432static std::map<const PassInfo *, AnalysisGroupInfo> *AnalysisGroupInfoMap = 0;
433
434// RegisterAGBase implementation
435//
436RegisterAGBase::RegisterAGBase(const std::type_info &Interface,
437 const std::type_info *Pass, bool isDefault)
438 : ImplementationInfo(0), isDefaultImplementation(isDefault) {
439
440 std::cerr << "Registering interface: " << Interface.name() << "\n";
441
442 InterfaceInfo = const_cast<PassInfo*>(Pass::lookupPassInfo(Interface));
443 if (InterfaceInfo == 0) { // First reference to Interface, add it now.
444 InterfaceInfo = // Create the new PassInfo for the interface...
445 new PassInfo("", "", Interface, PassInfo::AnalysisGroup, 0, 0);
446 registerPass(InterfaceInfo);
447 PIObj = 0;
448 }
449 assert(InterfaceInfo->getPassType() == PassInfo::AnalysisGroup &&
450 "Trying to join an analysis group that is a normal pass!");
451
452 if (Pass) {
453 std::cerr << "Registering interface impl: " << Pass->name() << "\n";
454
455 ImplementationInfo = Pass::lookupPassInfo(*Pass);
456 assert(ImplementationInfo &&
457 "Must register pass before adding to AnalysisGroup!");
458
459 // Lazily allocate to avoid nasty initialization order dependencies
460 if (AnalysisGroupInfoMap == 0)
461 AnalysisGroupInfoMap = new std::map<const PassInfo *,AnalysisGroupInfo>();
462
463 AnalysisGroupInfo &AGI = (*AnalysisGroupInfoMap)[InterfaceInfo];
464 assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
465 "Cannot add a pass to the same analysis group more than once!");
466 AGI.Implementations.insert(ImplementationInfo);
467 if (isDefault) {
468 assert(AGI.DefaultImpl == 0 && InterfaceInfo->getNormalCtor() == 0 &&
469 "Default implementation for analysis group already specified!");
470 assert(ImplementationInfo->getNormalCtor() &&
471 "Cannot specify pass as default if it does not have a default ctor");
472 AGI.DefaultImpl = ImplementationInfo;
473 InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
474 }
475 }
476}
477
478void RegisterAGBase::setGroupName(const char *Name) {
479 assert(InterfaceInfo->getPassName()[0] == 0 && "Interface Name already set!");
480 InterfaceInfo->setPassName(Name);
481}
482
483RegisterAGBase::~RegisterAGBase() {
484 if (ImplementationInfo) {
485 assert(AnalysisGroupInfoMap && "Inserted into map, but map doesn't exist?");
486 AnalysisGroupInfo &AGI = (*AnalysisGroupInfoMap)[InterfaceInfo];
487
488 assert(AGI.Implementations.count(ImplementationInfo) &&
489 "Pass not a member of analysis group?");
490
491 if (AGI.DefaultImpl == ImplementationInfo)
492 AGI.DefaultImpl = 0;
493
494 AGI.Implementations.erase(ImplementationInfo);
495
496 // Last member of this analysis group? Unregister PassInfo, delete map entry
497 if (AGI.Implementations.empty()) {
498 assert(AGI.DefaultImpl == 0 &&
499 "Default implementation didn't unregister?");
500 AnalysisGroupInfoMap->erase(InterfaceInfo);
501 if (AnalysisGroupInfoMap->empty()) { // Delete map if empty
502 delete AnalysisGroupInfoMap;
503 AnalysisGroupInfoMap = 0;
504 }
505
506 unregisterPass(InterfaceInfo);
507 }
508 }
509}
510
511
512// findAnalysisGroupMember - Return an iterator pointing to one of the elements
513// of Map if there is a pass in Map that is a member of the analysis group for
514// the specified AnalysisGroupID.
515//
516static std::map<const PassInfo*, Pass*>::const_iterator
517findAnalysisGroupMember(const PassInfo *AnalysisGroupID,
518 const std::map<const PassInfo*, Pass*> &Map) {
519 assert(AnalysisGroupID->getPassType() == PassInfo::AnalysisGroup &&
520 "AnalysisGroupID is not an analysis group!");
521 assert(AnalysisGroupInfoMap && AnalysisGroupInfoMap->count(AnalysisGroupID) &&
522 "Analysis Group does not have any registered members!");
523
524 // Get the set of all known implementations of this analysis group...
525 std::set<const PassInfo *> &Impls =
526 (*AnalysisGroupInfoMap)[AnalysisGroupID].Implementations;
527
528 // Scan over available passes, checking to see if any is a valid analysis
529 for (std::map<const PassInfo*, Pass*>::const_iterator I = Map.begin(),
530 E = Map.end(); I != E; ++I)
531 if (Impls.count(I->first)) // This is a valid analysis, return it.
532 return I;
533
534 return Map.end(); // Nothing of use found.
535}
536
537
Chris Lattner37d3c952002-07-23 18:08:00 +0000538
539
540//===----------------------------------------------------------------------===//
541// PassRegistrationListener implementation
542//
543
544// PassRegistrationListener ctor - Add the current object to the list of
545// PassRegistrationListeners...
546PassRegistrationListener::PassRegistrationListener() {
547 if (!Listeners) Listeners = new std::vector<PassRegistrationListener*>();
548 Listeners->push_back(this);
549}
550
551// dtor - Remove object from list of listeners...
552PassRegistrationListener::~PassRegistrationListener() {
553 std::vector<PassRegistrationListener*>::iterator I =
554 std::find(Listeners->begin(), Listeners->end(), this);
555 assert(Listeners && I != Listeners->end() &&
556 "PassRegistrationListener not registered!");
557 Listeners->erase(I);
558
559 if (Listeners->empty()) {
560 delete Listeners;
561 Listeners = 0;
562 }
563}
564
565// enumeratePasses - Iterate over the registered passes, calling the
566// passEnumerate callback on each PassInfo object.
567//
568void PassRegistrationListener::enumeratePasses() {
569 if (PassInfoMap)
570 for (std::map<TypeInfo, PassInfo*>::iterator I = PassInfoMap->begin(),
571 E = PassInfoMap->end(); I != E; ++I)
572 passEnumerate(I->second);
573}