1 //===------ MemoryBuiltins.cpp - Identify calls to memory builtins --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This family of functions identifies calls to builtin functions that allocate
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Analysis/MemoryBuiltins.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/Statistic.h"
18 #include "llvm/Analysis/TargetLibraryInfo.h"
19 #include "llvm/Analysis/ValueTracking.h"
20 #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"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/MathExtras.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/Transforms/Utils/Local.h"
32 #define DEBUG_TYPE "memory-builtins"
34 enum AllocType : uint8_t {
35 OpNewLike = 1<<0, // allocates; never returns null
36 MallocLike = 1<<1 | OpNewLike, // allocates; may return null
37 CallocLike = 1<<2, // allocates + bzero
38 ReallocLike = 1<<3, // reallocates
40 MallocOrCallocLike = MallocLike | CallocLike,
41 AllocLike = MallocLike | CallocLike | StrDupLike,
42 AnyAlloc = AllocLike | ReallocLike
48 // First and Second size parameters (or -1 if unused)
49 int FstParam, SndParam;
52 // FIXME: certain users need more information. E.g., SimplifyLibCalls needs to
53 // know which functions are nounwind, noalias, nocapture parameters, etc.
54 static const std::pair<LibFunc, AllocFnsTy> AllocationFnData[] = {
55 {LibFunc_malloc, {MallocLike, 1, 0, -1}},
56 {LibFunc_valloc, {MallocLike, 1, 0, -1}},
57 {LibFunc_Znwj, {OpNewLike, 1, 0, -1}}, // new(unsigned int)
58 {LibFunc_ZnwjRKSt9nothrow_t, {MallocLike, 2, 0, -1}}, // new(unsigned int, nothrow)
59 {LibFunc_Znwm, {OpNewLike, 1, 0, -1}}, // new(unsigned long)
60 {LibFunc_ZnwmRKSt9nothrow_t, {MallocLike, 2, 0, -1}}, // new(unsigned long, nothrow)
61 {LibFunc_Znaj, {OpNewLike, 1, 0, -1}}, // new[](unsigned int)
62 {LibFunc_ZnajRKSt9nothrow_t, {MallocLike, 2, 0, -1}}, // new[](unsigned int, nothrow)
63 {LibFunc_Znam, {OpNewLike, 1, 0, -1}}, // new[](unsigned long)
64 {LibFunc_ZnamRKSt9nothrow_t, {MallocLike, 2, 0, -1}}, // new[](unsigned long, nothrow)
65 {LibFunc_msvc_new_int, {OpNewLike, 1, 0, -1}}, // new(unsigned int)
66 {LibFunc_msvc_new_int_nothrow, {MallocLike, 2, 0, -1}}, // new(unsigned int, nothrow)
67 {LibFunc_msvc_new_longlong, {OpNewLike, 1, 0, -1}}, // new(unsigned long long)
68 {LibFunc_msvc_new_longlong_nothrow, {MallocLike, 2, 0, -1}}, // new(unsigned long long, nothrow)
69 {LibFunc_msvc_new_array_int, {OpNewLike, 1, 0, -1}}, // new[](unsigned int)
70 {LibFunc_msvc_new_array_int_nothrow, {MallocLike, 2, 0, -1}}, // new[](unsigned int, nothrow)
71 {LibFunc_msvc_new_array_longlong, {OpNewLike, 1, 0, -1}}, // new[](unsigned long long)
72 {LibFunc_msvc_new_array_longlong_nothrow, {MallocLike, 2, 0, -1}}, // new[](unsigned long long, nothrow)
73 {LibFunc_calloc, {CallocLike, 2, 0, 1}},
74 {LibFunc_realloc, {ReallocLike, 2, 1, -1}},
75 {LibFunc_reallocf, {ReallocLike, 2, 1, -1}},
76 {LibFunc_strdup, {StrDupLike, 1, -1, -1}},
77 {LibFunc_strndup, {StrDupLike, 2, 1, -1}}
78 // TODO: Handle "int posix_memalign(void **, size_t, size_t)"
81 static const Function *getCalledFunction(const Value *V, bool LookThroughBitCast,
83 // Don't care about intrinsics in this case.
84 if (isa<IntrinsicInst>(V))
87 if (LookThroughBitCast)
88 V = V->stripPointerCasts();
90 ImmutableCallSite CS(V);
91 if (!CS.getInstruction())
94 IsNoBuiltin = CS.isNoBuiltin();
96 const Function *Callee = CS.getCalledFunction();
97 if (!Callee || !Callee->isDeclaration())
102 /// Returns the allocation data for the given value if it's either a call to a
103 /// known allocation function, or a call to a function with the allocsize
105 static Optional<AllocFnsTy>
106 getAllocationDataForFunction(const Function *Callee, AllocType AllocTy,
107 const TargetLibraryInfo *TLI) {
108 // Make sure that the function is available.
109 StringRef FnName = Callee->getName();
111 if (!TLI || !TLI->getLibFunc(FnName, TLIFn) || !TLI->has(TLIFn))
114 const auto *Iter = find_if(
115 AllocationFnData, [TLIFn](const std::pair<LibFunc, AllocFnsTy> &P) {
116 return P.first == TLIFn;
119 if (Iter == std::end(AllocationFnData))
122 const AllocFnsTy *FnData = &Iter->second;
123 if ((FnData->AllocTy & AllocTy) != FnData->AllocTy)
126 // Check function prototype.
127 int FstParam = FnData->FstParam;
128 int SndParam = FnData->SndParam;
129 FunctionType *FTy = Callee->getFunctionType();
131 if (FTy->getReturnType() == Type::getInt8PtrTy(FTy->getContext()) &&
132 FTy->getNumParams() == FnData->NumParams &&
134 (FTy->getParamType(FstParam)->isIntegerTy(32) ||
135 FTy->getParamType(FstParam)->isIntegerTy(64))) &&
137 FTy->getParamType(SndParam)->isIntegerTy(32) ||
138 FTy->getParamType(SndParam)->isIntegerTy(64)))
143 static Optional<AllocFnsTy> getAllocationData(const Value *V, AllocType AllocTy,
144 const TargetLibraryInfo *TLI,
145 bool LookThroughBitCast = false) {
146 bool IsNoBuiltinCall;
147 if (const Function *Callee =
148 getCalledFunction(V, LookThroughBitCast, IsNoBuiltinCall))
149 if (!IsNoBuiltinCall)
150 return getAllocationDataForFunction(Callee, AllocTy, TLI);
154 static Optional<AllocFnsTy> getAllocationSize(const Value *V,
155 const TargetLibraryInfo *TLI) {
156 bool IsNoBuiltinCall;
157 const Function *Callee =
158 getCalledFunction(V, /*LookThroughBitCast=*/false, IsNoBuiltinCall);
162 // Prefer to use existing information over allocsize. This will give us an
164 if (!IsNoBuiltinCall)
165 if (Optional<AllocFnsTy> Data =
166 getAllocationDataForFunction(Callee, AnyAlloc, TLI))
169 Attribute Attr = Callee->getFnAttribute(Attribute::AllocSize);
170 if (Attr == Attribute())
173 std::pair<unsigned, Optional<unsigned>> Args = Attr.getAllocSizeArgs();
176 // Because allocsize only tells us how many bytes are allocated, we're not
177 // really allowed to assume anything, so we use MallocLike.
178 Result.AllocTy = MallocLike;
179 Result.NumParams = Callee->getNumOperands();
180 Result.FstParam = Args.first;
181 Result.SndParam = Args.second.getValueOr(-1);
185 static bool hasNoAliasAttr(const Value *V, bool LookThroughBitCast) {
186 ImmutableCallSite CS(LookThroughBitCast ? V->stripPointerCasts() : V);
187 return CS && CS.hasRetAttr(Attribute::NoAlias);
191 /// \brief Tests if a value is a call or invoke to a library function that
192 /// allocates or reallocates memory (either malloc, calloc, realloc, or strdup
194 bool llvm::isAllocationFn(const Value *V, const TargetLibraryInfo *TLI,
195 bool LookThroughBitCast) {
196 return getAllocationData(V, AnyAlloc, TLI, LookThroughBitCast).hasValue();
199 /// \brief Tests if a value is a call or invoke to a function that returns a
200 /// NoAlias pointer (including malloc/calloc/realloc/strdup-like functions).
201 bool llvm::isNoAliasFn(const Value *V, const TargetLibraryInfo *TLI,
202 bool LookThroughBitCast) {
203 // it's safe to consider realloc as noalias since accessing the original
204 // pointer is undefined behavior
205 return isAllocationFn(V, TLI, LookThroughBitCast) ||
206 hasNoAliasAttr(V, LookThroughBitCast);
209 /// \brief Tests if a value is a call or invoke to a library function that
210 /// allocates uninitialized memory (such as malloc).
211 bool llvm::isMallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
212 bool LookThroughBitCast) {
213 return getAllocationData(V, MallocLike, TLI, LookThroughBitCast).hasValue();
216 /// \brief Tests if a value is a call or invoke to a library function that
217 /// allocates zero-filled memory (such as calloc).
218 bool llvm::isCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
219 bool LookThroughBitCast) {
220 return getAllocationData(V, CallocLike, TLI, LookThroughBitCast).hasValue();
223 /// \brief Tests if a value is a call or invoke to a library function that
224 /// allocates memory similiar to malloc or calloc.
225 bool llvm::isMallocOrCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
226 bool LookThroughBitCast) {
227 return getAllocationData(V, MallocOrCallocLike, TLI,
228 LookThroughBitCast).hasValue();
231 /// \brief Tests if a value is a call or invoke to a library function that
232 /// allocates memory (either malloc, calloc, or strdup like).
233 bool llvm::isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
234 bool LookThroughBitCast) {
235 return getAllocationData(V, AllocLike, TLI, LookThroughBitCast).hasValue();
238 /// extractMallocCall - Returns the corresponding CallInst if the instruction
239 /// is a malloc call. Since CallInst::CreateMalloc() only creates calls, we
240 /// ignore InvokeInst here.
241 const CallInst *llvm::extractMallocCall(const Value *I,
242 const TargetLibraryInfo *TLI) {
243 return isMallocLikeFn(I, TLI) ? dyn_cast<CallInst>(I) : nullptr;
246 static Value *computeArraySize(const CallInst *CI, const DataLayout &DL,
247 const TargetLibraryInfo *TLI,
248 bool LookThroughSExt = false) {
252 // The size of the malloc's result type must be known to determine array size.
253 Type *T = getMallocAllocatedType(CI, TLI);
254 if (!T || !T->isSized())
257 unsigned ElementSize = DL.getTypeAllocSize(T);
258 if (StructType *ST = dyn_cast<StructType>(T))
259 ElementSize = DL.getStructLayout(ST)->getSizeInBytes();
261 // If malloc call's arg can be determined to be a multiple of ElementSize,
262 // return the multiple. Otherwise, return NULL.
263 Value *MallocArg = CI->getArgOperand(0);
264 Value *Multiple = nullptr;
265 if (ComputeMultiple(MallocArg, ElementSize, Multiple, LookThroughSExt))
271 /// getMallocType - Returns the PointerType resulting from the malloc call.
272 /// The PointerType depends on the number of bitcast uses of the malloc call:
273 /// 0: PointerType is the calls' return type.
274 /// 1: PointerType is the bitcast's result type.
275 /// >1: Unique PointerType cannot be determined, return NULL.
276 PointerType *llvm::getMallocType(const CallInst *CI,
277 const TargetLibraryInfo *TLI) {
278 assert(isMallocLikeFn(CI, TLI) && "getMallocType and not malloc call");
280 PointerType *MallocType = nullptr;
281 unsigned NumOfBitCastUses = 0;
283 // Determine if CallInst has a bitcast use.
284 for (Value::const_user_iterator UI = CI->user_begin(), E = CI->user_end();
286 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(*UI++)) {
287 MallocType = cast<PointerType>(BCI->getDestTy());
291 // Malloc call has 1 bitcast use, so type is the bitcast's destination type.
292 if (NumOfBitCastUses == 1)
295 // Malloc call was not bitcast, so type is the malloc function's return type.
296 if (NumOfBitCastUses == 0)
297 return cast<PointerType>(CI->getType());
299 // Type could not be determined.
303 /// getMallocAllocatedType - Returns the Type allocated by malloc call.
304 /// The Type depends on the number of bitcast uses of the malloc call:
305 /// 0: PointerType is the malloc calls' return type.
306 /// 1: PointerType is the bitcast's result type.
307 /// >1: Unique PointerType cannot be determined, return NULL.
308 Type *llvm::getMallocAllocatedType(const CallInst *CI,
309 const TargetLibraryInfo *TLI) {
310 PointerType *PT = getMallocType(CI, TLI);
311 return PT ? PT->getElementType() : nullptr;
314 /// getMallocArraySize - Returns the array size of a malloc call. If the
315 /// argument passed to malloc is a multiple of the size of the malloced type,
316 /// then return that multiple. For non-array mallocs, the multiple is
317 /// constant 1. Otherwise, return NULL for mallocs whose array size cannot be
319 Value *llvm::getMallocArraySize(CallInst *CI, const DataLayout &DL,
320 const TargetLibraryInfo *TLI,
321 bool LookThroughSExt) {
322 assert(isMallocLikeFn(CI, TLI) && "getMallocArraySize and not malloc call");
323 return computeArraySize(CI, DL, TLI, LookThroughSExt);
327 /// extractCallocCall - Returns the corresponding CallInst if the instruction
328 /// is a calloc call.
329 const CallInst *llvm::extractCallocCall(const Value *I,
330 const TargetLibraryInfo *TLI) {
331 return isCallocLikeFn(I, TLI) ? cast<CallInst>(I) : nullptr;
335 /// isFreeCall - Returns non-null if the value is a call to the builtin free()
336 const CallInst *llvm::isFreeCall(const Value *I, const TargetLibraryInfo *TLI) {
337 const CallInst *CI = dyn_cast<CallInst>(I);
338 if (!CI || isa<IntrinsicInst>(CI))
340 Function *Callee = CI->getCalledFunction();
341 if (Callee == nullptr)
344 StringRef FnName = Callee->getName();
346 if (!TLI || !TLI->getLibFunc(FnName, TLIFn) || !TLI->has(TLIFn))
349 unsigned ExpectedNumParams;
350 if (TLIFn == LibFunc_free ||
351 TLIFn == LibFunc_ZdlPv || // operator delete(void*)
352 TLIFn == LibFunc_ZdaPv || // operator delete[](void*)
353 TLIFn == LibFunc_msvc_delete_ptr32 || // operator delete(void*)
354 TLIFn == LibFunc_msvc_delete_ptr64 || // operator delete(void*)
355 TLIFn == LibFunc_msvc_delete_array_ptr32 || // operator delete[](void*)
356 TLIFn == LibFunc_msvc_delete_array_ptr64) // operator delete[](void*)
357 ExpectedNumParams = 1;
358 else if (TLIFn == LibFunc_ZdlPvj || // delete(void*, uint)
359 TLIFn == LibFunc_ZdlPvm || // delete(void*, ulong)
360 TLIFn == LibFunc_ZdlPvRKSt9nothrow_t || // delete(void*, nothrow)
361 TLIFn == LibFunc_ZdaPvj || // delete[](void*, uint)
362 TLIFn == LibFunc_ZdaPvm || // delete[](void*, ulong)
363 TLIFn == LibFunc_ZdaPvRKSt9nothrow_t || // delete[](void*, nothrow)
364 TLIFn == LibFunc_msvc_delete_ptr32_int || // delete(void*, uint)
365 TLIFn == LibFunc_msvc_delete_ptr64_longlong || // delete(void*, ulonglong)
366 TLIFn == LibFunc_msvc_delete_ptr32_nothrow || // delete(void*, nothrow)
367 TLIFn == LibFunc_msvc_delete_ptr64_nothrow || // delete(void*, nothrow)
368 TLIFn == LibFunc_msvc_delete_array_ptr32_int || // delete[](void*, uint)
369 TLIFn == LibFunc_msvc_delete_array_ptr64_longlong || // delete[](void*, ulonglong)
370 TLIFn == LibFunc_msvc_delete_array_ptr32_nothrow || // delete[](void*, nothrow)
371 TLIFn == LibFunc_msvc_delete_array_ptr64_nothrow) // delete[](void*, nothrow)
372 ExpectedNumParams = 2;
376 // Check free prototype.
377 // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin
378 // attribute will exist.
379 FunctionType *FTy = Callee->getFunctionType();
380 if (!FTy->getReturnType()->isVoidTy())
382 if (FTy->getNumParams() != ExpectedNumParams)
384 if (FTy->getParamType(0) != Type::getInt8PtrTy(Callee->getContext()))
392 //===----------------------------------------------------------------------===//
393 // Utility functions to compute size of objects.
395 static APInt getSizeWithOverflow(const SizeOffsetType &Data) {
396 if (Data.second.isNegative() || Data.first.ult(Data.second))
397 return APInt(Data.first.getBitWidth(), 0);
398 return Data.first - Data.second;
401 /// \brief Compute the size of the object pointed by Ptr. Returns true and the
402 /// object size in Size if successful, and false otherwise.
403 /// If RoundToAlign is true, then Size is rounded up to the alignment of
404 /// allocas, byval arguments, and global variables.
405 bool llvm::getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout &DL,
406 const TargetLibraryInfo *TLI, ObjectSizeOpts Opts) {
407 ObjectSizeOffsetVisitor Visitor(DL, TLI, Ptr->getContext(), Opts);
408 SizeOffsetType Data = Visitor.compute(const_cast<Value*>(Ptr));
409 if (!Visitor.bothKnown(Data))
412 Size = getSizeWithOverflow(Data).getZExtValue();
416 ConstantInt *llvm::lowerObjectSizeCall(IntrinsicInst *ObjectSize,
417 const DataLayout &DL,
418 const TargetLibraryInfo *TLI,
420 assert(ObjectSize->getIntrinsicID() == Intrinsic::objectsize &&
421 "ObjectSize must be a call to llvm.objectsize!");
423 bool MaxVal = cast<ConstantInt>(ObjectSize->getArgOperand(1))->isZero();
424 ObjectSizeOpts EvalOptions;
425 // Unless we have to fold this to something, try to be as accurate as
428 EvalOptions.EvalMode =
429 MaxVal ? ObjectSizeOpts::Mode::Max : ObjectSizeOpts::Mode::Min;
431 EvalOptions.EvalMode = ObjectSizeOpts::Mode::Exact;
433 EvalOptions.NullIsUnknownSize =
434 cast<ConstantInt>(ObjectSize->getArgOperand(2))->isOne();
436 // FIXME: Does it make sense to just return a failure value if the size won't
437 // fit in the output and `!MustSucceed`?
439 auto *ResultType = cast<IntegerType>(ObjectSize->getType());
440 if (getObjectSize(ObjectSize->getArgOperand(0), Size, DL, TLI, EvalOptions) &&
441 isUIntN(ResultType->getBitWidth(), Size))
442 return ConstantInt::get(ResultType, Size);
447 return ConstantInt::get(ResultType, MaxVal ? -1ULL : 0);
450 STATISTIC(ObjectVisitorArgument,
451 "Number of arguments with unsolved size and offset");
452 STATISTIC(ObjectVisitorLoad,
453 "Number of load instructions with unsolved size and offset");
456 APInt ObjectSizeOffsetVisitor::align(APInt Size, uint64_t Align) {
457 if (Options.RoundToAlign && Align)
458 return APInt(IntTyBits, alignTo(Size.getZExtValue(), Align));
462 ObjectSizeOffsetVisitor::ObjectSizeOffsetVisitor(const DataLayout &DL,
463 const TargetLibraryInfo *TLI,
464 LLVMContext &Context,
465 ObjectSizeOpts Options)
466 : DL(DL), TLI(TLI), Options(Options) {
467 // Pointer size must be rechecked for each object visited since it could have
468 // a different address space.
471 SizeOffsetType ObjectSizeOffsetVisitor::compute(Value *V) {
472 IntTyBits = DL.getPointerTypeSizeInBits(V->getType());
473 Zero = APInt::getNullValue(IntTyBits);
475 V = V->stripPointerCasts();
476 if (Instruction *I = dyn_cast<Instruction>(V)) {
477 // If we have already seen this instruction, bail out. Cycles can happen in
478 // unreachable code after constant propagation.
479 if (!SeenInsts.insert(I).second)
482 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V))
483 return visitGEPOperator(*GEP);
486 if (Argument *A = dyn_cast<Argument>(V))
487 return visitArgument(*A);
488 if (ConstantPointerNull *P = dyn_cast<ConstantPointerNull>(V))
489 return visitConstantPointerNull(*P);
490 if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
491 return visitGlobalAlias(*GA);
492 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
493 return visitGlobalVariable(*GV);
494 if (UndefValue *UV = dyn_cast<UndefValue>(V))
495 return visitUndefValue(*UV);
496 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
497 if (CE->getOpcode() == Instruction::IntToPtr)
498 return unknown(); // clueless
499 if (CE->getOpcode() == Instruction::GetElementPtr)
500 return visitGEPOperator(cast<GEPOperator>(*CE));
503 DEBUG(dbgs() << "ObjectSizeOffsetVisitor::compute() unhandled value: " << *V
508 /// When we're compiling N-bit code, and the user uses parameters that are
509 /// greater than N bits (e.g. uint64_t on a 32-bit build), we can run into
510 /// trouble with APInt size issues. This function handles resizing + overflow
511 /// checks for us. Check and zext or trunc \p I depending on IntTyBits and
513 bool ObjectSizeOffsetVisitor::CheckedZextOrTrunc(APInt &I) {
514 // More bits than we can handle. Checking the bit width isn't necessary, but
515 // it's faster than checking active bits, and should give `false` in the
516 // vast majority of cases.
517 if (I.getBitWidth() > IntTyBits && I.getActiveBits() > IntTyBits)
519 if (I.getBitWidth() != IntTyBits)
520 I = I.zextOrTrunc(IntTyBits);
524 SizeOffsetType ObjectSizeOffsetVisitor::visitAllocaInst(AllocaInst &I) {
525 if (!I.getAllocatedType()->isSized())
528 APInt Size(IntTyBits, DL.getTypeAllocSize(I.getAllocatedType()));
529 if (!I.isArrayAllocation())
530 return std::make_pair(align(Size, I.getAlignment()), Zero);
532 Value *ArraySize = I.getArraySize();
533 if (const ConstantInt *C = dyn_cast<ConstantInt>(ArraySize)) {
534 APInt NumElems = C->getValue();
535 if (!CheckedZextOrTrunc(NumElems))
539 Size = Size.umul_ov(NumElems, Overflow);
540 return Overflow ? unknown() : std::make_pair(align(Size, I.getAlignment()),
546 SizeOffsetType ObjectSizeOffsetVisitor::visitArgument(Argument &A) {
547 // No interprocedural analysis is done at the moment.
548 if (!A.hasByValOrInAllocaAttr()) {
549 ++ObjectVisitorArgument;
552 PointerType *PT = cast<PointerType>(A.getType());
553 APInt Size(IntTyBits, DL.getTypeAllocSize(PT->getElementType()));
554 return std::make_pair(align(Size, A.getParamAlignment()), Zero);
557 SizeOffsetType ObjectSizeOffsetVisitor::visitCallSite(CallSite CS) {
558 Optional<AllocFnsTy> FnData = getAllocationSize(CS.getInstruction(), TLI);
562 // Handle strdup-like functions separately.
563 if (FnData->AllocTy == StrDupLike) {
564 APInt Size(IntTyBits, GetStringLength(CS.getArgument(0)));
568 // Strndup limits strlen.
569 if (FnData->FstParam > 0) {
571 dyn_cast<ConstantInt>(CS.getArgument(FnData->FstParam));
575 APInt MaxSize = Arg->getValue().zextOrSelf(IntTyBits);
576 if (Size.ugt(MaxSize))
579 return std::make_pair(Size, Zero);
582 ConstantInt *Arg = dyn_cast<ConstantInt>(CS.getArgument(FnData->FstParam));
586 APInt Size = Arg->getValue();
587 if (!CheckedZextOrTrunc(Size))
590 // Size is determined by just 1 parameter.
591 if (FnData->SndParam < 0)
592 return std::make_pair(Size, Zero);
594 Arg = dyn_cast<ConstantInt>(CS.getArgument(FnData->SndParam));
598 APInt NumElems = Arg->getValue();
599 if (!CheckedZextOrTrunc(NumElems))
603 Size = Size.umul_ov(NumElems, Overflow);
604 return Overflow ? unknown() : std::make_pair(Size, Zero);
606 // TODO: handle more standard functions (+ wchar cousins):
607 // - strdup / strndup
608 // - strcpy / strncpy
609 // - strcat / strncat
610 // - memcpy / memmove
611 // - strcat / strncat
616 ObjectSizeOffsetVisitor::visitConstantPointerNull(ConstantPointerNull& CPN) {
617 if (Options.NullIsUnknownSize && CPN.getType()->getAddressSpace() == 0)
619 return std::make_pair(Zero, Zero);
623 ObjectSizeOffsetVisitor::visitExtractElementInst(ExtractElementInst&) {
628 ObjectSizeOffsetVisitor::visitExtractValueInst(ExtractValueInst&) {
629 // Easy cases were already folded by previous passes.
633 SizeOffsetType ObjectSizeOffsetVisitor::visitGEPOperator(GEPOperator &GEP) {
634 SizeOffsetType PtrData = compute(GEP.getPointerOperand());
635 APInt Offset(IntTyBits, 0);
636 if (!bothKnown(PtrData) || !GEP.accumulateConstantOffset(DL, Offset))
639 return std::make_pair(PtrData.first, PtrData.second + Offset);
642 SizeOffsetType ObjectSizeOffsetVisitor::visitGlobalAlias(GlobalAlias &GA) {
643 if (GA.isInterposable())
645 return compute(GA.getAliasee());
648 SizeOffsetType ObjectSizeOffsetVisitor::visitGlobalVariable(GlobalVariable &GV){
649 if (!GV.hasDefinitiveInitializer())
652 APInt Size(IntTyBits, DL.getTypeAllocSize(GV.getType()->getElementType()));
653 return std::make_pair(align(Size, GV.getAlignment()), Zero);
656 SizeOffsetType ObjectSizeOffsetVisitor::visitIntToPtrInst(IntToPtrInst&) {
661 SizeOffsetType ObjectSizeOffsetVisitor::visitLoadInst(LoadInst&) {
666 SizeOffsetType ObjectSizeOffsetVisitor::visitPHINode(PHINode&) {
667 // too complex to analyze statically.
671 SizeOffsetType ObjectSizeOffsetVisitor::visitSelectInst(SelectInst &I) {
672 SizeOffsetType TrueSide = compute(I.getTrueValue());
673 SizeOffsetType FalseSide = compute(I.getFalseValue());
674 if (bothKnown(TrueSide) && bothKnown(FalseSide)) {
675 if (TrueSide == FalseSide) {
679 APInt TrueResult = getSizeWithOverflow(TrueSide);
680 APInt FalseResult = getSizeWithOverflow(FalseSide);
682 if (TrueResult == FalseResult) {
685 if (Options.EvalMode == ObjectSizeOpts::Mode::Min) {
686 if (TrueResult.slt(FalseResult))
690 if (Options.EvalMode == ObjectSizeOpts::Mode::Max) {
691 if (TrueResult.sgt(FalseResult))
699 SizeOffsetType ObjectSizeOffsetVisitor::visitUndefValue(UndefValue&) {
700 return std::make_pair(Zero, Zero);
703 SizeOffsetType ObjectSizeOffsetVisitor::visitInstruction(Instruction &I) {
704 DEBUG(dbgs() << "ObjectSizeOffsetVisitor unknown instruction:" << I << '\n');
708 ObjectSizeOffsetEvaluator::ObjectSizeOffsetEvaluator(
709 const DataLayout &DL, const TargetLibraryInfo *TLI, LLVMContext &Context,
711 : DL(DL), TLI(TLI), Context(Context), Builder(Context, TargetFolder(DL)),
712 RoundToAlign(RoundToAlign) {
713 // IntTy and Zero must be set for each compute() since the address space may
714 // be different for later objects.
717 SizeOffsetEvalType ObjectSizeOffsetEvaluator::compute(Value *V) {
718 // XXX - Are vectors of pointers possible here?
719 IntTy = cast<IntegerType>(DL.getIntPtrType(V->getType()));
720 Zero = ConstantInt::get(IntTy, 0);
722 SizeOffsetEvalType Result = compute_(V);
724 if (!bothKnown(Result)) {
725 // Erase everything that was computed in this iteration from the cache, so
726 // that no dangling references are left behind. We could be a bit smarter if
727 // we kept a dependency graph. It's probably not worth the complexity.
728 for (const Value *SeenVal : SeenVals) {
729 CacheMapTy::iterator CacheIt = CacheMap.find(SeenVal);
730 // non-computable results can be safely cached
731 if (CacheIt != CacheMap.end() && anyKnown(CacheIt->second))
732 CacheMap.erase(CacheIt);
740 SizeOffsetEvalType ObjectSizeOffsetEvaluator::compute_(Value *V) {
741 ObjectSizeOpts ObjSizeOptions;
742 ObjSizeOptions.RoundToAlign = RoundToAlign;
744 ObjectSizeOffsetVisitor Visitor(DL, TLI, Context, ObjSizeOptions);
745 SizeOffsetType Const = Visitor.compute(V);
746 if (Visitor.bothKnown(Const))
747 return std::make_pair(ConstantInt::get(Context, Const.first),
748 ConstantInt::get(Context, Const.second));
750 V = V->stripPointerCasts();
753 CacheMapTy::iterator CacheIt = CacheMap.find(V);
754 if (CacheIt != CacheMap.end())
755 return CacheIt->second;
757 // Always generate code immediately before the instruction being
758 // processed, so that the generated code dominates the same BBs.
759 BuilderTy::InsertPointGuard Guard(Builder);
760 if (Instruction *I = dyn_cast<Instruction>(V))
761 Builder.SetInsertPoint(I);
763 // Now compute the size and offset.
764 SizeOffsetEvalType Result;
766 // Record the pointers that were handled in this run, so that they can be
767 // cleaned later if something fails. We also use this set to break cycles that
768 // can occur in dead code.
769 if (!SeenVals.insert(V).second) {
771 } else if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
772 Result = visitGEPOperator(*GEP);
773 } else if (Instruction *I = dyn_cast<Instruction>(V)) {
775 } else if (isa<Argument>(V) ||
776 (isa<ConstantExpr>(V) &&
777 cast<ConstantExpr>(V)->getOpcode() == Instruction::IntToPtr) ||
778 isa<GlobalAlias>(V) ||
779 isa<GlobalVariable>(V)) {
780 // Ignore values where we cannot do more than ObjectSizeVisitor.
783 DEBUG(dbgs() << "ObjectSizeOffsetEvaluator::compute() unhandled value: "
788 // Don't reuse CacheIt since it may be invalid at this point.
789 CacheMap[V] = Result;
793 SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitAllocaInst(AllocaInst &I) {
794 if (!I.getAllocatedType()->isSized())
798 assert(I.isArrayAllocation());
799 Value *ArraySize = I.getArraySize();
800 Value *Size = ConstantInt::get(ArraySize->getType(),
801 DL.getTypeAllocSize(I.getAllocatedType()));
802 Size = Builder.CreateMul(Size, ArraySize);
803 return std::make_pair(Size, Zero);
806 SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitCallSite(CallSite CS) {
807 Optional<AllocFnsTy> FnData = getAllocationSize(CS.getInstruction(), TLI);
811 // Handle strdup-like functions separately.
812 if (FnData->AllocTy == StrDupLike) {
817 Value *FirstArg = CS.getArgument(FnData->FstParam);
818 FirstArg = Builder.CreateZExt(FirstArg, IntTy);
819 if (FnData->SndParam < 0)
820 return std::make_pair(FirstArg, Zero);
822 Value *SecondArg = CS.getArgument(FnData->SndParam);
823 SecondArg = Builder.CreateZExt(SecondArg, IntTy);
824 Value *Size = Builder.CreateMul(FirstArg, SecondArg);
825 return std::make_pair(Size, Zero);
827 // TODO: handle more standard functions (+ wchar cousins):
828 // - strdup / strndup
829 // - strcpy / strncpy
830 // - strcat / strncat
831 // - memcpy / memmove
832 // - strcat / strncat
837 ObjectSizeOffsetEvaluator::visitExtractElementInst(ExtractElementInst&) {
842 ObjectSizeOffsetEvaluator::visitExtractValueInst(ExtractValueInst&) {
847 ObjectSizeOffsetEvaluator::visitGEPOperator(GEPOperator &GEP) {
848 SizeOffsetEvalType PtrData = compute_(GEP.getPointerOperand());
849 if (!bothKnown(PtrData))
852 Value *Offset = EmitGEPOffset(&Builder, DL, &GEP, /*NoAssumptions=*/true);
853 Offset = Builder.CreateAdd(PtrData.second, Offset);
854 return std::make_pair(PtrData.first, Offset);
857 SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitIntToPtrInst(IntToPtrInst&) {
862 SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitLoadInst(LoadInst&) {
866 SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitPHINode(PHINode &PHI) {
867 // Create 2 PHIs: one for size and another for offset.
868 PHINode *SizePHI = Builder.CreatePHI(IntTy, PHI.getNumIncomingValues());
869 PHINode *OffsetPHI = Builder.CreatePHI(IntTy, PHI.getNumIncomingValues());
871 // Insert right away in the cache to handle recursive PHIs.
872 CacheMap[&PHI] = std::make_pair(SizePHI, OffsetPHI);
874 // Compute offset/size for each PHI incoming pointer.
875 for (unsigned i = 0, e = PHI.getNumIncomingValues(); i != e; ++i) {
876 Builder.SetInsertPoint(&*PHI.getIncomingBlock(i)->getFirstInsertionPt());
877 SizeOffsetEvalType EdgeData = compute_(PHI.getIncomingValue(i));
879 if (!bothKnown(EdgeData)) {
880 OffsetPHI->replaceAllUsesWith(UndefValue::get(IntTy));
881 OffsetPHI->eraseFromParent();
882 SizePHI->replaceAllUsesWith(UndefValue::get(IntTy));
883 SizePHI->eraseFromParent();
886 SizePHI->addIncoming(EdgeData.first, PHI.getIncomingBlock(i));
887 OffsetPHI->addIncoming(EdgeData.second, PHI.getIncomingBlock(i));
890 Value *Size = SizePHI, *Offset = OffsetPHI, *Tmp;
891 if ((Tmp = SizePHI->hasConstantValue())) {
893 SizePHI->replaceAllUsesWith(Size);
894 SizePHI->eraseFromParent();
896 if ((Tmp = OffsetPHI->hasConstantValue())) {
898 OffsetPHI->replaceAllUsesWith(Offset);
899 OffsetPHI->eraseFromParent();
901 return std::make_pair(Size, Offset);
904 SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitSelectInst(SelectInst &I) {
905 SizeOffsetEvalType TrueSide = compute_(I.getTrueValue());
906 SizeOffsetEvalType FalseSide = compute_(I.getFalseValue());
908 if (!bothKnown(TrueSide) || !bothKnown(FalseSide))
910 if (TrueSide == FalseSide)
913 Value *Size = Builder.CreateSelect(I.getCondition(), TrueSide.first,
915 Value *Offset = Builder.CreateSelect(I.getCondition(), TrueSide.second,
917 return std::make_pair(Size, Offset);
920 SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitInstruction(Instruction &I) {
921 DEBUG(dbgs() << "ObjectSizeOffsetEvaluator unknown instruction:" << I <<'\n');