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/ADT/SmallString.h"
16 #include "llvm/IR/Constants.h"
17 #include "llvm/IR/DataLayout.h"
18 #include "llvm/IR/Function.h"
19 #include "llvm/IR/IRBuilder.h"
20 #include "llvm/IR/Intrinsics.h"
21 #include "llvm/IR/LLVMContext.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/IR/Type.h"
24 #include "llvm/Analysis/TargetLibraryInfo.h"
28 /// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
29 Value *llvm::CastToCStr(Value *V, IRBuilder<> &B) {
30 unsigned AS = V->getType()->getPointerAddressSpace();
31 return B.CreateBitCast(V, B.getInt8PtrTy(AS), "cstr");
34 /// EmitStrLen - Emit a call to the strlen function to the builder, for the
35 /// specified pointer. This always returns an integer value of size intptr_t.
36 Value *llvm::EmitStrLen(Value *Ptr, IRBuilder<> &B, const DataLayout &DL,
37 const TargetLibraryInfo *TLI) {
38 if (!TLI->has(LibFunc::strlen))
41 Module *M = B.GetInsertBlock()->getParent()->getParent();
43 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
44 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
45 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs);
47 LLVMContext &Context = B.GetInsertBlock()->getContext();
48 Constant *StrLen = M->getOrInsertFunction(
49 "strlen", AttributeSet::get(M->getContext(), AS),
50 DL.getIntPtrType(Context), B.getInt8PtrTy(), nullptr);
51 CallInst *CI = B.CreateCall(StrLen, CastToCStr(Ptr, B), "strlen");
52 if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts()))
53 CI->setCallingConv(F->getCallingConv());
58 /// EmitStrNLen - Emit a call to the strnlen function to the builder, for the
59 /// specified pointer. Ptr is required to be some pointer type, MaxLen must
60 /// be of size_t type, and the return value has 'intptr_t' type.
61 Value *llvm::EmitStrNLen(Value *Ptr, Value *MaxLen, IRBuilder<> &B,
62 const DataLayout &DL, const TargetLibraryInfo *TLI) {
63 if (!TLI->has(LibFunc::strnlen))
66 Module *M = B.GetInsertBlock()->getParent()->getParent();
68 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
69 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
70 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs);
72 LLVMContext &Context = B.GetInsertBlock()->getContext();
74 M->getOrInsertFunction("strnlen", AttributeSet::get(M->getContext(), AS),
75 DL.getIntPtrType(Context), B.getInt8PtrTy(),
76 DL.getIntPtrType(Context), nullptr);
77 CallInst *CI = B.CreateCall2(StrNLen, CastToCStr(Ptr, B), MaxLen, "strnlen");
78 if (const Function *F = dyn_cast<Function>(StrNLen->stripPointerCasts()))
79 CI->setCallingConv(F->getCallingConv());
84 /// EmitStrChr - Emit a call to the strchr function to the builder, for the
85 /// specified pointer and character. Ptr is required to be some pointer type,
86 /// and the return value has 'i8*' type.
87 Value *llvm::EmitStrChr(Value *Ptr, char C, IRBuilder<> &B,
88 const TargetLibraryInfo *TLI) {
89 if (!TLI->has(LibFunc::strchr))
92 Module *M = B.GetInsertBlock()->getParent()->getParent();
93 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
95 AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs);
97 Type *I8Ptr = B.getInt8PtrTy();
98 Type *I32Ty = B.getInt32Ty();
99 Constant *StrChr = M->getOrInsertFunction("strchr",
100 AttributeSet::get(M->getContext(),
102 I8Ptr, I8Ptr, I32Ty, nullptr);
103 CallInst *CI = B.CreateCall2(StrChr, CastToCStr(Ptr, B),
104 ConstantInt::get(I32Ty, C), "strchr");
105 if (const Function *F = dyn_cast<Function>(StrChr->stripPointerCasts()))
106 CI->setCallingConv(F->getCallingConv());
110 /// EmitStrNCmp - Emit a call to the strncmp function to the builder.
111 Value *llvm::EmitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
112 const DataLayout &DL, const TargetLibraryInfo *TLI) {
113 if (!TLI->has(LibFunc::strncmp))
116 Module *M = B.GetInsertBlock()->getParent()->getParent();
118 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
119 AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
120 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
121 AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs);
123 LLVMContext &Context = B.GetInsertBlock()->getContext();
124 Value *StrNCmp = M->getOrInsertFunction(
125 "strncmp", AttributeSet::get(M->getContext(), AS), B.getInt32Ty(),
126 B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context), nullptr);
127 CallInst *CI = B.CreateCall3(StrNCmp, CastToCStr(Ptr1, B),
128 CastToCStr(Ptr2, B), Len, "strncmp");
130 if (const Function *F = dyn_cast<Function>(StrNCmp->stripPointerCasts()))
131 CI->setCallingConv(F->getCallingConv());
136 /// EmitStrCpy - Emit a call to the strcpy function to the builder, for the
137 /// specified pointer arguments.
138 Value *llvm::EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
139 const TargetLibraryInfo *TLI, StringRef Name) {
140 if (!TLI->has(LibFunc::strcpy))
143 Module *M = B.GetInsertBlock()->getParent()->getParent();
145 AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
146 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
147 Attribute::NoUnwind);
148 Type *I8Ptr = B.getInt8PtrTy();
149 Value *StrCpy = M->getOrInsertFunction(Name,
150 AttributeSet::get(M->getContext(), AS),
151 I8Ptr, I8Ptr, I8Ptr, nullptr);
152 CallInst *CI = B.CreateCall2(StrCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
154 if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts()))
155 CI->setCallingConv(F->getCallingConv());
159 /// EmitStrNCpy - Emit a call to the strncpy function to the builder, for the
160 /// specified pointer arguments.
161 Value *llvm::EmitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
162 const TargetLibraryInfo *TLI, StringRef Name) {
163 if (!TLI->has(LibFunc::strncpy))
166 Module *M = B.GetInsertBlock()->getParent()->getParent();
168 AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
169 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
170 Attribute::NoUnwind);
171 Type *I8Ptr = B.getInt8PtrTy();
172 Value *StrNCpy = M->getOrInsertFunction(Name,
173 AttributeSet::get(M->getContext(),
176 Len->getType(), nullptr);
177 CallInst *CI = B.CreateCall3(StrNCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
179 if (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts()))
180 CI->setCallingConv(F->getCallingConv());
184 /// EmitMemCpyChk - Emit a call to the __memcpy_chk function to the builder.
185 /// This expects that the Len and ObjSize have type 'intptr_t' and Dst/Src
187 Value *llvm::EmitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
188 IRBuilder<> &B, const DataLayout &DL,
189 const TargetLibraryInfo *TLI) {
190 if (!TLI->has(LibFunc::memcpy_chk))
193 Module *M = B.GetInsertBlock()->getParent()->getParent();
195 AS = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
196 Attribute::NoUnwind);
197 LLVMContext &Context = B.GetInsertBlock()->getContext();
198 Value *MemCpy = M->getOrInsertFunction(
199 "__memcpy_chk", AttributeSet::get(M->getContext(), AS), B.getInt8PtrTy(),
200 B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context),
201 DL.getIntPtrType(Context), nullptr);
202 Dst = CastToCStr(Dst, B);
203 Src = CastToCStr(Src, B);
204 CallInst *CI = B.CreateCall4(MemCpy, Dst, Src, Len, ObjSize);
205 if (const Function *F = dyn_cast<Function>(MemCpy->stripPointerCasts()))
206 CI->setCallingConv(F->getCallingConv());
210 /// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
211 /// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
212 Value *llvm::EmitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B,
213 const DataLayout &DL, const TargetLibraryInfo *TLI) {
214 if (!TLI->has(LibFunc::memchr))
217 Module *M = B.GetInsertBlock()->getParent()->getParent();
219 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
220 AS = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs);
221 LLVMContext &Context = B.GetInsertBlock()->getContext();
222 Value *MemChr = M->getOrInsertFunction(
223 "memchr", AttributeSet::get(M->getContext(), AS), B.getInt8PtrTy(),
224 B.getInt8PtrTy(), B.getInt32Ty(), DL.getIntPtrType(Context), nullptr);
225 CallInst *CI = B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr");
227 if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts()))
228 CI->setCallingConv(F->getCallingConv());
233 /// EmitMemCmp - Emit a call to the memcmp function.
234 Value *llvm::EmitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
235 const DataLayout &DL, const TargetLibraryInfo *TLI) {
236 if (!TLI->has(LibFunc::memcmp))
239 Module *M = B.GetInsertBlock()->getParent()->getParent();
241 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
242 AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
243 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
244 AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs);
246 LLVMContext &Context = B.GetInsertBlock()->getContext();
247 Value *MemCmp = M->getOrInsertFunction(
248 "memcmp", AttributeSet::get(M->getContext(), AS), B.getInt32Ty(),
249 B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context), nullptr);
250 CallInst *CI = B.CreateCall3(MemCmp, CastToCStr(Ptr1, B), CastToCStr(Ptr2, B),
253 if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts()))
254 CI->setCallingConv(F->getCallingConv());
259 /// Append a suffix to the function name according to the type of 'Op'.
260 static void AppendTypeSuffix(Value *Op, StringRef &Name, SmallString<20> &NameBuffer) {
261 if (!Op->getType()->isDoubleTy()) {
264 if (Op->getType()->isFloatTy())
274 /// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
275 /// 'floor'). This function is known to take a single of type matching 'Op' and
276 /// returns one value with the same type. If 'Op' is a long double, 'l' is
277 /// added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
278 Value *llvm::EmitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B,
279 const AttributeSet &Attrs) {
280 SmallString<20> NameBuffer;
281 AppendTypeSuffix(Op, Name, NameBuffer);
283 Module *M = B.GetInsertBlock()->getParent()->getParent();
284 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
285 Op->getType(), nullptr);
286 CallInst *CI = B.CreateCall(Callee, Op, Name);
287 CI->setAttributes(Attrs);
288 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
289 CI->setCallingConv(F->getCallingConv());
294 /// EmitBinaryFloatFnCall - Emit a call to the binary function named 'Name'
295 /// (e.g. 'fmin'). This function is known to take type matching 'Op1' and 'Op2'
296 /// and return one value with the same type. If 'Op1/Op2' are long double, 'l'
297 /// is added as the suffix of name, if 'Op1/Op2' is a float, we add a 'f'
299 Value *llvm::EmitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name,
300 IRBuilder<> &B, const AttributeSet &Attrs) {
301 SmallString<20> NameBuffer;
302 AppendTypeSuffix(Op1, Name, NameBuffer);
304 Module *M = B.GetInsertBlock()->getParent()->getParent();
305 Value *Callee = M->getOrInsertFunction(Name, Op1->getType(),
306 Op1->getType(), Op2->getType(), nullptr);
307 CallInst *CI = B.CreateCall2(Callee, Op1, Op2, Name);
308 CI->setAttributes(Attrs);
309 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
310 CI->setCallingConv(F->getCallingConv());
315 /// EmitPutChar - Emit a call to the putchar function. This assumes that Char
317 Value *llvm::EmitPutChar(Value *Char, IRBuilder<> &B,
318 const TargetLibraryInfo *TLI) {
319 if (!TLI->has(LibFunc::putchar))
322 Module *M = B.GetInsertBlock()->getParent()->getParent();
323 Value *PutChar = M->getOrInsertFunction("putchar", B.getInt32Ty(),
324 B.getInt32Ty(), nullptr);
325 CallInst *CI = B.CreateCall(PutChar,
326 B.CreateIntCast(Char,
332 if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts()))
333 CI->setCallingConv(F->getCallingConv());
337 /// EmitPutS - Emit a call to the puts function. This assumes that Str is
339 Value *llvm::EmitPutS(Value *Str, IRBuilder<> &B,
340 const TargetLibraryInfo *TLI) {
341 if (!TLI->has(LibFunc::puts))
344 Module *M = B.GetInsertBlock()->getParent()->getParent();
346 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
347 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
348 Attribute::NoUnwind);
350 Value *PutS = M->getOrInsertFunction("puts",
351 AttributeSet::get(M->getContext(), AS),
355 CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts");
356 if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
357 CI->setCallingConv(F->getCallingConv());
361 /// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
362 /// an integer and File is a pointer to FILE.
363 Value *llvm::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B,
364 const TargetLibraryInfo *TLI) {
365 if (!TLI->has(LibFunc::fputc))
368 Module *M = B.GetInsertBlock()->getParent()->getParent();
370 AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
371 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
372 Attribute::NoUnwind);
374 if (File->getType()->isPointerTy())
375 F = M->getOrInsertFunction("fputc",
376 AttributeSet::get(M->getContext(), AS),
378 B.getInt32Ty(), File->getType(),
381 F = M->getOrInsertFunction("fputc",
384 File->getType(), nullptr);
385 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
387 CallInst *CI = B.CreateCall2(F, Char, File, "fputc");
389 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
390 CI->setCallingConv(Fn->getCallingConv());
394 /// EmitFPutS - Emit a call to the puts function. Str is required to be a
395 /// pointer and File is a pointer to FILE.
396 Value *llvm::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B,
397 const TargetLibraryInfo *TLI) {
398 if (!TLI->has(LibFunc::fputs))
401 Module *M = B.GetInsertBlock()->getParent()->getParent();
403 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
404 AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
405 AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
406 Attribute::NoUnwind);
407 StringRef FPutsName = TLI->getName(LibFunc::fputs);
409 if (File->getType()->isPointerTy())
410 F = M->getOrInsertFunction(FPutsName,
411 AttributeSet::get(M->getContext(), AS),
414 File->getType(), nullptr);
416 F = M->getOrInsertFunction(FPutsName, B.getInt32Ty(),
418 File->getType(), nullptr);
419 CallInst *CI = B.CreateCall2(F, CastToCStr(Str, B), File, "fputs");
421 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
422 CI->setCallingConv(Fn->getCallingConv());
426 /// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
427 /// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
428 Value *llvm::EmitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B,
429 const DataLayout &DL, const TargetLibraryInfo *TLI) {
430 if (!TLI->has(LibFunc::fwrite))
433 Module *M = B.GetInsertBlock()->getParent()->getParent();
435 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
436 AS[1] = AttributeSet::get(M->getContext(), 4, Attribute::NoCapture);
437 AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
438 Attribute::NoUnwind);
439 LLVMContext &Context = B.GetInsertBlock()->getContext();
440 StringRef FWriteName = TLI->getName(LibFunc::fwrite);
442 if (File->getType()->isPointerTy())
443 F = M->getOrInsertFunction(
444 FWriteName, AttributeSet::get(M->getContext(), AS),
445 DL.getIntPtrType(Context), B.getInt8PtrTy(), DL.getIntPtrType(Context),
446 DL.getIntPtrType(Context), File->getType(), nullptr);
448 F = M->getOrInsertFunction(FWriteName, DL.getIntPtrType(Context),
449 B.getInt8PtrTy(), DL.getIntPtrType(Context),
450 DL.getIntPtrType(Context), File->getType(),
453 B.CreateCall4(F, CastToCStr(Ptr, B), Size,
454 ConstantInt::get(DL.getIntPtrType(Context), 1), File);
456 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
457 CI->setCallingConv(Fn->getCallingConv());