LLVM 23.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.
44 delete Resolver;
45}
46
47// Force out-of-line virtual method.
48ModulePass::~ModulePass() = default;
49
51 const std::string &Banner) const {
52 return createPrintModulePass(OS, Banner);
53}
54
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, getDescription(M));
71}
72
73bool Pass::mustPreserveAnalysisID(char &AID) const {
74 return Resolver->getAnalysisIfAvailable(&AID) != nullptr;
75}
76
77// dumpPassStructure - Implement the -debug-pass=Structure option
78void Pass::dumpPassStructure(unsigned Offset) {
79 dbgs().indent(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.
86 AnalysisID AID = getPassID();
87 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(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
96 AnalysisID AID = getPassID();
97 const PassInfo *PI = Pass::lookupPassInfo(AID);
98 if (PI)
99 return PI->getPassArgument();
100 return "";
101}
102
104 // By default, don't do anything.
105}
106
108 // Default implementation.
109 return PMT_Unknown;
110}
111
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
125 return nullptr;
126}
127
129 return nullptr;
130}
131
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.
166
168 // By default, don't do anything.
169}
170
171//===----------------------------------------------------------------------===//
172// FunctionPass Implementation
173//
174
176 const std::string &Banner) const {
177 return createPrintFunctionPass(OS, Banner);
178}
179
181 F.print(OS);
182 return true;
183}
184
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, 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
216 return PassRegistry::getPassRegistry()->getPassInfo(Arg);
217}
218
219Pass *Pass::createPass(AnalysisID ID) {
220 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(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.
233 PassRegistry::getPassRegistry()->enumerateWith(this);
234}
235
237 : cl::parser<const PassInfo *>(O) {
238 PassRegistry::getPassRegistry()->addRegistrationListener(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.
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(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.
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
282 const PassInfo *PI = Pass::lookupPassInfo(Arg);
283 // If the pass exists, preserve it. Otherwise silently do nothing.
284 if (PI)
285 pushUnique(Preserved, PI->getTypeInfo());
286 return *this;
287}
288
290 pushUnique(Required, ID);
291 return *this;
292}
293
295 pushUnique(Required, &ID);
296 return *this;
297}
298
300 pushUnique(Required, &ID);
301 pushUnique(RequiredTransitive, &ID);
302 return *this;
303}
304
305#ifndef NDEBUG
306const char *llvm::to_string(ThinOrFullLTOPhase Phase) {
307 switch (Phase) {
309 return "None";
311 return "ThinLTOPreLink";
313 return "ThinLTOPostLink";
315 return "FullLTOPreLink";
317 return "FullLTOPostLink";
318 }
319 llvm_unreachable("invalid phase");
320}
321#endif
aarch64 falkor hwpf fix Falkor HW Prefetch Fix Late Phase
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:663
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.
static std::string getDescription(const Loop &L)
Definition LoopPass.cpp:365
#define F(x, y, z)
Definition MD5.cpp:54
print mir2vec MIR2Vec Vocabulary Printer Pass
Definition MIR2Vec.cpp:598
Machine Check Debug Module
This file declares the interface for bisecting optimizations.
#define P(N)
#define LLVM_DEBUG(...)
Definition Debug.h:119
static const char PassName[]
AnalysisResolver - Simple interface used by Pass objects to pull all analysis information out of pass...
Represent the analysis usage information of a pass.
LLVM_ABI AnalysisUsage & addRequiredID(const void *ID)
Definition Pass.cpp:289
LLVM_ABI AnalysisUsage & addRequiredTransitiveID(char &ID)
Definition Pass.cpp:299
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:275
PassManagerType getPotentialPassManagerType() const override
Return what kind of Pass Manager can manage this pass.
Definition Pass.cpp:185
Pass * createPrinterPass(raw_ostream &OS, const std::string &Banner) const override
createPrinterPass - Get a function printer pass.
Definition Pass.cpp:175
virtual bool printIRUnit(raw_ostream &OS, Function &F)
For –print-changed, serialize the IR unit this pass operates on.
Definition Pass.cpp:180
bool skipFunction(const Function &F) const
Optional passes call this function to check whether the pass should be skipped.
Definition Pass.cpp:193
ImmutablePass class - This class is used to provide information that does not need to be run.
Definition Pass.h:285
~ImmutablePass() override
virtual void initializePass()
initializePass - This method may be overriden by immutable passes to allow them to perform various in...
Definition Pass.cpp:167
PassManagerType getPotentialPassManagerType() const override
Return what kind of Pass Manager can manage this pass.
Definition Pass.cpp:55
bool skipModule(const Module &M) const
Optional passes call this function to check whether the pass should be skipped.
Definition Pass.cpp:63
~ModulePass() override
Pass * createPrinterPass(raw_ostream &OS, const std::string &Banner) const override
createPrinterPass - Get a module printer pass.
Definition Pass.cpp:50
Extensions to this class implement mechanisms to disable passes and individual optimizations at compi...
Definition OptBisect.h:26
virtual bool isEnabled() const
isEnabled() should return true before calling shouldRunPass().
Definition OptBisect.h:38
virtual bool shouldRunPass(StringRef PassName, StringRef IRDescription) const
IRDescription is a textual description of the IR unit the pass is running over.
Definition OptBisect.h:32
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:29
StringRef getPassArgument() const
getPassArgument - Return the command line option that may be passed to 'opt' that will cause this pas...
Definition PassInfo.h:58
StringRef getPassName() const
getPassName - Return the friendly name for the pass, never returns null
Definition PassInfo.h:53
Pass * createPass() const
createPass() - Use this method to create an instance of this pass.
Definition PassInfo.h:84
const void * getTypeInfo() const
getTypeInfo - Return the id object for the pass... TODO : Rename
Definition PassInfo.h:62
PassNameParser(cl::Option &O)
Definition Pass.cpp:236
~PassNameParser() override
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
virtual PassManagerType getPotentialPassManagerType() const
Return what kind of Pass Manager can manage this pass.
Definition Pass.cpp:107
virtual void print(raw_ostream &OS, const Module *M) const
print - Print out the internal state of the pass.
Definition Pass.cpp:140
bool mustPreserveAnalysisID(char &AID) const
mustPreserveAnalysisID - This method serves the same function as getAnalysisIfAvailable,...
Definition Pass.cpp:73
void dump() const
Definition Pass.cpp:146
void setResolver(AnalysisResolver *AR)
Definition Pass.cpp:132
static Pass * createPass(AnalysisID ID)
Definition Pass.cpp:219
virtual PMDataManager * getAsPMDataManager()
Definition Pass.cpp:128
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition Pass.cpp:112
virtual void preparePassManager(PMStack &)
Check if available pass managers are suitable for this pass or not.
Definition Pass.cpp:103
static const PassInfo * lookupPassInfo(const void *TI)
Definition Pass.cpp:211
virtual ~Pass()
Definition Pass.cpp:43
virtual void verifyAnalysis() const
verifyAnalysis() - This member can be implemented by a analysis pass to check state of analysis infor...
Definition Pass.cpp:120
virtual void dumpPassStructure(unsigned Offset=0)
Definition Pass.cpp:78
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition Pass.cpp:85
virtual ImmutablePass * getAsImmutablePass()
Definition Pass.cpp:124
virtual void releaseMemory()
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
Definition Pass.cpp:116
StringRef getPassArgument() const
Return a nice clean name for a pass corresponding to that used to enable the pass in opt.
Definition Pass.cpp:95
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition Record.h:2202
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
This namespace contains all of the command line option processing machinery.
Definition MCSchedule.h:35
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:558
PassManagerType
Different types of internal pass managers.
Definition Pass.h:56
@ PMT_Unknown
Definition Pass.h:57
@ PMT_ModulePassManager
MPPassManager.
Definition Pass.h:58
@ PMT_FunctionPassManager
FPPassManager.
Definition Pass.h:60
@ FullLTOPreLink
Full LTO prelink phase.
Definition Pass.h:85
@ ThinLTOPostLink
ThinLTO postlink (backend compile) phase.
Definition Pass.h:83
@ None
No LTO/ThinLTO behavior needed.
Definition Pass.h:79
@ FullLTOPostLink
Full LTO postlink (backend compile) phase.
Definition Pass.h:87
@ ThinLTOPreLink
ThinLTO prelink (summary) phase.
Definition Pass.h:81
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
const char * to_string(ThinOrFullLTOPhase Phase)
Definition Pass.cpp:306
LLVM_ABI 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.
LLVM_ABI 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.
LLVM_ABI 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...
LLVM_ABI void enumeratePasses()
enumeratePasses - Iterate over the registered passes, calling the passEnumerate callback on each Pass...
Definition Pass.cpp:232