LLVM 24.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/IR/PrintPasses.h"
24#include "llvm/PassInfo.h"
25#include "llvm/PassRegistry.h"
27#include "llvm/Support/Debug.h"
29#include <cassert>
30
31#ifdef EXPENSIVE_CHECKS
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.
45 delete Resolver;
46}
47
48// Force out-of-line virtual method.
49ModulePass::~ModulePass() = default;
50
52 const std::string &Banner) const {
53 return createPrintModulePass(OS, Banner);
54}
55
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, getDescription(M));
72}
73
74bool Pass::mustPreserveAnalysisID(char &AID) const {
75 return Resolver->getAnalysisIfAvailable(&AID) != nullptr;
76}
77
78// dumpPassStructure - Implement the -debug-pass=Structure option
79void Pass::dumpPassStructure(unsigned Offset) {
80 dbgs().indent(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.
87 AnalysisID AID = getPassID();
88 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(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
97 AnalysisID AID = getPassID();
98 const PassInfo *PI = Pass::lookupPassInfo(AID);
99 if (PI)
100 return PI->getPassArgument();
101 return "";
102}
103
105 // By default, don't do anything.
106}
107
109 // Default implementation.
110 return PMT_Unknown;
111}
112
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
126 return nullptr;
127}
128
130 return nullptr;
131}
132
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.
167
169 // By default, don't do anything.
170}
171
172//===----------------------------------------------------------------------===//
173// FunctionPass Implementation
174//
175
177 const std::string &Banner) const {
178 return createPrintFunctionPass(OS, Banner);
179}
180
183 return false;
184 F.print(OS);
185 return true;
186}
187
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, 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
219 return PassRegistry::getPassRegistry()->getPassInfo(Arg);
220}
221
222Pass *Pass::createPass(AnalysisID ID) {
223 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(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.
236 PassRegistry::getPassRegistry()->enumerateWith(this);
237}
238
240 : cl::parser<const PassInfo *>(O) {
241 PassRegistry::getPassRegistry()->addRegistrationListener(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.
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(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.
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
285 const PassInfo *PI = Pass::lookupPassInfo(Arg);
286 // If the pass exists, preserve it. Otherwise silently do nothing.
287 if (PI)
288 pushUnique(Preserved, PI->getTypeInfo());
289 return *this;
290}
291
293 pushUnique(Required, ID);
294 return *this;
295}
296
298 pushUnique(Required, &ID);
299 return *this;
300}
301
303 pushUnique(Required, &ID);
304 pushUnique(RequiredTransitive, &ID);
305 return *this;
306}
307
308#ifndef NDEBUG
309const char *llvm::to_string(ThinOrFullLTOPhase Phase) {
310 switch (Phase) {
312 return "None";
314 return "ThinLTOPreLink";
316 return "ThinLTOPostLink";
318 return "FullLTOPreLink";
320 return "FullLTOPostLink";
321 }
322 llvm_unreachable("invalid phase");
323}
324#endif
aarch64 falkor hwpf fix Falkor HW Prefetch Fix Late Phase
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
unsigned uint64_t
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:678
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:378
#define F(x, y, z)
Definition MD5.cpp:54
print mir2vec MIR2Vec Vocabulary Printer Pass
Definition MIR2Vec.cpp:621
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:292
LLVM_ABI AnalysisUsage & addRequiredTransitiveID(char &ID)
Definition Pass.cpp:302
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:278
PassManagerType getPotentialPassManagerType() const override
Return what kind of Pass Manager can manage this pass.
Definition Pass.cpp:188
Pass * createPrinterPass(raw_ostream &OS, const std::string &Banner) const override
createPrinterPass - Get a function printer pass.
Definition Pass.cpp:176
virtual bool printIRUnit(raw_ostream &OS, Function &F)
For –print-changed, serialize the IR unit this pass operates on.
Definition Pass.cpp:181
bool skipFunction(const Function &F) const
Optional passes call this function to check whether the pass should be skipped.
Definition Pass.cpp:196
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:168
PassManagerType getPotentialPassManagerType() const override
Return what kind of Pass Manager can manage this pass.
Definition Pass.cpp:56
bool skipModule(const Module &M) const
Optional passes call this function to check whether the pass should be skipped.
Definition Pass.cpp:64
~ModulePass() override
Pass * createPrinterPass(raw_ostream &OS, const std::string &Banner) const override
createPrinterPass - Get a module printer pass.
Definition Pass.cpp:51
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:239
~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:108
virtual void print(raw_ostream &OS, const Module *M) const
print - Print out the internal state of the pass.
Definition Pass.cpp:141
bool mustPreserveAnalysisID(char &AID) const
mustPreserveAnalysisID - This method serves the same function as getAnalysisIfAvailable,...
Definition Pass.cpp:74
void dump() const
Definition Pass.cpp:147
void setResolver(AnalysisResolver *AR)
Definition Pass.cpp:133
static Pass * createPass(AnalysisID ID)
Definition Pass.cpp:222
virtual PMDataManager * getAsPMDataManager()
Definition Pass.cpp:129
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition Pass.cpp:113
virtual void preparePassManager(PMStack &)
Check if available pass managers are suitable for this pass or not.
Definition Pass.cpp:104
static const PassInfo * lookupPassInfo(const void *TI)
Definition Pass.cpp:214
virtual ~Pass()
Definition Pass.cpp:44
virtual void verifyAnalysis() const
verifyAnalysis() - This member can be implemented by a analysis pass to check state of analysis infor...
Definition Pass.cpp:121
virtual void dumpPassStructure(unsigned Offset=0)
Definition Pass.cpp:79
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition Pass.cpp:86
virtual ImmutablePass * getAsImmutablePass()
Definition Pass.cpp:125
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:117
StringRef getPassArgument() const
Return a nice clean name for a pass corresponding to that used to enable the pass in opt.
Definition Pass.cpp:96
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition Record.h:2233
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.
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:577
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
LLVM_ABI bool shouldPrintFunction(const Function &F)
const char * to_string(ThinOrFullLTOPhase Phase)
Definition Pass.cpp:309
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:235