What the loop unroller cares about, rather than just not unrolling loops with calls, is
[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/PassRegistry.h"
18 #include "llvm/Assembly/PrintModulePass.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/PassNameParser.h"
21 #include "llvm/Support/raw_ostream.h"
22 using namespace llvm;
23
24 //===----------------------------------------------------------------------===//
25 // Pass Implementation
26 //
27
28 Pass::Pass(PassKind K, char &pid) : Resolver(0), PassID(&pid), Kind(K) { }
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 Pass *ModulePass::createPrinterPass(raw_ostream &O,
39                                     const std::string &Banner) const {
40   return createPrintModulePass(&O, false, Banner);
41 }
42
43 PassManagerType ModulePass::getPotentialPassManagerType() const {
44   return PMT_ModulePassManager;
45 }
46
47 bool Pass::mustPreserveAnalysisID(char &AID) const {
48   return Resolver->getAnalysisIfAvailable(&AID, true) != 0;
49 }
50
51 // dumpPassStructure - Implement the -debug-passes=Structure option
52 void Pass::dumpPassStructure(unsigned Offset) {
53   dbgs().indent(Offset*2) << getPassName() << "\n";
54 }
55
56 /// getPassName - Return a nice clean name for a pass.  This usually
57 /// implemented in terms of the name that is registered by one of the
58 /// Registration templates, but can be overloaded directly.
59 ///
60 const char *Pass::getPassName() const {
61   AnalysisID AID =  getPassID();
62   const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
63   if (PI)
64     return PI->getPassName();
65   return "Unnamed pass: implement Pass::getPassName()";
66 }
67
68 void Pass::preparePassManager(PMStack &) {
69   // By default, don't do anything.
70 }
71
72 PassManagerType Pass::getPotentialPassManagerType() const {
73   // Default implementation.
74   return PMT_Unknown; 
75 }
76
77 void Pass::getAnalysisUsage(AnalysisUsage &) const {
78   // By default, no analysis results are used, all are invalidated.
79 }
80
81 void Pass::releaseMemory() {
82   // By default, don't do anything.
83 }
84
85 void Pass::verifyAnalysis() const {
86   // By default, don't do anything.
87 }
88
89 void *Pass::getAdjustedAnalysisPointer(AnalysisID AID) {
90   return this;
91 }
92
93 ImmutablePass *Pass::getAsImmutablePass() {
94   return 0;
95 }
96
97 PMDataManager *Pass::getAsPMDataManager() {
98   return 0;
99 }
100
101 void Pass::setResolver(AnalysisResolver *AR) {
102   assert(!Resolver && "Resolver is already set");
103   Resolver = AR;
104 }
105
106 // print - Print out the internal state of the pass.  This is called by Analyze
107 // to print out the contents of an analysis.  Otherwise it is not necessary to
108 // implement this method.
109 //
110 void Pass::print(raw_ostream &O,const Module*) const {
111   O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
112 }
113
114 // dump - call print(cerr);
115 void Pass::dump() const {
116   print(dbgs(), 0);
117 }
118
119 //===----------------------------------------------------------------------===//
120 // ImmutablePass Implementation
121 //
122 // Force out-of-line virtual method.
123 ImmutablePass::~ImmutablePass() { }
124
125 void ImmutablePass::initializePass() {
126   // By default, don't do anything.
127 }
128
129 //===----------------------------------------------------------------------===//
130 // FunctionPass Implementation
131 //
132
133 Pass *FunctionPass::createPrinterPass(raw_ostream &O,
134                                       const std::string &Banner) const {
135   return createPrintFunctionPass(Banner, &O);
136 }
137
138 bool FunctionPass::doInitialization(Module &) {
139   // By default, don't do anything.
140   return false;
141 }
142
143 bool FunctionPass::doFinalization(Module &) {
144   // By default, don't do anything.
145   return false;
146 }
147
148 PassManagerType FunctionPass::getPotentialPassManagerType() const {
149   return PMT_FunctionPassManager;
150 }
151
152 //===----------------------------------------------------------------------===//
153 // BasicBlockPass Implementation
154 //
155
156 Pass *BasicBlockPass::createPrinterPass(raw_ostream &O,
157                                         const std::string &Banner) const {
158   
159   llvm_unreachable("BasicBlockPass printing unsupported.");
160   return 0;
161 }
162
163 bool BasicBlockPass::doInitialization(Module &) {
164   // By default, don't do anything.
165   return false;
166 }
167
168 bool BasicBlockPass::doInitialization(Function &) {
169   // By default, don't do anything.
170   return false;
171 }
172
173 bool BasicBlockPass::doFinalization(Function &) {
174   // By default, don't do anything.
175   return false;
176 }
177
178 bool BasicBlockPass::doFinalization(Module &) {
179   // By default, don't do anything.
180   return false;
181 }
182
183 PassManagerType BasicBlockPass::getPotentialPassManagerType() const {
184   return PMT_BasicBlockPassManager; 
185 }
186
187 const PassInfo *Pass::lookupPassInfo(const void *TI) {
188   return PassRegistry::getPassRegistry()->getPassInfo(TI);
189 }
190
191 const PassInfo *Pass::lookupPassInfo(StringRef Arg) {
192   return PassRegistry::getPassRegistry()->getPassInfo(Arg);
193 }
194
195 Pass *PassInfo::createPass() const {
196   assert((!isAnalysisGroup() || NormalCtor) &&
197          "No default implementation found for analysis group!");
198   assert(NormalCtor &&
199          "Cannot call createPass on PassInfo without default ctor!");
200   return NormalCtor();
201 }
202
203 //===----------------------------------------------------------------------===//
204 //                  Analysis Group Implementation Code
205 //===----------------------------------------------------------------------===//
206
207 // RegisterAGBase implementation
208 //
209 RegisterAGBase::RegisterAGBase(const char *Name, const void *InterfaceID,
210                                const void *PassID, bool isDefault)
211     : PassInfo(Name, InterfaceID) {
212   PassRegistry::getPassRegistry()->registerAnalysisGroup(InterfaceID, PassID,
213                                                          *this, isDefault);
214 }
215
216
217 //===----------------------------------------------------------------------===//
218 // PassRegistrationListener implementation
219 //
220
221 // PassRegistrationListener ctor - Add the current object to the list of
222 // PassRegistrationListeners...
223 PassRegistrationListener::PassRegistrationListener() {
224   PassRegistry::getPassRegistry()->addRegistrationListener(this);
225 }
226
227 // dtor - Remove object from list of listeners...
228 PassRegistrationListener::~PassRegistrationListener() {
229   PassRegistry::getPassRegistry()->removeRegistrationListener(this);
230 }
231
232 // enumeratePasses - Iterate over the registered passes, calling the
233 // passEnumerate callback on each PassInfo object.
234 //
235 void PassRegistrationListener::enumeratePasses() {
236   PassRegistry::getPassRegistry()->enumerateWith(this);
237 }
238
239 PassNameParser::~PassNameParser() {}
240
241 //===----------------------------------------------------------------------===//
242 //   AnalysisUsage Class Implementation
243 //
244
245 namespace {
246   struct GetCFGOnlyPasses : public PassRegistrationListener {
247     typedef AnalysisUsage::VectorType VectorType;
248     VectorType &CFGOnlyList;
249     GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
250     
251     void passEnumerate(const PassInfo *P) {
252       if (P->isCFGOnlyPass())
253         CFGOnlyList.push_back(P->getTypeInfo());
254     }
255   };
256 }
257
258 // setPreservesCFG - This function should be called to by the pass, iff they do
259 // not:
260 //
261 //  1. Add or remove basic blocks from the function
262 //  2. Modify terminator instructions in any way.
263 //
264 // This function annotates the AnalysisUsage info object to say that analyses
265 // that only depend on the CFG are preserved by this pass.
266 //
267 void AnalysisUsage::setPreservesCFG() {
268   // Since this transformation doesn't modify the CFG, it preserves all analyses
269   // that only depend on the CFG (like dominators, loop info, etc...)
270   GetCFGOnlyPasses(Preserved).enumeratePasses();
271 }
272
273 AnalysisUsage &AnalysisUsage::addPreserved(StringRef Arg) {
274   const PassInfo *PI = Pass::lookupPassInfo(Arg);
275   // If the pass exists, preserve it. Otherwise silently do nothing.
276   if (PI) Preserved.push_back(PI->getTypeInfo());
277   return *this;
278 }
279
280 AnalysisUsage &AnalysisUsage::addRequiredID(const void *ID) {
281   Required.push_back(ID);
282   return *this;
283 }
284
285 AnalysisUsage &AnalysisUsage::addRequiredID(char &ID) {
286   Required.push_back(&ID);
287   return *this;
288 }
289
290 AnalysisUsage &AnalysisUsage::addRequiredTransitiveID(char &ID) {
291   Required.push_back(&ID);
292   RequiredTransitive.push_back(&ID);
293   return *this;
294 }