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