LLVM 20.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
18
19namespace llvm {
20 class Value;
21 class DataLayout;
22 class IRBuilderBase;
23
24 /// Analyze the name and prototype of the given function and set any
25 /// applicable attributes. Note that this merely helps optimizations on an
26 /// already existing function but does not consider mandatory attributes.
27 ///
28 /// If the library function is unavailable, this doesn't modify it.
29 ///
30 /// Returns true if any attributes were set and false otherwise.
31 bool inferNonMandatoryLibFuncAttrs(Module *M, StringRef Name,
32 const TargetLibraryInfo &TLI);
33 bool inferNonMandatoryLibFuncAttrs(Function &F, const TargetLibraryInfo &TLI);
34
35 /// Calls getOrInsertFunction() and then makes sure to add mandatory
36 /// argument attributes.
37 FunctionCallee getOrInsertLibFunc(Module *M, const TargetLibraryInfo &TLI,
38 LibFunc TheLibFunc, FunctionType *T,
39 AttributeList AttributeList);
40 FunctionCallee getOrInsertLibFunc(Module *M, const TargetLibraryInfo &TLI,
41 LibFunc TheLibFunc, FunctionType *T);
42 template <typename... ArgsTy>
45 Type *RetTy, ArgsTy... Args) {
46 SmallVector<Type*, sizeof...(ArgsTy)> ArgTys{Args...};
47 return getOrInsertLibFunc(M, TLI, TheLibFunc,
48 FunctionType::get(RetTy, ArgTys, false),
50 }
51 /// Same as above, but without the attributes.
52 template <typename... ArgsTy>
54 LibFunc TheLibFunc, Type *RetTy, ArgsTy... Args) {
55 return getOrInsertLibFunc(M, TLI, TheLibFunc, AttributeList{}, RetTy,
56 Args...);
57 }
58 // Avoid an incorrect ordering that'd otherwise compile incorrectly.
59 template <typename... ArgsTy>
60 FunctionCallee
63 FunctionType *Invalid, ArgsTy... Args) = delete;
64
65 // Handle -mregparm for the given function.
66 // Note that this function is a rough approximation that only works for simple
67 // function signatures; it does not apply other relevant attributes for
68 // function signatures, including sign/zero-extension for arguments and return
69 // values.
71
72 /// Check whether the library function is available on target and also that
73 /// it in the current Module is a Function with the right type.
74 bool isLibFuncEmittable(const Module *M, const TargetLibraryInfo *TLI,
75 LibFunc TheLibFunc);
76 bool isLibFuncEmittable(const Module *M, const TargetLibraryInfo *TLI,
78
79 /// Check whether the overloaded floating point function
80 /// corresponding to \a Ty is available.
81 bool hasFloatFn(const Module *M, const TargetLibraryInfo *TLI, Type *Ty,
82 LibFunc DoubleFn, LibFunc FloatFn, LibFunc LongDoubleFn);
83
84 /// Get the name of the overloaded floating point function
85 /// corresponding to \a Ty. Return the LibFunc in \a TheLibFunc.
86 StringRef getFloatFn(const Module *M, const TargetLibraryInfo *TLI, Type *Ty,
87 LibFunc DoubleFn, LibFunc FloatFn, LibFunc LongDoubleFn,
88 LibFunc &TheLibFunc);
89
90 /// Emit a call to the strlen function to the builder, for the specified
91 /// pointer. Ptr is required to be some pointer type, and the return value has
92 /// 'size_t' type.
94 const TargetLibraryInfo *TLI);
95
96 /// Emit a call to the strdup function to the builder, for the specified
97 /// pointer. Ptr is required to be some pointer type, and the return value has
98 /// 'i8*' type.
100
101 /// Emit a call to the strchr function to the builder, for the specified
102 /// pointer and character. Ptr is required to be some pointer type, and the
103 /// return value has 'i8*' type.
105 const TargetLibraryInfo *TLI);
106
107 /// Emit a call to the strncmp function to the builder.
108 Value *emitStrNCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B,
109 const DataLayout &DL, const TargetLibraryInfo *TLI);
110
111 /// Emit a call to the strcpy function to the builder, for the specified
112 /// pointer arguments.
114 const TargetLibraryInfo *TLI);
115
116 /// Emit a call to the stpcpy function to the builder, for the specified
117 /// pointer arguments.
119 const TargetLibraryInfo *TLI);
120
121 /// Emit a call to the strncpy function to the builder, for the specified
122 /// pointer arguments and length.
123 Value *emitStrNCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B,
124 const TargetLibraryInfo *TLI);
125
126 /// Emit a call to the stpncpy function to the builder, for the specified
127 /// pointer arguments and length.
128 Value *emitStpNCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B,
129 const TargetLibraryInfo *TLI);
130
131 /// Emit a call to the __memcpy_chk function to the builder. This expects that
132 /// the Len and ObjSize have type 'size_t' and Dst/Src are pointers.
133 Value *emitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
134 IRBuilderBase &B, const DataLayout &DL,
135 const TargetLibraryInfo *TLI);
136
137 /// Emit a call to the mempcpy function.
138 Value *emitMemPCpy(Value *Dst, Value *Src, Value *Len, IRBuilderBase &B,
139 const DataLayout &DL, const TargetLibraryInfo *TLI);
140
141 /// Emit a call to the memchr function. This assumes that Ptr is a pointer,
142 /// Val is an 'int' value, and Len is an 'size_t' value.
144 const DataLayout &DL, const TargetLibraryInfo *TLI);
145
146 /// Emit a call to the memrchr function, analogously to emitMemChr.
148 const DataLayout &DL, const TargetLibraryInfo *TLI);
149
150 /// Emit a call to the memcmp function.
151 Value *emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B,
152 const DataLayout &DL, const TargetLibraryInfo *TLI);
153
154 /// Emit a call to the bcmp function.
155 Value *emitBCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B,
156 const DataLayout &DL, const TargetLibraryInfo *TLI);
157
158 /// Emit a call to the memccpy function.
159 Value *emitMemCCpy(Value *Ptr1, Value *Ptr2, Value *Val, Value *Len,
160 IRBuilderBase &B, const TargetLibraryInfo *TLI);
161
162 /// Emit a call to the snprintf function.
163 Value *emitSNPrintf(Value *Dest, Value *Size, Value *Fmt,
165 const TargetLibraryInfo *TLI);
166
167 /// Emit a call to the sprintf function.
168 Value *emitSPrintf(Value *Dest, Value *Fmt, ArrayRef<Value *> VariadicArgs,
169 IRBuilderBase &B, const TargetLibraryInfo *TLI);
170
171 /// Emit a call to the strcat function.
172 Value *emitStrCat(Value *Dest, Value *Src, IRBuilderBase &B,
173 const TargetLibraryInfo *TLI);
174
175 /// Emit a call to the strlcpy function.
177 const TargetLibraryInfo *TLI);
178
179 /// Emit a call to the strlcat function.
181 const TargetLibraryInfo *TLI);
182
183 /// Emit a call to the strncat function.
185 const TargetLibraryInfo *TLI);
186
187 /// Emit a call to the vsnprintf function.
188 Value *emitVSNPrintf(Value *Dest, Value *Size, Value *Fmt, Value *VAList,
189 IRBuilderBase &B, const TargetLibraryInfo *TLI);
190
191 /// Emit a call to the vsprintf function.
192 Value *emitVSPrintf(Value *Dest, Value *Fmt, Value *VAList, IRBuilderBase &B,
193 const TargetLibraryInfo *TLI);
194
195 /// Emit a call to the unary function named 'Name' (e.g. 'floor'). This
196 /// function is known to take a single of type matching 'Op' and returns one
197 /// value with the same type. If 'Op' is a long double, 'l' is added as the
198 /// suffix of name, if 'Op' is a float, we add a 'f' suffix.
201 const AttributeList &Attrs);
202
203 /// Emit a call to the unary function DoubleFn, FloatFn or LongDoubleFn,
204 /// depending of the type of Op.
206 LibFunc DoubleFn, LibFunc FloatFn,
207 LibFunc LongDoubleFn, IRBuilderBase &B,
208 const AttributeList &Attrs);
209
210 /// Emit a call to the binary function named 'Name' (e.g. 'fmin'). This
211 /// function is known to take type matching 'Op1' and 'Op2' and return one
212 /// value with the same type. If 'Op1/Op2' are long double, 'l' is added as
213 /// the suffix of name, if 'Op1/Op2' are float, we add a 'f' suffix.
215 const TargetLibraryInfo *TLI,
217 const AttributeList &Attrs);
218
219 /// Emit a call to the binary function DoubleFn, FloatFn or LongDoubleFn,
220 /// depending of the type of Op1.
222 const TargetLibraryInfo *TLI, LibFunc DoubleFn,
223 LibFunc FloatFn, LibFunc LongDoubleFn,
224 IRBuilderBase &B, const AttributeList &Attrs);
225
226 /// Emit a call to the putchar function. This assumes that Char is an 'int'.
228 const TargetLibraryInfo *TLI);
229
230 /// Emit a call to the puts function. This assumes that Str is some pointer.
232
233 /// Emit a call to the fputc function. This assumes that Char is an 'int', and
234 /// File is a pointer to FILE.
235 Value *emitFPutC(Value *Char, Value *File, IRBuilderBase &B,
236 const TargetLibraryInfo *TLI);
237
238 /// Emit a call to the fputs function. Str is required to be a pointer and
239 /// File is a pointer to FILE.
240 Value *emitFPutS(Value *Str, Value *File, IRBuilderBase &B,
241 const TargetLibraryInfo *TLI);
242
243 /// Emit a call to the fwrite function. This assumes that Ptr is a pointer,
244 /// Size is an 'size_t', and File is a pointer to FILE.
246 const DataLayout &DL, const TargetLibraryInfo *TLI);
247
248 /// Emit a call to the malloc function.
250 const TargetLibraryInfo *TLI);
251
252 /// Emit a call to the calloc function.
254 const TargetLibraryInfo &TLI);
255
256 /// Emit a call to the hot/cold operator new function.
258 const TargetLibraryInfo *TLI, LibFunc NewFunc,
259 uint8_t HotCold);
261 const TargetLibraryInfo *TLI, LibFunc NewFunc,
262 uint8_t HotCold);
264 const TargetLibraryInfo *TLI, LibFunc NewFunc,
265 uint8_t HotCold);
268 const TargetLibraryInfo *TLI,
269 LibFunc NewFunc, uint8_t HotCold);
271 const TargetLibraryInfo *TLI,
272 LibFunc NewFunc, uint8_t HotCold);
275 const TargetLibraryInfo *TLI,
276 LibFunc NewFunc, uint8_t HotCold);
277}
278
279#endif
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
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:168
Class to represent function types.
Definition: DerivedTypes.h:103
static 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:91
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
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:74
@ 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
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.
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.
Value * emitPutChar(Value *Char, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the putchar function. This assumes that Char is an 'int'.
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.
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.
Value * emitHotColdNewAlignedNoThrow(Value *Num, Value *Align, Value *NoThrow, IRBuilderBase &B, const TargetLibraryInfo *TLI, LibFunc NewFunc, uint8_t HotCold)
Value * emitCalloc(Value *Num, Value *Size, IRBuilderBase &B, const TargetLibraryInfo &TLI)
Emit a call to the calloc function.
Value * emitSPrintf(Value *Dest, Value *Fmt, ArrayRef< Value * > VariadicArgs, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the sprintf function.
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.
Value * emitStrLCat(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the strlcat function.
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.
bool inferNonMandatoryLibFuncAttrs(Module *M, StringRef Name, const TargetLibraryInfo &TLI)
Analyze the name and prototype of the given function and set any applicable attributes.
Value * emitStrNCat(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the strncat function.
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...
Value * emitVSNPrintf(Value *Dest, Value *Size, Value *Fmt, Value *VAList, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the vsnprintf function.
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.
Value * emitMemCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the memcmp function.
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.
Value * emitFPutS(Value *Str, Value *File, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the fputs function.
Value * emitStrDup(Value *Ptr, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the strdup function to the builder, for the specified pointer.
Value * emitBCmp(Value *Ptr1, Value *Ptr2, Value *Len, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the bcmp function.
void markRegisterParameterAttributes(Function *F)
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.
FunctionCallee getOrInsertLibFunc(Module *M, const TargetLibraryInfo &TLI, LibFunc TheLibFunc, FunctionType *T, AttributeList AttributeList)
Calls getOrInsertFunction() and then makes sure to add mandatory argument attributes.
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.
Value * emitFPutC(Value *Char, Value *File, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the fputc function.
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.
Value * emitStrCat(Value *Dest, Value *Src, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the strcat function.
Value * emitVSPrintf(Value *Dest, Value *Fmt, Value *VAList, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the vsprintf function.
Value * emitFWrite(Value *Ptr, Value *Size, Value *File, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the fwrite function.
Value * emitSNPrintf(Value *Dest, Value *Size, Value *Fmt, ArrayRef< Value * > Args, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the snprintf function.
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.
Value * emitHotColdNewNoThrow(Value *Num, Value *NoThrow, IRBuilderBase &B, const TargetLibraryInfo *TLI, LibFunc NewFunc, uint8_t HotCold)
Value * emitMalloc(Value *Num, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the malloc function.
Value * emitMemChr(Value *Ptr, Value *Val, Value *Len, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the memchr function.
Value * emitHotColdNewAligned(Value *Num, Value *Align, IRBuilderBase &B, const TargetLibraryInfo *TLI, LibFunc NewFunc, uint8_t HotCold)
Value * emitPutS(Value *Str, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the puts function. This assumes that Str is some pointer.
Value * emitMemCCpy(Value *Ptr1, Value *Ptr2, Value *Val, Value *Len, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the memccpy function.
Value * emitHotColdSizeReturningNew(Value *Num, IRBuilderBase &B, const TargetLibraryInfo *TLI, LibFunc NewFunc, uint8_t HotCold)
Value * emitHotColdNew(Value *Num, IRBuilderBase &B, const TargetLibraryInfo *TLI, LibFunc NewFunc, uint8_t HotCold)
Emit a call to the hot/cold operator new function.
Value * emitStrLCpy(Value *Dest, Value *Src, Value *Size, IRBuilderBase &B, const TargetLibraryInfo *TLI)
Emit a call to the strlcpy function.
Value * emitHotColdSizeReturningNewAligned(Value *Num, Value *Align, IRBuilderBase &B, const TargetLibraryInfo *TLI, LibFunc NewFunc, uint8_t HotCold)
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.
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