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