MemoryBuiltins.cpp revision 221345
1//===------ MemoryBuiltins.cpp - Identify calls to memory builtins --------===//
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 builtin functions that allocate
11// or free memory.
12//
13//===----------------------------------------------------------------------===//
14
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"
21using namespace llvm;
22
23//===----------------------------------------------------------------------===//
24//  malloc Call Utility Functions.
25//
26
27/// isMalloc - Returns true if the value is either a malloc call or a
28/// bitcast of the result of a malloc call.
29bool llvm::isMalloc(const Value *I) {
30  return extractMallocCall(I) || extractMallocCallFromBitCast(I);
31}
32
33static bool isMallocCall(const CallInst *CI) {
34  if (!CI)
35    return false;
36
37  Function *Callee = CI->getCalledFunction();
38  if (Callee == 0 || !Callee->isDeclaration())
39    return false;
40  if (Callee->getName() != "malloc" &&
41      Callee->getName() != "_Znwj" && // operator new(unsigned int)
42      Callee->getName() != "_Znwm" && // operator new(unsigned long)
43      Callee->getName() != "_Znaj" && // operator new[](unsigned int)
44      Callee->getName() != "_Znam")   // operator new[](unsigned long)
45    return false;
46
47  // Check malloc prototype.
48  // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin
49  // attribute will exist.
50  const FunctionType *FTy = Callee->getFunctionType();
51  if (FTy->getNumParams() != 1)
52    return false;
53  if (IntegerType *ITy = dyn_cast<IntegerType>(FTy->param_begin()->get())) {
54    if (ITy->getBitWidth() != 32 && ITy->getBitWidth() != 64)
55      return false;
56    return true;
57  }
58
59  return false;
60}
61
62/// extractMallocCall - Returns the corresponding CallInst if the instruction
63/// is a malloc call.  Since CallInst::CreateMalloc() only creates calls, we
64/// ignore InvokeInst here.
65const CallInst *llvm::extractMallocCall(const Value *I) {
66  const CallInst *CI = dyn_cast<CallInst>(I);
67  return (isMallocCall(CI)) ? CI : NULL;
68}
69
70CallInst *llvm::extractMallocCall(Value *I) {
71  CallInst *CI = dyn_cast<CallInst>(I);
72  return (isMallocCall(CI)) ? CI : NULL;
73}
74
75static bool isBitCastOfMallocCall(const BitCastInst *BCI) {
76  if (!BCI)
77    return false;
78
79  return isMallocCall(dyn_cast<CallInst>(BCI->getOperand(0)));
80}
81
82/// extractMallocCallFromBitCast - Returns the corresponding CallInst if the
83/// instruction is a bitcast of the result of a malloc call.
84CallInst *llvm::extractMallocCallFromBitCast(Value *I) {
85  BitCastInst *BCI = dyn_cast<BitCastInst>(I);
86  return (isBitCastOfMallocCall(BCI)) ? cast<CallInst>(BCI->getOperand(0))
87                                      : NULL;
88}
89
90const CallInst *llvm::extractMallocCallFromBitCast(const Value *I) {
91  const BitCastInst *BCI = dyn_cast<BitCastInst>(I);
92  return (isBitCastOfMallocCall(BCI)) ? cast<CallInst>(BCI->getOperand(0))
93                                      : NULL;
94}
95
96static Value *computeArraySize(const CallInst *CI, const TargetData *TD,
97                               bool LookThroughSExt = false) {
98  if (!CI)
99    return NULL;
100
101  // The size of the malloc's result type must be known to determine array size.
102  const Type *T = getMallocAllocatedType(CI);
103  if (!T || !T->isSized() || !TD)
104    return NULL;
105
106  unsigned ElementSize = TD->getTypeAllocSize(T);
107  if (const StructType *ST = dyn_cast<StructType>(T))
108    ElementSize = TD->getStructLayout(ST)->getSizeInBytes();
109
110  // If malloc call's arg can be determined to be a multiple of ElementSize,
111  // return the multiple.  Otherwise, return NULL.
112  Value *MallocArg = CI->getArgOperand(0);
113  Value *Multiple = NULL;
114  if (ComputeMultiple(MallocArg, ElementSize, Multiple,
115                      LookThroughSExt))
116    return Multiple;
117
118  return NULL;
119}
120
121/// isArrayMalloc - Returns the corresponding CallInst if the instruction
122/// is a call to malloc whose array size can be determined and the array size
123/// is not constant 1.  Otherwise, return NULL.
124const CallInst *llvm::isArrayMalloc(const Value *I, const TargetData *TD) {
125  const CallInst *CI = extractMallocCall(I);
126  Value *ArraySize = computeArraySize(CI, TD);
127
128  if (ArraySize &&
129      ArraySize != ConstantInt::get(CI->getArgOperand(0)->getType(), 1))
130    return CI;
131
132  // CI is a non-array malloc or we can't figure out that it is an array malloc.
133  return NULL;
134}
135
136/// getMallocType - Returns the PointerType resulting from the malloc call.
137/// The PointerType depends on the number of bitcast uses of the malloc call:
138///   0: PointerType is the calls' return type.
139///   1: PointerType is the bitcast's result type.
140///  >1: Unique PointerType cannot be determined, return NULL.
141const PointerType *llvm::getMallocType(const CallInst *CI) {
142  assert(isMalloc(CI) && "getMallocType and not malloc call");
143
144  const PointerType *MallocType = NULL;
145  unsigned NumOfBitCastUses = 0;
146
147  // Determine if CallInst has a bitcast use.
148  for (Value::const_use_iterator UI = CI->use_begin(), E = CI->use_end();
149       UI != E; )
150    if (const BitCastInst *BCI = dyn_cast<BitCastInst>(*UI++)) {
151      MallocType = cast<PointerType>(BCI->getDestTy());
152      NumOfBitCastUses++;
153    }
154
155  // Malloc call has 1 bitcast use, so type is the bitcast's destination type.
156  if (NumOfBitCastUses == 1)
157    return MallocType;
158
159  // Malloc call was not bitcast, so type is the malloc function's return type.
160  if (NumOfBitCastUses == 0)
161    return cast<PointerType>(CI->getType());
162
163  // Type could not be determined.
164  return NULL;
165}
166
167/// getMallocAllocatedType - Returns the Type allocated by malloc call.
168/// The Type depends on the number of bitcast uses of the malloc call:
169///   0: PointerType is the malloc calls' return type.
170///   1: PointerType is the bitcast's result type.
171///  >1: Unique PointerType cannot be determined, return NULL.
172const Type *llvm::getMallocAllocatedType(const CallInst *CI) {
173  const PointerType *PT = getMallocType(CI);
174  return PT ? PT->getElementType() : NULL;
175}
176
177/// getMallocArraySize - Returns the array size of a malloc call.  If the
178/// argument passed to malloc is a multiple of the size of the malloced type,
179/// then return that multiple.  For non-array mallocs, the multiple is
180/// constant 1.  Otherwise, return NULL for mallocs whose array size cannot be
181/// determined.
182Value *llvm::getMallocArraySize(CallInst *CI, const TargetData *TD,
183                                bool LookThroughSExt) {
184  assert(isMalloc(CI) && "getMallocArraySize and not malloc call");
185  return computeArraySize(CI, TD, LookThroughSExt);
186}
187
188//===----------------------------------------------------------------------===//
189//  free Call Utility Functions.
190//
191
192/// isFreeCall - Returns non-null if the value is a call to the builtin free()
193const CallInst *llvm::isFreeCall(const Value *I) {
194  const CallInst *CI = dyn_cast<CallInst>(I);
195  if (!CI)
196    return 0;
197  Function *Callee = CI->getCalledFunction();
198  if (Callee == 0 || !Callee->isDeclaration())
199    return 0;
200
201  if (Callee->getName() != "free" &&
202      Callee->getName() != "_ZdlPv" && // operator delete(void*)
203      Callee->getName() != "_ZdaPv")   // operator delete[](void*)
204    return 0;
205
206  // Check free prototype.
207  // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin
208  // attribute will exist.
209  const FunctionType *FTy = Callee->getFunctionType();
210  if (!FTy->getReturnType()->isVoidTy())
211    return 0;
212  if (FTy->getNumParams() != 1)
213    return 0;
214  if (FTy->param_begin()->get() != Type::getInt8PtrTy(Callee->getContext()))
215    return 0;
216
217  return CI;
218}
219