LLVM 24.0.0git
RuntimeLibcalls.h
Go to the documentation of this file.
1//===- RuntimeLibcalls.h - Interface for runtime 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 implements a common interface to work with library calls into a
10// runtime that may be emitted by a given backend.
11//
12// FIXME: This should probably move to Analysis
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_IR_RUNTIME_LIBCALLS_H
17#define LLVM_IR_RUNTIME_LIBCALLS_H
18
19#include "llvm/ADT/ArrayRef.h"
20#include "llvm/ADT/Bitset.h"
21#include "llvm/ADT/Sequence.h"
23#include "llvm/IR/CallingConv.h"
24#include "llvm/IR/InstrTypes.h"
25#include "llvm/IR/PassManager.h"
31#include <optional>
32#include <string>
33
34/// TableGen will produce 2 enums, RTLIB::Libcall and
35/// RTLIB::LibcallImpl. RTLIB::Libcall describes abstract functionality the
36/// compiler may choose to access, RTLIB::LibcallImpl describes a particular ABI
37/// implementation, which includes a name and type signature.
38#define GET_RUNTIME_LIBCALL_ENUM
39#include "llvm/IR/RuntimeLibcalls.inc"
40
41namespace llvm {
42
43template <> struct enum_iteration_traits<RTLIB::Libcall> {
44 static constexpr bool is_iterable = true;
45};
46
47template <> struct enum_iteration_traits<RTLIB::LibcallImpl> {
48 static constexpr bool is_iterable = true;
49};
50
52class GlobalValue;
53class Module;
54class Type;
55
56namespace Intrinsic {
57typedef unsigned ID;
58}
59
60namespace RTLIB {
61
62// Return an iterator over all Libcall values.
63static inline auto libcalls() {
64 return enum_seq(static_cast<RTLIB::Libcall>(0), RTLIB::UNKNOWN_LIBCALL);
65}
66
67static inline auto libcall_impls() {
68 return enum_seq(static_cast<RTLIB::LibcallImpl>(1),
69 static_cast<RTLIB::LibcallImpl>(RTLIB::NumLibcallImpls));
70}
71
72/// Manage a bitset representing the list of available libcalls for a module.
73class LibcallImplBitset : public Bitset<RTLIB::NumLibcallImpls> {
74public:
75 constexpr LibcallImplBitset() = default;
77 const std::array<uint64_t, (RTLIB::NumLibcallImpls + 63) / 64> &Src)
78 : Bitset(Src) {}
79};
80
81/// A simple container for information about the supported runtime calls.
83private:
84 /// Bitset of libcalls a module may emit a call to.
85 LibcallImplBitset AvailableLibcallImpls;
86
87 unsigned WCharSize;
88 LongDoubleFormat LongDoubleFmt;
89
90 /// Whether math library calls may write errno.
91 bool MathErrno = true;
92
93 std::string ABIName;
94
95 /// Lower a returned C struct of two \p EltTy values for the target ABI.
96 /// \returns the IR return type, and the struct type if the struct is
97 /// returned through an sret argument (in which case the IR return type is
98 /// void), or nullptr if it is returned in registers.
99 std::pair<Type *, Type *> lowerCPairReturn(const Triple &TT,
100 Type *EltTy) const;
101
102public:
104
106
108 const Triple &TT,
110 unsigned WCharSize = 0, FloatABI::ABIType FloatABI = FloatABI::Default,
111 std::optional<LongDoubleFormat> LDblFormat = std::nullopt,
112 StringRef ABIName = "", VectorLibrary VecLib = VectorLibrary::NoLibrary);
113
114 // FIXME: The exception model, floating-point ABI and long double format are
115 // read from the "exception-model", "float-abi" and "long-double-type" module
116 // flags, but the ABIName/VecLib parameters are still TargetOptions values
117 // that are not yet represented in the IR. Delete these parameters (and build
118 // everything from the Module) once those fields are migrated to module flags.
120 const Module &M, StringRef ABIName = "",
122
123 LLVM_ABI bool invalidate(Module &M, const PreservedAnalyses &PA,
124 ModuleAnalysisManager::Invalidator &);
125
126 /// Get the libcall routine name for the specified libcall implementation.
127 static StringRef getLibcallImplName(RTLIB::LibcallImpl CallImpl) {
128 if (CallImpl == RTLIB::Unsupported)
129 return StringRef();
130 return StringRef(RuntimeLibcallImplNameTable.getCString(
131 RuntimeLibcallNameOffsetTable[CallImpl]),
132 RuntimeLibcallNameSizeTable[CallImpl]);
133 }
134
135 /// Set the CallingConv that should be used for the specified libcall
136 /// implementation
138 LibcallImplCallingConvs[Call] = CC;
139 }
140
141 /// Get the CallingConv that should be used for the specified libcall.
143 return LibcallImplCallingConvs[Call];
144 }
145
146 /// Return the libcall provided by \p Impl
147 LLVM_ABI RTLIB::Libcall getLibcallFromImpl(RTLIB::LibcallImpl Impl) const;
148
149 /// Return the runtime libcall that the floating-point math intrinsic \p ID
150 /// may be lowered to, or RTLIB::UNKNOWN_LIBCALL if there is no such mapping.
151 ///
152 /// \p FTy must be the intrinsic's call signature.
154 FunctionType *FTy);
155
156 /// Return the runtime libraries enabled by default for \p TT.
159
161 return AvailableLibcallImpls.count();
162 }
163
164 bool isAvailable(RTLIB::LibcallImpl Impl) const {
165 return AvailableLibcallImpls.test(Impl);
166 }
167
168 void setAvailable(RTLIB::LibcallImpl Impl) {
169 AvailableLibcallImpls.set(Impl);
170 }
171
172 void setUnavailable(RTLIB::LibcallImpl Impl) {
173 AvailableLibcallImpls.reset(Impl);
174 }
175
176 /// Check if a function name is a recognized runtime call of any kind. This
177 /// does not consider if this call is available for any current compilation,
178 /// just that it is a known call somewhere. This returns the set of all
179 /// LibcallImpls which match the name; multiple implementations with the same
180 /// name may exist but differ in interpretation based on the target context.
181 ///
182 /// Generated by tablegen.
185 // Inlining the early exit on the string name appears to be worthwhile when
186 // querying a real set of symbols
187#define GET_LOOKUP_LIBCALL_IMPL_NAME_BODY
188#include "llvm/IR/RuntimeLibcalls.inc"
189 }
190
191 /// Check if this is valid libcall for the current module, otherwise
192 /// RTLIB::Unsupported.
193 RTLIB::LibcallImpl getSupportedLibcallImpl(StringRef FuncName) const {
194 for (RTLIB::LibcallImpl Impl : lookupLibcallImplName(FuncName)) {
195 if (isAvailable(Impl))
196 return Impl;
197 }
198
199 return RTLIB::Unsupported;
200 }
201
202 /// Get size of a C-level int or unsigned int, in bits.
203 // FIXME Could move into Triple class.
204 static unsigned getIntSize(const Triple &TT) {
205 return TT.isArch16Bit() ? 16 : 32;
206 }
207
208 /// Determine the extension attributes needed for i32 parameters and returns
209 /// corresponding to C-level int or unsigned int on \p T.
210 LLVM_ABI static void initExtensionsForTriple(bool &ShouldExtI32Param,
211 bool &ShouldExtI32Return,
212 bool &ShouldSignExtI32Param,
213 bool &ShouldSignExtI32Return,
214 const Triple &T);
215
216 /// Determine whether bool parameters and returns need zeroext on \p T.
217 LLVM_ABI static void initBoolExtensionsForTriple(bool &ShouldZExtBoolParam,
218 bool &ShouldZExtBoolReturn,
219 const Triple &T);
220
221 /// Get the C-type function signature of the Libcall if provided,
222 /// and convert it to IR FunctionType and Attributes.
223 /// If the C-type function signature is not provided, return nullptr.
224 LLVM_ABI std::pair<FunctionType *, AttributeList>
225 getDefaultFunctionTy(LLVMContext &Ctx, const Triple &TT, const DataLayout &DL,
226 RTLIB::LibcallImpl LibcallImpl) const;
227
228 /// \returns the function type and attributes for the \p LibcallImpl,
229 /// depending on the target \p TT. If the function has incomplete type
230 /// information, return nullptr for the function type.
231 LLVM_ABI std::pair<FunctionType *, AttributeList>
232 getFunctionTy(LLVMContext &Ctx, const Triple &TT, const DataLayout &DL,
233 RTLIB::LibcallImpl LibcallImpl) const;
234
235 /// \returns the value type of \p LibcallImpl if it is a global variable
236 /// rather than a function, or nullptr otherwise.
238 const DataLayout &DL,
239 RTLIB::LibcallImpl LibcallImpl) const;
240
241 /// \returns the declaration of \p LibcallImpl in \p M. If \p M does not
242 /// contain a global with its name, a function is inserted with the
243 /// signature, attributes and calling convention of \p LibcallImpl. If the
244 /// signature is unknown, the function is declared as void(...).
246 getOrInsertLibcallDecl(Module &M, RTLIB::LibcallImpl LibcallImpl) const;
247
248 /// \returns the global for the variable \p LibcallImpl in \p M. If \p M does
249 /// not contain a global with its name, a global variable is inserted with
250 /// the type and thread local mode of \p LibcallImpl.
252 getOrInsertImplGlobal(Module &M, RTLIB::LibcallImpl LibcallImpl) const;
253
254 /// \returns true if \p LibcallImpl is a global variable rather than a
255 /// function.
256 static bool isVariable(RTLIB::LibcallImpl LibcallImpl) {
257 RTLIB::FuncArgTypeID ID = SignatureTable[SignatureOffset[LibcallImpl]];
258 return ID == RTLIB::Variable || ID == RTLIB::ThreadLocalVariable;
259 }
260
261 /// \returns true if \p LibcallImpl is a thread local variable.
262 static bool isThreadLocalVariable(RTLIB::LibcallImpl LibcallImpl) {
263 return SignatureTable[SignatureOffset[LibcallImpl]] ==
264 RTLIB::ThreadLocalVariable;
265 }
266
267 /// Return the unmasked variant of the masked vector libcall \p LC, or
268 /// RTLIB::UNKNOWN_LIBCALL if \p LC is not a masked vector libcall.
269 LLVM_ABI static RTLIB::Libcall getUnmaskedVectorLibcall(RTLIB::Libcall LC);
270
271 /// Return the masked variant of the vector libcall \p LC, or
272 /// RTLIB::UNKNOWN_LIBCALL if \p LC has no masked variant.
273 LLVM_ABI static RTLIB::Libcall getMaskedVectorLibcall(RTLIB::Libcall LC);
274
275 /// Returns true if the function has a vector mask argument, which is assumed
276 /// to be the last argument.
277 static bool hasVectorMaskArgument(RTLIB::LibcallImpl Impl) {
278 return getUnmaskedVectorLibcall(ImplToLibcall[Impl]) !=
279 RTLIB::UNKNOWN_LIBCALL;
280 }
281
282private:
283 // Generated per-library setup helpers, dispatched from
284 // setTargetRuntimeLibcallSets.
285#define GET_RUNTIME_LIBCALLS_INFO_MEMBER_DECLS
286#include "llvm/IR/RuntimeLibcalls.inc"
287
288 LLVM_ABI static iota_range<RTLIB::LibcallImpl>
289 lookupLibcallImplNameImpl(StringRef Name);
290
291 static_assert(static_cast<int>(CallingConv::C) == 0,
292 "default calling conv should be encoded as 0");
293
294 /// Stores the CallingConv that should be used for each libcall
295 /// implementation.;
296 CallingConv::ID LibcallImplCallingConvs[RTLIB::NumLibcallImpls] = {};
297
298 /// Names of concrete implementations of runtime calls. e.g. __ashlsi3 for
299 /// SHL_I32
300 LLVM_ABI static const char RuntimeLibcallImplNameTableStorage[];
301 LLVM_ABI static const StringTable RuntimeLibcallImplNameTable;
302 LLVM_ABI static const uint16_t RuntimeLibcallNameOffsetTable[];
303 LLVM_ABI static const uint8_t RuntimeLibcallNameSizeTable[];
304
305 /// Function signature of runtime lib calls
306 LLVM_ABI static const FuncArgTypeID SignatureTable[];
307 LLVM_ABI static const uint16_t SignatureOffset[];
308
309 /// Map from a concrete LibcallImpl implementation to its RTLIB::Libcall kind.
310 LLVM_ABI static const RTLIB::Libcall ImplToLibcall[RTLIB::NumLibcallImpls];
311
312 RTLIB::Libcall selectLongDoubleLibcall(RTLIB::Libcall F80,
313 RTLIB::Libcall F128,
314 RTLIB::Libcall PPCF128) const;
315
316 /// Utility function for tablegenerated lookup function. Return a range of
317 /// enum values that apply for the function name at \p NameOffsetEntry with
318 /// the value \p StrOffset.
319 static inline iota_range<RTLIB::LibcallImpl>
320 libcallImplNameHit(uint16_t NameOffsetEntry, uint16_t StrOffset);
321
323 static bool isAAPCS_ABI(const Triple &TT, StringRef ABIName);
324
325 /// Return whether the runtime library \p Library is available for the
326 /// current module.
327 bool isLibraryAvailable(RTLIB::RuntimeLibrary Library) const;
328
329 /// Set the calling convention and enable the default runtime libraries for
330 /// \p TT.
331 void setTargetRuntimeLibcallSets(const Triple &TT,
332 ExceptionHandling ExceptionModel,
333 FloatABI::ABIType FloatABI,
334 StringRef ABIName,
336
337 /// Set default libcall names. If a target wants to opt-out of a libcall it
338 /// should be placed here.
339 LLVM_ABI void initLibcalls(const Triple &TT, ExceptionHandling ExceptionModel,
340 FloatABI::ABIType FloatABI, StringRef ABIName,
342};
343
344} // namespace RTLIB
345
346} // namespace llvm
347
348#endif // LLVM_IR_RUNTIME_LIBCALLS_H
uint64_t uint64_t
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Atomic ordering constants.
#define LLVM_ABI
Definition Compiler.h:211
#define LLVM_READONLY
Definition Compiler.h:334
This header defines various interfaces for pass management in LLVM.
#define T
RISCVCC::CondCode CC
Provides some synthesis utilities to produce sequences of values.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
constexpr Bitset(const std::array< uint64_t,(NumBits+63)/64 > &B)
Definition Bitset.h:56
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
Class to represent function types.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:76
Tracks which library functions to use for a particular subtarget or function.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
Manage a bitset representing the list of available libcalls for a module.
constexpr LibcallImplBitset(const std::array< uint64_t,(RTLIB::NumLibcallImpls+63)/64 > &Src)
constexpr LibcallImplBitset()=default
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:48
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
CallInst * Call
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
This namespace contains an enum with a value for every intrinsic/builtin function known by LLVM.
static auto libcall_impls()
static auto libcalls()
This is an optimization pass for GlobalISel generic memory operations.
LongDoubleFormat
The floating-point format used for the target's "long double" type.
Definition CodeGen.h:117
constexpr auto enum_seq(EnumT Begin, EnumT End)
Iterate over an enum type from Begin up to - but not including - End.
Definition Sequence.h:373
ExceptionHandling
Definition CodeGen.h:54
@ Default
Not specified; resolve to the target's default model.
Definition CodeGen.h:55
VectorLibrary
List of known vector-functions libraries.
LLVM_ABI FunctionCallee getOrInsertLibcallDecl(Module &M, RTLIB::LibcallImpl LibcallImpl) const
static bool isVariable(RTLIB::LibcallImpl LibcallImpl)
static LLVM_ABI ArrayRef< RTLIB::RuntimeLibrary > getDefaultRuntimeLibraries(const Triple &TT)
Return the runtime libraries enabled by default for TT.
LLVM_ABI RTLIB::Libcall getLibcallFromImpl(RTLIB::LibcallImpl Impl) const
Return the libcall provided by Impl.
LLVM_ABI GlobalValue * getOrInsertImplGlobal(Module &M, RTLIB::LibcallImpl LibcallImpl) const
CallingConv::ID getLibcallImplCallingConv(RTLIB::LibcallImpl Call) const
Get the CallingConv that should be used for the specified libcall.
static unsigned getIntSize(const Triple &TT)
Get size of a C-level int or unsigned int, in bits.
static bool hasVectorMaskArgument(RTLIB::LibcallImpl Impl)
Returns true if the function has a vector mask argument, which is assumed to be the last argument.
static LLVM_ABI RTLIB::Libcall getLibcallForIntrinsic(Intrinsic::ID ID, FunctionType *FTy)
Return the runtime libcall that the floating-point math intrinsic ID may be lowered to,...
unsigned getNumAvailableLibcallImpls() const
bool isAvailable(RTLIB::LibcallImpl Impl) const
LLVM_ABI Type * getVariableType(LLVMContext &Ctx, const Triple &TT, const DataLayout &DL, RTLIB::LibcallImpl LibcallImpl) const
LLVM_ABI std::pair< FunctionType *, AttributeList > getFunctionTy(LLVMContext &Ctx, const Triple &TT, const DataLayout &DL, RTLIB::LibcallImpl LibcallImpl) const
void setAvailable(RTLIB::LibcallImpl Impl)
static LLVM_ABI void initExtensionsForTriple(bool &ShouldExtI32Param, bool &ShouldExtI32Return, bool &ShouldSignExtI32Param, bool &ShouldSignExtI32Return, const Triple &T)
Determine the extension attributes needed for i32 parameters and returns corresponding to C-level int...
void setUnavailable(RTLIB::LibcallImpl Impl)
static bool isThreadLocalVariable(RTLIB::LibcallImpl LibcallImpl)
static iota_range< RTLIB::LibcallImpl > lookupLibcallImplName(StringRef Name)
Check if a function name is a recognized runtime call of any kind.
static LLVM_ABI RTLIB::Libcall getUnmaskedVectorLibcall(RTLIB::Libcall LC)
Return the unmasked variant of the masked vector libcall LC, or RTLIB::UNKNOWN_LIBCALL if LC is not a...
RTLIB::LibcallImpl getSupportedLibcallImpl(StringRef FuncName) const
Check if this is valid libcall for the current module, otherwise RTLIB::Unsupported.
static LLVM_ABI void initBoolExtensionsForTriple(bool &ShouldZExtBoolParam, bool &ShouldZExtBoolReturn, const Triple &T)
Determine whether bool parameters and returns need zeroext on T.
static StringRef getLibcallImplName(RTLIB::LibcallImpl CallImpl)
Get the libcall routine name for the specified libcall implementation.
void setLibcallImplCallingConv(RTLIB::LibcallImpl Call, CallingConv::ID CC)
Set the CallingConv that should be used for the specified libcall implementation.
LLVM_ABI std::pair< FunctionType *, AttributeList > getDefaultFunctionTy(LLVMContext &Ctx, const Triple &TT, const DataLayout &DL, RTLIB::LibcallImpl LibcallImpl) const
Get the C-type function signature of the Libcall if provided, and convert it to IR FunctionType and A...
LLVM_ABI bool invalidate(Module &M, const PreservedAnalyses &PA, ModuleAnalysisManager::Invalidator &)
static LLVM_ABI RTLIB::Libcall getMaskedVectorLibcall(RTLIB::Libcall LC)
Return the masked variant of the vector libcall LC, or RTLIB::UNKNOWN_LIBCALL if LC has no masked var...