blob: 347a7ea7a8b9cc17cf18ff76f8084ed8b0882512 [file] [log] [blame]
Victor Hernandezf006b182009-10-27 20:05:49 +00001//===------ MemoryBuiltins.cpp - Identify calls to memory builtins --------===//
Evan Chengfabcb912009-09-10 04:36:43 +00002//
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//
Victor Hernandezf006b182009-10-27 20:05:49 +000010// This family of functions identifies calls to builtin functions that allocate
11// or free memory.
Evan Chengfabcb912009-09-10 04:36:43 +000012//
13//===----------------------------------------------------------------------===//
14
Victor Hernandezf006b182009-10-27 20:05:49 +000015#include "llvm/Analysis/MemoryBuiltins.h"
Evan Chengfabcb912009-09-10 04:36:43 +000016#include "llvm/Constants.h"
17#include "llvm/Instructions.h"
18#include "llvm/Module.h"
Victor Hernandez8e345a12009-11-10 08:32:25 +000019#include "llvm/Analysis/ValueTracking.h"
Victor Hernandez9d0b7042009-11-07 00:16:28 +000020#include "llvm/Target/TargetData.h"
Evan Chengfabcb912009-09-10 04:36:43 +000021using namespace llvm;
22
23//===----------------------------------------------------------------------===//
24// malloc Call Utility Functions.
25//
26
Dan Gohmanf451cb82010-02-10 16:03:48 +000027/// isMalloc - Returns true if the value is either a malloc call or a
Evan Chengfabcb912009-09-10 04:36:43 +000028/// bitcast of the result of a malloc call.
Victor Hernandez88efeae2009-11-03 20:02:35 +000029bool llvm::isMalloc(const Value *I) {
Evan Chengfabcb912009-09-10 04:36:43 +000030 return extractMallocCall(I) || extractMallocCallFromBitCast(I);
31}
32
33static bool isMallocCall(const CallInst *CI) {
34 if (!CI)
35 return false;
36
Victor Hernandez3ad70d52009-11-03 20:39:35 +000037 Function *Callee = CI->getCalledFunction();
Nick Lewycky42e72ca2011-03-15 07:31:32 +000038 if (Callee == 0 || !Callee->isDeclaration())
39 return false;
40 if (Callee->getName() != "malloc" &&
Nick Lewycky1ace1692011-03-17 05:20:12 +000041 Callee->getName() != "_Znwj" && // operator new(unsigned int)
42 Callee->getName() != "_Znwm" && // operator new(unsigned long)
43 Callee->getName() != "_Znaj" && // operator new[](unsigned int)
44 Callee->getName() != "_Znam") // operator new[](unsigned long)
Evan Chengfabcb912009-09-10 04:36:43 +000045 return false;
46
Torok Edwin85c005a2009-10-05 21:15:43 +000047 // Check malloc prototype.
Torok Edwin4de86fe2009-10-07 09:22:55 +000048 // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin
49 // attribute will exist.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000050 FunctionType *FTy = Callee->getFunctionType();
Benjamin Kramer1db1c232011-11-23 17:58:47 +000051 return FTy->getReturnType() == Type::getInt8PtrTy(FTy->getContext()) &&
52 FTy->getNumParams() == 1 &&
53 (FTy->getParamType(0)->isIntegerTy(32) ||
54 FTy->getParamType(0)->isIntegerTy(64));
Evan Chengfabcb912009-09-10 04:36:43 +000055}
56
57/// extractMallocCall - Returns the corresponding CallInst if the instruction
58/// is a malloc call. Since CallInst::CreateMalloc() only creates calls, we
59/// ignore InvokeInst here.
Victor Hernandez88efeae2009-11-03 20:02:35 +000060const CallInst *llvm::extractMallocCall(const Value *I) {
Evan Chengfabcb912009-09-10 04:36:43 +000061 const CallInst *CI = dyn_cast<CallInst>(I);
62 return (isMallocCall(CI)) ? CI : NULL;
63}
64
Victor Hernandez88efeae2009-11-03 20:02:35 +000065CallInst *llvm::extractMallocCall(Value *I) {
Evan Chengfabcb912009-09-10 04:36:43 +000066 CallInst *CI = dyn_cast<CallInst>(I);
67 return (isMallocCall(CI)) ? CI : NULL;
68}
69
Victor Hernandez88efeae2009-11-03 20:02:35 +000070static bool isBitCastOfMallocCall(const BitCastInst *BCI) {
Evan Chengfabcb912009-09-10 04:36:43 +000071 if (!BCI)
72 return false;
73
74 return isMallocCall(dyn_cast<CallInst>(BCI->getOperand(0)));
75}
76
77/// extractMallocCallFromBitCast - Returns the corresponding CallInst if the
78/// instruction is a bitcast of the result of a malloc call.
Victor Hernandez88efeae2009-11-03 20:02:35 +000079CallInst *llvm::extractMallocCallFromBitCast(Value *I) {
Evan Chengfabcb912009-09-10 04:36:43 +000080 BitCastInst *BCI = dyn_cast<BitCastInst>(I);
Victor Hernandez399e45b2009-09-10 20:18:57 +000081 return (isBitCastOfMallocCall(BCI)) ? cast<CallInst>(BCI->getOperand(0))
82 : NULL;
Evan Chengfabcb912009-09-10 04:36:43 +000083}
84
Victor Hernandez88efeae2009-11-03 20:02:35 +000085const CallInst *llvm::extractMallocCallFromBitCast(const Value *I) {
Evan Chengfabcb912009-09-10 04:36:43 +000086 const BitCastInst *BCI = dyn_cast<BitCastInst>(I);
Victor Hernandez399e45b2009-09-10 20:18:57 +000087 return (isBitCastOfMallocCall(BCI)) ? cast<CallInst>(BCI->getOperand(0))
88 : NULL;
Evan Chengfabcb912009-09-10 04:36:43 +000089}
90
Victor Hernandez8e345a12009-11-10 08:32:25 +000091static Value *computeArraySize(const CallInst *CI, const TargetData *TD,
92 bool LookThroughSExt = false) {
Evan Chengfabcb912009-09-10 04:36:43 +000093 if (!CI)
Victor Hernandez90f48e72009-10-28 20:18:55 +000094 return NULL;
Evan Chengfabcb912009-09-10 04:36:43 +000095
Victor Hernandez9d0b7042009-11-07 00:16:28 +000096 // The size of the malloc's result type must be known to determine array size.
Chris Lattnerdb125cf2011-07-18 04:54:35 +000097 Type *T = getMallocAllocatedType(CI);
Victor Hernandez9d0b7042009-11-07 00:16:28 +000098 if (!T || !T->isSized() || !TD)
Victor Hernandez90f48e72009-10-28 20:18:55 +000099 return NULL;
Victor Hernandez88d98392009-09-18 19:20:02 +0000100
Victor Hernandez8e345a12009-11-10 08:32:25 +0000101 unsigned ElementSize = TD->getTypeAllocSize(T);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000102 if (StructType *ST = dyn_cast<StructType>(T))
Victor Hernandez8e345a12009-11-10 08:32:25 +0000103 ElementSize = TD->getStructLayout(ST)->getSizeInBytes();
Victor Hernandez88d98392009-09-18 19:20:02 +0000104
Gabor Greife3401c42010-06-23 21:41:47 +0000105 // If malloc call's arg can be determined to be a multiple of ElementSize,
Victor Hernandez8e345a12009-11-10 08:32:25 +0000106 // return the multiple. Otherwise, return NULL.
Gabor Greife3401c42010-06-23 21:41:47 +0000107 Value *MallocArg = CI->getArgOperand(0);
Victor Hernandez8e345a12009-11-10 08:32:25 +0000108 Value *Multiple = NULL;
Victor Hernandez8e345a12009-11-10 08:32:25 +0000109 if (ComputeMultiple(MallocArg, ElementSize, Multiple,
Dan Gohman3dbb9e62009-11-18 00:58:27 +0000110 LookThroughSExt))
Victor Hernandez8e345a12009-11-10 08:32:25 +0000111 return Multiple;
Victor Hernandez88d98392009-09-18 19:20:02 +0000112
Victor Hernandez90f48e72009-10-28 20:18:55 +0000113 return NULL;
Evan Chengfabcb912009-09-10 04:36:43 +0000114}
115
116/// isArrayMalloc - Returns the corresponding CallInst if the instruction
Victor Hernandez90f48e72009-10-28 20:18:55 +0000117/// is a call to malloc whose array size can be determined and the array size
118/// is not constant 1. Otherwise, return NULL.
Chris Lattner7b550cc2009-11-06 04:27:31 +0000119const CallInst *llvm::isArrayMalloc(const Value *I, const TargetData *TD) {
Evan Chengfabcb912009-09-10 04:36:43 +0000120 const CallInst *CI = extractMallocCall(I);
Victor Hernandez8e345a12009-11-10 08:32:25 +0000121 Value *ArraySize = computeArraySize(CI, TD);
Victor Hernandez90f48e72009-10-28 20:18:55 +0000122
123 if (ArraySize &&
Gabor Greife3401c42010-06-23 21:41:47 +0000124 ArraySize != ConstantInt::get(CI->getArgOperand(0)->getType(), 1))
Victor Hernandez90f48e72009-10-28 20:18:55 +0000125 return CI;
126
127 // CI is a non-array malloc or we can't figure out that it is an array malloc.
128 return NULL;
Evan Chengfabcb912009-09-10 04:36:43 +0000129}
130
131/// getMallocType - Returns the PointerType resulting from the malloc call.
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000132/// The PointerType depends on the number of bitcast uses of the malloc call:
133/// 0: PointerType is the calls' return type.
134/// 1: PointerType is the bitcast's result type.
135/// >1: Unique PointerType cannot be determined, return NULL.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000136PointerType *llvm::getMallocType(const CallInst *CI) {
Victor Hernandez8e345a12009-11-10 08:32:25 +0000137 assert(isMalloc(CI) && "getMallocType and not malloc call");
Evan Chengfabcb912009-09-10 04:36:43 +0000138
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000139 PointerType *MallocType = NULL;
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000140 unsigned NumOfBitCastUses = 0;
141
Victor Hernandez88d98392009-09-18 19:20:02 +0000142 // Determine if CallInst has a bitcast use.
Gabor Greif60ad7812010-03-25 23:06:16 +0000143 for (Value::const_use_iterator UI = CI->use_begin(), E = CI->use_end();
Victor Hernandez88d98392009-09-18 19:20:02 +0000144 UI != E; )
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000145 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(*UI++)) {
146 MallocType = cast<PointerType>(BCI->getDestTy());
147 NumOfBitCastUses++;
148 }
Evan Chengfabcb912009-09-10 04:36:43 +0000149
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000150 // Malloc call has 1 bitcast use, so type is the bitcast's destination type.
151 if (NumOfBitCastUses == 1)
152 return MallocType;
Evan Chengfabcb912009-09-10 04:36:43 +0000153
Victor Hernandez60cfc032009-09-22 18:50:03 +0000154 // Malloc call was not bitcast, so type is the malloc function's return type.
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000155 if (NumOfBitCastUses == 0)
Victor Hernandez88d98392009-09-18 19:20:02 +0000156 return cast<PointerType>(CI->getType());
157
158 // Type could not be determined.
159 return NULL;
Evan Chengfabcb912009-09-10 04:36:43 +0000160}
161
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000162/// getMallocAllocatedType - Returns the Type allocated by malloc call.
163/// The Type depends on the number of bitcast uses of the malloc call:
164/// 0: PointerType is the malloc calls' return type.
165/// 1: PointerType is the bitcast's result type.
166/// >1: Unique PointerType cannot be determined, return NULL.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000167Type *llvm::getMallocAllocatedType(const CallInst *CI) {
168 PointerType *PT = getMallocType(CI);
Evan Chengfabcb912009-09-10 04:36:43 +0000169 return PT ? PT->getElementType() : NULL;
170}
171
Victor Hernandez90f48e72009-10-28 20:18:55 +0000172/// getMallocArraySize - Returns the array size of a malloc call. If the
173/// argument passed to malloc is a multiple of the size of the malloced type,
174/// then return that multiple. For non-array mallocs, the multiple is
175/// constant 1. Otherwise, return NULL for mallocs whose array size cannot be
Victor Hernandez2491ce02009-10-15 20:14:52 +0000176/// determined.
Victor Hernandez8e345a12009-11-10 08:32:25 +0000177Value *llvm::getMallocArraySize(CallInst *CI, const TargetData *TD,
178 bool LookThroughSExt) {
179 assert(isMalloc(CI) && "getMallocArraySize and not malloc call");
180 return computeArraySize(CI, TD, LookThroughSExt);
Evan Chengfabcb912009-09-10 04:36:43 +0000181}
Victor Hernandez66284e02009-10-24 04:23:03 +0000182
Nuno Lopes252ef562012-05-03 21:19:58 +0000183
184//===----------------------------------------------------------------------===//
185// clloc Call Utility Functions.
186//
187
188static bool isCallocCall(const CallInst *CI) {
189 if (!CI)
190 return false;
191
192 Function *Callee = CI->getCalledFunction();
193 if (Callee == 0 || !Callee->isDeclaration())
194 return false;
195 if (Callee->getName() != "calloc")
196 return false;
197
198 // Check malloc prototype.
199 // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin
200 // attribute will exist.
201 FunctionType *FTy = Callee->getFunctionType();
202 return FTy->getReturnType() == Type::getInt8PtrTy(FTy->getContext()) &&
203 FTy->getNumParams() == 2 &&
204 ((FTy->getParamType(0)->isIntegerTy(32) &&
205 FTy->getParamType(1)->isIntegerTy(32)) ||
206 (FTy->getParamType(0)->isIntegerTy(64) &&
207 FTy->getParamType(1)->isIntegerTy(64)));
208}
209
210/// extractCallocCall - Returns the corresponding CallInst if the instruction
211/// is a calloc call.
212const CallInst *llvm::extractCallocCall(const Value *I) {
213 const CallInst *CI = dyn_cast<CallInst>(I);
214 return isCallocCall(CI) ? CI : 0;
215}
216
217CallInst *llvm::extractCallocCall(Value *I) {
218 CallInst *CI = dyn_cast<CallInst>(I);
219 return isCallocCall(CI) ? CI : 0;
220}
221
222
Victor Hernandez046e78c2009-10-26 23:43:48 +0000223//===----------------------------------------------------------------------===//
224// free Call Utility Functions.
225//
226
Gabor Greif02680f92010-06-23 21:51:12 +0000227/// isFreeCall - Returns non-null if the value is a call to the builtin free()
228const CallInst *llvm::isFreeCall(const Value *I) {
Victor Hernandez66284e02009-10-24 04:23:03 +0000229 const CallInst *CI = dyn_cast<CallInst>(I);
230 if (!CI)
Gabor Greif02680f92010-06-23 21:51:12 +0000231 return 0;
Victor Hernandez3ad70d52009-11-03 20:39:35 +0000232 Function *Callee = CI->getCalledFunction();
Nick Lewycky42e72ca2011-03-15 07:31:32 +0000233 if (Callee == 0 || !Callee->isDeclaration())
234 return 0;
235
236 if (Callee->getName() != "free" &&
Nick Lewycky1ace1692011-03-17 05:20:12 +0000237 Callee->getName() != "_ZdlPv" && // operator delete(void*)
238 Callee->getName() != "_ZdaPv") // operator delete[](void*)
Gabor Greif02680f92010-06-23 21:51:12 +0000239 return 0;
Victor Hernandez66284e02009-10-24 04:23:03 +0000240
241 // Check free prototype.
242 // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin
243 // attribute will exist.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000244 FunctionType *FTy = Callee->getFunctionType();
Victor Hernandez3ad70d52009-11-03 20:39:35 +0000245 if (!FTy->getReturnType()->isVoidTy())
Gabor Greif02680f92010-06-23 21:51:12 +0000246 return 0;
Victor Hernandez66284e02009-10-24 04:23:03 +0000247 if (FTy->getNumParams() != 1)
Gabor Greif02680f92010-06-23 21:51:12 +0000248 return 0;
Chris Lattnerebb21892011-06-18 21:46:23 +0000249 if (FTy->getParamType(0) != Type::getInt8PtrTy(Callee->getContext()))
Gabor Greif02680f92010-06-23 21:51:12 +0000250 return 0;
Victor Hernandez66284e02009-10-24 04:23:03 +0000251
Gabor Greif02680f92010-06-23 21:51:12 +0000252 return CI;
Victor Hernandez66284e02009-10-24 04:23:03 +0000253}