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"
25 //===----------------------------------------------------------------------===//
26 // AnalysisResolver Class Implementation
29 AnalysisResolver::~AnalysisResolver() {
32 //===----------------------------------------------------------------------===//
33 // Pass Implementation
36 // Force out-of-line virtual method.
37 ModulePass::~ModulePass() { }
39 bool Pass::mustPreserveAnalysisID(const PassInfo *AnalysisID) const {
40 return Resolver_New->getAnalysisToUpdate(AnalysisID, true) != 0;
43 // dumpPassStructure - Implement the -debug-passes=Structure option
44 void Pass::dumpPassStructure(unsigned Offset) {
45 cerr << std::string(Offset*2, ' ') << getPassName() << "\n";
48 // getPassName - Use C++ RTTI to get a SOMEWHAT intelligible name for the pass.
50 const char *Pass::getPassName() const {
51 if (const PassInfo *PI = getPassInfo())
52 return PI->getPassName();
53 return typeid(*this).name();
56 // print - Print out the internal state of the pass. This is called by Analyze
57 // to print out the contents of an analysis. Otherwise it is not necessary to
58 // implement this method.
60 void Pass::print(std::ostream &O,const Module*) const {
61 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
64 // dump - call print(cerr);
65 void Pass::dump() const {
66 print(*cerr.stream(), 0);
69 //===----------------------------------------------------------------------===//
70 // ImmutablePass Implementation
72 // Force out-of-line virtual method.
73 ImmutablePass::~ImmutablePass() { }
75 //===----------------------------------------------------------------------===//
76 // FunctionPass Implementation
79 // run - On a module, we run this pass by initializing, runOnFunction'ing once
80 // for every function in the module, then by finalizing.
82 bool FunctionPass::runOnModule(Module &M) {
83 bool Changed = doInitialization(M);
85 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
86 if (!I->isExternal()) // Passes are not run on external functions!
87 Changed |= runOnFunction(*I);
89 return Changed | doFinalization(M);
92 // run - On a function, we simply initialize, run the function, then finalize.
94 bool FunctionPass::run(Function &F) {
95 if (F.isExternal()) return false;// Passes are not run on external functions!
97 bool Changed = doInitialization(*F.getParent());
98 Changed |= runOnFunction(F);
99 return Changed | doFinalization(*F.getParent());
102 //===----------------------------------------------------------------------===//
103 // BasicBlockPass Implementation
106 // To run this pass on a function, we simply call runOnBasicBlock once for each
109 bool BasicBlockPass::runOnFunction(Function &F) {
110 bool Changed = doInitialization(F);
111 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
112 Changed |= runOnBasicBlock(*I);
113 return Changed | doFinalization(F);
116 // To run directly on the basic block, we initialize, runOnBasicBlock, then
119 bool BasicBlockPass::runPass(BasicBlock &BB) {
120 Function &F = *BB.getParent();
121 Module &M = *F.getParent();
122 bool Changed = doInitialization(M);
123 Changed |= doInitialization(F);
124 Changed |= runOnBasicBlock(BB);
125 Changed |= doFinalization(F);
126 Changed |= doFinalization(M);
130 //===----------------------------------------------------------------------===//
131 // Pass Registration mechanism
134 class PassRegistrar {
135 /// PassInfoMap - Keep track of the passinfo object for each registered llvm
137 std::map<TypeInfo, PassInfo*> PassInfoMap;
139 /// AnalysisGroupInfo - Keep track of information for each analysis group.
140 struct AnalysisGroupInfo {
141 const PassInfo *DefaultImpl;
142 std::set<const PassInfo *> Implementations;
143 AnalysisGroupInfo() : DefaultImpl(0) {}
146 /// AnalysisGroupInfoMap - Information for each analysis group.
147 std::map<const PassInfo *, AnalysisGroupInfo> AnalysisGroupInfoMap;
151 const PassInfo *GetPassInfo(const std::type_info &TI) const {
152 std::map<TypeInfo, PassInfo*>::const_iterator I = PassInfoMap.find(TI);
153 return I != PassInfoMap.end() ? I->second : 0;
156 void RegisterPass(PassInfo &PI) {
158 PassInfoMap.insert(std::make_pair(TypeInfo(PI.getTypeInfo()),&PI)).second;
159 assert(Inserted && "Pass registered multiple times!");
162 void UnregisterPass(PassInfo &PI) {
163 std::map<TypeInfo, PassInfo*>::iterator I =
164 PassInfoMap.find(PI.getTypeInfo());
165 assert(I != PassInfoMap.end() && "Pass registered but not in map!");
167 // Remove pass from the map.
168 PassInfoMap.erase(I);
171 void EnumerateWith(PassRegistrationListener *L) {
172 for (std::map<TypeInfo, PassInfo*>::const_iterator I = PassInfoMap.begin(),
173 E = PassInfoMap.end(); I != E; ++I)
174 L->passEnumerate(I->second);
178 /// Analysis Group Mechanisms.
179 void RegisterAnalysisGroup(PassInfo *InterfaceInfo,
180 const PassInfo *ImplementationInfo,
182 AnalysisGroupInfo &AGI = AnalysisGroupInfoMap[InterfaceInfo];
183 assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
184 "Cannot add a pass to the same analysis group more than once!");
185 AGI.Implementations.insert(ImplementationInfo);
187 assert(AGI.DefaultImpl == 0 && InterfaceInfo->getNormalCtor() == 0 &&
188 "Default implementation for analysis group already specified!");
189 assert(ImplementationInfo->getNormalCtor() &&
190 "Cannot specify pass as default if it does not have a default ctor");
191 AGI.DefaultImpl = ImplementationInfo;
192 InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
198 static ManagedStatic<PassRegistrar> PassRegistrarObj;
199 static std::vector<PassRegistrationListener*> *Listeners = 0;
201 // getPassInfo - Return the PassInfo data structure that corresponds to this
203 const PassInfo *Pass::getPassInfo() const {
204 if (PassInfoCache) return PassInfoCache;
205 return lookupPassInfo(typeid(*this));
208 const PassInfo *Pass::lookupPassInfo(const std::type_info &TI) {
209 return PassRegistrarObj->GetPassInfo(TI);
212 void RegisterPassBase::registerPass() {
213 PassRegistrarObj->RegisterPass(PIObj);
215 // Notify any listeners.
217 for (std::vector<PassRegistrationListener*>::iterator
218 I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
219 (*I)->passRegistered(&PIObj);
222 void RegisterPassBase::unregisterPass() {
223 PassRegistrarObj->UnregisterPass(PIObj);
226 //===----------------------------------------------------------------------===//
227 // Analysis Group Implementation Code
228 //===----------------------------------------------------------------------===//
230 // RegisterAGBase implementation
232 RegisterAGBase::RegisterAGBase(const std::type_info &Interface,
233 const std::type_info *Pass, bool isDefault)
234 : RegisterPassBase(Interface),
235 ImplementationInfo(0), isDefaultImplementation(isDefault) {
237 InterfaceInfo = const_cast<PassInfo*>(Pass::lookupPassInfo(Interface));
238 if (InterfaceInfo == 0) {
239 // First reference to Interface, register it now.
241 InterfaceInfo = &PIObj;
243 assert(PIObj.isAnalysisGroup() &&
244 "Trying to join an analysis group that is a normal pass!");
247 ImplementationInfo = Pass::lookupPassInfo(*Pass);
248 assert(ImplementationInfo &&
249 "Must register pass before adding to AnalysisGroup!");
251 // Make sure we keep track of the fact that the implementation implements
253 PassInfo *IIPI = const_cast<PassInfo*>(ImplementationInfo);
254 IIPI->addInterfaceImplemented(InterfaceInfo);
256 PassRegistrarObj->RegisterAnalysisGroup(InterfaceInfo, IIPI, isDefault);
260 void RegisterAGBase::setGroupName(const char *Name) {
261 assert(InterfaceInfo->getPassName()[0] == 0 && "Interface Name already set!");
262 InterfaceInfo->setPassName(Name);
266 //===----------------------------------------------------------------------===//
267 // PassRegistrationListener implementation
270 // PassRegistrationListener ctor - Add the current object to the list of
271 // PassRegistrationListeners...
272 PassRegistrationListener::PassRegistrationListener() {
273 if (!Listeners) Listeners = new std::vector<PassRegistrationListener*>();
274 Listeners->push_back(this);
277 // dtor - Remove object from list of listeners...
278 PassRegistrationListener::~PassRegistrationListener() {
279 std::vector<PassRegistrationListener*>::iterator I =
280 std::find(Listeners->begin(), Listeners->end(), this);
281 assert(Listeners && I != Listeners->end() &&
282 "PassRegistrationListener not registered!");
285 if (Listeners->empty()) {
291 // enumeratePasses - Iterate over the registered passes, calling the
292 // passEnumerate callback on each PassInfo object.
294 void PassRegistrationListener::enumeratePasses() {
295 PassRegistrarObj->EnumerateWith(this);
298 //===----------------------------------------------------------------------===//
299 // AnalysisUsage Class Implementation
303 struct GetCFGOnlyPasses : public PassRegistrationListener {
304 std::vector<AnalysisID> &CFGOnlyList;
305 GetCFGOnlyPasses(std::vector<AnalysisID> &L) : CFGOnlyList(L) {}
307 void passEnumerate(const PassInfo *P) {
308 if (P->isCFGOnlyPass())
309 CFGOnlyList.push_back(P);
314 // setPreservesCFG - This function should be called to by the pass, iff they do
317 // 1. Add or remove basic blocks from the function
318 // 2. Modify terminator instructions in any way.
320 // This function annotates the AnalysisUsage info object to say that analyses
321 // that only depend on the CFG are preserved by this pass.
323 void AnalysisUsage::setPreservesCFG() {
324 // Since this transformation doesn't modify the CFG, it preserves all analyses
325 // that only depend on the CFG (like dominators, loop info, etc...)
326 GetCFGOnlyPasses(Preserved).enumeratePasses();