Don't include <map> in Pass.h, which doesn't need it. This requires
[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 <map>
23 #include <set>
24 using namespace llvm;
25
26 //===----------------------------------------------------------------------===//
27 // Pass Implementation
28 //
29
30 // Force out-of-line virtual method.
31 Pass::~Pass() { 
32   delete Resolver; 
33 }
34
35 // Force out-of-line virtual method.
36 ModulePass::~ModulePass() { }
37
38 bool Pass::mustPreserveAnalysisID(const PassInfo *AnalysisID) const {
39   return Resolver->getAnalysisToUpdate(AnalysisID, true) != 0;
40 }
41
42 // dumpPassStructure - Implement the -debug-passes=Structure option
43 void Pass::dumpPassStructure(unsigned Offset) {
44   cerr << std::string(Offset*2, ' ') << getPassName() << "\n";
45 }
46
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.
50 ///
51 const char *Pass::getPassName() const {
52   if (const PassInfo *PI = getPassInfo())
53     return PI->getPassName();
54   return "Unnamed pass: implement Pass::getPassName()";
55 }
56
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.
60 //
61 void Pass::print(std::ostream &O,const Module*) const {
62   O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
63 }
64
65 // dump - call print(cerr);
66 void Pass::dump() const {
67   print(*cerr.stream(), 0);
68 }
69
70 //===----------------------------------------------------------------------===//
71 // ImmutablePass Implementation
72 //
73 // Force out-of-line virtual method.
74 ImmutablePass::~ImmutablePass() { }
75
76 //===----------------------------------------------------------------------===//
77 // FunctionPass Implementation
78 //
79
80 // run - On a module, we run this pass by initializing, runOnFunction'ing once
81 // for every function in the module, then by finalizing.
82 //
83 bool FunctionPass::runOnModule(Module &M) {
84   bool Changed = doInitialization(M);
85
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);
89
90   return Changed | doFinalization(M);
91 }
92
93 // run - On a function, we simply initialize, run the function, then finalize.
94 //
95 bool FunctionPass::run(Function &F) {
96   // Passes are not run on external functions!
97   if (F.isDeclaration()) return false;
98
99   bool Changed = doInitialization(*F.getParent());
100   Changed |= runOnFunction(F);
101   return Changed | doFinalization(*F.getParent());
102 }
103
104 //===----------------------------------------------------------------------===//
105 // BasicBlockPass Implementation
106 //
107
108 // To run this pass on a function, we simply call runOnBasicBlock once for each
109 // function.
110 //
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);
116 }
117
118 //===----------------------------------------------------------------------===//
119 // Pass Registration mechanism
120 //
121 namespace {
122 class PassRegistrar {
123   /// PassInfoMap - Keep track of the passinfo object for each registered llvm
124   /// pass.
125   std::map<intptr_t, PassInfo*> PassInfoMap;
126   
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) {}
132   };
133   
134   /// AnalysisGroupInfoMap - Information for each analysis group.
135   std::map<const PassInfo *, AnalysisGroupInfo> AnalysisGroupInfoMap;
136
137 public:
138   
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;
142   }
143   
144   void RegisterPass(PassInfo &PI) {
145     bool Inserted =
146       PassInfoMap.insert(std::make_pair(PI.getTypeInfo(),&PI)).second;
147     assert(Inserted && "Pass registered multiple times!");
148   }
149   
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!");
154     
155     // Remove pass from the map.
156     PassInfoMap.erase(I);
157   }
158   
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);
163   }
164   
165   
166   /// Analysis Group Mechanisms.
167   void RegisterAnalysisGroup(PassInfo *InterfaceInfo,
168                              const PassInfo *ImplementationInfo,
169                              bool isDefault) {
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);
174     if (isDefault) {
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());
181     }
182   }
183 };
184 }
185
186 static std::vector<PassRegistrationListener*> *Listeners = 0;
187
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;
197 }
198
199 // getPassInfo - Return the PassInfo data structure that corresponds to this
200 // pass...
201 const PassInfo *Pass::getPassInfo() const {
202   return lookupPassInfo(PassID);
203 }
204
205 const PassInfo *Pass::lookupPassInfo(intptr_t TI) {
206   return getPassRegistrar()->GetPassInfo(TI);
207 }
208
209 void RegisterPassBase::registerPass() {
210   getPassRegistrar()->RegisterPass(PIObj);
211
212   // Notify any listeners.
213   if (Listeners)
214     for (std::vector<PassRegistrationListener*>::iterator
215            I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
216       (*I)->passRegistered(&PIObj);
217 }
218
219 void RegisterPassBase::unregisterPass() {
220   getPassRegistrar()->UnregisterPass(PIObj);
221 }
222
223 //===----------------------------------------------------------------------===//
224 //                  Analysis Group Implementation Code
225 //===----------------------------------------------------------------------===//
226
227 // RegisterAGBase implementation
228 //
229 RegisterAGBase::RegisterAGBase(intptr_t InterfaceID,
230                                intptr_t PassID, bool isDefault)
231   : RegisterPassBase(InterfaceID),
232     ImplementationInfo(0), isDefaultImplementation(isDefault) {
233
234   InterfaceInfo = const_cast<PassInfo*>(Pass::lookupPassInfo(InterfaceID));
235   if (InterfaceInfo == 0) {
236     // First reference to Interface, register it now.
237     registerPass();
238     InterfaceInfo = &PIObj;
239   }
240   assert(PIObj.isAnalysisGroup() &&
241          "Trying to join an analysis group that is a normal pass!");
242
243   if (PassID) {
244     ImplementationInfo = Pass::lookupPassInfo(PassID);
245     assert(ImplementationInfo &&
246            "Must register pass before adding to AnalysisGroup!");
247
248     // Make sure we keep track of the fact that the implementation implements
249     // the interface.
250     PassInfo *IIPI = const_cast<PassInfo*>(ImplementationInfo);
251     IIPI->addInterfaceImplemented(InterfaceInfo);
252     
253     getPassRegistrar()->RegisterAnalysisGroup(InterfaceInfo, IIPI, isDefault);
254   }
255 }
256
257 void RegisterAGBase::setGroupName(const char *Name) {
258   assert(InterfaceInfo->getPassName()[0] == 0 && "Interface Name already set!");
259   InterfaceInfo->setPassName(Name);
260 }
261
262
263 //===----------------------------------------------------------------------===//
264 // PassRegistrationListener implementation
265 //
266
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);
272 }
273
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!");
280   Listeners->erase(I);
281
282   if (Listeners->empty()) {
283     delete Listeners;
284     Listeners = 0;
285   }
286 }
287
288 // enumeratePasses - Iterate over the registered passes, calling the
289 // passEnumerate callback on each PassInfo object.
290 //
291 void PassRegistrationListener::enumeratePasses() {
292   getPassRegistrar()->EnumerateWith(this);
293 }
294
295 //===----------------------------------------------------------------------===//
296 //   AnalysisUsage Class Implementation
297 //
298
299 namespace {
300   struct GetCFGOnlyPasses : public PassRegistrationListener {
301     std::vector<AnalysisID> &CFGOnlyList;
302     GetCFGOnlyPasses(std::vector<AnalysisID> &L) : CFGOnlyList(L) {}
303     
304     void passEnumerate(const PassInfo *P) {
305       if (P->isCFGOnlyPass())
306         CFGOnlyList.push_back(P);
307     }
308   };
309 }
310
311 // setPreservesCFG - This function should be called to by the pass, iff they do
312 // not:
313 //
314 //  1. Add or remove basic blocks from the function
315 //  2. Modify terminator instructions in any way.
316 //
317 // This function annotates the AnalysisUsage info object to say that analyses
318 // that only depend on the CFG are preserved by this pass.
319 //
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();
324 }
325
326