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 | 88d9839 | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/ConstantFolding.h" |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 20 | using namespace llvm; |
| 21 | |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | // malloc Call Utility Functions. |
| 24 | // |
| 25 | |
| 26 | /// isMalloc - Returns true if the the value is either a malloc call or a |
| 27 | /// bitcast of the result of a malloc call. |
Victor Hernandez | 88efeae | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 28 | bool llvm::isMalloc(const Value *I) { |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 29 | return extractMallocCall(I) || extractMallocCallFromBitCast(I); |
| 30 | } |
| 31 | |
| 32 | static bool isMallocCall(const CallInst *CI) { |
| 33 | if (!CI) |
| 34 | return false; |
| 35 | |
Victor Hernandez | 3ad70d5 | 2009-11-03 20:39:35 +0000 | [diff] [blame] | 36 | Function *Callee = CI->getCalledFunction(); |
| 37 | if (Callee == 0 || !Callee->isDeclaration() || Callee->getName() != "malloc") |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 38 | return false; |
| 39 | |
Torok Edwin | 85c005a | 2009-10-05 21:15:43 +0000 | [diff] [blame] | 40 | // Check malloc prototype. |
Torok Edwin | 4de86fe | 2009-10-07 09:22:55 +0000 | [diff] [blame] | 41 | // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin |
| 42 | // attribute will exist. |
Victor Hernandez | 3ad70d5 | 2009-11-03 20:39:35 +0000 | [diff] [blame] | 43 | const FunctionType *FTy = Callee->getFunctionType(); |
Torok Edwin | 85c005a | 2009-10-05 21:15:43 +0000 | [diff] [blame] | 44 | if (FTy->getNumParams() != 1) |
| 45 | return false; |
| 46 | if (IntegerType *ITy = dyn_cast<IntegerType>(FTy->param_begin()->get())) { |
| 47 | if (ITy->getBitWidth() != 32 && ITy->getBitWidth() != 64) |
| 48 | return false; |
| 49 | return true; |
| 50 | } |
| 51 | |
| 52 | return false; |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /// extractMallocCall - Returns the corresponding CallInst if the instruction |
| 56 | /// is a malloc call. Since CallInst::CreateMalloc() only creates calls, we |
| 57 | /// ignore InvokeInst here. |
Victor Hernandez | 88efeae | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 58 | const CallInst *llvm::extractMallocCall(const Value *I) { |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 59 | const CallInst *CI = dyn_cast<CallInst>(I); |
| 60 | return (isMallocCall(CI)) ? CI : NULL; |
| 61 | } |
| 62 | |
Victor Hernandez | 88efeae | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 63 | CallInst *llvm::extractMallocCall(Value *I) { |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 64 | CallInst *CI = dyn_cast<CallInst>(I); |
| 65 | return (isMallocCall(CI)) ? CI : NULL; |
| 66 | } |
| 67 | |
Victor Hernandez | 88efeae | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 68 | static bool isBitCastOfMallocCall(const BitCastInst *BCI) { |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 69 | if (!BCI) |
| 70 | return false; |
| 71 | |
| 72 | return isMallocCall(dyn_cast<CallInst>(BCI->getOperand(0))); |
| 73 | } |
| 74 | |
| 75 | /// extractMallocCallFromBitCast - Returns the corresponding CallInst if the |
| 76 | /// instruction is a bitcast of the result of a malloc call. |
Victor Hernandez | 88efeae | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 77 | CallInst *llvm::extractMallocCallFromBitCast(Value *I) { |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 78 | BitCastInst *BCI = dyn_cast<BitCastInst>(I); |
Victor Hernandez | 399e45b | 2009-09-10 20:18:57 +0000 | [diff] [blame] | 79 | return (isBitCastOfMallocCall(BCI)) ? cast<CallInst>(BCI->getOperand(0)) |
| 80 | : NULL; |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Victor Hernandez | 88efeae | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 83 | const CallInst *llvm::extractMallocCallFromBitCast(const Value *I) { |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 84 | const BitCastInst *BCI = dyn_cast<BitCastInst>(I); |
Victor Hernandez | 399e45b | 2009-09-10 20:18:57 +0000 | [diff] [blame] | 85 | return (isBitCastOfMallocCall(BCI)) ? cast<CallInst>(BCI->getOperand(0)) |
| 86 | : NULL; |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Victor Hernandez | 90f48e7 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 89 | /// isConstantOne - Return true only if val is constant int 1. |
| 90 | static bool isConstantOne(Value *val) { |
| 91 | return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne(); |
| 92 | } |
| 93 | |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame^] | 94 | static Value *isArrayMallocHelper(const CallInst *CI, const TargetData *TD) { |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 95 | if (!CI) |
Victor Hernandez | 90f48e7 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 96 | return NULL; |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 97 | |
Victor Hernandez | df98761 | 2009-11-06 01:33:24 +0000 | [diff] [blame] | 98 | // Type must be known to determine array size. |
Victor Hernandez | 88efeae | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 99 | const Type *T = getMallocAllocatedType(CI); |
Victor Hernandez | df98761 | 2009-11-06 01:33:24 +0000 | [diff] [blame] | 100 | if (!T) |
Victor Hernandez | 90f48e7 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 101 | return NULL; |
Victor Hernandez | 88d9839 | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 102 | |
Victor Hernandez | 88efeae | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 103 | Value *MallocArg = CI->getOperand(1); |
| 104 | ConstantExpr *CO = dyn_cast<ConstantExpr>(MallocArg); |
| 105 | BinaryOperator *BO = dyn_cast<BinaryOperator>(MallocArg); |
Victor Hernandez | 90f48e7 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 106 | |
Victor Hernandez | df98761 | 2009-11-06 01:33:24 +0000 | [diff] [blame] | 107 | Constant *ElementSize = ConstantExpr::getSizeOf(T); |
| 108 | ElementSize = ConstantExpr::getTruncOrBitCast(ElementSize, |
| 109 | MallocArg->getType()); |
| 110 | Constant *FoldedElementSize = |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame^] | 111 | ConstantFoldConstantExpression(cast<ConstantExpr>(ElementSize), TD); |
Victor Hernandez | 88d9839 | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 112 | |
Victor Hernandez | 90f48e7 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 113 | // First, check if CI is a non-array malloc. |
Victor Hernandez | df98761 | 2009-11-06 01:33:24 +0000 | [diff] [blame] | 114 | if (CO && ((CO == ElementSize) || |
| 115 | (FoldedElementSize && (CO == FoldedElementSize)))) |
Victor Hernandez | 90f48e7 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 116 | // Match CreateMalloc's use of constant 1 array-size for non-array mallocs. |
Victor Hernandez | df98761 | 2009-11-06 01:33:24 +0000 | [diff] [blame] | 117 | return ConstantInt::get(MallocArg->getType(), 1); |
Victor Hernandez | 88d9839 | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 118 | |
Victor Hernandez | 90f48e7 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 119 | // Second, check if CI is an array malloc whose array size can be determined. |
Victor Hernandez | df98761 | 2009-11-06 01:33:24 +0000 | [diff] [blame] | 120 | if (isConstantOne(ElementSize) || |
| 121 | (FoldedElementSize && isConstantOne(FoldedElementSize))) |
Victor Hernandez | 90f48e7 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 122 | return MallocArg; |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 123 | |
Victor Hernandez | 90f48e7 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 124 | if (!CO && !BO) |
| 125 | return NULL; |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 126 | |
Victor Hernandez | 88efeae | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 127 | Value *Op0 = NULL; |
| 128 | Value *Op1 = NULL; |
Victor Hernandez | 90f48e7 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 129 | unsigned Opcode = 0; |
Victor Hernandez | df98761 | 2009-11-06 01:33:24 +0000 | [diff] [blame] | 130 | if (CO && ((CO->getOpcode() == Instruction::Mul) || |
Victor Hernandez | 90f48e7 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 131 | (CO->getOpcode() == Instruction::Shl))) { |
| 132 | Op0 = CO->getOperand(0); |
| 133 | Op1 = CO->getOperand(1); |
| 134 | Opcode = CO->getOpcode(); |
| 135 | } |
Victor Hernandez | df98761 | 2009-11-06 01:33:24 +0000 | [diff] [blame] | 136 | if (BO && ((BO->getOpcode() == Instruction::Mul) || |
Victor Hernandez | 90f48e7 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 137 | (BO->getOpcode() == Instruction::Shl))) { |
| 138 | Op0 = BO->getOperand(0); |
| 139 | Op1 = BO->getOperand(1); |
| 140 | Opcode = BO->getOpcode(); |
| 141 | } |
Victor Hernandez | 88d9839 | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 142 | |
Victor Hernandez | 90f48e7 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 143 | // Determine array size if malloc's argument is the product of a mul or shl. |
| 144 | if (Op0) { |
| 145 | if (Opcode == Instruction::Mul) { |
Victor Hernandez | df98761 | 2009-11-06 01:33:24 +0000 | [diff] [blame] | 146 | if ((Op1 == ElementSize) || |
| 147 | (FoldedElementSize && (Op1 == FoldedElementSize))) |
Victor Hernandez | 90f48e7 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 148 | // ArraySize * ElementSize |
| 149 | return Op0; |
Victor Hernandez | df98761 | 2009-11-06 01:33:24 +0000 | [diff] [blame] | 150 | if ((Op0 == ElementSize) || |
| 151 | (FoldedElementSize && (Op0 == FoldedElementSize))) |
Victor Hernandez | 90f48e7 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 152 | // ElementSize * ArraySize |
| 153 | return Op1; |
| 154 | } |
| 155 | if (Opcode == Instruction::Shl) { |
Victor Hernandez | 88efeae | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 156 | ConstantInt *Op1CI = dyn_cast<ConstantInt>(Op1); |
Victor Hernandez | bc117b8 | 2009-11-02 18:51:28 +0000 | [diff] [blame] | 157 | if (!Op1CI) return NULL; |
| 158 | |
| 159 | APInt Op1Int = Op1CI->getValue(); |
Victor Hernandez | 88efeae | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 160 | uint64_t BitToSet = Op1Int.getLimitedValue(Op1Int.getBitWidth() - 1); |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame^] | 161 | Value *Op1Pow = ConstantInt::get(Op1CI->getContext(), |
Victor Hernandez | 88efeae | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 162 | APInt(Op1Int.getBitWidth(), 0).set(BitToSet)); |
Victor Hernandez | df98761 | 2009-11-06 01:33:24 +0000 | [diff] [blame] | 163 | if (Op0 == ElementSize || (FoldedElementSize && Op0 == FoldedElementSize)) |
Victor Hernandez | 90f48e7 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 164 | // ArraySize << log2(ElementSize) |
| 165 | return Op1Pow; |
Victor Hernandez | df98761 | 2009-11-06 01:33:24 +0000 | [diff] [blame] | 166 | if (Op1Pow == ElementSize || |
| 167 | (FoldedElementSize && Op1Pow == FoldedElementSize)) |
Victor Hernandez | 90f48e7 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 168 | // ElementSize << log2(ArraySize) |
| 169 | return Op0; |
| 170 | } |
| 171 | } |
Victor Hernandez | 88d9839 | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 172 | |
Victor Hernandez | 90f48e7 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 173 | // We could not determine the malloc array size from MallocArg. |
| 174 | return NULL; |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /// isArrayMalloc - Returns the corresponding CallInst if the instruction |
Victor Hernandez | 90f48e7 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 178 | /// is a call to malloc whose array size can be determined and the array size |
| 179 | /// is not constant 1. Otherwise, return NULL. |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame^] | 180 | CallInst *llvm::isArrayMalloc(Value *I, const TargetData *TD) { |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 181 | CallInst *CI = extractMallocCall(I); |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame^] | 182 | Value *ArraySize = isArrayMallocHelper(CI, TD); |
Victor Hernandez | 90f48e7 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 183 | |
| 184 | if (ArraySize && |
| 185 | ArraySize != ConstantInt::get(CI->getOperand(1)->getType(), 1)) |
| 186 | return CI; |
| 187 | |
| 188 | // CI is a non-array malloc or we can't figure out that it is an array malloc. |
| 189 | return NULL; |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame^] | 192 | const CallInst *llvm::isArrayMalloc(const Value *I, const TargetData *TD) { |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 193 | const CallInst *CI = extractMallocCall(I); |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame^] | 194 | Value *ArraySize = isArrayMallocHelper(CI, TD); |
Victor Hernandez | 90f48e7 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 195 | |
| 196 | if (ArraySize && |
| 197 | ArraySize != ConstantInt::get(CI->getOperand(1)->getType(), 1)) |
| 198 | return CI; |
| 199 | |
| 200 | // CI is a non-array malloc or we can't figure out that it is an array malloc. |
| 201 | return NULL; |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /// getMallocType - Returns the PointerType resulting from the malloc call. |
Victor Hernandez | df98761 | 2009-11-06 01:33:24 +0000 | [diff] [blame] | 205 | /// This PointerType is the result type of the call's only bitcast use. |
| 206 | /// If there is no unique bitcast use, then return NULL. |
Victor Hernandez | 88efeae | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 207 | const PointerType *llvm::getMallocType(const CallInst *CI) { |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 208 | assert(isMalloc(CI) && "GetMallocType and not malloc call"); |
| 209 | |
Victor Hernandez | df98761 | 2009-11-06 01:33:24 +0000 | [diff] [blame] | 210 | const BitCastInst *BCI = NULL; |
| 211 | |
Victor Hernandez | 88d9839 | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 212 | // Determine if CallInst has a bitcast use. |
| 213 | for (Value::use_const_iterator UI = CI->use_begin(), E = CI->use_end(); |
| 214 | UI != E; ) |
Victor Hernandez | df98761 | 2009-11-06 01:33:24 +0000 | [diff] [blame] | 215 | if ((BCI = dyn_cast<BitCastInst>(cast<Instruction>(*UI++)))) |
| 216 | break; |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 217 | |
Victor Hernandez | df98761 | 2009-11-06 01:33:24 +0000 | [diff] [blame] | 218 | // Malloc call has 1 bitcast use and no other uses, so type is the bitcast's |
| 219 | // destination type. |
| 220 | if (BCI && CI->hasOneUse()) |
| 221 | return cast<PointerType>(BCI->getDestTy()); |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 222 | |
Victor Hernandez | 60cfc03 | 2009-09-22 18:50:03 +0000 | [diff] [blame] | 223 | // Malloc call was not bitcast, so type is the malloc function's return type. |
Victor Hernandez | df98761 | 2009-11-06 01:33:24 +0000 | [diff] [blame] | 224 | if (!BCI) |
Victor Hernandez | 88d9839 | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 225 | return cast<PointerType>(CI->getType()); |
| 226 | |
| 227 | // Type could not be determined. |
| 228 | return NULL; |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Victor Hernandez | df98761 | 2009-11-06 01:33:24 +0000 | [diff] [blame] | 231 | /// getMallocAllocatedType - Returns the Type allocated by malloc call. This |
| 232 | /// Type is the result type of the call's only bitcast use. If there is no |
| 233 | /// unique bitcast use, then return NULL. |
Victor Hernandez | 88efeae | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 234 | const Type *llvm::getMallocAllocatedType(const CallInst *CI) { |
| 235 | const PointerType *PT = getMallocType(CI); |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 236 | return PT ? PT->getElementType() : NULL; |
| 237 | } |
| 238 | |
Victor Hernandez | 90f48e7 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 239 | /// getMallocArraySize - Returns the array size of a malloc call. If the |
| 240 | /// argument passed to malloc is a multiple of the size of the malloced type, |
| 241 | /// then return that multiple. For non-array mallocs, the multiple is |
| 242 | /// constant 1. Otherwise, return NULL for mallocs whose array size cannot be |
Victor Hernandez | 2491ce0 | 2009-10-15 20:14:52 +0000 | [diff] [blame] | 243 | /// determined. |
Chris Lattner | 7b550cc | 2009-11-06 04:27:31 +0000 | [diff] [blame^] | 244 | Value *llvm::getMallocArraySize(CallInst *CI, const TargetData *TD) { |
| 245 | return isArrayMallocHelper(CI, TD); |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 246 | } |
Victor Hernandez | 66284e0 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 247 | |
Victor Hernandez | 046e78c | 2009-10-26 23:43:48 +0000 | [diff] [blame] | 248 | //===----------------------------------------------------------------------===// |
| 249 | // free Call Utility Functions. |
| 250 | // |
| 251 | |
Victor Hernandez | 66284e0 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 252 | /// 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] | 253 | bool llvm::isFreeCall(const Value *I) { |
Victor Hernandez | 66284e0 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 254 | const CallInst *CI = dyn_cast<CallInst>(I); |
| 255 | if (!CI) |
| 256 | return false; |
Victor Hernandez | 3ad70d5 | 2009-11-03 20:39:35 +0000 | [diff] [blame] | 257 | Function *Callee = CI->getCalledFunction(); |
| 258 | if (Callee == 0 || !Callee->isDeclaration() || Callee->getName() != "free") |
Victor Hernandez | 66284e0 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 259 | return false; |
| 260 | |
| 261 | // Check free prototype. |
| 262 | // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin |
| 263 | // attribute will exist. |
Victor Hernandez | 3ad70d5 | 2009-11-03 20:39:35 +0000 | [diff] [blame] | 264 | const FunctionType *FTy = Callee->getFunctionType(); |
| 265 | if (!FTy->getReturnType()->isVoidTy()) |
Victor Hernandez | 66284e0 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 266 | return false; |
| 267 | if (FTy->getNumParams() != 1) |
| 268 | return false; |
Victor Hernandez | 3ad70d5 | 2009-11-03 20:39:35 +0000 | [diff] [blame] | 269 | if (FTy->param_begin()->get() != Type::getInt8PtrTy(Callee->getContext())) |
Victor Hernandez | 66284e0 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 270 | return false; |
| 271 | |
| 272 | return true; |
| 273 | } |