LLVM  9.0.0svn
MemoryBuiltins.cpp
Go to the documentation of this file.
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.
11 //
12 //===----------------------------------------------------------------------===//
13 
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"
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"
43 #include <cassert>
44 #include <cstdint>
45 #include <iterator>
46 #include <utility>
47 
48 using namespace llvm;
49 
50 #define DEBUG_TYPE "memory-builtins"
51 
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,
61 };
62 
63 struct AllocFnsTy {
65  unsigned NumParams;
66  // First and Second size parameters (or -1 if unused)
67  int FstParam, SndParam;
68 };
69 
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)"
109 };
110 
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;
116 
117  if (LookThroughBitCast)
118  V = V->stripPointerCasts();
119 
120  ImmutableCallSite CS(V);
121  if (!CS.getInstruction())
122  return nullptr;
123 
124  IsNoBuiltin = CS.isNoBuiltin();
125 
126  if (const Function *Callee = CS.getCalledFunction())
127  return Callee;
128  return nullptr;
129 }
130 
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.
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;
142 
143  const auto *Iter = find_if(
144  AllocationFnData, [TLIFn](const std::pair<LibFunc, AllocFnsTy> &P) {
145  return P.first == TLIFn;
146  });
147 
148  if (Iter == std::end(AllocationFnData))
149  return None;
150 
151  const AllocFnsTy *FnData = &Iter->second;
152  if ((FnData->AllocTy & AllocTy) != FnData->AllocTy)
153  return None;
154 
155  // Check function prototype.
156  int FstParam = FnData->FstParam;
157  int SndParam = FnData->SndParam;
158  FunctionType *FTy = Callee->getFunctionType();
159 
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;
170 }
171 
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;
181 }
182 
184  const TargetLibraryInfo *TLI) {
185  bool IsNoBuiltinCall;
186  const Function *Callee =
187  getCalledFunction(V, /*LookThroughBitCast=*/false, IsNoBuiltinCall);
188  if (!Callee)
189  return None;
190 
191  // Prefer to use existing information over allocsize. This will give us an
192  // accurate AllocTy.
193  if (!IsNoBuiltinCall)
196  return Data;
197 
198  Attribute Attr = Callee->getFnAttribute(Attribute::AllocSize);
199  if (Attr == Attribute())
200  return None;
201 
202  std::pair<unsigned, Optional<unsigned>> Args = Attr.getAllocSizeArgs();
203 
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;
212 }
213 
214 static bool hasNoAliasAttr(const Value *V, bool LookThroughBitCast) {
215  ImmutableCallSite CS(LookThroughBitCast ? V->stripPointerCasts() : V);
216  return CS && CS.hasRetAttr(Attribute::NoAlias);
217 }
218 
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();
225 }
226 
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);
235 }
236 
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();
242 }
243 
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();
249 }
250 
251 /// Tests if a value is a call or invoke to a library function that
252 /// allocates memory similar to malloc or calloc.
254  bool LookThroughBitCast) {
255  return getAllocationData(V, MallocOrCallocLike, TLI,
256  LookThroughBitCast).hasValue();
257 }
258 
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();
264 }
265 
266 /// Tests if a value is a call or invoke to a library function that
267 /// reallocates memory (e.g., realloc).
269  bool LookThroughBitCast) {
270  return getAllocationData(V, ReallocLike, TLI, LookThroughBitCast).hasValue();
271 }
272 
273 /// Tests if a functions is a call or invoke to a library function that
274 /// reallocates memory (e.g., realloc).
276  return getAllocationDataForFunction(F, ReallocLike, TLI).hasValue();
277 }
278 
279 /// extractMallocCall - Returns the corresponding CallInst if the instruction
280 /// is a malloc call. Since CallInst::CreateMalloc() only creates calls, we
281 /// ignore InvokeInst here.
283  const TargetLibraryInfo *TLI) {
284  return isMallocLikeFn(I, TLI) ? dyn_cast<CallInst>(I) : nullptr;
285 }
286 
287 static Value *computeArraySize(const CallInst *CI, const DataLayout &DL,
288  const TargetLibraryInfo *TLI,
289  bool LookThroughSExt = false) {
290  if (!CI)
291  return nullptr;
292 
293  // The size of the malloc's result type must be known to determine array size.
294  Type *T = getMallocAllocatedType(CI, TLI);
295  if (!T || !T->isSized())
296  return nullptr;
297 
298  unsigned ElementSize = DL.getTypeAllocSize(T);
299  if (StructType *ST = dyn_cast<StructType>(T))
300  ElementSize = DL.getStructLayout(ST)->getSizeInBytes();
301 
302  // If malloc call's arg can be determined to be a multiple of ElementSize,
303  // return the multiple. Otherwise, return NULL.
304  Value *MallocArg = CI->getArgOperand(0);
305  Value *Multiple = nullptr;
306  if (ComputeMultiple(MallocArg, ElementSize, Multiple, LookThroughSExt))
307  return Multiple;
308 
309  return nullptr;
310 }
311 
312 /// getMallocType - Returns the PointerType resulting from the malloc call.
313 /// The PointerType depends on the number of bitcast uses of the malloc call:
314 /// 0: PointerType is the calls' return type.
315 /// 1: PointerType is the bitcast's result type.
316 /// >1: Unique PointerType cannot be determined, return NULL.
318  const TargetLibraryInfo *TLI) {
319  assert(isMallocLikeFn(CI, TLI) && "getMallocType and not malloc call");
320 
321  PointerType *MallocType = nullptr;
322  unsigned NumOfBitCastUses = 0;
323 
324  // Determine if CallInst has a bitcast use.
325  for (Value::const_user_iterator UI = CI->user_begin(), E = CI->user_end();
326  UI != E;)
327  if (const BitCastInst *BCI = dyn_cast<BitCastInst>(*UI++)) {
328  MallocType = cast<PointerType>(BCI->getDestTy());
329  NumOfBitCastUses++;
330  }
331 
332  // Malloc call has 1 bitcast use, so type is the bitcast's destination type.
333  if (NumOfBitCastUses == 1)
334  return MallocType;
335 
336  // Malloc call was not bitcast, so type is the malloc function's return type.
337  if (NumOfBitCastUses == 0)
338  return cast<PointerType>(CI->getType());
339 
340  // Type could not be determined.
341  return nullptr;
342 }
343 
344 /// getMallocAllocatedType - Returns the Type allocated by malloc call.
345 /// The Type depends on the number of bitcast uses of the malloc call:
346 /// 0: PointerType is the malloc calls' return type.
347 /// 1: PointerType is the bitcast's result type.
348 /// >1: Unique PointerType cannot be determined, return NULL.
350  const TargetLibraryInfo *TLI) {
351  PointerType *PT = getMallocType(CI, TLI);
352  return PT ? PT->getElementType() : nullptr;
353 }
354 
355 /// getMallocArraySize - Returns the array size of a malloc call. If the
356 /// argument passed to malloc is a multiple of the size of the malloced type,
357 /// then return that multiple. For non-array mallocs, the multiple is
358 /// constant 1. Otherwise, return NULL for mallocs whose array size cannot be
359 /// determined.
361  const TargetLibraryInfo *TLI,
362  bool LookThroughSExt) {
363  assert(isMallocLikeFn(CI, TLI) && "getMallocArraySize and not malloc call");
364  return computeArraySize(CI, DL, TLI, LookThroughSExt);
365 }
366 
367 /// extractCallocCall - Returns the corresponding CallInst if the instruction
368 /// is a calloc call.
370  const TargetLibraryInfo *TLI) {
371  return isCallocLikeFn(I, TLI) ? cast<CallInst>(I) : nullptr;
372 }
373 
374 /// isLibFreeFunction - Returns true if the function is a builtin free()
375 bool llvm::isLibFreeFunction(const Function *F, const LibFunc TLIFn) {
376  unsigned ExpectedNumParams;
377  if (TLIFn == LibFunc_free ||
378  TLIFn == LibFunc_ZdlPv || // operator delete(void*)
379  TLIFn == LibFunc_ZdaPv || // operator delete[](void*)
380  TLIFn == LibFunc_msvc_delete_ptr32 || // operator delete(void*)
381  TLIFn == LibFunc_msvc_delete_ptr64 || // operator delete(void*)
382  TLIFn == LibFunc_msvc_delete_array_ptr32 || // operator delete[](void*)
383  TLIFn == LibFunc_msvc_delete_array_ptr64) // operator delete[](void*)
384  ExpectedNumParams = 1;
385  else if (TLIFn == LibFunc_ZdlPvj || // delete(void*, uint)
386  TLIFn == LibFunc_ZdlPvm || // delete(void*, ulong)
387  TLIFn == LibFunc_ZdlPvRKSt9nothrow_t || // delete(void*, nothrow)
388  TLIFn == LibFunc_ZdlPvSt11align_val_t || // delete(void*, align_val_t)
389  TLIFn == LibFunc_ZdaPvj || // delete[](void*, uint)
390  TLIFn == LibFunc_ZdaPvm || // delete[](void*, ulong)
391  TLIFn == LibFunc_ZdaPvRKSt9nothrow_t || // delete[](void*, nothrow)
392  TLIFn == LibFunc_ZdaPvSt11align_val_t || // delete[](void*, align_val_t)
393  TLIFn == LibFunc_msvc_delete_ptr32_int || // delete(void*, uint)
394  TLIFn == LibFunc_msvc_delete_ptr64_longlong || // delete(void*, ulonglong)
395  TLIFn == LibFunc_msvc_delete_ptr32_nothrow || // delete(void*, nothrow)
396  TLIFn == LibFunc_msvc_delete_ptr64_nothrow || // delete(void*, nothrow)
397  TLIFn == LibFunc_msvc_delete_array_ptr32_int || // delete[](void*, uint)
398  TLIFn == LibFunc_msvc_delete_array_ptr64_longlong || // delete[](void*, ulonglong)
399  TLIFn == LibFunc_msvc_delete_array_ptr32_nothrow || // delete[](void*, nothrow)
400  TLIFn == LibFunc_msvc_delete_array_ptr64_nothrow) // delete[](void*, nothrow)
401  ExpectedNumParams = 2;
402  else if (TLIFn == LibFunc_ZdaPvSt11align_val_tRKSt9nothrow_t || // delete(void*, align_val_t, nothrow)
403  TLIFn == LibFunc_ZdlPvSt11align_val_tRKSt9nothrow_t) // delete[](void*, align_val_t, nothrow)
404  ExpectedNumParams = 3;
405  else
406  return false;
407 
408  // Check free prototype.
409  // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin
410  // attribute will exist.
411  FunctionType *FTy = F->getFunctionType();
412  if (!FTy->getReturnType()->isVoidTy())
413  return false;
414  if (FTy->getNumParams() != ExpectedNumParams)
415  return false;
416  if (FTy->getParamType(0) != Type::getInt8PtrTy(F->getContext()))
417  return false;
418 
419  return true;
420 }
421 
422 /// isFreeCall - Returns non-null if the value is a call to the builtin free()
423 const CallInst *llvm::isFreeCall(const Value *I, const TargetLibraryInfo *TLI) {
424  bool IsNoBuiltinCall;
425  const Function *Callee =
426  getCalledFunction(I, /*LookThroughBitCast=*/false, IsNoBuiltinCall);
427  if (Callee == nullptr || IsNoBuiltinCall)
428  return nullptr;
429 
430  StringRef FnName = Callee->getName();
431  LibFunc TLIFn;
432  if (!TLI || !TLI->getLibFunc(FnName, TLIFn) || !TLI->has(TLIFn))
433  return nullptr;
434 
435  return isLibFreeFunction(Callee, TLIFn) ? dyn_cast<CallInst>(I) : nullptr;
436 }
437 
438 
439 //===----------------------------------------------------------------------===//
440 // Utility functions to compute size of objects.
441 //
443  if (Data.second.isNegative() || Data.first.ult(Data.second))
444  return APInt(Data.first.getBitWidth(), 0);
445  return Data.first - Data.second;
446 }
447 
448 /// Compute the size of the object pointed by Ptr. Returns true and the
449 /// object size in Size if successful, and false otherwise.
450 /// If RoundToAlign is true, then Size is rounded up to the alignment of
451 /// allocas, byval arguments, and global variables.
452 bool llvm::getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout &DL,
453  const TargetLibraryInfo *TLI, ObjectSizeOpts Opts) {
454  ObjectSizeOffsetVisitor Visitor(DL, TLI, Ptr->getContext(), Opts);
455  SizeOffsetType Data = Visitor.compute(const_cast<Value*>(Ptr));
456  if (!Visitor.bothKnown(Data))
457  return false;
458 
459  Size = getSizeWithOverflow(Data).getZExtValue();
460  return true;
461 }
462 
464  const DataLayout &DL,
465  const TargetLibraryInfo *TLI,
466  bool MustSucceed) {
467  assert(ObjectSize->getIntrinsicID() == Intrinsic::objectsize &&
468  "ObjectSize must be a call to llvm.objectsize!");
469 
470  bool MaxVal = cast<ConstantInt>(ObjectSize->getArgOperand(1))->isZero();
471  ObjectSizeOpts EvalOptions;
472  // Unless we have to fold this to something, try to be as accurate as
473  // possible.
474  if (MustSucceed)
475  EvalOptions.EvalMode =
477  else
479 
480  EvalOptions.NullIsUnknownSize =
481  cast<ConstantInt>(ObjectSize->getArgOperand(2))->isOne();
482 
483  auto *ResultType = cast<IntegerType>(ObjectSize->getType());
484  bool StaticOnly = cast<ConstantInt>(ObjectSize->getArgOperand(3))->isZero();
485  if (StaticOnly) {
486  // FIXME: Does it make sense to just return a failure value if the size won't
487  // fit in the output and `!MustSucceed`?
488  uint64_t Size;
489  if (getObjectSize(ObjectSize->getArgOperand(0), Size, DL, TLI, EvalOptions) &&
490  isUIntN(ResultType->getBitWidth(), Size))
491  return ConstantInt::get(ResultType, Size);
492  } else {
493  LLVMContext &Ctx = ObjectSize->getFunction()->getContext();
494  ObjectSizeOffsetEvaluator Eval(DL, TLI, Ctx, EvalOptions);
495  SizeOffsetEvalType SizeOffsetPair =
496  Eval.compute(ObjectSize->getArgOperand(0));
497 
498  if (SizeOffsetPair != ObjectSizeOffsetEvaluator::unknown()) {
499  IRBuilder<TargetFolder> Builder(Ctx, TargetFolder(DL));
500  Builder.SetInsertPoint(ObjectSize);
501 
502  // If we've outside the end of the object, then we can always access
503  // exactly 0 bytes.
504  Value *ResultSize =
505  Builder.CreateSub(SizeOffsetPair.first, SizeOffsetPair.second);
506  Value *UseZero =
507  Builder.CreateICmpULT(SizeOffsetPair.first, SizeOffsetPair.second);
508  return Builder.CreateSelect(UseZero, ConstantInt::get(ResultType, 0),
509  ResultSize);
510  }
511  }
512 
513  if (!MustSucceed)
514  return nullptr;
515 
516  return ConstantInt::get(ResultType, MaxVal ? -1ULL : 0);
517 }
518 
519 STATISTIC(ObjectVisitorArgument,
520  "Number of arguments with unsolved size and offset");
521 STATISTIC(ObjectVisitorLoad,
522  "Number of load instructions with unsolved size and offset");
523 
524 APInt ObjectSizeOffsetVisitor::align(APInt Size, uint64_t Align) {
525  if (Options.RoundToAlign && Align)
526  return APInt(IntTyBits, alignTo(Size.getZExtValue(), Align));
527  return Size;
528 }
529 
531  const TargetLibraryInfo *TLI,
533  ObjectSizeOpts Options)
534  : DL(DL), TLI(TLI), Options(Options) {
535  // Pointer size must be rechecked for each object visited since it could have
536  // a different address space.
537 }
538 
540  IntTyBits = DL.getPointerTypeSizeInBits(V->getType());
541  Zero = APInt::getNullValue(IntTyBits);
542 
543  V = V->stripPointerCasts();
544  if (Instruction *I = dyn_cast<Instruction>(V)) {
545  // If we have already seen this instruction, bail out. Cycles can happen in
546  // unreachable code after constant propagation.
547  if (!SeenInsts.insert(I).second)
548  return unknown();
549 
550  if (GEPOperator *GEP = dyn_cast<GEPOperator>(V))
551  return visitGEPOperator(*GEP);
552  return visit(*I);
553  }
554  if (Argument *A = dyn_cast<Argument>(V))
555  return visitArgument(*A);
556  if (ConstantPointerNull *P = dyn_cast<ConstantPointerNull>(V))
557  return visitConstantPointerNull(*P);
558  if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
559  return visitGlobalAlias(*GA);
560  if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
561  return visitGlobalVariable(*GV);
562  if (UndefValue *UV = dyn_cast<UndefValue>(V))
563  return visitUndefValue(*UV);
564  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
565  if (CE->getOpcode() == Instruction::IntToPtr)
566  return unknown(); // clueless
567  if (CE->getOpcode() == Instruction::GetElementPtr)
568  return visitGEPOperator(cast<GEPOperator>(*CE));
569  }
570 
571  LLVM_DEBUG(dbgs() << "ObjectSizeOffsetVisitor::compute() unhandled value: "
572  << *V << '\n');
573  return unknown();
574 }
575 
576 /// When we're compiling N-bit code, and the user uses parameters that are
577 /// greater than N bits (e.g. uint64_t on a 32-bit build), we can run into
578 /// trouble with APInt size issues. This function handles resizing + overflow
579 /// checks for us. Check and zext or trunc \p I depending on IntTyBits and
580 /// I's value.
581 bool ObjectSizeOffsetVisitor::CheckedZextOrTrunc(APInt &I) {
582  // More bits than we can handle. Checking the bit width isn't necessary, but
583  // it's faster than checking active bits, and should give `false` in the
584  // vast majority of cases.
585  if (I.getBitWidth() > IntTyBits && I.getActiveBits() > IntTyBits)
586  return false;
587  if (I.getBitWidth() != IntTyBits)
588  I = I.zextOrTrunc(IntTyBits);
589  return true;
590 }
591 
593  if (!I.getAllocatedType()->isSized())
594  return unknown();
595 
596  APInt Size(IntTyBits, DL.getTypeAllocSize(I.getAllocatedType()));
597  if (!I.isArrayAllocation())
598  return std::make_pair(align(Size, I.getAlignment()), Zero);
599 
600  Value *ArraySize = I.getArraySize();
601  if (const ConstantInt *C = dyn_cast<ConstantInt>(ArraySize)) {
602  APInt NumElems = C->getValue();
603  if (!CheckedZextOrTrunc(NumElems))
604  return unknown();
605 
606  bool Overflow;
607  Size = Size.umul_ov(NumElems, Overflow);
608  return Overflow ? unknown() : std::make_pair(align(Size, I.getAlignment()),
609  Zero);
610  }
611  return unknown();
612 }
613 
615  // No interprocedural analysis is done at the moment.
616  if (!A.hasByValOrInAllocaAttr()) {
617  ++ObjectVisitorArgument;
618  return unknown();
619  }
620  PointerType *PT = cast<PointerType>(A.getType());
621  APInt Size(IntTyBits, DL.getTypeAllocSize(PT->getElementType()));
622  return std::make_pair(align(Size, A.getParamAlignment()), Zero);
623 }
624 
627  if (!FnData)
628  return unknown();
629 
630  // Handle strdup-like functions separately.
631  if (FnData->AllocTy == StrDupLike) {
632  APInt Size(IntTyBits, GetStringLength(CS.getArgument(0)));
633  if (!Size)
634  return unknown();
635 
636  // Strndup limits strlen.
637  if (FnData->FstParam > 0) {
638  ConstantInt *Arg =
640  if (!Arg)
641  return unknown();
642 
643  APInt MaxSize = Arg->getValue().zextOrSelf(IntTyBits);
644  if (Size.ugt(MaxSize))
645  Size = MaxSize + 1;
646  }
647  return std::make_pair(Size, Zero);
648  }
649 
651  if (!Arg)
652  return unknown();
653 
654  APInt Size = Arg->getValue();
655  if (!CheckedZextOrTrunc(Size))
656  return unknown();
657 
658  // Size is determined by just 1 parameter.
659  if (FnData->SndParam < 0)
660  return std::make_pair(Size, Zero);
661 
662  Arg = dyn_cast<ConstantInt>(CS.getArgument(FnData->SndParam));
663  if (!Arg)
664  return unknown();
665 
666  APInt NumElems = Arg->getValue();
667  if (!CheckedZextOrTrunc(NumElems))
668  return unknown();
669 
670  bool Overflow;
671  Size = Size.umul_ov(NumElems, Overflow);
672  return Overflow ? unknown() : std::make_pair(Size, Zero);
673 
674  // TODO: handle more standard functions (+ wchar cousins):
675  // - strdup / strndup
676  // - strcpy / strncpy
677  // - strcat / strncat
678  // - memcpy / memmove
679  // - strcat / strncat
680  // - memset
681 }
682 
685  // If null is unknown, there's nothing we can do. Additionally, non-zero
686  // address spaces can make use of null, so we don't presume to know anything
687  // about that.
688  //
689  // TODO: How should this work with address space casts? We currently just drop
690  // them on the floor, but it's unclear what we should do when a NULL from
691  // addrspace(1) gets casted to addrspace(0) (or vice-versa).
692  if (Options.NullIsUnknownSize || CPN.getType()->getAddressSpace())
693  return unknown();
694  return std::make_pair(Zero, Zero);
695 }
696 
699  return unknown();
700 }
701 
704  // Easy cases were already folded by previous passes.
705  return unknown();
706 }
707 
709  SizeOffsetType PtrData = compute(GEP.getPointerOperand());
710  APInt Offset(IntTyBits, 0);
711  if (!bothKnown(PtrData) || !GEP.accumulateConstantOffset(DL, Offset))
712  return unknown();
713 
714  return std::make_pair(PtrData.first, PtrData.second + Offset);
715 }
716 
718  if (GA.isInterposable())
719  return unknown();
720  return compute(GA.getAliasee());
721 }
722 
724  if (!GV.hasDefinitiveInitializer())
725  return unknown();
726 
727  APInt Size(IntTyBits, DL.getTypeAllocSize(GV.getValueType()));
728  return std::make_pair(align(Size, GV.getAlignment()), Zero);
729 }
730 
732  // clueless
733  return unknown();
734 }
735 
737  ++ObjectVisitorLoad;
738  return unknown();
739 }
740 
742  // too complex to analyze statically.
743  return unknown();
744 }
745 
747  SizeOffsetType TrueSide = compute(I.getTrueValue());
748  SizeOffsetType FalseSide = compute(I.getFalseValue());
749  if (bothKnown(TrueSide) && bothKnown(FalseSide)) {
750  if (TrueSide == FalseSide) {
751  return TrueSide;
752  }
753 
754  APInt TrueResult = getSizeWithOverflow(TrueSide);
755  APInt FalseResult = getSizeWithOverflow(FalseSide);
756 
757  if (TrueResult == FalseResult) {
758  return TrueSide;
759  }
760  if (Options.EvalMode == ObjectSizeOpts::Mode::Min) {
761  if (TrueResult.slt(FalseResult))
762  return TrueSide;
763  return FalseSide;
764  }
765  if (Options.EvalMode == ObjectSizeOpts::Mode::Max) {
766  if (TrueResult.sgt(FalseResult))
767  return TrueSide;
768  return FalseSide;
769  }
770  }
771  return unknown();
772 }
773 
775  return std::make_pair(Zero, Zero);
776 }
777 
779  LLVM_DEBUG(dbgs() << "ObjectSizeOffsetVisitor unknown instruction:" << I
780  << '\n');
781  return unknown();
782 }
783 
785  const DataLayout &DL, const TargetLibraryInfo *TLI, LLVMContext &Context,
786  ObjectSizeOpts EvalOpts)
787  : DL(DL), TLI(TLI), Context(Context),
788  Builder(Context, TargetFolder(DL),
790  [&](Instruction *I) { InsertedInstructions.insert(I); })),
791  EvalOpts(EvalOpts) {
792  // IntTy and Zero must be set for each compute() since the address space may
793  // be different for later objects.
794 }
795 
797  // XXX - Are vectors of pointers possible here?
798  IntTy = cast<IntegerType>(DL.getIntPtrType(V->getType()));
799  Zero = ConstantInt::get(IntTy, 0);
800 
801  SizeOffsetEvalType Result = compute_(V);
802 
803  if (!bothKnown(Result)) {
804  // Erase everything that was computed in this iteration from the cache, so
805  // that no dangling references are left behind. We could be a bit smarter if
806  // we kept a dependency graph. It's probably not worth the complexity.
807  for (const Value *SeenVal : SeenVals) {
808  CacheMapTy::iterator CacheIt = CacheMap.find(SeenVal);
809  // non-computable results can be safely cached
810  if (CacheIt != CacheMap.end() && anyKnown(CacheIt->second))
811  CacheMap.erase(CacheIt);
812  }
813 
814  // Erase any instructions we inserted as part of the traversal.
815  for (Instruction *I : InsertedInstructions) {
816  I->replaceAllUsesWith(UndefValue::get(I->getType()));
817  I->eraseFromParent();
818  }
819  }
820 
821  SeenVals.clear();
822  InsertedInstructions.clear();
823  return Result;
824 }
825 
826 SizeOffsetEvalType ObjectSizeOffsetEvaluator::compute_(Value *V) {
827  ObjectSizeOffsetVisitor Visitor(DL, TLI, Context, EvalOpts);
828  SizeOffsetType Const = Visitor.compute(V);
829  if (Visitor.bothKnown(Const))
830  return std::make_pair(ConstantInt::get(Context, Const.first),
831  ConstantInt::get(Context, Const.second));
832 
833  V = V->stripPointerCasts();
834 
835  // Check cache.
836  CacheMapTy::iterator CacheIt = CacheMap.find(V);
837  if (CacheIt != CacheMap.end())
838  return CacheIt->second;
839 
840  // Always generate code immediately before the instruction being
841  // processed, so that the generated code dominates the same BBs.
842  BuilderTy::InsertPointGuard Guard(Builder);
843  if (Instruction *I = dyn_cast<Instruction>(V))
844  Builder.SetInsertPoint(I);
845 
846  // Now compute the size and offset.
847  SizeOffsetEvalType Result;
848 
849  // Record the pointers that were handled in this run, so that they can be
850  // cleaned later if something fails. We also use this set to break cycles that
851  // can occur in dead code.
852  if (!SeenVals.insert(V).second) {
853  Result = unknown();
854  } else if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
855  Result = visitGEPOperator(*GEP);
856  } else if (Instruction *I = dyn_cast<Instruction>(V)) {
857  Result = visit(*I);
858  } else if (isa<Argument>(V) ||
859  (isa<ConstantExpr>(V) &&
860  cast<ConstantExpr>(V)->getOpcode() == Instruction::IntToPtr) ||
861  isa<GlobalAlias>(V) ||
862  isa<GlobalVariable>(V)) {
863  // Ignore values where we cannot do more than ObjectSizeVisitor.
864  Result = unknown();
865  } else {
866  LLVM_DEBUG(
867  dbgs() << "ObjectSizeOffsetEvaluator::compute() unhandled value: " << *V
868  << '\n');
869  Result = unknown();
870  }
871 
872  // Don't reuse CacheIt since it may be invalid at this point.
873  CacheMap[V] = Result;
874  return Result;
875 }
876 
878  if (!I.getAllocatedType()->isSized())
879  return unknown();
880 
881  // must be a VLA
883  Value *ArraySize = I.getArraySize();
884  Value *Size = ConstantInt::get(ArraySize->getType(),
886  Size = Builder.CreateMul(Size, ArraySize);
887  return std::make_pair(Size, Zero);
888 }
889 
892  if (!FnData)
893  return unknown();
894 
895  // Handle strdup-like functions separately.
896  if (FnData->AllocTy == StrDupLike) {
897  // TODO
898  return unknown();
899  }
900 
901  Value *FirstArg = CS.getArgument(FnData->FstParam);
902  FirstArg = Builder.CreateZExt(FirstArg, IntTy);
903  if (FnData->SndParam < 0)
904  return std::make_pair(FirstArg, Zero);
905 
906  Value *SecondArg = CS.getArgument(FnData->SndParam);
907  SecondArg = Builder.CreateZExt(SecondArg, IntTy);
908  Value *Size = Builder.CreateMul(FirstArg, SecondArg);
909  return std::make_pair(Size, Zero);
910 
911  // TODO: handle more standard functions (+ wchar cousins):
912  // - strdup / strndup
913  // - strcpy / strncpy
914  // - strcat / strncat
915  // - memcpy / memmove
916  // - strcat / strncat
917  // - memset
918 }
919 
922  return unknown();
923 }
924 
927  return unknown();
928 }
929 
932  SizeOffsetEvalType PtrData = compute_(GEP.getPointerOperand());
933  if (!bothKnown(PtrData))
934  return unknown();
935 
936  Value *Offset = EmitGEPOffset(&Builder, DL, &GEP, /*NoAssumptions=*/true);
937  Offset = Builder.CreateAdd(PtrData.second, Offset);
938  return std::make_pair(PtrData.first, Offset);
939 }
940 
942  // clueless
943  return unknown();
944 }
945 
947  return unknown();
948 }
949 
951  // Create 2 PHIs: one for size and another for offset.
952  PHINode *SizePHI = Builder.CreatePHI(IntTy, PHI.getNumIncomingValues());
953  PHINode *OffsetPHI = Builder.CreatePHI(IntTy, PHI.getNumIncomingValues());
954 
955  // Insert right away in the cache to handle recursive PHIs.
956  CacheMap[&PHI] = std::make_pair(SizePHI, OffsetPHI);
957 
958  // Compute offset/size for each PHI incoming pointer.
959  for (unsigned i = 0, e = PHI.getNumIncomingValues(); i != e; ++i) {
961  SizeOffsetEvalType EdgeData = compute_(PHI.getIncomingValue(i));
962 
963  if (!bothKnown(EdgeData)) {
964  OffsetPHI->replaceAllUsesWith(UndefValue::get(IntTy));
965  OffsetPHI->eraseFromParent();
966  InsertedInstructions.erase(OffsetPHI);
967  SizePHI->replaceAllUsesWith(UndefValue::get(IntTy));
968  SizePHI->eraseFromParent();
969  InsertedInstructions.erase(SizePHI);
970  return unknown();
971  }
972  SizePHI->addIncoming(EdgeData.first, PHI.getIncomingBlock(i));
973  OffsetPHI->addIncoming(EdgeData.second, PHI.getIncomingBlock(i));
974  }
975 
976  Value *Size = SizePHI, *Offset = OffsetPHI;
977  if (Value *Tmp = SizePHI->hasConstantValue()) {
978  Size = Tmp;
979  SizePHI->replaceAllUsesWith(Size);
980  SizePHI->eraseFromParent();
981  InsertedInstructions.erase(SizePHI);
982  }
983  if (Value *Tmp = OffsetPHI->hasConstantValue()) {
984  Offset = Tmp;
985  OffsetPHI->replaceAllUsesWith(Offset);
986  OffsetPHI->eraseFromParent();
987  InsertedInstructions.erase(OffsetPHI);
988  }
989  return std::make_pair(Size, Offset);
990 }
991 
993  SizeOffsetEvalType TrueSide = compute_(I.getTrueValue());
994  SizeOffsetEvalType FalseSide = compute_(I.getFalseValue());
995 
996  if (!bothKnown(TrueSide) || !bothKnown(FalseSide))
997  return unknown();
998  if (TrueSide == FalseSide)
999  return TrueSide;
1000 
1001  Value *Size = Builder.CreateSelect(I.getCondition(), TrueSide.first,
1002  FalseSide.first);
1003  Value *Offset = Builder.CreateSelect(I.getCondition(), TrueSide.second,
1004  FalseSide.second);
1005  return std::make_pair(Size, Offset);
1006 }
1007 
1009  LLVM_DEBUG(dbgs() << "ObjectSizeOffsetEvaluator unknown instruction:" << I
1010  << '\n');
1011  return unknown();
1012 }
bool getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout &DL, const TargetLibraryInfo *TLI, ObjectSizeOpts Opts={})
Compute the size of the object pointed by Ptr.
Value * EmitGEPOffset(IRBuilderTy *Builder, const DataLayout &DL, User *GEP, bool NoAssumptions=false)
Given a getelementptr instruction/constantexpr, emit the code necessary to compute the offset from th...
Definition: Local.h:28
uint64_t CallInst * C
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks &#39;this&#39; from the containing basic block and deletes it.
Definition: Instruction.cpp:67
unsigned getAlignment() const
Definition: GlobalObject.h:58
bool isAllocationFn(const Value *V, const TargetLibraryInfo *TLI, bool LookThroughBitCast=false)
Tests if a value is a call or invoke to a library function that allocates or reallocates memory (eith...
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:110
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:233
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
bool hasDefinitiveInitializer() const
hasDefinitiveInitializer - Whether the global variable has an initializer, and any other instances of...
static const std::pair< LibFunc, AllocFnsTy > AllocationFnData[]
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
This instruction extracts a struct member or array element value from an aggregate value...
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1562
This class represents an incoming formal argument to a Function.
Definition: Argument.h:29
LLVMContext & Context
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Type * getParamType(unsigned i) const
Parameter type accessors.
Definition: DerivedTypes.h:135
Value * CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2074
static SizeOffsetEvalType unknown()
bool isSized(SmallPtrSetImpl< Type *> *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition: Type.h:264
unsigned NumParams
const CallInst * extractCallocCall(const Value *I, const TargetLibraryInfo *TLI)
extractCallocCall - Returns the corresponding CallInst if the instruction is a calloc call...
const StructLayout * getStructLayout(StructType *Ty) const
Returns a StructLayout object, indicating the alignment of the struct, its size, and the offsets of i...
Definition: DataLayout.cpp:604
bool hasByValOrInAllocaAttr() const
Return true if this argument has the byval attribute or inalloca attribute.
Definition: Function.cpp:104
std::pair< Value *, Value * > SizeOffsetEvalType
bool NullIsUnknownSize
If this is true, null pointers in address space 0 will be treated as though they can&#39;t be evaluated...
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition: APInt.h:1203
This class represents a function call, abstracting a target machine&#39;s calling convention.
static Optional< AllocFnsTy > getAllocationDataForFunction(const Function *Callee, AllocType AllocTy, const TargetLibraryInfo *TLI)
Returns the allocation data for the given value if it&#39;s either a call to a known allocation function...
const Value * getTrueValue() const
The two locations do not alias at all.
Definition: AliasAnalysis.h:84
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:732
bool hasRetAttr(Attribute::AttrKind Kind) const
Return true if this return value has the given attribute.
Definition: CallSite.h:380
SizeOffsetType visitAllocaInst(AllocaInst &I)
bool isInterposable() const
Return true if this global&#39;s definition can be substituted with an arbitrary definition at link time...
Definition: GlobalValue.h:429
bool sgt(const APInt &RHS) const
Signed greather than comparison.
Definition: APInt.h:1273
AllocType
STATISTIC(NumFunctions, "Total number of functions")
F(f)
bool isLibFreeFunction(const Function *F, const LibFunc TLIFn)
isLibFreeFunction - Returns true if the function is a builtin free()
uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew=0)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
Definition: MathExtras.h:689
An instruction for reading from memory.
Definition: Instructions.h:167
FunTy * getCalledFunction() const
Return the function being called if this is a direct call, otherwise return null (if it&#39;s an indirect...
Definition: CallSite.h:111
bool isReallocLikeFn(const Value *V, const TargetLibraryInfo *TLI, bool LookThroughBitCast=false)
Tests if a value is a call or invoke to a library function that reallocates memory (e...
APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition: APInt.cpp:878
SizeOffsetType visitArgument(Argument &A)
Hexagon Common GEP
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1508
bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const
Accumulate the constant address offset of this GEP if possible.
Definition: Operator.cpp:34
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition: Type.h:129
const CallInst * isFreeCall(const Value *I, const TargetLibraryInfo *TLI)
isFreeCall - Returns non-null if the value is a call to the builtin free()
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1241
This class represents the LLVM &#39;select&#39; instruction.
unsigned getAlignment() const
Return the alignment of the memory that is being allocated by the instruction.
Definition: Instructions.h:112
SizeOffsetEvalType visitGEPOperator(GEPOperator &GEP)
SizeOffsetType visitExtractValueInst(ExtractValueInst &I)
SizeOffsetType visitGEPOperator(GEPOperator &GEP)
unsigned getPointerTypeSizeInBits(Type *) const
Layout pointer size, in bits, based on the type.
Definition: DataLayout.cpp:662
&#39;undef&#39; values are things that do not have specified contents.
Definition: Constants.h:1285
static Optional< AllocFnsTy > getAllocationSize(const Value *V, const TargetLibraryInfo *TLI)
Class to represent struct types.
Definition: DerivedTypes.h:233
APInt zextOrSelf(unsigned width) const
Zero extend or truncate to width.
Definition: APInt.cpp:894
static Optional< unsigned > getOpcode(ArrayRef< VPValue *> Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition: VPlanSLP.cpp:196
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:196
SizeOffsetEvalType visitCallSite(CallSite CS)
SizeOffsetType visitIntToPtrInst(IntToPtrInst &)
This file contains the simple types necessary to represent the attributes associated with functions a...
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1118
This file implements a class to represent arbitrary precision integral constant values and operations...
Value * lowerObjectSizeCall(IntrinsicInst *ObjectSize, const DataLayout &DL, const TargetLibraryInfo *TLI, bool MustSucceed)
Try to turn a call to @llvm.objectsize into an integer value of the given Type.
InstrTy * getInstruction() const
Definition: CallSite.h:96
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition: APInt.h:1532
bool isNoAliasFn(const Value *V, const TargetLibraryInfo *TLI, bool LookThroughBitCast=false)
Tests if a value is a call or invoke to a function that returns a NoAlias pointer (including malloc/c...
Same as Min, except we pick the maximum size of all of the branches.
A constant value that is initialized with an expression using other constant values.
Definition: Constants.h:888
Class to represent function types.
Definition: DerivedTypes.h:103
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:244
Evaluate all branches of an unknown condition.
Evaluate the size and offset of an object pointed to by a Value*.
bool has(LibFunc F) const
Tests whether a library function is available.
This class represents a no-op cast from one type to another.
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:137
TargetFolder - Create constants with target dependent folding.
Definition: TargetFolder.h:31
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1135
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:429
std::pair< unsigned, Optional< unsigned > > getAllocSizeArgs() const
Returns the argument numbers for the allocsize attribute (or pair(0, 0) if not known).
Definition: Attributes.cpp:273
Value * CreateZExt(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1878
uint64_t GetStringLength(const Value *V, unsigned CharSize=8)
If we can compute the length of the string pointed to by the specified pointer, return &#39;len+1&#39;...
SizeOffsetType visitInstruction(Instruction &I)
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block...
Definition: IRBuilder.h:132
Class to represent pointers.
Definition: DerivedTypes.h:544
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:176
Value * getMallocArraySize(CallInst *CI, const DataLayout &DL, const TargetLibraryInfo *TLI, bool LookThroughSExt=false)
getMallocArraySize - Returns the array size of a malloc call.
SizeOffsetType visitGlobalVariable(GlobalVariable &GV)
bool isVoidTy() const
Return true if this is &#39;void&#39;.
Definition: Type.h:140
IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Returns an integer type with size at least as big as that of a pointer in the given address space...
Definition: DataLayout.cpp:766
#define P(N)
bool erase(const KeyT &Val)
Definition: DenseMap.h:298
bool isNoBuiltin() const
Return true if the call should not be treated as a call to a builtin.
Definition: CallSite.h:441
SizeOffsetEvalType visitSelectInst(SelectInst &I)
const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
Definition: BasicBlock.cpp:216
ObjectSizeOffsetEvaluator(const DataLayout &DL, const TargetLibraryInfo *TLI, LLVMContext &Context, ObjectSizeOpts EvalOpts={})
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:64
const CallInst * extractMallocCall(const Value *I, const TargetLibraryInfo *TLI)
extractMallocCall - Returns the corresponding CallInst if the instruction is a malloc call...
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="", Instruction *MDFrom=nullptr)
Definition: IRBuilder.h:2241
ValTy * getArgument(unsigned ArgNo) const
Definition: CallSite.h:193
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
Definition: DerivedTypes.h:139
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:370
static Value * computeArraySize(const CallInst *CI, const DataLayout &DL, const TargetLibraryInfo *TLI, bool LookThroughSExt=false)
static APInt getSizeWithOverflow(const SizeOffsetType &Data)
Value * getPointerOperand()
Definition: Operator.h:473
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
static bool hasNoAliasAttr(const Value *V, bool LookThroughBitCast)
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Definition: DerivedTypes.h:572
static Optional< AllocFnsTy > getAllocationData(const Value *V, AllocType AllocTy, const TargetLibraryInfo *TLI, bool LookThroughBitCast=false)
auto find_if(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range))
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1220
const Function * getFunction() const
Return the function this instruction belongs to.
Definition: Instruction.cpp:59
This class represents a cast from an integer to a pointer.
const Value * getCondition() const
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
Definition: Function.cpp:205
static UndefValue * get(Type *T)
Static factory methods - Return an &#39;undef&#39; object of the specified type.
Definition: Constants.cpp:1424
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs, address space casts, and aliases.
Definition: Value.cpp:535
SizeOffsetType visitCallSite(CallSite CS)
const Value * getArraySize() const
Get the number of elements allocated.
Definition: Instructions.h:92
std::pair< APInt, APInt > SizeOffsetType
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:219
PointerType * getMallocType(const CallInst *CI, const TargetLibraryInfo *TLI)
getMallocType - Returns the PointerType resulting from the malloc call.
Value * CreateMul(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1152
Type * getAllocatedType() const
Return the type that is being allocated by the instruction.
Definition: Instructions.h:105
SizeOffsetType visitSelectInst(SelectInst &I)
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
Definition: IntrinsicInst.h:50
SizeOffsetType visitExtractElementInst(ExtractElementInst &I)
SizeOffsetType visitGlobalAlias(GlobalAlias &GA)
PHINode * CreatePHI(Type *Ty, unsigned NumReservedValues, const Twine &Name="")
Definition: IRBuilder.h:2188
SizeOffsetEvalType visitInstruction(Instruction &I)
Value * hasConstantValue() const
If the specified PHI node always merges together the same value, return the value, otherwise return null.
unsigned getNumOperands() const
Definition: User.h:191
Mode EvalMode
How we want to evaluate this object&#39;s size.
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
Evaluate the size and offset of an object pointed to by a Value* statically.
unsigned getParamAlignment() const
If this is a byval or inalloca argument, return its alignment.
Definition: Function.cpp:111
Provides information about what library functions are available for the current target.
SizeOffsetType visitPHINode(PHINode &)
A constant pointer value that points to null.
Definition: Constants.h:538
Type * getReturnType() const
Definition: DerivedTypes.h:124
uint64_t getSizeInBytes() const
Definition: DataLayout.h:570
SizeOffsetType visitUndefValue(UndefValue &)
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:631
unsigned getNumIncomingValues() const
Return the number of incoming edges.
static const Function * getCalledFunction(const Value *V, bool LookThroughBitCast, bool &IsNoBuiltin)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
SizeOffsetType compute(Value *V)
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Definition: Function.h:163
Class for arbitrary precision integers.
Definition: APInt.h:69
amdgpu Simplify well known AMD library false FunctionCallee Callee
SizeOffsetEvalType visitIntToPtrInst(IntToPtrInst &)
Type * getMallocAllocatedType(const CallInst *CI, const TargetLibraryInfo *TLI)
getMallocAllocatedType - Returns the Type allocated by malloc call.
SizeOffsetEvalType visitPHINode(PHINode &PHI)
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
bool isMallocLikeFn(const Value *V, const TargetLibraryInfo *TLI, bool LookThroughBitCast=false)
Tests if a value is a call or invoke to a library function that allocates uninitialized memory (such ...
const Value * getFalseValue() const
user_iterator_impl< const User > const_user_iterator
Definition: Value.h:369
Fail to evaluate an unknown condition.
SizeOffsetEvalType compute(Value *V)
uint64_t getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:469
bool ugt(const APInt &RHS) const
Unsigned greather than comparison.
Definition: APInt.h:1254
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Definition: Lint.cpp:549
Provides an &#39;InsertHelper&#39; that calls a user-provided callback after performing the default insertion...
Definition: IRBuilder.h:72
ObjectSizeOffsetVisitor(const DataLayout &DL, const TargetLibraryInfo *TLI, LLVMContext &Context, ObjectSizeOpts Options={})
SizeOffsetEvalType visitLoadInst(LoadInst &I)
bool isMallocOrCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI, bool LookThroughBitCast=false)
Tests if a value is a call or invoke to a library function that allocates memory similar to malloc or...
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:214
Establish a view to a call site for examination.
Definition: CallSite.h:897
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
bool isCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI, bool LookThroughBitCast=false)
Tests if a value is a call or invoke to a library function that allocates zero-filled memory (such as...
#define I(x, y, z)
Definition: MD5.cpp:58
PointerType * getType() const
Specialize the getType() method to always return an PointerType, which reduces the amount of casting ...
Definition: Constants.h:554
bool isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI, bool LookThroughBitCast=false)
Tests if a value is a call or invoke to a library function that allocates memory (either malloc...
iterator end()
Definition: DenseMap.h:108
bool ComputeMultiple(Value *V, unsigned Base, Value *&Multiple, bool LookThroughSExt=false, unsigned Depth=0)
This function computes the integer multiple of Base that equals V.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
Type * getValueType() const
Definition: GlobalValue.h:279
This instruction extracts a single (scalar) element from a VectorType value.
uint32_t Size
Definition: Profile.cpp:46
bool anyKnown(SizeOffsetEvalType SizeOffset)
APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1921
bool isArrayAllocation() const
Return true if there is an allocation size parameter to the allocation instruction that is not 1...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
user_iterator user_begin()
Definition: Value.h:375
static bool bothKnown(const SizeOffsetType &SizeOffset)
LLVM Value Representation.
Definition: Value.h:72
SizeOffsetEvalType visitExtractElementInst(ExtractElementInst &I)
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
Definition: Function.h:333
AllocType AllocTy
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
Various options to control the behavior of getObjectSize.
static APInt getNullValue(unsigned numBits)
Get the &#39;0&#39; value.
Definition: APInt.h:568
bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition: MathExtras.h:392
#define LLVM_DEBUG(X)
Definition: Debug.h:122
SizeOffsetType visitLoadInst(LoadInst &I)
SizeOffsetEvalType visitAllocaInst(AllocaInst &I)
bool bothKnown(SizeOffsetEvalType SizeOffset)
SizeOffsetType visitConstantPointerNull(ConstantPointerNull &)
SizeOffsetEvalType visitExtractValueInst(ExtractValueInst &I)
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
Type * getElementType() const
Definition: DerivedTypes.h:563
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:43
an instruction to allocate memory on the stack
Definition: Instructions.h:59
user_iterator user_end()
Definition: Value.h:383
const Constant * getAliasee() const
Definition: GlobalAlias.h:77