Eric Christopher | 87abfc5 | 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 | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallString.h" |
Chandler Carruth | 9fb823b | 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" |
Chandler Carruth | 62d4215 | 2015-01-15 02:16:27 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Eric Christopher | 87abfc5 | 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) { |
Matt Arsenault | be55888 | 2014-04-23 20:58:57 +0000 | [diff] [blame] | 30 | unsigned AS = V->getType()->getPointerAddressSpace(); |
| 31 | return B.CreateBitCast(V, B.getInt8PtrTy(AS), "cstr"); |
Eric Christopher | 87abfc5 | 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. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 36 | Value *llvm::EmitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout &DL, |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 37 | const TargetLibraryInfo *TLI) { |
| 38 | if (!TLI->has(LibFunc::strlen)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 39 | return nullptr; |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 40 | |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 41 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
Bill Wendling | 201d7b2 | 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 | c79e42c | 2012-12-22 00:37:52 +0000 | [diff] [blame] | 44 | Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind }; |
Craig Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame] | 45 | AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 46 | |
Chandler Carruth | 7ec5085 | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 47 | LLVMContext &Context = B.GetInsertBlock()->getContext(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 48 | Constant *StrLen = M->getOrInsertFunction( |
| 49 | "strlen", AttributeSet::get(M->getContext(), AS), |
| 50 | DL.getIntPtrType(Context), B.getInt8PtrTy(), nullptr); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 51 | CallInst *CI = B.CreateCall(StrLen, CastToCStr(Ptr, B), "strlen"); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 52 | if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts())) |
| 53 | CI->setCallingConv(F->getCallingConv()); |
| 54 | |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 55 | return CI; |
| 56 | } |
| 57 | |
Nuno Lopes | 7ba5b98 | 2012-07-25 17:18:59 +0000 | [diff] [blame] | 58 | /// EmitStrNLen - Emit a call to the strnlen function to the builder, for the |
| 59 | /// specified pointer. Ptr is required to be some pointer type, MaxLen must |
| 60 | /// be of size_t type, and the return value has 'intptr_t' type. |
| 61 | Value *llvm::EmitStrNLen(Value *Ptr, Value *MaxLen, IRBuilder<> &B, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 62 | const DataLayout &DL, const TargetLibraryInfo *TLI) { |
Nuno Lopes | 7ba5b98 | 2012-07-25 17:18:59 +0000 | [diff] [blame] | 63 | if (!TLI->has(LibFunc::strnlen)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 64 | return nullptr; |
Nuno Lopes | 7ba5b98 | 2012-07-25 17:18:59 +0000 | [diff] [blame] | 65 | |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 66 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 67 | AttributeSet AS[2]; |
| 68 | AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture); |
Bill Wendling | c79e42c | 2012-12-22 00:37:52 +0000 | [diff] [blame] | 69 | Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind }; |
Craig Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame] | 70 | AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs); |
Nuno Lopes | 7ba5b98 | 2012-07-25 17:18:59 +0000 | [diff] [blame] | 71 | |
Chandler Carruth | 7ec5085 | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 72 | LLVMContext &Context = B.GetInsertBlock()->getContext(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 73 | Constant *StrNLen = |
| 74 | M->getOrInsertFunction("strnlen", AttributeSet::get(M->getContext(), AS), |
| 75 | DL.getIntPtrType(Context), B.getInt8PtrTy(), |
| 76 | DL.getIntPtrType(Context), nullptr); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 77 | CallInst *CI = B.CreateCall(StrNLen, {CastToCStr(Ptr, B), MaxLen}, "strnlen"); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 78 | if (const Function *F = dyn_cast<Function>(StrNLen->stripPointerCasts())) |
| 79 | CI->setCallingConv(F->getCallingConv()); |
| 80 | |
Nuno Lopes | 7ba5b98 | 2012-07-25 17:18:59 +0000 | [diff] [blame] | 81 | return CI; |
| 82 | } |
| 83 | |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 84 | /// EmitStrChr - Emit a call to the strchr function to the builder, for the |
| 85 | /// specified pointer and character. Ptr is required to be some pointer type, |
| 86 | /// and the return value has 'i8*' type. |
| 87 | Value *llvm::EmitStrChr(Value *Ptr, char C, IRBuilder<> &B, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 88 | const TargetLibraryInfo *TLI) { |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 89 | if (!TLI->has(LibFunc::strchr)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 90 | return nullptr; |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 91 | |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 92 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
Bill Wendling | c79e42c | 2012-12-22 00:37:52 +0000 | [diff] [blame] | 93 | Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind }; |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 94 | AttributeSet AS = |
Craig Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame] | 95 | AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 96 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 97 | Type *I8Ptr = B.getInt8PtrTy(); |
| 98 | Type *I32Ty = B.getInt32Ty(); |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 99 | Constant *StrChr = M->getOrInsertFunction("strchr", |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 100 | AttributeSet::get(M->getContext(), |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 101 | AS), |
Reid Kleckner | 971c3ea | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 102 | I8Ptr, I8Ptr, I32Ty, nullptr); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 103 | CallInst *CI = B.CreateCall( |
| 104 | StrChr, {CastToCStr(Ptr, B), ConstantInt::get(I32Ty, C)}, "strchr"); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 105 | if (const Function *F = dyn_cast<Function>(StrChr->stripPointerCasts())) |
| 106 | CI->setCallingConv(F->getCallingConv()); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 107 | return CI; |
| 108 | } |
| 109 | |
Benjamin Kramer | 1118860 | 2010-06-15 21:34:25 +0000 | [diff] [blame] | 110 | /// EmitStrNCmp - Emit a call to the strncmp function to the builder. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 111 | Value *llvm::EmitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B, |
| 112 | const DataLayout &DL, const TargetLibraryInfo *TLI) { |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 113 | if (!TLI->has(LibFunc::strncmp)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 114 | return nullptr; |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 115 | |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 116 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 117 | AttributeSet AS[3]; |
| 118 | AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture); |
| 119 | AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture); |
Bill Wendling | c79e42c | 2012-12-22 00:37:52 +0000 | [diff] [blame] | 120 | Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind }; |
Craig Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame] | 121 | AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs); |
Benjamin Kramer | 1118860 | 2010-06-15 21:34:25 +0000 | [diff] [blame] | 122 | |
Chandler Carruth | 7ec5085 | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 123 | LLVMContext &Context = B.GetInsertBlock()->getContext(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 124 | Value *StrNCmp = M->getOrInsertFunction( |
| 125 | "strncmp", AttributeSet::get(M->getContext(), AS), B.getInt32Ty(), |
| 126 | B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context), nullptr); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 127 | CallInst *CI = B.CreateCall( |
| 128 | StrNCmp, {CastToCStr(Ptr1, B), CastToCStr(Ptr2, B), Len}, "strncmp"); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 129 | |
| 130 | if (const Function *F = dyn_cast<Function>(StrNCmp->stripPointerCasts())) |
| 131 | CI->setCallingConv(F->getCallingConv()); |
| 132 | |
Benjamin Kramer | 1118860 | 2010-06-15 21:34:25 +0000 | [diff] [blame] | 133 | return CI; |
| 134 | } |
| 135 | |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 136 | /// EmitStrCpy - Emit a call to the strcpy function to the builder, for the |
| 137 | /// specified pointer arguments. |
| 138 | Value *llvm::EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 139 | const TargetLibraryInfo *TLI, StringRef Name) { |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 140 | if (!TLI->has(LibFunc::strcpy)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 141 | return nullptr; |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 142 | |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 143 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 144 | AttributeSet AS[2]; |
| 145 | AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture); |
| 146 | AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, |
| 147 | Attribute::NoUnwind); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 148 | Type *I8Ptr = B.getInt8PtrTy(); |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 149 | Value *StrCpy = M->getOrInsertFunction(Name, |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 150 | AttributeSet::get(M->getContext(), AS), |
Reid Kleckner | 971c3ea | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 151 | I8Ptr, I8Ptr, I8Ptr, nullptr); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 152 | CallInst *CI = |
| 153 | B.CreateCall(StrCpy, {CastToCStr(Dst, B), CastToCStr(Src, B)}, Name); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 154 | if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts())) |
| 155 | CI->setCallingConv(F->getCallingConv()); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 156 | return CI; |
| 157 | } |
| 158 | |
Eric Christopher | 103e3ef | 2010-03-11 17:45:38 +0000 | [diff] [blame] | 159 | /// EmitStrNCpy - Emit a call to the strncpy function to the builder, for the |
Eric Christopher | 43dc11c | 2010-03-11 01:25:07 +0000 | [diff] [blame] | 160 | /// specified pointer arguments. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 161 | Value *llvm::EmitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B, |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 162 | const TargetLibraryInfo *TLI, StringRef Name) { |
| 163 | if (!TLI->has(LibFunc::strncpy)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 164 | return nullptr; |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 165 | |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 166 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 167 | AttributeSet AS[2]; |
| 168 | AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture); |
| 169 | AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, |
| 170 | Attribute::NoUnwind); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 171 | Type *I8Ptr = B.getInt8PtrTy(); |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 172 | Value *StrNCpy = M->getOrInsertFunction(Name, |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 173 | AttributeSet::get(M->getContext(), |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 174 | AS), |
Eric Christopher | e8b281c | 2010-04-07 23:00:07 +0000 | [diff] [blame] | 175 | I8Ptr, I8Ptr, I8Ptr, |
Reid Kleckner | 971c3ea | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 176 | Len->getType(), nullptr); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 177 | CallInst *CI = B.CreateCall( |
| 178 | StrNCpy, {CastToCStr(Dst, B), CastToCStr(Src, B), Len}, "strncpy"); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 179 | if (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts())) |
| 180 | CI->setCallingConv(F->getCallingConv()); |
Eric Christopher | 43dc11c | 2010-03-11 01:25:07 +0000 | [diff] [blame] | 181 | return CI; |
| 182 | } |
| 183 | |
Evan Cheng | d9e8223 | 2010-03-23 15:48:04 +0000 | [diff] [blame] | 184 | /// EmitMemCpyChk - Emit a call to the __memcpy_chk function to the builder. |
| 185 | /// This expects that the Len and ObjSize have type 'intptr_t' and Dst/Src |
| 186 | /// are pointers. |
| 187 | Value *llvm::EmitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 188 | IRBuilder<> &B, const DataLayout &DL, |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 189 | const TargetLibraryInfo *TLI) { |
| 190 | if (!TLI->has(LibFunc::memcpy_chk)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 191 | return nullptr; |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 192 | |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 193 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 194 | AttributeSet AS; |
| 195 | AS = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, |
| 196 | Attribute::NoUnwind); |
Chandler Carruth | 7ec5085 | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 197 | LLVMContext &Context = B.GetInsertBlock()->getContext(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 198 | Value *MemCpy = M->getOrInsertFunction( |
| 199 | "__memcpy_chk", AttributeSet::get(M->getContext(), AS), B.getInt8PtrTy(), |
| 200 | B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context), |
| 201 | DL.getIntPtrType(Context), nullptr); |
Evan Cheng | d9e8223 | 2010-03-23 15:48:04 +0000 | [diff] [blame] | 202 | Dst = CastToCStr(Dst, B); |
| 203 | Src = CastToCStr(Src, B); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 204 | CallInst *CI = B.CreateCall(MemCpy, {Dst, Src, Len, ObjSize}); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 205 | if (const Function *F = dyn_cast<Function>(MemCpy->stripPointerCasts())) |
| 206 | CI->setCallingConv(F->getCallingConv()); |
Evan Cheng | d9e8223 | 2010-03-23 15:48:04 +0000 | [diff] [blame] | 207 | return CI; |
| 208 | } |
| 209 | |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 210 | /// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is |
| 211 | /// a pointer, Val is an i32 value, and Len is an 'intptr_t' value. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 212 | Value *llvm::EmitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B, |
| 213 | const DataLayout &DL, const TargetLibraryInfo *TLI) { |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 214 | if (!TLI->has(LibFunc::memchr)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 215 | return nullptr; |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 216 | |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 217 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 218 | AttributeSet AS; |
Bill Wendling | c79e42c | 2012-12-22 00:37:52 +0000 | [diff] [blame] | 219 | Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind }; |
Craig Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame] | 220 | AS = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs); |
Chandler Carruth | 7ec5085 | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 221 | LLVMContext &Context = B.GetInsertBlock()->getContext(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 222 | Value *MemChr = M->getOrInsertFunction( |
| 223 | "memchr", AttributeSet::get(M->getContext(), AS), B.getInt8PtrTy(), |
| 224 | B.getInt8PtrTy(), B.getInt32Ty(), DL.getIntPtrType(Context), nullptr); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 225 | CallInst *CI = B.CreateCall(MemChr, {CastToCStr(Ptr, B), Val, Len}, "memchr"); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 226 | |
| 227 | if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts())) |
| 228 | CI->setCallingConv(F->getCallingConv()); |
| 229 | |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 230 | return CI; |
| 231 | } |
| 232 | |
| 233 | /// EmitMemCmp - Emit a call to the memcmp function. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 234 | Value *llvm::EmitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B, |
| 235 | const DataLayout &DL, const TargetLibraryInfo *TLI) { |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 236 | if (!TLI->has(LibFunc::memcmp)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 237 | return nullptr; |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 238 | |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 239 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 240 | AttributeSet AS[3]; |
| 241 | AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture); |
| 242 | AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture); |
Bill Wendling | c79e42c | 2012-12-22 00:37:52 +0000 | [diff] [blame] | 243 | Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind }; |
Craig Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame] | 244 | AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 245 | |
Chandler Carruth | 7ec5085 | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 246 | LLVMContext &Context = B.GetInsertBlock()->getContext(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 247 | Value *MemCmp = M->getOrInsertFunction( |
| 248 | "memcmp", AttributeSet::get(M->getContext(), AS), B.getInt32Ty(), |
| 249 | B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context), nullptr); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 250 | CallInst *CI = B.CreateCall( |
| 251 | MemCmp, {CastToCStr(Ptr1, B), CastToCStr(Ptr2, B), Len}, "memcmp"); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 252 | |
| 253 | if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts())) |
| 254 | CI->setCallingConv(F->getCallingConv()); |
| 255 | |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 256 | return CI; |
| 257 | } |
| 258 | |
Yi Jiang | 6ab044e | 2013-12-16 22:42:40 +0000 | [diff] [blame] | 259 | /// Append a suffix to the function name according to the type of 'Op'. |
| 260 | static void AppendTypeSuffix(Value *Op, StringRef &Name, SmallString<20> &NameBuffer) { |
| 261 | if (!Op->getType()->isDoubleTy()) { |
| 262 | NameBuffer += Name; |
| 263 | |
| 264 | if (Op->getType()->isFloatTy()) |
| 265 | NameBuffer += 'f'; |
| 266 | else |
| 267 | NameBuffer += 'l'; |
| 268 | |
| 269 | Name = NameBuffer; |
| 270 | } |
| 271 | return; |
| 272 | } |
| 273 | |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 274 | /// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g. |
| 275 | /// 'floor'). This function is known to take a single of type matching 'Op' and |
| 276 | /// returns one value with the same type. If 'Op' is a long double, 'l' is |
| 277 | /// added as the suffix of name, if 'Op' is a float, we add a 'f' suffix. |
Benjamin Kramer | b106bcc | 2011-11-15 19:12:09 +0000 | [diff] [blame] | 278 | Value *llvm::EmitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B, |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 279 | const AttributeSet &Attrs) { |
Benjamin Kramer | b106bcc | 2011-11-15 19:12:09 +0000 | [diff] [blame] | 280 | SmallString<20> NameBuffer; |
Yi Jiang | 6ab044e | 2013-12-16 22:42:40 +0000 | [diff] [blame] | 281 | AppendTypeSuffix(Op, Name, NameBuffer); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 282 | |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 283 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 284 | Value *Callee = M->getOrInsertFunction(Name, Op->getType(), |
Reid Kleckner | 971c3ea | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 285 | Op->getType(), nullptr); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 286 | CallInst *CI = B.CreateCall(Callee, Op, Name); |
| 287 | CI->setAttributes(Attrs); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 288 | if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts())) |
| 289 | CI->setCallingConv(F->getCallingConv()); |
| 290 | |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 291 | return CI; |
| 292 | } |
| 293 | |
Yi Jiang | 6ab044e | 2013-12-16 22:42:40 +0000 | [diff] [blame] | 294 | /// EmitBinaryFloatFnCall - Emit a call to the binary function named 'Name' |
| 295 | /// (e.g. 'fmin'). This function is known to take type matching 'Op1' and 'Op2' |
| 296 | /// and return one value with the same type. If 'Op1/Op2' are long double, 'l' |
| 297 | /// is added as the suffix of name, if 'Op1/Op2' is a float, we add a 'f' |
| 298 | /// suffix. |
| 299 | Value *llvm::EmitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name, |
| 300 | IRBuilder<> &B, const AttributeSet &Attrs) { |
| 301 | SmallString<20> NameBuffer; |
| 302 | AppendTypeSuffix(Op1, Name, NameBuffer); |
| 303 | |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 304 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
Yi Jiang | 6ab044e | 2013-12-16 22:42:40 +0000 | [diff] [blame] | 305 | Value *Callee = M->getOrInsertFunction(Name, Op1->getType(), |
Reid Kleckner | 971c3ea | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 306 | Op1->getType(), Op2->getType(), nullptr); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 307 | CallInst *CI = B.CreateCall(Callee, {Op1, Op2}, Name); |
Yi Jiang | 6ab044e | 2013-12-16 22:42:40 +0000 | [diff] [blame] | 308 | CI->setAttributes(Attrs); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 309 | if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts())) |
| 310 | CI->setCallingConv(F->getCallingConv()); |
| 311 | |
Yi Jiang | 6ab044e | 2013-12-16 22:42:40 +0000 | [diff] [blame] | 312 | return CI; |
| 313 | } |
| 314 | |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 315 | /// EmitPutChar - Emit a call to the putchar function. This assumes that Char |
| 316 | /// is an integer. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 317 | Value *llvm::EmitPutChar(Value *Char, IRBuilder<> &B, |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 318 | const TargetLibraryInfo *TLI) { |
| 319 | if (!TLI->has(LibFunc::putchar)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 320 | return nullptr; |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 321 | |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 322 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 323 | Value *PutChar = M->getOrInsertFunction("putchar", B.getInt32Ty(), |
Reid Kleckner | 971c3ea | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 324 | B.getInt32Ty(), nullptr); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 325 | CallInst *CI = B.CreateCall(PutChar, |
| 326 | B.CreateIntCast(Char, |
| 327 | B.getInt32Ty(), |
| 328 | /*isSigned*/true, |
| 329 | "chari"), |
| 330 | "putchar"); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 331 | |
| 332 | if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts())) |
| 333 | CI->setCallingConv(F->getCallingConv()); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 334 | return CI; |
| 335 | } |
| 336 | |
| 337 | /// EmitPutS - Emit a call to the puts function. This assumes that Str is |
| 338 | /// some pointer. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 339 | Value *llvm::EmitPutS(Value *Str, IRBuilder<> &B, |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 340 | const TargetLibraryInfo *TLI) { |
| 341 | if (!TLI->has(LibFunc::puts)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 342 | return nullptr; |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 343 | |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 344 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 345 | AttributeSet AS[2]; |
| 346 | AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture); |
| 347 | AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, |
| 348 | Attribute::NoUnwind); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 349 | |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 350 | Value *PutS = M->getOrInsertFunction("puts", |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 351 | AttributeSet::get(M->getContext(), AS), |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 352 | B.getInt32Ty(), |
| 353 | B.getInt8PtrTy(), |
Reid Kleckner | 971c3ea | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 354 | nullptr); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 355 | CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts"); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 356 | if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts())) |
| 357 | CI->setCallingConv(F->getCallingConv()); |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 358 | return CI; |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | /// EmitFPutC - Emit a call to the fputc function. This assumes that Char is |
| 362 | /// an integer and File is a pointer to FILE. |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 363 | Value *llvm::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 364 | const TargetLibraryInfo *TLI) { |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 365 | if (!TLI->has(LibFunc::fputc)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 366 | return nullptr; |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 367 | |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 368 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 369 | AttributeSet AS[2]; |
| 370 | AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture); |
| 371 | AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, |
| 372 | Attribute::NoUnwind); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 373 | Constant *F; |
| 374 | if (File->getType()->isPointerTy()) |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 375 | F = M->getOrInsertFunction("fputc", |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 376 | AttributeSet::get(M->getContext(), AS), |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 377 | B.getInt32Ty(), |
| 378 | B.getInt32Ty(), File->getType(), |
Reid Kleckner | 971c3ea | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 379 | nullptr); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 380 | else |
| 381 | F = M->getOrInsertFunction("fputc", |
| 382 | B.getInt32Ty(), |
| 383 | B.getInt32Ty(), |
Reid Kleckner | 971c3ea | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 384 | File->getType(), nullptr); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 385 | Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true, |
| 386 | "chari"); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 387 | CallInst *CI = B.CreateCall(F, {Char, File}, "fputc"); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 388 | |
| 389 | if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) |
| 390 | CI->setCallingConv(Fn->getCallingConv()); |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 391 | return CI; |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | /// EmitFPutS - Emit a call to the puts function. Str is required to be a |
| 395 | /// pointer and File is a pointer to FILE. |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 396 | Value *llvm::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 397 | const TargetLibraryInfo *TLI) { |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 398 | if (!TLI->has(LibFunc::fputs)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 399 | return nullptr; |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 400 | |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 401 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 402 | AttributeSet AS[3]; |
| 403 | AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture); |
| 404 | AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture); |
| 405 | AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, |
| 406 | Attribute::NoUnwind); |
Eli Friedman | 489c0ff | 2011-11-17 01:27:36 +0000 | [diff] [blame] | 407 | StringRef FPutsName = TLI->getName(LibFunc::fputs); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 408 | Constant *F; |
| 409 | if (File->getType()->isPointerTy()) |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 410 | F = M->getOrInsertFunction(FPutsName, |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 411 | AttributeSet::get(M->getContext(), AS), |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 412 | B.getInt32Ty(), |
| 413 | B.getInt8PtrTy(), |
Reid Kleckner | 971c3ea | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 414 | File->getType(), nullptr); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 415 | else |
Eli Friedman | 489c0ff | 2011-11-17 01:27:36 +0000 | [diff] [blame] | 416 | F = M->getOrInsertFunction(FPutsName, B.getInt32Ty(), |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 417 | B.getInt8PtrTy(), |
Reid Kleckner | 971c3ea | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 418 | File->getType(), nullptr); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 419 | CallInst *CI = B.CreateCall(F, {CastToCStr(Str, B), File}, "fputs"); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 420 | |
| 421 | if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) |
| 422 | CI->setCallingConv(Fn->getCallingConv()); |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 423 | return CI; |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | /// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is |
| 427 | /// a pointer, Size is an 'intptr_t', and File is a pointer to FILE. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 428 | Value *llvm::EmitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B, |
| 429 | const DataLayout &DL, const TargetLibraryInfo *TLI) { |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 430 | if (!TLI->has(LibFunc::fwrite)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 431 | return nullptr; |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 432 | |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 433 | Module *M = B.GetInsertBlock()->getParent()->getParent(); |
Bill Wendling | 201d7b2 | 2013-01-26 00:03:11 +0000 | [diff] [blame] | 434 | AttributeSet AS[3]; |
| 435 | AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture); |
| 436 | AS[1] = AttributeSet::get(M->getContext(), 4, Attribute::NoCapture); |
| 437 | AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, |
| 438 | Attribute::NoUnwind); |
Chandler Carruth | 7ec5085 | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 439 | LLVMContext &Context = B.GetInsertBlock()->getContext(); |
Eli Friedman | 489c0ff | 2011-11-17 01:27:36 +0000 | [diff] [blame] | 440 | StringRef FWriteName = TLI->getName(LibFunc::fwrite); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 441 | Constant *F; |
| 442 | if (File->getType()->isPointerTy()) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 443 | F = M->getOrInsertFunction( |
| 444 | FWriteName, AttributeSet::get(M->getContext(), AS), |
| 445 | DL.getIntPtrType(Context), B.getInt8PtrTy(), DL.getIntPtrType(Context), |
| 446 | DL.getIntPtrType(Context), File->getType(), nullptr); |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 447 | else |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 448 | F = M->getOrInsertFunction(FWriteName, DL.getIntPtrType(Context), |
| 449 | B.getInt8PtrTy(), DL.getIntPtrType(Context), |
| 450 | DL.getIntPtrType(Context), File->getType(), |
| 451 | nullptr); |
| 452 | CallInst *CI = |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 453 | B.CreateCall(F, {CastToCStr(Ptr, B), Size, |
| 454 | ConstantInt::get(DL.getIntPtrType(Context), 1), File}); |
Evan Cheng | ad6efbf | 2014-03-12 18:09:37 +0000 | [diff] [blame] | 455 | |
| 456 | if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts())) |
| 457 | CI->setCallingConv(Fn->getCallingConv()); |
Nuno Lopes | 89702e9 | 2012-07-25 16:46:31 +0000 | [diff] [blame] | 458 | return CI; |
Eric Christopher | 87abfc5 | 2010-03-05 22:25:30 +0000 | [diff] [blame] | 459 | } |