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