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