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