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