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