blob: 9c1671d306416b0f40571a0102394a959b18c6dd [file] [log] [blame]
Evan Chengfabcb912009-09-10 04:36:43 +00001//===-- MallocHelper.cpp - Functions to identify malloc calls -------------===//
2//
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//
10// This family of functions identifies calls to malloc, bitcasts of malloc
11// calls, and the types and array sizes associated with them.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Analysis/MallocHelper.h"
16#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();
37 Constant *MallocFunc = M->getFunction("malloc");
38
39 if (CI->getOperand(0) != MallocFunc)
40 return false;
41
42 return true;
43}
44
45/// extractMallocCall - Returns the corresponding CallInst if the instruction
46/// is a malloc call. Since CallInst::CreateMalloc() only creates calls, we
47/// ignore InvokeInst here.
48const CallInst* llvm::extractMallocCall(const Value* I) {
49 const CallInst *CI = dyn_cast<CallInst>(I);
50 return (isMallocCall(CI)) ? CI : NULL;
51}
52
53CallInst* llvm::extractMallocCall(Value* I) {
54 CallInst *CI = dyn_cast<CallInst>(I);
55 return (isMallocCall(CI)) ? CI : NULL;
56}
57
58static bool isBitCastOfMallocCall(const BitCastInst* BCI) {
59 if (!BCI)
60 return false;
61
62 return isMallocCall(dyn_cast<CallInst>(BCI->getOperand(0)));
63}
64
65/// extractMallocCallFromBitCast - Returns the corresponding CallInst if the
66/// instruction is a bitcast of the result of a malloc call.
67CallInst* llvm::extractMallocCallFromBitCast(Value* I) {
68 BitCastInst *BCI = dyn_cast<BitCastInst>(I);
Victor Hernandez399e45b2009-09-10 20:18:57 +000069 return (isBitCastOfMallocCall(BCI)) ? cast<CallInst>(BCI->getOperand(0))
70 : NULL;
Evan Chengfabcb912009-09-10 04:36:43 +000071}
72
73const CallInst* llvm::extractMallocCallFromBitCast(const Value* I) {
74 const BitCastInst *BCI = dyn_cast<BitCastInst>(I);
Victor Hernandez399e45b2009-09-10 20:18:57 +000075 return (isBitCastOfMallocCall(BCI)) ? cast<CallInst>(BCI->getOperand(0))
76 : NULL;
Evan Chengfabcb912009-09-10 04:36:43 +000077}
78
Victor Hernandez88d98392009-09-18 19:20:02 +000079static bool isArrayMallocHelper(const CallInst *CI, LLVMContext &Context,
80 const TargetData* TD) {
Evan Chengfabcb912009-09-10 04:36:43 +000081 if (!CI)
82 return false;
83
Evan Chengfabcb912009-09-10 04:36:43 +000084 const Type* T = getMallocAllocatedType(CI);
Victor Hernandez88d98392009-09-18 19:20:02 +000085
86 // We can only indentify an array malloc if we know the type of the malloc
87 // call.
Evan Chengfabcb912009-09-10 04:36:43 +000088 if (!T) return false;
Victor Hernandez88d98392009-09-18 19:20:02 +000089
90 Value* MallocArg = CI->getOperand(1);
Evan Chengfabcb912009-09-10 04:36:43 +000091 Constant *ElementSize = ConstantExpr::getSizeOf(T);
Victor Hernandez88d98392009-09-18 19:20:02 +000092 ElementSize = ConstantExpr::getTruncOrBitCast(ElementSize,
93 MallocArg->getType());
94 Constant *FoldedElementSize = ConstantFoldConstantExpression(
95 cast<ConstantExpr>(ElementSize),
96 Context, TD);
97
98
Evan Chengfabcb912009-09-10 04:36:43 +000099 if (isa<ConstantExpr>(MallocArg))
Victor Hernandez88d98392009-09-18 19:20:02 +0000100 return (MallocArg != ElementSize);
Evan Chengfabcb912009-09-10 04:36:43 +0000101
102 BinaryOperator *BI = dyn_cast<BinaryOperator>(MallocArg);
103 if (!BI)
104 return false;
105
Victor Hernandez88d98392009-09-18 19:20:02 +0000106 if (BI->getOpcode() == Instruction::Mul)
107 // ArraySize * ElementSize
108 if (BI->getOperand(1) == ElementSize ||
109 (FoldedElementSize && BI->getOperand(1) == FoldedElementSize))
110 return true;
111
112 // TODO: Detect case where MallocArg mul has been transformed to shl.
113
114 return false;
Evan Chengfabcb912009-09-10 04:36:43 +0000115}
116
117/// isArrayMalloc - Returns the corresponding CallInst if the instruction
118/// matches the malloc call IR generated by CallInst::CreateMalloc(). This
119/// means that it is a malloc call with one bitcast use AND the malloc call's
120/// size argument is:
121/// 1. a constant not equal to the malloc's allocated type
122/// or
123/// 2. the result of a multiplication by the malloc's allocated type
124/// Otherwise it returns NULL.
125/// The unique bitcast is needed to determine the type/size of the array
126/// allocation.
Victor Hernandez88d98392009-09-18 19:20:02 +0000127CallInst* llvm::isArrayMalloc(Value* I, LLVMContext &Context,
128 const TargetData* TD) {
Evan Chengfabcb912009-09-10 04:36:43 +0000129 CallInst *CI = extractMallocCall(I);
Victor Hernandez88d98392009-09-18 19:20:02 +0000130 return (isArrayMallocHelper(CI, Context, TD)) ? CI : NULL;
Evan Chengfabcb912009-09-10 04:36:43 +0000131}
132
Victor Hernandez88d98392009-09-18 19:20:02 +0000133const CallInst* llvm::isArrayMalloc(const Value* I, LLVMContext &Context,
134 const TargetData* TD) {
Evan Chengfabcb912009-09-10 04:36:43 +0000135 const CallInst *CI = extractMallocCall(I);
Victor Hernandez88d98392009-09-18 19:20:02 +0000136 return (isArrayMallocHelper(CI, Context, TD)) ? CI : NULL;
Evan Chengfabcb912009-09-10 04:36:43 +0000137}
138
139/// getMallocType - Returns the PointerType resulting from the malloc call.
140/// This PointerType is the result type of the call's only bitcast use.
141/// If there is no unique bitcast use, then return NULL.
142const PointerType* llvm::getMallocType(const CallInst* CI) {
143 assert(isMalloc(CI) && "GetMallocType and not malloc call");
144
145 const BitCastInst* BCI = NULL;
Victor Hernandez88d98392009-09-18 19:20:02 +0000146
147 // Determine if CallInst has a bitcast use.
148 for (Value::use_const_iterator UI = CI->use_begin(), E = CI->use_end();
149 UI != E; )
150 if ((BCI = dyn_cast<BitCastInst>(cast<Instruction>(*UI++))))
151 break;
Evan Chengfabcb912009-09-10 04:36:43 +0000152
Victor Hernandez88d98392009-09-18 19:20:02 +0000153 // Malloc call has 1 bitcast use and no other uses, so type is the bitcast's
154 // destination type.
155 if (BCI && CI->hasOneUse())
156 return cast<PointerType>(BCI->getDestTy());
Evan Chengfabcb912009-09-10 04:36:43 +0000157
Victor Hernandez88d98392009-09-18 19:20:02 +0000158 // Malloc call was not bitcast, so the type is the malloc's return type, i8*.
159 if (!BCI)
160 return cast<PointerType>(CI->getType());
161
162 // Type could not be determined.
163 return NULL;
Evan Chengfabcb912009-09-10 04:36:43 +0000164}
165
166/// getMallocAllocatedType - Returns the Type allocated by malloc call. This
167/// Type is the result type of the call's only bitcast use. If there is no
168/// unique bitcast use, then return NULL.
169const Type* llvm::getMallocAllocatedType(const CallInst* CI) {
170 const PointerType* PT = getMallocType(CI);
171 return PT ? PT->getElementType() : NULL;
172}
173
174/// isConstantOne - Return true only if val is constant int 1.
175static bool isConstantOne(Value *val) {
176 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
177}
178
179/// getMallocArraySize - Returns the array size of a malloc call. The array
180/// size is computated in 1 of 3 ways:
181/// 1. If the element type if of size 1, then array size is the argument to
182/// malloc.
183/// 2. Else if the malloc's argument is a constant, the array size is that
184/// argument divided by the element type's size.
185/// 3. Else the malloc argument must be a multiplication and the array size is
186/// the first operand of the multiplication.
187/// This function returns constant 1 if:
188/// 1. The malloc call's allocated type cannot be determined.
189/// 2. IR wasn't created by a call to CallInst::CreateMalloc() with a non-NULL
190/// ArraySize.
Victor Hernandez88d98392009-09-18 19:20:02 +0000191Value* llvm::getMallocArraySize(CallInst* CI, LLVMContext &Context,
192 const TargetData* TD) {
Evan Chengfabcb912009-09-10 04:36:43 +0000193 // Match CreateMalloc's use of constant 1 array-size for non-array mallocs.
Victor Hernandez88d98392009-09-18 19:20:02 +0000194 if (!isArrayMalloc(CI, Context, TD))
Evan Chengfabcb912009-09-10 04:36:43 +0000195 return ConstantInt::get(CI->getOperand(1)->getType(), 1);
196
197 Value* MallocArg = CI->getOperand(1);
198 assert(getMallocAllocatedType(CI) && "getMallocArraySize and no type");
199 Constant *ElementSize = ConstantExpr::getSizeOf(getMallocAllocatedType(CI));
Victor Hernandez88d98392009-09-18 19:20:02 +0000200 ElementSize = ConstantExpr::getTruncOrBitCast(ElementSize,
Evan Chengfabcb912009-09-10 04:36:43 +0000201 MallocArg->getType());
202
203 Constant* CO = dyn_cast<Constant>(MallocArg);
204 BinaryOperator* BO = dyn_cast<BinaryOperator>(MallocArg);
Benjamin Kramerb84c5ae2009-09-10 11:31:39 +0000205 assert((isConstantOne(ElementSize) || CO || BO) &&
Evan Chengfabcb912009-09-10 04:36:43 +0000206 "getMallocArraySize and malformed malloc IR");
207
208 if (isConstantOne(ElementSize))
209 return MallocArg;
Victor Hernandez88d98392009-09-18 19:20:02 +0000210
211 if (CO)
212 return CO->getOperand(0);
213
214 // TODO: Detect case where MallocArg mul has been transformed to shl.
Evan Chengfabcb912009-09-10 04:36:43 +0000215
Evan Chengfabcb912009-09-10 04:36:43 +0000216 assert(BO && "getMallocArraySize not constant but not multiplication either");
217 return BO->getOperand(0);
218}