annotate llvm/lib/IR/Pass.cpp @ 239:173fe712db74

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