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