blob: bd28f106544cda48df29a64542e4fcdbc0bf44c7 [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 Wendling07aae2e2012-10-15 07:29:08 +000046 AWI[1] = AttributeWithIndex::get(M->getContext(), AttrListPtr::FunctionIndex,
Bill Wendlingcb3de0b2012-10-15 04:46:55 +000047 ArrayRef<Attributes::AttrVal>(AVs, 2));
Eric Christopherb6174e32010-03-05 22:25:30 +000048
Chris Lattnerd509d0b2012-05-28 01:47:44 +000049 Constant *StrLen = M->getOrInsertFunction("strlen", AttrListPtr::get(AWI),
Micah Villmowaa76e9e2012-10-24 15:52:52 +000050 TD->getIntPtrType(Ptr->getType()),
Eric Christopherb6174e32010-03-05 22:25:30 +000051 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 Wendlingcb3de0b2012-10-15 04:46:55 +000070 AWI[0] = AttributeWithIndex::get(M->getContext(), 1, Attributes::NoCapture);
Bill Wendling11d00422012-10-10 06:13:42 +000071 Attributes::AttrVal AVs[2] = { Attributes::ReadOnly, Attributes::NoUnwind };
Bill Wendling07aae2e2012-10-15 07:29:08 +000072 AWI[1] = AttributeWithIndex::get(M->getContext(), AttrListPtr::FunctionIndex,
Bill Wendlingcb3de0b2012-10-15 04:46:55 +000073 ArrayRef<Attributes::AttrVal>(AVs, 2));
Nuno Lopesa5368352012-07-25 17:18:59 +000074
Nuno Lopesa5368352012-07-25 17:18:59 +000075 Constant *StrNLen = M->getOrInsertFunction("strnlen", AttrListPtr::get(AWI),
Micah Villmowaa76e9e2012-10-24 15:52:52 +000076 TD->getIntPtrType(Ptr->getType()),
Nuno Lopesa5368352012-07-25 17:18:59 +000077 B.getInt8PtrTy(),
Micah Villmowaa76e9e2012-10-24 15:52:52 +000078 TD->getIntPtrType(Ptr->getType()),
Nuno Lopesa5368352012-07-25 17:18:59 +000079 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 Wendling07aae2e2012-10-15 07:29:08 +000098 AttributeWithIndex::get(M->getContext(), AttrListPtr::FunctionIndex,
Bill Wendlingcb3de0b2012-10-15 04:46:55 +000099 ArrayRef<Attributes::AttrVal>(AVs, 2));
Eric Christopherb6174e32010-03-05 22:25:30 +0000100
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000101 Type *I8Ptr = B.getInt8PtrTy();
102 Type *I32Ty = B.getInt32Ty();
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000103 Constant *StrChr = M->getOrInsertFunction("strchr", AttrListPtr::get(AWI),
Eric Christopherb6174e32010-03-05 22:25:30 +0000104 I8Ptr, I8Ptr, I32Ty, NULL);
105 CallInst *CI = B.CreateCall2(StrChr, CastToCStr(Ptr, B),
106 ConstantInt::get(I32Ty, C), "strchr");
107 if (const Function *F = dyn_cast<Function>(StrChr->stripPointerCasts()))
108 CI->setCallingConv(F->getCallingConv());
109 return CI;
110}
111
Benjamin Kramer386e9182010-06-15 21:34:25 +0000112/// EmitStrNCmp - Emit a call to the strncmp function to the builder.
113Value *llvm::EmitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len,
Micah Villmow3574eca2012-10-08 16:38:25 +0000114 IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000115 const TargetLibraryInfo *TLI) {
116 if (!TLI->has(LibFunc::strncmp))
117 return 0;
118
Benjamin Kramer386e9182010-06-15 21:34:25 +0000119 Module *M = B.GetInsertBlock()->getParent()->getParent();
120 AttributeWithIndex AWI[3];
Bill Wendlingcb3de0b2012-10-15 04:46:55 +0000121 AWI[0] = AttributeWithIndex::get(M->getContext(), 1, Attributes::NoCapture);
122 AWI[1] = AttributeWithIndex::get(M->getContext(), 2, Attributes::NoCapture);
Bill Wendling11d00422012-10-10 06:13:42 +0000123 Attributes::AttrVal AVs[2] = { Attributes::ReadOnly, Attributes::NoUnwind };
Bill Wendling07aae2e2012-10-15 07:29:08 +0000124 AWI[2] = AttributeWithIndex::get(M->getContext(), AttrListPtr::FunctionIndex,
Bill Wendlingcb3de0b2012-10-15 04:46:55 +0000125 ArrayRef<Attributes::AttrVal>(AVs, 2));
Benjamin Kramer386e9182010-06-15 21:34:25 +0000126
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000127 Value *StrNCmp = M->getOrInsertFunction("strncmp", AttrListPtr::get(AWI),
Benjamin Kramer386e9182010-06-15 21:34:25 +0000128 B.getInt32Ty(),
129 B.getInt8PtrTy(),
130 B.getInt8PtrTy(),
Micah Villmowaa76e9e2012-10-24 15:52:52 +0000131 TD->getIntPtrType(Ptr1->getType()),
132 NULL);
Benjamin Kramer386e9182010-06-15 21:34:25 +0000133 CallInst *CI = B.CreateCall3(StrNCmp, CastToCStr(Ptr1, B),
134 CastToCStr(Ptr2, B), Len, "strncmp");
135
136 if (const Function *F = dyn_cast<Function>(StrNCmp->stripPointerCasts()))
137 CI->setCallingConv(F->getCallingConv());
138
139 return CI;
140}
141
Eric Christopherb6174e32010-03-05 22:25:30 +0000142/// EmitStrCpy - Emit a call to the strcpy function to the builder, for the
143/// specified pointer arguments.
144Value *llvm::EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
Micah Villmow3574eca2012-10-08 16:38:25 +0000145 const DataLayout *TD, const TargetLibraryInfo *TLI,
Nuno Lopes51004df2012-07-25 16:46:31 +0000146 StringRef Name) {
147 if (!TLI->has(LibFunc::strcpy))
148 return 0;
149
Eric Christopherb6174e32010-03-05 22:25:30 +0000150 Module *M = B.GetInsertBlock()->getParent()->getParent();
151 AttributeWithIndex AWI[2];
Bill Wendlingcb3de0b2012-10-15 04:46:55 +0000152 AWI[0] = AttributeWithIndex::get(M->getContext(), 2, Attributes::NoCapture);
Bill Wendling07aae2e2012-10-15 07:29:08 +0000153 AWI[1] = AttributeWithIndex::get(M->getContext(), AttrListPtr::FunctionIndex,
154 Attributes::NoUnwind);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000155 Type *I8Ptr = B.getInt8PtrTy();
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000156 Value *StrCpy = M->getOrInsertFunction(Name, AttrListPtr::get(AWI),
Eric Christopherb6174e32010-03-05 22:25:30 +0000157 I8Ptr, I8Ptr, I8Ptr, NULL);
158 CallInst *CI = B.CreateCall2(StrCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
Benjamin Kramer7fa30b82010-03-11 20:45:13 +0000159 Name);
Eric Christopherb6174e32010-03-05 22:25:30 +0000160 if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts()))
161 CI->setCallingConv(F->getCallingConv());
162 return CI;
163}
164
Eric Christopherb0722af2010-03-11 17:45:38 +0000165/// EmitStrNCpy - Emit a call to the strncpy function to the builder, for the
Eric Christopherbd973762010-03-11 01:25:07 +0000166/// specified pointer arguments.
167Value *llvm::EmitStrNCpy(Value *Dst, Value *Src, Value *Len,
Micah Villmow3574eca2012-10-08 16:38:25 +0000168 IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000169 const TargetLibraryInfo *TLI, StringRef Name) {
170 if (!TLI->has(LibFunc::strncpy))
171 return 0;
172
Eric Christopherbd973762010-03-11 01:25:07 +0000173 Module *M = B.GetInsertBlock()->getParent()->getParent();
174 AttributeWithIndex AWI[2];
Bill Wendlingcb3de0b2012-10-15 04:46:55 +0000175 AWI[0] = AttributeWithIndex::get(M->getContext(), 2, Attributes::NoCapture);
Bill Wendling07aae2e2012-10-15 07:29:08 +0000176 AWI[1] = AttributeWithIndex::get(M->getContext(), AttrListPtr::FunctionIndex,
177 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 Wendling07aae2e2012-10-15 07:29:08 +0000200 AWI = AttributeWithIndex::get(M->getContext(), AttrListPtr::FunctionIndex,
201 Attributes::NoUnwind);
Evan Cheng0289b412010-03-23 15:48:04 +0000202 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(),
Micah Villmowaa76e9e2012-10-24 15:52:52 +0000207 TD->getIntPtrType(Dst->getType()),
208 TD->getIntPtrType(Src->getType()),
209 NULL);
Evan Cheng0289b412010-03-23 15:48:04 +0000210 Dst = CastToCStr(Dst, B);
211 Src = CastToCStr(Src, B);
212 CallInst *CI = B.CreateCall4(MemCpy, Dst, Src, Len, ObjSize);
213 if (const Function *F = dyn_cast<Function>(MemCpy->stripPointerCasts()))
214 CI->setCallingConv(F->getCallingConv());
215 return CI;
216}
217
Eric Christopherb6174e32010-03-05 22:25:30 +0000218/// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
219/// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
220Value *llvm::EmitMemChr(Value *Ptr, Value *Val,
Micah Villmow3574eca2012-10-08 16:38:25 +0000221 Value *Len, IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000222 const TargetLibraryInfo *TLI) {
223 if (!TLI->has(LibFunc::memchr))
224 return 0;
225
Eric Christopherb6174e32010-03-05 22:25:30 +0000226 Module *M = B.GetInsertBlock()->getParent()->getParent();
227 AttributeWithIndex AWI;
Bill Wendling11d00422012-10-10 06:13:42 +0000228 Attributes::AttrVal AVs[2] = { Attributes::ReadOnly, Attributes::NoUnwind };
Bill Wendling07aae2e2012-10-15 07:29:08 +0000229 AWI = AttributeWithIndex::get(M->getContext(), AttrListPtr::FunctionIndex,
Bill Wendlingcb3de0b2012-10-15 04:46:55 +0000230 ArrayRef<Attributes::AttrVal>(AVs, 2));
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(),
Micah Villmowaa76e9e2012-10-24 15:52:52 +0000235 TD->getIntPtrType(Ptr->getType()),
Eric Christopherb6174e32010-03-05 22:25:30 +0000236 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 Wendling07aae2e2012-10-15 07:29:08 +0000257 AWI[2] = AttributeWithIndex::get(M->getContext(), AttrListPtr::FunctionIndex,
Bill Wendlingcb3de0b2012-10-15 04:46:55 +0000258 ArrayRef<Attributes::AttrVal>(AVs, 2));
Eric Christopherb6174e32010-03-05 22:25:30 +0000259
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000260 Value *MemCmp = M->getOrInsertFunction("memcmp", AttrListPtr::get(AWI),
Eric Christopherb6174e32010-03-05 22:25:30 +0000261 B.getInt32Ty(),
262 B.getInt8PtrTy(),
263 B.getInt8PtrTy(),
Micah Villmowaa76e9e2012-10-24 15:52:52 +0000264 TD->getIntPtrType(Ptr1->getType()),
265 NULL);
Eric Christopherb6174e32010-03-05 22:25:30 +0000266 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);
Bill Wendling07aae2e2012-10-15 07:29:08 +0000335 AWI[1] = AttributeWithIndex::get(M->getContext(), AttrListPtr::FunctionIndex,
336 Attributes::NoUnwind);
Eric Christopherb6174e32010-03-05 22:25:30 +0000337
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000338 Value *PutS = M->getOrInsertFunction("puts", AttrListPtr::get(AWI),
Eric Christopherb6174e32010-03-05 22:25:30 +0000339 B.getInt32Ty(),
340 B.getInt8PtrTy(),
341 NULL);
342 CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts");
343 if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
344 CI->setCallingConv(F->getCallingConv());
Nuno Lopes51004df2012-07-25 16:46:31 +0000345 return CI;
Eric Christopherb6174e32010-03-05 22:25:30 +0000346}
347
348/// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
349/// an integer and File is a pointer to FILE.
Nuno Lopes51004df2012-07-25 16:46:31 +0000350Value *llvm::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B,
Micah Villmow3574eca2012-10-08 16:38:25 +0000351 const DataLayout *TD, const TargetLibraryInfo *TLI) {
Nuno Lopes51004df2012-07-25 16:46:31 +0000352 if (!TLI->has(LibFunc::fputc))
353 return 0;
354
Eric Christopherb6174e32010-03-05 22:25:30 +0000355 Module *M = B.GetInsertBlock()->getParent()->getParent();
356 AttributeWithIndex AWI[2];
Bill Wendlingcb3de0b2012-10-15 04:46:55 +0000357 AWI[0] = AttributeWithIndex::get(M->getContext(), 2, Attributes::NoCapture);
Bill Wendling07aae2e2012-10-15 07:29:08 +0000358 AWI[1] = AttributeWithIndex::get(M->getContext(), AttrListPtr::FunctionIndex,
359 Attributes::NoUnwind);
Eric Christopherb6174e32010-03-05 22:25:30 +0000360 Constant *F;
361 if (File->getType()->isPointerTy())
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000362 F = M->getOrInsertFunction("fputc", AttrListPtr::get(AWI),
Eric Christopherb6174e32010-03-05 22:25:30 +0000363 B.getInt32Ty(),
364 B.getInt32Ty(), File->getType(),
365 NULL);
366 else
367 F = M->getOrInsertFunction("fputc",
368 B.getInt32Ty(),
369 B.getInt32Ty(),
370 File->getType(), NULL);
371 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
372 "chari");
373 CallInst *CI = B.CreateCall2(F, Char, File, "fputc");
374
375 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
376 CI->setCallingConv(Fn->getCallingConv());
Nuno Lopes51004df2012-07-25 16:46:31 +0000377 return CI;
Eric Christopherb6174e32010-03-05 22:25:30 +0000378}
379
380/// EmitFPutS - Emit a call to the puts function. Str is required to be a
381/// pointer and File is a pointer to FILE.
Nuno Lopes51004df2012-07-25 16:46:31 +0000382Value *llvm::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B,
Micah Villmow3574eca2012-10-08 16:38:25 +0000383 const DataLayout *TD, const TargetLibraryInfo *TLI) {
Nuno Lopes51004df2012-07-25 16:46:31 +0000384 if (!TLI->has(LibFunc::fputs))
385 return 0;
386
Eric Christopherb6174e32010-03-05 22:25:30 +0000387 Module *M = B.GetInsertBlock()->getParent()->getParent();
388 AttributeWithIndex AWI[3];
Bill Wendlingcb3de0b2012-10-15 04:46:55 +0000389 AWI[0] = AttributeWithIndex::get(M->getContext(), 1, Attributes::NoCapture);
390 AWI[1] = AttributeWithIndex::get(M->getContext(), 2, Attributes::NoCapture);
Bill Wendling07aae2e2012-10-15 07:29:08 +0000391 AWI[2] = AttributeWithIndex::get(M->getContext(), AttrListPtr::FunctionIndex,
392 Attributes::NoUnwind);
Eli Friedman9d434db2011-11-17 01:27:36 +0000393 StringRef FPutsName = TLI->getName(LibFunc::fputs);
Eric Christopherb6174e32010-03-05 22:25:30 +0000394 Constant *F;
395 if (File->getType()->isPointerTy())
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000396 F = M->getOrInsertFunction(FPutsName, AttrListPtr::get(AWI),
Eric Christopherb6174e32010-03-05 22:25:30 +0000397 B.getInt32Ty(),
398 B.getInt8PtrTy(),
399 File->getType(), NULL);
400 else
Eli Friedman9d434db2011-11-17 01:27:36 +0000401 F = M->getOrInsertFunction(FPutsName, B.getInt32Ty(),
Eric Christopherb6174e32010-03-05 22:25:30 +0000402 B.getInt8PtrTy(),
403 File->getType(), NULL);
404 CallInst *CI = B.CreateCall2(F, CastToCStr(Str, B), File, "fputs");
405
406 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
407 CI->setCallingConv(Fn->getCallingConv());
Nuno Lopes51004df2012-07-25 16:46:31 +0000408 return CI;
Eric Christopherb6174e32010-03-05 22:25:30 +0000409}
410
411/// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
412/// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
Nuno Lopes51004df2012-07-25 16:46:31 +0000413Value *llvm::EmitFWrite(Value *Ptr, Value *Size, Value *File,
Micah Villmow3574eca2012-10-08 16:38:25 +0000414 IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000415 const TargetLibraryInfo *TLI) {
416 if (!TLI->has(LibFunc::fwrite))
417 return 0;
418
Eric Christopherb6174e32010-03-05 22:25:30 +0000419 Module *M = B.GetInsertBlock()->getParent()->getParent();
420 AttributeWithIndex AWI[3];
Bill Wendlingcb3de0b2012-10-15 04:46:55 +0000421 AWI[0] = AttributeWithIndex::get(M->getContext(), 1, Attributes::NoCapture);
422 AWI[1] = AttributeWithIndex::get(M->getContext(), 4, Attributes::NoCapture);
Bill Wendling07aae2e2012-10-15 07:29:08 +0000423 AWI[2] = AttributeWithIndex::get(M->getContext(), AttrListPtr::FunctionIndex,
424 Attributes::NoUnwind);
Eli Friedman9d434db2011-11-17 01:27:36 +0000425 StringRef FWriteName = TLI->getName(LibFunc::fwrite);
Eric Christopherb6174e32010-03-05 22:25:30 +0000426 Constant *F;
Micah Villmowaa76e9e2012-10-24 15:52:52 +0000427 Type *PtrTy = Ptr->getType();
Eric Christopherb6174e32010-03-05 22:25:30 +0000428 if (File->getType()->isPointerTy())
Chris Lattnerd509d0b2012-05-28 01:47:44 +0000429 F = M->getOrInsertFunction(FWriteName, AttrListPtr::get(AWI),
Micah Villmowaa76e9e2012-10-24 15:52:52 +0000430 TD->getIntPtrType(PtrTy),
Eric Christopherb6174e32010-03-05 22:25:30 +0000431 B.getInt8PtrTy(),
Micah Villmowaa76e9e2012-10-24 15:52:52 +0000432 TD->getIntPtrType(PtrTy),
433 TD->getIntPtrType(PtrTy),
Eric Christopherb6174e32010-03-05 22:25:30 +0000434 File->getType(), NULL);
435 else
Micah Villmowaa76e9e2012-10-24 15:52:52 +0000436 F = M->getOrInsertFunction(FWriteName, TD->getIntPtrType(PtrTy),
Eric Christopherb6174e32010-03-05 22:25:30 +0000437 B.getInt8PtrTy(),
Micah Villmowaa76e9e2012-10-24 15:52:52 +0000438 TD->getIntPtrType(PtrTy),
439 TD->getIntPtrType(PtrTy),
Eric Christopherb6174e32010-03-05 22:25:30 +0000440 File->getType(), NULL);
441 CallInst *CI = B.CreateCall4(F, CastToCStr(Ptr, B), Size,
Micah Villmowaa76e9e2012-10-24 15:52:52 +0000442 ConstantInt::get(TD->getIntPtrType(PtrTy), 1), File);
Eric Christopherb6174e32010-03-05 22:25:30 +0000443
444 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
445 CI->setCallingConv(Fn->getCallingConv());
Nuno Lopes51004df2012-07-25 16:46:31 +0000446 return CI;
Eric Christopherb6174e32010-03-05 22:25:30 +0000447}
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000448
Benjamin Kramera30b1812010-03-12 20:41:29 +0000449SimplifyFortifiedLibCalls::~SimplifyFortifiedLibCalls() { }
450
Micah Villmow3574eca2012-10-08 16:38:25 +0000451bool SimplifyFortifiedLibCalls::fold(CallInst *CI, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000452 const TargetLibraryInfo *TLI) {
Micah Villmow3574eca2012-10-08 16:38:25 +0000453 // We really need DataLayout for later.
Eric Christopher67a71b52010-04-12 04:48:00 +0000454 if (!TD) return false;
455
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000456 this->CI = CI;
Eric Christopher67a71b52010-04-12 04:48:00 +0000457 Function *Callee = CI->getCalledFunction();
458 StringRef Name = Callee->getName();
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000459 FunctionType *FT = Callee->getFunctionType();
Eric Christopher67a71b52010-04-12 04:48:00 +0000460 LLVMContext &Context = CI->getParent()->getContext();
Eli Friedman1a24bf02011-05-27 01:00:36 +0000461 IRBuilder<> B(CI);
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000462
463 if (Name == "__memcpy_chk") {
Micah Villmowaa76e9e2012-10-24 15:52:52 +0000464 Type *PT = FT->getParamType(0);
Eric Christopher67a71b52010-04-12 04:48:00 +0000465 // Check if this has the right signature.
466 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
467 !FT->getParamType(0)->isPointerTy() ||
468 !FT->getParamType(1)->isPointerTy() ||
Micah Villmowaa76e9e2012-10-24 15:52:52 +0000469 FT->getParamType(2) != TD->getIntPtrType(PT) ||
470 FT->getParamType(3) != TD->getIntPtrType(PT))
Eric Christopher67a71b52010-04-12 04:48:00 +0000471 return false;
Gabor Greiff148cb62010-06-28 12:29:20 +0000472
Gabor Greifa6aac4c2010-07-16 09:38:02 +0000473 if (isFoldable(3, 2, false)) {
Benjamin Kramerdef548f2010-12-27 00:25:32 +0000474 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
475 CI->getArgOperand(2), 1);
Gabor Greiff148cb62010-06-28 12:29:20 +0000476 replaceCall(CI->getArgOperand(0));
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000477 return true;
478 }
479 return false;
480 }
481
482 // Should be similar to memcpy.
483 if (Name == "__mempcpy_chk") {
484 return false;
485 }
486
487 if (Name == "__memmove_chk") {
Eric Christopher67a71b52010-04-12 04:48:00 +0000488 // Check if this has the right signature.
Micah Villmowaa76e9e2012-10-24 15:52:52 +0000489 Type *PT = FT->getParamType(0);
Eric Christopher67a71b52010-04-12 04:48:00 +0000490 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
491 !FT->getParamType(0)->isPointerTy() ||
492 !FT->getParamType(1)->isPointerTy() ||
Micah Villmowaa76e9e2012-10-24 15:52:52 +0000493 FT->getParamType(2) != TD->getIntPtrType(PT) ||
494 FT->getParamType(3) != TD->getIntPtrType(PT))
Eric Christopher67a71b52010-04-12 04:48:00 +0000495 return false;
Gabor Greiff148cb62010-06-28 12:29:20 +0000496
Gabor Greifa6aac4c2010-07-16 09:38:02 +0000497 if (isFoldable(3, 2, false)) {
Benjamin Kramerdef548f2010-12-27 00:25:32 +0000498 B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
499 CI->getArgOperand(2), 1);
Gabor Greiff148cb62010-06-28 12:29:20 +0000500 replaceCall(CI->getArgOperand(0));
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000501 return true;
502 }
503 return false;
504 }
505
506 if (Name == "__memset_chk") {
Eric Christopher67a71b52010-04-12 04:48:00 +0000507 // Check if this has the right signature.
Micah Villmowaa76e9e2012-10-24 15:52:52 +0000508 Type *PT = FT->getParamType(0);
Eric Christopher67a71b52010-04-12 04:48:00 +0000509 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
510 !FT->getParamType(0)->isPointerTy() ||
511 !FT->getParamType(1)->isIntegerTy() ||
Micah Villmowaa76e9e2012-10-24 15:52:52 +0000512 FT->getParamType(2) != TD->getIntPtrType(PT) ||
513 FT->getParamType(3) != TD->getIntPtrType(PT))
Eric Christopher67a71b52010-04-12 04:48:00 +0000514 return false;
Gabor Greiff148cb62010-06-28 12:29:20 +0000515
Gabor Greifa6aac4c2010-07-16 09:38:02 +0000516 if (isFoldable(3, 2, false)) {
Gabor Greiff148cb62010-06-28 12:29:20 +0000517 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(),
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000518 false);
Benjamin Kramerdef548f2010-12-27 00:25:32 +0000519 B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
Gabor Greiff1ba6512010-06-25 07:58:41 +0000520 replaceCall(CI->getArgOperand(0));
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000521 return true;
522 }
523 return false;
524 }
525
526 if (Name == "__strcpy_chk" || Name == "__stpcpy_chk") {
Eric Christopher67a71b52010-04-12 04:48:00 +0000527 // Check if this has the right signature.
Micah Villmowaa76e9e2012-10-24 15:52:52 +0000528 Type *PT = FT->getParamType(0);
Eric Christopher67a71b52010-04-12 04:48:00 +0000529 if (FT->getNumParams() != 3 ||
530 FT->getReturnType() != FT->getParamType(0) ||
531 FT->getParamType(0) != FT->getParamType(1) ||
532 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
Micah Villmowaa76e9e2012-10-24 15:52:52 +0000533 FT->getParamType(2) != TD->getIntPtrType(PT))
Eric Christopher67a71b52010-04-12 04:48:00 +0000534 return 0;
535
536
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000537 // If a) we don't have any length information, or b) we know this will
538 // fit then just lower to a plain st[rp]cpy. Otherwise we'll keep our
539 // st[rp]cpy_chk call which may fail at runtime if the size is too long.
540 // TODO: It might be nice to get a maximum length out of the possible
541 // string lengths for varying.
Gabor Greifa6aac4c2010-07-16 09:38:02 +0000542 if (isFoldable(2, 1, true)) {
Gabor Greiff148cb62010-06-28 12:29:20 +0000543 Value *Ret = EmitStrCpy(CI->getArgOperand(0), CI->getArgOperand(1), B, TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000544 TLI, Name.substr(2, 6));
Nuno Lopes28ad8632012-08-01 16:58:51 +0000545 if (!Ret)
Nuno Lopes918067d2012-08-01 17:13:28 +0000546 return false;
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000547 replaceCall(Ret);
548 return true;
549 }
550 return false;
551 }
552
Eric Christopher71988f12010-04-07 23:00:07 +0000553 if (Name == "__strncpy_chk" || Name == "__stpncpy_chk") {
Eric Christopher67a71b52010-04-12 04:48:00 +0000554 // Check if this has the right signature.
Micah Villmowaa76e9e2012-10-24 15:52:52 +0000555 Type *PT = FT->getParamType(0);
Eric Christopher67a71b52010-04-12 04:48:00 +0000556 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
557 FT->getParamType(0) != FT->getParamType(1) ||
558 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
559 !FT->getParamType(2)->isIntegerTy() ||
Micah Villmowaa76e9e2012-10-24 15:52:52 +0000560 FT->getParamType(3) != TD->getIntPtrType(PT))
Eric Christopher2a7cb9d2010-04-13 16:41:29 +0000561 return false;
Gabor Greiff148cb62010-06-28 12:29:20 +0000562
Gabor Greifa6aac4c2010-07-16 09:38:02 +0000563 if (isFoldable(3, 2, false)) {
Gabor Greiff148cb62010-06-28 12:29:20 +0000564 Value *Ret = EmitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
Nuno Lopes51004df2012-07-25 16:46:31 +0000565 CI->getArgOperand(2), B, TD, TLI,
566 Name.substr(2, 7));
Nuno Lopes28ad8632012-08-01 16:58:51 +0000567 if (!Ret)
Nuno Lopes918067d2012-08-01 17:13:28 +0000568 return false;
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000569 replaceCall(Ret);
570 return true;
571 }
572 return false;
573 }
574
575 if (Name == "__strcat_chk") {
576 return false;
577 }
578
579 if (Name == "__strncat_chk") {
580 return false;
581 }
582
583 return false;
584}