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/Type.h"
16 #include "llvm/Constants.h"
17 #include "llvm/Function.h"
18 #include "llvm/Module.h"
19 #include "llvm/Support/IRBuilder.h"
20 #include "llvm/Target/TargetData.h"
21 #include "llvm/LLVMContext.h"
22 #include "llvm/Intrinsics.h"
26 /// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
27 Value *llvm::CastToCStr(Value *V, IRBuilder<> &B) {
28 return B.CreateBitCast(V, B.getInt8PtrTy(), "cstr");
31 /// EmitStrLen - Emit a call to the strlen function to the builder, for the
32 /// specified pointer. This always returns an integer value of size intptr_t.
33 Value *llvm::EmitStrLen(Value *Ptr, IRBuilder<> &B, const TargetData *TD) {
34 Module *M = B.GetInsertBlock()->getParent()->getParent();
35 AttributeWithIndex AWI[2];
36 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
37 AWI[1] = AttributeWithIndex::get(~0u, Attribute::ReadOnly |
40 LLVMContext &Context = B.GetInsertBlock()->getContext();
41 Constant *StrLen = M->getOrInsertFunction("strlen", AttrListPtr::get(AWI, 2),
42 TD->getIntPtrType(Context),
45 CallInst *CI = B.CreateCall(StrLen, CastToCStr(Ptr, B), "strlen");
46 if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts()))
47 CI->setCallingConv(F->getCallingConv());
52 /// EmitStrChr - Emit a call to the strchr function to the builder, for the
53 /// specified pointer and character. Ptr is required to be some pointer type,
54 /// and the return value has 'i8*' type.
55 Value *llvm::EmitStrChr(Value *Ptr, char C, IRBuilder<> &B,
56 const TargetData *TD) {
57 Module *M = B.GetInsertBlock()->getParent()->getParent();
58 AttributeWithIndex AWI =
59 AttributeWithIndex::get(~0u, Attribute::ReadOnly | Attribute::NoUnwind);
61 const Type *I8Ptr = B.getInt8PtrTy();
62 const Type *I32Ty = B.getInt32Ty();
63 Constant *StrChr = M->getOrInsertFunction("strchr", AttrListPtr::get(&AWI, 1),
64 I8Ptr, I8Ptr, I32Ty, NULL);
65 CallInst *CI = B.CreateCall2(StrChr, CastToCStr(Ptr, B),
66 ConstantInt::get(I32Ty, C), "strchr");
67 if (const Function *F = dyn_cast<Function>(StrChr->stripPointerCasts()))
68 CI->setCallingConv(F->getCallingConv());
72 /// EmitStrNCmp - Emit a call to the strncmp function to the builder.
73 Value *llvm::EmitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len,
74 IRBuilder<> &B, const TargetData *TD) {
75 Module *M = B.GetInsertBlock()->getParent()->getParent();
76 AttributeWithIndex AWI[3];
77 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
78 AWI[1] = AttributeWithIndex::get(2, Attribute::NoCapture);
79 AWI[2] = AttributeWithIndex::get(~0u, Attribute::ReadOnly |
82 LLVMContext &Context = B.GetInsertBlock()->getContext();
83 Value *StrNCmp = M->getOrInsertFunction("strncmp", AttrListPtr::get(AWI, 3),
87 TD->getIntPtrType(Context), NULL);
88 CallInst *CI = B.CreateCall3(StrNCmp, CastToCStr(Ptr1, B),
89 CastToCStr(Ptr2, B), Len, "strncmp");
91 if (const Function *F = dyn_cast<Function>(StrNCmp->stripPointerCasts()))
92 CI->setCallingConv(F->getCallingConv());
97 /// EmitStrCpy - Emit a call to the strcpy function to the builder, for the
98 /// specified pointer arguments.
99 Value *llvm::EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
100 const TargetData *TD, StringRef Name) {
101 Module *M = B.GetInsertBlock()->getParent()->getParent();
102 AttributeWithIndex AWI[2];
103 AWI[0] = AttributeWithIndex::get(2, Attribute::NoCapture);
104 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
105 const Type *I8Ptr = B.getInt8PtrTy();
106 Value *StrCpy = M->getOrInsertFunction(Name, AttrListPtr::get(AWI, 2),
107 I8Ptr, I8Ptr, I8Ptr, NULL);
108 CallInst *CI = B.CreateCall2(StrCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
110 if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts()))
111 CI->setCallingConv(F->getCallingConv());
115 /// EmitStrNCpy - Emit a call to the strncpy function to the builder, for the
116 /// specified pointer arguments.
117 Value *llvm::EmitStrNCpy(Value *Dst, Value *Src, Value *Len,
118 IRBuilder<> &B, const TargetData *TD, StringRef Name) {
119 Module *M = B.GetInsertBlock()->getParent()->getParent();
120 AttributeWithIndex AWI[2];
121 AWI[0] = AttributeWithIndex::get(2, Attribute::NoCapture);
122 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
123 const Type *I8Ptr = B.getInt8PtrTy();
124 Value *StrNCpy = M->getOrInsertFunction(Name, AttrListPtr::get(AWI, 2),
126 Len->getType(), NULL);
127 CallInst *CI = B.CreateCall3(StrNCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
129 if (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts()))
130 CI->setCallingConv(F->getCallingConv());
135 /// EmitMemCpy - Emit a call to the memcpy function to the builder. This always
136 /// expects that Len has type 'intptr_t' and Dst/Src are pointers.
137 Value *llvm::EmitMemCpy(Value *Dst, Value *Src, Value *Len, unsigned Align,
138 bool isVolatile, IRBuilder<> &B, const TargetData *TD) {
139 Module *M = B.GetInsertBlock()->getParent()->getParent();
140 Dst = CastToCStr(Dst, B);
141 Src = CastToCStr(Src, B);
142 const Type *ArgTys[3] = { Dst->getType(), Src->getType(), Len->getType() };
143 Value *MemCpy = Intrinsic::getDeclaration(M, Intrinsic::memcpy, ArgTys, 3);
144 return B.CreateCall5(MemCpy, Dst, Src, Len,
145 ConstantInt::get(B.getInt32Ty(), Align),
146 ConstantInt::get(B.getInt1Ty(), isVolatile));
149 /// EmitMemCpyChk - Emit a call to the __memcpy_chk function to the builder.
150 /// This expects that the Len and ObjSize have type 'intptr_t' and Dst/Src
152 Value *llvm::EmitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
153 IRBuilder<> &B, const TargetData *TD) {
154 Module *M = B.GetInsertBlock()->getParent()->getParent();
155 AttributeWithIndex AWI;
156 AWI = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
157 LLVMContext &Context = B.GetInsertBlock()->getContext();
158 Value *MemCpy = M->getOrInsertFunction("__memcpy_chk",
159 AttrListPtr::get(&AWI, 1),
163 TD->getIntPtrType(Context),
164 TD->getIntPtrType(Context), NULL);
165 Dst = CastToCStr(Dst, B);
166 Src = CastToCStr(Src, B);
167 CallInst *CI = B.CreateCall4(MemCpy, Dst, Src, Len, ObjSize);
168 if (const Function *F = dyn_cast<Function>(MemCpy->stripPointerCasts()))
169 CI->setCallingConv(F->getCallingConv());
173 /// EmitMemMove - Emit a call to the memmove function to the builder. This
174 /// always expects that the size has type 'intptr_t' and Dst/Src are pointers.
175 Value *llvm::EmitMemMove(Value *Dst, Value *Src, Value *Len, unsigned Align,
176 bool isVolatile, IRBuilder<> &B, const TargetData *TD) {
177 Module *M = B.GetInsertBlock()->getParent()->getParent();
178 LLVMContext &Context = B.GetInsertBlock()->getContext();
179 const Type *ArgTys[3] = { Dst->getType(), Src->getType(),
180 TD->getIntPtrType(Context) };
181 Value *MemMove = Intrinsic::getDeclaration(M, Intrinsic::memmove, ArgTys, 3);
182 Dst = CastToCStr(Dst, B);
183 Src = CastToCStr(Src, B);
184 Value *A = ConstantInt::get(B.getInt32Ty(), Align);
185 Value *Vol = ConstantInt::get(B.getInt1Ty(), isVolatile);
186 return B.CreateCall5(MemMove, Dst, Src, Len, A, Vol);
189 /// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
190 /// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
191 Value *llvm::EmitMemChr(Value *Ptr, Value *Val,
192 Value *Len, IRBuilder<> &B, const TargetData *TD) {
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, 1),
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 Module *M = B.GetInsertBlock()->getParent()->getParent();
215 AttributeWithIndex AWI[3];
216 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
217 AWI[1] = AttributeWithIndex::get(2, Attribute::NoCapture);
218 AWI[2] = AttributeWithIndex::get(~0u, Attribute::ReadOnly |
219 Attribute::NoUnwind);
221 LLVMContext &Context = B.GetInsertBlock()->getContext();
222 Value *MemCmp = M->getOrInsertFunction("memcmp", AttrListPtr::get(AWI, 3),
226 TD->getIntPtrType(Context), NULL);
227 CallInst *CI = B.CreateCall3(MemCmp, CastToCStr(Ptr1, B), CastToCStr(Ptr2, B),
230 if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts()))
231 CI->setCallingConv(F->getCallingConv());
236 /// EmitMemSet - Emit a call to the memset function
237 Value *llvm::EmitMemSet(Value *Dst, Value *Val, Value *Len, bool isVolatile,
238 IRBuilder<> &B, const TargetData *TD) {
239 Module *M = B.GetInsertBlock()->getParent()->getParent();
240 Intrinsic::ID IID = Intrinsic::memset;
241 const Type *Tys[2] = { Dst->getType(), Len->getType() };
242 Value *MemSet = Intrinsic::getDeclaration(M, IID, Tys, 2);
243 Value *Align = ConstantInt::get(B.getInt32Ty(), 1);
244 Value *Vol = ConstantInt::get(B.getInt1Ty(), isVolatile);
245 return B.CreateCall5(MemSet, CastToCStr(Dst, B), Val, Len, Align, Vol);
248 /// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
249 /// 'floor'). This function is known to take a single of type matching 'Op' and
250 /// returns one value with the same type. If 'Op' is a long double, 'l' is
251 /// added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
252 Value *llvm::EmitUnaryFloatFnCall(Value *Op, const char *Name,
253 IRBuilder<> &B, const AttrListPtr &Attrs) {
255 if (!Op->getType()->isDoubleTy()) {
256 // If we need to add a suffix, copy into NameBuffer.
257 unsigned NameLen = strlen(Name);
258 assert(NameLen < sizeof(NameBuffer)-2);
259 memcpy(NameBuffer, Name, NameLen);
260 if (Op->getType()->isFloatTy())
261 NameBuffer[NameLen] = 'f'; // floorf
263 NameBuffer[NameLen] = 'l'; // floorl
264 NameBuffer[NameLen+1] = 0;
268 Module *M = B.GetInsertBlock()->getParent()->getParent();
269 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
270 Op->getType(), NULL);
271 CallInst *CI = B.CreateCall(Callee, Op, Name);
272 CI->setAttributes(Attrs);
273 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
274 CI->setCallingConv(F->getCallingConv());
279 /// EmitPutChar - Emit a call to the putchar function. This assumes that Char
281 Value *llvm::EmitPutChar(Value *Char, IRBuilder<> &B, const TargetData *TD) {
282 Module *M = B.GetInsertBlock()->getParent()->getParent();
283 Value *PutChar = M->getOrInsertFunction("putchar", B.getInt32Ty(),
284 B.getInt32Ty(), NULL);
285 CallInst *CI = B.CreateCall(PutChar,
286 B.CreateIntCast(Char,
292 if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts()))
293 CI->setCallingConv(F->getCallingConv());
297 /// EmitPutS - Emit a call to the puts function. This assumes that Str is
299 void llvm::EmitPutS(Value *Str, IRBuilder<> &B, const TargetData *TD) {
300 Module *M = B.GetInsertBlock()->getParent()->getParent();
301 AttributeWithIndex AWI[2];
302 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
303 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
305 Value *PutS = M->getOrInsertFunction("puts", AttrListPtr::get(AWI, 2),
309 CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts");
310 if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
311 CI->setCallingConv(F->getCallingConv());
315 /// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
316 /// an integer and File is a pointer to FILE.
317 void llvm::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B,
318 const TargetData *TD) {
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, 2),
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());
342 /// EmitFPutS - Emit a call to the puts function. Str is required to be a
343 /// pointer and File is a pointer to FILE.
344 void llvm::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B,
345 const TargetData *TD) {
346 Module *M = B.GetInsertBlock()->getParent()->getParent();
347 AttributeWithIndex AWI[3];
348 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
349 AWI[1] = AttributeWithIndex::get(2, Attribute::NoCapture);
350 AWI[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
352 if (File->getType()->isPointerTy())
353 F = M->getOrInsertFunction("fputs", AttrListPtr::get(AWI, 3),
356 File->getType(), NULL);
358 F = M->getOrInsertFunction("fputs", B.getInt32Ty(),
360 File->getType(), NULL);
361 CallInst *CI = B.CreateCall2(F, CastToCStr(Str, B), File, "fputs");
363 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
364 CI->setCallingConv(Fn->getCallingConv());
367 /// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
368 /// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
369 void llvm::EmitFWrite(Value *Ptr, Value *Size, Value *File,
370 IRBuilder<> &B, const TargetData *TD) {
371 Module *M = B.GetInsertBlock()->getParent()->getParent();
372 AttributeWithIndex AWI[3];
373 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
374 AWI[1] = AttributeWithIndex::get(4, Attribute::NoCapture);
375 AWI[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
376 LLVMContext &Context = B.GetInsertBlock()->getContext();
378 if (File->getType()->isPointerTy())
379 F = M->getOrInsertFunction("fwrite", AttrListPtr::get(AWI, 3),
380 TD->getIntPtrType(Context),
382 TD->getIntPtrType(Context),
383 TD->getIntPtrType(Context),
384 File->getType(), NULL);
386 F = M->getOrInsertFunction("fwrite", TD->getIntPtrType(Context),
388 TD->getIntPtrType(Context),
389 TD->getIntPtrType(Context),
390 File->getType(), NULL);
391 CallInst *CI = B.CreateCall4(F, CastToCStr(Ptr, B), Size,
392 ConstantInt::get(TD->getIntPtrType(Context), 1), File);
394 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
395 CI->setCallingConv(Fn->getCallingConv());
398 SimplifyFortifiedLibCalls::~SimplifyFortifiedLibCalls() { }
400 bool SimplifyFortifiedLibCalls::fold(CallInst *CI, const TargetData *TD) {
401 // We really need TargetData for later.
402 if (!TD) return false;
405 Function *Callee = CI->getCalledFunction();
406 StringRef Name = Callee->getName();
407 const FunctionType *FT = Callee->getFunctionType();
408 BasicBlock *BB = CI->getParent();
409 LLVMContext &Context = CI->getParent()->getContext();
410 IRBuilder<> B(Context);
412 // Set the builder to the instruction after the call.
413 B.SetInsertPoint(BB, CI);
415 if (Name == "__memcpy_chk") {
416 // Check if this has the right signature.
417 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
418 !FT->getParamType(0)->isPointerTy() ||
419 !FT->getParamType(1)->isPointerTy() ||
420 FT->getParamType(2) != TD->getIntPtrType(Context) ||
421 FT->getParamType(3) != TD->getIntPtrType(Context))
424 if (isFoldable(3, 2, false)) {
425 EmitMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
426 CI->getArgOperand(2), 1, false, B, TD);
427 replaceCall(CI->getArgOperand(0));
433 // Should be similar to memcpy.
434 if (Name == "__mempcpy_chk") {
438 if (Name == "__memmove_chk") {
439 // Check if this has the right signature.
440 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
441 !FT->getParamType(0)->isPointerTy() ||
442 !FT->getParamType(1)->isPointerTy() ||
443 FT->getParamType(2) != TD->getIntPtrType(Context) ||
444 FT->getParamType(3) != TD->getIntPtrType(Context))
447 if (isFoldable(3, 2, false)) {
448 EmitMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
449 CI->getArgOperand(2), 1, false, B, TD);
450 replaceCall(CI->getArgOperand(0));
456 if (Name == "__memset_chk") {
457 // Check if this has the right signature.
458 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
459 !FT->getParamType(0)->isPointerTy() ||
460 !FT->getParamType(1)->isIntegerTy() ||
461 FT->getParamType(2) != TD->getIntPtrType(Context) ||
462 FT->getParamType(3) != TD->getIntPtrType(Context))
465 if (isFoldable(3, 2, false)) {
466 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(),
468 EmitMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2),
470 replaceCall(CI->getArgOperand(0));
476 if (Name == "__strcpy_chk" || Name == "__stpcpy_chk") {
477 // Check if this has the right signature.
478 if (FT->getNumParams() != 3 ||
479 FT->getReturnType() != FT->getParamType(0) ||
480 FT->getParamType(0) != FT->getParamType(1) ||
481 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
482 FT->getParamType(2) != TD->getIntPtrType(Context))
486 // If a) we don't have any length information, or b) we know this will
487 // fit then just lower to a plain st[rp]cpy. Otherwise we'll keep our
488 // st[rp]cpy_chk call which may fail at runtime if the size is too long.
489 // TODO: It might be nice to get a maximum length out of the possible
490 // string lengths for varying.
491 if (isFoldable(2, 1, true)) {
492 Value *Ret = EmitStrCpy(CI->getArgOperand(0), CI->getArgOperand(1), B, TD,
500 if (Name == "__strncpy_chk" || Name == "__stpncpy_chk") {
501 // Check if this has the right signature.
502 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
503 FT->getParamType(0) != FT->getParamType(1) ||
504 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
505 !FT->getParamType(2)->isIntegerTy() ||
506 FT->getParamType(3) != TD->getIntPtrType(Context))
509 if (isFoldable(3, 2, false)) {
510 Value *Ret = EmitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
511 CI->getArgOperand(2), B, TD, Name.substr(2, 7));
518 if (Name == "__strcat_chk") {
522 if (Name == "__strncat_chk") {