blob: d490d5419f75e802eb62689cbacbd7e4ccbce298 [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},
55 {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)
63 {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
Nuno Lopes2bc689c2013-03-02 11:23:34 +0000367/// \brief Compute the size of the underlying object pointed by Ptr. Returns
368/// true and the object size in Size if successful, and false otherwise.
369/// If RoundToAlign is true, then Size is rounded up to the aligment of allocas,
370/// byval arguments, and global variables.
371bool llvm::getUnderlyingObjectSize(const Value *Ptr, uint64_t &Size,
372 const DataLayout *TD,
373 const TargetLibraryInfo *TLI,
374 bool RoundToAlign) {
375 if (!TD)
376 return false;
377
378 ObjectSizeOffsetVisitor Visitor(TD, TLI, Ptr->getContext(), RoundToAlign);
379 SizeOffsetType Data = Visitor.compute(const_cast<Value*>(Ptr));
380 if (!Visitor.knownSize(Data))
381 return false;
382
383 Size = Data.first.getZExtValue();
384 return true;
385}
386
Nuno Lopes9e72a792012-06-21 15:45:28 +0000387
388STATISTIC(ObjectVisitorArgument,
389 "Number of arguments with unsolved size and offset");
390STATISTIC(ObjectVisitorLoad,
391 "Number of load instructions with unsolved size and offset");
392
393
394APInt ObjectSizeOffsetVisitor::align(APInt Size, uint64_t Align) {
395 if (RoundToAlign && Align)
396 return APInt(IntTyBits, RoundUpToAlignment(Size.getZExtValue(), Align));
397 return Size;
398}
399
Micah Villmow3574eca2012-10-08 16:38:25 +0000400ObjectSizeOffsetVisitor::ObjectSizeOffsetVisitor(const DataLayout *TD,
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000401 const TargetLibraryInfo *TLI,
Nuno Lopes9e72a792012-06-21 15:45:28 +0000402 LLVMContext &Context,
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000403 bool RoundToAlign)
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000404: TD(TD), TLI(TLI), RoundToAlign(RoundToAlign) {
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000405 IntegerType *IntTy = TD->getIntPtrType(Context);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000406 IntTyBits = IntTy->getBitWidth();
407 Zero = APInt::getNullValue(IntTyBits);
408}
409
410SizeOffsetType ObjectSizeOffsetVisitor::compute(Value *V) {
411 V = V->stripPointerCasts();
Nuno Lopes0a9ff4c2012-12-31 20:45:10 +0000412
Nuno Lopesb443a0a2013-03-02 11:36:24 +0000413 if (isa<Instruction>(V) || isa<GEPOperator>(V)) {
414 // Return cached value or insert unknown in cache if size of V was not
415 // computed yet in order to avoid recursions in PHis.
416 std::pair<CacheMapTy::iterator, bool> CacheVal =
417 CacheMap.insert(std::make_pair(V, unknown()));
418 if (!CacheVal.second)
419 return CacheVal.first->second;
420
421 SizeOffsetType Result;
Benjamin Kramer168843c2012-08-17 19:26:41 +0000422 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V))
Nuno Lopesb443a0a2013-03-02 11:36:24 +0000423 Result = visitGEPOperator(*GEP);
424 else
425 Result = visit(cast<Instruction>(*V));
426 return CacheMap[V] = Result;
Benjamin Kramer168843c2012-08-17 19:26:41 +0000427 }
Nuno Lopesb443a0a2013-03-02 11:36:24 +0000428
Nuno Lopes9e72a792012-06-21 15:45:28 +0000429 if (Argument *A = dyn_cast<Argument>(V))
430 return visitArgument(*A);
431 if (ConstantPointerNull *P = dyn_cast<ConstantPointerNull>(V))
432 return visitConstantPointerNull(*P);
Nuno Lopes41be2fb2012-12-31 16:23:48 +0000433 if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
434 return visitGlobalAlias(*GA);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000435 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
436 return visitGlobalVariable(*GV);
437 if (UndefValue *UV = dyn_cast<UndefValue>(V))
438 return visitUndefValue(*UV);
Benjamin Kramer168843c2012-08-17 19:26:41 +0000439 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Nuno Lopes9e72a792012-06-21 15:45:28 +0000440 if (CE->getOpcode() == Instruction::IntToPtr)
441 return unknown(); // clueless
Benjamin Kramer168843c2012-08-17 19:26:41 +0000442 }
Nuno Lopes9e72a792012-06-21 15:45:28 +0000443
444 DEBUG(dbgs() << "ObjectSizeOffsetVisitor::compute() unhandled value: " << *V
445 << '\n');
446 return unknown();
447}
448
449SizeOffsetType ObjectSizeOffsetVisitor::visitAllocaInst(AllocaInst &I) {
450 if (!I.getAllocatedType()->isSized())
451 return unknown();
452
453 APInt Size(IntTyBits, TD->getTypeAllocSize(I.getAllocatedType()));
454 if (!I.isArrayAllocation())
455 return std::make_pair(align(Size, I.getAlignment()), Zero);
456
457 Value *ArraySize = I.getArraySize();
458 if (const ConstantInt *C = dyn_cast<ConstantInt>(ArraySize)) {
459 Size *= C->getValue().zextOrSelf(IntTyBits);
460 return std::make_pair(align(Size, I.getAlignment()), Zero);
461 }
462 return unknown();
463}
464
465SizeOffsetType ObjectSizeOffsetVisitor::visitArgument(Argument &A) {
466 // no interprocedural analysis is done at the moment
467 if (!A.hasByValAttr()) {
468 ++ObjectVisitorArgument;
469 return unknown();
470 }
471 PointerType *PT = cast<PointerType>(A.getType());
472 APInt Size(IntTyBits, TD->getTypeAllocSize(PT->getElementType()));
473 return std::make_pair(align(Size, A.getParamAlignment()), Zero);
474}
475
476SizeOffsetType ObjectSizeOffsetVisitor::visitCallSite(CallSite CS) {
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000477 const AllocFnsTy *FnData = getAllocationData(CS.getInstruction(), AnyAlloc,
478 TLI);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000479 if (!FnData)
480 return unknown();
481
482 // handle strdup-like functions separately
483 if (FnData->AllocTy == StrDupLike) {
Nuno Lopes9827c8e2012-07-24 16:28:13 +0000484 APInt Size(IntTyBits, GetStringLength(CS.getArgument(0)));
485 if (!Size)
486 return unknown();
487
488 // strndup limits strlen
489 if (FnData->FstParam > 0) {
490 ConstantInt *Arg= dyn_cast<ConstantInt>(CS.getArgument(FnData->FstParam));
491 if (!Arg)
492 return unknown();
493
494 APInt MaxSize = Arg->getValue().zextOrSelf(IntTyBits);
495 if (Size.ugt(MaxSize))
496 Size = MaxSize + 1;
497 }
498 return std::make_pair(Size, Zero);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000499 }
500
501 ConstantInt *Arg = dyn_cast<ConstantInt>(CS.getArgument(FnData->FstParam));
502 if (!Arg)
503 return unknown();
504
Nuno Lopes034dd6c2012-06-21 16:47:58 +0000505 APInt Size = Arg->getValue().zextOrSelf(IntTyBits);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000506 // size determined by just 1 parameter
Nuno Lopesef22f042012-06-21 18:38:26 +0000507 if (FnData->SndParam < 0)
Nuno Lopes9e72a792012-06-21 15:45:28 +0000508 return std::make_pair(Size, Zero);
509
510 Arg = dyn_cast<ConstantInt>(CS.getArgument(FnData->SndParam));
511 if (!Arg)
512 return unknown();
513
Nuno Lopes034dd6c2012-06-21 16:47:58 +0000514 Size *= Arg->getValue().zextOrSelf(IntTyBits);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000515 return std::make_pair(Size, Zero);
516
517 // TODO: handle more standard functions (+ wchar cousins):
Nuno Lopes6e699bf2012-07-25 18:49:28 +0000518 // - strdup / strndup
Nuno Lopes9e72a792012-06-21 15:45:28 +0000519 // - strcpy / strncpy
520 // - strcat / strncat
521 // - memcpy / memmove
Nuno Lopes6e699bf2012-07-25 18:49:28 +0000522 // - strcat / strncat
Nuno Lopes9e72a792012-06-21 15:45:28 +0000523 // - memset
524}
525
526SizeOffsetType
527ObjectSizeOffsetVisitor::visitConstantPointerNull(ConstantPointerNull&) {
528 return std::make_pair(Zero, Zero);
529}
530
531SizeOffsetType
Nuno Lopes41a3f252012-06-28 16:34:03 +0000532ObjectSizeOffsetVisitor::visitExtractElementInst(ExtractElementInst&) {
533 return unknown();
534}
535
536SizeOffsetType
Nuno Lopes9e72a792012-06-21 15:45:28 +0000537ObjectSizeOffsetVisitor::visitExtractValueInst(ExtractValueInst&) {
538 // Easy cases were already folded by previous passes.
539 return unknown();
540}
541
542SizeOffsetType ObjectSizeOffsetVisitor::visitGEPOperator(GEPOperator &GEP) {
543 SizeOffsetType PtrData = compute(GEP.getPointerOperand());
Nuno Lopes98281a22012-12-30 16:25:48 +0000544 APInt Offset(IntTyBits, 0);
545 if (!bothKnown(PtrData) || !GEP.accumulateConstantOffset(*TD, Offset))
Nuno Lopes9e72a792012-06-21 15:45:28 +0000546 return unknown();
547
Nuno Lopes9e72a792012-06-21 15:45:28 +0000548 return std::make_pair(PtrData.first, PtrData.second + Offset);
549}
550
Nuno Lopes41be2fb2012-12-31 16:23:48 +0000551SizeOffsetType ObjectSizeOffsetVisitor::visitGlobalAlias(GlobalAlias &GA) {
552 if (GA.mayBeOverridden())
553 return unknown();
554 return compute(GA.getAliasee());
555}
556
Nuno Lopes9e72a792012-06-21 15:45:28 +0000557SizeOffsetType ObjectSizeOffsetVisitor::visitGlobalVariable(GlobalVariable &GV){
558 if (!GV.hasDefinitiveInitializer())
559 return unknown();
560
561 APInt Size(IntTyBits, TD->getTypeAllocSize(GV.getType()->getElementType()));
562 return std::make_pair(align(Size, GV.getAlignment()), Zero);
563}
564
565SizeOffsetType ObjectSizeOffsetVisitor::visitIntToPtrInst(IntToPtrInst&) {
566 // clueless
567 return unknown();
568}
569
570SizeOffsetType ObjectSizeOffsetVisitor::visitLoadInst(LoadInst&) {
571 ++ObjectVisitorLoad;
572 return unknown();
573}
574
Nuno Lopesb443a0a2013-03-02 11:36:24 +0000575SizeOffsetType ObjectSizeOffsetVisitor::visitPHINode(PHINode &PHI) {
576 if (PHI.getNumIncomingValues() == 0)
577 return unknown();
578
579 SizeOffsetType Ret = compute(PHI.getIncomingValue(0));
580 if (!bothKnown(Ret))
581 return unknown();
582
583 // Verify that all PHI incoming pointers have the same size and offset.
584 for (unsigned i = 1, e = PHI.getNumIncomingValues(); i != e; ++i) {
585 SizeOffsetType EdgeData = compute(PHI.getIncomingValue(i));
586 if (!bothKnown(EdgeData) || EdgeData != Ret)
587 return unknown();
588 }
589 return Ret;
Nuno Lopes9e72a792012-06-21 15:45:28 +0000590}
591
592SizeOffsetType ObjectSizeOffsetVisitor::visitSelectInst(SelectInst &I) {
593 SizeOffsetType TrueSide = compute(I.getTrueValue());
594 SizeOffsetType FalseSide = compute(I.getFalseValue());
Nuno Lopes2e594fa2012-12-31 18:01:36 +0000595 if (bothKnown(TrueSide) && bothKnown(FalseSide) && TrueSide == FalseSide)
Nuno Lopes9e72a792012-06-21 15:45:28 +0000596 return TrueSide;
597 return unknown();
598}
599
600SizeOffsetType ObjectSizeOffsetVisitor::visitUndefValue(UndefValue&) {
601 return std::make_pair(Zero, Zero);
602}
603
604SizeOffsetType ObjectSizeOffsetVisitor::visitInstruction(Instruction &I) {
605 DEBUG(dbgs() << "ObjectSizeOffsetVisitor unknown instruction:" << I << '\n');
606 return unknown();
607}
608
609
Micah Villmow3574eca2012-10-08 16:38:25 +0000610ObjectSizeOffsetEvaluator::ObjectSizeOffsetEvaluator(const DataLayout *TD,
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000611 const TargetLibraryInfo *TLI,
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000612 LLVMContext &Context)
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000613: TD(TD), TLI(TLI), Context(Context), Builder(Context, TargetFolder(TD)) {
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000614 IntTy = TD->getIntPtrType(Context);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000615 Zero = ConstantInt::get(IntTy, 0);
616}
617
618SizeOffsetEvalType ObjectSizeOffsetEvaluator::compute(Value *V) {
619 SizeOffsetEvalType Result = compute_(V);
620
621 if (!bothKnown(Result)) {
622 // erase everything that was computed in this iteration from the cache, so
623 // that no dangling references are left behind. We could be a bit smarter if
624 // we kept a dependency graph. It's probably not worth the complexity.
625 for (PtrSetTy::iterator I=SeenVals.begin(), E=SeenVals.end(); I != E; ++I) {
626 CacheMapTy::iterator CacheIt = CacheMap.find(*I);
627 // non-computable results can be safely cached
628 if (CacheIt != CacheMap.end() && anyKnown(CacheIt->second))
629 CacheMap.erase(CacheIt);
630 }
631 }
632
633 SeenVals.clear();
634 return Result;
635}
636
637SizeOffsetEvalType ObjectSizeOffsetEvaluator::compute_(Value *V) {
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000638 ObjectSizeOffsetVisitor Visitor(TD, TLI, Context);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000639 SizeOffsetType Const = Visitor.compute(V);
640 if (Visitor.bothKnown(Const))
641 return std::make_pair(ConstantInt::get(Context, Const.first),
642 ConstantInt::get(Context, Const.second));
643
644 V = V->stripPointerCasts();
645
646 // check cache
647 CacheMapTy::iterator CacheIt = CacheMap.find(V);
648 if (CacheIt != CacheMap.end())
649 return CacheIt->second;
650
651 // always generate code immediately before the instruction being
652 // processed, so that the generated code dominates the same BBs
653 Instruction *PrevInsertPoint = Builder.GetInsertPoint();
654 if (Instruction *I = dyn_cast<Instruction>(V))
655 Builder.SetInsertPoint(I);
656
657 // record the pointers that were handled in this run, so that they can be
658 // cleaned later if something fails
659 SeenVals.insert(V);
660
661 // now compute the size and offset
662 SizeOffsetEvalType Result;
663 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
664 Result = visitGEPOperator(*GEP);
665 } else if (Instruction *I = dyn_cast<Instruction>(V)) {
666 Result = visit(*I);
667 } else if (isa<Argument>(V) ||
668 (isa<ConstantExpr>(V) &&
669 cast<ConstantExpr>(V)->getOpcode() == Instruction::IntToPtr) ||
Nuno Lopes41be2fb2012-12-31 16:23:48 +0000670 isa<GlobalAlias>(V) ||
Nuno Lopes9e72a792012-06-21 15:45:28 +0000671 isa<GlobalVariable>(V)) {
672 // ignore values where we cannot do more than what ObjectSizeVisitor can
673 Result = unknown();
674 } else {
675 DEBUG(dbgs() << "ObjectSizeOffsetEvaluator::compute() unhandled value: "
676 << *V << '\n');
677 Result = unknown();
678 }
679
680 if (PrevInsertPoint)
681 Builder.SetInsertPoint(PrevInsertPoint);
682
683 // Don't reuse CacheIt since it may be invalid at this point.
684 CacheMap[V] = Result;
685 return Result;
686}
687
688SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitAllocaInst(AllocaInst &I) {
689 if (!I.getAllocatedType()->isSized())
690 return unknown();
691
692 // must be a VLA
693 assert(I.isArrayAllocation());
694 Value *ArraySize = I.getArraySize();
695 Value *Size = ConstantInt::get(ArraySize->getType(),
696 TD->getTypeAllocSize(I.getAllocatedType()));
697 Size = Builder.CreateMul(Size, ArraySize);
698 return std::make_pair(Size, Zero);
699}
700
701SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitCallSite(CallSite CS) {
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000702 const AllocFnsTy *FnData = getAllocationData(CS.getInstruction(), AnyAlloc,
703 TLI);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000704 if (!FnData)
705 return unknown();
706
707 // handle strdup-like functions separately
708 if (FnData->AllocTy == StrDupLike) {
Nuno Lopes6e699bf2012-07-25 18:49:28 +0000709 // TODO
710 return unknown();
Nuno Lopes9e72a792012-06-21 15:45:28 +0000711 }
712
Nuno Lopes034dd6c2012-06-21 16:47:58 +0000713 Value *FirstArg = CS.getArgument(FnData->FstParam);
714 FirstArg = Builder.CreateZExt(FirstArg, IntTy);
Nuno Lopesef22f042012-06-21 18:38:26 +0000715 if (FnData->SndParam < 0)
Nuno Lopes9e72a792012-06-21 15:45:28 +0000716 return std::make_pair(FirstArg, Zero);
717
718 Value *SecondArg = CS.getArgument(FnData->SndParam);
Nuno Lopes034dd6c2012-06-21 16:47:58 +0000719 SecondArg = Builder.CreateZExt(SecondArg, IntTy);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000720 Value *Size = Builder.CreateMul(FirstArg, SecondArg);
721 return std::make_pair(Size, Zero);
722
723 // TODO: handle more standard functions (+ wchar cousins):
Nuno Lopes6e699bf2012-07-25 18:49:28 +0000724 // - strdup / strndup
Nuno Lopes9e72a792012-06-21 15:45:28 +0000725 // - strcpy / strncpy
726 // - strcat / strncat
727 // - memcpy / memmove
Nuno Lopes6e699bf2012-07-25 18:49:28 +0000728 // - strcat / strncat
Nuno Lopes9e72a792012-06-21 15:45:28 +0000729 // - memset
730}
731
732SizeOffsetEvalType
Nuno Lopes41a3f252012-06-28 16:34:03 +0000733ObjectSizeOffsetEvaluator::visitExtractElementInst(ExtractElementInst&) {
734 return unknown();
735}
736
737SizeOffsetEvalType
738ObjectSizeOffsetEvaluator::visitExtractValueInst(ExtractValueInst&) {
739 return unknown();
740}
741
742SizeOffsetEvalType
Nuno Lopes9e72a792012-06-21 15:45:28 +0000743ObjectSizeOffsetEvaluator::visitGEPOperator(GEPOperator &GEP) {
744 SizeOffsetEvalType PtrData = compute_(GEP.getPointerOperand());
745 if (!bothKnown(PtrData))
746 return unknown();
747
Nuno Lopesc606c3f2012-07-20 23:07:40 +0000748 Value *Offset = EmitGEPOffset(&Builder, *TD, &GEP, /*NoAssumptions=*/true);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000749 Offset = Builder.CreateAdd(PtrData.second, Offset);
750 return std::make_pair(PtrData.first, Offset);
751}
752
753SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitIntToPtrInst(IntToPtrInst&) {
754 // clueless
755 return unknown();
756}
757
758SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitLoadInst(LoadInst&) {
759 return unknown();
760}
761
762SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitPHINode(PHINode &PHI) {
763 // create 2 PHIs: one for size and another for offset
764 PHINode *SizePHI = Builder.CreatePHI(IntTy, PHI.getNumIncomingValues());
765 PHINode *OffsetPHI = Builder.CreatePHI(IntTy, PHI.getNumIncomingValues());
766
767 // insert right away in the cache to handle recursive PHIs
768 CacheMap[&PHI] = std::make_pair(SizePHI, OffsetPHI);
769
770 // compute offset/size for each PHI incoming pointer
771 for (unsigned i = 0, e = PHI.getNumIncomingValues(); i != e; ++i) {
772 Builder.SetInsertPoint(PHI.getIncomingBlock(i)->getFirstInsertionPt());
773 SizeOffsetEvalType EdgeData = compute_(PHI.getIncomingValue(i));
774
775 if (!bothKnown(EdgeData)) {
776 OffsetPHI->replaceAllUsesWith(UndefValue::get(IntTy));
777 OffsetPHI->eraseFromParent();
778 SizePHI->replaceAllUsesWith(UndefValue::get(IntTy));
779 SizePHI->eraseFromParent();
780 return unknown();
781 }
782 SizePHI->addIncoming(EdgeData.first, PHI.getIncomingBlock(i));
783 OffsetPHI->addIncoming(EdgeData.second, PHI.getIncomingBlock(i));
784 }
Nuno Lopes0dff5322012-07-03 17:13:25 +0000785
786 Value *Size = SizePHI, *Offset = OffsetPHI, *Tmp;
787 if ((Tmp = SizePHI->hasConstantValue())) {
788 Size = Tmp;
789 SizePHI->replaceAllUsesWith(Size);
790 SizePHI->eraseFromParent();
791 }
792 if ((Tmp = OffsetPHI->hasConstantValue())) {
793 Offset = Tmp;
794 OffsetPHI->replaceAllUsesWith(Offset);
795 OffsetPHI->eraseFromParent();
796 }
797 return std::make_pair(Size, Offset);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000798}
799
800SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitSelectInst(SelectInst &I) {
801 SizeOffsetEvalType TrueSide = compute_(I.getTrueValue());
802 SizeOffsetEvalType FalseSide = compute_(I.getFalseValue());
803
804 if (!bothKnown(TrueSide) || !bothKnown(FalseSide))
805 return unknown();
806 if (TrueSide == FalseSide)
807 return TrueSide;
808
809 Value *Size = Builder.CreateSelect(I.getCondition(), TrueSide.first,
810 FalseSide.first);
811 Value *Offset = Builder.CreateSelect(I.getCondition(), TrueSide.second,
812 FalseSide.second);
813 return std::make_pair(Size, Offset);
814}
815
816SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitInstruction(Instruction &I) {
817 DEBUG(dbgs() << "ObjectSizeOffsetEvaluator unknown instruction:" << I <<'\n');
818 return unknown();
819}