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