Home | History | Annotate | Download | only in IR
      1 //===- Pass.cpp - LLVM Pass Infrastructure Implementation -----------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file implements the LLVM Pass infrastructure.  It is primarily
     11 // responsible with ensuring that passes are executed and batched together
     12 // optimally.
     13 //
     14 //===----------------------------------------------------------------------===//
     15 
     16 #include "llvm/Pass.h"
     17 #include "llvm/IR/Function.h"
     18 #include "llvm/IR/IRPrintingPasses.h"
     19 #include "llvm/IR/LegacyPassNameParser.h"
     20 #include "llvm/IR/Module.h"
     21 #include "llvm/IR/OptBisect.h"
     22 #include "llvm/PassRegistry.h"
     23 #include "llvm/Support/Debug.h"
     24 #include "llvm/Support/raw_ostream.h"
     25 using namespace llvm;
     26 
     27 #define DEBUG_TYPE "ir"
     28 
     29 //===----------------------------------------------------------------------===//
     30 // Pass Implementation
     31 //
     32 
     33 // Force out-of-line virtual method.
     34 Pass::~Pass() {
     35   delete Resolver;
     36 }
     37 
     38 // Force out-of-line virtual method.
     39 ModulePass::~ModulePass() { }
     40 
     41 Pass *ModulePass::createPrinterPass(raw_ostream &O,
     42                                     const std::string &Banner) const {
     43   return createPrintModulePass(O, Banner);
     44 }
     45 
     46 PassManagerType ModulePass::getPotentialPassManagerType() const {
     47   return PMT_ModulePassManager;
     48 }
     49 
     50 bool ModulePass::skipModule(Module &M) const {
     51   return !M.getContext().getOptBisect().shouldRunPass(this, M);
     52 }
     53 
     54 bool Pass::mustPreserveAnalysisID(char &AID) const {
     55   return Resolver->getAnalysisIfAvailable(&AID, true) != nullptr;
     56 }
     57 
     58 // dumpPassStructure - Implement the -debug-pass=Structure option
     59 void Pass::dumpPassStructure(unsigned Offset) {
     60   dbgs().indent(Offset*2) << getPassName() << "\n";
     61 }
     62 
     63 /// getPassName - Return a nice clean name for a pass.  This usually
     64 /// implemented in terms of the name that is registered by one of the
     65 /// Registration templates, but can be overloaded directly.
     66 ///
     67 const char *Pass::getPassName() const {
     68   AnalysisID AID =  getPassID();
     69   const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
     70   if (PI)
     71     return PI->getPassName();
     72   return "Unnamed pass: implement Pass::getPassName()";
     73 }
     74 
     75 void Pass::preparePassManager(PMStack &) {
     76   // By default, don't do anything.
     77 }
     78 
     79 PassManagerType Pass::getPotentialPassManagerType() const {
     80   // Default implementation.
     81   return PMT_Unknown;
     82 }
     83 
     84 void Pass::getAnalysisUsage(AnalysisUsage &) const {
     85   // By default, no analysis results are used, all are invalidated.
     86 }
     87 
     88 void Pass::releaseMemory() {
     89   // By default, don't do anything.
     90 }
     91 
     92 void Pass::verifyAnalysis() const {
     93   // By default, don't do anything.
     94 }
     95 
     96 void *Pass::getAdjustedAnalysisPointer(AnalysisID AID) {
     97   return this;
     98 }
     99 
    100 ImmutablePass *Pass::getAsImmutablePass() {
    101   return nullptr;
    102 }
    103 
    104 PMDataManager *Pass::getAsPMDataManager() {
    105   return nullptr;
    106 }
    107 
    108 void Pass::setResolver(AnalysisResolver *AR) {
    109   assert(!Resolver && "Resolver is already set");
    110   Resolver = AR;
    111 }
    112 
    113 // print - Print out the internal state of the pass.  This is called by Analyze
    114 // to print out the contents of an analysis.  Otherwise it is not necessary to
    115 // implement this method.
    116 //
    117 void Pass::print(raw_ostream &O,const Module*) const {
    118   O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
    119 }
    120 
    121 // dump - call print(cerr);
    122 LLVM_DUMP_METHOD void Pass::dump() const {
    123   print(dbgs(), nullptr);
    124 }
    125 
    126 //===----------------------------------------------------------------------===//
    127 // ImmutablePass Implementation
    128 //
    129 // Force out-of-line virtual method.
    130 ImmutablePass::~ImmutablePass() { }
    131 
    132 void ImmutablePass::initializePass() {
    133   // By default, don't do anything.
    134 }
    135 
    136 //===----------------------------------------------------------------------===//
    137 // FunctionPass Implementation
    138 //
    139 
    140 Pass *FunctionPass::createPrinterPass(raw_ostream &O,
    141                                       const std::string &Banner) const {
    142   return createPrintFunctionPass(O, Banner);
    143 }
    144 
    145 PassManagerType FunctionPass::getPotentialPassManagerType() const {
    146   return PMT_FunctionPassManager;
    147 }
    148 
    149 bool FunctionPass::skipFunction(const Function &F) const {
    150   if (!F.getContext().getOptBisect().shouldRunPass(this, F))
    151     return true;
    152 
    153   if (F.hasFnAttribute(Attribute::OptimizeNone)) {
    154     DEBUG(dbgs() << "Skipping pass '" << getPassName() << "' on function "
    155                  << F.getName() << "\n");
    156     return true;
    157   }
    158   return false;
    159 }
    160 
    161 //===----------------------------------------------------------------------===//
    162 // BasicBlockPass Implementation
    163 //
    164 
    165 Pass *BasicBlockPass::createPrinterPass(raw_ostream &O,
    166                                         const std::string &Banner) const {
    167   return createPrintBasicBlockPass(O, Banner);
    168 }
    169 
    170 bool BasicBlockPass::doInitialization(Function &) {
    171   // By default, don't do anything.
    172   return false;
    173 }
    174 
    175 bool BasicBlockPass::doFinalization(Function &) {
    176   // By default, don't do anything.
    177   return false;
    178 }
    179 
    180 bool BasicBlockPass::skipBasicBlock(const BasicBlock &BB) const {
    181   const Function *F = BB.getParent();
    182   if (!F)
    183     return false;
    184   if (!F->getContext().getOptBisect().shouldRunPass(this, BB))
    185     return true;
    186   if (F->hasFnAttribute(Attribute::OptimizeNone)) {
    187     // Report this only once per function.
    188     if (&BB == &F->getEntryBlock())
    189       DEBUG(dbgs() << "Skipping pass '" << getPassName()
    190             << "' on function " << F->getName() << "\n");
    191     return true;
    192   }
    193   return false;
    194 }
    195 
    196 PassManagerType BasicBlockPass::getPotentialPassManagerType() const {
    197   return PMT_BasicBlockPassManager;
    198 }
    199 
    200 const PassInfo *Pass::lookupPassInfo(const void *TI) {
    201   return PassRegistry::getPassRegistry()->getPassInfo(TI);
    202 }
    203 
    204 const PassInfo *Pass::lookupPassInfo(StringRef Arg) {
    205   return PassRegistry::getPassRegistry()->getPassInfo(Arg);
    206 }
    207 
    208 Pass *Pass::createPass(AnalysisID ID) {
    209   const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
    210   if (!PI)
    211     return nullptr;
    212   return PI->createPass();
    213 }
    214 
    215 //===----------------------------------------------------------------------===//
    216 //                  Analysis Group Implementation Code
    217 //===----------------------------------------------------------------------===//
    218 
    219 // RegisterAGBase implementation
    220 //
    221 RegisterAGBase::RegisterAGBase(const char *Name, const void *InterfaceID,
    222                                const void *PassID, bool isDefault)
    223     : PassInfo(Name, InterfaceID) {
    224   PassRegistry::getPassRegistry()->registerAnalysisGroup(InterfaceID, PassID,
    225                                                          *this, isDefault);
    226 }
    227 
    228 //===----------------------------------------------------------------------===//
    229 // PassRegistrationListener implementation
    230 //
    231 
    232 // enumeratePasses - Iterate over the registered passes, calling the
    233 // passEnumerate callback on each PassInfo object.
    234 //
    235 void PassRegistrationListener::enumeratePasses() {
    236   PassRegistry::getPassRegistry()->enumerateWith(this);
    237 }
    238 
    239 PassNameParser::PassNameParser(cl::Option &O)
    240     : cl::parser<const PassInfo *>(O) {
    241   PassRegistry::getPassRegistry()->addRegistrationListener(this);
    242 }
    243 
    244 PassNameParser::~PassNameParser() {
    245   // This only gets called during static destruction, in which case the
    246   // PassRegistry will have already been destroyed by llvm_shutdown().  So
    247   // attempting to remove the registration listener is an error.
    248 }
    249 
    250 //===----------------------------------------------------------------------===//
    251 //   AnalysisUsage Class Implementation
    252 //
    253 
    254 namespace {
    255   struct GetCFGOnlyPasses : public PassRegistrationListener {
    256     typedef AnalysisUsage::VectorType VectorType;
    257     VectorType &CFGOnlyList;
    258     GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
    259 
    260     void passEnumerate(const PassInfo *P) override {
    261       if (P->isCFGOnlyPass())
    262         CFGOnlyList.push_back(P->getTypeInfo());
    263     }
    264   };
    265 }
    266 
    267 // setPreservesCFG - This function should be called to by the pass, iff they do
    268 // not:
    269 //
    270 //  1. Add or remove basic blocks from the function
    271 //  2. Modify terminator instructions in any way.
    272 //
    273 // This function annotates the AnalysisUsage info object to say that analyses
    274 // that only depend on the CFG are preserved by this pass.
    275 //
    276 void AnalysisUsage::setPreservesCFG() {
    277   // Since this transformation doesn't modify the CFG, it preserves all analyses
    278   // that only depend on the CFG (like dominators, loop info, etc...)
    279   GetCFGOnlyPasses(Preserved).enumeratePasses();
    280 }
    281 
    282 AnalysisUsage &AnalysisUsage::addPreserved(StringRef Arg) {
    283   const PassInfo *PI = Pass::lookupPassInfo(Arg);
    284   // If the pass exists, preserve it. Otherwise silently do nothing.
    285   if (PI) Preserved.push_back(PI->getTypeInfo());
    286   return *this;
    287 }
    288 
    289 AnalysisUsage &AnalysisUsage::addRequiredID(const void *ID) {
    290   Required.push_back(ID);
    291   return *this;
    292 }
    293 
    294 AnalysisUsage &AnalysisUsage::addRequiredID(char &ID) {
    295   Required.push_back(&ID);
    296   return *this;
    297 }
    298 
    299 AnalysisUsage &AnalysisUsage::addRequiredTransitiveID(char &ID) {
    300   Required.push_back(&ID);
    301   RequiredTransitive.push_back(&ID);
    302   return *this;
    303 }
    304