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