MemoryBuiltins.cpp revision 206083
1217309Snwhitehorn//===------ MemoryBuiltins.cpp - Identify calls to memory builtins --------===//
2217309Snwhitehorn//
3217309Snwhitehorn//                     The LLVM Compiler Infrastructure
4217309Snwhitehorn//
5217309Snwhitehorn// This file is distributed under the University of Illinois Open Source
6217309Snwhitehorn// License. See LICENSE.TXT for details.
7217309Snwhitehorn//
8217309Snwhitehorn//===----------------------------------------------------------------------===//
9217309Snwhitehorn//
10217309Snwhitehorn// This family of functions identifies calls to builtin functions that allocate
11217309Snwhitehorn// or free memory.
12217309Snwhitehorn//
13217309Snwhitehorn//===----------------------------------------------------------------------===//
14217309Snwhitehorn
15217309Snwhitehorn#include "llvm/Analysis/MemoryBuiltins.h"
16217309Snwhitehorn#include "llvm/Constants.h"
17217309Snwhitehorn#include "llvm/Instructions.h"
18217309Snwhitehorn#include "llvm/Module.h"
19217309Snwhitehorn#include "llvm/Analysis/ValueTracking.h"
20217309Snwhitehorn#include "llvm/Target/TargetData.h"
21217309Snwhitehornusing namespace llvm;
22217309Snwhitehorn
23217309Snwhitehorn//===----------------------------------------------------------------------===//
24217309Snwhitehorn//  malloc Call Utility Functions.
25217309Snwhitehorn//
26217309Snwhitehorn
27217309Snwhitehorn/// isMalloc - Returns true if the value is either a malloc call or a
28217309Snwhitehorn/// bitcast of the result of a malloc call.
29217309Snwhitehornbool llvm::isMalloc(const Value *I) {
30217309Snwhitehorn  return extractMallocCall(I) || extractMallocCallFromBitCast(I);
31217309Snwhitehorn}
32217309Snwhitehorn
33217309Snwhitehornstatic bool isMallocCall(const CallInst *CI) {
34217309Snwhitehorn  if (!CI)
35217309Snwhitehorn    return false;
36217309Snwhitehorn
37217309Snwhitehorn  Function *Callee = CI->getCalledFunction();
38217309Snwhitehorn  if (Callee == 0 || !Callee->isDeclaration() || Callee->getName() != "malloc")
39217309Snwhitehorn    return false;
40217309Snwhitehorn
41217309Snwhitehorn  // Check malloc prototype.
42217309Snwhitehorn  // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin
43217309Snwhitehorn  // attribute will exist.
44217309Snwhitehorn  const FunctionType *FTy = Callee->getFunctionType();
45217309Snwhitehorn  if (FTy->getNumParams() != 1)
46217309Snwhitehorn    return false;
47217309Snwhitehorn  if (IntegerType *ITy = dyn_cast<IntegerType>(FTy->param_begin()->get())) {
48217309Snwhitehorn    if (ITy->getBitWidth() != 32 && ITy->getBitWidth() != 64)
49217309Snwhitehorn      return false;
50217309Snwhitehorn    return true;
51217309Snwhitehorn  }
52217309Snwhitehorn
53217309Snwhitehorn  return false;
54217309Snwhitehorn}
55217309Snwhitehorn
56217309Snwhitehorn/// extractMallocCall - Returns the corresponding CallInst if the instruction
57217309Snwhitehorn/// is a malloc call.  Since CallInst::CreateMalloc() only creates calls, we
58217309Snwhitehorn/// ignore InvokeInst here.
59217309Snwhitehornconst CallInst *llvm::extractMallocCall(const Value *I) {
60217309Snwhitehorn  const CallInst *CI = dyn_cast<CallInst>(I);
61217309Snwhitehorn  return (isMallocCall(CI)) ? CI : NULL;
62217309Snwhitehorn}
63217309Snwhitehorn
64217309SnwhitehornCallInst *llvm::extractMallocCall(Value *I) {
65217309Snwhitehorn  CallInst *CI = dyn_cast<CallInst>(I);
66217309Snwhitehorn  return (isMallocCall(CI)) ? CI : NULL;
67217309Snwhitehorn}
68217309Snwhitehorn
69217309Snwhitehornstatic bool isBitCastOfMallocCall(const BitCastInst *BCI) {
70217309Snwhitehorn  if (!BCI)
71217309Snwhitehorn    return false;
72217309Snwhitehorn
73217309Snwhitehorn  return isMallocCall(dyn_cast<CallInst>(BCI->getOperand(0)));
74217309Snwhitehorn}
75217309Snwhitehorn
76217309Snwhitehorn/// extractMallocCallFromBitCast - Returns the corresponding CallInst if the
77217309Snwhitehorn/// instruction is a bitcast of the result of a malloc call.
78217309SnwhitehornCallInst *llvm::extractMallocCallFromBitCast(Value *I) {
79217309Snwhitehorn  BitCastInst *BCI = dyn_cast<BitCastInst>(I);
80217309Snwhitehorn  return (isBitCastOfMallocCall(BCI)) ? cast<CallInst>(BCI->getOperand(0))
81217309Snwhitehorn                                      : NULL;
82217309Snwhitehorn}
83217309Snwhitehorn
84217309Snwhitehornconst CallInst *llvm::extractMallocCallFromBitCast(const Value *I) {
85217309Snwhitehorn  const BitCastInst *BCI = dyn_cast<BitCastInst>(I);
86217309Snwhitehorn  return (isBitCastOfMallocCall(BCI)) ? cast<CallInst>(BCI->getOperand(0))
87217309Snwhitehorn                                      : NULL;
88217309Snwhitehorn}
89217309Snwhitehorn
90217309Snwhitehornstatic Value *computeArraySize(const CallInst *CI, const TargetData *TD,
91217309Snwhitehorn                               bool LookThroughSExt = false) {
92217309Snwhitehorn  if (!CI)
93217309Snwhitehorn    return NULL;
94217309Snwhitehorn
95217309Snwhitehorn  // The size of the malloc's result type must be known to determine array size.
96217309Snwhitehorn  const Type *T = getMallocAllocatedType(CI);
97217309Snwhitehorn  if (!T || !T->isSized() || !TD)
98217309Snwhitehorn    return NULL;
99217309Snwhitehorn
100217309Snwhitehorn  unsigned ElementSize = TD->getTypeAllocSize(T);
101217309Snwhitehorn  if (const StructType *ST = dyn_cast<StructType>(T))
102217309Snwhitehorn    ElementSize = TD->getStructLayout(ST)->getSizeInBytes();
103217309Snwhitehorn
104217309Snwhitehorn  // If malloc calls' arg can be determined to be a multiple of ElementSize,
105217309Snwhitehorn  // return the multiple.  Otherwise, return NULL.
106217309Snwhitehorn  Value *MallocArg = CI->getOperand(1);
107217309Snwhitehorn  Value *Multiple = NULL;
108217309Snwhitehorn  if (ComputeMultiple(MallocArg, ElementSize, Multiple,
109217309Snwhitehorn                      LookThroughSExt))
110217309Snwhitehorn    return Multiple;
111217309Snwhitehorn
112217309Snwhitehorn  return NULL;
113217309Snwhitehorn}
114217309Snwhitehorn
115217309Snwhitehorn/// isArrayMalloc - Returns the corresponding CallInst if the instruction
116217309Snwhitehorn/// is a call to malloc whose array size can be determined and the array size
117217309Snwhitehorn/// is not constant 1.  Otherwise, return NULL.
118217309Snwhitehornconst CallInst *llvm::isArrayMalloc(const Value *I, const TargetData *TD) {
119217309Snwhitehorn  const CallInst *CI = extractMallocCall(I);
120217309Snwhitehorn  Value *ArraySize = computeArraySize(CI, TD);
121217309Snwhitehorn
122217309Snwhitehorn  if (ArraySize &&
123217309Snwhitehorn      ArraySize != ConstantInt::get(CI->getOperand(1)->getType(), 1))
124217309Snwhitehorn    return CI;
125217309Snwhitehorn
126217309Snwhitehorn  // CI is a non-array malloc or we can't figure out that it is an array malloc.
127217309Snwhitehorn  return NULL;
128217309Snwhitehorn}
129217309Snwhitehorn
130217309Snwhitehorn/// getMallocType - Returns the PointerType resulting from the malloc call.
131217309Snwhitehorn/// The PointerType depends on the number of bitcast uses of the malloc call:
132217309Snwhitehorn///   0: PointerType is the calls' return type.
133217309Snwhitehorn///   1: PointerType is the bitcast's result type.
134217309Snwhitehorn///  >1: Unique PointerType cannot be determined, return NULL.
135217309Snwhitehornconst PointerType *llvm::getMallocType(const CallInst *CI) {
136217309Snwhitehorn  assert(isMalloc(CI) && "getMallocType and not malloc call");
137217309Snwhitehorn
138217309Snwhitehorn  const PointerType *MallocType = NULL;
139217309Snwhitehorn  unsigned NumOfBitCastUses = 0;
140217309Snwhitehorn
141217309Snwhitehorn  // Determine if CallInst has a bitcast use.
142217309Snwhitehorn  for (Value::const_use_iterator UI = CI->use_begin(), E = CI->use_end();
143217309Snwhitehorn       UI != E; )
144217309Snwhitehorn    if (const BitCastInst *BCI = dyn_cast<BitCastInst>(*UI++)) {
145217309Snwhitehorn      MallocType = cast<PointerType>(BCI->getDestTy());
146217309Snwhitehorn      NumOfBitCastUses++;
147217309Snwhitehorn    }
148217309Snwhitehorn
149217309Snwhitehorn  // Malloc call has 1 bitcast use, so type is the bitcast's destination type.
150217309Snwhitehorn  if (NumOfBitCastUses == 1)
151217309Snwhitehorn    return MallocType;
152217309Snwhitehorn
153217309Snwhitehorn  // Malloc call was not bitcast, so type is the malloc function's return type.
154217309Snwhitehorn  if (NumOfBitCastUses == 0)
155217309Snwhitehorn    return cast<PointerType>(CI->getType());
156217309Snwhitehorn
157217309Snwhitehorn  // Type could not be determined.
158217309Snwhitehorn  return NULL;
159217309Snwhitehorn}
160217309Snwhitehorn
161217309Snwhitehorn/// getMallocAllocatedType - Returns the Type allocated by malloc call.
162217309Snwhitehorn/// The Type depends on the number of bitcast uses of the malloc call:
163217309Snwhitehorn///   0: PointerType is the malloc calls' return type.
164217309Snwhitehorn///   1: PointerType is the bitcast's result type.
165217309Snwhitehorn///  >1: Unique PointerType cannot be determined, return NULL.
166217309Snwhitehornconst Type *llvm::getMallocAllocatedType(const CallInst *CI) {
167217309Snwhitehorn  const PointerType *PT = getMallocType(CI);
168217309Snwhitehorn  return PT ? PT->getElementType() : NULL;
169217309Snwhitehorn}
170217309Snwhitehorn
171217309Snwhitehorn/// getMallocArraySize - Returns the array size of a malloc call.  If the
172217309Snwhitehorn/// argument passed to malloc is a multiple of the size of the malloced type,
173217309Snwhitehorn/// then return that multiple.  For non-array mallocs, the multiple is
174217309Snwhitehorn/// constant 1.  Otherwise, return NULL for mallocs whose array size cannot be
175217309Snwhitehorn/// determined.
176217309SnwhitehornValue *llvm::getMallocArraySize(CallInst *CI, const TargetData *TD,
177217309Snwhitehorn                                bool LookThroughSExt) {
178217309Snwhitehorn  assert(isMalloc(CI) && "getMallocArraySize and not malloc call");
179217309Snwhitehorn  return computeArraySize(CI, TD, LookThroughSExt);
180217309Snwhitehorn}
181217309Snwhitehorn
182217309Snwhitehorn//===----------------------------------------------------------------------===//
183217309Snwhitehorn//  free Call Utility Functions.
184217309Snwhitehorn//
185217309Snwhitehorn
186217309Snwhitehorn/// isFreeCall - Returns true if the value is a call to the builtin free()
187217309Snwhitehornbool llvm::isFreeCall(const Value *I) {
188217309Snwhitehorn  const CallInst *CI = dyn_cast<CallInst>(I);
189217309Snwhitehorn  if (!CI)
190217309Snwhitehorn    return false;
191217309Snwhitehorn  Function *Callee = CI->getCalledFunction();
192217309Snwhitehorn  if (Callee == 0 || !Callee->isDeclaration() || Callee->getName() != "free")
193217309Snwhitehorn    return false;
194217309Snwhitehorn
195217309Snwhitehorn  // Check free prototype.
196217309Snwhitehorn  // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin
197217309Snwhitehorn  // attribute will exist.
198217309Snwhitehorn  const FunctionType *FTy = Callee->getFunctionType();
199217309Snwhitehorn  if (!FTy->getReturnType()->isVoidTy())
200217309Snwhitehorn    return false;
201217309Snwhitehorn  if (FTy->getNumParams() != 1)
202217309Snwhitehorn    return false;
203217309Snwhitehorn  if (FTy->param_begin()->get() != Type::getInt8PtrTy(Callee->getContext()))
204217309Snwhitehorn    return false;
205217309Snwhitehorn
206217309Snwhitehorn  return true;
207217309Snwhitehorn}
208217309Snwhitehorn