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 | |
Victor Hernandez | f006b18 | 2009-10-27 20:05:49 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/MemoryBuiltins.h" |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 16 | #include "llvm/Constants.h" |
| 17 | #include "llvm/Instructions.h" |
| 18 | #include "llvm/Module.h" |
Victor Hernandez | 8e345a1 | 2009-11-10 08:32:25 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/ValueTracking.h" |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 20 | #include "llvm/Target/TargetData.h" |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | // malloc Call Utility Functions. |
| 25 | // |
| 26 | |
| 27 | /// isMalloc - Returns true if the the value is either a malloc call or a |
| 28 | /// bitcast of the result of a malloc call. |
Victor Hernandez | 88efeae | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 29 | bool llvm::isMalloc(const Value *I) { |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 30 | return extractMallocCall(I) || extractMallocCallFromBitCast(I); |
| 31 | } |
| 32 | |
| 33 | static bool isMallocCall(const CallInst *CI) { |
| 34 | if (!CI) |
| 35 | return false; |
| 36 | |
Victor Hernandez | 3ad70d5 | 2009-11-03 20:39:35 +0000 | [diff] [blame] | 37 | Function *Callee = CI->getCalledFunction(); |
| 38 | if (Callee == 0 || !Callee->isDeclaration() || Callee->getName() != "malloc") |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 39 | return false; |
| 40 | |
Torok Edwin | 85c005a | 2009-10-05 21:15:43 +0000 | [diff] [blame] | 41 | // Check malloc prototype. |
Torok Edwin | 4de86fe | 2009-10-07 09:22:55 +0000 | [diff] [blame] | 42 | // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin |
| 43 | // attribute will exist. |
Victor Hernandez | 3ad70d5 | 2009-11-03 20:39:35 +0000 | [diff] [blame] | 44 | const FunctionType *FTy = Callee->getFunctionType(); |
Torok Edwin | 85c005a | 2009-10-05 21:15:43 +0000 | [diff] [blame] | 45 | if (FTy->getNumParams() != 1) |
| 46 | return false; |
| 47 | if (IntegerType *ITy = dyn_cast<IntegerType>(FTy->param_begin()->get())) { |
| 48 | if (ITy->getBitWidth() != 32 && ITy->getBitWidth() != 64) |
| 49 | return false; |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | return false; |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /// extractMallocCall - Returns the corresponding CallInst if the instruction |
| 57 | /// is a malloc call. Since CallInst::CreateMalloc() only creates calls, we |
| 58 | /// ignore InvokeInst here. |
Victor Hernandez | 88efeae | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 59 | const CallInst *llvm::extractMallocCall(const Value *I) { |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 60 | const CallInst *CI = dyn_cast<CallInst>(I); |
| 61 | return (isMallocCall(CI)) ? CI : NULL; |
| 62 | } |
| 63 | |
Victor Hernandez | 88efeae | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 64 | CallInst *llvm::extractMallocCall(Value *I) { |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 65 | CallInst *CI = dyn_cast<CallInst>(I); |
| 66 | return (isMallocCall(CI)) ? CI : NULL; |
| 67 | } |
| 68 | |
Victor Hernandez | 88efeae | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 69 | static bool isBitCastOfMallocCall(const BitCastInst *BCI) { |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 70 | if (!BCI) |
| 71 | return false; |
| 72 | |
| 73 | return isMallocCall(dyn_cast<CallInst>(BCI->getOperand(0))); |
| 74 | } |
| 75 | |
| 76 | /// extractMallocCallFromBitCast - Returns the corresponding CallInst if the |
| 77 | /// instruction is a bitcast of the result of a malloc call. |
Victor Hernandez | 88efeae | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 78 | CallInst *llvm::extractMallocCallFromBitCast(Value *I) { |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 79 | BitCastInst *BCI = dyn_cast<BitCastInst>(I); |
Victor Hernandez | 399e45b | 2009-09-10 20:18:57 +0000 | [diff] [blame] | 80 | return (isBitCastOfMallocCall(BCI)) ? cast<CallInst>(BCI->getOperand(0)) |
| 81 | : NULL; |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Victor Hernandez | 88efeae | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 84 | const CallInst *llvm::extractMallocCallFromBitCast(const Value *I) { |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 85 | const BitCastInst *BCI = dyn_cast<BitCastInst>(I); |
Victor Hernandez | 399e45b | 2009-09-10 20:18:57 +0000 | [diff] [blame] | 86 | return (isBitCastOfMallocCall(BCI)) ? cast<CallInst>(BCI->getOperand(0)) |
| 87 | : NULL; |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Victor Hernandez | 8e345a1 | 2009-11-10 08:32:25 +0000 | [diff] [blame] | 90 | static Value *computeArraySize(const CallInst *CI, const TargetData *TD, |
| 91 | bool LookThroughSExt = false) { |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 92 | if (!CI) |
Victor Hernandez | 90f48e7 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 93 | return NULL; |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 94 | |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 95 | // The size of the malloc's result type must be known to determine array size. |
Victor Hernandez | 88efeae | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 96 | const Type *T = getMallocAllocatedType(CI); |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 97 | if (!T || !T->isSized() || !TD) |
Victor Hernandez | 90f48e7 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 98 | return NULL; |
Victor Hernandez | 88d9839 | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 99 | |
Victor Hernandez | 8e345a1 | 2009-11-10 08:32:25 +0000 | [diff] [blame] | 100 | unsigned ElementSize = TD->getTypeAllocSize(T); |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 101 | if (const StructType *ST = dyn_cast<StructType>(T)) |
Victor Hernandez | 8e345a1 | 2009-11-10 08:32:25 +0000 | [diff] [blame] | 102 | ElementSize = TD->getStructLayout(ST)->getSizeInBytes(); |
Victor Hernandez | 88d9839 | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 103 | |
Victor Hernandez | 8e345a1 | 2009-11-10 08:32:25 +0000 | [diff] [blame] | 104 | // If malloc calls' arg can be determined to be a multiple of ElementSize, |
| 105 | // return the multiple. Otherwise, return NULL. |
| 106 | Value *MallocArg = CI->getOperand(1); |
| 107 | Value *Multiple = NULL; |
Victor Hernandez | 8e345a1 | 2009-11-10 08:32:25 +0000 | [diff] [blame] | 108 | if (ComputeMultiple(MallocArg, ElementSize, Multiple, |
Dan Gohman | 3dbb9e6 | 2009-11-18 00:58:27 +0000 | [diff] [blame^] | 109 | LookThroughSExt)) |
Victor Hernandez | 8e345a1 | 2009-11-10 08:32:25 +0000 | [diff] [blame] | 110 | return Multiple; |
Victor Hernandez | 88d9839 | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 111 | |
Victor Hernandez | 90f48e7 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 112 | return NULL; |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /// isArrayMalloc - Returns the corresponding CallInst if the instruction |
Victor Hernandez | 90f48e7 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 116 | /// is a call to malloc whose array size can be determined and the array size |
| 117 | /// is not constant 1. Otherwise, return NULL. |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame] | 118 | const CallInst *llvm::isArrayMalloc(const Value *I, const TargetData *TD) { |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 119 | const CallInst *CI = extractMallocCall(I); |
Victor Hernandez | 8e345a1 | 2009-11-10 08:32:25 +0000 | [diff] [blame] | 120 | Value *ArraySize = computeArraySize(CI, TD); |
Victor Hernandez | 90f48e7 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 121 | |
| 122 | if (ArraySize && |
| 123 | ArraySize != ConstantInt::get(CI->getOperand(1)->getType(), 1)) |
| 124 | return CI; |
| 125 | |
| 126 | // CI is a non-array malloc or we can't figure out that it is an array malloc. |
| 127 | return NULL; |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /// getMallocType - Returns the PointerType resulting from the malloc call. |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 131 | /// The PointerType depends on the number of bitcast uses of the malloc call: |
| 132 | /// 0: PointerType is the calls' return type. |
| 133 | /// 1: PointerType is the bitcast's result type. |
| 134 | /// >1: Unique PointerType cannot be determined, return NULL. |
Victor Hernandez | 88efeae | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 135 | const PointerType *llvm::getMallocType(const CallInst *CI) { |
Victor Hernandez | 8e345a1 | 2009-11-10 08:32:25 +0000 | [diff] [blame] | 136 | assert(isMalloc(CI) && "getMallocType and not malloc call"); |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 137 | |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 138 | const PointerType *MallocType = NULL; |
| 139 | unsigned NumOfBitCastUses = 0; |
| 140 | |
Victor Hernandez | 88d9839 | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 141 | // Determine if CallInst has a bitcast use. |
| 142 | for (Value::use_const_iterator UI = CI->use_begin(), E = CI->use_end(); |
| 143 | UI != E; ) |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 144 | if (const BitCastInst *BCI = dyn_cast<BitCastInst>(*UI++)) { |
| 145 | MallocType = cast<PointerType>(BCI->getDestTy()); |
| 146 | NumOfBitCastUses++; |
| 147 | } |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 148 | |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 149 | // Malloc call has 1 bitcast use, so type is the bitcast's destination type. |
| 150 | if (NumOfBitCastUses == 1) |
| 151 | return MallocType; |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 152 | |
Victor Hernandez | 60cfc03 | 2009-09-22 18:50:03 +0000 | [diff] [blame] | 153 | // 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] | 154 | if (NumOfBitCastUses == 0) |
Victor Hernandez | 88d9839 | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 155 | return cast<PointerType>(CI->getType()); |
| 156 | |
| 157 | // Type could not be determined. |
| 158 | return NULL; |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 161 | /// getMallocAllocatedType - Returns the Type allocated by malloc call. |
| 162 | /// The Type depends on the number of bitcast uses of the malloc call: |
| 163 | /// 0: PointerType is the malloc calls' return type. |
| 164 | /// 1: PointerType is the bitcast's result type. |
| 165 | /// >1: Unique PointerType cannot be determined, return NULL. |
Victor Hernandez | 88efeae | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 166 | const Type *llvm::getMallocAllocatedType(const CallInst *CI) { |
| 167 | const PointerType *PT = getMallocType(CI); |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 168 | return PT ? PT->getElementType() : NULL; |
| 169 | } |
| 170 | |
Victor Hernandez | 90f48e7 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 171 | /// getMallocArraySize - Returns the array size of a malloc call. If the |
| 172 | /// argument passed to malloc is a multiple of the size of the malloced type, |
| 173 | /// then return that multiple. For non-array mallocs, the multiple is |
| 174 | /// constant 1. Otherwise, return NULL for mallocs whose array size cannot be |
Victor Hernandez | 2491ce0 | 2009-10-15 20:14:52 +0000 | [diff] [blame] | 175 | /// determined. |
Victor Hernandez | 8e345a1 | 2009-11-10 08:32:25 +0000 | [diff] [blame] | 176 | Value *llvm::getMallocArraySize(CallInst *CI, const TargetData *TD, |
| 177 | bool LookThroughSExt) { |
| 178 | assert(isMalloc(CI) && "getMallocArraySize and not malloc call"); |
| 179 | return computeArraySize(CI, TD, LookThroughSExt); |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 180 | } |
Victor Hernandez | 66284e0 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 181 | |
Victor Hernandez | 046e78c | 2009-10-26 23:43:48 +0000 | [diff] [blame] | 182 | //===----------------------------------------------------------------------===// |
| 183 | // free Call Utility Functions. |
| 184 | // |
| 185 | |
Victor Hernandez | 66284e0 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 186 | /// isFreeCall - Returns true if the the value is a call to the builtin free() |
Victor Hernandez | 88efeae | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 187 | bool llvm::isFreeCall(const Value *I) { |
Victor Hernandez | 66284e0 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 188 | const CallInst *CI = dyn_cast<CallInst>(I); |
| 189 | if (!CI) |
| 190 | return false; |
Victor Hernandez | 3ad70d5 | 2009-11-03 20:39:35 +0000 | [diff] [blame] | 191 | Function *Callee = CI->getCalledFunction(); |
| 192 | if (Callee == 0 || !Callee->isDeclaration() || Callee->getName() != "free") |
Victor Hernandez | 66284e0 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 193 | return false; |
| 194 | |
| 195 | // Check free prototype. |
| 196 | // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin |
| 197 | // attribute will exist. |
Victor Hernandez | 3ad70d5 | 2009-11-03 20:39:35 +0000 | [diff] [blame] | 198 | const FunctionType *FTy = Callee->getFunctionType(); |
| 199 | if (!FTy->getReturnType()->isVoidTy()) |
Victor Hernandez | 66284e0 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 200 | return false; |
| 201 | if (FTy->getNumParams() != 1) |
| 202 | return false; |
Victor Hernandez | 3ad70d5 | 2009-11-03 20:39:35 +0000 | [diff] [blame] | 203 | if (FTy->param_begin()->get() != Type::getInt8PtrTy(Callee->getContext())) |
Victor Hernandez | 66284e0 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 204 | return false; |
| 205 | |
| 206 | return true; |
| 207 | } |