blob: 33bf11d8e7faeb95f23f6298193ccd868a298fa6 [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 Lattner675e7a92002-08-21 23:51:51 +000019// IncludeFile - Stub function used to help linking out.
20IncludeFile::IncludeFile(void*) {}
21
Chris Lattner7e0dbe62002-05-06 19:31:52 +000022//===----------------------------------------------------------------------===//
23// AnalysisID Class Implementation
24//
25
Chris Lattner198cf422002-07-30 16:27:02 +000026static std::vector<const PassInfo*> CFGOnlyAnalyses;
Chris Lattnercdd09c22002-01-31 00:45:31 +000027
Chris Lattner198cf422002-07-30 16:27:02 +000028void RegisterPassBase::setPreservesCFG() {
29 CFGOnlyAnalyses.push_back(PIObj);
Chris Lattner7e0dbe62002-05-06 19:31:52 +000030}
31
32//===----------------------------------------------------------------------===//
33// AnalysisResolver Class Implementation
34//
35
Chris Lattnercdd09c22002-01-31 00:45:31 +000036void AnalysisResolver::setAnalysisResolver(Pass *P, AnalysisResolver *AR) {
37 assert(P->Resolver == 0 && "Pass already in a PassManager!");
38 P->Resolver = AR;
39}
40
Chris Lattner7e0dbe62002-05-06 19:31:52 +000041//===----------------------------------------------------------------------===//
42// AnalysisUsage Class Implementation
43//
Chris Lattneree2ff5d2002-04-28 21:25:41 +000044
45// preservesCFG - This function should be called to by the pass, iff they do
46// not:
47//
48// 1. Add or remove basic blocks from the function
49// 2. Modify terminator instructions in any way.
50//
51// This function annotates the AnalysisUsage info object to say that analyses
52// that only depend on the CFG are preserved by this pass.
53//
54void AnalysisUsage::preservesCFG() {
Chris Lattner7e0dbe62002-05-06 19:31:52 +000055 // Since this transformation doesn't modify the CFG, it preserves all analyses
56 // that only depend on the CFG (like dominators, loop info, etc...)
57 //
58 Preserved.insert(Preserved.end(),
59 CFGOnlyAnalyses.begin(), CFGOnlyAnalyses.end());
Chris Lattneree2ff5d2002-04-28 21:25:41 +000060}
61
62
Chris Lattner37c86672002-04-28 20:46:05 +000063//===----------------------------------------------------------------------===//
64// PassManager implementation - The PassManager class is a simple Pimpl class
65// that wraps the PassManagerT template.
66//
67PassManager::PassManager() : PM(new PassManagerT<Module>()) {}
68PassManager::~PassManager() { delete PM; }
69void PassManager::add(Pass *P) { PM->add(P); }
Chris Lattner113f4f42002-06-25 16:13:24 +000070bool PassManager::run(Module &M) { return PM->run(M); }
Chris Lattnercdd09c22002-01-31 00:45:31 +000071
Chris Lattner37c86672002-04-28 20:46:05 +000072
73//===----------------------------------------------------------------------===//
Chris Lattnere2eb99e2002-04-29 04:04:29 +000074// TimingInfo Class - This class is used to calculate information about the
75// amount of time each pass takes to execute. This only happens with
76// -time-passes is enabled on the command line.
77//
Chris Lattnerf5cad152002-07-22 02:10:13 +000078static cl::opt<bool>
79EnableTiming("time-passes",
80 cl::desc("Time each pass, printing elapsed time for each on exit"));
Chris Lattnere2eb99e2002-04-29 04:04:29 +000081
Chris Lattner6a33d6f2002-08-01 19:33:09 +000082static TimeRecord getTimeRecord() {
83 static unsigned long PageSize = 0;
84
85 if (PageSize == 0) {
86#ifdef _SC_PAGE_SIZE
87 PageSize = sysconf(_SC_PAGE_SIZE);
88#else
89#ifdef _SC_PAGESIZE
90 PageSize = sysconf(_SC_PAGESIZE);
91#else
92 PageSize = getpagesize();
93#endif
94#endif
95 }
96
97 struct rusage RU;
Chris Lattnere2eb99e2002-04-29 04:04:29 +000098 struct timeval T;
99 gettimeofday(&T, 0);
Chris Lattner6a33d6f2002-08-01 19:33:09 +0000100 if (getrusage(RUSAGE_SELF, &RU)) {
101 perror("getrusage call failed: -time-passes info incorrect!");
102 }
103
104 TimeRecord Result;
105 Result.Elapsed = T.tv_sec + T.tv_usec/1000000.0;
106 Result.UserTime = RU.ru_utime.tv_sec + RU.ru_utime.tv_usec/1000000.0;
107 Result.SystemTime = RU.ru_stime.tv_sec + RU.ru_stime.tv_usec/1000000.0;
108 Result.MaxRSS = RU.ru_maxrss*PageSize;
109
110 return Result;
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000111}
112
Chris Lattnere821d782002-08-20 18:47:53 +0000113bool TimeRecord::operator<(const TimeRecord &TR) const {
114 // Primary sort key is User+System time
115 if (UserTime+SystemTime < TR.UserTime+TR.SystemTime)
116 return true;
117 if (UserTime+SystemTime > TR.UserTime+TR.SystemTime)
118 return false;
119
120 // Secondary sort key is Wall Time
121 return Elapsed < TR.Elapsed;
122}
123
Chris Lattner6a33d6f2002-08-01 19:33:09 +0000124void TimeRecord::passStart(const TimeRecord &T) {
125 Elapsed -= T.Elapsed;
126 UserTime -= T.UserTime;
127 SystemTime -= T.SystemTime;
128 RSSTemp = T.MaxRSS;
129}
130
131void TimeRecord::passEnd(const TimeRecord &T) {
132 Elapsed += T.Elapsed;
133 UserTime += T.UserTime;
134 SystemTime += T.SystemTime;
135 RSSTemp = T.MaxRSS - RSSTemp;
136 MaxRSS = std::max(MaxRSS, RSSTemp);
137}
138
Chris Lattner5ec216b2002-08-19 15:43:33 +0000139static void printVal(double Val, double Total) {
140 if (Total < 1e-7) // Avoid dividing by zero...
Chris Lattnerca5afe72002-08-19 20:42:12 +0000141 fprintf(stderr, " ----- ");
Chris Lattner5ec216b2002-08-19 15:43:33 +0000142 else
143 fprintf(stderr, " %7.4f (%5.1f%%)", Val, Val*100/Total);
144}
145
Chris Lattner6a33d6f2002-08-01 19:33:09 +0000146void TimeRecord::print(const char *PassName, const TimeRecord &Total) const {
Chris Lattner5ec216b2002-08-19 15:43:33 +0000147 printVal(UserTime, Total.UserTime);
148 printVal(SystemTime, Total.SystemTime);
149 printVal(UserTime+SystemTime, Total.UserTime+Total.SystemTime);
150 printVal(Elapsed, Total.Elapsed);
151
152 fprintf(stderr, " ");
Chris Lattner6a33d6f2002-08-01 19:33:09 +0000153
154 if (Total.MaxRSS)
155 std::cerr << MaxRSS << "\t";
156 std::cerr << PassName << "\n";
157}
158
159
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000160// Create method. If Timing is enabled, this creates and returns a new timing
161// object, otherwise it returns null.
162//
163TimingInfo *TimingInfo::create() {
164 return EnableTiming ? new TimingInfo() : 0;
165}
166
Chris Lattner6a33d6f2002-08-01 19:33:09 +0000167void TimingInfo::passStarted(Pass *P) {
168 TimingData[P].passStart(getTimeRecord());
169}
170void TimingInfo::passEnded(Pass *P) {
171 TimingData[P].passEnd(getTimeRecord());
172}
173void TimeRecord::sum(const TimeRecord &TR) {
174 Elapsed += TR.Elapsed;
175 UserTime += TR.UserTime;
176 SystemTime += TR.SystemTime;
177 MaxRSS += TR.MaxRSS;
178}
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000179
180// TimingDtor - Print out information about timing information
181TimingInfo::~TimingInfo() {
182 // Iterate over all of the data, converting it into the dual of the data map,
183 // so that the data is sorted by amount of time taken, instead of pointer.
184 //
Chris Lattner6a33d6f2002-08-01 19:33:09 +0000185 std::vector<std::pair<TimeRecord, Pass*> > Data;
186 TimeRecord Total;
187 for (std::map<Pass*, TimeRecord>::iterator I = TimingData.begin(),
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000188 E = TimingData.end(); I != E; ++I)
189 // Throw out results for "grouping" pass managers...
190 if (!dynamic_cast<AnalysisResolver*>(I->first)) {
191 Data.push_back(std::make_pair(I->second, I->first));
Chris Lattner6a33d6f2002-08-01 19:33:09 +0000192 Total.sum(I->second);
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000193 }
194
195 // Sort the data by time as the primary key, in reverse order...
Chris Lattner6a33d6f2002-08-01 19:33:09 +0000196 std::sort(Data.begin(), Data.end(),
197 std::greater<std::pair<TimeRecord, Pass*> >());
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000198
199 // Print out timing header...
Anand Shukla8c377892002-06-25 22:07:38 +0000200 std::cerr << std::string(79, '=') << "\n"
Chris Lattner6a33d6f2002-08-01 19:33:09 +0000201 << " ... Pass execution timing report ...\n"
202 << std::string(79, '=') << "\n Total Execution Time: "
203 << (Total.UserTime+Total.SystemTime) << " seconds ("
204 << Total.Elapsed << " wall clock)\n\n ---User Time--- "
205 << "--System Time-- --User+System-- ---Wall Time---";
206
207 if (Total.MaxRSS)
208 std::cerr << " ---Mem---";
209 std::cerr << " --- Pass Name ---\n";
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000210
211 // Loop through all of the timing data, printing it out...
Chris Lattner6a33d6f2002-08-01 19:33:09 +0000212 for (unsigned i = 0, e = Data.size(); i != e; ++i)
213 Data[i].first.print(Data[i].second->getPassName(), Total);
214
215 Total.print("TOTAL", Total);
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000216}
217
218
Chris Lattner1e4867f2002-07-30 19:51:02 +0000219void PMDebug::PrintArgumentInformation(const Pass *P) {
220 // Print out passes in pass manager...
221 if (const AnalysisResolver *PM = dynamic_cast<const AnalysisResolver*>(P)) {
222 for (unsigned i = 0, e = PM->getNumContainedPasses(); i != e; ++i)
223 PrintArgumentInformation(PM->getContainedPass(i));
224
225 } else { // Normal pass. Print argument information...
226 // Print out arguments for registered passes that are _optimizations_
227 if (const PassInfo *PI = P->getPassInfo())
228 if (PI->getPassType() & PassInfo::Optimization)
229 std::cerr << " -" << PI->getPassArgument();
230 }
231}
Chris Lattnercdd09c22002-01-31 00:45:31 +0000232
233void PMDebug::PrintPassInformation(unsigned Depth, const char *Action,
Chris Lattnera454b5b2002-04-28 05:14:06 +0000234 Pass *P, Annotable *V) {
Chris Lattnerf5cad152002-07-22 02:10:13 +0000235 if (PassDebugging >= Executions) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000236 std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '"
Chris Lattner37104aa2002-04-29 14:57:45 +0000237 << P->getPassName();
Chris Lattnercdd09c22002-01-31 00:45:31 +0000238 if (V) {
239 std::cerr << "' on ";
Chris Lattnera454b5b2002-04-28 05:14:06 +0000240
241 if (dynamic_cast<Module*>(V)) {
Chris Lattnercdd09c22002-01-31 00:45:31 +0000242 std::cerr << "Module\n"; return;
Chris Lattnera454b5b2002-04-28 05:14:06 +0000243 } else if (Function *F = dynamic_cast<Function*>(V))
244 std::cerr << "Function '" << F->getName();
245 else if (BasicBlock *BB = dynamic_cast<BasicBlock*>(V))
246 std::cerr << "BasicBlock '" << BB->getName();
247 else if (Value *Val = dynamic_cast<Value*>(V))
248 std::cerr << typeid(*Val).name() << " '" << Val->getName();
Chris Lattnercdd09c22002-01-31 00:45:31 +0000249 }
250 std::cerr << "'...\n";
251 }
252}
253
254void PMDebug::PrintAnalysisSetInfo(unsigned Depth, const char *Msg,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000255 Pass *P, const std::vector<AnalysisID> &Set){
Chris Lattnerf5cad152002-07-22 02:10:13 +0000256 if (PassDebugging >= Details && !Set.empty()) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000257 std::cerr << (void*)P << std::string(Depth*2+3, ' ') << Msg << " Analyses:";
Chris Lattner198cf422002-07-30 16:27:02 +0000258 for (unsigned i = 0; i != Set.size(); ++i)
259 std::cerr << " " << Set[i]->getPassName();
Chris Lattnercdd09c22002-01-31 00:45:31 +0000260 std::cerr << "\n";
261 }
262}
263
Chris Lattnercdd09c22002-01-31 00:45:31 +0000264//===----------------------------------------------------------------------===//
265// Pass Implementation
Chris Lattner654b5bc2002-01-22 00:17:48 +0000266//
Chris Lattnercdd09c22002-01-31 00:45:31 +0000267
Chris Lattnerc8e66542002-04-27 06:56:12 +0000268void Pass::addToPassManager(PassManagerT<Module> *PM, AnalysisUsage &AU) {
269 PM->addPass(this, AU);
Chris Lattner654b5bc2002-01-22 00:17:48 +0000270}
Chris Lattner26e4f892002-01-21 07:37:31 +0000271
Chris Lattner198cf422002-07-30 16:27:02 +0000272// dumpPassStructure - Implement the -debug-passes=Structure option
273void Pass::dumpPassStructure(unsigned Offset) {
274 std::cerr << std::string(Offset*2, ' ') << getPassName() << "\n";
275}
Chris Lattner37104aa2002-04-29 14:57:45 +0000276
277// getPassName - Use C++ RTTI to get a SOMEWHAT intelligable name for the pass.
278//
Chris Lattner071577d2002-07-29 21:02:31 +0000279const char *Pass::getPassName() const {
280 if (const PassInfo *PI = getPassInfo())
281 return PI->getPassName();
282 return typeid(*this).name();
283}
Chris Lattner37104aa2002-04-29 14:57:45 +0000284
Chris Lattner26750072002-07-27 01:12:17 +0000285// print - Print out the internal state of the pass. This is called by Analyse
286// to print out the contents of an analysis. Otherwise it is not neccesary to
287// implement this method.
288//
289void Pass::print(std::ostream &O) const {
290 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
291}
292
293// dump - call print(std::cerr);
294void Pass::dump() const {
295 print(std::cerr, 0);
296}
297
Chris Lattnercdd09c22002-01-31 00:45:31 +0000298//===----------------------------------------------------------------------===//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000299// FunctionPass Implementation
Chris Lattner26e4f892002-01-21 07:37:31 +0000300//
Chris Lattnercdd09c22002-01-31 00:45:31 +0000301
Chris Lattnerc8e66542002-04-27 06:56:12 +0000302// run - On a module, we run this pass by initializing, runOnFunction'ing once
303// for every function in the module, then by finalizing.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000304//
Chris Lattner113f4f42002-06-25 16:13:24 +0000305bool FunctionPass::run(Module &M) {
Chris Lattnercdd09c22002-01-31 00:45:31 +0000306 bool Changed = doInitialization(M);
307
Chris Lattner113f4f42002-06-25 16:13:24 +0000308 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
309 if (!I->isExternal()) // Passes are not run on external functions!
Chris Lattnerc8e66542002-04-27 06:56:12 +0000310 Changed |= runOnFunction(*I);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000311
312 return Changed | doFinalization(M);
Chris Lattner26e4f892002-01-21 07:37:31 +0000313}
314
Chris Lattnerc8e66542002-04-27 06:56:12 +0000315// run - On a function, we simply initialize, run the function, then finalize.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000316//
Chris Lattner113f4f42002-06-25 16:13:24 +0000317bool FunctionPass::run(Function &F) {
318 if (F.isExternal()) return false;// Passes are not run on external functions!
Chris Lattnercdd09c22002-01-31 00:45:31 +0000319
Chris Lattner113f4f42002-06-25 16:13:24 +0000320 return doInitialization(*F.getParent()) | runOnFunction(F)
321 | doFinalization(*F.getParent());
Chris Lattner26e4f892002-01-21 07:37:31 +0000322}
Chris Lattnerd013ba92002-01-23 05:49:41 +0000323
Chris Lattnerc8e66542002-04-27 06:56:12 +0000324void FunctionPass::addToPassManager(PassManagerT<Module> *PM,
325 AnalysisUsage &AU) {
326 PM->addPass(this, AU);
Chris Lattnerd013ba92002-01-23 05:49:41 +0000327}
Chris Lattnercdd09c22002-01-31 00:45:31 +0000328
Chris Lattnerc8e66542002-04-27 06:56:12 +0000329void FunctionPass::addToPassManager(PassManagerT<Function> *PM,
330 AnalysisUsage &AU) {
331 PM->addPass(this, AU);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000332}
333
334//===----------------------------------------------------------------------===//
335// BasicBlockPass Implementation
336//
337
Chris Lattnerc8e66542002-04-27 06:56:12 +0000338// To run this pass on a function, we simply call runOnBasicBlock once for each
339// function.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000340//
Chris Lattner113f4f42002-06-25 16:13:24 +0000341bool BasicBlockPass::runOnFunction(Function &F) {
Chris Lattnercdd09c22002-01-31 00:45:31 +0000342 bool Changed = false;
Chris Lattner113f4f42002-06-25 16:13:24 +0000343 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Chris Lattnercdd09c22002-01-31 00:45:31 +0000344 Changed |= runOnBasicBlock(*I);
345 return Changed;
346}
347
348// To run directly on the basic block, we initialize, runOnBasicBlock, then
349// finalize.
350//
Chris Lattner113f4f42002-06-25 16:13:24 +0000351bool BasicBlockPass::run(BasicBlock &BB) {
352 Module &M = *BB.getParent()->getParent();
Chris Lattnercdd09c22002-01-31 00:45:31 +0000353 return doInitialization(M) | runOnBasicBlock(BB) | doFinalization(M);
354}
355
Chris Lattner57698e22002-03-26 18:01:55 +0000356void BasicBlockPass::addToPassManager(PassManagerT<Function> *PM,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000357 AnalysisUsage &AU) {
358 PM->addPass(this, AU);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000359}
360
361void BasicBlockPass::addToPassManager(PassManagerT<BasicBlock> *PM,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000362 AnalysisUsage &AU) {
363 PM->addPass(this, AU);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000364}
365
Chris Lattner37d3c952002-07-23 18:08:00 +0000366
367//===----------------------------------------------------------------------===//
368// Pass Registration mechanism
369//
370static std::map<TypeInfo, PassInfo*> *PassInfoMap = 0;
371static std::vector<PassRegistrationListener*> *Listeners = 0;
372
373// getPassInfo - Return the PassInfo data structure that corresponds to this
374// pass...
375const PassInfo *Pass::getPassInfo() const {
Chris Lattner071577d2002-07-29 21:02:31 +0000376 if (PassInfoCache) return PassInfoCache;
Chris Lattner4b169632002-08-21 17:08:37 +0000377 return lookupPassInfo(typeid(*this));
378}
379
380const PassInfo *Pass::lookupPassInfo(const std::type_info &TI) {
Chris Lattner071577d2002-07-29 21:02:31 +0000381 if (PassInfoMap == 0) return 0;
Chris Lattner4b169632002-08-21 17:08:37 +0000382 std::map<TypeInfo, PassInfo*>::iterator I = PassInfoMap->find(TI);
Chris Lattner071577d2002-07-29 21:02:31 +0000383 return (I != PassInfoMap->end()) ? I->second : 0;
Chris Lattner37d3c952002-07-23 18:08:00 +0000384}
385
386void RegisterPassBase::registerPass(PassInfo *PI) {
387 if (PassInfoMap == 0)
388 PassInfoMap = new std::map<TypeInfo, PassInfo*>();
389
390 assert(PassInfoMap->find(PI->getTypeInfo()) == PassInfoMap->end() &&
391 "Pass already registered!");
392 PIObj = PI;
393 PassInfoMap->insert(std::make_pair(TypeInfo(PI->getTypeInfo()), PI));
394
395 // Notify any listeners...
396 if (Listeners)
397 for (std::vector<PassRegistrationListener*>::iterator
398 I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
399 (*I)->passRegistered(PI);
400}
401
Chris Lattner6e041bd2002-08-21 22:17:09 +0000402void RegisterPassBase::unregisterPass(PassInfo *PI) {
Chris Lattner37d3c952002-07-23 18:08:00 +0000403 assert(PassInfoMap && "Pass registered but not in map!");
404 std::map<TypeInfo, PassInfo*>::iterator I =
Chris Lattner6e041bd2002-08-21 22:17:09 +0000405 PassInfoMap->find(PI->getTypeInfo());
Chris Lattner37d3c952002-07-23 18:08:00 +0000406 assert(I != PassInfoMap->end() && "Pass registered but not in map!");
407
408 // Remove pass from the map...
409 PassInfoMap->erase(I);
410 if (PassInfoMap->empty()) {
411 delete PassInfoMap;
412 PassInfoMap = 0;
413 }
414
415 // Notify any listeners...
416 if (Listeners)
417 for (std::vector<PassRegistrationListener*>::iterator
418 I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
Chris Lattner6e041bd2002-08-21 22:17:09 +0000419 (*I)->passUnregistered(PI);
Chris Lattner37d3c952002-07-23 18:08:00 +0000420
421 // Delete the PassInfo object itself...
Chris Lattner6e041bd2002-08-21 22:17:09 +0000422 delete PI;
Chris Lattner37d3c952002-07-23 18:08:00 +0000423}
424
Chris Lattner6e041bd2002-08-21 22:17:09 +0000425//===----------------------------------------------------------------------===//
426// Analysis Group Implementation Code
427//===----------------------------------------------------------------------===//
428
429struct AnalysisGroupInfo {
430 const PassInfo *DefaultImpl;
431 std::set<const PassInfo *> Implementations;
432 AnalysisGroupInfo() : DefaultImpl(0) {}
433};
434
435static std::map<const PassInfo *, AnalysisGroupInfo> *AnalysisGroupInfoMap = 0;
436
437// RegisterAGBase implementation
438//
439RegisterAGBase::RegisterAGBase(const std::type_info &Interface,
440 const std::type_info *Pass, bool isDefault)
441 : ImplementationInfo(0), isDefaultImplementation(isDefault) {
442
Chris Lattner6e041bd2002-08-21 22:17:09 +0000443 InterfaceInfo = const_cast<PassInfo*>(Pass::lookupPassInfo(Interface));
444 if (InterfaceInfo == 0) { // First reference to Interface, add it now.
445 InterfaceInfo = // Create the new PassInfo for the interface...
446 new PassInfo("", "", Interface, PassInfo::AnalysisGroup, 0, 0);
447 registerPass(InterfaceInfo);
448 PIObj = 0;
449 }
450 assert(InterfaceInfo->getPassType() == PassInfo::AnalysisGroup &&
451 "Trying to join an analysis group that is a normal pass!");
452
453 if (Pass) {
Chris Lattner6e041bd2002-08-21 22:17:09 +0000454 ImplementationInfo = Pass::lookupPassInfo(*Pass);
455 assert(ImplementationInfo &&
456 "Must register pass before adding to AnalysisGroup!");
457
458 // Lazily allocate to avoid nasty initialization order dependencies
459 if (AnalysisGroupInfoMap == 0)
460 AnalysisGroupInfoMap = new std::map<const PassInfo *,AnalysisGroupInfo>();
461
462 AnalysisGroupInfo &AGI = (*AnalysisGroupInfoMap)[InterfaceInfo];
463 assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
464 "Cannot add a pass to the same analysis group more than once!");
465 AGI.Implementations.insert(ImplementationInfo);
466 if (isDefault) {
467 assert(AGI.DefaultImpl == 0 && InterfaceInfo->getNormalCtor() == 0 &&
468 "Default implementation for analysis group already specified!");
469 assert(ImplementationInfo->getNormalCtor() &&
470 "Cannot specify pass as default if it does not have a default ctor");
471 AGI.DefaultImpl = ImplementationInfo;
472 InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
473 }
474 }
475}
476
477void RegisterAGBase::setGroupName(const char *Name) {
478 assert(InterfaceInfo->getPassName()[0] == 0 && "Interface Name already set!");
479 InterfaceInfo->setPassName(Name);
480}
481
482RegisterAGBase::~RegisterAGBase() {
483 if (ImplementationInfo) {
484 assert(AnalysisGroupInfoMap && "Inserted into map, but map doesn't exist?");
485 AnalysisGroupInfo &AGI = (*AnalysisGroupInfoMap)[InterfaceInfo];
486
487 assert(AGI.Implementations.count(ImplementationInfo) &&
488 "Pass not a member of analysis group?");
489
490 if (AGI.DefaultImpl == ImplementationInfo)
491 AGI.DefaultImpl = 0;
492
493 AGI.Implementations.erase(ImplementationInfo);
494
495 // Last member of this analysis group? Unregister PassInfo, delete map entry
496 if (AGI.Implementations.empty()) {
497 assert(AGI.DefaultImpl == 0 &&
498 "Default implementation didn't unregister?");
499 AnalysisGroupInfoMap->erase(InterfaceInfo);
500 if (AnalysisGroupInfoMap->empty()) { // Delete map if empty
501 delete AnalysisGroupInfoMap;
502 AnalysisGroupInfoMap = 0;
503 }
504
505 unregisterPass(InterfaceInfo);
506 }
507 }
508}
509
510
511// findAnalysisGroupMember - Return an iterator pointing to one of the elements
512// of Map if there is a pass in Map that is a member of the analysis group for
513// the specified AnalysisGroupID.
514//
515static std::map<const PassInfo*, Pass*>::const_iterator
516findAnalysisGroupMember(const PassInfo *AnalysisGroupID,
517 const std::map<const PassInfo*, Pass*> &Map) {
518 assert(AnalysisGroupID->getPassType() == PassInfo::AnalysisGroup &&
519 "AnalysisGroupID is not an analysis group!");
520 assert(AnalysisGroupInfoMap && AnalysisGroupInfoMap->count(AnalysisGroupID) &&
521 "Analysis Group does not have any registered members!");
522
523 // Get the set of all known implementations of this analysis group...
524 std::set<const PassInfo *> &Impls =
525 (*AnalysisGroupInfoMap)[AnalysisGroupID].Implementations;
526
527 // Scan over available passes, checking to see if any is a valid analysis
528 for (std::map<const PassInfo*, Pass*>::const_iterator I = Map.begin(),
529 E = Map.end(); I != E; ++I)
530 if (Impls.count(I->first)) // This is a valid analysis, return it.
531 return I;
532
533 return Map.end(); // Nothing of use found.
534}
535
536
Chris Lattner37d3c952002-07-23 18:08:00 +0000537
538
539//===----------------------------------------------------------------------===//
540// PassRegistrationListener implementation
541//
542
543// PassRegistrationListener ctor - Add the current object to the list of
544// PassRegistrationListeners...
545PassRegistrationListener::PassRegistrationListener() {
546 if (!Listeners) Listeners = new std::vector<PassRegistrationListener*>();
547 Listeners->push_back(this);
548}
549
550// dtor - Remove object from list of listeners...
551PassRegistrationListener::~PassRegistrationListener() {
552 std::vector<PassRegistrationListener*>::iterator I =
553 std::find(Listeners->begin(), Listeners->end(), this);
554 assert(Listeners && I != Listeners->end() &&
555 "PassRegistrationListener not registered!");
556 Listeners->erase(I);
557
558 if (Listeners->empty()) {
559 delete Listeners;
560 Listeners = 0;
561 }
562}
563
564// enumeratePasses - Iterate over the registered passes, calling the
565// passEnumerate callback on each PassInfo object.
566//
567void PassRegistrationListener::enumeratePasses() {
568 if (PassInfoMap)
569 for (std::map<TypeInfo, PassInfo*>::iterator I = PassInfoMap->begin(),
570 E = PassInfoMap->end(); I != E; ++I)
571 passEnumerate(I->second);
572}