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
33/// TableGen will produce 2 enums, RTLIB::Libcall and
34/// RTLIB::LibcallImpl. RTLIB::Libcall describes abstract functionality the
35/// compiler may choose to access, RTLIB::LibcallImpl describes a particular ABI
36/// implementation, which includes a name and type signature.
37#define GET_RUNTIME_LIBCALL_ENUM
38#include "llvm/IR/RuntimeLibcalls.inc"
39
40namespace llvm {
41
42template <> struct enum_iteration_traits<RTLIB::Libcall> {
43 static constexpr bool is_iterable = true;
44};
45
46template <> struct enum_iteration_traits<RTLIB::LibcallImpl> {
47 static constexpr bool is_iterable = true;
48};
49
51class Type;
52
53namespace Intrinsic {
54typedef unsigned ID;
55}
56
57namespace RTLIB {
58
59// Return an iterator over all Libcall values.
60static inline auto libcalls() {
61 return enum_seq(static_cast<RTLIB::Libcall>(0), RTLIB::UNKNOWN_LIBCALL);
62}
63
64static inline auto libcall_impls() {
65 return enum_seq(static_cast<RTLIB::LibcallImpl>(1),
66 static_cast<RTLIB::LibcallImpl>(RTLIB::NumLibcallImpls));
67}
68
69/// Manage a bitset representing the list of available libcalls for a module.
70class LibcallImplBitset : public Bitset<RTLIB::NumLibcallImpls> {
71public:
72 constexpr LibcallImplBitset() = default;
74 const std::array<uint64_t, (RTLIB::NumLibcallImpls + 63) / 64> &Src)
75 : Bitset(Src) {}
76};
77
78/// A simple container for information about the supported runtime calls.
80private:
81 /// Bitset of libcalls a module may emit a call to.
82 LibcallImplBitset AvailableLibcallImpls;
83
84 LongDoubleFormat LongDoubleFmt;
85
86public:
88
90
92 const Triple &TT,
95 std::optional<LongDoubleFormat> LDblFormat = std::nullopt,
96 StringRef ABIName = "", VectorLibrary VecLib = VectorLibrary::NoLibrary);
97
98 // FIXME: The exception model, floating-point ABI and long double format are
99 // read from the "exception-model", "float-abi" and "long-double-type" module
100 // flags, but the ABIName/VecLib parameters are still TargetOptions values
101 // that are not yet represented in the IR. Delete these parameters (and build
102 // everything from the Module) once those fields are migrated to module flags.
104 const Module &M, StringRef ABIName = "",
106
107 LLVM_ABI bool invalidate(Module &M, const PreservedAnalyses &PA,
108 ModuleAnalysisManager::Invalidator &);
109
110 /// Get the libcall routine name for the specified libcall implementation.
111 static StringRef getLibcallImplName(RTLIB::LibcallImpl CallImpl) {
112 if (CallImpl == RTLIB::Unsupported)
113 return StringRef();
114 return StringRef(RuntimeLibcallImplNameTable.getCString(
115 RuntimeLibcallNameOffsetTable[CallImpl]),
116 RuntimeLibcallNameSizeTable[CallImpl]);
117 }
118
119 /// Set the CallingConv that should be used for the specified libcall
120 /// implementation
122 LibcallImplCallingConvs[Call] = CC;
123 }
124
125 /// Get the CallingConv that should be used for the specified libcall.
127 return LibcallImplCallingConvs[Call];
128 }
129
130 /// Return the libcall provided by \p Impl
131 LLVM_ABI RTLIB::Libcall getLibcallFromImpl(RTLIB::LibcallImpl Impl) const;
132
133 /// Return the runtime libcall that the floating-point math intrinsic \p ID
134 /// may be lowered to, or RTLIB::UNKNOWN_LIBCALL if there is no such mapping.
135 ///
136 /// \p FTy must be the intrinsic's call signature.
138 FunctionType *FTy);
139
140 /// Return the runtime libraries enabled by default for \p TT.
143
145 return AvailableLibcallImpls.count();
146 }
147
148 bool isAvailable(RTLIB::LibcallImpl Impl) const {
149 return AvailableLibcallImpls.test(Impl);
150 }
151
152 void setAvailable(RTLIB::LibcallImpl Impl) {
153 AvailableLibcallImpls.set(Impl);
154 }
155
156 void setUnavailable(RTLIB::LibcallImpl Impl) {
157 AvailableLibcallImpls.reset(Impl);
158 }
159
160 /// Check if a function name is a recognized runtime call of any kind. This
161 /// does not consider if this call is available for any current compilation,
162 /// just that it is a known call somewhere. This returns the set of all
163 /// LibcallImpls which match the name; multiple implementations with the same
164 /// name may exist but differ in interpretation based on the target context.
165 ///
166 /// Generated by tablegen.
169 // Inlining the early exit on the string name appears to be worthwhile when
170 // querying a real set of symbols
171#define GET_LOOKUP_LIBCALL_IMPL_NAME_BODY
172#include "llvm/IR/RuntimeLibcalls.inc"
173 }
174
175 /// Check if this is valid libcall for the current module, otherwise
176 /// RTLIB::Unsupported.
177 RTLIB::LibcallImpl getSupportedLibcallImpl(StringRef FuncName) const {
178 for (RTLIB::LibcallImpl Impl : lookupLibcallImplName(FuncName)) {
179 if (isAvailable(Impl))
180 return Impl;
181 }
182
183 return RTLIB::Unsupported;
184 }
185
186 /// Get size of a C-level int or unsigned int, in bits.
187 // FIXME Could move into Triple class.
188 unsigned getIntSize(const Triple &TT) const {
189 return TT.isArch16Bit() ? 16 : 32;
190 }
191
192 /// Determine the extension attributes needed for i32 parameters and returns
193 /// corresponding to C-level int or unsigned int on \p T.
194 LLVM_ABI static void initExtensionsForTriple(bool &ShouldExtI32Param,
195 bool &ShouldExtI32Return,
196 bool &ShouldSignExtI32Param,
197 bool &ShouldSignExtI32Return,
198 const Triple &T);
199
200 /// Get the C-type function signature of the Libcall if provided,
201 /// and convert it to IR FunctionType and Attributes.
202 /// If the C-type function signature is not provided, return nullptr.
203 LLVM_ABI std::pair<FunctionType *, AttributeList>
204 getDefaultFunctionTy(LLVMContext &Ctx, const Triple &TT, const DataLayout &DL,
205 RTLIB::LibcallImpl LibcallImpl) const;
206
207 /// \returns the function type and attributes for the \p LibcallImpl,
208 /// depending on the target \p TT. If the function has incomplete type
209 /// information, return nullptr for the function type.
210 LLVM_ABI std::pair<FunctionType *, AttributeList>
211 getFunctionTy(LLVMContext &Ctx, const Triple &TT, const DataLayout &DL,
212 RTLIB::LibcallImpl LibcallImpl) const;
213
214 /// Return the unmasked variant of the masked vector libcall \p LC, or
215 /// RTLIB::UNKNOWN_LIBCALL if \p LC is not a masked vector libcall.
216 LLVM_ABI static RTLIB::Libcall getUnmaskedVectorLibcall(RTLIB::Libcall LC);
217
218 /// Return the masked variant of the vector libcall \p LC, or
219 /// RTLIB::UNKNOWN_LIBCALL if \p LC has no masked variant.
220 LLVM_ABI static RTLIB::Libcall getMaskedVectorLibcall(RTLIB::Libcall LC);
221
222 /// Returns true if the function has a vector mask argument, which is assumed
223 /// to be the last argument.
224 static bool hasVectorMaskArgument(RTLIB::LibcallImpl Impl) {
225 return getUnmaskedVectorLibcall(ImplToLibcall[Impl]) !=
226 RTLIB::UNKNOWN_LIBCALL;
227 }
228
229private:
230 // Generated per-library setup helpers, dispatched from
231 // setTargetRuntimeLibcallSets.
232#define GET_RUNTIME_LIBCALLS_INFO_MEMBER_DECLS
233#include "llvm/IR/RuntimeLibcalls.inc"
234
235 LLVM_ABI static iota_range<RTLIB::LibcallImpl>
236 lookupLibcallImplNameImpl(StringRef Name);
237
238 static_assert(static_cast<int>(CallingConv::C) == 0,
239 "default calling conv should be encoded as 0");
240
241 /// Stores the CallingConv that should be used for each libcall
242 /// implementation.;
243 CallingConv::ID LibcallImplCallingConvs[RTLIB::NumLibcallImpls] = {};
244
245 /// Names of concrete implementations of runtime calls. e.g. __ashlsi3 for
246 /// SHL_I32
247 LLVM_ABI static const char RuntimeLibcallImplNameTableStorage[];
248 LLVM_ABI static const StringTable RuntimeLibcallImplNameTable;
249 LLVM_ABI static const uint16_t RuntimeLibcallNameOffsetTable[];
250 LLVM_ABI static const uint8_t RuntimeLibcallNameSizeTable[];
251
252 /// Function signature of runtime lib calls
253 LLVM_ABI static const FuncArgTypeID SignatureTable[];
254 LLVM_ABI static const uint16_t SignatureOffset[];
255
256 /// Map from a concrete LibcallImpl implementation to its RTLIB::Libcall kind.
257 LLVM_ABI static const RTLIB::Libcall ImplToLibcall[RTLIB::NumLibcallImpls];
258
259 RTLIB::Libcall selectLongDoubleLibcall(RTLIB::Libcall F80,
260 RTLIB::Libcall F128,
261 RTLIB::Libcall PPCF128) const;
262
263 /// Utility function for tablegenerated lookup function. Return a range of
264 /// enum values that apply for the function name at \p NameOffsetEntry with
265 /// the value \p StrOffset.
266 static inline iota_range<RTLIB::LibcallImpl>
267 libcallImplNameHit(uint16_t NameOffsetEntry, uint16_t StrOffset);
268
269 static bool darwinHasSinCosStret(const Triple &TT) {
270 if (!TT.isOSDarwin())
271 return false;
272
273 // Don't bother with 32 bit x86.
274 if (TT.getArch() == Triple::x86)
275 return false;
276 // Macos < 10.9 has no sincos_stret.
277 if (TT.isMacOSX())
278 return !TT.isMacOSXVersionLT(10, 9) && TT.isArch64Bit();
279 // iOS < 7.0 has no sincos_stret.
280 if (TT.isiOS())
281 return !TT.isOSVersionLT(7, 0);
282 // Any other darwin such as WatchOS/TvOS is new enough.
283 return true;
284 }
285
287 static bool isAAPCS_ABI(const Triple &TT, StringRef ABIName);
288
289 /// Return whether the runtime library \p Library is available for the
290 /// current module.
291 bool isLibraryAvailable(RTLIB::RuntimeLibrary Library) const;
292
293 /// Set the calling convention and enable the default runtime libraries for
294 /// \p TT.
295 void setTargetRuntimeLibcallSets(const Triple &TT,
296 ExceptionHandling ExceptionModel,
297 FloatABI::ABIType FloatABI,
298 StringRef ABIName,
300
301 /// Set default libcall names. If a target wants to opt-out of a libcall it
302 /// should be placed here.
303 LLVM_ABI void initLibcalls(const Triple &TT, ExceptionHandling ExceptionModel,
304 FloatABI::ABIType FloatABI, StringRef ABIName,
306};
307
308} // namespace RTLIB
309
310} // namespace llvm
311
312#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
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.
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.
CallingConv::ID getLibcallImplCallingConv(RTLIB::LibcallImpl Call) const
Get the CallingConv that should be used for the specified libcall.
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 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 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 StringRef getLibcallImplName(RTLIB::LibcallImpl CallImpl)
Get the libcall routine name for the specified libcall implementation.
unsigned getIntSize(const Triple &TT) const
Get size of a C-level int or unsigned int, in bits.
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...