Provide an ICmpInst::makeConstantRange to generate a ConstantRange value
[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 <set>
23 using namespace llvm;
24
25 //===----------------------------------------------------------------------===//
26 // Pass Implementation
27 //
28
29 // Force out-of-line virtual method.
30 ModulePass::~ModulePass() { }
31
32 bool Pass::mustPreserveAnalysisID(const PassInfo *AnalysisID) const {
33   return Resolver->getAnalysisToUpdate(AnalysisID, true) != 0;
34 }
35
36 // dumpPassStructure - Implement the -debug-passes=Structure option
37 void Pass::dumpPassStructure(unsigned Offset) {
38   cerr << std::string(Offset*2, ' ') << getPassName() << "\n";
39 }
40
41 // getPassName - Use C++ RTTI to get a SOMEWHAT intelligible name for the pass.
42 //
43 const char *Pass::getPassName() const {
44   if (const PassInfo *PI = getPassInfo())
45     return PI->getPassName();
46   return typeid(*this).name();
47 }
48
49 // print - Print out the internal state of the pass.  This is called by Analyze
50 // to print out the contents of an analysis.  Otherwise it is not necessary to
51 // implement this method.
52 //
53 void Pass::print(std::ostream &O,const Module*) const {
54   O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
55 }
56
57 // dump - call print(cerr);
58 void Pass::dump() const {
59   print(*cerr.stream(), 0);
60 }
61
62 //===----------------------------------------------------------------------===//
63 // ImmutablePass Implementation
64 //
65 // Force out-of-line virtual method.
66 ImmutablePass::~ImmutablePass() { }
67
68 //===----------------------------------------------------------------------===//
69 // FunctionPass Implementation
70 //
71
72 // run - On a module, we run this pass by initializing, runOnFunction'ing once
73 // for every function in the module, then by finalizing.
74 //
75 bool FunctionPass::runOnModule(Module &M) {
76   bool Changed = doInitialization(M);
77
78   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
79     if (!I->isDeclaration())      // Passes are not run on external functions!
80     Changed |= runOnFunction(*I);
81
82   return Changed | doFinalization(M);
83 }
84
85 // run - On a function, we simply initialize, run the function, then finalize.
86 //
87 bool FunctionPass::run(Function &F) {
88   if (F.isDeclaration()) return false;// Passes are not run on external functions!
89
90   bool Changed = doInitialization(*F.getParent());
91   Changed |= runOnFunction(F);
92   return Changed | doFinalization(*F.getParent());
93 }
94
95 //===----------------------------------------------------------------------===//
96 // BasicBlockPass Implementation
97 //
98
99 // To run this pass on a function, we simply call runOnBasicBlock once for each
100 // function.
101 //
102 bool BasicBlockPass::runOnFunction(Function &F) {
103   bool Changed = doInitialization(F);
104   for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
105     Changed |= runOnBasicBlock(*I);
106   return Changed | doFinalization(F);
107 }
108
109 // To run directly on the basic block, we initialize, runOnBasicBlock, then
110 // finalize.
111 //
112 bool BasicBlockPass::runPass(BasicBlock &BB) {
113   Function &F = *BB.getParent();
114   Module &M = *F.getParent();
115   bool Changed = doInitialization(M);
116   Changed |= doInitialization(F);
117   Changed |= runOnBasicBlock(BB);
118   Changed |= doFinalization(F);
119   Changed |= doFinalization(M);
120   return Changed;
121 }
122
123 //===----------------------------------------------------------------------===//
124 // Pass Registration mechanism
125 //
126 namespace {
127 class PassRegistrar {
128   /// PassInfoMap - Keep track of the passinfo object for each registered llvm
129   /// pass.
130   std::map<TypeInfo, PassInfo*> PassInfoMap;
131   
132   /// AnalysisGroupInfo - Keep track of information for each analysis group.
133   struct AnalysisGroupInfo {
134     const PassInfo *DefaultImpl;
135     std::set<const PassInfo *> Implementations;
136     AnalysisGroupInfo() : DefaultImpl(0) {}
137   };
138   
139   /// AnalysisGroupInfoMap - Information for each analysis group.
140   std::map<const PassInfo *, AnalysisGroupInfo> AnalysisGroupInfoMap;
141
142 public:
143   
144   const PassInfo *GetPassInfo(const std::type_info &TI) const {
145     std::map<TypeInfo, PassInfo*>::const_iterator I = PassInfoMap.find(TI);
146     return I != PassInfoMap.end() ? I->second : 0;
147   }
148   
149   void RegisterPass(PassInfo &PI) {
150     bool Inserted =
151       PassInfoMap.insert(std::make_pair(TypeInfo(PI.getTypeInfo()),&PI)).second;
152     assert(Inserted && "Pass registered multiple times!");
153   }
154   
155   void UnregisterPass(PassInfo &PI) {
156     std::map<TypeInfo, PassInfo*>::iterator I =
157       PassInfoMap.find(PI.getTypeInfo());
158     assert(I != PassInfoMap.end() && "Pass registered but not in map!");
159     
160     // Remove pass from the map.
161     PassInfoMap.erase(I);
162   }
163   
164   void EnumerateWith(PassRegistrationListener *L) {
165     for (std::map<TypeInfo, PassInfo*>::const_iterator I = PassInfoMap.begin(),
166          E = PassInfoMap.end(); I != E; ++I)
167       L->passEnumerate(I->second);
168   }
169   
170   
171   /// Analysis Group Mechanisms.
172   void RegisterAnalysisGroup(PassInfo *InterfaceInfo,
173                              const PassInfo *ImplementationInfo,
174                              bool isDefault) {
175     AnalysisGroupInfo &AGI = AnalysisGroupInfoMap[InterfaceInfo];
176     assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
177            "Cannot add a pass to the same analysis group more than once!");
178     AGI.Implementations.insert(ImplementationInfo);
179     if (isDefault) {
180       assert(AGI.DefaultImpl == 0 && InterfaceInfo->getNormalCtor() == 0 &&
181              "Default implementation for analysis group already specified!");
182       assert(ImplementationInfo->getNormalCtor() &&
183            "Cannot specify pass as default if it does not have a default ctor");
184       AGI.DefaultImpl = ImplementationInfo;
185       InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
186     }
187   }
188 };
189 }
190
191 static ManagedStatic<PassRegistrar> PassRegistrarObj;
192 static std::vector<PassRegistrationListener*> *Listeners = 0;
193
194 // getPassInfo - Return the PassInfo data structure that corresponds to this
195 // pass...
196 const PassInfo *Pass::getPassInfo() const {
197   if (PassInfoCache) return PassInfoCache;
198   return lookupPassInfo(typeid(*this));
199 }
200
201 const PassInfo *Pass::lookupPassInfo(const std::type_info &TI) {
202   return PassRegistrarObj->GetPassInfo(TI);
203 }
204
205 void RegisterPassBase::registerPass() {
206   PassRegistrarObj->RegisterPass(PIObj);
207
208   // Notify any listeners.
209   if (Listeners)
210     for (std::vector<PassRegistrationListener*>::iterator
211            I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
212       (*I)->passRegistered(&PIObj);
213 }
214
215 void RegisterPassBase::unregisterPass() {
216   PassRegistrarObj->UnregisterPass(PIObj);
217 }
218
219 //===----------------------------------------------------------------------===//
220 //                  Analysis Group Implementation Code
221 //===----------------------------------------------------------------------===//
222
223 // RegisterAGBase implementation
224 //
225 RegisterAGBase::RegisterAGBase(const std::type_info &Interface,
226                                const std::type_info *Pass, bool isDefault)
227   : RegisterPassBase(Interface),
228     ImplementationInfo(0), isDefaultImplementation(isDefault) {
229
230   InterfaceInfo = const_cast<PassInfo*>(Pass::lookupPassInfo(Interface));
231   if (InterfaceInfo == 0) {
232     // First reference to Interface, register it now.
233     registerPass();
234     InterfaceInfo = &PIObj;
235   }
236   assert(PIObj.isAnalysisGroup() &&
237          "Trying to join an analysis group that is a normal pass!");
238
239   if (Pass) {
240     ImplementationInfo = Pass::lookupPassInfo(*Pass);
241     assert(ImplementationInfo &&
242            "Must register pass before adding to AnalysisGroup!");
243
244     // Make sure we keep track of the fact that the implementation implements
245     // the interface.
246     PassInfo *IIPI = const_cast<PassInfo*>(ImplementationInfo);
247     IIPI->addInterfaceImplemented(InterfaceInfo);
248     
249     PassRegistrarObj->RegisterAnalysisGroup(InterfaceInfo, IIPI, isDefault);
250   }
251 }
252
253 void RegisterAGBase::setGroupName(const char *Name) {
254   assert(InterfaceInfo->getPassName()[0] == 0 && "Interface Name already set!");
255   InterfaceInfo->setPassName(Name);
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   PassRegistrarObj->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