blob: 3c108fbeb7eaf51c2184ef2f2a6903e7d2318409 [file] [log] [blame]
Chris Lattnerab16a652003-10-13 05:33:01 +00001//===- Pass.cpp - LLVM Pass Infrastructure Implementation -----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// 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.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner26e4f892002-01-21 07:37:31 +00009//
10// This file implements the LLVM Pass infrastructure. It is primarily
11// responsible with ensuring that passes are executed and batched together
12// optimally.
13//
14//===----------------------------------------------------------------------===//
15
Chris Lattnercdd09c22002-01-31 00:45:31 +000016#include "llvm/PassManager.h"
Chris Lattner37c86672002-04-28 20:46:05 +000017#include "PassManagerT.h" // PassManagerT implementation
Chris Lattnercdd09c22002-01-31 00:45:31 +000018#include "llvm/Module.h"
Misha Brukman56a86422003-10-14 21:38:42 +000019#include "llvm/ModuleProvider.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000020#include "llvm/ADT/STLExtras.h"
21#include "llvm/Support/TypeInfo.h"
Reid Spencer8baf8e22004-07-04 11:55:37 +000022#include <iostream>
Chris Lattner6e041bd2002-08-21 22:17:09 +000023#include <set>
Chris Lattner189d19f2003-11-21 20:23:48 +000024using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000025
Chris Lattner7e0dbe62002-05-06 19:31:52 +000026//===----------------------------------------------------------------------===//
Chris Lattner7e0dbe62002-05-06 19:31:52 +000027// AnalysisResolver Class Implementation
28//
29
Reid Spencer8edc8be2005-04-25 01:01:35 +000030AnalysisResolver::~AnalysisResolver() {
31}
Chris Lattnercdd09c22002-01-31 00:45:31 +000032void AnalysisResolver::setAnalysisResolver(Pass *P, AnalysisResolver *AR) {
33 assert(P->Resolver == 0 && "Pass already in a PassManager!");
34 P->Resolver = AR;
35}
36
Chris Lattner7e0dbe62002-05-06 19:31:52 +000037//===----------------------------------------------------------------------===//
Chris Lattner37c86672002-04-28 20:46:05 +000038// PassManager implementation - The PassManager class is a simple Pimpl class
39// that wraps the PassManagerT template.
40//
Chris Lattnerc47b0812006-01-04 07:47:13 +000041PassManager::PassManager() : PM(new ModulePassManager()) {}
Chris Lattner37c86672002-04-28 20:46:05 +000042PassManager::~PassManager() { delete PM; }
Chris Lattner79e523d2004-09-20 04:47:19 +000043void PassManager::add(Pass *P) {
44 ModulePass *MP = dynamic_cast<ModulePass*>(P);
45 assert(MP && "Not a modulepass?");
46 PM->add(MP);
47}
48bool PassManager::run(Module &M) { return PM->runOnModule(M); }
Chris Lattnercdd09c22002-01-31 00:45:31 +000049
Brian Gaekeb7a83882003-08-12 17:22:39 +000050//===----------------------------------------------------------------------===//
51// FunctionPassManager implementation - The FunctionPassManager class
52// is a simple Pimpl class that wraps the PassManagerT template. It
53// is like PassManager, but only deals in FunctionPasses.
54//
Misha Brukmanb1c93172005-04-21 23:48:37 +000055FunctionPassManager::FunctionPassManager(ModuleProvider *P) :
Chris Lattnerc47b0812006-01-04 07:47:13 +000056 PM(new FunctionPassManagerT()), MP(P) {}
Brian Gaekeb7a83882003-08-12 17:22:39 +000057FunctionPassManager::~FunctionPassManager() { delete PM; }
58void FunctionPassManager::add(FunctionPass *P) { PM->add(P); }
Brian Gaeke2cc4b9d2003-08-14 06:07:57 +000059void FunctionPassManager::add(ImmutablePass *IP) { PM->add(IP); }
Chris Lattner2f779222006-09-04 04:07:39 +000060
61/// doInitialization - Run all of the initializers for the function passes.
62///
63bool FunctionPassManager::doInitialization() {
64 return PM->doInitialization(*MP->getModule());
65}
66
Misha Brukmanb1c93172005-04-21 23:48:37 +000067bool FunctionPassManager::run(Function &F) {
Chris Lattner3d40daa2006-07-06 21:35:01 +000068 std::string errstr;
69 if (MP->materializeFunction(&F, &errstr)) {
Reid Spencer6967cd52004-07-07 21:01:38 +000070 std::cerr << "Error reading bytecode file: " << errstr << "\n";
71 abort();
Reid Spencer6967cd52004-07-07 21:01:38 +000072 }
Chris Lattner2f779222006-09-04 04:07:39 +000073 return PM->runOnFunction(F);
74}
75
76/// doFinalization - Run all of the initializers for the function passes.
77///
78bool FunctionPassManager::doFinalization() {
79 return PM->doFinalization(*MP->getModule());
Misha Brukman56a86422003-10-14 21:38:42 +000080}
Brian Gaekeb7a83882003-08-12 17:22:39 +000081
Chris Lattner37c86672002-04-28 20:46:05 +000082
83//===----------------------------------------------------------------------===//
Chris Lattnere2eb99e2002-04-29 04:04:29 +000084// TimingInfo Class - This class is used to calculate information about the
85// amount of time each pass takes to execute. This only happens with
86// -time-passes is enabled on the command line.
87//
Reid Spencer2e4bfff2004-08-24 17:52:35 +000088bool llvm::TimePassesIsEnabled = false;
89static cl::opt<bool,true>
90EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
Chris Lattnerf5cad152002-07-22 02:10:13 +000091 cl::desc("Time each pass, printing elapsed time for each on exit"));
Chris Lattnere2eb99e2002-04-29 04:04:29 +000092
Chris Lattnere7e09442003-08-14 05:20:28 +000093// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
94// a non null value (if the -time-passes option is enabled) or it leaves it
95// null. It may be called multiple times.
96void TimingInfo::createTheTimeInfo() {
Reid Spencer2e4bfff2004-08-24 17:52:35 +000097 if (!TimePassesIsEnabled || TheTimeInfo) return;
Chris Lattnere7e09442003-08-14 05:20:28 +000098
99 // Constructed the first time this is called, iff -time-passes is enabled.
100 // This guarantees that the object will be constructed before static globals,
101 // thus it will be destroyed before them.
102 static TimingInfo TTI;
103 TheTimeInfo = &TTI;
Chris Lattnere2eb99e2002-04-29 04:04:29 +0000104}
105
Chris Lattner1e4867f2002-07-30 19:51:02 +0000106void PMDebug::PrintArgumentInformation(const Pass *P) {
107 // Print out passes in pass manager...
108 if (const AnalysisResolver *PM = dynamic_cast<const AnalysisResolver*>(P)) {
109 for (unsigned i = 0, e = PM->getNumContainedPasses(); i != e; ++i)
110 PrintArgumentInformation(PM->getContainedPass(i));
111
112 } else { // Normal pass. Print argument information...
113 // Print out arguments for registered passes that are _optimizations_
114 if (const PassInfo *PI = P->getPassInfo())
Chris Lattnerfbb46502006-08-27 22:21:55 +0000115 if (!PI->isAnalysisGroup())
Chris Lattner1e4867f2002-07-30 19:51:02 +0000116 std::cerr << " -" << PI->getPassArgument();
117 }
118}
Chris Lattnercdd09c22002-01-31 00:45:31 +0000119
120void PMDebug::PrintPassInformation(unsigned Depth, const char *Action,
Chris Lattner43640d72004-02-29 22:37:04 +0000121 Pass *P, Module *M) {
Chris Lattnerf5cad152002-07-22 02:10:13 +0000122 if (PassDebugging >= Executions) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000123 std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '"
Chris Lattner37104aa2002-04-29 14:57:45 +0000124 << P->getPassName();
Chris Lattner43640d72004-02-29 22:37:04 +0000125 if (M) std::cerr << "' on Module '" << M->getModuleIdentifier() << "'\n";
126 std::cerr << "'...\n";
127 }
128}
Chris Lattnera454b5b2002-04-28 05:14:06 +0000129
Chris Lattner43640d72004-02-29 22:37:04 +0000130void PMDebug::PrintPassInformation(unsigned Depth, const char *Action,
131 Pass *P, Function *F) {
132 if (PassDebugging >= Executions) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000133 std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '"
Chris Lattner43640d72004-02-29 22:37:04 +0000134 << P->getPassName();
135 if (F) std::cerr << "' on Function '" << F->getName();
136 std::cerr << "'...\n";
137 }
138}
139
140void PMDebug::PrintPassInformation(unsigned Depth, const char *Action,
141 Pass *P, BasicBlock *BB) {
142 if (PassDebugging >= Executions) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000143 std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '"
Chris Lattner43640d72004-02-29 22:37:04 +0000144 << P->getPassName();
145 if (BB) std::cerr << "' on BasicBlock '" << BB->getName();
Chris Lattnercdd09c22002-01-31 00:45:31 +0000146 std::cerr << "'...\n";
147 }
148}
149
150void PMDebug::PrintAnalysisSetInfo(unsigned Depth, const char *Msg,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000151 Pass *P, const std::vector<AnalysisID> &Set){
Chris Lattnerf5cad152002-07-22 02:10:13 +0000152 if (PassDebugging >= Details && !Set.empty()) {
Chris Lattnerac3e0602002-01-31 18:32:27 +0000153 std::cerr << (void*)P << std::string(Depth*2+3, ' ') << Msg << " Analyses:";
Chris Lattnerb3708e22002-08-30 20:23:45 +0000154 for (unsigned i = 0; i != Set.size(); ++i) {
155 if (i) std::cerr << ",";
156 std::cerr << " " << Set[i]->getPassName();
157 }
Chris Lattnercdd09c22002-01-31 00:45:31 +0000158 std::cerr << "\n";
159 }
160}
161
Chris Lattnercdd09c22002-01-31 00:45:31 +0000162//===----------------------------------------------------------------------===//
163// Pass Implementation
Chris Lattner654b5bc2002-01-22 00:17:48 +0000164//
Chris Lattnercdd09c22002-01-31 00:45:31 +0000165
Chris Lattnerc47b0812006-01-04 07:47:13 +0000166void ModulePass::addToPassManager(ModulePassManager *PM, AnalysisUsage &AU) {
Chris Lattnerc8e66542002-04-27 06:56:12 +0000167 PM->addPass(this, AU);
Chris Lattner654b5bc2002-01-22 00:17:48 +0000168}
Chris Lattner26e4f892002-01-21 07:37:31 +0000169
Chris Lattner9ad77572003-03-21 21:41:02 +0000170bool Pass::mustPreserveAnalysisID(const PassInfo *AnalysisID) const {
171 return Resolver->getAnalysisToUpdate(AnalysisID) != 0;
172}
173
Chris Lattner198cf422002-07-30 16:27:02 +0000174// dumpPassStructure - Implement the -debug-passes=Structure option
175void Pass::dumpPassStructure(unsigned Offset) {
176 std::cerr << std::string(Offset*2, ' ') << getPassName() << "\n";
177}
Chris Lattner37104aa2002-04-29 14:57:45 +0000178
Brian Gaeke465a5cc2004-02-28 21:55:18 +0000179// getPassName - Use C++ RTTI to get a SOMEWHAT intelligible name for the pass.
Chris Lattner37104aa2002-04-29 14:57:45 +0000180//
Chris Lattner071577d2002-07-29 21:02:31 +0000181const char *Pass::getPassName() const {
182 if (const PassInfo *PI = getPassInfo())
183 return PI->getPassName();
184 return typeid(*this).name();
185}
Chris Lattner37104aa2002-04-29 14:57:45 +0000186
Misha Brukman56a86422003-10-14 21:38:42 +0000187// print - Print out the internal state of the pass. This is called by Analyze
Misha Brukman7eb05a12003-08-18 14:43:39 +0000188// to print out the contents of an analysis. Otherwise it is not necessary to
Chris Lattner26750072002-07-27 01:12:17 +0000189// implement this method.
190//
Reid Spencer90839362004-12-07 04:03:45 +0000191void Pass::print(std::ostream &O,const Module*) const {
Chris Lattner26750072002-07-27 01:12:17 +0000192 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
193}
194
195// dump - call print(std::cerr);
196void Pass::dump() const {
197 print(std::cerr, 0);
198}
199
Chris Lattnercdd09c22002-01-31 00:45:31 +0000200//===----------------------------------------------------------------------===//
Chris Lattneree0788d2002-09-25 21:59:11 +0000201// ImmutablePass Implementation
202//
Chris Lattnerc47b0812006-01-04 07:47:13 +0000203void ImmutablePass::addToPassManager(ModulePassManager *PM,
Chris Lattneree0788d2002-09-25 21:59:11 +0000204 AnalysisUsage &AU) {
205 PM->addPass(this, AU);
206}
207
208
209//===----------------------------------------------------------------------===//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000210// FunctionPass Implementation
Chris Lattner26e4f892002-01-21 07:37:31 +0000211//
Chris Lattnercdd09c22002-01-31 00:45:31 +0000212
Chris Lattnerc8e66542002-04-27 06:56:12 +0000213// run - On a module, we run this pass by initializing, runOnFunction'ing once
214// for every function in the module, then by finalizing.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000215//
Chris Lattner79e523d2004-09-20 04:47:19 +0000216bool FunctionPass::runOnModule(Module &M) {
Chris Lattnercdd09c22002-01-31 00:45:31 +0000217 bool Changed = doInitialization(M);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000218
Chris Lattner113f4f42002-06-25 16:13:24 +0000219 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
220 if (!I->isExternal()) // Passes are not run on external functions!
Chris Lattnerc8e66542002-04-27 06:56:12 +0000221 Changed |= runOnFunction(*I);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000222
Chris Lattnercdd09c22002-01-31 00:45:31 +0000223 return Changed | doFinalization(M);
Chris Lattner26e4f892002-01-21 07:37:31 +0000224}
225
Chris Lattnerc8e66542002-04-27 06:56:12 +0000226// run - On a function, we simply initialize, run the function, then finalize.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000227//
Chris Lattner113f4f42002-06-25 16:13:24 +0000228bool FunctionPass::run(Function &F) {
229 if (F.isExternal()) return false;// Passes are not run on external functions!
Chris Lattnercdd09c22002-01-31 00:45:31 +0000230
Chris Lattner79e523d2004-09-20 04:47:19 +0000231 bool Changed = doInitialization(*F.getParent());
232 Changed |= runOnFunction(F);
233 return Changed | doFinalization(*F.getParent());
Chris Lattner26e4f892002-01-21 07:37:31 +0000234}
Chris Lattnerd013ba92002-01-23 05:49:41 +0000235
Chris Lattnerc47b0812006-01-04 07:47:13 +0000236void FunctionPass::addToPassManager(ModulePassManager *PM,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000237 AnalysisUsage &AU) {
238 PM->addPass(this, AU);
Chris Lattnerd013ba92002-01-23 05:49:41 +0000239}
Chris Lattnercdd09c22002-01-31 00:45:31 +0000240
Chris Lattnerc47b0812006-01-04 07:47:13 +0000241void FunctionPass::addToPassManager(FunctionPassManagerT *PM,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000242 AnalysisUsage &AU) {
243 PM->addPass(this, AU);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000244}
245
246//===----------------------------------------------------------------------===//
247// BasicBlockPass Implementation
248//
249
Chris Lattnerc8e66542002-04-27 06:56:12 +0000250// To run this pass on a function, we simply call runOnBasicBlock once for each
251// function.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000252//
Chris Lattner113f4f42002-06-25 16:13:24 +0000253bool BasicBlockPass::runOnFunction(Function &F) {
Chris Lattnerbae3c672002-09-12 17:06:40 +0000254 bool Changed = doInitialization(F);
Chris Lattner113f4f42002-06-25 16:13:24 +0000255 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Chris Lattnercdd09c22002-01-31 00:45:31 +0000256 Changed |= runOnBasicBlock(*I);
Chris Lattnerbae3c672002-09-12 17:06:40 +0000257 return Changed | doFinalization(F);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000258}
259
260// To run directly on the basic block, we initialize, runOnBasicBlock, then
261// finalize.
262//
Chris Lattner79e523d2004-09-20 04:47:19 +0000263bool BasicBlockPass::runPass(BasicBlock &BB) {
Chris Lattnerbae3c672002-09-12 17:06:40 +0000264 Function &F = *BB.getParent();
265 Module &M = *F.getParent();
Chris Lattner79e523d2004-09-20 04:47:19 +0000266 bool Changed = doInitialization(M);
267 Changed |= doInitialization(F);
268 Changed |= runOnBasicBlock(BB);
269 Changed |= doFinalization(F);
270 Changed |= doFinalization(M);
271 return Changed;
Chris Lattnercdd09c22002-01-31 00:45:31 +0000272}
273
Chris Lattnerc47b0812006-01-04 07:47:13 +0000274void BasicBlockPass::addToPassManager(FunctionPassManagerT *PM,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000275 AnalysisUsage &AU) {
276 PM->addPass(this, AU);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000277}
278
Chris Lattnerc47b0812006-01-04 07:47:13 +0000279void BasicBlockPass::addToPassManager(BasicBlockPassManager *PM,
Chris Lattnerc8e66542002-04-27 06:56:12 +0000280 AnalysisUsage &AU) {
281 PM->addPass(this, AU);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000282}
283
Chris Lattner37d3c952002-07-23 18:08:00 +0000284
285//===----------------------------------------------------------------------===//
286// Pass Registration mechanism
287//
288static std::map<TypeInfo, PassInfo*> *PassInfoMap = 0;
289static std::vector<PassRegistrationListener*> *Listeners = 0;
290
291// getPassInfo - Return the PassInfo data structure that corresponds to this
292// pass...
293const PassInfo *Pass::getPassInfo() const {
Chris Lattner071577d2002-07-29 21:02:31 +0000294 if (PassInfoCache) return PassInfoCache;
Chris Lattner4b169632002-08-21 17:08:37 +0000295 return lookupPassInfo(typeid(*this));
296}
297
298const PassInfo *Pass::lookupPassInfo(const std::type_info &TI) {
Chris Lattner071577d2002-07-29 21:02:31 +0000299 if (PassInfoMap == 0) return 0;
Chris Lattner4b169632002-08-21 17:08:37 +0000300 std::map<TypeInfo, PassInfo*>::iterator I = PassInfoMap->find(TI);
Chris Lattner071577d2002-07-29 21:02:31 +0000301 return (I != PassInfoMap->end()) ? I->second : 0;
Chris Lattner37d3c952002-07-23 18:08:00 +0000302}
303
Chris Lattnerc66da832006-01-23 01:01:04 +0000304void RegisterPassBase::registerPass() {
Chris Lattner37d3c952002-07-23 18:08:00 +0000305 if (PassInfoMap == 0)
306 PassInfoMap = new std::map<TypeInfo, PassInfo*>();
307
Chris Lattnerc66da832006-01-23 01:01:04 +0000308 assert(PassInfoMap->find(PIObj.getTypeInfo()) == PassInfoMap->end() &&
Chris Lattner37d3c952002-07-23 18:08:00 +0000309 "Pass already registered!");
Chris Lattnerc66da832006-01-23 01:01:04 +0000310 PassInfoMap->insert(std::make_pair(TypeInfo(PIObj.getTypeInfo()), &PIObj));
Chris Lattner37d3c952002-07-23 18:08:00 +0000311
312 // Notify any listeners...
313 if (Listeners)
314 for (std::vector<PassRegistrationListener*>::iterator
315 I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
Chris Lattnerc66da832006-01-23 01:01:04 +0000316 (*I)->passRegistered(&PIObj);
Chris Lattner37d3c952002-07-23 18:08:00 +0000317}
318
Chris Lattnerc66da832006-01-23 01:01:04 +0000319void RegisterPassBase::unregisterPass() {
Chris Lattner37d3c952002-07-23 18:08:00 +0000320 assert(PassInfoMap && "Pass registered but not in map!");
321 std::map<TypeInfo, PassInfo*>::iterator I =
Chris Lattnerc66da832006-01-23 01:01:04 +0000322 PassInfoMap->find(PIObj.getTypeInfo());
Chris Lattner37d3c952002-07-23 18:08:00 +0000323 assert(I != PassInfoMap->end() && "Pass registered but not in map!");
324
325 // Remove pass from the map...
326 PassInfoMap->erase(I);
327 if (PassInfoMap->empty()) {
328 delete PassInfoMap;
329 PassInfoMap = 0;
330 }
331
332 // Notify any listeners...
333 if (Listeners)
334 for (std::vector<PassRegistrationListener*>::iterator
335 I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
Chris Lattnerc66da832006-01-23 01:01:04 +0000336 (*I)->passUnregistered(&PIObj);
Chris Lattner37d3c952002-07-23 18:08:00 +0000337}
338
Chris Lattner6e041bd2002-08-21 22:17:09 +0000339//===----------------------------------------------------------------------===//
340// Analysis Group Implementation Code
341//===----------------------------------------------------------------------===//
342
343struct AnalysisGroupInfo {
344 const PassInfo *DefaultImpl;
345 std::set<const PassInfo *> Implementations;
346 AnalysisGroupInfo() : DefaultImpl(0) {}
347};
348
349static std::map<const PassInfo *, AnalysisGroupInfo> *AnalysisGroupInfoMap = 0;
350
351// RegisterAGBase implementation
352//
353RegisterAGBase::RegisterAGBase(const std::type_info &Interface,
354 const std::type_info *Pass, bool isDefault)
Chris Lattnerfbb46502006-08-27 22:21:55 +0000355 : RegisterPassBase(Interface),
Chris Lattnerc66da832006-01-23 01:01:04 +0000356 ImplementationInfo(0), isDefaultImplementation(isDefault) {
Chris Lattner6e041bd2002-08-21 22:17:09 +0000357
Chris Lattner6e041bd2002-08-21 22:17:09 +0000358 InterfaceInfo = const_cast<PassInfo*>(Pass::lookupPassInfo(Interface));
Chris Lattnerc66da832006-01-23 01:01:04 +0000359 if (InterfaceInfo == 0) {
360 // First reference to Interface, register it now.
361 registerPass();
362 InterfaceInfo = &PIObj;
Chris Lattner6e041bd2002-08-21 22:17:09 +0000363 }
Chris Lattnerfbb46502006-08-27 22:21:55 +0000364 assert(PIObj.isAnalysisGroup() &&
Chris Lattner6e041bd2002-08-21 22:17:09 +0000365 "Trying to join an analysis group that is a normal pass!");
366
367 if (Pass) {
Chris Lattner6e041bd2002-08-21 22:17:09 +0000368 ImplementationInfo = Pass::lookupPassInfo(*Pass);
369 assert(ImplementationInfo &&
370 "Must register pass before adding to AnalysisGroup!");
371
Chris Lattnerb3708e22002-08-30 20:23:45 +0000372 // Make sure we keep track of the fact that the implementation implements
373 // the interface.
374 PassInfo *IIPI = const_cast<PassInfo*>(ImplementationInfo);
375 IIPI->addInterfaceImplemented(InterfaceInfo);
376
Chris Lattner6e041bd2002-08-21 22:17:09 +0000377 // Lazily allocate to avoid nasty initialization order dependencies
378 if (AnalysisGroupInfoMap == 0)
379 AnalysisGroupInfoMap = new std::map<const PassInfo *,AnalysisGroupInfo>();
380
381 AnalysisGroupInfo &AGI = (*AnalysisGroupInfoMap)[InterfaceInfo];
382 assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
383 "Cannot add a pass to the same analysis group more than once!");
384 AGI.Implementations.insert(ImplementationInfo);
385 if (isDefault) {
386 assert(AGI.DefaultImpl == 0 && InterfaceInfo->getNormalCtor() == 0 &&
387 "Default implementation for analysis group already specified!");
388 assert(ImplementationInfo->getNormalCtor() &&
389 "Cannot specify pass as default if it does not have a default ctor");
390 AGI.DefaultImpl = ImplementationInfo;
391 InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
392 }
393 }
394}
395
396void RegisterAGBase::setGroupName(const char *Name) {
397 assert(InterfaceInfo->getPassName()[0] == 0 && "Interface Name already set!");
398 InterfaceInfo->setPassName(Name);
399}
400
401RegisterAGBase::~RegisterAGBase() {
402 if (ImplementationInfo) {
403 assert(AnalysisGroupInfoMap && "Inserted into map, but map doesn't exist?");
404 AnalysisGroupInfo &AGI = (*AnalysisGroupInfoMap)[InterfaceInfo];
405
406 assert(AGI.Implementations.count(ImplementationInfo) &&
407 "Pass not a member of analysis group?");
408
409 if (AGI.DefaultImpl == ImplementationInfo)
410 AGI.DefaultImpl = 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000411
Chris Lattner6e041bd2002-08-21 22:17:09 +0000412 AGI.Implementations.erase(ImplementationInfo);
413
414 // Last member of this analysis group? Unregister PassInfo, delete map entry
415 if (AGI.Implementations.empty()) {
416 assert(AGI.DefaultImpl == 0 &&
417 "Default implementation didn't unregister?");
418 AnalysisGroupInfoMap->erase(InterfaceInfo);
419 if (AnalysisGroupInfoMap->empty()) { // Delete map if empty
420 delete AnalysisGroupInfoMap;
421 AnalysisGroupInfoMap = 0;
422 }
Chris Lattner6e041bd2002-08-21 22:17:09 +0000423 }
424 }
Chris Lattnerc66da832006-01-23 01:01:04 +0000425
426 if (InterfaceInfo == &PIObj)
427 unregisterPass();
Chris Lattner6e041bd2002-08-21 22:17:09 +0000428}
429
430
Chris Lattner37d3c952002-07-23 18:08:00 +0000431//===----------------------------------------------------------------------===//
432// PassRegistrationListener implementation
433//
434
435// PassRegistrationListener ctor - Add the current object to the list of
436// PassRegistrationListeners...
437PassRegistrationListener::PassRegistrationListener() {
438 if (!Listeners) Listeners = new std::vector<PassRegistrationListener*>();
439 Listeners->push_back(this);
440}
441
442// dtor - Remove object from list of listeners...
443PassRegistrationListener::~PassRegistrationListener() {
444 std::vector<PassRegistrationListener*>::iterator I =
445 std::find(Listeners->begin(), Listeners->end(), this);
446 assert(Listeners && I != Listeners->end() &&
447 "PassRegistrationListener not registered!");
448 Listeners->erase(I);
449
450 if (Listeners->empty()) {
451 delete Listeners;
452 Listeners = 0;
453 }
454}
455
456// enumeratePasses - Iterate over the registered passes, calling the
457// passEnumerate callback on each PassInfo object.
458//
459void PassRegistrationListener::enumeratePasses() {
460 if (PassInfoMap)
461 for (std::map<TypeInfo, PassInfo*>::iterator I = PassInfoMap->begin(),
462 E = PassInfoMap->end(); I != E; ++I)
463 passEnumerate(I->second);
464}
Reid Spencer8edc8be2005-04-25 01:01:35 +0000465
Chris Lattner23d54052006-12-01 22:21:11 +0000466//===----------------------------------------------------------------------===//
467// AnalysisUsage Class Implementation
468//
469
470// setPreservesCFG - This function should be called to by the pass, iff they do
471// not:
472//
473// 1. Add or remove basic blocks from the function
474// 2. Modify terminator instructions in any way.
475//
476// This function annotates the AnalysisUsage info object to say that analyses
477// that only depend on the CFG are preserved by this pass.
478//
479void AnalysisUsage::setPreservesCFG() {
480 // Since this transformation doesn't modify the CFG, it preserves all analyses
481 // that only depend on the CFG (like dominators, loop info, etc...)
482 //
483 if (PassInfoMap) {
484 for (std::map<TypeInfo, PassInfo*>::iterator I = PassInfoMap->begin(),
485 E = PassInfoMap->end(); I != E; ++I)
486 if (I->second->isCFGOnlyPass())
487 Preserved.push_back(I->second);
488 }
489}
490
491