Eric Christopher | b6174e3 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 1 | //===- BuildLibCalls.cpp - Utility builder for libcalls -------------------===// |
| 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 | // |
| 10 | // This file implements some functions that will create standard C libcalls. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Transforms/Utils/BuildLibCalls.h" |
| 15 | #include "llvm/Type.h" |
| 16 | #include "llvm/Constants.h" |
| 17 | #include "llvm/Function.h" |
| 18 | #include "llvm/Module.h" |
| 19 | #include "llvm/Support/IRBuilder.h" |
| 20 | #include "llvm/Target/TargetData.h" |
| 21 | #include "llvm/LLVMContext.h" |
| 22 | #include "llvm/Intrinsics.h" |
| 23 | |
| 24 | using namespace llvm; |
| 25 | |
| 26 | /// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*. |
| 27 | Value *llvm::CastToCStr(Value *V, IRBuilder<> &B) { |
| 28 | return B.CreateBitCast(V, B.getInt8PtrTy(), "cstr"); |
| 29 | } |
| 30 | |
| 31 | /// EmitStrLen - Emit a call to the strlen function to the builder, for the |
| 32 | /// specified pointer. This always returns an integer value of size intptr_t. |
| 33 | Value *llvm::EmitStrLen(Value *Ptr, IRBuilder<> &B, const TargetData *TD) { |
| 34 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
| 35 | AttributeWithIndex AWI[2]; |
| 36 | AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture); |
| 37 | AWI[1] = AttributeWithIndex::get(~0u, Attribute::ReadOnly | |
| 38 | Attribute::NoUnwind); |
| 39 | |
| 40 | LLVMContext &Context = B.GetInsertBlock()->getContext(); |
| 41 | Constant *StrLen = M->getOrInsertFunction("strlen", AttrListPtr::get(AWI, 2), |
| 42 | TD->getIntPtrType(Context), |
| 43 | B.getInt8PtrTy(), |
| 44 | NULL); |
| 45 | CallInst *CI = B.CreateCall(StrLen, CastToCStr(Ptr, B), "strlen"); |
| 46 | if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts())) |
| 47 | CI->setCallingConv(F->getCallingConv()); |
| 48 | |
| 49 | return CI; |
| 50 | } |
| 51 | |
| 52 | /// EmitStrChr - Emit a call to the strchr function to the builder, for the |
| 53 | /// specified pointer and character. Ptr is required to be some pointer type, |
| 54 | /// and the return value has 'i8*' type. |
| 55 | Value *llvm::EmitStrChr(Value *Ptr, char C, IRBuilder<> &B, |
| 56 | const TargetData *TD) { |
| 57 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
| 58 | AttributeWithIndex AWI = |
| 59 | AttributeWithIndex::get(~0u, Attribute::ReadOnly | Attribute::NoUnwind); |
| 60 | |
| 61 | const Type *I8Ptr = B.getInt8PtrTy(); |
| 62 | const Type *I32Ty = B.getInt32Ty(); |
| 63 | Constant *StrChr = M->getOrInsertFunction("strchr", AttrListPtr::get(&AWI, 1), |
| 64 | I8Ptr, I8Ptr, I32Ty, NULL); |
| 65 | CallInst *CI = B.CreateCall2(StrChr, CastToCStr(Ptr, B), |
| 66 | ConstantInt::get(I32Ty, C), "strchr"); |
| 67 | if (const Function *F = dyn_cast<Function>(StrChr->stripPointerCasts())) |
| 68 | CI->setCallingConv(F->getCallingConv()); |
| 69 | return CI; |
| 70 | } |
| 71 | |
| 72 | /// EmitStrCpy - Emit a call to the strcpy function to the builder, for the |
| 73 | /// specified pointer arguments. |
| 74 | Value *llvm::EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B, |
Benjamin Kramer | 7fa30b8 | 2010-03-11 20:45:13 +0000 | [diff] [blame] | 75 | const TargetData *TD, StringRef Name) { |
Eric Christopher | b6174e3 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 76 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
| 77 | AttributeWithIndex AWI[2]; |
| 78 | AWI[0] = AttributeWithIndex::get(2, Attribute::NoCapture); |
| 79 | AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind); |
| 80 | const Type *I8Ptr = B.getInt8PtrTy(); |
Benjamin Kramer | 7fa30b8 | 2010-03-11 20:45:13 +0000 | [diff] [blame] | 81 | Value *StrCpy = M->getOrInsertFunction(Name, AttrListPtr::get(AWI, 2), |
Eric Christopher | b6174e3 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 82 | I8Ptr, I8Ptr, I8Ptr, NULL); |
| 83 | CallInst *CI = B.CreateCall2(StrCpy, CastToCStr(Dst, B), CastToCStr(Src, B), |
Benjamin Kramer | 7fa30b8 | 2010-03-11 20:45:13 +0000 | [diff] [blame] | 84 | Name); |
Eric Christopher | b6174e3 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 85 | if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts())) |
| 86 | CI->setCallingConv(F->getCallingConv()); |
| 87 | return CI; |
| 88 | } |
| 89 | |
Eric Christopher | b0722af | 2010-03-11 17:45:38 +0000 | [diff] [blame] | 90 | /// EmitStrNCpy - Emit a call to the strncpy function to the builder, for the |
Eric Christopher | bd97376 | 2010-03-11 01:25:07 +0000 | [diff] [blame] | 91 | /// specified pointer arguments. |
| 92 | Value *llvm::EmitStrNCpy(Value *Dst, Value *Src, Value *Len, |
Eric Christopher | 71988f1 | 2010-04-07 23:00:07 +0000 | [diff] [blame] | 93 | IRBuilder<> &B, const TargetData *TD, StringRef Name) { |
Eric Christopher | bd97376 | 2010-03-11 01:25:07 +0000 | [diff] [blame] | 94 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
| 95 | AttributeWithIndex AWI[2]; |
| 96 | AWI[0] = AttributeWithIndex::get(2, Attribute::NoCapture); |
| 97 | AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind); |
| 98 | const Type *I8Ptr = B.getInt8PtrTy(); |
Eric Christopher | 71988f1 | 2010-04-07 23:00:07 +0000 | [diff] [blame] | 99 | Value *StrNCpy = M->getOrInsertFunction(Name, AttrListPtr::get(AWI, 2), |
| 100 | I8Ptr, I8Ptr, I8Ptr, |
| 101 | Len->getType(), NULL); |
Eric Christopher | bd97376 | 2010-03-11 01:25:07 +0000 | [diff] [blame] | 102 | CallInst *CI = B.CreateCall3(StrNCpy, CastToCStr(Dst, B), CastToCStr(Src, B), |
| 103 | Len, "strncpy"); |
| 104 | if (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts())) |
| 105 | CI->setCallingConv(F->getCallingConv()); |
| 106 | return CI; |
| 107 | } |
| 108 | |
| 109 | |
Eric Christopher | b6174e3 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 110 | /// EmitMemCpy - Emit a call to the memcpy function to the builder. This always |
Evan Cheng | 0289b41 | 2010-03-23 15:48:04 +0000 | [diff] [blame] | 111 | /// expects that Len has type 'intptr_t' and Dst/Src are pointers. |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 112 | Value *llvm::EmitMemCpy(Value *Dst, Value *Src, Value *Len, unsigned Align, |
| 113 | bool isVolatile, IRBuilder<> &B, const TargetData *TD) { |
Eric Christopher | b6174e3 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 114 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 115 | const Type *ArgTys[3] = { Dst->getType(), Src->getType(), Len->getType() }; |
| 116 | Value *MemCpy = Intrinsic::getDeclaration(M, Intrinsic::memcpy, ArgTys, 3); |
Eric Christopher | b6174e3 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 117 | Dst = CastToCStr(Dst, B); |
| 118 | Src = CastToCStr(Src, B); |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 119 | return B.CreateCall5(MemCpy, Dst, Src, Len, |
| 120 | ConstantInt::get(B.getInt32Ty(), Align), |
| 121 | ConstantInt::get(B.getInt1Ty(), isVolatile)); |
Eric Christopher | b6174e3 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Evan Cheng | 0289b41 | 2010-03-23 15:48:04 +0000 | [diff] [blame] | 124 | /// EmitMemCpyChk - Emit a call to the __memcpy_chk function to the builder. |
| 125 | /// This expects that the Len and ObjSize have type 'intptr_t' and Dst/Src |
| 126 | /// are pointers. |
| 127 | Value *llvm::EmitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize, |
| 128 | IRBuilder<> &B, const TargetData *TD) { |
| 129 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
| 130 | AttributeWithIndex AWI; |
| 131 | AWI = AttributeWithIndex::get(~0u, Attribute::NoUnwind); |
| 132 | LLVMContext &Context = B.GetInsertBlock()->getContext(); |
| 133 | Value *MemCpy = M->getOrInsertFunction("__memcpy_chk", |
| 134 | AttrListPtr::get(&AWI, 1), |
| 135 | B.getInt8PtrTy(), |
| 136 | B.getInt8PtrTy(), |
| 137 | B.getInt8PtrTy(), |
| 138 | TD->getIntPtrType(Context), |
| 139 | TD->getIntPtrType(Context), NULL); |
| 140 | Dst = CastToCStr(Dst, B); |
| 141 | Src = CastToCStr(Src, B); |
| 142 | CallInst *CI = B.CreateCall4(MemCpy, Dst, Src, Len, ObjSize); |
| 143 | if (const Function *F = dyn_cast<Function>(MemCpy->stripPointerCasts())) |
| 144 | CI->setCallingConv(F->getCallingConv()); |
| 145 | return CI; |
| 146 | } |
| 147 | |
Eric Christopher | b6174e3 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 148 | /// EmitMemMove - Emit a call to the memmove function to the builder. This |
| 149 | /// always expects that the size has type 'intptr_t' and Dst/Src are pointers. |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 150 | Value *llvm::EmitMemMove(Value *Dst, Value *Src, Value *Len, unsigned Align, |
| 151 | bool isVolatile, IRBuilder<> &B, const TargetData *TD) { |
Eric Christopher | b6174e3 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 152 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
| 153 | LLVMContext &Context = B.GetInsertBlock()->getContext(); |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 154 | const Type *ArgTys[3] = { Dst->getType(), Src->getType(), |
| 155 | TD->getIntPtrType(Context) }; |
| 156 | Value *MemMove = Intrinsic::getDeclaration(M, Intrinsic::memmove, ArgTys, 3); |
Eric Christopher | b6174e3 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 157 | Dst = CastToCStr(Dst, B); |
| 158 | Src = CastToCStr(Src, B); |
| 159 | Value *A = ConstantInt::get(B.getInt32Ty(), Align); |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 160 | Value *Vol = ConstantInt::get(B.getInt1Ty(), isVolatile); |
| 161 | return B.CreateCall5(MemMove, Dst, Src, Len, A, Vol); |
Eric Christopher | b6174e3 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is |
| 165 | /// a pointer, Val is an i32 value, and Len is an 'intptr_t' value. |
| 166 | Value *llvm::EmitMemChr(Value *Ptr, Value *Val, |
| 167 | Value *Len, IRBuilder<> &B, const TargetData *TD) { |
| 168 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
| 169 | AttributeWithIndex AWI; |
| 170 | AWI = AttributeWithIndex::get(~0u, Attribute::ReadOnly | Attribute::NoUnwind); |
| 171 | LLVMContext &Context = B.GetInsertBlock()->getContext(); |
| 172 | Value *MemChr = M->getOrInsertFunction("memchr", AttrListPtr::get(&AWI, 1), |
| 173 | B.getInt8PtrTy(), |
| 174 | B.getInt8PtrTy(), |
| 175 | B.getInt32Ty(), |
| 176 | TD->getIntPtrType(Context), |
| 177 | NULL); |
| 178 | CallInst *CI = B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr"); |
| 179 | |
| 180 | if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts())) |
| 181 | CI->setCallingConv(F->getCallingConv()); |
| 182 | |
| 183 | return CI; |
| 184 | } |
| 185 | |
| 186 | /// EmitMemCmp - Emit a call to the memcmp function. |
| 187 | Value *llvm::EmitMemCmp(Value *Ptr1, Value *Ptr2, |
| 188 | Value *Len, IRBuilder<> &B, const TargetData *TD) { |
| 189 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
| 190 | AttributeWithIndex AWI[3]; |
| 191 | AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture); |
| 192 | AWI[1] = AttributeWithIndex::get(2, Attribute::NoCapture); |
| 193 | AWI[2] = AttributeWithIndex::get(~0u, Attribute::ReadOnly | |
| 194 | Attribute::NoUnwind); |
| 195 | |
| 196 | LLVMContext &Context = B.GetInsertBlock()->getContext(); |
| 197 | Value *MemCmp = M->getOrInsertFunction("memcmp", AttrListPtr::get(AWI, 3), |
| 198 | B.getInt32Ty(), |
| 199 | B.getInt8PtrTy(), |
| 200 | B.getInt8PtrTy(), |
| 201 | TD->getIntPtrType(Context), NULL); |
| 202 | CallInst *CI = B.CreateCall3(MemCmp, CastToCStr(Ptr1, B), CastToCStr(Ptr2, B), |
| 203 | Len, "memcmp"); |
| 204 | |
| 205 | if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts())) |
| 206 | CI->setCallingConv(F->getCallingConv()); |
| 207 | |
| 208 | return CI; |
| 209 | } |
| 210 | |
| 211 | /// EmitMemSet - Emit a call to the memset function |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 212 | Value *llvm::EmitMemSet(Value *Dst, Value *Val, Value *Len, bool isVolatile, |
| 213 | IRBuilder<> &B, const TargetData *TD) { |
Eric Christopher | b6174e3 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 214 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
| 215 | Intrinsic::ID IID = Intrinsic::memset; |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 216 | const Type *Tys[2] = { Dst->getType(), Len->getType() }; |
| 217 | Value *MemSet = Intrinsic::getDeclaration(M, IID, Tys, 2); |
Eric Christopher | b6174e3 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 218 | Value *Align = ConstantInt::get(B.getInt32Ty(), 1); |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 219 | Value *Vol = ConstantInt::get(B.getInt1Ty(), isVolatile); |
| 220 | return B.CreateCall5(MemSet, CastToCStr(Dst, B), Val, Len, Align, Vol); |
Eric Christopher | b6174e3 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g. |
| 224 | /// 'floor'). This function is known to take a single of type matching 'Op' and |
| 225 | /// returns one value with the same type. If 'Op' is a long double, 'l' is |
| 226 | /// added as the suffix of name, if 'Op' is a float, we add a 'f' suffix. |
| 227 | Value *llvm::EmitUnaryFloatFnCall(Value *Op, const char *Name, |
| 228 | IRBuilder<> &B, const AttrListPtr &Attrs) { |
| 229 | char NameBuffer[20]; |
| 230 | if (!Op->getType()->isDoubleTy()) { |
| 231 | // If we need to add a suffix, copy into NameBuffer. |
| 232 | unsigned NameLen = strlen(Name); |
| 233 | assert(NameLen < sizeof(NameBuffer)-2); |
| 234 | memcpy(NameBuffer, Name, NameLen); |
| 235 | if (Op->getType()->isFloatTy()) |
| 236 | NameBuffer[NameLen] = 'f'; // floorf |
| 237 | else |
| 238 | NameBuffer[NameLen] = 'l'; // floorl |
| 239 | NameBuffer[NameLen+1] = 0; |
| 240 | Name = NameBuffer; |
| 241 | } |
| 242 | |
| 243 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
| 244 | Value *Callee = M->getOrInsertFunction(Name, Op->getType(), |
| 245 | Op->getType(), NULL); |
| 246 | CallInst *CI = B.CreateCall(Callee, Op, Name); |
| 247 | CI->setAttributes(Attrs); |
| 248 | if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts())) |
| 249 | CI->setCallingConv(F->getCallingConv()); |
| 250 | |
| 251 | return CI; |
| 252 | } |
| 253 | |
| 254 | /// EmitPutChar - Emit a call to the putchar function. This assumes that Char |
| 255 | /// is an integer. |
| 256 | Value *llvm::EmitPutChar(Value *Char, IRBuilder<> &B, const TargetData *TD) { |
| 257 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
| 258 | Value *PutChar = M->getOrInsertFunction("putchar", B.getInt32Ty(), |
| 259 | B.getInt32Ty(), NULL); |
| 260 | CallInst *CI = B.CreateCall(PutChar, |
| 261 | B.CreateIntCast(Char, |
| 262 | B.getInt32Ty(), |
| 263 | /*isSigned*/true, |
| 264 | "chari"), |
| 265 | "putchar"); |
| 266 | |
| 267 | if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts())) |
| 268 | CI->setCallingConv(F->getCallingConv()); |
| 269 | return CI; |
| 270 | } |
| 271 | |
| 272 | /// EmitPutS - Emit a call to the puts function. This assumes that Str is |
| 273 | /// some pointer. |
| 274 | void llvm::EmitPutS(Value *Str, IRBuilder<> &B, const TargetData *TD) { |
| 275 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
| 276 | AttributeWithIndex AWI[2]; |
| 277 | AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture); |
| 278 | AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind); |
| 279 | |
| 280 | Value *PutS = M->getOrInsertFunction("puts", AttrListPtr::get(AWI, 2), |
| 281 | B.getInt32Ty(), |
| 282 | B.getInt8PtrTy(), |
| 283 | NULL); |
| 284 | CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts"); |
| 285 | if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts())) |
| 286 | CI->setCallingConv(F->getCallingConv()); |
| 287 | |
| 288 | } |
| 289 | |
| 290 | /// EmitFPutC - Emit a call to the fputc function. This assumes that Char is |
| 291 | /// an integer and File is a pointer to FILE. |
| 292 | void llvm::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B, |
| 293 | const TargetData *TD) { |
| 294 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
| 295 | AttributeWithIndex AWI[2]; |
| 296 | AWI[0] = AttributeWithIndex::get(2, Attribute::NoCapture); |
| 297 | AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind); |
| 298 | Constant *F; |
| 299 | if (File->getType()->isPointerTy()) |
| 300 | F = M->getOrInsertFunction("fputc", AttrListPtr::get(AWI, 2), |
| 301 | B.getInt32Ty(), |
| 302 | B.getInt32Ty(), File->getType(), |
| 303 | NULL); |
| 304 | else |
| 305 | F = M->getOrInsertFunction("fputc", |
| 306 | B.getInt32Ty(), |
| 307 | B.getInt32Ty(), |
| 308 | File->getType(), NULL); |
| 309 | Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true, |
| 310 | "chari"); |
| 311 | CallInst *CI = B.CreateCall2(F, Char, File, "fputc"); |
| 312 | |
| 313 | if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) |
| 314 | CI->setCallingConv(Fn->getCallingConv()); |
| 315 | } |
| 316 | |
| 317 | /// EmitFPutS - Emit a call to the puts function. Str is required to be a |
| 318 | /// pointer and File is a pointer to FILE. |
| 319 | void llvm::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B, |
| 320 | const TargetData *TD) { |
| 321 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
| 322 | AttributeWithIndex AWI[3]; |
| 323 | AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture); |
| 324 | AWI[1] = AttributeWithIndex::get(2, Attribute::NoCapture); |
| 325 | AWI[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind); |
| 326 | Constant *F; |
| 327 | if (File->getType()->isPointerTy()) |
| 328 | F = M->getOrInsertFunction("fputs", AttrListPtr::get(AWI, 3), |
| 329 | B.getInt32Ty(), |
| 330 | B.getInt8PtrTy(), |
| 331 | File->getType(), NULL); |
| 332 | else |
| 333 | F = M->getOrInsertFunction("fputs", B.getInt32Ty(), |
| 334 | B.getInt8PtrTy(), |
| 335 | File->getType(), NULL); |
| 336 | CallInst *CI = B.CreateCall2(F, CastToCStr(Str, B), File, "fputs"); |
| 337 | |
| 338 | if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) |
| 339 | CI->setCallingConv(Fn->getCallingConv()); |
| 340 | } |
| 341 | |
| 342 | /// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is |
| 343 | /// a pointer, Size is an 'intptr_t', and File is a pointer to FILE. |
| 344 | void llvm::EmitFWrite(Value *Ptr, Value *Size, Value *File, |
| 345 | IRBuilder<> &B, const TargetData *TD) { |
| 346 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
| 347 | AttributeWithIndex AWI[3]; |
| 348 | AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture); |
| 349 | AWI[1] = AttributeWithIndex::get(4, Attribute::NoCapture); |
| 350 | AWI[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind); |
| 351 | LLVMContext &Context = B.GetInsertBlock()->getContext(); |
| 352 | Constant *F; |
| 353 | if (File->getType()->isPointerTy()) |
| 354 | F = M->getOrInsertFunction("fwrite", AttrListPtr::get(AWI, 3), |
| 355 | TD->getIntPtrType(Context), |
| 356 | B.getInt8PtrTy(), |
| 357 | TD->getIntPtrType(Context), |
| 358 | TD->getIntPtrType(Context), |
| 359 | File->getType(), NULL); |
| 360 | else |
| 361 | F = M->getOrInsertFunction("fwrite", TD->getIntPtrType(Context), |
| 362 | B.getInt8PtrTy(), |
| 363 | TD->getIntPtrType(Context), |
| 364 | TD->getIntPtrType(Context), |
| 365 | File->getType(), NULL); |
| 366 | CallInst *CI = B.CreateCall4(F, CastToCStr(Ptr, B), Size, |
| 367 | ConstantInt::get(TD->getIntPtrType(Context), 1), File); |
| 368 | |
| 369 | if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) |
| 370 | CI->setCallingConv(Fn->getCallingConv()); |
| 371 | } |
Benjamin Kramer | 0b6cb50 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 372 | |
Benjamin Kramer | a30b181 | 2010-03-12 20:41:29 +0000 | [diff] [blame] | 373 | SimplifyFortifiedLibCalls::~SimplifyFortifiedLibCalls() { } |
| 374 | |
Benjamin Kramer | 0b6cb50 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 375 | bool SimplifyFortifiedLibCalls::fold(CallInst *CI, const TargetData *TD) { |
Eric Christopher | 67a71b5 | 2010-04-12 04:48:00 +0000 | [diff] [blame] | 376 | // We really need TargetData for later. |
| 377 | if (!TD) return false; |
| 378 | |
Benjamin Kramer | 0b6cb50 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 379 | this->CI = CI; |
Eric Christopher | 67a71b5 | 2010-04-12 04:48:00 +0000 | [diff] [blame] | 380 | Function *Callee = CI->getCalledFunction(); |
| 381 | StringRef Name = Callee->getName(); |
| 382 | const FunctionType *FT = Callee->getFunctionType(); |
Benjamin Kramer | 0b6cb50 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 383 | BasicBlock *BB = CI->getParent(); |
Eric Christopher | 67a71b5 | 2010-04-12 04:48:00 +0000 | [diff] [blame] | 384 | LLVMContext &Context = CI->getParent()->getContext(); |
| 385 | IRBuilder<> B(Context); |
Benjamin Kramer | 0b6cb50 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 386 | |
| 387 | // Set the builder to the instruction after the call. |
| 388 | B.SetInsertPoint(BB, CI); |
| 389 | |
| 390 | if (Name == "__memcpy_chk") { |
Eric Christopher | 67a71b5 | 2010-04-12 04:48:00 +0000 | [diff] [blame] | 391 | // Check if this has the right signature. |
| 392 | if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) || |
| 393 | !FT->getParamType(0)->isPointerTy() || |
| 394 | !FT->getParamType(1)->isPointerTy() || |
| 395 | FT->getParamType(2) != TD->getIntPtrType(Context) || |
| 396 | FT->getParamType(3) != TD->getIntPtrType(Context)) |
| 397 | return false; |
| 398 | |
Benjamin Kramer | 0b6cb50 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 399 | if (isFoldable(4, 3, false)) { |
| 400 | EmitMemCpy(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3), |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 401 | 1, false, B, TD); |
Benjamin Kramer | 0b6cb50 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 402 | replaceCall(CI->getOperand(1)); |
| 403 | return true; |
| 404 | } |
| 405 | return false; |
| 406 | } |
| 407 | |
| 408 | // Should be similar to memcpy. |
| 409 | if (Name == "__mempcpy_chk") { |
| 410 | return false; |
| 411 | } |
| 412 | |
| 413 | if (Name == "__memmove_chk") { |
Eric Christopher | 67a71b5 | 2010-04-12 04:48:00 +0000 | [diff] [blame] | 414 | // Check if this has the right signature. |
| 415 | if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) || |
| 416 | !FT->getParamType(0)->isPointerTy() || |
| 417 | !FT->getParamType(1)->isPointerTy() || |
| 418 | FT->getParamType(2) != TD->getIntPtrType(Context) || |
| 419 | FT->getParamType(3) != TD->getIntPtrType(Context)) |
| 420 | return false; |
| 421 | |
Benjamin Kramer | 0b6cb50 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 422 | if (isFoldable(4, 3, false)) { |
| 423 | EmitMemMove(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3), |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 424 | 1, false, B, TD); |
Benjamin Kramer | 0b6cb50 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 425 | replaceCall(CI->getOperand(1)); |
| 426 | return true; |
| 427 | } |
| 428 | return false; |
| 429 | } |
| 430 | |
| 431 | if (Name == "__memset_chk") { |
Eric Christopher | 67a71b5 | 2010-04-12 04:48:00 +0000 | [diff] [blame] | 432 | // Check if this has the right signature. |
| 433 | if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) || |
| 434 | !FT->getParamType(0)->isPointerTy() || |
| 435 | !FT->getParamType(1)->isIntegerTy() || |
| 436 | FT->getParamType(2) != TD->getIntPtrType(Context) || |
| 437 | FT->getParamType(3) != TD->getIntPtrType(Context)) |
| 438 | return false; |
| 439 | |
Benjamin Kramer | 0b6cb50 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 440 | if (isFoldable(4, 3, false)) { |
| 441 | Value *Val = B.CreateIntCast(CI->getOperand(2), B.getInt8Ty(), |
| 442 | false); |
Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 443 | EmitMemSet(CI->getOperand(1), Val, CI->getOperand(3), false, B, TD); |
Benjamin Kramer | 0b6cb50 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 444 | replaceCall(CI->getOperand(1)); |
| 445 | return true; |
| 446 | } |
| 447 | return false; |
| 448 | } |
| 449 | |
| 450 | if (Name == "__strcpy_chk" || Name == "__stpcpy_chk") { |
Eric Christopher | 67a71b5 | 2010-04-12 04:48:00 +0000 | [diff] [blame] | 451 | // Check if this has the right signature. |
| 452 | if (FT->getNumParams() != 3 || |
| 453 | FT->getReturnType() != FT->getParamType(0) || |
| 454 | FT->getParamType(0) != FT->getParamType(1) || |
| 455 | FT->getParamType(0) != Type::getInt8PtrTy(Context) || |
| 456 | FT->getParamType(2) != TD->getIntPtrType(Context)) |
| 457 | return 0; |
| 458 | |
| 459 | |
Benjamin Kramer | 0b6cb50 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 460 | // If a) we don't have any length information, or b) we know this will |
| 461 | // fit then just lower to a plain st[rp]cpy. Otherwise we'll keep our |
| 462 | // st[rp]cpy_chk call which may fail at runtime if the size is too long. |
| 463 | // TODO: It might be nice to get a maximum length out of the possible |
| 464 | // string lengths for varying. |
| 465 | if (isFoldable(3, 2, true)) { |
| 466 | Value *Ret = EmitStrCpy(CI->getOperand(1), CI->getOperand(2), B, TD, |
| 467 | Name.substr(2, 6)); |
| 468 | replaceCall(Ret); |
| 469 | return true; |
| 470 | } |
| 471 | return false; |
| 472 | } |
| 473 | |
Eric Christopher | 71988f1 | 2010-04-07 23:00:07 +0000 | [diff] [blame] | 474 | if (Name == "__strncpy_chk" || Name == "__stpncpy_chk") { |
Eric Christopher | 67a71b5 | 2010-04-12 04:48:00 +0000 | [diff] [blame] | 475 | // Check if this has the right signature. |
| 476 | if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) || |
| 477 | FT->getParamType(0) != FT->getParamType(1) || |
| 478 | FT->getParamType(0) != Type::getInt8PtrTy(Context) || |
| 479 | !FT->getParamType(2)->isIntegerTy() || |
| 480 | FT->getParamType(3) != TD->getIntPtrType(Context)) |
Eric Christopher | 2a7cb9d | 2010-04-13 16:41:29 +0000 | [diff] [blame^] | 481 | return false; |
Eric Christopher | 67a71b5 | 2010-04-12 04:48:00 +0000 | [diff] [blame] | 482 | |
Benjamin Kramer | 0b6cb50 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 483 | if (isFoldable(4, 3, false)) { |
| 484 | Value *Ret = EmitStrNCpy(CI->getOperand(1), CI->getOperand(2), |
Eric Christopher | 71988f1 | 2010-04-07 23:00:07 +0000 | [diff] [blame] | 485 | CI->getOperand(3), B, TD, Name.substr(2, 7)); |
Benjamin Kramer | 0b6cb50 | 2010-03-12 09:27:41 +0000 | [diff] [blame] | 486 | replaceCall(Ret); |
| 487 | return true; |
| 488 | } |
| 489 | return false; |
| 490 | } |
| 491 | |
| 492 | if (Name == "__strcat_chk") { |
| 493 | return false; |
| 494 | } |
| 495 | |
| 496 | if (Name == "__strncat_chk") { |
| 497 | return false; |
| 498 | } |
| 499 | |
| 500 | return false; |
| 501 | } |