Home | History | Annotate | Line # | Download | only in IR
      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.
     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 using namespace llvm;
     31 
     32 #define DEBUG_TYPE "ir"
     33 
     34 //===----------------------------------------------------------------------===//
     35 // Pass Implementation
     36 //
     37 
     38 // Force out-of-line virtual method.
     39 Pass::~Pass() {
     40   delete Resolver;
     41 }
     42 
     43 // Force out-of-line virtual method.
     44 ModulePass::~ModulePass() = default;
     45 
     46 Pass *ModulePass::createPrinterPass(raw_ostream &OS,
     47                                     const std::string &Banner) const {
     48   return createPrintModulePass(OS, Banner);
     49 }
     50 
     51 PassManagerType ModulePass::getPotentialPassManagerType() const {
     52   return PMT_ModulePassManager;
     53 }
     54 
     55 static std::string getDescription(const Module &M) {
     56   return "module (" + M.getName().str() + ")";
     57 }
     58 
     59 bool ModulePass::skipModule(Module &M) const {
     60   OptPassGate &Gate = M.getContext().getOptPassGate();
     61   return Gate.isEnabled() && !Gate.shouldRunPass(this, getDescription(M));
     62 }
     63 
     64 bool Pass::mustPreserveAnalysisID(char &AID) const {
     65   return Resolver->getAnalysisIfAvailable(&AID) != nullptr;
     66 }
     67 
     68 // dumpPassStructure - Implement the -debug-pass=Structure option
     69 void Pass::dumpPassStructure(unsigned Offset) {
     70   dbgs().indent(Offset*2) << getPassName() << "\n";
     71 }
     72 
     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()";
     82 }
     83 
     84 void Pass::preparePassManager(PMStack &) {
     85   // By default, don't do anything.
     86 }
     87 
     88 PassManagerType Pass::getPotentialPassManagerType() const {
     89   // Default implementation.
     90   return PMT_Unknown;
     91 }
     92 
     93 void Pass::getAnalysisUsage(AnalysisUsage &) const {
     94   // By default, no analysis results are used, all are invalidated.
     95 }
     96 
     97 void Pass::releaseMemory() {
     98   // By default, don't do anything.
     99 }
    100 
    101 void Pass::verifyAnalysis() const {
    102   // By default, don't do anything.
    103 }
    104 
    105 void *Pass::getAdjustedAnalysisPointer(AnalysisID AID) {
    106   return this;
    107 }
    108 
    109 ImmutablePass *Pass::getAsImmutablePass() {
    110   return nullptr;
    111 }
    112 
    113 PMDataManager *Pass::getAsPMDataManager() {
    114   return nullptr;
    115 }
    116 
    117 void Pass::setResolver(AnalysisResolver *AR) {
    118   assert(!Resolver && "Resolver is already set");
    119   Resolver = AR;
    120 }
    121 
    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";
    127 }
    128 
    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);
    133 }
    134 #endif
    135 
    136 //===----------------------------------------------------------------------===//
    137 // ImmutablePass Implementation
    138 //
    139 // Force out-of-line virtual method.
    140 ImmutablePass::~ImmutablePass() = default;
    141 
    142 void ImmutablePass::initializePass() {
    143   // By default, don't do anything.
    144 }
    145 
    146 //===----------------------------------------------------------------------===//
    147 // FunctionPass Implementation
    148 //
    149 
    150 Pass *FunctionPass::createPrinterPass(raw_ostream &OS,
    151                                       const std::string &Banner) const {
    152   return createPrintFunctionPass(OS, Banner);
    153 }
    154 
    155 PassManagerType FunctionPass::getPotentialPassManagerType() const {
    156   return PMT_FunctionPassManager;
    157 }
    158 
    159 static std::string getDescription(const Function &F) {
    160   return "function (" + F.getName().str() + ")";
    161 }
    162 
    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;
    167 
    168   if (F.hasOptNone()) {
    169     LLVM_DEBUG(dbgs() << "Skipping pass '" << getPassName() << "' on function "
    170                       << F.getName() << "\n");
    171     return true;
    172   }
    173   return false;
    174 }
    175 
    176 const PassInfo *Pass::lookupPassInfo(const void *TI) {
    177   return PassRegistry::getPassRegistry()->getPassInfo(TI);
    178 }
    179 
    180 const PassInfo *Pass::lookupPassInfo(StringRef Arg) {
    181   return PassRegistry::getPassRegistry()->getPassInfo(Arg);
    182 }
    183 
    184 Pass *Pass::createPass(AnalysisID ID) {
    185   const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
    186   if (!PI)
    187     return nullptr;
    188   return PI->createPass();
    189 }
    190 
    191 //===----------------------------------------------------------------------===//
    192 //                  Analysis Group Implementation Code
    193 //===----------------------------------------------------------------------===//
    194 
    195 // RegisterAGBase implementation
    196 
    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);
    202 }
    203 
    204 //===----------------------------------------------------------------------===//
    205 // PassRegistrationListener implementation
    206 //
    207 
    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);
    212 }
    213 
    214 PassNameParser::PassNameParser(cl::Option &O)
    215     : cl::parser<const PassInfo *>(O) {
    216   PassRegistry::getPassRegistry()->addRegistrationListener(this);
    217 }
    218 
    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;
    223 
    224 //===----------------------------------------------------------------------===//
    225 //   AnalysisUsage Class Implementation
    226 //
    227 
    228 namespace {
    229 
    230 struct GetCFGOnlyPasses : public PassRegistrationListener {
    231   using VectorType = AnalysisUsage::VectorType;
    232 
    233   VectorType &CFGOnlyList;
    234 
    235   GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
    236 
    237   void passEnumerate(const PassInfo *P) override {
    238     if (P->isCFGOnlyPass())
    239       CFGOnlyList.push_back(P->getTypeInfo());
    240   }
    241 };
    242 
    243 } // end anonymous namespace
    244 
    245 // setPreservesCFG - This function should be called to by the pass, iff they do
    246 // not:
    247 //
    248 //  1. Add or remove basic blocks from the function
    249 //  2. Modify terminator instructions in any way.
    250 //
    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();
    257 }
    258 
    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;
    265 }
    266 
    267 AnalysisUsage &AnalysisUsage::addRequiredID(const void *ID) {
    268   pushUnique(Required, ID);
    269   return *this;
    270 }
    271 
    272 AnalysisUsage &AnalysisUsage::addRequiredID(char &ID) {
    273   pushUnique(Required, &ID);
    274   return *this;
    275 }
    276 
    277 AnalysisUsage &AnalysisUsage::addRequiredTransitiveID(char &ID) {
    278   pushUnique(Required, &ID);
    279   pushUnique(RequiredTransitive, &ID);
    280   return *this;
    281 }
    282