1 //===- Pass.cpp - LLVM Pass Infrastructure Implementation -----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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"
26 //===----------------------------------------------------------------------===//
27 // Pass Implementation
30 // Force out-of-line virtual method.
35 // Force out-of-line virtual method.
36 ModulePass::~ModulePass() { }
38 bool Pass::mustPreserveAnalysisID(const PassInfo *AnalysisID) const {
39 return Resolver->getAnalysisToUpdate(AnalysisID, true) != 0;
42 // dumpPassStructure - Implement the -debug-passes=Structure option
43 void Pass::dumpPassStructure(unsigned Offset) {
44 cerr << std::string(Offset*2, ' ') << getPassName() << "\n";
47 /// getPassName - Return a nice clean name for a pass. This usually
48 /// implemented in terms of the name that is registered by one of the
49 /// Registration templates, but can be overloaded directly.
51 const char *Pass::getPassName() const {
52 if (const PassInfo *PI = getPassInfo())
53 return PI->getPassName();
54 return "Unnamed pass: implement Pass::getPassName()";
57 // print - Print out the internal state of the pass. This is called by Analyze
58 // to print out the contents of an analysis. Otherwise it is not necessary to
59 // implement this method.
61 void Pass::print(std::ostream &O,const Module*) const {
62 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
65 // dump - call print(cerr);
66 void Pass::dump() const {
67 print(*cerr.stream(), 0);
70 //===----------------------------------------------------------------------===//
71 // ImmutablePass Implementation
73 // Force out-of-line virtual method.
74 ImmutablePass::~ImmutablePass() { }
76 //===----------------------------------------------------------------------===//
77 // FunctionPass Implementation
80 // run - On a module, we run this pass by initializing, runOnFunction'ing once
81 // for every function in the module, then by finalizing.
83 bool FunctionPass::runOnModule(Module &M) {
84 bool Changed = doInitialization(M);
86 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
87 if (!I->isDeclaration()) // Passes are not run on external functions!
88 Changed |= runOnFunction(*I);
90 return Changed | doFinalization(M);
93 // run - On a function, we simply initialize, run the function, then finalize.
95 bool FunctionPass::run(Function &F) {
96 // Passes are not run on external functions!
97 if (F.isDeclaration()) return false;
99 bool Changed = doInitialization(*F.getParent());
100 Changed |= runOnFunction(F);
101 return Changed | doFinalization(*F.getParent());
104 //===----------------------------------------------------------------------===//
105 // BasicBlockPass Implementation
108 // To run this pass on a function, we simply call runOnBasicBlock once for each
111 bool BasicBlockPass::runOnFunction(Function &F) {
112 bool Changed = doInitialization(F);
113 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
114 Changed |= runOnBasicBlock(*I);
115 return Changed | doFinalization(F);
118 //===----------------------------------------------------------------------===//
119 // Pass Registration mechanism
122 class PassRegistrar {
123 /// PassInfoMap - Keep track of the passinfo object for each registered llvm
125 std::map<intptr_t, PassInfo*> PassInfoMap;
127 /// AnalysisGroupInfo - Keep track of information for each analysis group.
128 struct AnalysisGroupInfo {
129 const PassInfo *DefaultImpl;
130 std::set<const PassInfo *> Implementations;
131 AnalysisGroupInfo() : DefaultImpl(0) {}
134 /// AnalysisGroupInfoMap - Information for each analysis group.
135 std::map<const PassInfo *, AnalysisGroupInfo> AnalysisGroupInfoMap;
139 const PassInfo *GetPassInfo(intptr_t TI) const {
140 std::map<intptr_t, PassInfo*>::const_iterator I = PassInfoMap.find(TI);
141 return I != PassInfoMap.end() ? I->second : 0;
144 void RegisterPass(PassInfo &PI) {
146 PassInfoMap.insert(std::make_pair(PI.getTypeInfo(),&PI)).second;
147 assert(Inserted && "Pass registered multiple times!");
150 void UnregisterPass(PassInfo &PI) {
151 std::map<intptr_t, PassInfo*>::iterator I =
152 PassInfoMap.find(PI.getTypeInfo());
153 assert(I != PassInfoMap.end() && "Pass registered but not in map!");
155 // Remove pass from the map.
156 PassInfoMap.erase(I);
159 void EnumerateWith(PassRegistrationListener *L) {
160 for (std::map<intptr_t, PassInfo*>::const_iterator I = PassInfoMap.begin(),
161 E = PassInfoMap.end(); I != E; ++I)
162 L->passEnumerate(I->second);
166 /// Analysis Group Mechanisms.
167 void RegisterAnalysisGroup(PassInfo *InterfaceInfo,
168 const PassInfo *ImplementationInfo,
170 AnalysisGroupInfo &AGI = AnalysisGroupInfoMap[InterfaceInfo];
171 assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
172 "Cannot add a pass to the same analysis group more than once!");
173 AGI.Implementations.insert(ImplementationInfo);
175 assert(AGI.DefaultImpl == 0 && InterfaceInfo->getNormalCtor() == 0 &&
176 "Default implementation for analysis group already specified!");
177 assert(ImplementationInfo->getNormalCtor() &&
178 "Cannot specify pass as default if it does not have a default ctor");
179 AGI.DefaultImpl = ImplementationInfo;
180 InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
186 static std::vector<PassRegistrationListener*> *Listeners = 0;
188 // FIXME: This should use ManagedStatic to manage the pass registrar.
189 // Unfortunately, we can't do this, because passes are registered with static
190 // ctors, and having llvm_shutdown clear this map prevents successful
191 // ressurection after llvm_shutdown is run.
192 static PassRegistrar *getPassRegistrar() {
193 static PassRegistrar *PassRegistrarObj = 0;
194 if (!PassRegistrarObj)
195 PassRegistrarObj = new PassRegistrar();
196 return PassRegistrarObj;
199 // getPassInfo - Return the PassInfo data structure that corresponds to this
201 const PassInfo *Pass::getPassInfo() const {
202 return lookupPassInfo(PassID);
205 const PassInfo *Pass::lookupPassInfo(intptr_t TI) {
206 return getPassRegistrar()->GetPassInfo(TI);
209 void RegisterPassBase::registerPass() {
210 getPassRegistrar()->RegisterPass(PIObj);
212 // Notify any listeners.
214 for (std::vector<PassRegistrationListener*>::iterator
215 I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
216 (*I)->passRegistered(&PIObj);
219 void RegisterPassBase::unregisterPass() {
220 getPassRegistrar()->UnregisterPass(PIObj);
223 //===----------------------------------------------------------------------===//
224 // Analysis Group Implementation Code
225 //===----------------------------------------------------------------------===//
227 // RegisterAGBase implementation
229 RegisterAGBase::RegisterAGBase(intptr_t InterfaceID,
230 intptr_t PassID, bool isDefault)
231 : RegisterPassBase(InterfaceID),
232 ImplementationInfo(0), isDefaultImplementation(isDefault) {
234 InterfaceInfo = const_cast<PassInfo*>(Pass::lookupPassInfo(InterfaceID));
235 if (InterfaceInfo == 0) {
236 // First reference to Interface, register it now.
238 InterfaceInfo = &PIObj;
240 assert(PIObj.isAnalysisGroup() &&
241 "Trying to join an analysis group that is a normal pass!");
244 ImplementationInfo = Pass::lookupPassInfo(PassID);
245 assert(ImplementationInfo &&
246 "Must register pass before adding to AnalysisGroup!");
248 // Make sure we keep track of the fact that the implementation implements
250 PassInfo *IIPI = const_cast<PassInfo*>(ImplementationInfo);
251 IIPI->addInterfaceImplemented(InterfaceInfo);
253 getPassRegistrar()->RegisterAnalysisGroup(InterfaceInfo, IIPI, isDefault);
257 void RegisterAGBase::setGroupName(const char *Name) {
258 assert(InterfaceInfo->getPassName()[0] == 0 && "Interface Name already set!");
259 InterfaceInfo->setPassName(Name);
263 //===----------------------------------------------------------------------===//
264 // PassRegistrationListener implementation
267 // PassRegistrationListener ctor - Add the current object to the list of
268 // PassRegistrationListeners...
269 PassRegistrationListener::PassRegistrationListener() {
270 if (!Listeners) Listeners = new std::vector<PassRegistrationListener*>();
271 Listeners->push_back(this);
274 // dtor - Remove object from list of listeners...
275 PassRegistrationListener::~PassRegistrationListener() {
276 std::vector<PassRegistrationListener*>::iterator I =
277 std::find(Listeners->begin(), Listeners->end(), this);
278 assert(Listeners && I != Listeners->end() &&
279 "PassRegistrationListener not registered!");
282 if (Listeners->empty()) {
288 // enumeratePasses - Iterate over the registered passes, calling the
289 // passEnumerate callback on each PassInfo object.
291 void PassRegistrationListener::enumeratePasses() {
292 getPassRegistrar()->EnumerateWith(this);
295 //===----------------------------------------------------------------------===//
296 // AnalysisUsage Class Implementation
300 struct GetCFGOnlyPasses : public PassRegistrationListener {
301 std::vector<AnalysisID> &CFGOnlyList;
302 GetCFGOnlyPasses(std::vector<AnalysisID> &L) : CFGOnlyList(L) {}
304 void passEnumerate(const PassInfo *P) {
305 if (P->isCFGOnlyPass())
306 CFGOnlyList.push_back(P);
311 // setPreservesCFG - This function should be called to by the pass, iff they do
314 // 1. Add or remove basic blocks from the function
315 // 2. Modify terminator instructions in any way.
317 // This function annotates the AnalysisUsage info object to say that analyses
318 // that only depend on the CFG are preserved by this pass.
320 void AnalysisUsage::setPreservesCFG() {
321 // Since this transformation doesn't modify the CFG, it preserves all analyses
322 // that only depend on the CFG (like dominators, loop info, etc...)
323 GetCFGOnlyPasses(Preserved).enumeratePasses();