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