Revert r354244 "[DAGCombiner] Eliminate dead stores to stack."
[llvm-complete.git] / lib / IR / Pass.cpp
blobe9a0c68495b7046463443191dd8877bf7d3a0e92
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/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"
31 #include <cassert>
33 using namespace llvm;
35 #define DEBUG_TYPE "ir"
37 //===----------------------------------------------------------------------===//
38 // Pass Implementation
41 // Force out-of-line virtual method.
42 Pass::~Pass() {
43 delete Resolver;
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 bool ModulePass::skipModule(Module &M) const {
59 return !M.getContext().getOptPassGate().shouldRunPass(this, M);
62 bool Pass::mustPreserveAnalysisID(char &AID) const {
63 return Resolver->getAnalysisIfAvailable(&AID, true) != nullptr;
66 // dumpPassStructure - Implement the -debug-pass=Structure option
67 void Pass::dumpPassStructure(unsigned Offset) {
68 dbgs().indent(Offset*2) << getPassName() << "\n";
71 /// getPassName - Return a nice clean name for a pass. This usually
72 /// implemented in terms of the name that is registered by one of the
73 /// Registration templates, but can be overloaded directly.
74 StringRef Pass::getPassName() const {
75 AnalysisID AID = getPassID();
76 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
77 if (PI)
78 return PI->getPassName();
79 return "Unnamed pass: implement Pass::getPassName()";
82 void Pass::preparePassManager(PMStack &) {
83 // By default, don't do anything.
86 PassManagerType Pass::getPotentialPassManagerType() const {
87 // Default implementation.
88 return PMT_Unknown;
91 void Pass::getAnalysisUsage(AnalysisUsage &) const {
92 // By default, no analysis results are used, all are invalidated.
95 void Pass::releaseMemory() {
96 // By default, don't do anything.
99 void Pass::verifyAnalysis() const {
100 // By default, don't do anything.
103 void *Pass::getAdjustedAnalysisPointer(AnalysisID AID) {
104 return this;
107 ImmutablePass *Pass::getAsImmutablePass() {
108 return nullptr;
111 PMDataManager *Pass::getAsPMDataManager() {
112 return nullptr;
115 void Pass::setResolver(AnalysisResolver *AR) {
116 assert(!Resolver && "Resolver is already set");
117 Resolver = AR;
120 // print - Print out the internal state of the pass. This is called by Analyze
121 // to print out the contents of an analysis. Otherwise it is not necessary to
122 // implement this method.
123 void Pass::print(raw_ostream &OS, const Module *) const {
124 OS << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
127 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
128 // dump - call print(cerr);
129 LLVM_DUMP_METHOD void Pass::dump() const {
130 print(dbgs(), nullptr);
132 #endif
134 //===----------------------------------------------------------------------===//
135 // ImmutablePass Implementation
137 // Force out-of-line virtual method.
138 ImmutablePass::~ImmutablePass() = default;
140 void ImmutablePass::initializePass() {
141 // By default, don't do anything.
144 //===----------------------------------------------------------------------===//
145 // FunctionPass Implementation
148 Pass *FunctionPass::createPrinterPass(raw_ostream &OS,
149 const std::string &Banner) const {
150 return createPrintFunctionPass(OS, Banner);
153 PassManagerType FunctionPass::getPotentialPassManagerType() const {
154 return PMT_FunctionPassManager;
157 bool FunctionPass::skipFunction(const Function &F) const {
158 if (!F.getContext().getOptPassGate().shouldRunPass(this, F))
159 return true;
161 if (F.hasFnAttribute(Attribute::OptimizeNone)) {
162 LLVM_DEBUG(dbgs() << "Skipping pass '" << getPassName() << "' on function "
163 << F.getName() << "\n");
164 return true;
166 return false;
169 //===----------------------------------------------------------------------===//
170 // BasicBlockPass Implementation
173 Pass *BasicBlockPass::createPrinterPass(raw_ostream &OS,
174 const std::string &Banner) const {
175 return createPrintBasicBlockPass(OS, Banner);
178 bool BasicBlockPass::doInitialization(Function &) {
179 // By default, don't do anything.
180 return false;
183 bool BasicBlockPass::doFinalization(Function &) {
184 // By default, don't do anything.
185 return false;
188 bool BasicBlockPass::skipBasicBlock(const BasicBlock &BB) const {
189 const Function *F = BB.getParent();
190 if (!F)
191 return false;
192 if (!F->getContext().getOptPassGate().shouldRunPass(this, BB))
193 return true;
194 if (F->hasFnAttribute(Attribute::OptimizeNone)) {
195 // Report this only once per function.
196 if (&BB == &F->getEntryBlock())
197 LLVM_DEBUG(dbgs() << "Skipping pass '" << getPassName()
198 << "' on function " << F->getName() << "\n");
199 return true;
201 return false;
204 PassManagerType BasicBlockPass::getPotentialPassManagerType() const {
205 return PMT_BasicBlockPassManager;
208 const PassInfo *Pass::lookupPassInfo(const void *TI) {
209 return PassRegistry::getPassRegistry()->getPassInfo(TI);
212 const PassInfo *Pass::lookupPassInfo(StringRef Arg) {
213 return PassRegistry::getPassRegistry()->getPassInfo(Arg);
216 Pass *Pass::createPass(AnalysisID ID) {
217 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
218 if (!PI)
219 return nullptr;
220 return PI->createPass();
223 //===----------------------------------------------------------------------===//
224 // Analysis Group Implementation Code
225 //===----------------------------------------------------------------------===//
227 // RegisterAGBase implementation
229 RegisterAGBase::RegisterAGBase(StringRef Name, const void *InterfaceID,
230 const void *PassID, bool isDefault)
231 : PassInfo(Name, InterfaceID) {
232 PassRegistry::getPassRegistry()->registerAnalysisGroup(InterfaceID, PassID,
233 *this, isDefault);
236 //===----------------------------------------------------------------------===//
237 // PassRegistrationListener implementation
240 // enumeratePasses - Iterate over the registered passes, calling the
241 // passEnumerate callback on each PassInfo object.
242 void PassRegistrationListener::enumeratePasses() {
243 PassRegistry::getPassRegistry()->enumerateWith(this);
246 PassNameParser::PassNameParser(cl::Option &O)
247 : cl::parser<const PassInfo *>(O) {
248 PassRegistry::getPassRegistry()->addRegistrationListener(this);
251 // This only gets called during static destruction, in which case the
252 // PassRegistry will have already been destroyed by llvm_shutdown(). So
253 // attempting to remove the registration listener is an error.
254 PassNameParser::~PassNameParser() = default;
256 //===----------------------------------------------------------------------===//
257 // AnalysisUsage Class Implementation
260 namespace {
262 struct GetCFGOnlyPasses : public PassRegistrationListener {
263 using VectorType = AnalysisUsage::VectorType;
265 VectorType &CFGOnlyList;
267 GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
269 void passEnumerate(const PassInfo *P) override {
270 if (P->isCFGOnlyPass())
271 CFGOnlyList.push_back(P->getTypeInfo());
275 } // end anonymous namespace
277 // setPreservesCFG - This function should be called to by the pass, iff they do
278 // not:
280 // 1. Add or remove basic blocks from the function
281 // 2. Modify terminator instructions in any way.
283 // This function annotates the AnalysisUsage info object to say that analyses
284 // that only depend on the CFG are preserved by this pass.
285 void AnalysisUsage::setPreservesCFG() {
286 // Since this transformation doesn't modify the CFG, it preserves all analyses
287 // that only depend on the CFG (like dominators, loop info, etc...)
288 GetCFGOnlyPasses(Preserved).enumeratePasses();
291 AnalysisUsage &AnalysisUsage::addPreserved(StringRef Arg) {
292 const PassInfo *PI = Pass::lookupPassInfo(Arg);
293 // If the pass exists, preserve it. Otherwise silently do nothing.
294 if (PI) Preserved.push_back(PI->getTypeInfo());
295 return *this;
298 AnalysisUsage &AnalysisUsage::addRequiredID(const void *ID) {
299 Required.push_back(ID);
300 return *this;
303 AnalysisUsage &AnalysisUsage::addRequiredID(char &ID) {
304 Required.push_back(&ID);
305 return *this;
308 AnalysisUsage &AnalysisUsage::addRequiredTransitiveID(char &ID) {
309 Required.push_back(&ID);
310 RequiredTransitive.push_back(&ID);
311 return *this;