Unbreak VC++ build.
[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 ManagedStatic<PassRegistrar> PassRegistrarObj;
193 static std::vector<PassRegistrationListener*> *Listeners = 0;
194
195 // getPassInfo - Return the PassInfo data structure that corresponds to this
196 // pass...
197 const PassInfo *Pass::getPassInfo() const {
198   if (PassInfoCache) return PassInfoCache;
199   return lookupPassInfo(typeid(*this));
200 }
201
202 const PassInfo *Pass::lookupPassInfo(const std::type_info &TI) {
203   return PassRegistrarObj->GetPassInfo(TI);
204 }
205
206 void RegisterPassBase::registerPass() {
207   PassRegistrarObj->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   PassRegistrarObj->UnregisterPass(PIObj);
218 }
219
220 //===----------------------------------------------------------------------===//
221 //                  Analysis Group Implementation Code
222 //===----------------------------------------------------------------------===//
223
224 // RegisterAGBase implementation
225 //
226 RegisterAGBase::RegisterAGBase(const std::type_info &Interface,
227                                const std::type_info *Pass, bool isDefault)
228   : RegisterPassBase(Interface),
229     ImplementationInfo(0), isDefaultImplementation(isDefault) {
230
231   InterfaceInfo = const_cast<PassInfo*>(Pass::lookupPassInfo(Interface));
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 (Pass) {
241     ImplementationInfo = Pass::lookupPassInfo(*Pass);
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     PassRegistrarObj->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   PassRegistrarObj->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