blob: 9c0d8ac6a3e2c16bbe78207cdfa997911345acb1 [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
Nuno Lopes2b3e9582012-06-21 21:25:05 +000080 Function *Callee = CS.getCalledFunction();
Nuno Lopes9e72a792012-06-21 15:45:28 +000081 if (!Callee || !Callee->isDeclaration())
82 return 0;
83 return Callee;
84}
Evan Chengfabcb912009-09-10 04:36:43 +000085
Nuno Lopes9e72a792012-06-21 15:45:28 +000086/// \brief Returns the allocation data for the given value if it is a call to a
87/// known allocation function, and NULL otherwise.
88static const AllocFnsTy *getAllocationData(const Value *V, AllocType AllocTy,
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +000089 const TargetLibraryInfo *TLI,
Nuno Lopes9e72a792012-06-21 15:45:28 +000090 bool LookThroughBitCast = false) {
Michael Ilsemancacf9712013-03-08 21:15:00 +000091 // Skip intrinsics
92 if (isa<IntrinsicInst>(V))
93 return 0;
94
Nuno Lopes9e72a792012-06-21 15:45:28 +000095 Function *Callee = getCalledFunction(V, LookThroughBitCast);
96 if (!Callee)
97 return 0;
98
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +000099 // Make sure that the function is available.
100 StringRef FnName = Callee->getName();
101 LibFunc::Func TLIFn;
102 if (!TLI || !TLI->getLibFunc(FnName, TLIFn) || !TLI->has(TLIFn))
103 return 0;
104
Nuno Lopes9e72a792012-06-21 15:45:28 +0000105 unsigned i = 0;
106 bool found = false;
107 for ( ; i < array_lengthof(AllocationFnData); ++i) {
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000108 if (AllocationFnData[i].Func == TLIFn) {
Nuno Lopes9e72a792012-06-21 15:45:28 +0000109 found = true;
110 break;
111 }
112 }
113 if (!found)
114 return 0;
115
116 const AllocFnsTy *FnData = &AllocationFnData[i];
117 if ((FnData->AllocTy & AllocTy) == 0)
118 return 0;
119
120 // Check function prototype.
Nuno Lopesef22f042012-06-21 18:38:26 +0000121 int FstParam = FnData->FstParam;
122 int SndParam = FnData->SndParam;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000123 FunctionType *FTy = Callee->getFunctionType();
Nuno Lopes9e72a792012-06-21 15:45:28 +0000124
125 if (FTy->getReturnType() == Type::getInt8PtrTy(FTy->getContext()) &&
126 FTy->getNumParams() == FnData->NumParams &&
Nuno Lopesef22f042012-06-21 18:38:26 +0000127 (FstParam < 0 ||
Nuno Lopes9e72a792012-06-21 15:45:28 +0000128 (FTy->getParamType(FstParam)->isIntegerTy(32) ||
129 FTy->getParamType(FstParam)->isIntegerTy(64))) &&
Nuno Lopesef22f042012-06-21 18:38:26 +0000130 (SndParam < 0 ||
Nuno Lopes9e72a792012-06-21 15:45:28 +0000131 FTy->getParamType(SndParam)->isIntegerTy(32) ||
132 FTy->getParamType(SndParam)->isIntegerTy(64)))
133 return FnData;
134 return 0;
135}
136
137static bool hasNoAliasAttr(const Value *V, bool LookThroughBitCast) {
Nuno Lopese8742d02012-06-25 16:17:54 +0000138 ImmutableCallSite CS(LookThroughBitCast ? V->stripPointerCasts() : V);
Bill Wendling034b94b2012-12-19 07:18:57 +0000139 return CS && CS.hasFnAttr(Attribute::NoAlias);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000140}
141
142
Nuno Lopes2b3e9582012-06-21 21:25:05 +0000143/// \brief Tests if a value is a call or invoke to a library function that
144/// allocates or reallocates memory (either malloc, calloc, realloc, or strdup
145/// like).
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000146bool llvm::isAllocationFn(const Value *V, const TargetLibraryInfo *TLI,
147 bool LookThroughBitCast) {
148 return getAllocationData(V, AnyAlloc, TLI, LookThroughBitCast);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000149}
150
Nuno Lopes2b3e9582012-06-21 21:25:05 +0000151/// \brief Tests if a value is a call or invoke to a function that returns a
Nuno Lopes41a3f252012-06-28 16:34:03 +0000152/// NoAlias pointer (including malloc/calloc/realloc/strdup-like functions).
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000153bool llvm::isNoAliasFn(const Value *V, const TargetLibraryInfo *TLI,
154 bool LookThroughBitCast) {
Nuno Lopes41a3f252012-06-28 16:34:03 +0000155 // it's safe to consider realloc as noalias since accessing the original
156 // pointer is undefined behavior
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000157 return isAllocationFn(V, TLI, LookThroughBitCast) ||
Nuno Lopes9e72a792012-06-21 15:45:28 +0000158 hasNoAliasAttr(V, LookThroughBitCast);
159}
160
Nuno Lopes2b3e9582012-06-21 21:25:05 +0000161/// \brief Tests if a value is a call or invoke to a library function that
162/// allocates uninitialized memory (such as malloc).
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000163bool llvm::isMallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
164 bool LookThroughBitCast) {
165 return getAllocationData(V, MallocLike, TLI, LookThroughBitCast);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000166}
167
Nuno Lopes2b3e9582012-06-21 21:25:05 +0000168/// \brief Tests if a value is a call or invoke to a library function that
169/// allocates zero-filled memory (such as calloc).
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000170bool llvm::isCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
171 bool LookThroughBitCast) {
172 return getAllocationData(V, CallocLike, TLI, LookThroughBitCast);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000173}
174
Nuno Lopes2b3e9582012-06-21 21:25:05 +0000175/// \brief Tests if a value is a call or invoke to a library function that
176/// allocates memory (either malloc, calloc, or strdup like).
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000177bool llvm::isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
178 bool LookThroughBitCast) {
179 return getAllocationData(V, AllocLike, TLI, LookThroughBitCast);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000180}
181
Nuno Lopes2b3e9582012-06-21 21:25:05 +0000182/// \brief Tests if a value is a call or invoke to a library function that
183/// reallocates memory (such as realloc).
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000184bool llvm::isReallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
185 bool LookThroughBitCast) {
186 return getAllocationData(V, ReallocLike, TLI, LookThroughBitCast);
Evan Chengfabcb912009-09-10 04:36:43 +0000187}
188
189/// extractMallocCall - Returns the corresponding CallInst if the instruction
190/// is a malloc call. Since CallInst::CreateMalloc() only creates calls, we
191/// ignore InvokeInst here.
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000192const CallInst *llvm::extractMallocCall(const Value *I,
193 const TargetLibraryInfo *TLI) {
194 return isMallocLikeFn(I, TLI) ? dyn_cast<CallInst>(I) : 0;
Evan Chengfabcb912009-09-10 04:36:43 +0000195}
196
Micah Villmow3574eca2012-10-08 16:38:25 +0000197static Value *computeArraySize(const CallInst *CI, const DataLayout *TD,
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000198 const TargetLibraryInfo *TLI,
Victor Hernandez8e345a12009-11-10 08:32:25 +0000199 bool LookThroughSExt = false) {
Evan Chengfabcb912009-09-10 04:36:43 +0000200 if (!CI)
Jakub Staszaka9cd5162013-03-07 20:01:47 +0000201 return 0;
Evan Chengfabcb912009-09-10 04:36:43 +0000202
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000203 // The size of the malloc's result type must be known to determine array size.
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000204 Type *T = getMallocAllocatedType(CI, TLI);
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000205 if (!T || !T->isSized() || !TD)
Jakub Staszaka9cd5162013-03-07 20:01:47 +0000206 return 0;
Victor Hernandez88d98392009-09-18 19:20:02 +0000207
Victor Hernandez8e345a12009-11-10 08:32:25 +0000208 unsigned ElementSize = TD->getTypeAllocSize(T);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000209 if (StructType *ST = dyn_cast<StructType>(T))
Victor Hernandez8e345a12009-11-10 08:32:25 +0000210 ElementSize = TD->getStructLayout(ST)->getSizeInBytes();
Victor Hernandez88d98392009-09-18 19:20:02 +0000211
Gabor Greife3401c42010-06-23 21:41:47 +0000212 // If malloc call's arg can be determined to be a multiple of ElementSize,
Victor Hernandez8e345a12009-11-10 08:32:25 +0000213 // return the multiple. Otherwise, return NULL.
Gabor Greife3401c42010-06-23 21:41:47 +0000214 Value *MallocArg = CI->getArgOperand(0);
Jakub Staszaka9cd5162013-03-07 20:01:47 +0000215 Value *Multiple = 0;
Victor Hernandez8e345a12009-11-10 08:32:25 +0000216 if (ComputeMultiple(MallocArg, ElementSize, Multiple,
Dan Gohman3dbb9e62009-11-18 00:58:27 +0000217 LookThroughSExt))
Victor Hernandez8e345a12009-11-10 08:32:25 +0000218 return Multiple;
Victor Hernandez88d98392009-09-18 19:20:02 +0000219
Jakub Staszaka9cd5162013-03-07 20:01:47 +0000220 return 0;
Evan Chengfabcb912009-09-10 04:36:43 +0000221}
222
Michael Ilseman9333ffb2013-03-08 21:03:09 +0000223/// isArrayMalloc - Returns the corresponding CallInst if the instruction
Victor Hernandez90f48e72009-10-28 20:18:55 +0000224/// is a call to malloc whose array size can be determined and the array size
225/// is not constant 1. Otherwise, return NULL.
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000226const CallInst *llvm::isArrayMalloc(const Value *I,
Micah Villmow3574eca2012-10-08 16:38:25 +0000227 const DataLayout *TD,
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000228 const TargetLibraryInfo *TLI) {
229 const CallInst *CI = extractMallocCall(I, TLI);
230 Value *ArraySize = computeArraySize(CI, TD, TLI);
Victor Hernandez90f48e72009-10-28 20:18:55 +0000231
Jakub Staszakb1b6c172013-03-07 20:22:39 +0000232 if (ConstantInt *ConstSize = dyn_cast_or_null<ConstantInt>(ArraySize))
233 if (ConstSize->isOne())
234 return CI;
Victor Hernandez90f48e72009-10-28 20:18:55 +0000235
236 // 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 +0000237 return 0;
Evan Chengfabcb912009-09-10 04:36:43 +0000238}
239
240/// getMallocType - Returns the PointerType resulting from the malloc call.
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000241/// The PointerType depends on the number of bitcast uses of the malloc call:
242/// 0: PointerType is the calls' return type.
243/// 1: PointerType is the bitcast's result type.
244/// >1: Unique PointerType cannot be determined, return NULL.
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000245PointerType *llvm::getMallocType(const CallInst *CI,
246 const TargetLibraryInfo *TLI) {
247 assert(isMallocLikeFn(CI, TLI) && "getMallocType and not malloc call");
Michael Ilseman9333ffb2013-03-08 21:03:09 +0000248
Jakub Staszaka9cd5162013-03-07 20:01:47 +0000249 PointerType *MallocType = 0;
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000250 unsigned NumOfBitCastUses = 0;
251
Victor Hernandez88d98392009-09-18 19:20:02 +0000252 // Determine if CallInst has a bitcast use.
Gabor Greif60ad7812010-03-25 23:06:16 +0000253 for (Value::const_use_iterator UI = CI->use_begin(), E = CI->use_end();
Victor Hernandez88d98392009-09-18 19:20:02 +0000254 UI != E; )
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000255 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(*UI++)) {
256 MallocType = cast<PointerType>(BCI->getDestTy());
257 NumOfBitCastUses++;
258 }
Evan Chengfabcb912009-09-10 04:36:43 +0000259
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000260 // Malloc call has 1 bitcast use, so type is the bitcast's destination type.
261 if (NumOfBitCastUses == 1)
262 return MallocType;
Evan Chengfabcb912009-09-10 04:36:43 +0000263
Victor Hernandez60cfc032009-09-22 18:50:03 +0000264 // Malloc call was not bitcast, so type is the malloc function's return type.
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000265 if (NumOfBitCastUses == 0)
Victor Hernandez88d98392009-09-18 19:20:02 +0000266 return cast<PointerType>(CI->getType());
267
268 // Type could not be determined.
Jakub Staszaka9cd5162013-03-07 20:01:47 +0000269 return 0;
Evan Chengfabcb912009-09-10 04:36:43 +0000270}
271
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000272/// getMallocAllocatedType - Returns the Type allocated by malloc call.
273/// The Type depends on the number of bitcast uses of the malloc call:
274/// 0: PointerType is the malloc calls' return type.
275/// 1: PointerType is the bitcast's result type.
276/// >1: Unique PointerType cannot be determined, return NULL.
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000277Type *llvm::getMallocAllocatedType(const CallInst *CI,
278 const TargetLibraryInfo *TLI) {
279 PointerType *PT = getMallocType(CI, TLI);
Jakub Staszaka9cd5162013-03-07 20:01:47 +0000280 return PT ? PT->getElementType() : 0;
Evan Chengfabcb912009-09-10 04:36:43 +0000281}
282
Michael Ilseman9333ffb2013-03-08 21:03:09 +0000283/// getMallocArraySize - Returns the array size of a malloc call. If the
Victor Hernandez90f48e72009-10-28 20:18:55 +0000284/// argument passed to malloc is a multiple of the size of the malloced type,
285/// then return that multiple. For non-array mallocs, the multiple is
286/// constant 1. Otherwise, return NULL for mallocs whose array size cannot be
Victor Hernandez2491ce02009-10-15 20:14:52 +0000287/// determined.
Micah Villmow3574eca2012-10-08 16:38:25 +0000288Value *llvm::getMallocArraySize(CallInst *CI, const DataLayout *TD,
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000289 const TargetLibraryInfo *TLI,
Victor Hernandez8e345a12009-11-10 08:32:25 +0000290 bool LookThroughSExt) {
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000291 assert(isMallocLikeFn(CI, TLI) && "getMallocArraySize and not malloc call");
292 return computeArraySize(CI, TD, TLI, LookThroughSExt);
Evan Chengfabcb912009-09-10 04:36:43 +0000293}
Victor Hernandez66284e02009-10-24 04:23:03 +0000294
Nuno Lopes252ef562012-05-03 21:19:58 +0000295
Nuno Lopes252ef562012-05-03 21:19:58 +0000296/// extractCallocCall - Returns the corresponding CallInst if the instruction
297/// is a calloc call.
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000298const CallInst *llvm::extractCallocCall(const Value *I,
299 const TargetLibraryInfo *TLI) {
300 return isCallocLikeFn(I, TLI) ? cast<CallInst>(I) : 0;
Nuno Lopes252ef562012-05-03 21:19:58 +0000301}
302
Victor Hernandez046e78c2009-10-26 23:43:48 +0000303
Gabor Greif02680f92010-06-23 21:51:12 +0000304/// isFreeCall - Returns non-null if the value is a call to the builtin free()
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000305const CallInst *llvm::isFreeCall(const Value *I, const TargetLibraryInfo *TLI) {
Victor Hernandez66284e02009-10-24 04:23:03 +0000306 const CallInst *CI = dyn_cast<CallInst>(I);
Michael Ilsemancacf9712013-03-08 21:15:00 +0000307 if (!CI || isa<IntrinsicInst>(CI))
Gabor Greif02680f92010-06-23 21:51:12 +0000308 return 0;
Victor Hernandez3ad70d52009-11-03 20:39:35 +0000309 Function *Callee = CI->getCalledFunction();
Nick Lewycky42e72ca2011-03-15 07:31:32 +0000310 if (Callee == 0 || !Callee->isDeclaration())
311 return 0;
312
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000313 StringRef FnName = Callee->getName();
314 LibFunc::Func TLIFn;
315 if (!TLI || !TLI->getLibFunc(FnName, TLIFn) || !TLI->has(TLIFn))
316 return 0;
317
318 if (TLIFn != LibFunc::free &&
319 TLIFn != LibFunc::ZdlPv && // operator delete(void*)
320 TLIFn != LibFunc::ZdaPv) // operator delete[](void*)
Gabor Greif02680f92010-06-23 21:51:12 +0000321 return 0;
Victor Hernandez66284e02009-10-24 04:23:03 +0000322
323 // Check free prototype.
Michael Ilseman9333ffb2013-03-08 21:03:09 +0000324 // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin
Victor Hernandez66284e02009-10-24 04:23:03 +0000325 // attribute will exist.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000326 FunctionType *FTy = Callee->getFunctionType();
Victor Hernandez3ad70d52009-11-03 20:39:35 +0000327 if (!FTy->getReturnType()->isVoidTy())
Gabor Greif02680f92010-06-23 21:51:12 +0000328 return 0;
Victor Hernandez66284e02009-10-24 04:23:03 +0000329 if (FTy->getNumParams() != 1)
Gabor Greif02680f92010-06-23 21:51:12 +0000330 return 0;
Chris Lattnerebb21892011-06-18 21:46:23 +0000331 if (FTy->getParamType(0) != Type::getInt8PtrTy(Callee->getContext()))
Gabor Greif02680f92010-06-23 21:51:12 +0000332 return 0;
Victor Hernandez66284e02009-10-24 04:23:03 +0000333
Gabor Greif02680f92010-06-23 21:51:12 +0000334 return CI;
Victor Hernandez66284e02009-10-24 04:23:03 +0000335}
Nuno Lopes9e72a792012-06-21 15:45:28 +0000336
337
338
339//===----------------------------------------------------------------------===//
340// Utility functions to compute size of objects.
341//
342
343
344/// \brief Compute the size of the object pointed by Ptr. Returns true and the
345/// object size in Size if successful, and false otherwise.
346/// If RoundToAlign is true, then Size is rounded up to the aligment of allocas,
347/// byval arguments, and global variables.
Micah Villmow3574eca2012-10-08 16:38:25 +0000348bool llvm::getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout *TD,
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000349 const TargetLibraryInfo *TLI, bool RoundToAlign) {
Nuno Lopes9e72a792012-06-21 15:45:28 +0000350 if (!TD)
351 return false;
352
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000353 ObjectSizeOffsetVisitor Visitor(TD, TLI, Ptr->getContext(), RoundToAlign);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000354 SizeOffsetType Data = Visitor.compute(const_cast<Value*>(Ptr));
355 if (!Visitor.bothKnown(Data))
356 return false;
357
358 APInt ObjSize = Data.first, Offset = Data.second;
359 // check for overflow
360 if (Offset.slt(0) || ObjSize.ult(Offset))
361 Size = 0;
362 else
363 Size = (ObjSize - Offset).getZExtValue();
364 return true;
365}
366
367
368STATISTIC(ObjectVisitorArgument,
369 "Number of arguments with unsolved size and offset");
370STATISTIC(ObjectVisitorLoad,
371 "Number of load instructions with unsolved size and offset");
372
373
374APInt ObjectSizeOffsetVisitor::align(APInt Size, uint64_t Align) {
375 if (RoundToAlign && Align)
376 return APInt(IntTyBits, RoundUpToAlignment(Size.getZExtValue(), Align));
377 return Size;
378}
379
Micah Villmow3574eca2012-10-08 16:38:25 +0000380ObjectSizeOffsetVisitor::ObjectSizeOffsetVisitor(const DataLayout *TD,
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000381 const TargetLibraryInfo *TLI,
Nuno Lopes9e72a792012-06-21 15:45:28 +0000382 LLVMContext &Context,
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000383 bool RoundToAlign)
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000384: TD(TD), TLI(TLI), RoundToAlign(RoundToAlign) {
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000385 IntegerType *IntTy = TD->getIntPtrType(Context);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000386 IntTyBits = IntTy->getBitWidth();
387 Zero = APInt::getNullValue(IntTyBits);
388}
389
390SizeOffsetType ObjectSizeOffsetVisitor::compute(Value *V) {
391 V = V->stripPointerCasts();
Nadav Rotem8e4df482013-04-09 18:16:05 +0000392 if (Instruction *I = dyn_cast<Instruction>(V)) {
393 // If we have already seen this instruction, bail out. Cycles can happen in
394 // unreachable code after constant propagation.
395 if (!SeenInsts.insert(I))
396 return unknown();
Nuno Lopes0a9ff4c2012-12-31 20:45:10 +0000397
Benjamin Kramer168843c2012-08-17 19:26:41 +0000398 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V))
Nadav Rotem8e4df482013-04-09 18:16:05 +0000399 return visitGEPOperator(*GEP);
400 return visit(*I);
Benjamin Kramer168843c2012-08-17 19:26:41 +0000401 }
Nuno Lopes9e72a792012-06-21 15:45:28 +0000402 if (Argument *A = dyn_cast<Argument>(V))
403 return visitArgument(*A);
404 if (ConstantPointerNull *P = dyn_cast<ConstantPointerNull>(V))
405 return visitConstantPointerNull(*P);
Nuno Lopes41be2fb2012-12-31 16:23:48 +0000406 if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
407 return visitGlobalAlias(*GA);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000408 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
409 return visitGlobalVariable(*GV);
410 if (UndefValue *UV = dyn_cast<UndefValue>(V))
411 return visitUndefValue(*UV);
Benjamin Kramer168843c2012-08-17 19:26:41 +0000412 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Nuno Lopes9e72a792012-06-21 15:45:28 +0000413 if (CE->getOpcode() == Instruction::IntToPtr)
414 return unknown(); // clueless
Nadav Rotem8e4df482013-04-09 18:16:05 +0000415 if (CE->getOpcode() == Instruction::GetElementPtr)
416 return visitGEPOperator(cast<GEPOperator>(*CE));
Benjamin Kramer168843c2012-08-17 19:26:41 +0000417 }
Nuno Lopes9e72a792012-06-21 15:45:28 +0000418
419 DEBUG(dbgs() << "ObjectSizeOffsetVisitor::compute() unhandled value: " << *V
420 << '\n');
421 return unknown();
422}
423
424SizeOffsetType ObjectSizeOffsetVisitor::visitAllocaInst(AllocaInst &I) {
425 if (!I.getAllocatedType()->isSized())
426 return unknown();
427
428 APInt Size(IntTyBits, TD->getTypeAllocSize(I.getAllocatedType()));
429 if (!I.isArrayAllocation())
430 return std::make_pair(align(Size, I.getAlignment()), Zero);
431
432 Value *ArraySize = I.getArraySize();
433 if (const ConstantInt *C = dyn_cast<ConstantInt>(ArraySize)) {
434 Size *= C->getValue().zextOrSelf(IntTyBits);
435 return std::make_pair(align(Size, I.getAlignment()), Zero);
436 }
437 return unknown();
438}
439
440SizeOffsetType ObjectSizeOffsetVisitor::visitArgument(Argument &A) {
441 // no interprocedural analysis is done at the moment
442 if (!A.hasByValAttr()) {
443 ++ObjectVisitorArgument;
444 return unknown();
445 }
446 PointerType *PT = cast<PointerType>(A.getType());
447 APInt Size(IntTyBits, TD->getTypeAllocSize(PT->getElementType()));
448 return std::make_pair(align(Size, A.getParamAlignment()), Zero);
449}
450
451SizeOffsetType ObjectSizeOffsetVisitor::visitCallSite(CallSite CS) {
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000452 const AllocFnsTy *FnData = getAllocationData(CS.getInstruction(), AnyAlloc,
453 TLI);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000454 if (!FnData)
455 return unknown();
456
457 // handle strdup-like functions separately
458 if (FnData->AllocTy == StrDupLike) {
Nuno Lopes9827c8e2012-07-24 16:28:13 +0000459 APInt Size(IntTyBits, GetStringLength(CS.getArgument(0)));
460 if (!Size)
461 return unknown();
462
463 // strndup limits strlen
464 if (FnData->FstParam > 0) {
465 ConstantInt *Arg= dyn_cast<ConstantInt>(CS.getArgument(FnData->FstParam));
466 if (!Arg)
467 return unknown();
468
469 APInt MaxSize = Arg->getValue().zextOrSelf(IntTyBits);
470 if (Size.ugt(MaxSize))
471 Size = MaxSize + 1;
472 }
473 return std::make_pair(Size, Zero);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000474 }
475
476 ConstantInt *Arg = dyn_cast<ConstantInt>(CS.getArgument(FnData->FstParam));
477 if (!Arg)
478 return unknown();
479
Nuno Lopes034dd6c2012-06-21 16:47:58 +0000480 APInt Size = Arg->getValue().zextOrSelf(IntTyBits);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000481 // size determined by just 1 parameter
Nuno Lopesef22f042012-06-21 18:38:26 +0000482 if (FnData->SndParam < 0)
Nuno Lopes9e72a792012-06-21 15:45:28 +0000483 return std::make_pair(Size, Zero);
484
485 Arg = dyn_cast<ConstantInt>(CS.getArgument(FnData->SndParam));
486 if (!Arg)
487 return unknown();
488
Nuno Lopes034dd6c2012-06-21 16:47:58 +0000489 Size *= Arg->getValue().zextOrSelf(IntTyBits);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000490 return std::make_pair(Size, Zero);
491
492 // TODO: handle more standard functions (+ wchar cousins):
Nuno Lopes6e699bf2012-07-25 18:49:28 +0000493 // - strdup / strndup
Nuno Lopes9e72a792012-06-21 15:45:28 +0000494 // - strcpy / strncpy
495 // - strcat / strncat
496 // - memcpy / memmove
Nuno Lopes6e699bf2012-07-25 18:49:28 +0000497 // - strcat / strncat
Nuno Lopes9e72a792012-06-21 15:45:28 +0000498 // - memset
499}
500
501SizeOffsetType
502ObjectSizeOffsetVisitor::visitConstantPointerNull(ConstantPointerNull&) {
503 return std::make_pair(Zero, Zero);
504}
505
506SizeOffsetType
Nuno Lopes41a3f252012-06-28 16:34:03 +0000507ObjectSizeOffsetVisitor::visitExtractElementInst(ExtractElementInst&) {
508 return unknown();
509}
510
511SizeOffsetType
Nuno Lopes9e72a792012-06-21 15:45:28 +0000512ObjectSizeOffsetVisitor::visitExtractValueInst(ExtractValueInst&) {
513 // Easy cases were already folded by previous passes.
514 return unknown();
515}
516
517SizeOffsetType ObjectSizeOffsetVisitor::visitGEPOperator(GEPOperator &GEP) {
518 SizeOffsetType PtrData = compute(GEP.getPointerOperand());
Nuno Lopes98281a22012-12-30 16:25:48 +0000519 APInt Offset(IntTyBits, 0);
520 if (!bothKnown(PtrData) || !GEP.accumulateConstantOffset(*TD, Offset))
Nuno Lopes9e72a792012-06-21 15:45:28 +0000521 return unknown();
522
Nuno Lopes9e72a792012-06-21 15:45:28 +0000523 return std::make_pair(PtrData.first, PtrData.second + Offset);
524}
525
Nuno Lopes41be2fb2012-12-31 16:23:48 +0000526SizeOffsetType ObjectSizeOffsetVisitor::visitGlobalAlias(GlobalAlias &GA) {
527 if (GA.mayBeOverridden())
528 return unknown();
529 return compute(GA.getAliasee());
530}
531
Nuno Lopes9e72a792012-06-21 15:45:28 +0000532SizeOffsetType ObjectSizeOffsetVisitor::visitGlobalVariable(GlobalVariable &GV){
533 if (!GV.hasDefinitiveInitializer())
534 return unknown();
535
536 APInt Size(IntTyBits, TD->getTypeAllocSize(GV.getType()->getElementType()));
537 return std::make_pair(align(Size, GV.getAlignment()), Zero);
538}
539
540SizeOffsetType ObjectSizeOffsetVisitor::visitIntToPtrInst(IntToPtrInst&) {
541 // clueless
542 return unknown();
543}
544
545SizeOffsetType ObjectSizeOffsetVisitor::visitLoadInst(LoadInst&) {
546 ++ObjectVisitorLoad;
547 return unknown();
548}
549
Nadav Rotem8e4df482013-04-09 18:16:05 +0000550SizeOffsetType ObjectSizeOffsetVisitor::visitPHINode(PHINode&) {
551 // too complex to analyze statically.
552 return unknown();
Nuno Lopes9e72a792012-06-21 15:45:28 +0000553}
554
555SizeOffsetType ObjectSizeOffsetVisitor::visitSelectInst(SelectInst &I) {
556 SizeOffsetType TrueSide = compute(I.getTrueValue());
557 SizeOffsetType FalseSide = compute(I.getFalseValue());
Nuno Lopes2e594fa2012-12-31 18:01:36 +0000558 if (bothKnown(TrueSide) && bothKnown(FalseSide) && TrueSide == FalseSide)
Nuno Lopes9e72a792012-06-21 15:45:28 +0000559 return TrueSide;
560 return unknown();
561}
562
563SizeOffsetType ObjectSizeOffsetVisitor::visitUndefValue(UndefValue&) {
564 return std::make_pair(Zero, Zero);
565}
566
567SizeOffsetType ObjectSizeOffsetVisitor::visitInstruction(Instruction &I) {
568 DEBUG(dbgs() << "ObjectSizeOffsetVisitor unknown instruction:" << I << '\n');
569 return unknown();
570}
571
572
Micah Villmow3574eca2012-10-08 16:38:25 +0000573ObjectSizeOffsetEvaluator::ObjectSizeOffsetEvaluator(const DataLayout *TD,
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000574 const TargetLibraryInfo *TLI,
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000575 LLVMContext &Context)
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000576: TD(TD), TLI(TLI), Context(Context), Builder(Context, TargetFolder(TD)) {
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000577 IntTy = TD->getIntPtrType(Context);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000578 Zero = ConstantInt::get(IntTy, 0);
579}
580
581SizeOffsetEvalType ObjectSizeOffsetEvaluator::compute(Value *V) {
582 SizeOffsetEvalType Result = compute_(V);
583
584 if (!bothKnown(Result)) {
585 // erase everything that was computed in this iteration from the cache, so
586 // that no dangling references are left behind. We could be a bit smarter if
587 // we kept a dependency graph. It's probably not worth the complexity.
588 for (PtrSetTy::iterator I=SeenVals.begin(), E=SeenVals.end(); I != E; ++I) {
589 CacheMapTy::iterator CacheIt = CacheMap.find(*I);
590 // non-computable results can be safely cached
591 if (CacheIt != CacheMap.end() && anyKnown(CacheIt->second))
592 CacheMap.erase(CacheIt);
593 }
594 }
595
596 SeenVals.clear();
597 return Result;
598}
599
600SizeOffsetEvalType ObjectSizeOffsetEvaluator::compute_(Value *V) {
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000601 ObjectSizeOffsetVisitor Visitor(TD, TLI, Context);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000602 SizeOffsetType Const = Visitor.compute(V);
603 if (Visitor.bothKnown(Const))
604 return std::make_pair(ConstantInt::get(Context, Const.first),
605 ConstantInt::get(Context, Const.second));
606
607 V = V->stripPointerCasts();
608
609 // check cache
610 CacheMapTy::iterator CacheIt = CacheMap.find(V);
611 if (CacheIt != CacheMap.end())
612 return CacheIt->second;
613
614 // always generate code immediately before the instruction being
615 // processed, so that the generated code dominates the same BBs
616 Instruction *PrevInsertPoint = Builder.GetInsertPoint();
617 if (Instruction *I = dyn_cast<Instruction>(V))
618 Builder.SetInsertPoint(I);
619
620 // record the pointers that were handled in this run, so that they can be
621 // cleaned later if something fails
622 SeenVals.insert(V);
623
624 // now compute the size and offset
625 SizeOffsetEvalType Result;
626 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
627 Result = visitGEPOperator(*GEP);
628 } else if (Instruction *I = dyn_cast<Instruction>(V)) {
629 Result = visit(*I);
630 } else if (isa<Argument>(V) ||
631 (isa<ConstantExpr>(V) &&
632 cast<ConstantExpr>(V)->getOpcode() == Instruction::IntToPtr) ||
Nuno Lopes41be2fb2012-12-31 16:23:48 +0000633 isa<GlobalAlias>(V) ||
Nuno Lopes9e72a792012-06-21 15:45:28 +0000634 isa<GlobalVariable>(V)) {
635 // ignore values where we cannot do more than what ObjectSizeVisitor can
636 Result = unknown();
637 } else {
638 DEBUG(dbgs() << "ObjectSizeOffsetEvaluator::compute() unhandled value: "
639 << *V << '\n');
640 Result = unknown();
641 }
642
643 if (PrevInsertPoint)
644 Builder.SetInsertPoint(PrevInsertPoint);
645
646 // Don't reuse CacheIt since it may be invalid at this point.
647 CacheMap[V] = Result;
648 return Result;
649}
650
651SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitAllocaInst(AllocaInst &I) {
652 if (!I.getAllocatedType()->isSized())
653 return unknown();
654
655 // must be a VLA
656 assert(I.isArrayAllocation());
657 Value *ArraySize = I.getArraySize();
658 Value *Size = ConstantInt::get(ArraySize->getType(),
659 TD->getTypeAllocSize(I.getAllocatedType()));
660 Size = Builder.CreateMul(Size, ArraySize);
661 return std::make_pair(Size, Zero);
662}
663
664SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitCallSite(CallSite CS) {
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000665 const AllocFnsTy *FnData = getAllocationData(CS.getInstruction(), AnyAlloc,
666 TLI);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000667 if (!FnData)
668 return unknown();
669
670 // handle strdup-like functions separately
671 if (FnData->AllocTy == StrDupLike) {
Nuno Lopes6e699bf2012-07-25 18:49:28 +0000672 // TODO
673 return unknown();
Nuno Lopes9e72a792012-06-21 15:45:28 +0000674 }
675
Nuno Lopes034dd6c2012-06-21 16:47:58 +0000676 Value *FirstArg = CS.getArgument(FnData->FstParam);
677 FirstArg = Builder.CreateZExt(FirstArg, IntTy);
Nuno Lopesef22f042012-06-21 18:38:26 +0000678 if (FnData->SndParam < 0)
Nuno Lopes9e72a792012-06-21 15:45:28 +0000679 return std::make_pair(FirstArg, Zero);
680
681 Value *SecondArg = CS.getArgument(FnData->SndParam);
Nuno Lopes034dd6c2012-06-21 16:47:58 +0000682 SecondArg = Builder.CreateZExt(SecondArg, IntTy);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000683 Value *Size = Builder.CreateMul(FirstArg, SecondArg);
684 return std::make_pair(Size, Zero);
685
686 // TODO: handle more standard functions (+ wchar cousins):
Nuno Lopes6e699bf2012-07-25 18:49:28 +0000687 // - strdup / strndup
Nuno Lopes9e72a792012-06-21 15:45:28 +0000688 // - strcpy / strncpy
689 // - strcat / strncat
690 // - memcpy / memmove
Nuno Lopes6e699bf2012-07-25 18:49:28 +0000691 // - strcat / strncat
Nuno Lopes9e72a792012-06-21 15:45:28 +0000692 // - memset
693}
694
695SizeOffsetEvalType
Nuno Lopes41a3f252012-06-28 16:34:03 +0000696ObjectSizeOffsetEvaluator::visitExtractElementInst(ExtractElementInst&) {
697 return unknown();
698}
699
700SizeOffsetEvalType
701ObjectSizeOffsetEvaluator::visitExtractValueInst(ExtractValueInst&) {
702 return unknown();
703}
704
705SizeOffsetEvalType
Nuno Lopes9e72a792012-06-21 15:45:28 +0000706ObjectSizeOffsetEvaluator::visitGEPOperator(GEPOperator &GEP) {
707 SizeOffsetEvalType PtrData = compute_(GEP.getPointerOperand());
708 if (!bothKnown(PtrData))
709 return unknown();
710
Nuno Lopesc606c3f2012-07-20 23:07:40 +0000711 Value *Offset = EmitGEPOffset(&Builder, *TD, &GEP, /*NoAssumptions=*/true);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000712 Offset = Builder.CreateAdd(PtrData.second, Offset);
713 return std::make_pair(PtrData.first, Offset);
714}
715
716SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitIntToPtrInst(IntToPtrInst&) {
717 // clueless
718 return unknown();
719}
720
721SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitLoadInst(LoadInst&) {
722 return unknown();
723}
724
725SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitPHINode(PHINode &PHI) {
726 // create 2 PHIs: one for size and another for offset
727 PHINode *SizePHI = Builder.CreatePHI(IntTy, PHI.getNumIncomingValues());
728 PHINode *OffsetPHI = Builder.CreatePHI(IntTy, PHI.getNumIncomingValues());
729
730 // insert right away in the cache to handle recursive PHIs
731 CacheMap[&PHI] = std::make_pair(SizePHI, OffsetPHI);
732
733 // compute offset/size for each PHI incoming pointer
734 for (unsigned i = 0, e = PHI.getNumIncomingValues(); i != e; ++i) {
735 Builder.SetInsertPoint(PHI.getIncomingBlock(i)->getFirstInsertionPt());
736 SizeOffsetEvalType EdgeData = compute_(PHI.getIncomingValue(i));
737
738 if (!bothKnown(EdgeData)) {
739 OffsetPHI->replaceAllUsesWith(UndefValue::get(IntTy));
740 OffsetPHI->eraseFromParent();
741 SizePHI->replaceAllUsesWith(UndefValue::get(IntTy));
742 SizePHI->eraseFromParent();
743 return unknown();
744 }
745 SizePHI->addIncoming(EdgeData.first, PHI.getIncomingBlock(i));
746 OffsetPHI->addIncoming(EdgeData.second, PHI.getIncomingBlock(i));
747 }
Nuno Lopes0dff5322012-07-03 17:13:25 +0000748
749 Value *Size = SizePHI, *Offset = OffsetPHI, *Tmp;
750 if ((Tmp = SizePHI->hasConstantValue())) {
751 Size = Tmp;
752 SizePHI->replaceAllUsesWith(Size);
753 SizePHI->eraseFromParent();
754 }
755 if ((Tmp = OffsetPHI->hasConstantValue())) {
756 Offset = Tmp;
757 OffsetPHI->replaceAllUsesWith(Offset);
758 OffsetPHI->eraseFromParent();
759 }
760 return std::make_pair(Size, Offset);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000761}
762
763SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitSelectInst(SelectInst &I) {
764 SizeOffsetEvalType TrueSide = compute_(I.getTrueValue());
765 SizeOffsetEvalType FalseSide = compute_(I.getFalseValue());
766
767 if (!bothKnown(TrueSide) || !bothKnown(FalseSide))
768 return unknown();
769 if (TrueSide == FalseSide)
770 return TrueSide;
771
772 Value *Size = Builder.CreateSelect(I.getCondition(), TrueSide.first,
773 FalseSide.first);
774 Value *Offset = Builder.CreateSelect(I.getCondition(), TrueSide.second,
775 FalseSide.second);
776 return std::make_pair(Size, Offset);
777}
778
779SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitInstruction(Instruction &I) {
780 DEBUG(dbgs() << "ObjectSizeOffsetEvaluator unknown instruction:" << I <<'\n');
781 return unknown();
782}