blob: 0f0a1c98d0d2560700be65b5f120b176c8c49a3d [file] [log] [blame]
Victor Hernandezf006b182009-10-27 20:05:49 +00001//===------ MemoryBuiltins.cpp - Identify calls to memory builtins --------===//
Evan Chengfabcb912009-09-10 04:36:43 +00002//
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//
Victor Hernandezf006b182009-10-27 20:05:49 +000010// This family of functions identifies calls to builtin functions that allocate
Michael Ilseman9333ffb2013-03-08 21:03:09 +000011// or free memory.
Evan Chengfabcb912009-09-10 04:36:43 +000012//
13//===----------------------------------------------------------------------===//
14
Nuno Lopes9e72a792012-06-21 15:45:28 +000015#define DEBUG_TYPE "memory-builtins"
Victor Hernandezf006b182009-10-27 20:05:49 +000016#include "llvm/Analysis/MemoryBuiltins.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/Statistic.h"
19#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000020#include "llvm/IR/DataLayout.h"
21#include "llvm/IR/GlobalVariable.h"
22#include "llvm/IR/Instructions.h"
23#include "llvm/IR/Intrinsics.h"
24#include "llvm/IR/Metadata.h"
25#include "llvm/IR/Module.h"
Nuno Lopes9e72a792012-06-21 15:45:28 +000026#include "llvm/Support/Debug.h"
27#include "llvm/Support/MathExtras.h"
28#include "llvm/Support/raw_ostream.h"
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +000029#include "llvm/Target/TargetLibraryInfo.h"
Nuno Lopes9e72a792012-06-21 15:45:28 +000030#include "llvm/Transforms/Utils/Local.h"
Evan Chengfabcb912009-09-10 04:36:43 +000031using namespace llvm;
32
Nuno Lopes9e72a792012-06-21 15:45:28 +000033enum AllocType {
34 MallocLike = 1<<0, // allocates
35 CallocLike = 1<<1, // allocates + bzero
36 ReallocLike = 1<<2, // reallocates
37 StrDupLike = 1<<3,
38 AllocLike = MallocLike | CallocLike | StrDupLike,
39 AnyAlloc = MallocLike | CallocLike | ReallocLike | StrDupLike
40};
Evan Chengfabcb912009-09-10 04:36:43 +000041
Nuno Lopes9e72a792012-06-21 15:45:28 +000042struct AllocFnsTy {
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +000043 LibFunc::Func Func;
Nuno Lopes9e72a792012-06-21 15:45:28 +000044 AllocType AllocTy;
45 unsigned char NumParams;
46 // First and Second size parameters (or -1 if unused)
Nuno Lopesef22f042012-06-21 18:38:26 +000047 signed char FstParam, SndParam;
Nuno Lopes9e72a792012-06-21 15:45:28 +000048};
Evan Chengfabcb912009-09-10 04:36:43 +000049
Nuno Lopes41a3f252012-06-28 16:34:03 +000050// FIXME: certain users need more information. E.g., SimplifyLibCalls needs to
51// know which functions are nounwind, noalias, nocapture parameters, etc.
Nuno Lopes9e72a792012-06-21 15:45:28 +000052static const AllocFnsTy AllocationFnData[] = {
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +000053 {LibFunc::malloc, MallocLike, 1, 0, -1},
54 {LibFunc::valloc, MallocLike, 1, 0, -1},
Nadav Roteme43e2d82013-04-09 04:43:46 +000055 {LibFunc::Znwj, MallocLike, 1, 0, -1}, // new(unsigned int)
56 {LibFunc::ZnwjRKSt9nothrow_t, MallocLike, 2, 0, -1}, // new(unsigned int, nothrow)
57 {LibFunc::Znwm, MallocLike, 1, 0, -1}, // new(unsigned long)
58 {LibFunc::ZnwmRKSt9nothrow_t, MallocLike, 2, 0, -1}, // new(unsigned long, nothrow)
59 {LibFunc::Znaj, MallocLike, 1, 0, -1}, // new[](unsigned int)
60 {LibFunc::ZnajRKSt9nothrow_t, MallocLike, 2, 0, -1}, // new[](unsigned int, nothrow)
61 {LibFunc::Znam, MallocLike, 1, 0, -1}, // new[](unsigned long)
62 {LibFunc::ZnamRKSt9nothrow_t, MallocLike, 2, 0, -1}, // new[](unsigned long, nothrow)
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +000063 {LibFunc::posix_memalign, MallocLike, 3, 2, -1},
64 {LibFunc::calloc, CallocLike, 2, 0, 1},
65 {LibFunc::realloc, ReallocLike, 2, 1, -1},
66 {LibFunc::reallocf, ReallocLike, 2, 1, -1},
67 {LibFunc::strdup, StrDupLike, 1, -1, -1},
68 {LibFunc::strndup, StrDupLike, 2, 1, -1}
Nuno Lopes9e72a792012-06-21 15:45:28 +000069};
70
71
72static Function *getCalledFunction(const Value *V, bool LookThroughBitCast) {
73 if (LookThroughBitCast)
74 V = V->stripPointerCasts();
Nuno Lopes2b3e9582012-06-21 21:25:05 +000075
Nuno Lopesd845c342012-06-22 15:50:53 +000076 CallSite CS(const_cast<Value*>(V));
77 if (!CS.getInstruction())
Nuno Lopes9e72a792012-06-21 15:45:28 +000078 return 0;
Evan Chengfabcb912009-09-10 04:36:43 +000079
Michael Gottesman2253a2f2013-06-27 00:25:01 +000080 if (CS.isNoBuiltin())
Richard Smitheb351eb2013-05-16 04:12:04 +000081 return 0;
82
Nuno Lopes2b3e9582012-06-21 21:25:05 +000083 Function *Callee = CS.getCalledFunction();
Nuno Lopes9e72a792012-06-21 15:45:28 +000084 if (!Callee || !Callee->isDeclaration())
85 return 0;
86 return Callee;
87}
Evan Chengfabcb912009-09-10 04:36:43 +000088
Nuno Lopes9e72a792012-06-21 15:45:28 +000089/// \brief Returns the allocation data for the given value if it is a call to a
90/// known allocation function, and NULL otherwise.
91static const AllocFnsTy *getAllocationData(const Value *V, AllocType AllocTy,
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +000092 const TargetLibraryInfo *TLI,
Nuno Lopes9e72a792012-06-21 15:45:28 +000093 bool LookThroughBitCast = false) {
Michael Ilsemancacf9712013-03-08 21:15:00 +000094 // Skip intrinsics
95 if (isa<IntrinsicInst>(V))
96 return 0;
97
Nuno Lopes9e72a792012-06-21 15:45:28 +000098 Function *Callee = getCalledFunction(V, LookThroughBitCast);
99 if (!Callee)
100 return 0;
101
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000102 // Make sure that the function is available.
103 StringRef FnName = Callee->getName();
104 LibFunc::Func TLIFn;
105 if (!TLI || !TLI->getLibFunc(FnName, TLIFn) || !TLI->has(TLIFn))
106 return 0;
107
Nuno Lopes9e72a792012-06-21 15:45:28 +0000108 unsigned i = 0;
109 bool found = false;
110 for ( ; i < array_lengthof(AllocationFnData); ++i) {
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000111 if (AllocationFnData[i].Func == TLIFn) {
Nuno Lopes9e72a792012-06-21 15:45:28 +0000112 found = true;
113 break;
114 }
115 }
116 if (!found)
117 return 0;
118
119 const AllocFnsTy *FnData = &AllocationFnData[i];
120 if ((FnData->AllocTy & AllocTy) == 0)
121 return 0;
122
123 // Check function prototype.
Nuno Lopesef22f042012-06-21 18:38:26 +0000124 int FstParam = FnData->FstParam;
125 int SndParam = FnData->SndParam;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000126 FunctionType *FTy = Callee->getFunctionType();
Nuno Lopes9e72a792012-06-21 15:45:28 +0000127
128 if (FTy->getReturnType() == Type::getInt8PtrTy(FTy->getContext()) &&
129 FTy->getNumParams() == FnData->NumParams &&
Nuno Lopesef22f042012-06-21 18:38:26 +0000130 (FstParam < 0 ||
Nuno Lopes9e72a792012-06-21 15:45:28 +0000131 (FTy->getParamType(FstParam)->isIntegerTy(32) ||
132 FTy->getParamType(FstParam)->isIntegerTy(64))) &&
Nuno Lopesef22f042012-06-21 18:38:26 +0000133 (SndParam < 0 ||
Nuno Lopes9e72a792012-06-21 15:45:28 +0000134 FTy->getParamType(SndParam)->isIntegerTy(32) ||
135 FTy->getParamType(SndParam)->isIntegerTy(64)))
136 return FnData;
137 return 0;
138}
139
140static bool hasNoAliasAttr(const Value *V, bool LookThroughBitCast) {
Nuno Lopese8742d02012-06-25 16:17:54 +0000141 ImmutableCallSite CS(LookThroughBitCast ? V->stripPointerCasts() : V);
Bill Wendling034b94b2012-12-19 07:18:57 +0000142 return CS && CS.hasFnAttr(Attribute::NoAlias);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000143}
144
145
Nuno Lopes2b3e9582012-06-21 21:25:05 +0000146/// \brief Tests if a value is a call or invoke to a library function that
147/// allocates or reallocates memory (either malloc, calloc, realloc, or strdup
148/// like).
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000149bool llvm::isAllocationFn(const Value *V, const TargetLibraryInfo *TLI,
150 bool LookThroughBitCast) {
151 return getAllocationData(V, AnyAlloc, TLI, LookThroughBitCast);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000152}
153
Nuno Lopes2b3e9582012-06-21 21:25:05 +0000154/// \brief Tests if a value is a call or invoke to a function that returns a
Nuno Lopes41a3f252012-06-28 16:34:03 +0000155/// NoAlias pointer (including malloc/calloc/realloc/strdup-like functions).
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000156bool llvm::isNoAliasFn(const Value *V, const TargetLibraryInfo *TLI,
157 bool LookThroughBitCast) {
Nuno Lopes41a3f252012-06-28 16:34:03 +0000158 // it's safe to consider realloc as noalias since accessing the original
159 // pointer is undefined behavior
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000160 return isAllocationFn(V, TLI, LookThroughBitCast) ||
Nuno Lopes9e72a792012-06-21 15:45:28 +0000161 hasNoAliasAttr(V, LookThroughBitCast);
162}
163
Nuno Lopes2b3e9582012-06-21 21:25:05 +0000164/// \brief Tests if a value is a call or invoke to a library function that
165/// allocates uninitialized memory (such as malloc).
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000166bool llvm::isMallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
167 bool LookThroughBitCast) {
168 return getAllocationData(V, MallocLike, TLI, LookThroughBitCast);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000169}
170
Nuno Lopes2b3e9582012-06-21 21:25:05 +0000171/// \brief Tests if a value is a call or invoke to a library function that
172/// allocates zero-filled memory (such as calloc).
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000173bool llvm::isCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
174 bool LookThroughBitCast) {
175 return getAllocationData(V, CallocLike, TLI, LookThroughBitCast);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000176}
177
Nuno Lopes2b3e9582012-06-21 21:25:05 +0000178/// \brief Tests if a value is a call or invoke to a library function that
179/// allocates memory (either malloc, calloc, or strdup like).
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000180bool llvm::isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
181 bool LookThroughBitCast) {
182 return getAllocationData(V, AllocLike, TLI, LookThroughBitCast);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000183}
184
Nuno Lopes2b3e9582012-06-21 21:25:05 +0000185/// \brief Tests if a value is a call or invoke to a library function that
186/// reallocates memory (such as realloc).
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000187bool llvm::isReallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
188 bool LookThroughBitCast) {
189 return getAllocationData(V, ReallocLike, TLI, LookThroughBitCast);
Evan Chengfabcb912009-09-10 04:36:43 +0000190}
191
192/// extractMallocCall - Returns the corresponding CallInst if the instruction
193/// is a malloc call. Since CallInst::CreateMalloc() only creates calls, we
194/// ignore InvokeInst here.
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000195const CallInst *llvm::extractMallocCall(const Value *I,
196 const TargetLibraryInfo *TLI) {
197 return isMallocLikeFn(I, TLI) ? dyn_cast<CallInst>(I) : 0;
Evan Chengfabcb912009-09-10 04:36:43 +0000198}
199
Micah Villmow3574eca2012-10-08 16:38:25 +0000200static Value *computeArraySize(const CallInst *CI, const DataLayout *TD,
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000201 const TargetLibraryInfo *TLI,
Victor Hernandez8e345a12009-11-10 08:32:25 +0000202 bool LookThroughSExt = false) {
Evan Chengfabcb912009-09-10 04:36:43 +0000203 if (!CI)
Jakub Staszaka9cd5162013-03-07 20:01:47 +0000204 return 0;
Evan Chengfabcb912009-09-10 04:36:43 +0000205
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000206 // The size of the malloc's result type must be known to determine array size.
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000207 Type *T = getMallocAllocatedType(CI, TLI);
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000208 if (!T || !T->isSized() || !TD)
Jakub Staszaka9cd5162013-03-07 20:01:47 +0000209 return 0;
Victor Hernandez88d98392009-09-18 19:20:02 +0000210
Victor Hernandez8e345a12009-11-10 08:32:25 +0000211 unsigned ElementSize = TD->getTypeAllocSize(T);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000212 if (StructType *ST = dyn_cast<StructType>(T))
Victor Hernandez8e345a12009-11-10 08:32:25 +0000213 ElementSize = TD->getStructLayout(ST)->getSizeInBytes();
Victor Hernandez88d98392009-09-18 19:20:02 +0000214
Gabor Greife3401c42010-06-23 21:41:47 +0000215 // If malloc call's arg can be determined to be a multiple of ElementSize,
Victor Hernandez8e345a12009-11-10 08:32:25 +0000216 // return the multiple. Otherwise, return NULL.
Gabor Greife3401c42010-06-23 21:41:47 +0000217 Value *MallocArg = CI->getArgOperand(0);
Jakub Staszaka9cd5162013-03-07 20:01:47 +0000218 Value *Multiple = 0;
Victor Hernandez8e345a12009-11-10 08:32:25 +0000219 if (ComputeMultiple(MallocArg, ElementSize, Multiple,
Dan Gohman3dbb9e62009-11-18 00:58:27 +0000220 LookThroughSExt))
Victor Hernandez8e345a12009-11-10 08:32:25 +0000221 return Multiple;
Victor Hernandez88d98392009-09-18 19:20:02 +0000222
Jakub Staszaka9cd5162013-03-07 20:01:47 +0000223 return 0;
Evan Chengfabcb912009-09-10 04:36:43 +0000224}
225
Michael Ilseman9333ffb2013-03-08 21:03:09 +0000226/// isArrayMalloc - Returns the corresponding CallInst if the instruction
Victor Hernandez90f48e72009-10-28 20:18:55 +0000227/// is a call to malloc whose array size can be determined and the array size
228/// is not constant 1. Otherwise, return NULL.
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000229const CallInst *llvm::isArrayMalloc(const Value *I,
Micah Villmow3574eca2012-10-08 16:38:25 +0000230 const DataLayout *TD,
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000231 const TargetLibraryInfo *TLI) {
232 const CallInst *CI = extractMallocCall(I, TLI);
233 Value *ArraySize = computeArraySize(CI, TD, TLI);
Victor Hernandez90f48e72009-10-28 20:18:55 +0000234
Jakub Staszakb1b6c172013-03-07 20:22:39 +0000235 if (ConstantInt *ConstSize = dyn_cast_or_null<ConstantInt>(ArraySize))
236 if (ConstSize->isOne())
237 return CI;
Victor Hernandez90f48e72009-10-28 20:18:55 +0000238
239 // CI is a non-array malloc or we can't figure out that it is an array malloc.
Jakub Staszaka9cd5162013-03-07 20:01:47 +0000240 return 0;
Evan Chengfabcb912009-09-10 04:36:43 +0000241}
242
243/// getMallocType - Returns the PointerType resulting from the malloc call.
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000244/// The PointerType depends on the number of bitcast uses of the malloc call:
245/// 0: PointerType is the calls' return type.
246/// 1: PointerType is the bitcast's result type.
247/// >1: Unique PointerType cannot be determined, return NULL.
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000248PointerType *llvm::getMallocType(const CallInst *CI,
249 const TargetLibraryInfo *TLI) {
250 assert(isMallocLikeFn(CI, TLI) && "getMallocType and not malloc call");
Michael Ilseman9333ffb2013-03-08 21:03:09 +0000251
Jakub Staszaka9cd5162013-03-07 20:01:47 +0000252 PointerType *MallocType = 0;
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000253 unsigned NumOfBitCastUses = 0;
254
Victor Hernandez88d98392009-09-18 19:20:02 +0000255 // Determine if CallInst has a bitcast use.
Gabor Greif60ad7812010-03-25 23:06:16 +0000256 for (Value::const_use_iterator UI = CI->use_begin(), E = CI->use_end();
Victor Hernandez88d98392009-09-18 19:20:02 +0000257 UI != E; )
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000258 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(*UI++)) {
259 MallocType = cast<PointerType>(BCI->getDestTy());
260 NumOfBitCastUses++;
261 }
Evan Chengfabcb912009-09-10 04:36:43 +0000262
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000263 // Malloc call has 1 bitcast use, so type is the bitcast's destination type.
264 if (NumOfBitCastUses == 1)
265 return MallocType;
Evan Chengfabcb912009-09-10 04:36:43 +0000266
Victor Hernandez60cfc032009-09-22 18:50:03 +0000267 // Malloc call was not bitcast, so type is the malloc function's return type.
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000268 if (NumOfBitCastUses == 0)
Victor Hernandez88d98392009-09-18 19:20:02 +0000269 return cast<PointerType>(CI->getType());
270
271 // Type could not be determined.
Jakub Staszaka9cd5162013-03-07 20:01:47 +0000272 return 0;
Evan Chengfabcb912009-09-10 04:36:43 +0000273}
274
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000275/// getMallocAllocatedType - Returns the Type allocated by malloc call.
276/// The Type depends on the number of bitcast uses of the malloc call:
277/// 0: PointerType is the malloc calls' return type.
278/// 1: PointerType is the bitcast's result type.
279/// >1: Unique PointerType cannot be determined, return NULL.
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000280Type *llvm::getMallocAllocatedType(const CallInst *CI,
281 const TargetLibraryInfo *TLI) {
282 PointerType *PT = getMallocType(CI, TLI);
Jakub Staszaka9cd5162013-03-07 20:01:47 +0000283 return PT ? PT->getElementType() : 0;
Evan Chengfabcb912009-09-10 04:36:43 +0000284}
285
Michael Ilseman9333ffb2013-03-08 21:03:09 +0000286/// getMallocArraySize - Returns the array size of a malloc call. If the
Victor Hernandez90f48e72009-10-28 20:18:55 +0000287/// argument passed to malloc is a multiple of the size of the malloced type,
288/// then return that multiple. For non-array mallocs, the multiple is
289/// constant 1. Otherwise, return NULL for mallocs whose array size cannot be
Victor Hernandez2491ce02009-10-15 20:14:52 +0000290/// determined.
Micah Villmow3574eca2012-10-08 16:38:25 +0000291Value *llvm::getMallocArraySize(CallInst *CI, const DataLayout *TD,
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000292 const TargetLibraryInfo *TLI,
Victor Hernandez8e345a12009-11-10 08:32:25 +0000293 bool LookThroughSExt) {
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000294 assert(isMallocLikeFn(CI, TLI) && "getMallocArraySize and not malloc call");
295 return computeArraySize(CI, TD, TLI, LookThroughSExt);
Evan Chengfabcb912009-09-10 04:36:43 +0000296}
Victor Hernandez66284e02009-10-24 04:23:03 +0000297
Nuno Lopes252ef562012-05-03 21:19:58 +0000298
Nuno Lopes252ef562012-05-03 21:19:58 +0000299/// extractCallocCall - Returns the corresponding CallInst if the instruction
300/// is a calloc call.
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000301const CallInst *llvm::extractCallocCall(const Value *I,
302 const TargetLibraryInfo *TLI) {
303 return isCallocLikeFn(I, TLI) ? cast<CallInst>(I) : 0;
Nuno Lopes252ef562012-05-03 21:19:58 +0000304}
305
Victor Hernandez046e78c2009-10-26 23:43:48 +0000306
Gabor Greif02680f92010-06-23 21:51:12 +0000307/// isFreeCall - Returns non-null if the value is a call to the builtin free()
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000308const CallInst *llvm::isFreeCall(const Value *I, const TargetLibraryInfo *TLI) {
Victor Hernandez66284e02009-10-24 04:23:03 +0000309 const CallInst *CI = dyn_cast<CallInst>(I);
Michael Ilsemancacf9712013-03-08 21:15:00 +0000310 if (!CI || isa<IntrinsicInst>(CI))
Gabor Greif02680f92010-06-23 21:51:12 +0000311 return 0;
Victor Hernandez3ad70d52009-11-03 20:39:35 +0000312 Function *Callee = CI->getCalledFunction();
Nick Lewycky42e72ca2011-03-15 07:31:32 +0000313 if (Callee == 0 || !Callee->isDeclaration())
314 return 0;
315
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000316 StringRef FnName = Callee->getName();
317 LibFunc::Func TLIFn;
318 if (!TLI || !TLI->getLibFunc(FnName, TLIFn) || !TLI->has(TLIFn))
319 return 0;
320
Richard Smith72c83312013-07-21 23:11:42 +0000321 unsigned ExpectedNumParams;
322 if (TLIFn == LibFunc::free ||
323 TLIFn == LibFunc::ZdlPv || // operator delete(void*)
324 TLIFn == LibFunc::ZdaPv) // operator delete[](void*)
325 ExpectedNumParams = 1;
326 else if (TLIFn == LibFunc::ZdlPvRKSt9nothrow_t || // delete(void*, nothrow)
327 TLIFn == LibFunc::ZdaPvRKSt9nothrow_t) // delete[](void*, nothrow)
328 ExpectedNumParams = 2;
329 else
Gabor Greif02680f92010-06-23 21:51:12 +0000330 return 0;
Victor Hernandez66284e02009-10-24 04:23:03 +0000331
332 // Check free prototype.
Michael Ilseman9333ffb2013-03-08 21:03:09 +0000333 // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin
Victor Hernandez66284e02009-10-24 04:23:03 +0000334 // attribute will exist.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000335 FunctionType *FTy = Callee->getFunctionType();
Victor Hernandez3ad70d52009-11-03 20:39:35 +0000336 if (!FTy->getReturnType()->isVoidTy())
Gabor Greif02680f92010-06-23 21:51:12 +0000337 return 0;
Richard Smith72c83312013-07-21 23:11:42 +0000338 if (FTy->getNumParams() != ExpectedNumParams)
Gabor Greif02680f92010-06-23 21:51:12 +0000339 return 0;
Chris Lattnerebb21892011-06-18 21:46:23 +0000340 if (FTy->getParamType(0) != Type::getInt8PtrTy(Callee->getContext()))
Gabor Greif02680f92010-06-23 21:51:12 +0000341 return 0;
Victor Hernandez66284e02009-10-24 04:23:03 +0000342
Gabor Greif02680f92010-06-23 21:51:12 +0000343 return CI;
Victor Hernandez66284e02009-10-24 04:23:03 +0000344}
Nuno Lopes9e72a792012-06-21 15:45:28 +0000345
346
347
348//===----------------------------------------------------------------------===//
349// Utility functions to compute size of objects.
350//
351
352
353/// \brief Compute the size of the object pointed by Ptr. Returns true and the
354/// object size in Size if successful, and false otherwise.
355/// If RoundToAlign is true, then Size is rounded up to the aligment of allocas,
356/// byval arguments, and global variables.
Micah Villmow3574eca2012-10-08 16:38:25 +0000357bool llvm::getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout *TD,
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000358 const TargetLibraryInfo *TLI, bool RoundToAlign) {
Nuno Lopes9e72a792012-06-21 15:45:28 +0000359 if (!TD)
360 return false;
361
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000362 ObjectSizeOffsetVisitor Visitor(TD, TLI, Ptr->getContext(), RoundToAlign);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000363 SizeOffsetType Data = Visitor.compute(const_cast<Value*>(Ptr));
364 if (!Visitor.bothKnown(Data))
365 return false;
366
367 APInt ObjSize = Data.first, Offset = Data.second;
368 // check for overflow
369 if (Offset.slt(0) || ObjSize.ult(Offset))
370 Size = 0;
371 else
372 Size = (ObjSize - Offset).getZExtValue();
373 return true;
374}
375
376
377STATISTIC(ObjectVisitorArgument,
378 "Number of arguments with unsolved size and offset");
379STATISTIC(ObjectVisitorLoad,
380 "Number of load instructions with unsolved size and offset");
381
382
383APInt ObjectSizeOffsetVisitor::align(APInt Size, uint64_t Align) {
384 if (RoundToAlign && Align)
385 return APInt(IntTyBits, RoundUpToAlignment(Size.getZExtValue(), Align));
386 return Size;
387}
388
Micah Villmow3574eca2012-10-08 16:38:25 +0000389ObjectSizeOffsetVisitor::ObjectSizeOffsetVisitor(const DataLayout *TD,
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000390 const TargetLibraryInfo *TLI,
Nuno Lopes9e72a792012-06-21 15:45:28 +0000391 LLVMContext &Context,
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000392 bool RoundToAlign)
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000393: TD(TD), TLI(TLI), RoundToAlign(RoundToAlign) {
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000394 IntegerType *IntTy = TD->getIntPtrType(Context);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000395 IntTyBits = IntTy->getBitWidth();
396 Zero = APInt::getNullValue(IntTyBits);
397}
398
399SizeOffsetType ObjectSizeOffsetVisitor::compute(Value *V) {
400 V = V->stripPointerCasts();
Nadav Rotem8e4df482013-04-09 18:16:05 +0000401 if (Instruction *I = dyn_cast<Instruction>(V)) {
402 // If we have already seen this instruction, bail out. Cycles can happen in
403 // unreachable code after constant propagation.
404 if (!SeenInsts.insert(I))
405 return unknown();
Nuno Lopes0a9ff4c2012-12-31 20:45:10 +0000406
Benjamin Kramer168843c2012-08-17 19:26:41 +0000407 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V))
Nadav Rotem8e4df482013-04-09 18:16:05 +0000408 return visitGEPOperator(*GEP);
409 return visit(*I);
Benjamin Kramer168843c2012-08-17 19:26:41 +0000410 }
Nuno Lopes9e72a792012-06-21 15:45:28 +0000411 if (Argument *A = dyn_cast<Argument>(V))
412 return visitArgument(*A);
413 if (ConstantPointerNull *P = dyn_cast<ConstantPointerNull>(V))
414 return visitConstantPointerNull(*P);
Nuno Lopes41be2fb2012-12-31 16:23:48 +0000415 if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
416 return visitGlobalAlias(*GA);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000417 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
418 return visitGlobalVariable(*GV);
419 if (UndefValue *UV = dyn_cast<UndefValue>(V))
420 return visitUndefValue(*UV);
Benjamin Kramer168843c2012-08-17 19:26:41 +0000421 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Nuno Lopes9e72a792012-06-21 15:45:28 +0000422 if (CE->getOpcode() == Instruction::IntToPtr)
423 return unknown(); // clueless
Nadav Rotem8e4df482013-04-09 18:16:05 +0000424 if (CE->getOpcode() == Instruction::GetElementPtr)
425 return visitGEPOperator(cast<GEPOperator>(*CE));
Benjamin Kramer168843c2012-08-17 19:26:41 +0000426 }
Nuno Lopes9e72a792012-06-21 15:45:28 +0000427
428 DEBUG(dbgs() << "ObjectSizeOffsetVisitor::compute() unhandled value: " << *V
429 << '\n');
430 return unknown();
431}
432
433SizeOffsetType ObjectSizeOffsetVisitor::visitAllocaInst(AllocaInst &I) {
434 if (!I.getAllocatedType()->isSized())
435 return unknown();
436
437 APInt Size(IntTyBits, TD->getTypeAllocSize(I.getAllocatedType()));
438 if (!I.isArrayAllocation())
439 return std::make_pair(align(Size, I.getAlignment()), Zero);
440
441 Value *ArraySize = I.getArraySize();
442 if (const ConstantInt *C = dyn_cast<ConstantInt>(ArraySize)) {
443 Size *= C->getValue().zextOrSelf(IntTyBits);
444 return std::make_pair(align(Size, I.getAlignment()), Zero);
445 }
446 return unknown();
447}
448
449SizeOffsetType ObjectSizeOffsetVisitor::visitArgument(Argument &A) {
450 // no interprocedural analysis is done at the moment
451 if (!A.hasByValAttr()) {
452 ++ObjectVisitorArgument;
453 return unknown();
454 }
455 PointerType *PT = cast<PointerType>(A.getType());
456 APInt Size(IntTyBits, TD->getTypeAllocSize(PT->getElementType()));
457 return std::make_pair(align(Size, A.getParamAlignment()), Zero);
458}
459
460SizeOffsetType ObjectSizeOffsetVisitor::visitCallSite(CallSite CS) {
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000461 const AllocFnsTy *FnData = getAllocationData(CS.getInstruction(), AnyAlloc,
462 TLI);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000463 if (!FnData)
464 return unknown();
465
466 // handle strdup-like functions separately
467 if (FnData->AllocTy == StrDupLike) {
Nuno Lopes9827c8e2012-07-24 16:28:13 +0000468 APInt Size(IntTyBits, GetStringLength(CS.getArgument(0)));
469 if (!Size)
470 return unknown();
471
472 // strndup limits strlen
473 if (FnData->FstParam > 0) {
474 ConstantInt *Arg= dyn_cast<ConstantInt>(CS.getArgument(FnData->FstParam));
475 if (!Arg)
476 return unknown();
477
478 APInt MaxSize = Arg->getValue().zextOrSelf(IntTyBits);
479 if (Size.ugt(MaxSize))
480 Size = MaxSize + 1;
481 }
482 return std::make_pair(Size, Zero);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000483 }
484
485 ConstantInt *Arg = dyn_cast<ConstantInt>(CS.getArgument(FnData->FstParam));
486 if (!Arg)
487 return unknown();
488
Nuno Lopes034dd6c2012-06-21 16:47:58 +0000489 APInt Size = Arg->getValue().zextOrSelf(IntTyBits);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000490 // size determined by just 1 parameter
Nuno Lopesef22f042012-06-21 18:38:26 +0000491 if (FnData->SndParam < 0)
Nuno Lopes9e72a792012-06-21 15:45:28 +0000492 return std::make_pair(Size, Zero);
493
494 Arg = dyn_cast<ConstantInt>(CS.getArgument(FnData->SndParam));
495 if (!Arg)
496 return unknown();
497
Nuno Lopes034dd6c2012-06-21 16:47:58 +0000498 Size *= Arg->getValue().zextOrSelf(IntTyBits);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000499 return std::make_pair(Size, Zero);
500
501 // TODO: handle more standard functions (+ wchar cousins):
Nuno Lopes6e699bf2012-07-25 18:49:28 +0000502 // - strdup / strndup
Nuno Lopes9e72a792012-06-21 15:45:28 +0000503 // - strcpy / strncpy
504 // - strcat / strncat
505 // - memcpy / memmove
Nuno Lopes6e699bf2012-07-25 18:49:28 +0000506 // - strcat / strncat
Nuno Lopes9e72a792012-06-21 15:45:28 +0000507 // - memset
508}
509
510SizeOffsetType
511ObjectSizeOffsetVisitor::visitConstantPointerNull(ConstantPointerNull&) {
512 return std::make_pair(Zero, Zero);
513}
514
515SizeOffsetType
Nuno Lopes41a3f252012-06-28 16:34:03 +0000516ObjectSizeOffsetVisitor::visitExtractElementInst(ExtractElementInst&) {
517 return unknown();
518}
519
520SizeOffsetType
Nuno Lopes9e72a792012-06-21 15:45:28 +0000521ObjectSizeOffsetVisitor::visitExtractValueInst(ExtractValueInst&) {
522 // Easy cases were already folded by previous passes.
523 return unknown();
524}
525
526SizeOffsetType ObjectSizeOffsetVisitor::visitGEPOperator(GEPOperator &GEP) {
527 SizeOffsetType PtrData = compute(GEP.getPointerOperand());
Nuno Lopes98281a22012-12-30 16:25:48 +0000528 APInt Offset(IntTyBits, 0);
529 if (!bothKnown(PtrData) || !GEP.accumulateConstantOffset(*TD, Offset))
Nuno Lopes9e72a792012-06-21 15:45:28 +0000530 return unknown();
531
Nuno Lopes9e72a792012-06-21 15:45:28 +0000532 return std::make_pair(PtrData.first, PtrData.second + Offset);
533}
534
Nuno Lopes41be2fb2012-12-31 16:23:48 +0000535SizeOffsetType ObjectSizeOffsetVisitor::visitGlobalAlias(GlobalAlias &GA) {
536 if (GA.mayBeOverridden())
537 return unknown();
538 return compute(GA.getAliasee());
539}
540
Nuno Lopes9e72a792012-06-21 15:45:28 +0000541SizeOffsetType ObjectSizeOffsetVisitor::visitGlobalVariable(GlobalVariable &GV){
542 if (!GV.hasDefinitiveInitializer())
543 return unknown();
544
545 APInt Size(IntTyBits, TD->getTypeAllocSize(GV.getType()->getElementType()));
546 return std::make_pair(align(Size, GV.getAlignment()), Zero);
547}
548
549SizeOffsetType ObjectSizeOffsetVisitor::visitIntToPtrInst(IntToPtrInst&) {
550 // clueless
551 return unknown();
552}
553
554SizeOffsetType ObjectSizeOffsetVisitor::visitLoadInst(LoadInst&) {
555 ++ObjectVisitorLoad;
556 return unknown();
557}
558
Nadav Rotem8e4df482013-04-09 18:16:05 +0000559SizeOffsetType ObjectSizeOffsetVisitor::visitPHINode(PHINode&) {
560 // too complex to analyze statically.
561 return unknown();
Nuno Lopes9e72a792012-06-21 15:45:28 +0000562}
563
564SizeOffsetType ObjectSizeOffsetVisitor::visitSelectInst(SelectInst &I) {
565 SizeOffsetType TrueSide = compute(I.getTrueValue());
566 SizeOffsetType FalseSide = compute(I.getFalseValue());
Nuno Lopes2e594fa2012-12-31 18:01:36 +0000567 if (bothKnown(TrueSide) && bothKnown(FalseSide) && TrueSide == FalseSide)
Nuno Lopes9e72a792012-06-21 15:45:28 +0000568 return TrueSide;
569 return unknown();
570}
571
572SizeOffsetType ObjectSizeOffsetVisitor::visitUndefValue(UndefValue&) {
573 return std::make_pair(Zero, Zero);
574}
575
576SizeOffsetType ObjectSizeOffsetVisitor::visitInstruction(Instruction &I) {
577 DEBUG(dbgs() << "ObjectSizeOffsetVisitor unknown instruction:" << I << '\n');
578 return unknown();
579}
580
581
Micah Villmow3574eca2012-10-08 16:38:25 +0000582ObjectSizeOffsetEvaluator::ObjectSizeOffsetEvaluator(const DataLayout *TD,
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000583 const TargetLibraryInfo *TLI,
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000584 LLVMContext &Context)
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000585: TD(TD), TLI(TLI), Context(Context), Builder(Context, TargetFolder(TD)) {
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000586 IntTy = TD->getIntPtrType(Context);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000587 Zero = ConstantInt::get(IntTy, 0);
588}
589
590SizeOffsetEvalType ObjectSizeOffsetEvaluator::compute(Value *V) {
591 SizeOffsetEvalType Result = compute_(V);
592
593 if (!bothKnown(Result)) {
594 // erase everything that was computed in this iteration from the cache, so
595 // that no dangling references are left behind. We could be a bit smarter if
596 // we kept a dependency graph. It's probably not worth the complexity.
597 for (PtrSetTy::iterator I=SeenVals.begin(), E=SeenVals.end(); I != E; ++I) {
598 CacheMapTy::iterator CacheIt = CacheMap.find(*I);
599 // non-computable results can be safely cached
600 if (CacheIt != CacheMap.end() && anyKnown(CacheIt->second))
601 CacheMap.erase(CacheIt);
602 }
603 }
604
605 SeenVals.clear();
606 return Result;
607}
608
609SizeOffsetEvalType ObjectSizeOffsetEvaluator::compute_(Value *V) {
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000610 ObjectSizeOffsetVisitor Visitor(TD, TLI, Context);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000611 SizeOffsetType Const = Visitor.compute(V);
612 if (Visitor.bothKnown(Const))
613 return std::make_pair(ConstantInt::get(Context, Const.first),
614 ConstantInt::get(Context, Const.second));
615
616 V = V->stripPointerCasts();
617
618 // check cache
619 CacheMapTy::iterator CacheIt = CacheMap.find(V);
620 if (CacheIt != CacheMap.end())
621 return CacheIt->second;
622
623 // always generate code immediately before the instruction being
624 // processed, so that the generated code dominates the same BBs
625 Instruction *PrevInsertPoint = Builder.GetInsertPoint();
626 if (Instruction *I = dyn_cast<Instruction>(V))
627 Builder.SetInsertPoint(I);
628
629 // record the pointers that were handled in this run, so that they can be
630 // cleaned later if something fails
631 SeenVals.insert(V);
632
633 // now compute the size and offset
634 SizeOffsetEvalType Result;
635 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
636 Result = visitGEPOperator(*GEP);
637 } else if (Instruction *I = dyn_cast<Instruction>(V)) {
638 Result = visit(*I);
639 } else if (isa<Argument>(V) ||
640 (isa<ConstantExpr>(V) &&
641 cast<ConstantExpr>(V)->getOpcode() == Instruction::IntToPtr) ||
Nuno Lopes41be2fb2012-12-31 16:23:48 +0000642 isa<GlobalAlias>(V) ||
Nuno Lopes9e72a792012-06-21 15:45:28 +0000643 isa<GlobalVariable>(V)) {
644 // ignore values where we cannot do more than what ObjectSizeVisitor can
645 Result = unknown();
646 } else {
647 DEBUG(dbgs() << "ObjectSizeOffsetEvaluator::compute() unhandled value: "
648 << *V << '\n');
649 Result = unknown();
650 }
651
652 if (PrevInsertPoint)
653 Builder.SetInsertPoint(PrevInsertPoint);
654
655 // Don't reuse CacheIt since it may be invalid at this point.
656 CacheMap[V] = Result;
657 return Result;
658}
659
660SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitAllocaInst(AllocaInst &I) {
661 if (!I.getAllocatedType()->isSized())
662 return unknown();
663
664 // must be a VLA
665 assert(I.isArrayAllocation());
666 Value *ArraySize = I.getArraySize();
667 Value *Size = ConstantInt::get(ArraySize->getType(),
668 TD->getTypeAllocSize(I.getAllocatedType()));
669 Size = Builder.CreateMul(Size, ArraySize);
670 return std::make_pair(Size, Zero);
671}
672
673SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitCallSite(CallSite CS) {
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000674 const AllocFnsTy *FnData = getAllocationData(CS.getInstruction(), AnyAlloc,
675 TLI);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000676 if (!FnData)
677 return unknown();
678
679 // handle strdup-like functions separately
680 if (FnData->AllocTy == StrDupLike) {
Nuno Lopes6e699bf2012-07-25 18:49:28 +0000681 // TODO
682 return unknown();
Nuno Lopes9e72a792012-06-21 15:45:28 +0000683 }
684
Nuno Lopes034dd6c2012-06-21 16:47:58 +0000685 Value *FirstArg = CS.getArgument(FnData->FstParam);
686 FirstArg = Builder.CreateZExt(FirstArg, IntTy);
Nuno Lopesef22f042012-06-21 18:38:26 +0000687 if (FnData->SndParam < 0)
Nuno Lopes9e72a792012-06-21 15:45:28 +0000688 return std::make_pair(FirstArg, Zero);
689
690 Value *SecondArg = CS.getArgument(FnData->SndParam);
Nuno Lopes034dd6c2012-06-21 16:47:58 +0000691 SecondArg = Builder.CreateZExt(SecondArg, IntTy);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000692 Value *Size = Builder.CreateMul(FirstArg, SecondArg);
693 return std::make_pair(Size, Zero);
694
695 // TODO: handle more standard functions (+ wchar cousins):
Nuno Lopes6e699bf2012-07-25 18:49:28 +0000696 // - strdup / strndup
Nuno Lopes9e72a792012-06-21 15:45:28 +0000697 // - strcpy / strncpy
698 // - strcat / strncat
699 // - memcpy / memmove
Nuno Lopes6e699bf2012-07-25 18:49:28 +0000700 // - strcat / strncat
Nuno Lopes9e72a792012-06-21 15:45:28 +0000701 // - memset
702}
703
704SizeOffsetEvalType
Nuno Lopes41a3f252012-06-28 16:34:03 +0000705ObjectSizeOffsetEvaluator::visitExtractElementInst(ExtractElementInst&) {
706 return unknown();
707}
708
709SizeOffsetEvalType
710ObjectSizeOffsetEvaluator::visitExtractValueInst(ExtractValueInst&) {
711 return unknown();
712}
713
714SizeOffsetEvalType
Nuno Lopes9e72a792012-06-21 15:45:28 +0000715ObjectSizeOffsetEvaluator::visitGEPOperator(GEPOperator &GEP) {
716 SizeOffsetEvalType PtrData = compute_(GEP.getPointerOperand());
717 if (!bothKnown(PtrData))
718 return unknown();
719
Nuno Lopesc606c3f2012-07-20 23:07:40 +0000720 Value *Offset = EmitGEPOffset(&Builder, *TD, &GEP, /*NoAssumptions=*/true);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000721 Offset = Builder.CreateAdd(PtrData.second, Offset);
722 return std::make_pair(PtrData.first, Offset);
723}
724
725SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitIntToPtrInst(IntToPtrInst&) {
726 // clueless
727 return unknown();
728}
729
730SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitLoadInst(LoadInst&) {
731 return unknown();
732}
733
734SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitPHINode(PHINode &PHI) {
735 // create 2 PHIs: one for size and another for offset
736 PHINode *SizePHI = Builder.CreatePHI(IntTy, PHI.getNumIncomingValues());
737 PHINode *OffsetPHI = Builder.CreatePHI(IntTy, PHI.getNumIncomingValues());
738
739 // insert right away in the cache to handle recursive PHIs
740 CacheMap[&PHI] = std::make_pair(SizePHI, OffsetPHI);
741
742 // compute offset/size for each PHI incoming pointer
743 for (unsigned i = 0, e = PHI.getNumIncomingValues(); i != e; ++i) {
744 Builder.SetInsertPoint(PHI.getIncomingBlock(i)->getFirstInsertionPt());
745 SizeOffsetEvalType EdgeData = compute_(PHI.getIncomingValue(i));
746
747 if (!bothKnown(EdgeData)) {
748 OffsetPHI->replaceAllUsesWith(UndefValue::get(IntTy));
749 OffsetPHI->eraseFromParent();
750 SizePHI->replaceAllUsesWith(UndefValue::get(IntTy));
751 SizePHI->eraseFromParent();
752 return unknown();
753 }
754 SizePHI->addIncoming(EdgeData.first, PHI.getIncomingBlock(i));
755 OffsetPHI->addIncoming(EdgeData.second, PHI.getIncomingBlock(i));
756 }
Nuno Lopes0dff5322012-07-03 17:13:25 +0000757
758 Value *Size = SizePHI, *Offset = OffsetPHI, *Tmp;
759 if ((Tmp = SizePHI->hasConstantValue())) {
760 Size = Tmp;
761 SizePHI->replaceAllUsesWith(Size);
762 SizePHI->eraseFromParent();
763 }
764 if ((Tmp = OffsetPHI->hasConstantValue())) {
765 Offset = Tmp;
766 OffsetPHI->replaceAllUsesWith(Offset);
767 OffsetPHI->eraseFromParent();
768 }
769 return std::make_pair(Size, Offset);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000770}
771
772SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitSelectInst(SelectInst &I) {
773 SizeOffsetEvalType TrueSide = compute_(I.getTrueValue());
774 SizeOffsetEvalType FalseSide = compute_(I.getFalseValue());
775
776 if (!bothKnown(TrueSide) || !bothKnown(FalseSide))
777 return unknown();
778 if (TrueSide == FalseSide)
779 return TrueSide;
780
781 Value *Size = Builder.CreateSelect(I.getCondition(), TrueSide.first,
782 FalseSide.first);
783 Value *Offset = Builder.CreateSelect(I.getCondition(), TrueSide.second,
784 FalseSide.second);
785 return std::make_pair(Size, Offset);
786}
787
788SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitInstruction(Instruction &I) {
789 DEBUG(dbgs() << "ObjectSizeOffsetEvaluator unknown instruction:" << I <<'\n');
790 return unknown();
791}