]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/llvm/lib/IR/Pass.cpp
Move all sources from the llvm project into contrib/llvm-project.
[FreeBSD/FreeBSD.git] / contrib / llvm-project / llvm / lib / IR / Pass.cpp
1 //===- Pass.cpp - LLVM Pass Infrastructure Implementation -----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the LLVM Pass infrastructure.  It is primarily
10 // responsible with ensuring that passes are executed and batched together
11 // optimally.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/Pass.h"
16 #include "llvm/Config/llvm-config.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 static std::string getDescription(const Module &M) {
59   return "module (" + M.getName().str() + ")";
60 }
61
62 bool ModulePass::skipModule(Module &M) const {
63   OptPassGate &Gate = M.getContext().getOptPassGate();
64   return Gate.isEnabled() && !Gate.shouldRunPass(this, getDescription(M));
65 }
66
67 bool Pass::mustPreserveAnalysisID(char &AID) const {
68   return Resolver->getAnalysisIfAvailable(&AID, true) != nullptr;
69 }
70
71 // dumpPassStructure - Implement the -debug-pass=Structure option
72 void Pass::dumpPassStructure(unsigned Offset) {
73   dbgs().indent(Offset*2) << getPassName() << "\n";
74 }
75
76 /// getPassName - Return a nice clean name for a pass.  This usually
77 /// implemented in terms of the name that is registered by one of the
78 /// Registration templates, but can be overloaded directly.
79 StringRef Pass::getPassName() const {
80   AnalysisID AID =  getPassID();
81   const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
82   if (PI)
83     return PI->getPassName();
84   return "Unnamed pass: implement Pass::getPassName()";
85 }
86
87 void Pass::preparePassManager(PMStack &) {
88   // By default, don't do anything.
89 }
90
91 PassManagerType Pass::getPotentialPassManagerType() const {
92   // Default implementation.
93   return PMT_Unknown;
94 }
95
96 void Pass::getAnalysisUsage(AnalysisUsage &) const {
97   // By default, no analysis results are used, all are invalidated.
98 }
99
100 void Pass::releaseMemory() {
101   // By default, don't do anything.
102 }
103
104 void Pass::verifyAnalysis() const {
105   // By default, don't do anything.
106 }
107
108 void *Pass::getAdjustedAnalysisPointer(AnalysisID AID) {
109   return this;
110 }
111
112 ImmutablePass *Pass::getAsImmutablePass() {
113   return nullptr;
114 }
115
116 PMDataManager *Pass::getAsPMDataManager() {
117   return nullptr;
118 }
119
120 void Pass::setResolver(AnalysisResolver *AR) {
121   assert(!Resolver && "Resolver is already set");
122   Resolver = AR;
123 }
124
125 // print - Print out the internal state of the pass.  This is called by Analyze
126 // to print out the contents of an analysis.  Otherwise it is not necessary to
127 // implement this method.
128 void Pass::print(raw_ostream &OS, const Module *) const {
129   OS << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
130 }
131
132 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
133 // dump - call print(cerr);
134 LLVM_DUMP_METHOD void Pass::dump() const {
135   print(dbgs(), nullptr);
136 }
137 #endif
138
139 //===----------------------------------------------------------------------===//
140 // ImmutablePass Implementation
141 //
142 // Force out-of-line virtual method.
143 ImmutablePass::~ImmutablePass() = default;
144
145 void ImmutablePass::initializePass() {
146   // By default, don't do anything.
147 }
148
149 //===----------------------------------------------------------------------===//
150 // FunctionPass Implementation
151 //
152
153 Pass *FunctionPass::createPrinterPass(raw_ostream &OS,
154                                       const std::string &Banner) const {
155   return createPrintFunctionPass(OS, Banner);
156 }
157
158 PassManagerType FunctionPass::getPotentialPassManagerType() const {
159   return PMT_FunctionPassManager;
160 }
161
162 static std::string getDescription(const Function &F) {
163   return "function (" + F.getName().str() + ")";
164 }
165
166 bool FunctionPass::skipFunction(const Function &F) const {
167   OptPassGate &Gate = F.getContext().getOptPassGate();
168   if (Gate.isEnabled() && !Gate.shouldRunPass(this, getDescription(F)))
169     return true;
170
171   if (F.hasOptNone()) {
172     LLVM_DEBUG(dbgs() << "Skipping pass '" << getPassName() << "' on function "
173                       << F.getName() << "\n");
174     return true;
175   }
176   return false;
177 }
178
179 //===----------------------------------------------------------------------===//
180 // BasicBlockPass Implementation
181 //
182
183 Pass *BasicBlockPass::createPrinterPass(raw_ostream &OS,
184                                         const std::string &Banner) const {
185   return createPrintBasicBlockPass(OS, Banner);
186 }
187
188 bool BasicBlockPass::doInitialization(Function &) {
189   // By default, don't do anything.
190   return false;
191 }
192
193 bool BasicBlockPass::doFinalization(Function &) {
194   // By default, don't do anything.
195   return false;
196 }
197
198 static std::string getDescription(const BasicBlock &BB) {
199   return "basic block (" + BB.getName().str() + ") in function (" +
200          BB.getParent()->getName().str() + ")";
201 }
202
203 bool BasicBlockPass::skipBasicBlock(const BasicBlock &BB) const {
204   const Function *F = BB.getParent();
205   if (!F)
206     return false;
207   OptPassGate &Gate = F->getContext().getOptPassGate();
208   if (Gate.isEnabled() && !Gate.shouldRunPass(this, getDescription(BB)))
209     return true;
210   if (F->hasOptNone()) {
211     // Report this only once per function.
212     if (&BB == &F->getEntryBlock())
213       LLVM_DEBUG(dbgs() << "Skipping pass '" << getPassName()
214                         << "' on function " << F->getName() << "\n");
215     return true;
216   }
217   return false;
218 }
219
220 PassManagerType BasicBlockPass::getPotentialPassManagerType() const {
221   return PMT_BasicBlockPassManager;
222 }
223
224 const PassInfo *Pass::lookupPassInfo(const void *TI) {
225   return PassRegistry::getPassRegistry()->getPassInfo(TI);
226 }
227
228 const PassInfo *Pass::lookupPassInfo(StringRef Arg) {
229   return PassRegistry::getPassRegistry()->getPassInfo(Arg);
230 }
231
232 Pass *Pass::createPass(AnalysisID ID) {
233   const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
234   if (!PI)
235     return nullptr;
236   return PI->createPass();
237 }
238
239 //===----------------------------------------------------------------------===//
240 //                  Analysis Group Implementation Code
241 //===----------------------------------------------------------------------===//
242
243 // RegisterAGBase implementation
244
245 RegisterAGBase::RegisterAGBase(StringRef Name, const void *InterfaceID,
246                                const void *PassID, bool isDefault)
247     : PassInfo(Name, InterfaceID) {
248   PassRegistry::getPassRegistry()->registerAnalysisGroup(InterfaceID, PassID,
249                                                          *this, isDefault);
250 }
251
252 //===----------------------------------------------------------------------===//
253 // PassRegistrationListener implementation
254 //
255
256 // enumeratePasses - Iterate over the registered passes, calling the
257 // passEnumerate callback on each PassInfo object.
258 void PassRegistrationListener::enumeratePasses() {
259   PassRegistry::getPassRegistry()->enumerateWith(this);
260 }
261
262 PassNameParser::PassNameParser(cl::Option &O)
263     : cl::parser<const PassInfo *>(O) {
264   PassRegistry::getPassRegistry()->addRegistrationListener(this);
265 }
266
267 // This only gets called during static destruction, in which case the
268 // PassRegistry will have already been destroyed by llvm_shutdown().  So
269 // attempting to remove the registration listener is an error.
270 PassNameParser::~PassNameParser() = default;
271
272 //===----------------------------------------------------------------------===//
273 //   AnalysisUsage Class Implementation
274 //
275
276 namespace {
277
278 struct GetCFGOnlyPasses : public PassRegistrationListener {
279   using VectorType = AnalysisUsage::VectorType;
280
281   VectorType &CFGOnlyList;
282
283   GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
284
285   void passEnumerate(const PassInfo *P) override {
286     if (P->isCFGOnlyPass())
287       CFGOnlyList.push_back(P->getTypeInfo());
288   }
289 };
290
291 } // end anonymous namespace
292
293 // setPreservesCFG - This function should be called to by the pass, iff they do
294 // not:
295 //
296 //  1. Add or remove basic blocks from the function
297 //  2. Modify terminator instructions in any way.
298 //
299 // This function annotates the AnalysisUsage info object to say that analyses
300 // that only depend on the CFG are preserved by this pass.
301 void AnalysisUsage::setPreservesCFG() {
302   // Since this transformation doesn't modify the CFG, it preserves all analyses
303   // that only depend on the CFG (like dominators, loop info, etc...)
304   GetCFGOnlyPasses(Preserved).enumeratePasses();
305 }
306
307 AnalysisUsage &AnalysisUsage::addPreserved(StringRef Arg) {
308   const PassInfo *PI = Pass::lookupPassInfo(Arg);
309   // If the pass exists, preserve it. Otherwise silently do nothing.
310   if (PI) Preserved.push_back(PI->getTypeInfo());
311   return *this;
312 }
313
314 AnalysisUsage &AnalysisUsage::addRequiredID(const void *ID) {
315   Required.push_back(ID);
316   return *this;
317 }
318
319 AnalysisUsage &AnalysisUsage::addRequiredID(char &ID) {
320   Required.push_back(&ID);
321   return *this;
322 }
323
324 AnalysisUsage &AnalysisUsage::addRequiredTransitiveID(char &ID) {
325   Required.push_back(&ID);
326   RequiredTransitive.push_back(&ID);
327   return *this;
328 }