blob: 02512520e5ba890ef38630f2b721efcfc209dbd5 [file] [log] [blame]
Eric Christopher87abfc52010-03-05 22:25:30 +00001//===- 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 Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/SmallString.h"
Weiming Zhao45d4cb92015-11-24 18:57:06 +000016#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#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 Christopher87abfc52010-03-05 22:25:30 +000025
26using namespace llvm;
27
Eric Christopher87abfc52010-03-05 22:25:30 +000028Value *llvm::CastToCStr(Value *V, IRBuilder<> &B) {
Matt Arsenaultbe558882014-04-23 20:58:57 +000029 unsigned AS = V->getType()->getPointerAddressSpace();
30 return B.CreateBitCast(V, B.getInt8PtrTy(AS), "cstr");
Eric Christopher87abfc52010-03-05 22:25:30 +000031}
32
Mehdi Aminia28d91d2015-03-10 02:37:25 +000033Value *llvm::EmitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout &DL,
Nuno Lopes89702e92012-07-25 16:46:31 +000034 const TargetLibraryInfo *TLI) {
35 if (!TLI->has(LibFunc::strlen))
Craig Topperf40110f2014-04-25 05:29:35 +000036 return nullptr;
Nuno Lopes89702e92012-07-25 16:46:31 +000037
Evan Chengad6efbf2014-03-12 18:09:37 +000038 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling201d7b22013-01-26 00:03:11 +000039 AttributeSet AS[2];
40 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
Bill Wendlingc79e42c2012-12-22 00:37:52 +000041 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
Craig Toppere1d12942014-08-27 05:25:25 +000042 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs);
Eric Christopher87abfc52010-03-05 22:25:30 +000043
Chandler Carruth7ec50852012-11-01 08:07:29 +000044 LLVMContext &Context = B.GetInsertBlock()->getContext();
Mehdi Aminia28d91d2015-03-10 02:37:25 +000045 Constant *StrLen = M->getOrInsertFunction(
46 "strlen", AttributeSet::get(M->getContext(), AS),
47 DL.getIntPtrType(Context), B.getInt8PtrTy(), nullptr);
Eric Christopher87abfc52010-03-05 22:25:30 +000048 CallInst *CI = B.CreateCall(StrLen, CastToCStr(Ptr, B), "strlen");
Evan Chengad6efbf2014-03-12 18:09:37 +000049 if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts()))
50 CI->setCallingConv(F->getCallingConv());
51
Eric Christopher87abfc52010-03-05 22:25:30 +000052 return CI;
53}
54
Eric Christopher87abfc52010-03-05 22:25:30 +000055Value *llvm::EmitStrChr(Value *Ptr, char C, IRBuilder<> &B,
Mehdi Aminia28d91d2015-03-10 02:37:25 +000056 const TargetLibraryInfo *TLI) {
Nuno Lopes89702e92012-07-25 16:46:31 +000057 if (!TLI->has(LibFunc::strchr))
Craig Topperf40110f2014-04-25 05:29:35 +000058 return nullptr;
Nuno Lopes89702e92012-07-25 16:46:31 +000059
Evan Chengad6efbf2014-03-12 18:09:37 +000060 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendlingc79e42c2012-12-22 00:37:52 +000061 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
Bill Wendling201d7b22013-01-26 00:03:11 +000062 AttributeSet AS =
Craig Toppere1d12942014-08-27 05:25:25 +000063 AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs);
Eric Christopher87abfc52010-03-05 22:25:30 +000064
Chris Lattner229907c2011-07-18 04:54:35 +000065 Type *I8Ptr = B.getInt8PtrTy();
66 Type *I32Ty = B.getInt32Ty();
Bill Wendlingf86efb92012-11-20 05:09:20 +000067 Constant *StrChr = M->getOrInsertFunction("strchr",
Bill Wendlinge94d8432012-12-07 23:16:57 +000068 AttributeSet::get(M->getContext(),
Bill Wendling201d7b22013-01-26 00:03:11 +000069 AS),
Reid Kleckner971c3ea2014-11-13 22:55:19 +000070 I8Ptr, I8Ptr, I32Ty, nullptr);
David Blaikieff6409d2015-05-18 22:13:54 +000071 CallInst *CI = B.CreateCall(
72 StrChr, {CastToCStr(Ptr, B), ConstantInt::get(I32Ty, C)}, "strchr");
Evan Chengad6efbf2014-03-12 18:09:37 +000073 if (const Function *F = dyn_cast<Function>(StrChr->stripPointerCasts()))
74 CI->setCallingConv(F->getCallingConv());
Eric Christopher87abfc52010-03-05 22:25:30 +000075 return CI;
76}
77
Mehdi Aminia28d91d2015-03-10 02:37:25 +000078Value *llvm::EmitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
79 const DataLayout &DL, const TargetLibraryInfo *TLI) {
Nuno Lopes89702e92012-07-25 16:46:31 +000080 if (!TLI->has(LibFunc::strncmp))
Craig Topperf40110f2014-04-25 05:29:35 +000081 return nullptr;
Nuno Lopes89702e92012-07-25 16:46:31 +000082
Evan Chengad6efbf2014-03-12 18:09:37 +000083 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling201d7b22013-01-26 00:03:11 +000084 AttributeSet AS[3];
85 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
86 AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
Bill Wendlingc79e42c2012-12-22 00:37:52 +000087 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
Craig Toppere1d12942014-08-27 05:25:25 +000088 AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs);
Benjamin Kramer11188602010-06-15 21:34:25 +000089
Chandler Carruth7ec50852012-11-01 08:07:29 +000090 LLVMContext &Context = B.GetInsertBlock()->getContext();
Mehdi Aminia28d91d2015-03-10 02:37:25 +000091 Value *StrNCmp = M->getOrInsertFunction(
92 "strncmp", AttributeSet::get(M->getContext(), AS), B.getInt32Ty(),
93 B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context), nullptr);
David Blaikieff6409d2015-05-18 22:13:54 +000094 CallInst *CI = B.CreateCall(
95 StrNCmp, {CastToCStr(Ptr1, B), CastToCStr(Ptr2, B), Len}, "strncmp");
Evan Chengad6efbf2014-03-12 18:09:37 +000096
97 if (const Function *F = dyn_cast<Function>(StrNCmp->stripPointerCasts()))
98 CI->setCallingConv(F->getCallingConv());
99
Benjamin Kramer11188602010-06-15 21:34:25 +0000100 return CI;
101}
102
Eric Christopher87abfc52010-03-05 22:25:30 +0000103Value *llvm::EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000104 const TargetLibraryInfo *TLI, StringRef Name) {
Nuno Lopes89702e92012-07-25 16:46:31 +0000105 if (!TLI->has(LibFunc::strcpy))
Craig Topperf40110f2014-04-25 05:29:35 +0000106 return nullptr;
Nuno Lopes89702e92012-07-25 16:46:31 +0000107
Evan Chengad6efbf2014-03-12 18:09:37 +0000108 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling201d7b22013-01-26 00:03:11 +0000109 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 Lattner229907c2011-07-18 04:54:35 +0000113 Type *I8Ptr = B.getInt8PtrTy();
Bill Wendlingf86efb92012-11-20 05:09:20 +0000114 Value *StrCpy = M->getOrInsertFunction(Name,
Bill Wendling201d7b22013-01-26 00:03:11 +0000115 AttributeSet::get(M->getContext(), AS),
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000116 I8Ptr, I8Ptr, I8Ptr, nullptr);
David Blaikieff6409d2015-05-18 22:13:54 +0000117 CallInst *CI =
118 B.CreateCall(StrCpy, {CastToCStr(Dst, B), CastToCStr(Src, B)}, Name);
Evan Chengad6efbf2014-03-12 18:09:37 +0000119 if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts()))
120 CI->setCallingConv(F->getCallingConv());
Eric Christopher87abfc52010-03-05 22:25:30 +0000121 return CI;
122}
123
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000124Value *llvm::EmitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
Nuno Lopes89702e92012-07-25 16:46:31 +0000125 const TargetLibraryInfo *TLI, StringRef Name) {
126 if (!TLI->has(LibFunc::strncpy))
Craig Topperf40110f2014-04-25 05:29:35 +0000127 return nullptr;
Nuno Lopes89702e92012-07-25 16:46:31 +0000128
Evan Chengad6efbf2014-03-12 18:09:37 +0000129 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling201d7b22013-01-26 00:03:11 +0000130 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 Lattner229907c2011-07-18 04:54:35 +0000134 Type *I8Ptr = B.getInt8PtrTy();
Bill Wendlingf86efb92012-11-20 05:09:20 +0000135 Value *StrNCpy = M->getOrInsertFunction(Name,
Bill Wendlinge94d8432012-12-07 23:16:57 +0000136 AttributeSet::get(M->getContext(),
Bill Wendling201d7b22013-01-26 00:03:11 +0000137 AS),
Eric Christophere8b281c2010-04-07 23:00:07 +0000138 I8Ptr, I8Ptr, I8Ptr,
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000139 Len->getType(), nullptr);
David Blaikieff6409d2015-05-18 22:13:54 +0000140 CallInst *CI = B.CreateCall(
141 StrNCpy, {CastToCStr(Dst, B), CastToCStr(Src, B), Len}, "strncpy");
Evan Chengad6efbf2014-03-12 18:09:37 +0000142 if (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts()))
143 CI->setCallingConv(F->getCallingConv());
Eric Christopher43dc11c2010-03-11 01:25:07 +0000144 return CI;
145}
146
Evan Chengd9e82232010-03-23 15:48:04 +0000147Value *llvm::EmitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000148 IRBuilder<> &B, const DataLayout &DL,
Nuno Lopes89702e92012-07-25 16:46:31 +0000149 const TargetLibraryInfo *TLI) {
150 if (!TLI->has(LibFunc::memcpy_chk))
Craig Topperf40110f2014-04-25 05:29:35 +0000151 return nullptr;
Nuno Lopes89702e92012-07-25 16:46:31 +0000152
Evan Chengad6efbf2014-03-12 18:09:37 +0000153 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling201d7b22013-01-26 00:03:11 +0000154 AttributeSet AS;
155 AS = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
156 Attribute::NoUnwind);
Chandler Carruth7ec50852012-11-01 08:07:29 +0000157 LLVMContext &Context = B.GetInsertBlock()->getContext();
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000158 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 Chengd9e82232010-03-23 15:48:04 +0000162 Dst = CastToCStr(Dst, B);
163 Src = CastToCStr(Src, B);
David Blaikieff6409d2015-05-18 22:13:54 +0000164 CallInst *CI = B.CreateCall(MemCpy, {Dst, Src, Len, ObjSize});
Evan Chengad6efbf2014-03-12 18:09:37 +0000165 if (const Function *F = dyn_cast<Function>(MemCpy->stripPointerCasts()))
166 CI->setCallingConv(F->getCallingConv());
Evan Chengd9e82232010-03-23 15:48:04 +0000167 return CI;
168}
169
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000170Value *llvm::EmitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B,
171 const DataLayout &DL, const TargetLibraryInfo *TLI) {
Nuno Lopes89702e92012-07-25 16:46:31 +0000172 if (!TLI->has(LibFunc::memchr))
Craig Topperf40110f2014-04-25 05:29:35 +0000173 return nullptr;
Nuno Lopes89702e92012-07-25 16:46:31 +0000174
Evan Chengad6efbf2014-03-12 18:09:37 +0000175 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling201d7b22013-01-26 00:03:11 +0000176 AttributeSet AS;
Bill Wendlingc79e42c2012-12-22 00:37:52 +0000177 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
Craig Toppere1d12942014-08-27 05:25:25 +0000178 AS = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs);
Chandler Carruth7ec50852012-11-01 08:07:29 +0000179 LLVMContext &Context = B.GetInsertBlock()->getContext();
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000180 Value *MemChr = M->getOrInsertFunction(
181 "memchr", AttributeSet::get(M->getContext(), AS), B.getInt8PtrTy(),
182 B.getInt8PtrTy(), B.getInt32Ty(), DL.getIntPtrType(Context), nullptr);
David Blaikieff6409d2015-05-18 22:13:54 +0000183 CallInst *CI = B.CreateCall(MemChr, {CastToCStr(Ptr, B), Val, Len}, "memchr");
Evan Chengad6efbf2014-03-12 18:09:37 +0000184
185 if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts()))
186 CI->setCallingConv(F->getCallingConv());
187
Eric Christopher87abfc52010-03-05 22:25:30 +0000188 return CI;
189}
190
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000191Value *llvm::EmitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
192 const DataLayout &DL, const TargetLibraryInfo *TLI) {
Nuno Lopes89702e92012-07-25 16:46:31 +0000193 if (!TLI->has(LibFunc::memcmp))
Craig Topperf40110f2014-04-25 05:29:35 +0000194 return nullptr;
Nuno Lopes89702e92012-07-25 16:46:31 +0000195
Evan Chengad6efbf2014-03-12 18:09:37 +0000196 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling201d7b22013-01-26 00:03:11 +0000197 AttributeSet AS[3];
198 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
199 AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
Bill Wendlingc79e42c2012-12-22 00:37:52 +0000200 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
Craig Toppere1d12942014-08-27 05:25:25 +0000201 AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs);
Eric Christopher87abfc52010-03-05 22:25:30 +0000202
Chandler Carruth7ec50852012-11-01 08:07:29 +0000203 LLVMContext &Context = B.GetInsertBlock()->getContext();
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000204 Value *MemCmp = M->getOrInsertFunction(
205 "memcmp", AttributeSet::get(M->getContext(), AS), B.getInt32Ty(),
206 B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context), nullptr);
David Blaikieff6409d2015-05-18 22:13:54 +0000207 CallInst *CI = B.CreateCall(
208 MemCmp, {CastToCStr(Ptr1, B), CastToCStr(Ptr2, B), Len}, "memcmp");
Evan Chengad6efbf2014-03-12 18:09:37 +0000209
210 if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts()))
211 CI->setCallingConv(F->getCallingConv());
212
Eric Christopher87abfc52010-03-05 22:25:30 +0000213 return CI;
214}
215
Yi Jiang6ab044e2013-12-16 22:42:40 +0000216/// Append a suffix to the function name according to the type of 'Op'.
217static 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 Kramerb106bcc2011-11-15 19:12:09 +0000231Value *llvm::EmitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B,
Bill Wendlinge94d8432012-12-07 23:16:57 +0000232 const AttributeSet &Attrs) {
Benjamin Kramerb106bcc2011-11-15 19:12:09 +0000233 SmallString<20> NameBuffer;
Yi Jiang6ab044e2013-12-16 22:42:40 +0000234 AppendTypeSuffix(Op, Name, NameBuffer);
Eric Christopher87abfc52010-03-05 22:25:30 +0000235
Evan Chengad6efbf2014-03-12 18:09:37 +0000236 Module *M = B.GetInsertBlock()->getParent()->getParent();
Eric Christopher87abfc52010-03-05 22:25:30 +0000237 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000238 Op->getType(), nullptr);
Eric Christopher87abfc52010-03-05 22:25:30 +0000239 CallInst *CI = B.CreateCall(Callee, Op, Name);
240 CI->setAttributes(Attrs);
Evan Chengad6efbf2014-03-12 18:09:37 +0000241 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
242 CI->setCallingConv(F->getCallingConv());
243
Eric Christopher87abfc52010-03-05 22:25:30 +0000244 return CI;
245}
246
Yi Jiang6ab044e2013-12-16 22:42:40 +0000247Value *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 Chengad6efbf2014-03-12 18:09:37 +0000252 Module *M = B.GetInsertBlock()->getParent()->getParent();
Yi Jiang6ab044e2013-12-16 22:42:40 +0000253 Value *Callee = M->getOrInsertFunction(Name, Op1->getType(),
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000254 Op1->getType(), Op2->getType(), nullptr);
David Blaikieff6409d2015-05-18 22:13:54 +0000255 CallInst *CI = B.CreateCall(Callee, {Op1, Op2}, Name);
Yi Jiang6ab044e2013-12-16 22:42:40 +0000256 CI->setAttributes(Attrs);
Evan Chengad6efbf2014-03-12 18:09:37 +0000257 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
258 CI->setCallingConv(F->getCallingConv());
259
Yi Jiang6ab044e2013-12-16 22:42:40 +0000260 return CI;
261}
262
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000263Value *llvm::EmitPutChar(Value *Char, IRBuilder<> &B,
Nuno Lopes89702e92012-07-25 16:46:31 +0000264 const TargetLibraryInfo *TLI) {
265 if (!TLI->has(LibFunc::putchar))
Craig Topperf40110f2014-04-25 05:29:35 +0000266 return nullptr;
Nuno Lopes89702e92012-07-25 16:46:31 +0000267
Evan Chengad6efbf2014-03-12 18:09:37 +0000268 Module *M = B.GetInsertBlock()->getParent()->getParent();
Eric Christopher87abfc52010-03-05 22:25:30 +0000269 Value *PutChar = M->getOrInsertFunction("putchar", B.getInt32Ty(),
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000270 B.getInt32Ty(), nullptr);
Eric Christopher87abfc52010-03-05 22:25:30 +0000271 CallInst *CI = B.CreateCall(PutChar,
272 B.CreateIntCast(Char,
273 B.getInt32Ty(),
274 /*isSigned*/true,
275 "chari"),
276 "putchar");
Evan Chengad6efbf2014-03-12 18:09:37 +0000277
278 if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts()))
279 CI->setCallingConv(F->getCallingConv());
Eric Christopher87abfc52010-03-05 22:25:30 +0000280 return CI;
281}
282
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000283Value *llvm::EmitPutS(Value *Str, IRBuilder<> &B,
Nuno Lopes89702e92012-07-25 16:46:31 +0000284 const TargetLibraryInfo *TLI) {
285 if (!TLI->has(LibFunc::puts))
Craig Topperf40110f2014-04-25 05:29:35 +0000286 return nullptr;
Nuno Lopes89702e92012-07-25 16:46:31 +0000287
Evan Chengad6efbf2014-03-12 18:09:37 +0000288 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling201d7b22013-01-26 00:03:11 +0000289 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 Christopher87abfc52010-03-05 22:25:30 +0000293
Bill Wendlingf86efb92012-11-20 05:09:20 +0000294 Value *PutS = M->getOrInsertFunction("puts",
Bill Wendling201d7b22013-01-26 00:03:11 +0000295 AttributeSet::get(M->getContext(), AS),
Eric Christopher87abfc52010-03-05 22:25:30 +0000296 B.getInt32Ty(),
297 B.getInt8PtrTy(),
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000298 nullptr);
Eric Christopher87abfc52010-03-05 22:25:30 +0000299 CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts");
Evan Chengad6efbf2014-03-12 18:09:37 +0000300 if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
301 CI->setCallingConv(F->getCallingConv());
Nuno Lopes89702e92012-07-25 16:46:31 +0000302 return CI;
Eric Christopher87abfc52010-03-05 22:25:30 +0000303}
304
Nuno Lopes89702e92012-07-25 16:46:31 +0000305Value *llvm::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000306 const TargetLibraryInfo *TLI) {
Nuno Lopes89702e92012-07-25 16:46:31 +0000307 if (!TLI->has(LibFunc::fputc))
Craig Topperf40110f2014-04-25 05:29:35 +0000308 return nullptr;
Nuno Lopes89702e92012-07-25 16:46:31 +0000309
Evan Chengad6efbf2014-03-12 18:09:37 +0000310 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling201d7b22013-01-26 00:03:11 +0000311 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 Christopher87abfc52010-03-05 22:25:30 +0000315 Constant *F;
316 if (File->getType()->isPointerTy())
Bill Wendlingf86efb92012-11-20 05:09:20 +0000317 F = M->getOrInsertFunction("fputc",
Bill Wendling201d7b22013-01-26 00:03:11 +0000318 AttributeSet::get(M->getContext(), AS),
Eric Christopher87abfc52010-03-05 22:25:30 +0000319 B.getInt32Ty(),
320 B.getInt32Ty(), File->getType(),
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000321 nullptr);
Eric Christopher87abfc52010-03-05 22:25:30 +0000322 else
323 F = M->getOrInsertFunction("fputc",
324 B.getInt32Ty(),
325 B.getInt32Ty(),
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000326 File->getType(), nullptr);
Eric Christopher87abfc52010-03-05 22:25:30 +0000327 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
328 "chari");
David Blaikieff6409d2015-05-18 22:13:54 +0000329 CallInst *CI = B.CreateCall(F, {Char, File}, "fputc");
Evan Chengad6efbf2014-03-12 18:09:37 +0000330
331 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
332 CI->setCallingConv(Fn->getCallingConv());
Nuno Lopes89702e92012-07-25 16:46:31 +0000333 return CI;
Eric Christopher87abfc52010-03-05 22:25:30 +0000334}
335
Nuno Lopes89702e92012-07-25 16:46:31 +0000336Value *llvm::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000337 const TargetLibraryInfo *TLI) {
Nuno Lopes89702e92012-07-25 16:46:31 +0000338 if (!TLI->has(LibFunc::fputs))
Craig Topperf40110f2014-04-25 05:29:35 +0000339 return nullptr;
Nuno Lopes89702e92012-07-25 16:46:31 +0000340
Evan Chengad6efbf2014-03-12 18:09:37 +0000341 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling201d7b22013-01-26 00:03:11 +0000342 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 Friedman489c0ff2011-11-17 01:27:36 +0000347 StringRef FPutsName = TLI->getName(LibFunc::fputs);
Eric Christopher87abfc52010-03-05 22:25:30 +0000348 Constant *F;
349 if (File->getType()->isPointerTy())
Bill Wendlingf86efb92012-11-20 05:09:20 +0000350 F = M->getOrInsertFunction(FPutsName,
Bill Wendling201d7b22013-01-26 00:03:11 +0000351 AttributeSet::get(M->getContext(), AS),
Eric Christopher87abfc52010-03-05 22:25:30 +0000352 B.getInt32Ty(),
353 B.getInt8PtrTy(),
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000354 File->getType(), nullptr);
Eric Christopher87abfc52010-03-05 22:25:30 +0000355 else
Eli Friedman489c0ff2011-11-17 01:27:36 +0000356 F = M->getOrInsertFunction(FPutsName, B.getInt32Ty(),
Eric Christopher87abfc52010-03-05 22:25:30 +0000357 B.getInt8PtrTy(),
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000358 File->getType(), nullptr);
David Blaikieff6409d2015-05-18 22:13:54 +0000359 CallInst *CI = B.CreateCall(F, {CastToCStr(Str, B), File}, "fputs");
Evan Chengad6efbf2014-03-12 18:09:37 +0000360
361 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
362 CI->setCallingConv(Fn->getCallingConv());
Nuno Lopes89702e92012-07-25 16:46:31 +0000363 return CI;
Eric Christopher87abfc52010-03-05 22:25:30 +0000364}
365
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000366Value *llvm::EmitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B,
367 const DataLayout &DL, const TargetLibraryInfo *TLI) {
Nuno Lopes89702e92012-07-25 16:46:31 +0000368 if (!TLI->has(LibFunc::fwrite))
Craig Topperf40110f2014-04-25 05:29:35 +0000369 return nullptr;
Nuno Lopes89702e92012-07-25 16:46:31 +0000370
Evan Chengad6efbf2014-03-12 18:09:37 +0000371 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling201d7b22013-01-26 00:03:11 +0000372 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 Carruth7ec50852012-11-01 08:07:29 +0000377 LLVMContext &Context = B.GetInsertBlock()->getContext();
Eli Friedman489c0ff2011-11-17 01:27:36 +0000378 StringRef FWriteName = TLI->getName(LibFunc::fwrite);
Eric Christopher87abfc52010-03-05 22:25:30 +0000379 Constant *F;
380 if (File->getType()->isPointerTy())
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000381 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 Christopher87abfc52010-03-05 22:25:30 +0000385 else
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000386 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 Blaikieff6409d2015-05-18 22:13:54 +0000391 B.CreateCall(F, {CastToCStr(Ptr, B), Size,
392 ConstantInt::get(DL.getIntPtrType(Context), 1), File});
Evan Chengad6efbf2014-03-12 18:09:37 +0000393
394 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
395 CI->setCallingConv(Fn->getCallingConv());
Nuno Lopes89702e92012-07-25 16:46:31 +0000396 return CI;
Eric Christopher87abfc52010-03-05 22:25:30 +0000397}