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
83 const Triple &TT,
86 EABI EABIVersion = EABI::Default, StringRef ABIName = "") {
87 // FIXME: The ExceptionModel parameter is to handle the field in
88 // TargetOptions. This interface fails to distinguish the forced disable
89 // case for targets which support exceptions by default. This should
90 // probably be a module flag and removed from TargetOptions.
91 if (ExceptionModel == ExceptionHandling::None)
92 ExceptionModel = TT.getDefaultExceptionHandling();
93
94 initLibcalls(TT, ExceptionModel, FloatABI, EABIVersion, ABIName);
95 }
96
97 explicit RuntimeLibcallsInfo(const Module &M);
98
99 bool invalidate(Module &M, const PreservedAnalyses &PA,
100 ModuleAnalysisManager::Invalidator &);
101
102 /// Get the libcall routine name for the specified libcall implementation.
103 static StringRef getLibcallImplName(RTLIB::LibcallImpl CallImpl) {
104 if (CallImpl == RTLIB::Unsupported)
105 return StringRef();
106 return StringRef(RuntimeLibcallImplNameTable.getCString(
107 RuntimeLibcallNameOffsetTable[CallImpl]),
108 RuntimeLibcallNameSizeTable[CallImpl]);
109 }
110
111 /// Set the CallingConv that should be used for the specified libcall
112 /// implementation
113 void setLibcallImplCallingConv(RTLIB::LibcallImpl Call, CallingConv::ID CC) {
114 LibcallImplCallingConvs[Call] = CC;
115 }
116
117 /// Get the CallingConv that should be used for the specified libcall.
119 return LibcallImplCallingConvs[Call];
120 }
121
122 /// Return the libcall provided by \p Impl
123 static RTLIB::Libcall getLibcallFromImpl(RTLIB::LibcallImpl Impl) {
124 return ImplToLibcall[Impl];
125 }
126
128 return AvailableLibcallImpls.count();
129 }
130
131 bool isAvailable(RTLIB::LibcallImpl Impl) const {
132 return AvailableLibcallImpls.test(Impl);
133 }
134
135 void setAvailable(RTLIB::LibcallImpl Impl) {
136 AvailableLibcallImpls.set(Impl);
137 }
138
139 /// Check if a function name is a recognized runtime call of any kind. This
140 /// does not consider if this call is available for any current compilation,
141 /// just that it is a known call somewhere. This returns the set of all
142 /// LibcallImpls which match the name; multiple implementations with the same
143 /// name may exist but differ in interpretation based on the target context.
144 ///
145 /// Generated by tablegen.
148 // Inlining the early exit on the string name appears to be worthwhile when
149 // querying a real set of symbols
150#define GET_LOOKUP_LIBCALL_IMPL_NAME_BODY
151#include "llvm/IR/RuntimeLibcalls.inc"
152 }
153
154 /// Check if this is valid libcall for the current module, otherwise
155 /// RTLIB::Unsupported.
156 LLVM_ABI RTLIB::LibcallImpl
158 for (RTLIB::LibcallImpl Impl : lookupLibcallImplName(FuncName)) {
159 if (isAvailable(Impl))
160 return Impl;
161 }
162
163 return RTLIB::Unsupported;
164 }
165
166 /// \returns the function type and attributes for the \p LibcallImpl,
167 /// depending on the target \p TT. If the function has incomplete type
168 /// information, return nullptr for the function type.
169 std::pair<FunctionType *, AttributeList>
170 getFunctionTy(LLVMContext &Ctx, const Triple &TT, const DataLayout &DL,
171 RTLIB::LibcallImpl LibcallImpl) const;
172
173private:
175 lookupLibcallImplNameImpl(StringRef Name);
176
177 static_assert(static_cast<int>(CallingConv::C) == 0,
178 "default calling conv should be encoded as 0");
179
180 /// Stores the CallingConv that should be used for each libcall
181 /// implementation.;
182 CallingConv::ID LibcallImplCallingConvs[RTLIB::NumLibcallImpls] = {};
183
184 /// Names of concrete implementations of runtime calls. e.g. __ashlsi3 for
185 /// SHL_I32
186 LLVM_ABI static const char RuntimeLibcallImplNameTableStorage[];
187 LLVM_ABI static const StringTable RuntimeLibcallImplNameTable;
188 LLVM_ABI static const uint16_t RuntimeLibcallNameOffsetTable[];
189 LLVM_ABI static const uint8_t RuntimeLibcallNameSizeTable[];
190
191 /// Map from a concrete LibcallImpl implementation to its RTLIB::Libcall kind.
192 LLVM_ABI static const RTLIB::Libcall ImplToLibcall[RTLIB::NumLibcallImpls];
193
194 /// Utility function for tablegenerated lookup function. Return a range of
195 /// enum values that apply for the function name at \p NameOffsetEntry with
196 /// the value \p StrOffset.
198 libcallImplNameHit(uint16_t NameOffsetEntry, uint16_t StrOffset);
199
200 static bool darwinHasSinCosStret(const Triple &TT) {
201 if (!TT.isOSDarwin())
202 return false;
203
204 // Don't bother with 32 bit x86.
205 if (TT.getArch() == Triple::x86)
206 return false;
207 // Macos < 10.9 has no sincos_stret.
208 if (TT.isMacOSX())
209 return !TT.isMacOSXVersionLT(10, 9) && TT.isArch64Bit();
210 // iOS < 7.0 has no sincos_stret.
211 if (TT.isiOS())
212 return !TT.isOSVersionLT(7, 0);
213 // Any other darwin such as WatchOS/TvOS is new enough.
214 return true;
215 }
216
217 static bool hasAEABILibcalls(const Triple &TT) {
218 return TT.isTargetAEABI() || TT.isTargetGNUAEABI() ||
219 TT.isTargetMuslAEABI() || TT.isOSFuchsia() || TT.isAndroid();
220 }
221
223 static bool isAAPCS_ABI(const Triple &TT, StringRef ABIName);
224
225 static bool darwinHasExp10(const Triple &TT);
226
227 /// Return true if the target has sincosf/sincos/sincosl functions
228 static bool hasSinCos(const Triple &TT) {
229 return TT.isGNUEnvironment() || TT.isOSFuchsia() || TT.isAndroid();
230 }
231
232 static bool hasSinCos_f32_f64(const Triple &TT) {
233 return hasSinCos(TT) || TT.isPS();
234 }
235
236 /// Generated by tablegen.
237 void setTargetRuntimeLibcallSets(const Triple &TT,
238 ExceptionHandling ExceptionModel,
239 FloatABI::ABIType FloatABI, EABI ABIType,
240 StringRef ABIName);
241
242 /// Set default libcall names. If a target wants to opt-out of a libcall it
243 /// should be placed here.
244 LLVM_ABI void initLibcalls(const Triple &TT, ExceptionHandling ExceptionModel,
245 FloatABI::ABIType FloatABI, EABI ABIType,
246 StringRef ABIName);
247};
248
249} // namespace RTLIB
250
251} // namespace llvm
252
253#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)
LLVM_ABI RTLIB::LibcallImpl getSupportedLibcallImpl(StringRef FuncName) const
Check if this is valid libcall for the current module, otherwise RTLIB::Unsupported.
RuntimeLibcallsInfo(const Triple &TT, ExceptionHandling ExceptionModel=ExceptionHandling::None, FloatABI::ABIType FloatABI=FloatABI::Default, EABI EABIVersion=EABI::Default, StringRef ABIName="")
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 &)