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