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/ValueTracking.h"
20 #include "llvm/Target/TargetData.h"
23 //===----------------------------------------------------------------------===//
24 // malloc Call Utility Functions.
27 /// isMalloc - Returns true if 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 static Value
*computeArraySize(const CallInst
*CI
, const TargetData
*TD
,
91 bool LookThroughSExt
= false) {
95 // The size of the malloc's result type must be known to determine array size.
96 const Type
*T
= getMallocAllocatedType(CI
);
97 if (!T
|| !T
->isSized() || !TD
)
100 unsigned ElementSize
= TD
->getTypeAllocSize(T
);
101 if (const StructType
*ST
= dyn_cast
<StructType
>(T
))
102 ElementSize
= TD
->getStructLayout(ST
)->getSizeInBytes();
104 // If malloc call's arg can be determined to be a multiple of ElementSize,
105 // return the multiple. Otherwise, return NULL.
106 Value
*MallocArg
= CI
->getArgOperand(0);
107 Value
*Multiple
= NULL
;
108 if (ComputeMultiple(MallocArg
, ElementSize
, Multiple
,
115 /// isArrayMalloc - Returns the corresponding CallInst if the instruction
116 /// is a call to malloc whose array size can be determined and the array size
117 /// is not constant 1. Otherwise, return NULL.
118 const CallInst
*llvm::isArrayMalloc(const Value
*I
, const TargetData
*TD
) {
119 const CallInst
*CI
= extractMallocCall(I
);
120 Value
*ArraySize
= computeArraySize(CI
, TD
);
123 ArraySize
!= ConstantInt::get(CI
->getArgOperand(0)->getType(), 1))
126 // CI is a non-array malloc or we can't figure out that it is an array malloc.
130 /// getMallocType - Returns the PointerType resulting from the malloc call.
131 /// The PointerType depends on the number of bitcast uses of the malloc call:
132 /// 0: PointerType is the calls' return type.
133 /// 1: PointerType is the bitcast's result type.
134 /// >1: Unique PointerType cannot be determined, return NULL.
135 const PointerType
*llvm::getMallocType(const CallInst
*CI
) {
136 assert(isMalloc(CI
) && "getMallocType and not malloc call");
138 const PointerType
*MallocType
= NULL
;
139 unsigned NumOfBitCastUses
= 0;
141 // Determine if CallInst has a bitcast use.
142 for (Value::const_use_iterator UI
= CI
->use_begin(), E
= CI
->use_end();
144 if (const BitCastInst
*BCI
= dyn_cast
<BitCastInst
>(*UI
++)) {
145 MallocType
= cast
<PointerType
>(BCI
->getDestTy());
149 // Malloc call has 1 bitcast use, so type is the bitcast's destination type.
150 if (NumOfBitCastUses
== 1)
153 // Malloc call was not bitcast, so type is the malloc function's return type.
154 if (NumOfBitCastUses
== 0)
155 return cast
<PointerType
>(CI
->getType());
157 // Type could not be determined.
161 /// getMallocAllocatedType - Returns the Type allocated by malloc call.
162 /// The Type depends on the number of bitcast uses of the malloc call:
163 /// 0: PointerType is the malloc calls' return type.
164 /// 1: PointerType is the bitcast's result type.
165 /// >1: Unique PointerType cannot be determined, return NULL.
166 const Type
*llvm::getMallocAllocatedType(const CallInst
*CI
) {
167 const PointerType
*PT
= getMallocType(CI
);
168 return PT
? PT
->getElementType() : NULL
;
171 /// getMallocArraySize - Returns the array size of a malloc call. If the
172 /// argument passed to malloc is a multiple of the size of the malloced type,
173 /// then return that multiple. For non-array mallocs, the multiple is
174 /// constant 1. Otherwise, return NULL for mallocs whose array size cannot be
176 Value
*llvm::getMallocArraySize(CallInst
*CI
, const TargetData
*TD
,
177 bool LookThroughSExt
) {
178 assert(isMalloc(CI
) && "getMallocArraySize and not malloc call");
179 return computeArraySize(CI
, TD
, LookThroughSExt
);
182 //===----------------------------------------------------------------------===//
183 // free Call Utility Functions.
186 /// isFreeCall - Returns non-null if the value is a call to the builtin free()
187 const CallInst
*llvm::isFreeCall(const Value
*I
) {
188 const CallInst
*CI
= dyn_cast
<CallInst
>(I
);
191 Function
*Callee
= CI
->getCalledFunction();
192 if (Callee
== 0 || !Callee
->isDeclaration() || Callee
->getName() != "free")
195 // Check free prototype.
196 // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin
197 // attribute will exist.
198 const FunctionType
*FTy
= Callee
->getFunctionType();
199 if (!FTy
->getReturnType()->isVoidTy())
201 if (FTy
->getNumParams() != 1)
203 if (FTy
->param_begin()->get() != Type::getInt8PtrTy(Callee
->getContext()))