1 //===- Pass.cpp - LLVM Pass Infrastructure Implementation -----------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the LLVM Pass infrastructure. It is primarily
11 // responsible with ensuring that passes are executed and batched together
14 //===----------------------------------------------------------------------===//
16 #include "llvm/PassManager.h"
17 #include "PassManagerT.h" // PassManagerT implementation
18 #include "llvm/Module.h"
19 #include "llvm/ModuleProvider.h"
20 #include "Support/STLExtras.h"
21 #include "Support/TypeInfo.h"
26 // IncludeFile - Stub function used to help linking out.
27 IncludeFile::IncludeFile(void*) {}
29 //===----------------------------------------------------------------------===//
30 // AnalysisID Class Implementation
33 // getCFGOnlyAnalyses - A wrapper around the CFGOnlyAnalyses which make it
34 // initializer order independent.
35 static std::vector<const PassInfo*> &getCFGOnlyAnalyses() {
36 static std::vector<const PassInfo*> CFGOnlyAnalyses;
37 return CFGOnlyAnalyses;
40 void RegisterPassBase::setOnlyUsesCFG() {
41 getCFGOnlyAnalyses().push_back(PIObj);
44 //===----------------------------------------------------------------------===//
45 // AnalysisResolver Class Implementation
48 void AnalysisResolver::setAnalysisResolver(Pass *P, AnalysisResolver *AR) {
49 assert(P->Resolver == 0 && "Pass already in a PassManager!");
53 //===----------------------------------------------------------------------===//
54 // AnalysisUsage Class Implementation
57 // setPreservesCFG - This function should be called to by the pass, iff they do
60 // 1. Add or remove basic blocks from the function
61 // 2. Modify terminator instructions in any way.
63 // This function annotates the AnalysisUsage info object to say that analyses
64 // that only depend on the CFG are preserved by this pass.
66 void AnalysisUsage::setPreservesCFG() {
67 // Since this transformation doesn't modify the CFG, it preserves all analyses
68 // that only depend on the CFG (like dominators, loop info, etc...)
70 Preserved.insert(Preserved.end(),
71 getCFGOnlyAnalyses().begin(), getCFGOnlyAnalyses().end());
75 //===----------------------------------------------------------------------===//
76 // PassManager implementation - The PassManager class is a simple Pimpl class
77 // that wraps the PassManagerT template.
79 PassManager::PassManager() : PM(new PassManagerT<Module>()) {}
80 PassManager::~PassManager() { delete PM; }
81 void PassManager::add(Pass *P) { PM->add(P); }
82 bool PassManager::run(Module &M) { return PM->run(M); }
84 //===----------------------------------------------------------------------===//
85 // FunctionPassManager implementation - The FunctionPassManager class
86 // is a simple Pimpl class that wraps the PassManagerT template. It
87 // is like PassManager, but only deals in FunctionPasses.
89 FunctionPassManager::FunctionPassManager(ModuleProvider *P) :
90 PM(new PassManagerT<Function>()), MP(P) {}
91 FunctionPassManager::~FunctionPassManager() { delete PM; }
92 void FunctionPassManager::add(FunctionPass *P) { PM->add(P); }
93 void FunctionPassManager::add(ImmutablePass *IP) { PM->add(IP); }
94 bool FunctionPassManager::run(Function &F) {
95 MP->materializeFunction(&F);
100 //===----------------------------------------------------------------------===//
101 // TimingInfo Class - This class is used to calculate information about the
102 // amount of time each pass takes to execute. This only happens with
103 // -time-passes is enabled on the command line.
106 EnableTiming("time-passes",
107 cl::desc("Time each pass, printing elapsed time for each on exit"));
109 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
110 // a non null value (if the -time-passes option is enabled) or it leaves it
111 // null. It may be called multiple times.
112 void TimingInfo::createTheTimeInfo() {
113 if (!EnableTiming || TheTimeInfo) return;
115 // Constructed the first time this is called, iff -time-passes is enabled.
116 // This guarantees that the object will be constructed before static globals,
117 // thus it will be destroyed before them.
118 static TimingInfo TTI;
122 void PMDebug::PrintArgumentInformation(const Pass *P) {
123 // Print out passes in pass manager...
124 if (const AnalysisResolver *PM = dynamic_cast<const AnalysisResolver*>(P)) {
125 for (unsigned i = 0, e = PM->getNumContainedPasses(); i != e; ++i)
126 PrintArgumentInformation(PM->getContainedPass(i));
128 } else { // Normal pass. Print argument information...
129 // Print out arguments for registered passes that are _optimizations_
130 if (const PassInfo *PI = P->getPassInfo())
131 if (PI->getPassType() & PassInfo::Optimization)
132 std::cerr << " -" << PI->getPassArgument();
136 void PMDebug::PrintPassInformation(unsigned Depth, const char *Action,
137 Pass *P, Module *M) {
138 if (PassDebugging >= Executions) {
139 std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '"
141 if (M) std::cerr << "' on Module '" << M->getModuleIdentifier() << "'\n";
142 std::cerr << "'...\n";
146 void PMDebug::PrintPassInformation(unsigned Depth, const char *Action,
147 Pass *P, Function *F) {
148 if (PassDebugging >= Executions) {
149 std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '"
151 if (F) std::cerr << "' on Function '" << F->getName();
152 std::cerr << "'...\n";
156 void PMDebug::PrintPassInformation(unsigned Depth, const char *Action,
157 Pass *P, BasicBlock *BB) {
158 if (PassDebugging >= Executions) {
159 std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '"
161 if (BB) std::cerr << "' on BasicBlock '" << BB->getName();
162 std::cerr << "'...\n";
166 void PMDebug::PrintAnalysisSetInfo(unsigned Depth, const char *Msg,
167 Pass *P, const std::vector<AnalysisID> &Set){
168 if (PassDebugging >= Details && !Set.empty()) {
169 std::cerr << (void*)P << std::string(Depth*2+3, ' ') << Msg << " Analyses:";
170 for (unsigned i = 0; i != Set.size(); ++i) {
171 if (i) std::cerr << ",";
172 std::cerr << " " << Set[i]->getPassName();
178 //===----------------------------------------------------------------------===//
179 // Pass Implementation
182 void Pass::addToPassManager(PassManagerT<Module> *PM, AnalysisUsage &AU) {
183 PM->addPass(this, AU);
186 bool Pass::mustPreserveAnalysisID(const PassInfo *AnalysisID) const {
187 return Resolver->getAnalysisToUpdate(AnalysisID) != 0;
190 // dumpPassStructure - Implement the -debug-passes=Structure option
191 void Pass::dumpPassStructure(unsigned Offset) {
192 std::cerr << std::string(Offset*2, ' ') << getPassName() << "\n";
195 // getPassName - Use C++ RTTI to get a SOMEWHAT intelligible name for the pass.
197 const char *Pass::getPassName() const {
198 if (const PassInfo *PI = getPassInfo())
199 return PI->getPassName();
200 return typeid(*this).name();
203 // print - Print out the internal state of the pass. This is called by Analyze
204 // to print out the contents of an analysis. Otherwise it is not necessary to
205 // implement this method.
207 void Pass::print(std::ostream &O) const {
208 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
211 // dump - call print(std::cerr);
212 void Pass::dump() const {
216 //===----------------------------------------------------------------------===//
217 // ImmutablePass Implementation
219 void ImmutablePass::addToPassManager(PassManagerT<Module> *PM,
221 PM->addPass(this, AU);
225 //===----------------------------------------------------------------------===//
226 // FunctionPass Implementation
229 // run - On a module, we run this pass by initializing, runOnFunction'ing once
230 // for every function in the module, then by finalizing.
232 bool FunctionPass::run(Module &M) {
233 bool Changed = doInitialization(M);
235 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
236 if (!I->isExternal()) // Passes are not run on external functions!
237 Changed |= runOnFunction(*I);
239 return Changed | doFinalization(M);
242 // run - On a function, we simply initialize, run the function, then finalize.
244 bool FunctionPass::run(Function &F) {
245 if (F.isExternal()) return false;// Passes are not run on external functions!
247 return doInitialization(*F.getParent()) | runOnFunction(F)
248 | doFinalization(*F.getParent());
251 void FunctionPass::addToPassManager(PassManagerT<Module> *PM,
253 PM->addPass(this, AU);
256 void FunctionPass::addToPassManager(PassManagerT<Function> *PM,
258 PM->addPass(this, AU);
261 //===----------------------------------------------------------------------===//
262 // BasicBlockPass Implementation
265 // To run this pass on a function, we simply call runOnBasicBlock once for each
268 bool BasicBlockPass::runOnFunction(Function &F) {
269 bool Changed = doInitialization(F);
270 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
271 Changed |= runOnBasicBlock(*I);
272 return Changed | doFinalization(F);
275 // To run directly on the basic block, we initialize, runOnBasicBlock, then
278 bool BasicBlockPass::run(BasicBlock &BB) {
279 Function &F = *BB.getParent();
280 Module &M = *F.getParent();
281 return doInitialization(M) | doInitialization(F) | runOnBasicBlock(BB) |
282 doFinalization(F) | doFinalization(M);
285 void BasicBlockPass::addToPassManager(PassManagerT<Function> *PM,
287 PM->addPass(this, AU);
290 void BasicBlockPass::addToPassManager(PassManagerT<BasicBlock> *PM,
292 PM->addPass(this, AU);
296 //===----------------------------------------------------------------------===//
297 // Pass Registration mechanism
299 static std::map<TypeInfo, PassInfo*> *PassInfoMap = 0;
300 static std::vector<PassRegistrationListener*> *Listeners = 0;
302 // getPassInfo - Return the PassInfo data structure that corresponds to this
304 const PassInfo *Pass::getPassInfo() const {
305 if (PassInfoCache) return PassInfoCache;
306 return lookupPassInfo(typeid(*this));
309 const PassInfo *Pass::lookupPassInfo(const std::type_info &TI) {
310 if (PassInfoMap == 0) return 0;
311 std::map<TypeInfo, PassInfo*>::iterator I = PassInfoMap->find(TI);
312 return (I != PassInfoMap->end()) ? I->second : 0;
315 void RegisterPassBase::registerPass(PassInfo *PI) {
316 if (PassInfoMap == 0)
317 PassInfoMap = new std::map<TypeInfo, PassInfo*>();
319 assert(PassInfoMap->find(PI->getTypeInfo()) == PassInfoMap->end() &&
320 "Pass already registered!");
322 PassInfoMap->insert(std::make_pair(TypeInfo(PI->getTypeInfo()), PI));
324 // Notify any listeners...
326 for (std::vector<PassRegistrationListener*>::iterator
327 I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
328 (*I)->passRegistered(PI);
331 void RegisterPassBase::unregisterPass(PassInfo *PI) {
332 assert(PassInfoMap && "Pass registered but not in map!");
333 std::map<TypeInfo, PassInfo*>::iterator I =
334 PassInfoMap->find(PI->getTypeInfo());
335 assert(I != PassInfoMap->end() && "Pass registered but not in map!");
337 // Remove pass from the map...
338 PassInfoMap->erase(I);
339 if (PassInfoMap->empty()) {
344 // Notify any listeners...
346 for (std::vector<PassRegistrationListener*>::iterator
347 I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
348 (*I)->passUnregistered(PI);
350 // Delete the PassInfo object itself...
354 //===----------------------------------------------------------------------===//
355 // Analysis Group Implementation Code
356 //===----------------------------------------------------------------------===//
358 struct AnalysisGroupInfo {
359 const PassInfo *DefaultImpl;
360 std::set<const PassInfo *> Implementations;
361 AnalysisGroupInfo() : DefaultImpl(0) {}
364 static std::map<const PassInfo *, AnalysisGroupInfo> *AnalysisGroupInfoMap = 0;
366 // RegisterAGBase implementation
368 RegisterAGBase::RegisterAGBase(const std::type_info &Interface,
369 const std::type_info *Pass, bool isDefault)
370 : ImplementationInfo(0), isDefaultImplementation(isDefault) {
372 InterfaceInfo = const_cast<PassInfo*>(Pass::lookupPassInfo(Interface));
373 if (InterfaceInfo == 0) { // First reference to Interface, add it now.
374 InterfaceInfo = // Create the new PassInfo for the interface...
375 new PassInfo("", "", Interface, PassInfo::AnalysisGroup, 0, 0);
376 registerPass(InterfaceInfo);
379 assert(InterfaceInfo->getPassType() == PassInfo::AnalysisGroup &&
380 "Trying to join an analysis group that is a normal pass!");
383 ImplementationInfo = Pass::lookupPassInfo(*Pass);
384 assert(ImplementationInfo &&
385 "Must register pass before adding to AnalysisGroup!");
387 // Make sure we keep track of the fact that the implementation implements
389 PassInfo *IIPI = const_cast<PassInfo*>(ImplementationInfo);
390 IIPI->addInterfaceImplemented(InterfaceInfo);
392 // Lazily allocate to avoid nasty initialization order dependencies
393 if (AnalysisGroupInfoMap == 0)
394 AnalysisGroupInfoMap = new std::map<const PassInfo *,AnalysisGroupInfo>();
396 AnalysisGroupInfo &AGI = (*AnalysisGroupInfoMap)[InterfaceInfo];
397 assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
398 "Cannot add a pass to the same analysis group more than once!");
399 AGI.Implementations.insert(ImplementationInfo);
401 assert(AGI.DefaultImpl == 0 && InterfaceInfo->getNormalCtor() == 0 &&
402 "Default implementation for analysis group already specified!");
403 assert(ImplementationInfo->getNormalCtor() &&
404 "Cannot specify pass as default if it does not have a default ctor");
405 AGI.DefaultImpl = ImplementationInfo;
406 InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
411 void RegisterAGBase::setGroupName(const char *Name) {
412 assert(InterfaceInfo->getPassName()[0] == 0 && "Interface Name already set!");
413 InterfaceInfo->setPassName(Name);
416 RegisterAGBase::~RegisterAGBase() {
417 if (ImplementationInfo) {
418 assert(AnalysisGroupInfoMap && "Inserted into map, but map doesn't exist?");
419 AnalysisGroupInfo &AGI = (*AnalysisGroupInfoMap)[InterfaceInfo];
421 assert(AGI.Implementations.count(ImplementationInfo) &&
422 "Pass not a member of analysis group?");
424 if (AGI.DefaultImpl == ImplementationInfo)
427 AGI.Implementations.erase(ImplementationInfo);
429 // Last member of this analysis group? Unregister PassInfo, delete map entry
430 if (AGI.Implementations.empty()) {
431 assert(AGI.DefaultImpl == 0 &&
432 "Default implementation didn't unregister?");
433 AnalysisGroupInfoMap->erase(InterfaceInfo);
434 if (AnalysisGroupInfoMap->empty()) { // Delete map if empty
435 delete AnalysisGroupInfoMap;
436 AnalysisGroupInfoMap = 0;
439 unregisterPass(InterfaceInfo);
445 //===----------------------------------------------------------------------===//
446 // PassRegistrationListener implementation
449 // PassRegistrationListener ctor - Add the current object to the list of
450 // PassRegistrationListeners...
451 PassRegistrationListener::PassRegistrationListener() {
452 if (!Listeners) Listeners = new std::vector<PassRegistrationListener*>();
453 Listeners->push_back(this);
456 // dtor - Remove object from list of listeners...
457 PassRegistrationListener::~PassRegistrationListener() {
458 std::vector<PassRegistrationListener*>::iterator I =
459 std::find(Listeners->begin(), Listeners->end(), this);
460 assert(Listeners && I != Listeners->end() &&
461 "PassRegistrationListener not registered!");
464 if (Listeners->empty()) {
470 // enumeratePasses - Iterate over the registered passes, calling the
471 // passEnumerate callback on each PassInfo object.
473 void PassRegistrationListener::enumeratePasses() {
475 for (std::map<TypeInfo, PassInfo*>::iterator I = PassInfoMap->begin(),
476 E = PassInfoMap->end(); I != E; ++I)
477 passEnumerate(I->second);