LLVM 20.0.0git
Pass.cpp
Go to the documentation of this file.
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"
19#include "llvm/IR/LLVMContext.h"
21#include "llvm/IR/Module.h"
22#include "llvm/IR/OptBisect.h"
23#include "llvm/PassInfo.h"
24#include "llvm/PassRegistry.h"
26#include "llvm/Support/Debug.h"
28#include <cassert>
29
30#ifdef EXPENSIVE_CHECKS
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 {
57}
58
59static std::string getDescription(const Module &M) {
60 return "module (" + M.getName().str() + ")";
61}
62
63bool ModulePass::skipModule(Module &M) const {
64 OptPassGate &Gate = M.getContext().getOptPassGate();
65 return Gate.isEnabled() &&
66 !Gate.shouldRunPass(this->getPassName(), getDescription(M));
67}
68
69bool Pass::mustPreserveAnalysisID(char &AID) const {
70 return Resolver->getAnalysisIfAvailable(&AID) != nullptr;
71}
72
73// dumpPassStructure - Implement the -debug-pass=Structure option
74void Pass::dumpPassStructure(unsigned Offset) {
75 dbgs().indent(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(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
110void *Pass::getAdjustedAnalysisPointer(AnalysisID AID) {
111 return this;
112}
113
114ImmutablePass *Pass::getAsImmutablePass() {
115 return nullptr;
116}
117
118PMDataManager *Pass::getAsPMDataManager() {
119 return nullptr;
120}
121
122void Pass::setResolver(AnalysisResolver *AR) {
123 assert(!Resolver && "Resolver is already set");
124 Resolver = AR;
125}
126
127// print - Print out the internal state of the pass. This is called by Analyze
128// to print out the contents of an analysis. Otherwise it is not necessary to
129// implement this method.
130void Pass::print(raw_ostream &OS, const Module *) const {
131 OS << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
132}
133
134#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
135// dump - call print(cerr);
136LLVM_DUMP_METHOD void Pass::dump() const {
137 print(dbgs(), nullptr);
138}
139#endif
140
141#ifdef EXPENSIVE_CHECKS
142uint64_t Pass::structuralHash(Module &M) const {
143 return StructuralHash(M, true);
144}
145
146uint64_t Pass::structuralHash(Function &F) const {
147 return StructuralHash(F, true);
148}
149#endif
150
151//===----------------------------------------------------------------------===//
152// ImmutablePass Implementation
153//
154// Force out-of-line virtual method.
155ImmutablePass::~ImmutablePass() = default;
156
157void ImmutablePass::initializePass() {
158 // By default, don't do anything.
159}
160
161//===----------------------------------------------------------------------===//
162// FunctionPass Implementation
163//
164
165Pass *FunctionPass::createPrinterPass(raw_ostream &OS,
166 const std::string &Banner) const {
167 return createPrintFunctionPass(OS, Banner);
168}
169
170PassManagerType FunctionPass::getPotentialPassManagerType() const {
172}
173
174static std::string getDescription(const Function &F) {
175 return "function (" + F.getName().str() + ")";
176}
177
178bool FunctionPass::skipFunction(const Function &F) const {
179 OptPassGate &Gate = F.getContext().getOptPassGate();
180 if (Gate.isEnabled() &&
181 !Gate.shouldRunPass(this->getPassName(), getDescription(F)))
182 return true;
183
184 if (F.hasOptNone()) {
185 LLVM_DEBUG(dbgs() << "Skipping pass '" << getPassName() << "' on function "
186 << F.getName() << "\n");
187 return true;
188 }
189 return false;
190}
191
192const PassInfo *Pass::lookupPassInfo(const void *TI) {
193 return PassRegistry::getPassRegistry()->getPassInfo(TI);
194}
195
196const PassInfo *Pass::lookupPassInfo(StringRef Arg) {
197 return PassRegistry::getPassRegistry()->getPassInfo(Arg);
198}
199
200Pass *Pass::createPass(AnalysisID ID) {
201 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
202 if (!PI)
203 return nullptr;
204 return PI->createPass();
205}
206
207//===----------------------------------------------------------------------===//
208// PassRegistrationListener implementation
209//
210
211// enumeratePasses - Iterate over the registered passes, calling the
212// passEnumerate callback on each PassInfo object.
213void PassRegistrationListener::enumeratePasses() {
214 PassRegistry::getPassRegistry()->enumerateWith(this);
215}
216
217PassNameParser::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.
225PassNameParser::~PassNameParser() = default;
226
227//===----------------------------------------------------------------------===//
228// AnalysisUsage Class Implementation
229//
230
231namespace {
232
233struct GetCFGOnlyPasses : public PassRegistrationListener {
235
236 VectorType &CFGOnlyList;
237
238 GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
239
240 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.
256void 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
262AnalysisUsage &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)
266 pushUnique(Preserved, PI->getTypeInfo());
267 return *this;
268}
269
270AnalysisUsage &AnalysisUsage::addRequiredID(const void *ID) {
271 pushUnique(Required, ID);
272 return *this;
273}
274
275AnalysisUsage &AnalysisUsage::addRequiredID(char &ID) {
276 pushUnique(Required, &ID);
277 return *this;
278}
279
280AnalysisUsage &AnalysisUsage::addRequiredTransitiveID(char &ID) {
281 pushUnique(Required, &ID);
282 pushUnique(RequiredTransitive, &ID);
283 return *this;
284}
aarch64 promote const
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
static std::string getDescription(const CallGraphSCC &SCC)
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:622
#define LLVM_DEBUG(...)
Definition: Debug.h:106
uint64_t Offset
Definition: ELF_riscv.cpp:478
This file contains an interface for creating legacy passes to print out IR in various granularities.
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition: MD5.cpp:55
This file declares the interface for bisecting optimizations.
#define P(N)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
AnalysisResolver - Simple interface used by Pass objects to pull all analysis information out of pass...
Represent the analysis usage information of a pass.
ImmutablePass class - This class is used to provide information that does not need to be run.
Definition: Pass.h:281
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
Extensions to this class implement mechanisms to disable passes and individual optimizations at compi...
Definition: OptBisect.h:24
virtual bool isEnabled() const
isEnabled() should return true before calling shouldRunPass().
Definition: OptBisect.h:36
virtual bool shouldRunPass(const StringRef PassName, StringRef IRDescription)
IRDescription is a textual description of the IR unit the pass is running over.
Definition: OptBisect.h:30
PMDataManager provides the common place to manage the analysis data used by pass managers.
PMStack - This class implements a stack data structure of PMDataManager pointers.
PassInfo class - An instance of this class exists for every pass known by the system,...
Definition: PassInfo.h:30
StringRef getPassName() const
getPassName - Return the friendly name for the pass, never returns null
Definition: PassInfo.h:54
Pass * createPass() const
createPass() - Use this method to create an instance of this pass.
Definition: PassInfo.h:85
const void * getTypeInfo() const
getTypeInfo - Return the id object for the pass... TODO : Rename
Definition: PassInfo.h:63
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:94
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition: Record.h:2203
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
PassManagerType
Different types of internal pass managers.
Definition: Pass.h:55
@ PMT_Unknown
Definition: Pass.h:56
@ PMT_ModulePassManager
MPPassManager.
Definition: Pass.h:57
@ PMT_FunctionPassManager
FPPassManager.
Definition: Pass.h:59
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
const void * AnalysisID
Definition: Pass.h:50
ModulePass * createPrintModulePass(raw_ostream &OS, const std::string &Banner="", bool ShouldPreserveUseListOrder=false)
Create and return a pass that writes the module to the specified raw_ostream.
FunctionPass * createPrintFunctionPass(raw_ostream &OS, const std::string &Banner="")
Create and return a pass that prints functions to the specified raw_ostream as they are processed.
stable_hash StructuralHash(const Function &F, bool DetailedHash=false)
Returns a hash of the function F.
PassRegistrationListener class - This class is meant to be derived from by clients that are intereste...
Definition: PassSupport.h:124