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"
14 #include "Config/sys/resource.h"
15 #include "Config/sys/time.h"
16 #include "Config/unistd.h"
19 // IncludeFile - Stub function used to help linking out.
20 IncludeFile::IncludeFile(void*) {}
22 //===----------------------------------------------------------------------===//
23 // AnalysisID Class Implementation
26 static std::vector<const PassInfo*> CFGOnlyAnalyses;
28 void RegisterPassBase::setPreservesCFG() {
29 CFGOnlyAnalyses.push_back(PIObj);
32 //===----------------------------------------------------------------------===//
33 // AnalysisResolver Class Implementation
36 void AnalysisResolver::setAnalysisResolver(Pass *P, AnalysisResolver *AR) {
37 assert(P->Resolver == 0 && "Pass already in a PassManager!");
41 //===----------------------------------------------------------------------===//
42 // AnalysisUsage Class Implementation
45 // setPreservesCFG - This function should be called to by the pass, iff they do
48 // 1. Add or remove basic blocks from the function
49 // 2. Modify terminator instructions in any way.
51 // This function annotates the AnalysisUsage info object to say that analyses
52 // that only depend on the CFG are preserved by this pass.
54 void AnalysisUsage::setPreservesCFG() {
55 // Since this transformation doesn't modify the CFG, it preserves all analyses
56 // that only depend on the CFG (like dominators, loop info, etc...)
58 Preserved.insert(Preserved.end(),
59 CFGOnlyAnalyses.begin(), CFGOnlyAnalyses.end());
63 //===----------------------------------------------------------------------===//
64 // PassManager implementation - The PassManager class is a simple Pimpl class
65 // that wraps the PassManagerT template.
67 PassManager::PassManager() : PM(new PassManagerT<Module>()) {}
68 PassManager::~PassManager() { delete PM; }
69 void PassManager::add(Pass *P) { PM->add(P); }
70 bool PassManager::run(Module &M) { return PM->run(M); }
72 //===----------------------------------------------------------------------===//
73 // FunctionPassManager implementation - The FunctionPassManager class
74 // is a simple Pimpl class that wraps the PassManagerT template. It
75 // is like PassManager, but only deals in FunctionPasses.
77 FunctionPassManager::FunctionPassManager() : PM(new PassManagerT<Function>()) {}
78 FunctionPassManager::~FunctionPassManager() { delete PM; }
79 void FunctionPassManager::add(FunctionPass *P) { PM->add(P); }
80 void FunctionPassManager::add(ImmutablePass *IP) { PM->add(IP); }
81 bool FunctionPassManager::run(Function &F) { return PM->run(F); }
84 //===----------------------------------------------------------------------===//
85 // TimingInfo Class - This class is used to calculate information about the
86 // amount of time each pass takes to execute. This only happens with
87 // -time-passes is enabled on the command line.
90 EnableTiming("time-passes",
91 cl::desc("Time each pass, printing elapsed time for each on exit"));
93 // 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.
96 void TimingInfo::createTheTimeInfo() {
97 if (!EnableTiming || TheTimeInfo) return;
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;
106 void 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));
112 } else { // Normal pass. Print argument information...
113 // Print out arguments for registered passes that are _optimizations_
114 if (const PassInfo *PI = P->getPassInfo())
115 if (PI->getPassType() & PassInfo::Optimization)
116 std::cerr << " -" << PI->getPassArgument();
120 void PMDebug::PrintPassInformation(unsigned Depth, const char *Action,
121 Pass *P, Annotable *V) {
122 if (PassDebugging >= Executions) {
123 std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '"
126 std::cerr << "' on ";
128 if (dynamic_cast<Module*>(V)) {
129 std::cerr << "Module\n"; return;
130 } else if (Function *F = dynamic_cast<Function*>(V))
131 std::cerr << "Function '" << F->getName();
132 else if (BasicBlock *BB = dynamic_cast<BasicBlock*>(V))
133 std::cerr << "BasicBlock '" << BB->getName();
134 else if (Value *Val = dynamic_cast<Value*>(V))
135 std::cerr << typeid(*Val).name() << " '" << Val->getName();
137 std::cerr << "'...\n";
141 void PMDebug::PrintAnalysisSetInfo(unsigned Depth, const char *Msg,
142 Pass *P, const std::vector<AnalysisID> &Set){
143 if (PassDebugging >= Details && !Set.empty()) {
144 std::cerr << (void*)P << std::string(Depth*2+3, ' ') << Msg << " Analyses:";
145 for (unsigned i = 0; i != Set.size(); ++i) {
146 if (i) std::cerr << ",";
147 std::cerr << " " << Set[i]->getPassName();
153 //===----------------------------------------------------------------------===//
154 // Pass Implementation
157 void Pass::addToPassManager(PassManagerT<Module> *PM, AnalysisUsage &AU) {
158 PM->addPass(this, AU);
161 bool Pass::mustPreserveAnalysisID(const PassInfo *AnalysisID) const {
162 return Resolver->getAnalysisToUpdate(AnalysisID) != 0;
165 // dumpPassStructure - Implement the -debug-passes=Structure option
166 void Pass::dumpPassStructure(unsigned Offset) {
167 std::cerr << std::string(Offset*2, ' ') << getPassName() << "\n";
170 // getPassName - Use C++ RTTI to get a SOMEWHAT intelligable name for the pass.
172 const char *Pass::getPassName() const {
173 if (const PassInfo *PI = getPassInfo())
174 return PI->getPassName();
175 return typeid(*this).name();
178 // print - Print out the internal state of the pass. This is called by Analyse
179 // to print out the contents of an analysis. Otherwise it is not necessary to
180 // implement this method.
182 void Pass::print(std::ostream &O) const {
183 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
186 // dump - call print(std::cerr);
187 void Pass::dump() const {
191 //===----------------------------------------------------------------------===//
192 // ImmutablePass Implementation
194 void ImmutablePass::addToPassManager(PassManagerT<Module> *PM,
196 PM->addPass(this, AU);
200 //===----------------------------------------------------------------------===//
201 // FunctionPass Implementation
204 // run - On a module, we run this pass by initializing, runOnFunction'ing once
205 // for every function in the module, then by finalizing.
207 bool FunctionPass::run(Module &M) {
208 bool Changed = doInitialization(M);
210 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
211 if (!I->isExternal()) // Passes are not run on external functions!
212 Changed |= runOnFunction(*I);
214 return Changed | doFinalization(M);
217 // run - On a function, we simply initialize, run the function, then finalize.
219 bool FunctionPass::run(Function &F) {
220 if (F.isExternal()) return false;// Passes are not run on external functions!
222 return doInitialization(*F.getParent()) | runOnFunction(F)
223 | doFinalization(*F.getParent());
226 void FunctionPass::addToPassManager(PassManagerT<Module> *PM,
228 PM->addPass(this, AU);
231 void FunctionPass::addToPassManager(PassManagerT<Function> *PM,
233 PM->addPass(this, AU);
236 //===----------------------------------------------------------------------===//
237 // BasicBlockPass Implementation
240 // To run this pass on a function, we simply call runOnBasicBlock once for each
243 bool BasicBlockPass::runOnFunction(Function &F) {
244 bool Changed = doInitialization(F);
245 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
246 Changed |= runOnBasicBlock(*I);
247 return Changed | doFinalization(F);
250 // To run directly on the basic block, we initialize, runOnBasicBlock, then
253 bool BasicBlockPass::run(BasicBlock &BB) {
254 Function &F = *BB.getParent();
255 Module &M = *F.getParent();
256 return doInitialization(M) | doInitialization(F) | runOnBasicBlock(BB) |
257 doFinalization(F) | doFinalization(M);
260 void BasicBlockPass::addToPassManager(PassManagerT<Function> *PM,
262 PM->addPass(this, AU);
265 void BasicBlockPass::addToPassManager(PassManagerT<BasicBlock> *PM,
267 PM->addPass(this, AU);
271 //===----------------------------------------------------------------------===//
272 // Pass Registration mechanism
274 static std::map<TypeInfo, PassInfo*> *PassInfoMap = 0;
275 static std::vector<PassRegistrationListener*> *Listeners = 0;
277 // getPassInfo - Return the PassInfo data structure that corresponds to this
279 const PassInfo *Pass::getPassInfo() const {
280 if (PassInfoCache) return PassInfoCache;
281 return lookupPassInfo(typeid(*this));
284 const PassInfo *Pass::lookupPassInfo(const std::type_info &TI) {
285 if (PassInfoMap == 0) return 0;
286 std::map<TypeInfo, PassInfo*>::iterator I = PassInfoMap->find(TI);
287 return (I != PassInfoMap->end()) ? I->second : 0;
290 void RegisterPassBase::registerPass(PassInfo *PI) {
291 if (PassInfoMap == 0)
292 PassInfoMap = new std::map<TypeInfo, PassInfo*>();
294 assert(PassInfoMap->find(PI->getTypeInfo()) == PassInfoMap->end() &&
295 "Pass already registered!");
297 PassInfoMap->insert(std::make_pair(TypeInfo(PI->getTypeInfo()), PI));
299 // Notify any listeners...
301 for (std::vector<PassRegistrationListener*>::iterator
302 I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
303 (*I)->passRegistered(PI);
306 void RegisterPassBase::unregisterPass(PassInfo *PI) {
307 assert(PassInfoMap && "Pass registered but not in map!");
308 std::map<TypeInfo, PassInfo*>::iterator I =
309 PassInfoMap->find(PI->getTypeInfo());
310 assert(I != PassInfoMap->end() && "Pass registered but not in map!");
312 // Remove pass from the map...
313 PassInfoMap->erase(I);
314 if (PassInfoMap->empty()) {
319 // Notify any listeners...
321 for (std::vector<PassRegistrationListener*>::iterator
322 I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
323 (*I)->passUnregistered(PI);
325 // Delete the PassInfo object itself...
329 //===----------------------------------------------------------------------===//
330 // Analysis Group Implementation Code
331 //===----------------------------------------------------------------------===//
333 struct AnalysisGroupInfo {
334 const PassInfo *DefaultImpl;
335 std::set<const PassInfo *> Implementations;
336 AnalysisGroupInfo() : DefaultImpl(0) {}
339 static std::map<const PassInfo *, AnalysisGroupInfo> *AnalysisGroupInfoMap = 0;
341 // RegisterAGBase implementation
343 RegisterAGBase::RegisterAGBase(const std::type_info &Interface,
344 const std::type_info *Pass, bool isDefault)
345 : ImplementationInfo(0), isDefaultImplementation(isDefault) {
347 InterfaceInfo = const_cast<PassInfo*>(Pass::lookupPassInfo(Interface));
348 if (InterfaceInfo == 0) { // First reference to Interface, add it now.
349 InterfaceInfo = // Create the new PassInfo for the interface...
350 new PassInfo("", "", Interface, PassInfo::AnalysisGroup, 0, 0);
351 registerPass(InterfaceInfo);
354 assert(InterfaceInfo->getPassType() == PassInfo::AnalysisGroup &&
355 "Trying to join an analysis group that is a normal pass!");
358 ImplementationInfo = Pass::lookupPassInfo(*Pass);
359 assert(ImplementationInfo &&
360 "Must register pass before adding to AnalysisGroup!");
362 // Make sure we keep track of the fact that the implementation implements
364 PassInfo *IIPI = const_cast<PassInfo*>(ImplementationInfo);
365 IIPI->addInterfaceImplemented(InterfaceInfo);
367 // Lazily allocate to avoid nasty initialization order dependencies
368 if (AnalysisGroupInfoMap == 0)
369 AnalysisGroupInfoMap = new std::map<const PassInfo *,AnalysisGroupInfo>();
371 AnalysisGroupInfo &AGI = (*AnalysisGroupInfoMap)[InterfaceInfo];
372 assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
373 "Cannot add a pass to the same analysis group more than once!");
374 AGI.Implementations.insert(ImplementationInfo);
376 assert(AGI.DefaultImpl == 0 && InterfaceInfo->getNormalCtor() == 0 &&
377 "Default implementation for analysis group already specified!");
378 assert(ImplementationInfo->getNormalCtor() &&
379 "Cannot specify pass as default if it does not have a default ctor");
380 AGI.DefaultImpl = ImplementationInfo;
381 InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
386 void RegisterAGBase::setGroupName(const char *Name) {
387 assert(InterfaceInfo->getPassName()[0] == 0 && "Interface Name already set!");
388 InterfaceInfo->setPassName(Name);
391 RegisterAGBase::~RegisterAGBase() {
392 if (ImplementationInfo) {
393 assert(AnalysisGroupInfoMap && "Inserted into map, but map doesn't exist?");
394 AnalysisGroupInfo &AGI = (*AnalysisGroupInfoMap)[InterfaceInfo];
396 assert(AGI.Implementations.count(ImplementationInfo) &&
397 "Pass not a member of analysis group?");
399 if (AGI.DefaultImpl == ImplementationInfo)
402 AGI.Implementations.erase(ImplementationInfo);
404 // Last member of this analysis group? Unregister PassInfo, delete map entry
405 if (AGI.Implementations.empty()) {
406 assert(AGI.DefaultImpl == 0 &&
407 "Default implementation didn't unregister?");
408 AnalysisGroupInfoMap->erase(InterfaceInfo);
409 if (AnalysisGroupInfoMap->empty()) { // Delete map if empty
410 delete AnalysisGroupInfoMap;
411 AnalysisGroupInfoMap = 0;
414 unregisterPass(InterfaceInfo);
420 //===----------------------------------------------------------------------===//
421 // PassRegistrationListener implementation
424 // PassRegistrationListener ctor - Add the current object to the list of
425 // PassRegistrationListeners...
426 PassRegistrationListener::PassRegistrationListener() {
427 if (!Listeners) Listeners = new std::vector<PassRegistrationListener*>();
428 Listeners->push_back(this);
431 // dtor - Remove object from list of listeners...
432 PassRegistrationListener::~PassRegistrationListener() {
433 std::vector<PassRegistrationListener*>::iterator I =
434 std::find(Listeners->begin(), Listeners->end(), this);
435 assert(Listeners && I != Listeners->end() &&
436 "PassRegistrationListener not registered!");
439 if (Listeners->empty()) {
445 // enumeratePasses - Iterate over the registered passes, calling the
446 // passEnumerate callback on each PassInfo object.
448 void PassRegistrationListener::enumeratePasses() {
450 for (std::map<TypeInfo, PassInfo*>::iterator I = PassInfoMap->begin(),
451 E = PassInfoMap->end(); I != E; ++I)
452 passEnumerate(I->second);