No need to verify that malloc's return type is i8*.
[oota-llvm.git] / lib / Analysis / MallocHelper.cpp
1 //===-- 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"
19 #include "llvm/Analysis/ConstantFolding.h"
20 using 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.
28 bool llvm::isMalloc(const Value* I) {
29   return extractMallocCall(I) || extractMallocCallFromBitCast(I);
30 }
31
32 static 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.
48 const CallInst* llvm::extractMallocCall(const Value* I) {
49   const CallInst *CI = dyn_cast<CallInst>(I);
50   return (isMallocCall(CI)) ? CI : NULL;
51 }
52
53 CallInst* llvm::extractMallocCall(Value* I) {
54   CallInst *CI = dyn_cast<CallInst>(I);
55   return (isMallocCall(CI)) ? CI : NULL;
56 }
57
58 static 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.
67 CallInst* llvm::extractMallocCallFromBitCast(Value* I) {
68   BitCastInst *BCI = dyn_cast<BitCastInst>(I);
69   return (isBitCastOfMallocCall(BCI)) ? cast<CallInst>(BCI->getOperand(0))
70                                       : NULL;
71 }
72
73 const CallInst* llvm::extractMallocCallFromBitCast(const Value* I) {
74   const BitCastInst *BCI = dyn_cast<BitCastInst>(I);
75   return (isBitCastOfMallocCall(BCI)) ? cast<CallInst>(BCI->getOperand(0))
76                                       : NULL;
77 }
78
79 static bool isArrayMallocHelper(const CallInst *CI, LLVMContext &Context,
80                                 const TargetData* TD) {
81   if (!CI)
82     return false;
83
84   const Type* T = getMallocAllocatedType(CI);
85
86   // We can only indentify an array malloc if we know the type of the malloc 
87   // call.
88   if (!T) return false;
89
90   Value* MallocArg = CI->getOperand(1);
91   Constant *ElementSize = ConstantExpr::getSizeOf(T);
92   ElementSize = ConstantExpr::getTruncOrBitCast(ElementSize, 
93                                                 MallocArg->getType());
94   Constant *FoldedElementSize = ConstantFoldConstantExpression(
95                                        cast<ConstantExpr>(ElementSize), 
96                                        Context, TD);
97
98
99   if (isa<ConstantExpr>(MallocArg))
100     return (MallocArg != ElementSize);
101
102   BinaryOperator *BI = dyn_cast<BinaryOperator>(MallocArg);
103   if (!BI)
104     return false;
105
106   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;
115 }
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.
127 CallInst* llvm::isArrayMalloc(Value* I, LLVMContext &Context,
128                               const TargetData* TD) {
129   CallInst *CI = extractMallocCall(I);
130   return (isArrayMallocHelper(CI, Context, TD)) ? CI : NULL;
131 }
132
133 const CallInst* llvm::isArrayMalloc(const Value* I, LLVMContext &Context,
134                                     const TargetData* TD) {
135   const CallInst *CI = extractMallocCall(I);
136   return (isArrayMallocHelper(CI, Context, TD)) ? CI : NULL;
137 }
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.
142 const PointerType* llvm::getMallocType(const CallInst* CI) {
143   assert(isMalloc(CI) && "GetMallocType and not malloc call");
144   
145   const BitCastInst* BCI = NULL;
146   
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;
152
153   // 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());
157
158   // Malloc call was not bitcast, so type is the malloc function's return type.
159   if (!BCI)
160     return cast<PointerType>(CI->getType());
161
162   // Type could not be determined.
163   return NULL;
164 }
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.
169 const 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.
175 static 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.
191 Value* llvm::getMallocArraySize(CallInst* CI, LLVMContext &Context,
192                                 const TargetData* TD) {
193   // Match CreateMalloc's use of constant 1 array-size for non-array mallocs.
194   if (!isArrayMalloc(CI, Context, TD))
195     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));
200   ElementSize = ConstantExpr::getTruncOrBitCast(ElementSize, 
201                                                 MallocArg->getType());
202
203   Constant* CO = dyn_cast<Constant>(MallocArg);
204   BinaryOperator* BO = dyn_cast<BinaryOperator>(MallocArg);
205   assert((isConstantOne(ElementSize) || CO || BO) &&
206          "getMallocArraySize and malformed malloc IR");
207       
208   if (isConstantOne(ElementSize))
209     return MallocArg;
210     
211   if (CO)
212     return CO->getOperand(0);
213     
214   // TODO: Detect case where MallocArg mul has been transformed to shl.
215
216   assert(BO && "getMallocArraySize not constant but not multiplication either");
217   return BO->getOperand(0);
218 }