1 //===- Pass.cpp - LLVM Pass Infrastructure Implementation -----------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the LLVM Pass infrastructure. It is primarily
10 // responsible with ensuring that passes are executed and batched together
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Pass.h"
16 #include "llvm/Config/llvm-config.h"
17 #include "llvm/IR/Attributes.h"
18 #include "llvm/IR/BasicBlock.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/IRPrintingPasses.h"
21 #include "llvm/IR/LLVMContext.h"
22 #include "llvm/IR/LegacyPassNameParser.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/IR/OptBisect.h"
25 #include "llvm/PassInfo.h"
26 #include "llvm/PassRegistry.h"
27 #include "llvm/PassSupport.h"
28 #include "llvm/Support/Compiler.h"
29 #include "llvm/Support/Debug.h"
30 #include "llvm/Support/raw_ostream.h"
35 #define DEBUG_TYPE "ir"
37 //===----------------------------------------------------------------------===//
38 // Pass Implementation
41 // Force out-of-line virtual method.
46 // Force out-of-line virtual method.
47 ModulePass::~ModulePass() = default;
49 Pass *ModulePass::createPrinterPass(raw_ostream &OS,
50 const std::string &Banner) const {
51 return createPrintModulePass(OS, Banner);
54 PassManagerType ModulePass::getPotentialPassManagerType() const {
55 return PMT_ModulePassManager;
58 static std::string getDescription(const Module &M) {
59 return "module (" + M.getName().str() + ")";
62 bool ModulePass::skipModule(Module &M) const {
63 OptPassGate &Gate = M.getContext().getOptPassGate();
64 return Gate.isEnabled() && !Gate.shouldRunPass(this, getDescription(M));
67 bool Pass::mustPreserveAnalysisID(char &AID) const {
68 return Resolver->getAnalysisIfAvailable(&AID, true) != nullptr;
71 // dumpPassStructure - Implement the -debug-pass=Structure option
72 void Pass::dumpPassStructure(unsigned Offset) {
73 dbgs().indent(Offset*2) << getPassName() << "\n";
76 /// getPassName - Return a nice clean name for a pass. This usually
77 /// implemented in terms of the name that is registered by one of the
78 /// Registration templates, but can be overloaded directly.
79 StringRef Pass::getPassName() const {
80 AnalysisID AID = getPassID();
81 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
83 return PI->getPassName();
84 return "Unnamed pass: implement Pass::getPassName()";
87 void Pass::preparePassManager(PMStack &) {
88 // By default, don't do anything.
91 PassManagerType Pass::getPotentialPassManagerType() const {
92 // Default implementation.
96 void Pass::getAnalysisUsage(AnalysisUsage &) const {
97 // By default, no analysis results are used, all are invalidated.
100 void Pass::releaseMemory() {
101 // By default, don't do anything.
104 void Pass::verifyAnalysis() const {
105 // By default, don't do anything.
108 void *Pass::getAdjustedAnalysisPointer(AnalysisID AID) {
112 ImmutablePass *Pass::getAsImmutablePass() {
116 PMDataManager *Pass::getAsPMDataManager() {
120 void Pass::setResolver(AnalysisResolver *AR) {
121 assert(!Resolver && "Resolver is already set");
125 // print - Print out the internal state of the pass. This is called by Analyze
126 // to print out the contents of an analysis. Otherwise it is not necessary to
127 // implement this method.
128 void Pass::print(raw_ostream &OS, const Module *) const {
129 OS << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
132 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
133 // dump - call print(cerr);
134 LLVM_DUMP_METHOD void Pass::dump() const {
135 print(dbgs(), nullptr);
139 //===----------------------------------------------------------------------===//
140 // ImmutablePass Implementation
142 // Force out-of-line virtual method.
143 ImmutablePass::~ImmutablePass() = default;
145 void ImmutablePass::initializePass() {
146 // By default, don't do anything.
149 //===----------------------------------------------------------------------===//
150 // FunctionPass Implementation
153 Pass *FunctionPass::createPrinterPass(raw_ostream &OS,
154 const std::string &Banner) const {
155 return createPrintFunctionPass(OS, Banner);
158 PassManagerType FunctionPass::getPotentialPassManagerType() const {
159 return PMT_FunctionPassManager;
162 static std::string getDescription(const Function &F) {
163 return "function (" + F.getName().str() + ")";
166 bool FunctionPass::skipFunction(const Function &F) const {
167 OptPassGate &Gate = F.getContext().getOptPassGate();
168 if (Gate.isEnabled() && !Gate.shouldRunPass(this, getDescription(F)))
171 if (F.hasOptNone()) {
172 LLVM_DEBUG(dbgs() << "Skipping pass '" << getPassName() << "' on function "
173 << F.getName() << "\n");
179 const PassInfo *Pass::lookupPassInfo(const void *TI) {
180 return PassRegistry::getPassRegistry()->getPassInfo(TI);
183 const PassInfo *Pass::lookupPassInfo(StringRef Arg) {
184 return PassRegistry::getPassRegistry()->getPassInfo(Arg);
187 Pass *Pass::createPass(AnalysisID ID) {
188 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
191 return PI->createPass();
194 //===----------------------------------------------------------------------===//
195 // Analysis Group Implementation Code
196 //===----------------------------------------------------------------------===//
198 // RegisterAGBase implementation
200 RegisterAGBase::RegisterAGBase(StringRef Name, const void *InterfaceID,
201 const void *PassID, bool isDefault)
202 : PassInfo(Name, InterfaceID) {
203 PassRegistry::getPassRegistry()->registerAnalysisGroup(InterfaceID, PassID,
207 //===----------------------------------------------------------------------===//
208 // PassRegistrationListener implementation
211 // enumeratePasses - Iterate over the registered passes, calling the
212 // passEnumerate callback on each PassInfo object.
213 void PassRegistrationListener::enumeratePasses() {
214 PassRegistry::getPassRegistry()->enumerateWith(this);
217 PassNameParser::PassNameParser(cl::Option &O)
218 : cl::parser<const PassInfo *>(O) {
219 PassRegistry::getPassRegistry()->addRegistrationListener(this);
222 // This only gets called during static destruction, in which case the
223 // PassRegistry will have already been destroyed by llvm_shutdown(). So
224 // attempting to remove the registration listener is an error.
225 PassNameParser::~PassNameParser() = default;
227 //===----------------------------------------------------------------------===//
228 // AnalysisUsage Class Implementation
233 struct GetCFGOnlyPasses : public PassRegistrationListener {
234 using VectorType = AnalysisUsage::VectorType;
236 VectorType &CFGOnlyList;
238 GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
240 void passEnumerate(const PassInfo *P) override {
241 if (P->isCFGOnlyPass())
242 CFGOnlyList.push_back(P->getTypeInfo());
246 } // end anonymous namespace
248 // setPreservesCFG - This function should be called to by the pass, iff they do
251 // 1. Add or remove basic blocks from the function
252 // 2. Modify terminator instructions in any way.
254 // This function annotates the AnalysisUsage info object to say that analyses
255 // that only depend on the CFG are preserved by this pass.
256 void AnalysisUsage::setPreservesCFG() {
257 // Since this transformation doesn't modify the CFG, it preserves all analyses
258 // that only depend on the CFG (like dominators, loop info, etc...)
259 GetCFGOnlyPasses(Preserved).enumeratePasses();
262 AnalysisUsage &AnalysisUsage::addPreserved(StringRef Arg) {
263 const PassInfo *PI = Pass::lookupPassInfo(Arg);
264 // If the pass exists, preserve it. Otherwise silently do nothing.
265 if (PI) Preserved.push_back(PI->getTypeInfo());
269 AnalysisUsage &AnalysisUsage::addRequiredID(const void *ID) {
270 Required.push_back(ID);
274 AnalysisUsage &AnalysisUsage::addRequiredID(char &ID) {
275 Required.push_back(&ID);
279 AnalysisUsage &AnalysisUsage::addRequiredTransitiveID(char &ID) {
280 Required.push_back(&ID);
281 RequiredTransitive.push_back(&ID);