blob: be00b6956199db698fbf1117829026b460f08797 [file] [log] [blame]
Eric Christopherb6174e32010-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 Carruthd04a8d42012-12-03 16:50:05 +000015#include "llvm/ADT/SmallString.h"
Chandler Carruth0b8c9a82013-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"
Eli Friedman9d434db2011-11-17 01:27:36 +000024#include "llvm/Target/TargetLibraryInfo.h"
Eric Christopherb6174e32010-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) {
Stephen Hinesdce4a402014-05-29 02:49:00 -070030 unsigned AS = V->getType()->getPointerAddressSpace();
31 return B.CreateBitCast(V, B.getInt8PtrTy(AS), "cstr");
Eric Christopherb6174e32010-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.
Micah Villmow3574eca2012-10-08 16:38:25 +000036Value *llvm::EmitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +000037 const TargetLibraryInfo *TLI) {
38 if (!TLI->has(LibFunc::strlen))
Stephen Hinesdce4a402014-05-29 02:49:00 -070039 return nullptr;
Nuno Lopes51004df2012-07-25 16:46:31 +000040
Eric Christopherb6174e32010-03-05 22:25:30 +000041 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling32a57952013-01-26 00:03:11 +000042 AttributeSet AS[2];
43 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
Bill Wendling629fb822012-12-22 00:37:52 +000044 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
Bill Wendling32a57952013-01-26 00:03:11 +000045 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
46 ArrayRef<Attribute::AttrKind>(AVs, 2));
Eric Christopherb6174e32010-03-05 22:25:30 +000047
Chandler Carruthece6c6b2012-11-01 08:07:29 +000048 LLVMContext &Context = B.GetInsertBlock()->getContext();
Bill Wendling0976e002012-11-20 05:09:20 +000049 Constant *StrLen = M->getOrInsertFunction("strlen",
Bill Wendling99faa3b2012-12-07 23:16:57 +000050 AttributeSet::get(M->getContext(),
Bill Wendling32a57952013-01-26 00:03:11 +000051 AS),
Chandler Carruthece6c6b2012-11-01 08:07:29 +000052 TD->getIntPtrType(Context),
Eric Christopherb6174e32010-03-05 22:25:30 +000053 B.getInt8PtrTy(),
54 NULL);
55 CallInst *CI = B.CreateCall(StrLen, CastToCStr(Ptr, B), "strlen");
56 if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts()))
57 CI->setCallingConv(F->getCallingConv());
58
59 return CI;
60}
61
Nuno Lopesa5368352012-07-25 17:18:59 +000062/// EmitStrNLen - Emit a call to the strnlen function to the builder, for the
63/// specified pointer. Ptr is required to be some pointer type, MaxLen must
64/// be of size_t type, and the return value has 'intptr_t' type.
65Value *llvm::EmitStrNLen(Value *Ptr, Value *MaxLen, IRBuilder<> &B,
Micah Villmow3574eca2012-10-08 16:38:25 +000066 const DataLayout *TD, const TargetLibraryInfo *TLI) {
Nuno Lopesa5368352012-07-25 17:18:59 +000067 if (!TLI->has(LibFunc::strnlen))
Stephen Hinesdce4a402014-05-29 02:49:00 -070068 return nullptr;
Nuno Lopesa5368352012-07-25 17:18:59 +000069
70 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling32a57952013-01-26 00:03:11 +000071 AttributeSet AS[2];
72 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
Bill Wendling629fb822012-12-22 00:37:52 +000073 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
Bill Wendling32a57952013-01-26 00:03:11 +000074 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
75 ArrayRef<Attribute::AttrKind>(AVs, 2));
Nuno Lopesa5368352012-07-25 17:18:59 +000076
Chandler Carruthece6c6b2012-11-01 08:07:29 +000077 LLVMContext &Context = B.GetInsertBlock()->getContext();
Bill Wendling0976e002012-11-20 05:09:20 +000078 Constant *StrNLen = M->getOrInsertFunction("strnlen",
Bill Wendling99faa3b2012-12-07 23:16:57 +000079 AttributeSet::get(M->getContext(),
Bill Wendling32a57952013-01-26 00:03:11 +000080 AS),
Chandler Carruthece6c6b2012-11-01 08:07:29 +000081 TD->getIntPtrType(Context),
Nuno Lopesa5368352012-07-25 17:18:59 +000082 B.getInt8PtrTy(),
Chandler Carruthece6c6b2012-11-01 08:07:29 +000083 TD->getIntPtrType(Context),
Nuno Lopesa5368352012-07-25 17:18:59 +000084 NULL);
85 CallInst *CI = B.CreateCall2(StrNLen, CastToCStr(Ptr, B), MaxLen, "strnlen");
86 if (const Function *F = dyn_cast<Function>(StrNLen->stripPointerCasts()))
87 CI->setCallingConv(F->getCallingConv());
88
89 return CI;
90}
91
Eric Christopherb6174e32010-03-05 22:25:30 +000092/// EmitStrChr - Emit a call to the strchr function to the builder, for the
93/// specified pointer and character. Ptr is required to be some pointer type,
94/// and the return value has 'i8*' type.
95Value *llvm::EmitStrChr(Value *Ptr, char C, IRBuilder<> &B,
Micah Villmow3574eca2012-10-08 16:38:25 +000096 const DataLayout *TD, const TargetLibraryInfo *TLI) {
Nuno Lopes51004df2012-07-25 16:46:31 +000097 if (!TLI->has(LibFunc::strchr))
Stephen Hinesdce4a402014-05-29 02:49:00 -070098 return nullptr;
Nuno Lopes51004df2012-07-25 16:46:31 +000099
Eric Christopherb6174e32010-03-05 22:25:30 +0000100 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling629fb822012-12-22 00:37:52 +0000101 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
Bill Wendling32a57952013-01-26 00:03:11 +0000102 AttributeSet AS =
103 AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
104 ArrayRef<Attribute::AttrKind>(AVs, 2));
Eric Christopherb6174e32010-03-05 22:25:30 +0000105
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000106 Type *I8Ptr = B.getInt8PtrTy();
107 Type *I32Ty = B.getInt32Ty();
Bill Wendling0976e002012-11-20 05:09:20 +0000108 Constant *StrChr = M->getOrInsertFunction("strchr",
Bill Wendling99faa3b2012-12-07 23:16:57 +0000109 AttributeSet::get(M->getContext(),
Bill Wendling32a57952013-01-26 00:03:11 +0000110 AS),
Eric Christopherb6174e32010-03-05 22:25:30 +0000111 I8Ptr, I8Ptr, I32Ty, NULL);
112 CallInst *CI = B.CreateCall2(StrChr, CastToCStr(Ptr, B),
113 ConstantInt::get(I32Ty, C), "strchr");
114 if (const Function *F = dyn_cast<Function>(StrChr->stripPointerCasts()))
115 CI->setCallingConv(F->getCallingConv());
116 return CI;
117}
118
Benjamin Kramer386e9182010-06-15 21:34:25 +0000119/// EmitStrNCmp - Emit a call to the strncmp function to the builder.
120Value *llvm::EmitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len,
Micah Villmow3574eca2012-10-08 16:38:25 +0000121 IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000122 const TargetLibraryInfo *TLI) {
123 if (!TLI->has(LibFunc::strncmp))
Stephen Hinesdce4a402014-05-29 02:49:00 -0700124 return nullptr;
Nuno Lopes51004df2012-07-25 16:46:31 +0000125
Benjamin Kramer386e9182010-06-15 21:34:25 +0000126 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling32a57952013-01-26 00:03:11 +0000127 AttributeSet AS[3];
128 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
129 AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
Bill Wendling629fb822012-12-22 00:37:52 +0000130 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
Bill Wendling32a57952013-01-26 00:03:11 +0000131 AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
132 ArrayRef<Attribute::AttrKind>(AVs, 2));
Benjamin Kramer386e9182010-06-15 21:34:25 +0000133
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000134 LLVMContext &Context = B.GetInsertBlock()->getContext();
Bill Wendling0976e002012-11-20 05:09:20 +0000135 Value *StrNCmp = M->getOrInsertFunction("strncmp",
Bill Wendling99faa3b2012-12-07 23:16:57 +0000136 AttributeSet::get(M->getContext(),
Bill Wendling32a57952013-01-26 00:03:11 +0000137 AS),
Benjamin Kramer386e9182010-06-15 21:34:25 +0000138 B.getInt32Ty(),
139 B.getInt8PtrTy(),
140 B.getInt8PtrTy(),
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000141 TD->getIntPtrType(Context), NULL);
Benjamin Kramer386e9182010-06-15 21:34:25 +0000142 CallInst *CI = B.CreateCall3(StrNCmp, CastToCStr(Ptr1, B),
143 CastToCStr(Ptr2, B), Len, "strncmp");
144
145 if (const Function *F = dyn_cast<Function>(StrNCmp->stripPointerCasts()))
146 CI->setCallingConv(F->getCallingConv());
147
148 return CI;
149}
150
Eric Christopherb6174e32010-03-05 22:25:30 +0000151/// EmitStrCpy - Emit a call to the strcpy function to the builder, for the
152/// specified pointer arguments.
153Value *llvm::EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
Micah Villmow3574eca2012-10-08 16:38:25 +0000154 const DataLayout *TD, const TargetLibraryInfo *TLI,
Nuno Lopes51004df2012-07-25 16:46:31 +0000155 StringRef Name) {
156 if (!TLI->has(LibFunc::strcpy))
Stephen Hinesdce4a402014-05-29 02:49:00 -0700157 return nullptr;
Nuno Lopes51004df2012-07-25 16:46:31 +0000158
Eric Christopherb6174e32010-03-05 22:25:30 +0000159 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling32a57952013-01-26 00:03:11 +0000160 AttributeSet AS[2];
161 AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
162 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
163 Attribute::NoUnwind);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000164 Type *I8Ptr = B.getInt8PtrTy();
Bill Wendling0976e002012-11-20 05:09:20 +0000165 Value *StrCpy = M->getOrInsertFunction(Name,
Bill Wendling32a57952013-01-26 00:03:11 +0000166 AttributeSet::get(M->getContext(), AS),
Eric Christopherb6174e32010-03-05 22:25:30 +0000167 I8Ptr, I8Ptr, I8Ptr, NULL);
168 CallInst *CI = B.CreateCall2(StrCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
Benjamin Kramer7fa30b82010-03-11 20:45:13 +0000169 Name);
Eric Christopherb6174e32010-03-05 22:25:30 +0000170 if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts()))
171 CI->setCallingConv(F->getCallingConv());
172 return CI;
173}
174
Eric Christopherb0722af2010-03-11 17:45:38 +0000175/// EmitStrNCpy - Emit a call to the strncpy function to the builder, for the
Eric Christopherbd973762010-03-11 01:25:07 +0000176/// specified pointer arguments.
177Value *llvm::EmitStrNCpy(Value *Dst, Value *Src, Value *Len,
Micah Villmow3574eca2012-10-08 16:38:25 +0000178 IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000179 const TargetLibraryInfo *TLI, StringRef Name) {
180 if (!TLI->has(LibFunc::strncpy))
Stephen Hinesdce4a402014-05-29 02:49:00 -0700181 return nullptr;
Nuno Lopes51004df2012-07-25 16:46:31 +0000182
Eric Christopherbd973762010-03-11 01:25:07 +0000183 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling32a57952013-01-26 00:03:11 +0000184 AttributeSet AS[2];
185 AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
186 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
187 Attribute::NoUnwind);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000188 Type *I8Ptr = B.getInt8PtrTy();
Bill Wendling0976e002012-11-20 05:09:20 +0000189 Value *StrNCpy = M->getOrInsertFunction(Name,
Bill Wendling99faa3b2012-12-07 23:16:57 +0000190 AttributeSet::get(M->getContext(),
Bill Wendling32a57952013-01-26 00:03:11 +0000191 AS),
Eric Christopher71988f12010-04-07 23:00:07 +0000192 I8Ptr, I8Ptr, I8Ptr,
193 Len->getType(), NULL);
Eric Christopherbd973762010-03-11 01:25:07 +0000194 CallInst *CI = B.CreateCall3(StrNCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
195 Len, "strncpy");
196 if (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts()))
197 CI->setCallingConv(F->getCallingConv());
198 return CI;
199}
200
Evan Cheng0289b412010-03-23 15:48:04 +0000201/// EmitMemCpyChk - Emit a call to the __memcpy_chk function to the builder.
202/// This expects that the Len and ObjSize have type 'intptr_t' and Dst/Src
203/// are pointers.
204Value *llvm::EmitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
Micah Villmow3574eca2012-10-08 16:38:25 +0000205 IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000206 const TargetLibraryInfo *TLI) {
207 if (!TLI->has(LibFunc::memcpy_chk))
Stephen Hinesdce4a402014-05-29 02:49:00 -0700208 return nullptr;
Nuno Lopes51004df2012-07-25 16:46:31 +0000209
Evan Cheng0289b412010-03-23 15:48:04 +0000210 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling32a57952013-01-26 00:03:11 +0000211 AttributeSet AS;
212 AS = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
213 Attribute::NoUnwind);
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000214 LLVMContext &Context = B.GetInsertBlock()->getContext();
Evan Cheng0289b412010-03-23 15:48:04 +0000215 Value *MemCpy = M->getOrInsertFunction("__memcpy_chk",
Bill Wendling32a57952013-01-26 00:03:11 +0000216 AttributeSet::get(M->getContext(), AS),
Evan Cheng0289b412010-03-23 15:48:04 +0000217 B.getInt8PtrTy(),
218 B.getInt8PtrTy(),
219 B.getInt8PtrTy(),
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000220 TD->getIntPtrType(Context),
221 TD->getIntPtrType(Context), NULL);
Evan Cheng0289b412010-03-23 15:48:04 +0000222 Dst = CastToCStr(Dst, B);
223 Src = CastToCStr(Src, B);
224 CallInst *CI = B.CreateCall4(MemCpy, Dst, Src, Len, ObjSize);
225 if (const Function *F = dyn_cast<Function>(MemCpy->stripPointerCasts()))
226 CI->setCallingConv(F->getCallingConv());
227 return CI;
228}
229
Eric Christopherb6174e32010-03-05 22:25:30 +0000230/// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
231/// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
232Value *llvm::EmitMemChr(Value *Ptr, Value *Val,
Micah Villmow3574eca2012-10-08 16:38:25 +0000233 Value *Len, IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000234 const TargetLibraryInfo *TLI) {
235 if (!TLI->has(LibFunc::memchr))
Stephen Hinesdce4a402014-05-29 02:49:00 -0700236 return nullptr;
Nuno Lopes51004df2012-07-25 16:46:31 +0000237
Eric Christopherb6174e32010-03-05 22:25:30 +0000238 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling32a57952013-01-26 00:03:11 +0000239 AttributeSet AS;
Bill Wendling629fb822012-12-22 00:37:52 +0000240 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
Bill Wendling32a57952013-01-26 00:03:11 +0000241 AS = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
242 ArrayRef<Attribute::AttrKind>(AVs, 2));
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000243 LLVMContext &Context = B.GetInsertBlock()->getContext();
Bill Wendling0976e002012-11-20 05:09:20 +0000244 Value *MemChr = M->getOrInsertFunction("memchr",
Bill Wendling32a57952013-01-26 00:03:11 +0000245 AttributeSet::get(M->getContext(), AS),
Eric Christopherb6174e32010-03-05 22:25:30 +0000246 B.getInt8PtrTy(),
247 B.getInt8PtrTy(),
248 B.getInt32Ty(),
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000249 TD->getIntPtrType(Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000250 NULL);
251 CallInst *CI = B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr");
252
253 if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts()))
254 CI->setCallingConv(F->getCallingConv());
255
256 return CI;
257}
258
259/// EmitMemCmp - Emit a call to the memcmp function.
260Value *llvm::EmitMemCmp(Value *Ptr1, Value *Ptr2,
Micah Villmow3574eca2012-10-08 16:38:25 +0000261 Value *Len, IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000262 const TargetLibraryInfo *TLI) {
263 if (!TLI->has(LibFunc::memcmp))
Stephen Hinesdce4a402014-05-29 02:49:00 -0700264 return nullptr;
Nuno Lopes51004df2012-07-25 16:46:31 +0000265
Eric Christopherb6174e32010-03-05 22:25:30 +0000266 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling32a57952013-01-26 00:03:11 +0000267 AttributeSet AS[3];
268 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
269 AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
Bill Wendling629fb822012-12-22 00:37:52 +0000270 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
Bill Wendling32a57952013-01-26 00:03:11 +0000271 AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
272 ArrayRef<Attribute::AttrKind>(AVs, 2));
Eric Christopherb6174e32010-03-05 22:25:30 +0000273
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000274 LLVMContext &Context = B.GetInsertBlock()->getContext();
Bill Wendling0976e002012-11-20 05:09:20 +0000275 Value *MemCmp = M->getOrInsertFunction("memcmp",
Bill Wendling32a57952013-01-26 00:03:11 +0000276 AttributeSet::get(M->getContext(), AS),
Eric Christopherb6174e32010-03-05 22:25:30 +0000277 B.getInt32Ty(),
278 B.getInt8PtrTy(),
279 B.getInt8PtrTy(),
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000280 TD->getIntPtrType(Context), NULL);
Eric Christopherb6174e32010-03-05 22:25:30 +0000281 CallInst *CI = B.CreateCall3(MemCmp, CastToCStr(Ptr1, B), CastToCStr(Ptr2, B),
282 Len, "memcmp");
283
284 if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts()))
285 CI->setCallingConv(F->getCallingConv());
286
287 return CI;
288}
289
Stephen Hines36b56882014-04-23 16:57:46 -0700290/// Append a suffix to the function name according to the type of 'Op'.
291static void AppendTypeSuffix(Value *Op, StringRef &Name, SmallString<20> &NameBuffer) {
292 if (!Op->getType()->isDoubleTy()) {
293 NameBuffer += Name;
294
295 if (Op->getType()->isFloatTy())
296 NameBuffer += 'f';
297 else
298 NameBuffer += 'l';
299
300 Name = NameBuffer;
301 }
302 return;
303}
304
Eric Christopherb6174e32010-03-05 22:25:30 +0000305/// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
306/// 'floor'). This function is known to take a single of type matching 'Op' and
307/// returns one value with the same type. If 'Op' is a long double, 'l' is
308/// added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
Benjamin Kramerb5ccb252011-11-15 19:12:09 +0000309Value *llvm::EmitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B,
Bill Wendling99faa3b2012-12-07 23:16:57 +0000310 const AttributeSet &Attrs) {
Benjamin Kramerb5ccb252011-11-15 19:12:09 +0000311 SmallString<20> NameBuffer;
Stephen Hines36b56882014-04-23 16:57:46 -0700312 AppendTypeSuffix(Op, Name, NameBuffer);
Eric Christopherb6174e32010-03-05 22:25:30 +0000313
314 Module *M = B.GetInsertBlock()->getParent()->getParent();
315 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
316 Op->getType(), NULL);
317 CallInst *CI = B.CreateCall(Callee, Op, Name);
318 CI->setAttributes(Attrs);
319 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
320 CI->setCallingConv(F->getCallingConv());
321
322 return CI;
323}
324
Stephen Hines36b56882014-04-23 16:57:46 -0700325/// EmitBinaryFloatFnCall - Emit a call to the binary function named 'Name'
326/// (e.g. 'fmin'). This function is known to take type matching 'Op1' and 'Op2'
327/// and return one value with the same type. If 'Op1/Op2' are long double, 'l'
328/// is added as the suffix of name, if 'Op1/Op2' is a float, we add a 'f'
329/// suffix.
330Value *llvm::EmitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name,
331 IRBuilder<> &B, const AttributeSet &Attrs) {
332 SmallString<20> NameBuffer;
333 AppendTypeSuffix(Op1, Name, NameBuffer);
334
335 Module *M = B.GetInsertBlock()->getParent()->getParent();
336 Value *Callee = M->getOrInsertFunction(Name, Op1->getType(),
337 Op1->getType(), Op2->getType(), NULL);
338 CallInst *CI = B.CreateCall2(Callee, Op1, Op2, Name);
339 CI->setAttributes(Attrs);
340 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
341 CI->setCallingConv(F->getCallingConv());
342
343 return CI;
344}
345
Eric Christopherb6174e32010-03-05 22:25:30 +0000346/// EmitPutChar - Emit a call to the putchar function. This assumes that Char
347/// is an integer.
Micah Villmow3574eca2012-10-08 16:38:25 +0000348Value *llvm::EmitPutChar(Value *Char, IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000349 const TargetLibraryInfo *TLI) {
350 if (!TLI->has(LibFunc::putchar))
Stephen Hinesdce4a402014-05-29 02:49:00 -0700351 return nullptr;
Nuno Lopes51004df2012-07-25 16:46:31 +0000352
Eric Christopherb6174e32010-03-05 22:25:30 +0000353 Module *M = B.GetInsertBlock()->getParent()->getParent();
354 Value *PutChar = M->getOrInsertFunction("putchar", B.getInt32Ty(),
355 B.getInt32Ty(), NULL);
356 CallInst *CI = B.CreateCall(PutChar,
357 B.CreateIntCast(Char,
358 B.getInt32Ty(),
359 /*isSigned*/true,
360 "chari"),
361 "putchar");
362
363 if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts()))
364 CI->setCallingConv(F->getCallingConv());
365 return CI;
366}
367
368/// EmitPutS - Emit a call to the puts function. This assumes that Str is
369/// some pointer.
Micah Villmow3574eca2012-10-08 16:38:25 +0000370Value *llvm::EmitPutS(Value *Str, IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000371 const TargetLibraryInfo *TLI) {
372 if (!TLI->has(LibFunc::puts))
Stephen Hinesdce4a402014-05-29 02:49:00 -0700373 return nullptr;
Nuno Lopes51004df2012-07-25 16:46:31 +0000374
Eric Christopherb6174e32010-03-05 22:25:30 +0000375 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling32a57952013-01-26 00:03:11 +0000376 AttributeSet AS[2];
377 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
378 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
379 Attribute::NoUnwind);
Eric Christopherb6174e32010-03-05 22:25:30 +0000380
Bill Wendling0976e002012-11-20 05:09:20 +0000381 Value *PutS = M->getOrInsertFunction("puts",
Bill Wendling32a57952013-01-26 00:03:11 +0000382 AttributeSet::get(M->getContext(), AS),
Eric Christopherb6174e32010-03-05 22:25:30 +0000383 B.getInt32Ty(),
384 B.getInt8PtrTy(),
385 NULL);
386 CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts");
387 if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
388 CI->setCallingConv(F->getCallingConv());
Nuno Lopes51004df2012-07-25 16:46:31 +0000389 return CI;
Eric Christopherb6174e32010-03-05 22:25:30 +0000390}
391
392/// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
393/// an integer and File is a pointer to FILE.
Nuno Lopes51004df2012-07-25 16:46:31 +0000394Value *llvm::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B,
Micah Villmow3574eca2012-10-08 16:38:25 +0000395 const DataLayout *TD, const TargetLibraryInfo *TLI) {
Nuno Lopes51004df2012-07-25 16:46:31 +0000396 if (!TLI->has(LibFunc::fputc))
Stephen Hinesdce4a402014-05-29 02:49:00 -0700397 return nullptr;
Nuno Lopes51004df2012-07-25 16:46:31 +0000398
Eric Christopherb6174e32010-03-05 22:25:30 +0000399 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling32a57952013-01-26 00:03:11 +0000400 AttributeSet AS[2];
401 AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
402 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
403 Attribute::NoUnwind);
Eric Christopherb6174e32010-03-05 22:25:30 +0000404 Constant *F;
405 if (File->getType()->isPointerTy())
Bill Wendling0976e002012-11-20 05:09:20 +0000406 F = M->getOrInsertFunction("fputc",
Bill Wendling32a57952013-01-26 00:03:11 +0000407 AttributeSet::get(M->getContext(), AS),
Eric Christopherb6174e32010-03-05 22:25:30 +0000408 B.getInt32Ty(),
409 B.getInt32Ty(), File->getType(),
410 NULL);
411 else
412 F = M->getOrInsertFunction("fputc",
413 B.getInt32Ty(),
414 B.getInt32Ty(),
415 File->getType(), NULL);
416 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
417 "chari");
418 CallInst *CI = B.CreateCall2(F, Char, File, "fputc");
419
420 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
421 CI->setCallingConv(Fn->getCallingConv());
Nuno Lopes51004df2012-07-25 16:46:31 +0000422 return CI;
Eric Christopherb6174e32010-03-05 22:25:30 +0000423}
424
425/// EmitFPutS - Emit a call to the puts function. Str is required to be a
426/// pointer and File is a pointer to FILE.
Nuno Lopes51004df2012-07-25 16:46:31 +0000427Value *llvm::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B,
Micah Villmow3574eca2012-10-08 16:38:25 +0000428 const DataLayout *TD, const TargetLibraryInfo *TLI) {
Nuno Lopes51004df2012-07-25 16:46:31 +0000429 if (!TLI->has(LibFunc::fputs))
Stephen Hinesdce4a402014-05-29 02:49:00 -0700430 return nullptr;
Nuno Lopes51004df2012-07-25 16:46:31 +0000431
Eric Christopherb6174e32010-03-05 22:25:30 +0000432 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling32a57952013-01-26 00:03:11 +0000433 AttributeSet AS[3];
434 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
435 AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
436 AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
437 Attribute::NoUnwind);
Eli Friedman9d434db2011-11-17 01:27:36 +0000438 StringRef FPutsName = TLI->getName(LibFunc::fputs);
Eric Christopherb6174e32010-03-05 22:25:30 +0000439 Constant *F;
440 if (File->getType()->isPointerTy())
Bill Wendling0976e002012-11-20 05:09:20 +0000441 F = M->getOrInsertFunction(FPutsName,
Bill Wendling32a57952013-01-26 00:03:11 +0000442 AttributeSet::get(M->getContext(), AS),
Eric Christopherb6174e32010-03-05 22:25:30 +0000443 B.getInt32Ty(),
444 B.getInt8PtrTy(),
445 File->getType(), NULL);
446 else
Eli Friedman9d434db2011-11-17 01:27:36 +0000447 F = M->getOrInsertFunction(FPutsName, B.getInt32Ty(),
Eric Christopherb6174e32010-03-05 22:25:30 +0000448 B.getInt8PtrTy(),
449 File->getType(), NULL);
450 CallInst *CI = B.CreateCall2(F, CastToCStr(Str, B), File, "fputs");
451
452 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
453 CI->setCallingConv(Fn->getCallingConv());
Nuno Lopes51004df2012-07-25 16:46:31 +0000454 return CI;
Eric Christopherb6174e32010-03-05 22:25:30 +0000455}
456
457/// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
458/// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
Nuno Lopes51004df2012-07-25 16:46:31 +0000459Value *llvm::EmitFWrite(Value *Ptr, Value *Size, Value *File,
Micah Villmow3574eca2012-10-08 16:38:25 +0000460 IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000461 const TargetLibraryInfo *TLI) {
462 if (!TLI->has(LibFunc::fwrite))
Stephen Hinesdce4a402014-05-29 02:49:00 -0700463 return nullptr;
Nuno Lopes51004df2012-07-25 16:46:31 +0000464
Eric Christopherb6174e32010-03-05 22:25:30 +0000465 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling32a57952013-01-26 00:03:11 +0000466 AttributeSet AS[3];
467 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
468 AS[1] = AttributeSet::get(M->getContext(), 4, Attribute::NoCapture);
469 AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
470 Attribute::NoUnwind);
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000471 LLVMContext &Context = B.GetInsertBlock()->getContext();
Eli Friedman9d434db2011-11-17 01:27:36 +0000472 StringRef FWriteName = TLI->getName(LibFunc::fwrite);
Eric Christopherb6174e32010-03-05 22:25:30 +0000473 Constant *F;
474 if (File->getType()->isPointerTy())
Bill Wendling0976e002012-11-20 05:09:20 +0000475 F = M->getOrInsertFunction(FWriteName,
Bill Wendling32a57952013-01-26 00:03:11 +0000476 AttributeSet::get(M->getContext(), AS),
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000477 TD->getIntPtrType(Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000478 B.getInt8PtrTy(),
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000479 TD->getIntPtrType(Context),
480 TD->getIntPtrType(Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000481 File->getType(), NULL);
482 else
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000483 F = M->getOrInsertFunction(FWriteName, TD->getIntPtrType(Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000484 B.getInt8PtrTy(),
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000485 TD->getIntPtrType(Context),
486 TD->getIntPtrType(Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000487 File->getType(), NULL);
488 CallInst *CI = B.CreateCall4(F, CastToCStr(Ptr, B), Size,
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000489 ConstantInt::get(TD->getIntPtrType(Context), 1), File);
Eric Christopherb6174e32010-03-05 22:25:30 +0000490
491 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
492 CI->setCallingConv(Fn->getCallingConv());
Nuno Lopes51004df2012-07-25 16:46:31 +0000493 return CI;
Eric Christopherb6174e32010-03-05 22:25:30 +0000494}
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000495
Benjamin Kramera30b1812010-03-12 20:41:29 +0000496SimplifyFortifiedLibCalls::~SimplifyFortifiedLibCalls() { }
497
Micah Villmow3574eca2012-10-08 16:38:25 +0000498bool SimplifyFortifiedLibCalls::fold(CallInst *CI, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000499 const TargetLibraryInfo *TLI) {
Micah Villmow3574eca2012-10-08 16:38:25 +0000500 // We really need DataLayout for later.
Eric Christopher67a71b52010-04-12 04:48:00 +0000501 if (!TD) return false;
502
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000503 this->CI = CI;
Eric Christopher67a71b52010-04-12 04:48:00 +0000504 Function *Callee = CI->getCalledFunction();
505 StringRef Name = Callee->getName();
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000506 FunctionType *FT = Callee->getFunctionType();
Eric Christopher67a71b52010-04-12 04:48:00 +0000507 LLVMContext &Context = CI->getParent()->getContext();
Eli Friedman1a24bf02011-05-27 01:00:36 +0000508 IRBuilder<> B(CI);
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000509
510 if (Name == "__memcpy_chk") {
Eric Christopher67a71b52010-04-12 04:48:00 +0000511 // Check if this has the right signature.
512 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
513 !FT->getParamType(0)->isPointerTy() ||
514 !FT->getParamType(1)->isPointerTy() ||
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000515 FT->getParamType(2) != TD->getIntPtrType(Context) ||
516 FT->getParamType(3) != TD->getIntPtrType(Context))
Eric Christopher67a71b52010-04-12 04:48:00 +0000517 return false;
Gabor Greiff148cb62010-06-28 12:29:20 +0000518
Gabor Greifa6aac4c2010-07-16 09:38:02 +0000519 if (isFoldable(3, 2, false)) {
Benjamin Kramerdef548f2010-12-27 00:25:32 +0000520 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
521 CI->getArgOperand(2), 1);
Gabor Greiff148cb62010-06-28 12:29:20 +0000522 replaceCall(CI->getArgOperand(0));
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000523 return true;
524 }
525 return false;
526 }
527
528 // Should be similar to memcpy.
529 if (Name == "__mempcpy_chk") {
530 return false;
531 }
532
533 if (Name == "__memmove_chk") {
Eric Christopher67a71b52010-04-12 04:48:00 +0000534 // Check if this has the right signature.
535 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
536 !FT->getParamType(0)->isPointerTy() ||
537 !FT->getParamType(1)->isPointerTy() ||
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000538 FT->getParamType(2) != TD->getIntPtrType(Context) ||
539 FT->getParamType(3) != TD->getIntPtrType(Context))
Eric Christopher67a71b52010-04-12 04:48:00 +0000540 return false;
Gabor Greiff148cb62010-06-28 12:29:20 +0000541
Gabor Greifa6aac4c2010-07-16 09:38:02 +0000542 if (isFoldable(3, 2, false)) {
Benjamin Kramerdef548f2010-12-27 00:25:32 +0000543 B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
544 CI->getArgOperand(2), 1);
Gabor Greiff148cb62010-06-28 12:29:20 +0000545 replaceCall(CI->getArgOperand(0));
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000546 return true;
547 }
548 return false;
549 }
550
551 if (Name == "__memset_chk") {
Eric Christopher67a71b52010-04-12 04:48:00 +0000552 // Check if this has the right signature.
553 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
554 !FT->getParamType(0)->isPointerTy() ||
555 !FT->getParamType(1)->isIntegerTy() ||
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000556 FT->getParamType(2) != TD->getIntPtrType(Context) ||
557 FT->getParamType(3) != TD->getIntPtrType(Context))
Eric Christopher67a71b52010-04-12 04:48:00 +0000558 return false;
Gabor Greiff148cb62010-06-28 12:29:20 +0000559
Gabor Greifa6aac4c2010-07-16 09:38:02 +0000560 if (isFoldable(3, 2, false)) {
Gabor Greiff148cb62010-06-28 12:29:20 +0000561 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(),
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000562 false);
Benjamin Kramerdef548f2010-12-27 00:25:32 +0000563 B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
Gabor Greiff1ba6512010-06-25 07:58:41 +0000564 replaceCall(CI->getArgOperand(0));
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000565 return true;
566 }
567 return false;
568 }
569
570 if (Name == "__strcpy_chk" || Name == "__stpcpy_chk") {
Eric Christopher67a71b52010-04-12 04:48:00 +0000571 // Check if this has the right signature.
572 if (FT->getNumParams() != 3 ||
573 FT->getReturnType() != FT->getParamType(0) ||
574 FT->getParamType(0) != FT->getParamType(1) ||
575 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000576 FT->getParamType(2) != TD->getIntPtrType(Context))
Eric Christopher67a71b52010-04-12 04:48:00 +0000577 return 0;
578
579
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000580 // If a) we don't have any length information, or b) we know this will
581 // fit then just lower to a plain st[rp]cpy. Otherwise we'll keep our
582 // st[rp]cpy_chk call which may fail at runtime if the size is too long.
583 // TODO: It might be nice to get a maximum length out of the possible
584 // string lengths for varying.
Gabor Greifa6aac4c2010-07-16 09:38:02 +0000585 if (isFoldable(2, 1, true)) {
Gabor Greiff148cb62010-06-28 12:29:20 +0000586 Value *Ret = EmitStrCpy(CI->getArgOperand(0), CI->getArgOperand(1), B, TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000587 TLI, Name.substr(2, 6));
Nuno Lopes28ad8632012-08-01 16:58:51 +0000588 if (!Ret)
Nuno Lopes918067d2012-08-01 17:13:28 +0000589 return false;
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000590 replaceCall(Ret);
591 return true;
592 }
593 return false;
594 }
595
Eric Christopher71988f12010-04-07 23:00:07 +0000596 if (Name == "__strncpy_chk" || Name == "__stpncpy_chk") {
Eric Christopher67a71b52010-04-12 04:48:00 +0000597 // Check if this has the right signature.
598 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
599 FT->getParamType(0) != FT->getParamType(1) ||
600 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
601 !FT->getParamType(2)->isIntegerTy() ||
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000602 FT->getParamType(3) != TD->getIntPtrType(Context))
Eric Christopher2a7cb9d2010-04-13 16:41:29 +0000603 return false;
Gabor Greiff148cb62010-06-28 12:29:20 +0000604
Gabor Greifa6aac4c2010-07-16 09:38:02 +0000605 if (isFoldable(3, 2, false)) {
Gabor Greiff148cb62010-06-28 12:29:20 +0000606 Value *Ret = EmitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
Nuno Lopes51004df2012-07-25 16:46:31 +0000607 CI->getArgOperand(2), B, TD, TLI,
608 Name.substr(2, 7));
Nuno Lopes28ad8632012-08-01 16:58:51 +0000609 if (!Ret)
Nuno Lopes918067d2012-08-01 17:13:28 +0000610 return false;
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000611 replaceCall(Ret);
612 return true;
613 }
614 return false;
615 }
616
617 if (Name == "__strcat_chk") {
618 return false;
619 }
620
621 if (Name == "__strncat_chk") {
622 return false;
623 }
624
625 return false;
626}