blob: 74b2ee10e01dee09bc3fe636ca2cd8b19929edf9 [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
Chandler Carruthece6c6b2012-11-01 08:07:29 +000049 LLVMContext &Context = B.GetInsertBlock()->getContext();
Bill Wendling0976e002012-11-20 05:09:20 +000050 Constant *StrLen = M->getOrInsertFunction("strlen",
51 AttrListPtr::get(M->getContext(),
52 AWI),
Chandler Carruthece6c6b2012-11-01 08:07:29 +000053 TD->getIntPtrType(Context),
Eric Christopherb6174e32010-03-05 22:25:30 +000054 B.getInt8PtrTy(),
55 NULL);
56 CallInst *CI = B.CreateCall(StrLen, CastToCStr(Ptr, B), "strlen");
57 if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts()))
58 CI->setCallingConv(F->getCallingConv());
59
60 return CI;
61}
62
Nuno Lopesa5368352012-07-25 17:18:59 +000063/// EmitStrNLen - Emit a call to the strnlen function to the builder, for the
64/// specified pointer. Ptr is required to be some pointer type, MaxLen must
65/// be of size_t type, and the return value has 'intptr_t' type.
66Value *llvm::EmitStrNLen(Value *Ptr, Value *MaxLen, IRBuilder<> &B,
Micah Villmow3574eca2012-10-08 16:38:25 +000067 const DataLayout *TD, const TargetLibraryInfo *TLI) {
Nuno Lopesa5368352012-07-25 17:18:59 +000068 if (!TLI->has(LibFunc::strnlen))
69 return 0;
70
71 Module *M = B.GetInsertBlock()->getParent()->getParent();
72 AttributeWithIndex AWI[2];
Bill Wendlingcb3de0b2012-10-15 04:46:55 +000073 AWI[0] = AttributeWithIndex::get(M->getContext(), 1, Attributes::NoCapture);
Bill Wendling11d00422012-10-10 06:13:42 +000074 Attributes::AttrVal AVs[2] = { Attributes::ReadOnly, Attributes::NoUnwind };
Bill Wendling07aae2e2012-10-15 07:29:08 +000075 AWI[1] = AttributeWithIndex::get(M->getContext(), AttrListPtr::FunctionIndex,
Bill Wendlingcb3de0b2012-10-15 04:46:55 +000076 ArrayRef<Attributes::AttrVal>(AVs, 2));
Nuno Lopesa5368352012-07-25 17:18:59 +000077
Chandler Carruthece6c6b2012-11-01 08:07:29 +000078 LLVMContext &Context = B.GetInsertBlock()->getContext();
Bill Wendling0976e002012-11-20 05:09:20 +000079 Constant *StrNLen = M->getOrInsertFunction("strnlen",
80 AttrListPtr::get(M->getContext(),
81 AWI),
Chandler Carruthece6c6b2012-11-01 08:07:29 +000082 TD->getIntPtrType(Context),
Nuno Lopesa5368352012-07-25 17:18:59 +000083 B.getInt8PtrTy(),
Chandler Carruthece6c6b2012-11-01 08:07:29 +000084 TD->getIntPtrType(Context),
Nuno Lopesa5368352012-07-25 17:18:59 +000085 NULL);
86 CallInst *CI = B.CreateCall2(StrNLen, CastToCStr(Ptr, B), MaxLen, "strnlen");
87 if (const Function *F = dyn_cast<Function>(StrNLen->stripPointerCasts()))
88 CI->setCallingConv(F->getCallingConv());
89
90 return CI;
91}
92
Eric Christopherb6174e32010-03-05 22:25:30 +000093/// EmitStrChr - Emit a call to the strchr function to the builder, for the
94/// specified pointer and character. Ptr is required to be some pointer type,
95/// and the return value has 'i8*' type.
96Value *llvm::EmitStrChr(Value *Ptr, char C, IRBuilder<> &B,
Micah Villmow3574eca2012-10-08 16:38:25 +000097 const DataLayout *TD, const TargetLibraryInfo *TLI) {
Nuno Lopes51004df2012-07-25 16:46:31 +000098 if (!TLI->has(LibFunc::strchr))
99 return 0;
100
Eric Christopherb6174e32010-03-05 22:25:30 +0000101 Module *M = B.GetInsertBlock()->getParent()->getParent();
Bill Wendling11d00422012-10-10 06:13:42 +0000102 Attributes::AttrVal AVs[2] = { Attributes::ReadOnly, Attributes::NoUnwind };
Eric Christopherb6174e32010-03-05 22:25:30 +0000103 AttributeWithIndex AWI =
Bill Wendling07aae2e2012-10-15 07:29:08 +0000104 AttributeWithIndex::get(M->getContext(), AttrListPtr::FunctionIndex,
Bill Wendlingcb3de0b2012-10-15 04:46:55 +0000105 ArrayRef<Attributes::AttrVal>(AVs, 2));
Eric Christopherb6174e32010-03-05 22:25:30 +0000106
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000107 Type *I8Ptr = B.getInt8PtrTy();
108 Type *I32Ty = B.getInt32Ty();
Bill Wendling0976e002012-11-20 05:09:20 +0000109 Constant *StrChr = M->getOrInsertFunction("strchr",
110 AttrListPtr::get(M->getContext(),
111 AWI),
Eric Christopherb6174e32010-03-05 22:25:30 +0000112 I8Ptr, I8Ptr, I32Ty, NULL);
113 CallInst *CI = B.CreateCall2(StrChr, CastToCStr(Ptr, B),
114 ConstantInt::get(I32Ty, C), "strchr");
115 if (const Function *F = dyn_cast<Function>(StrChr->stripPointerCasts()))
116 CI->setCallingConv(F->getCallingConv());
117 return CI;
118}
119
Benjamin Kramer386e9182010-06-15 21:34:25 +0000120/// EmitStrNCmp - Emit a call to the strncmp function to the builder.
121Value *llvm::EmitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len,
Micah Villmow3574eca2012-10-08 16:38:25 +0000122 IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000123 const TargetLibraryInfo *TLI) {
124 if (!TLI->has(LibFunc::strncmp))
125 return 0;
126
Benjamin Kramer386e9182010-06-15 21:34:25 +0000127 Module *M = B.GetInsertBlock()->getParent()->getParent();
128 AttributeWithIndex AWI[3];
Bill Wendlingcb3de0b2012-10-15 04:46:55 +0000129 AWI[0] = AttributeWithIndex::get(M->getContext(), 1, Attributes::NoCapture);
130 AWI[1] = AttributeWithIndex::get(M->getContext(), 2, Attributes::NoCapture);
Bill Wendling11d00422012-10-10 06:13:42 +0000131 Attributes::AttrVal AVs[2] = { Attributes::ReadOnly, Attributes::NoUnwind };
Bill Wendling07aae2e2012-10-15 07:29:08 +0000132 AWI[2] = AttributeWithIndex::get(M->getContext(), AttrListPtr::FunctionIndex,
Bill Wendlingcb3de0b2012-10-15 04:46:55 +0000133 ArrayRef<Attributes::AttrVal>(AVs, 2));
Benjamin Kramer386e9182010-06-15 21:34:25 +0000134
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000135 LLVMContext &Context = B.GetInsertBlock()->getContext();
Bill Wendling0976e002012-11-20 05:09:20 +0000136 Value *StrNCmp = M->getOrInsertFunction("strncmp",
137 AttrListPtr::get(M->getContext(),
138 AWI),
Benjamin Kramer386e9182010-06-15 21:34:25 +0000139 B.getInt32Ty(),
140 B.getInt8PtrTy(),
141 B.getInt8PtrTy(),
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000142 TD->getIntPtrType(Context), NULL);
Benjamin Kramer386e9182010-06-15 21:34:25 +0000143 CallInst *CI = B.CreateCall3(StrNCmp, CastToCStr(Ptr1, B),
144 CastToCStr(Ptr2, B), Len, "strncmp");
145
146 if (const Function *F = dyn_cast<Function>(StrNCmp->stripPointerCasts()))
147 CI->setCallingConv(F->getCallingConv());
148
149 return CI;
150}
151
Eric Christopherb6174e32010-03-05 22:25:30 +0000152/// EmitStrCpy - Emit a call to the strcpy function to the builder, for the
153/// specified pointer arguments.
154Value *llvm::EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
Micah Villmow3574eca2012-10-08 16:38:25 +0000155 const DataLayout *TD, const TargetLibraryInfo *TLI,
Nuno Lopes51004df2012-07-25 16:46:31 +0000156 StringRef Name) {
157 if (!TLI->has(LibFunc::strcpy))
158 return 0;
159
Eric Christopherb6174e32010-03-05 22:25:30 +0000160 Module *M = B.GetInsertBlock()->getParent()->getParent();
161 AttributeWithIndex AWI[2];
Bill Wendlingcb3de0b2012-10-15 04:46:55 +0000162 AWI[0] = AttributeWithIndex::get(M->getContext(), 2, Attributes::NoCapture);
Bill Wendling07aae2e2012-10-15 07:29:08 +0000163 AWI[1] = AttributeWithIndex::get(M->getContext(), AttrListPtr::FunctionIndex,
164 Attributes::NoUnwind);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000165 Type *I8Ptr = B.getInt8PtrTy();
Bill Wendling0976e002012-11-20 05:09:20 +0000166 Value *StrCpy = M->getOrInsertFunction(Name,
167 AttrListPtr::get(M->getContext(), AWI),
Eric Christopherb6174e32010-03-05 22:25:30 +0000168 I8Ptr, I8Ptr, I8Ptr, NULL);
169 CallInst *CI = B.CreateCall2(StrCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
Benjamin Kramer7fa30b82010-03-11 20:45:13 +0000170 Name);
Eric Christopherb6174e32010-03-05 22:25:30 +0000171 if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts()))
172 CI->setCallingConv(F->getCallingConv());
173 return CI;
174}
175
Eric Christopherb0722af2010-03-11 17:45:38 +0000176/// EmitStrNCpy - Emit a call to the strncpy function to the builder, for the
Eric Christopherbd973762010-03-11 01:25:07 +0000177/// specified pointer arguments.
178Value *llvm::EmitStrNCpy(Value *Dst, Value *Src, Value *Len,
Micah Villmow3574eca2012-10-08 16:38:25 +0000179 IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000180 const TargetLibraryInfo *TLI, StringRef Name) {
181 if (!TLI->has(LibFunc::strncpy))
182 return 0;
183
Eric Christopherbd973762010-03-11 01:25:07 +0000184 Module *M = B.GetInsertBlock()->getParent()->getParent();
185 AttributeWithIndex AWI[2];
Bill Wendlingcb3de0b2012-10-15 04:46:55 +0000186 AWI[0] = AttributeWithIndex::get(M->getContext(), 2, Attributes::NoCapture);
Bill Wendling07aae2e2012-10-15 07:29:08 +0000187 AWI[1] = AttributeWithIndex::get(M->getContext(), AttrListPtr::FunctionIndex,
188 Attributes::NoUnwind);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000189 Type *I8Ptr = B.getInt8PtrTy();
Bill Wendling0976e002012-11-20 05:09:20 +0000190 Value *StrNCpy = M->getOrInsertFunction(Name,
191 AttrListPtr::get(M->getContext(),
192 AWI),
Eric Christopher71988f12010-04-07 23:00:07 +0000193 I8Ptr, I8Ptr, I8Ptr,
194 Len->getType(), NULL);
Eric Christopherbd973762010-03-11 01:25:07 +0000195 CallInst *CI = B.CreateCall3(StrNCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
196 Len, "strncpy");
197 if (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts()))
198 CI->setCallingConv(F->getCallingConv());
199 return CI;
200}
201
Evan Cheng0289b412010-03-23 15:48:04 +0000202/// EmitMemCpyChk - Emit a call to the __memcpy_chk function to the builder.
203/// This expects that the Len and ObjSize have type 'intptr_t' and Dst/Src
204/// are pointers.
205Value *llvm::EmitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
Micah Villmow3574eca2012-10-08 16:38:25 +0000206 IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000207 const TargetLibraryInfo *TLI) {
208 if (!TLI->has(LibFunc::memcpy_chk))
209 return 0;
210
Evan Cheng0289b412010-03-23 15:48:04 +0000211 Module *M = B.GetInsertBlock()->getParent()->getParent();
212 AttributeWithIndex AWI;
Bill Wendling07aae2e2012-10-15 07:29:08 +0000213 AWI = AttributeWithIndex::get(M->getContext(), AttrListPtr::FunctionIndex,
214 Attributes::NoUnwind);
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000215 LLVMContext &Context = B.GetInsertBlock()->getContext();
Evan Cheng0289b412010-03-23 15:48:04 +0000216 Value *MemCpy = M->getOrInsertFunction("__memcpy_chk",
Bill Wendling0976e002012-11-20 05:09:20 +0000217 AttrListPtr::get(M->getContext(), AWI),
Evan Cheng0289b412010-03-23 15:48:04 +0000218 B.getInt8PtrTy(),
219 B.getInt8PtrTy(),
220 B.getInt8PtrTy(),
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000221 TD->getIntPtrType(Context),
222 TD->getIntPtrType(Context), NULL);
Evan Cheng0289b412010-03-23 15:48:04 +0000223 Dst = CastToCStr(Dst, B);
224 Src = CastToCStr(Src, B);
225 CallInst *CI = B.CreateCall4(MemCpy, Dst, Src, Len, ObjSize);
226 if (const Function *F = dyn_cast<Function>(MemCpy->stripPointerCasts()))
227 CI->setCallingConv(F->getCallingConv());
228 return CI;
229}
230
Eric Christopherb6174e32010-03-05 22:25:30 +0000231/// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
232/// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
233Value *llvm::EmitMemChr(Value *Ptr, Value *Val,
Micah Villmow3574eca2012-10-08 16:38:25 +0000234 Value *Len, IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000235 const TargetLibraryInfo *TLI) {
236 if (!TLI->has(LibFunc::memchr))
237 return 0;
238
Eric Christopherb6174e32010-03-05 22:25:30 +0000239 Module *M = B.GetInsertBlock()->getParent()->getParent();
240 AttributeWithIndex AWI;
Bill Wendling11d00422012-10-10 06:13:42 +0000241 Attributes::AttrVal AVs[2] = { Attributes::ReadOnly, Attributes::NoUnwind };
Bill Wendling07aae2e2012-10-15 07:29:08 +0000242 AWI = AttributeWithIndex::get(M->getContext(), AttrListPtr::FunctionIndex,
Bill Wendlingcb3de0b2012-10-15 04:46:55 +0000243 ArrayRef<Attributes::AttrVal>(AVs, 2));
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000244 LLVMContext &Context = B.GetInsertBlock()->getContext();
Bill Wendling0976e002012-11-20 05:09:20 +0000245 Value *MemChr = M->getOrInsertFunction("memchr",
246 AttrListPtr::get(M->getContext(), AWI),
Eric Christopherb6174e32010-03-05 22:25:30 +0000247 B.getInt8PtrTy(),
248 B.getInt8PtrTy(),
249 B.getInt32Ty(),
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000250 TD->getIntPtrType(Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000251 NULL);
252 CallInst *CI = B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr");
253
254 if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts()))
255 CI->setCallingConv(F->getCallingConv());
256
257 return CI;
258}
259
260/// EmitMemCmp - Emit a call to the memcmp function.
261Value *llvm::EmitMemCmp(Value *Ptr1, Value *Ptr2,
Micah Villmow3574eca2012-10-08 16:38:25 +0000262 Value *Len, IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000263 const TargetLibraryInfo *TLI) {
264 if (!TLI->has(LibFunc::memcmp))
265 return 0;
266
Eric Christopherb6174e32010-03-05 22:25:30 +0000267 Module *M = B.GetInsertBlock()->getParent()->getParent();
268 AttributeWithIndex AWI[3];
Bill Wendlingcb3de0b2012-10-15 04:46:55 +0000269 AWI[0] = AttributeWithIndex::get(M->getContext(), 1, Attributes::NoCapture);
270 AWI[1] = AttributeWithIndex::get(M->getContext(), 2, Attributes::NoCapture);
Bill Wendling11d00422012-10-10 06:13:42 +0000271 Attributes::AttrVal AVs[2] = { Attributes::ReadOnly, Attributes::NoUnwind };
Bill Wendling07aae2e2012-10-15 07:29:08 +0000272 AWI[2] = AttributeWithIndex::get(M->getContext(), AttrListPtr::FunctionIndex,
Bill Wendlingcb3de0b2012-10-15 04:46:55 +0000273 ArrayRef<Attributes::AttrVal>(AVs, 2));
Eric Christopherb6174e32010-03-05 22:25:30 +0000274
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000275 LLVMContext &Context = B.GetInsertBlock()->getContext();
Bill Wendling0976e002012-11-20 05:09:20 +0000276 Value *MemCmp = M->getOrInsertFunction("memcmp",
277 AttrListPtr::get(M->getContext(), AWI),
Eric Christopherb6174e32010-03-05 22:25:30 +0000278 B.getInt32Ty(),
279 B.getInt8PtrTy(),
280 B.getInt8PtrTy(),
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000281 TD->getIntPtrType(Context), NULL);
Eric Christopherb6174e32010-03-05 22:25:30 +0000282 CallInst *CI = B.CreateCall3(MemCmp, CastToCStr(Ptr1, B), CastToCStr(Ptr2, B),
283 Len, "memcmp");
284
285 if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts()))
286 CI->setCallingConv(F->getCallingConv());
287
288 return CI;
289}
290
Eric Christopherb6174e32010-03-05 22:25:30 +0000291/// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
292/// 'floor'). This function is known to take a single of type matching 'Op' and
293/// returns one value with the same type. If 'Op' is a long double, 'l' is
294/// added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
Benjamin Kramerb5ccb252011-11-15 19:12:09 +0000295Value *llvm::EmitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B,
296 const AttrListPtr &Attrs) {
297 SmallString<20> NameBuffer;
Eric Christopherb6174e32010-03-05 22:25:30 +0000298 if (!Op->getType()->isDoubleTy()) {
299 // If we need to add a suffix, copy into NameBuffer.
Benjamin Kramerb5ccb252011-11-15 19:12:09 +0000300 NameBuffer += Name;
Eric Christopherb6174e32010-03-05 22:25:30 +0000301 if (Op->getType()->isFloatTy())
Benjamin Kramerb5ccb252011-11-15 19:12:09 +0000302 NameBuffer += 'f'; // floorf
Eric Christopherb6174e32010-03-05 22:25:30 +0000303 else
Benjamin Kramerb5ccb252011-11-15 19:12:09 +0000304 NameBuffer += 'l'; // floorl
Eric Christopherb6174e32010-03-05 22:25:30 +0000305 Name = NameBuffer;
306 }
307
308 Module *M = B.GetInsertBlock()->getParent()->getParent();
309 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
310 Op->getType(), NULL);
311 CallInst *CI = B.CreateCall(Callee, Op, Name);
312 CI->setAttributes(Attrs);
313 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
314 CI->setCallingConv(F->getCallingConv());
315
316 return CI;
317}
318
319/// EmitPutChar - Emit a call to the putchar function. This assumes that Char
320/// is an integer.
Micah Villmow3574eca2012-10-08 16:38:25 +0000321Value *llvm::EmitPutChar(Value *Char, IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000322 const TargetLibraryInfo *TLI) {
323 if (!TLI->has(LibFunc::putchar))
324 return 0;
325
Eric Christopherb6174e32010-03-05 22:25:30 +0000326 Module *M = B.GetInsertBlock()->getParent()->getParent();
327 Value *PutChar = M->getOrInsertFunction("putchar", B.getInt32Ty(),
328 B.getInt32Ty(), NULL);
329 CallInst *CI = B.CreateCall(PutChar,
330 B.CreateIntCast(Char,
331 B.getInt32Ty(),
332 /*isSigned*/true,
333 "chari"),
334 "putchar");
335
336 if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts()))
337 CI->setCallingConv(F->getCallingConv());
338 return CI;
339}
340
341/// EmitPutS - Emit a call to the puts function. This assumes that Str is
342/// some pointer.
Micah Villmow3574eca2012-10-08 16:38:25 +0000343Value *llvm::EmitPutS(Value *Str, IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000344 const TargetLibraryInfo *TLI) {
345 if (!TLI->has(LibFunc::puts))
346 return 0;
347
Eric Christopherb6174e32010-03-05 22:25:30 +0000348 Module *M = B.GetInsertBlock()->getParent()->getParent();
349 AttributeWithIndex AWI[2];
Bill Wendlingcb3de0b2012-10-15 04:46:55 +0000350 AWI[0] = AttributeWithIndex::get(M->getContext(), 1, Attributes::NoCapture);
Bill Wendling07aae2e2012-10-15 07:29:08 +0000351 AWI[1] = AttributeWithIndex::get(M->getContext(), AttrListPtr::FunctionIndex,
352 Attributes::NoUnwind);
Eric Christopherb6174e32010-03-05 22:25:30 +0000353
Bill Wendling0976e002012-11-20 05:09:20 +0000354 Value *PutS = M->getOrInsertFunction("puts",
355 AttrListPtr::get(M->getContext(), AWI),
Eric Christopherb6174e32010-03-05 22:25:30 +0000356 B.getInt32Ty(),
357 B.getInt8PtrTy(),
358 NULL);
359 CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts");
360 if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
361 CI->setCallingConv(F->getCallingConv());
Nuno Lopes51004df2012-07-25 16:46:31 +0000362 return CI;
Eric Christopherb6174e32010-03-05 22:25:30 +0000363}
364
365/// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
366/// an integer and File is a pointer to FILE.
Nuno Lopes51004df2012-07-25 16:46:31 +0000367Value *llvm::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B,
Micah Villmow3574eca2012-10-08 16:38:25 +0000368 const DataLayout *TD, const TargetLibraryInfo *TLI) {
Nuno Lopes51004df2012-07-25 16:46:31 +0000369 if (!TLI->has(LibFunc::fputc))
370 return 0;
371
Eric Christopherb6174e32010-03-05 22:25:30 +0000372 Module *M = B.GetInsertBlock()->getParent()->getParent();
373 AttributeWithIndex AWI[2];
Bill Wendlingcb3de0b2012-10-15 04:46:55 +0000374 AWI[0] = AttributeWithIndex::get(M->getContext(), 2, Attributes::NoCapture);
Bill Wendling07aae2e2012-10-15 07:29:08 +0000375 AWI[1] = AttributeWithIndex::get(M->getContext(), AttrListPtr::FunctionIndex,
376 Attributes::NoUnwind);
Eric Christopherb6174e32010-03-05 22:25:30 +0000377 Constant *F;
378 if (File->getType()->isPointerTy())
Bill Wendling0976e002012-11-20 05:09:20 +0000379 F = M->getOrInsertFunction("fputc",
380 AttrListPtr::get(M->getContext(), AWI),
Eric Christopherb6174e32010-03-05 22:25:30 +0000381 B.getInt32Ty(),
382 B.getInt32Ty(), File->getType(),
383 NULL);
384 else
385 F = M->getOrInsertFunction("fputc",
386 B.getInt32Ty(),
387 B.getInt32Ty(),
388 File->getType(), NULL);
389 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
390 "chari");
391 CallInst *CI = B.CreateCall2(F, Char, File, "fputc");
392
393 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
394 CI->setCallingConv(Fn->getCallingConv());
Nuno Lopes51004df2012-07-25 16:46:31 +0000395 return CI;
Eric Christopherb6174e32010-03-05 22:25:30 +0000396}
397
398/// EmitFPutS - Emit a call to the puts function. Str is required to be a
399/// pointer and File is a pointer to FILE.
Nuno Lopes51004df2012-07-25 16:46:31 +0000400Value *llvm::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B,
Micah Villmow3574eca2012-10-08 16:38:25 +0000401 const DataLayout *TD, const TargetLibraryInfo *TLI) {
Nuno Lopes51004df2012-07-25 16:46:31 +0000402 if (!TLI->has(LibFunc::fputs))
403 return 0;
404
Eric Christopherb6174e32010-03-05 22:25:30 +0000405 Module *M = B.GetInsertBlock()->getParent()->getParent();
406 AttributeWithIndex AWI[3];
Bill Wendlingcb3de0b2012-10-15 04:46:55 +0000407 AWI[0] = AttributeWithIndex::get(M->getContext(), 1, Attributes::NoCapture);
408 AWI[1] = AttributeWithIndex::get(M->getContext(), 2, Attributes::NoCapture);
Bill Wendling07aae2e2012-10-15 07:29:08 +0000409 AWI[2] = AttributeWithIndex::get(M->getContext(), AttrListPtr::FunctionIndex,
410 Attributes::NoUnwind);
Eli Friedman9d434db2011-11-17 01:27:36 +0000411 StringRef FPutsName = TLI->getName(LibFunc::fputs);
Eric Christopherb6174e32010-03-05 22:25:30 +0000412 Constant *F;
413 if (File->getType()->isPointerTy())
Bill Wendling0976e002012-11-20 05:09:20 +0000414 F = M->getOrInsertFunction(FPutsName,
415 AttrListPtr::get(M->getContext(), AWI),
Eric Christopherb6174e32010-03-05 22:25:30 +0000416 B.getInt32Ty(),
417 B.getInt8PtrTy(),
418 File->getType(), NULL);
419 else
Eli Friedman9d434db2011-11-17 01:27:36 +0000420 F = M->getOrInsertFunction(FPutsName, B.getInt32Ty(),
Eric Christopherb6174e32010-03-05 22:25:30 +0000421 B.getInt8PtrTy(),
422 File->getType(), NULL);
423 CallInst *CI = B.CreateCall2(F, CastToCStr(Str, B), File, "fputs");
424
425 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
426 CI->setCallingConv(Fn->getCallingConv());
Nuno Lopes51004df2012-07-25 16:46:31 +0000427 return CI;
Eric Christopherb6174e32010-03-05 22:25:30 +0000428}
429
430/// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
431/// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
Nuno Lopes51004df2012-07-25 16:46:31 +0000432Value *llvm::EmitFWrite(Value *Ptr, Value *Size, Value *File,
Micah Villmow3574eca2012-10-08 16:38:25 +0000433 IRBuilder<> &B, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000434 const TargetLibraryInfo *TLI) {
435 if (!TLI->has(LibFunc::fwrite))
436 return 0;
437
Eric Christopherb6174e32010-03-05 22:25:30 +0000438 Module *M = B.GetInsertBlock()->getParent()->getParent();
439 AttributeWithIndex AWI[3];
Bill Wendlingcb3de0b2012-10-15 04:46:55 +0000440 AWI[0] = AttributeWithIndex::get(M->getContext(), 1, Attributes::NoCapture);
441 AWI[1] = AttributeWithIndex::get(M->getContext(), 4, Attributes::NoCapture);
Bill Wendling07aae2e2012-10-15 07:29:08 +0000442 AWI[2] = AttributeWithIndex::get(M->getContext(), AttrListPtr::FunctionIndex,
443 Attributes::NoUnwind);
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000444 LLVMContext &Context = B.GetInsertBlock()->getContext();
Eli Friedman9d434db2011-11-17 01:27:36 +0000445 StringRef FWriteName = TLI->getName(LibFunc::fwrite);
Eric Christopherb6174e32010-03-05 22:25:30 +0000446 Constant *F;
447 if (File->getType()->isPointerTy())
Bill Wendling0976e002012-11-20 05:09:20 +0000448 F = M->getOrInsertFunction(FWriteName,
449 AttrListPtr::get(M->getContext(), AWI),
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000450 TD->getIntPtrType(Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000451 B.getInt8PtrTy(),
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000452 TD->getIntPtrType(Context),
453 TD->getIntPtrType(Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000454 File->getType(), NULL);
455 else
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000456 F = M->getOrInsertFunction(FWriteName, TD->getIntPtrType(Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000457 B.getInt8PtrTy(),
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000458 TD->getIntPtrType(Context),
459 TD->getIntPtrType(Context),
Eric Christopherb6174e32010-03-05 22:25:30 +0000460 File->getType(), NULL);
461 CallInst *CI = B.CreateCall4(F, CastToCStr(Ptr, B), Size,
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000462 ConstantInt::get(TD->getIntPtrType(Context), 1), File);
Eric Christopherb6174e32010-03-05 22:25:30 +0000463
464 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
465 CI->setCallingConv(Fn->getCallingConv());
Nuno Lopes51004df2012-07-25 16:46:31 +0000466 return CI;
Eric Christopherb6174e32010-03-05 22:25:30 +0000467}
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000468
Benjamin Kramera30b1812010-03-12 20:41:29 +0000469SimplifyFortifiedLibCalls::~SimplifyFortifiedLibCalls() { }
470
Micah Villmow3574eca2012-10-08 16:38:25 +0000471bool SimplifyFortifiedLibCalls::fold(CallInst *CI, const DataLayout *TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000472 const TargetLibraryInfo *TLI) {
Micah Villmow3574eca2012-10-08 16:38:25 +0000473 // We really need DataLayout for later.
Eric Christopher67a71b52010-04-12 04:48:00 +0000474 if (!TD) return false;
475
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000476 this->CI = CI;
Eric Christopher67a71b52010-04-12 04:48:00 +0000477 Function *Callee = CI->getCalledFunction();
478 StringRef Name = Callee->getName();
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000479 FunctionType *FT = Callee->getFunctionType();
Eric Christopher67a71b52010-04-12 04:48:00 +0000480 LLVMContext &Context = CI->getParent()->getContext();
Eli Friedman1a24bf02011-05-27 01:00:36 +0000481 IRBuilder<> B(CI);
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000482
483 if (Name == "__memcpy_chk") {
Eric Christopher67a71b52010-04-12 04:48:00 +0000484 // Check if this has the right signature.
485 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
486 !FT->getParamType(0)->isPointerTy() ||
487 !FT->getParamType(1)->isPointerTy() ||
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000488 FT->getParamType(2) != TD->getIntPtrType(Context) ||
489 FT->getParamType(3) != TD->getIntPtrType(Context))
Eric Christopher67a71b52010-04-12 04:48:00 +0000490 return false;
Gabor Greiff148cb62010-06-28 12:29:20 +0000491
Gabor Greifa6aac4c2010-07-16 09:38:02 +0000492 if (isFoldable(3, 2, false)) {
Benjamin Kramerdef548f2010-12-27 00:25:32 +0000493 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
494 CI->getArgOperand(2), 1);
Gabor Greiff148cb62010-06-28 12:29:20 +0000495 replaceCall(CI->getArgOperand(0));
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000496 return true;
497 }
498 return false;
499 }
500
501 // Should be similar to memcpy.
502 if (Name == "__mempcpy_chk") {
503 return false;
504 }
505
506 if (Name == "__memmove_chk") {
Eric Christopher67a71b52010-04-12 04:48:00 +0000507 // Check if this has the right signature.
508 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
509 !FT->getParamType(0)->isPointerTy() ||
510 !FT->getParamType(1)->isPointerTy() ||
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000511 FT->getParamType(2) != TD->getIntPtrType(Context) ||
512 FT->getParamType(3) != TD->getIntPtrType(Context))
Eric Christopher67a71b52010-04-12 04:48:00 +0000513 return false;
Gabor Greiff148cb62010-06-28 12:29:20 +0000514
Gabor Greifa6aac4c2010-07-16 09:38:02 +0000515 if (isFoldable(3, 2, false)) {
Benjamin Kramerdef548f2010-12-27 00:25:32 +0000516 B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
517 CI->getArgOperand(2), 1);
Gabor Greiff148cb62010-06-28 12:29:20 +0000518 replaceCall(CI->getArgOperand(0));
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000519 return true;
520 }
521 return false;
522 }
523
524 if (Name == "__memset_chk") {
Eric Christopher67a71b52010-04-12 04:48:00 +0000525 // Check if this has the right signature.
526 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
527 !FT->getParamType(0)->isPointerTy() ||
528 !FT->getParamType(1)->isIntegerTy() ||
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000529 FT->getParamType(2) != TD->getIntPtrType(Context) ||
530 FT->getParamType(3) != TD->getIntPtrType(Context))
Eric Christopher67a71b52010-04-12 04:48:00 +0000531 return false;
Gabor Greiff148cb62010-06-28 12:29:20 +0000532
Gabor Greifa6aac4c2010-07-16 09:38:02 +0000533 if (isFoldable(3, 2, false)) {
Gabor Greiff148cb62010-06-28 12:29:20 +0000534 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(),
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000535 false);
Benjamin Kramerdef548f2010-12-27 00:25:32 +0000536 B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
Gabor Greiff1ba6512010-06-25 07:58:41 +0000537 replaceCall(CI->getArgOperand(0));
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000538 return true;
539 }
540 return false;
541 }
542
543 if (Name == "__strcpy_chk" || Name == "__stpcpy_chk") {
Eric Christopher67a71b52010-04-12 04:48:00 +0000544 // Check if this has the right signature.
545 if (FT->getNumParams() != 3 ||
546 FT->getReturnType() != FT->getParamType(0) ||
547 FT->getParamType(0) != FT->getParamType(1) ||
548 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000549 FT->getParamType(2) != TD->getIntPtrType(Context))
Eric Christopher67a71b52010-04-12 04:48:00 +0000550 return 0;
551
552
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000553 // If a) we don't have any length information, or b) we know this will
554 // fit then just lower to a plain st[rp]cpy. Otherwise we'll keep our
555 // st[rp]cpy_chk call which may fail at runtime if the size is too long.
556 // TODO: It might be nice to get a maximum length out of the possible
557 // string lengths for varying.
Gabor Greifa6aac4c2010-07-16 09:38:02 +0000558 if (isFoldable(2, 1, true)) {
Gabor Greiff148cb62010-06-28 12:29:20 +0000559 Value *Ret = EmitStrCpy(CI->getArgOperand(0), CI->getArgOperand(1), B, TD,
Nuno Lopes51004df2012-07-25 16:46:31 +0000560 TLI, Name.substr(2, 6));
Nuno Lopes28ad8632012-08-01 16:58:51 +0000561 if (!Ret)
Nuno Lopes918067d2012-08-01 17:13:28 +0000562 return false;
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000563 replaceCall(Ret);
564 return true;
565 }
566 return false;
567 }
568
Eric Christopher71988f12010-04-07 23:00:07 +0000569 if (Name == "__strncpy_chk" || Name == "__stpncpy_chk") {
Eric Christopher67a71b52010-04-12 04:48:00 +0000570 // Check if this has the right signature.
571 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
572 FT->getParamType(0) != FT->getParamType(1) ||
573 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
574 !FT->getParamType(2)->isIntegerTy() ||
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000575 FT->getParamType(3) != TD->getIntPtrType(Context))
Eric Christopher2a7cb9d2010-04-13 16:41:29 +0000576 return false;
Gabor Greiff148cb62010-06-28 12:29:20 +0000577
Gabor Greifa6aac4c2010-07-16 09:38:02 +0000578 if (isFoldable(3, 2, false)) {
Gabor Greiff148cb62010-06-28 12:29:20 +0000579 Value *Ret = EmitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
Nuno Lopes51004df2012-07-25 16:46:31 +0000580 CI->getArgOperand(2), B, TD, TLI,
581 Name.substr(2, 7));
Nuno Lopes28ad8632012-08-01 16:58:51 +0000582 if (!Ret)
Nuno Lopes918067d2012-08-01 17:13:28 +0000583 return false;
Benjamin Kramer0b6cb502010-03-12 09:27:41 +0000584 replaceCall(Ret);
585 return true;
586 }
587 return false;
588 }
589
590 if (Name == "__strcat_chk") {
591 return false;
592 }
593
594 if (Name == "__strncat_chk") {
595 return false;
596 }
597
598 return false;
599}