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