xref: /freebsd/contrib/llvm-project/llvm/lib/IR/RuntimeLibcalls.cpp (revision e53a21abdf2953714e44e3c54b4bb78557cb096c)
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 
9 #include "llvm/IR/RuntimeLibcalls.h"
10 
11 using namespace llvm;
12 using namespace RTLIB;
13 
14 /// Set default libcall names. If a target wants to opt-out of a libcall it
15 /// should be placed here.
16 void RuntimeLibcallsInfo::initLibcalls(const Triple &TT) {
17   std::fill(std::begin(LibcallRoutineNames), std::end(LibcallRoutineNames),
18             nullptr);
19 
20 #define HANDLE_LIBCALL(code, name) setLibcallName(RTLIB::code, name);
21 #include "llvm/IR/RuntimeLibcalls.def"
22 #undef HANDLE_LIBCALL
23 
24   // Initialize calling conventions to their default.
25   for (int LC = 0; LC < RTLIB::UNKNOWN_LIBCALL; ++LC)
26     setLibcallCallingConv((RTLIB::Libcall)LC, CallingConv::C);
27 
28   // Use the f128 variants of math functions on x86_64
29   if (TT.getArch() == Triple::ArchType::x86_64 && TT.isGNUEnvironment()) {
30     setLibcallName(RTLIB::REM_F128, "fmodf128");
31     setLibcallName(RTLIB::FMA_F128, "fmaf128");
32     setLibcallName(RTLIB::SQRT_F128, "sqrtf128");
33     setLibcallName(RTLIB::CBRT_F128, "cbrtf128");
34     setLibcallName(RTLIB::LOG_F128, "logf128");
35     setLibcallName(RTLIB::LOG_FINITE_F128, "__logf128_finite");
36     setLibcallName(RTLIB::LOG2_F128, "log2f128");
37     setLibcallName(RTLIB::LOG2_FINITE_F128, "__log2f128_finite");
38     setLibcallName(RTLIB::LOG10_F128, "log10f128");
39     setLibcallName(RTLIB::LOG10_FINITE_F128, "__log10f128_finite");
40     setLibcallName(RTLIB::EXP_F128, "expf128");
41     setLibcallName(RTLIB::EXP_FINITE_F128, "__expf128_finite");
42     setLibcallName(RTLIB::EXP2_F128, "exp2f128");
43     setLibcallName(RTLIB::EXP2_FINITE_F128, "__exp2f128_finite");
44     setLibcallName(RTLIB::EXP10_F128, "exp10f128");
45     setLibcallName(RTLIB::SIN_F128, "sinf128");
46     setLibcallName(RTLIB::COS_F128, "cosf128");
47     setLibcallName(RTLIB::TAN_F128, "tanf128");
48     setLibcallName(RTLIB::SINCOS_F128, "sincosf128");
49     setLibcallName(RTLIB::ASIN_F128, "asinf128");
50     setLibcallName(RTLIB::ACOS_F128, "acosf128");
51     setLibcallName(RTLIB::ATAN_F128, "atanf128");
52     setLibcallName(RTLIB::SINH_F128, "sinhf128");
53     setLibcallName(RTLIB::COSH_F128, "coshf128");
54     setLibcallName(RTLIB::TANH_F128, "tanhf128");
55     setLibcallName(RTLIB::POW_F128, "powf128");
56     setLibcallName(RTLIB::POW_FINITE_F128, "__powf128_finite");
57     setLibcallName(RTLIB::CEIL_F128, "ceilf128");
58     setLibcallName(RTLIB::TRUNC_F128, "truncf128");
59     setLibcallName(RTLIB::RINT_F128, "rintf128");
60     setLibcallName(RTLIB::NEARBYINT_F128, "nearbyintf128");
61     setLibcallName(RTLIB::ROUND_F128, "roundf128");
62     setLibcallName(RTLIB::ROUNDEVEN_F128, "roundevenf128");
63     setLibcallName(RTLIB::FLOOR_F128, "floorf128");
64     setLibcallName(RTLIB::COPYSIGN_F128, "copysignf128");
65     setLibcallName(RTLIB::FMIN_F128, "fminf128");
66     setLibcallName(RTLIB::FMAX_F128, "fmaxf128");
67     setLibcallName(RTLIB::LROUND_F128, "lroundf128");
68     setLibcallName(RTLIB::LLROUND_F128, "llroundf128");
69     setLibcallName(RTLIB::LRINT_F128, "lrintf128");
70     setLibcallName(RTLIB::LLRINT_F128, "llrintf128");
71     setLibcallName(RTLIB::LDEXP_F128, "ldexpf128");
72     setLibcallName(RTLIB::FREXP_F128, "frexpf128");
73   }
74 
75   // For IEEE quad-precision libcall names, PPC uses "kf" instead of "tf".
76   if (TT.isPPC()) {
77     setLibcallName(RTLIB::ADD_F128, "__addkf3");
78     setLibcallName(RTLIB::SUB_F128, "__subkf3");
79     setLibcallName(RTLIB::MUL_F128, "__mulkf3");
80     setLibcallName(RTLIB::DIV_F128, "__divkf3");
81     setLibcallName(RTLIB::POWI_F128, "__powikf2");
82     setLibcallName(RTLIB::FPEXT_F32_F128, "__extendsfkf2");
83     setLibcallName(RTLIB::FPEXT_F64_F128, "__extenddfkf2");
84     setLibcallName(RTLIB::FPROUND_F128_F32, "__trunckfsf2");
85     setLibcallName(RTLIB::FPROUND_F128_F64, "__trunckfdf2");
86     setLibcallName(RTLIB::FPTOSINT_F128_I32, "__fixkfsi");
87     setLibcallName(RTLIB::FPTOSINT_F128_I64, "__fixkfdi");
88     setLibcallName(RTLIB::FPTOSINT_F128_I128, "__fixkfti");
89     setLibcallName(RTLIB::FPTOUINT_F128_I32, "__fixunskfsi");
90     setLibcallName(RTLIB::FPTOUINT_F128_I64, "__fixunskfdi");
91     setLibcallName(RTLIB::FPTOUINT_F128_I128, "__fixunskfti");
92     setLibcallName(RTLIB::SINTTOFP_I32_F128, "__floatsikf");
93     setLibcallName(RTLIB::SINTTOFP_I64_F128, "__floatdikf");
94     setLibcallName(RTLIB::SINTTOFP_I128_F128, "__floattikf");
95     setLibcallName(RTLIB::UINTTOFP_I32_F128, "__floatunsikf");
96     setLibcallName(RTLIB::UINTTOFP_I64_F128, "__floatundikf");
97     setLibcallName(RTLIB::UINTTOFP_I128_F128, "__floatuntikf");
98     setLibcallName(RTLIB::OEQ_F128, "__eqkf2");
99     setLibcallName(RTLIB::UNE_F128, "__nekf2");
100     setLibcallName(RTLIB::OGE_F128, "__gekf2");
101     setLibcallName(RTLIB::OLT_F128, "__ltkf2");
102     setLibcallName(RTLIB::OLE_F128, "__lekf2");
103     setLibcallName(RTLIB::OGT_F128, "__gtkf2");
104     setLibcallName(RTLIB::UO_F128, "__unordkf2");
105   }
106 
107   // A few names are different on particular architectures or environments.
108   if (TT.isOSDarwin()) {
109     // For f16/f32 conversions, Darwin uses the standard naming scheme,
110     // instead of the gnueabi-style __gnu_*_ieee.
111     // FIXME: What about other targets?
112     setLibcallName(RTLIB::FPEXT_F16_F32, "__extendhfsf2");
113     setLibcallName(RTLIB::FPROUND_F32_F16, "__truncsfhf2");
114 
115     // Some darwins have an optimized __bzero/bzero function.
116     switch (TT.getArch()) {
117     case Triple::x86:
118     case Triple::x86_64:
119       if (TT.isMacOSX() && !TT.isMacOSXVersionLT(10, 6))
120         setLibcallName(RTLIB::BZERO, "__bzero");
121       break;
122     case Triple::aarch64:
123     case Triple::aarch64_32:
124       setLibcallName(RTLIB::BZERO, "bzero");
125       break;
126     default:
127       break;
128     }
129 
130     if (darwinHasSinCos(TT)) {
131       setLibcallName(RTLIB::SINCOS_STRET_F32, "__sincosf_stret");
132       setLibcallName(RTLIB::SINCOS_STRET_F64, "__sincos_stret");
133       if (TT.isWatchABI()) {
134         setLibcallCallingConv(RTLIB::SINCOS_STRET_F32,
135                               CallingConv::ARM_AAPCS_VFP);
136         setLibcallCallingConv(RTLIB::SINCOS_STRET_F64,
137                               CallingConv::ARM_AAPCS_VFP);
138       }
139     }
140 
141     switch (TT.getOS()) {
142     case Triple::MacOSX:
143       if (TT.isMacOSXVersionLT(10, 9)) {
144         setLibcallName(RTLIB::EXP10_F32, nullptr);
145         setLibcallName(RTLIB::EXP10_F64, nullptr);
146       } else {
147         setLibcallName(RTLIB::EXP10_F32, "__exp10f");
148         setLibcallName(RTLIB::EXP10_F64, "__exp10");
149       }
150       break;
151     case Triple::IOS:
152       if (TT.isOSVersionLT(7, 0)) {
153         setLibcallName(RTLIB::EXP10_F32, nullptr);
154         setLibcallName(RTLIB::EXP10_F64, nullptr);
155         break;
156       }
157       [[fallthrough]];
158     case Triple::TvOS:
159     case Triple::WatchOS:
160     case Triple::XROS:
161       setLibcallName(RTLIB::EXP10_F32, "__exp10f");
162       setLibcallName(RTLIB::EXP10_F64, "__exp10");
163       break;
164     default:
165       break;
166     }
167   } else {
168     setLibcallName(RTLIB::FPEXT_F16_F32, "__gnu_h2f_ieee");
169     setLibcallName(RTLIB::FPROUND_F32_F16, "__gnu_f2h_ieee");
170   }
171 
172   if (TT.isGNUEnvironment() || TT.isOSFuchsia() ||
173       (TT.isAndroid() && !TT.isAndroidVersionLT(9))) {
174     setLibcallName(RTLIB::SINCOS_F32, "sincosf");
175     setLibcallName(RTLIB::SINCOS_F64, "sincos");
176     setLibcallName(RTLIB::SINCOS_F80, "sincosl");
177     setLibcallName(RTLIB::SINCOS_F128, "sincosl");
178     setLibcallName(RTLIB::SINCOS_PPCF128, "sincosl");
179   }
180 
181   if (TT.isPS()) {
182     setLibcallName(RTLIB::SINCOS_F32, "sincosf");
183     setLibcallName(RTLIB::SINCOS_F64, "sincos");
184   }
185 
186   if (TT.isOSOpenBSD()) {
187     setLibcallName(RTLIB::STACKPROTECTOR_CHECK_FAIL, nullptr);
188   }
189 
190   if (TT.isOSWindows() && !TT.isOSCygMing()) {
191     setLibcallName(RTLIB::LDEXP_F32, nullptr);
192     setLibcallName(RTLIB::LDEXP_F80, nullptr);
193     setLibcallName(RTLIB::LDEXP_F128, nullptr);
194     setLibcallName(RTLIB::LDEXP_PPCF128, nullptr);
195 
196     setLibcallName(RTLIB::FREXP_F32, nullptr);
197     setLibcallName(RTLIB::FREXP_F80, nullptr);
198     setLibcallName(RTLIB::FREXP_F128, nullptr);
199     setLibcallName(RTLIB::FREXP_PPCF128, nullptr);
200   }
201 
202   if (TT.isAArch64()) {
203     if (TT.isOSMSVCRT()) {
204       // MSVCRT doesn't have powi; fall back to pow
205       setLibcallName(RTLIB::POWI_F32, nullptr);
206       setLibcallName(RTLIB::POWI_F64, nullptr);
207     }
208   }
209 
210   // Disable most libcalls on AMDGPU.
211   if (TT.isAMDGPU()) {
212     for (int I = 0; I < RTLIB::UNKNOWN_LIBCALL; ++I) {
213       if (I < RTLIB::ATOMIC_LOAD || I > RTLIB::ATOMIC_FETCH_NAND_16)
214         setLibcallName(static_cast<RTLIB::Libcall>(I), nullptr);
215     }
216   }
217 
218   // Disable most libcalls on NVPTX.
219   if (TT.isNVPTX()) {
220     for (int I = 0; I < RTLIB::UNKNOWN_LIBCALL; ++I)
221       if (I < RTLIB::ATOMIC_LOAD || I > RTLIB::ATOMIC_FETCH_NAND_16)
222         setLibcallName(static_cast<RTLIB::Libcall>(I), nullptr);
223   }
224 
225   if (TT.isARM() || TT.isThumb()) {
226     // These libcalls are not available in 32-bit.
227     setLibcallName(RTLIB::SHL_I128, nullptr);
228     setLibcallName(RTLIB::SRL_I128, nullptr);
229     setLibcallName(RTLIB::SRA_I128, nullptr);
230     setLibcallName(RTLIB::MUL_I128, nullptr);
231     setLibcallName(RTLIB::MULO_I64, nullptr);
232     setLibcallName(RTLIB::MULO_I128, nullptr);
233 
234     if (TT.isOSMSVCRT()) {
235       // MSVCRT doesn't have powi; fall back to pow
236       setLibcallName(RTLIB::POWI_F32, nullptr);
237       setLibcallName(RTLIB::POWI_F64, nullptr);
238     }
239   }
240 
241   if (TT.getArch() == Triple::ArchType::avr) {
242     // Division rtlib functions (not supported), use divmod functions instead
243     setLibcallName(RTLIB::SDIV_I8, nullptr);
244     setLibcallName(RTLIB::SDIV_I16, nullptr);
245     setLibcallName(RTLIB::SDIV_I32, nullptr);
246     setLibcallName(RTLIB::UDIV_I8, nullptr);
247     setLibcallName(RTLIB::UDIV_I16, nullptr);
248     setLibcallName(RTLIB::UDIV_I32, nullptr);
249 
250     // Modulus rtlib functions (not supported), use divmod functions instead
251     setLibcallName(RTLIB::SREM_I8, nullptr);
252     setLibcallName(RTLIB::SREM_I16, nullptr);
253     setLibcallName(RTLIB::SREM_I32, nullptr);
254     setLibcallName(RTLIB::UREM_I8, nullptr);
255     setLibcallName(RTLIB::UREM_I16, nullptr);
256     setLibcallName(RTLIB::UREM_I32, nullptr);
257   }
258 
259   if (TT.getArch() == Triple::ArchType::hexagon) {
260     // These cause problems when the shift amount is non-constant.
261     setLibcallName(RTLIB::SHL_I128, nullptr);
262     setLibcallName(RTLIB::SRL_I128, nullptr);
263     setLibcallName(RTLIB::SRA_I128, nullptr);
264   }
265 
266   if (TT.isLoongArch()) {
267     if (!TT.isLoongArch64()) {
268       // Set libcalls.
269       setLibcallName(RTLIB::MUL_I128, nullptr);
270       // The MULO libcall is not part of libgcc, only compiler-rt.
271       setLibcallName(RTLIB::MULO_I64, nullptr);
272     }
273     // The MULO libcall is not part of libgcc, only compiler-rt.
274     setLibcallName(RTLIB::MULO_I128, nullptr);
275   }
276 
277   if (TT.isMIPS32()) {
278     // These libcalls are not available in 32-bit.
279     setLibcallName(RTLIB::SHL_I128, nullptr);
280     setLibcallName(RTLIB::SRL_I128, nullptr);
281     setLibcallName(RTLIB::SRA_I128, nullptr);
282     setLibcallName(RTLIB::MUL_I128, nullptr);
283     setLibcallName(RTLIB::MULO_I64, nullptr);
284     setLibcallName(RTLIB::MULO_I128, nullptr);
285   }
286 
287   if (TT.isPPC()) {
288     if (!TT.isPPC64()) {
289       // These libcalls are not available in 32-bit.
290       setLibcallName(RTLIB::SHL_I128, nullptr);
291       setLibcallName(RTLIB::SRL_I128, nullptr);
292       setLibcallName(RTLIB::SRA_I128, nullptr);
293       setLibcallName(RTLIB::MUL_I128, nullptr);
294       setLibcallName(RTLIB::MULO_I64, nullptr);
295     }
296     setLibcallName(RTLIB::MULO_I128, nullptr);
297   }
298 
299   if (TT.isRISCV32()) {
300     // These libcalls are not available in 32-bit.
301     setLibcallName(RTLIB::SHL_I128, nullptr);
302     setLibcallName(RTLIB::SRL_I128, nullptr);
303     setLibcallName(RTLIB::SRA_I128, nullptr);
304     setLibcallName(RTLIB::MUL_I128, nullptr);
305     setLibcallName(RTLIB::MULO_I64, nullptr);
306   }
307 
308   if (TT.isSPARC()) {
309     if (!TT.isSPARC64()) {
310       // These libcalls are not available in 32-bit.
311       setLibcallName(RTLIB::MULO_I64, nullptr);
312       setLibcallName(RTLIB::MUL_I128, nullptr);
313       setLibcallName(RTLIB::SHL_I128, nullptr);
314       setLibcallName(RTLIB::SRL_I128, nullptr);
315       setLibcallName(RTLIB::SRA_I128, nullptr);
316     }
317     setLibcallName(RTLIB::MULO_I128, nullptr);
318   }
319 
320   if (TT.isSystemZ()) {
321     setLibcallName(RTLIB::SRL_I128, nullptr);
322     setLibcallName(RTLIB::SHL_I128, nullptr);
323     setLibcallName(RTLIB::SRA_I128, nullptr);
324   }
325 
326   if (TT.isX86()) {
327     if (TT.getArch() == Triple::ArchType::x86) {
328       // These libcalls are not available in 32-bit.
329       setLibcallName(RTLIB::SHL_I128, nullptr);
330       setLibcallName(RTLIB::SRL_I128, nullptr);
331       setLibcallName(RTLIB::SRA_I128, nullptr);
332       setLibcallName(RTLIB::MUL_I128, nullptr);
333       // The MULO libcall is not part of libgcc, only compiler-rt.
334       setLibcallName(RTLIB::MULO_I64, nullptr);
335     }
336 
337     // The MULO libcall is not part of libgcc, only compiler-rt.
338     setLibcallName(RTLIB::MULO_I128, nullptr);
339 
340     if (TT.isOSMSVCRT()) {
341       // MSVCRT doesn't have powi; fall back to pow
342       setLibcallName(RTLIB::POWI_F32, nullptr);
343       setLibcallName(RTLIB::POWI_F64, nullptr);
344     }
345   }
346 }
347