[ARM] Move MVE VPT block tests into the Thumb2 directory. NFC
[llvm-core.git] / lib / IR / Pass.cpp
blob699a7e17c0cbbd46af7b917af0479a6dbf86a5e0
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 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);
82 if (PI)
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.
93 return PMT_Unknown;
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) {
109 return this;
112 ImmutablePass *Pass::getAsImmutablePass() {
113 return nullptr;
116 PMDataManager *Pass::getAsPMDataManager() {
117 return nullptr;
120 void Pass::setResolver(AnalysisResolver *AR) {
121 assert(!Resolver && "Resolver is already set");
122 Resolver = AR;
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);
137 #endif
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)))
169 return true;
171 if (F.hasOptNone()) {
172 LLVM_DEBUG(dbgs() << "Skipping pass '" << getPassName() << "' on function "
173 << F.getName() << "\n");
174 return true;
176 return false;
179 //===----------------------------------------------------------------------===//
180 // BasicBlockPass Implementation
183 Pass *BasicBlockPass::createPrinterPass(raw_ostream &OS,
184 const std::string &Banner) const {
185 return createPrintBasicBlockPass(OS, Banner);
188 bool BasicBlockPass::doInitialization(Function &) {
189 // By default, don't do anything.
190 return false;
193 bool BasicBlockPass::doFinalization(Function &) {
194 // By default, don't do anything.
195 return false;
198 static std::string getDescription(const BasicBlock &BB) {
199 return "basic block (" + BB.getName().str() + ") in function (" +
200 BB.getParent()->getName().str() + ")";
203 bool BasicBlockPass::skipBasicBlock(const BasicBlock &BB) const {
204 const Function *F = BB.getParent();
205 if (!F)
206 return false;
207 OptPassGate &Gate = F->getContext().getOptPassGate();
208 if (Gate.isEnabled() && !Gate.shouldRunPass(this, getDescription(BB)))
209 return true;
210 if (F->hasOptNone()) {
211 // Report this only once per function.
212 if (&BB == &F->getEntryBlock())
213 LLVM_DEBUG(dbgs() << "Skipping pass '" << getPassName()
214 << "' on function " << F->getName() << "\n");
215 return true;
217 return false;
220 PassManagerType BasicBlockPass::getPotentialPassManagerType() const {
221 return PMT_BasicBlockPassManager;
224 const PassInfo *Pass::lookupPassInfo(const void *TI) {
225 return PassRegistry::getPassRegistry()->getPassInfo(TI);
228 const PassInfo *Pass::lookupPassInfo(StringRef Arg) {
229 return PassRegistry::getPassRegistry()->getPassInfo(Arg);
232 Pass *Pass::createPass(AnalysisID ID) {
233 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
234 if (!PI)
235 return nullptr;
236 return PI->createPass();
239 //===----------------------------------------------------------------------===//
240 // Analysis Group Implementation Code
241 //===----------------------------------------------------------------------===//
243 // RegisterAGBase implementation
245 RegisterAGBase::RegisterAGBase(StringRef Name, const void *InterfaceID,
246 const void *PassID, bool isDefault)
247 : PassInfo(Name, InterfaceID) {
248 PassRegistry::getPassRegistry()->registerAnalysisGroup(InterfaceID, PassID,
249 *this, isDefault);
252 //===----------------------------------------------------------------------===//
253 // PassRegistrationListener implementation
256 // enumeratePasses - Iterate over the registered passes, calling the
257 // passEnumerate callback on each PassInfo object.
258 void PassRegistrationListener::enumeratePasses() {
259 PassRegistry::getPassRegistry()->enumerateWith(this);
262 PassNameParser::PassNameParser(cl::Option &O)
263 : cl::parser<const PassInfo *>(O) {
264 PassRegistry::getPassRegistry()->addRegistrationListener(this);
267 // This only gets called during static destruction, in which case the
268 // PassRegistry will have already been destroyed by llvm_shutdown(). So
269 // attempting to remove the registration listener is an error.
270 PassNameParser::~PassNameParser() = default;
272 //===----------------------------------------------------------------------===//
273 // AnalysisUsage Class Implementation
276 namespace {
278 struct GetCFGOnlyPasses : public PassRegistrationListener {
279 using VectorType = AnalysisUsage::VectorType;
281 VectorType &CFGOnlyList;
283 GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
285 void passEnumerate(const PassInfo *P) override {
286 if (P->isCFGOnlyPass())
287 CFGOnlyList.push_back(P->getTypeInfo());
291 } // end anonymous namespace
293 // setPreservesCFG - This function should be called to by the pass, iff they do
294 // not:
296 // 1. Add or remove basic blocks from the function
297 // 2. Modify terminator instructions in any way.
299 // This function annotates the AnalysisUsage info object to say that analyses
300 // that only depend on the CFG are preserved by this pass.
301 void AnalysisUsage::setPreservesCFG() {
302 // Since this transformation doesn't modify the CFG, it preserves all analyses
303 // that only depend on the CFG (like dominators, loop info, etc...)
304 GetCFGOnlyPasses(Preserved).enumeratePasses();
307 AnalysisUsage &AnalysisUsage::addPreserved(StringRef Arg) {
308 const PassInfo *PI = Pass::lookupPassInfo(Arg);
309 // If the pass exists, preserve it. Otherwise silently do nothing.
310 if (PI) Preserved.push_back(PI->getTypeInfo());
311 return *this;
314 AnalysisUsage &AnalysisUsage::addRequiredID(const void *ID) {
315 Required.push_back(ID);
316 return *this;
319 AnalysisUsage &AnalysisUsage::addRequiredID(char &ID) {
320 Required.push_back(&ID);
321 return *this;
324 AnalysisUsage &AnalysisUsage::addRequiredTransitiveID(char &ID) {
325 Required.push_back(&ID);
326 RequiredTransitive.push_back(&ID);
327 return *this;