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"
30
31/// TableGen will produce 2 enums, RTLIB::Libcall and
32/// RTLIB::LibcallImpl. RTLIB::Libcall describes abstract functionality the
33/// compiler may choose to access, RTLIB::LibcallImpl describes a particular ABI
34/// implementation, which includes a name and type signature.
35#define GET_RUNTIME_LIBCALL_ENUM
36#include "llvm/IR/RuntimeLibcalls.inc"
37
38namespace llvm {
39
40template <> struct enum_iteration_traits<RTLIB::Libcall> {
41 static constexpr bool is_iterable = true;
42};
43
44template <> struct enum_iteration_traits<RTLIB::LibcallImpl> {
45 static constexpr bool is_iterable = true;
46};
47
49
50namespace RTLIB {
51
52// Return an iterator over all Libcall values.
53static inline auto libcalls() {
54 return enum_seq(static_cast<RTLIB::Libcall>(0), RTLIB::UNKNOWN_LIBCALL);
55}
56
57static inline auto libcall_impls() {
58 return enum_seq(static_cast<RTLIB::LibcallImpl>(1),
59 static_cast<RTLIB::LibcallImpl>(RTLIB::NumLibcallImpls));
60}
61
62/// Manage a bitset representing the list of available libcalls for a module.
63class LibcallImplBitset : public Bitset<RTLIB::NumLibcallImpls> {
64public:
65 constexpr LibcallImplBitset() = default;
67 const std::array<uint64_t, (RTLIB::NumLibcallImpls + 63) / 64> &Src)
68 : Bitset(Src) {}
69};
70
71/// A simple container for information about the supported runtime calls.
73private:
74 /// Bitset of libcalls a module may emit a call to.
75 LibcallImplBitset AvailableLibcallImpls;
76
77public:
79
81
82 explicit RuntimeLibcallsInfo(
83 const Triple &TT,
86 EABI EABIVersion = EABI::Default, StringRef ABIName = "");
87
88 explicit RuntimeLibcallsInfo(const Module &M);
89
90 bool invalidate(Module &M, const PreservedAnalyses &PA,
91 ModuleAnalysisManager::Invalidator &);
92
93 /// Get the libcall routine name for the specified libcall implementation.
94 static StringRef getLibcallImplName(RTLIB::LibcallImpl CallImpl) {
95 if (CallImpl == RTLIB::Unsupported)
96 return StringRef();
97 return StringRef(RuntimeLibcallImplNameTable.getCString(
98 RuntimeLibcallNameOffsetTable[CallImpl]),
99 RuntimeLibcallNameSizeTable[CallImpl]);
100 }
101
102 /// Set the CallingConv that should be used for the specified libcall
103 /// implementation
104 void setLibcallImplCallingConv(RTLIB::LibcallImpl Call, CallingConv::ID CC) {
105 LibcallImplCallingConvs[Call] = CC;
106 }
107
108 /// Get the CallingConv that should be used for the specified libcall.
110 return LibcallImplCallingConvs[Call];
111 }
112
113 /// Return the libcall provided by \p Impl
114 static RTLIB::Libcall getLibcallFromImpl(RTLIB::LibcallImpl Impl) {
115 return ImplToLibcall[Impl];
116 }
117
119 return AvailableLibcallImpls.count();
120 }
121
122 bool isAvailable(RTLIB::LibcallImpl Impl) const {
123 return AvailableLibcallImpls.test(Impl);
124 }
125
126 void setAvailable(RTLIB::LibcallImpl Impl) {
127 AvailableLibcallImpls.set(Impl);
128 }
129
130 /// Check if a function name is a recognized runtime call of any kind. This
131 /// does not consider if this call is available for any current compilation,
132 /// just that it is a known call somewhere. This returns the set of all
133 /// LibcallImpls which match the name; multiple implementations with the same
134 /// name may exist but differ in interpretation based on the target context.
135 ///
136 /// Generated by tablegen.
139 // Inlining the early exit on the string name appears to be worthwhile when
140 // querying a real set of symbols
141#define GET_LOOKUP_LIBCALL_IMPL_NAME_BODY
142#include "llvm/IR/RuntimeLibcalls.inc"
143 }
144
145 /// Check if this is valid libcall for the current module, otherwise
146 /// RTLIB::Unsupported.
147 LLVM_ABI RTLIB::LibcallImpl
149 for (RTLIB::LibcallImpl Impl : lookupLibcallImplName(FuncName)) {
150 if (isAvailable(Impl))
151 return Impl;
152 }
153
154 return RTLIB::Unsupported;
155 }
156
157 /// \returns the function type and attributes for the \p LibcallImpl,
158 /// depending on the target \p TT. If the function has incomplete type
159 /// information, return nullptr for the function type.
160 std::pair<FunctionType *, AttributeList>
161 getFunctionTy(LLVMContext &Ctx, const Triple &TT, const DataLayout &DL,
162 RTLIB::LibcallImpl LibcallImpl) const;
163
164 /// Returns true if the function has a vector mask argument, which is assumed
165 /// to be the last argument.
166 static bool hasVectorMaskArgument(RTLIB::LibcallImpl Impl);
167
168private:
170 lookupLibcallImplNameImpl(StringRef Name);
171
172 static_assert(static_cast<int>(CallingConv::C) == 0,
173 "default calling conv should be encoded as 0");
174
175 /// Stores the CallingConv that should be used for each libcall
176 /// implementation.;
177 CallingConv::ID LibcallImplCallingConvs[RTLIB::NumLibcallImpls] = {};
178
179 /// Names of concrete implementations of runtime calls. e.g. __ashlsi3 for
180 /// SHL_I32
181 LLVM_ABI static const char RuntimeLibcallImplNameTableStorage[];
182 LLVM_ABI static const StringTable RuntimeLibcallImplNameTable;
183 LLVM_ABI static const uint16_t RuntimeLibcallNameOffsetTable[];
184 LLVM_ABI static const uint8_t RuntimeLibcallNameSizeTable[];
185
186 /// Map from a concrete LibcallImpl implementation to its RTLIB::Libcall kind.
187 LLVM_ABI static const RTLIB::Libcall ImplToLibcall[RTLIB::NumLibcallImpls];
188
189 /// Utility function for tablegenerated lookup function. Return a range of
190 /// enum values that apply for the function name at \p NameOffsetEntry with
191 /// the value \p StrOffset.
193 libcallImplNameHit(uint16_t NameOffsetEntry, uint16_t StrOffset);
194
195 static bool darwinHasSinCosStret(const Triple &TT) {
196 if (!TT.isOSDarwin())
197 return false;
198
199 // Don't bother with 32 bit x86.
200 if (TT.getArch() == Triple::x86)
201 return false;
202 // Macos < 10.9 has no sincos_stret.
203 if (TT.isMacOSX())
204 return !TT.isMacOSXVersionLT(10, 9) && TT.isArch64Bit();
205 // iOS < 7.0 has no sincos_stret.
206 if (TT.isiOS())
207 return !TT.isOSVersionLT(7, 0);
208 // Any other darwin such as WatchOS/TvOS is new enough.
209 return true;
210 }
211
212 static bool hasAEABILibcalls(const Triple &TT) {
213 return TT.isTargetAEABI() || TT.isTargetGNUAEABI() ||
214 TT.isTargetMuslAEABI() || TT.isOSFuchsia() || TT.isAndroid();
215 }
216
218 static bool isAAPCS_ABI(const Triple &TT, StringRef ABIName);
219
220 static bool darwinHasExp10(const Triple &TT);
221
222 /// Return true if the target has sincosf/sincos/sincosl functions
223 static bool hasSinCos(const Triple &TT) {
224 return TT.isGNUEnvironment() || TT.isOSFuchsia() || TT.isAndroid();
225 }
226
227 static bool hasSinCos_f32_f64(const Triple &TT) {
228 return hasSinCos(TT) || TT.isPS();
229 }
230
231 /// Generated by tablegen.
232 void setTargetRuntimeLibcallSets(const Triple &TT,
233 ExceptionHandling ExceptionModel,
234 FloatABI::ABIType FloatABI, EABI ABIType,
235 StringRef ABIName);
236
237 /// Set default libcall names. If a target wants to opt-out of a libcall it
238 /// should be placed here.
239 LLVM_ABI void initLibcalls(const Triple &TT, ExceptionHandling ExceptionModel,
240 FloatABI::ABIType FloatABI, EABI ABIType,
241 StringRef ABIName);
242};
243
244} // namespace RTLIB
245
246} // namespace llvm
247
248#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
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 &)