blob: 51335ab363b604c0708a4190dd87c5c1a5f325a2 [file] [log] [blame]
Eric Christopherb6174e32010-03-05 22:25:30 +00001//===- BuildLibCalls.cpp - Utility builder for libcalls -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements some functions that will create standard C libcalls.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Transforms/Utils/BuildLibCalls.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000015#include "llvm/ADT/SmallString.h"
Eric Christopherb6174e32010-03-05 22:25:30 +000016#include "llvm/Constants.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "llvm/DataLayout.h"
Eric Christopherb6174e32010-03-05 22:25:30 +000018#include "llvm/Function.h"
Chandler Carruth06cb8ed2012-06-29 12:38:19 +000019#include "llvm/IRBuilder.h"
Benjamin Kramerb5ccb252011-11-15 19:12:09 +000020#include "llvm/Intrinsics.h"
21#include "llvm/LLVMContext.h"
Eric Christopherb6174e32010-03-05 22:25:30 +000022#include "llvm/Module.h"
Eli Friedman9d434db2011-11-17 01:27:36 +000023#include "llvm/Target/TargetLibraryInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000024#include "llvm/Type.h"
Eric Christopherb6174e32010-03-05 22:25:30 +000025
26using namespace llvm;
27
28/// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
29Value *llvm::CastToCStr(Value *V, IRBuilder<> &B) {
30 return B.CreateBitCast(V, B.getInt8PtrTy(), "cstr");
31}
32
33/// EmitStrLen - Emit a call to the strlen function to the builder, for the
34/// specified pointer. This always returns an integer value of size intptr_t.
Micah Villmow3574eca2012-10-08 16:38:25 +000035Value *llvm::EmitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +000036 const TargetLibraryInfo *TLI) {
37 if (!TLI->has(LibFunc::strlen))
38 return 0;
39
Eric Christopherb6174e32010-03-05 22:25:30 +000040 Module *M = B.GetInsertBlock()->getParent()->getParent();
41 AttributeWithIndex AWI[2];
Bill Wendling034b94b2012-12-19 07:18:57 +000042 AWI[0] = AttributeWithIndex::get(M->getContext(), 1, Attribute::NoCapture);
Bill Wendling629fb822012-12-22 00:37:52 +000043 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
Bill Wendling99faa3b2012-12-07 23:16:57 +000044 AWI[1] = AttributeWithIndex::get(M->getContext(), AttributeSet::FunctionIndex,
Bill Wendling629fb822012-12-22 00:37:52 +000045 ArrayRef<Attribute::AttrKind>(AVs, 2));
Eric Christopherb6174e32010-03-05 22:25:30 +000046
Chandler Carruthece6c6b2012-11-01 08:07:29 +000047 LLVMContext &Context = B.GetInsertBlock()->getContext();
Bill Wendling0976e002012-11-20 05:09:20 +000048 Constant *StrLen = M->getOrInsertFunction("strlen",
Bill Wendling99faa3b2012-12-07 23:16:57 +000049 AttributeSet::get(M->getContext(),
Bill Wendling0976e002012-11-20 05:09:20 +000050 AWI),
Chandler Carruthece6c6b2012-11-01 08:07:29 +000051 TD->getIntPtrType(Context),
Eric Christopherb6174e32010-03-05 22:25:30 +000052 B.getInt8PtrTy(),
53 NULL);
54 CallInst *CI = B.CreateCall(StrLen, CastToCStr(Ptr, B), "strlen");
55 if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts()))
56 CI->setCallingConv(F->getCallingConv());
57
58 return CI;
59}
60
Nuno Lopesa5368352012-07-25 17:18:59 +000061/// EmitStrNLen - Emit a call to the strnlen function to the builder, for the
62/// specified pointer. Ptr is required to be some pointer type, MaxLen must
63/// be of size_t type, and the return value has 'intptr_t' type.
64Value *llvm::EmitStrNLen(Value *Ptr, Value *MaxLen, IRBuilder<> &B,
Micah Villmow3574eca2012-10-08 16:38:25 +000065 const DataLayout *TD, const TargetLibraryInfo *TLI) {
Nuno Lopesa5368352012-07-25 17:18:59 +000066 if (!TLI->has(LibFunc::strnlen))
67 return 0;
68
69 Module *M = B.GetInsertBlock()->getParent()->getParent();
70 AttributeWithIndex AWI[2];
Bill Wendling034b94b2012-12-19 07:18:57 +000071 AWI[0] = AttributeWithIndex::get(M->getContext(), 1, Attribute::NoCapture);
Bill Wendling629fb822012-12-22 00:37:52 +000072 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
Bill Wendling99faa3b2012-12-07 23:16:57 +000073 AWI[1] = AttributeWithIndex::get(M->getContext(), AttributeSet::FunctionIndex,
Bill Wendling629fb822012-12-22 00:37:52 +000074 ArrayRef<Attribute::AttrKind>(AVs, 2));
Nuno Lopesa5368352012-07-25 17:18:59 +000075
Chandler Carruthece6c6b2012-11-01 08:07:29 +000076 LLVMContext &Context = B.GetInsertBlock()->getContext();
Bill Wendling0976e002012-11-20 05:09:20 +000077 Constant *StrNLen = M->getOrInsertFunction("strnlen",
Bill Wendling99faa3b2012-12-07 23:16:57 +000078 AttributeSet::get(M->getContext(),
Bill Wendling0976e002012-11-20 05:09:20 +000079 AWI),
Chandler Carruthece6c6b2012-11-01 08:07:29 +000080 TD->getIntPtrType(Context),
Nuno Lopesa5368352012-07-25 17:18:59 +000081 B.getInt8PtrTy(),
Chandler Carruthece6c6b2012-11-01 08:07:29 +000082 TD->getIntPtrType(Context),
Nuno Lopesa5368352012-07-25 17:18:59 +000083 NULL);
84 CallInst *CI = B.CreateCall2(StrNLen, CastToCStr(Ptr, B), MaxLen, "strnlen");
85 if (const Function *F = dyn_cast<Function>(StrNLen->stripPointerCasts()))
86 CI->setCallingConv(F->getCallingConv());
87
88 return CI;
89}
90
Eric Christopherb6174e32010-03-05 22:25:30 +000091/// EmitStrChr - Emit a call to the strchr function to the builder, for the
92/// specified pointer and character. Ptr is required to be some pointer type,
93/// and the return value has 'i8*' type.
94Value *llvm::EmitStrChr(Value *Ptr, char C, IRBuilder<> &B,
Micah Villmow3574eca2012-10-08 16:38:25 +000095 const DataLayout *TD, const TargetLibraryInfo *TLI) {
Nuno Lopes51004df2012-07-25 16:46:31 +000096 if (!TLI->has(LibFunc::strchr))
97 return 0;
98
Eric Christopherb6174e32010-03-05 22:25:30 +000099 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling629fb822012-12-22 00:37:52 +0000100 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
Eric Christopherb6174e32010-03-05 22:25:30 +0000101 AttributeWithIndex AWI =
Bill Wendling99faa3b2012-12-07 23:16:57 +0000102 AttributeWithIndex::get(M->getContext(), AttributeSet::FunctionIndex,
Bill Wendling629fb822012-12-22 00:37:52 +0000103 ArrayRef<Attribute::AttrKind>(AVs, 2));
Eric Christopherb6174e32010-03-05 22:25:30 +0000104
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000105 Type *I8Ptr = B.getInt8PtrTy();
106 Type *I32Ty = B.getInt32Ty();
Bill Wendling0976e002012-11-20 05:09:20 +0000107 Constant *StrChr = M->getOrInsertFunction("strchr",
Bill Wendling99faa3b2012-12-07 23:16:57 +0000108 AttributeSet::get(M->getContext(),
Bill Wendling0976e002012-11-20 05:09:20 +0000109 AWI),
Eric Christopherb6174e32010-03-05 22:25:30 +0000110 I8Ptr, I8Ptr, I32Ty, NULL);
111 CallInst *CI = B.CreateCall2(StrChr, CastToCStr(Ptr, B),
112 ConstantInt::get(I32Ty, C), "strchr");
113 if (const Function *F = dyn_cast<Function>(StrChr->stripPointerCasts()))
114 CI->setCallingConv(F->getCallingConv());
115 return CI;
116}
117
Benjamin Kramer386e9182010-06-15 21:34:25 +0000118/// EmitStrNCmp - Emit a call to the strncmp function to the builder.
119Value *llvm::EmitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len,
Micah Villmow3574eca2012-10-08 16:38:25 +0000120 IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000121 const TargetLibraryInfo *TLI) {
122 if (!TLI->has(LibFunc::strncmp))
123 return 0;
124
Benjamin Kramer386e9182010-06-15 21:34:25 +0000125 Module *M = B.GetInsertBlock()->getParent()->getParent();
126 AttributeWithIndex AWI[3];
Bill Wendling034b94b2012-12-19 07:18:57 +0000127 AWI[0] = AttributeWithIndex::get(M->getContext(), 1, Attribute::NoCapture);
128 AWI[1] = AttributeWithIndex::get(M->getContext(), 2, Attribute::NoCapture);
Bill Wendling629fb822012-12-22 00:37:52 +0000129 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
Bill Wendling99faa3b2012-12-07 23:16:57 +0000130 AWI[2] = AttributeWithIndex::get(M->getContext(), AttributeSet::FunctionIndex,
Bill Wendling629fb822012-12-22 00:37:52 +0000131 ArrayRef<Attribute::AttrKind>(AVs, 2));
Benjamin Kramer386e9182010-06-15 21:34:25 +0000132
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000133 LLVMContext &Context = B.GetInsertBlock()->getContext();
Bill Wendling0976e002012-11-20 05:09:20 +0000134 Value *StrNCmp = M->getOrInsertFunction("strncmp",
Bill Wendling99faa3b2012-12-07 23:16:57 +0000135 AttributeSet::get(M->getContext(),
Bill Wendling0976e002012-11-20 05:09:20 +0000136 AWI),
Benjamin Kramer386e9182010-06-15 21:34:25 +0000137 B.getInt32Ty(),
138 B.getInt8PtrTy(),
139 B.getInt8PtrTy(),
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000140 TD->getIntPtrType(Context), NULL);
Benjamin Kramer386e9182010-06-15 21:34:25 +0000141 CallInst *CI = B.CreateCall3(StrNCmp, CastToCStr(Ptr1, B),
142 CastToCStr(Ptr2, B), Len, "strncmp");
143
144 if (const Function *F = dyn_cast<Function>(StrNCmp->stripPointerCasts()))
145 CI->setCallingConv(F->getCallingConv());
146
147 return CI;
148}
149
Eric Christopherb6174e32010-03-05 22:25:30 +0000150/// EmitStrCpy - Emit a call to the strcpy function to the builder, for the
151/// specified pointer arguments.
152Value *llvm::EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
Micah Villmow3574eca2012-10-08 16:38:25 +0000153 const DataLayout *TD, const TargetLibraryInfo *TLI,
Nuno Lopes51004df2012-07-25 16:46:31 +0000154 StringRef Name) {
155 if (!TLI->has(LibFunc::strcpy))
156 return 0;
157
Eric Christopherb6174e32010-03-05 22:25:30 +0000158 Module *M = B.GetInsertBlock()->getParent()->getParent();
159 AttributeWithIndex AWI[2];
Bill Wendling034b94b2012-12-19 07:18:57 +0000160 AWI[0] = AttributeWithIndex::get(M->getContext(), 2, Attribute::NoCapture);
Bill Wendling99faa3b2012-12-07 23:16:57 +0000161 AWI[1] = AttributeWithIndex::get(M->getContext(), AttributeSet::FunctionIndex,
Bill Wendling034b94b2012-12-19 07:18:57 +0000162 Attribute::NoUnwind);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000163 Type *I8Ptr = B.getInt8PtrTy();
Bill Wendling0976e002012-11-20 05:09:20 +0000164 Value *StrCpy = M->getOrInsertFunction(Name,
Bill Wendling99faa3b2012-12-07 23:16:57 +0000165 AttributeSet::get(M->getContext(), AWI),
Eric Christopherb6174e32010-03-05 22:25:30 +0000166 I8Ptr, I8Ptr, I8Ptr, NULL);
167 CallInst *CI = B.CreateCall2(StrCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
Benjamin Kramer7fa30b82010-03-11 20:45:13 +0000168 Name);
Eric Christopherb6174e32010-03-05 22:25:30 +0000169 if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts()))
170 CI->setCallingConv(F->getCallingConv());
171 return CI;
172}
173
Eric Christopherb0722af2010-03-11 17:45:38 +0000174/// EmitStrNCpy - Emit a call to the strncpy function to the builder, for the
Eric Christopherbd973762010-03-11 01:25:07 +0000175/// specified pointer arguments.
176Value *llvm::EmitStrNCpy(Value *Dst, Value *Src, Value *Len,
Micah Villmow3574eca2012-10-08 16:38:25 +0000177 IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000178 const TargetLibraryInfo *TLI, StringRef Name) {
179 if (!TLI->has(LibFunc::strncpy))
180 return 0;
181
Eric Christopherbd973762010-03-11 01:25:07 +0000182 Module *M = B.GetInsertBlock()->getParent()->getParent();
183 AttributeWithIndex AWI[2];
Bill Wendling034b94b2012-12-19 07:18:57 +0000184 AWI[0] = AttributeWithIndex::get(M->getContext(), 2, Attribute::NoCapture);
Bill Wendling99faa3b2012-12-07 23:16:57 +0000185 AWI[1] = AttributeWithIndex::get(M->getContext(), AttributeSet::FunctionIndex,
Bill Wendling034b94b2012-12-19 07:18:57 +0000186 Attribute::NoUnwind);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000187 Type *I8Ptr = B.getInt8PtrTy();
Bill Wendling0976e002012-11-20 05:09:20 +0000188 Value *StrNCpy = M->getOrInsertFunction(Name,
Bill Wendling99faa3b2012-12-07 23:16:57 +0000189 AttributeSet::get(M->getContext(),
Bill Wendling0976e002012-11-20 05:09:20 +0000190 AWI),
Eric Christopher71988f12010-04-07 23:00:07 +0000191 I8Ptr, I8Ptr, I8Ptr,
192 Len->getType(), NULL);
Eric Christopherbd973762010-03-11 01:25:07 +0000193 CallInst *CI = B.CreateCall3(StrNCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
194 Len, "strncpy");
195 if (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts()))
196 CI->setCallingConv(F->getCallingConv());
197 return CI;
198}
199
Evan Cheng0289b412010-03-23 15:48:04 +0000200/// EmitMemCpyChk - Emit a call to the __memcpy_chk function to the builder.
201/// This expects that the Len and ObjSize have type 'intptr_t' and Dst/Src
202/// are pointers.
203Value *llvm::EmitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
Micah Villmow3574eca2012-10-08 16:38:25 +0000204 IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000205 const TargetLibraryInfo *TLI) {
206 if (!TLI->has(LibFunc::memcpy_chk))
207 return 0;
208
Evan Cheng0289b412010-03-23 15:48:04 +0000209 Module *M = B.GetInsertBlock()->getParent()->getParent();
210 AttributeWithIndex AWI;
Bill Wendling99faa3b2012-12-07 23:16:57 +0000211 AWI = AttributeWithIndex::get(M->getContext(), AttributeSet::FunctionIndex,
Bill Wendling034b94b2012-12-19 07:18:57 +0000212 Attribute::NoUnwind);
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000213 LLVMContext &Context = B.GetInsertBlock()->getContext();
Evan Cheng0289b412010-03-23 15:48:04 +0000214 Value *MemCpy = M->getOrInsertFunction("__memcpy_chk",
Bill Wendling99faa3b2012-12-07 23:16:57 +0000215 AttributeSet::get(M->getContext(), AWI),
Evan Cheng0289b412010-03-23 15:48:04 +0000216 B.getInt8PtrTy(),
217 B.getInt8PtrTy(),
218 B.getInt8PtrTy(),
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000219 TD->getIntPtrType(Context),
220 TD->getIntPtrType(Context), NULL);
Evan Cheng0289b412010-03-23 15:48:04 +0000221 Dst = CastToCStr(Dst, B);
222 Src = CastToCStr(Src, B);
223 CallInst *CI = B.CreateCall4(MemCpy, Dst, Src, Len, ObjSize);
224 if (const Function *F = dyn_cast<Function>(MemCpy->stripPointerCasts()))
225 CI->setCallingConv(F->getCallingConv());
226 return CI;
227}
228
Eric Christopherb6174e32010-03-05 22:25:30 +0000229/// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
230/// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
231Value *llvm::EmitMemChr(Value *Ptr, Value *Val,
Micah Villmow3574eca2012-10-08 16:38:25 +0000232 Value *Len, IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000233 const TargetLibraryInfo *TLI) {
234 if (!TLI->has(LibFunc::memchr))
235 return 0;
236
Eric Christopherb6174e32010-03-05 22:25:30 +0000237 Module *M = B.GetInsertBlock()->getParent()->getParent();
238 AttributeWithIndex AWI;
Bill Wendling629fb822012-12-22 00:37:52 +0000239 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
Bill Wendling99faa3b2012-12-07 23:16:57 +0000240 AWI = AttributeWithIndex::get(M->getContext(), AttributeSet::FunctionIndex,
Bill Wendling629fb822012-12-22 00:37:52 +0000241 ArrayRef<Attribute::AttrKind>(AVs, 2));
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000242 LLVMContext &Context = B.GetInsertBlock()->getContext();
Bill Wendling0976e002012-11-20 05:09:20 +0000243 Value *MemChr = M->getOrInsertFunction("memchr",
Bill Wendling99faa3b2012-12-07 23:16:57 +0000244 AttributeSet::get(M->getContext(), AWI),
Eric Christopherb6174e32010-03-05 22:25:30 +0000245 B.getInt8PtrTy(),
246 B.getInt8PtrTy(),
247 B.getInt32Ty(),
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000248 TD->getIntPtrType(Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000249 NULL);
250 CallInst *CI = B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr");
251
252 if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts()))
253 CI->setCallingConv(F->getCallingConv());
254
255 return CI;
256}
257
258/// EmitMemCmp - Emit a call to the memcmp function.
259Value *llvm::EmitMemCmp(Value *Ptr1, Value *Ptr2,
Micah Villmow3574eca2012-10-08 16:38:25 +0000260 Value *Len, IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000261 const TargetLibraryInfo *TLI) {
262 if (!TLI->has(LibFunc::memcmp))
263 return 0;
264
Eric Christopherb6174e32010-03-05 22:25:30 +0000265 Module *M = B.GetInsertBlock()->getParent()->getParent();
266 AttributeWithIndex AWI[3];
Bill Wendling034b94b2012-12-19 07:18:57 +0000267 AWI[0] = AttributeWithIndex::get(M->getContext(), 1, Attribute::NoCapture);
268 AWI[1] = AttributeWithIndex::get(M->getContext(), 2, Attribute::NoCapture);
Bill Wendling629fb822012-12-22 00:37:52 +0000269 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
Bill Wendling99faa3b2012-12-07 23:16:57 +0000270 AWI[2] = AttributeWithIndex::get(M->getContext(), AttributeSet::FunctionIndex,
Bill Wendling629fb822012-12-22 00:37:52 +0000271 ArrayRef<Attribute::AttrKind>(AVs, 2));
Eric Christopherb6174e32010-03-05 22:25:30 +0000272
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000273 LLVMContext &Context = B.GetInsertBlock()->getContext();
Bill Wendling0976e002012-11-20 05:09:20 +0000274 Value *MemCmp = M->getOrInsertFunction("memcmp",
Bill Wendling99faa3b2012-12-07 23:16:57 +0000275 AttributeSet::get(M->getContext(), AWI),
Eric Christopherb6174e32010-03-05 22:25:30 +0000276 B.getInt32Ty(),
277 B.getInt8PtrTy(),
278 B.getInt8PtrTy(),
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000279 TD->getIntPtrType(Context), NULL);
Eric Christopherb6174e32010-03-05 22:25:30 +0000280 CallInst *CI = B.CreateCall3(MemCmp, CastToCStr(Ptr1, B), CastToCStr(Ptr2, B),
281 Len, "memcmp");
282
283 if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts()))
284 CI->setCallingConv(F->getCallingConv());
285
286 return CI;
287}
288
Eric Christopherb6174e32010-03-05 22:25:30 +0000289/// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
290/// 'floor'). This function is known to take a single of type matching 'Op' and
291/// returns one value with the same type. If 'Op' is a long double, 'l' is
292/// added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
Benjamin Kramerb5ccb252011-11-15 19:12:09 +0000293Value *llvm::EmitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B,
Bill Wendling99faa3b2012-12-07 23:16:57 +0000294 const AttributeSet &Attrs) {
Benjamin Kramerb5ccb252011-11-15 19:12:09 +0000295 SmallString<20> NameBuffer;
Eric Christopherb6174e32010-03-05 22:25:30 +0000296 if (!Op->getType()->isDoubleTy()) {
297 // If we need to add a suffix, copy into NameBuffer.
Benjamin Kramerb5ccb252011-11-15 19:12:09 +0000298 NameBuffer += Name;
Eric Christopherb6174e32010-03-05 22:25:30 +0000299 if (Op->getType()->isFloatTy())
Benjamin Kramerb5ccb252011-11-15 19:12:09 +0000300 NameBuffer += 'f'; // floorf
Eric Christopherb6174e32010-03-05 22:25:30 +0000301 else
Benjamin Kramerb5ccb252011-11-15 19:12:09 +0000302 NameBuffer += 'l'; // floorl
Eric Christopherb6174e32010-03-05 22:25:30 +0000303 Name = NameBuffer;
304 }
305
306 Module *M = B.GetInsertBlock()->getParent()->getParent();
307 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
308 Op->getType(), NULL);
309 CallInst *CI = B.CreateCall(Callee, Op, Name);
310 CI->setAttributes(Attrs);
311 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
312 CI->setCallingConv(F->getCallingConv());
313
314 return CI;
315}
316
317/// EmitPutChar - Emit a call to the putchar function. This assumes that Char
318/// is an integer.
Micah Villmow3574eca2012-10-08 16:38:25 +0000319Value *llvm::EmitPutChar(Value *Char, IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000320 const TargetLibraryInfo *TLI) {
321 if (!TLI->has(LibFunc::putchar))
322 return 0;
323
Eric Christopherb6174e32010-03-05 22:25:30 +0000324 Module *M = B.GetInsertBlock()->getParent()->getParent();
325 Value *PutChar = M->getOrInsertFunction("putchar", B.getInt32Ty(),
326 B.getInt32Ty(), NULL);
327 CallInst *CI = B.CreateCall(PutChar,
328 B.CreateIntCast(Char,
329 B.getInt32Ty(),
330 /*isSigned*/true,
331 "chari"),
332 "putchar");
333
334 if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts()))
335 CI->setCallingConv(F->getCallingConv());
336 return CI;
337}
338
339/// EmitPutS - Emit a call to the puts function. This assumes that Str is
340/// some pointer.
Micah Villmow3574eca2012-10-08 16:38:25 +0000341Value *llvm::EmitPutS(Value *Str, IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000342 const TargetLibraryInfo *TLI) {
343 if (!TLI->has(LibFunc::puts))
344 return 0;
345
Eric Christopherb6174e32010-03-05 22:25:30 +0000346 Module *M = B.GetInsertBlock()->getParent()->getParent();
347 AttributeWithIndex AWI[2];
Bill Wendling034b94b2012-12-19 07:18:57 +0000348 AWI[0] = AttributeWithIndex::get(M->getContext(), 1, Attribute::NoCapture);
Bill Wendling99faa3b2012-12-07 23:16:57 +0000349 AWI[1] = AttributeWithIndex::get(M->getContext(), AttributeSet::FunctionIndex,
Bill Wendling034b94b2012-12-19 07:18:57 +0000350 Attribute::NoUnwind);
Eric Christopherb6174e32010-03-05 22:25:30 +0000351
Bill Wendling0976e002012-11-20 05:09:20 +0000352 Value *PutS = M->getOrInsertFunction("puts",
Bill Wendling99faa3b2012-12-07 23:16:57 +0000353 AttributeSet::get(M->getContext(), AWI),
Eric Christopherb6174e32010-03-05 22:25:30 +0000354 B.getInt32Ty(),
355 B.getInt8PtrTy(),
356 NULL);
357 CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts");
358 if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
359 CI->setCallingConv(F->getCallingConv());
Nuno Lopes51004df2012-07-25 16:46:31 +0000360 return CI;
Eric Christopherb6174e32010-03-05 22:25:30 +0000361}
362
363/// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
364/// an integer and File is a pointer to FILE.
Nuno Lopes51004df2012-07-25 16:46:31 +0000365Value *llvm::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B,
Micah Villmow3574eca2012-10-08 16:38:25 +0000366 const DataLayout *TD, const TargetLibraryInfo *TLI) {
Nuno Lopes51004df2012-07-25 16:46:31 +0000367 if (!TLI->has(LibFunc::fputc))
368 return 0;
369
Eric Christopherb6174e32010-03-05 22:25:30 +0000370 Module *M = B.GetInsertBlock()->getParent()->getParent();
371 AttributeWithIndex AWI[2];
Bill Wendling034b94b2012-12-19 07:18:57 +0000372 AWI[0] = AttributeWithIndex::get(M->getContext(), 2, Attribute::NoCapture);
Bill Wendling99faa3b2012-12-07 23:16:57 +0000373 AWI[1] = AttributeWithIndex::get(M->getContext(), AttributeSet::FunctionIndex,
Bill Wendling034b94b2012-12-19 07:18:57 +0000374 Attribute::NoUnwind);
Eric Christopherb6174e32010-03-05 22:25:30 +0000375 Constant *F;
376 if (File->getType()->isPointerTy())
Bill Wendling0976e002012-11-20 05:09:20 +0000377 F = M->getOrInsertFunction("fputc",
Bill Wendling99faa3b2012-12-07 23:16:57 +0000378 AttributeSet::get(M->getContext(), AWI),
Eric Christopherb6174e32010-03-05 22:25:30 +0000379 B.getInt32Ty(),
380 B.getInt32Ty(), File->getType(),
381 NULL);
382 else
383 F = M->getOrInsertFunction("fputc",
384 B.getInt32Ty(),
385 B.getInt32Ty(),
386 File->getType(), NULL);
387 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
388 "chari");
389 CallInst *CI = B.CreateCall2(F, Char, File, "fputc");
390
391 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
392 CI->setCallingConv(Fn->getCallingConv());
Nuno Lopes51004df2012-07-25 16:46:31 +0000393 return CI;
Eric Christopherb6174e32010-03-05 22:25:30 +0000394}
395
396/// EmitFPutS - Emit a call to the puts function. Str is required to be a
397/// pointer and File is a pointer to FILE.
Nuno Lopes51004df2012-07-25 16:46:31 +0000398Value *llvm::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B,
Micah Villmow3574eca2012-10-08 16:38:25 +0000399 const DataLayout *TD, const TargetLibraryInfo *TLI) {
Nuno Lopes51004df2012-07-25 16:46:31 +0000400 if (!TLI->has(LibFunc::fputs))
401 return 0;
402
Eric Christopherb6174e32010-03-05 22:25:30 +0000403 Module *M = B.GetInsertBlock()->getParent()->getParent();
404 AttributeWithIndex AWI[3];
Bill Wendling034b94b2012-12-19 07:18:57 +0000405 AWI[0] = AttributeWithIndex::get(M->getContext(), 1, Attribute::NoCapture);
406 AWI[1] = AttributeWithIndex::get(M->getContext(), 2, Attribute::NoCapture);
Bill Wendling99faa3b2012-12-07 23:16:57 +0000407 AWI[2] = AttributeWithIndex::get(M->getContext(), AttributeSet::FunctionIndex,
Bill Wendling034b94b2012-12-19 07:18:57 +0000408 Attribute::NoUnwind);
Eli Friedman9d434db2011-11-17 01:27:36 +0000409 StringRef FPutsName = TLI->getName(LibFunc::fputs);
Eric Christopherb6174e32010-03-05 22:25:30 +0000410 Constant *F;
411 if (File->getType()->isPointerTy())
Bill Wendling0976e002012-11-20 05:09:20 +0000412 F = M->getOrInsertFunction(FPutsName,
Bill Wendling99faa3b2012-12-07 23:16:57 +0000413 AttributeSet::get(M->getContext(), AWI),
Eric Christopherb6174e32010-03-05 22:25:30 +0000414 B.getInt32Ty(),
415 B.getInt8PtrTy(),
416 File->getType(), NULL);
417 else
Eli Friedman9d434db2011-11-17 01:27:36 +0000418 F = M->getOrInsertFunction(FPutsName, B.getInt32Ty(),
Eric Christopherb6174e32010-03-05 22:25:30 +0000419 B.getInt8PtrTy(),
420 File->getType(), NULL);
421 CallInst *CI = B.CreateCall2(F, CastToCStr(Str, B), File, "fputs");
422
423 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
424 CI->setCallingConv(Fn->getCallingConv());
Nuno Lopes51004df2012-07-25 16:46:31 +0000425 return CI;
Eric Christopherb6174e32010-03-05 22:25:30 +0000426}
427
428/// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
429/// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
Nuno Lopes51004df2012-07-25 16:46:31 +0000430Value *llvm::EmitFWrite(Value *Ptr, Value *Size, Value *File,
Micah Villmow3574eca2012-10-08 16:38:25 +0000431 IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000432 const TargetLibraryInfo *TLI) {
433 if (!TLI->has(LibFunc::fwrite))
434 return 0;
435
Eric Christopherb6174e32010-03-05 22:25:30 +0000436 Module *M = B.GetInsertBlock()->getParent()->getParent();
437 AttributeWithIndex AWI[3];
Bill Wendling034b94b2012-12-19 07:18:57 +0000438 AWI[0] = AttributeWithIndex::get(M->getContext(), 1, Attribute::NoCapture);
439 AWI[1] = AttributeWithIndex::get(M->getContext(), 4, Attribute::NoCapture);
Bill Wendling99faa3b2012-12-07 23:16:57 +0000440 AWI[2] = AttributeWithIndex::get(M->getContext(), AttributeSet::FunctionIndex,
Bill Wendling034b94b2012-12-19 07:18:57 +0000441 Attribute::NoUnwind);
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000442 LLVMContext &Context = B.GetInsertBlock()->getContext();
Eli Friedman9d434db2011-11-17 01:27:36 +0000443 StringRef FWriteName = TLI->getName(LibFunc::fwrite);
Eric Christopherb6174e32010-03-05 22:25:30 +0000444 Constant *F;
445 if (File->getType()->isPointerTy())
Bill Wendling0976e002012-11-20 05:09:20 +0000446 F = M->getOrInsertFunction(FWriteName,
Bill Wendling99faa3b2012-12-07 23:16:57 +0000447 AttributeSet::get(M->getContext(), AWI),
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000448 TD->getIntPtrType(Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000449 B.getInt8PtrTy(),
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000450 TD->getIntPtrType(Context),
451 TD->getIntPtrType(Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000452 File->getType(), NULL);
453 else
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000454 F = M->getOrInsertFunction(FWriteName, TD->getIntPtrType(Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000455 B.getInt8PtrTy(),
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000456 TD->getIntPtrType(Context),
457 TD->getIntPtrType(Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000458 File->getType(), NULL);
459 CallInst *CI = B.CreateCall4(F, CastToCStr(Ptr, B), Size,
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000460 ConstantInt::get(TD->getIntPtrType(Context), 1), File);
Eric Christopherb6174e32010-03-05 22:25:30 +0000461
462 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
463 CI->setCallingConv(Fn->getCallingConv());
Nuno Lopes51004df2012-07-25 16:46:31 +0000464 return CI;
Eric Christopherb6174e32010-03-05 22:25:30 +0000465}
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000466
Benjamin Kramera30b1812010-03-12 20:41:29 +0000467SimplifyFortifiedLibCalls::~SimplifyFortifiedLibCalls() { }
468
Micah Villmow3574eca2012-10-08 16:38:25 +0000469bool SimplifyFortifiedLibCalls::fold(CallInst *CI, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000470 const TargetLibraryInfo *TLI) {
Micah Villmow3574eca2012-10-08 16:38:25 +0000471 // We really need DataLayout for later.
Eric Christopher67a71b52010-04-12 04:48:00 +0000472 if (!TD) return false;
473
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000474 this->CI = CI;
Eric Christopher67a71b52010-04-12 04:48:00 +0000475 Function *Callee = CI->getCalledFunction();
476 StringRef Name = Callee->getName();
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000477 FunctionType *FT = Callee->getFunctionType();
Eric Christopher67a71b52010-04-12 04:48:00 +0000478 LLVMContext &Context = CI->getParent()->getContext();
Eli Friedman1a24bf02011-05-27 01:00:36 +0000479 IRBuilder<> B(CI);
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000480
481 if (Name == "__memcpy_chk") {
Eric Christopher67a71b52010-04-12 04:48:00 +0000482 // Check if this has the right signature.
483 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
484 !FT->getParamType(0)->isPointerTy() ||
485 !FT->getParamType(1)->isPointerTy() ||
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000486 FT->getParamType(2) != TD->getIntPtrType(Context) ||
487 FT->getParamType(3) != TD->getIntPtrType(Context))
Eric Christopher67a71b52010-04-12 04:48:00 +0000488 return false;
Gabor Greiff148cb62010-06-28 12:29:20 +0000489
Gabor Greifa6aac4c2010-07-16 09:38:02 +0000490 if (isFoldable(3, 2, false)) {
Benjamin Kramerdef548f2010-12-27 00:25:32 +0000491 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
492 CI->getArgOperand(2), 1);
Gabor Greiff148cb62010-06-28 12:29:20 +0000493 replaceCall(CI->getArgOperand(0));
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000494 return true;
495 }
496 return false;
497 }
498
499 // Should be similar to memcpy.
500 if (Name == "__mempcpy_chk") {
501 return false;
502 }
503
504 if (Name == "__memmove_chk") {
Eric Christopher67a71b52010-04-12 04:48:00 +0000505 // Check if this has the right signature.
506 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
507 !FT->getParamType(0)->isPointerTy() ||
508 !FT->getParamType(1)->isPointerTy() ||
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000509 FT->getParamType(2) != TD->getIntPtrType(Context) ||
510 FT->getParamType(3) != TD->getIntPtrType(Context))
Eric Christopher67a71b52010-04-12 04:48:00 +0000511 return false;
Gabor Greiff148cb62010-06-28 12:29:20 +0000512
Gabor Greifa6aac4c2010-07-16 09:38:02 +0000513 if (isFoldable(3, 2, false)) {
Benjamin Kramerdef548f2010-12-27 00:25:32 +0000514 B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
515 CI->getArgOperand(2), 1);
Gabor Greiff148cb62010-06-28 12:29:20 +0000516 replaceCall(CI->getArgOperand(0));
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000517 return true;
518 }
519 return false;
520 }
521
522 if (Name == "__memset_chk") {
Eric Christopher67a71b52010-04-12 04:48:00 +0000523 // Check if this has the right signature.
524 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
525 !FT->getParamType(0)->isPointerTy() ||
526 !FT->getParamType(1)->isIntegerTy() ||
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000527 FT->getParamType(2) != TD->getIntPtrType(Context) ||
528 FT->getParamType(3) != TD->getIntPtrType(Context))
Eric Christopher67a71b52010-04-12 04:48:00 +0000529 return false;
Gabor Greiff148cb62010-06-28 12:29:20 +0000530
Gabor Greifa6aac4c2010-07-16 09:38:02 +0000531 if (isFoldable(3, 2, false)) {
Gabor Greiff148cb62010-06-28 12:29:20 +0000532 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(),
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000533 false);
Benjamin Kramerdef548f2010-12-27 00:25:32 +0000534 B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
Gabor Greiff1ba6512010-06-25 07:58:41 +0000535 replaceCall(CI->getArgOperand(0));
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000536 return true;
537 }
538 return false;
539 }
540
541 if (Name == "__strcpy_chk" || Name == "__stpcpy_chk") {
Eric Christopher67a71b52010-04-12 04:48:00 +0000542 // Check if this has the right signature.
543 if (FT->getNumParams() != 3 ||
544 FT->getReturnType() != FT->getParamType(0) ||
545 FT->getParamType(0) != FT->getParamType(1) ||
546 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000547 FT->getParamType(2) != TD->getIntPtrType(Context))
Eric Christopher67a71b52010-04-12 04:48:00 +0000548 return 0;
549
550
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000551 // If a) we don't have any length information, or b) we know this will
552 // fit then just lower to a plain st[rp]cpy. Otherwise we'll keep our
553 // st[rp]cpy_chk call which may fail at runtime if the size is too long.
554 // TODO: It might be nice to get a maximum length out of the possible
555 // string lengths for varying.
Gabor Greifa6aac4c2010-07-16 09:38:02 +0000556 if (isFoldable(2, 1, true)) {
Gabor Greiff148cb62010-06-28 12:29:20 +0000557 Value *Ret = EmitStrCpy(CI->getArgOperand(0), CI->getArgOperand(1), B, TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000558 TLI, Name.substr(2, 6));
Nuno Lopes28ad8632012-08-01 16:58:51 +0000559 if (!Ret)
Nuno Lopes918067d2012-08-01 17:13:28 +0000560 return false;
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000561 replaceCall(Ret);
562 return true;
563 }
564 return false;
565 }
566
Eric Christopher71988f12010-04-07 23:00:07 +0000567 if (Name == "__strncpy_chk" || Name == "__stpncpy_chk") {
Eric Christopher67a71b52010-04-12 04:48:00 +0000568 // Check if this has the right signature.
569 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
570 FT->getParamType(0) != FT->getParamType(1) ||
571 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
572 !FT->getParamType(2)->isIntegerTy() ||
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000573 FT->getParamType(3) != TD->getIntPtrType(Context))
Eric Christopher2a7cb9d2010-04-13 16:41:29 +0000574 return false;
Gabor Greiff148cb62010-06-28 12:29:20 +0000575
Gabor Greifa6aac4c2010-07-16 09:38:02 +0000576 if (isFoldable(3, 2, false)) {
Gabor Greiff148cb62010-06-28 12:29:20 +0000577 Value *Ret = EmitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
Nuno Lopes51004df2012-07-25 16:46:31 +0000578 CI->getArgOperand(2), B, TD, TLI,
579 Name.substr(2, 7));
Nuno Lopes28ad8632012-08-01 16:58:51 +0000580 if (!Ret)
Nuno Lopes918067d2012-08-01 17:13:28 +0000581 return false;
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000582 replaceCall(Ret);
583 return true;
584 }
585 return false;
586 }
587
588 if (Name == "__strcat_chk") {
589 return false;
590 }
591
592 if (Name == "__strncat_chk") {
593 return false;
594 }
595
596 return false;
597}