MemoryBuiltins.cpp revision 199481
1198892Srdivacky//===------ MemoryBuiltins.cpp - Identify calls to memory builtins --------===//
2198892Srdivacky//
3198892Srdivacky//                     The LLVM Compiler Infrastructure
4198892Srdivacky//
5198892Srdivacky// This file is distributed under the University of Illinois Open Source
6198892Srdivacky// License. See LICENSE.TXT for details.
7198892Srdivacky//
8198892Srdivacky//===----------------------------------------------------------------------===//
9198892Srdivacky//
10198892Srdivacky// This family of functions identifies calls to builtin functions that allocate
11198892Srdivacky// or free memory.
12198892Srdivacky//
13198892Srdivacky//===----------------------------------------------------------------------===//
14198892Srdivacky
15198892Srdivacky#include "llvm/Analysis/MemoryBuiltins.h"
16198892Srdivacky#include "llvm/Constants.h"
17198892Srdivacky#include "llvm/Instructions.h"
18198892Srdivacky#include "llvm/Module.h"
19199481Srdivacky#include "llvm/Analysis/ValueTracking.h"
20198953Srdivacky#include "llvm/Target/TargetData.h"
21198892Srdivackyusing namespace llvm;
22198892Srdivacky
23198892Srdivacky//===----------------------------------------------------------------------===//
24198892Srdivacky//  malloc Call Utility Functions.
25198892Srdivacky//
26198892Srdivacky
27198892Srdivacky/// isMalloc - Returns true if the the value is either a malloc call or a
28198892Srdivacky/// bitcast of the result of a malloc call.
29198892Srdivackybool llvm::isMalloc(const Value *I) {
30198892Srdivacky  return extractMallocCall(I) || extractMallocCallFromBitCast(I);
31198892Srdivacky}
32198892Srdivacky
33198892Srdivackystatic bool isMallocCall(const CallInst *CI) {
34198892Srdivacky  if (!CI)
35198892Srdivacky    return false;
36198892Srdivacky
37198892Srdivacky  Function *Callee = CI->getCalledFunction();
38198892Srdivacky  if (Callee == 0 || !Callee->isDeclaration() || Callee->getName() != "malloc")
39198892Srdivacky    return false;
40198892Srdivacky
41198892Srdivacky  // Check malloc prototype.
42198892Srdivacky  // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin
43198892Srdivacky  // attribute will exist.
44198892Srdivacky  const FunctionType *FTy = Callee->getFunctionType();
45198892Srdivacky  if (FTy->getNumParams() != 1)
46198892Srdivacky    return false;
47198892Srdivacky  if (IntegerType *ITy = dyn_cast<IntegerType>(FTy->param_begin()->get())) {
48198892Srdivacky    if (ITy->getBitWidth() != 32 && ITy->getBitWidth() != 64)
49198892Srdivacky      return false;
50198892Srdivacky    return true;
51198892Srdivacky  }
52198892Srdivacky
53198892Srdivacky  return false;
54198892Srdivacky}
55198892Srdivacky
56198892Srdivacky/// extractMallocCall - Returns the corresponding CallInst if the instruction
57198892Srdivacky/// is a malloc call.  Since CallInst::CreateMalloc() only creates calls, we
58198892Srdivacky/// ignore InvokeInst here.
59198892Srdivackyconst CallInst *llvm::extractMallocCall(const Value *I) {
60198892Srdivacky  const CallInst *CI = dyn_cast<CallInst>(I);
61198892Srdivacky  return (isMallocCall(CI)) ? CI : NULL;
62198892Srdivacky}
63198892Srdivacky
64198892SrdivackyCallInst *llvm::extractMallocCall(Value *I) {
65198892Srdivacky  CallInst *CI = dyn_cast<CallInst>(I);
66198892Srdivacky  return (isMallocCall(CI)) ? CI : NULL;
67198892Srdivacky}
68198892Srdivacky
69198892Srdivackystatic bool isBitCastOfMallocCall(const BitCastInst *BCI) {
70198892Srdivacky  if (!BCI)
71198892Srdivacky    return false;
72198892Srdivacky
73198892Srdivacky  return isMallocCall(dyn_cast<CallInst>(BCI->getOperand(0)));
74198892Srdivacky}
75198892Srdivacky
76198892Srdivacky/// extractMallocCallFromBitCast - Returns the corresponding CallInst if the
77198892Srdivacky/// instruction is a bitcast of the result of a malloc call.
78198892SrdivackyCallInst *llvm::extractMallocCallFromBitCast(Value *I) {
79198892Srdivacky  BitCastInst *BCI = dyn_cast<BitCastInst>(I);
80198892Srdivacky  return (isBitCastOfMallocCall(BCI)) ? cast<CallInst>(BCI->getOperand(0))
81198892Srdivacky                                      : NULL;
82198892Srdivacky}
83198892Srdivacky
84198892Srdivackyconst CallInst *llvm::extractMallocCallFromBitCast(const Value *I) {
85198892Srdivacky  const BitCastInst *BCI = dyn_cast<BitCastInst>(I);
86198892Srdivacky  return (isBitCastOfMallocCall(BCI)) ? cast<CallInst>(BCI->getOperand(0))
87198892Srdivacky                                      : NULL;
88198892Srdivacky}
89198892Srdivacky
90199481Srdivackystatic Value *computeArraySize(const CallInst *CI, const TargetData *TD,
91199481Srdivacky                               bool LookThroughSExt = false) {
92198892Srdivacky  if (!CI)
93198892Srdivacky    return NULL;
94198892Srdivacky
95198953Srdivacky  // The size of the malloc's result type must be known to determine array size.
96198892Srdivacky  const Type *T = getMallocAllocatedType(CI);
97198953Srdivacky  if (!T || !T->isSized() || !TD)
98198892Srdivacky    return NULL;
99198892Srdivacky
100199481Srdivacky  unsigned ElementSize = TD->getTypeAllocSize(T);
101198953Srdivacky  if (const StructType *ST = dyn_cast<StructType>(T))
102199481Srdivacky    ElementSize = TD->getStructLayout(ST)->getSizeInBytes();
103198892Srdivacky
104199481Srdivacky  // If malloc calls' arg can be determined to be a multiple of ElementSize,
105199481Srdivacky  // return the multiple.  Otherwise, return NULL.
106199481Srdivacky  Value *MallocArg = CI->getOperand(1);
107199481Srdivacky  Value *Multiple = NULL;
108199481Srdivacky  if (ComputeMultiple(MallocArg, ElementSize, Multiple,
109199481Srdivacky                      LookThroughSExt))
110199481Srdivacky    return Multiple;
111198892Srdivacky
112198892Srdivacky  return NULL;
113198892Srdivacky}
114198892Srdivacky
115198892Srdivacky/// isArrayMalloc - Returns the corresponding CallInst if the instruction
116198892Srdivacky/// is a call to malloc whose array size can be determined and the array size
117198892Srdivacky/// is not constant 1.  Otherwise, return NULL.
118199481Srdivackyconst CallInst *llvm::isArrayMalloc(const Value *I, const TargetData *TD) {
119198892Srdivacky  const CallInst *CI = extractMallocCall(I);
120199481Srdivacky  Value *ArraySize = computeArraySize(CI, TD);
121198892Srdivacky
122198892Srdivacky  if (ArraySize &&
123198892Srdivacky      ArraySize != ConstantInt::get(CI->getOperand(1)->getType(), 1))
124198892Srdivacky    return CI;
125198892Srdivacky
126198892Srdivacky  // CI is a non-array malloc or we can't figure out that it is an array malloc.
127198892Srdivacky  return NULL;
128198892Srdivacky}
129198892Srdivacky
130198892Srdivacky/// getMallocType - Returns the PointerType resulting from the malloc call.
131198953Srdivacky/// The PointerType depends on the number of bitcast uses of the malloc call:
132198953Srdivacky///   0: PointerType is the calls' return type.
133198953Srdivacky///   1: PointerType is the bitcast's result type.
134198953Srdivacky///  >1: Unique PointerType cannot be determined, return NULL.
135198892Srdivackyconst PointerType *llvm::getMallocType(const CallInst *CI) {
136199481Srdivacky  assert(isMalloc(CI) && "getMallocType and not malloc call");
137198892Srdivacky
138198953Srdivacky  const PointerType *MallocType = NULL;
139198953Srdivacky  unsigned NumOfBitCastUses = 0;
140198953Srdivacky
141198892Srdivacky  // Determine if CallInst has a bitcast use.
142198892Srdivacky  for (Value::use_const_iterator UI = CI->use_begin(), E = CI->use_end();
143198892Srdivacky       UI != E; )
144198953Srdivacky    if (const BitCastInst *BCI = dyn_cast<BitCastInst>(*UI++)) {
145198953Srdivacky      MallocType = cast<PointerType>(BCI->getDestTy());
146198953Srdivacky      NumOfBitCastUses++;
147198953Srdivacky    }
148198892Srdivacky
149198953Srdivacky  // Malloc call has 1 bitcast use, so type is the bitcast's destination type.
150198953Srdivacky  if (NumOfBitCastUses == 1)
151198953Srdivacky    return MallocType;
152198892Srdivacky
153198892Srdivacky  // Malloc call was not bitcast, so type is the malloc function's return type.
154198953Srdivacky  if (NumOfBitCastUses == 0)
155198892Srdivacky    return cast<PointerType>(CI->getType());
156198892Srdivacky
157198892Srdivacky  // Type could not be determined.
158198892Srdivacky  return NULL;
159198892Srdivacky}
160198892Srdivacky
161198953Srdivacky/// getMallocAllocatedType - Returns the Type allocated by malloc call.
162198953Srdivacky/// The Type depends on the number of bitcast uses of the malloc call:
163198953Srdivacky///   0: PointerType is the malloc calls' return type.
164198953Srdivacky///   1: PointerType is the bitcast's result type.
165198953Srdivacky///  >1: Unique PointerType cannot be determined, return NULL.
166198892Srdivackyconst Type *llvm::getMallocAllocatedType(const CallInst *CI) {
167198892Srdivacky  const PointerType *PT = getMallocType(CI);
168198892Srdivacky  return PT ? PT->getElementType() : NULL;
169198892Srdivacky}
170198892Srdivacky
171198892Srdivacky/// getMallocArraySize - Returns the array size of a malloc call.  If the
172198892Srdivacky/// argument passed to malloc is a multiple of the size of the malloced type,
173198892Srdivacky/// then return that multiple.  For non-array mallocs, the multiple is
174198892Srdivacky/// constant 1.  Otherwise, return NULL for mallocs whose array size cannot be
175198892Srdivacky/// determined.
176199481SrdivackyValue *llvm::getMallocArraySize(CallInst *CI, const TargetData *TD,
177199481Srdivacky                                bool LookThroughSExt) {
178199481Srdivacky  assert(isMalloc(CI) && "getMallocArraySize and not malloc call");
179199481Srdivacky  return computeArraySize(CI, TD, LookThroughSExt);
180198892Srdivacky}
181198892Srdivacky
182198892Srdivacky//===----------------------------------------------------------------------===//
183198892Srdivacky//  free Call Utility Functions.
184198892Srdivacky//
185198892Srdivacky
186198892Srdivacky/// isFreeCall - Returns true if the the value is a call to the builtin free()
187198892Srdivackybool llvm::isFreeCall(const Value *I) {
188198892Srdivacky  const CallInst *CI = dyn_cast<CallInst>(I);
189198892Srdivacky  if (!CI)
190198892Srdivacky    return false;
191198892Srdivacky  Function *Callee = CI->getCalledFunction();
192198892Srdivacky  if (Callee == 0 || !Callee->isDeclaration() || Callee->getName() != "free")
193198892Srdivacky    return false;
194198892Srdivacky
195198892Srdivacky  // Check free prototype.
196198892Srdivacky  // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin
197198892Srdivacky  // attribute will exist.
198198892Srdivacky  const FunctionType *FTy = Callee->getFunctionType();
199198892Srdivacky  if (!FTy->getReturnType()->isVoidTy())
200198892Srdivacky    return false;
201198892Srdivacky  if (FTy->getNumParams() != 1)
202198892Srdivacky    return false;
203198892Srdivacky  if (FTy->param_begin()->get() != Type::getInt8PtrTy(Callee->getContext()))
204198892Srdivacky    return false;
205198892Srdivacky
206198892Srdivacky  return true;
207198892Srdivacky}
208