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