BuildLibCalls.cpp revision bd973762003329c217eec2439071e9f93af3eea5
1fadc83c699d16e07747b1ceca7fbb01390fdcb74Chris Lattner//===- BuildLibCalls.cpp - Utility builder for libcalls -------------------===//
2fadc83c699d16e07747b1ceca7fbb01390fdcb74Chris Lattner//
3fadc83c699d16e07747b1ceca7fbb01390fdcb74Chris Lattner//                     The LLVM Compiler Infrastructure
4fadc83c699d16e07747b1ceca7fbb01390fdcb74Chris Lattner//
5fadc83c699d16e07747b1ceca7fbb01390fdcb74Chris Lattner// This file is distributed under the University of Illinois Open Source
6fadc83c699d16e07747b1ceca7fbb01390fdcb74Chris Lattner// License. See LICENSE.TXT for details.
7fadc83c699d16e07747b1ceca7fbb01390fdcb74Chris Lattner//
8fadc83c699d16e07747b1ceca7fbb01390fdcb74Chris Lattner//===----------------------------------------------------------------------===//
9fadc83c699d16e07747b1ceca7fbb01390fdcb74Chris Lattner//
10fadc83c699d16e07747b1ceca7fbb01390fdcb74Chris Lattner// This file implements some functions that will create standard C libcalls.
11fadc83c699d16e07747b1ceca7fbb01390fdcb74Chris Lattner//
12fadc83c699d16e07747b1ceca7fbb01390fdcb74Chris Lattner//===----------------------------------------------------------------------===//
13fadc83c699d16e07747b1ceca7fbb01390fdcb74Chris Lattner
14fadc83c699d16e07747b1ceca7fbb01390fdcb74Chris Lattner#include "llvm/Transforms/Utils/BuildLibCalls.h"
15fadc83c699d16e07747b1ceca7fbb01390fdcb74Chris Lattner#include "llvm/Type.h"
16fadc83c699d16e07747b1ceca7fbb01390fdcb74Chris Lattner#include "llvm/Constants.h"
17fadc83c699d16e07747b1ceca7fbb01390fdcb74Chris Lattner#include "llvm/Function.h"
18d5fb7906130989a579d1bfe4490b414331e94feeChris Lattner#include "llvm/Module.h"
19fadc83c699d16e07747b1ceca7fbb01390fdcb74Chris Lattner#include "llvm/Support/IRBuilder.h"
20fadc83c699d16e07747b1ceca7fbb01390fdcb74Chris Lattner#include "llvm/Target/TargetData.h"
21d5fb7906130989a579d1bfe4490b414331e94feeChris Lattner#include "llvm/LLVMContext.h"
22d5fb7906130989a579d1bfe4490b414331e94feeChris Lattner#include "llvm/Intrinsics.h"
23d5fb7906130989a579d1bfe4490b414331e94feeChris Lattner
24d5fb7906130989a579d1bfe4490b414331e94feeChris Lattnerusing namespace llvm;
25d5fb7906130989a579d1bfe4490b414331e94feeChris Lattner
26d5fb7906130989a579d1bfe4490b414331e94feeChris Lattner/// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
27d5fb7906130989a579d1bfe4490b414331e94feeChris LattnerValue *llvm::CastToCStr(Value *V, IRBuilder<> &B) {
28d5fb7906130989a579d1bfe4490b414331e94feeChris Lattner  return B.CreateBitCast(V, B.getInt8PtrTy(), "cstr");
29d5fb7906130989a579d1bfe4490b414331e94feeChris Lattner}
30d5fb7906130989a579d1bfe4490b414331e94feeChris Lattner
31d5fb7906130989a579d1bfe4490b414331e94feeChris Lattner/// EmitStrLen - Emit a call to the strlen function to the builder, for the
32d5fb7906130989a579d1bfe4490b414331e94feeChris Lattner/// specified pointer.  This always returns an integer value of size intptr_t.
33d5fb7906130989a579d1bfe4490b414331e94feeChris LattnerValue *llvm::EmitStrLen(Value *Ptr, IRBuilder<> &B, const TargetData *TD) {
34d5fb7906130989a579d1bfe4490b414331e94feeChris Lattner  Module *M = B.GetInsertBlock()->getParent()->getParent();
35d5fb7906130989a579d1bfe4490b414331e94feeChris Lattner  AttributeWithIndex AWI[2];
36d5fb7906130989a579d1bfe4490b414331e94feeChris Lattner  AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
37d5fb7906130989a579d1bfe4490b414331e94feeChris Lattner  AWI[1] = AttributeWithIndex::get(~0u, Attribute::ReadOnly |
38d5fb7906130989a579d1bfe4490b414331e94feeChris Lattner                                   Attribute::NoUnwind);
39d5fb7906130989a579d1bfe4490b414331e94feeChris Lattner
40d5fb7906130989a579d1bfe4490b414331e94feeChris Lattner  LLVMContext &Context = B.GetInsertBlock()->getContext();
41d5fb7906130989a579d1bfe4490b414331e94feeChris Lattner  Constant *StrLen = M->getOrInsertFunction("strlen", AttrListPtr::get(AWI, 2),
42d5fb7906130989a579d1bfe4490b414331e94feeChris Lattner                                            TD->getIntPtrType(Context),
43d5fb7906130989a579d1bfe4490b414331e94feeChris Lattner                                            B.getInt8PtrTy(),
44d5fb7906130989a579d1bfe4490b414331e94feeChris Lattner                                            NULL);
45d5fb7906130989a579d1bfe4490b414331e94feeChris Lattner  CallInst *CI = B.CreateCall(StrLen, CastToCStr(Ptr, B), "strlen");
46d5fb7906130989a579d1bfe4490b414331e94feeChris Lattner  if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts()))
47d5fb7906130989a579d1bfe4490b414331e94feeChris Lattner    CI->setCallingConv(F->getCallingConv());
48d5fb7906130989a579d1bfe4490b414331e94feeChris Lattner
49d5fb7906130989a579d1bfe4490b414331e94feeChris Lattner  return CI;
50d5fb7906130989a579d1bfe4490b414331e94feeChris Lattner}
51d5fb7906130989a579d1bfe4490b414331e94feeChris Lattner
52d5fb7906130989a579d1bfe4490b414331e94feeChris Lattner/// EmitStrChr - Emit a call to the strchr function to the builder, for the
53d5fb7906130989a579d1bfe4490b414331e94feeChris Lattner/// specified pointer and character.  Ptr is required to be some pointer type,
54d5fb7906130989a579d1bfe4490b414331e94feeChris Lattner/// and the return value has 'i8*' type.
55d5fb7906130989a579d1bfe4490b414331e94feeChris LattnerValue *llvm::EmitStrChr(Value *Ptr, char C, IRBuilder<> &B,
56d5fb7906130989a579d1bfe4490b414331e94feeChris Lattner                        const TargetData *TD) {
57d5fb7906130989a579d1bfe4490b414331e94feeChris Lattner  Module *M = B.GetInsertBlock()->getParent()->getParent();
58d5fb7906130989a579d1bfe4490b414331e94feeChris Lattner  AttributeWithIndex AWI =
59d5fb7906130989a579d1bfe4490b414331e94feeChris Lattner    AttributeWithIndex::get(~0u, Attribute::ReadOnly | Attribute::NoUnwind);
60d5fb7906130989a579d1bfe4490b414331e94feeChris Lattner
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
90/// EmitStrNCpy - Emit a call to the strcpy function to the builder, for the
91/// specified pointer arguments.
92Value *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),
100                                         I8Ptr, I8Ptr, I8Ptr,
101                                         Len->getType(), NULL);
102  CallInst *CI = B.CreateCall3(StrNCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
103                               Len, "strncpy");
104  if (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts()))
105    CI->setCallingConv(F->getCallingConv());
106  return CI;
107}
108
109
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.
112Value *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));
121}
122
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.
125Value *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);
135}
136
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.
139Value *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),
146                                         B.getInt8PtrTy(),
147                                         B.getInt8PtrTy(),
148                                         B.getInt32Ty(),
149                                         TD->getIntPtrType(Context),
150                                         NULL);
151  CallInst *CI = B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr");
152
153  if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts()))
154    CI->setCallingConv(F->getCallingConv());
155
156  return CI;
157}
158
159/// EmitMemCmp - Emit a call to the memcmp function.
160Value *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);
168
169  LLVMContext &Context = B.GetInsertBlock()->getContext();
170  Value *MemCmp = M->getOrInsertFunction("memcmp", AttrListPtr::get(AWI, 3),
171                                         B.getInt32Ty(),
172                                         B.getInt8PtrTy(),
173                                         B.getInt8PtrTy(),
174                                         TD->getIntPtrType(Context), NULL);
175  CallInst *CI = B.CreateCall3(MemCmp, CastToCStr(Ptr1, B), CastToCStr(Ptr2, B),
176                               Len, "memcmp");
177
178  if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts()))
179    CI->setCallingConv(F->getCallingConv());
180
181  return CI;
182}
183
184/// EmitMemSet - Emit a call to the memset function
185Value *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;
189 const Type *Tys[1];
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);
194}
195
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.
200Value *llvm::EmitUnaryFloatFnCall(Value *Op, const char *Name,
201                                  IRBuilder<> &B, const AttrListPtr &Attrs) {
202  char NameBuffer[20];
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
210    else
211      NameBuffer[NameLen] = 'l';  // floorl
212    NameBuffer[NameLen+1] = 0;
213    Name = NameBuffer;
214  }
215
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());
223
224  return CI;
225}
226
227/// EmitPutChar - Emit a call to the putchar function.  This assumes that Char
228/// is an integer.
229Value *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,
235                              B.getInt32Ty(),
236                              /*isSigned*/true,
237                              "chari"),
238                              "putchar");
239
240  if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts()))
241    CI->setCallingConv(F->getCallingConv());
242  return CI;
243}
244
245/// EmitPutS - Emit a call to the puts function.  This assumes that Str is
246/// some pointer.
247void 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);
252
253  Value *PutS = M->getOrInsertFunction("puts", AttrListPtr::get(AWI, 2),
254                                       B.getInt32Ty(),
255                                       B.getInt8PtrTy(),
256                                       NULL);
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());
260
261}
262
263/// EmitFPutC - Emit a call to the fputc function.  This assumes that Char is
264/// an integer and File is a pointer to FILE.
265void 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);
271  Constant *F;
272  if (File->getType()->isPointerTy())
273    F = M->getOrInsertFunction("fputc", AttrListPtr::get(AWI, 2),
274                               B.getInt32Ty(),
275                               B.getInt32Ty(), File->getType(),
276                               NULL);
277  else
278    F = M->getOrInsertFunction("fputc",
279                               B.getInt32Ty(),
280                               B.getInt32Ty(),
281                               File->getType(), NULL);
282  Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
283                         "chari");
284  CallInst *CI = B.CreateCall2(F, Char, File, "fputc");
285
286  if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
287    CI->setCallingConv(Fn->getCallingConv());
288}
289
290/// EmitFPutS - Emit a call to the puts function.  Str is required to be a
291/// pointer and File is a pointer to FILE.
292void 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);
299  Constant *F;
300  if (File->getType()->isPointerTy())
301    F = M->getOrInsertFunction("fputs", AttrListPtr::get(AWI, 3),
302                               B.getInt32Ty(),
303                               B.getInt8PtrTy(),
304                               File->getType(), NULL);
305  else
306    F = M->getOrInsertFunction("fputs", B.getInt32Ty(),
307                               B.getInt8PtrTy(),
308                               File->getType(), NULL);
309  CallInst *CI = B.CreateCall2(F, CastToCStr(Str, B), File, "fputs");
310
311  if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
312    CI->setCallingConv(Fn->getCallingConv());
313}
314
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.
317void 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();
325  Constant *F;
326  if (File->getType()->isPointerTy())
327    F = M->getOrInsertFunction("fwrite", AttrListPtr::get(AWI, 3),
328                               TD->getIntPtrType(Context),
329                               B.getInt8PtrTy(),
330                               TD->getIntPtrType(Context),
331                               TD->getIntPtrType(Context),
332                               File->getType(), NULL);
333  else
334    F = M->getOrInsertFunction("fwrite", TD->getIntPtrType(Context),
335                               B.getInt8PtrTy(),
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);
341
342  if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
343    CI->setCallingConv(Fn->getCallingConv());
344}
345