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