LLVM  14.0.0git
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  AlignedAllocLike = 1<<2, // allocates with alignment; may return null
56  CallocLike = 1<<3, // allocates + bzero
57  ReallocLike = 1<<4, // reallocates
58  StrDupLike = 1<<5,
62 };
63 
64 struct AllocFnsTy {
66  unsigned NumParams;
67  // First and Second size parameters (or -1 if unused)
68  int FstParam, SndParam;
69 };
70 
71 // FIXME: certain users need more information. E.g., SimplifyLibCalls needs to
72 // know which functions are nounwind, noalias, nocapture parameters, etc.
73 static const std::pair<LibFunc, AllocFnsTy> AllocationFnData[] = {
74  {LibFunc_malloc, {MallocLike, 1, 0, -1}},
75  {LibFunc_vec_malloc, {MallocLike, 1, 0, -1}},
76  {LibFunc_valloc, {MallocLike, 1, 0, -1}},
77  {LibFunc_Znwj, {OpNewLike, 1, 0, -1}}, // new(unsigned int)
78  {LibFunc_ZnwjRKSt9nothrow_t, {MallocLike, 2, 0, -1}}, // new(unsigned int, nothrow)
79  {LibFunc_ZnwjSt11align_val_t, {OpNewLike, 2, 0, -1}}, // new(unsigned int, align_val_t)
80  {LibFunc_ZnwjSt11align_val_tRKSt9nothrow_t, // new(unsigned int, align_val_t, nothrow)
81  {MallocLike, 3, 0, -1}},
82  {LibFunc_Znwm, {OpNewLike, 1, 0, -1}}, // new(unsigned long)
83  {LibFunc_ZnwmRKSt9nothrow_t, {MallocLike, 2, 0, -1}}, // new(unsigned long, nothrow)
84  {LibFunc_ZnwmSt11align_val_t, {OpNewLike, 2, 0, -1}}, // new(unsigned long, align_val_t)
85  {LibFunc_ZnwmSt11align_val_tRKSt9nothrow_t, // new(unsigned long, align_val_t, nothrow)
86  {MallocLike, 3, 0, -1}},
87  {LibFunc_Znaj, {OpNewLike, 1, 0, -1}}, // new[](unsigned int)
88  {LibFunc_ZnajRKSt9nothrow_t, {MallocLike, 2, 0, -1}}, // new[](unsigned int, nothrow)
89  {LibFunc_ZnajSt11align_val_t, {OpNewLike, 2, 0, -1}}, // new[](unsigned int, align_val_t)
90  {LibFunc_ZnajSt11align_val_tRKSt9nothrow_t, // new[](unsigned int, align_val_t, nothrow)
91  {MallocLike, 3, 0, -1}},
92  {LibFunc_Znam, {OpNewLike, 1, 0, -1}}, // new[](unsigned long)
93  {LibFunc_ZnamRKSt9nothrow_t, {MallocLike, 2, 0, -1}}, // new[](unsigned long, nothrow)
94  {LibFunc_ZnamSt11align_val_t, {OpNewLike, 2, 0, -1}}, // new[](unsigned long, align_val_t)
95  {LibFunc_ZnamSt11align_val_tRKSt9nothrow_t, // new[](unsigned long, align_val_t, nothrow)
96  {MallocLike, 3, 0, -1}},
97  {LibFunc_msvc_new_int, {OpNewLike, 1, 0, -1}}, // new(unsigned int)
98  {LibFunc_msvc_new_int_nothrow, {MallocLike, 2, 0, -1}}, // new(unsigned int, nothrow)
99  {LibFunc_msvc_new_longlong, {OpNewLike, 1, 0, -1}}, // new(unsigned long long)
100  {LibFunc_msvc_new_longlong_nothrow, {MallocLike, 2, 0, -1}}, // new(unsigned long long, nothrow)
101  {LibFunc_msvc_new_array_int, {OpNewLike, 1, 0, -1}}, // new[](unsigned int)
102  {LibFunc_msvc_new_array_int_nothrow, {MallocLike, 2, 0, -1}}, // new[](unsigned int, nothrow)
103  {LibFunc_msvc_new_array_longlong, {OpNewLike, 1, 0, -1}}, // new[](unsigned long long)
104  {LibFunc_msvc_new_array_longlong_nothrow, {MallocLike, 2, 0, -1}}, // new[](unsigned long long, nothrow)
105  {LibFunc_aligned_alloc, {AlignedAllocLike, 2, 1, -1}},
106  {LibFunc_memalign, {AlignedAllocLike, 2, 1, -1}},
107  {LibFunc_calloc, {CallocLike, 2, 0, 1}},
108  {LibFunc_vec_calloc, {CallocLike, 2, 0, 1}},
109  {LibFunc_realloc, {ReallocLike, 2, 1, -1}},
110  {LibFunc_vec_realloc, {ReallocLike, 2, 1, -1}},
111  {LibFunc_reallocf, {ReallocLike, 2, 1, -1}},
112  {LibFunc_strdup, {StrDupLike, 1, -1, -1}},
113  {LibFunc_strndup, {StrDupLike, 2, 1, -1}},
114  {LibFunc___kmpc_alloc_shared, {MallocLike, 1, 0, -1}}
115  // TODO: Handle "int posix_memalign(void **, size_t, size_t)"
116 };
117 
118 static const Function *getCalledFunction(const Value *V, bool LookThroughBitCast,
119  bool &IsNoBuiltin) {
120  // Don't care about intrinsics in this case.
121  if (isa<IntrinsicInst>(V))
122  return nullptr;
123 
124  if (LookThroughBitCast)
125  V = V->stripPointerCasts();
126 
127  const auto *CB = dyn_cast<CallBase>(V);
128  if (!CB)
129  return nullptr;
130 
131  IsNoBuiltin = CB->isNoBuiltin();
132 
133  if (const Function *Callee = CB->getCalledFunction())
134  return Callee;
135  return nullptr;
136 }
137 
138 /// Returns the allocation data for the given value if it's either a call to a
139 /// known allocation function, or a call to a function with the allocsize
140 /// attribute.
143  const TargetLibraryInfo *TLI) {
144  // Make sure that the function is available.
145  LibFunc TLIFn;
146  if (!TLI || !TLI->getLibFunc(*Callee, TLIFn) || !TLI->has(TLIFn))
147  return None;
148 
149  const auto *Iter = find_if(
150  AllocationFnData, [TLIFn](const std::pair<LibFunc, AllocFnsTy> &P) {
151  return P.first == TLIFn;
152  });
153 
154  if (Iter == std::end(AllocationFnData))
155  return None;
156 
157  const AllocFnsTy *FnData = &Iter->second;
158  if ((FnData->AllocTy & AllocTy) != FnData->AllocTy)
159  return None;
160 
161  // Check function prototype.
162  int FstParam = FnData->FstParam;
163  int SndParam = FnData->SndParam;
165 
166  if (FTy->getReturnType() == Type::getInt8PtrTy(FTy->getContext()) &&
167  FTy->getNumParams() == FnData->NumParams &&
168  (FstParam < 0 ||
169  (FTy->getParamType(FstParam)->isIntegerTy(32) ||
170  FTy->getParamType(FstParam)->isIntegerTy(64))) &&
171  (SndParam < 0 ||
172  FTy->getParamType(SndParam)->isIntegerTy(32) ||
173  FTy->getParamType(SndParam)->isIntegerTy(64)))
174  return *FnData;
175  return None;
176 }
177 
179  const TargetLibraryInfo *TLI,
180  bool LookThroughBitCast = false) {
181  bool IsNoBuiltinCall;
182  if (const Function *Callee =
183  getCalledFunction(V, LookThroughBitCast, IsNoBuiltinCall))
184  if (!IsNoBuiltinCall)
185  return getAllocationDataForFunction(Callee, AllocTy, TLI);
186  return None;
187 }
188 
191  function_ref<const TargetLibraryInfo &(Function &)> GetTLI,
192  bool LookThroughBitCast = false) {
193  bool IsNoBuiltinCall;
194  if (const Function *Callee =
195  getCalledFunction(V, LookThroughBitCast, IsNoBuiltinCall))
196  if (!IsNoBuiltinCall)
198  Callee, AllocTy, &GetTLI(const_cast<Function &>(*Callee)));
199  return None;
200 }
201 
203  const TargetLibraryInfo *TLI) {
204  bool IsNoBuiltinCall;
205  const Function *Callee =
206  getCalledFunction(V, /*LookThroughBitCast=*/false, IsNoBuiltinCall);
207  if (!Callee)
208  return None;
209 
210  // Prefer to use existing information over allocsize. This will give us an
211  // accurate AllocTy.
212  if (!IsNoBuiltinCall)
215  return Data;
216 
217  Attribute Attr = Callee->getFnAttribute(Attribute::AllocSize);
218  if (Attr == Attribute())
219  return None;
220 
221  std::pair<unsigned, Optional<unsigned>> Args = Attr.getAllocSizeArgs();
222 
223  AllocFnsTy Result;
224  // Because allocsize only tells us how many bytes are allocated, we're not
225  // really allowed to assume anything, so we use MallocLike.
226  Result.AllocTy = MallocLike;
227  Result.NumParams = Callee->getNumOperands();
228  Result.FstParam = Args.first;
229  Result.SndParam = Args.second.getValueOr(-1);
230  return Result;
231 }
232 
233 static bool hasNoAliasAttr(const Value *V, bool LookThroughBitCast) {
234  const auto *CB =
235  dyn_cast<CallBase>(LookThroughBitCast ? V->stripPointerCasts() : V);
236  return CB && CB->hasRetAttr(Attribute::NoAlias);
237 }
238 
239 /// Tests if a value is a call or invoke to a library function that
240 /// allocates or reallocates memory (either malloc, calloc, realloc, or strdup
241 /// like).
242 bool llvm::isAllocationFn(const Value *V, const TargetLibraryInfo *TLI,
243  bool LookThroughBitCast) {
244  return getAllocationData(V, AnyAlloc, TLI, LookThroughBitCast).hasValue();
245 }
247  const Value *V, function_ref<const TargetLibraryInfo &(Function &)> GetTLI,
248  bool LookThroughBitCast) {
249  return getAllocationData(V, AnyAlloc, GetTLI, LookThroughBitCast).hasValue();
250 }
251 
252 /// Tests if a value is a call or invoke to a function that returns a
253 /// NoAlias pointer (including malloc/calloc/realloc/strdup-like functions).
254 bool llvm::isNoAliasFn(const Value *V, const TargetLibraryInfo *TLI,
255  bool LookThroughBitCast) {
256  // it's safe to consider realloc as noalias since accessing the original
257  // pointer is undefined behavior
258  return isAllocationFn(V, TLI, LookThroughBitCast) ||
259  hasNoAliasAttr(V, LookThroughBitCast);
260 }
261 
262 /// Tests if a value is a call or invoke to a library function that
263 /// allocates uninitialized memory (such as malloc).
264 bool llvm::isMallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
265  bool LookThroughBitCast) {
266  return getAllocationData(V, MallocLike, TLI, LookThroughBitCast).hasValue();
267 }
269  const Value *V, function_ref<const TargetLibraryInfo &(Function &)> GetTLI,
270  bool LookThroughBitCast) {
271  return getAllocationData(V, MallocLike, GetTLI, LookThroughBitCast)
272  .hasValue();
273 }
274 
275 /// Tests if a value is a call or invoke to a library function that
276 /// allocates uninitialized memory with alignment (such as aligned_alloc).
278  bool LookThroughBitCast) {
279  return getAllocationData(V, AlignedAllocLike, TLI, LookThroughBitCast)
280  .hasValue();
281 }
283  const Value *V, function_ref<const TargetLibraryInfo &(Function &)> GetTLI,
284  bool LookThroughBitCast) {
285  return getAllocationData(V, AlignedAllocLike, GetTLI, LookThroughBitCast)
286  .hasValue();
287 }
288 
289 /// Tests if a value is a call or invoke to a library function that
290 /// allocates zero-filled memory (such as calloc).
291 bool llvm::isCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
292  bool LookThroughBitCast) {
293  return getAllocationData(V, CallocLike, TLI, LookThroughBitCast).hasValue();
294 }
295 
296 /// Tests if a value is a call or invoke to a library function that
297 /// allocates memory similar to malloc or calloc.
299  bool LookThroughBitCast) {
300  return getAllocationData(V, MallocOrCallocLike, TLI,
301  LookThroughBitCast).hasValue();
302 }
303 
304 /// Tests if a value is a call or invoke to a library function that
305 /// allocates memory (either malloc, calloc, or strdup like).
306 bool llvm::isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI,
307  bool LookThroughBitCast) {
308  return getAllocationData(V, AllocLike, TLI, LookThroughBitCast).hasValue();
309 }
310 
311 /// Tests if a value is a call or invoke to a library function that
312 /// reallocates memory (e.g., realloc).
314  bool LookThroughBitCast) {
315  return getAllocationData(V, ReallocLike, TLI, LookThroughBitCast).hasValue();
316 }
317 
318 /// Tests if a functions is a call or invoke to a library function that
319 /// reallocates memory (e.g., realloc).
321  return getAllocationDataForFunction(F, ReallocLike, TLI).hasValue();
322 }
323 
324 /// Tests if a value is a call or invoke to a library function that
325 /// allocates memory and throws if an allocation failed (e.g., new).
326 bool llvm::isOpNewLikeFn(const Value *V, const TargetLibraryInfo *TLI,
327  bool LookThroughBitCast) {
328  return getAllocationData(V, OpNewLike, TLI, LookThroughBitCast).hasValue();
329 }
330 
331 /// Tests if a value is a call or invoke to a library function that
332 /// allocates memory (strdup, strndup).
333 bool llvm::isStrdupLikeFn(const Value *V, const TargetLibraryInfo *TLI,
334  bool LookThroughBitCast) {
335  return getAllocationData(V, StrDupLike, TLI, LookThroughBitCast).hasValue();
336 }
337 
338 /// extractMallocCall - Returns the corresponding CallInst if the instruction
339 /// is a malloc call. Since CallInst::CreateMalloc() only creates calls, we
340 /// ignore InvokeInst here.
342  const Value *I,
343  function_ref<const TargetLibraryInfo &(Function &)> GetTLI) {
344  return isMallocLikeFn(I, GetTLI) ? dyn_cast<CallInst>(I) : nullptr;
345 }
346 
347 static Value *computeArraySize(const CallInst *CI, const DataLayout &DL,
348  const TargetLibraryInfo *TLI,
349  bool LookThroughSExt = false) {
350  if (!CI)
351  return nullptr;
352 
353  // The size of the malloc's result type must be known to determine array size.
354  Type *T = getMallocAllocatedType(CI, TLI);
355  if (!T || !T->isSized())
356  return nullptr;
357 
358  unsigned ElementSize = DL.getTypeAllocSize(T);
359  if (StructType *ST = dyn_cast<StructType>(T))
360  ElementSize = DL.getStructLayout(ST)->getSizeInBytes();
361 
362  // If malloc call's arg can be determined to be a multiple of ElementSize,
363  // return the multiple. Otherwise, return NULL.
364  Value *MallocArg = CI->getArgOperand(0);
365  Value *Multiple = nullptr;
366  if (ComputeMultiple(MallocArg, ElementSize, Multiple, LookThroughSExt))
367  return Multiple;
368 
369  return nullptr;
370 }
371 
372 /// getMallocType - Returns the PointerType resulting from the malloc call.
373 /// The PointerType depends on the number of bitcast uses of the malloc call:
374 /// 0: PointerType is the calls' return type.
375 /// 1: PointerType is the bitcast's result type.
376 /// >1: Unique PointerType cannot be determined, return NULL.
378  const TargetLibraryInfo *TLI) {
379  assert(isMallocLikeFn(CI, TLI) && "getMallocType and not malloc call");
380 
381  PointerType *MallocType = nullptr;
382  unsigned NumOfBitCastUses = 0;
383 
384  // Determine if CallInst has a bitcast use.
385  for (const User *U : CI->users())
386  if (const BitCastInst *BCI = dyn_cast<BitCastInst>(U)) {
387  MallocType = cast<PointerType>(BCI->getDestTy());
388  NumOfBitCastUses++;
389  }
390 
391  // Malloc call has 1 bitcast use, so type is the bitcast's destination type.
392  if (NumOfBitCastUses == 1)
393  return MallocType;
394 
395  // Malloc call was not bitcast, so type is the malloc function's return type.
396  if (NumOfBitCastUses == 0)
397  return cast<PointerType>(CI->getType());
398 
399  // Type could not be determined.
400  return nullptr;
401 }
402 
403 /// getMallocAllocatedType - Returns the Type allocated by malloc call.
404 /// The Type depends on the number of bitcast uses of the malloc call:
405 /// 0: PointerType is the malloc calls' return type.
406 /// 1: PointerType is the bitcast's result type.
407 /// >1: Unique PointerType cannot be determined, return NULL.
409  const TargetLibraryInfo *TLI) {
410  PointerType *PT = getMallocType(CI, TLI);
411  return PT ? PT->getElementType() : nullptr;
412 }
413 
414 /// getMallocArraySize - Returns the array size of a malloc call. If the
415 /// argument passed to malloc is a multiple of the size of the malloced type,
416 /// then return that multiple. For non-array mallocs, the multiple is
417 /// constant 1. Otherwise, return NULL for mallocs whose array size cannot be
418 /// determined.
420  const TargetLibraryInfo *TLI,
421  bool LookThroughSExt) {
422  assert(isMallocLikeFn(CI, TLI) && "getMallocArraySize and not malloc call");
423  return computeArraySize(CI, DL, TLI, LookThroughSExt);
424 }
425 
426 /// extractCallocCall - Returns the corresponding CallInst if the instruction
427 /// is a calloc call.
429  const TargetLibraryInfo *TLI) {
430  return isCallocLikeFn(I, TLI) ? cast<CallInst>(I) : nullptr;
431 }
432 
433 /// isLibFreeFunction - Returns true if the function is a builtin free()
434 bool llvm::isLibFreeFunction(const Function *F, const LibFunc TLIFn) {
435  unsigned ExpectedNumParams;
436  if (TLIFn == LibFunc_free ||
437  TLIFn == LibFunc_ZdlPv || // operator delete(void*)
438  TLIFn == LibFunc_ZdaPv || // operator delete[](void*)
439  TLIFn == LibFunc_msvc_delete_ptr32 || // operator delete(void*)
440  TLIFn == LibFunc_msvc_delete_ptr64 || // operator delete(void*)
441  TLIFn == LibFunc_msvc_delete_array_ptr32 || // operator delete[](void*)
442  TLIFn == LibFunc_msvc_delete_array_ptr64) // operator delete[](void*)
443  ExpectedNumParams = 1;
444  else if (TLIFn == LibFunc_ZdlPvj || // delete(void*, uint)
445  TLIFn == LibFunc_ZdlPvm || // delete(void*, ulong)
446  TLIFn == LibFunc_ZdlPvRKSt9nothrow_t || // delete(void*, nothrow)
447  TLIFn == LibFunc_ZdlPvSt11align_val_t || // delete(void*, align_val_t)
448  TLIFn == LibFunc_ZdaPvj || // delete[](void*, uint)
449  TLIFn == LibFunc_ZdaPvm || // delete[](void*, ulong)
450  TLIFn == LibFunc_ZdaPvRKSt9nothrow_t || // delete[](void*, nothrow)
451  TLIFn == LibFunc_ZdaPvSt11align_val_t || // delete[](void*, align_val_t)
452  TLIFn == LibFunc_msvc_delete_ptr32_int || // delete(void*, uint)
453  TLIFn == LibFunc_msvc_delete_ptr64_longlong || // delete(void*, ulonglong)
454  TLIFn == LibFunc_msvc_delete_ptr32_nothrow || // delete(void*, nothrow)
455  TLIFn == LibFunc_msvc_delete_ptr64_nothrow || // delete(void*, nothrow)
456  TLIFn == LibFunc_msvc_delete_array_ptr32_int || // delete[](void*, uint)
457  TLIFn == LibFunc_msvc_delete_array_ptr64_longlong || // delete[](void*, ulonglong)
458  TLIFn == LibFunc_msvc_delete_array_ptr32_nothrow || // delete[](void*, nothrow)
459  TLIFn == LibFunc_msvc_delete_array_ptr64_nothrow || // delete[](void*, nothrow)
460  TLIFn == LibFunc___kmpc_free_shared) // OpenMP Offloading RTL free
461  ExpectedNumParams = 2;
462  else if (TLIFn == LibFunc_ZdaPvSt11align_val_tRKSt9nothrow_t || // delete(void*, align_val_t, nothrow)
463  TLIFn == LibFunc_ZdlPvSt11align_val_tRKSt9nothrow_t || // delete[](void*, align_val_t, nothrow)
464  TLIFn == LibFunc_ZdlPvjSt11align_val_t || // delete(void*, unsigned long, align_val_t)
465  TLIFn == LibFunc_ZdlPvmSt11align_val_t || // delete(void*, unsigned long, align_val_t)
466  TLIFn == LibFunc_ZdaPvjSt11align_val_t || // delete[](void*, unsigned int, align_val_t)
467  TLIFn == LibFunc_ZdaPvmSt11align_val_t) // delete[](void*, unsigned long, align_val_t)
468  ExpectedNumParams = 3;
469  else
470  return false;
471 
472  // Check free prototype.
473  // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin
474  // attribute will exist.
475  FunctionType *FTy = F->getFunctionType();
476  if (!FTy->getReturnType()->isVoidTy())
477  return false;
478  if (FTy->getNumParams() != ExpectedNumParams)
479  return false;
480  if (FTy->getParamType(0) != Type::getInt8PtrTy(F->getContext()))
481  return false;
482 
483  return true;
484 }
485 
486 /// isFreeCall - Returns non-null if the value is a call to the builtin free()
487 const CallInst *llvm::isFreeCall(const Value *I, const TargetLibraryInfo *TLI) {
488  bool IsNoBuiltinCall;
489  const Function *Callee =
490  getCalledFunction(I, /*LookThroughBitCast=*/false, IsNoBuiltinCall);
491  if (Callee == nullptr || IsNoBuiltinCall)
492  return nullptr;
493 
494  LibFunc TLIFn;
495  if (!TLI || !TLI->getLibFunc(*Callee, TLIFn) || !TLI->has(TLIFn))
496  return nullptr;
497 
498  return isLibFreeFunction(Callee, TLIFn) ? dyn_cast<CallInst>(I) : nullptr;
499 }
500 
501 
502 //===----------------------------------------------------------------------===//
503 // Utility functions to compute size of objects.
504 //
506  if (Data.second.isNegative() || Data.first.ult(Data.second))
507  return APInt(Data.first.getBitWidth(), 0);
508  return Data.first - Data.second;
509 }
510 
511 /// Compute the size of the object pointed by Ptr. Returns true and the
512 /// object size in Size if successful, and false otherwise.
513 /// If RoundToAlign is true, then Size is rounded up to the alignment of
514 /// allocas, byval arguments, and global variables.
515 bool llvm::getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout &DL,
516  const TargetLibraryInfo *TLI, ObjectSizeOpts Opts) {
517  ObjectSizeOffsetVisitor Visitor(DL, TLI, Ptr->getContext(), Opts);
518  SizeOffsetType Data = Visitor.compute(const_cast<Value*>(Ptr));
519  if (!Visitor.bothKnown(Data))
520  return false;
521 
523  return true;
524 }
525 
527  const DataLayout &DL,
528  const TargetLibraryInfo *TLI,
529  bool MustSucceed) {
530  assert(ObjectSize->getIntrinsicID() == Intrinsic::objectsize &&
531  "ObjectSize must be a call to llvm.objectsize!");
532 
533  bool MaxVal = cast<ConstantInt>(ObjectSize->getArgOperand(1))->isZero();
534  ObjectSizeOpts EvalOptions;
535  // Unless we have to fold this to something, try to be as accurate as
536  // possible.
537  if (MustSucceed)
538  EvalOptions.EvalMode =
540  else
542 
543  EvalOptions.NullIsUnknownSize =
544  cast<ConstantInt>(ObjectSize->getArgOperand(2))->isOne();
545 
546  auto *ResultType = cast<IntegerType>(ObjectSize->getType());
547  bool StaticOnly = cast<ConstantInt>(ObjectSize->getArgOperand(3))->isZero();
548  if (StaticOnly) {
549  // FIXME: Does it make sense to just return a failure value if the size won't
550  // fit in the output and `!MustSucceed`?
551  uint64_t Size;
552  if (getObjectSize(ObjectSize->getArgOperand(0), Size, DL, TLI, EvalOptions) &&
553  isUIntN(ResultType->getBitWidth(), Size))
554  return ConstantInt::get(ResultType, Size);
555  } else {
556  LLVMContext &Ctx = ObjectSize->getFunction()->getContext();
557  ObjectSizeOffsetEvaluator Eval(DL, TLI, Ctx, EvalOptions);
558  SizeOffsetEvalType SizeOffsetPair =
559  Eval.compute(ObjectSize->getArgOperand(0));
560 
561  if (SizeOffsetPair != ObjectSizeOffsetEvaluator::unknown()) {
563  Builder.SetInsertPoint(ObjectSize);
564 
565  // If we've outside the end of the object, then we can always access
566  // exactly 0 bytes.
567  Value *ResultSize =
568  Builder.CreateSub(SizeOffsetPair.first, SizeOffsetPair.second);
569  Value *UseZero =
570  Builder.CreateICmpULT(SizeOffsetPair.first, SizeOffsetPair.second);
571  ResultSize = Builder.CreateZExtOrTrunc(ResultSize, ResultType);
572  Value *Ret = Builder.CreateSelect(
573  UseZero, ConstantInt::get(ResultType, 0), ResultSize);
574 
575  // The non-constant size expression cannot evaluate to -1.
576  if (!isa<Constant>(SizeOffsetPair.first) ||
577  !isa<Constant>(SizeOffsetPair.second))
578  Builder.CreateAssumption(
579  Builder.CreateICmpNE(Ret, ConstantInt::get(ResultType, -1)));
580 
581  return Ret;
582  }
583  }
584 
585  if (!MustSucceed)
586  return nullptr;
587 
588  return ConstantInt::get(ResultType, MaxVal ? -1ULL : 0);
589 }
590 
591 STATISTIC(ObjectVisitorArgument,
592  "Number of arguments with unsolved size and offset");
593 STATISTIC(ObjectVisitorLoad,
594  "Number of load instructions with unsolved size and offset");
595 
596 APInt ObjectSizeOffsetVisitor::align(APInt Size, uint64_t Alignment) {
597  if (Options.RoundToAlign && Alignment)
598  return APInt(IntTyBits, alignTo(Size.getZExtValue(), Align(Alignment)));
599  return Size;
600 }
601 
603  const TargetLibraryInfo *TLI,
606  : DL(DL), TLI(TLI), Options(Options) {
607  // Pointer size must be rechecked for each object visited since it could have
608  // a different address space.
609 }
610 
612  IntTyBits = DL.getIndexTypeSizeInBits(V->getType());
613  Zero = APInt::getNullValue(IntTyBits);
614 
615  V = V->stripPointerCasts();
616  if (Instruction *I = dyn_cast<Instruction>(V)) {
617  // If we have already seen this instruction, bail out. Cycles can happen in
618  // unreachable code after constant propagation.
619  if (!SeenInsts.insert(I).second)
620  return unknown();
621 
622  if (GEPOperator *GEP = dyn_cast<GEPOperator>(V))
623  return visitGEPOperator(*GEP);
624  return visit(*I);
625  }
626  if (Argument *A = dyn_cast<Argument>(V))
627  return visitArgument(*A);
628  if (ConstantPointerNull *P = dyn_cast<ConstantPointerNull>(V))
629  return visitConstantPointerNull(*P);
630  if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
631  return visitGlobalAlias(*GA);
632  if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
633  return visitGlobalVariable(*GV);
634  if (UndefValue *UV = dyn_cast<UndefValue>(V))
635  return visitUndefValue(*UV);
636  if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
637  if (CE->getOpcode() == Instruction::IntToPtr)
638  return unknown(); // clueless
639  if (CE->getOpcode() == Instruction::GetElementPtr)
640  return visitGEPOperator(cast<GEPOperator>(*CE));
641  }
642 
643  LLVM_DEBUG(dbgs() << "ObjectSizeOffsetVisitor::compute() unhandled value: "
644  << *V << '\n');
645  return unknown();
646 }
647 
648 /// When we're compiling N-bit code, and the user uses parameters that are
649 /// greater than N bits (e.g. uint64_t on a 32-bit build), we can run into
650 /// trouble with APInt size issues. This function handles resizing + overflow
651 /// checks for us. Check and zext or trunc \p I depending on IntTyBits and
652 /// I's value.
653 bool ObjectSizeOffsetVisitor::CheckedZextOrTrunc(APInt &I) {
654  // More bits than we can handle. Checking the bit width isn't necessary, but
655  // it's faster than checking active bits, and should give `false` in the
656  // vast majority of cases.
657  if (I.getBitWidth() > IntTyBits && I.getActiveBits() > IntTyBits)
658  return false;
659  if (I.getBitWidth() != IntTyBits)
660  I = I.zextOrTrunc(IntTyBits);
661  return true;
662 }
663 
665  if (!I.getAllocatedType()->isSized())
666  return unknown();
667 
668  if (isa<ScalableVectorType>(I.getAllocatedType()))
669  return unknown();
670 
671  APInt Size(IntTyBits, DL.getTypeAllocSize(I.getAllocatedType()));
672  if (!I.isArrayAllocation())
673  return std::make_pair(align(Size, I.getAlignment()), Zero);
674 
675  Value *ArraySize = I.getArraySize();
676  if (const ConstantInt *C = dyn_cast<ConstantInt>(ArraySize)) {
677  APInt NumElems = C->getValue();
678  if (!CheckedZextOrTrunc(NumElems))
679  return unknown();
680 
681  bool Overflow;
682  Size = Size.umul_ov(NumElems, Overflow);
683  return Overflow ? unknown() : std::make_pair(align(Size, I.getAlignment()),
684  Zero);
685  }
686  return unknown();
687 }
688 
690  Type *MemoryTy = A.getPointeeInMemoryValueType();
691  // No interprocedural analysis is done at the moment.
692  if (!MemoryTy|| !MemoryTy->isSized()) {
693  ++ObjectVisitorArgument;
694  return unknown();
695  }
696 
697  APInt Size(IntTyBits, DL.getTypeAllocSize(MemoryTy));
698  return std::make_pair(align(Size, A.getParamAlignment()), Zero);
699 }
700 
702  Optional<AllocFnsTy> FnData = getAllocationSize(&CB, TLI);
703  if (!FnData)
704  return unknown();
705 
706  // Handle strdup-like functions separately.
707  if (FnData->AllocTy == StrDupLike) {
708  APInt Size(IntTyBits, GetStringLength(CB.getArgOperand(0)));
709  if (!Size)
710  return unknown();
711 
712  // Strndup limits strlen.
713  if (FnData->FstParam > 0) {
714  ConstantInt *Arg =
715  dyn_cast<ConstantInt>(CB.getArgOperand(FnData->FstParam));
716  if (!Arg)
717  return unknown();
718 
719  APInt MaxSize = Arg->getValue().zextOrSelf(IntTyBits);
720  if (Size.ugt(MaxSize))
721  Size = MaxSize + 1;
722  }
723  return std::make_pair(Size, Zero);
724  }
725 
726  ConstantInt *Arg = dyn_cast<ConstantInt>(CB.getArgOperand(FnData->FstParam));
727  if (!Arg)
728  return unknown();
729 
730  APInt Size = Arg->getValue();
731  if (!CheckedZextOrTrunc(Size))
732  return unknown();
733 
734  // Size is determined by just 1 parameter.
735  if (FnData->SndParam < 0)
736  return std::make_pair(Size, Zero);
737 
738  Arg = dyn_cast<ConstantInt>(CB.getArgOperand(FnData->SndParam));
739  if (!Arg)
740  return unknown();
741 
742  APInt NumElems = Arg->getValue();
743  if (!CheckedZextOrTrunc(NumElems))
744  return unknown();
745 
746  bool Overflow;
747  Size = Size.umul_ov(NumElems, Overflow);
748  return Overflow ? unknown() : std::make_pair(Size, Zero);
749 
750  // TODO: handle more standard functions (+ wchar cousins):
751  // - strdup / strndup
752  // - strcpy / strncpy
753  // - strcat / strncat
754  // - memcpy / memmove
755  // - strcat / strncat
756  // - memset
757 }
758 
761  // If null is unknown, there's nothing we can do. Additionally, non-zero
762  // address spaces can make use of null, so we don't presume to know anything
763  // about that.
764  //
765  // TODO: How should this work with address space casts? We currently just drop
766  // them on the floor, but it's unclear what we should do when a NULL from
767  // addrspace(1) gets casted to addrspace(0) (or vice-versa).
768  if (Options.NullIsUnknownSize || CPN.getType()->getAddressSpace())
769  return unknown();
770  return std::make_pair(Zero, Zero);
771 }
772 
775  return unknown();
776 }
777 
780  // Easy cases were already folded by previous passes.
781  return unknown();
782 }
783 
785  SizeOffsetType PtrData = compute(GEP.getPointerOperand());
786  APInt Offset(DL.getIndexTypeSizeInBits(GEP.getPointerOperand()->getType()), 0);
787  if (!bothKnown(PtrData) || !GEP.accumulateConstantOffset(DL, Offset))
788  return unknown();
789 
790  return std::make_pair(PtrData.first, PtrData.second + Offset);
791 }
792 
794  if (GA.isInterposable())
795  return unknown();
796  return compute(GA.getAliasee());
797 }
798 
800  if (!GV.hasDefinitiveInitializer())
801  return unknown();
802 
803  APInt Size(IntTyBits, DL.getTypeAllocSize(GV.getValueType()));
804  return std::make_pair(align(Size, GV.getAlignment()), Zero);
805 }
806 
808  // clueless
809  return unknown();
810 }
811 
813  ++ObjectVisitorLoad;
814  return unknown();
815 }
816 
818  // too complex to analyze statically.
819  return unknown();
820 }
821 
823  SizeOffsetType TrueSide = compute(I.getTrueValue());
824  SizeOffsetType FalseSide = compute(I.getFalseValue());
825  if (bothKnown(TrueSide) && bothKnown(FalseSide)) {
826  if (TrueSide == FalseSide) {
827  return TrueSide;
828  }
829 
830  APInt TrueResult = getSizeWithOverflow(TrueSide);
831  APInt FalseResult = getSizeWithOverflow(FalseSide);
832 
833  if (TrueResult == FalseResult) {
834  return TrueSide;
835  }
836  if (Options.EvalMode == ObjectSizeOpts::Mode::Min) {
837  if (TrueResult.slt(FalseResult))
838  return TrueSide;
839  return FalseSide;
840  }
841  if (Options.EvalMode == ObjectSizeOpts::Mode::Max) {
842  if (TrueResult.sgt(FalseResult))
843  return TrueSide;
844  return FalseSide;
845  }
846  }
847  return unknown();
848 }
849 
851  return std::make_pair(Zero, Zero);
852 }
853 
855  LLVM_DEBUG(dbgs() << "ObjectSizeOffsetVisitor unknown instruction:" << I
856  << '\n');
857  return unknown();
858 }
859 
861  const DataLayout &DL, const TargetLibraryInfo *TLI, LLVMContext &Context,
862  ObjectSizeOpts EvalOpts)
863  : DL(DL), TLI(TLI), Context(Context),
866  [&](Instruction *I) { InsertedInstructions.insert(I); })),
867  EvalOpts(EvalOpts) {
868  // IntTy and Zero must be set for each compute() since the address space may
869  // be different for later objects.
870 }
871 
873  // XXX - Are vectors of pointers possible here?
874  IntTy = cast<IntegerType>(DL.getIndexType(V->getType()));
875  Zero = ConstantInt::get(IntTy, 0);
876 
877  SizeOffsetEvalType Result = compute_(V);
878 
879  if (!bothKnown(Result)) {
880  // Erase everything that was computed in this iteration from the cache, so
881  // that no dangling references are left behind. We could be a bit smarter if
882  // we kept a dependency graph. It's probably not worth the complexity.
883  for (const Value *SeenVal : SeenVals) {
884  CacheMapTy::iterator CacheIt = CacheMap.find(SeenVal);
885  // non-computable results can be safely cached
886  if (CacheIt != CacheMap.end() && anyKnown(CacheIt->second))
887  CacheMap.erase(CacheIt);
888  }
889 
890  // Erase any instructions we inserted as part of the traversal.
891  for (Instruction *I : InsertedInstructions) {
892  I->replaceAllUsesWith(UndefValue::get(I->getType()));
893  I->eraseFromParent();
894  }
895  }
896 
897  SeenVals.clear();
898  InsertedInstructions.clear();
899  return Result;
900 }
901 
902 SizeOffsetEvalType ObjectSizeOffsetEvaluator::compute_(Value *V) {
903  ObjectSizeOffsetVisitor Visitor(DL, TLI, Context, EvalOpts);
904  SizeOffsetType Const = Visitor.compute(V);
905  if (Visitor.bothKnown(Const))
906  return std::make_pair(ConstantInt::get(Context, Const.first),
907  ConstantInt::get(Context, Const.second));
908 
909  V = V->stripPointerCasts();
910 
911  // Check cache.
912  CacheMapTy::iterator CacheIt = CacheMap.find(V);
913  if (CacheIt != CacheMap.end())
914  return CacheIt->second;
915 
916  // Always generate code immediately before the instruction being
917  // processed, so that the generated code dominates the same BBs.
918  BuilderTy::InsertPointGuard Guard(Builder);
919  if (Instruction *I = dyn_cast<Instruction>(V))
920  Builder.SetInsertPoint(I);
921 
922  // Now compute the size and offset.
923  SizeOffsetEvalType Result;
924 
925  // Record the pointers that were handled in this run, so that they can be
926  // cleaned later if something fails. We also use this set to break cycles that
927  // can occur in dead code.
928  if (!SeenVals.insert(V).second) {
929  Result = unknown();
930  } else if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
931  Result = visitGEPOperator(*GEP);
932  } else if (Instruction *I = dyn_cast<Instruction>(V)) {
933  Result = visit(*I);
934  } else if (isa<Argument>(V) ||
935  (isa<ConstantExpr>(V) &&
936  cast<ConstantExpr>(V)->getOpcode() == Instruction::IntToPtr) ||
937  isa<GlobalAlias>(V) ||
938  isa<GlobalVariable>(V)) {
939  // Ignore values where we cannot do more than ObjectSizeVisitor.
940  Result = unknown();
941  } else {
942  LLVM_DEBUG(
943  dbgs() << "ObjectSizeOffsetEvaluator::compute() unhandled value: " << *V
944  << '\n');
945  Result = unknown();
946  }
947 
948  // Don't reuse CacheIt since it may be invalid at this point.
949  CacheMap[V] = Result;
950  return Result;
951 }
952 
954  if (!I.getAllocatedType()->isSized())
955  return unknown();
956 
957  // must be a VLA
958  assert(I.isArrayAllocation());
959 
960  // If needed, adjust the alloca's operand size to match the pointer size.
961  // Subsequent math operations expect the types to match.
962  Value *ArraySize = Builder.CreateZExtOrTrunc(
963  I.getArraySize(), DL.getIntPtrType(I.getContext()));
964  assert(ArraySize->getType() == Zero->getType() &&
965  "Expected zero constant to have pointer type");
966 
967  Value *Size = ConstantInt::get(ArraySize->getType(),
968  DL.getTypeAllocSize(I.getAllocatedType()));
969  Size = Builder.CreateMul(Size, ArraySize);
970  return std::make_pair(Size, Zero);
971 }
972 
974  Optional<AllocFnsTy> FnData = getAllocationSize(&CB, TLI);
975  if (!FnData)
976  return unknown();
977 
978  // Handle strdup-like functions separately.
979  if (FnData->AllocTy == StrDupLike) {
980  // TODO
981  return unknown();
982  }
983 
984  Value *FirstArg = CB.getArgOperand(FnData->FstParam);
985  FirstArg = Builder.CreateZExtOrTrunc(FirstArg, IntTy);
986  if (FnData->SndParam < 0)
987  return std::make_pair(FirstArg, Zero);
988 
989  Value *SecondArg = CB.getArgOperand(FnData->SndParam);
990  SecondArg = Builder.CreateZExtOrTrunc(SecondArg, IntTy);
991  Value *Size = Builder.CreateMul(FirstArg, SecondArg);
992  return std::make_pair(Size, Zero);
993 
994  // TODO: handle more standard functions (+ wchar cousins):
995  // - strdup / strndup
996  // - strcpy / strncpy
997  // - strcat / strncat
998  // - memcpy / memmove
999  // - strcat / strncat
1000  // - memset
1001 }
1002 
1005  return unknown();
1006 }
1007 
1010  return unknown();
1011 }
1012 
1015  SizeOffsetEvalType PtrData = compute_(GEP.getPointerOperand());
1016  if (!bothKnown(PtrData))
1017  return unknown();
1018 
1019  Value *Offset = EmitGEPOffset(&Builder, DL, &GEP, /*NoAssumptions=*/true);
1020  Offset = Builder.CreateAdd(PtrData.second, Offset);
1021  return std::make_pair(PtrData.first, Offset);
1022 }
1023 
1025  // clueless
1026  return unknown();
1027 }
1028 
1030  return unknown();
1031 }
1032 
1034  // Create 2 PHIs: one for size and another for offset.
1035  PHINode *SizePHI = Builder.CreatePHI(IntTy, PHI.getNumIncomingValues());
1036  PHINode *OffsetPHI = Builder.CreatePHI(IntTy, PHI.getNumIncomingValues());
1037 
1038  // Insert right away in the cache to handle recursive PHIs.
1039  CacheMap[&PHI] = std::make_pair(SizePHI, OffsetPHI);
1040 
1041  // Compute offset/size for each PHI incoming pointer.
1042  for (unsigned i = 0, e = PHI.getNumIncomingValues(); i != e; ++i) {
1044  SizeOffsetEvalType EdgeData = compute_(PHI.getIncomingValue(i));
1045 
1046  if (!bothKnown(EdgeData)) {
1047  OffsetPHI->replaceAllUsesWith(UndefValue::get(IntTy));
1048  OffsetPHI->eraseFromParent();
1049  InsertedInstructions.erase(OffsetPHI);
1050  SizePHI->replaceAllUsesWith(UndefValue::get(IntTy));
1051  SizePHI->eraseFromParent();
1052  InsertedInstructions.erase(SizePHI);
1053  return unknown();
1054  }
1055  SizePHI->addIncoming(EdgeData.first, PHI.getIncomingBlock(i));
1056  OffsetPHI->addIncoming(EdgeData.second, PHI.getIncomingBlock(i));
1057  }
1058 
1059  Value *Size = SizePHI, *Offset = OffsetPHI;
1060  if (Value *Tmp = SizePHI->hasConstantValue()) {
1061  Size = Tmp;
1062  SizePHI->replaceAllUsesWith(Size);
1063  SizePHI->eraseFromParent();
1064  InsertedInstructions.erase(SizePHI);
1065  }
1066  if (Value *Tmp = OffsetPHI->hasConstantValue()) {
1067  Offset = Tmp;
1068  OffsetPHI->replaceAllUsesWith(Offset);
1069  OffsetPHI->eraseFromParent();
1070  InsertedInstructions.erase(OffsetPHI);
1071  }
1072  return std::make_pair(Size, Offset);
1073 }
1074 
1076  SizeOffsetEvalType TrueSide = compute_(I.getTrueValue());
1077  SizeOffsetEvalType FalseSide = compute_(I.getFalseValue());
1078 
1079  if (!bothKnown(TrueSide) || !bothKnown(FalseSide))
1080  return unknown();
1081  if (TrueSide == FalseSide)
1082  return TrueSide;
1083 
1084  Value *Size = Builder.CreateSelect(I.getCondition(), TrueSide.first,
1085  FalseSide.first);
1086  Value *Offset = Builder.CreateSelect(I.getCondition(), TrueSide.second,
1087  FalseSide.second);
1088  return std::make_pair(Size, Offset);
1089 }
1090 
1092  LLVM_DEBUG(dbgs() << "ObjectSizeOffsetEvaluator unknown instruction:" << I
1093  << '\n');
1094  return unknown();
1095 }
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
i
i
Definition: README.txt:29
llvm::ObjectSizeOffsetEvaluator::unknown
static SizeOffsetEvalType unknown()
Definition: MemoryBuiltins.h:315
llvm::alignTo
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:148
llvm::SizeOffsetType
std::pair< APInt, APInt > SizeOffsetType
Definition: MemoryBuiltins.h:231
llvm::Argument
This class represents an incoming formal argument to a Function.
Definition: Argument.h:29
llvm::Type::isSized
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition: Type.h:274
getAllocationSize
static Optional< AllocFnsTy > getAllocationSize(const Value *V, const TargetLibraryInfo *TLI)
Definition: MemoryBuiltins.cpp:202
llvm::isMallocOrCallocLikeFn
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...
Definition: MemoryBuiltins.cpp:298
llvm::ObjectSizeOffsetVisitor::visitGlobalAlias
SizeOffsetType visitGlobalAlias(GlobalAlias &GA)
Definition: MemoryBuiltins.cpp:793
MathExtras.h
llvm::IRBuilderBase::SetInsertPoint
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Definition: IRBuilder.h:184
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::isCallocLikeFn
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...
Definition: MemoryBuiltins.cpp:291
AllocFnsTy::NumParams
unsigned NumParams
Definition: MemoryBuiltins.cpp:66
llvm::SmallPtrSetImpl::erase
bool erase(PtrType Ptr)
erase - If the set contains the specified pointer, remove it and return true, otherwise return false.
Definition: SmallPtrSet.h:378
llvm::DataLayout::getIndexTypeSizeInBits
unsigned getIndexTypeSizeInBits(Type *Ty) const
Layout size of the index used in GEP calculation.
Definition: DataLayout.cpp:724
Optional.h
llvm::DataLayout
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:112
llvm::Type::getInt8PtrTy
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:255
IntrinsicInst.h
llvm::ExtractElementInst
This instruction extracts a single (scalar) element from a VectorType value.
Definition: Instructions.h:1875
llvm::PointerType::getElementType
Type * getElementType() const
Definition: DerivedTypes.h:672
llvm::Function
Definition: Function.h:61
llvm::Attribute
Definition: Attributes.h:52
StringRef.h
TargetFolder.h
P
This currently compiles esp xmm0 movsd esp eax eax esp ret We should use not the dag combiner This is because dagcombine2 needs to be able to see through the X86ISD::Wrapper which DAGCombine can t really do The code for turning x load into a single vector load is target independent and should be moved to the dag combiner The code for turning x load into a vector load can only handle a direct load from a global or a direct load from the stack It should be generalized to handle any load from P
Definition: README-SSE.txt:411
llvm::IntrinsicInst::getIntrinsicID
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
Definition: IntrinsicInst.h:52
llvm::BitCastInst
This class represents a no-op cast from one type to another.
Definition: Instructions.h:5194
Statistic.h
llvm::ObjectSizeOffsetEvaluator::compute
SizeOffsetEvalType compute(Value *V)
Definition: MemoryBuiltins.cpp:872
llvm::ObjectSizeOffsetVisitor::visitExtractValueInst
SizeOffsetType visitExtractValueInst(ExtractValueInst &I)
Definition: MemoryBuiltins.cpp:779
llvm::IRBuilder
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2643
llvm::GlobalVariable
Definition: GlobalVariable.h:40
llvm::PointerType::getAddressSpace
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Definition: DerivedTypes.h:686
llvm::ObjectSizeOffsetEvaluator::visitSelectInst
SizeOffsetEvalType visitSelectInst(SelectInst &I)
Definition: MemoryBuiltins.cpp:1075
llvm::ObjectSizeOffsetEvaluator::visitGEPOperator
SizeOffsetEvalType visitGEPOperator(GEPOperator &GEP)
Definition: MemoryBuiltins.cpp:1014
llvm::GlobalAlias
Definition: GlobalAlias.h:27
ValueTracking.h
llvm::isAllocLikeFn
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,...
Definition: MemoryBuiltins.cpp:306
llvm::DenseMapBase::erase
bool erase(const KeyT &Val)
Definition: DenseMap.h:302
APInt.h
llvm::Function::getContext
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition: Function.cpp:321
CallocLike
@ CallocLike
Definition: MemoryBuiltins.cpp:56
llvm::DataLayout::getIndexType
Type * getIndexType(Type *PtrTy) const
Returns the type of a GEP index.
Definition: DataLayout.cpp:861
llvm::DenseMapIterator
Definition: DenseMap.h:56
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
MemoryBuiltins.h
llvm::ObjectSizeOpts::NullIsUnknownSize
bool NullIsUnknownSize
If this is true, null pointers in address space 0 will be treated as though they can't be evaluated.
Definition: MemoryBuiltins.h:208
AllocFnsTy
Definition: MemoryBuiltins.cpp:64
llvm::sys::path::end
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:233
llvm::Optional
Definition: APInt.h:33
llvm::ObjectSizeOffsetEvaluator::visitCallBase
SizeOffsetEvalType visitCallBase(CallBase &CB)
Definition: MemoryBuiltins.cpp:973
T
#define T
Definition: Mips16ISelLowering.cpp:341
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
llvm::ObjectSizeOpts::RoundToAlign
bool RoundToAlign
Whether to round the result up to the alignment of allocas, byval arguments, and global variables.
Definition: MemoryBuiltins.h:204
Operator.h
llvm::FunctionType::getNumParams
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
Definition: DerivedTypes.h:138
llvm::MipsISD::Ret
@ Ret
Definition: MipsISelLowering.h:116
STLExtras.h
llvm::ObjectSizeOffsetEvaluator::visitLoadInst
SizeOffsetEvalType visitLoadInst(LoadInst &I)
Definition: MemoryBuiltins.cpp:1029
llvm::EmitGEPOffset
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:29
llvm::Data
@ Data
Definition: SIMachineScheduler.h:56
llvm::isAllocationFn
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...
Definition: MemoryBuiltins.cpp:242
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:122
F
#define F(x, y, z)
Definition: MD5.cpp:56
computeArraySize
static Value * computeArraySize(const CallInst *CI, const DataLayout &DL, const TargetLibraryInfo *TLI, bool LookThroughSExt=false)
Definition: MemoryBuiltins.cpp:347
Context
LLVMContext & Context
Definition: NVVMIntrRange.cpp:66
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
llvm::ObjectSizeOffsetEvaluator::visitIntToPtrInst
SizeOffsetEvalType visitIntToPtrInst(IntToPtrInst &)
Definition: MemoryBuiltins.cpp:1024
Arg
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
Definition: AMDGPULibCalls.cpp:206
llvm::ConstantPointerNull
A constant pointer value that points to null.
Definition: Constants.h:534
Instruction.h
MallocOrCallocLike
@ MallocOrCallocLike
Definition: MemoryBuiltins.cpp:59
llvm::getMallocArraySize
Value * getMallocArraySize(CallInst *CI, const DataLayout &DL, const TargetLibraryInfo *TLI, bool LookThroughSExt=false)
getMallocArraySize - Returns the array size of a malloc call.
Definition: MemoryBuiltins.cpp:419
llvm::ConstantInt
This is the shared class of boolean and integer constants.
Definition: Constants.h:79
llvm::getMallocAllocatedType
Type * getMallocAllocatedType(const CallInst *CI, const TargetLibraryInfo *TLI)
getMallocAllocatedType - Returns the Type allocated by malloc call.
Definition: MemoryBuiltins.cpp:408
llvm::ObjectSizeOffsetVisitor::bothKnown
static bool bothKnown(const SizeOffsetType &SizeOffset)
Definition: MemoryBuiltins.h:264
llvm::IRBuilderBase::CreateMul
Value * CreateMul(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1242
hasNoAliasAttr
static bool hasNoAliasAttr(const Value *V, bool LookThroughBitCast)
Definition: MemoryBuiltins.cpp:233
Constants.h
llvm::PHINode::getIncomingValue
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
Definition: Instructions.h:2723
llvm::User
Definition: User.h:44
llvm::LibFunc
LibFunc
Definition: TargetLibraryInfo.h:34
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::ObjectSizeOffsetVisitor::visitIntToPtrInst
SizeOffsetType visitIntToPtrInst(IntToPtrInst &)
Definition: MemoryBuiltins.cpp:807
llvm::ObjectSizeOffsetEvaluator::ObjectSizeOffsetEvaluator
ObjectSizeOffsetEvaluator(const DataLayout &DL, const TargetLibraryInfo *TLI, LLVMContext &Context, ObjectSizeOpts EvalOpts={})
Definition: MemoryBuiltins.cpp:860
llvm::ObjectSizeOpts::Mode::Exact
@ Exact
Fail to evaluate an unknown condition.
llvm::getMallocType
PointerType * getMallocType(const CallInst *CI, const TargetLibraryInfo *TLI)
getMallocType - Returns the PointerType resulting from the malloc call.
Definition: MemoryBuiltins.cpp:377
getOpcode
static Optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition: VPlanSLP.cpp:199
llvm::ms_demangle::QualifierMangleMode::Result
@ Result
TargetLibraryInfo.h
llvm::BasicBlock::getFirstInsertionPt
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:249
llvm::isUIntN
bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition: MathExtras.h:455
llvm::TargetLibraryInfo::getLibFunc
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
Definition: TargetLibraryInfo.h:289
llvm::ObjectSizeOffsetVisitor::visitGlobalVariable
SizeOffsetType visitGlobalVariable(GlobalVariable &GV)
Definition: MemoryBuiltins.cpp:799
llvm::Instruction
Definition: Instruction.h:45
llvm::ObjectSizeOffsetVisitor::visitAllocaInst
SizeOffsetType visitAllocaInst(AllocaInst &I)
Definition: MemoryBuiltins.cpp:664
llvm::ObjectSizeOffsetEvaluator::visitExtractElementInst
SizeOffsetEvalType visitExtractElementInst(ExtractElementInst &I)
Definition: MemoryBuiltins.cpp:1004
llvm::ComputeMultiple
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.
Definition: ValueTracking.cpp:3212
AnyAlloc
@ AnyAlloc
Definition: MemoryBuiltins.cpp:61
llvm::APInt::getZExtValue
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1631
Options
const char LLVMTargetMachineRef LLVMPassBuilderOptionsRef Options
Definition: PassBuilderBindings.cpp:48
llvm::STATISTIC
STATISTIC(NumFunctions, "Total number of functions")
ReallocLike
@ ReallocLike
Definition: MemoryBuiltins.cpp:57
llvm::UndefValue::get
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1784
llvm::ConstantInt::get
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:899
llvm::ObjectSizeOffsetEvaluator
Evaluate the size and offset of an object pointed to by a Value*.
Definition: MemoryBuiltins.h:294
llvm::isReallocLikeFn
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....
Definition: MemoryBuiltins.cpp:313
llvm::FunctionCallee::getFunctionType
FunctionType * getFunctionType()
Definition: DerivedTypes.h:181
llvm::ObjectSizeOffsetEvaluator::visitPHINode
SizeOffsetEvalType visitPHINode(PHINode &PHI)
Definition: MemoryBuiltins.cpp:1033
llvm::Align
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
llvm::getObjectSize
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.
Definition: MemoryBuiltins.cpp:515
llvm::PHINode::getNumIncomingValues
unsigned getNumIncomingValues() const
Return the number of incoming edges.
Definition: Instructions.h:2719
llvm::None
const NoneType None
Definition: None.h:23
Type.h
getCalledFunction
static const Function * getCalledFunction(const Value *V, bool LookThroughBitCast, bool &IsNoBuiltin)
Definition: MemoryBuiltins.cpp:118
llvm::Type::isIntegerTy
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:201
llvm::function_ref
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:168
llvm::APInt::slt
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition: APInt.h:1224
llvm::isMallocLikeFn
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 ...
Definition: MemoryBuiltins.cpp:264
llvm::Instruction::eraseFromParent
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Definition: Instruction.cpp:78
llvm::PHINode::hasConstantValue
Value * hasConstantValue() const
If the specified PHI node always merges together the same value, return the value,...
Definition: Instructions.cpp:152
AlignedAllocLike
@ AlignedAllocLike
Definition: MemoryBuiltins.cpp:55
llvm::extractCallocCall
const CallInst * extractCallocCall(const Value *I, const TargetLibraryInfo *TLI)
extractCallocCall - Returns the corresponding CallInst if the instruction is a calloc call.
Definition: MemoryBuiltins.cpp:428
llvm::GlobalVariable::hasDefinitiveInitializer
bool hasDefinitiveInitializer() const
hasDefinitiveInitializer - Whether the global variable has an initializer, and any other instances of...
Definition: GlobalVariable.h:110
llvm::ARM_MB::ST
@ ST
Definition: ARMBaseInfo.h:73
AllocFnsTy::SndParam
int SndParam
Definition: MemoryBuiltins.cpp:68
llvm::isStrdupLikeFn
bool isStrdupLikeFn(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 (strdup,...
Definition: MemoryBuiltins.cpp:333
llvm::PHINode::addIncoming
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
Definition: Instructions.h:2777
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
llvm::numbers::e
constexpr double e
Definition: MathExtras.h:57
llvm::ObjectSizeOffsetVisitor::visitUndefValue
SizeOffsetType visitUndefValue(UndefValue &)
Definition: MemoryBuiltins.cpp:850
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::FunctionType::getParamType
Type * getParamType(unsigned i) const
Parameter type accessors.
Definition: DerivedTypes.h:134
OpNewLike
@ OpNewLike
Definition: MemoryBuiltins.cpp:53
llvm::UndefValue
'undef' values are things that do not have specified contents.
Definition: Constants.h:1352
llvm::ObjectSizeOffsetVisitor
Evaluate the size and offset of an object pointed to by a Value* statically.
Definition: MemoryBuiltins.h:235
llvm::lowerObjectSizeCall
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.
Definition: MemoryBuiltins.cpp:526
llvm::IRBuilderBase::CreateSelect
Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="", Instruction *MDFrom=nullptr)
Definition: IRBuilder.cpp:974
llvm::ObjectSizeOffsetVisitor::visitSelectInst
SizeOffsetType visitSelectInst(SelectInst &I)
Definition: MemoryBuiltins.cpp:822
llvm::PointerType
Class to represent pointers.
Definition: DerivedTypes.h:631
llvm::InstVisitor< ObjectSizeOffsetVisitor, SizeOffsetType >::visit
void visit(Iterator Start, Iterator End)
Definition: InstVisitor.h:88
llvm::IRBuilderBase::CreateAdd
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1208
MallocLike
@ MallocLike
Definition: MemoryBuiltins.cpp:54
llvm::DenseMapBase::find
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:150
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::extractMallocCall
const CallInst * extractMallocCall(const Value *I, function_ref< const TargetLibraryInfo &(Function &)> GetTLI)
extractMallocCall - Returns the corresponding CallInst if the instruction is a malloc call.
Definition: MemoryBuiltins.cpp:341
llvm::ObjectSizeOpts::Mode::Min
@ Min
Evaluate all branches of an unknown condition.
llvm::isAlignedAllocLikeFn
bool isAlignedAllocLikeFn(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 with a...
Definition: MemoryBuiltins.cpp:277
llvm::SelectInst
This class represents the LLVM 'select' instruction.
Definition: Instructions.h:1740
llvm::TargetLibraryInfo::has
bool has(LibFunc F) const
Tests whether a library function is available.
Definition: TargetLibraryInfo.h:323
llvm::Type::isVoidTy
bool isVoidTy() const
Return true if this is 'void'.
Definition: Type.h:138
llvm::GlobalObject::getAlignment
unsigned getAlignment() const
FIXME: Remove this function once transition to Align is over.
Definition: GlobalObject.h:71
llvm::IRBuilderCallbackInserter
Provides an 'InsertHelper' that calls a user-provided callback after performing the default insertion...
Definition: IRBuilder.h:77
llvm::GEPOperator
Definition: Operator.h:458
Builder
assume Assume Builder
Definition: AssumeBundleBuilder.cpp:651
llvm::APInt
Class for arbitrary precision integers.
Definition: APInt.h:70
llvm::IRBuilderBase::CreatePHI
PHINode * CreatePHI(Type *Ty, unsigned NumReservedValues, const Twine &Name="")
Definition: IRBuilder.h:2383
llvm::SmallPtrSetImplBase::clear
void clear()
Definition: SmallPtrSet.h:94
llvm::ObjectSizeOffsetEvaluator::visitAllocaInst
SizeOffsetEvalType visitAllocaInst(AllocaInst &I)
Definition: MemoryBuiltins.cpp:953
None.h
DataLayout.h
llvm::StructType
Class to represent struct types.
Definition: DerivedTypes.h:212
llvm::Value::getType
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:256
llvm::Instruction::getFunction
const Function * getFunction() const
Return the function this instruction belongs to.
Definition: Instruction.cpp:70
llvm::Value::replaceAllUsesWith
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:520
llvm::ObjectSizeOffsetVisitor::visitGEPOperator
SizeOffsetType visitGEPOperator(GEPOperator &GEP)
Definition: MemoryBuiltins.cpp:784
llvm::Value::getContext
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:979
llvm::IRBuilderBase::InsertPointGuard
Definition: IRBuilder.h:367
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
Local.h
llvm::ObjectSizeOpts
Various options to control the behavior of getObjectSize.
Definition: MemoryBuiltins.h:188
llvm::GlobalAlias::getAliasee
const Constant * getAliasee() const
Definition: GlobalAlias.h:73
AllocationFnData
static const std::pair< LibFunc, AllocFnsTy > AllocationFnData[]
Definition: MemoryBuiltins.cpp:73
llvm::Type::getContext
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition: Type.h:127
llvm::LoadInst
An instruction for reading from memory.
Definition: Instructions.h:175
llvm::isOpNewLikeFn
bool isOpNewLikeFn(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 and throws if an all...
Definition: MemoryBuiltins.cpp:326
llvm::ObjectSizeOpts::Mode::Max
@ Max
Same as Min, except we pick the maximum size of all of the branches.
llvm::ObjectSizeOffsetVisitor::visitLoadInst
SizeOffsetType visitLoadInst(LoadInst &I)
Definition: MemoryBuiltins.cpp:812
llvm::Value::stripPointerCasts
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition: Value.cpp:675
Argument.h
llvm::find_if
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1574
llvm::ObjectSizeOffsetVisitor::visitInstruction
SizeOffsetType visitInstruction(Instruction &I)
Definition: MemoryBuiltins.cpp:854
Callee
amdgpu Simplify well known AMD library false FunctionCallee Callee
Definition: AMDGPULibCalls.cpp:206
llvm::APInt::getNullValue
static APInt getNullValue(unsigned numBits)
Get the '0' value.
Definition: APInt.h:574
llvm::GlobalValue::isInterposable
bool isInterposable() const
Return true if this global's definition can be substituted with an arbitrary definition at link time ...
Definition: Globals.cpp:98
llvm::GetStringLength
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 'len+1'.
Definition: ValueTracking.cpp:4289
Attributes.h
llvm::DataLayout::getIntPtrType
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:834
llvm::ConstantExpr
A constant value that is initialized with an expression using other constant values.
Definition: Constants.h:936
llvm::TargetFolder
TargetFolder - Create constants with target dependent folding.
Definition: TargetFolder.h:32
llvm::ObjectSizeOffsetVisitor::visitCallBase
SizeOffsetType visitCallBase(CallBase &CB)
Definition: MemoryBuiltins.cpp:701
llvm::ObjectSizeOffsetVisitor::visitArgument
SizeOffsetType visitArgument(Argument &A)
Definition: MemoryBuiltins.cpp:689
llvm::isNoAliasFn
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...
Definition: MemoryBuiltins.cpp:254
llvm::DenseMapBase::end
iterator end()
Definition: DenseMap.h:83
GlobalVariable.h
llvm::ObjectSizeOpts::EvalMode
Mode EvalMode
How we want to evaluate this object's size.
Definition: MemoryBuiltins.h:201
llvm::ExtractValueInst
This instruction extracts a struct member or array element value from an aggregate value.
Definition: Instructions.h:2374
getAllocationData
static Optional< AllocFnsTy > getAllocationData(const Value *V, AllocType AllocTy, const TargetLibraryInfo *TLI, bool LookThroughBitCast=false)
Definition: MemoryBuiltins.cpp:178
llvm::SizeOffsetEvalType
std::pair< Value *, Value * > SizeOffsetEvalType
Definition: MemoryBuiltins.h:290
Casting.h
Function.h
llvm::TargetLibraryInfo
Provides information about what library functions are available for the current target.
Definition: TargetLibraryInfo.h:219
llvm::IntToPtrInst
This class represents a cast from an integer to a pointer.
Definition: Instructions.h:5100
GlobalAlias.h
AllocType
AllocType
Definition: MemoryBuiltins.cpp:52
llvm::IntrinsicInst
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:45
llvm::ObjectSizeOffsetVisitor::compute
SizeOffsetType compute(Value *V)
Definition: MemoryBuiltins.cpp:611
llvm::ObjectSizeOffsetVisitor::visitConstantPointerNull
SizeOffsetType visitConstantPointerNull(ConstantPointerNull &)
Definition: MemoryBuiltins.cpp:760
Instructions.h
getSizeWithOverflow
static APInt getSizeWithOverflow(const SizeOffsetType &Data)
Definition: MemoryBuiltins.cpp:505
llvm::DenseMapBase< DenseMap< const Value *, WeakEvalType, DenseMapInfo< const Value * >, llvm::detail::DenseMapPair< const Value *, WeakEvalType > >, const Value *, WeakEvalType, DenseMapInfo< const Value * >, llvm::detail::DenseMapPair< const Value *, WeakEvalType > >::iterator
DenseMapIterator< const Value *, WeakEvalType, DenseMapInfo< const Value * >, llvm::detail::DenseMapPair< const Value *, WeakEvalType > > iterator
Definition: DenseMap.h:70
llvm::ObjectSizeOffsetEvaluator::visitExtractValueInst
SizeOffsetEvalType visitExtractValueInst(ExtractValueInst &I)
Definition: MemoryBuiltins.cpp:1009
StrDupLike
@ StrDupLike
Definition: MemoryBuiltins.cpp:58
llvm::CallBase::getArgOperand
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1338
llvm::ObjectSizeOffsetEvaluator::visitInstruction
SizeOffsetEvalType visitInstruction(Instruction &I)
Definition: MemoryBuiltins.cpp:1091
llvm::PHINode::getIncomingBlock
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Definition: Instructions.h:2743
llvm::PHINode
Definition: Instructions.h:2627
getAllocationDataForFunction
static Optional< AllocFnsTy > getAllocationDataForFunction(const Function *Callee, AllocType AllocTy, const TargetLibraryInfo *TLI)
Returns the allocation data for the given value if it's either a call to a known allocation function,...
Definition: MemoryBuiltins.cpp:142
llvm::IRBuilderBase::CreateZExtOrTrunc
Value * CreateZExtOrTrunc(Value *V, Type *DestTy, const Twine &Name="")
Create a ZExt or Trunc from the integer value V to DestTy.
Definition: IRBuilder.h:2031
llvm::ObjectSizeOffsetEvaluator::bothKnown
bool bothKnown(SizeOffsetEvalType SizeOffset)
Definition: MemoryBuiltins.h:336
llvm::CallBase
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1161
llvm::APInt::sgt
bool sgt(const APInt &RHS) const
Signed greater than comparison.
Definition: APInt.h:1294
DerivedTypes.h
llvm::GlobalValue::getValueType
Type * getValueType() const
Definition: GlobalValue.h:273
llvm::CallInst
This class represents a function call, abstracting a target machine's calling convention.
Definition: Instructions.h:1475
AllocLike
@ AllocLike
Definition: MemoryBuiltins.cpp:60
llvm::ObjectSizeOffsetVisitor::ObjectSizeOffsetVisitor
ObjectSizeOffsetVisitor(const DataLayout &DL, const TargetLibraryInfo *TLI, LLVMContext &Context, ObjectSizeOpts Options={})
Definition: MemoryBuiltins.cpp:602
GEP
Hexagon Common GEP
Definition: HexagonCommonGEP.cpp:172
llvm::ConstantPointerNull::getType
PointerType * getType() const
Specialize the getType() method to always return an PointerType, which reduces the amount of casting ...
Definition: Constants.h:550
llvm::AMDGPU::HSAMD::Kernel::Key::Args
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
Definition: AMDGPUMetadata.h:389
llvm::AllocaInst
an instruction to allocate memory on the stack
Definition: Instructions.h:62
llvm::isLibFreeFunction
bool isLibFreeFunction(const Function *F, const LibFunc TLIFn)
isLibFreeFunction - Returns true if the function is a builtin free()
Definition: MemoryBuiltins.cpp:434
raw_ostream.h
llvm::ObjectSizeOffsetVisitor::visitExtractElementInst
SizeOffsetType visitExtractElementInst(ExtractElementInst &I)
Definition: MemoryBuiltins.cpp:774
AllocFnsTy::FstParam
int FstParam
Definition: MemoryBuiltins.cpp:68
Value.h
llvm::FunctionType::getReturnType
Type * getReturnType() const
Definition: DerivedTypes.h:123
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
llvm::ObjectSizeOffsetEvaluator::anyKnown
bool anyKnown(SizeOffsetEvalType SizeOffset)
Definition: MemoryBuiltins.h:332
Debug.h
llvm::Value::users
iterator_range< user_iterator > users()
Definition: Value.h:422
AllocFnsTy::AllocTy
AllocType AllocTy
Definition: MemoryBuiltins.cpp:65
llvm::isFreeCall
const CallInst * isFreeCall(const Value *I, const TargetLibraryInfo *TLI)
isFreeCall - Returns non-null if the value is a call to the builtin free()
Definition: MemoryBuiltins.cpp:487
llvm::ObjectSizeOffsetVisitor::visitPHINode
SizeOffsetType visitPHINode(PHINode &)
Definition: MemoryBuiltins.cpp:817
llvm::Attribute::getAllocSizeArgs
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:351
llvm::FunctionType
Class to represent function types.
Definition: DerivedTypes.h:102
llvm::SmallPtrSetImpl::insert
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:364
llvm::DataLayout::getTypeAllocSize
TypeSize getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:498