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