Victor Hernandez | 28f4d2f | 2009-10-27 20:05:49 +0000 | [diff] [blame] | 1 | //===------ MemoryBuiltins.cpp - Identify calls to memory builtins --------===// |
Evan Cheng | e540646 | 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 | 28f4d2f | 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 | e540646 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Victor Hernandez | 28f4d2f | 2009-10-27 20:05:49 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/MemoryBuiltins.h" |
Evan Cheng | e540646 | 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 | 811ebe0 | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/ConstantFolding.h" |
Victor Hernandez | e67ebab | 2009-11-05 00:03:03 +0000 | [diff] [blame] | 20 | #include "llvm/Target/TargetData.h" |
Evan Cheng | e540646 | 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 | d3406e9 | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 29 | bool llvm::isMalloc(const Value *I) { |
Evan Cheng | e540646 | 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 | fb4ea01 | 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 | e540646 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 39 | return false; |
| 40 | |
Edwin Török | 72c9ef4 | 2009-10-05 21:15:43 +0000 | [diff] [blame] | 41 | // Check malloc prototype. |
Edwin Török | 1d7579c | 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 | fb4ea01 | 2009-11-03 20:39:35 +0000 | [diff] [blame] | 44 | const FunctionType *FTy = Callee->getFunctionType(); |
Edwin Török | 72c9ef4 | 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 | e540646 | 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 | d3406e9 | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 59 | const CallInst *llvm::extractMallocCall(const Value *I) { |
Evan Cheng | e540646 | 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 | d3406e9 | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 64 | CallInst *llvm::extractMallocCall(Value *I) { |
Evan Cheng | e540646 | 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 | d3406e9 | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 69 | static bool isBitCastOfMallocCall(const BitCastInst *BCI) { |
Evan Cheng | e540646 | 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 | d3406e9 | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 78 | CallInst *llvm::extractMallocCallFromBitCast(Value *I) { |
Evan Cheng | e540646 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 79 | BitCastInst *BCI = dyn_cast<BitCastInst>(I); |
Victor Hernandez | 546b28b | 2009-09-10 20:18:57 +0000 | [diff] [blame] | 80 | return (isBitCastOfMallocCall(BCI)) ? cast<CallInst>(BCI->getOperand(0)) |
| 81 | : NULL; |
Evan Cheng | e540646 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Victor Hernandez | d3406e9 | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 84 | const CallInst *llvm::extractMallocCallFromBitCast(const Value *I) { |
Evan Cheng | e540646 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 85 | const BitCastInst *BCI = dyn_cast<BitCastInst>(I); |
Victor Hernandez | 546b28b | 2009-09-10 20:18:57 +0000 | [diff] [blame] | 86 | return (isBitCastOfMallocCall(BCI)) ? cast<CallInst>(BCI->getOperand(0)) |
| 87 | : NULL; |
Evan Cheng | e540646 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Victor Hernandez | 9d632b6 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 90 | /// isConstantOne - Return true only if val is constant int 1. |
| 91 | static bool isConstantOne(Value *val) { |
| 92 | return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne(); |
| 93 | } |
| 94 | |
Victor Hernandez | d3406e9 | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 95 | static Value *isArrayMallocHelper(const CallInst *CI, LLVMContext &Context, |
| 96 | const TargetData *TD) { |
Evan Cheng | e540646 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 97 | if (!CI) |
Victor Hernandez | 9d632b6 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 98 | return NULL; |
Evan Cheng | e540646 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 99 | |
Victor Hernandez | e67ebab | 2009-11-05 00:03:03 +0000 | [diff] [blame] | 100 | // The size of the malloc's result type must be known to determine array size. |
Victor Hernandez | d3406e9 | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 101 | const Type *T = getMallocAllocatedType(CI); |
Victor Hernandez | e67ebab | 2009-11-05 00:03:03 +0000 | [diff] [blame] | 102 | if (!T || !T->isSized() || !TD) |
Victor Hernandez | 9d632b6 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 103 | return NULL; |
Victor Hernandez | 811ebe0 | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 104 | |
Victor Hernandez | d3406e9 | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 105 | Value *MallocArg = CI->getOperand(1); |
Victor Hernandez | e67ebab | 2009-11-05 00:03:03 +0000 | [diff] [blame] | 106 | const Type *ArgType = MallocArg->getType(); |
Victor Hernandez | d3406e9 | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 107 | ConstantExpr *CO = dyn_cast<ConstantExpr>(MallocArg); |
| 108 | BinaryOperator *BO = dyn_cast<BinaryOperator>(MallocArg); |
Victor Hernandez | 9d632b6 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 109 | |
Victor Hernandez | e67ebab | 2009-11-05 00:03:03 +0000 | [diff] [blame] | 110 | unsigned ElementSizeInt = TD->getTypeAllocSize(T); |
| 111 | if (const StructType *ST = dyn_cast<StructType>(T)) |
| 112 | ElementSizeInt = TD->getStructLayout(ST)->getSizeInBytes(); |
| 113 | Constant *ElementSize = ConstantInt::get(ArgType, ElementSizeInt); |
Victor Hernandez | 811ebe0 | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 114 | |
Victor Hernandez | 9d632b6 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 115 | // First, check if CI is a non-array malloc. |
Victor Hernandez | e67ebab | 2009-11-05 00:03:03 +0000 | [diff] [blame] | 116 | if (CO && CO == ElementSize) |
Victor Hernandez | 9d632b6 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 117 | // Match CreateMalloc's use of constant 1 array-size for non-array mallocs. |
Victor Hernandez | e67ebab | 2009-11-05 00:03:03 +0000 | [diff] [blame] | 118 | return ConstantInt::get(ArgType, 1); |
Victor Hernandez | 811ebe0 | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 119 | |
Victor Hernandez | 9d632b6 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 120 | // Second, check if CI is an array malloc whose array size can be determined. |
Victor Hernandez | e67ebab | 2009-11-05 00:03:03 +0000 | [diff] [blame] | 121 | if (isConstantOne(ElementSize)) |
Victor Hernandez | 9d632b6 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 122 | return MallocArg; |
Evan Cheng | e540646 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 123 | |
Victor Hernandez | e67ebab | 2009-11-05 00:03:03 +0000 | [diff] [blame] | 124 | if (ConstantInt *CInt = dyn_cast<ConstantInt>(MallocArg)) |
| 125 | if (CInt->getZExtValue() % ElementSizeInt == 0) |
| 126 | return ConstantInt::get(ArgType, CInt->getZExtValue() / ElementSizeInt); |
| 127 | |
Victor Hernandez | 9d632b6 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 128 | if (!CO && !BO) |
| 129 | return NULL; |
Evan Cheng | e540646 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 130 | |
Victor Hernandez | d3406e9 | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 131 | Value *Op0 = NULL; |
| 132 | Value *Op1 = NULL; |
Victor Hernandez | 9d632b6 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 133 | unsigned Opcode = 0; |
Victor Hernandez | e67ebab | 2009-11-05 00:03:03 +0000 | [diff] [blame] | 134 | if (CO && ((CO->getOpcode() == Instruction::Mul) || |
Victor Hernandez | 9d632b6 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 135 | (CO->getOpcode() == Instruction::Shl))) { |
| 136 | Op0 = CO->getOperand(0); |
| 137 | Op1 = CO->getOperand(1); |
| 138 | Opcode = CO->getOpcode(); |
| 139 | } |
Victor Hernandez | e67ebab | 2009-11-05 00:03:03 +0000 | [diff] [blame] | 140 | if (BO && ((BO->getOpcode() == Instruction::Mul) || |
Victor Hernandez | 9d632b6 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 141 | (BO->getOpcode() == Instruction::Shl))) { |
| 142 | Op0 = BO->getOperand(0); |
| 143 | Op1 = BO->getOperand(1); |
| 144 | Opcode = BO->getOpcode(); |
| 145 | } |
Victor Hernandez | 811ebe0 | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 146 | |
Victor Hernandez | 9d632b6 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 147 | // Determine array size if malloc's argument is the product of a mul or shl. |
| 148 | if (Op0) { |
| 149 | if (Opcode == Instruction::Mul) { |
Victor Hernandez | e67ebab | 2009-11-05 00:03:03 +0000 | [diff] [blame] | 150 | if (Op1 == ElementSize) |
Victor Hernandez | 9d632b6 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 151 | // ArraySize * ElementSize |
| 152 | return Op0; |
Victor Hernandez | e67ebab | 2009-11-05 00:03:03 +0000 | [diff] [blame] | 153 | if (Op0 == ElementSize) |
Victor Hernandez | 9d632b6 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 154 | // ElementSize * ArraySize |
| 155 | return Op1; |
| 156 | } |
| 157 | if (Opcode == Instruction::Shl) { |
Victor Hernandez | d3406e9 | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 158 | ConstantInt *Op1CI = dyn_cast<ConstantInt>(Op1); |
Victor Hernandez | 529fbeb | 2009-11-02 18:51:28 +0000 | [diff] [blame] | 159 | if (!Op1CI) return NULL; |
| 160 | |
| 161 | APInt Op1Int = Op1CI->getValue(); |
Victor Hernandez | d3406e9 | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 162 | uint64_t BitToSet = Op1Int.getLimitedValue(Op1Int.getBitWidth() - 1); |
| 163 | Value *Op1Pow = ConstantInt::get(Context, |
| 164 | APInt(Op1Int.getBitWidth(), 0).set(BitToSet)); |
Victor Hernandez | e67ebab | 2009-11-05 00:03:03 +0000 | [diff] [blame] | 165 | if (Op0 == ElementSize) |
Victor Hernandez | 9d632b6 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 166 | // ArraySize << log2(ElementSize) |
| 167 | return Op1Pow; |
Victor Hernandez | e67ebab | 2009-11-05 00:03:03 +0000 | [diff] [blame] | 168 | if (Op1Pow == ElementSize) |
Victor Hernandez | 9d632b6 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 169 | // ElementSize << log2(ArraySize) |
| 170 | return Op0; |
| 171 | } |
| 172 | } |
Victor Hernandez | 811ebe0 | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 173 | |
Victor Hernandez | 9d632b6 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 174 | // We could not determine the malloc array size from MallocArg. |
| 175 | return NULL; |
Evan Cheng | e540646 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /// isArrayMalloc - Returns the corresponding CallInst if the instruction |
Victor Hernandez | 9d632b6 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 179 | /// is a call to malloc whose array size can be determined and the array size |
| 180 | /// is not constant 1. Otherwise, return NULL. |
Victor Hernandez | d3406e9 | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 181 | CallInst *llvm::isArrayMalloc(Value *I, LLVMContext &Context, |
| 182 | const TargetData *TD) { |
Evan Cheng | e540646 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 183 | CallInst *CI = extractMallocCall(I); |
Victor Hernandez | d3406e9 | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 184 | Value *ArraySize = isArrayMallocHelper(CI, Context, TD); |
Victor Hernandez | 9d632b6 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 185 | |
| 186 | if (ArraySize && |
| 187 | ArraySize != ConstantInt::get(CI->getOperand(1)->getType(), 1)) |
| 188 | return CI; |
| 189 | |
| 190 | // CI is a non-array malloc or we can't figure out that it is an array malloc. |
| 191 | return NULL; |
Evan Cheng | e540646 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Victor Hernandez | d3406e9 | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 194 | const CallInst *llvm::isArrayMalloc(const Value *I, LLVMContext &Context, |
| 195 | const TargetData *TD) { |
Evan Cheng | e540646 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 196 | const CallInst *CI = extractMallocCall(I); |
Victor Hernandez | d3406e9 | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 197 | Value *ArraySize = isArrayMallocHelper(CI, Context, TD); |
Victor Hernandez | 9d632b6 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 198 | |
| 199 | if (ArraySize && |
| 200 | ArraySize != ConstantInt::get(CI->getOperand(1)->getType(), 1)) |
| 201 | return CI; |
| 202 | |
| 203 | // CI is a non-array malloc or we can't figure out that it is an array malloc. |
| 204 | return NULL; |
Evan Cheng | e540646 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | /// getMallocType - Returns the PointerType resulting from the malloc call. |
Victor Hernandez | e67ebab | 2009-11-05 00:03:03 +0000 | [diff] [blame] | 208 | /// The PointerType depends on the number of bitcast uses of the malloc call: |
| 209 | /// 0: PointerType is the calls' return type. |
| 210 | /// 1: PointerType is the bitcast's result type. |
| 211 | /// >1: Unique PointerType cannot be determined, return NULL. |
Victor Hernandez | d3406e9 | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 212 | const PointerType *llvm::getMallocType(const CallInst *CI) { |
Evan Cheng | e540646 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 213 | assert(isMalloc(CI) && "GetMallocType and not malloc call"); |
| 214 | |
Victor Hernandez | e67ebab | 2009-11-05 00:03:03 +0000 | [diff] [blame] | 215 | const PointerType *MallocType = NULL; |
| 216 | unsigned NumOfBitCastUses = 0; |
| 217 | |
Victor Hernandez | 811ebe0 | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 218 | // Determine if CallInst has a bitcast use. |
| 219 | for (Value::use_const_iterator UI = CI->use_begin(), E = CI->use_end(); |
| 220 | UI != E; ) |
Victor Hernandez | e67ebab | 2009-11-05 00:03:03 +0000 | [diff] [blame] | 221 | if (const BitCastInst *BCI = dyn_cast<BitCastInst>(*UI++)) { |
| 222 | MallocType = cast<PointerType>(BCI->getDestTy()); |
| 223 | NumOfBitCastUses++; |
| 224 | } |
Evan Cheng | e540646 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 225 | |
Victor Hernandez | e67ebab | 2009-11-05 00:03:03 +0000 | [diff] [blame] | 226 | // Malloc call has 1 bitcast use, so type is the bitcast's destination type. |
| 227 | if (NumOfBitCastUses == 1) |
| 228 | return MallocType; |
Evan Cheng | e540646 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 229 | |
Victor Hernandez | d80fa7f | 2009-09-22 18:50:03 +0000 | [diff] [blame] | 230 | // Malloc call was not bitcast, so type is the malloc function's return type. |
Victor Hernandez | e67ebab | 2009-11-05 00:03:03 +0000 | [diff] [blame] | 231 | if (NumOfBitCastUses == 0) |
Victor Hernandez | 811ebe0 | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 232 | return cast<PointerType>(CI->getType()); |
| 233 | |
| 234 | // Type could not be determined. |
| 235 | return NULL; |
Evan Cheng | e540646 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Victor Hernandez | e67ebab | 2009-11-05 00:03:03 +0000 | [diff] [blame] | 238 | /// getMallocAllocatedType - Returns the Type allocated by malloc call. |
| 239 | /// The Type depends on the number of bitcast uses of the malloc call: |
| 240 | /// 0: PointerType is the malloc calls' return type. |
| 241 | /// 1: PointerType is the bitcast's result type. |
| 242 | /// >1: Unique PointerType cannot be determined, return NULL. |
Victor Hernandez | d3406e9 | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 243 | const Type *llvm::getMallocAllocatedType(const CallInst *CI) { |
| 244 | const PointerType *PT = getMallocType(CI); |
Evan Cheng | e540646 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 245 | return PT ? PT->getElementType() : NULL; |
| 246 | } |
| 247 | |
Victor Hernandez | 9d632b6 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 248 | /// getMallocArraySize - Returns the array size of a malloc call. If the |
| 249 | /// argument passed to malloc is a multiple of the size of the malloced type, |
| 250 | /// then return that multiple. For non-array mallocs, the multiple is |
| 251 | /// constant 1. Otherwise, return NULL for mallocs whose array size cannot be |
Victor Hernandez | 8905891 | 2009-10-15 20:14:52 +0000 | [diff] [blame] | 252 | /// determined. |
Victor Hernandez | d3406e9 | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 253 | Value *llvm::getMallocArraySize(CallInst *CI, LLVMContext &Context, |
| 254 | const TargetData *TD) { |
Victor Hernandez | 9d632b6 | 2009-10-28 20:18:55 +0000 | [diff] [blame] | 255 | return isArrayMallocHelper(CI, Context, TD); |
Evan Cheng | e540646 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 256 | } |
Victor Hernandez | 9394608 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 257 | |
Victor Hernandez | f9a7a33 | 2009-10-26 23:43:48 +0000 | [diff] [blame] | 258 | //===----------------------------------------------------------------------===// |
| 259 | // free Call Utility Functions. |
| 260 | // |
| 261 | |
Victor Hernandez | 9394608 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 262 | /// isFreeCall - Returns true if the the value is a call to the builtin free() |
Victor Hernandez | d3406e9 | 2009-11-03 20:02:35 +0000 | [diff] [blame] | 263 | bool llvm::isFreeCall(const Value *I) { |
Victor Hernandez | 9394608 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 264 | const CallInst *CI = dyn_cast<CallInst>(I); |
| 265 | if (!CI) |
| 266 | return false; |
Victor Hernandez | fb4ea01 | 2009-11-03 20:39:35 +0000 | [diff] [blame] | 267 | Function *Callee = CI->getCalledFunction(); |
| 268 | if (Callee == 0 || !Callee->isDeclaration() || Callee->getName() != "free") |
Victor Hernandez | 9394608 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 269 | return false; |
| 270 | |
| 271 | // Check free prototype. |
| 272 | // FIXME: workaround for PR5130, this will be obsolete when a nobuiltin |
| 273 | // attribute will exist. |
Victor Hernandez | fb4ea01 | 2009-11-03 20:39:35 +0000 | [diff] [blame] | 274 | const FunctionType *FTy = Callee->getFunctionType(); |
| 275 | if (!FTy->getReturnType()->isVoidTy()) |
Victor Hernandez | 9394608 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 276 | return false; |
| 277 | if (FTy->getNumParams() != 1) |
| 278 | return false; |
Victor Hernandez | fb4ea01 | 2009-11-03 20:39:35 +0000 | [diff] [blame] | 279 | if (FTy->param_begin()->get() != Type::getInt8PtrTy(Callee->getContext())) |
Victor Hernandez | 9394608 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 280 | return false; |
| 281 | |
| 282 | return true; |
| 283 | } |