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 /// EmitStrCpy - Emit a call to the strcpy function to the builder, for the
73 /// specified pointer arguments.
74 Value *llvm::EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
75 const TargetData *TD, StringRef Name) {
76 Module *M = B.GetInsertBlock()->getParent()->getParent();
77 AttributeWithIndex AWI[2];
78 AWI[0] = AttributeWithIndex::get(2, Attribute::NoCapture);
79 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
80 const Type *I8Ptr = B.getInt8PtrTy();
81 Value *StrCpy = M->getOrInsertFunction(Name, AttrListPtr::get(AWI, 2),
82 I8Ptr, I8Ptr, I8Ptr, NULL);
83 CallInst *CI = B.CreateCall2(StrCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
85 if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts()))
86 CI->setCallingConv(F->getCallingConv());
90 /// EmitStrNCpy - Emit a call to the strncpy function to the builder, for the
91 /// specified pointer arguments.
92 Value *llvm::EmitStrNCpy(Value *Dst, Value *Src, Value *Len,
93 IRBuilder<> &B, const TargetData *TD) {
94 Module *M = B.GetInsertBlock()->getParent()->getParent();
95 AttributeWithIndex AWI[2];
96 AWI[0] = AttributeWithIndex::get(2, Attribute::NoCapture);
97 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
98 const Type *I8Ptr = B.getInt8PtrTy();
99 Value *StrNCpy = M->getOrInsertFunction("strncpy", AttrListPtr::get(AWI, 2),
101 Len->getType(), NULL);
102 CallInst *CI = B.CreateCall3(StrNCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
104 if (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts()))
105 CI->setCallingConv(F->getCallingConv());
110 /// EmitMemCpy - Emit a call to the memcpy function to the builder. This always
111 /// expects that Len has type 'intptr_t' and Dst/Src are pointers.
112 Value *llvm::EmitMemCpy(Value *Dst, Value *Src, Value *Len,
113 unsigned Align, IRBuilder<> &B, const TargetData *TD) {
114 Module *M = B.GetInsertBlock()->getParent()->getParent();
115 const Type *Ty = Len->getType();
116 Value *MemCpy = Intrinsic::getDeclaration(M, Intrinsic::memcpy, &Ty, 1);
117 Dst = CastToCStr(Dst, B);
118 Src = CastToCStr(Src, B);
119 return B.CreateCall4(MemCpy, Dst, Src, Len,
120 ConstantInt::get(B.getInt32Ty(), Align));
123 /// EmitMemCpyChk - Emit a call to the __memcpy_chk function to the builder.
124 /// This expects that the Len and ObjSize have type 'intptr_t' and Dst/Src
126 Value *llvm::EmitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
127 IRBuilder<> &B, const TargetData *TD) {
128 Module *M = B.GetInsertBlock()->getParent()->getParent();
129 AttributeWithIndex AWI;
130 AWI = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
131 LLVMContext &Context = B.GetInsertBlock()->getContext();
132 Value *MemCpy = M->getOrInsertFunction("__memcpy_chk",
133 AttrListPtr::get(&AWI, 1),
137 TD->getIntPtrType(Context),
138 TD->getIntPtrType(Context), NULL);
139 Dst = CastToCStr(Dst, B);
140 Src = CastToCStr(Src, B);
141 CallInst *CI = B.CreateCall4(MemCpy, Dst, Src, Len, ObjSize);
142 if (const Function *F = dyn_cast<Function>(MemCpy->stripPointerCasts()))
143 CI->setCallingConv(F->getCallingConv());
147 /// EmitMemMove - Emit a call to the memmove function to the builder. This
148 /// always expects that the size has type 'intptr_t' and Dst/Src are pointers.
149 Value *llvm::EmitMemMove(Value *Dst, Value *Src, Value *Len,
150 unsigned Align, IRBuilder<> &B, const TargetData *TD) {
151 Module *M = B.GetInsertBlock()->getParent()->getParent();
152 LLVMContext &Context = B.GetInsertBlock()->getContext();
153 const Type *Ty = TD->getIntPtrType(Context);
154 Value *MemMove = Intrinsic::getDeclaration(M, Intrinsic::memmove, &Ty, 1);
155 Dst = CastToCStr(Dst, B);
156 Src = CastToCStr(Src, B);
157 Value *A = ConstantInt::get(B.getInt32Ty(), Align);
158 return B.CreateCall4(MemMove, Dst, Src, Len, A);
161 /// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
162 /// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
163 Value *llvm::EmitMemChr(Value *Ptr, Value *Val,
164 Value *Len, IRBuilder<> &B, const TargetData *TD) {
165 Module *M = B.GetInsertBlock()->getParent()->getParent();
166 AttributeWithIndex AWI;
167 AWI = AttributeWithIndex::get(~0u, Attribute::ReadOnly | Attribute::NoUnwind);
168 LLVMContext &Context = B.GetInsertBlock()->getContext();
169 Value *MemChr = M->getOrInsertFunction("memchr", AttrListPtr::get(&AWI, 1),
173 TD->getIntPtrType(Context),
175 CallInst *CI = B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr");
177 if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts()))
178 CI->setCallingConv(F->getCallingConv());
183 /// EmitMemCmp - Emit a call to the memcmp function.
184 Value *llvm::EmitMemCmp(Value *Ptr1, Value *Ptr2,
185 Value *Len, IRBuilder<> &B, const TargetData *TD) {
186 Module *M = B.GetInsertBlock()->getParent()->getParent();
187 AttributeWithIndex AWI[3];
188 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
189 AWI[1] = AttributeWithIndex::get(2, Attribute::NoCapture);
190 AWI[2] = AttributeWithIndex::get(~0u, Attribute::ReadOnly |
191 Attribute::NoUnwind);
193 LLVMContext &Context = B.GetInsertBlock()->getContext();
194 Value *MemCmp = M->getOrInsertFunction("memcmp", AttrListPtr::get(AWI, 3),
198 TD->getIntPtrType(Context), NULL);
199 CallInst *CI = B.CreateCall3(MemCmp, CastToCStr(Ptr1, B), CastToCStr(Ptr2, B),
202 if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts()))
203 CI->setCallingConv(F->getCallingConv());
208 /// EmitMemSet - Emit a call to the memset function
209 Value *llvm::EmitMemSet(Value *Dst, Value *Val,
210 Value *Len, IRBuilder<> &B, const TargetData *TD) {
211 Module *M = B.GetInsertBlock()->getParent()->getParent();
212 Intrinsic::ID IID = Intrinsic::memset;
214 Tys[0] = Len->getType();
215 Value *MemSet = Intrinsic::getDeclaration(M, IID, Tys, 1);
216 Value *Align = ConstantInt::get(B.getInt32Ty(), 1);
217 return B.CreateCall4(MemSet, CastToCStr(Dst, B), Val, Len, Align);
220 /// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
221 /// 'floor'). This function is known to take a single of type matching 'Op' and
222 /// returns one value with the same type. If 'Op' is a long double, 'l' is
223 /// added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
224 Value *llvm::EmitUnaryFloatFnCall(Value *Op, const char *Name,
225 IRBuilder<> &B, const AttrListPtr &Attrs) {
227 if (!Op->getType()->isDoubleTy()) {
228 // If we need to add a suffix, copy into NameBuffer.
229 unsigned NameLen = strlen(Name);
230 assert(NameLen < sizeof(NameBuffer)-2);
231 memcpy(NameBuffer, Name, NameLen);
232 if (Op->getType()->isFloatTy())
233 NameBuffer[NameLen] = 'f'; // floorf
235 NameBuffer[NameLen] = 'l'; // floorl
236 NameBuffer[NameLen+1] = 0;
240 Module *M = B.GetInsertBlock()->getParent()->getParent();
241 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
242 Op->getType(), NULL);
243 CallInst *CI = B.CreateCall(Callee, Op, Name);
244 CI->setAttributes(Attrs);
245 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
246 CI->setCallingConv(F->getCallingConv());
251 /// EmitPutChar - Emit a call to the putchar function. This assumes that Char
253 Value *llvm::EmitPutChar(Value *Char, IRBuilder<> &B, const TargetData *TD) {
254 Module *M = B.GetInsertBlock()->getParent()->getParent();
255 Value *PutChar = M->getOrInsertFunction("putchar", B.getInt32Ty(),
256 B.getInt32Ty(), NULL);
257 CallInst *CI = B.CreateCall(PutChar,
258 B.CreateIntCast(Char,
264 if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts()))
265 CI->setCallingConv(F->getCallingConv());
269 /// EmitPutS - Emit a call to the puts function. This assumes that Str is
271 void llvm::EmitPutS(Value *Str, IRBuilder<> &B, const TargetData *TD) {
272 Module *M = B.GetInsertBlock()->getParent()->getParent();
273 AttributeWithIndex AWI[2];
274 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
275 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
277 Value *PutS = M->getOrInsertFunction("puts", AttrListPtr::get(AWI, 2),
281 CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts");
282 if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
283 CI->setCallingConv(F->getCallingConv());
287 /// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
288 /// an integer and File is a pointer to FILE.
289 void llvm::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B,
290 const TargetData *TD) {
291 Module *M = B.GetInsertBlock()->getParent()->getParent();
292 AttributeWithIndex AWI[2];
293 AWI[0] = AttributeWithIndex::get(2, Attribute::NoCapture);
294 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
296 if (File->getType()->isPointerTy())
297 F = M->getOrInsertFunction("fputc", AttrListPtr::get(AWI, 2),
299 B.getInt32Ty(), File->getType(),
302 F = M->getOrInsertFunction("fputc",
305 File->getType(), NULL);
306 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
308 CallInst *CI = B.CreateCall2(F, Char, File, "fputc");
310 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
311 CI->setCallingConv(Fn->getCallingConv());
314 /// EmitFPutS - Emit a call to the puts function. Str is required to be a
315 /// pointer and File is a pointer to FILE.
316 void llvm::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B,
317 const TargetData *TD) {
318 Module *M = B.GetInsertBlock()->getParent()->getParent();
319 AttributeWithIndex AWI[3];
320 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
321 AWI[1] = AttributeWithIndex::get(2, Attribute::NoCapture);
322 AWI[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
324 if (File->getType()->isPointerTy())
325 F = M->getOrInsertFunction("fputs", AttrListPtr::get(AWI, 3),
328 File->getType(), NULL);
330 F = M->getOrInsertFunction("fputs", B.getInt32Ty(),
332 File->getType(), NULL);
333 CallInst *CI = B.CreateCall2(F, CastToCStr(Str, B), File, "fputs");
335 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
336 CI->setCallingConv(Fn->getCallingConv());
339 /// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
340 /// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
341 void llvm::EmitFWrite(Value *Ptr, Value *Size, Value *File,
342 IRBuilder<> &B, const TargetData *TD) {
343 Module *M = B.GetInsertBlock()->getParent()->getParent();
344 AttributeWithIndex AWI[3];
345 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
346 AWI[1] = AttributeWithIndex::get(4, Attribute::NoCapture);
347 AWI[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
348 LLVMContext &Context = B.GetInsertBlock()->getContext();
350 if (File->getType()->isPointerTy())
351 F = M->getOrInsertFunction("fwrite", AttrListPtr::get(AWI, 3),
352 TD->getIntPtrType(Context),
354 TD->getIntPtrType(Context),
355 TD->getIntPtrType(Context),
356 File->getType(), NULL);
358 F = M->getOrInsertFunction("fwrite", TD->getIntPtrType(Context),
360 TD->getIntPtrType(Context),
361 TD->getIntPtrType(Context),
362 File->getType(), NULL);
363 CallInst *CI = B.CreateCall4(F, CastToCStr(Ptr, B), Size,
364 ConstantInt::get(TD->getIntPtrType(Context), 1), File);
366 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
367 CI->setCallingConv(Fn->getCallingConv());
370 SimplifyFortifiedLibCalls::~SimplifyFortifiedLibCalls() { }
372 bool SimplifyFortifiedLibCalls::fold(CallInst *CI, const TargetData *TD) {
374 StringRef Name = CI->getCalledFunction()->getName();
375 BasicBlock *BB = CI->getParent();
376 IRBuilder<> B(CI->getParent()->getContext());
378 // Set the builder to the instruction after the call.
379 B.SetInsertPoint(BB, CI);
381 if (Name == "__memcpy_chk") {
382 if (isFoldable(4, 3, false)) {
383 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3),
385 replaceCall(CI->getOperand(1));
391 // Should be similar to memcpy.
392 if (Name == "__mempcpy_chk") {
396 if (Name == "__memmove_chk") {
397 if (isFoldable(4, 3, false)) {
398 EmitMemMove(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3),
400 replaceCall(CI->getOperand(1));
406 if (Name == "__memset_chk") {
407 if (isFoldable(4, 3, false)) {
408 Value *Val = B.CreateIntCast(CI->getOperand(2), B.getInt8Ty(),
410 EmitMemSet(CI->getOperand(1), Val, CI->getOperand(3), B, TD);
411 replaceCall(CI->getOperand(1));
417 if (Name == "__strcpy_chk" || Name == "__stpcpy_chk") {
418 // If a) we don't have any length information, or b) we know this will
419 // fit then just lower to a plain st[rp]cpy. Otherwise we'll keep our
420 // st[rp]cpy_chk call which may fail at runtime if the size is too long.
421 // TODO: It might be nice to get a maximum length out of the possible
422 // string lengths for varying.
423 if (isFoldable(3, 2, true)) {
424 Value *Ret = EmitStrCpy(CI->getOperand(1), CI->getOperand(2), B, TD,
432 if (Name == "__strncpy_chk") {
433 if (isFoldable(4, 3, false)) {
434 Value *Ret = EmitStrNCpy(CI->getOperand(1), CI->getOperand(2),
435 CI->getOperand(3), B, TD);
442 if (Name == "__strcat_chk") {
446 if (Name == "__strncat_chk") {