1 //===------ MemoryBuiltins.cpp - Identify calls to memory builtins --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This family of functions identifies calls to builtin functions that allocate
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Analysis/MemoryBuiltins.h"
16 #include "llvm/Constants.h"
17 #include "llvm/Instructions.h"
18 #include "llvm/Module.h"
19 #include "llvm/Analysis/ConstantFolding.h"
20 #include "llvm/Target/TargetData.h"
23 //===----------------------------------------------------------------------===//
24 // malloc Call Utility Functions.
27 /// isMalloc - Returns true if the the value is either a malloc call or a
28 /// bitcast of the result of a malloc call.
29 bool llvm::isMalloc(const Value *I) {
30 return extractMallocCall(I) || extractMallocCallFromBitCast(I);
33 static bool isMallocCall(const CallInst *CI) {
37 Function *Callee = CI->getCalledFunction();
38 if (Callee == 0 || !Callee->isDeclaration() || Callee->getName() != "malloc")
41 // Check malloc prototype.
42 // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin
43 // attribute will exist.
44 const FunctionType *FTy = Callee->getFunctionType();
45 if (FTy->getNumParams() != 1)
47 if (IntegerType *ITy = dyn_cast<IntegerType>(FTy->param_begin()->get())) {
48 if (ITy->getBitWidth() != 32 && ITy->getBitWidth() != 64)
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.
59 const CallInst *llvm::extractMallocCall(const Value *I) {
60 const CallInst *CI = dyn_cast<CallInst>(I);
61 return (isMallocCall(CI)) ? CI : NULL;
64 CallInst *llvm::extractMallocCall(Value *I) {
65 CallInst *CI = dyn_cast<CallInst>(I);
66 return (isMallocCall(CI)) ? CI : NULL;
69 static bool isBitCastOfMallocCall(const BitCastInst *BCI) {
73 return isMallocCall(dyn_cast<CallInst>(BCI->getOperand(0)));
76 /// extractMallocCallFromBitCast - Returns the corresponding CallInst if the
77 /// instruction is a bitcast of the result of a malloc call.
78 CallInst *llvm::extractMallocCallFromBitCast(Value *I) {
79 BitCastInst *BCI = dyn_cast<BitCastInst>(I);
80 return (isBitCastOfMallocCall(BCI)) ? cast<CallInst>(BCI->getOperand(0))
84 const CallInst *llvm::extractMallocCallFromBitCast(const Value *I) {
85 const BitCastInst *BCI = dyn_cast<BitCastInst>(I);
86 return (isBitCastOfMallocCall(BCI)) ? cast<CallInst>(BCI->getOperand(0))
90 /// isConstantOne - Return true only if val is constant int 1.
91 static bool isConstantOne(Value *val) {
92 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
95 static Value *isArrayMallocHelper(const CallInst *CI, const TargetData *TD) {
99 // The size of the malloc's result type must be known to determine array size.
100 const Type *T = getMallocAllocatedType(CI);
101 if (!T || !T->isSized() || !TD)
104 Value *MallocArg = CI->getOperand(1);
105 const Type *ArgType = MallocArg->getType();
106 ConstantExpr *CO = dyn_cast<ConstantExpr>(MallocArg);
107 BinaryOperator *BO = dyn_cast<BinaryOperator>(MallocArg);
109 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);
114 // First, check if CI is a non-array malloc.
115 if (CO && CO == ElementSize)
116 // Match CreateMalloc's use of constant 1 array-size for non-array mallocs.
117 return ConstantInt::get(ArgType, 1);
119 // Second, check if CI is an array malloc whose array size can be determined.
120 if (isConstantOne(ElementSize))
123 if (ConstantInt *CInt = dyn_cast<ConstantInt>(MallocArg))
124 if (CInt->getZExtValue() % ElementSizeInt == 0)
125 return ConstantInt::get(ArgType, CInt->getZExtValue() / ElementSizeInt);
133 if (CO && ((CO->getOpcode() == Instruction::Mul) ||
134 (CO->getOpcode() == Instruction::Shl))) {
135 Op0 = CO->getOperand(0);
136 Op1 = CO->getOperand(1);
137 Opcode = CO->getOpcode();
139 if (BO && ((BO->getOpcode() == Instruction::Mul) ||
140 (BO->getOpcode() == Instruction::Shl))) {
141 Op0 = BO->getOperand(0);
142 Op1 = BO->getOperand(1);
143 Opcode = BO->getOpcode();
146 // Determine array size if malloc's argument is the product of a mul or shl.
148 if (Opcode == Instruction::Mul) {
149 if (Op1 == ElementSize)
150 // ArraySize * ElementSize
152 if (Op0 == ElementSize)
153 // ElementSize * ArraySize
156 if (Opcode == Instruction::Shl) {
157 ConstantInt *Op1CI = dyn_cast<ConstantInt>(Op1);
158 if (!Op1CI) return NULL;
160 APInt Op1Int = Op1CI->getValue();
161 uint64_t BitToSet = Op1Int.getLimitedValue(Op1Int.getBitWidth() - 1);
162 Value *Op1Pow = ConstantInt::get(Op1CI->getContext(),
163 APInt(Op1Int.getBitWidth(), 0).set(BitToSet));
164 if (Op0 == ElementSize)
165 // ArraySize << log2(ElementSize)
167 if (Op1Pow == ElementSize)
168 // ElementSize << log2(ArraySize)
173 // We could not determine the malloc array size from MallocArg.
177 /// isArrayMalloc - Returns the corresponding CallInst if the instruction
178 /// is a call to malloc whose array size can be determined and the array size
179 /// is not constant 1. Otherwise, return NULL.
180 CallInst *llvm::isArrayMalloc(Value *I, const TargetData *TD) {
181 CallInst *CI = extractMallocCall(I);
182 Value *ArraySize = isArrayMallocHelper(CI, TD);
185 ArraySize != ConstantInt::get(CI->getOperand(1)->getType(), 1))
188 // CI is a non-array malloc or we can't figure out that it is an array malloc.
192 const CallInst *llvm::isArrayMalloc(const Value *I, const TargetData *TD) {
193 const CallInst *CI = extractMallocCall(I);
194 Value *ArraySize = isArrayMallocHelper(CI, TD);
197 ArraySize != ConstantInt::get(CI->getOperand(1)->getType(), 1))
200 // CI is a non-array malloc or we can't figure out that it is an array malloc.
204 /// getMallocType - Returns the PointerType resulting from the malloc call.
205 /// 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.
209 const PointerType *llvm::getMallocType(const CallInst *CI) {
210 assert(isMalloc(CI) && "GetMallocType and not malloc call");
212 const PointerType *MallocType = NULL;
213 unsigned NumOfBitCastUses = 0;
215 // Determine if CallInst has a bitcast use.
216 for (Value::use_const_iterator UI = CI->use_begin(), E = CI->use_end();
218 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(*UI++)) {
219 MallocType = cast<PointerType>(BCI->getDestTy());
223 // Malloc call has 1 bitcast use, so type is the bitcast's destination type.
224 if (NumOfBitCastUses == 1)
227 // Malloc call was not bitcast, so type is the malloc function's return type.
228 if (NumOfBitCastUses == 0)
229 return cast<PointerType>(CI->getType());
231 // Type could not be determined.
235 /// 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.
240 const Type *llvm::getMallocAllocatedType(const CallInst *CI) {
241 const PointerType *PT = getMallocType(CI);
242 return PT ? PT->getElementType() : NULL;
245 /// 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
250 Value *llvm::getMallocArraySize(CallInst *CI, const TargetData *TD) {
251 return isArrayMallocHelper(CI, TD);
254 //===----------------------------------------------------------------------===//
255 // free Call Utility Functions.
258 /// isFreeCall - Returns true if the the value is a call to the builtin free()
259 bool llvm::isFreeCall(const Value *I) {
260 const CallInst *CI = dyn_cast<CallInst>(I);
263 Function *Callee = CI->getCalledFunction();
264 if (Callee == 0 || !Callee->isDeclaration() || Callee->getName() != "free")
267 // Check free prototype.
268 // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin
269 // attribute will exist.
270 const FunctionType *FTy = Callee->getFunctionType();
271 if (!FTy->getReturnType()->isVoidTy())
273 if (FTy->getNumParams() != 1)
275 if (FTy->param_begin()->get() != Type::getInt8PtrTy(Callee->getContext()))