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