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