1 //===-- MallocHelper.cpp - Functions to identify malloc calls -------------===//
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 malloc, bitcasts of malloc
11 // calls, and the types and array sizes associated with them.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Analysis/MallocHelper.h"
16 #include "llvm/Constants.h"
17 #include "llvm/Instructions.h"
18 #include "llvm/Module.h"
21 //===----------------------------------------------------------------------===//
22 // malloc Call Utility Functions.
25 /// isMalloc - Returns true if the the value is either a malloc call or a
26 /// bitcast of the result of a malloc call.
27 bool llvm::isMalloc(Value* I) {
28 return extractMallocCall(I) || extractMallocCallFromBitCast(I);
31 bool llvm::isMalloc(const Value* I) {
32 return extractMallocCall(I) || extractMallocCallFromBitCast(I);
35 static bool isMallocCall(const CallInst *CI) {
39 const Module* M = CI->getParent()->getParent()->getParent();
40 Constant *MallocFunc = M->getFunction("malloc");
42 if (CI->getOperand(0) != MallocFunc)
48 /// extractMallocCall - Returns the corresponding CallInst if the instruction
49 /// is a malloc call. Since CallInst::CreateMalloc() only creates calls, we
50 /// ignore InvokeInst here.
51 const CallInst* llvm::extractMallocCall(const Value* I) {
52 const CallInst *CI = dyn_cast<CallInst>(I);
53 return (isMallocCall(CI)) ? CI : NULL;
56 CallInst* llvm::extractMallocCall(Value* I) {
57 CallInst *CI = dyn_cast<CallInst>(I);
58 return (isMallocCall(CI)) ? CI : NULL;
61 static bool isBitCastOfMallocCall(const BitCastInst* BCI) {
65 return isMallocCall(dyn_cast<CallInst>(BCI->getOperand(0)));
68 /// extractMallocCallFromBitCast - Returns the corresponding CallInst if the
69 /// instruction is a bitcast of the result of a malloc call.
70 CallInst* llvm::extractMallocCallFromBitCast(Value* I) {
71 BitCastInst *BCI = dyn_cast<BitCastInst>(I);
72 return (isBitCastOfMallocCall(BCI)) ? cast<CallInst>(BCI->getOperand(0)) : NULL;
75 const CallInst* llvm::extractMallocCallFromBitCast(const Value* I) {
76 const BitCastInst *BCI = dyn_cast<BitCastInst>(I);
77 return (isBitCastOfMallocCall(BCI)) ? cast<CallInst>(BCI->getOperand(0)) : NULL;
80 static bool isArrayMallocHelper(const CallInst *CI) {
84 // Only identify array mallocs for mallocs with 1 bitcast use. The unique
85 // bitcast is needed to determine the type/size of the array allocation.
86 if (!CI->hasOneUse()) return false;
88 for (Value::use_const_iterator UI = CI->use_begin(), E = CI->use_end();
90 if (!isa<BitCastInst>(cast<Instruction>(*UI++)))
94 Value* MallocArg = CI->getOperand(1);
96 const Type* T = getMallocAllocatedType(CI);
98 Constant *ElementSize = ConstantExpr::getSizeOf(T);
100 if (isa<ConstantExpr>(MallocArg))
101 return (MallocArg == ElementSize) ? false : true;
103 BinaryOperator *BI = dyn_cast<BinaryOperator>(MallocArg);
107 if (BI->getOpcode() != Instruction::Mul)
110 if (BI->getOperand(1) != ElementSize)
116 /// isArrayMalloc - Returns the corresponding CallInst if the instruction
117 /// matches the malloc call IR generated by CallInst::CreateMalloc(). This
118 /// means that it is a malloc call with one bitcast use AND the malloc call's
119 /// size argument is:
120 /// 1. a constant not equal to the malloc's allocated type
122 /// 2. the result of a multiplication by the malloc's allocated type
123 /// Otherwise it returns NULL.
124 /// The unique bitcast is needed to determine the type/size of the array
126 CallInst* llvm::isArrayMalloc(Value* I) {
127 CallInst *CI = extractMallocCall(I);
128 return (isArrayMallocHelper(CI)) ? CI : NULL;
131 const CallInst* llvm::isArrayMalloc(const Value* I) {
132 const CallInst *CI = extractMallocCall(I);
133 return (isArrayMallocHelper(CI)) ? CI : NULL;
136 /// getMallocType - Returns the PointerType resulting from the malloc call.
137 /// This PointerType is the result type of the call's only bitcast use.
138 /// If there is no unique bitcast use, then return NULL.
139 const PointerType* llvm::getMallocType(const CallInst* CI) {
140 assert(isMalloc(CI) && "GetMallocType and not malloc call");
142 const BitCastInst* BCI = NULL;
144 // Determine type only if there is only 1 bitcast use of CI.
146 for (Value::use_const_iterator UI = CI->use_begin(), E = CI->use_end();
148 BCI = dyn_cast<BitCastInst>(cast<Instruction>(*UI++));
150 return BCI ? reinterpret_cast<const PointerType*>(BCI->getDestTy()) : NULL;
153 /// getMallocAllocatedType - Returns the Type allocated by malloc call. This
154 /// Type is the result type of the call's only bitcast use. If there is no
155 /// unique bitcast use, then return NULL.
156 const Type* llvm::getMallocAllocatedType(const CallInst* CI) {
157 const PointerType* PT = getMallocType(CI);
158 return PT ? PT->getElementType() : NULL;
161 /// isConstantOne - Return true only if val is constant int 1.
162 static bool isConstantOne(Value *val) {
163 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
166 /// getMallocArraySize - Returns the array size of a malloc call. The array
167 /// size is computated in 1 of 3 ways:
168 /// 1. If the element type if of size 1, then array size is the argument to
170 /// 2. Else if the malloc's argument is a constant, the array size is that
171 /// argument divided by the element type's size.
172 /// 3. Else the malloc argument must be a multiplication and the array size is
173 /// the first operand of the multiplication.
174 /// This function returns constant 1 if:
175 /// 1. The malloc call's allocated type cannot be determined.
176 /// 2. IR wasn't created by a call to CallInst::CreateMalloc() with a non-NULL
178 Value* llvm::getMallocArraySize(CallInst* CI) {
179 // Match CreateMalloc's use of constant 1 array-size for non-array mallocs.
180 if (!isArrayMalloc(CI))
181 return ConstantInt::get(CI->getOperand(1)->getType(), 1);
183 Value* MallocArg = CI->getOperand(1);
184 assert(getMallocAllocatedType(CI) && "getMallocArraySize and no type");
185 Constant *ElementSize = ConstantExpr::getSizeOf(getMallocAllocatedType(CI));
186 ElementSize = ConstantExpr::getTruncOrBitCast(cast<Constant>(ElementSize),
187 MallocArg->getType());
189 Constant* CO = dyn_cast<Constant>(MallocArg);
190 BinaryOperator* BO = dyn_cast<BinaryOperator>(MallocArg);
191 assert(isConstantOne(ElementSize) || CO || BO &&
192 "getMallocArraySize and malformed malloc IR");
194 if (isConstantOne(ElementSize))
198 return ConstantExpr::getUDiv(CO, ElementSize);
200 assert(BO && "getMallocArraySize not constant but not multiplication either");
201 return BO->getOperand(0);