LLVM 22.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
50
51namespace RTLIB {
52
53// Return an iterator over all Libcall values.
54static inline auto libcalls() {
55 return enum_seq(static_cast<RTLIB::Libcall>(0), RTLIB::UNKNOWN_LIBCALL);
56}
57
58static inline auto libcall_impls() {
59 return enum_seq(static_cast<RTLIB::LibcallImpl>(1),
60 static_cast<RTLIB::LibcallImpl>(RTLIB::NumLibcallImpls));
61}
62
63/// Manage a bitset representing the list of available libcalls for a module.
64class LibcallImplBitset : public Bitset<RTLIB::NumLibcallImpls> {
65public:
66 constexpr LibcallImplBitset() = default;
68 const std::array<uint64_t, (RTLIB::NumLibcallImpls + 63) / 64> &Src)
69 : Bitset(Src) {}
70};
71
72/// A simple container for information about the supported runtime calls.
74private:
75 /// Bitset of libcalls a module may emit a call to.
76 LibcallImplBitset AvailableLibcallImpls;
77
78public:
80
82
83 explicit RuntimeLibcallsInfo(
84 const Triple &TT,
87 EABI EABIVersion = EABI::Default, StringRef ABIName = "",
89
90 explicit RuntimeLibcallsInfo(const Module &M);
91
92 bool invalidate(Module &M, const PreservedAnalyses &PA,
93 ModuleAnalysisManager::Invalidator &);
94
95 /// Get the libcall routine name for the specified libcall implementation.
96 static StringRef getLibcallImplName(RTLIB::LibcallImpl CallImpl) {
97 if (CallImpl == RTLIB::Unsupported)
98 return StringRef();
99 return StringRef(RuntimeLibcallImplNameTable.getCString(
100 RuntimeLibcallNameOffsetTable[CallImpl]),
101 RuntimeLibcallNameSizeTable[CallImpl]);
102 }
103
104 /// Set the CallingConv that should be used for the specified libcall
105 /// implementation
106 void setLibcallImplCallingConv(RTLIB::LibcallImpl Call, CallingConv::ID CC) {
107 LibcallImplCallingConvs[Call] = CC;
108 }
109
110 /// Get the CallingConv that should be used for the specified libcall.
112 return LibcallImplCallingConvs[Call];
113 }
114
115 /// Return the libcall provided by \p Impl
116 static RTLIB::Libcall getLibcallFromImpl(RTLIB::LibcallImpl Impl) {
117 return ImplToLibcall[Impl];
118 }
119
121 return AvailableLibcallImpls.count();
122 }
123
124 bool isAvailable(RTLIB::LibcallImpl Impl) const {
125 return AvailableLibcallImpls.test(Impl);
126 }
127
128 void setAvailable(RTLIB::LibcallImpl Impl) {
129 AvailableLibcallImpls.set(Impl);
130 }
131
132 /// Check if a function name is a recognized runtime call of any kind. This
133 /// does not consider if this call is available for any current compilation,
134 /// just that it is a known call somewhere. This returns the set of all
135 /// LibcallImpls which match the name; multiple implementations with the same
136 /// name may exist but differ in interpretation based on the target context.
137 ///
138 /// Generated by tablegen.
141 // Inlining the early exit on the string name appears to be worthwhile when
142 // querying a real set of symbols
143#define GET_LOOKUP_LIBCALL_IMPL_NAME_BODY
144#include "llvm/IR/RuntimeLibcalls.inc"
145 }
146
147 /// Check if this is valid libcall for the current module, otherwise
148 /// RTLIB::Unsupported.
149 LLVM_ABI RTLIB::LibcallImpl
151 for (RTLIB::LibcallImpl Impl : lookupLibcallImplName(FuncName)) {
152 if (isAvailable(Impl))
153 return Impl;
154 }
155
156 return RTLIB::Unsupported;
157 }
158
159 /// \returns the function type and attributes for the \p LibcallImpl,
160 /// depending on the target \p TT. If the function has incomplete type
161 /// information, return nullptr for the function type.
162 std::pair<FunctionType *, AttributeList>
163 getFunctionTy(LLVMContext &Ctx, const Triple &TT, const DataLayout &DL,
164 RTLIB::LibcallImpl LibcallImpl) const;
165
166 /// Returns true if the function has a vector mask argument, which is assumed
167 /// to be the last argument.
168 static bool hasVectorMaskArgument(RTLIB::LibcallImpl Impl);
169
170private:
172 lookupLibcallImplNameImpl(StringRef Name);
173
174 static_assert(static_cast<int>(CallingConv::C) == 0,
175 "default calling conv should be encoded as 0");
176
177 /// Stores the CallingConv that should be used for each libcall
178 /// implementation.;
179 CallingConv::ID LibcallImplCallingConvs[RTLIB::NumLibcallImpls] = {};
180
181 /// Names of concrete implementations of runtime calls. e.g. __ashlsi3 for
182 /// SHL_I32
183 LLVM_ABI static const char RuntimeLibcallImplNameTableStorage[];
184 LLVM_ABI static const StringTable RuntimeLibcallImplNameTable;
185 LLVM_ABI static const uint16_t RuntimeLibcallNameOffsetTable[];
186 LLVM_ABI static const uint8_t RuntimeLibcallNameSizeTable[];
187
188 /// Map from a concrete LibcallImpl implementation to its RTLIB::Libcall kind.
189 LLVM_ABI static const RTLIB::Libcall ImplToLibcall[RTLIB::NumLibcallImpls];
190
191 /// Utility function for tablegenerated lookup function. Return a range of
192 /// enum values that apply for the function name at \p NameOffsetEntry with
193 /// the value \p StrOffset.
195 libcallImplNameHit(uint16_t NameOffsetEntry, uint16_t StrOffset);
196
197 static bool darwinHasSinCosStret(const Triple &TT) {
198 if (!TT.isOSDarwin())
199 return false;
200
201 // Don't bother with 32 bit x86.
202 if (TT.getArch() == Triple::x86)
203 return false;
204 // Macos < 10.9 has no sincos_stret.
205 if (TT.isMacOSX())
206 return !TT.isMacOSXVersionLT(10, 9) && TT.isArch64Bit();
207 // iOS < 7.0 has no sincos_stret.
208 if (TT.isiOS())
209 return !TT.isOSVersionLT(7, 0);
210 // Any other darwin such as WatchOS/TvOS is new enough.
211 return true;
212 }
213
214 static bool hasAEABILibcalls(const Triple &TT) {
215 return TT.isTargetAEABI() || TT.isTargetGNUAEABI() ||
216 TT.isTargetMuslAEABI() || TT.isOSFuchsia() || TT.isAndroid();
217 }
218
220 static bool isAAPCS_ABI(const Triple &TT, StringRef ABIName);
221
222 static bool darwinHasExp10(const Triple &TT);
223
224 /// Return true if the target has sincosf/sincos/sincosl functions
225 static bool hasSinCos(const Triple &TT) {
226 return TT.isGNUEnvironment() || TT.isOSFuchsia() || TT.isAndroid();
227 }
228
229 static bool hasSinCos_f32_f64(const Triple &TT) {
230 return hasSinCos(TT) || TT.isPS();
231 }
232
233 /// Generated by tablegen.
234 void setTargetRuntimeLibcallSets(const Triple &TT,
235 ExceptionHandling ExceptionModel,
236 FloatABI::ABIType FloatABI, EABI ABIType,
237 StringRef ABIName);
238
239 /// Set default libcall names. If a target wants to opt-out of a libcall it
240 /// should be placed here.
241 LLVM_ABI void initLibcalls(const Triple &TT, ExceptionHandling ExceptionModel,
242 FloatABI::ABIType FloatABI, EABI ABIType,
243 StringRef ABIName);
244};
245
246} // namespace RTLIB
247
248} // namespace llvm
249
250#endif // LLVM_IR_RUNTIME_LIBCALLS_H
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Atomic ordering constants.
#define LLVM_ABI
Definition Compiler.h:213
#define LLVM_READONLY
Definition Compiler.h:322
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:45
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
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
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
A table of densely packed, null-terminated strings indexed by offset.
Definition StringTable.h:33
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:47
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
static auto libcall_impls()
static auto libcalls()
This is an optimization pass for GlobalISel generic memory operations.
auto enum_seq(EnumT Begin, EnumT End)
Iterate over an enum type from Begin up to - but not including - End.
Definition Sequence.h:337
ExceptionHandling
Definition CodeGen.h:53
@ None
No exception support.
Definition CodeGen.h:54
VectorLibrary
List of known vector-functions libraries.
EABI
Definition CodeGen.h:73
static LLVM_ABI iota_range< RTLIB::LibcallImpl > lookupLibcallImplName(StringRef Name)
Check if a function name is a recognized runtime call of any kind.
CallingConv::ID getLibcallImplCallingConv(RTLIB::LibcallImpl Call) const
Get the CallingConv that should be used for the specified libcall.
unsigned getNumAvailableLibcallImpls() const
bool isAvailable(RTLIB::LibcallImpl Impl) const
std::pair< FunctionType *, AttributeList > getFunctionTy(LLVMContext &Ctx, const Triple &TT, const DataLayout &DL, RTLIB::LibcallImpl LibcallImpl) const
void setAvailable(RTLIB::LibcallImpl Impl)
static bool hasVectorMaskArgument(RTLIB::LibcallImpl Impl)
Returns true if the function has a vector mask argument, which is assumed to be the last argument.
LLVM_ABI 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.
bool invalidate(Module &M, const PreservedAnalyses &PA, ModuleAnalysisManager::Invalidator &)