LLVM  15.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"
25 #include "llvm/Support/Compiler.h"
26 #include "llvm/Support/Debug.h"
28 #include <cassert>
29 
30 #ifdef EXPENSIVE_CHECKS
31 #include "llvm/IR/StructuralHash.h"
32 #endif
33 
34 using 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.
48 ModulePass::~ModulePass() = default;
49 
51  const std::string &Banner) const {
52  return createPrintModulePass(OS, Banner);
53 }
54 
56  return PMT_ModulePassManager;
57 }
58 
59 static std::string getDescription(const Module &M) {
60  return "module (" + M.getName().str() + ")";
61 }
62 
64  OptPassGate &Gate = M.getContext().getOptPassGate();
65  return Gate.isEnabled() && !Gate.shouldRunPass(this, getDescription(M));
66 }
67 
68 bool Pass::mustPreserveAnalysisID(char &AID) const {
69  return Resolver->getAnalysisIfAvailable(&AID) != nullptr;
70 }
71 
72 // dumpPassStructure - Implement the -debug-pass=Structure option
73 void Pass::dumpPassStructure(unsigned Offset) {
74  dbgs().indent(Offset*2) << getPassName() << "\n";
75 }
76 
77 /// getPassName - Return a nice clean name for a pass. This usually
78 /// implemented in terms of the name that is registered by one of the
79 /// Registration templates, but can be overloaded directly.
81  AnalysisID AID = getPassID();
83  if (PI)
84  return PI->getPassName();
85  return "Unnamed pass: implement Pass::getPassName()";
86 }
87 
89  // By default, don't do anything.
90 }
91 
93  // Default implementation.
94  return PMT_Unknown;
95 }
96 
98  // By default, no analysis results are used, all are invalidated.
99 }
100 
102  // By default, don't do anything.
103 }
104 
105 void Pass::verifyAnalysis() const {
106  // By default, don't do anything.
107 }
108 
110  return this;
111 }
112 
114  return nullptr;
115 }
116 
118  return nullptr;
119 }
120 
122  assert(!Resolver && "Resolver is already set");
123  Resolver = AR;
124 }
125 
126 // print - Print out the internal state of the pass. This is called by Analyze
127 // to print out the contents of an analysis. Otherwise it is not necessary to
128 // implement this method.
129 void Pass::print(raw_ostream &OS, const Module *) const {
130  OS << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
131 }
132 
133 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
134 // dump - call print(cerr);
136  print(dbgs(), nullptr);
137 }
138 #endif
139 
140 #ifdef EXPENSIVE_CHECKS
141 uint64_t Pass::structuralHash(Module &M) const { return StructuralHash(M); }
142 
143 uint64_t Pass::structuralHash(Function &F) const { return StructuralHash(F); }
144 #endif
145 
146 //===----------------------------------------------------------------------===//
147 // ImmutablePass Implementation
148 //
149 // Force out-of-line virtual method.
151 
153  // By default, don't do anything.
154 }
155 
156 //===----------------------------------------------------------------------===//
157 // FunctionPass Implementation
158 //
159 
161  const std::string &Banner) const {
162  return createPrintFunctionPass(OS, Banner);
163 }
164 
167 }
168 
169 static std::string getDescription(const Function &F) {
170  return "function (" + F.getName().str() + ")";
171 }
172 
174  OptPassGate &Gate = F.getContext().getOptPassGate();
175  if (Gate.isEnabled() && !Gate.shouldRunPass(this, getDescription(F)))
176  return true;
177 
178  if (F.hasOptNone()) {
179  LLVM_DEBUG(dbgs() << "Skipping pass '" << getPassName() << "' on function "
180  << F.getName() << "\n");
181  return true;
182  }
183  return false;
184 }
185 
186 const PassInfo *Pass::lookupPassInfo(const void *TI) {
188 }
189 
192 }
193 
196  if (!PI)
197  return nullptr;
198  return PI->createPass();
199 }
200 
201 //===----------------------------------------------------------------------===//
202 // Analysis Group Implementation Code
203 //===----------------------------------------------------------------------===//
204 
205 // RegisterAGBase implementation
206 
208  const void *PassID, bool isDefault)
209  : PassInfo(Name, InterfaceID) {
211  *this, isDefault);
212 }
213 
214 //===----------------------------------------------------------------------===//
215 // PassRegistrationListener implementation
216 //
217 
218 // enumeratePasses - Iterate over the registered passes, calling the
219 // passEnumerate callback on each PassInfo object.
222 }
223 
225  : cl::parser<const PassInfo *>(O) {
227 }
228 
229 // This only gets called during static destruction, in which case the
230 // PassRegistry will have already been destroyed by llvm_shutdown(). So
231 // attempting to remove the registration listener is an error.
233 
234 //===----------------------------------------------------------------------===//
235 // AnalysisUsage Class Implementation
236 //
237 
238 namespace {
239 
240 struct GetCFGOnlyPasses : public PassRegistrationListener {
242 
243  VectorType &CFGOnlyList;
244 
245  GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
246 
247  void passEnumerate(const PassInfo *P) override {
248  if (P->isCFGOnlyPass())
249  CFGOnlyList.push_back(P->getTypeInfo());
250  }
251 };
252 
253 } // end anonymous namespace
254 
255 // setPreservesCFG - This function should be called to by the pass, iff they do
256 // not:
257 //
258 // 1. Add or remove basic blocks from the function
259 // 2. Modify terminator instructions in any way.
260 //
261 // This function annotates the AnalysisUsage info object to say that analyses
262 // that only depend on the CFG are preserved by this pass.
264  // Since this transformation doesn't modify the CFG, it preserves all analyses
265  // that only depend on the CFG (like dominators, loop info, etc...)
266  GetCFGOnlyPasses(Preserved).enumeratePasses();
267 }
268 
270  const PassInfo *PI = Pass::lookupPassInfo(Arg);
271  // If the pass exists, preserve it. Otherwise silently do nothing.
272  if (PI)
273  pushUnique(Preserved, PI->getTypeInfo());
274  return *this;
275 }
276 
278  pushUnique(Required, ID);
279  return *this;
280 }
281 
283  pushUnique(Required, &ID);
284  return *this;
285 }
286 
288  pushUnique(Required, &ID);
289  pushUnique(RequiredTransitive, &ID);
290  return *this;
291 }
llvm::Pass::verifyAnalysis
virtual void verifyAnalysis() const
verifyAnalysis() - This member can be implemented by a analysis pass to check state of analysis infor...
Definition: Pass.cpp:105
llvm::PassInfo::getTypeInfo
const void * getTypeInfo() const
getTypeInfo - Return the id object for the pass...
Definition: PassInfo.h:71
LLVM_DUMP_METHOD
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:494
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::ImmutablePass
ImmutablePass class - This class is used to provide information that does not need to be run.
Definition: Pass.h:279
llvm::Function
Definition: Function.h:60
llvm::Pass::getPassID
AnalysisID getPassID() const
getPassID - Return the PassID number that corresponds to this pass.
Definition: Pass.h:110
P
This currently compiles esp xmm0 movsd esp eax eax esp ret We should use not the dag combiner This is because dagcombine2 needs to be able to see through the X86ISD::Wrapper which DAGCombine can t really do The code for turning x load into a single vector load is target independent and should be moved to the dag combiner The code for turning x load into a vector load can only handle a direct load from a global or a direct load from the stack It should be generalized to handle any load from P
Definition: README-SSE.txt:411
Pass.h
llvm::PassRegistry::enumerateWith
void enumerateWith(PassRegistrationListener *L)
enumerateWith - Enumerate the registered passes, calling the provided PassRegistrationListener's pass...
Definition: PassRegistry.cpp:71
llvm::FunctionPass::skipFunction
bool skipFunction(const Function &F) const
Optional passes call this function to check whether the pass should be skipped.
Definition: Pass.cpp:173
llvm::OptPassGate::isEnabled
virtual bool isEnabled() const
isEnabled() should return true before calling shouldRunPass().
Definition: OptBisect.h:38
llvm::AnalysisID
const void * AnalysisID
Definition: Pass.h:47
llvm::PassManagerType
PassManagerType
Different types of internal pass managers.
Definition: Pass.h:52
llvm::Pass::createPass
static Pass * createPass(AnalysisID ID)
Definition: Pass.cpp:194
Module.h
llvm::PassInfo
PassInfo class - An instance of this class exists for every pass known by the system,...
Definition: PassInfo.h:30
llvm::PMT_Unknown
@ PMT_Unknown
Definition: Pass.h:53
OptBisect.h
LegacyPassNameParser.h
llvm::PassInfo::createPass
Pass * createPass() const
createPass() - Use this method to create an instance of this pass.
Definition: PassInfo.h:96
PassRegistry.h
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:101
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::PMT_ModulePassManager
@ PMT_ModulePassManager
MPPassManager.
Definition: Pass.h:54
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
llvm::Pass::getAdjustedAnalysisPointer
virtual void * getAdjustedAnalysisPointer(AnalysisID ID)
getAdjustedAnalysisPointer - This method is used when a pass implements an analysis interface through...
Definition: Pass.cpp:109
llvm::Resolver
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition: Record.h:2052
Arg
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
Definition: AMDGPULibCalls.cpp:186
llvm::PMDataManager
PMDataManager provides the common place to manage the analysis data used by pass managers.
Definition: LegacyPassManagers.h:295
llvm::Pass::preparePassManager
virtual void preparePassManager(PMStack &)
Check if available pass managers are suitable for this pass or not.
Definition: Pass.cpp:88
llvm::AnalysisUsage::addRequiredTransitiveID
AnalysisUsage & addRequiredTransitiveID(char &ID)
Definition: Pass.cpp:287
llvm::Pass::lookupPassInfo
static const PassInfo * lookupPassInfo(const void *TI)
Definition: Pass.cpp:186
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:31
llvm::PassInfo::getPassName
StringRef getPassName() const
getPassName - Return the friendly name for the pass, never returns null
Definition: PassInfo.h:62
llvm::ModulePass::~ModulePass
~ModulePass() override
llvm::PassRegistrationListener::enumeratePasses
void enumeratePasses()
enumeratePasses - Iterate over the registered passes, calling the passEnumerate callback on each Pass...
Definition: Pass.cpp:220
llvm::FunctionPass::getPotentialPassManagerType
PassManagerType getPotentialPassManagerType() const override
Return what kind of Pass Manager can manage this pass.
Definition: Pass.cpp:165
llvm::OptPassGate
Extensions to this class implement mechanisms to disable passes and individual optimizations at compi...
Definition: OptBisect.h:27
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
llvm::PassNameParser::PassNameParser
PassNameParser(cl::Option &O)
Definition: Pass.cpp:224
llvm::AnalysisResolver
AnalysisResolver - Simple interface used by Pass objects to pull all analysis information out of pass...
Definition: PassAnalysisSupport.h:157
IRPrintingPasses.h
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:54
llvm::Pass::getAsImmutablePass
virtual ImmutablePass * getAsImmutablePass()
Definition: Pass.cpp:113
llvm::FunctionPass::createPrinterPass
Pass * createPrinterPass(raw_ostream &OS, const std::string &Banner) const override
createPrinterPass - Get a function printer pass.
Definition: Pass.cpp:160
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
llvm::Pass::print
virtual void print(raw_ostream &OS, const Module *M) const
print - Print out the internal state of the pass.
Definition: Pass.cpp:129
StructuralHash.h
llvm::VectorType
Base class of all SIMD vector types.
Definition: DerivedTypes.h:389
llvm::Pass::~Pass
virtual ~Pass()
Definition: Pass.cpp:43
llvm::RegisterAGBase::RegisterAGBase
RegisterAGBase(StringRef Name, const void *InterfaceID, const void *PassID=nullptr, bool isDefault=false)
Definition: Pass.cpp:207
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:239
llvm::cl::Option
Definition: CommandLine.h:242
uint64_t
const
aarch64 promote const
Definition: AArch64PromoteConstant.cpp:232
llvm::ModulePass::createPrinterPass
Pass * createPrinterPass(raw_ostream &OS, const std::string &Banner) const override
createPrinterPass - Get a module printer pass.
Definition: Pass.cpp:50
llvm::PassRegistrationListener
PassRegistrationListener class - This class is meant to be derived from by clients that are intereste...
Definition: PassSupport.h:200
llvm::ModulePass::skipModule
bool skipModule(Module &M) const
Optional passes call this function to check whether the pass should be skipped.
Definition: Pass.cpp:63
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
llvm::Pass::mustPreserveAnalysisID
bool mustPreserveAnalysisID(char &AID) const
mustPreserveAnalysisID - This method serves the same function as getAnalysisIfAvailable,...
Definition: Pass.cpp:68
getDescription
static std::string getDescription(const Module &M)
Definition: Pass.cpp:59
llvm::Pass::releaseMemory
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:101
llvm::AnalysisUsage::setPreservesCFG
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:263
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::Pass::getPassName
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:80
llvm::PMT_FunctionPassManager
@ PMT_FunctionPassManager
FPPassManager.
Definition: Pass.h:56
cl
http eax xorl edx cl sete al setne dl sall cl
Definition: README.txt:25
llvm::AnalysisUsage::VectorType
SmallVectorImpl< AnalysisID > VectorType
Definition: PassAnalysisSupport.h:49
llvm::ModulePass::getPotentialPassManagerType
PassManagerType getPotentialPassManagerType() const override
Return what kind of Pass Manager can manage this pass.
Definition: Pass.cpp:55
llvm::AnalysisUsage::addPreserved
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
Definition: PassAnalysisSupport.h:98
llvm::PassRegistry::addRegistrationListener
void addRegistrationListener(PassRegistrationListener *L)
addRegistrationListener - Register the given PassRegistrationListener to receive passRegistered() cal...
Definition: PassRegistry.cpp:116
Compiler.h
llvm::ImmutablePass::initializePass
virtual void initializePass()
initializePass - This method may be overriden by immutable passes to allow them to perform various in...
Definition: Pass.cpp:152
llvm::Pass::dump
void dump() const
Definition: Pass.cpp:135
llvm::Pass::getAsPMDataManager
virtual PMDataManager * getAsPMDataManager()
Definition: Pass.cpp:117
llvm::PMStack
PMStack - This class implements a stack data structure of PMDataManager pointers.
Definition: LegacyPassManagers.h:136
llvm::Pass::dumpPassStructure
virtual void dumpPassStructure(unsigned Offset=0)
Definition: Pass.cpp:73
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:50
Function.h
llvm::Pass::setResolver
void setResolver(AnalysisResolver *AR)
Definition: Pass.cpp:121
llvm::Pass::getPotentialPassManagerType
virtual PassManagerType getPotentialPassManagerType() const
Return what kind of Pass Manager can manage this pass.
Definition: Pass.cpp:92
llvm::PassNameParser::~PassNameParser
~PassNameParser() override
llvm::Pass
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:91
llvm::raw_ostream::indent
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
Definition: raw_ostream.cpp:496
llvm::ImmutablePass::~ImmutablePass
~ImmutablePass() override
llvm::Pass::getAnalysisUsage
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:97
PassInfo.h
llvm::AnalysisUsage::addRequiredID
AnalysisUsage & addRequiredID(const void *ID)
Definition: Pass.cpp:277
LLVMContext.h
llvm::PassRegistry::getPassInfo
const PassInfo * getPassInfo(const void *TI) const
getPassInfo - Look up a pass' corresponding PassInfo, indexed by the pass' type identifier (&MyPass::...
Definition: PassRegistry.cpp:41
raw_ostream.h
llvm::OptPassGate::shouldRunPass
virtual bool shouldRunPass(const Pass *P, StringRef IRDescription)
IRDescription is a textual description of the IR unit the pass is running over.
Definition: OptBisect.h:33
llvm::PassRegistry::registerAnalysisGroup
void registerAnalysisGroup(const void *InterfaceID, const void *PassID, PassInfo &Registeree, bool isDefault, bool ShouldFree=false)
registerAnalysisGroup - Register an analysis group (or a pass implementing
Definition: PassRegistry.cpp:78
Debug.h
llvm::createPrintFunctionPass
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.
Definition: IRPrintingPasses.cpp:130
llvm::createPrintModulePass
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.