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