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 "llvm/Module.h"
18 #include "llvm/ModuleProvider.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/Support/ManagedStatic.h"
21 #include "llvm/Support/TypeInfo.h"
26 //===----------------------------------------------------------------------===//
27 // Pass Implementation
30 // Force out-of-line virtual method.
31 ModulePass::~ModulePass() { }
33 bool Pass::mustPreserveAnalysisID(const PassInfo *AnalysisID) const {
34 return Resolver->getAnalysisToUpdate(AnalysisID, true) != 0;
37 // dumpPassStructure - Implement the -debug-passes=Structure option
38 void Pass::dumpPassStructure(unsigned Offset) {
39 cerr << std::string(Offset*2, ' ') << getPassName() << "\n";
42 // getPassName - Use C++ RTTI to get a SOMEWHAT intelligible name for the pass.
44 const char *Pass::getPassName() const {
45 if (const PassInfo *PI = getPassInfo())
46 return PI->getPassName();
47 return typeid(*this).name();
50 // print - Print out the internal state of the pass. This is called by Analyze
51 // to print out the contents of an analysis. Otherwise it is not necessary to
52 // implement this method.
54 void Pass::print(std::ostream &O,const Module*) const {
55 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
58 // dump - call print(cerr);
59 void Pass::dump() const {
60 print(*cerr.stream(), 0);
63 //===----------------------------------------------------------------------===//
64 // ImmutablePass Implementation
66 // Force out-of-line virtual method.
67 ImmutablePass::~ImmutablePass() { }
69 //===----------------------------------------------------------------------===//
70 // FunctionPass Implementation
73 // run - On a module, we run this pass by initializing, runOnFunction'ing once
74 // for every function in the module, then by finalizing.
76 bool FunctionPass::runOnModule(Module &M) {
77 bool Changed = doInitialization(M);
79 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
80 if (!I->isDeclaration()) // Passes are not run on external functions!
81 Changed |= runOnFunction(*I);
83 return Changed | doFinalization(M);
86 // run - On a function, we simply initialize, run the function, then finalize.
88 bool FunctionPass::run(Function &F) {
89 if (F.isDeclaration()) return false;// Passes are not run on external functions!
91 bool Changed = doInitialization(*F.getParent());
92 Changed |= runOnFunction(F);
93 return Changed | doFinalization(*F.getParent());
96 //===----------------------------------------------------------------------===//
97 // BasicBlockPass Implementation
100 // To run this pass on a function, we simply call runOnBasicBlock once for each
103 bool BasicBlockPass::runOnFunction(Function &F) {
104 bool Changed = doInitialization(F);
105 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
106 Changed |= runOnBasicBlock(*I);
107 return Changed | doFinalization(F);
110 // To run directly on the basic block, we initialize, runOnBasicBlock, then
113 bool BasicBlockPass::runPass(BasicBlock &BB) {
114 Function &F = *BB.getParent();
115 Module &M = *F.getParent();
116 bool Changed = doInitialization(M);
117 Changed |= doInitialization(F);
118 Changed |= runOnBasicBlock(BB);
119 Changed |= doFinalization(F);
120 Changed |= doFinalization(M);
124 //===----------------------------------------------------------------------===//
125 // Pass Registration mechanism
128 class PassRegistrar {
129 /// PassInfoMap - Keep track of the passinfo object for each registered llvm
131 std::map<TypeInfo, PassInfo*> PassInfoMap;
133 /// AnalysisGroupInfo - Keep track of information for each analysis group.
134 struct AnalysisGroupInfo {
135 const PassInfo *DefaultImpl;
136 std::set<const PassInfo *> Implementations;
137 AnalysisGroupInfo() : DefaultImpl(0) {}
140 /// AnalysisGroupInfoMap - Information for each analysis group.
141 std::map<const PassInfo *, AnalysisGroupInfo> AnalysisGroupInfoMap;
145 const PassInfo *GetPassInfo(const std::type_info &TI) const {
146 std::map<TypeInfo, PassInfo*>::const_iterator I = PassInfoMap.find(TI);
147 return I != PassInfoMap.end() ? I->second : 0;
150 void RegisterPass(PassInfo &PI) {
152 PassInfoMap.insert(std::make_pair(TypeInfo(PI.getTypeInfo()),&PI)).second;
153 assert(Inserted && "Pass registered multiple times!");
156 void UnregisterPass(PassInfo &PI) {
157 std::map<TypeInfo, PassInfo*>::iterator I =
158 PassInfoMap.find(PI.getTypeInfo());
159 assert(I != PassInfoMap.end() && "Pass registered but not in map!");
161 // Remove pass from the map.
162 PassInfoMap.erase(I);
165 void EnumerateWith(PassRegistrationListener *L) {
166 for (std::map<TypeInfo, PassInfo*>::const_iterator I = PassInfoMap.begin(),
167 E = PassInfoMap.end(); I != E; ++I)
168 L->passEnumerate(I->second);
172 /// Analysis Group Mechanisms.
173 void RegisterAnalysisGroup(PassInfo *InterfaceInfo,
174 const PassInfo *ImplementationInfo,
176 AnalysisGroupInfo &AGI = AnalysisGroupInfoMap[InterfaceInfo];
177 assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
178 "Cannot add a pass to the same analysis group more than once!");
179 AGI.Implementations.insert(ImplementationInfo);
181 assert(AGI.DefaultImpl == 0 && InterfaceInfo->getNormalCtor() == 0 &&
182 "Default implementation for analysis group already specified!");
183 assert(ImplementationInfo->getNormalCtor() &&
184 "Cannot specify pass as default if it does not have a default ctor");
185 AGI.DefaultImpl = ImplementationInfo;
186 InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
192 static ManagedStatic<PassRegistrar> PassRegistrarObj;
193 static std::vector<PassRegistrationListener*> *Listeners = 0;
195 // getPassInfo - Return the PassInfo data structure that corresponds to this
197 const PassInfo *Pass::getPassInfo() const {
198 if (PassInfoCache) return PassInfoCache;
199 return lookupPassInfo(typeid(*this));
202 const PassInfo *Pass::lookupPassInfo(const std::type_info &TI) {
203 return PassRegistrarObj->GetPassInfo(TI);
206 void RegisterPassBase::registerPass() {
207 PassRegistrarObj->RegisterPass(PIObj);
209 // Notify any listeners.
211 for (std::vector<PassRegistrationListener*>::iterator
212 I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
213 (*I)->passRegistered(&PIObj);
216 void RegisterPassBase::unregisterPass() {
217 PassRegistrarObj->UnregisterPass(PIObj);
220 //===----------------------------------------------------------------------===//
221 // Analysis Group Implementation Code
222 //===----------------------------------------------------------------------===//
224 // RegisterAGBase implementation
226 RegisterAGBase::RegisterAGBase(const std::type_info &Interface,
227 const std::type_info *Pass, bool isDefault)
228 : RegisterPassBase(Interface),
229 ImplementationInfo(0), isDefaultImplementation(isDefault) {
231 InterfaceInfo = const_cast<PassInfo*>(Pass::lookupPassInfo(Interface));
232 if (InterfaceInfo == 0) {
233 // First reference to Interface, register it now.
235 InterfaceInfo = &PIObj;
237 assert(PIObj.isAnalysisGroup() &&
238 "Trying to join an analysis group that is a normal pass!");
241 ImplementationInfo = Pass::lookupPassInfo(*Pass);
242 assert(ImplementationInfo &&
243 "Must register pass before adding to AnalysisGroup!");
245 // Make sure we keep track of the fact that the implementation implements
247 PassInfo *IIPI = const_cast<PassInfo*>(ImplementationInfo);
248 IIPI->addInterfaceImplemented(InterfaceInfo);
250 PassRegistrarObj->RegisterAnalysisGroup(InterfaceInfo, IIPI, isDefault);
254 void RegisterAGBase::setGroupName(const char *Name) {
255 assert(InterfaceInfo->getPassName()[0] == 0 && "Interface Name already set!");
256 InterfaceInfo->setPassName(Name);
260 //===----------------------------------------------------------------------===//
261 // PassRegistrationListener implementation
264 // PassRegistrationListener ctor - Add the current object to the list of
265 // PassRegistrationListeners...
266 PassRegistrationListener::PassRegistrationListener() {
267 if (!Listeners) Listeners = new std::vector<PassRegistrationListener*>();
268 Listeners->push_back(this);
271 // dtor - Remove object from list of listeners...
272 PassRegistrationListener::~PassRegistrationListener() {
273 std::vector<PassRegistrationListener*>::iterator I =
274 std::find(Listeners->begin(), Listeners->end(), this);
275 assert(Listeners && I != Listeners->end() &&
276 "PassRegistrationListener not registered!");
279 if (Listeners->empty()) {
285 // enumeratePasses - Iterate over the registered passes, calling the
286 // passEnumerate callback on each PassInfo object.
288 void PassRegistrationListener::enumeratePasses() {
289 PassRegistrarObj->EnumerateWith(this);
292 //===----------------------------------------------------------------------===//
293 // AnalysisUsage Class Implementation
297 struct GetCFGOnlyPasses : public PassRegistrationListener {
298 std::vector<AnalysisID> &CFGOnlyList;
299 GetCFGOnlyPasses(std::vector<AnalysisID> &L) : CFGOnlyList(L) {}
301 void passEnumerate(const PassInfo *P) {
302 if (P->isCFGOnlyPass())
303 CFGOnlyList.push_back(P);
308 // setPreservesCFG - This function should be called to by the pass, iff they do
311 // 1. Add or remove basic blocks from the function
312 // 2. Modify terminator instructions in any way.
314 // This function annotates the AnalysisUsage info object to say that analyses
315 // that only depend on the CFG are preserved by this pass.
317 void AnalysisUsage::setPreservesCFG() {
318 // Since this transformation doesn't modify the CFG, it preserves all analyses
319 // that only depend on the CFG (like dominators, loop info, etc...)
320 GetCFGOnlyPasses(Preserved).enumeratePasses();