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
32/// TableGen will produce 2 enums, RTLIB::Libcall and
33/// RTLIB::LibcallImpl. RTLIB::Libcall describes abstract functionality the
34/// compiler may choose to access, RTLIB::LibcallImpl describes a particular ABI
35/// implementation, which includes a name and type signature.
36#define GET_RUNTIME_LIBCALL_ENUM
37#include "llvm/IR/RuntimeLibcalls.inc"
38
39namespace llvm {
40
41template <> struct enum_iteration_traits<RTLIB::Libcall> {
42 static constexpr bool is_iterable = true;
43};
44
45template <> struct enum_iteration_traits<RTLIB::LibcallImpl> {
46 static constexpr bool is_iterable = true;
47};
48
50class Type;
51
52namespace Intrinsic {
53typedef unsigned ID;
54}
55
56namespace RTLIB {
57
58// Return an iterator over all Libcall values.
59static inline auto libcalls() {
60 return enum_seq(static_cast<RTLIB::Libcall>(0), RTLIB::UNKNOWN_LIBCALL);
61}
62
63static inline auto libcall_impls() {
64 return enum_seq(static_cast<RTLIB::LibcallImpl>(1),
65 static_cast<RTLIB::LibcallImpl>(RTLIB::NumLibcallImpls));
66}
67
68/// Manage a bitset representing the list of available libcalls for a module.
69class LibcallImplBitset : public Bitset<RTLIB::NumLibcallImpls> {
70public:
71 constexpr LibcallImplBitset() = default;
73 const std::array<uint64_t, (RTLIB::NumLibcallImpls + 63) / 64> &Src)
74 : Bitset(Src) {}
75};
76
77/// A simple container for information about the supported runtime calls.
79private:
80 /// Bitset of libcalls a module may emit a call to.
81 LibcallImplBitset AvailableLibcallImpls;
82
83public:
85
87
89 const Triple &TT,
93
94 // FIXME: The exception model and floating-point ABI are read from the
95 // "exception-model" and "float-abi" module flags, but the ABIName/VecLib
96 // parameters are still TargetOptions values that are not yet represented in
97 // the IR. Delete these parameters (and build everything from the Module) once
98 // those fields are migrated to module flags.
100 const Module &M, StringRef ABIName = "",
102
103 LLVM_ABI bool invalidate(Module &M, const PreservedAnalyses &PA,
104 ModuleAnalysisManager::Invalidator &);
105
106 /// Get the libcall routine name for the specified libcall implementation.
107 static StringRef getLibcallImplName(RTLIB::LibcallImpl CallImpl) {
108 if (CallImpl == RTLIB::Unsupported)
109 return StringRef();
110 return StringRef(RuntimeLibcallImplNameTable.getCString(
111 RuntimeLibcallNameOffsetTable[CallImpl]),
112 RuntimeLibcallNameSizeTable[CallImpl]);
113 }
114
115 /// Set the CallingConv that should be used for the specified libcall
116 /// implementation
117 void setLibcallImplCallingConv(RTLIB::LibcallImpl Call, CallingConv::ID CC) {
118 LibcallImplCallingConvs[Call] = CC;
119 }
120
121 /// Get the CallingConv that should be used for the specified libcall.
123 return LibcallImplCallingConvs[Call];
124 }
125
126 /// Return the libcall provided by \p Impl
127 static RTLIB::Libcall getLibcallFromImpl(RTLIB::LibcallImpl Impl) {
128 return ImplToLibcall[Impl];
129 }
130
131 /// Return the runtime libcall that the floating-point math intrinsic \p ID
132 /// may be lowered to, or RTLIB::UNKNOWN_LIBCALL if there is no such mapping.
133 ///
134 /// \p FTy must be the intrinsic's call signature.
136 FunctionType *FTy);
137
139 return AvailableLibcallImpls.count();
140 }
141
142 bool isAvailable(RTLIB::LibcallImpl Impl) const {
143 return AvailableLibcallImpls.test(Impl);
144 }
145
146 void setAvailable(RTLIB::LibcallImpl Impl) {
147 AvailableLibcallImpls.set(Impl);
148 }
149
150 void setUnavailable(RTLIB::LibcallImpl Impl) {
151 AvailableLibcallImpls.reset(Impl);
152 }
153
154 /// Check if a function name is a recognized runtime call of any kind. This
155 /// does not consider if this call is available for any current compilation,
156 /// just that it is a known call somewhere. This returns the set of all
157 /// LibcallImpls which match the name; multiple implementations with the same
158 /// name may exist but differ in interpretation based on the target context.
159 ///
160 /// Generated by tablegen.
163 // Inlining the early exit on the string name appears to be worthwhile when
164 // querying a real set of symbols
165#define GET_LOOKUP_LIBCALL_IMPL_NAME_BODY
166#include "llvm/IR/RuntimeLibcalls.inc"
167 }
168
169 /// Check if this is valid libcall for the current module, otherwise
170 /// RTLIB::Unsupported.
171 RTLIB::LibcallImpl getSupportedLibcallImpl(StringRef FuncName) const {
172 for (RTLIB::LibcallImpl Impl : lookupLibcallImplName(FuncName)) {
173 if (isAvailable(Impl))
174 return Impl;
175 }
176
177 return RTLIB::Unsupported;
178 }
179
180 /// \returns the function type and attributes for the \p LibcallImpl,
181 /// depending on the target \p TT. If the function has incomplete type
182 /// information, return nullptr for the function type.
183 LLVM_ABI std::pair<FunctionType *, AttributeList>
184 getFunctionTy(LLVMContext &Ctx, const Triple &TT, const DataLayout &DL,
185 RTLIB::LibcallImpl LibcallImpl) const;
186
187 /// Returns true if the function has a vector mask argument, which is assumed
188 /// to be the last argument.
189 LLVM_ABI static bool hasVectorMaskArgument(RTLIB::LibcallImpl Impl);
190
191private:
192 // Generated per-library setup helpers, dispatched from
193 // setTargetRuntimeLibcallSets.
194#define GET_RUNTIME_LIBCALLS_INFO_MEMBER_DECLS
195#include "llvm/IR/RuntimeLibcalls.inc"
196
197 LLVM_ABI static iota_range<RTLIB::LibcallImpl>
198 lookupLibcallImplNameImpl(StringRef Name);
199
200 static_assert(static_cast<int>(CallingConv::C) == 0,
201 "default calling conv should be encoded as 0");
202
203 /// Stores the CallingConv that should be used for each libcall
204 /// implementation.;
205 CallingConv::ID LibcallImplCallingConvs[RTLIB::NumLibcallImpls] = {};
206
207 /// Names of concrete implementations of runtime calls. e.g. __ashlsi3 for
208 /// SHL_I32
209 LLVM_ABI static const char RuntimeLibcallImplNameTableStorage[];
210 LLVM_ABI static const StringTable RuntimeLibcallImplNameTable;
211 LLVM_ABI static const uint16_t RuntimeLibcallNameOffsetTable[];
212 LLVM_ABI static const uint8_t RuntimeLibcallNameSizeTable[];
213
214 /// Map from a concrete LibcallImpl implementation to its RTLIB::Libcall kind.
215 LLVM_ABI static const RTLIB::Libcall ImplToLibcall[RTLIB::NumLibcallImpls];
216
217 /// Utility function for tablegenerated lookup function. Return a range of
218 /// enum values that apply for the function name at \p NameOffsetEntry with
219 /// the value \p StrOffset.
220 static inline iota_range<RTLIB::LibcallImpl>
221 libcallImplNameHit(uint16_t NameOffsetEntry, uint16_t StrOffset);
222
223 static bool darwinHasSinCosStret(const Triple &TT) {
224 if (!TT.isOSDarwin())
225 return false;
226
227 // Don't bother with 32 bit x86.
228 if (TT.getArch() == Triple::x86)
229 return false;
230 // Macos < 10.9 has no sincos_stret.
231 if (TT.isMacOSX())
232 return !TT.isMacOSXVersionLT(10, 9) && TT.isArch64Bit();
233 // iOS < 7.0 has no sincos_stret.
234 if (TT.isiOS())
235 return !TT.isOSVersionLT(7, 0);
236 // Any other darwin such as WatchOS/TvOS is new enough.
237 return true;
238 }
239
241 static bool isAAPCS_ABI(const Triple &TT, StringRef ABIName);
242
243 /// Return whether the runtime library \p LibraryName is available for the
244 /// current module.
245 bool isLibraryAvailable(StringRef LibraryName) const;
246
247 /// Generated by tablegen.
248 void setTargetRuntimeLibcallSets(const Triple &TT,
249 ExceptionHandling ExceptionModel,
250 FloatABI::ABIType FloatABI,
251 StringRef ABIName,
253
254 /// Set default libcall names. If a target wants to opt-out of a libcall it
255 /// should be placed here.
256 LLVM_ABI void initLibcalls(const Triple &TT, ExceptionHandling ExceptionModel,
257 FloatABI::ABIType FloatABI, StringRef ABIName,
259};
260
261} // namespace RTLIB
262
263} // namespace llvm
264
265#endif // LLVM_IR_RUNTIME_LIBCALLS_H
unsigned uint64_t
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Atomic ordering constants.
#define LLVM_ABI
Definition Compiler.h:215
#define LLVM_READONLY
Definition Compiler.h:330
This header defines various interfaces for pass management in LLVM.
Provides some synthesis utilities to produce sequences of values.
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:68
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
@ None
No exception support.
Definition CodeGen.h:56
VectorLibrary
List of known vector-functions libraries.
CallingConv::ID getLibcallImplCallingConv(RTLIB::LibcallImpl Call) const
Get the CallingConv that should be used for the specified libcall.
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 bool hasVectorMaskArgument(RTLIB::LibcallImpl Impl)
Returns true if the function has a vector mask argument, which is assumed to be the last argument.
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.
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.
void setLibcallImplCallingConv(RTLIB::LibcallImpl Call, CallingConv::ID CC)
Set the CallingConv that should be used for the specified libcall implementation.
static RTLIB::Libcall getLibcallFromImpl(RTLIB::LibcallImpl Impl)
Return the libcall provided by Impl.
LLVM_ABI bool invalidate(Module &M, const PreservedAnalyses &PA, ModuleAnalysisManager::Invalidator &)