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/Intrinsics.h"
19 #include "llvm/LLVMContext.h"
20 #include "llvm/Module.h"
21 #include "llvm/Support/IRBuilder.h"
22 #include "llvm/Target/TargetData.h"
23 #include "llvm/Target/TargetLibraryInfo.h"
24 #include "llvm/LLVMContext.h"
25 #include "llvm/Intrinsics.h"
26 #include "llvm/ADT/SmallString.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 Module *M = B.GetInsertBlock()->getParent()->getParent();
39 AttributeWithIndex AWI[2];
40 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
41 AWI[1] = AttributeWithIndex::get(~0u, Attribute::ReadOnly |
44 LLVMContext &Context = B.GetInsertBlock()->getContext();
45 Constant *StrLen = M->getOrInsertFunction("strlen", AttrListPtr::get(AWI, 2),
46 TD->getIntPtrType(Context),
49 CallInst *CI = B.CreateCall(StrLen, CastToCStr(Ptr, B), "strlen");
50 if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts()))
51 CI->setCallingConv(F->getCallingConv());
56 /// EmitStrChr - Emit a call to the strchr function to the builder, for the
57 /// specified pointer and character. Ptr is required to be some pointer type,
58 /// and the return value has 'i8*' type.
59 Value *llvm::EmitStrChr(Value *Ptr, char C, IRBuilder<> &B,
60 const TargetData *TD) {
61 Module *M = B.GetInsertBlock()->getParent()->getParent();
62 AttributeWithIndex AWI =
63 AttributeWithIndex::get(~0u, Attribute::ReadOnly | Attribute::NoUnwind);
65 Type *I8Ptr = B.getInt8PtrTy();
66 Type *I32Ty = B.getInt32Ty();
67 Constant *StrChr = M->getOrInsertFunction("strchr", AttrListPtr::get(&AWI, 1),
68 I8Ptr, I8Ptr, I32Ty, NULL);
69 CallInst *CI = B.CreateCall2(StrChr, CastToCStr(Ptr, B),
70 ConstantInt::get(I32Ty, C), "strchr");
71 if (const Function *F = dyn_cast<Function>(StrChr->stripPointerCasts()))
72 CI->setCallingConv(F->getCallingConv());
76 /// EmitStrNCmp - Emit a call to the strncmp function to the builder.
77 Value *llvm::EmitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len,
78 IRBuilder<> &B, const TargetData *TD) {
79 Module *M = B.GetInsertBlock()->getParent()->getParent();
80 AttributeWithIndex AWI[3];
81 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
82 AWI[1] = AttributeWithIndex::get(2, Attribute::NoCapture);
83 AWI[2] = AttributeWithIndex::get(~0u, Attribute::ReadOnly |
86 LLVMContext &Context = B.GetInsertBlock()->getContext();
87 Value *StrNCmp = M->getOrInsertFunction("strncmp", AttrListPtr::get(AWI, 3),
91 TD->getIntPtrType(Context), NULL);
92 CallInst *CI = B.CreateCall3(StrNCmp, CastToCStr(Ptr1, B),
93 CastToCStr(Ptr2, B), Len, "strncmp");
95 if (const Function *F = dyn_cast<Function>(StrNCmp->stripPointerCasts()))
96 CI->setCallingConv(F->getCallingConv());
101 /// EmitStrCpy - Emit a call to the strcpy function to the builder, for the
102 /// specified pointer arguments.
103 Value *llvm::EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
104 const TargetData *TD, StringRef Name) {
105 Module *M = B.GetInsertBlock()->getParent()->getParent();
106 AttributeWithIndex AWI[2];
107 AWI[0] = AttributeWithIndex::get(2, Attribute::NoCapture);
108 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
109 Type *I8Ptr = B.getInt8PtrTy();
110 Value *StrCpy = M->getOrInsertFunction(Name, AttrListPtr::get(AWI, 2),
111 I8Ptr, I8Ptr, I8Ptr, NULL);
112 CallInst *CI = B.CreateCall2(StrCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
114 if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts()))
115 CI->setCallingConv(F->getCallingConv());
119 /// EmitStrNCpy - Emit a call to the strncpy function to the builder, for the
120 /// specified pointer arguments.
121 Value *llvm::EmitStrNCpy(Value *Dst, Value *Src, Value *Len,
122 IRBuilder<> &B, const TargetData *TD, StringRef Name) {
123 Module *M = B.GetInsertBlock()->getParent()->getParent();
124 AttributeWithIndex AWI[2];
125 AWI[0] = AttributeWithIndex::get(2, Attribute::NoCapture);
126 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
127 Type *I8Ptr = B.getInt8PtrTy();
128 Value *StrNCpy = M->getOrInsertFunction(Name, AttrListPtr::get(AWI, 2),
130 Len->getType(), NULL);
131 CallInst *CI = B.CreateCall3(StrNCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
133 if (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts()))
134 CI->setCallingConv(F->getCallingConv());
138 /// EmitMemCpyChk - Emit a call to the __memcpy_chk function to the builder.
139 /// This expects that the Len and ObjSize have type 'intptr_t' and Dst/Src
141 Value *llvm::EmitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
142 IRBuilder<> &B, const TargetData *TD) {
143 Module *M = B.GetInsertBlock()->getParent()->getParent();
144 AttributeWithIndex AWI;
145 AWI = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
146 LLVMContext &Context = B.GetInsertBlock()->getContext();
147 Value *MemCpy = M->getOrInsertFunction("__memcpy_chk",
148 AttrListPtr::get(&AWI, 1),
152 TD->getIntPtrType(Context),
153 TD->getIntPtrType(Context), NULL);
154 Dst = CastToCStr(Dst, B);
155 Src = CastToCStr(Src, B);
156 CallInst *CI = B.CreateCall4(MemCpy, Dst, Src, Len, ObjSize);
157 if (const Function *F = dyn_cast<Function>(MemCpy->stripPointerCasts()))
158 CI->setCallingConv(F->getCallingConv());
162 /// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
163 /// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
164 Value *llvm::EmitMemChr(Value *Ptr, Value *Val,
165 Value *Len, IRBuilder<> &B, const TargetData *TD) {
166 Module *M = B.GetInsertBlock()->getParent()->getParent();
167 AttributeWithIndex AWI;
168 AWI = AttributeWithIndex::get(~0u, Attribute::ReadOnly | Attribute::NoUnwind);
169 LLVMContext &Context = B.GetInsertBlock()->getContext();
170 Value *MemChr = M->getOrInsertFunction("memchr", AttrListPtr::get(&AWI, 1),
174 TD->getIntPtrType(Context),
176 CallInst *CI = B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr");
178 if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts()))
179 CI->setCallingConv(F->getCallingConv());
184 /// EmitMemCmp - Emit a call to the memcmp function.
185 Value *llvm::EmitMemCmp(Value *Ptr1, Value *Ptr2,
186 Value *Len, IRBuilder<> &B, const TargetData *TD) {
187 Module *M = B.GetInsertBlock()->getParent()->getParent();
188 AttributeWithIndex AWI[3];
189 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
190 AWI[1] = AttributeWithIndex::get(2, Attribute::NoCapture);
191 AWI[2] = AttributeWithIndex::get(~0u, Attribute::ReadOnly |
192 Attribute::NoUnwind);
194 LLVMContext &Context = B.GetInsertBlock()->getContext();
195 Value *MemCmp = M->getOrInsertFunction("memcmp", AttrListPtr::get(AWI, 3),
199 TD->getIntPtrType(Context), NULL);
200 CallInst *CI = B.CreateCall3(MemCmp, CastToCStr(Ptr1, B), CastToCStr(Ptr2, B),
203 if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts()))
204 CI->setCallingConv(F->getCallingConv());
209 /// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
210 /// 'floor'). This function is known to take a single of type matching 'Op' and
211 /// returns one value with the same type. If 'Op' is a long double, 'l' is
212 /// added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
213 Value *llvm::EmitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B,
214 const AttrListPtr &Attrs) {
215 SmallString<20> NameBuffer;
216 if (!Op->getType()->isDoubleTy()) {
217 // If we need to add a suffix, copy into NameBuffer.
219 if (Op->getType()->isFloatTy())
220 NameBuffer += 'f'; // floorf
222 NameBuffer += 'l'; // floorl
226 Module *M = B.GetInsertBlock()->getParent()->getParent();
227 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
228 Op->getType(), NULL);
229 CallInst *CI = B.CreateCall(Callee, Op, Name);
230 CI->setAttributes(Attrs);
231 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
232 CI->setCallingConv(F->getCallingConv());
237 /// EmitPutChar - Emit a call to the putchar function. This assumes that Char
239 Value *llvm::EmitPutChar(Value *Char, IRBuilder<> &B, const TargetData *TD) {
240 Module *M = B.GetInsertBlock()->getParent()->getParent();
241 Value *PutChar = M->getOrInsertFunction("putchar", B.getInt32Ty(),
242 B.getInt32Ty(), NULL);
243 CallInst *CI = B.CreateCall(PutChar,
244 B.CreateIntCast(Char,
250 if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts()))
251 CI->setCallingConv(F->getCallingConv());
255 /// EmitPutS - Emit a call to the puts function. This assumes that Str is
257 void llvm::EmitPutS(Value *Str, IRBuilder<> &B, const TargetData *TD) {
258 Module *M = B.GetInsertBlock()->getParent()->getParent();
259 AttributeWithIndex AWI[2];
260 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
261 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
263 Value *PutS = M->getOrInsertFunction("puts", AttrListPtr::get(AWI, 2),
267 CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts");
268 if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
269 CI->setCallingConv(F->getCallingConv());
273 /// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
274 /// an integer and File is a pointer to FILE.
275 void llvm::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B,
276 const TargetData *TD) {
277 Module *M = B.GetInsertBlock()->getParent()->getParent();
278 AttributeWithIndex AWI[2];
279 AWI[0] = AttributeWithIndex::get(2, Attribute::NoCapture);
280 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
282 if (File->getType()->isPointerTy())
283 F = M->getOrInsertFunction("fputc", AttrListPtr::get(AWI, 2),
285 B.getInt32Ty(), File->getType(),
288 F = M->getOrInsertFunction("fputc",
291 File->getType(), NULL);
292 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
294 CallInst *CI = B.CreateCall2(F, Char, File, "fputc");
296 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
297 CI->setCallingConv(Fn->getCallingConv());
300 /// EmitFPutS - Emit a call to the puts function. Str is required to be a
301 /// pointer and File is a pointer to FILE.
302 void llvm::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B,
303 const TargetData *TD, const TargetLibraryInfo *TLI) {
304 Module *M = B.GetInsertBlock()->getParent()->getParent();
305 AttributeWithIndex AWI[3];
306 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
307 AWI[1] = AttributeWithIndex::get(2, Attribute::NoCapture);
308 AWI[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
309 StringRef FPutsName = TLI->getName(LibFunc::fputs);
311 if (File->getType()->isPointerTy())
312 F = M->getOrInsertFunction(FPutsName, AttrListPtr::get(AWI, 3),
315 File->getType(), NULL);
317 F = M->getOrInsertFunction(FPutsName, B.getInt32Ty(),
319 File->getType(), NULL);
320 CallInst *CI = B.CreateCall2(F, CastToCStr(Str, B), File, "fputs");
322 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
323 CI->setCallingConv(Fn->getCallingConv());
326 /// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
327 /// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
328 void llvm::EmitFWrite(Value *Ptr, Value *Size, Value *File,
329 IRBuilder<> &B, const TargetData *TD,
330 const TargetLibraryInfo *TLI) {
331 Module *M = B.GetInsertBlock()->getParent()->getParent();
332 AttributeWithIndex AWI[3];
333 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
334 AWI[1] = AttributeWithIndex::get(4, Attribute::NoCapture);
335 AWI[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
336 LLVMContext &Context = B.GetInsertBlock()->getContext();
337 StringRef FWriteName = TLI->getName(LibFunc::fwrite);
339 if (File->getType()->isPointerTy())
340 F = M->getOrInsertFunction(FWriteName, AttrListPtr::get(AWI, 3),
341 TD->getIntPtrType(Context),
343 TD->getIntPtrType(Context),
344 TD->getIntPtrType(Context),
345 File->getType(), NULL);
347 F = M->getOrInsertFunction(FWriteName, TD->getIntPtrType(Context),
349 TD->getIntPtrType(Context),
350 TD->getIntPtrType(Context),
351 File->getType(), NULL);
352 CallInst *CI = B.CreateCall4(F, CastToCStr(Ptr, B), Size,
353 ConstantInt::get(TD->getIntPtrType(Context), 1), File);
355 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
356 CI->setCallingConv(Fn->getCallingConv());
359 SimplifyFortifiedLibCalls::~SimplifyFortifiedLibCalls() { }
361 bool SimplifyFortifiedLibCalls::fold(CallInst *CI, const TargetData *TD) {
362 // We really need TargetData for later.
363 if (!TD) return false;
366 Function *Callee = CI->getCalledFunction();
367 StringRef Name = Callee->getName();
368 FunctionType *FT = Callee->getFunctionType();
369 LLVMContext &Context = CI->getParent()->getContext();
372 if (Name == "__memcpy_chk") {
373 // Check if this has the right signature.
374 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
375 !FT->getParamType(0)->isPointerTy() ||
376 !FT->getParamType(1)->isPointerTy() ||
377 FT->getParamType(2) != TD->getIntPtrType(Context) ||
378 FT->getParamType(3) != TD->getIntPtrType(Context))
381 if (isFoldable(3, 2, false)) {
382 B.CreateMemCpy(CI->getArgOperand(0), CI->getArgOperand(1),
383 CI->getArgOperand(2), 1);
384 replaceCall(CI->getArgOperand(0));
390 // Should be similar to memcpy.
391 if (Name == "__mempcpy_chk") {
395 if (Name == "__memmove_chk") {
396 // Check if this has the right signature.
397 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
398 !FT->getParamType(0)->isPointerTy() ||
399 !FT->getParamType(1)->isPointerTy() ||
400 FT->getParamType(2) != TD->getIntPtrType(Context) ||
401 FT->getParamType(3) != TD->getIntPtrType(Context))
404 if (isFoldable(3, 2, false)) {
405 B.CreateMemMove(CI->getArgOperand(0), CI->getArgOperand(1),
406 CI->getArgOperand(2), 1);
407 replaceCall(CI->getArgOperand(0));
413 if (Name == "__memset_chk") {
414 // Check if this has the right signature.
415 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
416 !FT->getParamType(0)->isPointerTy() ||
417 !FT->getParamType(1)->isIntegerTy() ||
418 FT->getParamType(2) != TD->getIntPtrType(Context) ||
419 FT->getParamType(3) != TD->getIntPtrType(Context))
422 if (isFoldable(3, 2, false)) {
423 Value *Val = B.CreateIntCast(CI->getArgOperand(1), B.getInt8Ty(),
425 B.CreateMemSet(CI->getArgOperand(0), Val, CI->getArgOperand(2), 1);
426 replaceCall(CI->getArgOperand(0));
432 if (Name == "__strcpy_chk" || Name == "__stpcpy_chk") {
433 // Check if this has the right signature.
434 if (FT->getNumParams() != 3 ||
435 FT->getReturnType() != FT->getParamType(0) ||
436 FT->getParamType(0) != FT->getParamType(1) ||
437 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
438 FT->getParamType(2) != TD->getIntPtrType(Context))
442 // If a) we don't have any length information, or b) we know this will
443 // fit then just lower to a plain st[rp]cpy. Otherwise we'll keep our
444 // st[rp]cpy_chk call which may fail at runtime if the size is too long.
445 // TODO: It might be nice to get a maximum length out of the possible
446 // string lengths for varying.
447 if (isFoldable(2, 1, true)) {
448 Value *Ret = EmitStrCpy(CI->getArgOperand(0), CI->getArgOperand(1), B, TD,
456 if (Name == "__strncpy_chk" || Name == "__stpncpy_chk") {
457 // Check if this has the right signature.
458 if (FT->getNumParams() != 4 || FT->getReturnType() != FT->getParamType(0) ||
459 FT->getParamType(0) != FT->getParamType(1) ||
460 FT->getParamType(0) != Type::getInt8PtrTy(Context) ||
461 !FT->getParamType(2)->isIntegerTy() ||
462 FT->getParamType(3) != TD->getIntPtrType(Context))
465 if (isFoldable(3, 2, false)) {
466 Value *Ret = EmitStrNCpy(CI->getArgOperand(0), CI->getArgOperand(1),
467 CI->getArgOperand(2), B, TD, Name.substr(2, 7));
474 if (Name == "__strcat_chk") {
478 if (Name == "__strncat_chk") {