blob: 7aac79fa124718957935f778eddebd5ba9db22fb [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 Wendlingcb3de0b2012-10-15 04:46:55 +000044 AWI[0] = AttributeWithIndex::get(M->getContext(), 1, Attributes::NoCapture);
Bill Wendling11d00422012-10-10 06:13:42 +000045 Attributes::AttrVal AVs[2] = { Attributes::ReadOnly, Attributes::NoUnwind };
Bill Wendlingcb3de0b2012-10-15 04:46:55 +000046 AWI[1] = AttributeWithIndex::get(M->getContext(), ~0u,
47 ArrayRef<Attributes::AttrVal>(AVs, 2));
Eric Christopherb6174e32010-03-05 22:25:30 +000048
49 LLVMContext &Context = B.GetInsertBlock()->getContext();
Chris Lattnerd509d0b2012-05-28 01:47:44 +000050 Constant *StrLen = M->getOrInsertFunction("strlen", AttrListPtr::get(AWI),
Eric Christopherb6174e32010-03-05 22:25:30 +000051 TD->getIntPtrType(Context),
52 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();
70 AttributeWithIndex AWI[2];
Bill Wendlingcb3de0b2012-10-15 04:46:55 +000071 AWI[0] = AttributeWithIndex::get(M->getContext(), 1, Attributes::NoCapture);
Bill Wendling11d00422012-10-10 06:13:42 +000072 Attributes::AttrVal AVs[2] = { Attributes::ReadOnly, Attributes::NoUnwind };
Bill Wendlingcb3de0b2012-10-15 04:46:55 +000073 AWI[1] = AttributeWithIndex::get(M->getContext(), ~0u,
74 ArrayRef<Attributes::AttrVal>(AVs, 2));
Nuno Lopesa5368352012-07-25 17:18:59 +000075
76 LLVMContext &Context = B.GetInsertBlock()->getContext();
77 Constant *StrNLen = M->getOrInsertFunction("strnlen", AttrListPtr::get(AWI),
78 TD->getIntPtrType(Context),
79 B.getInt8PtrTy(),
80 TD->getIntPtrType(Context),
81 NULL);
82 CallInst *CI = B.CreateCall2(StrNLen, CastToCStr(Ptr, B), MaxLen, "strnlen");
83 if (const Function *F = dyn_cast<Function>(StrNLen->stripPointerCasts()))
84 CI->setCallingConv(F->getCallingConv());
85
86 return CI;
87}
88
Eric Christopherb6174e32010-03-05 22:25:30 +000089/// EmitStrChr - Emit a call to the strchr function to the builder, for the
90/// specified pointer and character. Ptr is required to be some pointer type,
91/// and the return value has 'i8*' type.
92Value *llvm::EmitStrChr(Value *Ptr, char C, IRBuilder<> &B,
Micah Villmow3574eca2012-10-08 16:38:25 +000093 const DataLayout *TD, const TargetLibraryInfo *TLI) {
Nuno Lopes51004df2012-07-25 16:46:31 +000094 if (!TLI->has(LibFunc::strchr))
95 return 0;
96
Eric Christopherb6174e32010-03-05 22:25:30 +000097 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling11d00422012-10-10 06:13:42 +000098 Attributes::AttrVal AVs[2] = { Attributes::ReadOnly, Attributes::NoUnwind };
Eric Christopherb6174e32010-03-05 22:25:30 +000099 AttributeWithIndex AWI =
Bill Wendlingcb3de0b2012-10-15 04:46:55 +0000100 AttributeWithIndex::get(M->getContext(), ~0u,
101 ArrayRef<Attributes::AttrVal>(AVs, 2));
Eric Christopherb6174e32010-03-05 22:25:30 +0000102
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000103 Type *I8Ptr = B.getInt8PtrTy();
104 Type *I32Ty = B.getInt32Ty();
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000105 Constant *StrChr = M->getOrInsertFunction("strchr", AttrListPtr::get(AWI),
Eric Christopherb6174e32010-03-05 22:25:30 +0000106 I8Ptr, I8Ptr, I32Ty, NULL);
107 CallInst *CI = B.CreateCall2(StrChr, CastToCStr(Ptr, B),
108 ConstantInt::get(I32Ty, C), "strchr");
109 if (const Function *F = dyn_cast<Function>(StrChr->stripPointerCasts()))
110 CI->setCallingConv(F->getCallingConv());
111 return CI;
112}
113
Benjamin Kramer386e9182010-06-15 21:34:25 +0000114/// EmitStrNCmp - Emit a call to the strncmp function to the builder.
115Value *llvm::EmitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len,
Micah Villmow3574eca2012-10-08 16:38:25 +0000116 IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000117 const TargetLibraryInfo *TLI) {
118 if (!TLI->has(LibFunc::strncmp))
119 return 0;
120
Benjamin Kramer386e9182010-06-15 21:34:25 +0000121 Module *M = B.GetInsertBlock()->getParent()->getParent();
122 AttributeWithIndex AWI[3];
Bill Wendlingcb3de0b2012-10-15 04:46:55 +0000123 AWI[0] = AttributeWithIndex::get(M->getContext(), 1, Attributes::NoCapture);
124 AWI[1] = AttributeWithIndex::get(M->getContext(), 2, Attributes::NoCapture);
Bill Wendling11d00422012-10-10 06:13:42 +0000125 Attributes::AttrVal AVs[2] = { Attributes::ReadOnly, Attributes::NoUnwind };
Bill Wendlingcb3de0b2012-10-15 04:46:55 +0000126 AWI[2] = AttributeWithIndex::get(M->getContext(), ~0u,
127 ArrayRef<Attributes::AttrVal>(AVs, 2));
Benjamin Kramer386e9182010-06-15 21:34:25 +0000128
129 LLVMContext &Context = B.GetInsertBlock()->getContext();
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000130 Value *StrNCmp = M->getOrInsertFunction("strncmp", AttrListPtr::get(AWI),
Benjamin Kramer386e9182010-06-15 21:34:25 +0000131 B.getInt32Ty(),
132 B.getInt8PtrTy(),
133 B.getInt8PtrTy(),
134 TD->getIntPtrType(Context), NULL);
135 CallInst *CI = B.CreateCall3(StrNCmp, CastToCStr(Ptr1, B),
136 CastToCStr(Ptr2, B), Len, "strncmp");
137
138 if (const Function *F = dyn_cast<Function>(StrNCmp->stripPointerCasts()))
139 CI->setCallingConv(F->getCallingConv());
140
141 return CI;
142}
143
Eric Christopherb6174e32010-03-05 22:25:30 +0000144/// EmitStrCpy - Emit a call to the strcpy function to the builder, for the
145/// specified pointer arguments.
146Value *llvm::EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
Micah Villmow3574eca2012-10-08 16:38:25 +0000147 const DataLayout *TD, const TargetLibraryInfo *TLI,
Nuno Lopes51004df2012-07-25 16:46:31 +0000148 StringRef Name) {
149 if (!TLI->has(LibFunc::strcpy))
150 return 0;
151
Eric Christopherb6174e32010-03-05 22:25:30 +0000152 Module *M = B.GetInsertBlock()->getParent()->getParent();
153 AttributeWithIndex AWI[2];
Bill Wendlingcb3de0b2012-10-15 04:46:55 +0000154 AWI[0] = AttributeWithIndex::get(M->getContext(), 2, Attributes::NoCapture);
155 AWI[1] = AttributeWithIndex::get(M->getContext(), ~0u, Attributes::NoUnwind);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000156 Type *I8Ptr = B.getInt8PtrTy();
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000157 Value *StrCpy = M->getOrInsertFunction(Name, AttrListPtr::get(AWI),
Eric Christopherb6174e32010-03-05 22:25:30 +0000158 I8Ptr, I8Ptr, I8Ptr, NULL);
159 CallInst *CI = B.CreateCall2(StrCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
Benjamin Kramer7fa30b82010-03-11 20:45:13 +0000160 Name);
Eric Christopherb6174e32010-03-05 22:25:30 +0000161 if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts()))
162 CI->setCallingConv(F->getCallingConv());
163 return CI;
164}
165
Eric Christopherb0722af2010-03-11 17:45:38 +0000166/// EmitStrNCpy - Emit a call to the strncpy function to the builder, for the
Eric Christopherbd973762010-03-11 01:25:07 +0000167/// specified pointer arguments.
168Value *llvm::EmitStrNCpy(Value *Dst, Value *Src, Value *Len,
Micah Villmow3574eca2012-10-08 16:38:25 +0000169 IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000170 const TargetLibraryInfo *TLI, StringRef Name) {
171 if (!TLI->has(LibFunc::strncpy))
172 return 0;
173
Eric Christopherbd973762010-03-11 01:25:07 +0000174 Module *M = B.GetInsertBlock()->getParent()->getParent();
175 AttributeWithIndex AWI[2];
Bill Wendlingcb3de0b2012-10-15 04:46:55 +0000176 AWI[0] = AttributeWithIndex::get(M->getContext(), 2, Attributes::NoCapture);
177 AWI[1] = AttributeWithIndex::get(M->getContext(), ~0u, Attributes::NoUnwind);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000178 Type *I8Ptr = B.getInt8PtrTy();
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000179 Value *StrNCpy = M->getOrInsertFunction(Name, AttrListPtr::get(AWI),
Eric Christopher71988f12010-04-07 23:00:07 +0000180 I8Ptr, I8Ptr, I8Ptr,
181 Len->getType(), NULL);
Eric Christopherbd973762010-03-11 01:25:07 +0000182 CallInst *CI = B.CreateCall3(StrNCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
183 Len, "strncpy");
184 if (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts()))
185 CI->setCallingConv(F->getCallingConv());
186 return CI;
187}
188
Evan Cheng0289b412010-03-23 15:48:04 +0000189/// EmitMemCpyChk - Emit a call to the __memcpy_chk function to the builder.
190/// This expects that the Len and ObjSize have type 'intptr_t' and Dst/Src
191/// are pointers.
192Value *llvm::EmitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
Micah Villmow3574eca2012-10-08 16:38:25 +0000193 IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000194 const TargetLibraryInfo *TLI) {
195 if (!TLI->has(LibFunc::memcpy_chk))
196 return 0;
197
Evan Cheng0289b412010-03-23 15:48:04 +0000198 Module *M = B.GetInsertBlock()->getParent()->getParent();
199 AttributeWithIndex AWI;
Bill Wendlingcb3de0b2012-10-15 04:46:55 +0000200 AWI = AttributeWithIndex::get(M->getContext(), ~0u, Attributes::NoUnwind);
Evan Cheng0289b412010-03-23 15:48:04 +0000201 LLVMContext &Context = B.GetInsertBlock()->getContext();
202 Value *MemCpy = M->getOrInsertFunction("__memcpy_chk",
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000203 AttrListPtr::get(AWI),
Evan Cheng0289b412010-03-23 15:48:04 +0000204 B.getInt8PtrTy(),
205 B.getInt8PtrTy(),
206 B.getInt8PtrTy(),
207 TD->getIntPtrType(Context),
208 TD->getIntPtrType(Context), NULL);
209 Dst = CastToCStr(Dst, B);
210 Src = CastToCStr(Src, B);
211 CallInst *CI = B.CreateCall4(MemCpy, Dst, Src, Len, ObjSize);
212 if (const Function *F = dyn_cast<Function>(MemCpy->stripPointerCasts()))
213 CI->setCallingConv(F->getCallingConv());
214 return CI;
215}
216
Eric Christopherb6174e32010-03-05 22:25:30 +0000217/// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
218/// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
219Value *llvm::EmitMemChr(Value *Ptr, Value *Val,
Micah Villmow3574eca2012-10-08 16:38:25 +0000220 Value *Len, IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000221 const TargetLibraryInfo *TLI) {
222 if (!TLI->has(LibFunc::memchr))
223 return 0;
224
Eric Christopherb6174e32010-03-05 22:25:30 +0000225 Module *M = B.GetInsertBlock()->getParent()->getParent();
226 AttributeWithIndex AWI;
Bill Wendling11d00422012-10-10 06:13:42 +0000227 Attributes::AttrVal AVs[2] = { Attributes::ReadOnly, Attributes::NoUnwind };
Bill Wendlingcb3de0b2012-10-15 04:46:55 +0000228 AWI = AttributeWithIndex::get(M->getContext(), ~0u,
229 ArrayRef<Attributes::AttrVal>(AVs, 2));
Eric Christopherb6174e32010-03-05 22:25:30 +0000230 LLVMContext &Context = B.GetInsertBlock()->getContext();
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000231 Value *MemChr = M->getOrInsertFunction("memchr", AttrListPtr::get(AWI),
Eric Christopherb6174e32010-03-05 22:25:30 +0000232 B.getInt8PtrTy(),
233 B.getInt8PtrTy(),
234 B.getInt32Ty(),
235 TD->getIntPtrType(Context),
236 NULL);
237 CallInst *CI = B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr");
238
239 if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts()))
240 CI->setCallingConv(F->getCallingConv());
241
242 return CI;
243}
244
245/// EmitMemCmp - Emit a call to the memcmp function.
246Value *llvm::EmitMemCmp(Value *Ptr1, Value *Ptr2,
Micah Villmow3574eca2012-10-08 16:38:25 +0000247 Value *Len, IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000248 const TargetLibraryInfo *TLI) {
249 if (!TLI->has(LibFunc::memcmp))
250 return 0;
251
Eric Christopherb6174e32010-03-05 22:25:30 +0000252 Module *M = B.GetInsertBlock()->getParent()->getParent();
253 AttributeWithIndex AWI[3];
Bill Wendlingcb3de0b2012-10-15 04:46:55 +0000254 AWI[0] = AttributeWithIndex::get(M->getContext(), 1, Attributes::NoCapture);
255 AWI[1] = AttributeWithIndex::get(M->getContext(), 2, Attributes::NoCapture);
Bill Wendling11d00422012-10-10 06:13:42 +0000256 Attributes::AttrVal AVs[2] = { Attributes::ReadOnly, Attributes::NoUnwind };
Bill Wendlingcb3de0b2012-10-15 04:46:55 +0000257 AWI[2] = AttributeWithIndex::get(M->getContext(), ~0u,
258 ArrayRef<Attributes::AttrVal>(AVs, 2));
Eric Christopherb6174e32010-03-05 22:25:30 +0000259
260 LLVMContext &Context = B.GetInsertBlock()->getContext();
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000261 Value *MemCmp = M->getOrInsertFunction("memcmp", AttrListPtr::get(AWI),
Eric Christopherb6174e32010-03-05 22:25:30 +0000262 B.getInt32Ty(),
263 B.getInt8PtrTy(),
264 B.getInt8PtrTy(),
265 TD->getIntPtrType(Context), NULL);
266 CallInst *CI = B.CreateCall3(MemCmp, CastToCStr(Ptr1, B), CastToCStr(Ptr2, B),
267 Len, "memcmp");
268
269 if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts()))
270 CI->setCallingConv(F->getCallingConv());
271
272 return CI;
273}
274
Eric Christopherb6174e32010-03-05 22:25:30 +0000275/// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
276/// 'floor'). This function is known to take a single of type matching 'Op' and
277/// returns one value with the same type. If 'Op' is a long double, 'l' is
278/// added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
Benjamin Kramerb5ccb252011-11-15 19:12:09 +0000279Value *llvm::EmitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B,
280 const AttrListPtr &Attrs) {
281 SmallString<20> NameBuffer;
Eric Christopherb6174e32010-03-05 22:25:30 +0000282 if (!Op->getType()->isDoubleTy()) {
283 // If we need to add a suffix, copy into NameBuffer.
Benjamin Kramerb5ccb252011-11-15 19:12:09 +0000284 NameBuffer += Name;
Eric Christopherb6174e32010-03-05 22:25:30 +0000285 if (Op->getType()->isFloatTy())
Benjamin Kramerb5ccb252011-11-15 19:12:09 +0000286 NameBuffer += 'f'; // floorf
Eric Christopherb6174e32010-03-05 22:25:30 +0000287 else
Benjamin Kramerb5ccb252011-11-15 19:12:09 +0000288 NameBuffer += 'l'; // floorl
Eric Christopherb6174e32010-03-05 22:25:30 +0000289 Name = NameBuffer;
290 }
291
292 Module *M = B.GetInsertBlock()->getParent()->getParent();
293 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
294 Op->getType(), NULL);
295 CallInst *CI = B.CreateCall(Callee, Op, Name);
296 CI->setAttributes(Attrs);
297 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
298 CI->setCallingConv(F->getCallingConv());
299
300 return CI;
301}
302
303/// EmitPutChar - Emit a call to the putchar function. This assumes that Char
304/// is an integer.
Micah Villmow3574eca2012-10-08 16:38:25 +0000305Value *llvm::EmitPutChar(Value *Char, IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000306 const TargetLibraryInfo *TLI) {
307 if (!TLI->has(LibFunc::putchar))
308 return 0;
309
Eric Christopherb6174e32010-03-05 22:25:30 +0000310 Module *M = B.GetInsertBlock()->getParent()->getParent();
311 Value *PutChar = M->getOrInsertFunction("putchar", B.getInt32Ty(),
312 B.getInt32Ty(), NULL);
313 CallInst *CI = B.CreateCall(PutChar,
314 B.CreateIntCast(Char,
315 B.getInt32Ty(),
316 /*isSigned*/true,
317 "chari"),
318 "putchar");
319
320 if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts()))
321 CI->setCallingConv(F->getCallingConv());
322 return CI;
323}
324
325/// EmitPutS - Emit a call to the puts function. This assumes that Str is
326/// some pointer.
Micah Villmow3574eca2012-10-08 16:38:25 +0000327Value *llvm::EmitPutS(Value *Str, IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000328 const TargetLibraryInfo *TLI) {
329 if (!TLI->has(LibFunc::puts))
330 return 0;
331
Eric Christopherb6174e32010-03-05 22:25:30 +0000332 Module *M = B.GetInsertBlock()->getParent()->getParent();
333 AttributeWithIndex AWI[2];
Bill Wendlingcb3de0b2012-10-15 04:46:55 +0000334 AWI[0] = AttributeWithIndex::get(M->getContext(), 1, Attributes::NoCapture);
335 AWI[1] = AttributeWithIndex::get(M->getContext(), ~0u, Attributes::NoUnwind);
Eric Christopherb6174e32010-03-05 22:25:30 +0000336
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000337 Value *PutS = M->getOrInsertFunction("puts", AttrListPtr::get(AWI),
Eric Christopherb6174e32010-03-05 22:25:30 +0000338 B.getInt32Ty(),
339 B.getInt8PtrTy(),
340 NULL);
341 CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts");
342 if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
343 CI->setCallingConv(F->getCallingConv());
Nuno Lopes51004df2012-07-25 16:46:31 +0000344 return CI;
Eric Christopherb6174e32010-03-05 22:25:30 +0000345}
346
347/// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
348/// an integer and File is a pointer to FILE.
Nuno Lopes51004df2012-07-25 16:46:31 +0000349Value *llvm::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B,
Micah Villmow3574eca2012-10-08 16:38:25 +0000350 const DataLayout *TD, const TargetLibraryInfo *TLI) {
Nuno Lopes51004df2012-07-25 16:46:31 +0000351 if (!TLI->has(LibFunc::fputc))
352 return 0;
353
Eric Christopherb6174e32010-03-05 22:25:30 +0000354 Module *M = B.GetInsertBlock()->getParent()->getParent();
355 AttributeWithIndex AWI[2];
Bill Wendlingcb3de0b2012-10-15 04:46:55 +0000356 AWI[0] = AttributeWithIndex::get(M->getContext(), 2, Attributes::NoCapture);
357 AWI[1] = AttributeWithIndex::get(M->getContext(), ~0u, Attributes::NoUnwind);
Eric Christopherb6174e32010-03-05 22:25:30 +0000358 Constant *F;
359 if (File->getType()->isPointerTy())
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000360 F = M->getOrInsertFunction("fputc", AttrListPtr::get(AWI),
Eric Christopherb6174e32010-03-05 22:25:30 +0000361 B.getInt32Ty(),
362 B.getInt32Ty(), File->getType(),
363 NULL);
364 else
365 F = M->getOrInsertFunction("fputc",
366 B.getInt32Ty(),
367 B.getInt32Ty(),
368 File->getType(), NULL);
369 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
370 "chari");
371 CallInst *CI = B.CreateCall2(F, Char, File, "fputc");
372
373 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
374 CI->setCallingConv(Fn->getCallingConv());
Nuno Lopes51004df2012-07-25 16:46:31 +0000375 return CI;
Eric Christopherb6174e32010-03-05 22:25:30 +0000376}
377
378/// EmitFPutS - Emit a call to the puts function. Str is required to be a
379/// pointer and File is a pointer to FILE.
Nuno Lopes51004df2012-07-25 16:46:31 +0000380Value *llvm::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B,
Micah Villmow3574eca2012-10-08 16:38:25 +0000381 const DataLayout *TD, const TargetLibraryInfo *TLI) {
Nuno Lopes51004df2012-07-25 16:46:31 +0000382 if (!TLI->has(LibFunc::fputs))
383 return 0;
384
Eric Christopherb6174e32010-03-05 22:25:30 +0000385 Module *M = B.GetInsertBlock()->getParent()->getParent();
386 AttributeWithIndex AWI[3];
Bill Wendlingcb3de0b2012-10-15 04:46:55 +0000387 AWI[0] = AttributeWithIndex::get(M->getContext(), 1, Attributes::NoCapture);
388 AWI[1] = AttributeWithIndex::get(M->getContext(), 2, Attributes::NoCapture);
389 AWI[2] = AttributeWithIndex::get(M->getContext(), ~0u, Attributes::NoUnwind);
Eli Friedman9d434db2011-11-17 01:27:36 +0000390 StringRef FPutsName = TLI->getName(LibFunc::fputs);
Eric Christopherb6174e32010-03-05 22:25:30 +0000391 Constant *F;
392 if (File->getType()->isPointerTy())
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000393 F = M->getOrInsertFunction(FPutsName, AttrListPtr::get(AWI),
Eric Christopherb6174e32010-03-05 22:25:30 +0000394 B.getInt32Ty(),
395 B.getInt8PtrTy(),
396 File->getType(), NULL);
397 else
Eli Friedman9d434db2011-11-17 01:27:36 +0000398 F = M->getOrInsertFunction(FPutsName, B.getInt32Ty(),
Eric Christopherb6174e32010-03-05 22:25:30 +0000399 B.getInt8PtrTy(),
400 File->getType(), NULL);
401 CallInst *CI = B.CreateCall2(F, CastToCStr(Str, B), File, "fputs");
402
403 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
404 CI->setCallingConv(Fn->getCallingConv());
Nuno Lopes51004df2012-07-25 16:46:31 +0000405 return CI;
Eric Christopherb6174e32010-03-05 22:25:30 +0000406}
407
408/// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
409/// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
Nuno Lopes51004df2012-07-25 16:46:31 +0000410Value *llvm::EmitFWrite(Value *Ptr, Value *Size, Value *File,
Micah Villmow3574eca2012-10-08 16:38:25 +0000411 IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000412 const TargetLibraryInfo *TLI) {
413 if (!TLI->has(LibFunc::fwrite))
414 return 0;
415
Eric Christopherb6174e32010-03-05 22:25:30 +0000416 Module *M = B.GetInsertBlock()->getParent()->getParent();
417 AttributeWithIndex AWI[3];
Bill Wendlingcb3de0b2012-10-15 04:46:55 +0000418 AWI[0] = AttributeWithIndex::get(M->getContext(), 1, Attributes::NoCapture);
419 AWI[1] = AttributeWithIndex::get(M->getContext(), 4, Attributes::NoCapture);
420 AWI[2] = AttributeWithIndex::get(M->getContext(), ~0u, Attributes::NoUnwind);
Eric Christopherb6174e32010-03-05 22:25:30 +0000421 LLVMContext &Context = B.GetInsertBlock()->getContext();
Eli Friedman9d434db2011-11-17 01:27:36 +0000422 StringRef FWriteName = TLI->getName(LibFunc::fwrite);
Eric Christopherb6174e32010-03-05 22:25:30 +0000423 Constant *F;
424 if (File->getType()->isPointerTy())
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000425 F = M->getOrInsertFunction(FWriteName, AttrListPtr::get(AWI),
Eric Christopherb6174e32010-03-05 22:25:30 +0000426 TD->getIntPtrType(Context),
427 B.getInt8PtrTy(),
428 TD->getIntPtrType(Context),
429 TD->getIntPtrType(Context),
430 File->getType(), NULL);
431 else
Eli Friedman9d434db2011-11-17 01:27:36 +0000432 F = M->getOrInsertFunction(FWriteName, TD->getIntPtrType(Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000433 B.getInt8PtrTy(),
434 TD->getIntPtrType(Context),
435 TD->getIntPtrType(Context),
436 File->getType(), NULL);
437 CallInst *CI = B.CreateCall4(F, CastToCStr(Ptr, B), Size,
438 ConstantInt::get(TD->getIntPtrType(Context), 1), File);
439
440 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
441 CI->setCallingConv(Fn->getCallingConv());
Nuno Lopes51004df2012-07-25 16:46:31 +0000442 return CI;
Eric Christopherb6174e32010-03-05 22:25:30 +0000443}
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000444
Benjamin Kramera30b1812010-03-12 20:41:29 +0000445SimplifyFortifiedLibCalls::~SimplifyFortifiedLibCalls() { }
446
Micah Villmow3574eca2012-10-08 16:38:25 +0000447bool SimplifyFortifiedLibCalls::fold(CallInst *CI, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000448 const TargetLibraryInfo *TLI) {
Micah Villmow3574eca2012-10-08 16:38:25 +0000449 // We really need DataLayout for later.
Eric Christopher67a71b52010-04-12 04:48:00 +0000450 if (!TD) return false;
451
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000452 this->CI = CI;
Eric Christopher67a71b52010-04-12 04:48:00 +0000453 Function *Callee = CI->getCalledFunction();
454 StringRef Name = Callee->getName();
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000455 FunctionType *FT = Callee->getFunctionType();
Eric Christopher67a71b52010-04-12 04:48:00 +0000456 LLVMContext &Context = CI->getParent()->getContext();
Eli Friedman1a24bf02011-05-27 01:00:36 +0000457 IRBuilder<> B(CI);
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000458
459 if (Name == "__memcpy_chk") {
Eric Christopher67a71b52010-04-12 04:48:00 +0000460 // Check if this has the right signature.
461 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
462 !FT->getParamType(0)->isPointerTy() ||
463 !FT->getParamType(1)->isPointerTy() ||
464 FT->getParamType(2) != TD->getIntPtrType(Context) ||
465 FT->getParamType(3) != TD->getIntPtrType(Context))
466 return false;
Gabor Greiff148cb62010-06-28 12:29:20 +0000467
Gabor Greifa6aac4c2010-07-16 09:38:02 +0000468 if (isFoldable(3, 2, false)) {
Benjamin Kramerdef548f2010-12-27 00:25:32 +0000469 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
470 CI->getArgOperand(2), 1);
Gabor Greiff148cb62010-06-28 12:29:20 +0000471 replaceCall(CI->getArgOperand(0));
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000472 return true;
473 }
474 return false;
475 }
476
477 // Should be similar to memcpy.
478 if (Name == "__mempcpy_chk") {
479 return false;
480 }
481
482 if (Name == "__memmove_chk") {
Eric Christopher67a71b52010-04-12 04:48:00 +0000483 // Check if this has the right signature.
484 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
485 !FT->getParamType(0)->isPointerTy() ||
486 !FT->getParamType(1)->isPointerTy() ||
487 FT->getParamType(2) != TD->getIntPtrType(Context) ||
488 FT->getParamType(3) != TD->getIntPtrType(Context))
489 return false;
Gabor Greiff148cb62010-06-28 12:29:20 +0000490
Gabor Greifa6aac4c2010-07-16 09:38:02 +0000491 if (isFoldable(3, 2, false)) {
Benjamin Kramerdef548f2010-12-27 00:25:32 +0000492 B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
493 CI->getArgOperand(2), 1);
Gabor Greiff148cb62010-06-28 12:29:20 +0000494 replaceCall(CI->getArgOperand(0));
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000495 return true;
496 }
497 return false;
498 }
499
500 if (Name == "__memset_chk") {
Eric Christopher67a71b52010-04-12 04:48:00 +0000501 // Check if this has the right signature.
502 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
503 !FT->getParamType(0)->isPointerTy() ||
504 !FT->getParamType(1)->isIntegerTy() ||
505 FT->getParamType(2) != TD->getIntPtrType(Context) ||
506 FT->getParamType(3) != TD->getIntPtrType(Context))
507 return false;
Gabor Greiff148cb62010-06-28 12:29:20 +0000508
Gabor Greifa6aac4c2010-07-16 09:38:02 +0000509 if (isFoldable(3, 2, false)) {
Gabor Greiff148cb62010-06-28 12:29:20 +0000510 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(),
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000511 false);
Benjamin Kramerdef548f2010-12-27 00:25:32 +0000512 B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
Gabor Greiff1ba6512010-06-25 07:58:41 +0000513 replaceCall(CI->getArgOperand(0));
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000514 return true;
515 }
516 return false;
517 }
518
519 if (Name == "__strcpy_chk" || Name == "__stpcpy_chk") {
Eric Christopher67a71b52010-04-12 04:48:00 +0000520 // Check if this has the right signature.
521 if (FT->getNumParams() != 3 ||
522 FT->getReturnType() != FT->getParamType(0) ||
523 FT->getParamType(0) != FT->getParamType(1) ||
524 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
525 FT->getParamType(2) != TD->getIntPtrType(Context))
526 return 0;
527
528
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000529 // If a) we don't have any length information, or b) we know this will
530 // fit then just lower to a plain st[rp]cpy. Otherwise we'll keep our
531 // st[rp]cpy_chk call which may fail at runtime if the size is too long.
532 // TODO: It might be nice to get a maximum length out of the possible
533 // string lengths for varying.
Gabor Greifa6aac4c2010-07-16 09:38:02 +0000534 if (isFoldable(2, 1, true)) {
Gabor Greiff148cb62010-06-28 12:29:20 +0000535 Value *Ret = EmitStrCpy(CI->getArgOperand(0), CI->getArgOperand(1), B, TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000536 TLI, Name.substr(2, 6));
Nuno Lopes28ad8632012-08-01 16:58:51 +0000537 if (!Ret)
Nuno Lopes918067d2012-08-01 17:13:28 +0000538 return false;
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000539 replaceCall(Ret);
540 return true;
541 }
542 return false;
543 }
544
Eric Christopher71988f12010-04-07 23:00:07 +0000545 if (Name == "__strncpy_chk" || Name == "__stpncpy_chk") {
Eric Christopher67a71b52010-04-12 04:48:00 +0000546 // Check if this has the right signature.
547 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
548 FT->getParamType(0) != FT->getParamType(1) ||
549 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
550 !FT->getParamType(2)->isIntegerTy() ||
551 FT->getParamType(3) != TD->getIntPtrType(Context))
Eric Christopher2a7cb9d2010-04-13 16:41:29 +0000552 return false;
Gabor Greiff148cb62010-06-28 12:29:20 +0000553
Gabor Greifa6aac4c2010-07-16 09:38:02 +0000554 if (isFoldable(3, 2, false)) {
Gabor Greiff148cb62010-06-28 12:29:20 +0000555 Value *Ret = EmitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
Nuno Lopes51004df2012-07-25 16:46:31 +0000556 CI->getArgOperand(2), B, TD, TLI,
557 Name.substr(2, 7));
Nuno Lopes28ad8632012-08-01 16:58:51 +0000558 if (!Ret)
Nuno Lopes918067d2012-08-01 17:13:28 +0000559 return false;
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000560 replaceCall(Ret);
561 return true;
562 }
563 return false;
564 }
565
566 if (Name == "__strcat_chk") {
567 return false;
568 }
569
570 if (Name == "__strncat_chk") {
571 return false;
572 }
573
574 return false;
575}