LLVM 22.0.0git
RuntimeLibcalls.cpp
Go to the documentation of this file.
1//===- RuntimeLibcalls.cpp - 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
12#include "llvm/IR/DataLayout.h"
13#include "llvm/Support/Debug.h"
14#include "llvm/Support/xxhash.h"
16
17#define DEBUG_TYPE "runtime-libcalls-info"
18
19using namespace llvm;
20using namespace RTLIB;
21
22#define GET_INIT_RUNTIME_LIBCALL_NAMES
23#define GET_SET_TARGET_RUNTIME_LIBCALL_SETS
24#define DEFINE_GET_LOOKUP_LIBCALL_IMPL_NAME
25#include "llvm/IR/RuntimeLibcalls.inc"
26
27/// Set default libcall names. If a target wants to opt-out of a libcall it
28/// should be placed here.
29void RuntimeLibcallsInfo::initLibcalls(const Triple &TT,
30 ExceptionHandling ExceptionModel,
32 EABI EABIVersion, StringRef ABIName) {
33 setTargetRuntimeLibcallSets(TT, ExceptionModel, FloatABI, EABIVersion,
34 ABIName);
35}
36
38iota_range<RTLIB::LibcallImpl>
39RuntimeLibcallsInfo::libcallImplNameHit(uint16_t NameOffsetEntry,
40 uint16_t StrOffset) {
41 int NumAliases = 1;
42 for (uint16_t Entry : ArrayRef(RuntimeLibcallNameOffsetTable)
43 .drop_front(NameOffsetEntry + 1)) {
44 if (Entry != StrOffset)
45 break;
46 ++NumAliases;
47 }
48
49 RTLIB::LibcallImpl ImplStart = static_cast<RTLIB::LibcallImpl>(
50 &RuntimeLibcallNameOffsetTable[NameOffsetEntry] -
51 &RuntimeLibcallNameOffsetTable[0]);
52 return enum_seq(ImplStart,
53 static_cast<RTLIB::LibcallImpl>(ImplStart + NumAliases));
54}
55
56bool RuntimeLibcallsInfo::isAAPCS_ABI(const Triple &TT, StringRef ABIName) {
57 const ARM::ARMABI TargetABI = ARM::computeTargetABI(TT, ABIName);
58 return TargetABI == ARM::ARM_ABI_AAPCS || TargetABI == ARM::ARM_ABI_AAPCS16;
59}
60
61bool RuntimeLibcallsInfo::darwinHasExp10(const Triple &TT) {
62 switch (TT.getOS()) {
63 case Triple::MacOSX:
64 return !TT.isMacOSXVersionLT(10, 9);
65 case Triple::IOS:
66 return !TT.isOSVersionLT(7, 0);
68 case Triple::TvOS:
69 case Triple::WatchOS:
70 case Triple::XROS:
72 return true;
73 default:
74 return false;
75 }
76}
77
78std::pair<FunctionType *, AttributeList>
80 const DataLayout &DL,
81 RTLIB::LibcallImpl LibcallImpl) const {
82 static constexpr Attribute::AttrKind CommonFnAttrs[] = {
83 Attribute::NoCallback, Attribute::NoFree, Attribute::NoSync,
84 Attribute::NoUnwind, Attribute::WillReturn};
85
86 switch (LibcallImpl) {
87 case RTLIB::impl___sincos_stret:
88 case RTLIB::impl___sincosf_stret: {
89 if (!darwinHasSinCosStret(TT)) // Non-darwin currently unexpected
90 return {};
91
92 Type *ScalarTy = LibcallImpl == RTLIB::impl___sincosf_stret
93 ? Type::getFloatTy(Ctx)
94 : Type::getDoubleTy(Ctx);
95
96 AttrBuilder FuncAttrBuilder(Ctx);
97 for (Attribute::AttrKind Attr : CommonFnAttrs)
98 FuncAttrBuilder.addAttribute(Attr);
99
100 const bool UseSret =
101 TT.isX86_32() || ((TT.isARM() || TT.isThumb()) &&
103
104 FuncAttrBuilder.addMemoryAttr(MemoryEffects::argumentOrErrnoMemOnly(
106
107 AttributeList Attrs;
108 Attrs = Attrs.addFnAttributes(Ctx, FuncAttrBuilder);
109
110 if (UseSret) {
111 AttrBuilder AttrBuilder(Ctx);
112 StructType *StructTy = StructType::get(ScalarTy, ScalarTy);
113 AttrBuilder.addStructRetAttr(StructTy);
114 AttrBuilder.addAlignmentAttr(DL.getABITypeAlign(StructTy));
116 Type::getVoidTy(Ctx), {DL.getAllocaPtrType(Ctx), ScalarTy}, false);
117
118 return {FuncTy, Attrs.addParamAttributes(Ctx, 0, AttrBuilder)};
119 }
120
121 Type *RetTy =
122 LibcallImpl == RTLIB::impl___sincosf_stret && TT.isX86_64()
123 ? static_cast<Type *>(FixedVectorType::get(ScalarTy, 2))
124 : static_cast<Type *>(StructType::get(ScalarTy, ScalarTy));
125
126 return {FunctionType::get(RetTy, {ScalarTy}, false), Attrs};
127 }
128 case RTLIB::impl_sqrtf:
129 case RTLIB::impl_sqrt: {
130 AttrBuilder FuncAttrBuilder(Ctx);
131
132 for (Attribute::AttrKind Attr : CommonFnAttrs)
133 FuncAttrBuilder.addAttribute(Attr);
134 FuncAttrBuilder.addMemoryAttr(MemoryEffects::errnoMemOnly(ModRefInfo::Mod));
135
136 AttributeList Attrs;
137 Attrs = Attrs.addFnAttributes(Ctx, FuncAttrBuilder);
138
139 Type *ScalarTy = LibcallImpl == RTLIB::impl_sqrtf ? Type::getFloatTy(Ctx)
140 : Type::getDoubleTy(Ctx);
141 FunctionType *FuncTy = FunctionType::get(ScalarTy, {ScalarTy}, false);
142
143 Attrs = Attrs.addRetAttribute(
145 fcNegNormal));
146 return {FuncTy, Attrs};
147 }
148 default:
149 return {};
150 }
151
152 return {};
153}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
#define LLVM_ATTRIBUTE_ALWAYS_INLINE
LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do so, mark a method "always...
Definition Compiler.h:356
Utilities for dealing with flags related to floating point properties and mode controls.
static LLVM_ABI Attribute getWithNoFPClass(LLVMContext &Context, FPClassTest Mask)
AttrKind
This enumeration lists the attributes that can be associated with parameters, function results,...
Definition Attributes.h:88
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
static LLVM_ABI FixedVectorType * get(Type *ElementType, unsigned NumElts)
Definition Type.cpp:803
Class to represent function types.
static LLVM_ABI FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
static MemoryEffectsBase errnoMemOnly(ModRefInfo MR=ModRefInfo::ModRef)
Definition ModRef.h:146
static MemoryEffectsBase argumentOrErrnoMemOnly(ModRefInfo ArgMR=ModRefInfo::ModRef, ModRefInfo ErrnoMR=ModRefInfo::ModRef)
Definition ModRef.h:167
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
Class to represent struct types.
static LLVM_ABI StructType * get(LLVMContext &Context, ArrayRef< Type * > Elements, bool isPacked=false)
This static method is the primary way to create a literal StructType.
Definition Type.cpp:414
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:47
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
Definition Type.cpp:281
static LLVM_ABI Type * getDoubleTy(LLVMContext &C)
Definition Type.cpp:286
static LLVM_ABI Type * getFloatTy(LLVMContext &C)
Definition Type.cpp:285
LLVM_ABI LLVM_READONLY ARMABI computeTargetABI(const Triple &TT, StringRef ABIName="")
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
@ Mod
The access may modify the value stored in memory.
Definition ModRef.h:34
@ NoModRef
The access neither references nor modifies the value stored in memory.
Definition ModRef.h:30
ArrayRef(const T &OneElt) -> ArrayRef< T >
EABI
Definition CodeGen.h:73
std::pair< FunctionType *, AttributeList > getFunctionTy(LLVMContext &Ctx, const Triple &TT, const DataLayout &DL, RTLIB::LibcallImpl LibcallImpl) const