annotate llvm/lib/IR/Pass.cpp @ 220:42394fc6a535

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