LCOV - code coverage report
Current view: top level - lib/IR - Pass.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 85 107 79.4 %
Date: 2018-06-17 00:07:59 Functions: 30 41 73.2 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13