1 //===- BuildLibCalls.h - Utility builder for libcalls -----------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file exposes an interface to build some C language libcalls for
10 // optimization passes that need to call the various functions.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_TRANSFORMS_UTILS_BUILDLIBCALLS_H
15 #define LLVM_TRANSFORMS_UTILS_BUILDLIBCALLS_H
17 #include "llvm/Analysis/TargetLibraryInfo.h"
18 #include "llvm/IR/IRBuilder.h"
23 class TargetLibraryInfo
;
25 /// Analyze the name and prototype of the given function and set any
26 /// applicable attributes.
27 /// If the library function is unavailable, this doesn't modify it.
29 /// Returns true if any attributes were set and false otherwise.
30 bool inferLibFuncAttributes(Function
&F
, const TargetLibraryInfo
&TLI
);
31 bool inferLibFuncAttributes(Module
*M
, StringRef Name
, const TargetLibraryInfo
&TLI
);
33 /// Check whether the overloaded floating point function
34 /// corresponding to \a Ty is available.
35 bool hasFloatFn(const TargetLibraryInfo
*TLI
, Type
*Ty
,
36 LibFunc DoubleFn
, LibFunc FloatFn
, LibFunc LongDoubleFn
);
38 /// Get the name of the overloaded floating point function
39 /// corresponding to \a Ty.
40 StringRef
getFloatFnName(const TargetLibraryInfo
*TLI
, Type
*Ty
,
41 LibFunc DoubleFn
, LibFunc FloatFn
,
42 LibFunc LongDoubleFn
);
44 /// Return V if it is an i8*, otherwise cast it to i8*.
45 Value
*castToCStr(Value
*V
, IRBuilder
<> &B
);
47 /// Emit a call to the strlen function to the builder, for the specified
48 /// pointer. Ptr is required to be some pointer type, and the return value has
50 Value
*emitStrLen(Value
*Ptr
, IRBuilder
<> &B
, const DataLayout
&DL
,
51 const TargetLibraryInfo
*TLI
);
53 /// Emit a call to the strnlen function to the builder, for the specified
54 /// pointer. Ptr is required to be some pointer type, MaxLen must be of size_t
55 /// type, and the return value has 'intptr_t' type.
56 Value
*emitStrNLen(Value
*Ptr
, Value
*MaxLen
, IRBuilder
<> &B
,
57 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
);
59 /// Emit a call to the strchr function to the builder, for the specified
60 /// pointer and character. Ptr is required to be some pointer type, and the
61 /// return value has 'i8*' type.
62 Value
*emitStrChr(Value
*Ptr
, char C
, IRBuilder
<> &B
,
63 const TargetLibraryInfo
*TLI
);
65 /// Emit a call to the strncmp function to the builder.
66 Value
*emitStrNCmp(Value
*Ptr1
, Value
*Ptr2
, Value
*Len
, IRBuilder
<> &B
,
67 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
);
69 /// Emit a call to the strcpy function to the builder, for the specified
70 /// pointer arguments.
71 Value
*emitStrCpy(Value
*Dst
, Value
*Src
, IRBuilder
<> &B
,
72 const TargetLibraryInfo
*TLI
);
74 /// Emit a call to the stpcpy function to the builder, for the specified
75 /// pointer arguments.
76 Value
*emitStpCpy(Value
*Dst
, Value
*Src
, IRBuilder
<> &B
,
77 const TargetLibraryInfo
*TLI
);
79 /// Emit a call to the strncpy function to the builder, for the specified
80 /// pointer arguments and length.
81 Value
*emitStrNCpy(Value
*Dst
, Value
*Src
, Value
*Len
, IRBuilder
<> &B
,
82 const TargetLibraryInfo
*TLI
);
84 /// Emit a call to the stpncpy function to the builder, for the specified
85 /// pointer arguments and length.
86 Value
*emitStpNCpy(Value
*Dst
, Value
*Src
, Value
*Len
, IRBuilder
<> &B
,
87 const TargetLibraryInfo
*TLI
);
89 /// Emit a call to the __memcpy_chk function to the builder. This expects that
90 /// the Len and ObjSize have type 'intptr_t' and Dst/Src are pointers.
91 Value
*emitMemCpyChk(Value
*Dst
, Value
*Src
, Value
*Len
, Value
*ObjSize
,
92 IRBuilder
<> &B
, const DataLayout
&DL
,
93 const TargetLibraryInfo
*TLI
);
95 /// Emit a call to the memchr function. This assumes that Ptr is a pointer,
96 /// Val is an i32 value, and Len is an 'intptr_t' value.
97 Value
*emitMemChr(Value
*Ptr
, Value
*Val
, Value
*Len
, IRBuilder
<> &B
,
98 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
);
100 /// Emit a call to the memcmp function.
101 Value
*emitMemCmp(Value
*Ptr1
, Value
*Ptr2
, Value
*Len
, IRBuilder
<> &B
,
102 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
);
104 /// Emit a call to the bcmp function.
105 Value
*emitBCmp(Value
*Ptr1
, Value
*Ptr2
, Value
*Len
, IRBuilder
<> &B
,
106 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
);
108 /// Emit a call to the memccpy function.
109 Value
*emitMemCCpy(Value
*Ptr1
, Value
*Ptr2
, Value
*Val
, Value
*Len
,
110 IRBuilder
<> &B
, const TargetLibraryInfo
*TLI
);
112 /// Emit a call to the snprintf function.
113 Value
*emitSNPrintf(Value
*Dest
, Value
*Size
, Value
*Fmt
,
114 ArrayRef
<Value
*> Args
, IRBuilder
<> &B
,
115 const TargetLibraryInfo
*TLI
);
117 /// Emit a call to the sprintf function.
118 Value
*emitSPrintf(Value
*Dest
, Value
*Fmt
, ArrayRef
<Value
*> VariadicArgs
,
119 IRBuilder
<> &B
, const TargetLibraryInfo
*TLI
);
121 /// Emit a call to the strcat function.
122 Value
*emitStrCat(Value
*Dest
, Value
*Src
, IRBuilder
<> &B
,
123 const TargetLibraryInfo
*TLI
);
125 /// Emit a call to the strlcpy function.
126 Value
*emitStrLCpy(Value
*Dest
, Value
*Src
, Value
*Size
, IRBuilder
<> &B
,
127 const TargetLibraryInfo
*TLI
);
129 /// Emit a call to the strlcat function.
130 Value
*emitStrLCat(Value
*Dest
, Value
*Src
, Value
*Size
, IRBuilder
<> &B
,
131 const TargetLibraryInfo
*TLI
);
133 /// Emit a call to the strncat function.
134 Value
*emitStrNCat(Value
*Dest
, Value
*Src
, Value
*Size
, IRBuilder
<> &B
,
135 const TargetLibraryInfo
*TLI
);
137 /// Emit a call to the vsnprintf function.
138 Value
*emitVSNPrintf(Value
*Dest
, Value
*Size
, Value
*Fmt
, Value
*VAList
,
139 IRBuilder
<> &B
, const TargetLibraryInfo
*TLI
);
141 /// Emit a call to the vsprintf function.
142 Value
*emitVSPrintf(Value
*Dest
, Value
*Fmt
, Value
*VAList
, IRBuilder
<> &B
,
143 const TargetLibraryInfo
*TLI
);
145 /// Emit a call to the unary function named 'Name' (e.g. 'floor'). This
146 /// function is known to take a single of type matching 'Op' and returns one
147 /// value with the same type. If 'Op' is a long double, 'l' is added as the
148 /// suffix of name, if 'Op' is a float, we add a 'f' suffix.
149 Value
*emitUnaryFloatFnCall(Value
*Op
, StringRef Name
, IRBuilder
<> &B
,
150 const AttributeList
&Attrs
);
152 /// Emit a call to the unary function DoubleFn, FloatFn or LongDoubleFn,
153 /// depending of the type of Op.
154 Value
*emitUnaryFloatFnCall(Value
*Op
, const TargetLibraryInfo
*TLI
,
155 LibFunc DoubleFn
, LibFunc FloatFn
,
156 LibFunc LongDoubleFn
, IRBuilder
<> &B
,
157 const AttributeList
&Attrs
);
159 /// Emit a call to the binary function named 'Name' (e.g. 'fmin'). This
160 /// function is known to take type matching 'Op1' and 'Op2' and return one
161 /// value with the same type. If 'Op1/Op2' are long double, 'l' is added as
162 /// the suffix of name, if 'Op1/Op2' are float, we add a 'f' suffix.
163 Value
*emitBinaryFloatFnCall(Value
*Op1
, Value
*Op2
, StringRef Name
,
164 IRBuilder
<> &B
, const AttributeList
&Attrs
);
166 /// Emit a call to the binary function DoubleFn, FloatFn or LongDoubleFn,
167 /// depending of the type of Op1.
168 Value
*emitBinaryFloatFnCall(Value
*Op1
, Value
*Op2
,
169 const TargetLibraryInfo
*TLI
, LibFunc DoubleFn
,
170 LibFunc FloatFn
, LibFunc LongDoubleFn
,
171 IRBuilder
<> &B
, const AttributeList
&Attrs
);
173 /// Emit a call to the putchar function. This assumes that Char is an integer.
174 Value
*emitPutChar(Value
*Char
, IRBuilder
<> &B
, const TargetLibraryInfo
*TLI
);
176 /// Emit a call to the puts function. This assumes that Str is some pointer.
177 Value
*emitPutS(Value
*Str
, IRBuilder
<> &B
, const TargetLibraryInfo
*TLI
);
179 /// Emit a call to the fputc function. This assumes that Char is an i32, and
180 /// File is a pointer to FILE.
181 Value
*emitFPutC(Value
*Char
, Value
*File
, IRBuilder
<> &B
,
182 const TargetLibraryInfo
*TLI
);
184 /// Emit a call to the fputc_unlocked function. This assumes that Char is an
185 /// i32, and File is a pointer to FILE.
186 Value
*emitFPutCUnlocked(Value
*Char
, Value
*File
, IRBuilder
<> &B
,
187 const TargetLibraryInfo
*TLI
);
189 /// Emit a call to the fputs function. Str is required to be a pointer and
190 /// File is a pointer to FILE.
191 Value
*emitFPutS(Value
*Str
, Value
*File
, IRBuilder
<> &B
,
192 const TargetLibraryInfo
*TLI
);
194 /// Emit a call to the fputs_unlocked function. Str is required to be a
195 /// pointer and File is a pointer to FILE.
196 Value
*emitFPutSUnlocked(Value
*Str
, Value
*File
, IRBuilder
<> &B
,
197 const TargetLibraryInfo
*TLI
);
199 /// Emit a call to the fwrite function. This assumes that Ptr is a pointer,
200 /// Size is an 'intptr_t', and File is a pointer to FILE.
201 Value
*emitFWrite(Value
*Ptr
, Value
*Size
, Value
*File
, IRBuilder
<> &B
,
202 const DataLayout
&DL
, const TargetLibraryInfo
*TLI
);
204 /// Emit a call to the malloc function.
205 Value
*emitMalloc(Value
*Num
, IRBuilder
<> &B
, const DataLayout
&DL
,
206 const TargetLibraryInfo
*TLI
);
208 /// Emit a call to the calloc function.
209 Value
*emitCalloc(Value
*Num
, Value
*Size
, const AttributeList
&Attrs
,
210 IRBuilder
<> &B
, const TargetLibraryInfo
&TLI
);
212 /// Emit a call to the fwrite_unlocked function. This assumes that Ptr is a
213 /// pointer, Size is an 'intptr_t', N is nmemb and File is a pointer to FILE.
214 Value
*emitFWriteUnlocked(Value
*Ptr
, Value
*Size
, Value
*N
, Value
*File
,
215 IRBuilder
<> &B
, const DataLayout
&DL
,
216 const TargetLibraryInfo
*TLI
);
218 /// Emit a call to the fgetc_unlocked function. File is a pointer to FILE.
219 Value
*emitFGetCUnlocked(Value
*File
, IRBuilder
<> &B
,
220 const TargetLibraryInfo
*TLI
);
222 /// Emit a call to the fgets_unlocked function. Str is required to be a
223 /// pointer, Size is an i32 and File is a pointer to FILE.
224 Value
*emitFGetSUnlocked(Value
*Str
, Value
*Size
, Value
*File
, IRBuilder
<> &B
,
225 const TargetLibraryInfo
*TLI
);
227 /// Emit a call to the fread_unlocked function. This assumes that Ptr is a
228 /// pointer, Size is an 'intptr_t', N is nmemb and File is a pointer to FILE.
229 Value
*emitFReadUnlocked(Value
*Ptr
, Value
*Size
, Value
*N
, Value
*File
,
230 IRBuilder
<> &B
, const DataLayout
&DL
,
231 const TargetLibraryInfo
*TLI
);