blob: 6e2094027e5e9f45b97bcc8c13f259abe7bb96f1 [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 Hernandez88d98392009-09-18 19:20:02 +000019#include "llvm/Analysis/ConstantFolding.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
27/// isMalloc - Returns true if the the value is either a malloc call or a
28/// 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();
38 if (Callee == 0 || !Callee->isDeclaration() || Callee->getName() != "malloc")
Evan Chengfabcb912009-09-10 04:36:43 +000039 return false;
40
Torok Edwin85c005a2009-10-05 21:15:43 +000041 // Check malloc prototype.
Torok Edwin4de86fe2009-10-07 09:22:55 +000042 // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin
43 // attribute will exist.
Victor Hernandez3ad70d52009-11-03 20:39:35 +000044 const FunctionType *FTy = Callee->getFunctionType();
Torok Edwin85c005a2009-10-05 21:15:43 +000045 if (FTy->getNumParams() != 1)
46 return false;
47 if (IntegerType *ITy = dyn_cast<IntegerType>(FTy->param_begin()->get())) {
48 if (ITy->getBitWidth() != 32 && ITy->getBitWidth() != 64)
49 return false;
50 return true;
51 }
52
53 return false;
Evan Chengfabcb912009-09-10 04:36:43 +000054}
55
56/// extractMallocCall - Returns the corresponding CallInst if the instruction
57/// is a malloc call. Since CallInst::CreateMalloc() only creates calls, we
58/// ignore InvokeInst here.
Victor Hernandez88efeae2009-11-03 20:02:35 +000059const CallInst *llvm::extractMallocCall(const Value *I) {
Evan Chengfabcb912009-09-10 04:36:43 +000060 const CallInst *CI = dyn_cast<CallInst>(I);
61 return (isMallocCall(CI)) ? CI : NULL;
62}
63
Victor Hernandez88efeae2009-11-03 20:02:35 +000064CallInst *llvm::extractMallocCall(Value *I) {
Evan Chengfabcb912009-09-10 04:36:43 +000065 CallInst *CI = dyn_cast<CallInst>(I);
66 return (isMallocCall(CI)) ? CI : NULL;
67}
68
Victor Hernandez88efeae2009-11-03 20:02:35 +000069static bool isBitCastOfMallocCall(const BitCastInst *BCI) {
Evan Chengfabcb912009-09-10 04:36:43 +000070 if (!BCI)
71 return false;
72
73 return isMallocCall(dyn_cast<CallInst>(BCI->getOperand(0)));
74}
75
76/// extractMallocCallFromBitCast - Returns the corresponding CallInst if the
77/// instruction is a bitcast of the result of a malloc call.
Victor Hernandez88efeae2009-11-03 20:02:35 +000078CallInst *llvm::extractMallocCallFromBitCast(Value *I) {
Evan Chengfabcb912009-09-10 04:36:43 +000079 BitCastInst *BCI = dyn_cast<BitCastInst>(I);
Victor Hernandez399e45b2009-09-10 20:18:57 +000080 return (isBitCastOfMallocCall(BCI)) ? cast<CallInst>(BCI->getOperand(0))
81 : NULL;
Evan Chengfabcb912009-09-10 04:36:43 +000082}
83
Victor Hernandez88efeae2009-11-03 20:02:35 +000084const CallInst *llvm::extractMallocCallFromBitCast(const Value *I) {
Evan Chengfabcb912009-09-10 04:36:43 +000085 const BitCastInst *BCI = dyn_cast<BitCastInst>(I);
Victor Hernandez399e45b2009-09-10 20:18:57 +000086 return (isBitCastOfMallocCall(BCI)) ? cast<CallInst>(BCI->getOperand(0))
87 : NULL;
Evan Chengfabcb912009-09-10 04:36:43 +000088}
89
Victor Hernandez90f48e72009-10-28 20:18:55 +000090/// isConstantOne - Return true only if val is constant int 1.
91static bool isConstantOne(Value *val) {
92 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
93}
94
Chris Lattner7b550cc2009-11-06 04:27:31 +000095static Value *isArrayMallocHelper(const CallInst *CI, const TargetData *TD) {
Evan Chengfabcb912009-09-10 04:36:43 +000096 if (!CI)
Victor Hernandez90f48e72009-10-28 20:18:55 +000097 return NULL;
Evan Chengfabcb912009-09-10 04:36:43 +000098
Victor Hernandez9d0b7042009-11-07 00:16:28 +000099 // The size of the malloc's result type must be known to determine array size.
Victor Hernandez88efeae2009-11-03 20:02:35 +0000100 const Type *T = getMallocAllocatedType(CI);
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000101 if (!T || !T->isSized() || !TD)
Victor Hernandez90f48e72009-10-28 20:18:55 +0000102 return NULL;
Victor Hernandez88d98392009-09-18 19:20:02 +0000103
Victor Hernandez88efeae2009-11-03 20:02:35 +0000104 Value *MallocArg = CI->getOperand(1);
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000105 const Type *ArgType = MallocArg->getType();
Victor Hernandez88efeae2009-11-03 20:02:35 +0000106 ConstantExpr *CO = dyn_cast<ConstantExpr>(MallocArg);
107 BinaryOperator *BO = dyn_cast<BinaryOperator>(MallocArg);
Victor Hernandez90f48e72009-10-28 20:18:55 +0000108
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000109 unsigned ElementSizeInt = TD->getTypeAllocSize(T);
110 if (const StructType *ST = dyn_cast<StructType>(T))
111 ElementSizeInt = TD->getStructLayout(ST)->getSizeInBytes();
112 Constant *ElementSize = ConstantInt::get(ArgType, ElementSizeInt);
Victor Hernandez88d98392009-09-18 19:20:02 +0000113
Victor Hernandez90f48e72009-10-28 20:18:55 +0000114 // First, check if CI is a non-array malloc.
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000115 if (CO && CO == ElementSize)
Victor Hernandez90f48e72009-10-28 20:18:55 +0000116 // Match CreateMalloc's use of constant 1 array-size for non-array mallocs.
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000117 return ConstantInt::get(ArgType, 1);
Victor Hernandez88d98392009-09-18 19:20:02 +0000118
Victor Hernandez90f48e72009-10-28 20:18:55 +0000119 // Second, check if CI is an array malloc whose array size can be determined.
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000120 if (isConstantOne(ElementSize))
Victor Hernandez90f48e72009-10-28 20:18:55 +0000121 return MallocArg;
Evan Chengfabcb912009-09-10 04:36:43 +0000122
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000123 if (ConstantInt *CInt = dyn_cast<ConstantInt>(MallocArg))
124 if (CInt->getZExtValue() % ElementSizeInt == 0)
125 return ConstantInt::get(ArgType, CInt->getZExtValue() / ElementSizeInt);
126
Victor Hernandez90f48e72009-10-28 20:18:55 +0000127 if (!CO && !BO)
128 return NULL;
Evan Chengfabcb912009-09-10 04:36:43 +0000129
Victor Hernandez88efeae2009-11-03 20:02:35 +0000130 Value *Op0 = NULL;
131 Value *Op1 = NULL;
Victor Hernandez90f48e72009-10-28 20:18:55 +0000132 unsigned Opcode = 0;
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000133 if (CO && ((CO->getOpcode() == Instruction::Mul) ||
Victor Hernandez90f48e72009-10-28 20:18:55 +0000134 (CO->getOpcode() == Instruction::Shl))) {
135 Op0 = CO->getOperand(0);
136 Op1 = CO->getOperand(1);
137 Opcode = CO->getOpcode();
138 }
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000139 if (BO && ((BO->getOpcode() == Instruction::Mul) ||
Victor Hernandez90f48e72009-10-28 20:18:55 +0000140 (BO->getOpcode() == Instruction::Shl))) {
141 Op0 = BO->getOperand(0);
142 Op1 = BO->getOperand(1);
143 Opcode = BO->getOpcode();
144 }
Victor Hernandez88d98392009-09-18 19:20:02 +0000145
Victor Hernandez90f48e72009-10-28 20:18:55 +0000146 // Determine array size if malloc's argument is the product of a mul or shl.
147 if (Op0) {
148 if (Opcode == Instruction::Mul) {
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000149 if (Op1 == ElementSize)
Victor Hernandez90f48e72009-10-28 20:18:55 +0000150 // ArraySize * ElementSize
151 return Op0;
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000152 if (Op0 == ElementSize)
Victor Hernandez90f48e72009-10-28 20:18:55 +0000153 // ElementSize * ArraySize
154 return Op1;
155 }
156 if (Opcode == Instruction::Shl) {
Victor Hernandez88efeae2009-11-03 20:02:35 +0000157 ConstantInt *Op1CI = dyn_cast<ConstantInt>(Op1);
Victor Hernandezbc117b82009-11-02 18:51:28 +0000158 if (!Op1CI) return NULL;
159
160 APInt Op1Int = Op1CI->getValue();
Victor Hernandez88efeae2009-11-03 20:02:35 +0000161 uint64_t BitToSet = Op1Int.getLimitedValue(Op1Int.getBitWidth() - 1);
Chris Lattner7b550cc2009-11-06 04:27:31 +0000162 Value *Op1Pow = ConstantInt::get(Op1CI->getContext(),
Victor Hernandez88efeae2009-11-03 20:02:35 +0000163 APInt(Op1Int.getBitWidth(), 0).set(BitToSet));
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000164 if (Op0 == ElementSize)
Victor Hernandez90f48e72009-10-28 20:18:55 +0000165 // ArraySize << log2(ElementSize)
166 return Op1Pow;
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000167 if (Op1Pow == ElementSize)
Victor Hernandez90f48e72009-10-28 20:18:55 +0000168 // ElementSize << log2(ArraySize)
169 return Op0;
170 }
171 }
Victor Hernandez88d98392009-09-18 19:20:02 +0000172
Victor Hernandez90f48e72009-10-28 20:18:55 +0000173 // We could not determine the malloc array size from MallocArg.
174 return NULL;
Evan Chengfabcb912009-09-10 04:36:43 +0000175}
176
177/// isArrayMalloc - Returns the corresponding CallInst if the instruction
Victor Hernandez90f48e72009-10-28 20:18:55 +0000178/// is a call to malloc whose array size can be determined and the array size
179/// is not constant 1. Otherwise, return NULL.
Chris Lattner7b550cc2009-11-06 04:27:31 +0000180CallInst *llvm::isArrayMalloc(Value *I, const TargetData *TD) {
Evan Chengfabcb912009-09-10 04:36:43 +0000181 CallInst *CI = extractMallocCall(I);
Chris Lattner7b550cc2009-11-06 04:27:31 +0000182 Value *ArraySize = isArrayMallocHelper(CI, TD);
Victor Hernandez90f48e72009-10-28 20:18:55 +0000183
184 if (ArraySize &&
185 ArraySize != ConstantInt::get(CI->getOperand(1)->getType(), 1))
186 return CI;
187
188 // CI is a non-array malloc or we can't figure out that it is an array malloc.
189 return NULL;
Evan Chengfabcb912009-09-10 04:36:43 +0000190}
191
Chris Lattner7b550cc2009-11-06 04:27:31 +0000192const CallInst *llvm::isArrayMalloc(const Value *I, const TargetData *TD) {
Evan Chengfabcb912009-09-10 04:36:43 +0000193 const CallInst *CI = extractMallocCall(I);
Chris Lattner7b550cc2009-11-06 04:27:31 +0000194 Value *ArraySize = isArrayMallocHelper(CI, TD);
Victor Hernandez90f48e72009-10-28 20:18:55 +0000195
196 if (ArraySize &&
197 ArraySize != ConstantInt::get(CI->getOperand(1)->getType(), 1))
198 return CI;
199
200 // CI is a non-array malloc or we can't figure out that it is an array malloc.
201 return NULL;
Evan Chengfabcb912009-09-10 04:36:43 +0000202}
203
204/// getMallocType - Returns the PointerType resulting from the malloc call.
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000205/// The PointerType depends on the number of bitcast uses of the malloc call:
206/// 0: PointerType is the calls' return type.
207/// 1: PointerType is the bitcast's result type.
208/// >1: Unique PointerType cannot be determined, return NULL.
Victor Hernandez88efeae2009-11-03 20:02:35 +0000209const PointerType *llvm::getMallocType(const CallInst *CI) {
Evan Chengfabcb912009-09-10 04:36:43 +0000210 assert(isMalloc(CI) && "GetMallocType and not malloc call");
211
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000212 const PointerType *MallocType = NULL;
213 unsigned NumOfBitCastUses = 0;
214
Victor Hernandez88d98392009-09-18 19:20:02 +0000215 // Determine if CallInst has a bitcast use.
216 for (Value::use_const_iterator UI = CI->use_begin(), E = CI->use_end();
217 UI != E; )
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000218 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(*UI++)) {
219 MallocType = cast<PointerType>(BCI->getDestTy());
220 NumOfBitCastUses++;
221 }
Evan Chengfabcb912009-09-10 04:36:43 +0000222
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000223 // Malloc call has 1 bitcast use, so type is the bitcast's destination type.
224 if (NumOfBitCastUses == 1)
225 return MallocType;
Evan Chengfabcb912009-09-10 04:36:43 +0000226
Victor Hernandez60cfc032009-09-22 18:50:03 +0000227 // Malloc call was not bitcast, so type is the malloc function's return type.
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000228 if (NumOfBitCastUses == 0)
Victor Hernandez88d98392009-09-18 19:20:02 +0000229 return cast<PointerType>(CI->getType());
230
231 // Type could not be determined.
232 return NULL;
Evan Chengfabcb912009-09-10 04:36:43 +0000233}
234
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000235/// getMallocAllocatedType - Returns the Type allocated by malloc call.
236/// The Type depends on the number of bitcast uses of the malloc call:
237/// 0: PointerType is the malloc calls' return type.
238/// 1: PointerType is the bitcast's result type.
239/// >1: Unique PointerType cannot be determined, return NULL.
Victor Hernandez88efeae2009-11-03 20:02:35 +0000240const Type *llvm::getMallocAllocatedType(const CallInst *CI) {
241 const PointerType *PT = getMallocType(CI);
Evan Chengfabcb912009-09-10 04:36:43 +0000242 return PT ? PT->getElementType() : NULL;
243}
244
Victor Hernandez90f48e72009-10-28 20:18:55 +0000245/// getMallocArraySize - Returns the array size of a malloc call. If the
246/// argument passed to malloc is a multiple of the size of the malloced type,
247/// then return that multiple. For non-array mallocs, the multiple is
248/// constant 1. Otherwise, return NULL for mallocs whose array size cannot be
Victor Hernandez2491ce02009-10-15 20:14:52 +0000249/// determined.
Chris Lattner7b550cc2009-11-06 04:27:31 +0000250Value *llvm::getMallocArraySize(CallInst *CI, const TargetData *TD) {
251 return isArrayMallocHelper(CI, TD);
Evan Chengfabcb912009-09-10 04:36:43 +0000252}
Victor Hernandez66284e02009-10-24 04:23:03 +0000253
Victor Hernandez046e78c2009-10-26 23:43:48 +0000254//===----------------------------------------------------------------------===//
255// free Call Utility Functions.
256//
257
Victor Hernandez66284e02009-10-24 04:23:03 +0000258/// isFreeCall - Returns true if the the value is a call to the builtin free()
Victor Hernandez88efeae2009-11-03 20:02:35 +0000259bool llvm::isFreeCall(const Value *I) {
Victor Hernandez66284e02009-10-24 04:23:03 +0000260 const CallInst *CI = dyn_cast<CallInst>(I);
261 if (!CI)
262 return false;
Victor Hernandez3ad70d52009-11-03 20:39:35 +0000263 Function *Callee = CI->getCalledFunction();
264 if (Callee == 0 || !Callee->isDeclaration() || Callee->getName() != "free")
Victor Hernandez66284e02009-10-24 04:23:03 +0000265 return false;
266
267 // Check free prototype.
268 // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin
269 // attribute will exist.
Victor Hernandez3ad70d52009-11-03 20:39:35 +0000270 const FunctionType *FTy = Callee->getFunctionType();
271 if (!FTy->getReturnType()->isVoidTy())
Victor Hernandez66284e02009-10-24 04:23:03 +0000272 return false;
273 if (FTy->getNumParams() != 1)
274 return false;
Victor Hernandez3ad70d52009-11-03 20:39:35 +0000275 if (FTy->param_begin()->get() != Type::getInt8PtrTy(Callee->getContext()))
Victor Hernandez66284e02009-10-24 04:23:03 +0000276 return false;
277
278 return true;
279}