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