LCOV - code coverage report
Current view: top level - include/llvm/Analysis - MemoryBuiltins.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 11 11 100.0 %
Date: 2017-09-14 15:23:50 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //==- llvm/Analysis/MemoryBuiltins.h - Calls to memory builtins --*- C++ -*-==//
       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 family of functions identifies calls to builtin functions that allocate
      11             : // or free memory.
      12             : //
      13             : //===----------------------------------------------------------------------===//
      14             : 
      15             : #ifndef LLVM_ANALYSIS_MEMORYBUILTINS_H
      16             : #define LLVM_ANALYSIS_MEMORYBUILTINS_H
      17             : 
      18             : #include "llvm/ADT/APInt.h"
      19             : #include "llvm/ADT/DenseMap.h"
      20             : #include "llvm/ADT/SmallPtrSet.h"
      21             : #include "llvm/Analysis/TargetFolder.h"
      22             : #include "llvm/IR/CallSite.h"
      23             : #include "llvm/IR/IRBuilder.h"
      24             : #include "llvm/IR/InstVisitor.h"
      25             : #include "llvm/IR/ValueHandle.h"
      26             : #include <cstdint>
      27             : #include <utility>
      28             : 
      29             : namespace llvm {
      30             : 
      31             : class AllocaInst;
      32             : class Argument;
      33             : class CallInst;
      34             : class ConstantInt;
      35             : class ConstantPointerNull;
      36             : class DataLayout;
      37             : class ExtractElementInst;
      38             : class ExtractValueInst;
      39             : class GEPOperator;
      40             : class GlobalAlias;
      41             : class GlobalVariable;
      42             : class Instruction;
      43             : class IntegerType;
      44             : class IntrinsicInst;
      45             : class IntToPtrInst;
      46             : class LLVMContext;
      47             : class LoadInst;
      48             : class PHINode;
      49             : class PointerType;
      50             : class SelectInst;
      51             : class TargetLibraryInfo;
      52             : class Type;
      53             : class UndefValue;
      54             : class Value;
      55             : 
      56             : /// \brief Tests if a value is a call or invoke to a library function that
      57             : /// allocates or reallocates memory (either malloc, calloc, realloc, or strdup
      58             : /// like).
      59             : bool isAllocationFn(const Value *V, const TargetLibraryInfo *TLI,
      60             :                     bool LookThroughBitCast = false);
      61             : 
      62             : /// \brief Tests if a value is a call or invoke to a function that returns a
      63             : /// NoAlias pointer (including malloc/calloc/realloc/strdup-like functions).
      64             : bool isNoAliasFn(const Value *V, const TargetLibraryInfo *TLI,
      65             :                  bool LookThroughBitCast = false);
      66             : 
      67             : /// \brief Tests if a value is a call or invoke to a library function that
      68             : /// allocates uninitialized memory (such as malloc).
      69             : bool isMallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
      70             :                     bool LookThroughBitCast = false);
      71             : 
      72             : /// \brief Tests if a value is a call or invoke to a library function that
      73             : /// allocates zero-filled memory (such as calloc).
      74             : bool isCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
      75             :                     bool LookThroughBitCast = false);
      76             : 
      77             : /// \brief Tests if a value is a call or invoke to a library function that
      78             : /// allocates memory similar to malloc or calloc.
      79             : bool isMallocOrCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
      80             :                             bool LookThroughBitCast = false);
      81             : 
      82             : /// \brief Tests if a value is a call or invoke to a library function that
      83             : /// allocates memory (either malloc, calloc, or strdup like).
      84             : bool isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
      85             :                    bool LookThroughBitCast = false);
      86             : 
      87             : //===----------------------------------------------------------------------===//
      88             : //  malloc Call Utility Functions.
      89             : //
      90             : 
      91             : /// extractMallocCall - Returns the corresponding CallInst if the instruction
      92             : /// is a malloc call.  Since CallInst::CreateMalloc() only creates calls, we
      93             : /// ignore InvokeInst here.
      94             : const CallInst *extractMallocCall(const Value *I, const TargetLibraryInfo *TLI);
      95             : static inline CallInst *extractMallocCall(Value *I,
      96             :                                           const TargetLibraryInfo *TLI) {
      97          74 :   return const_cast<CallInst*>(extractMallocCall((const Value*)I, TLI));
      98             : }
      99             : 
     100             : /// getMallocType - Returns the PointerType resulting from the malloc call.
     101             : /// The PointerType depends on the number of bitcast uses of the malloc call:
     102             : ///   0: PointerType is the malloc calls' return type.
     103             : ///   1: PointerType is the bitcast's result type.
     104             : ///  >1: Unique PointerType cannot be determined, return NULL.
     105             : PointerType *getMallocType(const CallInst *CI, const TargetLibraryInfo *TLI);
     106             : 
     107             : /// getMallocAllocatedType - Returns the Type allocated by malloc call.
     108             : /// The Type depends on the number of bitcast uses of the malloc call:
     109             : ///   0: PointerType is the malloc calls' return type.
     110             : ///   1: PointerType is the bitcast's result type.
     111             : ///  >1: Unique PointerType cannot be determined, return NULL.
     112             : Type *getMallocAllocatedType(const CallInst *CI, const TargetLibraryInfo *TLI);
     113             : 
     114             : /// getMallocArraySize - Returns the array size of a malloc call.  If the
     115             : /// argument passed to malloc is a multiple of the size of the malloced type,
     116             : /// then return that multiple.  For non-array mallocs, the multiple is
     117             : /// constant 1.  Otherwise, return NULL for mallocs whose array size cannot be
     118             : /// determined.
     119             : Value *getMallocArraySize(CallInst *CI, const DataLayout &DL,
     120             :                           const TargetLibraryInfo *TLI,
     121             :                           bool LookThroughSExt = false);
     122             : 
     123             : //===----------------------------------------------------------------------===//
     124             : //  calloc Call Utility Functions.
     125             : //
     126             : 
     127             : /// extractCallocCall - Returns the corresponding CallInst if the instruction
     128             : /// is a calloc call.
     129             : const CallInst *extractCallocCall(const Value *I, const TargetLibraryInfo *TLI);
     130             : static inline CallInst *extractCallocCall(Value *I,
     131             :                                           const TargetLibraryInfo *TLI) {
     132             :   return const_cast<CallInst*>(extractCallocCall((const Value*)I, TLI));
     133             : }
     134             : 
     135             : 
     136             : //===----------------------------------------------------------------------===//
     137             : //  free Call Utility Functions.
     138             : //
     139             : 
     140             : /// isFreeCall - Returns non-null if the value is a call to the builtin free()
     141             : const CallInst *isFreeCall(const Value *I, const TargetLibraryInfo *TLI);
     142             : 
     143             : static inline CallInst *isFreeCall(Value *I, const TargetLibraryInfo *TLI) {
     144    19373699 :   return const_cast<CallInst*>(isFreeCall((const Value*)I, TLI));
     145             : }
     146             : 
     147             : //===----------------------------------------------------------------------===//
     148             : //  Utility functions to compute size of objects.
     149             : //
     150             : 
     151             : /// Various options to control the behavior of getObjectSize.
     152             : struct ObjectSizeOpts {
     153             :   /// Controls how we handle conditional statements with unknown conditions.
     154             :   enum class Mode : uint8_t {
     155             :     /// Fail to evaluate an unknown condition.
     156             :     Exact,
     157             :     /// Evaluate all branches of an unknown condition. If all evaluations
     158             :     /// succeed, pick the minimum size.
     159             :     Min,
     160             :     /// Same as Min, except we pick the maximum size of all of the branches.
     161             :     Max
     162             :   };
     163             : 
     164             :   /// How we want to evaluate this object's size.
     165             :   Mode EvalMode = Mode::Exact;
     166             :   /// Whether to round the result up to the alignment of allocas, byval
     167             :   /// arguments, and global variables.
     168             :   bool RoundToAlign = false;
     169             :   /// If this is true, null pointers in address space 0 will be treated as
     170             :   /// though they can't be evaluated. Otherwise, null is always considered to
     171             :   /// point to a 0 byte region of memory.
     172             :   bool NullIsUnknownSize = false;
     173             : };
     174             : 
     175             : /// \brief Compute the size of the object pointed by Ptr. Returns true and the
     176             : /// object size in Size if successful, and false otherwise. In this context, by
     177             : /// object we mean the region of memory starting at Ptr to the end of the
     178             : /// underlying object pointed to by Ptr.
     179             : bool getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout &DL,
     180             :                    const TargetLibraryInfo *TLI, ObjectSizeOpts Opts = {});
     181             : 
     182             : /// Try to turn a call to @llvm.objectsize into an integer value of the given
     183             : /// Type. Returns null on failure.
     184             : /// If MustSucceed is true, this function will not return null, and may return
     185             : /// conservative values governed by the second argument of the call to
     186             : /// objectsize.
     187             : ConstantInt *lowerObjectSizeCall(IntrinsicInst *ObjectSize,
     188             :                                  const DataLayout &DL,
     189             :                                  const TargetLibraryInfo *TLI,
     190             :                                  bool MustSucceed);
     191             : 
     192             : using SizeOffsetType = std::pair<APInt, APInt>;
     193             : 
     194             : /// \brief Evaluate the size and offset of an object pointed to by a Value*
     195             : /// statically. Fails if size or offset are not known at compile time.
     196    17095071 : class ObjectSizeOffsetVisitor
     197             :   : public InstVisitor<ObjectSizeOffsetVisitor, SizeOffsetType> {
     198             :   const DataLayout &DL;
     199             :   const TargetLibraryInfo *TLI;
     200             :   ObjectSizeOpts Options;
     201             :   unsigned IntTyBits;
     202             :   APInt Zero;
     203             :   SmallPtrSet<Instruction *, 8> SeenInsts;
     204             : 
     205             :   APInt align(APInt Size, uint64_t Align);
     206             : 
     207             :   SizeOffsetType unknown() {
     208      753908 :     return std::make_pair(APInt(), APInt());
     209             :   }
     210             : 
     211             : public:
     212             :   ObjectSizeOffsetVisitor(const DataLayout &DL, const TargetLibraryInfo *TLI,
     213             :                           LLVMContext &Context, ObjectSizeOpts Options = {});
     214             : 
     215             :   SizeOffsetType compute(Value *V);
     216             : 
     217             :   static bool knownSize(const SizeOffsetType &SizeOffset) {
     218     5697848 :     return SizeOffset.first.getBitWidth() > 1;
     219             :   }
     220             : 
     221             :   static bool knownOffset(const SizeOffsetType &SizeOffset) {
     222     5509374 :     return SizeOffset.second.getBitWidth() > 1;
     223             :   }
     224             : 
     225             :   static bool bothKnown(const SizeOffsetType &SizeOffset) {
     226    11207222 :     return knownSize(SizeOffset) && knownOffset(SizeOffset);
     227             :   }
     228             : 
     229             :   // These are "private", except they can't actually be made private. Only
     230             :   // compute() should be used by external users.
     231             :   SizeOffsetType visitAllocaInst(AllocaInst &I);
     232             :   SizeOffsetType visitArgument(Argument &A);
     233             :   SizeOffsetType visitCallSite(CallSite CS);
     234             :   SizeOffsetType visitConstantPointerNull(ConstantPointerNull&);
     235             :   SizeOffsetType visitExtractElementInst(ExtractElementInst &I);
     236             :   SizeOffsetType visitExtractValueInst(ExtractValueInst &I);
     237             :   SizeOffsetType visitGEPOperator(GEPOperator &GEP);
     238             :   SizeOffsetType visitGlobalAlias(GlobalAlias &GA);
     239             :   SizeOffsetType visitGlobalVariable(GlobalVariable &GV);
     240             :   SizeOffsetType visitIntToPtrInst(IntToPtrInst&);
     241             :   SizeOffsetType visitLoadInst(LoadInst &I);
     242             :   SizeOffsetType visitPHINode(PHINode&);
     243             :   SizeOffsetType visitSelectInst(SelectInst &I);
     244             :   SizeOffsetType visitUndefValue(UndefValue&);
     245             :   SizeOffsetType visitInstruction(Instruction &I);
     246             : 
     247             : private:
     248             :   bool CheckedZextOrTrunc(APInt &I);
     249             : };
     250             : 
     251             : using SizeOffsetEvalType = std::pair<Value *, Value *>;
     252             : 
     253             : /// \brief Evaluate the size and offset of an object pointed to by a Value*.
     254             : /// May create code to compute the result at run-time.
     255         104 : class ObjectSizeOffsetEvaluator
     256             :   : public InstVisitor<ObjectSizeOffsetEvaluator, SizeOffsetEvalType> {
     257             :   using BuilderTy = IRBuilder<TargetFolder>;
     258             :   using WeakEvalType = std::pair<WeakTrackingVH, WeakTrackingVH>;
     259             :   using CacheMapTy = DenseMap<const Value *, WeakEvalType>;
     260             :   using PtrSetTy = SmallPtrSet<const Value *, 8>;
     261             : 
     262             :   const DataLayout &DL;
     263             :   const TargetLibraryInfo *TLI;
     264             :   LLVMContext &Context;
     265             :   BuilderTy Builder;
     266             :   IntegerType *IntTy;
     267             :   Value *Zero;
     268             :   CacheMapTy CacheMap;
     269             :   PtrSetTy SeenVals;
     270             :   bool RoundToAlign;
     271             : 
     272             :   SizeOffsetEvalType unknown() {
     273          16 :     return std::make_pair(nullptr, nullptr);
     274             :   }
     275             : 
     276             :   SizeOffsetEvalType compute_(Value *V);
     277             : 
     278             : public:
     279             :   ObjectSizeOffsetEvaluator(const DataLayout &DL, const TargetLibraryInfo *TLI,
     280             :                             LLVMContext &Context, bool RoundToAlign = false);
     281             : 
     282             :   SizeOffsetEvalType compute(Value *V);
     283             : 
     284             :   bool knownSize(SizeOffsetEvalType SizeOffset) {
     285             :     return SizeOffset.first;
     286             :   }
     287             : 
     288             :   bool knownOffset(SizeOffsetEvalType SizeOffset) {
     289             :     return SizeOffset.second;
     290             :   }
     291             : 
     292             :   bool anyKnown(SizeOffsetEvalType SizeOffset) {
     293          13 :     return knownSize(SizeOffset) || knownOffset(SizeOffset);
     294             :   }
     295             : 
     296             :   bool bothKnown(SizeOffsetEvalType SizeOffset) {
     297         115 :     return knownSize(SizeOffset) && knownOffset(SizeOffset);
     298             :   }
     299             : 
     300             :   // The individual instruction visitors should be treated as private.
     301             :   SizeOffsetEvalType visitAllocaInst(AllocaInst &I);
     302             :   SizeOffsetEvalType visitCallSite(CallSite CS);
     303             :   SizeOffsetEvalType visitExtractElementInst(ExtractElementInst &I);
     304             :   SizeOffsetEvalType visitExtractValueInst(ExtractValueInst &I);
     305             :   SizeOffsetEvalType visitGEPOperator(GEPOperator &GEP);
     306             :   SizeOffsetEvalType visitIntToPtrInst(IntToPtrInst&);
     307             :   SizeOffsetEvalType visitLoadInst(LoadInst &I);
     308             :   SizeOffsetEvalType visitPHINode(PHINode &PHI);
     309             :   SizeOffsetEvalType visitSelectInst(SelectInst &I);
     310             :   SizeOffsetEvalType visitInstruction(Instruction &I);
     311             : };
     312             : 
     313             : } // end namespace llvm
     314             : 
     315             : #endif // LLVM_ANALYSIS_MEMORYBUILTINS_H

Generated by: LCOV version 1.13