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