Fix a bug that prevented the JIT from working correctly after llvm_shutdown.
[oota-llvm.git] / lib / VMCore / Pass.cpp
1 //===- Pass.cpp - LLVM Pass Infrastructure Implementation -----------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
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 "llvm/Support/TypeInfo.h"
22 #include <algorithm>
23 #include <set>
24 using namespace llvm;
25
26 //===----------------------------------------------------------------------===//
27 // Pass Implementation
28 //
29
30 // Force out-of-line virtual method.
31 ModulePass::~ModulePass() { }
32
33 bool Pass::mustPreserveAnalysisID(const PassInfo *AnalysisID) const {
34   return Resolver->getAnalysisToUpdate(AnalysisID, true) != 0;
35 }
36
37 // dumpPassStructure - Implement the -debug-passes=Structure option
38 void Pass::dumpPassStructure(unsigned Offset) {
39   cerr << std::string(Offset*2, ' ') << getPassName() << "\n";
40 }
41
42 // getPassName - Use C++ RTTI to get a SOMEWHAT intelligible name for the pass.
43 //
44 const char *Pass::getPassName() const {
45   if (const PassInfo *PI = getPassInfo())
46     return PI->getPassName();
47   return typeid(*this).name();
48 }
49
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.
53 //
54 void Pass::print(std::ostream &O,const Module*) const {
55   O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
56 }
57
58 // dump - call print(cerr);
59 void Pass::dump() const {
60   print(*cerr.stream(), 0);
61 }
62
63 //===----------------------------------------------------------------------===//
64 // ImmutablePass Implementation
65 //
66 // Force out-of-line virtual method.
67 ImmutablePass::~ImmutablePass() { }
68
69 //===----------------------------------------------------------------------===//
70 // FunctionPass Implementation
71 //
72
73 // run - On a module, we run this pass by initializing, runOnFunction'ing once
74 // for every function in the module, then by finalizing.
75 //
76 bool FunctionPass::runOnModule(Module &M) {
77   bool Changed = doInitialization(M);
78
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);
82
83   return Changed | doFinalization(M);
84 }
85
86 // run - On a function, we simply initialize, run the function, then finalize.
87 //
88 bool FunctionPass::run(Function &F) {
89   if (F.isDeclaration()) return false;// Passes are not run on external functions!
90
91   bool Changed = doInitialization(*F.getParent());
92   Changed |= runOnFunction(F);
93   return Changed | doFinalization(*F.getParent());
94 }
95
96 //===----------------------------------------------------------------------===//
97 // BasicBlockPass Implementation
98 //
99
100 // To run this pass on a function, we simply call runOnBasicBlock once for each
101 // function.
102 //
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);
108 }
109
110 // To run directly on the basic block, we initialize, runOnBasicBlock, then
111 // finalize.
112 //
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);
121   return Changed;
122 }
123
124 //===----------------------------------------------------------------------===//
125 // Pass Registration mechanism
126 //
127 namespace {
128 class PassRegistrar {
129   /// PassInfoMap - Keep track of the passinfo object for each registered llvm
130   /// pass.
131   std::map<TypeInfo, PassInfo*> PassInfoMap;
132   
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) {}
138   };
139   
140   /// AnalysisGroupInfoMap - Information for each analysis group.
141   std::map<const PassInfo *, AnalysisGroupInfo> AnalysisGroupInfoMap;
142
143 public:
144   
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;
148   }
149   
150   void RegisterPass(PassInfo &PI) {
151     bool Inserted =
152       PassInfoMap.insert(std::make_pair(TypeInfo(PI.getTypeInfo()),&PI)).second;
153     assert(Inserted && "Pass registered multiple times!");
154   }
155   
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!");
160     
161     // Remove pass from the map.
162     PassInfoMap.erase(I);
163   }
164   
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);
169   }
170   
171   
172   /// Analysis Group Mechanisms.
173   void RegisterAnalysisGroup(PassInfo *InterfaceInfo,
174                              const PassInfo *ImplementationInfo,
175                              bool isDefault) {
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);
180     if (isDefault) {
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());
187     }
188   }
189 };
190 }
191
192 static std::vector<PassRegistrationListener*> *Listeners = 0;
193
194 // FIXME: This should use ManagedStatic to manage the pass registrar.
195 // Unfortunately, we can't do this, because passes are registered with static
196 // ctors, and having llvm_shutdown clear this map prevents successful
197 // ressurection after llvm_shutdown is run.
198 static PassRegistrar *getPassRegistrar() {
199   static PassRegistrar *PassRegistrarObj = 0;
200   if (!PassRegistrarObj)
201     PassRegistrarObj = new PassRegistrar();
202   return PassRegistrarObj;
203 }
204
205 // getPassInfo - Return the PassInfo data structure that corresponds to this
206 // pass...
207 const PassInfo *Pass::getPassInfo() const {
208   if (PassInfoCache) return PassInfoCache;
209   return lookupPassInfo(typeid(*this));
210 }
211
212 const PassInfo *Pass::lookupPassInfo(const std::type_info &TI) {
213   return getPassRegistrar()->GetPassInfo(TI);
214 }
215
216 void RegisterPassBase::registerPass() {
217   getPassRegistrar()->RegisterPass(PIObj);
218
219   // Notify any listeners.
220   if (Listeners)
221     for (std::vector<PassRegistrationListener*>::iterator
222            I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
223       (*I)->passRegistered(&PIObj);
224 }
225
226 void RegisterPassBase::unregisterPass() {
227   getPassRegistrar()->UnregisterPass(PIObj);
228 }
229
230 //===----------------------------------------------------------------------===//
231 //                  Analysis Group Implementation Code
232 //===----------------------------------------------------------------------===//
233
234 // RegisterAGBase implementation
235 //
236 RegisterAGBase::RegisterAGBase(const std::type_info &Interface,
237                                const std::type_info *Pass, bool isDefault)
238   : RegisterPassBase(Interface),
239     ImplementationInfo(0), isDefaultImplementation(isDefault) {
240
241   InterfaceInfo = const_cast<PassInfo*>(Pass::lookupPassInfo(Interface));
242   if (InterfaceInfo == 0) {
243     // First reference to Interface, register it now.
244     registerPass();
245     InterfaceInfo = &PIObj;
246   }
247   assert(PIObj.isAnalysisGroup() &&
248          "Trying to join an analysis group that is a normal pass!");
249
250   if (Pass) {
251     ImplementationInfo = Pass::lookupPassInfo(*Pass);
252     assert(ImplementationInfo &&
253            "Must register pass before adding to AnalysisGroup!");
254
255     // Make sure we keep track of the fact that the implementation implements
256     // the interface.
257     PassInfo *IIPI = const_cast<PassInfo*>(ImplementationInfo);
258     IIPI->addInterfaceImplemented(InterfaceInfo);
259     
260     getPassRegistrar()->RegisterAnalysisGroup(InterfaceInfo, IIPI, isDefault);
261   }
262 }
263
264 void RegisterAGBase::setGroupName(const char *Name) {
265   assert(InterfaceInfo->getPassName()[0] == 0 && "Interface Name already set!");
266   InterfaceInfo->setPassName(Name);
267 }
268
269
270 //===----------------------------------------------------------------------===//
271 // PassRegistrationListener implementation
272 //
273
274 // PassRegistrationListener ctor - Add the current object to the list of
275 // PassRegistrationListeners...
276 PassRegistrationListener::PassRegistrationListener() {
277   if (!Listeners) Listeners = new std::vector<PassRegistrationListener*>();
278   Listeners->push_back(this);
279 }
280
281 // dtor - Remove object from list of listeners...
282 PassRegistrationListener::~PassRegistrationListener() {
283   std::vector<PassRegistrationListener*>::iterator I =
284     std::find(Listeners->begin(), Listeners->end(), this);
285   assert(Listeners && I != Listeners->end() &&
286          "PassRegistrationListener not registered!");
287   Listeners->erase(I);
288
289   if (Listeners->empty()) {
290     delete Listeners;
291     Listeners = 0;
292   }
293 }
294
295 // enumeratePasses - Iterate over the registered passes, calling the
296 // passEnumerate callback on each PassInfo object.
297 //
298 void PassRegistrationListener::enumeratePasses() {
299   getPassRegistrar()->EnumerateWith(this);
300 }
301
302 //===----------------------------------------------------------------------===//
303 //   AnalysisUsage Class Implementation
304 //
305
306 namespace {
307   struct GetCFGOnlyPasses : public PassRegistrationListener {
308     std::vector<AnalysisID> &CFGOnlyList;
309     GetCFGOnlyPasses(std::vector<AnalysisID> &L) : CFGOnlyList(L) {}
310     
311     void passEnumerate(const PassInfo *P) {
312       if (P->isCFGOnlyPass())
313         CFGOnlyList.push_back(P);
314     }
315   };
316 }
317
318 // setPreservesCFG - This function should be called to by the pass, iff they do
319 // not:
320 //
321 //  1. Add or remove basic blocks from the function
322 //  2. Modify terminator instructions in any way.
323 //
324 // This function annotates the AnalysisUsage info object to say that analyses
325 // that only depend on the CFG are preserved by this pass.
326 //
327 void AnalysisUsage::setPreservesCFG() {
328   // Since this transformation doesn't modify the CFG, it preserves all analyses
329   // that only depend on the CFG (like dominators, loop info, etc...)
330   GetCFGOnlyPasses(Preserved).enumeratePasses();
331 }
332
333