LLVM  14.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 using namespace llvm;
31 
32 #define DEBUG_TYPE "ir"
33 
34 //===----------------------------------------------------------------------===//
35 // Pass Implementation
36 //
37 
38 // Force out-of-line virtual method.
40  delete Resolver;
41 }
42 
43 // Force out-of-line virtual method.
44 ModulePass::~ModulePass() = default;
45 
47  const std::string &Banner) const {
48  return createPrintModulePass(OS, Banner);
49 }
50 
52  return PMT_ModulePassManager;
53 }
54 
55 static std::string getDescription(const Module &M) {
56  return "module (" + M.getName().str() + ")";
57 }
58 
60  OptPassGate &Gate = M.getContext().getOptPassGate();
61  return Gate.isEnabled() && !Gate.shouldRunPass(this, getDescription(M));
62 }
63 
64 bool Pass::mustPreserveAnalysisID(char &AID) const {
65  return Resolver->getAnalysisIfAvailable(&AID) != nullptr;
66 }
67 
68 // dumpPassStructure - Implement the -debug-pass=Structure option
70  dbgs().indent(Offset*2) << getPassName() << "\n";
71 }
72 
73 /// getPassName - Return a nice clean name for a pass. This usually
74 /// implemented in terms of the name that is registered by one of the
75 /// Registration templates, but can be overloaded directly.
77  AnalysisID AID = getPassID();
79  if (PI)
80  return PI->getPassName();
81  return "Unnamed pass: implement Pass::getPassName()";
82 }
83 
85  // By default, don't do anything.
86 }
87 
89  // Default implementation.
90  return PMT_Unknown;
91 }
92 
94  // By default, no analysis results are used, all are invalidated.
95 }
96 
98  // By default, don't do anything.
99 }
100 
101 void Pass::verifyAnalysis() const {
102  // By default, don't do anything.
103 }
104 
106  return this;
107 }
108 
110  return nullptr;
111 }
112 
114  return nullptr;
115 }
116 
118  assert(!Resolver && "Resolver is already set");
119  Resolver = AR;
120 }
121 
122 // print - Print out the internal state of the pass. This is called by Analyze
123 // to print out the contents of an analysis. Otherwise it is not necessary to
124 // implement this method.
125 void Pass::print(raw_ostream &OS, const Module *) const {
126  OS << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
127 }
128 
129 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
130 // dump - call print(cerr);
132  print(dbgs(), nullptr);
133 }
134 #endif
135 
136 //===----------------------------------------------------------------------===//
137 // ImmutablePass Implementation
138 //
139 // Force out-of-line virtual method.
141 
143  // By default, don't do anything.
144 }
145 
146 //===----------------------------------------------------------------------===//
147 // FunctionPass Implementation
148 //
149 
151  const std::string &Banner) const {
152  return createPrintFunctionPass(OS, Banner);
153 }
154 
157 }
158 
159 static std::string getDescription(const Function &F) {
160  return "function (" + F.getName().str() + ")";
161 }
162 
164  OptPassGate &Gate = F.getContext().getOptPassGate();
165  if (Gate.isEnabled() && !Gate.shouldRunPass(this, getDescription(F)))
166  return true;
167 
168  if (F.hasOptNone()) {
169  LLVM_DEBUG(dbgs() << "Skipping pass '" << getPassName() << "' on function "
170  << F.getName() << "\n");
171  return true;
172  }
173  return false;
174 }
175 
176 const PassInfo *Pass::lookupPassInfo(const void *TI) {
178 }
179 
182 }
183 
186  if (!PI)
187  return nullptr;
188  return PI->createPass();
189 }
190 
191 //===----------------------------------------------------------------------===//
192 // Analysis Group Implementation Code
193 //===----------------------------------------------------------------------===//
194 
195 // RegisterAGBase implementation
196 
198  const void *PassID, bool isDefault)
199  : PassInfo(Name, InterfaceID) {
201  *this, isDefault);
202 }
203 
204 //===----------------------------------------------------------------------===//
205 // PassRegistrationListener implementation
206 //
207 
208 // enumeratePasses - Iterate over the registered passes, calling the
209 // passEnumerate callback on each PassInfo object.
212 }
213 
215  : cl::parser<const PassInfo *>(O) {
217 }
218 
219 // This only gets called during static destruction, in which case the
220 // PassRegistry will have already been destroyed by llvm_shutdown(). So
221 // attempting to remove the registration listener is an error.
223 
224 //===----------------------------------------------------------------------===//
225 // AnalysisUsage Class Implementation
226 //
227 
228 namespace {
229 
230 struct GetCFGOnlyPasses : public PassRegistrationListener {
232 
233  VectorType &CFGOnlyList;
234 
235  GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
236 
237  void passEnumerate(const PassInfo *P) override {
238  if (P->isCFGOnlyPass())
239  CFGOnlyList.push_back(P->getTypeInfo());
240  }
241 };
242 
243 } // end anonymous namespace
244 
245 // setPreservesCFG - This function should be called to by the pass, iff they do
246 // not:
247 //
248 // 1. Add or remove basic blocks from the function
249 // 2. Modify terminator instructions in any way.
250 //
251 // This function annotates the AnalysisUsage info object to say that analyses
252 // that only depend on the CFG are preserved by this pass.
254  // Since this transformation doesn't modify the CFG, it preserves all analyses
255  // that only depend on the CFG (like dominators, loop info, etc...)
256  GetCFGOnlyPasses(Preserved).enumeratePasses();
257 }
258 
260  const PassInfo *PI = Pass::lookupPassInfo(Arg);
261  // If the pass exists, preserve it. Otherwise silently do nothing.
262  if (PI)
263  pushUnique(Preserved, PI->getTypeInfo());
264  return *this;
265 }
266 
268  pushUnique(Required, ID);
269  return *this;
270 }
271 
273  pushUnique(Required, &ID);
274  return *this;
275 }
276 
278  pushUnique(Required, &ID);
279  pushUnique(RequiredTransitive, &ID);
280  return *this;
281 }
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:101
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:506
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
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:269
llvm::Function
Definition: Function.h:62
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:163
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:184
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
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
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:56
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:105
llvm::Resolver
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition: Record.h:2000
Arg
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
Definition: AMDGPULibCalls.cpp:206
llvm::PMDataManager
PMDataManager provides the common place to manage the analysis data used by pass managers.
Definition: LegacyPassManagers.h:296
llvm::Pass::preparePassManager
virtual void preparePassManager(PMStack &)
Check if available pass managers are suitable for this pass or not.
Definition: Pass.cpp:84
llvm::AnalysisUsage::addRequiredTransitiveID
AnalysisUsage & addRequiredTransitiveID(char &ID)
Definition: Pass.cpp:277
llvm::Pass::lookupPassInfo
static const PassInfo * lookupPassInfo(const void *TI)
Definition: Pass.cpp:176
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:210
llvm::FunctionPass::getPotentialPassManagerType
PassManagerType getPotentialPassManagerType() const override
Return what kind of Pass Manager can manage this pass.
Definition: Pass.cpp:155
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:214
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:53
llvm::Pass::getAsImmutablePass
virtual ImmutablePass * getAsImmutablePass()
Definition: Pass.cpp:109
llvm::FunctionPass::createPrinterPass
Pass * createPrinterPass(raw_ostream &OS, const std::string &Banner) const override
createPrinterPass - Get a function printer pass.
Definition: Pass.cpp:150
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:125
llvm::VectorType
Base class of all SIMD vector types.
Definition: DerivedTypes.h:389
llvm::Pass::~Pass
virtual ~Pass()
Definition: Pass.cpp:39
llvm::RegisterAGBase::RegisterAGBase
RegisterAGBase(StringRef Name, const void *InterfaceID, const void *PassID=nullptr, bool isDefault=false)
Definition: Pass.cpp:197
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:197
llvm::cl::Option
Definition: CommandLine.h:249
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:46
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:59
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:67
llvm::Pass::mustPreserveAnalysisID
bool mustPreserveAnalysisID(char &AID) const
mustPreserveAnalysisID - This method serves the same function as getAnalysisIfAvailable,...
Definition: Pass.cpp:64
getDescription
static std::string getDescription(const Module &M)
Definition: Pass.cpp:55
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:97
llvm::AnalysisUsage::setPreservesCFG
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:253
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:76
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:51
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:142
llvm::Pass::dump
void dump() const
Definition: Pass.cpp:131
llvm::Pass::getAsPMDataManager
virtual PMDataManager * getAsPMDataManager()
Definition: Pass.cpp:113
llvm::PMStack
PMStack - This class implements a stack data structure of PMDataManager pointers.
Definition: LegacyPassManagers.h:137
llvm::Pass::dumpPassStructure
virtual void dumpPassStructure(unsigned Offset=0)
Definition: Pass.cpp:69
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
Function.h
llvm::Pass::setResolver
void setResolver(AnalysisResolver *AR)
Definition: Pass.cpp:117
llvm::Pass::getPotentialPassManagerType
virtual PassManagerType getPotentialPassManagerType() const
Return what kind of Pass Manager can manage this pass.
Definition: Pass.cpp:88
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:497
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:93
PassInfo.h
llvm::AnalysisUsage::addRequiredID
AnalysisUsage & addRequiredID(const void *ID)
Definition: Pass.cpp:267
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.
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:37