blob: 7f62166f2e2dfa88262059f662eb972a3c99343e [file] [log] [blame]
Eric Christopherb6174e32010-03-05 22:25:30 +00001//===- BuildLibCalls.cpp - Utility builder for libcalls -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements some functions that will create standard C libcalls.
11//
12//===----------------------------------------------------------------------===//
13
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"
23
24using namespace llvm;
25
26/// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
27Value *llvm::CastToCStr(Value *V, IRBuilder<> &B) {
28 return B.CreateBitCast(V, B.getInt8PtrTy(), "cstr");
29}
30
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.
33Value *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 |
38 Attribute::NoUnwind);
39
40 LLVMContext &Context = B.GetInsertBlock()->getContext();
41 Constant *StrLen = M->getOrInsertFunction("strlen", AttrListPtr::get(AWI, 2),
42 TD->getIntPtrType(Context),
43 B.getInt8PtrTy(),
44 NULL);
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());
48
49 return CI;
50}
51
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.
55Value *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);
60
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());
69 return CI;
70}
71
72/// EmitStrCpy - Emit a call to the strcpy function to the builder, for the
73/// specified pointer arguments.
74Value *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),
84 "strcpy");
85 if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts()))
86 CI->setCallingConv(F->getCallingConv());
87 return CI;
88}
89
Eric Christopher19888ca2010-03-11 19:24:34 +000090/// EmitStpCpy - Emit a call to the stpcpy function to the builder, for the
91/// specified pointer arguments.
92Value *llvm::EmitStpCpy(Value *Dst, Value *Src, IRBuilder<> &B,
93 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 *StpCpy = M->getOrInsertFunction("stpcpy", AttrListPtr::get(AWI, 2),
100 I8Ptr, I8Ptr, I8Ptr, NULL);
101 CallInst *CI = B.CreateCall2(StpCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
102 "stpcpy");
103 if (const Function *F = dyn_cast<Function>(StpCpy->stripPointerCasts()))
104 CI->setCallingConv(F->getCallingConv());
105 return CI;
106}
107
Eric Christopherb0722af2010-03-11 17:45:38 +0000108/// EmitStrNCpy - Emit a call to the strncpy function to the builder, for the
Eric Christopherbd973762010-03-11 01:25:07 +0000109/// specified pointer arguments.
110Value *llvm::EmitStrNCpy(Value *Dst, Value *Src, Value *Len,
111 IRBuilder<> &B, const TargetData *TD) {
112 Module *M = B.GetInsertBlock()->getParent()->getParent();
113 AttributeWithIndex AWI[2];
114 AWI[0] = AttributeWithIndex::get(2, Attribute::NoCapture);
115 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
116 const Type *I8Ptr = B.getInt8PtrTy();
117 Value *StrNCpy = M->getOrInsertFunction("strncpy", AttrListPtr::get(AWI, 2),
118 I8Ptr, I8Ptr, I8Ptr,
119 Len->getType(), NULL);
120 CallInst *CI = B.CreateCall3(StrNCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
121 Len, "strncpy");
122 if (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts()))
123 CI->setCallingConv(F->getCallingConv());
124 return CI;
125}
126
127
Eric Christopherb6174e32010-03-05 22:25:30 +0000128/// EmitMemCpy - Emit a call to the memcpy function to the builder. This always
129/// expects that the size has type 'intptr_t' and Dst/Src are pointers.
130Value *llvm::EmitMemCpy(Value *Dst, Value *Src, Value *Len,
131 unsigned Align, IRBuilder<> &B, const TargetData *TD) {
132 Module *M = B.GetInsertBlock()->getParent()->getParent();
133 const Type *Ty = Len->getType();
134 Value *MemCpy = Intrinsic::getDeclaration(M, Intrinsic::memcpy, &Ty, 1);
135 Dst = CastToCStr(Dst, B);
136 Src = CastToCStr(Src, B);
137 return B.CreateCall4(MemCpy, Dst, Src, Len,
138 ConstantInt::get(B.getInt32Ty(), Align));
139}
140
141/// EmitMemMove - Emit a call to the memmove function to the builder. This
142/// always expects that the size has type 'intptr_t' and Dst/Src are pointers.
143Value *llvm::EmitMemMove(Value *Dst, Value *Src, Value *Len,
144 unsigned Align, IRBuilder<> &B, const TargetData *TD) {
145 Module *M = B.GetInsertBlock()->getParent()->getParent();
146 LLVMContext &Context = B.GetInsertBlock()->getContext();
147 const Type *Ty = TD->getIntPtrType(Context);
148 Value *MemMove = Intrinsic::getDeclaration(M, Intrinsic::memmove, &Ty, 1);
149 Dst = CastToCStr(Dst, B);
150 Src = CastToCStr(Src, B);
151 Value *A = ConstantInt::get(B.getInt32Ty(), Align);
152 return B.CreateCall4(MemMove, Dst, Src, Len, A);
153}
154
155/// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
156/// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
157Value *llvm::EmitMemChr(Value *Ptr, Value *Val,
158 Value *Len, IRBuilder<> &B, const TargetData *TD) {
159 Module *M = B.GetInsertBlock()->getParent()->getParent();
160 AttributeWithIndex AWI;
161 AWI = AttributeWithIndex::get(~0u, Attribute::ReadOnly | Attribute::NoUnwind);
162 LLVMContext &Context = B.GetInsertBlock()->getContext();
163 Value *MemChr = M->getOrInsertFunction("memchr", AttrListPtr::get(&AWI, 1),
164 B.getInt8PtrTy(),
165 B.getInt8PtrTy(),
166 B.getInt32Ty(),
167 TD->getIntPtrType(Context),
168 NULL);
169 CallInst *CI = B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr");
170
171 if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts()))
172 CI->setCallingConv(F->getCallingConv());
173
174 return CI;
175}
176
177/// EmitMemCmp - Emit a call to the memcmp function.
178Value *llvm::EmitMemCmp(Value *Ptr1, Value *Ptr2,
179 Value *Len, IRBuilder<> &B, const TargetData *TD) {
180 Module *M = B.GetInsertBlock()->getParent()->getParent();
181 AttributeWithIndex AWI[3];
182 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
183 AWI[1] = AttributeWithIndex::get(2, Attribute::NoCapture);
184 AWI[2] = AttributeWithIndex::get(~0u, Attribute::ReadOnly |
185 Attribute::NoUnwind);
186
187 LLVMContext &Context = B.GetInsertBlock()->getContext();
188 Value *MemCmp = M->getOrInsertFunction("memcmp", AttrListPtr::get(AWI, 3),
189 B.getInt32Ty(),
190 B.getInt8PtrTy(),
191 B.getInt8PtrTy(),
192 TD->getIntPtrType(Context), NULL);
193 CallInst *CI = B.CreateCall3(MemCmp, CastToCStr(Ptr1, B), CastToCStr(Ptr2, B),
194 Len, "memcmp");
195
196 if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts()))
197 CI->setCallingConv(F->getCallingConv());
198
199 return CI;
200}
201
202/// EmitMemSet - Emit a call to the memset function
203Value *llvm::EmitMemSet(Value *Dst, Value *Val,
204 Value *Len, IRBuilder<> &B, const TargetData *TD) {
205 Module *M = B.GetInsertBlock()->getParent()->getParent();
206 Intrinsic::ID IID = Intrinsic::memset;
207 const Type *Tys[1];
208 Tys[0] = Len->getType();
209 Value *MemSet = Intrinsic::getDeclaration(M, IID, Tys, 1);
210 Value *Align = ConstantInt::get(B.getInt32Ty(), 1);
211 return B.CreateCall4(MemSet, CastToCStr(Dst, B), Val, Len, Align);
212}
213
214/// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
215/// 'floor'). This function is known to take a single of type matching 'Op' and
216/// returns one value with the same type. If 'Op' is a long double, 'l' is
217/// added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
218Value *llvm::EmitUnaryFloatFnCall(Value *Op, const char *Name,
219 IRBuilder<> &B, const AttrListPtr &Attrs) {
220 char NameBuffer[20];
221 if (!Op->getType()->isDoubleTy()) {
222 // If we need to add a suffix, copy into NameBuffer.
223 unsigned NameLen = strlen(Name);
224 assert(NameLen < sizeof(NameBuffer)-2);
225 memcpy(NameBuffer, Name, NameLen);
226 if (Op->getType()->isFloatTy())
227 NameBuffer[NameLen] = 'f'; // floorf
228 else
229 NameBuffer[NameLen] = 'l'; // floorl
230 NameBuffer[NameLen+1] = 0;
231 Name = NameBuffer;
232 }
233
234 Module *M = B.GetInsertBlock()->getParent()->getParent();
235 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
236 Op->getType(), NULL);
237 CallInst *CI = B.CreateCall(Callee, Op, Name);
238 CI->setAttributes(Attrs);
239 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
240 CI->setCallingConv(F->getCallingConv());
241
242 return CI;
243}
244
245/// EmitPutChar - Emit a call to the putchar function. This assumes that Char
246/// is an integer.
247Value *llvm::EmitPutChar(Value *Char, IRBuilder<> &B, const TargetData *TD) {
248 Module *M = B.GetInsertBlock()->getParent()->getParent();
249 Value *PutChar = M->getOrInsertFunction("putchar", B.getInt32Ty(),
250 B.getInt32Ty(), NULL);
251 CallInst *CI = B.CreateCall(PutChar,
252 B.CreateIntCast(Char,
253 B.getInt32Ty(),
254 /*isSigned*/true,
255 "chari"),
256 "putchar");
257
258 if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts()))
259 CI->setCallingConv(F->getCallingConv());
260 return CI;
261}
262
263/// EmitPutS - Emit a call to the puts function. This assumes that Str is
264/// some pointer.
265void llvm::EmitPutS(Value *Str, IRBuilder<> &B, const TargetData *TD) {
266 Module *M = B.GetInsertBlock()->getParent()->getParent();
267 AttributeWithIndex AWI[2];
268 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
269 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
270
271 Value *PutS = M->getOrInsertFunction("puts", AttrListPtr::get(AWI, 2),
272 B.getInt32Ty(),
273 B.getInt8PtrTy(),
274 NULL);
275 CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts");
276 if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
277 CI->setCallingConv(F->getCallingConv());
278
279}
280
281/// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
282/// an integer and File is a pointer to FILE.
283void llvm::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B,
284 const TargetData *TD) {
285 Module *M = B.GetInsertBlock()->getParent()->getParent();
286 AttributeWithIndex AWI[2];
287 AWI[0] = AttributeWithIndex::get(2, Attribute::NoCapture);
288 AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
289 Constant *F;
290 if (File->getType()->isPointerTy())
291 F = M->getOrInsertFunction("fputc", AttrListPtr::get(AWI, 2),
292 B.getInt32Ty(),
293 B.getInt32Ty(), File->getType(),
294 NULL);
295 else
296 F = M->getOrInsertFunction("fputc",
297 B.getInt32Ty(),
298 B.getInt32Ty(),
299 File->getType(), NULL);
300 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
301 "chari");
302 CallInst *CI = B.CreateCall2(F, Char, File, "fputc");
303
304 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
305 CI->setCallingConv(Fn->getCallingConv());
306}
307
308/// EmitFPutS - Emit a call to the puts function. Str is required to be a
309/// pointer and File is a pointer to FILE.
310void llvm::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B,
311 const TargetData *TD) {
312 Module *M = B.GetInsertBlock()->getParent()->getParent();
313 AttributeWithIndex AWI[3];
314 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
315 AWI[1] = AttributeWithIndex::get(2, Attribute::NoCapture);
316 AWI[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
317 Constant *F;
318 if (File->getType()->isPointerTy())
319 F = M->getOrInsertFunction("fputs", AttrListPtr::get(AWI, 3),
320 B.getInt32Ty(),
321 B.getInt8PtrTy(),
322 File->getType(), NULL);
323 else
324 F = M->getOrInsertFunction("fputs", B.getInt32Ty(),
325 B.getInt8PtrTy(),
326 File->getType(), NULL);
327 CallInst *CI = B.CreateCall2(F, CastToCStr(Str, B), File, "fputs");
328
329 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
330 CI->setCallingConv(Fn->getCallingConv());
331}
332
333/// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
334/// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
335void llvm::EmitFWrite(Value *Ptr, Value *Size, Value *File,
336 IRBuilder<> &B, const TargetData *TD) {
337 Module *M = B.GetInsertBlock()->getParent()->getParent();
338 AttributeWithIndex AWI[3];
339 AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
340 AWI[1] = AttributeWithIndex::get(4, Attribute::NoCapture);
341 AWI[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
342 LLVMContext &Context = B.GetInsertBlock()->getContext();
343 Constant *F;
344 if (File->getType()->isPointerTy())
345 F = M->getOrInsertFunction("fwrite", AttrListPtr::get(AWI, 3),
346 TD->getIntPtrType(Context),
347 B.getInt8PtrTy(),
348 TD->getIntPtrType(Context),
349 TD->getIntPtrType(Context),
350 File->getType(), NULL);
351 else
352 F = M->getOrInsertFunction("fwrite", TD->getIntPtrType(Context),
353 B.getInt8PtrTy(),
354 TD->getIntPtrType(Context),
355 TD->getIntPtrType(Context),
356 File->getType(), NULL);
357 CallInst *CI = B.CreateCall4(F, CastToCStr(Ptr, B), Size,
358 ConstantInt::get(TD->getIntPtrType(Context), 1), File);
359
360 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
361 CI->setCallingConv(Fn->getCallingConv());
362}