LLVM 22.0.0git
BuildLibCalls.h
Go to the documentation of this file.
1//===- BuildLibCalls.h - Utility builder for libcalls -----------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file exposes an interface to build some C language libcalls for
10// optimization passes that need to call the various functions.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_TRANSFORMS_UTILS_BUILDLIBCALLS_H
15#define LLVM_TRANSFORMS_UTILS_BUILDLIBCALLS_H
16
19
20namespace llvm {
21 class Value;
22 class DataLayout;
23 class IRBuilderBase;
24
25 /// Analyze the name and prototype of the given function and set any
26 /// applicable attributes. Note that this merely helps optimizations on an
27 /// already existing function but does not consider mandatory attributes.
28 ///
29 /// If the library function is unavailable, this doesn't modify it.
30 ///
31 /// Returns true if any attributes were set and false otherwise.
33 const TargetLibraryInfo &TLI);
35 const TargetLibraryInfo &TLI);
36
37 /// Calls getOrInsertFunction() and then makes sure to add mandatory
38 /// argument attributes.
39 LLVM_ABI FunctionCallee getOrInsertLibFunc(Module *M,
40 const TargetLibraryInfo &TLI,
41 LibFunc TheLibFunc,
43 AttributeList AttributeList);
44 LLVM_ABI FunctionCallee getOrInsertLibFunc(Module *M,
45 const TargetLibraryInfo &TLI,
46 LibFunc TheLibFunc,
48 template <typename... ArgsTy>
51 Type *RetTy, ArgsTy... Args) {
52 SmallVector<Type*, sizeof...(ArgsTy)> ArgTys{Args...};
53 return getOrInsertLibFunc(M, TLI, TheLibFunc,
54 FunctionType::get(RetTy, ArgTys, false),
56 }
57 /// Same as above, but without the attributes.
58 template <typename... ArgsTy>
60 LibFunc TheLibFunc, Type *RetTy, ArgsTy... Args) {
61 return getOrInsertLibFunc(M, TLI, TheLibFunc, AttributeList{}, RetTy,
62 Args...);
63 }
64 // Avoid an incorrect ordering that'd otherwise compile incorrectly.
65 template <typename... ArgsTy>
66 FunctionCallee
69 FunctionType *Invalid, ArgsTy... Args) = delete;
70
71 // Handle -mregparm for the given function.
72 // Note that this function is a rough approximation that only works for simple
73 // function signatures; it does not apply other relevant attributes for
74 // function signatures, including sign/zero-extension for arguments and return
75 // values.
77
78 /// Check whether the library function is available on target and also that
79 /// it in the current Module is a Function with the right type.
81 const TargetLibraryInfo *TLI,
82 LibFunc TheLibFunc);
84 const TargetLibraryInfo *TLI,
86
87 /// Check whether the overloaded floating point function
88 /// corresponding to \a Ty is available.
89 LLVM_ABI bool hasFloatFn(const Module *M, const TargetLibraryInfo *TLI,
90 Type *Ty, LibFunc DoubleFn, LibFunc FloatFn,
91 LibFunc LongDoubleFn);
92
93 /// Get the name of the overloaded floating point function
94 /// corresponding to \a Ty. Return the LibFunc in \a TheLibFunc.
96 Type *Ty, LibFunc DoubleFn, LibFunc FloatFn,
97 LibFunc LongDoubleFn, LibFunc &TheLibFunc);
98
99 /// Emit a call to the strlen function to the builder, for the specified
100 /// pointer. Ptr is required to be some pointer type, and the return value has
101 /// 'size_t' type.
103 const TargetLibraryInfo *TLI);
104
105 /// Emit a call to the wcslen function to the builder, for the specified
106 /// pointer. Ptr is required to be some pointer type, and the return value has
107 /// 'size_t' type.
109 const TargetLibraryInfo *TLI);
110
111 /// Emit a call to the strdup function to the builder, for the specified
112 /// pointer. Ptr is required to be some pointer type, and the return value has
113 /// 'i8*' type.
115 const TargetLibraryInfo *TLI);
116
117 /// Emit a call to the strchr function to the builder, for the specified
118 /// pointer and character. Ptr is required to be some pointer type, and the
119 /// return value has 'i8*' type.
121 const TargetLibraryInfo *TLI);
122
123 /// Emit a call to the strncmp function to the builder.
124 LLVM_ABI Value *emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len,
125 IRBuilderBase &B, const DataLayout &DL,
126 const TargetLibraryInfo *TLI);
127
128 /// Emit a call to the strcpy function to the builder, for the specified
129 /// pointer arguments.
131 const TargetLibraryInfo *TLI);
132
133 /// Emit a call to the stpcpy function to the builder, for the specified
134 /// pointer arguments.
136 const TargetLibraryInfo *TLI);
137
138 /// Emit a call to the strncpy function to the builder, for the specified
139 /// pointer arguments and length.
140 LLVM_ABI Value *emitStrNCpy(Value *Dst, Value *Src, Value *Len,
141 IRBuilderBase &B, const TargetLibraryInfo *TLI);
142
143 /// Emit a call to the stpncpy function to the builder, for the specified
144 /// pointer arguments and length.
145 LLVM_ABI Value *emitStpNCpy(Value *Dst, Value *Src, Value *Len,
146 IRBuilderBase &B, const TargetLibraryInfo *TLI);
147
148 /// Emit a call to the __memcpy_chk function to the builder. This expects that
149 /// the Len and ObjSize have type 'size_t' and Dst/Src are pointers.
150 LLVM_ABI Value *emitMemCpyChk(Value *Dst, Value *Src, Value *Len,
151 Value *ObjSize, IRBuilderBase &B,
152 const DataLayout &DL,
153 const TargetLibraryInfo *TLI);
154
155 /// Emit a call to the mempcpy function.
156 LLVM_ABI Value *emitMemPCpy(Value *Dst, Value *Src, Value *Len,
157 IRBuilderBase &B, const DataLayout &DL,
158 const TargetLibraryInfo *TLI);
159
160 /// Emit a call to the memchr function. This assumes that Ptr is a pointer,
161 /// Val is an 'int' value, and Len is an 'size_t' value.
163 IRBuilderBase &B, const DataLayout &DL,
164 const TargetLibraryInfo *TLI);
165
166 /// Emit a call to the memrchr function, analogously to emitMemChr.
168 IRBuilderBase &B, const DataLayout &DL,
169 const TargetLibraryInfo *TLI);
170
171 /// Emit a call to the memcmp function.
172 LLVM_ABI Value *emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len,
173 IRBuilderBase &B, const DataLayout &DL,
174 const TargetLibraryInfo *TLI);
175
176 /// Emit a call to the bcmp function.
177 LLVM_ABI Value *emitBCmp(Value *Ptr1, Value *Ptr2, Value *Len,
178 IRBuilderBase &B, const DataLayout &DL,
179 const TargetLibraryInfo *TLI);
180
181 /// Emit a call to the memccpy function.
182 LLVM_ABI Value *emitMemCCpy(Value *Ptr1, Value *Ptr2, Value *Val, Value *Len,
183 IRBuilderBase &B, const TargetLibraryInfo *TLI);
184
185 /// Emit a call to the snprintf function.
188 const TargetLibraryInfo *TLI);
189
190 /// Emit a call to the sprintf function.
191 LLVM_ABI Value *emitSPrintf(Value *Dest, Value *Fmt,
192 ArrayRef<Value *> VariadicArgs, IRBuilderBase &B,
193 const TargetLibraryInfo *TLI);
194
195 /// Emit a call to the strcat function.
197 const TargetLibraryInfo *TLI);
198
199 /// Emit a call to the strlcpy function.
201 IRBuilderBase &B, const TargetLibraryInfo *TLI);
202
203 /// Emit a call to the strlcat function.
205 IRBuilderBase &B, const TargetLibraryInfo *TLI);
206
207 /// Emit a call to the strncat function.
209 IRBuilderBase &B, const TargetLibraryInfo *TLI);
210
211 /// Emit a call to the vsnprintf function.
213 Value *VAList, IRBuilderBase &B,
214 const TargetLibraryInfo *TLI);
215
216 /// Emit a call to the vsprintf function.
217 LLVM_ABI Value *emitVSPrintf(Value *Dest, Value *Fmt, Value *VAList,
218 IRBuilderBase &B, const TargetLibraryInfo *TLI);
219
220 /// Emit a call to the unary function named 'Name' (e.g. 'floor'). This
221 /// function is known to take a single of type matching 'Op' and returns one
222 /// value with the same type. If 'Op' is a long double, 'l' is added as the
223 /// suffix of name, if 'Op' is a float, we add a 'f' suffix.
226 const AttributeList &Attrs);
227
228 /// Emit a call to the unary function DoubleFn, FloatFn or LongDoubleFn,
229 /// depending of the type of Op.
231 LibFunc DoubleFn, LibFunc FloatFn,
232 LibFunc LongDoubleFn, IRBuilderBase &B,
233 const AttributeList &Attrs);
234
235 /// Emit a call to the binary function named 'Name' (e.g. 'fmin'). This
236 /// function is known to take type matching 'Op1' and 'Op2' and return one
237 /// value with the same type. If 'Op1/Op2' are long double, 'l' is added as
238 /// the suffix of name, if 'Op1/Op2' are float, we add a 'f' suffix.
240 const TargetLibraryInfo *TLI,
242 const AttributeList &Attrs);
243
244 /// Emit a call to the binary function DoubleFn, FloatFn or LongDoubleFn,
245 /// depending of the type of Op1.
247 const TargetLibraryInfo *TLI,
248 LibFunc DoubleFn, LibFunc FloatFn,
249 LibFunc LongDoubleFn, IRBuilderBase &B,
250 const AttributeList &Attrs);
251
252 /// Emit a call to the putchar function. This assumes that Char is an 'int'.
254 const TargetLibraryInfo *TLI);
255
256 /// Emit a call to the puts function. This assumes that Str is some pointer.
258 const TargetLibraryInfo *TLI);
259
260 /// Emit a call to the fputc function. This assumes that Char is an 'int', and
261 /// File is a pointer to FILE.
263 const TargetLibraryInfo *TLI);
264
265 /// Emit a call to the fputs function. Str is required to be a pointer and
266 /// File is a pointer to FILE.
268 const TargetLibraryInfo *TLI);
269
270 /// Emit a call to the fwrite function. This assumes that Ptr is a pointer,
271 /// Size is an 'size_t', and File is a pointer to FILE.
273 IRBuilderBase &B, const DataLayout &DL,
274 const TargetLibraryInfo *TLI);
275
276 /// Emit a call to the malloc function.
278 const TargetLibraryInfo *TLI);
279
280 /// Emit a call to the calloc function.
282 const TargetLibraryInfo &TLI, unsigned AddrSpace);
283
284 /// Emit a call to the hot/cold operator new function.
286 const TargetLibraryInfo *TLI, LibFunc NewFunc,
287 uint8_t HotCold);
290 const TargetLibraryInfo *TLI,
291 LibFunc NewFunc, uint8_t HotCold);
294 const TargetLibraryInfo *TLI,
295 LibFunc NewFunc, uint8_t HotCold);
297 Value *NoThrow, IRBuilderBase &B,
298 const TargetLibraryInfo *TLI,
299 LibFunc NewFunc,
300 uint8_t HotCold);
302 const TargetLibraryInfo *TLI,
303 LibFunc NewFunc, uint8_t HotCold);
306 const TargetLibraryInfo *TLI,
307 LibFunc NewFunc, uint8_t HotCold);
308}
309
310#endif
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_ABI
Definition: Compiler.h:213
return RetTy
std::string Name
uint64_t Size
#define F(x, y, z)
Definition: MD5.cpp:55
Machine Check Debug Module
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
Definition: DerivedTypes.h:170
Class to represent function types.
Definition: DerivedTypes.h:105
static LLVM_ABI FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
Common base class shared among various IRBuilders.
Definition: IRBuilder.h:114
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
Provides information about what library functions are available for the current target.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
LLVM Value Representation.
Definition: Value.h:75
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
LLVM_ABI Value * emitUnaryFloatFnCall(Value *Op, const TargetLibraryInfo *TLI, StringRef Name, IRBuilderBase &B, const AttributeList &Attrs)
Emit a call to the unary function named 'Name' (e.g.
LLVM_ABI Value * emitStrChr(Value *Ptr, char C, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the strchr function to the builder, for the specified pointer and character.
LLVM_ABI Value * emitPutChar(Value *Char, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the putchar function. This assumes that Char is an 'int'.
LLVM_ABI Value * emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the __memcpy_chk function to the builder.
LLVM_ABI Value * emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the strncpy function to the builder, for the specified pointer arguments and length.
LLVM_ABI Value * emitHotColdNewAlignedNoThrow(Value *Num, Value *Align, Value *NoThrow, IRBuilderBase &B, const TargetLibraryInfo *TLI, LibFunc NewFunc, uint8_t HotCold)
LLVM_ABI Value * emitSPrintf(Value *Dest, Value *Fmt, ArrayRef< Value * > VariadicArgs, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the sprintf function.
LLVM_ABI Value * emitMemRChr(Value *Ptr, Value *Val, Value *Len, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the memrchr function, analogously to emitMemChr.
LLVM_ABI Value * emitStrLCat(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the strlcat function.
LLVM_ABI bool hasFloatFn(const Module *M, const TargetLibraryInfo *TLI, Type *Ty, LibFunc DoubleFn, LibFunc FloatFn, LibFunc LongDoubleFn)
Check whether the overloaded floating point function corresponding to Ty is available.
LLVM_ABI bool inferNonMandatoryLibFuncAttrs(Module *M, StringRef Name, const TargetLibraryInfo &TLI)
Analyze the name and prototype of the given function and set any applicable attributes.
LLVM_ABI Value * emitStrNCat(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the strncat function.
LLVM_ABI bool isLibFuncEmittable(const Module *M, const TargetLibraryInfo *TLI, LibFunc TheLibFunc)
Check whether the library function is available on target and also that it in the current Module is a...
LLVM_ABI Value * emitVSNPrintf(Value *Dest, Value *Size, Value *Fmt, Value *VAList, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the vsnprintf function.
LLVM_ABI Value * emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the strncmp function to the builder.
LLVM_ABI Value * emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the memcmp function.
LLVM_ABI Value * emitBinaryFloatFnCall(Value *Op1, Value *Op2, const TargetLibraryInfo *TLI, StringRef Name, IRBuilderBase &B, const AttributeList &Attrs)
Emit a call to the binary function named 'Name' (e.g.
LLVM_ABI Value * emitFPutS(Value *Str, Value *File, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the fputs function.
LLVM_ABI Value * emitStrDup(Value *Ptr, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the strdup function to the builder, for the specified pointer.
LLVM_ABI Value * emitBCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the bcmp function.
LLVM_ABI void markRegisterParameterAttributes(Function *F)
LLVM_ABI StringRef getFloatFn(const Module *M, const TargetLibraryInfo *TLI, Type *Ty, LibFunc DoubleFn, LibFunc FloatFn, LibFunc LongDoubleFn, LibFunc &TheLibFunc)
Get the name of the overloaded floating point function corresponding to Ty.
LLVM_ABI FunctionCallee getOrInsertLibFunc(Module *M, const TargetLibraryInfo &TLI, LibFunc TheLibFunc, FunctionType *T, AttributeList AttributeList)
Calls getOrInsertFunction() and then makes sure to add mandatory argument attributes.
LLVM_ABI Value * emitStrLen(Value *Ptr, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the strlen function to the builder, for the specified pointer.
LLVM_ABI Value * emitFPutC(Value *Char, Value *File, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the fputc function.
LLVM_ABI Value * emitStpNCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the stpncpy function to the builder, for the specified pointer arguments and length.
LLVM_ABI Value * emitStrCat(Value *Dest, Value *Src, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the strcat function.
LLVM_ABI Value * emitCalloc(Value *Num, Value *Size, IRBuilderBase &B, const TargetLibraryInfo &TLI, unsigned AddrSpace)
Emit a call to the calloc function.
LLVM_ABI Value * emitVSPrintf(Value *Dest, Value *Fmt, Value *VAList, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the vsprintf function.
LLVM_ABI Value * emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the fwrite function.
LLVM_ABI Value * emitSNPrintf(Value *Dest, Value *Size, Value *Fmt, ArrayRef< Value * > Args, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the snprintf function.
LLVM_ABI Value * emitStpCpy(Value *Dst, Value *Src, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the stpcpy function to the builder, for the specified pointer arguments.
LLVM_ABI Value * emitWcsLen(Value *Ptr, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the wcslen function to the builder, for the specified pointer.
LLVM_ABI Value * emitHotColdNewNoThrow(Value *Num, Value *NoThrow, IRBuilderBase &B, const TargetLibraryInfo *TLI, LibFunc NewFunc, uint8_t HotCold)
LLVM_ABI Value * emitMalloc(Value *Num, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the malloc function.
LLVM_ABI Value * emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the memchr function.
LLVM_ABI Value * emitHotColdNewAligned(Value *Num, Value *Align, IRBuilderBase &B, const TargetLibraryInfo *TLI, LibFunc NewFunc, uint8_t HotCold)
LLVM_ABI Value * emitPutS(Value *Str, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the puts function. This assumes that Str is some pointer.
LLVM_ABI Value * emitMemCCpy(Value *Ptr1, Value *Ptr2, Value *Val, Value *Len, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the memccpy function.
LLVM_ABI Value * emitHotColdSizeReturningNew(Value *Num, IRBuilderBase &B, const TargetLibraryInfo *TLI, LibFunc NewFunc, uint8_t HotCold)
LLVM_ABI Value * emitHotColdNew(Value *Num, IRBuilderBase &B, const TargetLibraryInfo *TLI, LibFunc NewFunc, uint8_t HotCold)
Emit a call to the hot/cold operator new function.
LLVM_ABI Value * emitStrLCpy(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the strlcpy function.
LLVM_ABI Value * emitHotColdSizeReturningNewAligned(Value *Num, Value *Align, IRBuilderBase &B, const TargetLibraryInfo *TLI, LibFunc NewFunc, uint8_t HotCold)
LLVM_ABI Value * emitStrCpy(Value *Dst, Value *Src, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the strcpy function to the builder, for the specified pointer arguments.
LLVM_ABI Value * emitMemPCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the mempcpy function.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39