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/Constants.h"
16 #include "llvm/Function.h"
17 #include "llvm/IRBuilder.h"
18 #include "llvm/Intrinsics.h"
19 #include "llvm/Intrinsics.h"
20 #include "llvm/LLVMContext.h"
21 #include "llvm/LLVMContext.h"
22 #include "llvm/Module.h"
23 #include "llvm/Type.h"
24 #include "llvm/ADT/SmallString.h"
25 #include "llvm/Target/TargetData.h"
26 #include "llvm/Target/TargetLibraryInfo.h"
30 /// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
31 Value *llvm::CastToCStr(Value *V, IRBuilder<> &B) {
32 return B.CreateBitCast(V, B.getInt8PtrTy(), "cstr");
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.
37 Value *llvm::EmitStrLen(Value *Ptr, IRBuilder<> &B, const TargetData *TD,
38 const TargetLibraryInfo *TLI) {
39 if (!TLI->has(LibFunc::strlen))
42 Module *M = B.GetInsertBlock()->getParent()->getParent();
43 AttributeWithIndex AWI[2];
44 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
45 AWI[1] = AttributeWithIndex::get(~0u, Attribute::ReadOnly |
48 LLVMContext &Context = B.GetInsertBlock()->getContext();
49 Constant *StrLen = M->getOrInsertFunction("strlen", AttrListPtr::get(AWI),
50 TD->getIntPtrType(Context),
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());
60 /// EmitStrChr - Emit a call to the strchr function to the builder, for the
61 /// specified pointer and character. Ptr is required to be some pointer type,
62 /// and the return value has 'i8*' type.
63 Value *llvm::EmitStrChr(Value *Ptr, char C, IRBuilder<> &B,
64 const TargetData *TD, const TargetLibraryInfo *TLI) {
65 if (!TLI->has(LibFunc::strchr))
68 Module *M = B.GetInsertBlock()->getParent()->getParent();
69 AttributeWithIndex AWI =
70 AttributeWithIndex::get(~0u, Attribute::ReadOnly | Attribute::NoUnwind);
72 Type *I8Ptr = B.getInt8PtrTy();
73 Type *I32Ty = B.getInt32Ty();
74 Constant *StrChr = M->getOrInsertFunction("strchr", AttrListPtr::get(AWI),
75 I8Ptr, I8Ptr, I32Ty, NULL);
76 CallInst *CI = B.CreateCall2(StrChr, CastToCStr(Ptr, B),
77 ConstantInt::get(I32Ty, C), "strchr");
78 if (const Function *F = dyn_cast<Function>(StrChr->stripPointerCasts()))
79 CI->setCallingConv(F->getCallingConv());
83 /// EmitStrNCmp - Emit a call to the strncmp function to the builder.
84 Value *llvm::EmitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len,
85 IRBuilder<> &B, const TargetData *TD,
86 const TargetLibraryInfo *TLI) {
87 if (!TLI->has(LibFunc::strncmp))
90 Module *M = B.GetInsertBlock()->getParent()->getParent();
91 AttributeWithIndex AWI[3];
92 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
93 AWI[1] = AttributeWithIndex::get(2, Attribute::NoCapture);
94 AWI[2] = AttributeWithIndex::get(~0u, Attribute::ReadOnly |
97 LLVMContext &Context = B.GetInsertBlock()->getContext();
98 Value *StrNCmp = M->getOrInsertFunction("strncmp", AttrListPtr::get(AWI),
102 TD->getIntPtrType(Context), NULL);
103 CallInst *CI = B.CreateCall3(StrNCmp, CastToCStr(Ptr1, B),
104 CastToCStr(Ptr2, B), Len, "strncmp");
106 if (const Function *F = dyn_cast<Function>(StrNCmp->stripPointerCasts()))
107 CI->setCallingConv(F->getCallingConv());
112 /// EmitStrCpy - Emit a call to the strcpy function to the builder, for the
113 /// specified pointer arguments.
114 Value *llvm::EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
115 const TargetData *TD, const TargetLibraryInfo *TLI,
117 if (!TLI->has(LibFunc::strcpy))
120 Module *M = B.GetInsertBlock()->getParent()->getParent();
121 AttributeWithIndex AWI[2];
122 AWI[0] = AttributeWithIndex::get(2, Attribute::NoCapture);
123 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
124 Type *I8Ptr = B.getInt8PtrTy();
125 Value *StrCpy = M->getOrInsertFunction(Name, AttrListPtr::get(AWI),
126 I8Ptr, I8Ptr, I8Ptr, NULL);
127 CallInst *CI = B.CreateCall2(StrCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
129 if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts()))
130 CI->setCallingConv(F->getCallingConv());
134 /// EmitStrNCpy - Emit a call to the strncpy function to the builder, for the
135 /// specified pointer arguments.
136 Value *llvm::EmitStrNCpy(Value *Dst, Value *Src, Value *Len,
137 IRBuilder<> &B, const TargetData *TD,
138 const TargetLibraryInfo *TLI, StringRef Name) {
139 if (!TLI->has(LibFunc::strncpy))
142 Module *M = B.GetInsertBlock()->getParent()->getParent();
143 AttributeWithIndex AWI[2];
144 AWI[0] = AttributeWithIndex::get(2, Attribute::NoCapture);
145 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
146 Type *I8Ptr = B.getInt8PtrTy();
147 Value *StrNCpy = M->getOrInsertFunction(Name, AttrListPtr::get(AWI),
149 Len->getType(), NULL);
150 CallInst *CI = B.CreateCall3(StrNCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
152 if (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts()))
153 CI->setCallingConv(F->getCallingConv());
157 /// EmitMemCpyChk - Emit a call to the __memcpy_chk function to the builder.
158 /// This expects that the Len and ObjSize have type 'intptr_t' and Dst/Src
160 Value *llvm::EmitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
161 IRBuilder<> &B, const TargetData *TD,
162 const TargetLibraryInfo *TLI) {
163 if (!TLI->has(LibFunc::memcpy_chk))
166 Module *M = B.GetInsertBlock()->getParent()->getParent();
167 AttributeWithIndex AWI;
168 AWI = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
169 LLVMContext &Context = B.GetInsertBlock()->getContext();
170 Value *MemCpy = M->getOrInsertFunction("__memcpy_chk",
171 AttrListPtr::get(AWI),
175 TD->getIntPtrType(Context),
176 TD->getIntPtrType(Context), NULL);
177 Dst = CastToCStr(Dst, B);
178 Src = CastToCStr(Src, B);
179 CallInst *CI = B.CreateCall4(MemCpy, Dst, Src, Len, ObjSize);
180 if (const Function *F = dyn_cast<Function>(MemCpy->stripPointerCasts()))
181 CI->setCallingConv(F->getCallingConv());
185 /// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
186 /// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
187 Value *llvm::EmitMemChr(Value *Ptr, Value *Val,
188 Value *Len, IRBuilder<> &B, const TargetData *TD,
189 const TargetLibraryInfo *TLI) {
190 if (!TLI->has(LibFunc::memchr))
193 Module *M = B.GetInsertBlock()->getParent()->getParent();
194 AttributeWithIndex AWI;
195 AWI = AttributeWithIndex::get(~0u, Attribute::ReadOnly | Attribute::NoUnwind);
196 LLVMContext &Context = B.GetInsertBlock()->getContext();
197 Value *MemChr = M->getOrInsertFunction("memchr", AttrListPtr::get(AWI),
201 TD->getIntPtrType(Context),
203 CallInst *CI = B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr");
205 if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts()))
206 CI->setCallingConv(F->getCallingConv());
211 /// EmitMemCmp - Emit a call to the memcmp function.
212 Value *llvm::EmitMemCmp(Value *Ptr1, Value *Ptr2,
213 Value *Len, IRBuilder<> &B, const TargetData *TD,
214 const TargetLibraryInfo *TLI) {
215 if (!TLI->has(LibFunc::memcmp))
218 Module *M = B.GetInsertBlock()->getParent()->getParent();
219 AttributeWithIndex AWI[3];
220 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
221 AWI[1] = AttributeWithIndex::get(2, Attribute::NoCapture);
222 AWI[2] = AttributeWithIndex::get(~0u, Attribute::ReadOnly |
223 Attribute::NoUnwind);
225 LLVMContext &Context = B.GetInsertBlock()->getContext();
226 Value *MemCmp = M->getOrInsertFunction("memcmp", AttrListPtr::get(AWI),
230 TD->getIntPtrType(Context), NULL);
231 CallInst *CI = B.CreateCall3(MemCmp, CastToCStr(Ptr1, B), CastToCStr(Ptr2, B),
234 if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts()))
235 CI->setCallingConv(F->getCallingConv());
240 /// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
241 /// 'floor'). This function is known to take a single of type matching 'Op' and
242 /// returns one value with the same type. If 'Op' is a long double, 'l' is
243 /// added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
244 Value *llvm::EmitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B,
245 const AttrListPtr &Attrs) {
246 SmallString<20> NameBuffer;
247 if (!Op->getType()->isDoubleTy()) {
248 // If we need to add a suffix, copy into NameBuffer.
250 if (Op->getType()->isFloatTy())
251 NameBuffer += 'f'; // floorf
253 NameBuffer += 'l'; // floorl
257 Module *M = B.GetInsertBlock()->getParent()->getParent();
258 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
259 Op->getType(), NULL);
260 CallInst *CI = B.CreateCall(Callee, Op, Name);
261 CI->setAttributes(Attrs);
262 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
263 CI->setCallingConv(F->getCallingConv());
268 /// EmitPutChar - Emit a call to the putchar function. This assumes that Char
270 Value *llvm::EmitPutChar(Value *Char, IRBuilder<> &B, const TargetData *TD,
271 const TargetLibraryInfo *TLI) {
272 if (!TLI->has(LibFunc::putchar))
275 Module *M = B.GetInsertBlock()->getParent()->getParent();
276 Value *PutChar = M->getOrInsertFunction("putchar", B.getInt32Ty(),
277 B.getInt32Ty(), NULL);
278 CallInst *CI = B.CreateCall(PutChar,
279 B.CreateIntCast(Char,
285 if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts()))
286 CI->setCallingConv(F->getCallingConv());
290 /// EmitPutS - Emit a call to the puts function. This assumes that Str is
292 Value *llvm::EmitPutS(Value *Str, IRBuilder<> &B, const TargetData *TD,
293 const TargetLibraryInfo *TLI) {
294 if (!TLI->has(LibFunc::puts))
297 Module *M = B.GetInsertBlock()->getParent()->getParent();
298 AttributeWithIndex AWI[2];
299 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
300 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
302 Value *PutS = M->getOrInsertFunction("puts", AttrListPtr::get(AWI),
306 CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts");
307 if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
308 CI->setCallingConv(F->getCallingConv());
312 /// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
313 /// an integer and File is a pointer to FILE.
314 Value *llvm::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B,
315 const TargetData *TD, const TargetLibraryInfo *TLI) {
316 if (!TLI->has(LibFunc::fputc))
319 Module *M = B.GetInsertBlock()->getParent()->getParent();
320 AttributeWithIndex AWI[2];
321 AWI[0] = AttributeWithIndex::get(2, Attribute::NoCapture);
322 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
324 if (File->getType()->isPointerTy())
325 F = M->getOrInsertFunction("fputc", AttrListPtr::get(AWI),
327 B.getInt32Ty(), File->getType(),
330 F = M->getOrInsertFunction("fputc",
333 File->getType(), NULL);
334 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
336 CallInst *CI = B.CreateCall2(F, Char, File, "fputc");
338 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
339 CI->setCallingConv(Fn->getCallingConv());
343 /// EmitFPutS - Emit a call to the puts function. Str is required to be a
344 /// pointer and File is a pointer to FILE.
345 Value *llvm::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B,
346 const TargetData *TD, const TargetLibraryInfo *TLI) {
347 if (!TLI->has(LibFunc::fputs))
350 Module *M = B.GetInsertBlock()->getParent()->getParent();
351 AttributeWithIndex AWI[3];
352 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
353 AWI[1] = AttributeWithIndex::get(2, Attribute::NoCapture);
354 AWI[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
355 StringRef FPutsName = TLI->getName(LibFunc::fputs);
357 if (File->getType()->isPointerTy())
358 F = M->getOrInsertFunction(FPutsName, AttrListPtr::get(AWI),
361 File->getType(), NULL);
363 F = M->getOrInsertFunction(FPutsName, B.getInt32Ty(),
365 File->getType(), NULL);
366 CallInst *CI = B.CreateCall2(F, CastToCStr(Str, B), File, "fputs");
368 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
369 CI->setCallingConv(Fn->getCallingConv());
373 /// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
374 /// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
375 Value *llvm::EmitFWrite(Value *Ptr, Value *Size, Value *File,
376 IRBuilder<> &B, const TargetData *TD,
377 const TargetLibraryInfo *TLI) {
378 if (!TLI->has(LibFunc::fwrite))
381 Module *M = B.GetInsertBlock()->getParent()->getParent();
382 AttributeWithIndex AWI[3];
383 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
384 AWI[1] = AttributeWithIndex::get(4, Attribute::NoCapture);
385 AWI[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
386 LLVMContext &Context = B.GetInsertBlock()->getContext();
387 StringRef FWriteName = TLI->getName(LibFunc::fwrite);
389 if (File->getType()->isPointerTy())
390 F = M->getOrInsertFunction(FWriteName, AttrListPtr::get(AWI),
391 TD->getIntPtrType(Context),
393 TD->getIntPtrType(Context),
394 TD->getIntPtrType(Context),
395 File->getType(), NULL);
397 F = M->getOrInsertFunction(FWriteName, TD->getIntPtrType(Context),
399 TD->getIntPtrType(Context),
400 TD->getIntPtrType(Context),
401 File->getType(), NULL);
402 CallInst *CI = B.CreateCall4(F, CastToCStr(Ptr, B), Size,
403 ConstantInt::get(TD->getIntPtrType(Context), 1), File);
405 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
406 CI->setCallingConv(Fn->getCallingConv());
410 SimplifyFortifiedLibCalls::~SimplifyFortifiedLibCalls() { }
412 bool SimplifyFortifiedLibCalls::fold(CallInst *CI, const TargetData *TD,
413 const TargetLibraryInfo *TLI) {
414 // We really need TargetData for later.
415 if (!TD) return false;
418 Function *Callee = CI->getCalledFunction();
419 StringRef Name = Callee->getName();
420 FunctionType *FT = Callee->getFunctionType();
421 LLVMContext &Context = CI->getParent()->getContext();
424 if (Name == "__memcpy_chk") {
425 // Check if this has the right signature.
426 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
427 !FT->getParamType(0)->isPointerTy() ||
428 !FT->getParamType(1)->isPointerTy() ||
429 FT->getParamType(2) != TD->getIntPtrType(Context) ||
430 FT->getParamType(3) != TD->getIntPtrType(Context))
433 if (isFoldable(3, 2, false)) {
434 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
435 CI->getArgOperand(2), 1);
436 replaceCall(CI->getArgOperand(0));
442 // Should be similar to memcpy.
443 if (Name == "__mempcpy_chk") {
447 if (Name == "__memmove_chk") {
448 // Check if this has the right signature.
449 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
450 !FT->getParamType(0)->isPointerTy() ||
451 !FT->getParamType(1)->isPointerTy() ||
452 FT->getParamType(2) != TD->getIntPtrType(Context) ||
453 FT->getParamType(3) != TD->getIntPtrType(Context))
456 if (isFoldable(3, 2, false)) {
457 B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
458 CI->getArgOperand(2), 1);
459 replaceCall(CI->getArgOperand(0));
465 if (Name == "__memset_chk") {
466 // Check if this has the right signature.
467 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
468 !FT->getParamType(0)->isPointerTy() ||
469 !FT->getParamType(1)->isIntegerTy() ||
470 FT->getParamType(2) != TD->getIntPtrType(Context) ||
471 FT->getParamType(3) != TD->getIntPtrType(Context))
474 if (isFoldable(3, 2, false)) {
475 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(),
477 B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
478 replaceCall(CI->getArgOperand(0));
484 if (Name == "__strcpy_chk" || Name == "__stpcpy_chk") {
485 // Check if this has the right signature.
486 if (FT->getNumParams() != 3 ||
487 FT->getReturnType() != FT->getParamType(0) ||
488 FT->getParamType(0) != FT->getParamType(1) ||
489 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
490 FT->getParamType(2) != TD->getIntPtrType(Context))
494 // If a) we don't have any length information, or b) we know this will
495 // fit then just lower to a plain st[rp]cpy. Otherwise we'll keep our
496 // st[rp]cpy_chk call which may fail at runtime if the size is too long.
497 // TODO: It might be nice to get a maximum length out of the possible
498 // string lengths for varying.
499 if (isFoldable(2, 1, true)) {
500 Value *Ret = EmitStrCpy(CI->getArgOperand(0), CI->getArgOperand(1), B, TD,
501 TLI, Name.substr(2, 6));
508 if (Name == "__strncpy_chk" || Name == "__stpncpy_chk") {
509 // Check if this has the right signature.
510 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
511 FT->getParamType(0) != FT->getParamType(1) ||
512 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
513 !FT->getParamType(2)->isIntegerTy() ||
514 FT->getParamType(3) != TD->getIntPtrType(Context))
517 if (isFoldable(3, 2, false)) {
518 Value *Ret = EmitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
519 CI->getArgOperand(2), B, TD, TLI,
527 if (Name == "__strcat_chk") {
531 if (Name == "__strncat_chk") {