LCOV - code coverage report
Current view: top level - lib/Analysis - MemDerefPrinter.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 32 34 94.1 %
Date: 2017-09-14 15:23:50 Functions: 9 10 90.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- MemDerefPrinter.cpp - Printer for isDereferenceablePointer ---------===//
       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             : #include "llvm/ADT/SetVector.h"
      11             : #include "llvm/Analysis/Loads.h"
      12             : #include "llvm/Analysis/MemoryDependenceAnalysis.h"
      13             : #include "llvm/Analysis/Passes.h"
      14             : #include "llvm/IR/CallSite.h"
      15             : #include "llvm/IR/DataLayout.h"
      16             : #include "llvm/IR/InstIterator.h"
      17             : #include "llvm/IR/LLVMContext.h"
      18             : #include "llvm/IR/Module.h"
      19             : #include "llvm/Support/ErrorHandling.h"
      20             : #include "llvm/Support/raw_ostream.h"
      21             : using namespace llvm;
      22             : 
      23             : namespace {
      24           5 :   struct MemDerefPrinter : public FunctionPass {
      25             :     SmallVector<Value *, 4> Deref;
      26             :     SmallPtrSet<Value *, 4> DerefAndAligned;
      27             : 
      28             :     static char ID; // Pass identification, replacement for typeid
      29           4 :     MemDerefPrinter() : FunctionPass(ID) {
      30           1 :       initializeMemDerefPrinterPass(*PassRegistry::getPassRegistry());
      31           1 :     }
      32           1 :     void getAnalysisUsage(AnalysisUsage &AU) const override {
      33           1 :       AU.setPreservesAll();
      34           1 :     }
      35             :     bool runOnFunction(Function &F) override;
      36             :     void print(raw_ostream &OS, const Module * = nullptr) const override;
      37           2 :     void releaseMemory() override {
      38           4 :       Deref.clear();
      39           2 :       DerefAndAligned.clear();
      40           2 :     }
      41             :   };
      42             : }
      43             : 
      44             : char MemDerefPrinter::ID = 0;
      45        9158 : INITIALIZE_PASS_BEGIN(MemDerefPrinter, "print-memderefs",
      46             :                       "Memory Dereferenciblity of pointers in function", false, true)
      47       45793 : INITIALIZE_PASS_END(MemDerefPrinter, "print-memderefs",
      48             :                     "Memory Dereferenciblity of pointers in function", false, true)
      49             : 
      50           0 : FunctionPass *llvm::createMemDerefPrinter() {
      51           0 :   return new MemDerefPrinter();
      52             : }
      53             : 
      54           2 : bool MemDerefPrinter::runOnFunction(Function &F) {
      55           2 :   const DataLayout &DL = F.getParent()->getDataLayout();
      56         126 :   for (auto &I: instructions(F)) {
      57          36 :     if (LoadInst *LI = dyn_cast<LoadInst>(&I)) {
      58          36 :       Value *PO = LI->getPointerOperand();
      59          36 :       if (isDereferenceablePointer(PO, DL))
      60          29 :         Deref.push_back(PO);
      61          36 :       if (isDereferenceableAndAlignedPointer(PO, LI->getAlignment(), DL))
      62          21 :         DerefAndAligned.insert(PO);
      63             :     }
      64             :   }
      65           2 :   return false;
      66             : }
      67             : 
      68           2 : void MemDerefPrinter::print(raw_ostream &OS, const Module *M) const {
      69           2 :   OS << "The following are dereferenceable:\n";
      70          35 :   for (Value *V: Deref) {
      71          29 :     V->print(OS);
      72          29 :     if (DerefAndAligned.count(V))
      73          21 :       OS << "\t(aligned)";
      74             :     else
      75           8 :       OS << "\t(unaligned)";
      76          29 :     OS << "\n\n";
      77             :   }
      78           2 : }

Generated by: LCOV version 1.13