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