[RISCV] Fix mgather -> riscv.masked.strided.load combine not extending indices (...
[llvm-project.git] / llvm / lib / IR / Pass.cpp
blobd6096ebb3af7007a1ba4ed314595a2a83c1ffcd5
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.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Pass.h"
16 #include "llvm/Config/llvm-config.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/IRPrintingPasses.h"
19 #include "llvm/IR/LLVMContext.h"
20 #include "llvm/IR/LegacyPassNameParser.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"
27 #include "llvm/Support/raw_ostream.h"
28 #include <cassert>
30 #ifdef EXPENSIVE_CHECKS
31 #include "llvm/IR/StructuralHash.h"
32 #endif
34 using namespace llvm;
36 #define DEBUG_TYPE "ir"
38 //===----------------------------------------------------------------------===//
39 // Pass Implementation
42 // Force out-of-line virtual method.
43 Pass::~Pass() {
44 delete Resolver;
47 // Force out-of-line virtual method.
48 ModulePass::~ModulePass() = default;
50 Pass *ModulePass::createPrinterPass(raw_ostream &OS,
51 const std::string &Banner) const {
52 return createPrintModulePass(OS, Banner);
55 PassManagerType ModulePass::getPotentialPassManagerType() const {
56 return PMT_ModulePassManager;
59 static std::string getDescription(const Module &M) {
60 return "module (" + M.getName().str() + ")";
63 bool ModulePass::skipModule(Module &M) const {
64 OptPassGate &Gate = M.getContext().getOptPassGate();
65 return Gate.isEnabled() &&
66 !Gate.shouldRunPass(this->getPassName(), getDescription(M));
69 bool Pass::mustPreserveAnalysisID(char &AID) const {
70 return Resolver->getAnalysisIfAvailable(&AID) != nullptr;
73 // dumpPassStructure - Implement the -debug-pass=Structure option
74 void Pass::dumpPassStructure(unsigned Offset) {
75 dbgs().indent(Offset*2) << getPassName() << "\n";
78 /// getPassName - Return a nice clean name for a pass. This usually
79 /// implemented in terms of the name that is registered by one of the
80 /// Registration templates, but can be overloaded directly.
81 StringRef Pass::getPassName() const {
82 AnalysisID AID = getPassID();
83 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
84 if (PI)
85 return PI->getPassName();
86 return "Unnamed pass: implement Pass::getPassName()";
89 void Pass::preparePassManager(PMStack &) {
90 // By default, don't do anything.
93 PassManagerType Pass::getPotentialPassManagerType() const {
94 // Default implementation.
95 return PMT_Unknown;
98 void Pass::getAnalysisUsage(AnalysisUsage &) const {
99 // By default, no analysis results are used, all are invalidated.
102 void Pass::releaseMemory() {
103 // By default, don't do anything.
106 void Pass::verifyAnalysis() const {
107 // By default, don't do anything.
110 void *Pass::getAdjustedAnalysisPointer(AnalysisID AID) {
111 return this;
114 ImmutablePass *Pass::getAsImmutablePass() {
115 return nullptr;
118 PMDataManager *Pass::getAsPMDataManager() {
119 return nullptr;
122 void Pass::setResolver(AnalysisResolver *AR) {
123 assert(!Resolver && "Resolver is already set");
124 Resolver = AR;
127 // print - Print out the internal state of the pass. This is called by Analyze
128 // to print out the contents of an analysis. Otherwise it is not necessary to
129 // implement this method.
130 void Pass::print(raw_ostream &OS, const Module *) const {
131 OS << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
134 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
135 // dump - call print(cerr);
136 LLVM_DUMP_METHOD void Pass::dump() const {
137 print(dbgs(), nullptr);
139 #endif
141 #ifdef EXPENSIVE_CHECKS
142 uint64_t Pass::structuralHash(Module &M) const {
143 return StructuralHash(M, true);
146 uint64_t Pass::structuralHash(Function &F) const {
147 return StructuralHash(F, true);
149 #endif
151 //===----------------------------------------------------------------------===//
152 // ImmutablePass Implementation
154 // Force out-of-line virtual method.
155 ImmutablePass::~ImmutablePass() = default;
157 void ImmutablePass::initializePass() {
158 // By default, don't do anything.
161 //===----------------------------------------------------------------------===//
162 // FunctionPass Implementation
165 Pass *FunctionPass::createPrinterPass(raw_ostream &OS,
166 const std::string &Banner) const {
167 return createPrintFunctionPass(OS, Banner);
170 PassManagerType FunctionPass::getPotentialPassManagerType() const {
171 return PMT_FunctionPassManager;
174 static std::string getDescription(const Function &F) {
175 return "function (" + F.getName().str() + ")";
178 bool FunctionPass::skipFunction(const Function &F) const {
179 OptPassGate &Gate = F.getContext().getOptPassGate();
180 if (Gate.isEnabled() &&
181 !Gate.shouldRunPass(this->getPassName(), getDescription(F)))
182 return true;
184 if (F.hasOptNone()) {
185 LLVM_DEBUG(dbgs() << "Skipping pass '" << getPassName() << "' on function "
186 << F.getName() << "\n");
187 return true;
189 return false;
192 const PassInfo *Pass::lookupPassInfo(const void *TI) {
193 return PassRegistry::getPassRegistry()->getPassInfo(TI);
196 const PassInfo *Pass::lookupPassInfo(StringRef Arg) {
197 return PassRegistry::getPassRegistry()->getPassInfo(Arg);
200 Pass *Pass::createPass(AnalysisID ID) {
201 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
202 if (!PI)
203 return nullptr;
204 return PI->createPass();
207 //===----------------------------------------------------------------------===//
208 // Analysis Group Implementation Code
209 //===----------------------------------------------------------------------===//
211 // RegisterAGBase implementation
213 RegisterAGBase::RegisterAGBase(StringRef Name, const void *InterfaceID,
214 const void *PassID, bool isDefault)
215 : PassInfo(Name, InterfaceID) {
216 PassRegistry::getPassRegistry()->registerAnalysisGroup(InterfaceID, PassID,
217 *this, isDefault);
220 //===----------------------------------------------------------------------===//
221 // PassRegistrationListener implementation
224 // enumeratePasses - Iterate over the registered passes, calling the
225 // passEnumerate callback on each PassInfo object.
226 void PassRegistrationListener::enumeratePasses() {
227 PassRegistry::getPassRegistry()->enumerateWith(this);
230 PassNameParser::PassNameParser(cl::Option &O)
231 : cl::parser<const PassInfo *>(O) {
232 PassRegistry::getPassRegistry()->addRegistrationListener(this);
235 // This only gets called during static destruction, in which case the
236 // PassRegistry will have already been destroyed by llvm_shutdown(). So
237 // attempting to remove the registration listener is an error.
238 PassNameParser::~PassNameParser() = default;
240 //===----------------------------------------------------------------------===//
241 // AnalysisUsage Class Implementation
244 namespace {
246 struct GetCFGOnlyPasses : public PassRegistrationListener {
247 using VectorType = AnalysisUsage::VectorType;
249 VectorType &CFGOnlyList;
251 GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
253 void passEnumerate(const PassInfo *P) override {
254 if (P->isCFGOnlyPass())
255 CFGOnlyList.push_back(P->getTypeInfo());
259 } // end anonymous namespace
261 // setPreservesCFG - This function should be called to by the pass, iff they do
262 // not:
264 // 1. Add or remove basic blocks from the function
265 // 2. Modify terminator instructions in any way.
267 // This function annotates the AnalysisUsage info object to say that analyses
268 // that only depend on the CFG are preserved by this pass.
269 void AnalysisUsage::setPreservesCFG() {
270 // Since this transformation doesn't modify the CFG, it preserves all analyses
271 // that only depend on the CFG (like dominators, loop info, etc...)
272 GetCFGOnlyPasses(Preserved).enumeratePasses();
275 AnalysisUsage &AnalysisUsage::addPreserved(StringRef Arg) {
276 const PassInfo *PI = Pass::lookupPassInfo(Arg);
277 // If the pass exists, preserve it. Otherwise silently do nothing.
278 if (PI)
279 pushUnique(Preserved, PI->getTypeInfo());
280 return *this;
283 AnalysisUsage &AnalysisUsage::addRequiredID(const void *ID) {
284 pushUnique(Required, ID);
285 return *this;
288 AnalysisUsage &AnalysisUsage::addRequiredID(char &ID) {
289 pushUnique(Required, &ID);
290 return *this;
293 AnalysisUsage &AnalysisUsage::addRequiredTransitiveID(char &ID) {
294 pushUnique(Required, &ID);
295 pushUnique(RequiredTransitive, &ID);
296 return *this;