LLVM 22.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.
43Pass::~Pass() {
44 delete Resolver;
45}
46
47// Force out-of-line virtual method.
48ModulePass::~ModulePass() = default;
49
50Pass *ModulePass::createPrinterPass(raw_ostream &OS,
51 const std::string &Banner) const {
52 return createPrintModulePass(OS, Banner);
53}
54
55PassManagerType ModulePass::getPotentialPassManagerType() const {
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.
85StringRef Pass::getPassName() const {
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
95StringRef Pass::getPassArgument() const {
96 AnalysisID AID = getPassID();
97 const PassInfo *PI = Pass::lookupPassInfo(AID);
98 if (PI)
99 return PI->getPassArgument();
100 return "";
101}
102
103void Pass::preparePassManager(PMStack &) {
104 // By default, don't do anything.
105}
106
107PassManagerType Pass::getPotentialPassManagerType() const {
108 // Default implementation.
109 return PMT_Unknown;
110}
111
112void Pass::getAnalysisUsage(AnalysisUsage &) const {
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
124ImmutablePass *Pass::getAsImmutablePass() {
125 return nullptr;
126}
127
128PMDataManager *Pass::getAsPMDataManager() {
129 return nullptr;
130}
131
132void Pass::setResolver(AnalysisResolver *AR) {
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.
165ImmutablePass::~ImmutablePass() = default;
166
167void ImmutablePass::initializePass() {
168 // By default, don't do anything.
169}
170
171//===----------------------------------------------------------------------===//
172// FunctionPass Implementation
173//
174
175Pass *FunctionPass::createPrinterPass(raw_ostream &OS,
176 const std::string &Banner) const {
177 return createPrintFunctionPass(OS, Banner);
178}
179
180PassManagerType FunctionPass::getPotentialPassManagerType() const {
182}
183
184static std::string getDescription(const Function &F) {
185 return "function (" + F.getName().str() + ")";
186}
187
188bool FunctionPass::skipFunction(const Function &F) const {
189 OptPassGate &Gate = F.getContext().getOptPassGate();
190
191 StringRef PassName = getPassArgument();
192 if (PassName.empty())
193 PassName = this->getPassName();
194
195 if (Gate.isEnabled() && !Gate.shouldRunPass(PassName, getDescription(F)))
196 return true;
197
198 if (F.hasOptNone()) {
199 LLVM_DEBUG(dbgs() << "Skipping pass '" << getPassName() << "' on function "
200 << F.getName() << "\n");
201 return true;
202 }
203 return false;
204}
205
206const PassInfo *Pass::lookupPassInfo(const void *TI) {
207 return PassRegistry::getPassRegistry()->getPassInfo(TI);
208}
209
210const PassInfo *Pass::lookupPassInfo(StringRef Arg) {
211 return PassRegistry::getPassRegistry()->getPassInfo(Arg);
212}
213
214Pass *Pass::createPass(AnalysisID ID) {
215 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
216 if (!PI)
217 return nullptr;
218 return PI->createPass();
219}
220
221//===----------------------------------------------------------------------===//
222// PassRegistrationListener implementation
223//
224
225// enumeratePasses - Iterate over the registered passes, calling the
226// passEnumerate callback on each PassInfo object.
227void PassRegistrationListener::enumeratePasses() {
228 PassRegistry::getPassRegistry()->enumerateWith(this);
229}
230
231PassNameParser::PassNameParser(cl::Option &O)
232 : cl::parser<const PassInfo *>(O) {
233 PassRegistry::getPassRegistry()->addRegistrationListener(this);
234}
235
236// This only gets called during static destruction, in which case the
237// PassRegistry will have already been destroyed by llvm_shutdown(). So
238// attempting to remove the registration listener is an error.
239PassNameParser::~PassNameParser() = default;
240
241//===----------------------------------------------------------------------===//
242// AnalysisUsage Class Implementation
243//
244
245namespace {
246
247struct GetCFGOnlyPasses : public PassRegistrationListener {
249
250 VectorType &CFGOnlyList;
251
252 GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
253
254 void passEnumerate(const PassInfo *P) override {
255 if (P->isCFGOnlyPass())
256 CFGOnlyList.push_back(P->getTypeInfo());
257 }
258};
259
260} // end anonymous namespace
261
262// setPreservesCFG - This function should be called to by the pass, iff they do
263// not:
264//
265// 1. Add or remove basic blocks from the function
266// 2. Modify terminator instructions in any way.
267//
268// This function annotates the AnalysisUsage info object to say that analyses
269// that only depend on the CFG are preserved by this pass.
270void AnalysisUsage::setPreservesCFG() {
271 // Since this transformation doesn't modify the CFG, it preserves all analyses
272 // that only depend on the CFG (like dominators, loop info, etc...)
273 GetCFGOnlyPasses(Preserved).enumeratePasses();
274}
275
276AnalysisUsage &AnalysisUsage::addPreserved(StringRef Arg) {
277 const PassInfo *PI = Pass::lookupPassInfo(Arg);
278 // If the pass exists, preserve it. Otherwise silently do nothing.
279 if (PI)
280 pushUnique(Preserved, PI->getTypeInfo());
281 return *this;
282}
283
284AnalysisUsage &AnalysisUsage::addRequiredID(const void *ID) {
285 pushUnique(Required, ID);
286 return *this;
287}
288
289AnalysisUsage &AnalysisUsage::addRequiredID(char &ID) {
290 pushUnique(Required, &ID);
291 return *this;
292}
293
294AnalysisUsage &AnalysisUsage::addRequiredTransitiveID(char &ID) {
295 pushUnique(Required, &ID);
296 pushUnique(RequiredTransitive, &ID);
297 return *this;
298}
299
300#ifndef NDEBUG
302 switch (Phase) {
303 case ThinOrFullLTOPhase::None:
304 return "None";
305 case ThinOrFullLTOPhase::ThinLTOPreLink:
306 return "ThinLTOPreLink";
307 case ThinOrFullLTOPhase::ThinLTOPostLink:
308 return "ThinLTOPostLink";
309 case ThinOrFullLTOPhase::FullLTOPreLink:
310 return "FullLTOPreLink";
311 case ThinOrFullLTOPhase::FullLTOPostLink:
312 return "FullLTOPostLink";
313 }
314 llvm_unreachable("invalid phase");
315}
316#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:638
uint64_t Offset
Definition: ELF_riscv.cpp:478
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:367
#define F(x, y, z)
Definition: MD5.cpp:55
This file declares the interface for bisecting optimizations.
#define P(N)
raw_pwrite_stream & OS
#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.
ImmutablePass class - This class is used to provide information that does not need to be run.
Definition: Pass.h:285
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
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:30
StringRef getPassArgument() const
getPassArgument - Return the command line option that may be passed to 'opt' that will cause this pas...
Definition: PassInfo.h:59
StringRef getPassName() const
getPassName - Return the friendly name for the pass, never returns null
Definition: PassInfo.h:54
Pass * createPass() const
createPass() - Use this method to create an instance of this pass.
Definition: PassInfo.h:85
const void * getTypeInfo() const
getTypeInfo - Return the id object for the pass... TODO : Rename
Definition: PassInfo.h:63
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:99
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition: Record.h:2196
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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
ThinOrFullLTOPhase
This enumerates the LLVM full LTO or ThinLTO optimization phases.
Definition: Pass.h:77
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:207
const void * AnalysisID
Definition: Pass.h:51
const char * to_string(ThinOrFullLTOPhase Phase)
Definition: Pass.cpp:301
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...
Definition: PassSupport.h:106