blob: 26240d4dfe4120cbc1fd5faa02a6a091adf5ae87 [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"
Eric Christopherb6174e32010-03-05 22:25:30 +000015#include "llvm/Constants.h"
16#include "llvm/Function.h"
Chandler Carruth06cb8ed2012-06-29 12:38:19 +000017#include "llvm/IRBuilder.h"
Benjamin Kramerb5ccb252011-11-15 19:12:09 +000018#include "llvm/Intrinsics.h"
Chandler Carruth06cb8ed2012-06-29 12:38:19 +000019#include "llvm/Intrinsics.h"
20#include "llvm/LLVMContext.h"
Benjamin Kramerb5ccb252011-11-15 19:12:09 +000021#include "llvm/LLVMContext.h"
Eric Christopherb6174e32010-03-05 22:25:30 +000022#include "llvm/Module.h"
Chandler Carruth06cb8ed2012-06-29 12:38:19 +000023#include "llvm/Type.h"
24#include "llvm/ADT/SmallString.h"
Micah Villmow3574eca2012-10-08 16:38:25 +000025#include "llvm/DataLayout.h"
Eli Friedman9d434db2011-11-17 01:27:36 +000026#include "llvm/Target/TargetLibraryInfo.h"
Eric Christopherb6174e32010-03-05 22:25:30 +000027
28using namespace llvm;
29
30/// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
31Value *llvm::CastToCStr(Value *V, IRBuilder<> &B) {
32 return B.CreateBitCast(V, B.getInt8PtrTy(), "cstr");
33}
34
35/// EmitStrLen - Emit a call to the strlen function to the builder, for the
36/// specified pointer. This always returns an integer value of size intptr_t.
Micah Villmow3574eca2012-10-08 16:38:25 +000037Value *llvm::EmitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +000038 const TargetLibraryInfo *TLI) {
39 if (!TLI->has(LibFunc::strlen))
40 return 0;
41
Eric Christopherb6174e32010-03-05 22:25:30 +000042 Module *M = B.GetInsertBlock()->getParent()->getParent();
43 AttributeWithIndex AWI[2];
Bill Wendling11d00422012-10-10 06:13:42 +000044 AWI[0] = AttributeWithIndex::get(1, Attributes::NoCapture);
45 Attributes::AttrVal AVs[2] = { Attributes::ReadOnly, Attributes::NoUnwind };
46 AWI[1] = AttributeWithIndex::get(~0u, ArrayRef<Attributes::AttrVal>(AVs, 2));
Eric Christopherb6174e32010-03-05 22:25:30 +000047
48 LLVMContext &Context = B.GetInsertBlock()->getContext();
Chris Lattnerd509d0b2012-05-28 01:47:44 +000049 Constant *StrLen = M->getOrInsertFunction("strlen", AttrListPtr::get(AWI),
Eric Christopherb6174e32010-03-05 22:25:30 +000050 TD->getIntPtrType(Context),
51 B.getInt8PtrTy(),
52 NULL);
53 CallInst *CI = B.CreateCall(StrLen, CastToCStr(Ptr, B), "strlen");
54 if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts()))
55 CI->setCallingConv(F->getCallingConv());
56
57 return CI;
58}
59
Nuno Lopesa5368352012-07-25 17:18:59 +000060/// EmitStrNLen - Emit a call to the strnlen function to the builder, for the
61/// specified pointer. Ptr is required to be some pointer type, MaxLen must
62/// be of size_t type, and the return value has 'intptr_t' type.
63Value *llvm::EmitStrNLen(Value *Ptr, Value *MaxLen, IRBuilder<> &B,
Micah Villmow3574eca2012-10-08 16:38:25 +000064 const DataLayout *TD, const TargetLibraryInfo *TLI) {
Nuno Lopesa5368352012-07-25 17:18:59 +000065 if (!TLI->has(LibFunc::strnlen))
66 return 0;
67
68 Module *M = B.GetInsertBlock()->getParent()->getParent();
69 AttributeWithIndex AWI[2];
Bill Wendling11d00422012-10-10 06:13:42 +000070 AWI[0] = AttributeWithIndex::get(1, Attributes::NoCapture);
71 Attributes::AttrVal AVs[2] = { Attributes::ReadOnly, Attributes::NoUnwind };
72 AWI[1] = AttributeWithIndex::get(~0u, ArrayRef<Attributes::AttrVal>(AVs, 2));
Nuno Lopesa5368352012-07-25 17:18:59 +000073
74 LLVMContext &Context = B.GetInsertBlock()->getContext();
75 Constant *StrNLen = M->getOrInsertFunction("strnlen", AttrListPtr::get(AWI),
76 TD->getIntPtrType(Context),
77 B.getInt8PtrTy(),
78 TD->getIntPtrType(Context),
79 NULL);
80 CallInst *CI = B.CreateCall2(StrNLen, CastToCStr(Ptr, B), MaxLen, "strnlen");
81 if (const Function *F = dyn_cast<Function>(StrNLen->stripPointerCasts()))
82 CI->setCallingConv(F->getCallingConv());
83
84 return CI;
85}
86
Eric Christopherb6174e32010-03-05 22:25:30 +000087/// EmitStrChr - Emit a call to the strchr function to the builder, for the
88/// specified pointer and character. Ptr is required to be some pointer type,
89/// and the return value has 'i8*' type.
90Value *llvm::EmitStrChr(Value *Ptr, char C, IRBuilder<> &B,
Micah Villmow3574eca2012-10-08 16:38:25 +000091 const DataLayout *TD, const TargetLibraryInfo *TLI) {
Nuno Lopes51004df2012-07-25 16:46:31 +000092 if (!TLI->has(LibFunc::strchr))
93 return 0;
94
Eric Christopherb6174e32010-03-05 22:25:30 +000095 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling11d00422012-10-10 06:13:42 +000096 Attributes::AttrVal AVs[2] = { Attributes::ReadOnly, Attributes::NoUnwind };
Eric Christopherb6174e32010-03-05 22:25:30 +000097 AttributeWithIndex AWI =
Bill Wendling11d00422012-10-10 06:13:42 +000098 AttributeWithIndex::get(~0u, ArrayRef<Attributes::AttrVal>(AVs, 2));
Eric Christopherb6174e32010-03-05 22:25:30 +000099
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000100 Type *I8Ptr = B.getInt8PtrTy();
101 Type *I32Ty = B.getInt32Ty();
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000102 Constant *StrChr = M->getOrInsertFunction("strchr", AttrListPtr::get(AWI),
Eric Christopherb6174e32010-03-05 22:25:30 +0000103 I8Ptr, I8Ptr, I32Ty, NULL);
104 CallInst *CI = B.CreateCall2(StrChr, CastToCStr(Ptr, B),
105 ConstantInt::get(I32Ty, C), "strchr");
106 if (const Function *F = dyn_cast<Function>(StrChr->stripPointerCasts()))
107 CI->setCallingConv(F->getCallingConv());
108 return CI;
109}
110
Benjamin Kramer386e9182010-06-15 21:34:25 +0000111/// EmitStrNCmp - Emit a call to the strncmp function to the builder.
112Value *llvm::EmitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len,
Micah Villmow3574eca2012-10-08 16:38:25 +0000113 IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000114 const TargetLibraryInfo *TLI) {
115 if (!TLI->has(LibFunc::strncmp))
116 return 0;
117
Benjamin Kramer386e9182010-06-15 21:34:25 +0000118 Module *M = B.GetInsertBlock()->getParent()->getParent();
119 AttributeWithIndex AWI[3];
Bill Wendling11d00422012-10-10 06:13:42 +0000120 AWI[0] = AttributeWithIndex::get(1, Attributes::NoCapture);
121 AWI[1] = AttributeWithIndex::get(2, Attributes::NoCapture);
122 Attributes::AttrVal AVs[2] = { Attributes::ReadOnly, Attributes::NoUnwind };
123 AWI[2] = AttributeWithIndex::get(~0u, ArrayRef<Attributes::AttrVal>(AVs, 2));
Benjamin Kramer386e9182010-06-15 21:34:25 +0000124
125 LLVMContext &Context = B.GetInsertBlock()->getContext();
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000126 Value *StrNCmp = M->getOrInsertFunction("strncmp", AttrListPtr::get(AWI),
Benjamin Kramer386e9182010-06-15 21:34:25 +0000127 B.getInt32Ty(),
128 B.getInt8PtrTy(),
129 B.getInt8PtrTy(),
130 TD->getIntPtrType(Context), NULL);
131 CallInst *CI = B.CreateCall3(StrNCmp, CastToCStr(Ptr1, B),
132 CastToCStr(Ptr2, B), Len, "strncmp");
133
134 if (const Function *F = dyn_cast<Function>(StrNCmp->stripPointerCasts()))
135 CI->setCallingConv(F->getCallingConv());
136
137 return CI;
138}
139
Eric Christopherb6174e32010-03-05 22:25:30 +0000140/// EmitStrCpy - Emit a call to the strcpy function to the builder, for the
141/// specified pointer arguments.
142Value *llvm::EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
Micah Villmow3574eca2012-10-08 16:38:25 +0000143 const DataLayout *TD, const TargetLibraryInfo *TLI,
Nuno Lopes51004df2012-07-25 16:46:31 +0000144 StringRef Name) {
145 if (!TLI->has(LibFunc::strcpy))
146 return 0;
147
Eric Christopherb6174e32010-03-05 22:25:30 +0000148 Module *M = B.GetInsertBlock()->getParent()->getParent();
149 AttributeWithIndex AWI[2];
Bill Wendling11d00422012-10-10 06:13:42 +0000150 AWI[0] = AttributeWithIndex::get(2, Attributes::NoCapture);
151 AWI[1] = AttributeWithIndex::get(~0u, Attributes::NoUnwind);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000152 Type *I8Ptr = B.getInt8PtrTy();
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000153 Value *StrCpy = M->getOrInsertFunction(Name, AttrListPtr::get(AWI),
Eric Christopherb6174e32010-03-05 22:25:30 +0000154 I8Ptr, I8Ptr, I8Ptr, NULL);
155 CallInst *CI = B.CreateCall2(StrCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
Benjamin Kramer7fa30b82010-03-11 20:45:13 +0000156 Name);
Eric Christopherb6174e32010-03-05 22:25:30 +0000157 if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts()))
158 CI->setCallingConv(F->getCallingConv());
159 return CI;
160}
161
Eric Christopherb0722af2010-03-11 17:45:38 +0000162/// EmitStrNCpy - Emit a call to the strncpy function to the builder, for the
Eric Christopherbd973762010-03-11 01:25:07 +0000163/// specified pointer arguments.
164Value *llvm::EmitStrNCpy(Value *Dst, Value *Src, Value *Len,
Micah Villmow3574eca2012-10-08 16:38:25 +0000165 IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000166 const TargetLibraryInfo *TLI, StringRef Name) {
167 if (!TLI->has(LibFunc::strncpy))
168 return 0;
169
Eric Christopherbd973762010-03-11 01:25:07 +0000170 Module *M = B.GetInsertBlock()->getParent()->getParent();
171 AttributeWithIndex AWI[2];
Bill Wendling11d00422012-10-10 06:13:42 +0000172 AWI[0] = AttributeWithIndex::get(2, Attributes::NoCapture);
173 AWI[1] = AttributeWithIndex::get(~0u, Attributes::NoUnwind);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000174 Type *I8Ptr = B.getInt8PtrTy();
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000175 Value *StrNCpy = M->getOrInsertFunction(Name, AttrListPtr::get(AWI),
Eric Christopher71988f12010-04-07 23:00:07 +0000176 I8Ptr, I8Ptr, I8Ptr,
177 Len->getType(), NULL);
Eric Christopherbd973762010-03-11 01:25:07 +0000178 CallInst *CI = B.CreateCall3(StrNCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
179 Len, "strncpy");
180 if (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts()))
181 CI->setCallingConv(F->getCallingConv());
182 return CI;
183}
184
Evan Cheng0289b412010-03-23 15:48:04 +0000185/// EmitMemCpyChk - Emit a call to the __memcpy_chk function to the builder.
186/// This expects that the Len and ObjSize have type 'intptr_t' and Dst/Src
187/// are pointers.
188Value *llvm::EmitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
Micah Villmow3574eca2012-10-08 16:38:25 +0000189 IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000190 const TargetLibraryInfo *TLI) {
191 if (!TLI->has(LibFunc::memcpy_chk))
192 return 0;
193
Evan Cheng0289b412010-03-23 15:48:04 +0000194 Module *M = B.GetInsertBlock()->getParent()->getParent();
195 AttributeWithIndex AWI;
Bill Wendling11d00422012-10-10 06:13:42 +0000196 AWI = AttributeWithIndex::get(~0u, Attributes::NoUnwind);
Evan Cheng0289b412010-03-23 15:48:04 +0000197 LLVMContext &Context = B.GetInsertBlock()->getContext();
198 Value *MemCpy = M->getOrInsertFunction("__memcpy_chk",
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000199 AttrListPtr::get(AWI),
Evan Cheng0289b412010-03-23 15:48:04 +0000200 B.getInt8PtrTy(),
201 B.getInt8PtrTy(),
202 B.getInt8PtrTy(),
203 TD->getIntPtrType(Context),
204 TD->getIntPtrType(Context), NULL);
205 Dst = CastToCStr(Dst, B);
206 Src = CastToCStr(Src, B);
207 CallInst *CI = B.CreateCall4(MemCpy, Dst, Src, Len, ObjSize);
208 if (const Function *F = dyn_cast<Function>(MemCpy->stripPointerCasts()))
209 CI->setCallingConv(F->getCallingConv());
210 return CI;
211}
212
Eric Christopherb6174e32010-03-05 22:25:30 +0000213/// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
214/// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
215Value *llvm::EmitMemChr(Value *Ptr, Value *Val,
Micah Villmow3574eca2012-10-08 16:38:25 +0000216 Value *Len, IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000217 const TargetLibraryInfo *TLI) {
218 if (!TLI->has(LibFunc::memchr))
219 return 0;
220
Eric Christopherb6174e32010-03-05 22:25:30 +0000221 Module *M = B.GetInsertBlock()->getParent()->getParent();
222 AttributeWithIndex AWI;
Bill Wendling11d00422012-10-10 06:13:42 +0000223 Attributes::AttrVal AVs[2] = { Attributes::ReadOnly, Attributes::NoUnwind };
224 AWI = AttributeWithIndex::get(~0u, ArrayRef<Attributes::AttrVal>(AVs, 2));
Eric Christopherb6174e32010-03-05 22:25:30 +0000225 LLVMContext &Context = B.GetInsertBlock()->getContext();
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000226 Value *MemChr = M->getOrInsertFunction("memchr", AttrListPtr::get(AWI),
Eric Christopherb6174e32010-03-05 22:25:30 +0000227 B.getInt8PtrTy(),
228 B.getInt8PtrTy(),
229 B.getInt32Ty(),
230 TD->getIntPtrType(Context),
231 NULL);
232 CallInst *CI = B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr");
233
234 if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts()))
235 CI->setCallingConv(F->getCallingConv());
236
237 return CI;
238}
239
240/// EmitMemCmp - Emit a call to the memcmp function.
241Value *llvm::EmitMemCmp(Value *Ptr1, Value *Ptr2,
Micah Villmow3574eca2012-10-08 16:38:25 +0000242 Value *Len, IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000243 const TargetLibraryInfo *TLI) {
244 if (!TLI->has(LibFunc::memcmp))
245 return 0;
246
Eric Christopherb6174e32010-03-05 22:25:30 +0000247 Module *M = B.GetInsertBlock()->getParent()->getParent();
248 AttributeWithIndex AWI[3];
Bill Wendling11d00422012-10-10 06:13:42 +0000249 AWI[0] = AttributeWithIndex::get(1, Attributes::NoCapture);
250 AWI[1] = AttributeWithIndex::get(2, Attributes::NoCapture);
251 Attributes::AttrVal AVs[2] = { Attributes::ReadOnly, Attributes::NoUnwind };
252 AWI[2] = AttributeWithIndex::get(~0u, ArrayRef<Attributes::AttrVal>(AVs, 2));
Eric Christopherb6174e32010-03-05 22:25:30 +0000253
254 LLVMContext &Context = B.GetInsertBlock()->getContext();
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000255 Value *MemCmp = M->getOrInsertFunction("memcmp", AttrListPtr::get(AWI),
Eric Christopherb6174e32010-03-05 22:25:30 +0000256 B.getInt32Ty(),
257 B.getInt8PtrTy(),
258 B.getInt8PtrTy(),
259 TD->getIntPtrType(Context), NULL);
260 CallInst *CI = B.CreateCall3(MemCmp, CastToCStr(Ptr1, B), CastToCStr(Ptr2, B),
261 Len, "memcmp");
262
263 if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts()))
264 CI->setCallingConv(F->getCallingConv());
265
266 return CI;
267}
268
Eric Christopherb6174e32010-03-05 22:25:30 +0000269/// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
270/// 'floor'). This function is known to take a single of type matching 'Op' and
271/// returns one value with the same type. If 'Op' is a long double, 'l' is
272/// added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
Benjamin Kramerb5ccb252011-11-15 19:12:09 +0000273Value *llvm::EmitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B,
274 const AttrListPtr &Attrs) {
275 SmallString<20> NameBuffer;
Eric Christopherb6174e32010-03-05 22:25:30 +0000276 if (!Op->getType()->isDoubleTy()) {
277 // If we need to add a suffix, copy into NameBuffer.
Benjamin Kramerb5ccb252011-11-15 19:12:09 +0000278 NameBuffer += Name;
Eric Christopherb6174e32010-03-05 22:25:30 +0000279 if (Op->getType()->isFloatTy())
Benjamin Kramerb5ccb252011-11-15 19:12:09 +0000280 NameBuffer += 'f'; // floorf
Eric Christopherb6174e32010-03-05 22:25:30 +0000281 else
Benjamin Kramerb5ccb252011-11-15 19:12:09 +0000282 NameBuffer += 'l'; // floorl
Eric Christopherb6174e32010-03-05 22:25:30 +0000283 Name = NameBuffer;
284 }
285
286 Module *M = B.GetInsertBlock()->getParent()->getParent();
287 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
288 Op->getType(), NULL);
289 CallInst *CI = B.CreateCall(Callee, Op, Name);
290 CI->setAttributes(Attrs);
291 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
292 CI->setCallingConv(F->getCallingConv());
293
294 return CI;
295}
296
297/// EmitPutChar - Emit a call to the putchar function. This assumes that Char
298/// is an integer.
Micah Villmow3574eca2012-10-08 16:38:25 +0000299Value *llvm::EmitPutChar(Value *Char, IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000300 const TargetLibraryInfo *TLI) {
301 if (!TLI->has(LibFunc::putchar))
302 return 0;
303
Eric Christopherb6174e32010-03-05 22:25:30 +0000304 Module *M = B.GetInsertBlock()->getParent()->getParent();
305 Value *PutChar = M->getOrInsertFunction("putchar", B.getInt32Ty(),
306 B.getInt32Ty(), NULL);
307 CallInst *CI = B.CreateCall(PutChar,
308 B.CreateIntCast(Char,
309 B.getInt32Ty(),
310 /*isSigned*/true,
311 "chari"),
312 "putchar");
313
314 if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts()))
315 CI->setCallingConv(F->getCallingConv());
316 return CI;
317}
318
319/// EmitPutS - Emit a call to the puts function. This assumes that Str is
320/// some pointer.
Micah Villmow3574eca2012-10-08 16:38:25 +0000321Value *llvm::EmitPutS(Value *Str, IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000322 const TargetLibraryInfo *TLI) {
323 if (!TLI->has(LibFunc::puts))
324 return 0;
325
Eric Christopherb6174e32010-03-05 22:25:30 +0000326 Module *M = B.GetInsertBlock()->getParent()->getParent();
327 AttributeWithIndex AWI[2];
Bill Wendling11d00422012-10-10 06:13:42 +0000328 AWI[0] = AttributeWithIndex::get(1, Attributes::NoCapture);
329 AWI[1] = AttributeWithIndex::get(~0u, Attributes::NoUnwind);
Eric Christopherb6174e32010-03-05 22:25:30 +0000330
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000331 Value *PutS = M->getOrInsertFunction("puts", AttrListPtr::get(AWI),
Eric Christopherb6174e32010-03-05 22:25:30 +0000332 B.getInt32Ty(),
333 B.getInt8PtrTy(),
334 NULL);
335 CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts");
336 if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
337 CI->setCallingConv(F->getCallingConv());
Nuno Lopes51004df2012-07-25 16:46:31 +0000338 return CI;
Eric Christopherb6174e32010-03-05 22:25:30 +0000339}
340
341/// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
342/// an integer and File is a pointer to FILE.
Nuno Lopes51004df2012-07-25 16:46:31 +0000343Value *llvm::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B,
Micah Villmow3574eca2012-10-08 16:38:25 +0000344 const DataLayout *TD, const TargetLibraryInfo *TLI) {
Nuno Lopes51004df2012-07-25 16:46:31 +0000345 if (!TLI->has(LibFunc::fputc))
346 return 0;
347
Eric Christopherb6174e32010-03-05 22:25:30 +0000348 Module *M = B.GetInsertBlock()->getParent()->getParent();
349 AttributeWithIndex AWI[2];
Bill Wendling11d00422012-10-10 06:13:42 +0000350 AWI[0] = AttributeWithIndex::get(2, Attributes::NoCapture);
351 AWI[1] = AttributeWithIndex::get(~0u, Attributes::NoUnwind);
Eric Christopherb6174e32010-03-05 22:25:30 +0000352 Constant *F;
353 if (File->getType()->isPointerTy())
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000354 F = M->getOrInsertFunction("fputc", AttrListPtr::get(AWI),
Eric Christopherb6174e32010-03-05 22:25:30 +0000355 B.getInt32Ty(),
356 B.getInt32Ty(), File->getType(),
357 NULL);
358 else
359 F = M->getOrInsertFunction("fputc",
360 B.getInt32Ty(),
361 B.getInt32Ty(),
362 File->getType(), NULL);
363 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
364 "chari");
365 CallInst *CI = B.CreateCall2(F, Char, File, "fputc");
366
367 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
368 CI->setCallingConv(Fn->getCallingConv());
Nuno Lopes51004df2012-07-25 16:46:31 +0000369 return CI;
Eric Christopherb6174e32010-03-05 22:25:30 +0000370}
371
372/// EmitFPutS - Emit a call to the puts function. Str is required to be a
373/// pointer and File is a pointer to FILE.
Nuno Lopes51004df2012-07-25 16:46:31 +0000374Value *llvm::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B,
Micah Villmow3574eca2012-10-08 16:38:25 +0000375 const DataLayout *TD, const TargetLibraryInfo *TLI) {
Nuno Lopes51004df2012-07-25 16:46:31 +0000376 if (!TLI->has(LibFunc::fputs))
377 return 0;
378
Eric Christopherb6174e32010-03-05 22:25:30 +0000379 Module *M = B.GetInsertBlock()->getParent()->getParent();
380 AttributeWithIndex AWI[3];
Bill Wendling11d00422012-10-10 06:13:42 +0000381 AWI[0] = AttributeWithIndex::get(1, Attributes::NoCapture);
382 AWI[1] = AttributeWithIndex::get(2, Attributes::NoCapture);
383 AWI[2] = AttributeWithIndex::get(~0u, Attributes::NoUnwind);
Eli Friedman9d434db2011-11-17 01:27:36 +0000384 StringRef FPutsName = TLI->getName(LibFunc::fputs);
Eric Christopherb6174e32010-03-05 22:25:30 +0000385 Constant *F;
386 if (File->getType()->isPointerTy())
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000387 F = M->getOrInsertFunction(FPutsName, AttrListPtr::get(AWI),
Eric Christopherb6174e32010-03-05 22:25:30 +0000388 B.getInt32Ty(),
389 B.getInt8PtrTy(),
390 File->getType(), NULL);
391 else
Eli Friedman9d434db2011-11-17 01:27:36 +0000392 F = M->getOrInsertFunction(FPutsName, B.getInt32Ty(),
Eric Christopherb6174e32010-03-05 22:25:30 +0000393 B.getInt8PtrTy(),
394 File->getType(), NULL);
395 CallInst *CI = B.CreateCall2(F, CastToCStr(Str, B), File, "fputs");
396
397 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
398 CI->setCallingConv(Fn->getCallingConv());
Nuno Lopes51004df2012-07-25 16:46:31 +0000399 return CI;
Eric Christopherb6174e32010-03-05 22:25:30 +0000400}
401
402/// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
403/// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
Nuno Lopes51004df2012-07-25 16:46:31 +0000404Value *llvm::EmitFWrite(Value *Ptr, Value *Size, Value *File,
Micah Villmow3574eca2012-10-08 16:38:25 +0000405 IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000406 const TargetLibraryInfo *TLI) {
407 if (!TLI->has(LibFunc::fwrite))
408 return 0;
409
Eric Christopherb6174e32010-03-05 22:25:30 +0000410 Module *M = B.GetInsertBlock()->getParent()->getParent();
411 AttributeWithIndex AWI[3];
Bill Wendling11d00422012-10-10 06:13:42 +0000412 AWI[0] = AttributeWithIndex::get(1, Attributes::NoCapture);
413 AWI[1] = AttributeWithIndex::get(4, Attributes::NoCapture);
414 AWI[2] = AttributeWithIndex::get(~0u, Attributes::NoUnwind);
Eric Christopherb6174e32010-03-05 22:25:30 +0000415 LLVMContext &Context = B.GetInsertBlock()->getContext();
Eli Friedman9d434db2011-11-17 01:27:36 +0000416 StringRef FWriteName = TLI->getName(LibFunc::fwrite);
Eric Christopherb6174e32010-03-05 22:25:30 +0000417 Constant *F;
418 if (File->getType()->isPointerTy())
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000419 F = M->getOrInsertFunction(FWriteName, AttrListPtr::get(AWI),
Eric Christopherb6174e32010-03-05 22:25:30 +0000420 TD->getIntPtrType(Context),
421 B.getInt8PtrTy(),
422 TD->getIntPtrType(Context),
423 TD->getIntPtrType(Context),
424 File->getType(), NULL);
425 else
Eli Friedman9d434db2011-11-17 01:27:36 +0000426 F = M->getOrInsertFunction(FWriteName, TD->getIntPtrType(Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000427 B.getInt8PtrTy(),
428 TD->getIntPtrType(Context),
429 TD->getIntPtrType(Context),
430 File->getType(), NULL);
431 CallInst *CI = B.CreateCall4(F, CastToCStr(Ptr, B), Size,
432 ConstantInt::get(TD->getIntPtrType(Context), 1), File);
433
434 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
435 CI->setCallingConv(Fn->getCallingConv());
Nuno Lopes51004df2012-07-25 16:46:31 +0000436 return CI;
Eric Christopherb6174e32010-03-05 22:25:30 +0000437}
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000438
Benjamin Kramera30b1812010-03-12 20:41:29 +0000439SimplifyFortifiedLibCalls::~SimplifyFortifiedLibCalls() { }
440
Micah Villmow3574eca2012-10-08 16:38:25 +0000441bool SimplifyFortifiedLibCalls::fold(CallInst *CI, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000442 const TargetLibraryInfo *TLI) {
Micah Villmow3574eca2012-10-08 16:38:25 +0000443 // We really need DataLayout for later.
Eric Christopher67a71b52010-04-12 04:48:00 +0000444 if (!TD) return false;
445
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000446 this->CI = CI;
Eric Christopher67a71b52010-04-12 04:48:00 +0000447 Function *Callee = CI->getCalledFunction();
448 StringRef Name = Callee->getName();
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000449 FunctionType *FT = Callee->getFunctionType();
Eric Christopher67a71b52010-04-12 04:48:00 +0000450 LLVMContext &Context = CI->getParent()->getContext();
Eli Friedman1a24bf02011-05-27 01:00:36 +0000451 IRBuilder<> B(CI);
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000452
453 if (Name == "__memcpy_chk") {
Eric Christopher67a71b52010-04-12 04:48:00 +0000454 // Check if this has the right signature.
455 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
456 !FT->getParamType(0)->isPointerTy() ||
457 !FT->getParamType(1)->isPointerTy() ||
458 FT->getParamType(2) != TD->getIntPtrType(Context) ||
459 FT->getParamType(3) != TD->getIntPtrType(Context))
460 return false;
Gabor Greiff148cb62010-06-28 12:29:20 +0000461
Gabor Greifa6aac4c2010-07-16 09:38:02 +0000462 if (isFoldable(3, 2, false)) {
Benjamin Kramerdef548f2010-12-27 00:25:32 +0000463 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
464 CI->getArgOperand(2), 1);
Gabor Greiff148cb62010-06-28 12:29:20 +0000465 replaceCall(CI->getArgOperand(0));
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000466 return true;
467 }
468 return false;
469 }
470
471 // Should be similar to memcpy.
472 if (Name == "__mempcpy_chk") {
473 return false;
474 }
475
476 if (Name == "__memmove_chk") {
Eric Christopher67a71b52010-04-12 04:48:00 +0000477 // Check if this has the right signature.
478 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
479 !FT->getParamType(0)->isPointerTy() ||
480 !FT->getParamType(1)->isPointerTy() ||
481 FT->getParamType(2) != TD->getIntPtrType(Context) ||
482 FT->getParamType(3) != TD->getIntPtrType(Context))
483 return false;
Gabor Greiff148cb62010-06-28 12:29:20 +0000484
Gabor Greifa6aac4c2010-07-16 09:38:02 +0000485 if (isFoldable(3, 2, false)) {
Benjamin Kramerdef548f2010-12-27 00:25:32 +0000486 B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
487 CI->getArgOperand(2), 1);
Gabor Greiff148cb62010-06-28 12:29:20 +0000488 replaceCall(CI->getArgOperand(0));
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000489 return true;
490 }
491 return false;
492 }
493
494 if (Name == "__memset_chk") {
Eric Christopher67a71b52010-04-12 04:48:00 +0000495 // Check if this has the right signature.
496 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
497 !FT->getParamType(0)->isPointerTy() ||
498 !FT->getParamType(1)->isIntegerTy() ||
499 FT->getParamType(2) != TD->getIntPtrType(Context) ||
500 FT->getParamType(3) != TD->getIntPtrType(Context))
501 return false;
Gabor Greiff148cb62010-06-28 12:29:20 +0000502
Gabor Greifa6aac4c2010-07-16 09:38:02 +0000503 if (isFoldable(3, 2, false)) {
Gabor Greiff148cb62010-06-28 12:29:20 +0000504 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(),
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000505 false);
Benjamin Kramerdef548f2010-12-27 00:25:32 +0000506 B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
Gabor Greiff1ba6512010-06-25 07:58:41 +0000507 replaceCall(CI->getArgOperand(0));
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000508 return true;
509 }
510 return false;
511 }
512
513 if (Name == "__strcpy_chk" || Name == "__stpcpy_chk") {
Eric Christopher67a71b52010-04-12 04:48:00 +0000514 // Check if this has the right signature.
515 if (FT->getNumParams() != 3 ||
516 FT->getReturnType() != FT->getParamType(0) ||
517 FT->getParamType(0) != FT->getParamType(1) ||
518 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
519 FT->getParamType(2) != TD->getIntPtrType(Context))
520 return 0;
521
522
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000523 // If a) we don't have any length information, or b) we know this will
524 // fit then just lower to a plain st[rp]cpy. Otherwise we'll keep our
525 // st[rp]cpy_chk call which may fail at runtime if the size is too long.
526 // TODO: It might be nice to get a maximum length out of the possible
527 // string lengths for varying.
Gabor Greifa6aac4c2010-07-16 09:38:02 +0000528 if (isFoldable(2, 1, true)) {
Gabor Greiff148cb62010-06-28 12:29:20 +0000529 Value *Ret = EmitStrCpy(CI->getArgOperand(0), CI->getArgOperand(1), B, TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000530 TLI, Name.substr(2, 6));
Nuno Lopes28ad8632012-08-01 16:58:51 +0000531 if (!Ret)
Nuno Lopes918067d2012-08-01 17:13:28 +0000532 return false;
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000533 replaceCall(Ret);
534 return true;
535 }
536 return false;
537 }
538
Eric Christopher71988f12010-04-07 23:00:07 +0000539 if (Name == "__strncpy_chk" || Name == "__stpncpy_chk") {
Eric Christopher67a71b52010-04-12 04:48:00 +0000540 // Check if this has the right signature.
541 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
542 FT->getParamType(0) != FT->getParamType(1) ||
543 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
544 !FT->getParamType(2)->isIntegerTy() ||
545 FT->getParamType(3) != TD->getIntPtrType(Context))
Eric Christopher2a7cb9d2010-04-13 16:41:29 +0000546 return false;
Gabor Greiff148cb62010-06-28 12:29:20 +0000547
Gabor Greifa6aac4c2010-07-16 09:38:02 +0000548 if (isFoldable(3, 2, false)) {
Gabor Greiff148cb62010-06-28 12:29:20 +0000549 Value *Ret = EmitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
Nuno Lopes51004df2012-07-25 16:46:31 +0000550 CI->getArgOperand(2), B, TD, TLI,
551 Name.substr(2, 7));
Nuno Lopes28ad8632012-08-01 16:58:51 +0000552 if (!Ret)
Nuno Lopes918067d2012-08-01 17:13:28 +0000553 return false;
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000554 replaceCall(Ret);
555 return true;
556 }
557 return false;
558 }
559
560 if (Name == "__strcat_chk") {
561 return false;
562 }
563
564 if (Name == "__strncat_chk") {
565 return false;
566 }
567
568 return false;
569}