blob: 0bf0b47879525150ff808bb80b96629385bc3ba0 [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},
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +000055 {LibFunc::posix_memalign, MallocLike, 3, 2, -1},
56 {LibFunc::calloc, CallocLike, 2, 0, 1},
57 {LibFunc::realloc, ReallocLike, 2, 1, -1},
58 {LibFunc::reallocf, ReallocLike, 2, 1, -1},
59 {LibFunc::strdup, StrDupLike, 1, -1, -1},
60 {LibFunc::strndup, StrDupLike, 2, 1, -1}
Nuno Lopes9e72a792012-06-21 15:45:28 +000061};
62
63
64static Function *getCalledFunction(const Value *V, bool LookThroughBitCast) {
65 if (LookThroughBitCast)
66 V = V->stripPointerCasts();
Nuno Lopes2b3e9582012-06-21 21:25:05 +000067
Nuno Lopesd845c342012-06-22 15:50:53 +000068 CallSite CS(const_cast<Value*>(V));
69 if (!CS.getInstruction())
Nuno Lopes9e72a792012-06-21 15:45:28 +000070 return 0;
Evan Chengfabcb912009-09-10 04:36:43 +000071
Nuno Lopes2b3e9582012-06-21 21:25:05 +000072 Function *Callee = CS.getCalledFunction();
Nuno Lopes9e72a792012-06-21 15:45:28 +000073 if (!Callee || !Callee->isDeclaration())
74 return 0;
75 return Callee;
76}
Evan Chengfabcb912009-09-10 04:36:43 +000077
Nuno Lopes9e72a792012-06-21 15:45:28 +000078/// \brief Returns the allocation data for the given value if it is a call to a
79/// known allocation function, and NULL otherwise.
80static const AllocFnsTy *getAllocationData(const Value *V, AllocType AllocTy,
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +000081 const TargetLibraryInfo *TLI,
Nuno Lopes9e72a792012-06-21 15:45:28 +000082 bool LookThroughBitCast = false) {
Michael Ilsemancacf9712013-03-08 21:15:00 +000083 // Skip intrinsics
84 if (isa<IntrinsicInst>(V))
85 return 0;
86
Nuno Lopes9e72a792012-06-21 15:45:28 +000087 Function *Callee = getCalledFunction(V, LookThroughBitCast);
88 if (!Callee)
89 return 0;
90
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +000091 // Make sure that the function is available.
92 StringRef FnName = Callee->getName();
93 LibFunc::Func TLIFn;
94 if (!TLI || !TLI->getLibFunc(FnName, TLIFn) || !TLI->has(TLIFn))
95 return 0;
96
Nuno Lopes9e72a792012-06-21 15:45:28 +000097 unsigned i = 0;
98 bool found = false;
99 for ( ; i < array_lengthof(AllocationFnData); ++i) {
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000100 if (AllocationFnData[i].Func == TLIFn) {
Nuno Lopes9e72a792012-06-21 15:45:28 +0000101 found = true;
102 break;
103 }
104 }
105 if (!found)
106 return 0;
107
108 const AllocFnsTy *FnData = &AllocationFnData[i];
109 if ((FnData->AllocTy & AllocTy) == 0)
110 return 0;
111
112 // Check function prototype.
Nuno Lopesef22f042012-06-21 18:38:26 +0000113 int FstParam = FnData->FstParam;
114 int SndParam = FnData->SndParam;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000115 FunctionType *FTy = Callee->getFunctionType();
Nuno Lopes9e72a792012-06-21 15:45:28 +0000116
117 if (FTy->getReturnType() == Type::getInt8PtrTy(FTy->getContext()) &&
118 FTy->getNumParams() == FnData->NumParams &&
Nuno Lopesef22f042012-06-21 18:38:26 +0000119 (FstParam < 0 ||
Nuno Lopes9e72a792012-06-21 15:45:28 +0000120 (FTy->getParamType(FstParam)->isIntegerTy(32) ||
121 FTy->getParamType(FstParam)->isIntegerTy(64))) &&
Nuno Lopesef22f042012-06-21 18:38:26 +0000122 (SndParam < 0 ||
Nuno Lopes9e72a792012-06-21 15:45:28 +0000123 FTy->getParamType(SndParam)->isIntegerTy(32) ||
124 FTy->getParamType(SndParam)->isIntegerTy(64)))
125 return FnData;
126 return 0;
127}
128
129static bool hasNoAliasAttr(const Value *V, bool LookThroughBitCast) {
Nuno Lopese8742d02012-06-25 16:17:54 +0000130 ImmutableCallSite CS(LookThroughBitCast ? V->stripPointerCasts() : V);
Bill Wendling034b94b2012-12-19 07:18:57 +0000131 return CS && CS.hasFnAttr(Attribute::NoAlias);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000132}
133
134
Nuno Lopes2b3e9582012-06-21 21:25:05 +0000135/// \brief Tests if a value is a call or invoke to a library function that
136/// allocates or reallocates memory (either malloc, calloc, realloc, or strdup
137/// like).
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000138bool llvm::isAllocationFn(const Value *V, const TargetLibraryInfo *TLI,
139 bool LookThroughBitCast) {
140 return getAllocationData(V, AnyAlloc, TLI, LookThroughBitCast);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000141}
142
Nuno Lopes2b3e9582012-06-21 21:25:05 +0000143/// \brief Tests if a value is a call or invoke to a function that returns a
Nuno Lopes41a3f252012-06-28 16:34:03 +0000144/// NoAlias pointer (including malloc/calloc/realloc/strdup-like functions).
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000145bool llvm::isNoAliasFn(const Value *V, const TargetLibraryInfo *TLI,
146 bool LookThroughBitCast) {
Nuno Lopes41a3f252012-06-28 16:34:03 +0000147 // it's safe to consider realloc as noalias since accessing the original
148 // pointer is undefined behavior
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000149 return isAllocationFn(V, TLI, LookThroughBitCast) ||
Nuno Lopes9e72a792012-06-21 15:45:28 +0000150 hasNoAliasAttr(V, LookThroughBitCast);
151}
152
Nuno Lopes2b3e9582012-06-21 21:25:05 +0000153/// \brief Tests if a value is a call or invoke to a library function that
154/// allocates uninitialized memory (such as malloc).
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000155bool llvm::isMallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
156 bool LookThroughBitCast) {
157 return getAllocationData(V, MallocLike, TLI, LookThroughBitCast);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000158}
159
Nuno Lopes2b3e9582012-06-21 21:25:05 +0000160/// \brief Tests if a value is a call or invoke to a library function that
161/// allocates zero-filled memory (such as calloc).
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000162bool llvm::isCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
163 bool LookThroughBitCast) {
164 return getAllocationData(V, CallocLike, TLI, LookThroughBitCast);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000165}
166
Nuno Lopes2b3e9582012-06-21 21:25:05 +0000167/// \brief Tests if a value is a call or invoke to a library function that
168/// allocates memory (either malloc, calloc, or strdup like).
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000169bool llvm::isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
170 bool LookThroughBitCast) {
171 return getAllocationData(V, AllocLike, TLI, LookThroughBitCast);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000172}
173
Nuno Lopes2b3e9582012-06-21 21:25:05 +0000174/// \brief Tests if a value is a call or invoke to a library function that
175/// reallocates memory (such as realloc).
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000176bool llvm::isReallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
177 bool LookThroughBitCast) {
178 return getAllocationData(V, ReallocLike, TLI, LookThroughBitCast);
Evan Chengfabcb912009-09-10 04:36:43 +0000179}
180
181/// extractMallocCall - Returns the corresponding CallInst if the instruction
182/// is a malloc call. Since CallInst::CreateMalloc() only creates calls, we
183/// ignore InvokeInst here.
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000184const CallInst *llvm::extractMallocCall(const Value *I,
185 const TargetLibraryInfo *TLI) {
186 return isMallocLikeFn(I, TLI) ? dyn_cast<CallInst>(I) : 0;
Evan Chengfabcb912009-09-10 04:36:43 +0000187}
188
Micah Villmow3574eca2012-10-08 16:38:25 +0000189static Value *computeArraySize(const CallInst *CI, const DataLayout *TD,
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000190 const TargetLibraryInfo *TLI,
Victor Hernandez8e345a12009-11-10 08:32:25 +0000191 bool LookThroughSExt = false) {
Evan Chengfabcb912009-09-10 04:36:43 +0000192 if (!CI)
Jakub Staszaka9cd5162013-03-07 20:01:47 +0000193 return 0;
Evan Chengfabcb912009-09-10 04:36:43 +0000194
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000195 // The size of the malloc's result type must be known to determine array size.
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000196 Type *T = getMallocAllocatedType(CI, TLI);
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000197 if (!T || !T->isSized() || !TD)
Jakub Staszaka9cd5162013-03-07 20:01:47 +0000198 return 0;
Victor Hernandez88d98392009-09-18 19:20:02 +0000199
Victor Hernandez8e345a12009-11-10 08:32:25 +0000200 unsigned ElementSize = TD->getTypeAllocSize(T);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000201 if (StructType *ST = dyn_cast<StructType>(T))
Victor Hernandez8e345a12009-11-10 08:32:25 +0000202 ElementSize = TD->getStructLayout(ST)->getSizeInBytes();
Victor Hernandez88d98392009-09-18 19:20:02 +0000203
Gabor Greife3401c42010-06-23 21:41:47 +0000204 // If malloc call's arg can be determined to be a multiple of ElementSize,
Victor Hernandez8e345a12009-11-10 08:32:25 +0000205 // return the multiple. Otherwise, return NULL.
Gabor Greife3401c42010-06-23 21:41:47 +0000206 Value *MallocArg = CI->getArgOperand(0);
Jakub Staszaka9cd5162013-03-07 20:01:47 +0000207 Value *Multiple = 0;
Victor Hernandez8e345a12009-11-10 08:32:25 +0000208 if (ComputeMultiple(MallocArg, ElementSize, Multiple,
Dan Gohman3dbb9e62009-11-18 00:58:27 +0000209 LookThroughSExt))
Victor Hernandez8e345a12009-11-10 08:32:25 +0000210 return Multiple;
Victor Hernandez88d98392009-09-18 19:20:02 +0000211
Jakub Staszaka9cd5162013-03-07 20:01:47 +0000212 return 0;
Evan Chengfabcb912009-09-10 04:36:43 +0000213}
214
Michael Ilseman9333ffb2013-03-08 21:03:09 +0000215/// isArrayMalloc - Returns the corresponding CallInst if the instruction
Victor Hernandez90f48e72009-10-28 20:18:55 +0000216/// is a call to malloc whose array size can be determined and the array size
217/// is not constant 1. Otherwise, return NULL.
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000218const CallInst *llvm::isArrayMalloc(const Value *I,
Micah Villmow3574eca2012-10-08 16:38:25 +0000219 const DataLayout *TD,
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000220 const TargetLibraryInfo *TLI) {
221 const CallInst *CI = extractMallocCall(I, TLI);
222 Value *ArraySize = computeArraySize(CI, TD, TLI);
Victor Hernandez90f48e72009-10-28 20:18:55 +0000223
Jakub Staszakb1b6c172013-03-07 20:22:39 +0000224 if (ConstantInt *ConstSize = dyn_cast_or_null<ConstantInt>(ArraySize))
225 if (ConstSize->isOne())
226 return CI;
Victor Hernandez90f48e72009-10-28 20:18:55 +0000227
228 // 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 +0000229 return 0;
Evan Chengfabcb912009-09-10 04:36:43 +0000230}
231
232/// getMallocType - Returns the PointerType resulting from the malloc call.
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000233/// The PointerType depends on the number of bitcast uses of the malloc call:
234/// 0: PointerType is the calls' return type.
235/// 1: PointerType is the bitcast's result type.
236/// >1: Unique PointerType cannot be determined, return NULL.
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000237PointerType *llvm::getMallocType(const CallInst *CI,
238 const TargetLibraryInfo *TLI) {
239 assert(isMallocLikeFn(CI, TLI) && "getMallocType and not malloc call");
Michael Ilseman9333ffb2013-03-08 21:03:09 +0000240
Jakub Staszaka9cd5162013-03-07 20:01:47 +0000241 PointerType *MallocType = 0;
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000242 unsigned NumOfBitCastUses = 0;
243
Victor Hernandez88d98392009-09-18 19:20:02 +0000244 // Determine if CallInst has a bitcast use.
Gabor Greif60ad7812010-03-25 23:06:16 +0000245 for (Value::const_use_iterator UI = CI->use_begin(), E = CI->use_end();
Victor Hernandez88d98392009-09-18 19:20:02 +0000246 UI != E; )
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000247 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(*UI++)) {
248 MallocType = cast<PointerType>(BCI->getDestTy());
249 NumOfBitCastUses++;
250 }
Evan Chengfabcb912009-09-10 04:36:43 +0000251
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000252 // Malloc call has 1 bitcast use, so type is the bitcast's destination type.
253 if (NumOfBitCastUses == 1)
254 return MallocType;
Evan Chengfabcb912009-09-10 04:36:43 +0000255
Victor Hernandez60cfc032009-09-22 18:50:03 +0000256 // Malloc call was not bitcast, so type is the malloc function's return type.
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000257 if (NumOfBitCastUses == 0)
Victor Hernandez88d98392009-09-18 19:20:02 +0000258 return cast<PointerType>(CI->getType());
259
260 // Type could not be determined.
Jakub Staszaka9cd5162013-03-07 20:01:47 +0000261 return 0;
Evan Chengfabcb912009-09-10 04:36:43 +0000262}
263
Victor Hernandez9d0b7042009-11-07 00:16:28 +0000264/// getMallocAllocatedType - Returns the Type allocated by malloc call.
265/// The Type depends on the number of bitcast uses of the malloc call:
266/// 0: PointerType is the malloc calls' return type.
267/// 1: PointerType is the bitcast's result type.
268/// >1: Unique PointerType cannot be determined, return NULL.
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000269Type *llvm::getMallocAllocatedType(const CallInst *CI,
270 const TargetLibraryInfo *TLI) {
271 PointerType *PT = getMallocType(CI, TLI);
Jakub Staszaka9cd5162013-03-07 20:01:47 +0000272 return PT ? PT->getElementType() : 0;
Evan Chengfabcb912009-09-10 04:36:43 +0000273}
274
Michael Ilseman9333ffb2013-03-08 21:03:09 +0000275/// getMallocArraySize - Returns the array size of a malloc call. If the
Victor Hernandez90f48e72009-10-28 20:18:55 +0000276/// argument passed to malloc is a multiple of the size of the malloced type,
277/// then return that multiple. For non-array mallocs, the multiple is
278/// constant 1. Otherwise, return NULL for mallocs whose array size cannot be
Victor Hernandez2491ce02009-10-15 20:14:52 +0000279/// determined.
Micah Villmow3574eca2012-10-08 16:38:25 +0000280Value *llvm::getMallocArraySize(CallInst *CI, const DataLayout *TD,
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000281 const TargetLibraryInfo *TLI,
Victor Hernandez8e345a12009-11-10 08:32:25 +0000282 bool LookThroughSExt) {
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000283 assert(isMallocLikeFn(CI, TLI) && "getMallocArraySize and not malloc call");
284 return computeArraySize(CI, TD, TLI, LookThroughSExt);
Evan Chengfabcb912009-09-10 04:36:43 +0000285}
Victor Hernandez66284e02009-10-24 04:23:03 +0000286
Nuno Lopes252ef562012-05-03 21:19:58 +0000287
Nuno Lopes252ef562012-05-03 21:19:58 +0000288/// extractCallocCall - Returns the corresponding CallInst if the instruction
289/// is a calloc call.
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000290const CallInst *llvm::extractCallocCall(const Value *I,
291 const TargetLibraryInfo *TLI) {
292 return isCallocLikeFn(I, TLI) ? cast<CallInst>(I) : 0;
Nuno Lopes252ef562012-05-03 21:19:58 +0000293}
294
Victor Hernandez046e78c2009-10-26 23:43:48 +0000295
Gabor Greif02680f92010-06-23 21:51:12 +0000296/// isFreeCall - Returns non-null if the value is a call to the builtin free()
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000297const CallInst *llvm::isFreeCall(const Value *I, const TargetLibraryInfo *TLI) {
Victor Hernandez66284e02009-10-24 04:23:03 +0000298 const CallInst *CI = dyn_cast<CallInst>(I);
Michael Ilsemancacf9712013-03-08 21:15:00 +0000299 if (!CI || isa<IntrinsicInst>(CI))
Gabor Greif02680f92010-06-23 21:51:12 +0000300 return 0;
Victor Hernandez3ad70d52009-11-03 20:39:35 +0000301 Function *Callee = CI->getCalledFunction();
Nick Lewycky42e72ca2011-03-15 07:31:32 +0000302 if (Callee == 0 || !Callee->isDeclaration())
303 return 0;
304
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000305 StringRef FnName = Callee->getName();
306 LibFunc::Func TLIFn;
307 if (!TLI || !TLI->getLibFunc(FnName, TLIFn) || !TLI->has(TLIFn))
308 return 0;
309
310 if (TLIFn != LibFunc::free &&
311 TLIFn != LibFunc::ZdlPv && // operator delete(void*)
312 TLIFn != LibFunc::ZdaPv) // operator delete[](void*)
Gabor Greif02680f92010-06-23 21:51:12 +0000313 return 0;
Victor Hernandez66284e02009-10-24 04:23:03 +0000314
315 // Check free prototype.
Michael Ilseman9333ffb2013-03-08 21:03:09 +0000316 // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin
Victor Hernandez66284e02009-10-24 04:23:03 +0000317 // attribute will exist.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000318 FunctionType *FTy = Callee->getFunctionType();
Victor Hernandez3ad70d52009-11-03 20:39:35 +0000319 if (!FTy->getReturnType()->isVoidTy())
Gabor Greif02680f92010-06-23 21:51:12 +0000320 return 0;
Victor Hernandez66284e02009-10-24 04:23:03 +0000321 if (FTy->getNumParams() != 1)
Gabor Greif02680f92010-06-23 21:51:12 +0000322 return 0;
Chris Lattnerebb21892011-06-18 21:46:23 +0000323 if (FTy->getParamType(0) != Type::getInt8PtrTy(Callee->getContext()))
Gabor Greif02680f92010-06-23 21:51:12 +0000324 return 0;
Victor Hernandez66284e02009-10-24 04:23:03 +0000325
Gabor Greif02680f92010-06-23 21:51:12 +0000326 return CI;
Victor Hernandez66284e02009-10-24 04:23:03 +0000327}
Nuno Lopes9e72a792012-06-21 15:45:28 +0000328
329
330
331//===----------------------------------------------------------------------===//
332// Utility functions to compute size of objects.
333//
334
335
336/// \brief Compute the size of the object pointed by Ptr. Returns true and the
337/// object size in Size if successful, and false otherwise.
338/// If RoundToAlign is true, then Size is rounded up to the aligment of allocas,
339/// byval arguments, and global variables.
Micah Villmow3574eca2012-10-08 16:38:25 +0000340bool llvm::getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout *TD,
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000341 const TargetLibraryInfo *TLI, bool RoundToAlign) {
Nuno Lopes9e72a792012-06-21 15:45:28 +0000342 if (!TD)
343 return false;
344
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000345 ObjectSizeOffsetVisitor Visitor(TD, TLI, Ptr->getContext(), RoundToAlign);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000346 SizeOffsetType Data = Visitor.compute(const_cast<Value*>(Ptr));
347 if (!Visitor.bothKnown(Data))
348 return false;
349
350 APInt ObjSize = Data.first, Offset = Data.second;
351 // check for overflow
352 if (Offset.slt(0) || ObjSize.ult(Offset))
353 Size = 0;
354 else
355 Size = (ObjSize - Offset).getZExtValue();
356 return true;
357}
358
Nuno Lopes2bc689c2013-03-02 11:23:34 +0000359/// \brief Compute the size of the underlying object pointed by Ptr. Returns
360/// true and the object size in Size if successful, and false otherwise.
361/// If RoundToAlign is true, then Size is rounded up to the aligment of allocas,
362/// byval arguments, and global variables.
363bool llvm::getUnderlyingObjectSize(const Value *Ptr, uint64_t &Size,
364 const DataLayout *TD,
365 const TargetLibraryInfo *TLI,
366 bool RoundToAlign) {
367 if (!TD)
368 return false;
369
370 ObjectSizeOffsetVisitor Visitor(TD, TLI, Ptr->getContext(), RoundToAlign);
371 SizeOffsetType Data = Visitor.compute(const_cast<Value*>(Ptr));
372 if (!Visitor.knownSize(Data))
373 return false;
374
375 Size = Data.first.getZExtValue();
376 return true;
377}
378
Nuno Lopes9e72a792012-06-21 15:45:28 +0000379
380STATISTIC(ObjectVisitorArgument,
381 "Number of arguments with unsolved size and offset");
382STATISTIC(ObjectVisitorLoad,
383 "Number of load instructions with unsolved size and offset");
384
385
386APInt ObjectSizeOffsetVisitor::align(APInt Size, uint64_t Align) {
387 if (RoundToAlign && Align)
388 return APInt(IntTyBits, RoundUpToAlignment(Size.getZExtValue(), Align));
389 return Size;
390}
391
Micah Villmow3574eca2012-10-08 16:38:25 +0000392ObjectSizeOffsetVisitor::ObjectSizeOffsetVisitor(const DataLayout *TD,
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000393 const TargetLibraryInfo *TLI,
Nuno Lopes9e72a792012-06-21 15:45:28 +0000394 LLVMContext &Context,
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000395 bool RoundToAlign)
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000396: TD(TD), TLI(TLI), RoundToAlign(RoundToAlign) {
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000397 IntegerType *IntTy = TD->getIntPtrType(Context);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000398 IntTyBits = IntTy->getBitWidth();
399 Zero = APInt::getNullValue(IntTyBits);
400}
401
402SizeOffsetType ObjectSizeOffsetVisitor::compute(Value *V) {
403 V = V->stripPointerCasts();
Nuno Lopes0a9ff4c2012-12-31 20:45:10 +0000404
Nuno Lopesb443a0a2013-03-02 11:36:24 +0000405 if (isa<Instruction>(V) || isa<GEPOperator>(V)) {
406 // Return cached value or insert unknown in cache if size of V was not
407 // computed yet in order to avoid recursions in PHis.
408 std::pair<CacheMapTy::iterator, bool> CacheVal =
409 CacheMap.insert(std::make_pair(V, unknown()));
410 if (!CacheVal.second)
411 return CacheVal.first->second;
412
413 SizeOffsetType Result;
Benjamin Kramer168843c2012-08-17 19:26:41 +0000414 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V))
Nuno Lopesb443a0a2013-03-02 11:36:24 +0000415 Result = visitGEPOperator(*GEP);
416 else
417 Result = visit(cast<Instruction>(*V));
418 return CacheMap[V] = Result;
Benjamin Kramer168843c2012-08-17 19:26:41 +0000419 }
Nuno Lopesb443a0a2013-03-02 11:36:24 +0000420
Nuno Lopes9e72a792012-06-21 15:45:28 +0000421 if (Argument *A = dyn_cast<Argument>(V))
422 return visitArgument(*A);
423 if (ConstantPointerNull *P = dyn_cast<ConstantPointerNull>(V))
424 return visitConstantPointerNull(*P);
Nuno Lopes41be2fb2012-12-31 16:23:48 +0000425 if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
426 return visitGlobalAlias(*GA);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000427 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
428 return visitGlobalVariable(*GV);
429 if (UndefValue *UV = dyn_cast<UndefValue>(V))
430 return visitUndefValue(*UV);
Benjamin Kramer168843c2012-08-17 19:26:41 +0000431 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Nuno Lopes9e72a792012-06-21 15:45:28 +0000432 if (CE->getOpcode() == Instruction::IntToPtr)
433 return unknown(); // clueless
Benjamin Kramer168843c2012-08-17 19:26:41 +0000434 }
Nuno Lopes9e72a792012-06-21 15:45:28 +0000435
436 DEBUG(dbgs() << "ObjectSizeOffsetVisitor::compute() unhandled value: " << *V
437 << '\n');
438 return unknown();
439}
440
441SizeOffsetType ObjectSizeOffsetVisitor::visitAllocaInst(AllocaInst &I) {
442 if (!I.getAllocatedType()->isSized())
443 return unknown();
444
445 APInt Size(IntTyBits, TD->getTypeAllocSize(I.getAllocatedType()));
446 if (!I.isArrayAllocation())
447 return std::make_pair(align(Size, I.getAlignment()), Zero);
448
449 Value *ArraySize = I.getArraySize();
450 if (const ConstantInt *C = dyn_cast<ConstantInt>(ArraySize)) {
451 Size *= C->getValue().zextOrSelf(IntTyBits);
452 return std::make_pair(align(Size, I.getAlignment()), Zero);
453 }
454 return unknown();
455}
456
457SizeOffsetType ObjectSizeOffsetVisitor::visitArgument(Argument &A) {
458 // no interprocedural analysis is done at the moment
459 if (!A.hasByValAttr()) {
460 ++ObjectVisitorArgument;
461 return unknown();
462 }
463 PointerType *PT = cast<PointerType>(A.getType());
464 APInt Size(IntTyBits, TD->getTypeAllocSize(PT->getElementType()));
465 return std::make_pair(align(Size, A.getParamAlignment()), Zero);
466}
467
468SizeOffsetType ObjectSizeOffsetVisitor::visitCallSite(CallSite CS) {
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000469 const AllocFnsTy *FnData = getAllocationData(CS.getInstruction(), AnyAlloc,
470 TLI);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000471 if (!FnData)
472 return unknown();
473
474 // handle strdup-like functions separately
475 if (FnData->AllocTy == StrDupLike) {
Nuno Lopes9827c8e2012-07-24 16:28:13 +0000476 APInt Size(IntTyBits, GetStringLength(CS.getArgument(0)));
477 if (!Size)
478 return unknown();
479
480 // strndup limits strlen
481 if (FnData->FstParam > 0) {
482 ConstantInt *Arg= dyn_cast<ConstantInt>(CS.getArgument(FnData->FstParam));
483 if (!Arg)
484 return unknown();
485
486 APInt MaxSize = Arg->getValue().zextOrSelf(IntTyBits);
487 if (Size.ugt(MaxSize))
488 Size = MaxSize + 1;
489 }
490 return std::make_pair(Size, Zero);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000491 }
492
493 ConstantInt *Arg = dyn_cast<ConstantInt>(CS.getArgument(FnData->FstParam));
494 if (!Arg)
495 return unknown();
496
Nuno Lopes034dd6c2012-06-21 16:47:58 +0000497 APInt Size = Arg->getValue().zextOrSelf(IntTyBits);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000498 // size determined by just 1 parameter
Nuno Lopesef22f042012-06-21 18:38:26 +0000499 if (FnData->SndParam < 0)
Nuno Lopes9e72a792012-06-21 15:45:28 +0000500 return std::make_pair(Size, Zero);
501
502 Arg = dyn_cast<ConstantInt>(CS.getArgument(FnData->SndParam));
503 if (!Arg)
504 return unknown();
505
Nuno Lopes034dd6c2012-06-21 16:47:58 +0000506 Size *= Arg->getValue().zextOrSelf(IntTyBits);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000507 return std::make_pair(Size, Zero);
508
509 // TODO: handle more standard functions (+ wchar cousins):
Nuno Lopes6e699bf2012-07-25 18:49:28 +0000510 // - strdup / strndup
Nuno Lopes9e72a792012-06-21 15:45:28 +0000511 // - strcpy / strncpy
512 // - strcat / strncat
513 // - memcpy / memmove
Nuno Lopes6e699bf2012-07-25 18:49:28 +0000514 // - strcat / strncat
Nuno Lopes9e72a792012-06-21 15:45:28 +0000515 // - memset
516}
517
518SizeOffsetType
519ObjectSizeOffsetVisitor::visitConstantPointerNull(ConstantPointerNull&) {
520 return std::make_pair(Zero, Zero);
521}
522
523SizeOffsetType
Nuno Lopes41a3f252012-06-28 16:34:03 +0000524ObjectSizeOffsetVisitor::visitExtractElementInst(ExtractElementInst&) {
525 return unknown();
526}
527
528SizeOffsetType
Nuno Lopes9e72a792012-06-21 15:45:28 +0000529ObjectSizeOffsetVisitor::visitExtractValueInst(ExtractValueInst&) {
530 // Easy cases were already folded by previous passes.
531 return unknown();
532}
533
534SizeOffsetType ObjectSizeOffsetVisitor::visitGEPOperator(GEPOperator &GEP) {
535 SizeOffsetType PtrData = compute(GEP.getPointerOperand());
Nuno Lopes98281a22012-12-30 16:25:48 +0000536 APInt Offset(IntTyBits, 0);
537 if (!bothKnown(PtrData) || !GEP.accumulateConstantOffset(*TD, Offset))
Nuno Lopes9e72a792012-06-21 15:45:28 +0000538 return unknown();
539
Nuno Lopes9e72a792012-06-21 15:45:28 +0000540 return std::make_pair(PtrData.first, PtrData.second + Offset);
541}
542
Nuno Lopes41be2fb2012-12-31 16:23:48 +0000543SizeOffsetType ObjectSizeOffsetVisitor::visitGlobalAlias(GlobalAlias &GA) {
544 if (GA.mayBeOverridden())
545 return unknown();
546 return compute(GA.getAliasee());
547}
548
Nuno Lopes9e72a792012-06-21 15:45:28 +0000549SizeOffsetType ObjectSizeOffsetVisitor::visitGlobalVariable(GlobalVariable &GV){
550 if (!GV.hasDefinitiveInitializer())
551 return unknown();
552
553 APInt Size(IntTyBits, TD->getTypeAllocSize(GV.getType()->getElementType()));
554 return std::make_pair(align(Size, GV.getAlignment()), Zero);
555}
556
557SizeOffsetType ObjectSizeOffsetVisitor::visitIntToPtrInst(IntToPtrInst&) {
558 // clueless
559 return unknown();
560}
561
562SizeOffsetType ObjectSizeOffsetVisitor::visitLoadInst(LoadInst&) {
563 ++ObjectVisitorLoad;
564 return unknown();
565}
566
Nuno Lopesb443a0a2013-03-02 11:36:24 +0000567SizeOffsetType ObjectSizeOffsetVisitor::visitPHINode(PHINode &PHI) {
568 if (PHI.getNumIncomingValues() == 0)
569 return unknown();
570
571 SizeOffsetType Ret = compute(PHI.getIncomingValue(0));
572 if (!bothKnown(Ret))
573 return unknown();
574
575 // Verify that all PHI incoming pointers have the same size and offset.
576 for (unsigned i = 1, e = PHI.getNumIncomingValues(); i != e; ++i) {
577 SizeOffsetType EdgeData = compute(PHI.getIncomingValue(i));
578 if (!bothKnown(EdgeData) || EdgeData != Ret)
579 return unknown();
580 }
581 return Ret;
Nuno Lopes9e72a792012-06-21 15:45:28 +0000582}
583
584SizeOffsetType ObjectSizeOffsetVisitor::visitSelectInst(SelectInst &I) {
585 SizeOffsetType TrueSide = compute(I.getTrueValue());
586 SizeOffsetType FalseSide = compute(I.getFalseValue());
Nuno Lopes2e594fa2012-12-31 18:01:36 +0000587 if (bothKnown(TrueSide) && bothKnown(FalseSide) && TrueSide == FalseSide)
Nuno Lopes9e72a792012-06-21 15:45:28 +0000588 return TrueSide;
589 return unknown();
590}
591
592SizeOffsetType ObjectSizeOffsetVisitor::visitUndefValue(UndefValue&) {
593 return std::make_pair(Zero, Zero);
594}
595
596SizeOffsetType ObjectSizeOffsetVisitor::visitInstruction(Instruction &I) {
597 DEBUG(dbgs() << "ObjectSizeOffsetVisitor unknown instruction:" << I << '\n');
598 return unknown();
599}
600
601
Micah Villmow3574eca2012-10-08 16:38:25 +0000602ObjectSizeOffsetEvaluator::ObjectSizeOffsetEvaluator(const DataLayout *TD,
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000603 const TargetLibraryInfo *TLI,
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000604 LLVMContext &Context)
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000605: TD(TD), TLI(TLI), Context(Context), Builder(Context, TargetFolder(TD)) {
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000606 IntTy = TD->getIntPtrType(Context);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000607 Zero = ConstantInt::get(IntTy, 0);
608}
609
610SizeOffsetEvalType ObjectSizeOffsetEvaluator::compute(Value *V) {
611 SizeOffsetEvalType Result = compute_(V);
612
613 if (!bothKnown(Result)) {
614 // erase everything that was computed in this iteration from the cache, so
615 // that no dangling references are left behind. We could be a bit smarter if
616 // we kept a dependency graph. It's probably not worth the complexity.
617 for (PtrSetTy::iterator I=SeenVals.begin(), E=SeenVals.end(); I != E; ++I) {
618 CacheMapTy::iterator CacheIt = CacheMap.find(*I);
619 // non-computable results can be safely cached
620 if (CacheIt != CacheMap.end() && anyKnown(CacheIt->second))
621 CacheMap.erase(CacheIt);
622 }
623 }
624
625 SeenVals.clear();
626 return Result;
627}
628
629SizeOffsetEvalType ObjectSizeOffsetEvaluator::compute_(Value *V) {
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000630 ObjectSizeOffsetVisitor Visitor(TD, TLI, Context);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000631 SizeOffsetType Const = Visitor.compute(V);
632 if (Visitor.bothKnown(Const))
633 return std::make_pair(ConstantInt::get(Context, Const.first),
634 ConstantInt::get(Context, Const.second));
635
636 V = V->stripPointerCasts();
637
638 // check cache
639 CacheMapTy::iterator CacheIt = CacheMap.find(V);
640 if (CacheIt != CacheMap.end())
641 return CacheIt->second;
642
643 // always generate code immediately before the instruction being
644 // processed, so that the generated code dominates the same BBs
645 Instruction *PrevInsertPoint = Builder.GetInsertPoint();
646 if (Instruction *I = dyn_cast<Instruction>(V))
647 Builder.SetInsertPoint(I);
648
649 // record the pointers that were handled in this run, so that they can be
650 // cleaned later if something fails
651 SeenVals.insert(V);
652
653 // now compute the size and offset
654 SizeOffsetEvalType Result;
655 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
656 Result = visitGEPOperator(*GEP);
657 } else if (Instruction *I = dyn_cast<Instruction>(V)) {
658 Result = visit(*I);
659 } else if (isa<Argument>(V) ||
660 (isa<ConstantExpr>(V) &&
661 cast<ConstantExpr>(V)->getOpcode() == Instruction::IntToPtr) ||
Nuno Lopes41be2fb2012-12-31 16:23:48 +0000662 isa<GlobalAlias>(V) ||
Nuno Lopes9e72a792012-06-21 15:45:28 +0000663 isa<GlobalVariable>(V)) {
664 // ignore values where we cannot do more than what ObjectSizeVisitor can
665 Result = unknown();
666 } else {
667 DEBUG(dbgs() << "ObjectSizeOffsetEvaluator::compute() unhandled value: "
668 << *V << '\n');
669 Result = unknown();
670 }
671
672 if (PrevInsertPoint)
673 Builder.SetInsertPoint(PrevInsertPoint);
674
675 // Don't reuse CacheIt since it may be invalid at this point.
676 CacheMap[V] = Result;
677 return Result;
678}
679
680SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitAllocaInst(AllocaInst &I) {
681 if (!I.getAllocatedType()->isSized())
682 return unknown();
683
684 // must be a VLA
685 assert(I.isArrayAllocation());
686 Value *ArraySize = I.getArraySize();
687 Value *Size = ConstantInt::get(ArraySize->getType(),
688 TD->getTypeAllocSize(I.getAllocatedType()));
689 Size = Builder.CreateMul(Size, ArraySize);
690 return std::make_pair(Size, Zero);
691}
692
693SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitCallSite(CallSite CS) {
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000694 const AllocFnsTy *FnData = getAllocationData(CS.getInstruction(), AnyAlloc,
695 TLI);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000696 if (!FnData)
697 return unknown();
698
699 // handle strdup-like functions separately
700 if (FnData->AllocTy == StrDupLike) {
Nuno Lopes6e699bf2012-07-25 18:49:28 +0000701 // TODO
702 return unknown();
Nuno Lopes9e72a792012-06-21 15:45:28 +0000703 }
704
Nuno Lopes034dd6c2012-06-21 16:47:58 +0000705 Value *FirstArg = CS.getArgument(FnData->FstParam);
706 FirstArg = Builder.CreateZExt(FirstArg, IntTy);
Nuno Lopesef22f042012-06-21 18:38:26 +0000707 if (FnData->SndParam < 0)
Nuno Lopes9e72a792012-06-21 15:45:28 +0000708 return std::make_pair(FirstArg, Zero);
709
710 Value *SecondArg = CS.getArgument(FnData->SndParam);
Nuno Lopes034dd6c2012-06-21 16:47:58 +0000711 SecondArg = Builder.CreateZExt(SecondArg, IntTy);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000712 Value *Size = Builder.CreateMul(FirstArg, SecondArg);
713 return std::make_pair(Size, Zero);
714
715 // TODO: handle more standard functions (+ wchar cousins):
Nuno Lopes6e699bf2012-07-25 18:49:28 +0000716 // - strdup / strndup
Nuno Lopes9e72a792012-06-21 15:45:28 +0000717 // - strcpy / strncpy
718 // - strcat / strncat
719 // - memcpy / memmove
Nuno Lopes6e699bf2012-07-25 18:49:28 +0000720 // - strcat / strncat
Nuno Lopes9e72a792012-06-21 15:45:28 +0000721 // - memset
722}
723
724SizeOffsetEvalType
Nuno Lopes41a3f252012-06-28 16:34:03 +0000725ObjectSizeOffsetEvaluator::visitExtractElementInst(ExtractElementInst&) {
726 return unknown();
727}
728
729SizeOffsetEvalType
730ObjectSizeOffsetEvaluator::visitExtractValueInst(ExtractValueInst&) {
731 return unknown();
732}
733
734SizeOffsetEvalType
Nuno Lopes9e72a792012-06-21 15:45:28 +0000735ObjectSizeOffsetEvaluator::visitGEPOperator(GEPOperator &GEP) {
736 SizeOffsetEvalType PtrData = compute_(GEP.getPointerOperand());
737 if (!bothKnown(PtrData))
738 return unknown();
739
Nuno Lopesc606c3f2012-07-20 23:07:40 +0000740 Value *Offset = EmitGEPOffset(&Builder, *TD, &GEP, /*NoAssumptions=*/true);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000741 Offset = Builder.CreateAdd(PtrData.second, Offset);
742 return std::make_pair(PtrData.first, Offset);
743}
744
745SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitIntToPtrInst(IntToPtrInst&) {
746 // clueless
747 return unknown();
748}
749
750SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitLoadInst(LoadInst&) {
751 return unknown();
752}
753
754SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitPHINode(PHINode &PHI) {
755 // create 2 PHIs: one for size and another for offset
756 PHINode *SizePHI = Builder.CreatePHI(IntTy, PHI.getNumIncomingValues());
757 PHINode *OffsetPHI = Builder.CreatePHI(IntTy, PHI.getNumIncomingValues());
758
759 // insert right away in the cache to handle recursive PHIs
760 CacheMap[&PHI] = std::make_pair(SizePHI, OffsetPHI);
761
762 // compute offset/size for each PHI incoming pointer
763 for (unsigned i = 0, e = PHI.getNumIncomingValues(); i != e; ++i) {
764 Builder.SetInsertPoint(PHI.getIncomingBlock(i)->getFirstInsertionPt());
765 SizeOffsetEvalType EdgeData = compute_(PHI.getIncomingValue(i));
766
767 if (!bothKnown(EdgeData)) {
768 OffsetPHI->replaceAllUsesWith(UndefValue::get(IntTy));
769 OffsetPHI->eraseFromParent();
770 SizePHI->replaceAllUsesWith(UndefValue::get(IntTy));
771 SizePHI->eraseFromParent();
772 return unknown();
773 }
774 SizePHI->addIncoming(EdgeData.first, PHI.getIncomingBlock(i));
775 OffsetPHI->addIncoming(EdgeData.second, PHI.getIncomingBlock(i));
776 }
Nuno Lopes0dff5322012-07-03 17:13:25 +0000777
778 Value *Size = SizePHI, *Offset = OffsetPHI, *Tmp;
779 if ((Tmp = SizePHI->hasConstantValue())) {
780 Size = Tmp;
781 SizePHI->replaceAllUsesWith(Size);
782 SizePHI->eraseFromParent();
783 }
784 if ((Tmp = OffsetPHI->hasConstantValue())) {
785 Offset = Tmp;
786 OffsetPHI->replaceAllUsesWith(Offset);
787 OffsetPHI->eraseFromParent();
788 }
789 return std::make_pair(Size, Offset);
Nuno Lopes9e72a792012-06-21 15:45:28 +0000790}
791
792SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitSelectInst(SelectInst &I) {
793 SizeOffsetEvalType TrueSide = compute_(I.getTrueValue());
794 SizeOffsetEvalType FalseSide = compute_(I.getFalseValue());
795
796 if (!bothKnown(TrueSide) || !bothKnown(FalseSide))
797 return unknown();
798 if (TrueSide == FalseSide)
799 return TrueSide;
800
801 Value *Size = Builder.CreateSelect(I.getCondition(), TrueSide.first,
802 FalseSide.first);
803 Value *Offset = Builder.CreateSelect(I.getCondition(), TrueSide.second,
804 FalseSide.second);
805 return std::make_pair(Size, Offset);
806}
807
808SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitInstruction(Instruction &I) {
809 DEBUG(dbgs() << "ObjectSizeOffsetEvaluator unknown instruction:" << I <<'\n');
810 return unknown();
811}