blob: 0982345cfa9fb23b216971d12c3e47cc217dcfc2 [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.
Evan Chengfabcb912009-09-10 04:36:43 +000028bool llvm::isMalloc(const Value* I) {
29 return extractMallocCall(I) || extractMallocCallFromBitCast(I);
30}
31
32static bool isMallocCall(const CallInst *CI) {
33 if (!CI)
34 return false;
35
36 const Module* M = CI->getParent()->getParent()->getParent();
Torok Edwin85c005a2009-10-05 21:15:43 +000037 Function *MallocFunc = M->getFunction("malloc");
Evan Chengfabcb912009-09-10 04:36:43 +000038
39 if (CI->getOperand(0) != MallocFunc)
40 return false;
41
Torok Edwin85c005a2009-10-05 21:15:43 +000042 // Check malloc prototype.
Torok Edwin4de86fe2009-10-07 09:22:55 +000043 // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin
44 // attribute will exist.
Torok Edwin85c005a2009-10-05 21:15:43 +000045 const FunctionType *FTy = MallocFunc->getFunctionType();
46 if (FTy->getNumParams() != 1)
47 return false;
48 if (IntegerType *ITy = dyn_cast<IntegerType>(FTy->param_begin()->get())) {
49 if (ITy->getBitWidth() != 32 && ITy->getBitWidth() != 64)
50 return false;
51 return true;
52 }
53
54 return false;
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.
60const CallInst* llvm::extractMallocCall(const Value* I) {
61 const CallInst *CI = dyn_cast<CallInst>(I);
62 return (isMallocCall(CI)) ? CI : NULL;
63}
64
65CallInst* llvm::extractMallocCall(Value* I) {
66 CallInst *CI = dyn_cast<CallInst>(I);
67 return (isMallocCall(CI)) ? CI : NULL;
68}
69
70static bool isBitCastOfMallocCall(const BitCastInst* BCI) {
71 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.
79CallInst* llvm::extractMallocCallFromBitCast(Value* I) {
80 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
85const CallInst* llvm::extractMallocCallFromBitCast(const Value* I) {
86 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 Hernandez90f48e72009-10-28 20:18:55 +000091/// isConstantOne - Return true only if val is constant int 1.
92static bool isConstantOne(Value *val) {
93 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
94}
95
96static Value* isArrayMallocHelper(const CallInst *CI, LLVMContext &Context,
97 const TargetData* TD) {
Evan Chengfabcb912009-09-10 04:36:43 +000098 if (!CI)
Victor Hernandez90f48e72009-10-28 20:18:55 +000099 return NULL;
Evan Chengfabcb912009-09-10 04:36:43 +0000100
Victor Hernandez90f48e72009-10-28 20:18:55 +0000101 // Type must be known to determine array size.
Evan Chengfabcb912009-09-10 04:36:43 +0000102 const Type* T = getMallocAllocatedType(CI);
Victor Hernandez90f48e72009-10-28 20:18:55 +0000103 if (!T)
104 return NULL;
Victor Hernandez88d98392009-09-18 19:20:02 +0000105
106 Value* MallocArg = CI->getOperand(1);
Victor Hernandez90f48e72009-10-28 20:18:55 +0000107 ConstantExpr* CO = dyn_cast<ConstantExpr>(MallocArg);
108 BinaryOperator* BO = dyn_cast<BinaryOperator>(MallocArg);
109
110 Constant* ElementSize = ConstantExpr::getSizeOf(T);
Victor Hernandez88d98392009-09-18 19:20:02 +0000111 ElementSize = ConstantExpr::getTruncOrBitCast(ElementSize,
112 MallocArg->getType());
Victor Hernandez90f48e72009-10-28 20:18:55 +0000113 Constant *FoldedElementSize =
114 ConstantFoldConstantExpression(cast<ConstantExpr>(ElementSize), Context, TD);
Victor Hernandez88d98392009-09-18 19:20:02 +0000115
Victor Hernandez90f48e72009-10-28 20:18:55 +0000116 // First, check if CI is a non-array malloc.
117 if (CO && ((CO == ElementSize) ||
118 (FoldedElementSize && (CO == FoldedElementSize))))
119 // Match CreateMalloc's use of constant 1 array-size for non-array mallocs.
120 return ConstantInt::get(MallocArg->getType(), 1);
Victor Hernandez88d98392009-09-18 19:20:02 +0000121
Victor Hernandez90f48e72009-10-28 20:18:55 +0000122 // Second, check if CI is an array malloc whose array size can be determined.
123 if (isConstantOne(ElementSize) ||
124 (FoldedElementSize && isConstantOne(FoldedElementSize)))
125 return MallocArg;
Evan Chengfabcb912009-09-10 04:36:43 +0000126
Victor Hernandez90f48e72009-10-28 20:18:55 +0000127 if (!CO && !BO)
128 return NULL;
Evan Chengfabcb912009-09-10 04:36:43 +0000129
Victor Hernandez90f48e72009-10-28 20:18:55 +0000130 Value* Op0 = NULL;
131 Value* Op1 = NULL;
132 unsigned Opcode = 0;
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();
138 }
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();
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) {
149 if ((Op1 == ElementSize) ||
150 (FoldedElementSize && (Op1 == FoldedElementSize)))
151 // ArraySize * ElementSize
152 return Op0;
153 if ((Op0 == ElementSize) ||
154 (FoldedElementSize && (Op0 == FoldedElementSize)))
155 // ElementSize * ArraySize
156 return Op1;
157 }
158 if (Opcode == Instruction::Shl) {
159 ConstantInt* Op1Int = dyn_cast<ConstantInt>(Op1);
160 if (!Op1Int) return NULL;
161 Value* Op1Pow = ConstantInt::get(Op1->getType(),
162 pow(2, Op1Int->getZExtValue()));
163 if (Op0 == ElementSize || (FoldedElementSize && Op0 == FoldedElementSize))
164 // ArraySize << log2(ElementSize)
165 return Op1Pow;
166 if (Op1Pow == ElementSize ||
167 (FoldedElementSize && Op1Pow == FoldedElementSize))
168 // 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.
Victor Hernandez88d98392009-09-18 19:20:02 +0000180CallInst* llvm::isArrayMalloc(Value* I, LLVMContext &Context,
181 const TargetData* TD) {
Evan Chengfabcb912009-09-10 04:36:43 +0000182 CallInst *CI = extractMallocCall(I);
Victor Hernandez90f48e72009-10-28 20:18:55 +0000183 Value* ArraySize = isArrayMallocHelper(CI, Context, TD);
184
185 if (ArraySize &&
186 ArraySize != ConstantInt::get(CI->getOperand(1)->getType(), 1))
187 return CI;
188
189 // CI is a non-array malloc or we can't figure out that it is an array malloc.
190 return NULL;
Evan Chengfabcb912009-09-10 04:36:43 +0000191}
192
Victor Hernandez88d98392009-09-18 19:20:02 +0000193const CallInst* llvm::isArrayMalloc(const Value* I, LLVMContext &Context,
194 const TargetData* TD) {
Evan Chengfabcb912009-09-10 04:36:43 +0000195 const CallInst *CI = extractMallocCall(I);
Victor Hernandez90f48e72009-10-28 20:18:55 +0000196 Value* ArraySize = isArrayMallocHelper(CI, Context, TD);
197
198 if (ArraySize &&
199 ArraySize != ConstantInt::get(CI->getOperand(1)->getType(), 1))
200 return CI;
201
202 // CI is a non-array malloc or we can't figure out that it is an array malloc.
203 return NULL;
Evan Chengfabcb912009-09-10 04:36:43 +0000204}
205
206/// getMallocType - Returns the PointerType resulting from the malloc call.
207/// This PointerType is the result type of the call's only bitcast use.
208/// If there is no unique bitcast use, then return NULL.
209const PointerType* llvm::getMallocType(const CallInst* CI) {
210 assert(isMalloc(CI) && "GetMallocType and not malloc call");
211
212 const BitCastInst* BCI = NULL;
Victor Hernandez88d98392009-09-18 19:20:02 +0000213
214 // Determine if CallInst has a bitcast use.
215 for (Value::use_const_iterator UI = CI->use_begin(), E = CI->use_end();
216 UI != E; )
217 if ((BCI = dyn_cast<BitCastInst>(cast<Instruction>(*UI++))))
218 break;
Evan Chengfabcb912009-09-10 04:36:43 +0000219
Victor Hernandez88d98392009-09-18 19:20:02 +0000220 // Malloc call has 1 bitcast use and no other uses, so type is the bitcast's
221 // destination type.
222 if (BCI && CI->hasOneUse())
223 return cast<PointerType>(BCI->getDestTy());
Evan Chengfabcb912009-09-10 04:36:43 +0000224
Victor Hernandez60cfc032009-09-22 18:50:03 +0000225 // Malloc call was not bitcast, so type is the malloc function's return type.
Victor Hernandez88d98392009-09-18 19:20:02 +0000226 if (!BCI)
227 return cast<PointerType>(CI->getType());
228
229 // Type could not be determined.
230 return NULL;
Evan Chengfabcb912009-09-10 04:36:43 +0000231}
232
233/// getMallocAllocatedType - Returns the Type allocated by malloc call. This
234/// Type is the result type of the call's only bitcast use. If there is no
235/// unique bitcast use, then return NULL.
236const Type* llvm::getMallocAllocatedType(const CallInst* CI) {
237 const PointerType* PT = getMallocType(CI);
238 return PT ? PT->getElementType() : NULL;
239}
240
Victor Hernandez90f48e72009-10-28 20:18:55 +0000241/// getMallocArraySize - Returns the array size of a malloc call. If the
242/// argument passed to malloc is a multiple of the size of the malloced type,
243/// then return that multiple. For non-array mallocs, the multiple is
244/// constant 1. Otherwise, return NULL for mallocs whose array size cannot be
Victor Hernandez2491ce02009-10-15 20:14:52 +0000245/// determined.
Victor Hernandez88d98392009-09-18 19:20:02 +0000246Value* llvm::getMallocArraySize(CallInst* CI, LLVMContext &Context,
247 const TargetData* TD) {
Victor Hernandez90f48e72009-10-28 20:18:55 +0000248 return isArrayMallocHelper(CI, Context, TD);
Evan Chengfabcb912009-09-10 04:36:43 +0000249}
Victor Hernandez66284e02009-10-24 04:23:03 +0000250
Victor Hernandez046e78c2009-10-26 23:43:48 +0000251//===----------------------------------------------------------------------===//
252// free Call Utility Functions.
253//
254
Victor Hernandez66284e02009-10-24 04:23:03 +0000255/// isFreeCall - Returns true if the the value is a call to the builtin free()
256bool llvm::isFreeCall(const Value* I) {
257 const CallInst *CI = dyn_cast<CallInst>(I);
258 if (!CI)
259 return false;
260
261 const Module* M = CI->getParent()->getParent()->getParent();
262 Function *FreeFunc = M->getFunction("free");
263
264 if (CI->getOperand(0) != FreeFunc)
265 return false;
266
267 // Check free prototype.
268 // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin
269 // attribute will exist.
270 const FunctionType *FTy = FreeFunc->getFunctionType();
271 if (FTy->getReturnType() != Type::getVoidTy(M->getContext()))
272 return false;
273 if (FTy->getNumParams() != 1)
274 return false;
275 if (FTy->param_begin()->get() != Type::getInt8PtrTy(M->getContext()))
276 return false;
277
278 return true;
279}