[ORC] Add std::tuple support to SimplePackedSerialization.
[llvm-project.git] / llvm / lib / IR / Pass.cpp
blob755ea57c63fd0b6701d6f81c6d85d6a09089be83
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 using namespace llvm;
32 #define DEBUG_TYPE "ir"
34 //===----------------------------------------------------------------------===//
35 // Pass Implementation
38 // Force out-of-line virtual method.
39 Pass::~Pass() {
40 delete Resolver;
43 // Force out-of-line virtual method.
44 ModulePass::~ModulePass() = default;
46 Pass *ModulePass::createPrinterPass(raw_ostream &OS,
47 const std::string &Banner) const {
48 return createPrintModulePass(OS, Banner);
51 PassManagerType ModulePass::getPotentialPassManagerType() const {
52 return PMT_ModulePassManager;
55 static std::string getDescription(const Module &M) {
56 return "module (" + M.getName().str() + ")";
59 bool ModulePass::skipModule(Module &M) const {
60 OptPassGate &Gate = M.getContext().getOptPassGate();
61 return Gate.isEnabled() && !Gate.shouldRunPass(this, getDescription(M));
64 bool Pass::mustPreserveAnalysisID(char &AID) const {
65 return Resolver->getAnalysisIfAvailable(&AID) != nullptr;
68 // dumpPassStructure - Implement the -debug-pass=Structure option
69 void Pass::dumpPassStructure(unsigned Offset) {
70 dbgs().indent(Offset*2) << getPassName() << "\n";
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.
76 StringRef Pass::getPassName() const {
77 AnalysisID AID = getPassID();
78 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
79 if (PI)
80 return PI->getPassName();
81 return "Unnamed pass: implement Pass::getPassName()";
84 void Pass::preparePassManager(PMStack &) {
85 // By default, don't do anything.
88 PassManagerType Pass::getPotentialPassManagerType() const {
89 // Default implementation.
90 return PMT_Unknown;
93 void Pass::getAnalysisUsage(AnalysisUsage &) const {
94 // By default, no analysis results are used, all are invalidated.
97 void Pass::releaseMemory() {
98 // By default, don't do anything.
101 void Pass::verifyAnalysis() const {
102 // By default, don't do anything.
105 void *Pass::getAdjustedAnalysisPointer(AnalysisID AID) {
106 return this;
109 ImmutablePass *Pass::getAsImmutablePass() {
110 return nullptr;
113 PMDataManager *Pass::getAsPMDataManager() {
114 return nullptr;
117 void Pass::setResolver(AnalysisResolver *AR) {
118 assert(!Resolver && "Resolver is already set");
119 Resolver = AR;
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";
129 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
130 // dump - call print(cerr);
131 LLVM_DUMP_METHOD void Pass::dump() const {
132 print(dbgs(), nullptr);
134 #endif
136 //===----------------------------------------------------------------------===//
137 // ImmutablePass Implementation
139 // Force out-of-line virtual method.
140 ImmutablePass::~ImmutablePass() = default;
142 void ImmutablePass::initializePass() {
143 // By default, don't do anything.
146 //===----------------------------------------------------------------------===//
147 // FunctionPass Implementation
150 Pass *FunctionPass::createPrinterPass(raw_ostream &OS,
151 const std::string &Banner) const {
152 return createPrintFunctionPass(OS, Banner);
155 PassManagerType FunctionPass::getPotentialPassManagerType() const {
156 return PMT_FunctionPassManager;
159 static std::string getDescription(const Function &F) {
160 return "function (" + F.getName().str() + ")";
163 bool FunctionPass::skipFunction(const Function &F) const {
164 OptPassGate &Gate = F.getContext().getOptPassGate();
165 if (Gate.isEnabled() && !Gate.shouldRunPass(this, getDescription(F)))
166 return true;
168 if (F.hasOptNone()) {
169 LLVM_DEBUG(dbgs() << "Skipping pass '" << getPassName() << "' on function "
170 << F.getName() << "\n");
171 return true;
173 return false;
176 const PassInfo *Pass::lookupPassInfo(const void *TI) {
177 return PassRegistry::getPassRegistry()->getPassInfo(TI);
180 const PassInfo *Pass::lookupPassInfo(StringRef Arg) {
181 return PassRegistry::getPassRegistry()->getPassInfo(Arg);
184 Pass *Pass::createPass(AnalysisID ID) {
185 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
186 if (!PI)
187 return nullptr;
188 return PI->createPass();
191 //===----------------------------------------------------------------------===//
192 // Analysis Group Implementation Code
193 //===----------------------------------------------------------------------===//
195 // RegisterAGBase implementation
197 RegisterAGBase::RegisterAGBase(StringRef Name, const void *InterfaceID,
198 const void *PassID, bool isDefault)
199 : PassInfo(Name, InterfaceID) {
200 PassRegistry::getPassRegistry()->registerAnalysisGroup(InterfaceID, PassID,
201 *this, isDefault);
204 //===----------------------------------------------------------------------===//
205 // PassRegistrationListener implementation
208 // enumeratePasses - Iterate over the registered passes, calling the
209 // passEnumerate callback on each PassInfo object.
210 void PassRegistrationListener::enumeratePasses() {
211 PassRegistry::getPassRegistry()->enumerateWith(this);
214 PassNameParser::PassNameParser(cl::Option &O)
215 : cl::parser<const PassInfo *>(O) {
216 PassRegistry::getPassRegistry()->addRegistrationListener(this);
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.
222 PassNameParser::~PassNameParser() = default;
224 //===----------------------------------------------------------------------===//
225 // AnalysisUsage Class Implementation
228 namespace {
230 struct GetCFGOnlyPasses : public PassRegistrationListener {
231 using VectorType = AnalysisUsage::VectorType;
233 VectorType &CFGOnlyList;
235 GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
237 void passEnumerate(const PassInfo *P) override {
238 if (P->isCFGOnlyPass())
239 CFGOnlyList.push_back(P->getTypeInfo());
243 } // end anonymous namespace
245 // setPreservesCFG - This function should be called to by the pass, iff they do
246 // not:
248 // 1. Add or remove basic blocks from the function
249 // 2. Modify terminator instructions in any way.
251 // This function annotates the AnalysisUsage info object to say that analyses
252 // that only depend on the CFG are preserved by this pass.
253 void AnalysisUsage::setPreservesCFG() {
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();
259 AnalysisUsage &AnalysisUsage::addPreserved(StringRef Arg) {
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;
267 AnalysisUsage &AnalysisUsage::addRequiredID(const void *ID) {
268 pushUnique(Required, ID);
269 return *this;
272 AnalysisUsage &AnalysisUsage::addRequiredID(char &ID) {
273 pushUnique(Required, &ID);
274 return *this;
277 AnalysisUsage &AnalysisUsage::addRequiredTransitiveID(char &ID) {
278 pushUnique(Required, &ID);
279 pushUnique(RequiredTransitive, &ID);
280 return *this;