blob: e710350fa063a329752d81dd9143a6bdf6d1f579 [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"
Evan Chengfabcb912009-09-10 04:36:43 +000020using namespace llvm;
21
22//===----------------------------------------------------------------------===//
23// malloc Call Utility Functions.
24//
25
26/// isMalloc - Returns true if the the value is either a malloc call or a
27/// bitcast of the result of a malloc call.
Victor Hernandez88efeae2009-11-03 20:02:35 +000028bool llvm::isMalloc(const Value *I) {
Evan Chengfabcb912009-09-10 04:36:43 +000029 return extractMallocCall(I) || extractMallocCallFromBitCast(I);
30}
31
32static bool isMallocCall(const CallInst *CI) {
33 if (!CI)
34 return false;
35
Victor Hernandez3ad70d52009-11-03 20:39:35 +000036 Function *Callee = CI->getCalledFunction();
37 if (Callee == 0 || !Callee->isDeclaration() || Callee->getName() != "malloc")
Evan Chengfabcb912009-09-10 04:36:43 +000038 return false;
39
Torok Edwin85c005a2009-10-05 21:15:43 +000040 // Check malloc prototype.
Torok Edwin4de86fe2009-10-07 09:22:55 +000041 // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin
42 // attribute will exist.
Victor Hernandez3ad70d52009-11-03 20:39:35 +000043 const FunctionType *FTy = Callee->getFunctionType();
Torok Edwin85c005a2009-10-05 21:15:43 +000044 if (FTy->getNumParams() != 1)
45 return false;
46 if (IntegerType *ITy = dyn_cast<IntegerType>(FTy->param_begin()->get())) {
47 if (ITy->getBitWidth() != 32 && ITy->getBitWidth() != 64)
48 return false;
49 return true;
50 }
51
52 return false;
Evan Chengfabcb912009-09-10 04:36:43 +000053}
54
55/// extractMallocCall - Returns the corresponding CallInst if the instruction
56/// is a malloc call. Since CallInst::CreateMalloc() only creates calls, we
57/// ignore InvokeInst here.
Victor Hernandez88efeae2009-11-03 20:02:35 +000058const CallInst *llvm::extractMallocCall(const Value *I) {
Evan Chengfabcb912009-09-10 04:36:43 +000059 const CallInst *CI = dyn_cast<CallInst>(I);
60 return (isMallocCall(CI)) ? CI : NULL;
61}
62
Victor Hernandez88efeae2009-11-03 20:02:35 +000063CallInst *llvm::extractMallocCall(Value *I) {
Evan Chengfabcb912009-09-10 04:36:43 +000064 CallInst *CI = dyn_cast<CallInst>(I);
65 return (isMallocCall(CI)) ? CI : NULL;
66}
67
Victor Hernandez88efeae2009-11-03 20:02:35 +000068static bool isBitCastOfMallocCall(const BitCastInst *BCI) {
Evan Chengfabcb912009-09-10 04:36:43 +000069 if (!BCI)
70 return false;
71
72 return isMallocCall(dyn_cast<CallInst>(BCI->getOperand(0)));
73}
74
75/// extractMallocCallFromBitCast - Returns the corresponding CallInst if the
76/// instruction is a bitcast of the result of a malloc call.
Victor Hernandez88efeae2009-11-03 20:02:35 +000077CallInst *llvm::extractMallocCallFromBitCast(Value *I) {
Evan Chengfabcb912009-09-10 04:36:43 +000078 BitCastInst *BCI = dyn_cast<BitCastInst>(I);
Victor Hernandez399e45b2009-09-10 20:18:57 +000079 return (isBitCastOfMallocCall(BCI)) ? cast<CallInst>(BCI->getOperand(0))
80 : NULL;
Evan Chengfabcb912009-09-10 04:36:43 +000081}
82
Victor Hernandez88efeae2009-11-03 20:02:35 +000083const CallInst *llvm::extractMallocCallFromBitCast(const Value *I) {
Evan Chengfabcb912009-09-10 04:36:43 +000084 const BitCastInst *BCI = dyn_cast<BitCastInst>(I);
Victor Hernandez399e45b2009-09-10 20:18:57 +000085 return (isBitCastOfMallocCall(BCI)) ? cast<CallInst>(BCI->getOperand(0))
86 : NULL;
Evan Chengfabcb912009-09-10 04:36:43 +000087}
88
Victor Hernandez90f48e72009-10-28 20:18:55 +000089/// isConstantOne - Return true only if val is constant int 1.
90static bool isConstantOne(Value *val) {
91 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
92}
93
Victor Hernandez88efeae2009-11-03 20:02:35 +000094static Value *isArrayMallocHelper(const CallInst *CI, LLVMContext &Context,
95 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 Hernandez90f48e72009-10-28 20:18:55 +000099 // Type must be known to determine array size.
Victor Hernandez88efeae2009-11-03 20:02:35 +0000100 const Type *T = getMallocAllocatedType(CI);
Victor Hernandez90f48e72009-10-28 20:18:55 +0000101 if (!T)
102 return NULL;
Victor Hernandez88d98392009-09-18 19:20:02 +0000103
Victor Hernandez88efeae2009-11-03 20:02:35 +0000104 Value *MallocArg = CI->getOperand(1);
105 ConstantExpr *CO = dyn_cast<ConstantExpr>(MallocArg);
106 BinaryOperator *BO = dyn_cast<BinaryOperator>(MallocArg);
Victor Hernandez90f48e72009-10-28 20:18:55 +0000107
Victor Hernandez88efeae2009-11-03 20:02:35 +0000108 Constant *ElementSize = ConstantExpr::getSizeOf(T);
Victor Hernandez88d98392009-09-18 19:20:02 +0000109 ElementSize = ConstantExpr::getTruncOrBitCast(ElementSize,
110 MallocArg->getType());
Victor Hernandez90f48e72009-10-28 20:18:55 +0000111 Constant *FoldedElementSize =
112 ConstantFoldConstantExpression(cast<ConstantExpr>(ElementSize), Context, TD);
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.
115 if (CO && ((CO == ElementSize) ||
116 (FoldedElementSize && (CO == FoldedElementSize))))
117 // Match CreateMalloc's use of constant 1 array-size for non-array mallocs.
118 return ConstantInt::get(MallocArg->getType(), 1);
Victor Hernandez88d98392009-09-18 19:20:02 +0000119
Victor Hernandez90f48e72009-10-28 20:18:55 +0000120 // Second, check if CI is an array malloc whose array size can be determined.
121 if (isConstantOne(ElementSize) ||
122 (FoldedElementSize && isConstantOne(FoldedElementSize)))
123 return MallocArg;
Evan Chengfabcb912009-09-10 04:36:43 +0000124
Victor Hernandez90f48e72009-10-28 20:18:55 +0000125 if (!CO && !BO)
126 return NULL;
Evan Chengfabcb912009-09-10 04:36:43 +0000127
Victor Hernandez88efeae2009-11-03 20:02:35 +0000128 Value *Op0 = NULL;
129 Value *Op1 = NULL;
Victor Hernandez90f48e72009-10-28 20:18:55 +0000130 unsigned Opcode = 0;
131 if (CO && ((CO->getOpcode() == Instruction::Mul) ||
132 (CO->getOpcode() == Instruction::Shl))) {
133 Op0 = CO->getOperand(0);
134 Op1 = CO->getOperand(1);
135 Opcode = CO->getOpcode();
136 }
137 if (BO && ((BO->getOpcode() == Instruction::Mul) ||
138 (BO->getOpcode() == Instruction::Shl))) {
139 Op0 = BO->getOperand(0);
140 Op1 = BO->getOperand(1);
141 Opcode = BO->getOpcode();
142 }
Victor Hernandez88d98392009-09-18 19:20:02 +0000143
Victor Hernandez90f48e72009-10-28 20:18:55 +0000144 // Determine array size if malloc's argument is the product of a mul or shl.
145 if (Op0) {
146 if (Opcode == Instruction::Mul) {
147 if ((Op1 == ElementSize) ||
148 (FoldedElementSize && (Op1 == FoldedElementSize)))
149 // ArraySize * ElementSize
150 return Op0;
151 if ((Op0 == ElementSize) ||
152 (FoldedElementSize && (Op0 == FoldedElementSize)))
153 // 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);
162 Value *Op1Pow = ConstantInt::get(Context,
163 APInt(Op1Int.getBitWidth(), 0).set(BitToSet));
Victor Hernandez90f48e72009-10-28 20:18:55 +0000164 if (Op0 == ElementSize || (FoldedElementSize && Op0 == FoldedElementSize))
165 // ArraySize << log2(ElementSize)
166 return Op1Pow;
167 if (Op1Pow == ElementSize ||
Victor Hernandezbc117b82009-11-02 18:51:28 +0000168 (FoldedElementSize && Op1Pow == FoldedElementSize))
Victor Hernandez90f48e72009-10-28 20:18:55 +0000169 // ElementSize << log2(ArraySize)
170 return Op0;
171 }
172 }
Victor Hernandez88d98392009-09-18 19:20:02 +0000173
Victor Hernandez90f48e72009-10-28 20:18:55 +0000174 // We could not determine the malloc array size from MallocArg.
175 return NULL;
Evan Chengfabcb912009-09-10 04:36:43 +0000176}
177
178/// isArrayMalloc - Returns the corresponding CallInst if the instruction
Victor Hernandez90f48e72009-10-28 20:18:55 +0000179/// is a call to malloc whose array size can be determined and the array size
180/// is not constant 1. Otherwise, return NULL.
Victor Hernandez88efeae2009-11-03 20:02:35 +0000181CallInst *llvm::isArrayMalloc(Value *I, LLVMContext &Context,
182 const TargetData *TD) {
Evan Chengfabcb912009-09-10 04:36:43 +0000183 CallInst *CI = extractMallocCall(I);
Victor Hernandez88efeae2009-11-03 20:02:35 +0000184 Value *ArraySize = isArrayMallocHelper(CI, Context, TD);
Victor Hernandez90f48e72009-10-28 20:18:55 +0000185
186 if (ArraySize &&
187 ArraySize != ConstantInt::get(CI->getOperand(1)->getType(), 1))
188 return CI;
189
190 // CI is a non-array malloc or we can't figure out that it is an array malloc.
191 return NULL;
Evan Chengfabcb912009-09-10 04:36:43 +0000192}
193
Victor Hernandez88efeae2009-11-03 20:02:35 +0000194const CallInst *llvm::isArrayMalloc(const Value *I, LLVMContext &Context,
195 const TargetData *TD) {
Evan Chengfabcb912009-09-10 04:36:43 +0000196 const CallInst *CI = extractMallocCall(I);
Victor Hernandez88efeae2009-11-03 20:02:35 +0000197 Value *ArraySize = isArrayMallocHelper(CI, Context, TD);
Victor Hernandez90f48e72009-10-28 20:18:55 +0000198
199 if (ArraySize &&
200 ArraySize != ConstantInt::get(CI->getOperand(1)->getType(), 1))
201 return CI;
202
203 // CI is a non-array malloc or we can't figure out that it is an array malloc.
204 return NULL;
Evan Chengfabcb912009-09-10 04:36:43 +0000205}
206
207/// getMallocType - Returns the PointerType resulting from the malloc call.
208/// This PointerType is the result type of the call's only bitcast use.
209/// If there is no unique bitcast use, then return NULL.
Victor Hernandez88efeae2009-11-03 20:02:35 +0000210const PointerType *llvm::getMallocType(const CallInst *CI) {
Evan Chengfabcb912009-09-10 04:36:43 +0000211 assert(isMalloc(CI) && "GetMallocType and not malloc call");
212
Victor Hernandez88efeae2009-11-03 20:02:35 +0000213 const BitCastInst *BCI = NULL;
Victor Hernandez88d98392009-09-18 19:20:02 +0000214
215 // Determine if CallInst has a bitcast use.
216 for (Value::use_const_iterator UI = CI->use_begin(), E = CI->use_end();
217 UI != E; )
218 if ((BCI = dyn_cast<BitCastInst>(cast<Instruction>(*UI++))))
219 break;
Evan Chengfabcb912009-09-10 04:36:43 +0000220
Victor Hernandez88d98392009-09-18 19:20:02 +0000221 // Malloc call has 1 bitcast use and no other uses, so type is the bitcast's
222 // destination type.
223 if (BCI && CI->hasOneUse())
224 return cast<PointerType>(BCI->getDestTy());
Evan Chengfabcb912009-09-10 04:36:43 +0000225
Victor Hernandez60cfc032009-09-22 18:50:03 +0000226 // Malloc call was not bitcast, so type is the malloc function's return type.
Victor Hernandez88d98392009-09-18 19:20:02 +0000227 if (!BCI)
228 return cast<PointerType>(CI->getType());
229
230 // Type could not be determined.
231 return NULL;
Evan Chengfabcb912009-09-10 04:36:43 +0000232}
233
234/// getMallocAllocatedType - Returns the Type allocated by malloc call. This
235/// Type is the result type of the call's only bitcast use. If there is no
236/// unique bitcast use, then return NULL.
Victor Hernandez88efeae2009-11-03 20:02:35 +0000237const Type *llvm::getMallocAllocatedType(const CallInst *CI) {
238 const PointerType *PT = getMallocType(CI);
Evan Chengfabcb912009-09-10 04:36:43 +0000239 return PT ? PT->getElementType() : NULL;
240}
241
Victor Hernandez90f48e72009-10-28 20:18:55 +0000242/// getMallocArraySize - Returns the array size of a malloc call. If the
243/// argument passed to malloc is a multiple of the size of the malloced type,
244/// then return that multiple. For non-array mallocs, the multiple is
245/// constant 1. Otherwise, return NULL for mallocs whose array size cannot be
Victor Hernandez2491ce02009-10-15 20:14:52 +0000246/// determined.
Victor Hernandez88efeae2009-11-03 20:02:35 +0000247Value *llvm::getMallocArraySize(CallInst *CI, LLVMContext &Context,
248 const TargetData *TD) {
Victor Hernandez90f48e72009-10-28 20:18:55 +0000249 return isArrayMallocHelper(CI, Context, TD);
Evan Chengfabcb912009-09-10 04:36:43 +0000250}
Victor Hernandez66284e02009-10-24 04:23:03 +0000251
Victor Hernandez046e78c2009-10-26 23:43:48 +0000252//===----------------------------------------------------------------------===//
253// free Call Utility Functions.
254//
255
Victor Hernandez66284e02009-10-24 04:23:03 +0000256/// isFreeCall - Returns true if the the value is a call to the builtin free()
Victor Hernandez88efeae2009-11-03 20:02:35 +0000257bool llvm::isFreeCall(const Value *I) {
Victor Hernandez66284e02009-10-24 04:23:03 +0000258 const CallInst *CI = dyn_cast<CallInst>(I);
259 if (!CI)
260 return false;
Victor Hernandez3ad70d52009-11-03 20:39:35 +0000261 Function *Callee = CI->getCalledFunction();
262 if (Callee == 0 || !Callee->isDeclaration() || Callee->getName() != "free")
Victor Hernandez66284e02009-10-24 04:23:03 +0000263 return false;
264
265 // Check free prototype.
266 // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin
267 // attribute will exist.
Victor Hernandez3ad70d52009-11-03 20:39:35 +0000268 const FunctionType *FTy = Callee->getFunctionType();
269 if (!FTy->getReturnType()->isVoidTy())
Victor Hernandez66284e02009-10-24 04:23:03 +0000270 return false;
271 if (FTy->getNumParams() != 1)
272 return false;
Victor Hernandez3ad70d52009-11-03 20:39:35 +0000273 if (FTy->param_begin()->get() != Type::getInt8PtrTy(Callee->getContext()))
Victor Hernandez66284e02009-10-24 04:23:03 +0000274 return false;
275
276 return true;
277}