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 /// EmitStrChr - Emit a call to the strchr function to the builder, for the
59 /// specified pointer and character. Ptr is required to be some pointer type,
60 /// and the return value has 'i8*' type.
61 Value *llvm::EmitStrChr(Value *Ptr, char C, IRBuilder<> &B,
62 const TargetLibraryInfo *TLI) {
63 if (!TLI->has(LibFunc::strchr))
66 Module *M = B.GetInsertBlock()->getParent()->getParent();
67 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
69 AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs);
71 Type *I8Ptr = B.getInt8PtrTy();
72 Type *I32Ty = B.getInt32Ty();
73 Constant *StrChr = M->getOrInsertFunction("strchr",
74 AttributeSet::get(M->getContext(),
76 I8Ptr, I8Ptr, I32Ty, nullptr);
77 CallInst *CI = B.CreateCall(
78 StrChr, {CastToCStr(Ptr, B), ConstantInt::get(I32Ty, C)}, "strchr");
79 if (const Function *F = dyn_cast<Function>(StrChr->stripPointerCasts()))
80 CI->setCallingConv(F->getCallingConv());
84 /// EmitStrNCmp - Emit a call to the strncmp function to the builder.
85 Value *llvm::EmitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
86 const DataLayout &DL, const TargetLibraryInfo *TLI) {
87 if (!TLI->has(LibFunc::strncmp))
90 Module *M = B.GetInsertBlock()->getParent()->getParent();
92 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
93 AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
94 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
95 AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs);
97 LLVMContext &Context = B.GetInsertBlock()->getContext();
98 Value *StrNCmp = M->getOrInsertFunction(
99 "strncmp", AttributeSet::get(M->getContext(), AS), B.getInt32Ty(),
100 B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context), nullptr);
101 CallInst *CI = B.CreateCall(
102 StrNCmp, {CastToCStr(Ptr1, B), CastToCStr(Ptr2, B), Len}, "strncmp");
104 if (const Function *F = dyn_cast<Function>(StrNCmp->stripPointerCasts()))
105 CI->setCallingConv(F->getCallingConv());
110 /// EmitStrCpy - Emit a call to the strcpy function to the builder, for the
111 /// specified pointer arguments.
112 Value *llvm::EmitStrCpy(Value *Dst, Value *Src, IRBuilder<> &B,
113 const TargetLibraryInfo *TLI, StringRef Name) {
114 if (!TLI->has(LibFunc::strcpy))
117 Module *M = B.GetInsertBlock()->getParent()->getParent();
119 AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
120 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
121 Attribute::NoUnwind);
122 Type *I8Ptr = B.getInt8PtrTy();
123 Value *StrCpy = M->getOrInsertFunction(Name,
124 AttributeSet::get(M->getContext(), AS),
125 I8Ptr, I8Ptr, I8Ptr, nullptr);
127 B.CreateCall(StrCpy, {CastToCStr(Dst, B), CastToCStr(Src, B)}, Name);
128 if (const Function *F = dyn_cast<Function>(StrCpy->stripPointerCasts()))
129 CI->setCallingConv(F->getCallingConv());
133 /// EmitStrNCpy - Emit a call to the strncpy function to the builder, for the
134 /// specified pointer arguments.
135 Value *llvm::EmitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilder<> &B,
136 const TargetLibraryInfo *TLI, StringRef Name) {
137 if (!TLI->has(LibFunc::strncpy))
140 Module *M = B.GetInsertBlock()->getParent()->getParent();
142 AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
143 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
144 Attribute::NoUnwind);
145 Type *I8Ptr = B.getInt8PtrTy();
146 Value *StrNCpy = M->getOrInsertFunction(Name,
147 AttributeSet::get(M->getContext(),
150 Len->getType(), nullptr);
151 CallInst *CI = B.CreateCall(
152 StrNCpy, {CastToCStr(Dst, B), CastToCStr(Src, B), Len}, "strncpy");
153 if (const Function *F = dyn_cast<Function>(StrNCpy->stripPointerCasts()))
154 CI->setCallingConv(F->getCallingConv());
158 /// EmitMemCpyChk - Emit a call to the __memcpy_chk function to the builder.
159 /// This expects that the Len and ObjSize have type 'intptr_t' and Dst/Src
161 Value *llvm::EmitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
162 IRBuilder<> &B, const DataLayout &DL,
163 const TargetLibraryInfo *TLI) {
164 if (!TLI->has(LibFunc::memcpy_chk))
167 Module *M = B.GetInsertBlock()->getParent()->getParent();
169 AS = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
170 Attribute::NoUnwind);
171 LLVMContext &Context = B.GetInsertBlock()->getContext();
172 Value *MemCpy = M->getOrInsertFunction(
173 "__memcpy_chk", AttributeSet::get(M->getContext(), AS), B.getInt8PtrTy(),
174 B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context),
175 DL.getIntPtrType(Context), nullptr);
176 Dst = CastToCStr(Dst, B);
177 Src = CastToCStr(Src, B);
178 CallInst *CI = B.CreateCall(MemCpy, {Dst, Src, Len, ObjSize});
179 if (const Function *F = dyn_cast<Function>(MemCpy->stripPointerCasts()))
180 CI->setCallingConv(F->getCallingConv());
184 /// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
185 /// a pointer, Val is an i32 value, and Len is an 'intptr_t' value.
186 Value *llvm::EmitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilder<> &B,
187 const DataLayout &DL, const TargetLibraryInfo *TLI) {
188 if (!TLI->has(LibFunc::memchr))
191 Module *M = B.GetInsertBlock()->getParent()->getParent();
193 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
194 AS = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs);
195 LLVMContext &Context = B.GetInsertBlock()->getContext();
196 Value *MemChr = M->getOrInsertFunction(
197 "memchr", AttributeSet::get(M->getContext(), AS), B.getInt8PtrTy(),
198 B.getInt8PtrTy(), B.getInt32Ty(), DL.getIntPtrType(Context), nullptr);
199 CallInst *CI = B.CreateCall(MemChr, {CastToCStr(Ptr, B), Val, Len}, "memchr");
201 if (const Function *F = dyn_cast<Function>(MemChr->stripPointerCasts()))
202 CI->setCallingConv(F->getCallingConv());
207 /// EmitMemCmp - Emit a call to the memcmp function.
208 Value *llvm::EmitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilder<> &B,
209 const DataLayout &DL, const TargetLibraryInfo *TLI) {
210 if (!TLI->has(LibFunc::memcmp))
213 Module *M = B.GetInsertBlock()->getParent()->getParent();
215 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
216 AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
217 Attribute::AttrKind AVs[2] = { Attribute::ReadOnly, Attribute::NoUnwind };
218 AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex, AVs);
220 LLVMContext &Context = B.GetInsertBlock()->getContext();
221 Value *MemCmp = M->getOrInsertFunction(
222 "memcmp", AttributeSet::get(M->getContext(), AS), B.getInt32Ty(),
223 B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context), nullptr);
224 CallInst *CI = B.CreateCall(
225 MemCmp, {CastToCStr(Ptr1, B), CastToCStr(Ptr2, B), Len}, "memcmp");
227 if (const Function *F = dyn_cast<Function>(MemCmp->stripPointerCasts()))
228 CI->setCallingConv(F->getCallingConv());
233 /// Append a suffix to the function name according to the type of 'Op'.
234 static void AppendTypeSuffix(Value *Op, StringRef &Name, SmallString<20> &NameBuffer) {
235 if (!Op->getType()->isDoubleTy()) {
238 if (Op->getType()->isFloatTy())
248 /// EmitUnaryFloatFnCall - Emit a call to the unary function named 'Name' (e.g.
249 /// 'floor'). This function is known to take a single of type matching 'Op' and
250 /// returns one value with the same type. If 'Op' is a long double, 'l' is
251 /// added as the suffix of name, if 'Op' is a float, we add a 'f' suffix.
252 Value *llvm::EmitUnaryFloatFnCall(Value *Op, StringRef Name, IRBuilder<> &B,
253 const AttributeSet &Attrs) {
254 SmallString<20> NameBuffer;
255 AppendTypeSuffix(Op, Name, NameBuffer);
257 Module *M = B.GetInsertBlock()->getParent()->getParent();
258 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
259 Op->getType(), nullptr);
260 CallInst *CI = B.CreateCall(Callee, Op, Name);
261 CI->setAttributes(Attrs);
262 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
263 CI->setCallingConv(F->getCallingConv());
268 /// EmitBinaryFloatFnCall - Emit a call to the binary function named 'Name'
269 /// (e.g. 'fmin'). This function is known to take type matching 'Op1' and 'Op2'
270 /// and return one value with the same type. If 'Op1/Op2' are long double, 'l'
271 /// is added as the suffix of name, if 'Op1/Op2' is a float, we add a 'f'
273 Value *llvm::EmitBinaryFloatFnCall(Value *Op1, Value *Op2, StringRef Name,
274 IRBuilder<> &B, const AttributeSet &Attrs) {
275 SmallString<20> NameBuffer;
276 AppendTypeSuffix(Op1, Name, NameBuffer);
278 Module *M = B.GetInsertBlock()->getParent()->getParent();
279 Value *Callee = M->getOrInsertFunction(Name, Op1->getType(),
280 Op1->getType(), Op2->getType(), nullptr);
281 CallInst *CI = B.CreateCall(Callee, {Op1, Op2}, Name);
282 CI->setAttributes(Attrs);
283 if (const Function *F = dyn_cast<Function>(Callee->stripPointerCasts()))
284 CI->setCallingConv(F->getCallingConv());
289 /// EmitPutChar - Emit a call to the putchar function. This assumes that Char
291 Value *llvm::EmitPutChar(Value *Char, IRBuilder<> &B,
292 const TargetLibraryInfo *TLI) {
293 if (!TLI->has(LibFunc::putchar))
296 Module *M = B.GetInsertBlock()->getParent()->getParent();
297 Value *PutChar = M->getOrInsertFunction("putchar", B.getInt32Ty(),
298 B.getInt32Ty(), nullptr);
299 CallInst *CI = B.CreateCall(PutChar,
300 B.CreateIntCast(Char,
306 if (const Function *F = dyn_cast<Function>(PutChar->stripPointerCasts()))
307 CI->setCallingConv(F->getCallingConv());
311 /// EmitPutS - Emit a call to the puts function. This assumes that Str is
313 Value *llvm::EmitPutS(Value *Str, IRBuilder<> &B,
314 const TargetLibraryInfo *TLI) {
315 if (!TLI->has(LibFunc::puts))
318 Module *M = B.GetInsertBlock()->getParent()->getParent();
320 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
321 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
322 Attribute::NoUnwind);
324 Value *PutS = M->getOrInsertFunction("puts",
325 AttributeSet::get(M->getContext(), AS),
329 CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts");
330 if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
331 CI->setCallingConv(F->getCallingConv());
335 /// EmitFPutC - Emit a call to the fputc function. This assumes that Char is
336 /// an integer and File is a pointer to FILE.
337 Value *llvm::EmitFPutC(Value *Char, Value *File, IRBuilder<> &B,
338 const TargetLibraryInfo *TLI) {
339 if (!TLI->has(LibFunc::fputc))
342 Module *M = B.GetInsertBlock()->getParent()->getParent();
344 AS[0] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
345 AS[1] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
346 Attribute::NoUnwind);
348 if (File->getType()->isPointerTy())
349 F = M->getOrInsertFunction("fputc",
350 AttributeSet::get(M->getContext(), AS),
352 B.getInt32Ty(), File->getType(),
355 F = M->getOrInsertFunction("fputc",
358 File->getType(), nullptr);
359 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
361 CallInst *CI = B.CreateCall(F, {Char, File}, "fputc");
363 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
364 CI->setCallingConv(Fn->getCallingConv());
368 /// EmitFPutS - Emit a call to the puts function. Str is required to be a
369 /// pointer and File is a pointer to FILE.
370 Value *llvm::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B,
371 const TargetLibraryInfo *TLI) {
372 if (!TLI->has(LibFunc::fputs))
375 Module *M = B.GetInsertBlock()->getParent()->getParent();
377 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
378 AS[1] = AttributeSet::get(M->getContext(), 2, Attribute::NoCapture);
379 AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
380 Attribute::NoUnwind);
381 StringRef FPutsName = TLI->getName(LibFunc::fputs);
383 if (File->getType()->isPointerTy())
384 F = M->getOrInsertFunction(FPutsName,
385 AttributeSet::get(M->getContext(), AS),
388 File->getType(), nullptr);
390 F = M->getOrInsertFunction(FPutsName, B.getInt32Ty(),
392 File->getType(), nullptr);
393 CallInst *CI = B.CreateCall(F, {CastToCStr(Str, B), File}, "fputs");
395 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
396 CI->setCallingConv(Fn->getCallingConv());
400 /// EmitFWrite - Emit a call to the fwrite function. This assumes that Ptr is
401 /// a pointer, Size is an 'intptr_t', and File is a pointer to FILE.
402 Value *llvm::EmitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilder<> &B,
403 const DataLayout &DL, const TargetLibraryInfo *TLI) {
404 if (!TLI->has(LibFunc::fwrite))
407 Module *M = B.GetInsertBlock()->getParent()->getParent();
409 AS[0] = AttributeSet::get(M->getContext(), 1, Attribute::NoCapture);
410 AS[1] = AttributeSet::get(M->getContext(), 4, Attribute::NoCapture);
411 AS[2] = AttributeSet::get(M->getContext(), AttributeSet::FunctionIndex,
412 Attribute::NoUnwind);
413 LLVMContext &Context = B.GetInsertBlock()->getContext();
414 StringRef FWriteName = TLI->getName(LibFunc::fwrite);
416 if (File->getType()->isPointerTy())
417 F = M->getOrInsertFunction(
418 FWriteName, AttributeSet::get(M->getContext(), AS),
419 DL.getIntPtrType(Context), B.getInt8PtrTy(), DL.getIntPtrType(Context),
420 DL.getIntPtrType(Context), File->getType(), nullptr);
422 F = M->getOrInsertFunction(FWriteName, DL.getIntPtrType(Context),
423 B.getInt8PtrTy(), DL.getIntPtrType(Context),
424 DL.getIntPtrType(Context), File->getType(),
427 B.CreateCall(F, {CastToCStr(Ptr, B), Size,
428 ConstantInt::get(DL.getIntPtrType(Context), 1), File});
430 if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
431 CI->setCallingConv(Fn->getCallingConv());