Remove unused runPass methods.
[oota-llvm.git] / lib / VMCore / Pass.cpp
1 //===- Pass.cpp - LLVM Pass Infrastructure Implementation -----------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the LLVM Pass infrastructure.  It is primarily
11 // responsible with ensuring that passes are executed and batched together
12 // optimally.
13 //
14 //===----------------------------------------------------------------------===//
15
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 <algorithm>
22 #include <set>
23 using namespace llvm;
24
25 //===----------------------------------------------------------------------===//
26 // Pass Implementation
27 //
28
29 // Force out-of-line virtual method.
30 Pass::~Pass() { 
31   delete Resolver; 
32 }
33
34 // Force out-of-line virtual method.
35 ModulePass::~ModulePass() { }
36
37 bool Pass::mustPreserveAnalysisID(const PassInfo *AnalysisID) const {
38   return Resolver->getAnalysisToUpdate(AnalysisID, true) != 0;
39 }
40
41 // dumpPassStructure - Implement the -debug-passes=Structure option
42 void Pass::dumpPassStructure(unsigned Offset) {
43   cerr << std::string(Offset*2, ' ') << getPassName() << "\n";
44 }
45
46 // getPassName - Use C++ RTTI to get a SOMEWHAT intelligible name for the pass.
47 //
48 const char *Pass::getPassName() const {
49   if (const PassInfo *PI = getPassInfo())
50     return PI->getPassName();
51   return "Unnamed pass: implement Pass::getPassName()";
52 }
53
54 // print - Print out the internal state of the pass.  This is called by Analyze
55 // to print out the contents of an analysis.  Otherwise it is not necessary to
56 // implement this method.
57 //
58 void Pass::print(std::ostream &O,const Module*) const {
59   O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
60 }
61
62 // dump - call print(cerr);
63 void Pass::dump() const {
64   print(*cerr.stream(), 0);
65 }
66
67 //===----------------------------------------------------------------------===//
68 // ImmutablePass Implementation
69 //
70 // Force out-of-line virtual method.
71 ImmutablePass::~ImmutablePass() { }
72
73 //===----------------------------------------------------------------------===//
74 // FunctionPass Implementation
75 //
76
77 // run - On a module, we run this pass by initializing, runOnFunction'ing once
78 // for every function in the module, then by finalizing.
79 //
80 bool FunctionPass::runOnModule(Module &M) {
81   bool Changed = doInitialization(M);
82
83   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
84     if (!I->isDeclaration())      // Passes are not run on external functions!
85     Changed |= runOnFunction(*I);
86
87   return Changed | doFinalization(M);
88 }
89
90 // run - On a function, we simply initialize, run the function, then finalize.
91 //
92 bool FunctionPass::run(Function &F) {
93   // Passes are not run on external functions!
94   if (F.isDeclaration()) return false;
95
96   bool Changed = doInitialization(*F.getParent());
97   Changed |= runOnFunction(F);
98   return Changed | doFinalization(*F.getParent());
99 }
100
101 //===----------------------------------------------------------------------===//
102 // BasicBlockPass Implementation
103 //
104
105 // To run this pass on a function, we simply call runOnBasicBlock once for each
106 // function.
107 //
108 bool BasicBlockPass::runOnFunction(Function &F) {
109   bool Changed = doInitialization(F);
110   for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
111     Changed |= runOnBasicBlock(*I);
112   return Changed | doFinalization(F);
113 }
114
115 //===----------------------------------------------------------------------===//
116 // Pass Registration mechanism
117 //
118 namespace {
119 class PassRegistrar {
120   /// PassInfoMap - Keep track of the passinfo object for each registered llvm
121   /// pass.
122   std::map<intptr_t, PassInfo*> PassInfoMap;
123   
124   /// AnalysisGroupInfo - Keep track of information for each analysis group.
125   struct AnalysisGroupInfo {
126     const PassInfo *DefaultImpl;
127     std::set<const PassInfo *> Implementations;
128     AnalysisGroupInfo() : DefaultImpl(0) {}
129   };
130   
131   /// AnalysisGroupInfoMap - Information for each analysis group.
132   std::map<const PassInfo *, AnalysisGroupInfo> AnalysisGroupInfoMap;
133
134 public:
135   
136   const PassInfo *GetPassInfo(intptr_t TI) const {
137     std::map<intptr_t, PassInfo*>::const_iterator I = PassInfoMap.find(TI);
138     return I != PassInfoMap.end() ? I->second : 0;
139   }
140   
141   void RegisterPass(PassInfo &PI) {
142     bool Inserted =
143       PassInfoMap.insert(std::make_pair(PI.getTypeInfo(),&PI)).second;
144     assert(Inserted && "Pass registered multiple times!");
145   }
146   
147   void UnregisterPass(PassInfo &PI) {
148     std::map<intptr_t, PassInfo*>::iterator I =
149       PassInfoMap.find(PI.getTypeInfo());
150     assert(I != PassInfoMap.end() && "Pass registered but not in map!");
151     
152     // Remove pass from the map.
153     PassInfoMap.erase(I);
154   }
155   
156   void EnumerateWith(PassRegistrationListener *L) {
157     for (std::map<intptr_t, PassInfo*>::const_iterator I = PassInfoMap.begin(),
158          E = PassInfoMap.end(); I != E; ++I)
159       L->passEnumerate(I->second);
160   }
161   
162   
163   /// Analysis Group Mechanisms.
164   void RegisterAnalysisGroup(PassInfo *InterfaceInfo,
165                              const PassInfo *ImplementationInfo,
166                              bool isDefault) {
167     AnalysisGroupInfo &AGI = AnalysisGroupInfoMap[InterfaceInfo];
168     assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
169            "Cannot add a pass to the same analysis group more than once!");
170     AGI.Implementations.insert(ImplementationInfo);
171     if (isDefault) {
172       assert(AGI.DefaultImpl == 0 && InterfaceInfo->getNormalCtor() == 0 &&
173              "Default implementation for analysis group already specified!");
174       assert(ImplementationInfo->getNormalCtor() &&
175            "Cannot specify pass as default if it does not have a default ctor");
176       AGI.DefaultImpl = ImplementationInfo;
177       InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
178     }
179   }
180 };
181 }
182
183 static std::vector<PassRegistrationListener*> *Listeners = 0;
184
185 // FIXME: This should use ManagedStatic to manage the pass registrar.
186 // Unfortunately, we can't do this, because passes are registered with static
187 // ctors, and having llvm_shutdown clear this map prevents successful
188 // ressurection after llvm_shutdown is run.
189 static PassRegistrar *getPassRegistrar() {
190   static PassRegistrar *PassRegistrarObj = 0;
191   if (!PassRegistrarObj)
192     PassRegistrarObj = new PassRegistrar();
193   return PassRegistrarObj;
194 }
195
196 // getPassInfo - Return the PassInfo data structure that corresponds to this
197 // pass...
198 const PassInfo *Pass::getPassInfo() const {
199   return lookupPassInfo(PassID);
200 }
201
202 const PassInfo *Pass::lookupPassInfo(intptr_t TI) {
203   return getPassRegistrar()->GetPassInfo(TI);
204 }
205
206 void RegisterPassBase::registerPass() {
207   getPassRegistrar()->RegisterPass(PIObj);
208
209   // Notify any listeners.
210   if (Listeners)
211     for (std::vector<PassRegistrationListener*>::iterator
212            I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
213       (*I)->passRegistered(&PIObj);
214 }
215
216 void RegisterPassBase::unregisterPass() {
217   getPassRegistrar()->UnregisterPass(PIObj);
218 }
219
220 //===----------------------------------------------------------------------===//
221 //                  Analysis Group Implementation Code
222 //===----------------------------------------------------------------------===//
223
224 // RegisterAGBase implementation
225 //
226 RegisterAGBase::RegisterAGBase(intptr_t InterfaceID,
227                                intptr_t PassID, bool isDefault)
228   : RegisterPassBase(InterfaceID),
229     ImplementationInfo(0), isDefaultImplementation(isDefault) {
230
231   InterfaceInfo = const_cast<PassInfo*>(Pass::lookupPassInfo(InterfaceID));
232   if (InterfaceInfo == 0) {
233     // First reference to Interface, register it now.
234     registerPass();
235     InterfaceInfo = &PIObj;
236   }
237   assert(PIObj.isAnalysisGroup() &&
238          "Trying to join an analysis group that is a normal pass!");
239
240   if (PassID) {
241     ImplementationInfo = Pass::lookupPassInfo(PassID);
242     assert(ImplementationInfo &&
243            "Must register pass before adding to AnalysisGroup!");
244
245     // Make sure we keep track of the fact that the implementation implements
246     // the interface.
247     PassInfo *IIPI = const_cast<PassInfo*>(ImplementationInfo);
248     IIPI->addInterfaceImplemented(InterfaceInfo);
249     
250     getPassRegistrar()->RegisterAnalysisGroup(InterfaceInfo, IIPI, isDefault);
251   }
252 }
253
254 void RegisterAGBase::setGroupName(const char *Name) {
255   assert(InterfaceInfo->getPassName()[0] == 0 && "Interface Name already set!");
256   InterfaceInfo->setPassName(Name);
257 }
258
259
260 //===----------------------------------------------------------------------===//
261 // PassRegistrationListener implementation
262 //
263
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);
269 }
270
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!");
277   Listeners->erase(I);
278
279   if (Listeners->empty()) {
280     delete Listeners;
281     Listeners = 0;
282   }
283 }
284
285 // enumeratePasses - Iterate over the registered passes, calling the
286 // passEnumerate callback on each PassInfo object.
287 //
288 void PassRegistrationListener::enumeratePasses() {
289   getPassRegistrar()->EnumerateWith(this);
290 }
291
292 //===----------------------------------------------------------------------===//
293 //   AnalysisUsage Class Implementation
294 //
295
296 namespace {
297   struct GetCFGOnlyPasses : public PassRegistrationListener {
298     std::vector<AnalysisID> &CFGOnlyList;
299     GetCFGOnlyPasses(std::vector<AnalysisID> &L) : CFGOnlyList(L) {}
300     
301     void passEnumerate(const PassInfo *P) {
302       if (P->isCFGOnlyPass())
303         CFGOnlyList.push_back(P);
304     }
305   };
306 }
307
308 // setPreservesCFG - This function should be called to by the pass, iff they do
309 // not:
310 //
311 //  1. Add or remove basic blocks from the function
312 //  2. Modify terminator instructions in any way.
313 //
314 // This function annotates the AnalysisUsage info object to say that analyses
315 // that only depend on the CFG are preserved by this pass.
316 //
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();
321 }
322
323