blob: 8aa7b2a65ba9c0e80772e77ad39d186031aa3237 [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"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#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 Carruth62d42152015-01-15 02:16:27 +000024#include "llvm/Analysis/TargetLibraryInfo.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
Nuno Lopes7ba5b982012-07-25 17:18:59 +000058/// 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.
61Value *llvm::EmitStrNLen(Value *Ptr, Value *MaxLen, IRBuilder<> &B,
Mehdi Aminia28d91d2015-03-10 02:37:25 +000062 const DataLayout &DL, const TargetLibraryInfo *TLI) {
Nuno Lopes7ba5b982012-07-25 17:18:59 +000063 if (!TLI->has(LibFunc::strnlen))
Craig Topperf40110f2014-04-25 05:29:35 +000064 return nullptr;
Nuno Lopes7ba5b982012-07-25 17:18:59 +000065
Evan Chengad6efbf2014-03-12 18:09:37 +000066 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling201d7b22013-01-26 00:03:11 +000067 AttributeSet AS[2];
68 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
Bill Wendlingc79e42c2012-12-22 00:37:52 +000069 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
Craig Toppere1d12942014-08-27 05:25:25 +000070 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs);
Nuno Lopes7ba5b982012-07-25 17:18:59 +000071
Chandler Carruth7ec50852012-11-01 08:07:29 +000072 LLVMContext &Context = B.GetInsertBlock()->getContext();
Mehdi Aminia28d91d2015-03-10 02:37:25 +000073 Constant *StrNLen =
74 M->getOrInsertFunction("strnlen", AttributeSet::get(M->getContext(), AS),
75 DL.getIntPtrType(Context), B.getInt8PtrTy(),
76 DL.getIntPtrType(Context), nullptr);
David Blaikieff6409d2015-05-18 22:13:54 +000077 CallInst *CI = B.CreateCall(StrNLen, {CastToCStr(Ptr, B), MaxLen}, "strnlen");
Evan Chengad6efbf2014-03-12 18:09:37 +000078 if (const Function *F = dyn_cast<Function>(StrNLen->stripPointerCasts()))
79 CI->setCallingConv(F->getCallingConv());
80
Nuno Lopes7ba5b982012-07-25 17:18:59 +000081 return CI;
82}
83
Eric Christopher87abfc52010-03-05 22:25:30 +000084/// 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.
87Value *llvm::EmitStrChr(Value *Ptr, char C, IRBuilder<> &B,
Mehdi Aminia28d91d2015-03-10 02:37:25 +000088 const TargetLibraryInfo *TLI) {
Nuno Lopes89702e92012-07-25 16:46:31 +000089 if (!TLI->has(LibFunc::strchr))
Craig Topperf40110f2014-04-25 05:29:35 +000090 return nullptr;
Nuno Lopes89702e92012-07-25 16:46:31 +000091
Evan Chengad6efbf2014-03-12 18:09:37 +000092 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendlingc79e42c2012-12-22 00:37:52 +000093 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
Bill Wendling201d7b22013-01-26 00:03:11 +000094 AttributeSet AS =
Craig Toppere1d12942014-08-27 05:25:25 +000095 AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs);
Eric Christopher87abfc52010-03-05 22:25:30 +000096
Chris Lattner229907c2011-07-18 04:54:35 +000097 Type *I8Ptr = B.getInt8PtrTy();
98 Type *I32Ty = B.getInt32Ty();
Bill Wendlingf86efb92012-11-20 05:09:20 +000099 Constant *StrChr = M->getOrInsertFunction("strchr",
Bill Wendlinge94d8432012-12-07 23:16:57 +0000100 AttributeSet::get(M->getContext(),
Bill Wendling201d7b22013-01-26 00:03:11 +0000101 AS),
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000102 I8Ptr, I8Ptr, I32Ty, nullptr);
David Blaikieff6409d2015-05-18 22:13:54 +0000103 CallInst *CI = B.CreateCall(
104 StrChr, {CastToCStr(Ptr, B), ConstantInt::get(I32Ty, C)}, "strchr");
Evan Chengad6efbf2014-03-12 18:09:37 +0000105 if (const Function *F = dyn_cast<Function>(StrChr->stripPointerCasts()))
106 CI->setCallingConv(F->getCallingConv());
Eric Christopher87abfc52010-03-05 22:25:30 +0000107 return CI;
108}
109
Benjamin Kramer11188602010-06-15 21:34:25 +0000110/// EmitStrNCmp - Emit a call to the strncmp function to the builder.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000111Value *llvm::EmitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
112 const DataLayout &DL, const TargetLibraryInfo *TLI) {
Nuno Lopes89702e92012-07-25 16:46:31 +0000113 if (!TLI->has(LibFunc::strncmp))
Craig Topperf40110f2014-04-25 05:29:35 +0000114 return nullptr;
Nuno Lopes89702e92012-07-25 16:46:31 +0000115
Evan Chengad6efbf2014-03-12 18:09:37 +0000116 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling201d7b22013-01-26 00:03:11 +0000117 AttributeSet AS[3];
118 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
119 AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
Bill Wendlingc79e42c2012-12-22 00:37:52 +0000120 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
Craig Toppere1d12942014-08-27 05:25:25 +0000121 AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs);
Benjamin Kramer11188602010-06-15 21:34:25 +0000122
Chandler Carruth7ec50852012-11-01 08:07:29 +0000123 LLVMContext &Context = B.GetInsertBlock()->getContext();
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000124 Value *StrNCmp = M->getOrInsertFunction(
125 "strncmp", AttributeSet::get(M->getContext(), AS), B.getInt32Ty(),
126 B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context), nullptr);
David Blaikieff6409d2015-05-18 22:13:54 +0000127 CallInst *CI = B.CreateCall(
128 StrNCmp, {CastToCStr(Ptr1, B), CastToCStr(Ptr2, B), Len}, "strncmp");
Evan Chengad6efbf2014-03-12 18:09:37 +0000129
130 if (const Function *F = dyn_cast<Function>(StrNCmp->stripPointerCasts()))
131 CI->setCallingConv(F->getCallingConv());
132
Benjamin Kramer11188602010-06-15 21:34:25 +0000133 return CI;
134}
135
Eric Christopher87abfc52010-03-05 22:25:30 +0000136/// EmitStrCpy - Emit a call to the strcpy function to the builder, for the
137/// specified pointer arguments.
138Value *llvm::EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000139 const TargetLibraryInfo *TLI, StringRef Name) {
Nuno Lopes89702e92012-07-25 16:46:31 +0000140 if (!TLI->has(LibFunc::strcpy))
Craig Topperf40110f2014-04-25 05:29:35 +0000141 return nullptr;
Nuno Lopes89702e92012-07-25 16:46:31 +0000142
Evan Chengad6efbf2014-03-12 18:09:37 +0000143 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling201d7b22013-01-26 00:03:11 +0000144 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 Lattner229907c2011-07-18 04:54:35 +0000148 Type *I8Ptr = B.getInt8PtrTy();
Bill Wendlingf86efb92012-11-20 05:09:20 +0000149 Value *StrCpy = M->getOrInsertFunction(Name,
Bill Wendling201d7b22013-01-26 00:03:11 +0000150 AttributeSet::get(M->getContext(), AS),
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000151 I8Ptr, I8Ptr, I8Ptr, nullptr);
David Blaikieff6409d2015-05-18 22:13:54 +0000152 CallInst *CI =
153 B.CreateCall(StrCpy, {CastToCStr(Dst, B), CastToCStr(Src, B)}, Name);
Evan Chengad6efbf2014-03-12 18:09:37 +0000154 if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts()))
155 CI->setCallingConv(F->getCallingConv());
Eric Christopher87abfc52010-03-05 22:25:30 +0000156 return CI;
157}
158
Eric Christopher103e3ef2010-03-11 17:45:38 +0000159/// EmitStrNCpy - Emit a call to the strncpy function to the builder, for the
Eric Christopher43dc11c2010-03-11 01:25:07 +0000160/// specified pointer arguments.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000161Value *llvm::EmitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
Nuno Lopes89702e92012-07-25 16:46:31 +0000162 const TargetLibraryInfo *TLI, StringRef Name) {
163 if (!TLI->has(LibFunc::strncpy))
Craig Topperf40110f2014-04-25 05:29:35 +0000164 return nullptr;
Nuno Lopes89702e92012-07-25 16:46:31 +0000165
Evan Chengad6efbf2014-03-12 18:09:37 +0000166 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling201d7b22013-01-26 00:03:11 +0000167 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 Lattner229907c2011-07-18 04:54:35 +0000171 Type *I8Ptr = B.getInt8PtrTy();
Bill Wendlingf86efb92012-11-20 05:09:20 +0000172 Value *StrNCpy = M->getOrInsertFunction(Name,
Bill Wendlinge94d8432012-12-07 23:16:57 +0000173 AttributeSet::get(M->getContext(),
Bill Wendling201d7b22013-01-26 00:03:11 +0000174 AS),
Eric Christophere8b281c2010-04-07 23:00:07 +0000175 I8Ptr, I8Ptr, I8Ptr,
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000176 Len->getType(), nullptr);
David Blaikieff6409d2015-05-18 22:13:54 +0000177 CallInst *CI = B.CreateCall(
178 StrNCpy, {CastToCStr(Dst, B), CastToCStr(Src, B), Len}, "strncpy");
Evan Chengad6efbf2014-03-12 18:09:37 +0000179 if (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts()))
180 CI->setCallingConv(F->getCallingConv());
Eric Christopher43dc11c2010-03-11 01:25:07 +0000181 return CI;
182}
183
Evan Chengd9e82232010-03-23 15:48:04 +0000184/// 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.
187Value *llvm::EmitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000188 IRBuilder<> &B, const DataLayout &DL,
Nuno Lopes89702e92012-07-25 16:46:31 +0000189 const TargetLibraryInfo *TLI) {
190 if (!TLI->has(LibFunc::memcpy_chk))
Craig Topperf40110f2014-04-25 05:29:35 +0000191 return nullptr;
Nuno Lopes89702e92012-07-25 16:46:31 +0000192
Evan Chengad6efbf2014-03-12 18:09:37 +0000193 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling201d7b22013-01-26 00:03:11 +0000194 AttributeSet AS;
195 AS = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
196 Attribute::NoUnwind);
Chandler Carruth7ec50852012-11-01 08:07:29 +0000197 LLVMContext &Context = B.GetInsertBlock()->getContext();
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000198 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 Chengd9e82232010-03-23 15:48:04 +0000202 Dst = CastToCStr(Dst, B);
203 Src = CastToCStr(Src, B);
David Blaikieff6409d2015-05-18 22:13:54 +0000204 CallInst *CI = B.CreateCall(MemCpy, {Dst, Src, Len, ObjSize});
Evan Chengad6efbf2014-03-12 18:09:37 +0000205 if (const Function *F = dyn_cast<Function>(MemCpy->stripPointerCasts()))
206 CI->setCallingConv(F->getCallingConv());
Evan Chengd9e82232010-03-23 15:48:04 +0000207 return CI;
208}
209
Eric Christopher87abfc52010-03-05 22:25:30 +0000210/// 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 Aminia28d91d2015-03-10 02:37:25 +0000212Value *llvm::EmitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B,
213 const DataLayout &DL, const TargetLibraryInfo *TLI) {
Nuno Lopes89702e92012-07-25 16:46:31 +0000214 if (!TLI->has(LibFunc::memchr))
Craig Topperf40110f2014-04-25 05:29:35 +0000215 return nullptr;
Nuno Lopes89702e92012-07-25 16:46:31 +0000216
Evan Chengad6efbf2014-03-12 18:09:37 +0000217 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling201d7b22013-01-26 00:03:11 +0000218 AttributeSet AS;
Bill Wendlingc79e42c2012-12-22 00:37:52 +0000219 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
Craig Toppere1d12942014-08-27 05:25:25 +0000220 AS = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs);
Chandler Carruth7ec50852012-11-01 08:07:29 +0000221 LLVMContext &Context = B.GetInsertBlock()->getContext();
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000222 Value *MemChr = M->getOrInsertFunction(
223 "memchr", AttributeSet::get(M->getContext(), AS), B.getInt8PtrTy(),
224 B.getInt8PtrTy(), B.getInt32Ty(), DL.getIntPtrType(Context), nullptr);
David Blaikieff6409d2015-05-18 22:13:54 +0000225 CallInst *CI = B.CreateCall(MemChr, {CastToCStr(Ptr, B), Val, Len}, "memchr");
Evan Chengad6efbf2014-03-12 18:09:37 +0000226
227 if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts()))
228 CI->setCallingConv(F->getCallingConv());
229
Eric Christopher87abfc52010-03-05 22:25:30 +0000230 return CI;
231}
232
233/// EmitMemCmp - Emit a call to the memcmp function.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000234Value *llvm::EmitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
235 const DataLayout &DL, const TargetLibraryInfo *TLI) {
Nuno Lopes89702e92012-07-25 16:46:31 +0000236 if (!TLI->has(LibFunc::memcmp))
Craig Topperf40110f2014-04-25 05:29:35 +0000237 return nullptr;
Nuno Lopes89702e92012-07-25 16:46:31 +0000238
Evan Chengad6efbf2014-03-12 18:09:37 +0000239 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling201d7b22013-01-26 00:03:11 +0000240 AttributeSet AS[3];
241 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
242 AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
Bill Wendlingc79e42c2012-12-22 00:37:52 +0000243 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
Craig Toppere1d12942014-08-27 05:25:25 +0000244 AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs);
Eric Christopher87abfc52010-03-05 22:25:30 +0000245
Chandler Carruth7ec50852012-11-01 08:07:29 +0000246 LLVMContext &Context = B.GetInsertBlock()->getContext();
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000247 Value *MemCmp = M->getOrInsertFunction(
248 "memcmp", AttributeSet::get(M->getContext(), AS), B.getInt32Ty(),
249 B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context), nullptr);
David Blaikieff6409d2015-05-18 22:13:54 +0000250 CallInst *CI = B.CreateCall(
251 MemCmp, {CastToCStr(Ptr1, B), CastToCStr(Ptr2, B), Len}, "memcmp");
Evan Chengad6efbf2014-03-12 18:09:37 +0000252
253 if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts()))
254 CI->setCallingConv(F->getCallingConv());
255
Eric Christopher87abfc52010-03-05 22:25:30 +0000256 return CI;
257}
258
Yi Jiang6ab044e2013-12-16 22:42:40 +0000259/// Append a suffix to the function name according to the type of 'Op'.
260static 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 Christopher87abfc52010-03-05 22:25:30 +0000274/// 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 Kramerb106bcc2011-11-15 19:12:09 +0000278Value *llvm::EmitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B,
Bill Wendlinge94d8432012-12-07 23:16:57 +0000279 const AttributeSet &Attrs) {
Benjamin Kramerb106bcc2011-11-15 19:12:09 +0000280 SmallString<20> NameBuffer;
Yi Jiang6ab044e2013-12-16 22:42:40 +0000281 AppendTypeSuffix(Op, Name, NameBuffer);
Eric Christopher87abfc52010-03-05 22:25:30 +0000282
Evan Chengad6efbf2014-03-12 18:09:37 +0000283 Module *M = B.GetInsertBlock()->getParent()->getParent();
Eric Christopher87abfc52010-03-05 22:25:30 +0000284 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000285 Op->getType(), nullptr);
Eric Christopher87abfc52010-03-05 22:25:30 +0000286 CallInst *CI = B.CreateCall(Callee, Op, Name);
287 CI->setAttributes(Attrs);
Evan Chengad6efbf2014-03-12 18:09:37 +0000288 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
289 CI->setCallingConv(F->getCallingConv());
290
Eric Christopher87abfc52010-03-05 22:25:30 +0000291 return CI;
292}
293
Yi Jiang6ab044e2013-12-16 22:42:40 +0000294/// 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.
299Value *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 Chengad6efbf2014-03-12 18:09:37 +0000304 Module *M = B.GetInsertBlock()->getParent()->getParent();
Yi Jiang6ab044e2013-12-16 22:42:40 +0000305 Value *Callee = M->getOrInsertFunction(Name, Op1->getType(),
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000306 Op1->getType(), Op2->getType(), nullptr);
David Blaikieff6409d2015-05-18 22:13:54 +0000307 CallInst *CI = B.CreateCall(Callee, {Op1, Op2}, Name);
Yi Jiang6ab044e2013-12-16 22:42:40 +0000308 CI->setAttributes(Attrs);
Evan Chengad6efbf2014-03-12 18:09:37 +0000309 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
310 CI->setCallingConv(F->getCallingConv());
311
Yi Jiang6ab044e2013-12-16 22:42:40 +0000312 return CI;
313}
314
Eric Christopher87abfc52010-03-05 22:25:30 +0000315/// EmitPutChar - Emit a call to the putchar function. This assumes that Char
316/// is an integer.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000317Value *llvm::EmitPutChar(Value *Char, IRBuilder<> &B,
Nuno Lopes89702e92012-07-25 16:46:31 +0000318 const TargetLibraryInfo *TLI) {
319 if (!TLI->has(LibFunc::putchar))
Craig Topperf40110f2014-04-25 05:29:35 +0000320 return nullptr;
Nuno Lopes89702e92012-07-25 16:46:31 +0000321
Evan Chengad6efbf2014-03-12 18:09:37 +0000322 Module *M = B.GetInsertBlock()->getParent()->getParent();
Eric Christopher87abfc52010-03-05 22:25:30 +0000323 Value *PutChar = M->getOrInsertFunction("putchar", B.getInt32Ty(),
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000324 B.getInt32Ty(), nullptr);
Eric Christopher87abfc52010-03-05 22:25:30 +0000325 CallInst *CI = B.CreateCall(PutChar,
326 B.CreateIntCast(Char,
327 B.getInt32Ty(),
328 /*isSigned*/true,
329 "chari"),
330 "putchar");
Evan Chengad6efbf2014-03-12 18:09:37 +0000331
332 if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts()))
333 CI->setCallingConv(F->getCallingConv());
Eric Christopher87abfc52010-03-05 22:25:30 +0000334 return CI;
335}
336
337/// EmitPutS - Emit a call to the puts function. This assumes that Str is
338/// some pointer.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000339Value *llvm::EmitPutS(Value *Str, IRBuilder<> &B,
Nuno Lopes89702e92012-07-25 16:46:31 +0000340 const TargetLibraryInfo *TLI) {
341 if (!TLI->has(LibFunc::puts))
Craig Topperf40110f2014-04-25 05:29:35 +0000342 return nullptr;
Nuno Lopes89702e92012-07-25 16:46:31 +0000343
Evan Chengad6efbf2014-03-12 18:09:37 +0000344 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling201d7b22013-01-26 00:03:11 +0000345 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 Christopher87abfc52010-03-05 22:25:30 +0000349
Bill Wendlingf86efb92012-11-20 05:09:20 +0000350 Value *PutS = M->getOrInsertFunction("puts",
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 nullptr);
Eric Christopher87abfc52010-03-05 22:25:30 +0000355 CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts");
Evan Chengad6efbf2014-03-12 18:09:37 +0000356 if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
357 CI->setCallingConv(F->getCallingConv());
Nuno Lopes89702e92012-07-25 16:46:31 +0000358 return CI;
Eric Christopher87abfc52010-03-05 22:25:30 +0000359}
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 Lopes89702e92012-07-25 16:46:31 +0000363Value *llvm::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000364 const TargetLibraryInfo *TLI) {
Nuno Lopes89702e92012-07-25 16:46:31 +0000365 if (!TLI->has(LibFunc::fputc))
Craig Topperf40110f2014-04-25 05:29:35 +0000366 return nullptr;
Nuno Lopes89702e92012-07-25 16:46:31 +0000367
Evan Chengad6efbf2014-03-12 18:09:37 +0000368 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling201d7b22013-01-26 00:03:11 +0000369 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 Christopher87abfc52010-03-05 22:25:30 +0000373 Constant *F;
374 if (File->getType()->isPointerTy())
Bill Wendlingf86efb92012-11-20 05:09:20 +0000375 F = M->getOrInsertFunction("fputc",
Bill Wendling201d7b22013-01-26 00:03:11 +0000376 AttributeSet::get(M->getContext(), AS),
Eric Christopher87abfc52010-03-05 22:25:30 +0000377 B.getInt32Ty(),
378 B.getInt32Ty(), File->getType(),
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000379 nullptr);
Eric Christopher87abfc52010-03-05 22:25:30 +0000380 else
381 F = M->getOrInsertFunction("fputc",
382 B.getInt32Ty(),
383 B.getInt32Ty(),
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000384 File->getType(), nullptr);
Eric Christopher87abfc52010-03-05 22:25:30 +0000385 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
386 "chari");
David Blaikieff6409d2015-05-18 22:13:54 +0000387 CallInst *CI = B.CreateCall(F, {Char, File}, "fputc");
Evan Chengad6efbf2014-03-12 18:09:37 +0000388
389 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
390 CI->setCallingConv(Fn->getCallingConv());
Nuno Lopes89702e92012-07-25 16:46:31 +0000391 return CI;
Eric Christopher87abfc52010-03-05 22:25:30 +0000392}
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 Lopes89702e92012-07-25 16:46:31 +0000396Value *llvm::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B,
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000397 const TargetLibraryInfo *TLI) {
Nuno Lopes89702e92012-07-25 16:46:31 +0000398 if (!TLI->has(LibFunc::fputs))
Craig Topperf40110f2014-04-25 05:29:35 +0000399 return nullptr;
Nuno Lopes89702e92012-07-25 16:46:31 +0000400
Evan Chengad6efbf2014-03-12 18:09:37 +0000401 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling201d7b22013-01-26 00:03:11 +0000402 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 Friedman489c0ff2011-11-17 01:27:36 +0000407 StringRef FPutsName = TLI->getName(LibFunc::fputs);
Eric Christopher87abfc52010-03-05 22:25:30 +0000408 Constant *F;
409 if (File->getType()->isPointerTy())
Bill Wendlingf86efb92012-11-20 05:09:20 +0000410 F = M->getOrInsertFunction(FPutsName,
Bill Wendling201d7b22013-01-26 00:03:11 +0000411 AttributeSet::get(M->getContext(), AS),
Eric Christopher87abfc52010-03-05 22:25:30 +0000412 B.getInt32Ty(),
413 B.getInt8PtrTy(),
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000414 File->getType(), nullptr);
Eric Christopher87abfc52010-03-05 22:25:30 +0000415 else
Eli Friedman489c0ff2011-11-17 01:27:36 +0000416 F = M->getOrInsertFunction(FPutsName, B.getInt32Ty(),
Eric Christopher87abfc52010-03-05 22:25:30 +0000417 B.getInt8PtrTy(),
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000418 File->getType(), nullptr);
David Blaikieff6409d2015-05-18 22:13:54 +0000419 CallInst *CI = B.CreateCall(F, {CastToCStr(Str, B), File}, "fputs");
Evan Chengad6efbf2014-03-12 18:09:37 +0000420
421 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
422 CI->setCallingConv(Fn->getCallingConv());
Nuno Lopes89702e92012-07-25 16:46:31 +0000423 return CI;
Eric Christopher87abfc52010-03-05 22:25:30 +0000424}
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 Aminia28d91d2015-03-10 02:37:25 +0000428Value *llvm::EmitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B,
429 const DataLayout &DL, const TargetLibraryInfo *TLI) {
Nuno Lopes89702e92012-07-25 16:46:31 +0000430 if (!TLI->has(LibFunc::fwrite))
Craig Topperf40110f2014-04-25 05:29:35 +0000431 return nullptr;
Nuno Lopes89702e92012-07-25 16:46:31 +0000432
Evan Chengad6efbf2014-03-12 18:09:37 +0000433 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling201d7b22013-01-26 00:03:11 +0000434 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 Carruth7ec50852012-11-01 08:07:29 +0000439 LLVMContext &Context = B.GetInsertBlock()->getContext();
Eli Friedman489c0ff2011-11-17 01:27:36 +0000440 StringRef FWriteName = TLI->getName(LibFunc::fwrite);
Eric Christopher87abfc52010-03-05 22:25:30 +0000441 Constant *F;
442 if (File->getType()->isPointerTy())
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000443 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 Christopher87abfc52010-03-05 22:25:30 +0000447 else
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000448 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 Blaikieff6409d2015-05-18 22:13:54 +0000453 B.CreateCall(F, {CastToCStr(Ptr, B), Size,
454 ConstantInt::get(DL.getIntPtrType(Context), 1), File});
Evan Chengad6efbf2014-03-12 18:09:37 +0000455
456 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
457 CI->setCallingConv(Fn->getCallingConv());
Nuno Lopes89702e92012-07-25 16:46:31 +0000458 return CI;
Eric Christopher87abfc52010-03-05 22:25:30 +0000459}