LCOV - code coverage report
Current view: top level - lib/Analysis - MemoryBuiltins.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 371 406 91.4 %
Date: 2017-09-14 15:23:50 Functions: 45 53 84.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- MemoryBuiltins.cpp - Identify calls to memory builtins -------------===//
       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             : #include "llvm/Analysis/MemoryBuiltins.h"
      16             : #include "llvm/ADT/APInt.h"
      17             : #include "llvm/ADT/None.h"
      18             : #include "llvm/ADT/Optional.h"
      19             : #include "llvm/ADT/STLExtras.h"
      20             : #include "llvm/ADT/Statistic.h"
      21             : #include "llvm/ADT/StringRef.h"
      22             : #include "llvm/Analysis/TargetFolder.h"
      23             : #include "llvm/Analysis/TargetLibraryInfo.h"
      24             : #include "llvm/Analysis/ValueTracking.h"
      25             : #include "llvm/IR/Argument.h"
      26             : #include "llvm/IR/Attributes.h"
      27             : #include "llvm/IR/Constants.h"
      28             : #include "llvm/IR/DataLayout.h"
      29             : #include "llvm/IR/DerivedTypes.h"
      30             : #include "llvm/IR/Function.h"
      31             : #include "llvm/IR/GlobalAlias.h"
      32             : #include "llvm/IR/GlobalVariable.h"
      33             : #include "llvm/IR/Instruction.h"
      34             : #include "llvm/IR/Instructions.h"
      35             : #include "llvm/IR/IntrinsicInst.h"
      36             : #include "llvm/IR/Operator.h"
      37             : #include "llvm/IR/Type.h"
      38             : #include "llvm/IR/Value.h"
      39             : #include "llvm/Support/Casting.h"
      40             : #include "llvm/Support/Debug.h"
      41             : #include "llvm/Support/MathExtras.h"
      42             : #include "llvm/Support/raw_ostream.h"
      43             : #include "llvm/Transforms/Utils/Local.h"
      44             : #include <cassert>
      45             : #include <cstdint>
      46             : #include <iterator>
      47             : #include <utility>
      48             : 
      49             : using namespace llvm;
      50             : 
      51             : #define DEBUG_TYPE "memory-builtins"
      52             : 
      53             : enum AllocType : uint8_t {
      54             :   OpNewLike          = 1<<0, // allocates; never returns null
      55             :   MallocLike         = 1<<1 | OpNewLike, // allocates; may return null
      56             :   CallocLike         = 1<<2, // allocates + bzero
      57             :   ReallocLike        = 1<<3, // reallocates
      58             :   StrDupLike         = 1<<4,
      59             :   MallocOrCallocLike = MallocLike | CallocLike,
      60             :   AllocLike          = MallocLike | CallocLike | StrDupLike,
      61             :   AnyAlloc           = AllocLike | ReallocLike
      62             : };
      63             : 
      64             : struct AllocFnsTy {
      65             :   AllocType AllocTy;
      66             :   unsigned NumParams;
      67             :   // First and Second size parameters (or -1 if unused)
      68             :   int FstParam, SndParam;
      69             : };
      70             : 
      71             : // FIXME: certain users need more information. E.g., SimplifyLibCalls needs to
      72             : // know which functions are nounwind, noalias, nocapture parameters, etc.
      73             : static const std::pair<LibFunc, AllocFnsTy> AllocationFnData[] = {
      74             :   {LibFunc_malloc,              {MallocLike,  1, 0,  -1}},
      75             :   {LibFunc_valloc,              {MallocLike,  1, 0,  -1}},
      76             :   {LibFunc_Znwj,                {OpNewLike,   1, 0,  -1}}, // new(unsigned int)
      77             :   {LibFunc_ZnwjRKSt9nothrow_t,  {MallocLike,  2, 0,  -1}}, // new(unsigned int, nothrow)
      78             :   {LibFunc_Znwm,                {OpNewLike,   1, 0,  -1}}, // new(unsigned long)
      79             :   {LibFunc_ZnwmRKSt9nothrow_t,  {MallocLike,  2, 0,  -1}}, // new(unsigned long, nothrow)
      80             :   {LibFunc_Znaj,                {OpNewLike,   1, 0,  -1}}, // new[](unsigned int)
      81             :   {LibFunc_ZnajRKSt9nothrow_t,  {MallocLike,  2, 0,  -1}}, // new[](unsigned int, nothrow)
      82             :   {LibFunc_Znam,                {OpNewLike,   1, 0,  -1}}, // new[](unsigned long)
      83             :   {LibFunc_ZnamRKSt9nothrow_t,  {MallocLike,  2, 0,  -1}}, // new[](unsigned long, nothrow)
      84             :   {LibFunc_msvc_new_int,         {OpNewLike,   1, 0,  -1}}, // new(unsigned int)
      85             :   {LibFunc_msvc_new_int_nothrow, {MallocLike,  2, 0,  -1}}, // new(unsigned int, nothrow)
      86             :   {LibFunc_msvc_new_longlong,         {OpNewLike,   1, 0,  -1}}, // new(unsigned long long)
      87             :   {LibFunc_msvc_new_longlong_nothrow, {MallocLike,  2, 0,  -1}}, // new(unsigned long long, nothrow)
      88             :   {LibFunc_msvc_new_array_int,         {OpNewLike,   1, 0,  -1}}, // new[](unsigned int)
      89             :   {LibFunc_msvc_new_array_int_nothrow, {MallocLike,  2, 0,  -1}}, // new[](unsigned int, nothrow)
      90             :   {LibFunc_msvc_new_array_longlong,         {OpNewLike,   1, 0,  -1}}, // new[](unsigned long long)
      91             :   {LibFunc_msvc_new_array_longlong_nothrow, {MallocLike,  2, 0,  -1}}, // new[](unsigned long long, nothrow)
      92             :   {LibFunc_calloc,              {CallocLike,  2, 0,   1}},
      93             :   {LibFunc_realloc,             {ReallocLike, 2, 1,  -1}},
      94             :   {LibFunc_reallocf,            {ReallocLike, 2, 1,  -1}},
      95             :   {LibFunc_strdup,              {StrDupLike,  1, -1, -1}},
      96             :   {LibFunc_strndup,             {StrDupLike,  2, 1,  -1}}
      97             :   // TODO: Handle "int posix_memalign(void **, size_t, size_t)"
      98             : };
      99             : 
     100    30769783 : static const Function *getCalledFunction(const Value *V, bool LookThroughBitCast,
     101             :                                          bool &IsNoBuiltin) {
     102             :   // Don't care about intrinsics in this case.
     103    30769783 :   if (isa<IntrinsicInst>(V))
     104             :     return nullptr;
     105             : 
     106    27021639 :   if (LookThroughBitCast)
     107           0 :     V = V->stripPointerCasts();
     108             : 
     109    54043278 :   ImmutableCallSite CS(V);
     110    27021639 :   if (!CS.getInstruction())
     111             :     return nullptr;
     112             : 
     113     8186631 :   IsNoBuiltin = CS.isNoBuiltin();
     114             : 
     115     8115666 :   const Function *Callee = CS.getCalledFunction();
     116     8115666 :   if (!Callee || !Callee->isDeclaration())
     117             :     return nullptr;
     118             :   return Callee;
     119             : }
     120             : 
     121             : /// Returns the allocation data for the given value if it's either a call to a
     122             : /// known allocation function, or a call to a function with the allocsize
     123             : /// attribute.
     124             : static Optional<AllocFnsTy>
     125     2480000 : getAllocationDataForFunction(const Function *Callee, AllocType AllocTy,
     126             :                              const TargetLibraryInfo *TLI) {
     127             :   // Make sure that the function is available.
     128     2480000 :   StringRef FnName = Callee->getName();
     129             :   LibFunc TLIFn;
     130     5079741 :   if (!TLI || !TLI->getLibFunc(FnName, TLIFn) || !TLI->has(TLIFn))
     131             :     return None;
     132             : 
     133      185514 :   const auto *Iter = find_if(
     134             :       AllocationFnData, [TLIFn](const std::pair<LibFunc, AllocFnsTy> &P) {
     135             :         return P.first == TLIFn;
     136      185514 :       });
     137             : 
     138      185514 :   if (Iter == std::end(AllocationFnData))
     139             :     return None;
     140             : 
     141       27834 :   const AllocFnsTy *FnData = &Iter->second;
     142       27834 :   if ((FnData->AllocTy & AllocTy) != FnData->AllocTy)
     143             :     return None;
     144             : 
     145             :   // Check function prototype.
     146       27540 :   int FstParam = FnData->FstParam;
     147       27540 :   int SndParam = FnData->SndParam;
     148       27540 :   FunctionType *FTy = Callee->getFunctionType();
     149             : 
     150       55078 :   if (FTy->getReturnType() == Type::getInt8PtrTy(FTy->getContext()) &&
     151       55064 :       FTy->getNumParams() == FnData->NumParams &&
     152       27384 :       (FstParam < 0 ||
     153       70302 :        (FTy->getParamType(FstParam)->isIntegerTy(32) ||
     154       86134 :         FTy->getParamType(FstParam)->isIntegerTy(64))) &&
     155          43 :       (SndParam < 0 ||
     156         118 :        FTy->getParamType(SndParam)->isIntegerTy(32) ||
     157          64 :        FTy->getParamType(SndParam)->isIntegerTy(64)))
     158             :     return *FnData;
     159             :   return None;
     160             : }
     161             : 
     162    30765308 : static Optional<AllocFnsTy> getAllocationData(const Value *V, AllocType AllocTy,
     163             :                                               const TargetLibraryInfo *TLI,
     164             :                                               bool LookThroughBitCast = false) {
     165             :   bool IsNoBuiltinCall;
     166    30765308 :   if (const Function *Callee =
     167    30765308 :           getCalledFunction(V, LookThroughBitCast, IsNoBuiltinCall))
     168     2520919 :     if (!IsNoBuiltinCall)
     169     2478034 :       return getAllocationDataForFunction(Callee, AllocTy, TLI);
     170             :   return None;
     171             : }
     172             : 
     173        4475 : static Optional<AllocFnsTy> getAllocationSize(const Value *V,
     174             :                                               const TargetLibraryInfo *TLI) {
     175             :   bool IsNoBuiltinCall;
     176             :   const Function *Callee =
     177        4475 :       getCalledFunction(V, /*LookThroughBitCast=*/false, IsNoBuiltinCall);
     178        4475 :   if (!Callee)
     179             :     return None;
     180             : 
     181             :   // Prefer to use existing information over allocsize. This will give us an
     182             :   // accurate AllocTy.
     183        3645 :   if (!IsNoBuiltinCall)
     184        1966 :     if (Optional<AllocFnsTy> Data =
     185        2063 :             getAllocationDataForFunction(Callee, AnyAlloc, TLI))
     186        3738 :       return Data;
     187             : 
     188        1776 :   Attribute Attr = Callee->getFnAttribute(Attribute::AllocSize);
     189        1776 :   if (Attr == Attribute())
     190             :     return None;
     191             : 
     192          18 :   std::pair<unsigned, Optional<unsigned>> Args = Attr.getAllocSizeArgs();
     193             : 
     194             :   AllocFnsTy Result;
     195             :   // Because allocsize only tells us how many bytes are allocated, we're not
     196             :   // really allowed to assume anything, so we use MallocLike.
     197          18 :   Result.AllocTy = MallocLike;
     198          18 :   Result.NumParams = Callee->getNumOperands();
     199          18 :   Result.FstParam = Args.first;
     200          36 :   Result.SndParam = Args.second.getValueOr(-1);
     201          18 :   return Result;
     202             : }
     203             : 
     204     7744652 : static bool hasNoAliasAttr(const Value *V, bool LookThroughBitCast) {
     205    15489304 :   ImmutableCallSite CS(LookThroughBitCast ? V->stripPointerCasts() : V);
     206     7744652 :   return CS && CS.hasRetAttr(Attribute::NoAlias);
     207             : }
     208             : 
     209             : /// \brief Tests if a value is a call or invoke to a library function that
     210             : /// allocates or reallocates memory (either malloc, calloc, realloc, or strdup
     211             : /// like).
     212     7753655 : bool llvm::isAllocationFn(const Value *V, const TargetLibraryInfo *TLI,
     213             :                           bool LookThroughBitCast) {
     214    15507310 :   return getAllocationData(V, AnyAlloc, TLI, LookThroughBitCast).hasValue();
     215             : }
     216             : 
     217             : /// \brief Tests if a value is a call or invoke to a function that returns a
     218             : /// NoAlias pointer (including malloc/calloc/realloc/strdup-like functions).
     219     7746317 : bool llvm::isNoAliasFn(const Value *V, const TargetLibraryInfo *TLI,
     220             :                        bool LookThroughBitCast) {
     221             :   // it's safe to consider realloc as noalias since accessing the original
     222             :   // pointer is undefined behavior
     223    15490969 :   return isAllocationFn(V, TLI, LookThroughBitCast) ||
     224    15490969 :          hasNoAliasAttr(V, LookThroughBitCast);
     225             : }
     226             : 
     227             : /// \brief Tests if a value is a call or invoke to a library function that
     228             : /// allocates uninitialized memory (such as malloc).
     229       23322 : bool llvm::isMallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
     230             :                           bool LookThroughBitCast) {
     231       46644 :   return getAllocationData(V, MallocLike, TLI, LookThroughBitCast).hasValue();
     232             : }
     233             : 
     234             : /// \brief Tests if a value is a call or invoke to a library function that
     235             : /// allocates zero-filled memory (such as calloc).
     236       30387 : bool llvm::isCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
     237             :                           bool LookThroughBitCast) {
     238       60774 :   return getAllocationData(V, CallocLike, TLI, LookThroughBitCast).hasValue();
     239             : }
     240             : 
     241             : /// \brief Tests if a value is a call or invoke to a library function that
     242             : /// allocates memory similiar to malloc or calloc.
     243     4061247 : bool llvm::isMallocOrCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
     244             :                                   bool LookThroughBitCast) {
     245     8122494 :   return getAllocationData(V, MallocOrCallocLike, TLI,
     246     8122494 :                            LookThroughBitCast).hasValue();
     247             : }
     248             : 
     249             : /// \brief Tests if a value is a call or invoke to a library function that
     250             : /// allocates memory (either malloc, calloc, or strdup like).
     251    18896697 : bool llvm::isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
     252             :                          bool LookThroughBitCast) {
     253    37793394 :   return getAllocationData(V, AllocLike, TLI, LookThroughBitCast).hasValue();
     254             : }
     255             : 
     256             : /// extractMallocCall - Returns the corresponding CallInst if the instruction
     257             : /// is a malloc call.  Since CallInst::CreateMalloc() only creates calls, we
     258             : /// ignore InvokeInst here.
     259          74 : const CallInst *llvm::extractMallocCall(const Value *I,
     260             :                                         const TargetLibraryInfo *TLI) {
     261         148 :   return isMallocLikeFn(I, TLI) ? dyn_cast<CallInst>(I) : nullptr;
     262             : }
     263             : 
     264          26 : static Value *computeArraySize(const CallInst *CI, const DataLayout &DL,
     265             :                                const TargetLibraryInfo *TLI,
     266             :                                bool LookThroughSExt = false) {
     267          26 :   if (!CI)
     268             :     return nullptr;
     269             : 
     270             :   // The size of the malloc's result type must be known to determine array size.
     271          26 :   Type *T = getMallocAllocatedType(CI, TLI);
     272          26 :   if (!T || !T->isSized())
     273             :     return nullptr;
     274             : 
     275          26 :   unsigned ElementSize = DL.getTypeAllocSize(T);
     276          14 :   if (StructType *ST = dyn_cast<StructType>(T))
     277          14 :     ElementSize = DL.getStructLayout(ST)->getSizeInBytes();
     278             : 
     279             :   // If malloc call's arg can be determined to be a multiple of ElementSize,
     280             :   // return the multiple.  Otherwise, return NULL.
     281          26 :   Value *MallocArg = CI->getArgOperand(0);
     282          26 :   Value *Multiple = nullptr;
     283          26 :   if (ComputeMultiple(MallocArg, ElementSize, Multiple, LookThroughSExt))
     284          18 :     return Multiple;
     285             : 
     286             :   return nullptr;
     287             : }
     288             : 
     289             : /// getMallocType - Returns the PointerType resulting from the malloc call.
     290             : /// The PointerType depends on the number of bitcast uses of the malloc call:
     291             : ///   0: PointerType is the calls' return type.
     292             : ///   1: PointerType is the bitcast's result type.
     293             : ///  >1: Unique PointerType cannot be determined, return NULL.
     294          81 : PointerType *llvm::getMallocType(const CallInst *CI,
     295             :                                  const TargetLibraryInfo *TLI) {
     296             :   assert(isMallocLikeFn(CI, TLI) && "getMallocType and not malloc call");
     297             : 
     298          81 :   PointerType *MallocType = nullptr;
     299          81 :   unsigned NumOfBitCastUses = 0;
     300             : 
     301             :   // Determine if CallInst has a bitcast use.
     302         243 :   for (Value::const_user_iterator UI = CI->user_begin(), E = CI->user_end();
     303         162 :        UI != E;)
     304         324 :     if (const BitCastInst *BCI = dyn_cast<BitCastInst>(*UI++)) {
     305         243 :       MallocType = cast<PointerType>(BCI->getDestTy());
     306          81 :       NumOfBitCastUses++;
     307             :     }
     308             : 
     309             :   // Malloc call has 1 bitcast use, so type is the bitcast's destination type.
     310          81 :   if (NumOfBitCastUses == 1)
     311             :     return MallocType;
     312             : 
     313             :   // Malloc call was not bitcast, so type is the malloc function's return type.
     314           0 :   if (NumOfBitCastUses == 0)
     315           0 :     return cast<PointerType>(CI->getType());
     316             : 
     317             :   // Type could not be determined.
     318             :   return nullptr;
     319             : }
     320             : 
     321             : /// getMallocAllocatedType - Returns the Type allocated by malloc call.
     322             : /// The Type depends on the number of bitcast uses of the malloc call:
     323             : ///   0: PointerType is the malloc calls' return type.
     324             : ///   1: PointerType is the bitcast's result type.
     325             : ///  >1: Unique PointerType cannot be determined, return NULL.
     326          81 : Type *llvm::getMallocAllocatedType(const CallInst *CI,
     327             :                                    const TargetLibraryInfo *TLI) {
     328          81 :   PointerType *PT = getMallocType(CI, TLI);
     329          81 :   return PT ? PT->getElementType() : nullptr;
     330             : }
     331             : 
     332             : /// getMallocArraySize - Returns the array size of a malloc call.  If the
     333             : /// argument passed to malloc is a multiple of the size of the malloced type,
     334             : /// then return that multiple.  For non-array mallocs, the multiple is
     335             : /// constant 1.  Otherwise, return NULL for mallocs whose array size cannot be
     336             : /// determined.
     337          26 : Value *llvm::getMallocArraySize(CallInst *CI, const DataLayout &DL,
     338             :                                 const TargetLibraryInfo *TLI,
     339             :                                 bool LookThroughSExt) {
     340             :   assert(isMallocLikeFn(CI, TLI) && "getMallocArraySize and not malloc call");
     341          26 :   return computeArraySize(CI, DL, TLI, LookThroughSExt);
     342             : }
     343             : 
     344             : /// extractCallocCall - Returns the corresponding CallInst if the instruction
     345             : /// is a calloc call.
     346           0 : const CallInst *llvm::extractCallocCall(const Value *I,
     347             :                                         const TargetLibraryInfo *TLI) {
     348           0 :   return isCallocLikeFn(I, TLI) ? cast<CallInst>(I) : nullptr;
     349             : }
     350             : 
     351             : /// isFreeCall - Returns non-null if the value is a call to the builtin free()
     352    21236157 : const CallInst *llvm::isFreeCall(const Value *I, const TargetLibraryInfo *TLI) {
     353     9308810 :   const CallInst *CI = dyn_cast<CallInst>(I);
     354    18569019 :   if (!CI || isa<IntrinsicInst>(CI))
     355             :     return nullptr;
     356    12331025 :   Function *Callee = CI->getCalledFunction();
     357             :   if (Callee == nullptr)
     358             :     return nullptr;
     359             : 
     360     6141212 :   StringRef FnName = Callee->getName();
     361             :   LibFunc TLIFn;
     362    12350628 :   if (!TLI || !TLI->getLibFunc(FnName, TLIFn) || !TLI->has(TLIFn))
     363             :     return nullptr;
     364             : 
     365             :   unsigned ExpectedNumParams;
     366      220508 :   if (TLIFn == LibFunc_free ||
     367       40254 :       TLIFn == LibFunc_ZdlPv || // operator delete(void*)
     368       39787 :       TLIFn == LibFunc_ZdaPv || // operator delete[](void*)
     369       39737 :       TLIFn == LibFunc_msvc_delete_ptr32 || // operator delete(void*)
     370       39735 :       TLIFn == LibFunc_msvc_delete_ptr64 || // operator delete(void*)
     371       39735 :       TLIFn == LibFunc_msvc_delete_array_ptr32 || // operator delete[](void*)
     372             :       TLIFn == LibFunc_msvc_delete_array_ptr64)   // operator delete[](void*)
     373             :     ExpectedNumParams = 1;
     374       39735 :   else if (TLIFn == LibFunc_ZdlPvj ||              // delete(void*, uint)
     375       39735 :            TLIFn == LibFunc_ZdlPvm ||              // delete(void*, ulong)
     376       39584 :            TLIFn == LibFunc_ZdlPvRKSt9nothrow_t || // delete(void*, nothrow)
     377       39582 :            TLIFn == LibFunc_ZdaPvj ||              // delete[](void*, uint)
     378       39580 :            TLIFn == LibFunc_ZdaPvm ||              // delete[](void*, ulong)
     379       39580 :            TLIFn == LibFunc_ZdaPvRKSt9nothrow_t || // delete[](void*, nothrow)
     380       39580 :            TLIFn == LibFunc_msvc_delete_ptr32_int ||      // delete(void*, uint)
     381       39580 :            TLIFn == LibFunc_msvc_delete_ptr64_longlong || // delete(void*, ulonglong)
     382       39580 :            TLIFn == LibFunc_msvc_delete_ptr32_nothrow || // delete(void*, nothrow)
     383       39580 :            TLIFn == LibFunc_msvc_delete_ptr64_nothrow || // delete(void*, nothrow)
     384       39580 :            TLIFn == LibFunc_msvc_delete_array_ptr32_int ||      // delete[](void*, uint)
     385       39580 :            TLIFn == LibFunc_msvc_delete_array_ptr64_longlong || // delete[](void*, ulonglong)
     386       39580 :            TLIFn == LibFunc_msvc_delete_array_ptr32_nothrow || // delete[](void*, nothrow)
     387             :            TLIFn == LibFunc_msvc_delete_array_ptr64_nothrow)   // delete[](void*, nothrow)
     388             :     ExpectedNumParams = 2;
     389             :   else
     390             :     return nullptr;
     391             : 
     392             :   // Check free prototype.
     393             :   // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin
     394             :   // attribute will exist.
     395      180928 :   FunctionType *FTy = Callee->getFunctionType();
     396      361856 :   if (!FTy->getReturnType()->isVoidTy())
     397             :     return nullptr;
     398      180928 :   if (FTy->getNumParams() != ExpectedNumParams)
     399             :     return nullptr;
     400      361856 :   if (FTy->getParamType(0) != Type::getInt8PtrTy(Callee->getContext()))
     401             :     return nullptr;
     402             : 
     403      180928 :   return CI;
     404             : }
     405             : 
     406             : //===----------------------------------------------------------------------===//
     407             : //  Utility functions to compute size of objects.
     408             : //
     409     5509277 : static APInt getSizeWithOverflow(const SizeOffsetType &Data) {
     410    16527831 :   if (Data.second.isNegative() || Data.first.ult(Data.second))
     411           0 :     return APInt(Data.first.getBitWidth(), 0);
     412    22037108 :   return Data.first - Data.second;
     413             : }
     414             : 
     415             : /// \brief Compute the size of the object pointed by Ptr. Returns true and the
     416             : /// object size in Size if successful, and false otherwise.
     417             : /// If RoundToAlign is true, then Size is rounded up to the alignment of
     418             : /// allocas, byval arguments, and global variables.
     419     5697673 : bool llvm::getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout &DL,
     420             :                          const TargetLibraryInfo *TLI, ObjectSizeOpts Opts) {
     421    11395346 :   ObjectSizeOffsetVisitor Visitor(DL, TLI, Ptr->getContext(), Opts);
     422    11395346 :   SizeOffsetType Data = Visitor.compute(const_cast<Value*>(Ptr));
     423     5509269 :   if (!Visitor.bothKnown(Data))
     424             :     return false;
     425             : 
     426    16527807 :   Size = getSizeWithOverflow(Data).getZExtValue();
     427     5509269 :   return true;
     428             : }
     429             : 
     430         119 : ConstantInt *llvm::lowerObjectSizeCall(IntrinsicInst *ObjectSize,
     431             :                                        const DataLayout &DL,
     432             :                                        const TargetLibraryInfo *TLI,
     433             :                                        bool MustSucceed) {
     434             :   assert(ObjectSize->getIntrinsicID() == Intrinsic::objectsize &&
     435             :          "ObjectSize must be a call to llvm.objectsize!");
     436             : 
     437         476 :   bool MaxVal = cast<ConstantInt>(ObjectSize->getArgOperand(1))->isZero();
     438         119 :   ObjectSizeOpts EvalOptions;
     439             :   // Unless we have to fold this to something, try to be as accurate as
     440             :   // possible.
     441         119 :   if (MustSucceed)
     442          30 :     EvalOptions.EvalMode =
     443          30 :         MaxVal ? ObjectSizeOpts::Mode::Max : ObjectSizeOpts::Mode::Min;
     444             :   else
     445             :     EvalOptions.EvalMode = ObjectSizeOpts::Mode::Exact;
     446             : 
     447         119 :   EvalOptions.NullIsUnknownSize =
     448         476 :       cast<ConstantInt>(ObjectSize->getArgOperand(2))->isOne();
     449             : 
     450             :   // FIXME: Does it make sense to just return a failure value if the size won't
     451             :   // fit in the output and `!MustSucceed`?
     452             :   uint64_t Size;
     453         238 :   auto *ResultType = cast<IntegerType>(ObjectSize->getType());
     454         238 :   if (getObjectSize(ObjectSize->getArgOperand(0), Size, DL, TLI, EvalOptions) &&
     455         122 :       isUIntN(ResultType->getBitWidth(), Size))
     456          56 :     return ConstantInt::get(ResultType, Size);
     457             : 
     458          63 :   if (!MustSucceed)
     459             :     return nullptr;
     460             : 
     461          18 :   return ConstantInt::get(ResultType, MaxVal ? -1ULL : 0);
     462             : }
     463             : 
     464             : STATISTIC(ObjectVisitorArgument,
     465             :           "Number of arguments with unsolved size and offset");
     466             : STATISTIC(ObjectVisitorLoad,
     467             :           "Number of load instructions with unsolved size and offset");
     468             : 
     469     5507565 : APInt ObjectSizeOffsetVisitor::align(APInt Size, uint64_t Align) {
     470     5507565 :   if (Options.RoundToAlign && Align)
     471     1044126 :     return APInt(IntTyBits, alignTo(Size.getZExtValue(), Align));
     472             :   return Size;
     473             : }
     474             : 
     475     5698357 : ObjectSizeOffsetVisitor::ObjectSizeOffsetVisitor(const DataLayout &DL,
     476             :                                                  const TargetLibraryInfo *TLI,
     477             :                                                  LLVMContext &Context,
     478     5698357 :                                                  ObjectSizeOpts Options)
     479    17095071 :     : DL(DL), TLI(TLI), Options(Options) {
     480             :   // Pointer size must be rechecked for each object visited since it could have
     481             :   // a different address space.
     482     5698357 : }
     483             : 
     484     5697852 : SizeOffsetType ObjectSizeOffsetVisitor::compute(Value *V) {
     485     5697852 :   IntTyBits = DL.getPointerTypeSizeInBits(V->getType());
     486    17093556 :   Zero = APInt::getNullValue(IntTyBits);
     487             : 
     488     5697852 :   V = V->stripPointerCasts();
     489      531648 :   if (Instruction *I = dyn_cast<Instruction>(V)) {
     490             :     // If we have already seen this instruction, bail out. Cycles can happen in
     491             :     // unreachable code after constant propagation.
     492      531648 :     if (!SeenInsts.insert(I).second)
     493           5 :       return unknown();
     494             : 
     495          38 :     if (GEPOperator *GEP = dyn_cast<GEPOperator>(V))
     496          38 :       return visitGEPOperator(*GEP);
     497      531605 :     return visit(*I);
     498             :   }
     499       51848 :   if (Argument *A = dyn_cast<Argument>(V))
     500       51848 :     return visitArgument(*A);
     501          15 :   if (ConstantPointerNull *P = dyn_cast<ConstantPointerNull>(V))
     502          15 :     return visitConstantPointerNull(*P);
     503           2 :   if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
     504           2 :     return visitGlobalAlias(*GA);
     505     5114312 :   if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
     506     5114312 :     return visitGlobalVariable(*GV);
     507          10 :   if (UndefValue *UV = dyn_cast<UndefValue>(V))
     508          10 :     return visitUndefValue(*UV);
     509          17 :   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
     510          17 :     if (CE->getOpcode() == Instruction::IntToPtr)
     511           8 :       return unknown(); // clueless
     512           9 :     if (CE->getOpcode() == Instruction::GetElementPtr)
     513           9 :       return visitGEPOperator(cast<GEPOperator>(*CE));
     514             :   }
     515             : 
     516             :   DEBUG(dbgs() << "ObjectSizeOffsetVisitor::compute() unhandled value: " << *V
     517             :         << '\n');
     518           0 :   return unknown();
     519             : }
     520             : 
     521             : /// When we're compiling N-bit code, and the user uses parameters that are
     522             : /// greater than N bits (e.g. uint64_t on a 32-bit build), we can run into
     523             : /// trouble with APInt size issues. This function handles resizing + overflow
     524             : /// checks for us. Check and zext or trunc \p I depending on IntTyBits and
     525             : /// I's value.
     526        1839 : bool ObjectSizeOffsetVisitor::CheckedZextOrTrunc(APInt &I) {
     527             :   // More bits than we can handle. Checking the bit width isn't necessary, but
     528             :   // it's faster than checking active bits, and should give `false` in the
     529             :   // vast majority of cases.
     530        1844 :   if (I.getBitWidth() > IntTyBits && I.getActiveBits() > IntTyBits)
     531             :     return false;
     532        1836 :   if (I.getBitWidth() != IntTyBits)
     533         174 :     I = I.zextOrTrunc(IntTyBits);
     534             :   return true;
     535             : }
     536             : 
     537      524301 : SizeOffsetType ObjectSizeOffsetVisitor::visitAllocaInst(AllocaInst &I) {
     538      524301 :   if (!I.getAllocatedType()->isSized())
     539           0 :     return unknown();
     540             : 
     541     1048602 :   APInt Size(IntTyBits, DL.getTypeAllocSize(I.getAllocatedType()));
     542      524301 :   if (!I.isArrayAllocation())
     543     3145176 :     return std::make_pair(align(Size, I.getAlignment()), Zero);
     544             : 
     545         105 :   Value *ArraySize = I.getArraySize();
     546          70 :   if (const ConstantInt *C = dyn_cast<ConstantInt>(ArraySize)) {
     547         210 :     APInt NumElems = C->getValue();
     548          70 :     if (!CheckedZextOrTrunc(NumElems))
     549           1 :       return unknown();
     550             : 
     551             :     bool Overflow;
     552         207 :     Size = Size.umul_ov(NumElems, Overflow);
     553         276 :     return Overflow ? unknown() : std::make_pair(align(Size, I.getAlignment()),
     554         207 :                                                  Zero);
     555             :   }
     556          35 :   return unknown();
     557             : }
     558             : 
     559       51848 : SizeOffsetType ObjectSizeOffsetVisitor::visitArgument(Argument &A) {
     560             :   // No interprocedural analysis is done at the moment.
     561       51848 :   if (!A.hasByValOrInAllocaAttr()) {
     562       49706 :     ++ObjectVisitorArgument;
     563       49706 :     return unknown();
     564             :   }
     565        4284 :   PointerType *PT = cast<PointerType>(A.getType());
     566        4284 :   APInt Size(IntTyBits, DL.getTypeAllocSize(PT->getElementType()));
     567       10710 :   return std::make_pair(align(Size, A.getParamAlignment()), Zero);
     568             : }
     569             : 
     570        4472 : SizeOffsetType ObjectSizeOffsetVisitor::visitCallSite(CallSite CS) {
     571       13416 :   Optional<AllocFnsTy> FnData = getAllocationSize(CS.getInstruction(), TLI);
     572        4472 :   if (!FnData)
     573        2588 :     return unknown();
     574             : 
     575             :   // Handle strdup-like functions separately.
     576        1884 :   if (FnData->AllocTy == StrDupLike) {
     577          15 :     APInt Size(IntTyBits, GetStringLength(CS.getArgument(0)));
     578           5 :     if (!Size)
     579           0 :       return unknown();
     580             : 
     581             :     // Strndup limits strlen.
     582           5 :     if (FnData->FstParam > 0) {
     583             :       ConstantInt *Arg =
     584          12 :           dyn_cast<ConstantInt>(CS.getArgument(FnData->FstParam));
     585             :       if (!Arg)
     586           0 :         return unknown();
     587             : 
     588           8 :       APInt MaxSize = Arg->getValue().zextOrSelf(IntTyBits);
     589           4 :       if (Size.ugt(MaxSize))
     590          10 :         Size = MaxSize + 1;
     591             :     }
     592           5 :     return std::make_pair(Size, Zero);
     593             :   }
     594             : 
     595        5519 :   ConstantInt *Arg = dyn_cast<ConstantInt>(CS.getArgument(FnData->FstParam));
     596             :   if (!Arg)
     597         118 :     return unknown();
     598             : 
     599        3522 :   APInt Size = Arg->getValue();
     600        1761 :   if (!CheckedZextOrTrunc(Size))
     601           2 :     return unknown();
     602             : 
     603             :   // Size is determined by just 1 parameter.
     604        1759 :   if (FnData->SndParam < 0)
     605        1746 :     return std::make_pair(Size, Zero);
     606             : 
     607          34 :   Arg = dyn_cast<ConstantInt>(CS.getArgument(FnData->SndParam));
     608             :   if (!Arg)
     609           5 :     return unknown();
     610             : 
     611          16 :   APInt NumElems = Arg->getValue();
     612           8 :   if (!CheckedZextOrTrunc(NumElems))
     613           0 :     return unknown();
     614             : 
     615             :   bool Overflow;
     616          24 :   Size = Size.umul_ov(NumElems, Overflow);
     617          16 :   return Overflow ? unknown() : std::make_pair(Size, Zero);
     618             : 
     619             :   // TODO: handle more standard functions (+ wchar cousins):
     620             :   // - strdup / strndup
     621             :   // - strcpy / strncpy
     622             :   // - strcat / strncat
     623             :   // - memcpy / memmove
     624             :   // - strcat / strncat
     625             :   // - memset
     626             : }
     627             : 
     628             : SizeOffsetType
     629          15 : ObjectSizeOffsetVisitor::visitConstantPointerNull(ConstantPointerNull& CPN) {
     630          31 :   if (Options.NullIsUnknownSize && CPN.getType()->getAddressSpace() == 0)
     631           4 :     return unknown();
     632          11 :   return std::make_pair(Zero, Zero);
     633             : }
     634             : 
     635             : SizeOffsetType
     636           0 : ObjectSizeOffsetVisitor::visitExtractElementInst(ExtractElementInst&) {
     637           0 :   return unknown();
     638             : }
     639             : 
     640             : SizeOffsetType
     641           0 : ObjectSizeOffsetVisitor::visitExtractValueInst(ExtractValueInst&) {
     642             :   // Easy cases were already folded by previous passes.
     643           0 :   return unknown();
     644             : }
     645             : 
     646          47 : SizeOffsetType ObjectSizeOffsetVisitor::visitGEPOperator(GEPOperator &GEP) {
     647          94 :   SizeOffsetType PtrData = compute(GEP.getPointerOperand());
     648         141 :   APInt Offset(IntTyBits, 0);
     649          29 :   if (!bothKnown(PtrData) || !GEP.accumulateConstantOffset(DL, Offset))
     650          22 :     return unknown();
     651             : 
     652         125 :   return std::make_pair(PtrData.first, PtrData.second + Offset);
     653             : }
     654             : 
     655           2 : SizeOffsetType ObjectSizeOffsetVisitor::visitGlobalAlias(GlobalAlias &GA) {
     656           4 :   if (GA.isInterposable())
     657           2 :     return unknown();
     658           0 :   return compute(GA.getAliasee());
     659             : }
     660             : 
     661     5114312 : SizeOffsetType ObjectSizeOffsetVisitor::visitGlobalVariable(GlobalVariable &GV){
     662     5114312 :   if (!GV.hasDefinitiveInitializer())
     663      133154 :     return unknown();
     664             : 
     665    14943474 :   APInt Size(IntTyBits, DL.getTypeAllocSize(GV.getType()->getElementType()));
     666    29886948 :   return std::make_pair(align(Size, GV.getAlignment()), Zero);
     667             : }
     668             : 
     669         491 : SizeOffsetType ObjectSizeOffsetVisitor::visitIntToPtrInst(IntToPtrInst&) {
     670             :   // clueless
     671         982 :   return unknown();
     672             : }
     673             : 
     674         685 : SizeOffsetType ObjectSizeOffsetVisitor::visitLoadInst(LoadInst&) {
     675         685 :   ++ObjectVisitorLoad;
     676        1370 :   return unknown();
     677             : }
     678             : 
     679        1647 : SizeOffsetType ObjectSizeOffsetVisitor::visitPHINode(PHINode&) {
     680             :   // too complex to analyze statically.
     681        3294 :   return unknown();
     682             : }
     683             : 
     684           9 : SizeOffsetType ObjectSizeOffsetVisitor::visitSelectInst(SelectInst &I) {
     685          18 :   SizeOffsetType TrueSide  = compute(I.getTrueValue());
     686          18 :   SizeOffsetType FalseSide = compute(I.getFalseValue());
     687          10 :   if (bothKnown(TrueSide) && bothKnown(FalseSide)) {
     688           5 :     if (TrueSide == FalseSide) {
     689           5 :         return TrueSide;
     690             :     }
     691             : 
     692           4 :     APInt TrueResult = getSizeWithOverflow(TrueSide);
     693           4 :     APInt FalseResult = getSizeWithOverflow(FalseSide);
     694             : 
     695           4 :     if (TrueResult == FalseResult) {
     696           4 :       return TrueSide;
     697             :     }
     698           3 :     if (Options.EvalMode == ObjectSizeOpts::Mode::Min) {
     699           1 :       if (TrueResult.slt(FalseResult))
     700             :         return TrueSide;
     701             :       return FalseSide;
     702             :     }
     703           2 :     if (Options.EvalMode == ObjectSizeOpts::Mode::Max) {
     704           2 :       if (TrueResult.sgt(FalseResult))
     705             :         return TrueSide;
     706             :       return FalseSide;
     707             :     }
     708             :   }
     709           4 :   return unknown();
     710             : }
     711             : 
     712          10 : SizeOffsetType ObjectSizeOffsetVisitor::visitUndefValue(UndefValue&) {
     713          20 :   return std::make_pair(Zero, Zero);
     714             : }
     715             : 
     716           0 : SizeOffsetType ObjectSizeOffsetVisitor::visitInstruction(Instruction &I) {
     717             :   DEBUG(dbgs() << "ObjectSizeOffsetVisitor unknown instruction:" << I << '\n');
     718           0 :   return unknown();
     719             : }
     720             : 
     721          26 : ObjectSizeOffsetEvaluator::ObjectSizeOffsetEvaluator(
     722             :     const DataLayout &DL, const TargetLibraryInfo *TLI, LLVMContext &Context,
     723          26 :     bool RoundToAlign)
     724          26 :     : DL(DL), TLI(TLI), Context(Context), Builder(Context, TargetFolder(DL)),
     725         104 :       RoundToAlign(RoundToAlign) {
     726             :   // IntTy and Zero must be set for each compute() since the address space may
     727             :   // be different for later objects.
     728          26 : }
     729             : 
     730          45 : SizeOffsetEvalType ObjectSizeOffsetEvaluator::compute(Value *V) {
     731             :   // XXX - Are vectors of pointers possible here?
     732          90 :   IntTy = cast<IntegerType>(DL.getIntPtrType(V->getType()));
     733          45 :   Zero = ConstantInt::get(IntTy, 0);
     734             : 
     735          45 :   SizeOffsetEvalType Result = compute_(V);
     736             : 
     737          45 :   if (!bothKnown(Result)) {
     738             :     // Erase everything that was computed in this iteration from the cache, so
     739             :     // that no dangling references are left behind. We could be a bit smarter if
     740             :     // we kept a dependency graph. It's probably not worth the complexity.
     741          22 :     for (const Value *SeenVal : SeenVals) {
     742          13 :       CacheMapTy::iterator CacheIt = CacheMap.find(SeenVal);
     743             :       // non-computable results can be safely cached
     744          52 :       if (CacheIt != CacheMap.end() && anyKnown(CacheIt->second))
     745           2 :         CacheMap.erase(CacheIt);
     746             :     }
     747             :   }
     748             : 
     749          45 :   SeenVals.clear();
     750          45 :   return Result;
     751             : }
     752             : 
     753          71 : SizeOffsetEvalType ObjectSizeOffsetEvaluator::compute_(Value *V) {
     754          71 :   ObjectSizeOpts ObjSizeOptions;
     755          71 :   ObjSizeOptions.RoundToAlign = RoundToAlign;
     756             : 
     757         142 :   ObjectSizeOffsetVisitor Visitor(DL, TLI, Context, ObjSizeOptions);
     758         142 :   SizeOffsetType Const = Visitor.compute(V);
     759          25 :   if (Visitor.bothKnown(Const))
     760          50 :     return std::make_pair(ConstantInt::get(Context, Const.first),
     761          50 :                           ConstantInt::get(Context, Const.second));
     762             : 
     763          92 :   V = V->stripPointerCasts();
     764             : 
     765             :   // Check cache.
     766          46 :   CacheMapTy::iterator CacheIt = CacheMap.find(V);
     767         138 :   if (CacheIt != CacheMap.end())
     768          24 :     return CacheIt->second;
     769             : 
     770             :   // Always generate code immediately before the instruction being
     771             :   // processed, so that the generated code dominates the same BBs.
     772          68 :   BuilderTy::InsertPointGuard Guard(Builder);
     773          65 :   if (Instruction *I = dyn_cast<Instruction>(V))
     774          31 :     Builder.SetInsertPoint(I);
     775             : 
     776             :   // Now compute the size and offset.
     777          34 :   SizeOffsetEvalType Result;
     778             : 
     779             :   // Record the pointers that were handled in this run, so that they can be
     780             :   // cleaned later if something fails. We also use this set to break cycles that
     781             :   // can occur in dead code.
     782          34 :   if (!SeenVals.insert(V).second) {
     783             :     Result = unknown();
     784          47 :   } else if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
     785          28 :     Result = visitGEPOperator(*GEP);
     786          35 :   } else if (Instruction *I = dyn_cast<Instruction>(V)) {
     787          32 :     Result = visit(*I);
     788           3 :   } else if (isa<Argument>(V) ||
     789           0 :              (isa<ConstantExpr>(V) &&
     790           0 :               cast<ConstantExpr>(V)->getOpcode() == Instruction::IntToPtr) ||
     791           3 :              isa<GlobalAlias>(V) ||
     792             :              isa<GlobalVariable>(V)) {
     793             :     // Ignore values where we cannot do more than ObjectSizeVisitor.
     794             :     Result = unknown();
     795             :   } else {
     796             :     DEBUG(dbgs() << "ObjectSizeOffsetEvaluator::compute() unhandled value: "
     797             :           << *V << '\n');
     798             :     Result = unknown();
     799             :   }
     800             : 
     801             :   // Don't reuse CacheIt since it may be invalid at this point.
     802         102 :   CacheMap[V] = Result;
     803          34 :   return Result;
     804             : }
     805             : 
     806           6 : SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitAllocaInst(AllocaInst &I) {
     807           6 :   if (!I.getAllocatedType()->isSized())
     808           0 :     return unknown();
     809             : 
     810             :   // must be a VLA
     811             :   assert(I.isArrayAllocation());
     812           6 :   Value *ArraySize = I.getArraySize();
     813           6 :   Value *Size = ConstantInt::get(ArraySize->getType(),
     814          12 :                                  DL.getTypeAllocSize(I.getAllocatedType()));
     815          12 :   Size = Builder.CreateMul(Size, ArraySize);
     816           6 :   return std::make_pair(Size, Zero);
     817             : }
     818             : 
     819           3 : SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitCallSite(CallSite CS) {
     820           9 :   Optional<AllocFnsTy> FnData = getAllocationSize(CS.getInstruction(), TLI);
     821           3 :   if (!FnData)
     822           0 :     return unknown();
     823             : 
     824             :   // Handle strdup-like functions separately.
     825           3 :   if (FnData->AllocTy == StrDupLike) {
     826             :     // TODO
     827           0 :     return unknown();
     828             :   }
     829             : 
     830           6 :   Value *FirstArg = CS.getArgument(FnData->FstParam);
     831           9 :   FirstArg = Builder.CreateZExt(FirstArg, IntTy);
     832           3 :   if (FnData->SndParam < 0)
     833           1 :     return std::make_pair(FirstArg, Zero);
     834             : 
     835           4 :   Value *SecondArg = CS.getArgument(FnData->SndParam);
     836           6 :   SecondArg = Builder.CreateZExt(SecondArg, IntTy);
     837           4 :   Value *Size = Builder.CreateMul(FirstArg, SecondArg);
     838           2 :   return std::make_pair(Size, Zero);
     839             : 
     840             :   // TODO: handle more standard functions (+ wchar cousins):
     841             :   // - strdup / strndup
     842             :   // - strcpy / strncpy
     843             :   // - strcat / strncat
     844             :   // - memcpy / memmove
     845             :   // - strcat / strncat
     846             :   // - memset
     847             : }
     848             : 
     849             : SizeOffsetEvalType
     850           0 : ObjectSizeOffsetEvaluator::visitExtractElementInst(ExtractElementInst&) {
     851           0 :   return unknown();
     852             : }
     853             : 
     854             : SizeOffsetEvalType
     855           0 : ObjectSizeOffsetEvaluator::visitExtractValueInst(ExtractValueInst&) {
     856           0 :   return unknown();
     857             : }
     858             : 
     859             : SizeOffsetEvalType
     860          14 : ObjectSizeOffsetEvaluator::visitGEPOperator(GEPOperator &GEP) {
     861          14 :   SizeOffsetEvalType PtrData = compute_(GEP.getPointerOperand());
     862          14 :   if (!bothKnown(PtrData))
     863           4 :     return unknown();
     864             : 
     865          10 :   Value *Offset = EmitGEPOffset(&Builder, DL, &GEP, /*NoAssumptions=*/true);
     866          20 :   Offset = Builder.CreateAdd(PtrData.second, Offset);
     867          10 :   return std::make_pair(PtrData.first, Offset);
     868             : }
     869             : 
     870           0 : SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitIntToPtrInst(IntToPtrInst&) {
     871             :   // clueless
     872           0 :   return unknown();
     873             : }
     874             : 
     875           1 : SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitLoadInst(LoadInst&) {
     876           2 :   return unknown();
     877             : }
     878             : 
     879           4 : SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitPHINode(PHINode &PHI) {
     880             :   // Create 2 PHIs: one for size and another for offset.
     881          12 :   PHINode *SizePHI   = Builder.CreatePHI(IntTy, PHI.getNumIncomingValues());
     882          12 :   PHINode *OffsetPHI = Builder.CreatePHI(IntTy, PHI.getNumIncomingValues());
     883             : 
     884             :   // Insert right away in the cache to handle recursive PHIs.
     885          16 :   CacheMap[&PHI] = std::make_pair(SizePHI, OffsetPHI);
     886             : 
     887             :   // Compute offset/size for each PHI incoming pointer.
     888          14 :   for (unsigned i = 0, e = PHI.getNumIncomingValues(); i != e; ++i) {
     889          32 :     Builder.SetInsertPoint(&*PHI.getIncomingBlock(i)->getFirstInsertionPt());
     890           8 :     SizeOffsetEvalType EdgeData = compute_(PHI.getIncomingValue(i));
     891             : 
     892           8 :     if (!bothKnown(EdgeData)) {
     893           2 :       OffsetPHI->replaceAllUsesWith(UndefValue::get(IntTy));
     894           2 :       OffsetPHI->eraseFromParent();
     895           2 :       SizePHI->replaceAllUsesWith(UndefValue::get(IntTy));
     896           2 :       SizePHI->eraseFromParent();
     897           4 :       return unknown();
     898             :     }
     899          12 :     SizePHI->addIncoming(EdgeData.first, PHI.getIncomingBlock(i));
     900          12 :     OffsetPHI->addIncoming(EdgeData.second, PHI.getIncomingBlock(i));
     901             :   }
     902             : 
     903           2 :   Value *Size = SizePHI, *Offset = OffsetPHI, *Tmp;
     904           2 :   if ((Tmp = SizePHI->hasConstantValue())) {
     905           2 :     Size = Tmp;
     906           2 :     SizePHI->replaceAllUsesWith(Size);
     907           2 :     SizePHI->eraseFromParent();
     908             :   }
     909           2 :   if ((Tmp = OffsetPHI->hasConstantValue())) {
     910           0 :     Offset = Tmp;
     911           0 :     OffsetPHI->replaceAllUsesWith(Offset);
     912           0 :     OffsetPHI->eraseFromParent();
     913             :   }
     914           2 :   return std::make_pair(Size, Offset);
     915             : }
     916             : 
     917           2 : SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitSelectInst(SelectInst &I) {
     918           2 :   SizeOffsetEvalType TrueSide  = compute_(I.getTrueValue());
     919           2 :   SizeOffsetEvalType FalseSide = compute_(I.getFalseValue());
     920             : 
     921           3 :   if (!bothKnown(TrueSide) || !bothKnown(FalseSide))
     922           1 :     return unknown();
     923           1 :   if (TrueSide == FalseSide)
     924           0 :     return TrueSide;
     925             : 
     926           4 :   Value *Size = Builder.CreateSelect(I.getCondition(), TrueSide.first,
     927           1 :                                      FalseSide.first);
     928           4 :   Value *Offset = Builder.CreateSelect(I.getCondition(), TrueSide.second,
     929           1 :                                        FalseSide.second);
     930           1 :   return std::make_pair(Size, Offset);
     931             : }
     932             : 
     933           0 : SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitInstruction(Instruction &I) {
     934             :   DEBUG(dbgs() << "ObjectSizeOffsetEvaluator unknown instruction:" << I <<'\n');
     935           0 :   return unknown();
     936             : }

Generated by: LCOV version 1.13