1 //===- BuildLibCalls.cpp - Utility builder for libcalls -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements some functions that will create standard C libcalls.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Transforms/Utils/BuildLibCalls.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/IR/Constants.h"
17 #include "llvm/IR/DataLayout.h"
18 #include "llvm/IR/Function.h"
19 #include "llvm/IR/IRBuilder.h"
20 #include "llvm/IR/Intrinsics.h"
21 #include "llvm/IR/LLVMContext.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/IR/Type.h"
24 #include "llvm/Target/TargetLibraryInfo.h"
28 /// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
29 Value *llvm::CastToCStr(Value *V, IRBuilder<> &B) {
30 return B.CreateBitCast(V, B.getInt8PtrTy(), "cstr");
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.
35 Value *llvm::EmitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout *TD,
36 const TargetLibraryInfo *TLI) {
37 if (!TLI->has(LibFunc::strlen))
40 Module *M = B.GetInsertBlock()->getParent()->getParent();
42 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
43 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
44 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
45 ArrayRef<Attribute::AttrKind>(AVs, 2));
47 LLVMContext &Context = B.GetInsertBlock()->getContext();
48 Constant *StrLen = M->getOrInsertFunction("strlen",
49 AttributeSet::get(M->getContext(),
51 TD->getIntPtrType(Context),
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());
61 /// 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.
64 Value *llvm::EmitStrNLen(Value *Ptr, Value *MaxLen, IRBuilder<> &B,
65 const DataLayout *TD, const TargetLibraryInfo *TLI) {
66 if (!TLI->has(LibFunc::strnlen))
69 Module *M = B.GetInsertBlock()->getParent()->getParent();
71 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
72 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
73 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
74 ArrayRef<Attribute::AttrKind>(AVs, 2));
76 LLVMContext &Context = B.GetInsertBlock()->getContext();
77 Constant *StrNLen = M->getOrInsertFunction("strnlen",
78 AttributeSet::get(M->getContext(),
80 TD->getIntPtrType(Context),
82 TD->getIntPtrType(Context),
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());
91 /// 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.
94 Value *llvm::EmitStrChr(Value *Ptr, char C, IRBuilder<> &B,
95 const DataLayout *TD, const TargetLibraryInfo *TLI) {
96 if (!TLI->has(LibFunc::strchr))
99 Module *M = B.GetInsertBlock()->getParent()->getParent();
100 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
102 AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
103 ArrayRef<Attribute::AttrKind>(AVs, 2));
105 Type *I8Ptr = B.getInt8PtrTy();
106 Type *I32Ty = B.getInt32Ty();
107 Constant *StrChr = M->getOrInsertFunction("strchr",
108 AttributeSet::get(M->getContext(),
110 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());
118 /// EmitStrNCmp - Emit a call to the strncmp function to the builder.
119 Value *llvm::EmitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len,
120 IRBuilder<> &B, const DataLayout *TD,
121 const TargetLibraryInfo *TLI) {
122 if (!TLI->has(LibFunc::strncmp))
125 Module *M = B.GetInsertBlock()->getParent()->getParent();
127 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
128 AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
129 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
130 AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
131 ArrayRef<Attribute::AttrKind>(AVs, 2));
133 LLVMContext &Context = B.GetInsertBlock()->getContext();
134 Value *StrNCmp = M->getOrInsertFunction("strncmp",
135 AttributeSet::get(M->getContext(),
140 TD->getIntPtrType(Context), NULL);
141 CallInst *CI = B.CreateCall3(StrNCmp, CastToCStr(Ptr1, B),
142 CastToCStr(Ptr2, B), Len, "strncmp");
144 if (const Function *F = dyn_cast<Function>(StrNCmp->stripPointerCasts()))
145 CI->setCallingConv(F->getCallingConv());
150 /// EmitStrCpy - Emit a call to the strcpy function to the builder, for the
151 /// specified pointer arguments.
152 Value *llvm::EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
153 const DataLayout *TD, const TargetLibraryInfo *TLI,
155 if (!TLI->has(LibFunc::strcpy))
158 Module *M = B.GetInsertBlock()->getParent()->getParent();
160 AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
161 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
162 Attribute::NoUnwind);
163 Type *I8Ptr = B.getInt8PtrTy();
164 Value *StrCpy = M->getOrInsertFunction(Name,
165 AttributeSet::get(M->getContext(), AS),
166 I8Ptr, I8Ptr, I8Ptr, NULL);
167 CallInst *CI = B.CreateCall2(StrCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
169 if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts()))
170 CI->setCallingConv(F->getCallingConv());
174 /// EmitStrNCpy - Emit a call to the strncpy function to the builder, for the
175 /// specified pointer arguments.
176 Value *llvm::EmitStrNCpy(Value *Dst, Value *Src, Value *Len,
177 IRBuilder<> &B, const DataLayout *TD,
178 const TargetLibraryInfo *TLI, StringRef Name) {
179 if (!TLI->has(LibFunc::strncpy))
182 Module *M = B.GetInsertBlock()->getParent()->getParent();
184 AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
185 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
186 Attribute::NoUnwind);
187 Type *I8Ptr = B.getInt8PtrTy();
188 Value *StrNCpy = M->getOrInsertFunction(Name,
189 AttributeSet::get(M->getContext(),
192 Len->getType(), NULL);
193 CallInst *CI = B.CreateCall3(StrNCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
195 if (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts()))
196 CI->setCallingConv(F->getCallingConv());
200 /// 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
203 Value *llvm::EmitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
204 IRBuilder<> &B, const DataLayout *TD,
205 const TargetLibraryInfo *TLI) {
206 if (!TLI->has(LibFunc::memcpy_chk))
209 Module *M = B.GetInsertBlock()->getParent()->getParent();
211 AS = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
212 Attribute::NoUnwind);
213 LLVMContext &Context = B.GetInsertBlock()->getContext();
214 Value *MemCpy = M->getOrInsertFunction("__memcpy_chk",
215 AttributeSet::get(M->getContext(), AS),
219 TD->getIntPtrType(Context),
220 TD->getIntPtrType(Context), NULL);
221 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());
229 /// 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.
231 Value *llvm::EmitMemChr(Value *Ptr, Value *Val,
232 Value *Len, IRBuilder<> &B, const DataLayout *TD,
233 const TargetLibraryInfo *TLI) {
234 if (!TLI->has(LibFunc::memchr))
237 Module *M = B.GetInsertBlock()->getParent()->getParent();
239 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
240 AS = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
241 ArrayRef<Attribute::AttrKind>(AVs, 2));
242 LLVMContext &Context = B.GetInsertBlock()->getContext();
243 Value *MemChr = M->getOrInsertFunction("memchr",
244 AttributeSet::get(M->getContext(), AS),
248 TD->getIntPtrType(Context),
250 CallInst *CI = B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr");
252 if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts()))
253 CI->setCallingConv(F->getCallingConv());
258 /// EmitMemCmp - Emit a call to the memcmp function.
259 Value *llvm::EmitMemCmp(Value *Ptr1, Value *Ptr2,
260 Value *Len, IRBuilder<> &B, const DataLayout *TD,
261 const TargetLibraryInfo *TLI) {
262 if (!TLI->has(LibFunc::memcmp))
265 Module *M = B.GetInsertBlock()->getParent()->getParent();
267 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
268 AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
269 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
270 AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
271 ArrayRef<Attribute::AttrKind>(AVs, 2));
273 LLVMContext &Context = B.GetInsertBlock()->getContext();
274 Value *MemCmp = M->getOrInsertFunction("memcmp",
275 AttributeSet::get(M->getContext(), AS),
279 TD->getIntPtrType(Context), NULL);
280 CallInst *CI = B.CreateCall3(MemCmp, CastToCStr(Ptr1, B), CastToCStr(Ptr2, B),
283 if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts()))
284 CI->setCallingConv(F->getCallingConv());
289 /// 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.
293 Value *llvm::EmitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B,
294 const AttributeSet &Attrs) {
295 SmallString<20> NameBuffer;
296 if (!Op->getType()->isDoubleTy()) {
297 // If we need to add a suffix, copy into NameBuffer.
299 if (Op->getType()->isFloatTy())
300 NameBuffer += 'f'; // floorf
302 NameBuffer += 'l'; // floorl
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());
317 /// EmitPutChar - Emit a call to the putchar function. This assumes that Char
319 Value *llvm::EmitPutChar(Value *Char, IRBuilder<> &B, const DataLayout *TD,
320 const TargetLibraryInfo *TLI) {
321 if (!TLI->has(LibFunc::putchar))
324 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,
334 if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts()))
335 CI->setCallingConv(F->getCallingConv());
339 /// EmitPutS - Emit a call to the puts function. This assumes that Str is
341 Value *llvm::EmitPutS(Value *Str, IRBuilder<> &B, const DataLayout *TD,
342 const TargetLibraryInfo *TLI) {
343 if (!TLI->has(LibFunc::puts))
346 Module *M = B.GetInsertBlock()->getParent()->getParent();
348 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
349 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
350 Attribute::NoUnwind);
352 Value *PutS = M->getOrInsertFunction("puts",
353 AttributeSet::get(M->getContext(), AS),
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());
363 /// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
364 /// an integer and File is a pointer to FILE.
365 Value *llvm::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B,
366 const DataLayout *TD, const TargetLibraryInfo *TLI) {
367 if (!TLI->has(LibFunc::fputc))
370 Module *M = B.GetInsertBlock()->getParent()->getParent();
372 AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
373 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
374 Attribute::NoUnwind);
376 if (File->getType()->isPointerTy())
377 F = M->getOrInsertFunction("fputc",
378 AttributeSet::get(M->getContext(), AS),
380 B.getInt32Ty(), File->getType(),
383 F = M->getOrInsertFunction("fputc",
386 File->getType(), NULL);
387 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
389 CallInst *CI = B.CreateCall2(F, Char, File, "fputc");
391 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
392 CI->setCallingConv(Fn->getCallingConv());
396 /// EmitFPutS - Emit a call to the puts function. Str is required to be a
397 /// pointer and File is a pointer to FILE.
398 Value *llvm::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B,
399 const DataLayout *TD, const TargetLibraryInfo *TLI) {
400 if (!TLI->has(LibFunc::fputs))
403 Module *M = B.GetInsertBlock()->getParent()->getParent();
405 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
406 AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
407 AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
408 Attribute::NoUnwind);
409 StringRef FPutsName = TLI->getName(LibFunc::fputs);
411 if (File->getType()->isPointerTy())
412 F = M->getOrInsertFunction(FPutsName,
413 AttributeSet::get(M->getContext(), AS),
416 File->getType(), NULL);
418 F = M->getOrInsertFunction(FPutsName, B.getInt32Ty(),
420 File->getType(), NULL);
421 CallInst *CI = B.CreateCall2(F, CastToCStr(Str, B), File, "fputs");
423 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
424 CI->setCallingConv(Fn->getCallingConv());
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.
430 Value *llvm::EmitFWrite(Value *Ptr, Value *Size, Value *File,
431 IRBuilder<> &B, const DataLayout *TD,
432 const TargetLibraryInfo *TLI) {
433 if (!TLI->has(LibFunc::fwrite))
436 Module *M = B.GetInsertBlock()->getParent()->getParent();
438 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
439 AS[1] = AttributeSet::get(M->getContext(), 4, Attribute::NoCapture);
440 AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
441 Attribute::NoUnwind);
442 LLVMContext &Context = B.GetInsertBlock()->getContext();
443 StringRef FWriteName = TLI->getName(LibFunc::fwrite);
445 if (File->getType()->isPointerTy())
446 F = M->getOrInsertFunction(FWriteName,
447 AttributeSet::get(M->getContext(), AS),
448 TD->getIntPtrType(Context),
450 TD->getIntPtrType(Context),
451 TD->getIntPtrType(Context),
452 File->getType(), NULL);
454 F = M->getOrInsertFunction(FWriteName, TD->getIntPtrType(Context),
456 TD->getIntPtrType(Context),
457 TD->getIntPtrType(Context),
458 File->getType(), NULL);
459 CallInst *CI = B.CreateCall4(F, CastToCStr(Ptr, B), Size,
460 ConstantInt::get(TD->getIntPtrType(Context), 1), File);
462 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
463 CI->setCallingConv(Fn->getCallingConv());
467 SimplifyFortifiedLibCalls::~SimplifyFortifiedLibCalls() { }
469 bool SimplifyFortifiedLibCalls::fold(CallInst *CI, const DataLayout *TD,
470 const TargetLibraryInfo *TLI) {
471 // We really need DataLayout for later.
472 if (!TD) return false;
475 Function *Callee = CI->getCalledFunction();
476 StringRef Name = Callee->getName();
477 FunctionType *FT = Callee->getFunctionType();
478 LLVMContext &Context = CI->getParent()->getContext();
481 if (Name == "__memcpy_chk") {
482 // 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() ||
486 FT->getParamType(2) != TD->getIntPtrType(Context) ||
487 FT->getParamType(3) != TD->getIntPtrType(Context))
490 if (isFoldable(3, 2, false)) {
491 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
492 CI->getArgOperand(2), 1);
493 replaceCall(CI->getArgOperand(0));
499 // Should be similar to memcpy.
500 if (Name == "__mempcpy_chk") {
504 if (Name == "__memmove_chk") {
505 // 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() ||
509 FT->getParamType(2) != TD->getIntPtrType(Context) ||
510 FT->getParamType(3) != TD->getIntPtrType(Context))
513 if (isFoldable(3, 2, false)) {
514 B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
515 CI->getArgOperand(2), 1);
516 replaceCall(CI->getArgOperand(0));
522 if (Name == "__memset_chk") {
523 // 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() ||
527 FT->getParamType(2) != TD->getIntPtrType(Context) ||
528 FT->getParamType(3) != TD->getIntPtrType(Context))
531 if (isFoldable(3, 2, false)) {
532 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(),
534 B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
535 replaceCall(CI->getArgOperand(0));
541 if (Name == "__strcpy_chk" || Name == "__stpcpy_chk") {
542 // 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) ||
547 FT->getParamType(2) != TD->getIntPtrType(Context))
551 // 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.
556 if (isFoldable(2, 1, true)) {
557 Value *Ret = EmitStrCpy(CI->getArgOperand(0), CI->getArgOperand(1), B, TD,
558 TLI, Name.substr(2, 6));
567 if (Name == "__strncpy_chk" || Name == "__stpncpy_chk") {
568 // 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() ||
573 FT->getParamType(3) != TD->getIntPtrType(Context))
576 if (isFoldable(3, 2, false)) {
577 Value *Ret = EmitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
578 CI->getArgOperand(2), B, TD, TLI,
588 if (Name == "__strcat_chk") {
592 if (Name == "__strncat_chk") {