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

Generated by: LCOV version 1.13