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