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) {
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("strcpy", 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 the size 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 /// EmitMemMove - Emit a call to the memmove function to the builder. This
124 /// always expects that the size has type 'intptr_t' and Dst/Src are pointers.
125 Value *llvm::EmitMemMove(Value *Dst, Value *Src, Value *Len,
126 unsigned Align, IRBuilder<> &B, const TargetData *TD) {
127 Module *M = B.GetInsertBlock()->getParent()->getParent();
128 LLVMContext &Context = B.GetInsertBlock()->getContext();
129 const Type *Ty = TD->getIntPtrType(Context);
130 Value *MemMove = Intrinsic::getDeclaration(M, Intrinsic::memmove, &Ty, 1);
131 Dst = CastToCStr(Dst, B);
132 Src = CastToCStr(Src, B);
133 Value *A = ConstantInt::get(B.getInt32Ty(), Align);
134 return B.CreateCall4(MemMove, Dst, Src, Len, A);
137 /// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
138 /// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
139 Value *llvm::EmitMemChr(Value *Ptr, Value *Val,
140 Value *Len, IRBuilder<> &B, const TargetData *TD) {
141 Module *M = B.GetInsertBlock()->getParent()->getParent();
142 AttributeWithIndex AWI;
143 AWI = AttributeWithIndex::get(~0u, Attribute::ReadOnly | Attribute::NoUnwind);
144 LLVMContext &Context = B.GetInsertBlock()->getContext();
145 Value *MemChr = M->getOrInsertFunction("memchr", AttrListPtr::get(&AWI, 1),
149 TD->getIntPtrType(Context),
151 CallInst *CI = B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr");
153 if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts()))
154 CI->setCallingConv(F->getCallingConv());
159 /// EmitMemCmp - Emit a call to the memcmp function.
160 Value *llvm::EmitMemCmp(Value *Ptr1, Value *Ptr2,
161 Value *Len, IRBuilder<> &B, const TargetData *TD) {
162 Module *M = B.GetInsertBlock()->getParent()->getParent();
163 AttributeWithIndex AWI[3];
164 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
165 AWI[1] = AttributeWithIndex::get(2, Attribute::NoCapture);
166 AWI[2] = AttributeWithIndex::get(~0u, Attribute::ReadOnly |
167 Attribute::NoUnwind);
169 LLVMContext &Context = B.GetInsertBlock()->getContext();
170 Value *MemCmp = M->getOrInsertFunction("memcmp", AttrListPtr::get(AWI, 3),
174 TD->getIntPtrType(Context), NULL);
175 CallInst *CI = B.CreateCall3(MemCmp, CastToCStr(Ptr1, B), CastToCStr(Ptr2, B),
178 if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts()))
179 CI->setCallingConv(F->getCallingConv());
184 /// EmitMemSet - Emit a call to the memset function
185 Value *llvm::EmitMemSet(Value *Dst, Value *Val,
186 Value *Len, IRBuilder<> &B, const TargetData *TD) {
187 Module *M = B.GetInsertBlock()->getParent()->getParent();
188 Intrinsic::ID IID = Intrinsic::memset;
190 Tys[0] = Len->getType();
191 Value *MemSet = Intrinsic::getDeclaration(M, IID, Tys, 1);
192 Value *Align = ConstantInt::get(B.getInt32Ty(), 1);
193 return B.CreateCall4(MemSet, CastToCStr(Dst, B), Val, Len, Align);
196 /// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
197 /// 'floor'). This function is known to take a single of type matching 'Op' and
198 /// returns one value with the same type. If 'Op' is a long double, 'l' is
199 /// added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
200 Value *llvm::EmitUnaryFloatFnCall(Value *Op, const char *Name,
201 IRBuilder<> &B, const AttrListPtr &Attrs) {
203 if (!Op->getType()->isDoubleTy()) {
204 // If we need to add a suffix, copy into NameBuffer.
205 unsigned NameLen = strlen(Name);
206 assert(NameLen < sizeof(NameBuffer)-2);
207 memcpy(NameBuffer, Name, NameLen);
208 if (Op->getType()->isFloatTy())
209 NameBuffer[NameLen] = 'f'; // floorf
211 NameBuffer[NameLen] = 'l'; // floorl
212 NameBuffer[NameLen+1] = 0;
216 Module *M = B.GetInsertBlock()->getParent()->getParent();
217 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
218 Op->getType(), NULL);
219 CallInst *CI = B.CreateCall(Callee, Op, Name);
220 CI->setAttributes(Attrs);
221 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
222 CI->setCallingConv(F->getCallingConv());
227 /// EmitPutChar - Emit a call to the putchar function. This assumes that Char
229 Value *llvm::EmitPutChar(Value *Char, IRBuilder<> &B, const TargetData *TD) {
230 Module *M = B.GetInsertBlock()->getParent()->getParent();
231 Value *PutChar = M->getOrInsertFunction("putchar", B.getInt32Ty(),
232 B.getInt32Ty(), NULL);
233 CallInst *CI = B.CreateCall(PutChar,
234 B.CreateIntCast(Char,
240 if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts()))
241 CI->setCallingConv(F->getCallingConv());
245 /// EmitPutS - Emit a call to the puts function. This assumes that Str is
247 void llvm::EmitPutS(Value *Str, IRBuilder<> &B, const TargetData *TD) {
248 Module *M = B.GetInsertBlock()->getParent()->getParent();
249 AttributeWithIndex AWI[2];
250 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
251 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
253 Value *PutS = M->getOrInsertFunction("puts", AttrListPtr::get(AWI, 2),
257 CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts");
258 if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
259 CI->setCallingConv(F->getCallingConv());
263 /// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
264 /// an integer and File is a pointer to FILE.
265 void llvm::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B,
266 const TargetData *TD) {
267 Module *M = B.GetInsertBlock()->getParent()->getParent();
268 AttributeWithIndex AWI[2];
269 AWI[0] = AttributeWithIndex::get(2, Attribute::NoCapture);
270 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
272 if (File->getType()->isPointerTy())
273 F = M->getOrInsertFunction("fputc", AttrListPtr::get(AWI, 2),
275 B.getInt32Ty(), File->getType(),
278 F = M->getOrInsertFunction("fputc",
281 File->getType(), NULL);
282 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
284 CallInst *CI = B.CreateCall2(F, Char, File, "fputc");
286 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
287 CI->setCallingConv(Fn->getCallingConv());
290 /// EmitFPutS - Emit a call to the puts function. Str is required to be a
291 /// pointer and File is a pointer to FILE.
292 void llvm::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B,
293 const TargetData *TD) {
294 Module *M = B.GetInsertBlock()->getParent()->getParent();
295 AttributeWithIndex AWI[3];
296 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
297 AWI[1] = AttributeWithIndex::get(2, Attribute::NoCapture);
298 AWI[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
300 if (File->getType()->isPointerTy())
301 F = M->getOrInsertFunction("fputs", AttrListPtr::get(AWI, 3),
304 File->getType(), NULL);
306 F = M->getOrInsertFunction("fputs", B.getInt32Ty(),
308 File->getType(), NULL);
309 CallInst *CI = B.CreateCall2(F, CastToCStr(Str, B), File, "fputs");
311 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
312 CI->setCallingConv(Fn->getCallingConv());
315 /// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
316 /// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
317 void llvm::EmitFWrite(Value *Ptr, Value *Size, Value *File,
318 IRBuilder<> &B, const TargetData *TD) {
319 Module *M = B.GetInsertBlock()->getParent()->getParent();
320 AttributeWithIndex AWI[3];
321 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
322 AWI[1] = AttributeWithIndex::get(4, Attribute::NoCapture);
323 AWI[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
324 LLVMContext &Context = B.GetInsertBlock()->getContext();
326 if (File->getType()->isPointerTy())
327 F = M->getOrInsertFunction("fwrite", AttrListPtr::get(AWI, 3),
328 TD->getIntPtrType(Context),
330 TD->getIntPtrType(Context),
331 TD->getIntPtrType(Context),
332 File->getType(), NULL);
334 F = M->getOrInsertFunction("fwrite", TD->getIntPtrType(Context),
336 TD->getIntPtrType(Context),
337 TD->getIntPtrType(Context),
338 File->getType(), NULL);
339 CallInst *CI = B.CreateCall4(F, CastToCStr(Ptr, B), Size,
340 ConstantInt::get(TD->getIntPtrType(Context), 1), File);
342 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
343 CI->setCallingConv(Fn->getCallingConv());