blob: 64b44a6b79194a23328254d67feb7c6fe537a246 [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
28/// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
29Value *llvm::CastToCStr(Value *V, IRBuilder<> &B) {
Matt Arsenaultbe558882014-04-23 20:58:57 +000030 unsigned AS = V->getType()->getPointerAddressSpace();
31 return B.CreateBitCast(V, B.getInt8PtrTy(AS), "cstr");
Eric Christopher87abfc52010-03-05 22:25:30 +000032}
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 Aminia28d91d2015-03-10 02:37:25 +000036Value *llvm::EmitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout &DL,
Nuno Lopes89702e92012-07-25 16:46:31 +000037 const TargetLibraryInfo *TLI) {
38 if (!TLI->has(LibFunc::strlen))
Craig Topperf40110f2014-04-25 05:29:35 +000039 return nullptr;
Nuno Lopes89702e92012-07-25 16:46:31 +000040
Evan Chengad6efbf2014-03-12 18:09:37 +000041 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling201d7b22013-01-26 00:03:11 +000042 AttributeSet AS[2];
43 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
Bill Wendlingc79e42c2012-12-22 00:37:52 +000044 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
Craig Toppere1d12942014-08-27 05:25:25 +000045 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs);
Eric Christopher87abfc52010-03-05 22:25:30 +000046
Chandler Carruth7ec50852012-11-01 08:07:29 +000047 LLVMContext &Context = B.GetInsertBlock()->getContext();
Mehdi Aminia28d91d2015-03-10 02:37:25 +000048 Constant *StrLen = M->getOrInsertFunction(
49 "strlen", AttributeSet::get(M->getContext(), AS),
50 DL.getIntPtrType(Context), B.getInt8PtrTy(), nullptr);
Eric Christopher87abfc52010-03-05 22:25:30 +000051 CallInst *CI = B.CreateCall(StrLen, CastToCStr(Ptr, B), "strlen");
Evan Chengad6efbf2014-03-12 18:09:37 +000052 if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts()))
53 CI->setCallingConv(F->getCallingConv());
54
Eric Christopher87abfc52010-03-05 22:25:30 +000055 return CI;
56}
57
58/// EmitStrChr - Emit a call to the strchr function to the builder, for the
59/// specified pointer and character. Ptr is required to be some pointer type,
60/// and the return value has 'i8*' type.
61Value *llvm::EmitStrChr(Value *Ptr, char C, IRBuilder<> &B,
Mehdi Aminia28d91d2015-03-10 02:37:25 +000062 const TargetLibraryInfo *TLI) {
Nuno Lopes89702e92012-07-25 16:46:31 +000063 if (!TLI->has(LibFunc::strchr))
Craig Topperf40110f2014-04-25 05:29:35 +000064 return nullptr;
Nuno Lopes89702e92012-07-25 16:46:31 +000065
Evan Chengad6efbf2014-03-12 18:09:37 +000066 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendlingc79e42c2012-12-22 00:37:52 +000067 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
Bill Wendling201d7b22013-01-26 00:03:11 +000068 AttributeSet AS =
Craig Toppere1d12942014-08-27 05:25:25 +000069 AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs);
Eric Christopher87abfc52010-03-05 22:25:30 +000070
Chris Lattner229907c2011-07-18 04:54:35 +000071 Type *I8Ptr = B.getInt8PtrTy();
72 Type *I32Ty = B.getInt32Ty();
Bill Wendlingf86efb92012-11-20 05:09:20 +000073 Constant *StrChr = M->getOrInsertFunction("strchr",
Bill Wendlinge94d8432012-12-07 23:16:57 +000074 AttributeSet::get(M->getContext(),
Bill Wendling201d7b22013-01-26 00:03:11 +000075 AS),
Reid Kleckner971c3ea2014-11-13 22:55:19 +000076 I8Ptr, I8Ptr, I32Ty, nullptr);
David Blaikieff6409d2015-05-18 22:13:54 +000077 CallInst *CI = B.CreateCall(
78 StrChr, {CastToCStr(Ptr, B), ConstantInt::get(I32Ty, C)}, "strchr");
Evan Chengad6efbf2014-03-12 18:09:37 +000079 if (const Function *F = dyn_cast<Function>(StrChr->stripPointerCasts()))
80 CI->setCallingConv(F->getCallingConv());
Eric Christopher87abfc52010-03-05 22:25:30 +000081 return CI;
82}
83
Benjamin Kramer11188602010-06-15 21:34:25 +000084/// EmitStrNCmp - Emit a call to the strncmp function to the builder.
Mehdi Aminia28d91d2015-03-10 02:37:25 +000085Value *llvm::EmitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
86 const DataLayout &DL, const TargetLibraryInfo *TLI) {
Nuno Lopes89702e92012-07-25 16:46:31 +000087 if (!TLI->has(LibFunc::strncmp))
Craig Topperf40110f2014-04-25 05:29:35 +000088 return nullptr;
Nuno Lopes89702e92012-07-25 16:46:31 +000089
Evan Chengad6efbf2014-03-12 18:09:37 +000090 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling201d7b22013-01-26 00:03:11 +000091 AttributeSet AS[3];
92 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
93 AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
Bill Wendlingc79e42c2012-12-22 00:37:52 +000094 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
Craig Toppere1d12942014-08-27 05:25:25 +000095 AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs);
Benjamin Kramer11188602010-06-15 21:34:25 +000096
Chandler Carruth7ec50852012-11-01 08:07:29 +000097 LLVMContext &Context = B.GetInsertBlock()->getContext();
Mehdi Aminia28d91d2015-03-10 02:37:25 +000098 Value *StrNCmp = M->getOrInsertFunction(
99 "strncmp", AttributeSet::get(M->getContext(), AS), B.getInt32Ty(),
100 B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context), nullptr);
David Blaikieff6409d2015-05-18 22:13:54 +0000101 CallInst *CI = B.CreateCall(
102 StrNCmp, {CastToCStr(Ptr1, B), CastToCStr(Ptr2, B), Len}, "strncmp");
Evan Chengad6efbf2014-03-12 18:09:37 +0000103
104 if (const Function *F = dyn_cast<Function>(StrNCmp->stripPointerCasts()))
105 CI->setCallingConv(F->getCallingConv());
106
Benjamin Kramer11188602010-06-15 21:34:25 +0000107 return CI;
108}
109
Eric Christopher87abfc52010-03-05 22:25:30 +0000110/// EmitStrCpy - Emit a call to the strcpy function to the builder, for the
111/// specified pointer arguments.
112Value *llvm::EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000113 const TargetLibraryInfo *TLI, StringRef Name) {
Nuno Lopes89702e92012-07-25 16:46:31 +0000114 if (!TLI->has(LibFunc::strcpy))
Craig Topperf40110f2014-04-25 05:29:35 +0000115 return nullptr;
Nuno Lopes89702e92012-07-25 16:46:31 +0000116
Evan Chengad6efbf2014-03-12 18:09:37 +0000117 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling201d7b22013-01-26 00:03:11 +0000118 AttributeSet AS[2];
119 AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
120 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
121 Attribute::NoUnwind);
Chris Lattner229907c2011-07-18 04:54:35 +0000122 Type *I8Ptr = B.getInt8PtrTy();
Bill Wendlingf86efb92012-11-20 05:09:20 +0000123 Value *StrCpy = M->getOrInsertFunction(Name,
Bill Wendling201d7b22013-01-26 00:03:11 +0000124 AttributeSet::get(M->getContext(), AS),
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000125 I8Ptr, I8Ptr, I8Ptr, nullptr);
David Blaikieff6409d2015-05-18 22:13:54 +0000126 CallInst *CI =
127 B.CreateCall(StrCpy, {CastToCStr(Dst, B), CastToCStr(Src, B)}, Name);
Evan Chengad6efbf2014-03-12 18:09:37 +0000128 if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts()))
129 CI->setCallingConv(F->getCallingConv());
Eric Christopher87abfc52010-03-05 22:25:30 +0000130 return CI;
131}
132
Eric Christopher103e3ef2010-03-11 17:45:38 +0000133/// EmitStrNCpy - Emit a call to the strncpy function to the builder, for the
Eric Christopher43dc11c2010-03-11 01:25:07 +0000134/// specified pointer arguments.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000135Value *llvm::EmitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
Nuno Lopes89702e92012-07-25 16:46:31 +0000136 const TargetLibraryInfo *TLI, StringRef Name) {
137 if (!TLI->has(LibFunc::strncpy))
Craig Topperf40110f2014-04-25 05:29:35 +0000138 return nullptr;
Nuno Lopes89702e92012-07-25 16:46:31 +0000139
Evan Chengad6efbf2014-03-12 18:09:37 +0000140 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling201d7b22013-01-26 00:03:11 +0000141 AttributeSet AS[2];
142 AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
143 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
144 Attribute::NoUnwind);
Chris Lattner229907c2011-07-18 04:54:35 +0000145 Type *I8Ptr = B.getInt8PtrTy();
Bill Wendlingf86efb92012-11-20 05:09:20 +0000146 Value *StrNCpy = M->getOrInsertFunction(Name,
Bill Wendlinge94d8432012-12-07 23:16:57 +0000147 AttributeSet::get(M->getContext(),
Bill Wendling201d7b22013-01-26 00:03:11 +0000148 AS),
Eric Christophere8b281c2010-04-07 23:00:07 +0000149 I8Ptr, I8Ptr, I8Ptr,
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000150 Len->getType(), nullptr);
David Blaikieff6409d2015-05-18 22:13:54 +0000151 CallInst *CI = B.CreateCall(
152 StrNCpy, {CastToCStr(Dst, B), CastToCStr(Src, B), Len}, "strncpy");
Evan Chengad6efbf2014-03-12 18:09:37 +0000153 if (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts()))
154 CI->setCallingConv(F->getCallingConv());
Eric Christopher43dc11c2010-03-11 01:25:07 +0000155 return CI;
156}
157
Evan Chengd9e82232010-03-23 15:48:04 +0000158/// EmitMemCpyChk - Emit a call to the __memcpy_chk function to the builder.
159/// This expects that the Len and ObjSize have type 'intptr_t' and Dst/Src
160/// are pointers.
161Value *llvm::EmitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000162 IRBuilder<> &B, const DataLayout &DL,
Nuno Lopes89702e92012-07-25 16:46:31 +0000163 const TargetLibraryInfo *TLI) {
164 if (!TLI->has(LibFunc::memcpy_chk))
Craig Topperf40110f2014-04-25 05:29:35 +0000165 return nullptr;
Nuno Lopes89702e92012-07-25 16:46:31 +0000166
Evan Chengad6efbf2014-03-12 18:09:37 +0000167 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling201d7b22013-01-26 00:03:11 +0000168 AttributeSet AS;
169 AS = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
170 Attribute::NoUnwind);
Chandler Carruth7ec50852012-11-01 08:07:29 +0000171 LLVMContext &Context = B.GetInsertBlock()->getContext();
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000172 Value *MemCpy = M->getOrInsertFunction(
173 "__memcpy_chk", AttributeSet::get(M->getContext(), AS), B.getInt8PtrTy(),
174 B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context),
175 DL.getIntPtrType(Context), nullptr);
Evan Chengd9e82232010-03-23 15:48:04 +0000176 Dst = CastToCStr(Dst, B);
177 Src = CastToCStr(Src, B);
David Blaikieff6409d2015-05-18 22:13:54 +0000178 CallInst *CI = B.CreateCall(MemCpy, {Dst, Src, Len, ObjSize});
Evan Chengad6efbf2014-03-12 18:09:37 +0000179 if (const Function *F = dyn_cast<Function>(MemCpy->stripPointerCasts()))
180 CI->setCallingConv(F->getCallingConv());
Evan Chengd9e82232010-03-23 15:48:04 +0000181 return CI;
182}
183
Eric Christopher87abfc52010-03-05 22:25:30 +0000184/// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
185/// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000186Value *llvm::EmitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B,
187 const DataLayout &DL, const TargetLibraryInfo *TLI) {
Nuno Lopes89702e92012-07-25 16:46:31 +0000188 if (!TLI->has(LibFunc::memchr))
Craig Topperf40110f2014-04-25 05:29:35 +0000189 return nullptr;
Nuno Lopes89702e92012-07-25 16:46:31 +0000190
Evan Chengad6efbf2014-03-12 18:09:37 +0000191 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling201d7b22013-01-26 00:03:11 +0000192 AttributeSet AS;
Bill Wendlingc79e42c2012-12-22 00:37:52 +0000193 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
Craig Toppere1d12942014-08-27 05:25:25 +0000194 AS = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs);
Chandler Carruth7ec50852012-11-01 08:07:29 +0000195 LLVMContext &Context = B.GetInsertBlock()->getContext();
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000196 Value *MemChr = M->getOrInsertFunction(
197 "memchr", AttributeSet::get(M->getContext(), AS), B.getInt8PtrTy(),
198 B.getInt8PtrTy(), B.getInt32Ty(), DL.getIntPtrType(Context), nullptr);
David Blaikieff6409d2015-05-18 22:13:54 +0000199 CallInst *CI = B.CreateCall(MemChr, {CastToCStr(Ptr, B), Val, Len}, "memchr");
Evan Chengad6efbf2014-03-12 18:09:37 +0000200
201 if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts()))
202 CI->setCallingConv(F->getCallingConv());
203
Eric Christopher87abfc52010-03-05 22:25:30 +0000204 return CI;
205}
206
207/// EmitMemCmp - Emit a call to the memcmp function.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000208Value *llvm::EmitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
209 const DataLayout &DL, const TargetLibraryInfo *TLI) {
Nuno Lopes89702e92012-07-25 16:46:31 +0000210 if (!TLI->has(LibFunc::memcmp))
Craig Topperf40110f2014-04-25 05:29:35 +0000211 return nullptr;
Nuno Lopes89702e92012-07-25 16:46:31 +0000212
Evan Chengad6efbf2014-03-12 18:09:37 +0000213 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling201d7b22013-01-26 00:03:11 +0000214 AttributeSet AS[3];
215 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
216 AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
Bill Wendlingc79e42c2012-12-22 00:37:52 +0000217 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
Craig Toppere1d12942014-08-27 05:25:25 +0000218 AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs);
Eric Christopher87abfc52010-03-05 22:25:30 +0000219
Chandler Carruth7ec50852012-11-01 08:07:29 +0000220 LLVMContext &Context = B.GetInsertBlock()->getContext();
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000221 Value *MemCmp = M->getOrInsertFunction(
222 "memcmp", AttributeSet::get(M->getContext(), AS), B.getInt32Ty(),
223 B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context), nullptr);
David Blaikieff6409d2015-05-18 22:13:54 +0000224 CallInst *CI = B.CreateCall(
225 MemCmp, {CastToCStr(Ptr1, B), CastToCStr(Ptr2, B), Len}, "memcmp");
Evan Chengad6efbf2014-03-12 18:09:37 +0000226
227 if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts()))
228 CI->setCallingConv(F->getCallingConv());
229
Eric Christopher87abfc52010-03-05 22:25:30 +0000230 return CI;
231}
232
Yi Jiang6ab044e2013-12-16 22:42:40 +0000233/// Append a suffix to the function name according to the type of 'Op'.
234static void AppendTypeSuffix(Value *Op, StringRef &Name, SmallString<20> &NameBuffer) {
235 if (!Op->getType()->isDoubleTy()) {
236 NameBuffer += Name;
237
238 if (Op->getType()->isFloatTy())
239 NameBuffer += 'f';
240 else
241 NameBuffer += 'l';
242
243 Name = NameBuffer;
244 }
245 return;
246}
247
Eric Christopher87abfc52010-03-05 22:25:30 +0000248/// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
249/// 'floor'). This function is known to take a single of type matching 'Op' and
250/// returns one value with the same type. If 'Op' is a long double, 'l' is
251/// added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
Benjamin Kramerb106bcc2011-11-15 19:12:09 +0000252Value *llvm::EmitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B,
Bill Wendlinge94d8432012-12-07 23:16:57 +0000253 const AttributeSet &Attrs) {
Benjamin Kramerb106bcc2011-11-15 19:12:09 +0000254 SmallString<20> NameBuffer;
Yi Jiang6ab044e2013-12-16 22:42:40 +0000255 AppendTypeSuffix(Op, Name, NameBuffer);
Eric Christopher87abfc52010-03-05 22:25:30 +0000256
Evan Chengad6efbf2014-03-12 18:09:37 +0000257 Module *M = B.GetInsertBlock()->getParent()->getParent();
Eric Christopher87abfc52010-03-05 22:25:30 +0000258 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000259 Op->getType(), nullptr);
Eric Christopher87abfc52010-03-05 22:25:30 +0000260 CallInst *CI = B.CreateCall(Callee, Op, Name);
261 CI->setAttributes(Attrs);
Evan Chengad6efbf2014-03-12 18:09:37 +0000262 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
263 CI->setCallingConv(F->getCallingConv());
264
Eric Christopher87abfc52010-03-05 22:25:30 +0000265 return CI;
266}
267
Yi Jiang6ab044e2013-12-16 22:42:40 +0000268/// EmitBinaryFloatFnCall - Emit a call to the binary function named 'Name'
269/// (e.g. 'fmin'). This function is known to take type matching 'Op1' and 'Op2'
270/// and return one value with the same type. If 'Op1/Op2' are long double, 'l'
271/// is added as the suffix of name, if 'Op1/Op2' is a float, we add a 'f'
272/// suffix.
273Value *llvm::EmitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name,
274 IRBuilder<> &B, const AttributeSet &Attrs) {
275 SmallString<20> NameBuffer;
276 AppendTypeSuffix(Op1, Name, NameBuffer);
277
Evan Chengad6efbf2014-03-12 18:09:37 +0000278 Module *M = B.GetInsertBlock()->getParent()->getParent();
Yi Jiang6ab044e2013-12-16 22:42:40 +0000279 Value *Callee = M->getOrInsertFunction(Name, Op1->getType(),
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000280 Op1->getType(), Op2->getType(), nullptr);
David Blaikieff6409d2015-05-18 22:13:54 +0000281 CallInst *CI = B.CreateCall(Callee, {Op1, Op2}, Name);
Yi Jiang6ab044e2013-12-16 22:42:40 +0000282 CI->setAttributes(Attrs);
Evan Chengad6efbf2014-03-12 18:09:37 +0000283 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
284 CI->setCallingConv(F->getCallingConv());
285
Yi Jiang6ab044e2013-12-16 22:42:40 +0000286 return CI;
287}
288
Eric Christopher87abfc52010-03-05 22:25:30 +0000289/// EmitPutChar - Emit a call to the putchar function. This assumes that Char
290/// is an integer.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000291Value *llvm::EmitPutChar(Value *Char, IRBuilder<> &B,
Nuno Lopes89702e92012-07-25 16:46:31 +0000292 const TargetLibraryInfo *TLI) {
293 if (!TLI->has(LibFunc::putchar))
Craig Topperf40110f2014-04-25 05:29:35 +0000294 return nullptr;
Nuno Lopes89702e92012-07-25 16:46:31 +0000295
Evan Chengad6efbf2014-03-12 18:09:37 +0000296 Module *M = B.GetInsertBlock()->getParent()->getParent();
Eric Christopher87abfc52010-03-05 22:25:30 +0000297 Value *PutChar = M->getOrInsertFunction("putchar", B.getInt32Ty(),
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000298 B.getInt32Ty(), nullptr);
Eric Christopher87abfc52010-03-05 22:25:30 +0000299 CallInst *CI = B.CreateCall(PutChar,
300 B.CreateIntCast(Char,
301 B.getInt32Ty(),
302 /*isSigned*/true,
303 "chari"),
304 "putchar");
Evan Chengad6efbf2014-03-12 18:09:37 +0000305
306 if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts()))
307 CI->setCallingConv(F->getCallingConv());
Eric Christopher87abfc52010-03-05 22:25:30 +0000308 return CI;
309}
310
311/// EmitPutS - Emit a call to the puts function. This assumes that Str is
312/// some pointer.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000313Value *llvm::EmitPutS(Value *Str, IRBuilder<> &B,
Nuno Lopes89702e92012-07-25 16:46:31 +0000314 const TargetLibraryInfo *TLI) {
315 if (!TLI->has(LibFunc::puts))
Craig Topperf40110f2014-04-25 05:29:35 +0000316 return nullptr;
Nuno Lopes89702e92012-07-25 16:46:31 +0000317
Evan Chengad6efbf2014-03-12 18:09:37 +0000318 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling201d7b22013-01-26 00:03:11 +0000319 AttributeSet AS[2];
320 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
321 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
322 Attribute::NoUnwind);
Eric Christopher87abfc52010-03-05 22:25:30 +0000323
Bill Wendlingf86efb92012-11-20 05:09:20 +0000324 Value *PutS = M->getOrInsertFunction("puts",
Bill Wendling201d7b22013-01-26 00:03:11 +0000325 AttributeSet::get(M->getContext(), AS),
Eric Christopher87abfc52010-03-05 22:25:30 +0000326 B.getInt32Ty(),
327 B.getInt8PtrTy(),
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000328 nullptr);
Eric Christopher87abfc52010-03-05 22:25:30 +0000329 CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts");
Evan Chengad6efbf2014-03-12 18:09:37 +0000330 if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
331 CI->setCallingConv(F->getCallingConv());
Nuno Lopes89702e92012-07-25 16:46:31 +0000332 return CI;
Eric Christopher87abfc52010-03-05 22:25:30 +0000333}
334
335/// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
336/// an integer and File is a pointer to FILE.
Nuno Lopes89702e92012-07-25 16:46:31 +0000337Value *llvm::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000338 const TargetLibraryInfo *TLI) {
Nuno Lopes89702e92012-07-25 16:46:31 +0000339 if (!TLI->has(LibFunc::fputc))
Craig Topperf40110f2014-04-25 05:29:35 +0000340 return nullptr;
Nuno Lopes89702e92012-07-25 16:46:31 +0000341
Evan Chengad6efbf2014-03-12 18:09:37 +0000342 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling201d7b22013-01-26 00:03:11 +0000343 AttributeSet AS[2];
344 AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
345 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
346 Attribute::NoUnwind);
Eric Christopher87abfc52010-03-05 22:25:30 +0000347 Constant *F;
348 if (File->getType()->isPointerTy())
Bill Wendlingf86efb92012-11-20 05:09:20 +0000349 F = M->getOrInsertFunction("fputc",
Bill Wendling201d7b22013-01-26 00:03:11 +0000350 AttributeSet::get(M->getContext(), AS),
Eric Christopher87abfc52010-03-05 22:25:30 +0000351 B.getInt32Ty(),
352 B.getInt32Ty(), File->getType(),
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000353 nullptr);
Eric Christopher87abfc52010-03-05 22:25:30 +0000354 else
355 F = M->getOrInsertFunction("fputc",
356 B.getInt32Ty(),
357 B.getInt32Ty(),
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000358 File->getType(), nullptr);
Eric Christopher87abfc52010-03-05 22:25:30 +0000359 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
360 "chari");
David Blaikieff6409d2015-05-18 22:13:54 +0000361 CallInst *CI = B.CreateCall(F, {Char, File}, "fputc");
Evan Chengad6efbf2014-03-12 18:09:37 +0000362
363 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
364 CI->setCallingConv(Fn->getCallingConv());
Nuno Lopes89702e92012-07-25 16:46:31 +0000365 return CI;
Eric Christopher87abfc52010-03-05 22:25:30 +0000366}
367
368/// EmitFPutS - Emit a call to the puts function. Str is required to be a
369/// pointer and File is a pointer to FILE.
Nuno Lopes89702e92012-07-25 16:46:31 +0000370Value *llvm::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000371 const TargetLibraryInfo *TLI) {
Nuno Lopes89702e92012-07-25 16:46:31 +0000372 if (!TLI->has(LibFunc::fputs))
Craig Topperf40110f2014-04-25 05:29:35 +0000373 return nullptr;
Nuno Lopes89702e92012-07-25 16:46:31 +0000374
Evan Chengad6efbf2014-03-12 18:09:37 +0000375 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling201d7b22013-01-26 00:03:11 +0000376 AttributeSet AS[3];
377 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
378 AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
379 AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
380 Attribute::NoUnwind);
Eli Friedman489c0ff2011-11-17 01:27:36 +0000381 StringRef FPutsName = TLI->getName(LibFunc::fputs);
Eric Christopher87abfc52010-03-05 22:25:30 +0000382 Constant *F;
383 if (File->getType()->isPointerTy())
Bill Wendlingf86efb92012-11-20 05:09:20 +0000384 F = M->getOrInsertFunction(FPutsName,
Bill Wendling201d7b22013-01-26 00:03:11 +0000385 AttributeSet::get(M->getContext(), AS),
Eric Christopher87abfc52010-03-05 22:25:30 +0000386 B.getInt32Ty(),
387 B.getInt8PtrTy(),
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000388 File->getType(), nullptr);
Eric Christopher87abfc52010-03-05 22:25:30 +0000389 else
Eli Friedman489c0ff2011-11-17 01:27:36 +0000390 F = M->getOrInsertFunction(FPutsName, B.getInt32Ty(),
Eric Christopher87abfc52010-03-05 22:25:30 +0000391 B.getInt8PtrTy(),
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000392 File->getType(), nullptr);
David Blaikieff6409d2015-05-18 22:13:54 +0000393 CallInst *CI = B.CreateCall(F, {CastToCStr(Str, B), File}, "fputs");
Evan Chengad6efbf2014-03-12 18:09:37 +0000394
395 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
396 CI->setCallingConv(Fn->getCallingConv());
Nuno Lopes89702e92012-07-25 16:46:31 +0000397 return CI;
Eric Christopher87abfc52010-03-05 22:25:30 +0000398}
399
400/// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
401/// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000402Value *llvm::EmitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B,
403 const DataLayout &DL, const TargetLibraryInfo *TLI) {
Nuno Lopes89702e92012-07-25 16:46:31 +0000404 if (!TLI->has(LibFunc::fwrite))
Craig Topperf40110f2014-04-25 05:29:35 +0000405 return nullptr;
Nuno Lopes89702e92012-07-25 16:46:31 +0000406
Evan Chengad6efbf2014-03-12 18:09:37 +0000407 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling201d7b22013-01-26 00:03:11 +0000408 AttributeSet AS[3];
409 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
410 AS[1] = AttributeSet::get(M->getContext(), 4, Attribute::NoCapture);
411 AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
412 Attribute::NoUnwind);
Chandler Carruth7ec50852012-11-01 08:07:29 +0000413 LLVMContext &Context = B.GetInsertBlock()->getContext();
Eli Friedman489c0ff2011-11-17 01:27:36 +0000414 StringRef FWriteName = TLI->getName(LibFunc::fwrite);
Eric Christopher87abfc52010-03-05 22:25:30 +0000415 Constant *F;
416 if (File->getType()->isPointerTy())
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000417 F = M->getOrInsertFunction(
418 FWriteName, AttributeSet::get(M->getContext(), AS),
419 DL.getIntPtrType(Context), B.getInt8PtrTy(), DL.getIntPtrType(Context),
420 DL.getIntPtrType(Context), File->getType(), nullptr);
Eric Christopher87abfc52010-03-05 22:25:30 +0000421 else
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000422 F = M->getOrInsertFunction(FWriteName, DL.getIntPtrType(Context),
423 B.getInt8PtrTy(), DL.getIntPtrType(Context),
424 DL.getIntPtrType(Context), File->getType(),
425 nullptr);
426 CallInst *CI =
David Blaikieff6409d2015-05-18 22:13:54 +0000427 B.CreateCall(F, {CastToCStr(Ptr, B), Size,
428 ConstantInt::get(DL.getIntPtrType(Context), 1), File});
Evan Chengad6efbf2014-03-12 18:09:37 +0000429
430 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
431 CI->setCallingConv(Fn->getCallingConv());
Nuno Lopes89702e92012-07-25 16:46:31 +0000432 return CI;
Eric Christopher87abfc52010-03-05 22:25:30 +0000433}