blob: 82384a1edf52c8b3c8a92ccd7cffacd00d3ddfe1 [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) {
30 return B.CreateBitCast(V, B.getInt8PtrTy(), "cstr");
31}
32
33/// EmitStrLen - Emit a call to the strlen function to the builder, for the
34/// specified pointer. This always returns an integer value of size intptr_t.
Micah Villmow3574eca2012-10-08 16:38:25 +000035Value *llvm::EmitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +000036 const TargetLibraryInfo *TLI) {
37 if (!TLI->has(LibFunc::strlen))
38 return 0;
39
Eric Christopherb6174e32010-03-05 22:25:30 +000040 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling32a57952013-01-26 00:03:11 +000041 AttributeSet AS[2];
42 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
Bill Wendling629fb822012-12-22 00:37:52 +000043 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
Bill Wendling32a57952013-01-26 00:03:11 +000044 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
45 ArrayRef<Attribute::AttrKind>(AVs, 2));
Eric Christopherb6174e32010-03-05 22:25:30 +000046
Chandler Carruthece6c6b2012-11-01 08:07:29 +000047 LLVMContext &Context = B.GetInsertBlock()->getContext();
Bill Wendling0976e002012-11-20 05:09:20 +000048 Constant *StrLen = M->getOrInsertFunction("strlen",
Bill Wendling99faa3b2012-12-07 23:16:57 +000049 AttributeSet::get(M->getContext(),
Bill Wendling32a57952013-01-26 00:03:11 +000050 AS),
Chandler Carruthece6c6b2012-11-01 08:07:29 +000051 TD->getIntPtrType(Context),
Eric Christopherb6174e32010-03-05 22:25:30 +000052 B.getInt8PtrTy(),
53 NULL);
54 CallInst *CI = B.CreateCall(StrLen, CastToCStr(Ptr, B), "strlen");
55 if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts()))
56 CI->setCallingConv(F->getCallingConv());
57
58 return CI;
59}
60
Nuno Lopesa5368352012-07-25 17:18:59 +000061/// EmitStrNLen - Emit a call to the strnlen function to the builder, for the
62/// specified pointer. Ptr is required to be some pointer type, MaxLen must
63/// be of size_t type, and the return value has 'intptr_t' type.
64Value *llvm::EmitStrNLen(Value *Ptr, Value *MaxLen, IRBuilder<> &B,
Micah Villmow3574eca2012-10-08 16:38:25 +000065 const DataLayout *TD, const TargetLibraryInfo *TLI) {
Nuno Lopesa5368352012-07-25 17:18:59 +000066 if (!TLI->has(LibFunc::strnlen))
67 return 0;
68
69 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling32a57952013-01-26 00:03:11 +000070 AttributeSet AS[2];
71 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
Bill Wendling629fb822012-12-22 00:37:52 +000072 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
Bill Wendling32a57952013-01-26 00:03:11 +000073 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
74 ArrayRef<Attribute::AttrKind>(AVs, 2));
Nuno Lopesa5368352012-07-25 17:18:59 +000075
Chandler Carruthece6c6b2012-11-01 08:07:29 +000076 LLVMContext &Context = B.GetInsertBlock()->getContext();
Bill Wendling0976e002012-11-20 05:09:20 +000077 Constant *StrNLen = M->getOrInsertFunction("strnlen",
Bill Wendling99faa3b2012-12-07 23:16:57 +000078 AttributeSet::get(M->getContext(),
Bill Wendling32a57952013-01-26 00:03:11 +000079 AS),
Chandler Carruthece6c6b2012-11-01 08:07:29 +000080 TD->getIntPtrType(Context),
Nuno Lopesa5368352012-07-25 17:18:59 +000081 B.getInt8PtrTy(),
Chandler Carruthece6c6b2012-11-01 08:07:29 +000082 TD->getIntPtrType(Context),
Nuno Lopesa5368352012-07-25 17:18:59 +000083 NULL);
84 CallInst *CI = B.CreateCall2(StrNLen, CastToCStr(Ptr, B), MaxLen, "strnlen");
85 if (const Function *F = dyn_cast<Function>(StrNLen->stripPointerCasts()))
86 CI->setCallingConv(F->getCallingConv());
87
88 return CI;
89}
90
Eric Christopherb6174e32010-03-05 22:25:30 +000091/// EmitStrChr - Emit a call to the strchr function to the builder, for the
92/// specified pointer and character. Ptr is required to be some pointer type,
93/// and the return value has 'i8*' type.
94Value *llvm::EmitStrChr(Value *Ptr, char C, IRBuilder<> &B,
Micah Villmow3574eca2012-10-08 16:38:25 +000095 const DataLayout *TD, const TargetLibraryInfo *TLI) {
Nuno Lopes51004df2012-07-25 16:46:31 +000096 if (!TLI->has(LibFunc::strchr))
97 return 0;
98
Eric Christopherb6174e32010-03-05 22:25:30 +000099 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling629fb822012-12-22 00:37:52 +0000100 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
Bill Wendling32a57952013-01-26 00:03:11 +0000101 AttributeSet AS =
102 AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
103 ArrayRef<Attribute::AttrKind>(AVs, 2));
Eric Christopherb6174e32010-03-05 22:25:30 +0000104
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000105 Type *I8Ptr = B.getInt8PtrTy();
106 Type *I32Ty = B.getInt32Ty();
Bill Wendling0976e002012-11-20 05:09:20 +0000107 Constant *StrChr = M->getOrInsertFunction("strchr",
Bill Wendling99faa3b2012-12-07 23:16:57 +0000108 AttributeSet::get(M->getContext(),
Bill Wendling32a57952013-01-26 00:03:11 +0000109 AS),
Eric Christopherb6174e32010-03-05 22:25:30 +0000110 I8Ptr, I8Ptr, I32Ty, NULL);
111 CallInst *CI = B.CreateCall2(StrChr, CastToCStr(Ptr, B),
112 ConstantInt::get(I32Ty, C), "strchr");
113 if (const Function *F = dyn_cast<Function>(StrChr->stripPointerCasts()))
114 CI->setCallingConv(F->getCallingConv());
115 return CI;
116}
117
Benjamin Kramer386e9182010-06-15 21:34:25 +0000118/// EmitStrNCmp - Emit a call to the strncmp function to the builder.
119Value *llvm::EmitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len,
Micah Villmow3574eca2012-10-08 16:38:25 +0000120 IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000121 const TargetLibraryInfo *TLI) {
122 if (!TLI->has(LibFunc::strncmp))
123 return 0;
124
Benjamin Kramer386e9182010-06-15 21:34:25 +0000125 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling32a57952013-01-26 00:03:11 +0000126 AttributeSet AS[3];
127 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
128 AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
Bill Wendling629fb822012-12-22 00:37:52 +0000129 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
Bill Wendling32a57952013-01-26 00:03:11 +0000130 AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
131 ArrayRef<Attribute::AttrKind>(AVs, 2));
Benjamin Kramer386e9182010-06-15 21:34:25 +0000132
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000133 LLVMContext &Context = B.GetInsertBlock()->getContext();
Bill Wendling0976e002012-11-20 05:09:20 +0000134 Value *StrNCmp = M->getOrInsertFunction("strncmp",
Bill Wendling99faa3b2012-12-07 23:16:57 +0000135 AttributeSet::get(M->getContext(),
Bill Wendling32a57952013-01-26 00:03:11 +0000136 AS),
Benjamin Kramer386e9182010-06-15 21:34:25 +0000137 B.getInt32Ty(),
138 B.getInt8PtrTy(),
139 B.getInt8PtrTy(),
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000140 TD->getIntPtrType(Context), NULL);
Benjamin Kramer386e9182010-06-15 21:34:25 +0000141 CallInst *CI = B.CreateCall3(StrNCmp, CastToCStr(Ptr1, B),
142 CastToCStr(Ptr2, B), Len, "strncmp");
143
144 if (const Function *F = dyn_cast<Function>(StrNCmp->stripPointerCasts()))
145 CI->setCallingConv(F->getCallingConv());
146
147 return CI;
148}
149
Eric Christopherb6174e32010-03-05 22:25:30 +0000150/// EmitStrCpy - Emit a call to the strcpy function to the builder, for the
151/// specified pointer arguments.
152Value *llvm::EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
Micah Villmow3574eca2012-10-08 16:38:25 +0000153 const DataLayout *TD, const TargetLibraryInfo *TLI,
Nuno Lopes51004df2012-07-25 16:46:31 +0000154 StringRef Name) {
155 if (!TLI->has(LibFunc::strcpy))
156 return 0;
157
Eric Christopherb6174e32010-03-05 22:25:30 +0000158 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling32a57952013-01-26 00:03:11 +0000159 AttributeSet AS[2];
160 AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
161 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
162 Attribute::NoUnwind);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000163 Type *I8Ptr = B.getInt8PtrTy();
Bill Wendling0976e002012-11-20 05:09:20 +0000164 Value *StrCpy = M->getOrInsertFunction(Name,
Bill Wendling32a57952013-01-26 00:03:11 +0000165 AttributeSet::get(M->getContext(), AS),
Eric Christopherb6174e32010-03-05 22:25:30 +0000166 I8Ptr, I8Ptr, I8Ptr, NULL);
167 CallInst *CI = B.CreateCall2(StrCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
Benjamin Kramer7fa30b82010-03-11 20:45:13 +0000168 Name);
Eric Christopherb6174e32010-03-05 22:25:30 +0000169 if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts()))
170 CI->setCallingConv(F->getCallingConv());
171 return CI;
172}
173
Eric Christopherb0722af2010-03-11 17:45:38 +0000174/// EmitStrNCpy - Emit a call to the strncpy function to the builder, for the
Eric Christopherbd973762010-03-11 01:25:07 +0000175/// specified pointer arguments.
176Value *llvm::EmitStrNCpy(Value *Dst, Value *Src, Value *Len,
Micah Villmow3574eca2012-10-08 16:38:25 +0000177 IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000178 const TargetLibraryInfo *TLI, StringRef Name) {
179 if (!TLI->has(LibFunc::strncpy))
180 return 0;
181
Eric Christopherbd973762010-03-11 01:25:07 +0000182 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling32a57952013-01-26 00:03:11 +0000183 AttributeSet AS[2];
184 AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
185 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
186 Attribute::NoUnwind);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000187 Type *I8Ptr = B.getInt8PtrTy();
Bill Wendling0976e002012-11-20 05:09:20 +0000188 Value *StrNCpy = M->getOrInsertFunction(Name,
Bill Wendling99faa3b2012-12-07 23:16:57 +0000189 AttributeSet::get(M->getContext(),
Bill Wendling32a57952013-01-26 00:03:11 +0000190 AS),
Eric Christopher71988f12010-04-07 23:00:07 +0000191 I8Ptr, I8Ptr, I8Ptr,
192 Len->getType(), NULL);
Eric Christopherbd973762010-03-11 01:25:07 +0000193 CallInst *CI = B.CreateCall3(StrNCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
194 Len, "strncpy");
195 if (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts()))
196 CI->setCallingConv(F->getCallingConv());
197 return CI;
198}
199
Evan Cheng0289b412010-03-23 15:48:04 +0000200/// EmitMemCpyChk - Emit a call to the __memcpy_chk function to the builder.
201/// This expects that the Len and ObjSize have type 'intptr_t' and Dst/Src
202/// are pointers.
203Value *llvm::EmitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
Micah Villmow3574eca2012-10-08 16:38:25 +0000204 IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000205 const TargetLibraryInfo *TLI) {
206 if (!TLI->has(LibFunc::memcpy_chk))
207 return 0;
208
Evan Cheng0289b412010-03-23 15:48:04 +0000209 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling32a57952013-01-26 00:03:11 +0000210 AttributeSet AS;
211 AS = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
212 Attribute::NoUnwind);
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000213 LLVMContext &Context = B.GetInsertBlock()->getContext();
Evan Cheng0289b412010-03-23 15:48:04 +0000214 Value *MemCpy = M->getOrInsertFunction("__memcpy_chk",
Bill Wendling32a57952013-01-26 00:03:11 +0000215 AttributeSet::get(M->getContext(), AS),
Evan Cheng0289b412010-03-23 15:48:04 +0000216 B.getInt8PtrTy(),
217 B.getInt8PtrTy(),
218 B.getInt8PtrTy(),
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000219 TD->getIntPtrType(Context),
220 TD->getIntPtrType(Context), NULL);
Evan Cheng0289b412010-03-23 15:48:04 +0000221 Dst = CastToCStr(Dst, B);
222 Src = CastToCStr(Src, B);
223 CallInst *CI = B.CreateCall4(MemCpy, Dst, Src, Len, ObjSize);
224 if (const Function *F = dyn_cast<Function>(MemCpy->stripPointerCasts()))
225 CI->setCallingConv(F->getCallingConv());
226 return CI;
227}
228
Eric Christopherb6174e32010-03-05 22:25:30 +0000229/// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
230/// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
231Value *llvm::EmitMemChr(Value *Ptr, Value *Val,
Micah Villmow3574eca2012-10-08 16:38:25 +0000232 Value *Len, IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000233 const TargetLibraryInfo *TLI) {
234 if (!TLI->has(LibFunc::memchr))
235 return 0;
236
Eric Christopherb6174e32010-03-05 22:25:30 +0000237 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling32a57952013-01-26 00:03:11 +0000238 AttributeSet AS;
Bill Wendling629fb822012-12-22 00:37:52 +0000239 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
Bill Wendling32a57952013-01-26 00:03:11 +0000240 AS = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
241 ArrayRef<Attribute::AttrKind>(AVs, 2));
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000242 LLVMContext &Context = B.GetInsertBlock()->getContext();
Bill Wendling0976e002012-11-20 05:09:20 +0000243 Value *MemChr = M->getOrInsertFunction("memchr",
Bill Wendling32a57952013-01-26 00:03:11 +0000244 AttributeSet::get(M->getContext(), AS),
Eric Christopherb6174e32010-03-05 22:25:30 +0000245 B.getInt8PtrTy(),
246 B.getInt8PtrTy(),
247 B.getInt32Ty(),
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000248 TD->getIntPtrType(Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000249 NULL);
250 CallInst *CI = B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr");
251
252 if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts()))
253 CI->setCallingConv(F->getCallingConv());
254
255 return CI;
256}
257
258/// EmitMemCmp - Emit a call to the memcmp function.
259Value *llvm::EmitMemCmp(Value *Ptr1, Value *Ptr2,
Micah Villmow3574eca2012-10-08 16:38:25 +0000260 Value *Len, IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000261 const TargetLibraryInfo *TLI) {
262 if (!TLI->has(LibFunc::memcmp))
263 return 0;
264
Eric Christopherb6174e32010-03-05 22:25:30 +0000265 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling32a57952013-01-26 00:03:11 +0000266 AttributeSet AS[3];
267 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
268 AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
Bill Wendling629fb822012-12-22 00:37:52 +0000269 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
Bill Wendling32a57952013-01-26 00:03:11 +0000270 AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
271 ArrayRef<Attribute::AttrKind>(AVs, 2));
Eric Christopherb6174e32010-03-05 22:25:30 +0000272
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000273 LLVMContext &Context = B.GetInsertBlock()->getContext();
Bill Wendling0976e002012-11-20 05:09:20 +0000274 Value *MemCmp = M->getOrInsertFunction("memcmp",
Bill Wendling32a57952013-01-26 00:03:11 +0000275 AttributeSet::get(M->getContext(), AS),
Eric Christopherb6174e32010-03-05 22:25:30 +0000276 B.getInt32Ty(),
277 B.getInt8PtrTy(),
278 B.getInt8PtrTy(),
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000279 TD->getIntPtrType(Context), NULL);
Eric Christopherb6174e32010-03-05 22:25:30 +0000280 CallInst *CI = B.CreateCall3(MemCmp, CastToCStr(Ptr1, B), CastToCStr(Ptr2, B),
281 Len, "memcmp");
282
283 if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts()))
284 CI->setCallingConv(F->getCallingConv());
285
286 return CI;
287}
288
Stephen Hines36b56882014-04-23 16:57:46 -0700289/// Append a suffix to the function name according to the type of 'Op'.
290static void AppendTypeSuffix(Value *Op, StringRef &Name, SmallString<20> &NameBuffer) {
291 if (!Op->getType()->isDoubleTy()) {
292 NameBuffer += Name;
293
294 if (Op->getType()->isFloatTy())
295 NameBuffer += 'f';
296 else
297 NameBuffer += 'l';
298
299 Name = NameBuffer;
300 }
301 return;
302}
303
Eric Christopherb6174e32010-03-05 22:25:30 +0000304/// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
305/// 'floor'). This function is known to take a single of type matching 'Op' and
306/// returns one value with the same type. If 'Op' is a long double, 'l' is
307/// added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
Benjamin Kramerb5ccb252011-11-15 19:12:09 +0000308Value *llvm::EmitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B,
Bill Wendling99faa3b2012-12-07 23:16:57 +0000309 const AttributeSet &Attrs) {
Benjamin Kramerb5ccb252011-11-15 19:12:09 +0000310 SmallString<20> NameBuffer;
Stephen Hines36b56882014-04-23 16:57:46 -0700311 AppendTypeSuffix(Op, Name, NameBuffer);
Eric Christopherb6174e32010-03-05 22:25:30 +0000312
313 Module *M = B.GetInsertBlock()->getParent()->getParent();
314 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
315 Op->getType(), NULL);
316 CallInst *CI = B.CreateCall(Callee, Op, Name);
317 CI->setAttributes(Attrs);
318 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
319 CI->setCallingConv(F->getCallingConv());
320
321 return CI;
322}
323
Stephen Hines36b56882014-04-23 16:57:46 -0700324/// EmitBinaryFloatFnCall - Emit a call to the binary function named 'Name'
325/// (e.g. 'fmin'). This function is known to take type matching 'Op1' and 'Op2'
326/// and return one value with the same type. If 'Op1/Op2' are long double, 'l'
327/// is added as the suffix of name, if 'Op1/Op2' is a float, we add a 'f'
328/// suffix.
329Value *llvm::EmitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name,
330 IRBuilder<> &B, const AttributeSet &Attrs) {
331 SmallString<20> NameBuffer;
332 AppendTypeSuffix(Op1, Name, NameBuffer);
333
334 Module *M = B.GetInsertBlock()->getParent()->getParent();
335 Value *Callee = M->getOrInsertFunction(Name, Op1->getType(),
336 Op1->getType(), Op2->getType(), NULL);
337 CallInst *CI = B.CreateCall2(Callee, Op1, Op2, Name);
338 CI->setAttributes(Attrs);
339 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
340 CI->setCallingConv(F->getCallingConv());
341
342 return CI;
343}
344
Eric Christopherb6174e32010-03-05 22:25:30 +0000345/// EmitPutChar - Emit a call to the putchar function. This assumes that Char
346/// is an integer.
Micah Villmow3574eca2012-10-08 16:38:25 +0000347Value *llvm::EmitPutChar(Value *Char, IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000348 const TargetLibraryInfo *TLI) {
349 if (!TLI->has(LibFunc::putchar))
350 return 0;
351
Eric Christopherb6174e32010-03-05 22:25:30 +0000352 Module *M = B.GetInsertBlock()->getParent()->getParent();
353 Value *PutChar = M->getOrInsertFunction("putchar", B.getInt32Ty(),
354 B.getInt32Ty(), NULL);
355 CallInst *CI = B.CreateCall(PutChar,
356 B.CreateIntCast(Char,
357 B.getInt32Ty(),
358 /*isSigned*/true,
359 "chari"),
360 "putchar");
361
362 if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts()))
363 CI->setCallingConv(F->getCallingConv());
364 return CI;
365}
366
367/// EmitPutS - Emit a call to the puts function. This assumes that Str is
368/// some pointer.
Micah Villmow3574eca2012-10-08 16:38:25 +0000369Value *llvm::EmitPutS(Value *Str, IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000370 const TargetLibraryInfo *TLI) {
371 if (!TLI->has(LibFunc::puts))
372 return 0;
373
Eric Christopherb6174e32010-03-05 22:25:30 +0000374 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling32a57952013-01-26 00:03:11 +0000375 AttributeSet AS[2];
376 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
377 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
378 Attribute::NoUnwind);
Eric Christopherb6174e32010-03-05 22:25:30 +0000379
Bill Wendling0976e002012-11-20 05:09:20 +0000380 Value *PutS = M->getOrInsertFunction("puts",
Bill Wendling32a57952013-01-26 00:03:11 +0000381 AttributeSet::get(M->getContext(), AS),
Eric Christopherb6174e32010-03-05 22:25:30 +0000382 B.getInt32Ty(),
383 B.getInt8PtrTy(),
384 NULL);
385 CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts");
386 if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
387 CI->setCallingConv(F->getCallingConv());
Nuno Lopes51004df2012-07-25 16:46:31 +0000388 return CI;
Eric Christopherb6174e32010-03-05 22:25:30 +0000389}
390
391/// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
392/// an integer and File is a pointer to FILE.
Nuno Lopes51004df2012-07-25 16:46:31 +0000393Value *llvm::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B,
Micah Villmow3574eca2012-10-08 16:38:25 +0000394 const DataLayout *TD, const TargetLibraryInfo *TLI) {
Nuno Lopes51004df2012-07-25 16:46:31 +0000395 if (!TLI->has(LibFunc::fputc))
396 return 0;
397
Eric Christopherb6174e32010-03-05 22:25:30 +0000398 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling32a57952013-01-26 00:03:11 +0000399 AttributeSet AS[2];
400 AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
401 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
402 Attribute::NoUnwind);
Eric Christopherb6174e32010-03-05 22:25:30 +0000403 Constant *F;
404 if (File->getType()->isPointerTy())
Bill Wendling0976e002012-11-20 05:09:20 +0000405 F = M->getOrInsertFunction("fputc",
Bill Wendling32a57952013-01-26 00:03:11 +0000406 AttributeSet::get(M->getContext(), AS),
Eric Christopherb6174e32010-03-05 22:25:30 +0000407 B.getInt32Ty(),
408 B.getInt32Ty(), File->getType(),
409 NULL);
410 else
411 F = M->getOrInsertFunction("fputc",
412 B.getInt32Ty(),
413 B.getInt32Ty(),
414 File->getType(), NULL);
415 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
416 "chari");
417 CallInst *CI = B.CreateCall2(F, Char, File, "fputc");
418
419 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
420 CI->setCallingConv(Fn->getCallingConv());
Nuno Lopes51004df2012-07-25 16:46:31 +0000421 return CI;
Eric Christopherb6174e32010-03-05 22:25:30 +0000422}
423
424/// EmitFPutS - Emit a call to the puts function. Str is required to be a
425/// pointer and File is a pointer to FILE.
Nuno Lopes51004df2012-07-25 16:46:31 +0000426Value *llvm::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B,
Micah Villmow3574eca2012-10-08 16:38:25 +0000427 const DataLayout *TD, const TargetLibraryInfo *TLI) {
Nuno Lopes51004df2012-07-25 16:46:31 +0000428 if (!TLI->has(LibFunc::fputs))
429 return 0;
430
Eric Christopherb6174e32010-03-05 22:25:30 +0000431 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling32a57952013-01-26 00:03:11 +0000432 AttributeSet AS[3];
433 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
434 AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
435 AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
436 Attribute::NoUnwind);
Eli Friedman9d434db2011-11-17 01:27:36 +0000437 StringRef FPutsName = TLI->getName(LibFunc::fputs);
Eric Christopherb6174e32010-03-05 22:25:30 +0000438 Constant *F;
439 if (File->getType()->isPointerTy())
Bill Wendling0976e002012-11-20 05:09:20 +0000440 F = M->getOrInsertFunction(FPutsName,
Bill Wendling32a57952013-01-26 00:03:11 +0000441 AttributeSet::get(M->getContext(), AS),
Eric Christopherb6174e32010-03-05 22:25:30 +0000442 B.getInt32Ty(),
443 B.getInt8PtrTy(),
444 File->getType(), NULL);
445 else
Eli Friedman9d434db2011-11-17 01:27:36 +0000446 F = M->getOrInsertFunction(FPutsName, B.getInt32Ty(),
Eric Christopherb6174e32010-03-05 22:25:30 +0000447 B.getInt8PtrTy(),
448 File->getType(), NULL);
449 CallInst *CI = B.CreateCall2(F, CastToCStr(Str, B), File, "fputs");
450
451 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
452 CI->setCallingConv(Fn->getCallingConv());
Nuno Lopes51004df2012-07-25 16:46:31 +0000453 return CI;
Eric Christopherb6174e32010-03-05 22:25:30 +0000454}
455
456/// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
457/// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
Nuno Lopes51004df2012-07-25 16:46:31 +0000458Value *llvm::EmitFWrite(Value *Ptr, Value *Size, Value *File,
Micah Villmow3574eca2012-10-08 16:38:25 +0000459 IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000460 const TargetLibraryInfo *TLI) {
461 if (!TLI->has(LibFunc::fwrite))
462 return 0;
463
Eric Christopherb6174e32010-03-05 22:25:30 +0000464 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling32a57952013-01-26 00:03:11 +0000465 AttributeSet AS[3];
466 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
467 AS[1] = AttributeSet::get(M->getContext(), 4, Attribute::NoCapture);
468 AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
469 Attribute::NoUnwind);
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000470 LLVMContext &Context = B.GetInsertBlock()->getContext();
Eli Friedman9d434db2011-11-17 01:27:36 +0000471 StringRef FWriteName = TLI->getName(LibFunc::fwrite);
Eric Christopherb6174e32010-03-05 22:25:30 +0000472 Constant *F;
473 if (File->getType()->isPointerTy())
Bill Wendling0976e002012-11-20 05:09:20 +0000474 F = M->getOrInsertFunction(FWriteName,
Bill Wendling32a57952013-01-26 00:03:11 +0000475 AttributeSet::get(M->getContext(), AS),
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000476 TD->getIntPtrType(Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000477 B.getInt8PtrTy(),
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000478 TD->getIntPtrType(Context),
479 TD->getIntPtrType(Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000480 File->getType(), NULL);
481 else
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000482 F = M->getOrInsertFunction(FWriteName, TD->getIntPtrType(Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000483 B.getInt8PtrTy(),
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000484 TD->getIntPtrType(Context),
485 TD->getIntPtrType(Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000486 File->getType(), NULL);
487 CallInst *CI = B.CreateCall4(F, CastToCStr(Ptr, B), Size,
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000488 ConstantInt::get(TD->getIntPtrType(Context), 1), File);
Eric Christopherb6174e32010-03-05 22:25:30 +0000489
490 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
491 CI->setCallingConv(Fn->getCallingConv());
Nuno Lopes51004df2012-07-25 16:46:31 +0000492 return CI;
Eric Christopherb6174e32010-03-05 22:25:30 +0000493}
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000494
Benjamin Kramera30b1812010-03-12 20:41:29 +0000495SimplifyFortifiedLibCalls::~SimplifyFortifiedLibCalls() { }
496
Micah Villmow3574eca2012-10-08 16:38:25 +0000497bool SimplifyFortifiedLibCalls::fold(CallInst *CI, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000498 const TargetLibraryInfo *TLI) {
Micah Villmow3574eca2012-10-08 16:38:25 +0000499 // We really need DataLayout for later.
Eric Christopher67a71b52010-04-12 04:48:00 +0000500 if (!TD) return false;
501
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000502 this->CI = CI;
Eric Christopher67a71b52010-04-12 04:48:00 +0000503 Function *Callee = CI->getCalledFunction();
504 StringRef Name = Callee->getName();
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000505 FunctionType *FT = Callee->getFunctionType();
Eric Christopher67a71b52010-04-12 04:48:00 +0000506 LLVMContext &Context = CI->getParent()->getContext();
Eli Friedman1a24bf02011-05-27 01:00:36 +0000507 IRBuilder<> B(CI);
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000508
509 if (Name == "__memcpy_chk") {
Eric Christopher67a71b52010-04-12 04:48:00 +0000510 // Check if this has the right signature.
511 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
512 !FT->getParamType(0)->isPointerTy() ||
513 !FT->getParamType(1)->isPointerTy() ||
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000514 FT->getParamType(2) != TD->getIntPtrType(Context) ||
515 FT->getParamType(3) != TD->getIntPtrType(Context))
Eric Christopher67a71b52010-04-12 04:48:00 +0000516 return false;
Gabor Greiff148cb62010-06-28 12:29:20 +0000517
Gabor Greifa6aac4c2010-07-16 09:38:02 +0000518 if (isFoldable(3, 2, false)) {
Benjamin Kramerdef548f2010-12-27 00:25:32 +0000519 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
520 CI->getArgOperand(2), 1);
Gabor Greiff148cb62010-06-28 12:29:20 +0000521 replaceCall(CI->getArgOperand(0));
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000522 return true;
523 }
524 return false;
525 }
526
527 // Should be similar to memcpy.
528 if (Name == "__mempcpy_chk") {
529 return false;
530 }
531
532 if (Name == "__memmove_chk") {
Eric Christopher67a71b52010-04-12 04:48:00 +0000533 // Check if this has the right signature.
534 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
535 !FT->getParamType(0)->isPointerTy() ||
536 !FT->getParamType(1)->isPointerTy() ||
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000537 FT->getParamType(2) != TD->getIntPtrType(Context) ||
538 FT->getParamType(3) != TD->getIntPtrType(Context))
Eric Christopher67a71b52010-04-12 04:48:00 +0000539 return false;
Gabor Greiff148cb62010-06-28 12:29:20 +0000540
Gabor Greifa6aac4c2010-07-16 09:38:02 +0000541 if (isFoldable(3, 2, false)) {
Benjamin Kramerdef548f2010-12-27 00:25:32 +0000542 B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
543 CI->getArgOperand(2), 1);
Gabor Greiff148cb62010-06-28 12:29:20 +0000544 replaceCall(CI->getArgOperand(0));
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000545 return true;
546 }
547 return false;
548 }
549
550 if (Name == "__memset_chk") {
Eric Christopher67a71b52010-04-12 04:48:00 +0000551 // Check if this has the right signature.
552 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
553 !FT->getParamType(0)->isPointerTy() ||
554 !FT->getParamType(1)->isIntegerTy() ||
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000555 FT->getParamType(2) != TD->getIntPtrType(Context) ||
556 FT->getParamType(3) != TD->getIntPtrType(Context))
Eric Christopher67a71b52010-04-12 04:48:00 +0000557 return false;
Gabor Greiff148cb62010-06-28 12:29:20 +0000558
Gabor Greifa6aac4c2010-07-16 09:38:02 +0000559 if (isFoldable(3, 2, false)) {
Gabor Greiff148cb62010-06-28 12:29:20 +0000560 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(),
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000561 false);
Benjamin Kramerdef548f2010-12-27 00:25:32 +0000562 B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
Gabor Greiff1ba6512010-06-25 07:58:41 +0000563 replaceCall(CI->getArgOperand(0));
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000564 return true;
565 }
566 return false;
567 }
568
569 if (Name == "__strcpy_chk" || Name == "__stpcpy_chk") {
Eric Christopher67a71b52010-04-12 04:48:00 +0000570 // Check if this has the right signature.
571 if (FT->getNumParams() != 3 ||
572 FT->getReturnType() != FT->getParamType(0) ||
573 FT->getParamType(0) != FT->getParamType(1) ||
574 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000575 FT->getParamType(2) != TD->getIntPtrType(Context))
Eric Christopher67a71b52010-04-12 04:48:00 +0000576 return 0;
577
578
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000579 // If a) we don't have any length information, or b) we know this will
580 // fit then just lower to a plain st[rp]cpy. Otherwise we'll keep our
581 // st[rp]cpy_chk call which may fail at runtime if the size is too long.
582 // TODO: It might be nice to get a maximum length out of the possible
583 // string lengths for varying.
Gabor Greifa6aac4c2010-07-16 09:38:02 +0000584 if (isFoldable(2, 1, true)) {
Gabor Greiff148cb62010-06-28 12:29:20 +0000585 Value *Ret = EmitStrCpy(CI->getArgOperand(0), CI->getArgOperand(1), B, TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000586 TLI, Name.substr(2, 6));
Nuno Lopes28ad8632012-08-01 16:58:51 +0000587 if (!Ret)
Nuno Lopes918067d2012-08-01 17:13:28 +0000588 return false;
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000589 replaceCall(Ret);
590 return true;
591 }
592 return false;
593 }
594
Eric Christopher71988f12010-04-07 23:00:07 +0000595 if (Name == "__strncpy_chk" || Name == "__stpncpy_chk") {
Eric Christopher67a71b52010-04-12 04:48:00 +0000596 // Check if this has the right signature.
597 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
598 FT->getParamType(0) != FT->getParamType(1) ||
599 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
600 !FT->getParamType(2)->isIntegerTy() ||
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000601 FT->getParamType(3) != TD->getIntPtrType(Context))
Eric Christopher2a7cb9d2010-04-13 16:41:29 +0000602 return false;
Gabor Greiff148cb62010-06-28 12:29:20 +0000603
Gabor Greifa6aac4c2010-07-16 09:38:02 +0000604 if (isFoldable(3, 2, false)) {
Gabor Greiff148cb62010-06-28 12:29:20 +0000605 Value *Ret = EmitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
Nuno Lopes51004df2012-07-25 16:46:31 +0000606 CI->getArgOperand(2), B, TD, TLI,
607 Name.substr(2, 7));
Nuno Lopes28ad8632012-08-01 16:58:51 +0000608 if (!Ret)
Nuno Lopes918067d2012-08-01 17:13:28 +0000609 return false;
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000610 replaceCall(Ret);
611 return true;
612 }
613 return false;
614 }
615
616 if (Name == "__strcat_chk") {
617 return false;
618 }
619
620 if (Name == "__strncat_chk") {
621 return false;
622 }
623
624 return false;
625}