1 //===- Pass.cpp - LLVM Pass Infrastructure Implementation -----------------===//
3 // This file implements the LLVM Pass infrastructure. It is primarily
4 // responsible with ensuring that passes are executed and batched together
7 //===----------------------------------------------------------------------===//
9 #include "llvm/PassManager.h"
10 #include "PassManagerT.h" // PassManagerT implementation
11 #include "llvm/Module.h"
12 #include "Support/STLExtras.h"
13 #include "Support/TypeInfo.h"
16 // IncludeFile - Stub function used to help linking out.
17 IncludeFile::IncludeFile(void*) {}
19 //===----------------------------------------------------------------------===//
20 // AnalysisID Class Implementation
23 // getCFGOnlyAnalyses - A wrapper around the CFGOnlyAnalyses which make it
24 // initializer order independent.
25 static std::vector<const PassInfo*> &getCFGOnlyAnalyses() {
26 static std::vector<const PassInfo*> CFGOnlyAnalyses;
27 return CFGOnlyAnalyses;
30 void RegisterPassBase::setOnlyUsesCFG() {
31 getCFGOnlyAnalyses().push_back(PIObj);
34 //===----------------------------------------------------------------------===//
35 // AnalysisResolver Class Implementation
38 void AnalysisResolver::setAnalysisResolver(Pass *P, AnalysisResolver *AR) {
39 assert(P->Resolver == 0 && "Pass already in a PassManager!");
43 //===----------------------------------------------------------------------===//
44 // AnalysisUsage Class Implementation
47 // setPreservesCFG - This function should be called to by the pass, iff they do
50 // 1. Add or remove basic blocks from the function
51 // 2. Modify terminator instructions in any way.
53 // This function annotates the AnalysisUsage info object to say that analyses
54 // that only depend on the CFG are preserved by this pass.
56 void AnalysisUsage::setPreservesCFG() {
57 // Since this transformation doesn't modify the CFG, it preserves all analyses
58 // that only depend on the CFG (like dominators, loop info, etc...)
60 Preserved.insert(Preserved.end(),
61 getCFGOnlyAnalyses().begin(), getCFGOnlyAnalyses().end());
65 //===----------------------------------------------------------------------===//
66 // PassManager implementation - The PassManager class is a simple Pimpl class
67 // that wraps the PassManagerT template.
69 PassManager::PassManager() : PM(new PassManagerT<Module>()) {}
70 PassManager::~PassManager() { delete PM; }
71 void PassManager::add(Pass *P) { PM->add(P); }
72 bool PassManager::run(Module &M) { return PM->run(M); }
74 //===----------------------------------------------------------------------===//
75 // FunctionPassManager implementation - The FunctionPassManager class
76 // is a simple Pimpl class that wraps the PassManagerT template. It
77 // is like PassManager, but only deals in FunctionPasses.
79 FunctionPassManager::FunctionPassManager() : PM(new PassManagerT<Function>()) {}
80 FunctionPassManager::~FunctionPassManager() { delete PM; }
81 void FunctionPassManager::add(FunctionPass *P) { PM->add(P); }
82 void FunctionPassManager::add(ImmutablePass *IP) { PM->add(IP); }
83 bool FunctionPassManager::run(Function &F) { return PM->run(F); }
86 //===----------------------------------------------------------------------===//
87 // TimingInfo Class - This class is used to calculate information about the
88 // amount of time each pass takes to execute. This only happens with
89 // -time-passes is enabled on the command line.
92 EnableTiming("time-passes",
93 cl::desc("Time each pass, printing elapsed time for each on exit"));
95 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
96 // a non null value (if the -time-passes option is enabled) or it leaves it
97 // null. It may be called multiple times.
98 void TimingInfo::createTheTimeInfo() {
99 if (!EnableTiming || TheTimeInfo) return;
101 // Constructed the first time this is called, iff -time-passes is enabled.
102 // This guarantees that the object will be constructed before static globals,
103 // thus it will be destroyed before them.
104 static TimingInfo TTI;
108 void PMDebug::PrintArgumentInformation(const Pass *P) {
109 // Print out passes in pass manager...
110 if (const AnalysisResolver *PM = dynamic_cast<const AnalysisResolver*>(P)) {
111 for (unsigned i = 0, e = PM->getNumContainedPasses(); i != e; ++i)
112 PrintArgumentInformation(PM->getContainedPass(i));
114 } else { // Normal pass. Print argument information...
115 // Print out arguments for registered passes that are _optimizations_
116 if (const PassInfo *PI = P->getPassInfo())
117 if (PI->getPassType() & PassInfo::Optimization)
118 std::cerr << " -" << PI->getPassArgument();
122 void PMDebug::PrintPassInformation(unsigned Depth, const char *Action,
123 Pass *P, Annotable *V) {
124 if (PassDebugging >= Executions) {
125 std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '"
128 std::cerr << "' on ";
130 if (dynamic_cast<Module*>(V)) {
131 std::cerr << "Module\n"; return;
132 } else if (Function *F = dynamic_cast<Function*>(V))
133 std::cerr << "Function '" << F->getName();
134 else if (BasicBlock *BB = dynamic_cast<BasicBlock*>(V))
135 std::cerr << "BasicBlock '" << BB->getName();
136 else if (Value *Val = dynamic_cast<Value*>(V))
137 std::cerr << typeid(*Val).name() << " '" << Val->getName();
139 std::cerr << "'...\n";
143 void PMDebug::PrintAnalysisSetInfo(unsigned Depth, const char *Msg,
144 Pass *P, const std::vector<AnalysisID> &Set){
145 if (PassDebugging >= Details && !Set.empty()) {
146 std::cerr << (void*)P << std::string(Depth*2+3, ' ') << Msg << " Analyses:";
147 for (unsigned i = 0; i != Set.size(); ++i) {
148 if (i) std::cerr << ",";
149 std::cerr << " " << Set[i]->getPassName();
155 //===----------------------------------------------------------------------===//
156 // Pass Implementation
159 void Pass::addToPassManager(PassManagerT<Module> *PM, AnalysisUsage &AU) {
160 PM->addPass(this, AU);
163 bool Pass::mustPreserveAnalysisID(const PassInfo *AnalysisID) const {
164 return Resolver->getAnalysisToUpdate(AnalysisID) != 0;
167 // dumpPassStructure - Implement the -debug-passes=Structure option
168 void Pass::dumpPassStructure(unsigned Offset) {
169 std::cerr << std::string(Offset*2, ' ') << getPassName() << "\n";
172 // getPassName - Use C++ RTTI to get a SOMEWHAT intelligable name for the pass.
174 const char *Pass::getPassName() const {
175 if (const PassInfo *PI = getPassInfo())
176 return PI->getPassName();
177 return typeid(*this).name();
180 // print - Print out the internal state of the pass. This is called by Analyse
181 // to print out the contents of an analysis. Otherwise it is not necessary to
182 // implement this method.
184 void Pass::print(std::ostream &O) const {
185 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
188 // dump - call print(std::cerr);
189 void Pass::dump() const {
193 //===----------------------------------------------------------------------===//
194 // ImmutablePass Implementation
196 void ImmutablePass::addToPassManager(PassManagerT<Module> *PM,
198 PM->addPass(this, AU);
202 //===----------------------------------------------------------------------===//
203 // FunctionPass Implementation
206 // run - On a module, we run this pass by initializing, runOnFunction'ing once
207 // for every function in the module, then by finalizing.
209 bool FunctionPass::run(Module &M) {
210 bool Changed = doInitialization(M);
212 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
213 if (!I->isExternal()) // Passes are not run on external functions!
214 Changed |= runOnFunction(*I);
216 return Changed | doFinalization(M);
219 // run - On a function, we simply initialize, run the function, then finalize.
221 bool FunctionPass::run(Function &F) {
222 if (F.isExternal()) return false;// Passes are not run on external functions!
224 return doInitialization(*F.getParent()) | runOnFunction(F)
225 | doFinalization(*F.getParent());
228 void FunctionPass::addToPassManager(PassManagerT<Module> *PM,
230 PM->addPass(this, AU);
233 void FunctionPass::addToPassManager(PassManagerT<Function> *PM,
235 PM->addPass(this, AU);
238 //===----------------------------------------------------------------------===//
239 // BasicBlockPass Implementation
242 // To run this pass on a function, we simply call runOnBasicBlock once for each
245 bool BasicBlockPass::runOnFunction(Function &F) {
246 bool Changed = doInitialization(F);
247 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
248 Changed |= runOnBasicBlock(*I);
249 return Changed | doFinalization(F);
252 // To run directly on the basic block, we initialize, runOnBasicBlock, then
255 bool BasicBlockPass::run(BasicBlock &BB) {
256 Function &F = *BB.getParent();
257 Module &M = *F.getParent();
258 return doInitialization(M) | doInitialization(F) | runOnBasicBlock(BB) |
259 doFinalization(F) | doFinalization(M);
262 void BasicBlockPass::addToPassManager(PassManagerT<Function> *PM,
264 PM->addPass(this, AU);
267 void BasicBlockPass::addToPassManager(PassManagerT<BasicBlock> *PM,
269 PM->addPass(this, AU);
273 //===----------------------------------------------------------------------===//
274 // Pass Registration mechanism
276 static std::map<TypeInfo, PassInfo*> *PassInfoMap = 0;
277 static std::vector<PassRegistrationListener*> *Listeners = 0;
279 // getPassInfo - Return the PassInfo data structure that corresponds to this
281 const PassInfo *Pass::getPassInfo() const {
282 if (PassInfoCache) return PassInfoCache;
283 return lookupPassInfo(typeid(*this));
286 const PassInfo *Pass::lookupPassInfo(const std::type_info &TI) {
287 if (PassInfoMap == 0) return 0;
288 std::map<TypeInfo, PassInfo*>::iterator I = PassInfoMap->find(TI);
289 return (I != PassInfoMap->end()) ? I->second : 0;
292 void RegisterPassBase::registerPass(PassInfo *PI) {
293 if (PassInfoMap == 0)
294 PassInfoMap = new std::map<TypeInfo, PassInfo*>();
296 assert(PassInfoMap->find(PI->getTypeInfo()) == PassInfoMap->end() &&
297 "Pass already registered!");
299 PassInfoMap->insert(std::make_pair(TypeInfo(PI->getTypeInfo()), PI));
301 // Notify any listeners...
303 for (std::vector<PassRegistrationListener*>::iterator
304 I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
305 (*I)->passRegistered(PI);
308 void RegisterPassBase::unregisterPass(PassInfo *PI) {
309 assert(PassInfoMap && "Pass registered but not in map!");
310 std::map<TypeInfo, PassInfo*>::iterator I =
311 PassInfoMap->find(PI->getTypeInfo());
312 assert(I != PassInfoMap->end() && "Pass registered but not in map!");
314 // Remove pass from the map...
315 PassInfoMap->erase(I);
316 if (PassInfoMap->empty()) {
321 // Notify any listeners...
323 for (std::vector<PassRegistrationListener*>::iterator
324 I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
325 (*I)->passUnregistered(PI);
327 // Delete the PassInfo object itself...
331 //===----------------------------------------------------------------------===//
332 // Analysis Group Implementation Code
333 //===----------------------------------------------------------------------===//
335 struct AnalysisGroupInfo {
336 const PassInfo *DefaultImpl;
337 std::set<const PassInfo *> Implementations;
338 AnalysisGroupInfo() : DefaultImpl(0) {}
341 static std::map<const PassInfo *, AnalysisGroupInfo> *AnalysisGroupInfoMap = 0;
343 // RegisterAGBase implementation
345 RegisterAGBase::RegisterAGBase(const std::type_info &Interface,
346 const std::type_info *Pass, bool isDefault)
347 : ImplementationInfo(0), isDefaultImplementation(isDefault) {
349 InterfaceInfo = const_cast<PassInfo*>(Pass::lookupPassInfo(Interface));
350 if (InterfaceInfo == 0) { // First reference to Interface, add it now.
351 InterfaceInfo = // Create the new PassInfo for the interface...
352 new PassInfo("", "", Interface, PassInfo::AnalysisGroup, 0, 0);
353 registerPass(InterfaceInfo);
356 assert(InterfaceInfo->getPassType() == PassInfo::AnalysisGroup &&
357 "Trying to join an analysis group that is a normal pass!");
360 ImplementationInfo = Pass::lookupPassInfo(*Pass);
361 assert(ImplementationInfo &&
362 "Must register pass before adding to AnalysisGroup!");
364 // Make sure we keep track of the fact that the implementation implements
366 PassInfo *IIPI = const_cast<PassInfo*>(ImplementationInfo);
367 IIPI->addInterfaceImplemented(InterfaceInfo);
369 // Lazily allocate to avoid nasty initialization order dependencies
370 if (AnalysisGroupInfoMap == 0)
371 AnalysisGroupInfoMap = new std::map<const PassInfo *,AnalysisGroupInfo>();
373 AnalysisGroupInfo &AGI = (*AnalysisGroupInfoMap)[InterfaceInfo];
374 assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
375 "Cannot add a pass to the same analysis group more than once!");
376 AGI.Implementations.insert(ImplementationInfo);
378 assert(AGI.DefaultImpl == 0 && InterfaceInfo->getNormalCtor() == 0 &&
379 "Default implementation for analysis group already specified!");
380 assert(ImplementationInfo->getNormalCtor() &&
381 "Cannot specify pass as default if it does not have a default ctor");
382 AGI.DefaultImpl = ImplementationInfo;
383 InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
388 void RegisterAGBase::setGroupName(const char *Name) {
389 assert(InterfaceInfo->getPassName()[0] == 0 && "Interface Name already set!");
390 InterfaceInfo->setPassName(Name);
393 RegisterAGBase::~RegisterAGBase() {
394 if (ImplementationInfo) {
395 assert(AnalysisGroupInfoMap && "Inserted into map, but map doesn't exist?");
396 AnalysisGroupInfo &AGI = (*AnalysisGroupInfoMap)[InterfaceInfo];
398 assert(AGI.Implementations.count(ImplementationInfo) &&
399 "Pass not a member of analysis group?");
401 if (AGI.DefaultImpl == ImplementationInfo)
404 AGI.Implementations.erase(ImplementationInfo);
406 // Last member of this analysis group? Unregister PassInfo, delete map entry
407 if (AGI.Implementations.empty()) {
408 assert(AGI.DefaultImpl == 0 &&
409 "Default implementation didn't unregister?");
410 AnalysisGroupInfoMap->erase(InterfaceInfo);
411 if (AnalysisGroupInfoMap->empty()) { // Delete map if empty
412 delete AnalysisGroupInfoMap;
413 AnalysisGroupInfoMap = 0;
416 unregisterPass(InterfaceInfo);
422 //===----------------------------------------------------------------------===//
423 // PassRegistrationListener implementation
426 // PassRegistrationListener ctor - Add the current object to the list of
427 // PassRegistrationListeners...
428 PassRegistrationListener::PassRegistrationListener() {
429 if (!Listeners) Listeners = new std::vector<PassRegistrationListener*>();
430 Listeners->push_back(this);
433 // dtor - Remove object from list of listeners...
434 PassRegistrationListener::~PassRegistrationListener() {
435 std::vector<PassRegistrationListener*>::iterator I =
436 std::find(Listeners->begin(), Listeners->end(), this);
437 assert(Listeners && I != Listeners->end() &&
438 "PassRegistrationListener not registered!");
441 if (Listeners->empty()) {
447 // enumeratePasses - Iterate over the registered passes, calling the
448 // passEnumerate callback on each PassInfo object.
450 void PassRegistrationListener::enumeratePasses() {
452 for (std::map<TypeInfo, PassInfo*>::iterator I = PassInfoMap->begin(),
453 E = PassInfoMap->end(); I != E; ++I)
454 passEnumerate(I->second);