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