1 //===- Pass.cpp - LLVM Pass Infrastructure Impementation ------------------===//
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"
15 #include <sys/resource.h>
17 #include <sys/unistd.h>
20 // IncludeFile - Stub function used to help linking out.
21 IncludeFile::IncludeFile(void*) {}
23 //===----------------------------------------------------------------------===//
24 // AnalysisID Class Implementation
27 static std::vector<const PassInfo*> CFGOnlyAnalyses;
29 void RegisterPassBase::setPreservesCFG() {
30 CFGOnlyAnalyses.push_back(PIObj);
33 //===----------------------------------------------------------------------===//
34 // AnalysisResolver Class Implementation
37 void AnalysisResolver::setAnalysisResolver(Pass *P, AnalysisResolver *AR) {
38 assert(P->Resolver == 0 && "Pass already in a PassManager!");
42 //===----------------------------------------------------------------------===//
43 // AnalysisUsage Class Implementation
46 // setPreservesCFG - This function should be called to by the pass, iff they do
49 // 1. Add or remove basic blocks from the function
50 // 2. Modify terminator instructions in any way.
52 // This function annotates the AnalysisUsage info object to say that analyses
53 // that only depend on the CFG are preserved by this pass.
55 void AnalysisUsage::setPreservesCFG() {
56 // Since this transformation doesn't modify the CFG, it preserves all analyses
57 // that only depend on the CFG (like dominators, loop info, etc...)
59 Preserved.insert(Preserved.end(),
60 CFGOnlyAnalyses.begin(), CFGOnlyAnalyses.end());
64 //===----------------------------------------------------------------------===//
65 // PassManager implementation - The PassManager class is a simple Pimpl class
66 // that wraps the PassManagerT template.
68 PassManager::PassManager() : PM(new PassManagerT<Module>()) {}
69 PassManager::~PassManager() { delete PM; }
70 void PassManager::add(Pass *P) { PM->add(P); }
71 bool PassManager::run(Module &M) { return PM->run(M); }
74 //===----------------------------------------------------------------------===//
75 // TimingInfo Class - This class is used to calculate information about the
76 // amount of time each pass takes to execute. This only happens with
77 // -time-passes is enabled on the command line.
80 EnableTiming("time-passes",
81 cl::desc("Time each pass, printing elapsed time for each on exit"));
83 // Create method. If Timing is enabled, this creates and returns a new timing
84 // object, otherwise it returns null.
86 TimingInfo *TimingInfo::create() {
87 return EnableTiming ? new TimingInfo() : 0;
90 void PMDebug::PrintArgumentInformation(const Pass *P) {
91 // Print out passes in pass manager...
92 if (const AnalysisResolver *PM = dynamic_cast<const AnalysisResolver*>(P)) {
93 for (unsigned i = 0, e = PM->getNumContainedPasses(); i != e; ++i)
94 PrintArgumentInformation(PM->getContainedPass(i));
96 } else { // Normal pass. Print argument information...
97 // Print out arguments for registered passes that are _optimizations_
98 if (const PassInfo *PI = P->getPassInfo())
99 if (PI->getPassType() & PassInfo::Optimization)
100 std::cerr << " -" << PI->getPassArgument();
104 void PMDebug::PrintPassInformation(unsigned Depth, const char *Action,
105 Pass *P, Annotable *V) {
106 if (PassDebugging >= Executions) {
107 std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '"
110 std::cerr << "' on ";
112 if (dynamic_cast<Module*>(V)) {
113 std::cerr << "Module\n"; return;
114 } else if (Function *F = dynamic_cast<Function*>(V))
115 std::cerr << "Function '" << F->getName();
116 else if (BasicBlock *BB = dynamic_cast<BasicBlock*>(V))
117 std::cerr << "BasicBlock '" << BB->getName();
118 else if (Value *Val = dynamic_cast<Value*>(V))
119 std::cerr << typeid(*Val).name() << " '" << Val->getName();
121 std::cerr << "'...\n";
125 void PMDebug::PrintAnalysisSetInfo(unsigned Depth, const char *Msg,
126 Pass *P, const std::vector<AnalysisID> &Set){
127 if (PassDebugging >= Details && !Set.empty()) {
128 std::cerr << (void*)P << std::string(Depth*2+3, ' ') << Msg << " Analyses:";
129 for (unsigned i = 0; i != Set.size(); ++i) {
130 if (i) std::cerr << ",";
131 std::cerr << " " << Set[i]->getPassName();
137 //===----------------------------------------------------------------------===//
138 // Pass Implementation
141 void Pass::addToPassManager(PassManagerT<Module> *PM, AnalysisUsage &AU) {
142 PM->addPass(this, AU);
145 bool Pass::mustPreserveAnalysisID(const PassInfo *AnalysisID) const {
146 return Resolver->getAnalysisToUpdate(AnalysisID) != 0;
149 // dumpPassStructure - Implement the -debug-passes=Structure option
150 void Pass::dumpPassStructure(unsigned Offset) {
151 std::cerr << std::string(Offset*2, ' ') << getPassName() << "\n";
154 // getPassName - Use C++ RTTI to get a SOMEWHAT intelligable name for the pass.
156 const char *Pass::getPassName() const {
157 if (const PassInfo *PI = getPassInfo())
158 return PI->getPassName();
159 return typeid(*this).name();
162 // print - Print out the internal state of the pass. This is called by Analyse
163 // to print out the contents of an analysis. Otherwise it is not neccesary to
164 // implement this method.
166 void Pass::print(std::ostream &O) const {
167 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
170 // dump - call print(std::cerr);
171 void Pass::dump() const {
175 //===----------------------------------------------------------------------===//
176 // ImmutablePass Implementation
178 void ImmutablePass::addToPassManager(PassManagerT<Module> *PM,
180 PM->addPass(this, AU);
184 //===----------------------------------------------------------------------===//
185 // FunctionPass Implementation
188 // run - On a module, we run this pass by initializing, runOnFunction'ing once
189 // for every function in the module, then by finalizing.
191 bool FunctionPass::run(Module &M) {
192 bool Changed = doInitialization(M);
194 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
195 if (!I->isExternal()) // Passes are not run on external functions!
196 Changed |= runOnFunction(*I);
198 return Changed | doFinalization(M);
201 // run - On a function, we simply initialize, run the function, then finalize.
203 bool FunctionPass::run(Function &F) {
204 if (F.isExternal()) return false;// Passes are not run on external functions!
206 return doInitialization(*F.getParent()) | runOnFunction(F)
207 | doFinalization(*F.getParent());
210 void FunctionPass::addToPassManager(PassManagerT<Module> *PM,
212 PM->addPass(this, AU);
215 void FunctionPass::addToPassManager(PassManagerT<Function> *PM,
217 PM->addPass(this, AU);
220 //===----------------------------------------------------------------------===//
221 // BasicBlockPass Implementation
224 // To run this pass on a function, we simply call runOnBasicBlock once for each
227 bool BasicBlockPass::runOnFunction(Function &F) {
228 bool Changed = doInitialization(F);
229 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
230 Changed |= runOnBasicBlock(*I);
231 return Changed | doFinalization(F);
234 // To run directly on the basic block, we initialize, runOnBasicBlock, then
237 bool BasicBlockPass::run(BasicBlock &BB) {
238 Function &F = *BB.getParent();
239 Module &M = *F.getParent();
240 return doInitialization(M) | doInitialization(F) | runOnBasicBlock(BB) |
241 doFinalization(F) | doFinalization(M);
244 void BasicBlockPass::addToPassManager(PassManagerT<Function> *PM,
246 PM->addPass(this, AU);
249 void BasicBlockPass::addToPassManager(PassManagerT<BasicBlock> *PM,
251 PM->addPass(this, AU);
255 //===----------------------------------------------------------------------===//
256 // Pass Registration mechanism
258 static std::map<TypeInfo, PassInfo*> *PassInfoMap = 0;
259 static std::vector<PassRegistrationListener*> *Listeners = 0;
261 // getPassInfo - Return the PassInfo data structure that corresponds to this
263 const PassInfo *Pass::getPassInfo() const {
264 if (PassInfoCache) return PassInfoCache;
265 return lookupPassInfo(typeid(*this));
268 const PassInfo *Pass::lookupPassInfo(const std::type_info &TI) {
269 if (PassInfoMap == 0) return 0;
270 std::map<TypeInfo, PassInfo*>::iterator I = PassInfoMap->find(TI);
271 return (I != PassInfoMap->end()) ? I->second : 0;
274 void RegisterPassBase::registerPass(PassInfo *PI) {
275 if (PassInfoMap == 0)
276 PassInfoMap = new std::map<TypeInfo, PassInfo*>();
278 assert(PassInfoMap->find(PI->getTypeInfo()) == PassInfoMap->end() &&
279 "Pass already registered!");
281 PassInfoMap->insert(std::make_pair(TypeInfo(PI->getTypeInfo()), PI));
283 // Notify any listeners...
285 for (std::vector<PassRegistrationListener*>::iterator
286 I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
287 (*I)->passRegistered(PI);
290 void RegisterPassBase::unregisterPass(PassInfo *PI) {
291 assert(PassInfoMap && "Pass registered but not in map!");
292 std::map<TypeInfo, PassInfo*>::iterator I =
293 PassInfoMap->find(PI->getTypeInfo());
294 assert(I != PassInfoMap->end() && "Pass registered but not in map!");
296 // Remove pass from the map...
297 PassInfoMap->erase(I);
298 if (PassInfoMap->empty()) {
303 // Notify any listeners...
305 for (std::vector<PassRegistrationListener*>::iterator
306 I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
307 (*I)->passUnregistered(PI);
309 // Delete the PassInfo object itself...
313 //===----------------------------------------------------------------------===//
314 // Analysis Group Implementation Code
315 //===----------------------------------------------------------------------===//
317 struct AnalysisGroupInfo {
318 const PassInfo *DefaultImpl;
319 std::set<const PassInfo *> Implementations;
320 AnalysisGroupInfo() : DefaultImpl(0) {}
323 static std::map<const PassInfo *, AnalysisGroupInfo> *AnalysisGroupInfoMap = 0;
325 // RegisterAGBase implementation
327 RegisterAGBase::RegisterAGBase(const std::type_info &Interface,
328 const std::type_info *Pass, bool isDefault)
329 : ImplementationInfo(0), isDefaultImplementation(isDefault) {
331 InterfaceInfo = const_cast<PassInfo*>(Pass::lookupPassInfo(Interface));
332 if (InterfaceInfo == 0) { // First reference to Interface, add it now.
333 InterfaceInfo = // Create the new PassInfo for the interface...
334 new PassInfo("", "", Interface, PassInfo::AnalysisGroup, 0, 0);
335 registerPass(InterfaceInfo);
338 assert(InterfaceInfo->getPassType() == PassInfo::AnalysisGroup &&
339 "Trying to join an analysis group that is a normal pass!");
342 ImplementationInfo = Pass::lookupPassInfo(*Pass);
343 assert(ImplementationInfo &&
344 "Must register pass before adding to AnalysisGroup!");
346 // Make sure we keep track of the fact that the implementation implements
348 PassInfo *IIPI = const_cast<PassInfo*>(ImplementationInfo);
349 IIPI->addInterfaceImplemented(InterfaceInfo);
351 // Lazily allocate to avoid nasty initialization order dependencies
352 if (AnalysisGroupInfoMap == 0)
353 AnalysisGroupInfoMap = new std::map<const PassInfo *,AnalysisGroupInfo>();
355 AnalysisGroupInfo &AGI = (*AnalysisGroupInfoMap)[InterfaceInfo];
356 assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
357 "Cannot add a pass to the same analysis group more than once!");
358 AGI.Implementations.insert(ImplementationInfo);
360 assert(AGI.DefaultImpl == 0 && InterfaceInfo->getNormalCtor() == 0 &&
361 "Default implementation for analysis group already specified!");
362 assert(ImplementationInfo->getNormalCtor() &&
363 "Cannot specify pass as default if it does not have a default ctor");
364 AGI.DefaultImpl = ImplementationInfo;
365 InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
370 void RegisterAGBase::setGroupName(const char *Name) {
371 assert(InterfaceInfo->getPassName()[0] == 0 && "Interface Name already set!");
372 InterfaceInfo->setPassName(Name);
375 RegisterAGBase::~RegisterAGBase() {
376 if (ImplementationInfo) {
377 assert(AnalysisGroupInfoMap && "Inserted into map, but map doesn't exist?");
378 AnalysisGroupInfo &AGI = (*AnalysisGroupInfoMap)[InterfaceInfo];
380 assert(AGI.Implementations.count(ImplementationInfo) &&
381 "Pass not a member of analysis group?");
383 if (AGI.DefaultImpl == ImplementationInfo)
386 AGI.Implementations.erase(ImplementationInfo);
388 // Last member of this analysis group? Unregister PassInfo, delete map entry
389 if (AGI.Implementations.empty()) {
390 assert(AGI.DefaultImpl == 0 &&
391 "Default implementation didn't unregister?");
392 AnalysisGroupInfoMap->erase(InterfaceInfo);
393 if (AnalysisGroupInfoMap->empty()) { // Delete map if empty
394 delete AnalysisGroupInfoMap;
395 AnalysisGroupInfoMap = 0;
398 unregisterPass(InterfaceInfo);
404 //===----------------------------------------------------------------------===//
405 // PassRegistrationListener implementation
408 // PassRegistrationListener ctor - Add the current object to the list of
409 // PassRegistrationListeners...
410 PassRegistrationListener::PassRegistrationListener() {
411 if (!Listeners) Listeners = new std::vector<PassRegistrationListener*>();
412 Listeners->push_back(this);
415 // dtor - Remove object from list of listeners...
416 PassRegistrationListener::~PassRegistrationListener() {
417 std::vector<PassRegistrationListener*>::iterator I =
418 std::find(Listeners->begin(), Listeners->end(), this);
419 assert(Listeners && I != Listeners->end() &&
420 "PassRegistrationListener not registered!");
423 if (Listeners->empty()) {
429 // enumeratePasses - Iterate over the registered passes, calling the
430 // passEnumerate callback on each PassInfo object.
432 void PassRegistrationListener::enumeratePasses() {
434 for (std::map<TypeInfo, PassInfo*>::iterator I = PassInfoMap->begin(),
435 E = PassInfoMap->end(); I != E; ++I)
436 passEnumerate(I->second);