MemoryBuiltins.cpp revision 206083
1193323Sed//===------ MemoryBuiltins.cpp - Identify calls to memory builtins --------===// 2193323Sed// 3193323Sed// The LLVM Compiler Infrastructure 4193323Sed// 5193323Sed// This file is distributed under the University of Illinois Open Source 6193323Sed// License. See LICENSE.TXT for details. 7193323Sed// 8193323Sed//===----------------------------------------------------------------------===// 9193323Sed// 10193323Sed// This family of functions identifies calls to builtin functions that allocate 11193323Sed// or free memory. 12193323Sed// 13193323Sed//===----------------------------------------------------------------------===// 14193323Sed 15193323Sed#include "llvm/Analysis/MemoryBuiltins.h" 16193323Sed#include "llvm/Constants.h" 17193323Sed#include "llvm/Instructions.h" 18195340Sed#include "llvm/Module.h" 19193323Sed#include "llvm/Analysis/ValueTracking.h" 20193323Sed#include "llvm/Target/TargetData.h" 21193323Sedusing namespace llvm; 22193323Sed 23193323Sed//===----------------------------------------------------------------------===// 24193323Sed// malloc Call Utility Functions. 25193323Sed// 26195340Sed 27193323Sed/// isMalloc - Returns true if the value is either a malloc call or a 28218885Sdim/// bitcast of the result of a malloc call. 29218885Sdimbool llvm::isMalloc(const Value *I) { 30193323Sed return extractMallocCall(I) || extractMallocCallFromBitCast(I); 31193323Sed} 32193323Sed 33193323Sedstatic bool isMallocCall(const CallInst *CI) { 34193323Sed if (!CI) 35193323Sed return false; 36193323Sed 37193323Sed Function *Callee = CI->getCalledFunction(); 38193323Sed if (Callee == 0 || !Callee->isDeclaration() || Callee->getName() != "malloc") 39193323Sed return false; 40193323Sed 41198090Srdivacky // Check malloc prototype. 42193323Sed // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin 43193323Sed // attribute will exist. 44193323Sed const FunctionType *FTy = Callee->getFunctionType(); 45193323Sed if (FTy->getNumParams() != 1) 46193323Sed return false; 47193323Sed if (IntegerType *ITy = dyn_cast<IntegerType>(FTy->param_begin()->get())) { 48193323Sed if (ITy->getBitWidth() != 32 && ITy->getBitWidth() != 64) 49193323Sed return false; 50193323Sed return true; 51193323Sed } 52193323Sed 53198396Srdivacky return false; 54198090Srdivacky} 55198090Srdivacky 56198090Srdivacky/// extractMallocCall - Returns the corresponding CallInst if the instruction 57198090Srdivacky/// is a malloc call. Since CallInst::CreateMalloc() only creates calls, we 58193323Sed/// ignore InvokeInst here. 59198090Srdivackyconst CallInst *llvm::extractMallocCall(const Value *I) { 60198090Srdivacky const CallInst *CI = dyn_cast<CallInst>(I); 61198090Srdivacky return (isMallocCall(CI)) ? CI : NULL; 62198090Srdivacky} 63198090Srdivacky 64193323SedCallInst *llvm::extractMallocCall(Value *I) { 65198090Srdivacky CallInst *CI = dyn_cast<CallInst>(I); 66193323Sed return (isMallocCall(CI)) ? CI : NULL; 67198090Srdivacky} 68193323Sed 69198090Srdivackystatic bool isBitCastOfMallocCall(const BitCastInst *BCI) { 70193323Sed if (!BCI) 71198090Srdivacky return false; 72212793Sdim 73212793Sdim return isMallocCall(dyn_cast<CallInst>(BCI->getOperand(0))); 74212793Sdim} 75198090Srdivacky 76198090Srdivacky/// extractMallocCallFromBitCast - Returns the corresponding CallInst if the 77198396Srdivacky/// instruction is a bitcast of the result of a malloc call. 78198396SrdivackyCallInst *llvm::extractMallocCallFromBitCast(Value *I) { 79198396Srdivacky BitCastInst *BCI = dyn_cast<BitCastInst>(I); 80212793Sdim return (isBitCastOfMallocCall(BCI)) ? cast<CallInst>(BCI->getOperand(0)) 81212793Sdim : NULL; 82212793Sdim} 83212793Sdim 84212793Sdimconst CallInst *llvm::extractMallocCallFromBitCast(const Value *I) { 85198396Srdivacky const BitCastInst *BCI = dyn_cast<BitCastInst>(I); 86198396Srdivacky return (isBitCastOfMallocCall(BCI)) ? cast<CallInst>(BCI->getOperand(0)) 87198396Srdivacky : NULL; 88198396Srdivacky} 89198396Srdivacky 90198396Srdivackystatic Value *computeArraySize(const CallInst *CI, const TargetData *TD, 91198396Srdivacky bool LookThroughSExt = false) { 92198396Srdivacky if (!CI) 93198396Srdivacky return NULL; 94198396Srdivacky 95198396Srdivacky // The size of the malloc's result type must be known to determine array size. 96198396Srdivacky const Type *T = getMallocAllocatedType(CI); 97198396Srdivacky if (!T || !T->isSized() || !TD) 98198396Srdivacky return NULL; 99198396Srdivacky 100198090Srdivacky unsigned ElementSize = TD->getTypeAllocSize(T); 101193323Sed if (const StructType *ST = dyn_cast<StructType>(T)) 102193323Sed ElementSize = TD->getStructLayout(ST)->getSizeInBytes(); 103198396Srdivacky 104198396Srdivacky // If malloc calls' arg can be determined to be a multiple of ElementSize, 105198396Srdivacky // return the multiple. Otherwise, return NULL. 106198396Srdivacky Value *MallocArg = CI->getOperand(1); 107198396Srdivacky Value *Multiple = NULL; 108198396Srdivacky if (ComputeMultiple(MallocArg, ElementSize, Multiple, 109198396Srdivacky LookThroughSExt)) 110198396Srdivacky return Multiple; 111198396Srdivacky 112198396Srdivacky return NULL; 113198396Srdivacky} 114198396Srdivacky 115198090Srdivacky/// isArrayMalloc - Returns the corresponding CallInst if the instruction 116198396Srdivacky/// is a call to malloc whose array size can be determined and the array size 117198396Srdivacky/// is not constant 1. Otherwise, return NULL. 118198090Srdivackyconst CallInst *llvm::isArrayMalloc(const Value *I, const TargetData *TD) { 119193323Sed const CallInst *CI = extractMallocCall(I); 120 Value *ArraySize = computeArraySize(CI, TD); 121 122 if (ArraySize && 123 ArraySize != ConstantInt::get(CI->getOperand(1)->getType(), 1)) 124 return CI; 125 126 // CI is a non-array malloc or we can't figure out that it is an array malloc. 127 return NULL; 128} 129 130/// getMallocType - Returns the PointerType resulting from the malloc call. 131/// The PointerType depends on the number of bitcast uses of the malloc call: 132/// 0: PointerType is the calls' return type. 133/// 1: PointerType is the bitcast's result type. 134/// >1: Unique PointerType cannot be determined, return NULL. 135const PointerType *llvm::getMallocType(const CallInst *CI) { 136 assert(isMalloc(CI) && "getMallocType and not malloc call"); 137 138 const PointerType *MallocType = NULL; 139 unsigned NumOfBitCastUses = 0; 140 141 // Determine if CallInst has a bitcast use. 142 for (Value::const_use_iterator UI = CI->use_begin(), E = CI->use_end(); 143 UI != E; ) 144 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(*UI++)) { 145 MallocType = cast<PointerType>(BCI->getDestTy()); 146 NumOfBitCastUses++; 147 } 148 149 // Malloc call has 1 bitcast use, so type is the bitcast's destination type. 150 if (NumOfBitCastUses == 1) 151 return MallocType; 152 153 // Malloc call was not bitcast, so type is the malloc function's return type. 154 if (NumOfBitCastUses == 0) 155 return cast<PointerType>(CI->getType()); 156 157 // Type could not be determined. 158 return NULL; 159} 160 161/// getMallocAllocatedType - Returns the Type allocated by malloc call. 162/// The Type depends on the number of bitcast uses of the malloc call: 163/// 0: PointerType is the malloc calls' return type. 164/// 1: PointerType is the bitcast's result type. 165/// >1: Unique PointerType cannot be determined, return NULL. 166const Type *llvm::getMallocAllocatedType(const CallInst *CI) { 167 const PointerType *PT = getMallocType(CI); 168 return PT ? PT->getElementType() : NULL; 169} 170 171/// getMallocArraySize - Returns the array size of a malloc call. If the 172/// argument passed to malloc is a multiple of the size of the malloced type, 173/// then return that multiple. For non-array mallocs, the multiple is 174/// constant 1. Otherwise, return NULL for mallocs whose array size cannot be 175/// determined. 176Value *llvm::getMallocArraySize(CallInst *CI, const TargetData *TD, 177 bool LookThroughSExt) { 178 assert(isMalloc(CI) && "getMallocArraySize and not malloc call"); 179 return computeArraySize(CI, TD, LookThroughSExt); 180} 181 182//===----------------------------------------------------------------------===// 183// free Call Utility Functions. 184// 185 186/// isFreeCall - Returns true if the value is a call to the builtin free() 187bool llvm::isFreeCall(const Value *I) { 188 const CallInst *CI = dyn_cast<CallInst>(I); 189 if (!CI) 190 return false; 191 Function *Callee = CI->getCalledFunction(); 192 if (Callee == 0 || !Callee->isDeclaration() || Callee->getName() != "free") 193 return false; 194 195 // Check free prototype. 196 // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin 197 // attribute will exist. 198 const FunctionType *FTy = Callee->getFunctionType(); 199 if (!FTy->getReturnType()->isVoidTy()) 200 return false; 201 if (FTy->getNumParams() != 1) 202 return false; 203 if (FTy->param_begin()->get() != Type::getInt8PtrTy(Callee->getContext())) 204 return false; 205 206 return true; 207} 208