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