[ARM] Generate 8.1-m CSINC, CSNEG and CSINV instructions.
[llvm-core.git] / lib / Analysis / MemoryBuiltins.cpp
blob337d256d3de40aa4173d3a596f3484ae46cce243
1 //===- MemoryBuiltins.cpp - Identify calls to memory builtins -------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This family of functions identifies calls to builtin functions that allocate
10 // or free memory.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Analysis/MemoryBuiltins.h"
15 #include "llvm/ADT/APInt.h"
16 #include "llvm/ADT/None.h"
17 #include "llvm/ADT/Optional.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/Analysis/TargetFolder.h"
22 #include "llvm/Analysis/TargetLibraryInfo.h"
23 #include "llvm/Analysis/Utils/Local.h"
24 #include "llvm/Analysis/ValueTracking.h"
25 #include "llvm/IR/Argument.h"
26 #include "llvm/IR/Attributes.h"
27 #include "llvm/IR/Constants.h"
28 #include "llvm/IR/DataLayout.h"
29 #include "llvm/IR/DerivedTypes.h"
30 #include "llvm/IR/Function.h"
31 #include "llvm/IR/GlobalAlias.h"
32 #include "llvm/IR/GlobalVariable.h"
33 #include "llvm/IR/Instruction.h"
34 #include "llvm/IR/Instructions.h"
35 #include "llvm/IR/IntrinsicInst.h"
36 #include "llvm/IR/Operator.h"
37 #include "llvm/IR/Type.h"
38 #include "llvm/IR/Value.h"
39 #include "llvm/Support/Casting.h"
40 #include "llvm/Support/Debug.h"
41 #include "llvm/Support/MathExtras.h"
42 #include "llvm/Support/raw_ostream.h"
43 #include <cassert>
44 #include <cstdint>
45 #include <iterator>
46 #include <utility>
48 using namespace llvm;
50 #define DEBUG_TYPE "memory-builtins"
52 enum AllocType : uint8_t {
53 OpNewLike = 1<<0, // allocates; never returns null
54 MallocLike = 1<<1 | OpNewLike, // allocates; may return null
55 CallocLike = 1<<2, // allocates + bzero
56 ReallocLike = 1<<3, // reallocates
57 StrDupLike = 1<<4,
58 MallocOrCallocLike = MallocLike | CallocLike,
59 AllocLike = MallocLike | CallocLike | StrDupLike,
60 AnyAlloc = AllocLike | ReallocLike
63 struct AllocFnsTy {
64 AllocType AllocTy;
65 unsigned NumParams;
66 // First and Second size parameters (or -1 if unused)
67 int FstParam, SndParam;
70 // FIXME: certain users need more information. E.g., SimplifyLibCalls needs to
71 // know which functions are nounwind, noalias, nocapture parameters, etc.
72 static const std::pair<LibFunc, AllocFnsTy> AllocationFnData[] = {
73 {LibFunc_malloc, {MallocLike, 1, 0, -1}},
74 {LibFunc_valloc, {MallocLike, 1, 0, -1}},
75 {LibFunc_Znwj, {OpNewLike, 1, 0, -1}}, // new(unsigned int)
76 {LibFunc_ZnwjRKSt9nothrow_t, {MallocLike, 2, 0, -1}}, // new(unsigned int, nothrow)
77 {LibFunc_ZnwjSt11align_val_t, {OpNewLike, 2, 0, -1}}, // new(unsigned int, align_val_t)
78 {LibFunc_ZnwjSt11align_val_tRKSt9nothrow_t, // new(unsigned int, align_val_t, nothrow)
79 {MallocLike, 3, 0, -1}},
80 {LibFunc_Znwm, {OpNewLike, 1, 0, -1}}, // new(unsigned long)
81 {LibFunc_ZnwmRKSt9nothrow_t, {MallocLike, 2, 0, -1}}, // new(unsigned long, nothrow)
82 {LibFunc_ZnwmSt11align_val_t, {OpNewLike, 2, 0, -1}}, // new(unsigned long, align_val_t)
83 {LibFunc_ZnwmSt11align_val_tRKSt9nothrow_t, // new(unsigned long, align_val_t, nothrow)
84 {MallocLike, 3, 0, -1}},
85 {LibFunc_Znaj, {OpNewLike, 1, 0, -1}}, // new[](unsigned int)
86 {LibFunc_ZnajRKSt9nothrow_t, {MallocLike, 2, 0, -1}}, // new[](unsigned int, nothrow)
87 {LibFunc_ZnajSt11align_val_t, {OpNewLike, 2, 0, -1}}, // new[](unsigned int, align_val_t)
88 {LibFunc_ZnajSt11align_val_tRKSt9nothrow_t, // new[](unsigned int, align_val_t, nothrow)
89 {MallocLike, 3, 0, -1}},
90 {LibFunc_Znam, {OpNewLike, 1, 0, -1}}, // new[](unsigned long)
91 {LibFunc_ZnamRKSt9nothrow_t, {MallocLike, 2, 0, -1}}, // new[](unsigned long, nothrow)
92 {LibFunc_ZnamSt11align_val_t, {OpNewLike, 2, 0, -1}}, // new[](unsigned long, align_val_t)
93 {LibFunc_ZnamSt11align_val_tRKSt9nothrow_t, // new[](unsigned long, align_val_t, nothrow)
94 {MallocLike, 3, 0, -1}},
95 {LibFunc_msvc_new_int, {OpNewLike, 1, 0, -1}}, // new(unsigned int)
96 {LibFunc_msvc_new_int_nothrow, {MallocLike, 2, 0, -1}}, // new(unsigned int, nothrow)
97 {LibFunc_msvc_new_longlong, {OpNewLike, 1, 0, -1}}, // new(unsigned long long)
98 {LibFunc_msvc_new_longlong_nothrow, {MallocLike, 2, 0, -1}}, // new(unsigned long long, nothrow)
99 {LibFunc_msvc_new_array_int, {OpNewLike, 1, 0, -1}}, // new[](unsigned int)
100 {LibFunc_msvc_new_array_int_nothrow, {MallocLike, 2, 0, -1}}, // new[](unsigned int, nothrow)
101 {LibFunc_msvc_new_array_longlong, {OpNewLike, 1, 0, -1}}, // new[](unsigned long long)
102 {LibFunc_msvc_new_array_longlong_nothrow, {MallocLike, 2, 0, -1}}, // new[](unsigned long long, nothrow)
103 {LibFunc_calloc, {CallocLike, 2, 0, 1}},
104 {LibFunc_realloc, {ReallocLike, 2, 1, -1}},
105 {LibFunc_reallocf, {ReallocLike, 2, 1, -1}},
106 {LibFunc_strdup, {StrDupLike, 1, -1, -1}},
107 {LibFunc_strndup, {StrDupLike, 2, 1, -1}}
108 // TODO: Handle "int posix_memalign(void **, size_t, size_t)"
111 static const Function *getCalledFunction(const Value *V, bool LookThroughBitCast,
112 bool &IsNoBuiltin) {
113 // Don't care about intrinsics in this case.
114 if (isa<IntrinsicInst>(V))
115 return nullptr;
117 if (LookThroughBitCast)
118 V = V->stripPointerCasts();
120 ImmutableCallSite CS(V);
121 if (!CS.getInstruction())
122 return nullptr;
124 IsNoBuiltin = CS.isNoBuiltin();
126 if (const Function *Callee = CS.getCalledFunction())
127 return Callee;
128 return nullptr;
131 /// Returns the allocation data for the given value if it's either a call to a
132 /// known allocation function, or a call to a function with the allocsize
133 /// attribute.
134 static Optional<AllocFnsTy>
135 getAllocationDataForFunction(const Function *Callee, AllocType AllocTy,
136 const TargetLibraryInfo *TLI) {
137 // Make sure that the function is available.
138 StringRef FnName = Callee->getName();
139 LibFunc TLIFn;
140 if (!TLI || !TLI->getLibFunc(FnName, TLIFn) || !TLI->has(TLIFn))
141 return None;
143 const auto *Iter = find_if(
144 AllocationFnData, [TLIFn](const std::pair<LibFunc, AllocFnsTy> &P) {
145 return P.first == TLIFn;
148 if (Iter == std::end(AllocationFnData))
149 return None;
151 const AllocFnsTy *FnData = &Iter->second;
152 if ((FnData->AllocTy & AllocTy) != FnData->AllocTy)
153 return None;
155 // Check function prototype.
156 int FstParam = FnData->FstParam;
157 int SndParam = FnData->SndParam;
158 FunctionType *FTy = Callee->getFunctionType();
160 if (FTy->getReturnType() == Type::getInt8PtrTy(FTy->getContext()) &&
161 FTy->getNumParams() == FnData->NumParams &&
162 (FstParam < 0 ||
163 (FTy->getParamType(FstParam)->isIntegerTy(32) ||
164 FTy->getParamType(FstParam)->isIntegerTy(64))) &&
165 (SndParam < 0 ||
166 FTy->getParamType(SndParam)->isIntegerTy(32) ||
167 FTy->getParamType(SndParam)->isIntegerTy(64)))
168 return *FnData;
169 return None;
172 static Optional<AllocFnsTy> getAllocationData(const Value *V, AllocType AllocTy,
173 const TargetLibraryInfo *TLI,
174 bool LookThroughBitCast = false) {
175 bool IsNoBuiltinCall;
176 if (const Function *Callee =
177 getCalledFunction(V, LookThroughBitCast, IsNoBuiltinCall))
178 if (!IsNoBuiltinCall)
179 return getAllocationDataForFunction(Callee, AllocTy, TLI);
180 return None;
183 static Optional<AllocFnsTy> getAllocationSize(const Value *V,
184 const TargetLibraryInfo *TLI) {
185 bool IsNoBuiltinCall;
186 const Function *Callee =
187 getCalledFunction(V, /*LookThroughBitCast=*/false, IsNoBuiltinCall);
188 if (!Callee)
189 return None;
191 // Prefer to use existing information over allocsize. This will give us an
192 // accurate AllocTy.
193 if (!IsNoBuiltinCall)
194 if (Optional<AllocFnsTy> Data =
195 getAllocationDataForFunction(Callee, AnyAlloc, TLI))
196 return Data;
198 Attribute Attr = Callee->getFnAttribute(Attribute::AllocSize);
199 if (Attr == Attribute())
200 return None;
202 std::pair<unsigned, Optional<unsigned>> Args = Attr.getAllocSizeArgs();
204 AllocFnsTy Result;
205 // Because allocsize only tells us how many bytes are allocated, we're not
206 // really allowed to assume anything, so we use MallocLike.
207 Result.AllocTy = MallocLike;
208 Result.NumParams = Callee->getNumOperands();
209 Result.FstParam = Args.first;
210 Result.SndParam = Args.second.getValueOr(-1);
211 return Result;
214 static bool hasNoAliasAttr(const Value *V, bool LookThroughBitCast) {
215 ImmutableCallSite CS(LookThroughBitCast ? V->stripPointerCasts() : V);
216 return CS && CS.hasRetAttr(Attribute::NoAlias);
219 /// Tests if a value is a call or invoke to a library function that
220 /// allocates or reallocates memory (either malloc, calloc, realloc, or strdup
221 /// like).
222 bool llvm::isAllocationFn(const Value *V, const TargetLibraryInfo *TLI,
223 bool LookThroughBitCast) {
224 return getAllocationData(V, AnyAlloc, TLI, LookThroughBitCast).hasValue();
227 /// Tests if a value is a call or invoke to a function that returns a
228 /// NoAlias pointer (including malloc/calloc/realloc/strdup-like functions).
229 bool llvm::isNoAliasFn(const Value *V, const TargetLibraryInfo *TLI,
230 bool LookThroughBitCast) {
231 // it's safe to consider realloc as noalias since accessing the original
232 // pointer is undefined behavior
233 return isAllocationFn(V, TLI, LookThroughBitCast) ||
234 hasNoAliasAttr(V, LookThroughBitCast);
237 /// Tests if a value is a call or invoke to a library function that
238 /// allocates uninitialized memory (such as malloc).
239 bool llvm::isMallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
240 bool LookThroughBitCast) {
241 return getAllocationData(V, MallocLike, TLI, LookThroughBitCast).hasValue();
244 /// Tests if a value is a call or invoke to a library function that
245 /// allocates zero-filled memory (such as calloc).
246 bool llvm::isCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
247 bool LookThroughBitCast) {
248 return getAllocationData(V, CallocLike, TLI, LookThroughBitCast).hasValue();
251 /// Tests if a value is a call or invoke to a library function that
252 /// allocates memory similar to malloc or calloc.
253 bool llvm::isMallocOrCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
254 bool LookThroughBitCast) {
255 return getAllocationData(V, MallocOrCallocLike, TLI,
256 LookThroughBitCast).hasValue();
259 /// Tests if a value is a call or invoke to a library function that
260 /// allocates memory (either malloc, calloc, or strdup like).
261 bool llvm::isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
262 bool LookThroughBitCast) {
263 return getAllocationData(V, AllocLike, TLI, LookThroughBitCast).hasValue();
266 /// Tests if a value is a call or invoke to a library function that
267 /// reallocates memory (e.g., realloc).
268 bool llvm::isReallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
269 bool LookThroughBitCast) {
270 return getAllocationData(V, ReallocLike, TLI, LookThroughBitCast).hasValue();
273 /// Tests if a functions is a call or invoke to a library function that
274 /// reallocates memory (e.g., realloc).
275 bool llvm::isReallocLikeFn(const Function *F, const TargetLibraryInfo *TLI) {
276 return getAllocationDataForFunction(F, ReallocLike, TLI).hasValue();
279 /// Tests if a value is a call or invoke to a library function that
280 /// allocates memory and throws if an allocation failed (e.g., new).
281 bool llvm::isOpNewLikeFn(const Value *V, const TargetLibraryInfo *TLI,
282 bool LookThroughBitCast) {
283 return getAllocationData(V, OpNewLike, TLI, LookThroughBitCast).hasValue();
286 /// extractMallocCall - Returns the corresponding CallInst if the instruction
287 /// is a malloc call. Since CallInst::CreateMalloc() only creates calls, we
288 /// ignore InvokeInst here.
289 const CallInst *llvm::extractMallocCall(const Value *I,
290 const TargetLibraryInfo *TLI) {
291 return isMallocLikeFn(I, TLI) ? dyn_cast<CallInst>(I) : nullptr;
294 static Value *computeArraySize(const CallInst *CI, const DataLayout &DL,
295 const TargetLibraryInfo *TLI,
296 bool LookThroughSExt = false) {
297 if (!CI)
298 return nullptr;
300 // The size of the malloc's result type must be known to determine array size.
301 Type *T = getMallocAllocatedType(CI, TLI);
302 if (!T || !T->isSized())
303 return nullptr;
305 unsigned ElementSize = DL.getTypeAllocSize(T);
306 if (StructType *ST = dyn_cast<StructType>(T))
307 ElementSize = DL.getStructLayout(ST)->getSizeInBytes();
309 // If malloc call's arg can be determined to be a multiple of ElementSize,
310 // return the multiple. Otherwise, return NULL.
311 Value *MallocArg = CI->getArgOperand(0);
312 Value *Multiple = nullptr;
313 if (ComputeMultiple(MallocArg, ElementSize, Multiple, LookThroughSExt))
314 return Multiple;
316 return nullptr;
319 /// getMallocType - Returns the PointerType resulting from the malloc call.
320 /// The PointerType depends on the number of bitcast uses of the malloc call:
321 /// 0: PointerType is the calls' return type.
322 /// 1: PointerType is the bitcast's result type.
323 /// >1: Unique PointerType cannot be determined, return NULL.
324 PointerType *llvm::getMallocType(const CallInst *CI,
325 const TargetLibraryInfo *TLI) {
326 assert(isMallocLikeFn(CI, TLI) && "getMallocType and not malloc call");
328 PointerType *MallocType = nullptr;
329 unsigned NumOfBitCastUses = 0;
331 // Determine if CallInst has a bitcast use.
332 for (Value::const_user_iterator UI = CI->user_begin(), E = CI->user_end();
333 UI != E;)
334 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(*UI++)) {
335 MallocType = cast<PointerType>(BCI->getDestTy());
336 NumOfBitCastUses++;
339 // Malloc call has 1 bitcast use, so type is the bitcast's destination type.
340 if (NumOfBitCastUses == 1)
341 return MallocType;
343 // Malloc call was not bitcast, so type is the malloc function's return type.
344 if (NumOfBitCastUses == 0)
345 return cast<PointerType>(CI->getType());
347 // Type could not be determined.
348 return nullptr;
351 /// getMallocAllocatedType - Returns the Type allocated by malloc call.
352 /// The Type depends on the number of bitcast uses of the malloc call:
353 /// 0: PointerType is the malloc calls' return type.
354 /// 1: PointerType is the bitcast's result type.
355 /// >1: Unique PointerType cannot be determined, return NULL.
356 Type *llvm::getMallocAllocatedType(const CallInst *CI,
357 const TargetLibraryInfo *TLI) {
358 PointerType *PT = getMallocType(CI, TLI);
359 return PT ? PT->getElementType() : nullptr;
362 /// getMallocArraySize - Returns the array size of a malloc call. If the
363 /// argument passed to malloc is a multiple of the size of the malloced type,
364 /// then return that multiple. For non-array mallocs, the multiple is
365 /// constant 1. Otherwise, return NULL for mallocs whose array size cannot be
366 /// determined.
367 Value *llvm::getMallocArraySize(CallInst *CI, const DataLayout &DL,
368 const TargetLibraryInfo *TLI,
369 bool LookThroughSExt) {
370 assert(isMallocLikeFn(CI, TLI) && "getMallocArraySize and not malloc call");
371 return computeArraySize(CI, DL, TLI, LookThroughSExt);
374 /// extractCallocCall - Returns the corresponding CallInst if the instruction
375 /// is a calloc call.
376 const CallInst *llvm::extractCallocCall(const Value *I,
377 const TargetLibraryInfo *TLI) {
378 return isCallocLikeFn(I, TLI) ? cast<CallInst>(I) : nullptr;
381 /// isLibFreeFunction - Returns true if the function is a builtin free()
382 bool llvm::isLibFreeFunction(const Function *F, const LibFunc TLIFn) {
383 unsigned ExpectedNumParams;
384 if (TLIFn == LibFunc_free ||
385 TLIFn == LibFunc_ZdlPv || // operator delete(void*)
386 TLIFn == LibFunc_ZdaPv || // operator delete[](void*)
387 TLIFn == LibFunc_msvc_delete_ptr32 || // operator delete(void*)
388 TLIFn == LibFunc_msvc_delete_ptr64 || // operator delete(void*)
389 TLIFn == LibFunc_msvc_delete_array_ptr32 || // operator delete[](void*)
390 TLIFn == LibFunc_msvc_delete_array_ptr64) // operator delete[](void*)
391 ExpectedNumParams = 1;
392 else if (TLIFn == LibFunc_ZdlPvj || // delete(void*, uint)
393 TLIFn == LibFunc_ZdlPvm || // delete(void*, ulong)
394 TLIFn == LibFunc_ZdlPvRKSt9nothrow_t || // delete(void*, nothrow)
395 TLIFn == LibFunc_ZdlPvSt11align_val_t || // delete(void*, align_val_t)
396 TLIFn == LibFunc_ZdaPvj || // delete[](void*, uint)
397 TLIFn == LibFunc_ZdaPvm || // delete[](void*, ulong)
398 TLIFn == LibFunc_ZdaPvRKSt9nothrow_t || // delete[](void*, nothrow)
399 TLIFn == LibFunc_ZdaPvSt11align_val_t || // delete[](void*, align_val_t)
400 TLIFn == LibFunc_msvc_delete_ptr32_int || // delete(void*, uint)
401 TLIFn == LibFunc_msvc_delete_ptr64_longlong || // delete(void*, ulonglong)
402 TLIFn == LibFunc_msvc_delete_ptr32_nothrow || // delete(void*, nothrow)
403 TLIFn == LibFunc_msvc_delete_ptr64_nothrow || // delete(void*, nothrow)
404 TLIFn == LibFunc_msvc_delete_array_ptr32_int || // delete[](void*, uint)
405 TLIFn == LibFunc_msvc_delete_array_ptr64_longlong || // delete[](void*, ulonglong)
406 TLIFn == LibFunc_msvc_delete_array_ptr32_nothrow || // delete[](void*, nothrow)
407 TLIFn == LibFunc_msvc_delete_array_ptr64_nothrow) // delete[](void*, nothrow)
408 ExpectedNumParams = 2;
409 else if (TLIFn == LibFunc_ZdaPvSt11align_val_tRKSt9nothrow_t || // delete(void*, align_val_t, nothrow)
410 TLIFn == LibFunc_ZdlPvSt11align_val_tRKSt9nothrow_t) // delete[](void*, align_val_t, nothrow)
411 ExpectedNumParams = 3;
412 else
413 return false;
415 // Check free prototype.
416 // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin
417 // attribute will exist.
418 FunctionType *FTy = F->getFunctionType();
419 if (!FTy->getReturnType()->isVoidTy())
420 return false;
421 if (FTy->getNumParams() != ExpectedNumParams)
422 return false;
423 if (FTy->getParamType(0) != Type::getInt8PtrTy(F->getContext()))
424 return false;
426 return true;
429 /// isFreeCall - Returns non-null if the value is a call to the builtin free()
430 const CallInst *llvm::isFreeCall(const Value *I, const TargetLibraryInfo *TLI) {
431 bool IsNoBuiltinCall;
432 const Function *Callee =
433 getCalledFunction(I, /*LookThroughBitCast=*/false, IsNoBuiltinCall);
434 if (Callee == nullptr || IsNoBuiltinCall)
435 return nullptr;
437 StringRef FnName = Callee->getName();
438 LibFunc TLIFn;
439 if (!TLI || !TLI->getLibFunc(FnName, TLIFn) || !TLI->has(TLIFn))
440 return nullptr;
442 return isLibFreeFunction(Callee, TLIFn) ? dyn_cast<CallInst>(I) : nullptr;
446 //===----------------------------------------------------------------------===//
447 // Utility functions to compute size of objects.
449 static APInt getSizeWithOverflow(const SizeOffsetType &Data) {
450 if (Data.second.isNegative() || Data.first.ult(Data.second))
451 return APInt(Data.first.getBitWidth(), 0);
452 return Data.first - Data.second;
455 /// Compute the size of the object pointed by Ptr. Returns true and the
456 /// object size in Size if successful, and false otherwise.
457 /// If RoundToAlign is true, then Size is rounded up to the alignment of
458 /// allocas, byval arguments, and global variables.
459 bool llvm::getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout &DL,
460 const TargetLibraryInfo *TLI, ObjectSizeOpts Opts) {
461 ObjectSizeOffsetVisitor Visitor(DL, TLI, Ptr->getContext(), Opts);
462 SizeOffsetType Data = Visitor.compute(const_cast<Value*>(Ptr));
463 if (!Visitor.bothKnown(Data))
464 return false;
466 Size = getSizeWithOverflow(Data).getZExtValue();
467 return true;
470 Value *llvm::lowerObjectSizeCall(IntrinsicInst *ObjectSize,
471 const DataLayout &DL,
472 const TargetLibraryInfo *TLI,
473 bool MustSucceed) {
474 assert(ObjectSize->getIntrinsicID() == Intrinsic::objectsize &&
475 "ObjectSize must be a call to llvm.objectsize!");
477 bool MaxVal = cast<ConstantInt>(ObjectSize->getArgOperand(1))->isZero();
478 ObjectSizeOpts EvalOptions;
479 // Unless we have to fold this to something, try to be as accurate as
480 // possible.
481 if (MustSucceed)
482 EvalOptions.EvalMode =
483 MaxVal ? ObjectSizeOpts::Mode::Max : ObjectSizeOpts::Mode::Min;
484 else
485 EvalOptions.EvalMode = ObjectSizeOpts::Mode::Exact;
487 EvalOptions.NullIsUnknownSize =
488 cast<ConstantInt>(ObjectSize->getArgOperand(2))->isOne();
490 auto *ResultType = cast<IntegerType>(ObjectSize->getType());
491 bool StaticOnly = cast<ConstantInt>(ObjectSize->getArgOperand(3))->isZero();
492 if (StaticOnly) {
493 // FIXME: Does it make sense to just return a failure value if the size won't
494 // fit in the output and `!MustSucceed`?
495 uint64_t Size;
496 if (getObjectSize(ObjectSize->getArgOperand(0), Size, DL, TLI, EvalOptions) &&
497 isUIntN(ResultType->getBitWidth(), Size))
498 return ConstantInt::get(ResultType, Size);
499 } else {
500 LLVMContext &Ctx = ObjectSize->getFunction()->getContext();
501 ObjectSizeOffsetEvaluator Eval(DL, TLI, Ctx, EvalOptions);
502 SizeOffsetEvalType SizeOffsetPair =
503 Eval.compute(ObjectSize->getArgOperand(0));
505 if (SizeOffsetPair != ObjectSizeOffsetEvaluator::unknown()) {
506 IRBuilder<TargetFolder> Builder(Ctx, TargetFolder(DL));
507 Builder.SetInsertPoint(ObjectSize);
509 // If we've outside the end of the object, then we can always access
510 // exactly 0 bytes.
511 Value *ResultSize =
512 Builder.CreateSub(SizeOffsetPair.first, SizeOffsetPair.second);
513 Value *UseZero =
514 Builder.CreateICmpULT(SizeOffsetPair.first, SizeOffsetPair.second);
515 return Builder.CreateSelect(UseZero, ConstantInt::get(ResultType, 0),
516 ResultSize);
520 if (!MustSucceed)
521 return nullptr;
523 return ConstantInt::get(ResultType, MaxVal ? -1ULL : 0);
526 STATISTIC(ObjectVisitorArgument,
527 "Number of arguments with unsolved size and offset");
528 STATISTIC(ObjectVisitorLoad,
529 "Number of load instructions with unsolved size and offset");
531 APInt ObjectSizeOffsetVisitor::align(APInt Size, uint64_t Align) {
532 if (Options.RoundToAlign && Align)
533 return APInt(IntTyBits, alignTo(Size.getZExtValue(), llvm::Align(Align)));
534 return Size;
537 ObjectSizeOffsetVisitor::ObjectSizeOffsetVisitor(const DataLayout &DL,
538 const TargetLibraryInfo *TLI,
539 LLVMContext &Context,
540 ObjectSizeOpts Options)
541 : DL(DL), TLI(TLI), Options(Options) {
542 // Pointer size must be rechecked for each object visited since it could have
543 // a different address space.
546 SizeOffsetType ObjectSizeOffsetVisitor::compute(Value *V) {
547 IntTyBits = DL.getPointerTypeSizeInBits(V->getType());
548 Zero = APInt::getNullValue(IntTyBits);
550 V = V->stripPointerCasts();
551 if (Instruction *I = dyn_cast<Instruction>(V)) {
552 // If we have already seen this instruction, bail out. Cycles can happen in
553 // unreachable code after constant propagation.
554 if (!SeenInsts.insert(I).second)
555 return unknown();
557 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V))
558 return visitGEPOperator(*GEP);
559 return visit(*I);
561 if (Argument *A = dyn_cast<Argument>(V))
562 return visitArgument(*A);
563 if (ConstantPointerNull *P = dyn_cast<ConstantPointerNull>(V))
564 return visitConstantPointerNull(*P);
565 if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
566 return visitGlobalAlias(*GA);
567 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
568 return visitGlobalVariable(*GV);
569 if (UndefValue *UV = dyn_cast<UndefValue>(V))
570 return visitUndefValue(*UV);
571 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
572 if (CE->getOpcode() == Instruction::IntToPtr)
573 return unknown(); // clueless
574 if (CE->getOpcode() == Instruction::GetElementPtr)
575 return visitGEPOperator(cast<GEPOperator>(*CE));
578 LLVM_DEBUG(dbgs() << "ObjectSizeOffsetVisitor::compute() unhandled value: "
579 << *V << '\n');
580 return unknown();
583 /// When we're compiling N-bit code, and the user uses parameters that are
584 /// greater than N bits (e.g. uint64_t on a 32-bit build), we can run into
585 /// trouble with APInt size issues. This function handles resizing + overflow
586 /// checks for us. Check and zext or trunc \p I depending on IntTyBits and
587 /// I's value.
588 bool ObjectSizeOffsetVisitor::CheckedZextOrTrunc(APInt &I) {
589 // More bits than we can handle. Checking the bit width isn't necessary, but
590 // it's faster than checking active bits, and should give `false` in the
591 // vast majority of cases.
592 if (I.getBitWidth() > IntTyBits && I.getActiveBits() > IntTyBits)
593 return false;
594 if (I.getBitWidth() != IntTyBits)
595 I = I.zextOrTrunc(IntTyBits);
596 return true;
599 SizeOffsetType ObjectSizeOffsetVisitor::visitAllocaInst(AllocaInst &I) {
600 if (!I.getAllocatedType()->isSized())
601 return unknown();
603 APInt Size(IntTyBits, DL.getTypeAllocSize(I.getAllocatedType()));
604 if (!I.isArrayAllocation())
605 return std::make_pair(align(Size, I.getAlignment()), Zero);
607 Value *ArraySize = I.getArraySize();
608 if (const ConstantInt *C = dyn_cast<ConstantInt>(ArraySize)) {
609 APInt NumElems = C->getValue();
610 if (!CheckedZextOrTrunc(NumElems))
611 return unknown();
613 bool Overflow;
614 Size = Size.umul_ov(NumElems, Overflow);
615 return Overflow ? unknown() : std::make_pair(align(Size, I.getAlignment()),
616 Zero);
618 return unknown();
621 SizeOffsetType ObjectSizeOffsetVisitor::visitArgument(Argument &A) {
622 // No interprocedural analysis is done at the moment.
623 if (!A.hasByValOrInAllocaAttr()) {
624 ++ObjectVisitorArgument;
625 return unknown();
627 PointerType *PT = cast<PointerType>(A.getType());
628 APInt Size(IntTyBits, DL.getTypeAllocSize(PT->getElementType()));
629 return std::make_pair(align(Size, A.getParamAlignment()), Zero);
632 SizeOffsetType ObjectSizeOffsetVisitor::visitCallSite(CallSite CS) {
633 Optional<AllocFnsTy> FnData = getAllocationSize(CS.getInstruction(), TLI);
634 if (!FnData)
635 return unknown();
637 // Handle strdup-like functions separately.
638 if (FnData->AllocTy == StrDupLike) {
639 APInt Size(IntTyBits, GetStringLength(CS.getArgument(0)));
640 if (!Size)
641 return unknown();
643 // Strndup limits strlen.
644 if (FnData->FstParam > 0) {
645 ConstantInt *Arg =
646 dyn_cast<ConstantInt>(CS.getArgument(FnData->FstParam));
647 if (!Arg)
648 return unknown();
650 APInt MaxSize = Arg->getValue().zextOrSelf(IntTyBits);
651 if (Size.ugt(MaxSize))
652 Size = MaxSize + 1;
654 return std::make_pair(Size, Zero);
657 ConstantInt *Arg = dyn_cast<ConstantInt>(CS.getArgument(FnData->FstParam));
658 if (!Arg)
659 return unknown();
661 APInt Size = Arg->getValue();
662 if (!CheckedZextOrTrunc(Size))
663 return unknown();
665 // Size is determined by just 1 parameter.
666 if (FnData->SndParam < 0)
667 return std::make_pair(Size, Zero);
669 Arg = dyn_cast<ConstantInt>(CS.getArgument(FnData->SndParam));
670 if (!Arg)
671 return unknown();
673 APInt NumElems = Arg->getValue();
674 if (!CheckedZextOrTrunc(NumElems))
675 return unknown();
677 bool Overflow;
678 Size = Size.umul_ov(NumElems, Overflow);
679 return Overflow ? unknown() : std::make_pair(Size, Zero);
681 // TODO: handle more standard functions (+ wchar cousins):
682 // - strdup / strndup
683 // - strcpy / strncpy
684 // - strcat / strncat
685 // - memcpy / memmove
686 // - strcat / strncat
687 // - memset
690 SizeOffsetType
691 ObjectSizeOffsetVisitor::visitConstantPointerNull(ConstantPointerNull& CPN) {
692 // If null is unknown, there's nothing we can do. Additionally, non-zero
693 // address spaces can make use of null, so we don't presume to know anything
694 // about that.
696 // TODO: How should this work with address space casts? We currently just drop
697 // them on the floor, but it's unclear what we should do when a NULL from
698 // addrspace(1) gets casted to addrspace(0) (or vice-versa).
699 if (Options.NullIsUnknownSize || CPN.getType()->getAddressSpace())
700 return unknown();
701 return std::make_pair(Zero, Zero);
704 SizeOffsetType
705 ObjectSizeOffsetVisitor::visitExtractElementInst(ExtractElementInst&) {
706 return unknown();
709 SizeOffsetType
710 ObjectSizeOffsetVisitor::visitExtractValueInst(ExtractValueInst&) {
711 // Easy cases were already folded by previous passes.
712 return unknown();
715 SizeOffsetType ObjectSizeOffsetVisitor::visitGEPOperator(GEPOperator &GEP) {
716 SizeOffsetType PtrData = compute(GEP.getPointerOperand());
717 APInt Offset(IntTyBits, 0);
718 if (!bothKnown(PtrData) || !GEP.accumulateConstantOffset(DL, Offset))
719 return unknown();
721 return std::make_pair(PtrData.first, PtrData.second + Offset);
724 SizeOffsetType ObjectSizeOffsetVisitor::visitGlobalAlias(GlobalAlias &GA) {
725 if (GA.isInterposable())
726 return unknown();
727 return compute(GA.getAliasee());
730 SizeOffsetType ObjectSizeOffsetVisitor::visitGlobalVariable(GlobalVariable &GV){
731 if (!GV.hasDefinitiveInitializer())
732 return unknown();
734 APInt Size(IntTyBits, DL.getTypeAllocSize(GV.getValueType()));
735 return std::make_pair(align(Size, GV.getAlignment()), Zero);
738 SizeOffsetType ObjectSizeOffsetVisitor::visitIntToPtrInst(IntToPtrInst&) {
739 // clueless
740 return unknown();
743 SizeOffsetType ObjectSizeOffsetVisitor::visitLoadInst(LoadInst&) {
744 ++ObjectVisitorLoad;
745 return unknown();
748 SizeOffsetType ObjectSizeOffsetVisitor::visitPHINode(PHINode&) {
749 // too complex to analyze statically.
750 return unknown();
753 SizeOffsetType ObjectSizeOffsetVisitor::visitSelectInst(SelectInst &I) {
754 SizeOffsetType TrueSide = compute(I.getTrueValue());
755 SizeOffsetType FalseSide = compute(I.getFalseValue());
756 if (bothKnown(TrueSide) && bothKnown(FalseSide)) {
757 if (TrueSide == FalseSide) {
758 return TrueSide;
761 APInt TrueResult = getSizeWithOverflow(TrueSide);
762 APInt FalseResult = getSizeWithOverflow(FalseSide);
764 if (TrueResult == FalseResult) {
765 return TrueSide;
767 if (Options.EvalMode == ObjectSizeOpts::Mode::Min) {
768 if (TrueResult.slt(FalseResult))
769 return TrueSide;
770 return FalseSide;
772 if (Options.EvalMode == ObjectSizeOpts::Mode::Max) {
773 if (TrueResult.sgt(FalseResult))
774 return TrueSide;
775 return FalseSide;
778 return unknown();
781 SizeOffsetType ObjectSizeOffsetVisitor::visitUndefValue(UndefValue&) {
782 return std::make_pair(Zero, Zero);
785 SizeOffsetType ObjectSizeOffsetVisitor::visitInstruction(Instruction &I) {
786 LLVM_DEBUG(dbgs() << "ObjectSizeOffsetVisitor unknown instruction:" << I
787 << '\n');
788 return unknown();
791 ObjectSizeOffsetEvaluator::ObjectSizeOffsetEvaluator(
792 const DataLayout &DL, const TargetLibraryInfo *TLI, LLVMContext &Context,
793 ObjectSizeOpts EvalOpts)
794 : DL(DL), TLI(TLI), Context(Context),
795 Builder(Context, TargetFolder(DL),
796 IRBuilderCallbackInserter(
797 [&](Instruction *I) { InsertedInstructions.insert(I); })),
798 EvalOpts(EvalOpts) {
799 // IntTy and Zero must be set for each compute() since the address space may
800 // be different for later objects.
803 SizeOffsetEvalType ObjectSizeOffsetEvaluator::compute(Value *V) {
804 // XXX - Are vectors of pointers possible here?
805 IntTy = cast<IntegerType>(DL.getIntPtrType(V->getType()));
806 Zero = ConstantInt::get(IntTy, 0);
808 SizeOffsetEvalType Result = compute_(V);
810 if (!bothKnown(Result)) {
811 // Erase everything that was computed in this iteration from the cache, so
812 // that no dangling references are left behind. We could be a bit smarter if
813 // we kept a dependency graph. It's probably not worth the complexity.
814 for (const Value *SeenVal : SeenVals) {
815 CacheMapTy::iterator CacheIt = CacheMap.find(SeenVal);
816 // non-computable results can be safely cached
817 if (CacheIt != CacheMap.end() && anyKnown(CacheIt->second))
818 CacheMap.erase(CacheIt);
821 // Erase any instructions we inserted as part of the traversal.
822 for (Instruction *I : InsertedInstructions) {
823 I->replaceAllUsesWith(UndefValue::get(I->getType()));
824 I->eraseFromParent();
828 SeenVals.clear();
829 InsertedInstructions.clear();
830 return Result;
833 SizeOffsetEvalType ObjectSizeOffsetEvaluator::compute_(Value *V) {
834 ObjectSizeOffsetVisitor Visitor(DL, TLI, Context, EvalOpts);
835 SizeOffsetType Const = Visitor.compute(V);
836 if (Visitor.bothKnown(Const))
837 return std::make_pair(ConstantInt::get(Context, Const.first),
838 ConstantInt::get(Context, Const.second));
840 V = V->stripPointerCasts();
842 // Check cache.
843 CacheMapTy::iterator CacheIt = CacheMap.find(V);
844 if (CacheIt != CacheMap.end())
845 return CacheIt->second;
847 // Always generate code immediately before the instruction being
848 // processed, so that the generated code dominates the same BBs.
849 BuilderTy::InsertPointGuard Guard(Builder);
850 if (Instruction *I = dyn_cast<Instruction>(V))
851 Builder.SetInsertPoint(I);
853 // Now compute the size and offset.
854 SizeOffsetEvalType Result;
856 // Record the pointers that were handled in this run, so that they can be
857 // cleaned later if something fails. We also use this set to break cycles that
858 // can occur in dead code.
859 if (!SeenVals.insert(V).second) {
860 Result = unknown();
861 } else if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
862 Result = visitGEPOperator(*GEP);
863 } else if (Instruction *I = dyn_cast<Instruction>(V)) {
864 Result = visit(*I);
865 } else if (isa<Argument>(V) ||
866 (isa<ConstantExpr>(V) &&
867 cast<ConstantExpr>(V)->getOpcode() == Instruction::IntToPtr) ||
868 isa<GlobalAlias>(V) ||
869 isa<GlobalVariable>(V)) {
870 // Ignore values where we cannot do more than ObjectSizeVisitor.
871 Result = unknown();
872 } else {
873 LLVM_DEBUG(
874 dbgs() << "ObjectSizeOffsetEvaluator::compute() unhandled value: " << *V
875 << '\n');
876 Result = unknown();
879 // Don't reuse CacheIt since it may be invalid at this point.
880 CacheMap[V] = Result;
881 return Result;
884 SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitAllocaInst(AllocaInst &I) {
885 if (!I.getAllocatedType()->isSized())
886 return unknown();
888 // must be a VLA
889 assert(I.isArrayAllocation());
890 Value *ArraySize = I.getArraySize();
891 Value *Size = ConstantInt::get(ArraySize->getType(),
892 DL.getTypeAllocSize(I.getAllocatedType()));
893 Size = Builder.CreateMul(Size, ArraySize);
894 return std::make_pair(Size, Zero);
897 SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitCallSite(CallSite CS) {
898 Optional<AllocFnsTy> FnData = getAllocationSize(CS.getInstruction(), TLI);
899 if (!FnData)
900 return unknown();
902 // Handle strdup-like functions separately.
903 if (FnData->AllocTy == StrDupLike) {
904 // TODO
905 return unknown();
908 Value *FirstArg = CS.getArgument(FnData->FstParam);
909 FirstArg = Builder.CreateZExt(FirstArg, IntTy);
910 if (FnData->SndParam < 0)
911 return std::make_pair(FirstArg, Zero);
913 Value *SecondArg = CS.getArgument(FnData->SndParam);
914 SecondArg = Builder.CreateZExt(SecondArg, IntTy);
915 Value *Size = Builder.CreateMul(FirstArg, SecondArg);
916 return std::make_pair(Size, Zero);
918 // TODO: handle more standard functions (+ wchar cousins):
919 // - strdup / strndup
920 // - strcpy / strncpy
921 // - strcat / strncat
922 // - memcpy / memmove
923 // - strcat / strncat
924 // - memset
927 SizeOffsetEvalType
928 ObjectSizeOffsetEvaluator::visitExtractElementInst(ExtractElementInst&) {
929 return unknown();
932 SizeOffsetEvalType
933 ObjectSizeOffsetEvaluator::visitExtractValueInst(ExtractValueInst&) {
934 return unknown();
937 SizeOffsetEvalType
938 ObjectSizeOffsetEvaluator::visitGEPOperator(GEPOperator &GEP) {
939 SizeOffsetEvalType PtrData = compute_(GEP.getPointerOperand());
940 if (!bothKnown(PtrData))
941 return unknown();
943 Value *Offset = EmitGEPOffset(&Builder, DL, &GEP, /*NoAssumptions=*/true);
944 Offset = Builder.CreateAdd(PtrData.second, Offset);
945 return std::make_pair(PtrData.first, Offset);
948 SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitIntToPtrInst(IntToPtrInst&) {
949 // clueless
950 return unknown();
953 SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitLoadInst(LoadInst&) {
954 return unknown();
957 SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitPHINode(PHINode &PHI) {
958 // Create 2 PHIs: one for size and another for offset.
959 PHINode *SizePHI = Builder.CreatePHI(IntTy, PHI.getNumIncomingValues());
960 PHINode *OffsetPHI = Builder.CreatePHI(IntTy, PHI.getNumIncomingValues());
962 // Insert right away in the cache to handle recursive PHIs.
963 CacheMap[&PHI] = std::make_pair(SizePHI, OffsetPHI);
965 // Compute offset/size for each PHI incoming pointer.
966 for (unsigned i = 0, e = PHI.getNumIncomingValues(); i != e; ++i) {
967 Builder.SetInsertPoint(&*PHI.getIncomingBlock(i)->getFirstInsertionPt());
968 SizeOffsetEvalType EdgeData = compute_(PHI.getIncomingValue(i));
970 if (!bothKnown(EdgeData)) {
971 OffsetPHI->replaceAllUsesWith(UndefValue::get(IntTy));
972 OffsetPHI->eraseFromParent();
973 InsertedInstructions.erase(OffsetPHI);
974 SizePHI->replaceAllUsesWith(UndefValue::get(IntTy));
975 SizePHI->eraseFromParent();
976 InsertedInstructions.erase(SizePHI);
977 return unknown();
979 SizePHI->addIncoming(EdgeData.first, PHI.getIncomingBlock(i));
980 OffsetPHI->addIncoming(EdgeData.second, PHI.getIncomingBlock(i));
983 Value *Size = SizePHI, *Offset = OffsetPHI;
984 if (Value *Tmp = SizePHI->hasConstantValue()) {
985 Size = Tmp;
986 SizePHI->replaceAllUsesWith(Size);
987 SizePHI->eraseFromParent();
988 InsertedInstructions.erase(SizePHI);
990 if (Value *Tmp = OffsetPHI->hasConstantValue()) {
991 Offset = Tmp;
992 OffsetPHI->replaceAllUsesWith(Offset);
993 OffsetPHI->eraseFromParent();
994 InsertedInstructions.erase(OffsetPHI);
996 return std::make_pair(Size, Offset);
999 SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitSelectInst(SelectInst &I) {
1000 SizeOffsetEvalType TrueSide = compute_(I.getTrueValue());
1001 SizeOffsetEvalType FalseSide = compute_(I.getFalseValue());
1003 if (!bothKnown(TrueSide) || !bothKnown(FalseSide))
1004 return unknown();
1005 if (TrueSide == FalseSide)
1006 return TrueSide;
1008 Value *Size = Builder.CreateSelect(I.getCondition(), TrueSide.first,
1009 FalseSide.first);
1010 Value *Offset = Builder.CreateSelect(I.getCondition(), TrueSide.second,
1011 FalseSide.second);
1012 return std::make_pair(Size, Offset);
1015 SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitInstruction(Instruction &I) {
1016 LLVM_DEBUG(dbgs() << "ObjectSizeOffsetEvaluator unknown instruction:" << I
1017 << '\n');
1018 return unknown();