MemoryBuiltins.cpp revision 224145
1198892Srdivacky//===------ MemoryBuiltins.cpp - Identify calls to memory builtins --------===// 2198892Srdivacky// 3198892Srdivacky// The LLVM Compiler Infrastructure 4198892Srdivacky// 5198892Srdivacky// This file is distributed under the University of Illinois Open Source 6198892Srdivacky// License. See LICENSE.TXT for details. 7198892Srdivacky// 8198892Srdivacky//===----------------------------------------------------------------------===// 9198892Srdivacky// 10198892Srdivacky// This family of functions identifies calls to builtin functions that allocate 11198892Srdivacky// or free memory. 12198892Srdivacky// 13198892Srdivacky//===----------------------------------------------------------------------===// 14198892Srdivacky 15198892Srdivacky#include "llvm/Analysis/MemoryBuiltins.h" 16198892Srdivacky#include "llvm/Constants.h" 17198892Srdivacky#include "llvm/Instructions.h" 18198892Srdivacky#include "llvm/Module.h" 19199481Srdivacky#include "llvm/Analysis/ValueTracking.h" 20198953Srdivacky#include "llvm/Target/TargetData.h" 21198892Srdivackyusing namespace llvm; 22198892Srdivacky 23198892Srdivacky//===----------------------------------------------------------------------===// 24198892Srdivacky// malloc Call Utility Functions. 25198892Srdivacky// 26198892Srdivacky 27203954Srdivacky/// isMalloc - Returns true if the value is either a malloc call or a 28198892Srdivacky/// bitcast of the result of a malloc call. 29198892Srdivackybool llvm::isMalloc(const Value *I) { 30198892Srdivacky return extractMallocCall(I) || extractMallocCallFromBitCast(I); 31198892Srdivacky} 32198892Srdivacky 33198892Srdivackystatic bool isMallocCall(const CallInst *CI) { 34198892Srdivacky if (!CI) 35198892Srdivacky return false; 36198892Srdivacky 37198892Srdivacky Function *Callee = CI->getCalledFunction(); 38221345Sdim if (Callee == 0 || !Callee->isDeclaration()) 39198892Srdivacky return false; 40221345Sdim if (Callee->getName() != "malloc" && 41221345Sdim Callee->getName() != "_Znwj" && // operator new(unsigned int) 42221345Sdim Callee->getName() != "_Znwm" && // operator new(unsigned long) 43221345Sdim Callee->getName() != "_Znaj" && // operator new[](unsigned int) 44221345Sdim Callee->getName() != "_Znam") // operator new[](unsigned long) 45221345Sdim return false; 46198892Srdivacky 47198892Srdivacky // Check malloc prototype. 48198892Srdivacky // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin 49198892Srdivacky // attribute will exist. 50198892Srdivacky const FunctionType *FTy = Callee->getFunctionType(); 51198892Srdivacky if (FTy->getNumParams() != 1) 52198892Srdivacky return false; 53224145Sdim return FTy->getParamType(0)->isIntegerTy(32) || 54224145Sdim FTy->getParamType(0)->isIntegerTy(64); 55198892Srdivacky} 56198892Srdivacky 57198892Srdivacky/// extractMallocCall - Returns the corresponding CallInst if the instruction 58198892Srdivacky/// is a malloc call. Since CallInst::CreateMalloc() only creates calls, we 59198892Srdivacky/// ignore InvokeInst here. 60198892Srdivackyconst CallInst *llvm::extractMallocCall(const Value *I) { 61198892Srdivacky const CallInst *CI = dyn_cast<CallInst>(I); 62198892Srdivacky return (isMallocCall(CI)) ? CI : NULL; 63198892Srdivacky} 64198892Srdivacky 65198892SrdivackyCallInst *llvm::extractMallocCall(Value *I) { 66198892Srdivacky CallInst *CI = dyn_cast<CallInst>(I); 67198892Srdivacky return (isMallocCall(CI)) ? CI : NULL; 68198892Srdivacky} 69198892Srdivacky 70198892Srdivackystatic bool isBitCastOfMallocCall(const BitCastInst *BCI) { 71198892Srdivacky if (!BCI) 72198892Srdivacky return false; 73198892Srdivacky 74198892Srdivacky return isMallocCall(dyn_cast<CallInst>(BCI->getOperand(0))); 75198892Srdivacky} 76198892Srdivacky 77198892Srdivacky/// extractMallocCallFromBitCast - Returns the corresponding CallInst if the 78198892Srdivacky/// instruction is a bitcast of the result of a malloc call. 79198892SrdivackyCallInst *llvm::extractMallocCallFromBitCast(Value *I) { 80198892Srdivacky BitCastInst *BCI = dyn_cast<BitCastInst>(I); 81198892Srdivacky return (isBitCastOfMallocCall(BCI)) ? cast<CallInst>(BCI->getOperand(0)) 82198892Srdivacky : NULL; 83198892Srdivacky} 84198892Srdivacky 85198892Srdivackyconst CallInst *llvm::extractMallocCallFromBitCast(const Value *I) { 86198892Srdivacky const BitCastInst *BCI = dyn_cast<BitCastInst>(I); 87198892Srdivacky return (isBitCastOfMallocCall(BCI)) ? cast<CallInst>(BCI->getOperand(0)) 88198892Srdivacky : NULL; 89198892Srdivacky} 90198892Srdivacky 91199481Srdivackystatic Value *computeArraySize(const CallInst *CI, const TargetData *TD, 92199481Srdivacky bool LookThroughSExt = false) { 93198892Srdivacky if (!CI) 94198892Srdivacky return NULL; 95198892Srdivacky 96198953Srdivacky // The size of the malloc's result type must be known to determine array size. 97198892Srdivacky const Type *T = getMallocAllocatedType(CI); 98198953Srdivacky if (!T || !T->isSized() || !TD) 99198892Srdivacky return NULL; 100198892Srdivacky 101199481Srdivacky unsigned ElementSize = TD->getTypeAllocSize(T); 102198953Srdivacky if (const StructType *ST = dyn_cast<StructType>(T)) 103199481Srdivacky ElementSize = TD->getStructLayout(ST)->getSizeInBytes(); 104198892Srdivacky 105210299Sed // If malloc call's arg can be determined to be a multiple of ElementSize, 106199481Srdivacky // return the multiple. Otherwise, return NULL. 107210299Sed Value *MallocArg = CI->getArgOperand(0); 108199481Srdivacky Value *Multiple = NULL; 109199481Srdivacky if (ComputeMultiple(MallocArg, ElementSize, Multiple, 110199481Srdivacky LookThroughSExt)) 111199481Srdivacky return Multiple; 112198892Srdivacky 113198892Srdivacky return NULL; 114198892Srdivacky} 115198892Srdivacky 116198892Srdivacky/// isArrayMalloc - Returns the corresponding CallInst if the instruction 117198892Srdivacky/// is a call to malloc whose array size can be determined and the array size 118198892Srdivacky/// is not constant 1. Otherwise, return NULL. 119199481Srdivackyconst CallInst *llvm::isArrayMalloc(const Value *I, const TargetData *TD) { 120198892Srdivacky const CallInst *CI = extractMallocCall(I); 121199481Srdivacky Value *ArraySize = computeArraySize(CI, TD); 122198892Srdivacky 123198892Srdivacky if (ArraySize && 124210299Sed ArraySize != ConstantInt::get(CI->getArgOperand(0)->getType(), 1)) 125198892Srdivacky return CI; 126198892Srdivacky 127198892Srdivacky // CI is a non-array malloc or we can't figure out that it is an array malloc. 128198892Srdivacky return NULL; 129198892Srdivacky} 130198892Srdivacky 131198892Srdivacky/// getMallocType - Returns the PointerType resulting from the malloc call. 132198953Srdivacky/// The PointerType depends on the number of bitcast uses of the malloc call: 133198953Srdivacky/// 0: PointerType is the calls' return type. 134198953Srdivacky/// 1: PointerType is the bitcast's result type. 135198953Srdivacky/// >1: Unique PointerType cannot be determined, return NULL. 136198892Srdivackyconst PointerType *llvm::getMallocType(const CallInst *CI) { 137199481Srdivacky assert(isMalloc(CI) && "getMallocType and not malloc call"); 138198892Srdivacky 139198953Srdivacky const PointerType *MallocType = NULL; 140198953Srdivacky unsigned NumOfBitCastUses = 0; 141198953Srdivacky 142198892Srdivacky // Determine if CallInst has a bitcast use. 143206083Srdivacky for (Value::const_use_iterator UI = CI->use_begin(), E = CI->use_end(); 144198892Srdivacky UI != E; ) 145198953Srdivacky if (const BitCastInst *BCI = dyn_cast<BitCastInst>(*UI++)) { 146198953Srdivacky MallocType = cast<PointerType>(BCI->getDestTy()); 147198953Srdivacky NumOfBitCastUses++; 148198953Srdivacky } 149198892Srdivacky 150198953Srdivacky // Malloc call has 1 bitcast use, so type is the bitcast's destination type. 151198953Srdivacky if (NumOfBitCastUses == 1) 152198953Srdivacky return MallocType; 153198892Srdivacky 154198892Srdivacky // Malloc call was not bitcast, so type is the malloc function's return type. 155198953Srdivacky if (NumOfBitCastUses == 0) 156198892Srdivacky return cast<PointerType>(CI->getType()); 157198892Srdivacky 158198892Srdivacky // Type could not be determined. 159198892Srdivacky return NULL; 160198892Srdivacky} 161198892Srdivacky 162198953Srdivacky/// getMallocAllocatedType - Returns the Type allocated by malloc call. 163198953Srdivacky/// The Type depends on the number of bitcast uses of the malloc call: 164198953Srdivacky/// 0: PointerType is the malloc calls' return type. 165198953Srdivacky/// 1: PointerType is the bitcast's result type. 166198953Srdivacky/// >1: Unique PointerType cannot be determined, return NULL. 167198892Srdivackyconst Type *llvm::getMallocAllocatedType(const CallInst *CI) { 168198892Srdivacky const PointerType *PT = getMallocType(CI); 169198892Srdivacky return PT ? PT->getElementType() : NULL; 170198892Srdivacky} 171198892Srdivacky 172198892Srdivacky/// getMallocArraySize - Returns the array size of a malloc call. If the 173198892Srdivacky/// argument passed to malloc is a multiple of the size of the malloced type, 174198892Srdivacky/// then return that multiple. For non-array mallocs, the multiple is 175198892Srdivacky/// constant 1. Otherwise, return NULL for mallocs whose array size cannot be 176198892Srdivacky/// determined. 177199481SrdivackyValue *llvm::getMallocArraySize(CallInst *CI, const TargetData *TD, 178199481Srdivacky bool LookThroughSExt) { 179199481Srdivacky assert(isMalloc(CI) && "getMallocArraySize and not malloc call"); 180199481Srdivacky return computeArraySize(CI, TD, LookThroughSExt); 181198892Srdivacky} 182198892Srdivacky 183198892Srdivacky//===----------------------------------------------------------------------===// 184198892Srdivacky// free Call Utility Functions. 185198892Srdivacky// 186198892Srdivacky 187210299Sed/// isFreeCall - Returns non-null if the value is a call to the builtin free() 188210299Sedconst CallInst *llvm::isFreeCall(const Value *I) { 189198892Srdivacky const CallInst *CI = dyn_cast<CallInst>(I); 190198892Srdivacky if (!CI) 191210299Sed return 0; 192198892Srdivacky Function *Callee = CI->getCalledFunction(); 193221345Sdim if (Callee == 0 || !Callee->isDeclaration()) 194210299Sed return 0; 195198892Srdivacky 196221345Sdim if (Callee->getName() != "free" && 197221345Sdim Callee->getName() != "_ZdlPv" && // operator delete(void*) 198221345Sdim Callee->getName() != "_ZdaPv") // operator delete[](void*) 199221345Sdim return 0; 200221345Sdim 201198892Srdivacky // Check free prototype. 202198892Srdivacky // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin 203198892Srdivacky // attribute will exist. 204198892Srdivacky const FunctionType *FTy = Callee->getFunctionType(); 205198892Srdivacky if (!FTy->getReturnType()->isVoidTy()) 206210299Sed return 0; 207198892Srdivacky if (FTy->getNumParams() != 1) 208210299Sed return 0; 209224145Sdim if (FTy->getParamType(0) != Type::getInt8PtrTy(Callee->getContext())) 210210299Sed return 0; 211198892Srdivacky 212210299Sed return CI; 213198892Srdivacky} 214