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