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
30 #ifdef EXPENSIVE_CHECKS
31 #include "llvm/IR/StructuralHash.h"
32 #endif
33
34 using namespace llvm;
35
36 #define DEBUG_TYPE "ir"
37
38 //===----------------------------------------------------------------------===//
39 // Pass Implementation
40 //
41
42 // Force out-of-line virtual method.
~Pass()43 Pass::~Pass() {
44 delete Resolver;
45 }
46
47 // Force out-of-line virtual method.
48 ModulePass::~ModulePass() = default;
49
createPrinterPass(raw_ostream & OS,const std::string & Banner) const50 Pass *ModulePass::createPrinterPass(raw_ostream &OS,
51 const std::string &Banner) const {
52 return createPrintModulePass(OS, Banner);
53 }
54
getPotentialPassManagerType() const55 PassManagerType ModulePass::getPotentialPassManagerType() const {
56 return PMT_ModulePassManager;
57 }
58
getDescription(const Module & M)59 static std::string getDescription(const Module &M) {
60 return "module (" + M.getName().str() + ")";
61 }
62
skipModule(Module & M) const63 bool ModulePass::skipModule(Module &M) const {
64 OptPassGate &Gate = M.getContext().getOptPassGate();
65 return Gate.isEnabled() && !Gate.shouldRunPass(this, getDescription(M));
66 }
67
mustPreserveAnalysisID(char & AID) const68 bool Pass::mustPreserveAnalysisID(char &AID) const {
69 return Resolver->getAnalysisIfAvailable(&AID) != nullptr;
70 }
71
72 // dumpPassStructure - Implement the -debug-pass=Structure option
dumpPassStructure(unsigned Offset)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.
getPassName() const80 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
preparePassManager(PMStack &)88 void Pass::preparePassManager(PMStack &) {
89 // By default, don't do anything.
90 }
91
getPotentialPassManagerType() const92 PassManagerType Pass::getPotentialPassManagerType() const {
93 // Default implementation.
94 return PMT_Unknown;
95 }
96
getAnalysisUsage(AnalysisUsage &) const97 void Pass::getAnalysisUsage(AnalysisUsage &) const {
98 // By default, no analysis results are used, all are invalidated.
99 }
100
releaseMemory()101 void Pass::releaseMemory() {
102 // By default, don't do anything.
103 }
104
verifyAnalysis() const105 void Pass::verifyAnalysis() const {
106 // By default, don't do anything.
107 }
108
getAdjustedAnalysisPointer(AnalysisID AID)109 void *Pass::getAdjustedAnalysisPointer(AnalysisID AID) {
110 return this;
111 }
112
getAsImmutablePass()113 ImmutablePass *Pass::getAsImmutablePass() {
114 return nullptr;
115 }
116
getAsPMDataManager()117 PMDataManager *Pass::getAsPMDataManager() {
118 return nullptr;
119 }
120
setResolver(AnalysisResolver * AR)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.
print(raw_ostream & OS,const Module *) const129 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);
dump() const135 LLVM_DUMP_METHOD void Pass::dump() const {
136 print(dbgs(), nullptr);
137 }
138 #endif
139
140 #ifdef EXPENSIVE_CHECKS
structuralHash(Module & M) const141 uint64_t Pass::structuralHash(Module &M) const { return StructuralHash(M); }
142
structuralHash(Function & F) const143 uint64_t Pass::structuralHash(Function &F) const { return StructuralHash(F); }
144 #endif
145
146 //===----------------------------------------------------------------------===//
147 // ImmutablePass Implementation
148 //
149 // Force out-of-line virtual method.
150 ImmutablePass::~ImmutablePass() = default;
151
initializePass()152 void ImmutablePass::initializePass() {
153 // By default, don't do anything.
154 }
155
156 //===----------------------------------------------------------------------===//
157 // FunctionPass Implementation
158 //
159
createPrinterPass(raw_ostream & OS,const std::string & Banner) const160 Pass *FunctionPass::createPrinterPass(raw_ostream &OS,
161 const std::string &Banner) const {
162 return createPrintFunctionPass(OS, Banner);
163 }
164
getPotentialPassManagerType() const165 PassManagerType FunctionPass::getPotentialPassManagerType() const {
166 return PMT_FunctionPassManager;
167 }
168
getDescription(const Function & F)169 static std::string getDescription(const Function &F) {
170 return "function (" + F.getName().str() + ")";
171 }
172
skipFunction(const Function & F) const173 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
lookupPassInfo(const void * TI)186 const PassInfo *Pass::lookupPassInfo(const void *TI) {
187 return PassRegistry::getPassRegistry()->getPassInfo(TI);
188 }
189
lookupPassInfo(StringRef Arg)190 const PassInfo *Pass::lookupPassInfo(StringRef Arg) {
191 return PassRegistry::getPassRegistry()->getPassInfo(Arg);
192 }
193
createPass(AnalysisID ID)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
RegisterAGBase(StringRef Name,const void * InterfaceID,const void * PassID,bool isDefault)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.
enumeratePasses()220 void PassRegistrationListener::enumeratePasses() {
221 PassRegistry::getPassRegistry()->enumerateWith(this);
222 }
223
PassNameParser(cl::Option & O)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
GetCFGOnlyPasses__anon4583b1410111::GetCFGOnlyPasses245 GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
246
passEnumerate__anon4583b1410111::GetCFGOnlyPasses247 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.
setPreservesCFG()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
addPreserved(StringRef Arg)269 AnalysisUsage &AnalysisUsage::addPreserved(StringRef Arg) {
270 const PassInfo *PI = Pass::lookupPassInfo(Arg);
271 // If the pass exists, preserve it. Otherwise silently do nothing.
272 if (PI)
273 pushUnique(Preserved, PI->getTypeInfo());
274 return *this;
275 }
276
addRequiredID(const void * ID)277 AnalysisUsage &AnalysisUsage::addRequiredID(const void *ID) {
278 pushUnique(Required, ID);
279 return *this;
280 }
281
addRequiredID(char & ID)282 AnalysisUsage &AnalysisUsage::addRequiredID(char &ID) {
283 pushUnique(Required, &ID);
284 return *this;
285 }
286
addRequiredTransitiveID(char & ID)287 AnalysisUsage &AnalysisUsage::addRequiredTransitiveID(char &ID) {
288 pushUnique(Required, &ID);
289 pushUnique(RequiredTransitive, &ID);
290 return *this;
291 }
292