317 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8//9// This file implements the LLVM Pass infrastructure. It is primarily10// responsible with ensuring that passes are executed and batched together11// optimally.12//13//===----------------------------------------------------------------------===//14 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>29 30#ifdef EXPENSIVE_CHECKS31#include "llvm/IR/StructuralHash.h"32#endif33 34using namespace llvm;35 36#define DEBUG_TYPE "ir"37 38//===----------------------------------------------------------------------===//39// Pass Implementation40//41 42// Force out-of-line virtual method.43Pass::~Pass() {44 delete Resolver;45}46 47// Force out-of-line virtual method.48ModulePass::~ModulePass() = default;49 50Pass *ModulePass::createPrinterPass(raw_ostream &OS,51 const std::string &Banner) const {52 return createPrintModulePass(OS, Banner);53}54 55PassManagerType ModulePass::getPotentialPassManagerType() const {56 return PMT_ModulePassManager;57}58 59static std::string getDescription(const Module &M) {60 return "module (" + M.getName().str() + ")";61}62 63bool ModulePass::skipModule(const Module &M) const {64 const OptPassGate &Gate = M.getContext().getOptPassGate();65 66 StringRef PassName = getPassArgument();67 if (PassName.empty())68 PassName = this->getPassName();69 70 return Gate.isEnabled() && !Gate.shouldRunPass(PassName, getDescription(M));71}72 73bool Pass::mustPreserveAnalysisID(char &AID) const {74 return Resolver->getAnalysisIfAvailable(&AID) != nullptr;75}76 77// dumpPassStructure - Implement the -debug-pass=Structure option78void Pass::dumpPassStructure(unsigned Offset) {79 dbgs().indent(Offset*2) << getPassName() << "\n";80}81 82/// getPassName - Return a nice clean name for a pass. This usually83/// implemented in terms of the name that is registered by one of the84/// Registration templates, but can be overloaded directly.85StringRef Pass::getPassName() const {86 AnalysisID AID = getPassID();87 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(AID);88 if (PI)89 return PI->getPassName();90 return "Unnamed pass: implement Pass::getPassName()";91}92 93/// getPassArgument - Return a nice clean name for a pass94/// corresponding to that used to enable the pass in opt95StringRef Pass::getPassArgument() const {96 AnalysisID AID = getPassID();97 const PassInfo *PI = Pass::lookupPassInfo(AID);98 if (PI)99 return PI->getPassArgument();100 return "";101}102 103void Pass::preparePassManager(PMStack &) {104 // By default, don't do anything.105}106 107PassManagerType Pass::getPotentialPassManagerType() const {108 // Default implementation.109 return PMT_Unknown;110}111 112void Pass::getAnalysisUsage(AnalysisUsage &) const {113 // By default, no analysis results are used, all are invalidated.114}115 116void Pass::releaseMemory() {117 // By default, don't do anything.118}119 120void Pass::verifyAnalysis() const {121 // By default, don't do anything.122}123 124ImmutablePass *Pass::getAsImmutablePass() {125 return nullptr;126}127 128PMDataManager *Pass::getAsPMDataManager() {129 return nullptr;130}131 132void Pass::setResolver(AnalysisResolver *AR) {133 assert(!Resolver && "Resolver is already set");134 Resolver = AR;135}136 137// print - Print out the internal state of the pass. This is called by Analyze138// to print out the contents of an analysis. Otherwise it is not necessary to139// implement this method.140void Pass::print(raw_ostream &OS, const Module *) const {141 OS << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";142}143 144#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)145// dump - call print(cerr);146LLVM_DUMP_METHOD void Pass::dump() const {147 print(dbgs(), nullptr);148}149#endif150 151#ifdef EXPENSIVE_CHECKS152uint64_t Pass::structuralHash(Module &M) const {153 return StructuralHash(M, true);154}155 156uint64_t Pass::structuralHash(Function &F) const {157 return StructuralHash(F, true);158}159#endif160 161//===----------------------------------------------------------------------===//162// ImmutablePass Implementation163//164// Force out-of-line virtual method.165ImmutablePass::~ImmutablePass() = default;166 167void ImmutablePass::initializePass() {168 // By default, don't do anything.169}170 171//===----------------------------------------------------------------------===//172// FunctionPass Implementation173//174 175Pass *FunctionPass::createPrinterPass(raw_ostream &OS,176 const std::string &Banner) const {177 return createPrintFunctionPass(OS, Banner);178}179 180PassManagerType FunctionPass::getPotentialPassManagerType() const {181 return PMT_FunctionPassManager;182}183 184static std::string getDescription(const Function &F) {185 return "function (" + F.getName().str() + ")";186}187 188bool FunctionPass::skipFunction(const Function &F) const {189 OptPassGate &Gate = F.getContext().getOptPassGate();190 191 StringRef PassName = getPassArgument();192 if (PassName.empty())193 PassName = this->getPassName();194 195 if (Gate.isEnabled() && !Gate.shouldRunPass(PassName, getDescription(F)))196 return true;197 198 if (F.hasOptNone()) {199 LLVM_DEBUG(dbgs() << "Skipping pass '" << getPassName() << "' on function "200 << F.getName() << "\n");201 return true;202 }203 return false;204}205 206const PassInfo *Pass::lookupPassInfo(const void *TI) {207 return PassRegistry::getPassRegistry()->getPassInfo(TI);208}209 210const PassInfo *Pass::lookupPassInfo(StringRef Arg) {211 return PassRegistry::getPassRegistry()->getPassInfo(Arg);212}213 214Pass *Pass::createPass(AnalysisID ID) {215 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);216 if (!PI)217 return nullptr;218 return PI->createPass();219}220 221//===----------------------------------------------------------------------===//222// PassRegistrationListener implementation223//224 225// enumeratePasses - Iterate over the registered passes, calling the226// passEnumerate callback on each PassInfo object.227void PassRegistrationListener::enumeratePasses() {228 PassRegistry::getPassRegistry()->enumerateWith(this);229}230 231PassNameParser::PassNameParser(cl::Option &O)232 : cl::parser<const PassInfo *>(O) {233 PassRegistry::getPassRegistry()->addRegistrationListener(this);234}235 236// This only gets called during static destruction, in which case the237// PassRegistry will have already been destroyed by llvm_shutdown(). So238// attempting to remove the registration listener is an error.239PassNameParser::~PassNameParser() = default;240 241//===----------------------------------------------------------------------===//242// AnalysisUsage Class Implementation243//244 245namespace {246 247struct GetCFGOnlyPasses : public PassRegistrationListener {248 using VectorType = AnalysisUsage::VectorType;249 250 VectorType &CFGOnlyList;251 252 GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}253 254 void passEnumerate(const PassInfo *P) override {255 if (P->isCFGOnlyPass())256 CFGOnlyList.push_back(P->getTypeInfo());257 }258};259 260} // end anonymous namespace261 262// setPreservesCFG - This function should be called to by the pass, iff they do263// not:264//265// 1. Add or remove basic blocks from the function266// 2. Modify terminator instructions in any way.267//268// This function annotates the AnalysisUsage info object to say that analyses269// that only depend on the CFG are preserved by this pass.270void AnalysisUsage::setPreservesCFG() {271 // Since this transformation doesn't modify the CFG, it preserves all analyses272 // that only depend on the CFG (like dominators, loop info, etc...)273 GetCFGOnlyPasses(Preserved).enumeratePasses();274}275 276AnalysisUsage &AnalysisUsage::addPreserved(StringRef Arg) {277 const PassInfo *PI = Pass::lookupPassInfo(Arg);278 // If the pass exists, preserve it. Otherwise silently do nothing.279 if (PI)280 pushUnique(Preserved, PI->getTypeInfo());281 return *this;282}283 284AnalysisUsage &AnalysisUsage::addRequiredID(const void *ID) {285 pushUnique(Required, ID);286 return *this;287}288 289AnalysisUsage &AnalysisUsage::addRequiredID(char &ID) {290 pushUnique(Required, &ID);291 return *this;292}293 294AnalysisUsage &AnalysisUsage::addRequiredTransitiveID(char &ID) {295 pushUnique(Required, &ID);296 pushUnique(RequiredTransitive, &ID);297 return *this;298}299 300#ifndef NDEBUG301const char *llvm::to_string(ThinOrFullLTOPhase Phase) {302 switch (Phase) {303 case ThinOrFullLTOPhase::None:304 return "None";305 case ThinOrFullLTOPhase::ThinLTOPreLink:306 return "ThinLTOPreLink";307 case ThinOrFullLTOPhase::ThinLTOPostLink:308 return "ThinLTOPostLink";309 case ThinOrFullLTOPhase::FullLTOPreLink:310 return "FullLTOPreLink";311 case ThinOrFullLTOPhase::FullLTOPostLink:312 return "FullLTOPostLink";313 }314 llvm_unreachable("invalid phase");315}316#endif317