Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 1 | //===- MemoryBuiltins.cpp - Identify calls to memory builtins -------------===// |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Victor Hernandez | f390e04 | 2009-10-27 20:05:49 +0000 | [diff] [blame] | 9 | // This family of functions identifies calls to builtin functions that allocate |
Michael Ilseman | d974524 | 2013-03-08 21:03:09 +0000 | [diff] [blame] | 10 | // or free memory. |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Victor Hernandez | f390e04 | 2009-10-27 20:05:49 +0000 | [diff] [blame] | 14 | #include "llvm/Analysis/MemoryBuiltins.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/APInt.h" |
| 16 | #include "llvm/ADT/None.h" |
| 17 | #include "llvm/ADT/Optional.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/STLExtras.h" |
| 19 | #include "llvm/ADT/Statistic.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringRef.h" |
| 21 | #include "llvm/Analysis/TargetFolder.h" |
Chandler Carruth | 62d4215 | 2015-01-15 02:16:27 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/TargetLibraryInfo.h" |
David Blaikie | 2be3922 | 2018-03-21 22:34:23 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/Utils/Local.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/ValueTracking.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Argument.h" |
| 26 | #include "llvm/IR/Attributes.h" |
| 27 | #include "llvm/IR/Constants.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 28 | #include "llvm/IR/DataLayout.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 29 | #include "llvm/IR/DerivedTypes.h" |
| 30 | #include "llvm/IR/Function.h" |
| 31 | #include "llvm/IR/GlobalAlias.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 32 | #include "llvm/IR/GlobalVariable.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 33 | #include "llvm/IR/Instruction.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 34 | #include "llvm/IR/Instructions.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 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" |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 40 | #include "llvm/Support/Debug.h" |
| 41 | #include "llvm/Support/MathExtras.h" |
| 42 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | bb1b2d0 | 2017-08-16 22:07:40 +0000 | [diff] [blame] | 43 | #include <cassert> |
| 44 | #include <cstdint> |
| 45 | #include <iterator> |
| 46 | #include <utility> |
| 47 | |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 48 | using namespace llvm; |
| 49 | |
Chandler Carruth | f1221bd | 2014-04-22 02:48:03 +0000 | [diff] [blame] | 50 | #define DEBUG_TYPE "memory-builtins" |
| 51 | |
George Burgess IV | 2ae15e0 | 2015-11-17 19:48:06 +0000 | [diff] [blame] | 52 | enum AllocType : uint8_t { |
Benjamin Kramer | 2939dd3 | 2013-09-24 17:34:29 +0000 | [diff] [blame] | 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, |
Craig Topper | 09bb760 | 2017-04-18 21:43:46 +0000 | [diff] [blame] | 58 | MallocOrCallocLike = MallocLike | CallocLike, |
Benjamin Kramer | 2939dd3 | 2013-09-24 17:34:29 +0000 | [diff] [blame] | 59 | AllocLike = MallocLike | CallocLike | StrDupLike, |
Benjamin Kramer | 4d4df04 | 2013-09-24 17:15:14 +0000 | [diff] [blame] | 60 | AnyAlloc = AllocLike | ReallocLike |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 61 | }; |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 62 | |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 63 | struct AllocFnsTy { |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 64 | AllocType AllocTy; |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 65 | unsigned NumParams; |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 66 | // First and Second size parameters (or -1 if unused) |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 67 | int FstParam, SndParam; |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 68 | }; |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 69 | |
Nuno Lopes | 181d67e | 2012-06-28 16:34:03 +0000 | [diff] [blame] | 70 | // FIXME: certain users need more information. E.g., SimplifyLibCalls needs to |
| 71 | // know which functions are nounwind, noalias, nocapture parameters, etc. |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 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) |
Eric Fiselier | 96bbec7 | 2018-04-04 19:01:51 +0000 | [diff] [blame] | 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}}, |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 80 | {LibFunc_Znwm, {OpNewLike, 1, 0, -1}}, // new(unsigned long) |
| 81 | {LibFunc_ZnwmRKSt9nothrow_t, {MallocLike, 2, 0, -1}}, // new(unsigned long, nothrow) |
Eric Fiselier | 96bbec7 | 2018-04-04 19:01:51 +0000 | [diff] [blame] | 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}}, |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 85 | {LibFunc_Znaj, {OpNewLike, 1, 0, -1}}, // new[](unsigned int) |
| 86 | {LibFunc_ZnajRKSt9nothrow_t, {MallocLike, 2, 0, -1}}, // new[](unsigned int, nothrow) |
Eric Fiselier | 96bbec7 | 2018-04-04 19:01:51 +0000 | [diff] [blame] | 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}}, |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 90 | {LibFunc_Znam, {OpNewLike, 1, 0, -1}}, // new[](unsigned long) |
| 91 | {LibFunc_ZnamRKSt9nothrow_t, {MallocLike, 2, 0, -1}}, // new[](unsigned long, nothrow) |
Eric Fiselier | 96bbec7 | 2018-04-04 19:01:51 +0000 | [diff] [blame] | 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}}, |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 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}} |
Benjamin Kramer | 01df817 | 2013-09-24 17:49:08 +0000 | [diff] [blame] | 108 | // TODO: Handle "int posix_memalign(void **, size_t, size_t)" |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
Craig Topper | eae6db0 | 2017-04-18 20:17:23 +0000 | [diff] [blame] | 111 | static const Function *getCalledFunction(const Value *V, bool LookThroughBitCast, |
| 112 | bool &IsNoBuiltin) { |
George Burgess IV | ce04489 | 2016-12-27 06:10:50 +0000 | [diff] [blame] | 113 | // Don't care about intrinsics in this case. |
| 114 | if (isa<IntrinsicInst>(V)) |
| 115 | return nullptr; |
| 116 | |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 117 | if (LookThroughBitCast) |
| 118 | V = V->stripPointerCasts(); |
Nuno Lopes | dc6085e | 2012-06-21 21:25:05 +0000 | [diff] [blame] | 119 | |
Craig Topper | eae6db0 | 2017-04-18 20:17:23 +0000 | [diff] [blame] | 120 | ImmutableCallSite CS(V); |
Nuno Lopes | 15dbcb4 | 2012-06-22 15:50:53 +0000 | [diff] [blame] | 121 | if (!CS.getInstruction()) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 122 | return nullptr; |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 123 | |
George Burgess IV | ed16024 | 2016-12-27 06:32:14 +0000 | [diff] [blame] | 124 | IsNoBuiltin = CS.isNoBuiltin(); |
Richard Smith | e04f0d3 | 2013-05-16 04:12:04 +0000 | [diff] [blame] | 125 | |
Benjamin Kramer | fd06306 | 2018-02-20 22:00:33 +0000 | [diff] [blame] | 126 | if (const Function *Callee = CS.getCalledFunction()) |
| 127 | return Callee; |
| 128 | return nullptr; |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 129 | } |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 130 | |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 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. |
George Burgess IV | ce04489 | 2016-12-27 06:10:50 +0000 | [diff] [blame] | 134 | static Optional<AllocFnsTy> |
| 135 | getAllocationDataForFunction(const Function *Callee, AllocType AllocTy, |
| 136 | const TargetLibraryInfo *TLI) { |
Benjamin Kramer | 8bcc971 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 137 | // Make sure that the function is available. |
| 138 | StringRef FnName = Callee->getName(); |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 139 | LibFunc TLIFn; |
Benjamin Kramer | 8bcc971 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 140 | if (!TLI || !TLI->getLibFunc(FnName, TLIFn) || !TLI->has(TLIFn)) |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 141 | return None; |
Benjamin Kramer | 8bcc971 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 142 | |
George Burgess IV | 45a540f | 2016-12-20 18:46:27 +0000 | [diff] [blame] | 143 | const auto *Iter = find_if( |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 144 | AllocationFnData, [TLIFn](const std::pair<LibFunc, AllocFnsTy> &P) { |
George Burgess IV | 45a540f | 2016-12-20 18:46:27 +0000 | [diff] [blame] | 145 | return P.first == TLIFn; |
| 146 | }); |
Benjamin Kramer | 74b6d3b | 2015-10-24 19:03:15 +0000 | [diff] [blame] | 147 | |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 148 | if (Iter == std::end(AllocationFnData)) |
| 149 | return None; |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 150 | |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 151 | const AllocFnsTy *FnData = &Iter->second; |
Andrew Kaylor | d9b6b81 | 2018-08-30 18:37:18 +0000 | [diff] [blame] | 152 | if ((FnData->AllocTy & AllocTy) != FnData->AllocTy) |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 153 | return None; |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 154 | |
| 155 | // Check function prototype. |
Nuno Lopes | f06b731 | 2012-06-21 18:38:26 +0000 | [diff] [blame] | 156 | int FstParam = FnData->FstParam; |
| 157 | int SndParam = FnData->SndParam; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 158 | FunctionType *FTy = Callee->getFunctionType(); |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 159 | |
| 160 | if (FTy->getReturnType() == Type::getInt8PtrTy(FTy->getContext()) && |
| 161 | FTy->getNumParams() == FnData->NumParams && |
Nuno Lopes | f06b731 | 2012-06-21 18:38:26 +0000 | [diff] [blame] | 162 | (FstParam < 0 || |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 163 | (FTy->getParamType(FstParam)->isIntegerTy(32) || |
| 164 | FTy->getParamType(FstParam)->isIntegerTy(64))) && |
Nuno Lopes | f06b731 | 2012-06-21 18:38:26 +0000 | [diff] [blame] | 165 | (SndParam < 0 || |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 166 | FTy->getParamType(SndParam)->isIntegerTy(32) || |
| 167 | FTy->getParamType(SndParam)->isIntegerTy(64))) |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 168 | return *FnData; |
| 169 | return None; |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 170 | } |
| 171 | |
George Burgess IV | ce04489 | 2016-12-27 06:10:50 +0000 | [diff] [blame] | 172 | static Optional<AllocFnsTy> getAllocationData(const Value *V, AllocType AllocTy, |
| 173 | const TargetLibraryInfo *TLI, |
| 174 | bool LookThroughBitCast = false) { |
George Burgess IV | ed16024 | 2016-12-27 06:32:14 +0000 | [diff] [blame] | 175 | bool IsNoBuiltinCall; |
| 176 | if (const Function *Callee = |
| 177 | getCalledFunction(V, LookThroughBitCast, IsNoBuiltinCall)) |
| 178 | if (!IsNoBuiltinCall) |
| 179 | return getAllocationDataForFunction(Callee, AllocTy, TLI); |
George Burgess IV | ce04489 | 2016-12-27 06:10:50 +0000 | [diff] [blame] | 180 | return None; |
| 181 | } |
| 182 | |
Teresa Johnson | 9c27b59 | 2019-09-07 03:09:36 +0000 | [diff] [blame] | 183 | static Optional<AllocFnsTy> |
| 184 | getAllocationData(const Value *V, AllocType AllocTy, |
| 185 | function_ref<const TargetLibraryInfo &(Function &)> GetTLI, |
| 186 | bool LookThroughBitCast = false) { |
| 187 | bool IsNoBuiltinCall; |
| 188 | if (const Function *Callee = |
| 189 | getCalledFunction(V, LookThroughBitCast, IsNoBuiltinCall)) |
| 190 | if (!IsNoBuiltinCall) |
| 191 | return getAllocationDataForFunction( |
| 192 | Callee, AllocTy, &GetTLI(const_cast<Function &>(*Callee))); |
| 193 | return None; |
| 194 | } |
| 195 | |
George Burgess IV | ccae43a | 2016-12-23 01:18:09 +0000 | [diff] [blame] | 196 | static Optional<AllocFnsTy> getAllocationSize(const Value *V, |
| 197 | const TargetLibraryInfo *TLI) { |
George Burgess IV | ed16024 | 2016-12-27 06:32:14 +0000 | [diff] [blame] | 198 | bool IsNoBuiltinCall; |
| 199 | const Function *Callee = |
| 200 | getCalledFunction(V, /*LookThroughBitCast=*/false, IsNoBuiltinCall); |
George Burgess IV | ce04489 | 2016-12-27 06:10:50 +0000 | [diff] [blame] | 201 | if (!Callee) |
| 202 | return None; |
| 203 | |
George Burgess IV | ccae43a | 2016-12-23 01:18:09 +0000 | [diff] [blame] | 204 | // Prefer to use existing information over allocsize. This will give us an |
| 205 | // accurate AllocTy. |
George Burgess IV | ed16024 | 2016-12-27 06:32:14 +0000 | [diff] [blame] | 206 | if (!IsNoBuiltinCall) |
| 207 | if (Optional<AllocFnsTy> Data = |
| 208 | getAllocationDataForFunction(Callee, AnyAlloc, TLI)) |
| 209 | return Data; |
George Burgess IV | ccae43a | 2016-12-23 01:18:09 +0000 | [diff] [blame] | 210 | |
George Burgess IV | ce04489 | 2016-12-27 06:10:50 +0000 | [diff] [blame] | 211 | Attribute Attr = Callee->getFnAttribute(Attribute::AllocSize); |
| 212 | if (Attr == Attribute()) |
George Burgess IV | ccae43a | 2016-12-23 01:18:09 +0000 | [diff] [blame] | 213 | return None; |
| 214 | |
George Burgess IV | ccae43a | 2016-12-23 01:18:09 +0000 | [diff] [blame] | 215 | std::pair<unsigned, Optional<unsigned>> Args = Attr.getAllocSizeArgs(); |
| 216 | |
| 217 | AllocFnsTy Result; |
| 218 | // Because allocsize only tells us how many bytes are allocated, we're not |
| 219 | // really allowed to assume anything, so we use MallocLike. |
| 220 | Result.AllocTy = MallocLike; |
| 221 | Result.NumParams = Callee->getNumOperands(); |
| 222 | Result.FstParam = Args.first; |
| 223 | Result.SndParam = Args.second.getValueOr(-1); |
| 224 | return Result; |
| 225 | } |
| 226 | |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 227 | static bool hasNoAliasAttr(const Value *V, bool LookThroughBitCast) { |
Nuno Lopes | 9ecc876 | 2012-06-25 16:17:54 +0000 | [diff] [blame] | 228 | ImmutableCallSite CS(LookThroughBitCast ? V->stripPointerCasts() : V); |
Reid Kleckner | fb502d2 | 2017-04-14 20:19:02 +0000 | [diff] [blame] | 229 | return CS && CS.hasRetAttr(Attribute::NoAlias); |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 232 | /// Tests if a value is a call or invoke to a library function that |
Nuno Lopes | dc6085e | 2012-06-21 21:25:05 +0000 | [diff] [blame] | 233 | /// allocates or reallocates memory (either malloc, calloc, realloc, or strdup |
| 234 | /// like). |
Benjamin Kramer | 8bcc971 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 235 | bool llvm::isAllocationFn(const Value *V, const TargetLibraryInfo *TLI, |
| 236 | bool LookThroughBitCast) { |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 237 | return getAllocationData(V, AnyAlloc, TLI, LookThroughBitCast).hasValue(); |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 238 | } |
Teresa Johnson | 9c27b59 | 2019-09-07 03:09:36 +0000 | [diff] [blame] | 239 | bool llvm::isAllocationFn( |
| 240 | const Value *V, function_ref<const TargetLibraryInfo &(Function &)> GetTLI, |
| 241 | bool LookThroughBitCast) { |
| 242 | return getAllocationData(V, AnyAlloc, GetTLI, LookThroughBitCast).hasValue(); |
| 243 | } |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 244 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 245 | /// Tests if a value is a call or invoke to a function that returns a |
Nuno Lopes | 181d67e | 2012-06-28 16:34:03 +0000 | [diff] [blame] | 246 | /// NoAlias pointer (including malloc/calloc/realloc/strdup-like functions). |
Benjamin Kramer | 8bcc971 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 247 | bool llvm::isNoAliasFn(const Value *V, const TargetLibraryInfo *TLI, |
| 248 | bool LookThroughBitCast) { |
Nuno Lopes | 181d67e | 2012-06-28 16:34:03 +0000 | [diff] [blame] | 249 | // it's safe to consider realloc as noalias since accessing the original |
| 250 | // pointer is undefined behavior |
Benjamin Kramer | 8bcc971 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 251 | return isAllocationFn(V, TLI, LookThroughBitCast) || |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 252 | hasNoAliasAttr(V, LookThroughBitCast); |
| 253 | } |
| 254 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 255 | /// Tests if a value is a call or invoke to a library function that |
Nuno Lopes | dc6085e | 2012-06-21 21:25:05 +0000 | [diff] [blame] | 256 | /// allocates uninitialized memory (such as malloc). |
Benjamin Kramer | 8bcc971 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 257 | bool llvm::isMallocLikeFn(const Value *V, const TargetLibraryInfo *TLI, |
| 258 | bool LookThroughBitCast) { |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 259 | return getAllocationData(V, MallocLike, TLI, LookThroughBitCast).hasValue(); |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 260 | } |
Teresa Johnson | 9c27b59 | 2019-09-07 03:09:36 +0000 | [diff] [blame] | 261 | bool llvm::isMallocLikeFn( |
| 262 | const Value *V, function_ref<const TargetLibraryInfo &(Function &)> GetTLI, |
| 263 | bool LookThroughBitCast) { |
| 264 | return getAllocationData(V, MallocLike, GetTLI, LookThroughBitCast) |
| 265 | .hasValue(); |
| 266 | } |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 267 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 268 | /// Tests if a value is a call or invoke to a library function that |
Nuno Lopes | dc6085e | 2012-06-21 21:25:05 +0000 | [diff] [blame] | 269 | /// allocates zero-filled memory (such as calloc). |
Benjamin Kramer | 8bcc971 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 270 | bool llvm::isCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI, |
| 271 | bool LookThroughBitCast) { |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 272 | return getAllocationData(V, CallocLike, TLI, LookThroughBitCast).hasValue(); |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 275 | /// Tests if a value is a call or invoke to a library function that |
Vedant Kumar | 1a8456d | 2018-03-02 18:57:02 +0000 | [diff] [blame] | 276 | /// allocates memory similar to malloc or calloc. |
Craig Topper | 09bb760 | 2017-04-18 21:43:46 +0000 | [diff] [blame] | 277 | bool llvm::isMallocOrCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI, |
| 278 | bool LookThroughBitCast) { |
| 279 | return getAllocationData(V, MallocOrCallocLike, TLI, |
| 280 | LookThroughBitCast).hasValue(); |
| 281 | } |
| 282 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 283 | /// Tests if a value is a call or invoke to a library function that |
Nuno Lopes | dc6085e | 2012-06-21 21:25:05 +0000 | [diff] [blame] | 284 | /// allocates memory (either malloc, calloc, or strdup like). |
Benjamin Kramer | 8bcc971 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 285 | bool llvm::isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI, |
| 286 | bool LookThroughBitCast) { |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 287 | return getAllocationData(V, AllocLike, TLI, LookThroughBitCast).hasValue(); |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Brian Homerding | b4b21d8 | 2019-07-08 15:57:56 +0000 | [diff] [blame] | 290 | /// Tests if a value is a call or invoke to a library function that |
| 291 | /// reallocates memory (e.g., realloc). |
| 292 | bool llvm::isReallocLikeFn(const Value *V, const TargetLibraryInfo *TLI, |
| 293 | bool LookThroughBitCast) { |
| 294 | return getAllocationData(V, ReallocLike, TLI, LookThroughBitCast).hasValue(); |
| 295 | } |
| 296 | |
| 297 | /// Tests if a functions is a call or invoke to a library function that |
| 298 | /// reallocates memory (e.g., realloc). |
| 299 | bool llvm::isReallocLikeFn(const Function *F, const TargetLibraryInfo *TLI) { |
| 300 | return getAllocationDataForFunction(F, ReallocLike, TLI).hasValue(); |
| 301 | } |
| 302 | |
David Bolvansky | 05bda8b | 2019-08-28 08:28:20 +0000 | [diff] [blame] | 303 | /// Tests if a value is a call or invoke to a library function that |
| 304 | /// allocates memory and throws if an allocation failed (e.g., new). |
| 305 | bool llvm::isOpNewLikeFn(const Value *V, const TargetLibraryInfo *TLI, |
| 306 | bool LookThroughBitCast) { |
| 307 | return getAllocationData(V, OpNewLike, TLI, LookThroughBitCast).hasValue(); |
| 308 | } |
| 309 | |
David Bolvansky | be2487a | 2019-09-17 10:12:48 +0000 | [diff] [blame] | 310 | /// Tests if a value is a call or invoke to a library function that |
| 311 | /// allocates memory (strdup, strndup). |
| 312 | bool llvm::isStrdupLikeFn(const Value *V, const TargetLibraryInfo *TLI, |
| 313 | bool LookThroughBitCast) { |
| 314 | return getAllocationData(V, StrDupLike, TLI, LookThroughBitCast).hasValue(); |
| 315 | } |
| 316 | |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 317 | /// extractMallocCall - Returns the corresponding CallInst if the instruction |
| 318 | /// is a malloc call. Since CallInst::CreateMalloc() only creates calls, we |
| 319 | /// ignore InvokeInst here. |
Teresa Johnson | 9c27b59 | 2019-09-07 03:09:36 +0000 | [diff] [blame] | 320 | const CallInst *llvm::extractMallocCall( |
| 321 | const Value *I, |
| 322 | function_ref<const TargetLibraryInfo &(Function &)> GetTLI) { |
| 323 | return isMallocLikeFn(I, GetTLI) ? dyn_cast<CallInst>(I) : nullptr; |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 326 | static Value *computeArraySize(const CallInst *CI, const DataLayout &DL, |
Benjamin Kramer | 8bcc971 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 327 | const TargetLibraryInfo *TLI, |
Victor Hernandez | fcc77b1 | 2009-11-10 08:32:25 +0000 | [diff] [blame] | 328 | bool LookThroughSExt = false) { |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 329 | if (!CI) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 330 | return nullptr; |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 331 | |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 332 | // The size of the malloc's result type must be known to determine array size. |
Benjamin Kramer | 8bcc971 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 333 | Type *T = getMallocAllocatedType(CI, TLI); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 334 | if (!T || !T->isSized()) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 335 | return nullptr; |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 336 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 337 | unsigned ElementSize = DL.getTypeAllocSize(T); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 338 | if (StructType *ST = dyn_cast<StructType>(T)) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 339 | ElementSize = DL.getStructLayout(ST)->getSizeInBytes(); |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 340 | |
Gabor Greif | ad7884a | 2010-06-23 21:41:47 +0000 | [diff] [blame] | 341 | // If malloc call's arg can be determined to be a multiple of ElementSize, |
Victor Hernandez | fcc77b1 | 2009-11-10 08:32:25 +0000 | [diff] [blame] | 342 | // return the multiple. Otherwise, return NULL. |
Gabor Greif | ad7884a | 2010-06-23 21:41:47 +0000 | [diff] [blame] | 343 | Value *MallocArg = CI->getArgOperand(0); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 344 | Value *Multiple = nullptr; |
Sanjay Patel | 490193d | 2016-07-07 16:19:09 +0000 | [diff] [blame] | 345 | if (ComputeMultiple(MallocArg, ElementSize, Multiple, LookThroughSExt)) |
Victor Hernandez | fcc77b1 | 2009-11-10 08:32:25 +0000 | [diff] [blame] | 346 | return Multiple; |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 347 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 348 | return nullptr; |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 351 | /// getMallocType - Returns the PointerType resulting from the malloc call. |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 352 | /// The PointerType depends on the number of bitcast uses of the malloc call: |
| 353 | /// 0: PointerType is the calls' return type. |
| 354 | /// 1: PointerType is the bitcast's result type. |
| 355 | /// >1: Unique PointerType cannot be determined, return NULL. |
Benjamin Kramer | 8bcc971 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 356 | PointerType *llvm::getMallocType(const CallInst *CI, |
| 357 | const TargetLibraryInfo *TLI) { |
| 358 | assert(isMallocLikeFn(CI, TLI) && "getMallocType and not malloc call"); |
Michael Ilseman | d974524 | 2013-03-08 21:03:09 +0000 | [diff] [blame] | 359 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 360 | PointerType *MallocType = nullptr; |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 361 | unsigned NumOfBitCastUses = 0; |
| 362 | |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 363 | // Determine if CallInst has a bitcast use. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 364 | for (Value::const_user_iterator UI = CI->user_begin(), E = CI->user_end(); |
| 365 | UI != E;) |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 366 | if (const BitCastInst *BCI = dyn_cast<BitCastInst>(*UI++)) { |
| 367 | MallocType = cast<PointerType>(BCI->getDestTy()); |
| 368 | NumOfBitCastUses++; |
| 369 | } |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 370 | |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 371 | // Malloc call has 1 bitcast use, so type is the bitcast's destination type. |
| 372 | if (NumOfBitCastUses == 1) |
| 373 | return MallocType; |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 374 | |
Victor Hernandez | ddc2ce4 | 2009-09-22 18:50:03 +0000 | [diff] [blame] | 375 | // Malloc call was not bitcast, so type is the malloc function's return type. |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 376 | if (NumOfBitCastUses == 0) |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 377 | return cast<PointerType>(CI->getType()); |
| 378 | |
| 379 | // Type could not be determined. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 380 | return nullptr; |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 383 | /// getMallocAllocatedType - Returns the Type allocated by malloc call. |
| 384 | /// The Type depends on the number of bitcast uses of the malloc call: |
| 385 | /// 0: PointerType is the malloc calls' return type. |
| 386 | /// 1: PointerType is the bitcast's result type. |
| 387 | /// >1: Unique PointerType cannot be determined, return NULL. |
Benjamin Kramer | 8bcc971 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 388 | Type *llvm::getMallocAllocatedType(const CallInst *CI, |
| 389 | const TargetLibraryInfo *TLI) { |
| 390 | PointerType *PT = getMallocType(CI, TLI); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 391 | return PT ? PT->getElementType() : nullptr; |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Michael Ilseman | d974524 | 2013-03-08 21:03:09 +0000 | [diff] [blame] | 394 | /// getMallocArraySize - Returns the array size of a malloc call. If the |
Victor Hernandez | 0d02542 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 395 | /// argument passed to malloc is a multiple of the size of the malloced type, |
| 396 | /// then return that multiple. For non-array mallocs, the multiple is |
| 397 | /// constant 1. Otherwise, return NULL for mallocs whose array size cannot be |
Victor Hernandez | 13020b1 | 2009-10-15 20:14:52 +0000 | [diff] [blame] | 398 | /// determined. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 399 | Value *llvm::getMallocArraySize(CallInst *CI, const DataLayout &DL, |
Benjamin Kramer | 8bcc971 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 400 | const TargetLibraryInfo *TLI, |
Victor Hernandez | fcc77b1 | 2009-11-10 08:32:25 +0000 | [diff] [blame] | 401 | bool LookThroughSExt) { |
Benjamin Kramer | 8bcc971 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 402 | assert(isMallocLikeFn(CI, TLI) && "getMallocArraySize and not malloc call"); |
Matt Arsenault | 40dddd7 | 2013-10-03 19:50:01 +0000 | [diff] [blame] | 403 | return computeArraySize(CI, DL, TLI, LookThroughSExt); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 404 | } |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 405 | |
Nuno Lopes | d2b71e7 | 2012-05-03 21:19:58 +0000 | [diff] [blame] | 406 | /// extractCallocCall - Returns the corresponding CallInst if the instruction |
| 407 | /// is a calloc call. |
Benjamin Kramer | 8bcc971 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 408 | const CallInst *llvm::extractCallocCall(const Value *I, |
| 409 | const TargetLibraryInfo *TLI) { |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 410 | return isCallocLikeFn(I, TLI) ? cast<CallInst>(I) : nullptr; |
Nuno Lopes | d2b71e7 | 2012-05-03 21:19:58 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Brian Homerding | b4b21d8 | 2019-07-08 15:57:56 +0000 | [diff] [blame] | 413 | /// isLibFreeFunction - Returns true if the function is a builtin free() |
| 414 | bool llvm::isLibFreeFunction(const Function *F, const LibFunc TLIFn) { |
Richard Smith | 70523c7 | 2013-07-21 23:11:42 +0000 | [diff] [blame] | 415 | unsigned ExpectedNumParams; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 416 | if (TLIFn == LibFunc_free || |
| 417 | TLIFn == LibFunc_ZdlPv || // operator delete(void*) |
| 418 | TLIFn == LibFunc_ZdaPv || // operator delete[](void*) |
| 419 | TLIFn == LibFunc_msvc_delete_ptr32 || // operator delete(void*) |
| 420 | TLIFn == LibFunc_msvc_delete_ptr64 || // operator delete(void*) |
| 421 | TLIFn == LibFunc_msvc_delete_array_ptr32 || // operator delete[](void*) |
| 422 | TLIFn == LibFunc_msvc_delete_array_ptr64) // operator delete[](void*) |
Richard Smith | 70523c7 | 2013-07-21 23:11:42 +0000 | [diff] [blame] | 423 | ExpectedNumParams = 1; |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 424 | else if (TLIFn == LibFunc_ZdlPvj || // delete(void*, uint) |
| 425 | TLIFn == LibFunc_ZdlPvm || // delete(void*, ulong) |
| 426 | TLIFn == LibFunc_ZdlPvRKSt9nothrow_t || // delete(void*, nothrow) |
Eric Fiselier | 96bbec7 | 2018-04-04 19:01:51 +0000 | [diff] [blame] | 427 | TLIFn == LibFunc_ZdlPvSt11align_val_t || // delete(void*, align_val_t) |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 428 | TLIFn == LibFunc_ZdaPvj || // delete[](void*, uint) |
| 429 | TLIFn == LibFunc_ZdaPvm || // delete[](void*, ulong) |
| 430 | TLIFn == LibFunc_ZdaPvRKSt9nothrow_t || // delete[](void*, nothrow) |
Eric Fiselier | 96bbec7 | 2018-04-04 19:01:51 +0000 | [diff] [blame] | 431 | TLIFn == LibFunc_ZdaPvSt11align_val_t || // delete[](void*, align_val_t) |
David L. Jones | d21529f | 2017-01-23 23:16:46 +0000 | [diff] [blame] | 432 | TLIFn == LibFunc_msvc_delete_ptr32_int || // delete(void*, uint) |
| 433 | TLIFn == LibFunc_msvc_delete_ptr64_longlong || // delete(void*, ulonglong) |
| 434 | TLIFn == LibFunc_msvc_delete_ptr32_nothrow || // delete(void*, nothrow) |
| 435 | TLIFn == LibFunc_msvc_delete_ptr64_nothrow || // delete(void*, nothrow) |
| 436 | TLIFn == LibFunc_msvc_delete_array_ptr32_int || // delete[](void*, uint) |
| 437 | TLIFn == LibFunc_msvc_delete_array_ptr64_longlong || // delete[](void*, ulonglong) |
| 438 | TLIFn == LibFunc_msvc_delete_array_ptr32_nothrow || // delete[](void*, nothrow) |
| 439 | TLIFn == LibFunc_msvc_delete_array_ptr64_nothrow) // delete[](void*, nothrow) |
Richard Smith | 70523c7 | 2013-07-21 23:11:42 +0000 | [diff] [blame] | 440 | ExpectedNumParams = 2; |
Eric Fiselier | 96bbec7 | 2018-04-04 19:01:51 +0000 | [diff] [blame] | 441 | else if (TLIFn == LibFunc_ZdaPvSt11align_val_tRKSt9nothrow_t || // delete(void*, align_val_t, nothrow) |
| 442 | TLIFn == LibFunc_ZdlPvSt11align_val_tRKSt9nothrow_t) // delete[](void*, align_val_t, nothrow) |
| 443 | ExpectedNumParams = 3; |
Richard Smith | 70523c7 | 2013-07-21 23:11:42 +0000 | [diff] [blame] | 444 | else |
Brian Homerding | b4b21d8 | 2019-07-08 15:57:56 +0000 | [diff] [blame] | 445 | return false; |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 446 | |
| 447 | // Check free prototype. |
Michael Ilseman | d974524 | 2013-03-08 21:03:09 +0000 | [diff] [blame] | 448 | // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 449 | // attribute will exist. |
Brian Homerding | b4b21d8 | 2019-07-08 15:57:56 +0000 | [diff] [blame] | 450 | FunctionType *FTy = F->getFunctionType(); |
Victor Hernandez | 3318858 | 2009-11-03 20:39:35 +0000 | [diff] [blame] | 451 | if (!FTy->getReturnType()->isVoidTy()) |
Brian Homerding | b4b21d8 | 2019-07-08 15:57:56 +0000 | [diff] [blame] | 452 | return false; |
Richard Smith | 70523c7 | 2013-07-21 23:11:42 +0000 | [diff] [blame] | 453 | if (FTy->getNumParams() != ExpectedNumParams) |
Brian Homerding | b4b21d8 | 2019-07-08 15:57:56 +0000 | [diff] [blame] | 454 | return false; |
| 455 | if (FTy->getParamType(0) != Type::getInt8PtrTy(F->getContext())) |
| 456 | return false; |
| 457 | |
| 458 | return true; |
| 459 | } |
| 460 | |
| 461 | /// isFreeCall - Returns non-null if the value is a call to the builtin free() |
| 462 | const CallInst *llvm::isFreeCall(const Value *I, const TargetLibraryInfo *TLI) { |
| 463 | bool IsNoBuiltinCall; |
| 464 | const Function *Callee = |
| 465 | getCalledFunction(I, /*LookThroughBitCast=*/false, IsNoBuiltinCall); |
| 466 | if (Callee == nullptr || IsNoBuiltinCall) |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 467 | return nullptr; |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 468 | |
Brian Homerding | b4b21d8 | 2019-07-08 15:57:56 +0000 | [diff] [blame] | 469 | StringRef FnName = Callee->getName(); |
| 470 | LibFunc TLIFn; |
| 471 | if (!TLI || !TLI->getLibFunc(FnName, TLIFn) || !TLI->has(TLIFn)) |
| 472 | return nullptr; |
| 473 | |
| 474 | return isLibFreeFunction(Callee, TLIFn) ? dyn_cast<CallInst>(I) : nullptr; |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 475 | } |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 476 | |
Brian Homerding | b4b21d8 | 2019-07-08 15:57:56 +0000 | [diff] [blame] | 477 | |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 478 | //===----------------------------------------------------------------------===// |
| 479 | // Utility functions to compute size of objects. |
| 480 | // |
Petar Jovanovic | 644b8c1 | 2016-04-13 12:25:25 +0000 | [diff] [blame] | 481 | static APInt getSizeWithOverflow(const SizeOffsetType &Data) { |
| 482 | if (Data.second.isNegative() || Data.first.ult(Data.second)) |
| 483 | return APInt(Data.first.getBitWidth(), 0); |
| 484 | return Data.first - Data.second; |
| 485 | } |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 486 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 487 | /// Compute the size of the object pointed by Ptr. Returns true and the |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 488 | /// object size in Size if successful, and false otherwise. |
Hiroshi Inoue | ef1c2ba | 2017-07-01 07:12:15 +0000 | [diff] [blame] | 489 | /// If RoundToAlign is true, then Size is rounded up to the alignment of |
| 490 | /// allocas, byval arguments, and global variables. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 491 | bool llvm::getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout &DL, |
George Burgess IV | 56c7e88 | 2017-03-21 20:08:59 +0000 | [diff] [blame] | 492 | const TargetLibraryInfo *TLI, ObjectSizeOpts Opts) { |
| 493 | ObjectSizeOffsetVisitor Visitor(DL, TLI, Ptr->getContext(), Opts); |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 494 | SizeOffsetType Data = Visitor.compute(const_cast<Value*>(Ptr)); |
| 495 | if (!Visitor.bothKnown(Data)) |
| 496 | return false; |
| 497 | |
Petar Jovanovic | 644b8c1 | 2016-04-13 12:25:25 +0000 | [diff] [blame] | 498 | Size = getSizeWithOverflow(Data).getZExtValue(); |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 499 | return true; |
| 500 | } |
| 501 | |
Erik Pilkington | 600e9de | 2019-01-30 20:34:35 +0000 | [diff] [blame] | 502 | Value *llvm::lowerObjectSizeCall(IntrinsicInst *ObjectSize, |
| 503 | const DataLayout &DL, |
| 504 | const TargetLibraryInfo *TLI, |
| 505 | bool MustSucceed) { |
George Burgess IV | 3f08914 | 2016-12-20 23:46:36 +0000 | [diff] [blame] | 506 | assert(ObjectSize->getIntrinsicID() == Intrinsic::objectsize && |
| 507 | "ObjectSize must be a call to llvm.objectsize!"); |
| 508 | |
| 509 | bool MaxVal = cast<ConstantInt>(ObjectSize->getArgOperand(1))->isZero(); |
George Burgess IV | 56c7e88 | 2017-03-21 20:08:59 +0000 | [diff] [blame] | 510 | ObjectSizeOpts EvalOptions; |
George Burgess IV | 3f08914 | 2016-12-20 23:46:36 +0000 | [diff] [blame] | 511 | // Unless we have to fold this to something, try to be as accurate as |
| 512 | // possible. |
| 513 | if (MustSucceed) |
George Burgess IV | 56c7e88 | 2017-03-21 20:08:59 +0000 | [diff] [blame] | 514 | EvalOptions.EvalMode = |
| 515 | MaxVal ? ObjectSizeOpts::Mode::Max : ObjectSizeOpts::Mode::Min; |
George Burgess IV | 3f08914 | 2016-12-20 23:46:36 +0000 | [diff] [blame] | 516 | else |
George Burgess IV | 56c7e88 | 2017-03-21 20:08:59 +0000 | [diff] [blame] | 517 | EvalOptions.EvalMode = ObjectSizeOpts::Mode::Exact; |
| 518 | |
| 519 | EvalOptions.NullIsUnknownSize = |
| 520 | cast<ConstantInt>(ObjectSize->getArgOperand(2))->isOne(); |
George Burgess IV | 3f08914 | 2016-12-20 23:46:36 +0000 | [diff] [blame] | 521 | |
George Burgess IV | 3f08914 | 2016-12-20 23:46:36 +0000 | [diff] [blame] | 522 | auto *ResultType = cast<IntegerType>(ObjectSize->getType()); |
Erik Pilkington | 600e9de | 2019-01-30 20:34:35 +0000 | [diff] [blame] | 523 | bool StaticOnly = cast<ConstantInt>(ObjectSize->getArgOperand(3))->isZero(); |
| 524 | if (StaticOnly) { |
| 525 | // FIXME: Does it make sense to just return a failure value if the size won't |
| 526 | // fit in the output and `!MustSucceed`? |
| 527 | uint64_t Size; |
| 528 | if (getObjectSize(ObjectSize->getArgOperand(0), Size, DL, TLI, EvalOptions) && |
| 529 | isUIntN(ResultType->getBitWidth(), Size)) |
| 530 | return ConstantInt::get(ResultType, Size); |
| 531 | } else { |
| 532 | LLVMContext &Ctx = ObjectSize->getFunction()->getContext(); |
| 533 | ObjectSizeOffsetEvaluator Eval(DL, TLI, Ctx, EvalOptions); |
| 534 | SizeOffsetEvalType SizeOffsetPair = |
| 535 | Eval.compute(ObjectSize->getArgOperand(0)); |
| 536 | |
| 537 | if (SizeOffsetPair != ObjectSizeOffsetEvaluator::unknown()) { |
| 538 | IRBuilder<TargetFolder> Builder(Ctx, TargetFolder(DL)); |
| 539 | Builder.SetInsertPoint(ObjectSize); |
| 540 | |
| 541 | // If we've outside the end of the object, then we can always access |
| 542 | // exactly 0 bytes. |
| 543 | Value *ResultSize = |
| 544 | Builder.CreateSub(SizeOffsetPair.first, SizeOffsetPair.second); |
| 545 | Value *UseZero = |
| 546 | Builder.CreateICmpULT(SizeOffsetPair.first, SizeOffsetPair.second); |
Nicola Zaghen | 9757277 | 2019-12-13 09:55:45 +0000 | [diff] [blame^] | 547 | ResultSize = Builder.CreateZExtOrTrunc(ResultSize, ResultType); |
Erik Pilkington | 600e9de | 2019-01-30 20:34:35 +0000 | [diff] [blame] | 548 | return Builder.CreateSelect(UseZero, ConstantInt::get(ResultType, 0), |
| 549 | ResultSize); |
| 550 | } |
| 551 | } |
George Burgess IV | 3f08914 | 2016-12-20 23:46:36 +0000 | [diff] [blame] | 552 | |
| 553 | if (!MustSucceed) |
| 554 | return nullptr; |
| 555 | |
| 556 | return ConstantInt::get(ResultType, MaxVal ? -1ULL : 0); |
| 557 | } |
| 558 | |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 559 | STATISTIC(ObjectVisitorArgument, |
| 560 | "Number of arguments with unsolved size and offset"); |
| 561 | STATISTIC(ObjectVisitorLoad, |
| 562 | "Number of load instructions with unsolved size and offset"); |
| 563 | |
Guillaume Chatelet | 18f805a | 2019-09-27 12:54:21 +0000 | [diff] [blame] | 564 | APInt ObjectSizeOffsetVisitor::align(APInt Size, uint64_t Alignment) { |
| 565 | if (Options.RoundToAlign && Alignment) |
| 566 | return APInt(IntTyBits, alignTo(Size.getZExtValue(), Align(Alignment))); |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 567 | return Size; |
| 568 | } |
| 569 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 570 | ObjectSizeOffsetVisitor::ObjectSizeOffsetVisitor(const DataLayout &DL, |
Benjamin Kramer | 8bcc971 | 2012-08-29 15:32:21 +0000 | [diff] [blame] | 571 | const TargetLibraryInfo *TLI, |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 572 | LLVMContext &Context, |
George Burgess IV | 56c7e88 | 2017-03-21 20:08:59 +0000 | [diff] [blame] | 573 | ObjectSizeOpts Options) |
| 574 | : DL(DL), TLI(TLI), Options(Options) { |
Matt Arsenault | d3ee7af | 2013-12-14 00:27:48 +0000 | [diff] [blame] | 575 | // Pointer size must be rechecked for each object visited since it could have |
| 576 | // a different address space. |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | SizeOffsetType ObjectSizeOffsetVisitor::compute(Value *V) { |
Nicola Zaghen | 9757277 | 2019-12-13 09:55:45 +0000 | [diff] [blame^] | 580 | IntTyBits = DL.getIndexTypeSizeInBits(V->getType()); |
Matt Arsenault | d3ee7af | 2013-12-14 00:27:48 +0000 | [diff] [blame] | 581 | Zero = APInt::getNullValue(IntTyBits); |
| 582 | |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 583 | V = V->stripPointerCasts(); |
Nadav Rotem | abcc64f | 2013-04-09 18:16:05 +0000 | [diff] [blame] | 584 | if (Instruction *I = dyn_cast<Instruction>(V)) { |
| 585 | // If we have already seen this instruction, bail out. Cycles can happen in |
| 586 | // unreachable code after constant propagation. |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 587 | if (!SeenInsts.insert(I).second) |
Nadav Rotem | abcc64f | 2013-04-09 18:16:05 +0000 | [diff] [blame] | 588 | return unknown(); |
Nuno Lopes | d896a40 | 2012-12-31 20:45:10 +0000 | [diff] [blame] | 589 | |
Benjamin Kramer | 34764fe | 2012-08-17 19:26:41 +0000 | [diff] [blame] | 590 | if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) |
Nadav Rotem | abcc64f | 2013-04-09 18:16:05 +0000 | [diff] [blame] | 591 | return visitGEPOperator(*GEP); |
| 592 | return visit(*I); |
Benjamin Kramer | 34764fe | 2012-08-17 19:26:41 +0000 | [diff] [blame] | 593 | } |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 594 | if (Argument *A = dyn_cast<Argument>(V)) |
| 595 | return visitArgument(*A); |
| 596 | if (ConstantPointerNull *P = dyn_cast<ConstantPointerNull>(V)) |
| 597 | return visitConstantPointerNull(*P); |
Nuno Lopes | e9d6dbf | 2012-12-31 16:23:48 +0000 | [diff] [blame] | 598 | if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) |
| 599 | return visitGlobalAlias(*GA); |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 600 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) |
| 601 | return visitGlobalVariable(*GV); |
| 602 | if (UndefValue *UV = dyn_cast<UndefValue>(V)) |
| 603 | return visitUndefValue(*UV); |
Benjamin Kramer | 34764fe | 2012-08-17 19:26:41 +0000 | [diff] [blame] | 604 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 605 | if (CE->getOpcode() == Instruction::IntToPtr) |
| 606 | return unknown(); // clueless |
Nadav Rotem | abcc64f | 2013-04-09 18:16:05 +0000 | [diff] [blame] | 607 | if (CE->getOpcode() == Instruction::GetElementPtr) |
| 608 | return visitGEPOperator(cast<GEPOperator>(*CE)); |
Benjamin Kramer | 34764fe | 2012-08-17 19:26:41 +0000 | [diff] [blame] | 609 | } |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 610 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 611 | LLVM_DEBUG(dbgs() << "ObjectSizeOffsetVisitor::compute() unhandled value: " |
| 612 | << *V << '\n'); |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 613 | return unknown(); |
| 614 | } |
| 615 | |
Mikael Holmen | ad7e718 | 2017-07-12 06:19:10 +0000 | [diff] [blame] | 616 | /// When we're compiling N-bit code, and the user uses parameters that are |
| 617 | /// greater than N bits (e.g. uint64_t on a 32-bit build), we can run into |
| 618 | /// trouble with APInt size issues. This function handles resizing + overflow |
| 619 | /// checks for us. Check and zext or trunc \p I depending on IntTyBits and |
| 620 | /// I's value. |
| 621 | bool ObjectSizeOffsetVisitor::CheckedZextOrTrunc(APInt &I) { |
| 622 | // More bits than we can handle. Checking the bit width isn't necessary, but |
| 623 | // it's faster than checking active bits, and should give `false` in the |
| 624 | // vast majority of cases. |
| 625 | if (I.getBitWidth() > IntTyBits && I.getActiveBits() > IntTyBits) |
| 626 | return false; |
| 627 | if (I.getBitWidth() != IntTyBits) |
| 628 | I = I.zextOrTrunc(IntTyBits); |
| 629 | return true; |
| 630 | } |
| 631 | |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 632 | SizeOffsetType ObjectSizeOffsetVisitor::visitAllocaInst(AllocaInst &I) { |
| 633 | if (!I.getAllocatedType()->isSized()) |
| 634 | return unknown(); |
| 635 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 636 | APInt Size(IntTyBits, DL.getTypeAllocSize(I.getAllocatedType())); |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 637 | if (!I.isArrayAllocation()) |
| 638 | return std::make_pair(align(Size, I.getAlignment()), Zero); |
| 639 | |
| 640 | Value *ArraySize = I.getArraySize(); |
| 641 | if (const ConstantInt *C = dyn_cast<ConstantInt>(ArraySize)) { |
Mikael Holmen | ad7e718 | 2017-07-12 06:19:10 +0000 | [diff] [blame] | 642 | APInt NumElems = C->getValue(); |
| 643 | if (!CheckedZextOrTrunc(NumElems)) |
| 644 | return unknown(); |
| 645 | |
| 646 | bool Overflow; |
| 647 | Size = Size.umul_ov(NumElems, Overflow); |
| 648 | return Overflow ? unknown() : std::make_pair(align(Size, I.getAlignment()), |
| 649 | Zero); |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 650 | } |
| 651 | return unknown(); |
| 652 | } |
| 653 | |
| 654 | SizeOffsetType ObjectSizeOffsetVisitor::visitArgument(Argument &A) { |
Sanjay Patel | 490193d | 2016-07-07 16:19:09 +0000 | [diff] [blame] | 655 | // No interprocedural analysis is done at the moment. |
Reid Kleckner | 26af2ca | 2014-01-28 02:38:36 +0000 | [diff] [blame] | 656 | if (!A.hasByValOrInAllocaAttr()) { |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 657 | ++ObjectVisitorArgument; |
| 658 | return unknown(); |
| 659 | } |
| 660 | PointerType *PT = cast<PointerType>(A.getType()); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 661 | APInt Size(IntTyBits, DL.getTypeAllocSize(PT->getElementType())); |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 662 | return std::make_pair(align(Size, A.getParamAlignment()), Zero); |
| 663 | } |
| 664 | |
| 665 | SizeOffsetType ObjectSizeOffsetVisitor::visitCallSite(CallSite CS) { |
George Burgess IV | ccae43a | 2016-12-23 01:18:09 +0000 | [diff] [blame] | 666 | Optional<AllocFnsTy> FnData = getAllocationSize(CS.getInstruction(), TLI); |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 667 | if (!FnData) |
| 668 | return unknown(); |
| 669 | |
Sanjay Patel | 490193d | 2016-07-07 16:19:09 +0000 | [diff] [blame] | 670 | // Handle strdup-like functions separately. |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 671 | if (FnData->AllocTy == StrDupLike) { |
David Bolvansky | 1f343fa | 2018-05-22 20:27:36 +0000 | [diff] [blame] | 672 | APInt Size(IntTyBits, GetStringLength(CS.getArgument(0))); |
Nuno Lopes | 2a4b09c | 2012-07-24 16:28:13 +0000 | [diff] [blame] | 673 | if (!Size) |
| 674 | return unknown(); |
| 675 | |
Sanjay Patel | 490193d | 2016-07-07 16:19:09 +0000 | [diff] [blame] | 676 | // Strndup limits strlen. |
Nuno Lopes | 2a4b09c | 2012-07-24 16:28:13 +0000 | [diff] [blame] | 677 | if (FnData->FstParam > 0) { |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 678 | ConstantInt *Arg = |
| 679 | dyn_cast<ConstantInt>(CS.getArgument(FnData->FstParam)); |
Nuno Lopes | 2a4b09c | 2012-07-24 16:28:13 +0000 | [diff] [blame] | 680 | if (!Arg) |
| 681 | return unknown(); |
| 682 | |
| 683 | APInt MaxSize = Arg->getValue().zextOrSelf(IntTyBits); |
| 684 | if (Size.ugt(MaxSize)) |
| 685 | Size = MaxSize + 1; |
| 686 | } |
| 687 | return std::make_pair(Size, Zero); |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | ConstantInt *Arg = dyn_cast<ConstantInt>(CS.getArgument(FnData->FstParam)); |
| 691 | if (!Arg) |
| 692 | return unknown(); |
| 693 | |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 694 | APInt Size = Arg->getValue(); |
| 695 | if (!CheckedZextOrTrunc(Size)) |
| 696 | return unknown(); |
| 697 | |
Sanjay Patel | 490193d | 2016-07-07 16:19:09 +0000 | [diff] [blame] | 698 | // Size is determined by just 1 parameter. |
Nuno Lopes | f06b731 | 2012-06-21 18:38:26 +0000 | [diff] [blame] | 699 | if (FnData->SndParam < 0) |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 700 | return std::make_pair(Size, Zero); |
| 701 | |
| 702 | Arg = dyn_cast<ConstantInt>(CS.getArgument(FnData->SndParam)); |
| 703 | if (!Arg) |
| 704 | return unknown(); |
| 705 | |
George Burgess IV | 278199f | 2016-04-12 01:05:35 +0000 | [diff] [blame] | 706 | APInt NumElems = Arg->getValue(); |
| 707 | if (!CheckedZextOrTrunc(NumElems)) |
| 708 | return unknown(); |
| 709 | |
| 710 | bool Overflow; |
| 711 | Size = Size.umul_ov(NumElems, Overflow); |
| 712 | return Overflow ? unknown() : std::make_pair(Size, Zero); |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 713 | |
| 714 | // TODO: handle more standard functions (+ wchar cousins): |
Nuno Lopes | f0626f2 | 2012-07-25 18:49:28 +0000 | [diff] [blame] | 715 | // - strdup / strndup |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 716 | // - strcpy / strncpy |
| 717 | // - strcat / strncat |
| 718 | // - memcpy / memmove |
Nuno Lopes | f0626f2 | 2012-07-25 18:49:28 +0000 | [diff] [blame] | 719 | // - strcat / strncat |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 720 | // - memset |
| 721 | } |
| 722 | |
| 723 | SizeOffsetType |
George Burgess IV | 56c7e88 | 2017-03-21 20:08:59 +0000 | [diff] [blame] | 724 | ObjectSizeOffsetVisitor::visitConstantPointerNull(ConstantPointerNull& CPN) { |
George Burgess IV | 3fbfa9c4 | 2018-07-09 22:21:16 +0000 | [diff] [blame] | 725 | // If null is unknown, there's nothing we can do. Additionally, non-zero |
| 726 | // address spaces can make use of null, so we don't presume to know anything |
| 727 | // about that. |
| 728 | // |
| 729 | // TODO: How should this work with address space casts? We currently just drop |
| 730 | // them on the floor, but it's unclear what we should do when a NULL from |
| 731 | // addrspace(1) gets casted to addrspace(0) (or vice-versa). |
| 732 | if (Options.NullIsUnknownSize || CPN.getType()->getAddressSpace()) |
George Burgess IV | 56c7e88 | 2017-03-21 20:08:59 +0000 | [diff] [blame] | 733 | return unknown(); |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 734 | return std::make_pair(Zero, Zero); |
| 735 | } |
| 736 | |
| 737 | SizeOffsetType |
Nuno Lopes | 181d67e | 2012-06-28 16:34:03 +0000 | [diff] [blame] | 738 | ObjectSizeOffsetVisitor::visitExtractElementInst(ExtractElementInst&) { |
| 739 | return unknown(); |
| 740 | } |
| 741 | |
| 742 | SizeOffsetType |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 743 | ObjectSizeOffsetVisitor::visitExtractValueInst(ExtractValueInst&) { |
| 744 | // Easy cases were already folded by previous passes. |
| 745 | return unknown(); |
| 746 | } |
| 747 | |
| 748 | SizeOffsetType ObjectSizeOffsetVisitor::visitGEPOperator(GEPOperator &GEP) { |
| 749 | SizeOffsetType PtrData = compute(GEP.getPointerOperand()); |
Nicola Zaghen | 9757277 | 2019-12-13 09:55:45 +0000 | [diff] [blame^] | 750 | APInt Offset(DL.getIndexTypeSizeInBits(GEP.getPointerOperand()->getType()), 0); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 751 | if (!bothKnown(PtrData) || !GEP.accumulateConstantOffset(DL, Offset)) |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 752 | return unknown(); |
| 753 | |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 754 | return std::make_pair(PtrData.first, PtrData.second + Offset); |
| 755 | } |
| 756 | |
Nuno Lopes | e9d6dbf | 2012-12-31 16:23:48 +0000 | [diff] [blame] | 757 | SizeOffsetType ObjectSizeOffsetVisitor::visitGlobalAlias(GlobalAlias &GA) { |
Sanjoy Das | 5ce3272 | 2016-04-08 00:48:30 +0000 | [diff] [blame] | 758 | if (GA.isInterposable()) |
Nuno Lopes | e9d6dbf | 2012-12-31 16:23:48 +0000 | [diff] [blame] | 759 | return unknown(); |
| 760 | return compute(GA.getAliasee()); |
| 761 | } |
| 762 | |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 763 | SizeOffsetType ObjectSizeOffsetVisitor::visitGlobalVariable(GlobalVariable &GV){ |
| 764 | if (!GV.hasDefinitiveInitializer()) |
| 765 | return unknown(); |
| 766 | |
Tim Northover | 67828ed | 2019-07-11 13:13:02 +0000 | [diff] [blame] | 767 | APInt Size(IntTyBits, DL.getTypeAllocSize(GV.getValueType())); |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 768 | return std::make_pair(align(Size, GV.getAlignment()), Zero); |
| 769 | } |
| 770 | |
| 771 | SizeOffsetType ObjectSizeOffsetVisitor::visitIntToPtrInst(IntToPtrInst&) { |
| 772 | // clueless |
| 773 | return unknown(); |
| 774 | } |
| 775 | |
| 776 | SizeOffsetType ObjectSizeOffsetVisitor::visitLoadInst(LoadInst&) { |
| 777 | ++ObjectVisitorLoad; |
| 778 | return unknown(); |
| 779 | } |
| 780 | |
Nadav Rotem | abcc64f | 2013-04-09 18:16:05 +0000 | [diff] [blame] | 781 | SizeOffsetType ObjectSizeOffsetVisitor::visitPHINode(PHINode&) { |
| 782 | // too complex to analyze statically. |
| 783 | return unknown(); |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | SizeOffsetType ObjectSizeOffsetVisitor::visitSelectInst(SelectInst &I) { |
| 787 | SizeOffsetType TrueSide = compute(I.getTrueValue()); |
| 788 | SizeOffsetType FalseSide = compute(I.getFalseValue()); |
Petar Jovanovic | 644b8c1 | 2016-04-13 12:25:25 +0000 | [diff] [blame] | 789 | if (bothKnown(TrueSide) && bothKnown(FalseSide)) { |
| 790 | if (TrueSide == FalseSide) { |
| 791 | return TrueSide; |
| 792 | } |
| 793 | |
| 794 | APInt TrueResult = getSizeWithOverflow(TrueSide); |
| 795 | APInt FalseResult = getSizeWithOverflow(FalseSide); |
| 796 | |
| 797 | if (TrueResult == FalseResult) { |
| 798 | return TrueSide; |
| 799 | } |
George Burgess IV | 56c7e88 | 2017-03-21 20:08:59 +0000 | [diff] [blame] | 800 | if (Options.EvalMode == ObjectSizeOpts::Mode::Min) { |
Petar Jovanovic | 644b8c1 | 2016-04-13 12:25:25 +0000 | [diff] [blame] | 801 | if (TrueResult.slt(FalseResult)) |
| 802 | return TrueSide; |
| 803 | return FalseSide; |
| 804 | } |
George Burgess IV | 56c7e88 | 2017-03-21 20:08:59 +0000 | [diff] [blame] | 805 | if (Options.EvalMode == ObjectSizeOpts::Mode::Max) { |
Petar Jovanovic | 644b8c1 | 2016-04-13 12:25:25 +0000 | [diff] [blame] | 806 | if (TrueResult.sgt(FalseResult)) |
| 807 | return TrueSide; |
| 808 | return FalseSide; |
| 809 | } |
| 810 | } |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 811 | return unknown(); |
| 812 | } |
| 813 | |
| 814 | SizeOffsetType ObjectSizeOffsetVisitor::visitUndefValue(UndefValue&) { |
| 815 | return std::make_pair(Zero, Zero); |
| 816 | } |
| 817 | |
| 818 | SizeOffsetType ObjectSizeOffsetVisitor::visitInstruction(Instruction &I) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 819 | LLVM_DEBUG(dbgs() << "ObjectSizeOffsetVisitor unknown instruction:" << I |
| 820 | << '\n'); |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 821 | return unknown(); |
| 822 | } |
| 823 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 824 | ObjectSizeOffsetEvaluator::ObjectSizeOffsetEvaluator( |
| 825 | const DataLayout &DL, const TargetLibraryInfo *TLI, LLVMContext &Context, |
Erik Pilkington | 600e9de | 2019-01-30 20:34:35 +0000 | [diff] [blame] | 826 | ObjectSizeOpts EvalOpts) |
Erik Pilkington | cb5c7bd | 2019-04-10 23:42:11 +0000 | [diff] [blame] | 827 | : DL(DL), TLI(TLI), Context(Context), |
| 828 | Builder(Context, TargetFolder(DL), |
| 829 | IRBuilderCallbackInserter( |
| 830 | [&](Instruction *I) { InsertedInstructions.insert(I); })), |
Erik Pilkington | 600e9de | 2019-01-30 20:34:35 +0000 | [diff] [blame] | 831 | EvalOpts(EvalOpts) { |
Matt Arsenault | d3ee7af | 2013-12-14 00:27:48 +0000 | [diff] [blame] | 832 | // IntTy and Zero must be set for each compute() since the address space may |
| 833 | // be different for later objects. |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | SizeOffsetEvalType ObjectSizeOffsetEvaluator::compute(Value *V) { |
Matt Arsenault | d3ee7af | 2013-12-14 00:27:48 +0000 | [diff] [blame] | 837 | // XXX - Are vectors of pointers possible here? |
Nicola Zaghen | 9757277 | 2019-12-13 09:55:45 +0000 | [diff] [blame^] | 838 | IntTy = cast<IntegerType>(DL.getIndexType(V->getType())); |
Matt Arsenault | d3ee7af | 2013-12-14 00:27:48 +0000 | [diff] [blame] | 839 | Zero = ConstantInt::get(IntTy, 0); |
| 840 | |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 841 | SizeOffsetEvalType Result = compute_(V); |
| 842 | |
| 843 | if (!bothKnown(Result)) { |
Sanjay Patel | 490193d | 2016-07-07 16:19:09 +0000 | [diff] [blame] | 844 | // Erase everything that was computed in this iteration from the cache, so |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 845 | // that no dangling references are left behind. We could be a bit smarter if |
| 846 | // we kept a dependency graph. It's probably not worth the complexity. |
Benjamin Kramer | aa20915 | 2016-06-26 17:27:42 +0000 | [diff] [blame] | 847 | for (const Value *SeenVal : SeenVals) { |
| 848 | CacheMapTy::iterator CacheIt = CacheMap.find(SeenVal); |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 849 | // non-computable results can be safely cached |
| 850 | if (CacheIt != CacheMap.end() && anyKnown(CacheIt->second)) |
| 851 | CacheMap.erase(CacheIt); |
| 852 | } |
Erik Pilkington | cb5c7bd | 2019-04-10 23:42:11 +0000 | [diff] [blame] | 853 | |
| 854 | // Erase any instructions we inserted as part of the traversal. |
| 855 | for (Instruction *I : InsertedInstructions) { |
| 856 | I->replaceAllUsesWith(UndefValue::get(I->getType())); |
| 857 | I->eraseFromParent(); |
| 858 | } |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | SeenVals.clear(); |
Erik Pilkington | cb5c7bd | 2019-04-10 23:42:11 +0000 | [diff] [blame] | 862 | InsertedInstructions.clear(); |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 863 | return Result; |
| 864 | } |
| 865 | |
| 866 | SizeOffsetEvalType ObjectSizeOffsetEvaluator::compute_(Value *V) { |
Erik Pilkington | 600e9de | 2019-01-30 20:34:35 +0000 | [diff] [blame] | 867 | ObjectSizeOffsetVisitor Visitor(DL, TLI, Context, EvalOpts); |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 868 | SizeOffsetType Const = Visitor.compute(V); |
| 869 | if (Visitor.bothKnown(Const)) |
| 870 | return std::make_pair(ConstantInt::get(Context, Const.first), |
| 871 | ConstantInt::get(Context, Const.second)); |
| 872 | |
| 873 | V = V->stripPointerCasts(); |
| 874 | |
Sanjay Patel | 490193d | 2016-07-07 16:19:09 +0000 | [diff] [blame] | 875 | // Check cache. |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 876 | CacheMapTy::iterator CacheIt = CacheMap.find(V); |
| 877 | if (CacheIt != CacheMap.end()) |
| 878 | return CacheIt->second; |
| 879 | |
Sanjay Patel | 490193d | 2016-07-07 16:19:09 +0000 | [diff] [blame] | 880 | // Always generate code immediately before the instruction being |
| 881 | // processed, so that the generated code dominates the same BBs. |
Duncan P. N. Exon Smith | 5a82c91 | 2015-10-10 00:53:03 +0000 | [diff] [blame] | 882 | BuilderTy::InsertPointGuard Guard(Builder); |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 883 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 884 | Builder.SetInsertPoint(I); |
| 885 | |
Sanjay Patel | 490193d | 2016-07-07 16:19:09 +0000 | [diff] [blame] | 886 | // Now compute the size and offset. |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 887 | SizeOffsetEvalType Result; |
Benjamin Kramer | 155c9d5 | 2013-09-29 19:39:13 +0000 | [diff] [blame] | 888 | |
| 889 | // Record the pointers that were handled in this run, so that they can be |
| 890 | // cleaned later if something fails. We also use this set to break cycles that |
| 891 | // can occur in dead code. |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 892 | if (!SeenVals.insert(V).second) { |
Benjamin Kramer | 155c9d5 | 2013-09-29 19:39:13 +0000 | [diff] [blame] | 893 | Result = unknown(); |
| 894 | } else if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 895 | Result = visitGEPOperator(*GEP); |
| 896 | } else if (Instruction *I = dyn_cast<Instruction>(V)) { |
| 897 | Result = visit(*I); |
| 898 | } else if (isa<Argument>(V) || |
| 899 | (isa<ConstantExpr>(V) && |
| 900 | cast<ConstantExpr>(V)->getOpcode() == Instruction::IntToPtr) || |
Nuno Lopes | e9d6dbf | 2012-12-31 16:23:48 +0000 | [diff] [blame] | 901 | isa<GlobalAlias>(V) || |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 902 | isa<GlobalVariable>(V)) { |
Sanjay Patel | 490193d | 2016-07-07 16:19:09 +0000 | [diff] [blame] | 903 | // Ignore values where we cannot do more than ObjectSizeVisitor. |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 904 | Result = unknown(); |
| 905 | } else { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 906 | LLVM_DEBUG( |
| 907 | dbgs() << "ObjectSizeOffsetEvaluator::compute() unhandled value: " << *V |
| 908 | << '\n'); |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 909 | Result = unknown(); |
| 910 | } |
| 911 | |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 912 | // Don't reuse CacheIt since it may be invalid at this point. |
| 913 | CacheMap[V] = Result; |
| 914 | return Result; |
| 915 | } |
| 916 | |
| 917 | SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitAllocaInst(AllocaInst &I) { |
| 918 | if (!I.getAllocatedType()->isSized()) |
| 919 | return unknown(); |
| 920 | |
| 921 | // must be a VLA |
| 922 | assert(I.isArrayAllocation()); |
| 923 | Value *ArraySize = I.getArraySize(); |
| 924 | Value *Size = ConstantInt::get(ArraySize->getType(), |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 925 | DL.getTypeAllocSize(I.getAllocatedType())); |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 926 | Size = Builder.CreateMul(Size, ArraySize); |
| 927 | return std::make_pair(Size, Zero); |
| 928 | } |
| 929 | |
| 930 | SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitCallSite(CallSite CS) { |
George Burgess IV | ccae43a | 2016-12-23 01:18:09 +0000 | [diff] [blame] | 931 | Optional<AllocFnsTy> FnData = getAllocationSize(CS.getInstruction(), TLI); |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 932 | if (!FnData) |
| 933 | return unknown(); |
| 934 | |
Sanjay Patel | 490193d | 2016-07-07 16:19:09 +0000 | [diff] [blame] | 935 | // Handle strdup-like functions separately. |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 936 | if (FnData->AllocTy == StrDupLike) { |
Nuno Lopes | f0626f2 | 2012-07-25 18:49:28 +0000 | [diff] [blame] | 937 | // TODO |
| 938 | return unknown(); |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 939 | } |
| 940 | |
Nuno Lopes | a6aa3d3 | 2012-06-21 16:47:58 +0000 | [diff] [blame] | 941 | Value *FirstArg = CS.getArgument(FnData->FstParam); |
Nicola Zaghen | 9757277 | 2019-12-13 09:55:45 +0000 | [diff] [blame^] | 942 | FirstArg = Builder.CreateZExtOrTrunc(FirstArg, IntTy); |
Nuno Lopes | f06b731 | 2012-06-21 18:38:26 +0000 | [diff] [blame] | 943 | if (FnData->SndParam < 0) |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 944 | return std::make_pair(FirstArg, Zero); |
| 945 | |
| 946 | Value *SecondArg = CS.getArgument(FnData->SndParam); |
Nicola Zaghen | 9757277 | 2019-12-13 09:55:45 +0000 | [diff] [blame^] | 947 | SecondArg = Builder.CreateZExtOrTrunc(SecondArg, IntTy); |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 948 | Value *Size = Builder.CreateMul(FirstArg, SecondArg); |
| 949 | return std::make_pair(Size, Zero); |
| 950 | |
| 951 | // TODO: handle more standard functions (+ wchar cousins): |
Nuno Lopes | f0626f2 | 2012-07-25 18:49:28 +0000 | [diff] [blame] | 952 | // - strdup / strndup |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 953 | // - strcpy / strncpy |
| 954 | // - strcat / strncat |
| 955 | // - memcpy / memmove |
Nuno Lopes | f0626f2 | 2012-07-25 18:49:28 +0000 | [diff] [blame] | 956 | // - strcat / strncat |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 957 | // - memset |
| 958 | } |
| 959 | |
| 960 | SizeOffsetEvalType |
Nuno Lopes | 181d67e | 2012-06-28 16:34:03 +0000 | [diff] [blame] | 961 | ObjectSizeOffsetEvaluator::visitExtractElementInst(ExtractElementInst&) { |
| 962 | return unknown(); |
| 963 | } |
| 964 | |
| 965 | SizeOffsetEvalType |
| 966 | ObjectSizeOffsetEvaluator::visitExtractValueInst(ExtractValueInst&) { |
| 967 | return unknown(); |
| 968 | } |
| 969 | |
| 970 | SizeOffsetEvalType |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 971 | ObjectSizeOffsetEvaluator::visitGEPOperator(GEPOperator &GEP) { |
| 972 | SizeOffsetEvalType PtrData = compute_(GEP.getPointerOperand()); |
| 973 | if (!bothKnown(PtrData)) |
| 974 | return unknown(); |
| 975 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 976 | Value *Offset = EmitGEPOffset(&Builder, DL, &GEP, /*NoAssumptions=*/true); |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 977 | Offset = Builder.CreateAdd(PtrData.second, Offset); |
| 978 | return std::make_pair(PtrData.first, Offset); |
| 979 | } |
| 980 | |
| 981 | SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitIntToPtrInst(IntToPtrInst&) { |
| 982 | // clueless |
| 983 | return unknown(); |
| 984 | } |
| 985 | |
| 986 | SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitLoadInst(LoadInst&) { |
| 987 | return unknown(); |
| 988 | } |
| 989 | |
| 990 | SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitPHINode(PHINode &PHI) { |
Sanjay Patel | 490193d | 2016-07-07 16:19:09 +0000 | [diff] [blame] | 991 | // Create 2 PHIs: one for size and another for offset. |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 992 | PHINode *SizePHI = Builder.CreatePHI(IntTy, PHI.getNumIncomingValues()); |
| 993 | PHINode *OffsetPHI = Builder.CreatePHI(IntTy, PHI.getNumIncomingValues()); |
| 994 | |
Sanjay Patel | 490193d | 2016-07-07 16:19:09 +0000 | [diff] [blame] | 995 | // Insert right away in the cache to handle recursive PHIs. |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 996 | CacheMap[&PHI] = std::make_pair(SizePHI, OffsetPHI); |
| 997 | |
Sanjay Patel | 490193d | 2016-07-07 16:19:09 +0000 | [diff] [blame] | 998 | // Compute offset/size for each PHI incoming pointer. |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 999 | for (unsigned i = 0, e = PHI.getNumIncomingValues(); i != e; ++i) { |
Duncan P. N. Exon Smith | 5a82c91 | 2015-10-10 00:53:03 +0000 | [diff] [blame] | 1000 | Builder.SetInsertPoint(&*PHI.getIncomingBlock(i)->getFirstInsertionPt()); |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 1001 | SizeOffsetEvalType EdgeData = compute_(PHI.getIncomingValue(i)); |
| 1002 | |
| 1003 | if (!bothKnown(EdgeData)) { |
| 1004 | OffsetPHI->replaceAllUsesWith(UndefValue::get(IntTy)); |
| 1005 | OffsetPHI->eraseFromParent(); |
Erik Pilkington | cb5c7bd | 2019-04-10 23:42:11 +0000 | [diff] [blame] | 1006 | InsertedInstructions.erase(OffsetPHI); |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 1007 | SizePHI->replaceAllUsesWith(UndefValue::get(IntTy)); |
| 1008 | SizePHI->eraseFromParent(); |
Erik Pilkington | cb5c7bd | 2019-04-10 23:42:11 +0000 | [diff] [blame] | 1009 | InsertedInstructions.erase(SizePHI); |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 1010 | return unknown(); |
| 1011 | } |
| 1012 | SizePHI->addIncoming(EdgeData.first, PHI.getIncomingBlock(i)); |
| 1013 | OffsetPHI->addIncoming(EdgeData.second, PHI.getIncomingBlock(i)); |
| 1014 | } |
Nuno Lopes | 9291ff4 | 2012-07-03 17:13:25 +0000 | [diff] [blame] | 1015 | |
Erik Pilkington | cb5c7bd | 2019-04-10 23:42:11 +0000 | [diff] [blame] | 1016 | Value *Size = SizePHI, *Offset = OffsetPHI; |
| 1017 | if (Value *Tmp = SizePHI->hasConstantValue()) { |
Nuno Lopes | 9291ff4 | 2012-07-03 17:13:25 +0000 | [diff] [blame] | 1018 | Size = Tmp; |
| 1019 | SizePHI->replaceAllUsesWith(Size); |
| 1020 | SizePHI->eraseFromParent(); |
Erik Pilkington | cb5c7bd | 2019-04-10 23:42:11 +0000 | [diff] [blame] | 1021 | InsertedInstructions.erase(SizePHI); |
Nuno Lopes | 9291ff4 | 2012-07-03 17:13:25 +0000 | [diff] [blame] | 1022 | } |
Erik Pilkington | cb5c7bd | 2019-04-10 23:42:11 +0000 | [diff] [blame] | 1023 | if (Value *Tmp = OffsetPHI->hasConstantValue()) { |
Nuno Lopes | 9291ff4 | 2012-07-03 17:13:25 +0000 | [diff] [blame] | 1024 | Offset = Tmp; |
| 1025 | OffsetPHI->replaceAllUsesWith(Offset); |
| 1026 | OffsetPHI->eraseFromParent(); |
Erik Pilkington | cb5c7bd | 2019-04-10 23:42:11 +0000 | [diff] [blame] | 1027 | InsertedInstructions.erase(OffsetPHI); |
Nuno Lopes | 9291ff4 | 2012-07-03 17:13:25 +0000 | [diff] [blame] | 1028 | } |
| 1029 | return std::make_pair(Size, Offset); |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitSelectInst(SelectInst &I) { |
| 1033 | SizeOffsetEvalType TrueSide = compute_(I.getTrueValue()); |
| 1034 | SizeOffsetEvalType FalseSide = compute_(I.getFalseValue()); |
| 1035 | |
| 1036 | if (!bothKnown(TrueSide) || !bothKnown(FalseSide)) |
| 1037 | return unknown(); |
| 1038 | if (TrueSide == FalseSide) |
| 1039 | return TrueSide; |
| 1040 | |
| 1041 | Value *Size = Builder.CreateSelect(I.getCondition(), TrueSide.first, |
| 1042 | FalseSide.first); |
| 1043 | Value *Offset = Builder.CreateSelect(I.getCondition(), TrueSide.second, |
| 1044 | FalseSide.second); |
| 1045 | return std::make_pair(Size, Offset); |
| 1046 | } |
| 1047 | |
| 1048 | SizeOffsetEvalType ObjectSizeOffsetEvaluator::visitInstruction(Instruction &I) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1049 | LLVM_DEBUG(dbgs() << "ObjectSizeOffsetEvaluator unknown instruction:" << I |
| 1050 | << '\n'); |
Nuno Lopes | 55fff83 | 2012-06-21 15:45:28 +0000 | [diff] [blame] | 1051 | return unknown(); |
| 1052 | } |