Avoid using DIDescriptor.isNull().
[oota-llvm.git] / lib / Transforms / Utils / BuildLibCalls.cpp
1 //===- 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
24 using namespace llvm;
25
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");
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.
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 |
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.
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);
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.
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),
84                                "strcpy");
85   if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts()))
86     CI->setCallingConv(F->getCallingConv());
87   return CI;
88 }
89
90 /// EmitMemCpy - Emit a call to the memcpy function to the builder.  This always
91 /// expects that the size has type 'intptr_t' and Dst/Src are pointers.
92 Value *llvm::EmitMemCpy(Value *Dst, Value *Src, Value *Len,
93                         unsigned Align, IRBuilder<> &B, const TargetData *TD) {
94   Module *M = B.GetInsertBlock()->getParent()->getParent();
95   const Type *Ty = Len->getType();
96   Value *MemCpy = Intrinsic::getDeclaration(M, Intrinsic::memcpy, &Ty, 1);
97   Dst = CastToCStr(Dst, B);
98   Src = CastToCStr(Src, B);
99   return B.CreateCall4(MemCpy, Dst, Src, Len,
100                        ConstantInt::get(B.getInt32Ty(), Align));
101 }
102
103 /// EmitMemMove - Emit a call to the memmove function to the builder.  This
104 /// always expects that the size has type 'intptr_t' and Dst/Src are pointers.
105 Value *llvm::EmitMemMove(Value *Dst, Value *Src, Value *Len,
106                                                        unsigned Align, IRBuilder<> &B, const TargetData *TD) {
107   Module *M = B.GetInsertBlock()->getParent()->getParent();
108   LLVMContext &Context = B.GetInsertBlock()->getContext();
109   const Type *Ty = TD->getIntPtrType(Context);
110   Value *MemMove = Intrinsic::getDeclaration(M, Intrinsic::memmove, &Ty, 1);
111   Dst = CastToCStr(Dst, B);
112   Src = CastToCStr(Src, B);
113   Value *A = ConstantInt::get(B.getInt32Ty(), Align);
114   return B.CreateCall4(MemMove, Dst, Src, Len, A);
115 }
116
117 /// EmitMemChr - Emit a call to the memchr function.  This assumes that Ptr is
118 /// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
119 Value *llvm::EmitMemChr(Value *Ptr, Value *Val,
120                         Value *Len, IRBuilder<> &B, const TargetData *TD) {
121   Module *M = B.GetInsertBlock()->getParent()->getParent();
122   AttributeWithIndex AWI;
123   AWI = AttributeWithIndex::get(~0u, Attribute::ReadOnly | Attribute::NoUnwind);
124   LLVMContext &Context = B.GetInsertBlock()->getContext();
125   Value *MemChr = M->getOrInsertFunction("memchr", AttrListPtr::get(&AWI, 1),
126                                          B.getInt8PtrTy(),
127                                          B.getInt8PtrTy(),
128                                          B.getInt32Ty(),
129                                          TD->getIntPtrType(Context),
130                                          NULL);
131   CallInst *CI = B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr");
132
133   if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts()))
134     CI->setCallingConv(F->getCallingConv());
135
136   return CI;
137 }
138
139 /// EmitMemCmp - Emit a call to the memcmp function.
140 Value *llvm::EmitMemCmp(Value *Ptr1, Value *Ptr2,
141                         Value *Len, IRBuilder<> &B, const TargetData *TD) {
142   Module *M = B.GetInsertBlock()->getParent()->getParent();
143   AttributeWithIndex AWI[3];
144   AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
145   AWI[1] = AttributeWithIndex::get(2, Attribute::NoCapture);
146   AWI[2] = AttributeWithIndex::get(~0u, Attribute::ReadOnly |
147                                    Attribute::NoUnwind);
148
149   LLVMContext &Context = B.GetInsertBlock()->getContext();
150   Value *MemCmp = M->getOrInsertFunction("memcmp", AttrListPtr::get(AWI, 3),
151                                          B.getInt32Ty(),
152                                          B.getInt8PtrTy(),
153                                          B.getInt8PtrTy(),
154                                          TD->getIntPtrType(Context), NULL);
155   CallInst *CI = B.CreateCall3(MemCmp, CastToCStr(Ptr1, B), CastToCStr(Ptr2, B),
156                                Len, "memcmp");
157
158   if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts()))
159     CI->setCallingConv(F->getCallingConv());
160
161   return CI;
162 }
163
164 /// EmitMemSet - Emit a call to the memset function
165 Value *llvm::EmitMemSet(Value *Dst, Value *Val,
166                         Value *Len, IRBuilder<> &B, const TargetData *TD) {
167  Module *M = B.GetInsertBlock()->getParent()->getParent();
168  Intrinsic::ID IID = Intrinsic::memset;
169  const Type *Tys[1];
170  Tys[0] = Len->getType();
171  Value *MemSet = Intrinsic::getDeclaration(M, IID, Tys, 1);
172  Value *Align = ConstantInt::get(B.getInt32Ty(), 1);
173  return B.CreateCall4(MemSet, CastToCStr(Dst, B), Val, Len, Align);
174 }
175
176 /// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
177 /// 'floor').  This function is known to take a single of type matching 'Op' and
178 /// returns one value with the same type.  If 'Op' is a long double, 'l' is
179 /// added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
180 Value *llvm::EmitUnaryFloatFnCall(Value *Op, const char *Name,
181                                   IRBuilder<> &B, const AttrListPtr &Attrs) {
182   char NameBuffer[20];
183   if (!Op->getType()->isDoubleTy()) {
184     // If we need to add a suffix, copy into NameBuffer.
185     unsigned NameLen = strlen(Name);
186     assert(NameLen < sizeof(NameBuffer)-2);
187     memcpy(NameBuffer, Name, NameLen);
188     if (Op->getType()->isFloatTy())
189       NameBuffer[NameLen] = 'f';  // floorf
190     else
191       NameBuffer[NameLen] = 'l';  // floorl
192     NameBuffer[NameLen+1] = 0;
193     Name = NameBuffer;
194   }
195
196   Module *M = B.GetInsertBlock()->getParent()->getParent();
197   Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
198                                          Op->getType(), NULL);
199   CallInst *CI = B.CreateCall(Callee, Op, Name);
200   CI->setAttributes(Attrs);
201   if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
202     CI->setCallingConv(F->getCallingConv());
203
204   return CI;
205 }
206
207 /// EmitPutChar - Emit a call to the putchar function.  This assumes that Char
208 /// is an integer.
209 Value *llvm::EmitPutChar(Value *Char, IRBuilder<> &B, const TargetData *TD) {
210   Module *M = B.GetInsertBlock()->getParent()->getParent();
211   Value *PutChar = M->getOrInsertFunction("putchar", B.getInt32Ty(),
212                                           B.getInt32Ty(), NULL);
213   CallInst *CI = B.CreateCall(PutChar,
214                               B.CreateIntCast(Char,
215                               B.getInt32Ty(),
216                               /*isSigned*/true,
217                               "chari"),
218                               "putchar");
219
220   if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts()))
221     CI->setCallingConv(F->getCallingConv());
222   return CI;
223 }
224
225 /// EmitPutS - Emit a call to the puts function.  This assumes that Str is
226 /// some pointer.
227 void llvm::EmitPutS(Value *Str, IRBuilder<> &B, const TargetData *TD) {
228   Module *M = B.GetInsertBlock()->getParent()->getParent();
229   AttributeWithIndex AWI[2];
230   AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
231   AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
232
233   Value *PutS = M->getOrInsertFunction("puts", AttrListPtr::get(AWI, 2),
234                                        B.getInt32Ty(),
235                                        B.getInt8PtrTy(),
236                                        NULL);
237   CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts");
238   if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
239     CI->setCallingConv(F->getCallingConv());
240
241 }
242
243 /// EmitFPutC - Emit a call to the fputc function.  This assumes that Char is
244 /// an integer and File is a pointer to FILE.
245 void llvm::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B,
246                      const TargetData *TD) {
247   Module *M = B.GetInsertBlock()->getParent()->getParent();
248   AttributeWithIndex AWI[2];
249   AWI[0] = AttributeWithIndex::get(2, Attribute::NoCapture);
250   AWI[1] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
251   Constant *F;
252   if (File->getType()->isPointerTy())
253     F = M->getOrInsertFunction("fputc", AttrListPtr::get(AWI, 2),
254                                B.getInt32Ty(),
255                                B.getInt32Ty(), File->getType(),
256                                NULL);
257   else
258     F = M->getOrInsertFunction("fputc",
259                                B.getInt32Ty(),
260                                B.getInt32Ty(),
261                                File->getType(), NULL);
262   Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
263                          "chari");
264   CallInst *CI = B.CreateCall2(F, Char, File, "fputc");
265
266   if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
267     CI->setCallingConv(Fn->getCallingConv());
268 }
269
270 /// EmitFPutS - Emit a call to the puts function.  Str is required to be a
271 /// pointer and File is a pointer to FILE.
272 void llvm::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B,
273                      const TargetData *TD) {
274   Module *M = B.GetInsertBlock()->getParent()->getParent();
275   AttributeWithIndex AWI[3];
276   AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
277   AWI[1] = AttributeWithIndex::get(2, Attribute::NoCapture);
278   AWI[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
279   Constant *F;
280   if (File->getType()->isPointerTy())
281     F = M->getOrInsertFunction("fputs", AttrListPtr::get(AWI, 3),
282                                B.getInt32Ty(),
283                                B.getInt8PtrTy(),
284                                File->getType(), NULL);
285   else
286     F = M->getOrInsertFunction("fputs", B.getInt32Ty(),
287                                B.getInt8PtrTy(),
288                                File->getType(), NULL);
289   CallInst *CI = B.CreateCall2(F, CastToCStr(Str, B), File, "fputs");
290
291   if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
292     CI->setCallingConv(Fn->getCallingConv());
293 }
294
295 /// EmitFWrite - Emit a call to the fwrite function.  This assumes that Ptr is
296 /// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
297 void llvm::EmitFWrite(Value *Ptr, Value *Size, Value *File,
298                       IRBuilder<> &B, const TargetData *TD) {
299   Module *M = B.GetInsertBlock()->getParent()->getParent();
300   AttributeWithIndex AWI[3];
301   AWI[0] = AttributeWithIndex::get(1, Attribute::NoCapture);
302   AWI[1] = AttributeWithIndex::get(4, Attribute::NoCapture);
303   AWI[2] = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
304   LLVMContext &Context = B.GetInsertBlock()->getContext();
305   Constant *F;
306   if (File->getType()->isPointerTy())
307     F = M->getOrInsertFunction("fwrite", AttrListPtr::get(AWI, 3),
308                                TD->getIntPtrType(Context),
309                                B.getInt8PtrTy(),
310                                TD->getIntPtrType(Context),
311                                TD->getIntPtrType(Context),
312                                File->getType(), NULL);
313   else
314     F = M->getOrInsertFunction("fwrite", TD->getIntPtrType(Context),
315                                B.getInt8PtrTy(),
316                                TD->getIntPtrType(Context),
317                                TD->getIntPtrType(Context),
318                                File->getType(), NULL);
319   CallInst *CI = B.CreateCall4(F, CastToCStr(Ptr, B), Size,
320                         ConstantInt::get(TD->getIntPtrType(Context), 1), File);
321
322   if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
323     CI->setCallingConv(Fn->getCallingConv());
324 }