]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/config/riscv/riscv-builtins.cc
Update copyright years.
[thirdparty/gcc.git] / gcc / config / riscv / riscv-builtins.cc
CommitLineData
09cae750 1/* Subroutines used for expanding RISC-V builtins.
a945c346 2 Copyright (C) 2011-2024 Free Software Foundation, Inc.
09cae750
PD
3 Contributed by Andrew Waterman (andrew@sifive.com).
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 3, or (at your option)
10any later version.
11
12GCC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
8fcc61f8
RS
21#define IN_TARGET_CODE 1
22
09cae750
PD
23#include "config.h"
24#include "system.h"
25#include "coretypes.h"
26#include "tm.h"
27#include "rtl.h"
28#include "tree.h"
29#include "gimple-expr.h"
30#include "memmodel.h"
31#include "expmed.h"
1b68a156 32#include "profile-count.h"
09cae750
PD
33#include "optabs.h"
34#include "recog.h"
35#include "diagnostic-core.h"
36#include "stor-layout.h"
27d68a60 37#include "stringpool.h"
09cae750
PD
38#include "expr.h"
39#include "langhooks.h"
7d935cdd 40#include "tm_p.h"
60bd33bc
JZZ
41#include "backend.h"
42#include "gimple.h"
43#include "gimple-iterator.h"
09cae750
PD
44
45/* Macros to create an enumeration identifier for a function prototype. */
33d9794b 46#define RISCV_FTYPE_NAME0(A) RISCV_##A##_FTYPE
09cae750 47#define RISCV_FTYPE_NAME1(A, B) RISCV_##A##_FTYPE_##B
89456334
LS
48#define RISCV_FTYPE_NAME2(A, B, C) RISCV_##A##_FTYPE_##B##_##C
49#define RISCV_FTYPE_NAME3(A, B, C, D) RISCV_##A##_FTYPE_##B##_##C##_##D
400efddd
MB
50#define RISCV_FTYPE_NAME4(A, B, C, D, E) \
51 RISCV_##A##_FTYPE_##B##_##C##_##D##_##E
09cae750
PD
52
53/* Classifies the prototype of a built-in function. */
54enum riscv_function_type {
55#define DEF_RISCV_FTYPE(NARGS, LIST) RISCV_FTYPE_NAME##NARGS LIST,
56#include "config/riscv/riscv-ftypes.def"
57#undef DEF_RISCV_FTYPE
58 RISCV_MAX_FTYPE_MAX
59};
60
61/* Specifies how a built-in function should be converted into rtl. */
62enum riscv_builtin_type {
63 /* The function corresponds directly to an .md pattern. */
64 RISCV_BUILTIN_DIRECT,
65
66 /* Likewise, but with return type VOID. */
67 RISCV_BUILTIN_DIRECT_NO_TARGET
68};
69
70/* Declare an availability predicate for built-in functions. */
71#define AVAIL(NAME, COND) \
72 static unsigned int \
73 riscv_builtin_avail_##NAME (void) \
74 { \
75 return (COND); \
76 }
77
78/* This structure describes a single built-in function. */
79struct riscv_builtin_description {
80 /* The code of the main .md file instruction. See riscv_builtin_type
81 for more information. */
82 enum insn_code icode;
83
84 /* The name of the built-in function. */
85 const char *name;
86
87 /* Specifies how the function should be expanded. */
88 enum riscv_builtin_type builtin_type;
89
90 /* The function's prototype. */
91 enum riscv_function_type prototype;
92
93 /* Whether the function is available. */
94 unsigned int (*avail) (void);
95};
96
ac96e906 97AVAIL (hard_float, TARGET_HARD_FLOAT || TARGET_ZFINX)
3df3ca90
S
98AVAIL (clean32, TARGET_ZICBOM && !TARGET_64BIT)
99AVAIL (clean64, TARGET_ZICBOM && TARGET_64BIT)
100AVAIL (flush32, TARGET_ZICBOM && !TARGET_64BIT)
101AVAIL (flush64, TARGET_ZICBOM && TARGET_64BIT)
102AVAIL (inval32, TARGET_ZICBOM && !TARGET_64BIT)
103AVAIL (inval64, TARGET_ZICBOM && TARGET_64BIT)
104AVAIL (zero32, TARGET_ZICBOZ && !TARGET_64BIT)
105AVAIL (zero64, TARGET_ZICBOZ && TARGET_64BIT)
106AVAIL (prefetchi32, TARGET_ZICBOP && !TARGET_64BIT)
107AVAIL (prefetchi64, TARGET_ZICBOP && TARGET_64BIT)
2c809510
LS
108AVAIL (crypto_zbkb32, TARGET_ZBKB && !TARGET_64BIT)
109AVAIL (crypto_zbkb64, TARGET_ZBKB && TARGET_64BIT)
2c809510
LS
110AVAIL (crypto_zbkx32, TARGET_ZBKX && !TARGET_64BIT)
111AVAIL (crypto_zbkx64, TARGET_ZBKX && TARGET_64BIT)
072c558a
LS
112AVAIL (crypto_zknd32, TARGET_ZKND && !TARGET_64BIT)
113AVAIL (crypto_zknd64, TARGET_ZKND && TARGET_64BIT)
114AVAIL (crypto_zkne32, TARGET_ZKNE && !TARGET_64BIT)
115AVAIL (crypto_zkne64, TARGET_ZKNE && TARGET_64BIT)
116AVAIL (crypto_zkne_or_zknd, (TARGET_ZKNE || TARGET_ZKND) && TARGET_64BIT)
9882b814 117AVAIL (crypto_zknh, TARGET_ZKNH)
e6416e43
LS
118AVAIL (crypto_zknh32, TARGET_ZKNH && !TARGET_64BIT)
119AVAIL (crypto_zknh64, TARGET_ZKNH && TARGET_64BIT)
9882b814
TO
120AVAIL (crypto_zksh, TARGET_ZKSH)
121AVAIL (crypto_zksed, TARGET_ZKSED)
d9df45a6
KS
122AVAIL (clmul_zbkc32_or_zbc32, (TARGET_ZBKC || TARGET_ZBC) && !TARGET_64BIT)
123AVAIL (clmul_zbkc64_or_zbc64, (TARGET_ZBKC || TARGET_ZBC) && TARGET_64BIT)
124AVAIL (clmulr_zbc32, TARGET_ZBC && !TARGET_64BIT)
125AVAIL (clmulr_zbc64, TARGET_ZBC && TARGET_64BIT)
c2d04dd6 126AVAIL (hint_pause, (!0))
3df3ca90 127
400efddd
MB
128// CORE-V AVAIL
129AVAIL (cvmac, TARGET_XCVMAC && !TARGET_64BIT)
5ef248c1 130AVAIL (cvalu, TARGET_XCVALU && !TARGET_64BIT)
14876d6a 131AVAIL (cvelw, TARGET_XCVELW && !TARGET_64BIT)
400efddd 132
09cae750
PD
133/* Construct a riscv_builtin_description from the given arguments.
134
135 INSN is the name of the associated instruction pattern, without the
136 leading CODE_FOR_riscv_.
137
138 NAME is the name of the function itself, without the leading
139 "__builtin_riscv_".
140
141 BUILTIN_TYPE and FUNCTION_TYPE are riscv_builtin_description fields.
142
143 AVAIL is the name of the availability predicate, without the leading
144 riscv_builtin_avail_. */
145#define RISCV_BUILTIN(INSN, NAME, BUILTIN_TYPE, FUNCTION_TYPE, AVAIL) \
146 { CODE_FOR_riscv_ ## INSN, "__builtin_riscv_" NAME, \
147 BUILTIN_TYPE, FUNCTION_TYPE, riscv_builtin_avail_ ## AVAIL }
148
149/* Define __builtin_riscv_<INSN>, which is a RISCV_BUILTIN_DIRECT function
150 mapped to instruction CODE_FOR_riscv_<INSN>, FUNCTION_TYPE and AVAIL
151 are as for RISCV_BUILTIN. */
152#define DIRECT_BUILTIN(INSN, FUNCTION_TYPE, AVAIL) \
153 RISCV_BUILTIN (INSN, #INSN, RISCV_BUILTIN_DIRECT, FUNCTION_TYPE, AVAIL)
154
155/* Define __builtin_riscv_<INSN>, which is a RISCV_BUILTIN_DIRECT_NO_TARGET
156 function mapped to instruction CODE_FOR_riscv_<INSN>, FUNCTION_TYPE
157 and AVAIL are as for RISCV_BUILTIN. */
158#define DIRECT_NO_TARGET_BUILTIN(INSN, FUNCTION_TYPE, AVAIL) \
159 RISCV_BUILTIN (INSN, #INSN, RISCV_BUILTIN_DIRECT_NO_TARGET, \
160 FUNCTION_TYPE, AVAIL)
161
162/* Argument types. */
163#define RISCV_ATYPE_VOID void_type_node
a1751681
TO
164#define RISCV_ATYPE_UQI unsigned_intQI_type_node
165#define RISCV_ATYPE_UHI unsigned_intHI_type_node
09cae750 166#define RISCV_ATYPE_USI unsigned_intSI_type_node
a1751681 167#define RISCV_ATYPE_UDI unsigned_intDI_type_node
5ef248c1
MB
168#define RISCV_ATYPE_QI intQI_type_node
169#define RISCV_ATYPE_HI intHI_type_node
400efddd 170#define RISCV_ATYPE_SI intSI_type_node
970b03c0 171#define RISCV_ATYPE_VOID_PTR ptr_type_node
14876d6a 172#define RISCV_ATYPE_INT_PTR integer_ptr_type_node
09cae750
PD
173
174/* RISCV_FTYPE_ATYPESN takes N RISCV_FTYPES-like type codes and lists
175 their associated RISCV_ATYPEs. */
33d9794b
KC
176#define RISCV_FTYPE_ATYPES0(A) \
177 RISCV_ATYPE_##A
09cae750
PD
178#define RISCV_FTYPE_ATYPES1(A, B) \
179 RISCV_ATYPE_##A, RISCV_ATYPE_##B
89456334
LS
180#define RISCV_FTYPE_ATYPES2(A, B, C) \
181 RISCV_ATYPE_##A, RISCV_ATYPE_##B, RISCV_ATYPE_##C
182#define RISCV_FTYPE_ATYPES3(A, B, C, D) \
183 RISCV_ATYPE_##A, RISCV_ATYPE_##B, RISCV_ATYPE_##C, RISCV_ATYPE_##D
400efddd
MB
184#define RISCV_FTYPE_ATYPES4(A, B, C, D, E) \
185 RISCV_ATYPE_##A, RISCV_ATYPE_##B, RISCV_ATYPE_##C, RISCV_ATYPE_##D, \
186 RISCV_ATYPE_##E
09cae750
PD
187
188static const struct riscv_builtin_description riscv_builtins[] = {
3df3ca90 189 #include "riscv-cmo.def"
2c809510 190 #include "riscv-scalar-crypto.def"
400efddd 191 #include "corev.def"
3df3ca90 192
33d9794b 193 DIRECT_BUILTIN (frflags, RISCV_USI_FTYPE, hard_float),
c717a92d 194 DIRECT_NO_TARGET_BUILTIN (fsflags, RISCV_VOID_FTYPE_USI, hard_float),
c2d04dd6 195 RISCV_BUILTIN (pause, "pause", RISCV_BUILTIN_DIRECT_NO_TARGET, RISCV_VOID_FTYPE, hint_pause),
09cae750
PD
196};
197
198/* Index I is the function declaration for riscv_builtins[I], or null if the
199 function isn't defined on this target. */
200static GTY(()) tree riscv_builtin_decls[ARRAY_SIZE (riscv_builtins)];
201
202/* Get the index I of the function declaration for riscv_builtin_decls[I]
203 using the instruction code or return null if not defined for the target. */
204static GTY(()) int riscv_builtin_decl_index[NUM_INSN_CODES];
205
206#define GET_BUILTIN_DECL(CODE) \
207 riscv_builtin_decls[riscv_builtin_decl_index[(CODE)]]
208
27d68a60
KC
209tree riscv_float16_type_node = NULL_TREE;
210
09cae750
PD
211/* Return the function type associated with function prototype TYPE. */
212
213static tree
214riscv_build_function_type (enum riscv_function_type type)
215{
216 static tree types[(int) RISCV_MAX_FTYPE_MAX];
217
218 if (types[(int) type] == NULL_TREE)
219 switch (type)
220 {
221#define DEF_RISCV_FTYPE(NUM, ARGS) \
222 case RISCV_FTYPE_NAME##NUM ARGS: \
223 types[(int) type] \
224 = build_function_type_list (RISCV_FTYPE_ATYPES##NUM ARGS, \
225 NULL_TREE); \
226 break;
227#include "config/riscv/riscv-ftypes.def"
228#undef DEF_RISCV_FTYPE
229 default:
230 gcc_unreachable ();
231 }
232
233 return types[(int) type];
234}
235
27d68a60
KC
236static void
237riscv_init_builtin_types (void)
238{
239 /* Provide the _Float16 type and float16_type_node if needed. */
240 if (!float16_type_node)
241 {
242 riscv_float16_type_node = make_node (REAL_TYPE);
243 TYPE_PRECISION (riscv_float16_type_node) = 16;
244 SET_TYPE_MODE (riscv_float16_type_node, HFmode);
245 layout_type (riscv_float16_type_node);
246 }
247 else
248 riscv_float16_type_node = float16_type_node;
249
250 if (!maybe_get_identifier ("_Float16"))
251 lang_hooks.types.register_builtin_type (riscv_float16_type_node,
252 "_Float16");
253}
254
09cae750
PD
255/* Implement TARGET_INIT_BUILTINS. */
256
257void
258riscv_init_builtins (void)
259{
27d68a60 260 riscv_init_builtin_types ();
03f33657 261 riscv_vector::init_builtins ();
27d68a60 262
09cae750
PD
263 for (size_t i = 0; i < ARRAY_SIZE (riscv_builtins); i++)
264 {
265 const struct riscv_builtin_description *d = &riscv_builtins[i];
266 if (d->avail ())
267 {
268 tree type = riscv_build_function_type (d->prototype);
269 riscv_builtin_decls[i]
cbd50570
JZZ
270 = add_builtin_function (d->name, type,
271 (i << RISCV_BUILTIN_SHIFT)
272 + RISCV_BUILTIN_GENERAL,
273 BUILT_IN_MD, NULL, NULL);
09cae750
PD
274 riscv_builtin_decl_index[d->icode] = i;
275 }
276 }
277}
278
279/* Implement TARGET_BUILTIN_DECL. */
280
281tree
282riscv_builtin_decl (unsigned int code, bool initialize_p ATTRIBUTE_UNUSED)
283{
cbd50570
JZZ
284 unsigned int subcode = code >> RISCV_BUILTIN_SHIFT;
285 switch (code & RISCV_BUILTIN_CLASS)
286 {
287 case RISCV_BUILTIN_GENERAL:
288 if (subcode >= ARRAY_SIZE (riscv_builtins))
289 return error_mark_node;
290 return riscv_builtin_decls[subcode];
291
292 case RISCV_BUILTIN_VECTOR:
293 return riscv_vector::builtin_decl (subcode, initialize_p);
294 }
295 return error_mark_node;
09cae750
PD
296}
297
298/* Take argument ARGNO from EXP's argument list and convert it into
299 an expand operand. Store the operand in *OP. */
300
301static void
302riscv_prepare_builtin_arg (struct expand_operand *op, tree exp, unsigned argno)
303{
304 tree arg = CALL_EXPR_ARG (exp, argno);
305 create_input_operand (op, expand_normal (arg), TYPE_MODE (TREE_TYPE (arg)));
306}
307
308/* Expand instruction ICODE as part of a built-in function sequence.
309 Use the first NOPS elements of OPS as the instruction's operands.
310 HAS_TARGET_P is true if operand 0 is a target; it is false if the
311 instruction has no target.
312
313 Return the target rtx if HAS_TARGET_P, otherwise return const0_rtx. */
314
315static rtx
316riscv_expand_builtin_insn (enum insn_code icode, unsigned int n_ops,
317 struct expand_operand *ops, bool has_target_p)
318{
319 if (!maybe_expand_insn (icode, n_ops, ops))
320 {
321 error ("invalid argument to built-in function");
322 return has_target_p ? gen_reg_rtx (ops[0].mode) : const0_rtx;
323 }
324
325 return has_target_p ? ops[0].value : const0_rtx;
326}
327
328/* Expand a RISCV_BUILTIN_DIRECT or RISCV_BUILTIN_DIRECT_NO_TARGET function;
329 HAS_TARGET_P says which. EXP is the CALL_EXPR that calls the function
330 and ICODE is the code of the associated .md pattern. TARGET, if nonnull,
331 suggests a good place to put the result. */
332
333static rtx
334riscv_expand_builtin_direct (enum insn_code icode, rtx target, tree exp,
335 bool has_target_p)
336{
337 struct expand_operand ops[MAX_RECOG_OPERANDS];
338
339 /* Map any target to operand 0. */
340 int opno = 0;
341 if (has_target_p)
342 create_output_operand (&ops[opno++], target, TYPE_MODE (TREE_TYPE (exp)));
343
344 /* Map the arguments to the other operands. */
345 gcc_assert (opno + call_expr_nargs (exp)
346 == insn_data[icode].n_generator_args);
347 for (int argno = 0; argno < call_expr_nargs (exp); argno++)
348 riscv_prepare_builtin_arg (&ops[opno++], exp, argno);
349
350 return riscv_expand_builtin_insn (icode, opno, ops, has_target_p);
351}
352
60bd33bc
JZZ
353/* Implement TARGET_GIMPLE_FOLD_BUILTIN. */
354
355bool
356riscv_gimple_fold_builtin (gimple_stmt_iterator *gsi)
357{
358 gcall *stmt = as_a<gcall *> (gsi_stmt (*gsi));
359 tree fndecl = gimple_call_fndecl (stmt);
360 unsigned int code = DECL_MD_FUNCTION_CODE (fndecl);
361 unsigned int subcode = code >> RISCV_BUILTIN_SHIFT;
362 gimple *new_stmt = NULL;
363 switch (code & RISCV_BUILTIN_CLASS)
364 {
365 case RISCV_BUILTIN_GENERAL:
366 new_stmt = NULL;
367 break;
368
369 case RISCV_BUILTIN_VECTOR:
370 new_stmt = riscv_vector::gimple_fold_builtin (subcode, gsi, stmt);
371 break;
372 }
373
374 if (!new_stmt)
375 return false;
376
377 gsi_replace (gsi, new_stmt, false);
378 return true;
379}
380
09cae750
PD
381/* Implement TARGET_EXPAND_BUILTIN. */
382
383rtx
384riscv_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
385 machine_mode mode ATTRIBUTE_UNUSED,
386 int ignore ATTRIBUTE_UNUSED)
387{
388 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
4d732405 389 unsigned int fcode = DECL_MD_FUNCTION_CODE (fndecl);
cbd50570
JZZ
390 unsigned int subcode = fcode >> RISCV_BUILTIN_SHIFT;
391 switch (fcode & RISCV_BUILTIN_CLASS)
09cae750 392 {
cbd50570
JZZ
393 case RISCV_BUILTIN_VECTOR:
394 return riscv_vector::expand_builtin (subcode, exp, target);
395 case RISCV_BUILTIN_GENERAL: {
396 const struct riscv_builtin_description *d = &riscv_builtins[subcode];
397
398 switch (d->builtin_type)
399 {
400 case RISCV_BUILTIN_DIRECT:
401 return riscv_expand_builtin_direct (d->icode, target, exp, true);
402
403 case RISCV_BUILTIN_DIRECT_NO_TARGET:
404 return riscv_expand_builtin_direct (d->icode, target, exp, false);
405 }
406 }
09cae750
PD
407 }
408
409 gcc_unreachable ();
410}
411
412/* Implement TARGET_ATOMIC_ASSIGN_EXPAND_FENV. */
413
414void
415riscv_atomic_assign_expand_fenv (tree *hold, tree *clear, tree *update)
416{
ac96e906 417 if (!(TARGET_HARD_FLOAT || TARGET_ZFINX))
09cae750
PD
418 return;
419
420 tree frflags = GET_BUILTIN_DECL (CODE_FOR_riscv_frflags);
421 tree fsflags = GET_BUILTIN_DECL (CODE_FOR_riscv_fsflags);
422 tree old_flags = create_tmp_var_raw (RISCV_ATYPE_USI);
423
b4ace720
JJ
424 *hold = build4 (TARGET_EXPR, RISCV_ATYPE_USI, old_flags,
425 build_call_expr (frflags, 0), NULL_TREE, NULL_TREE);
09cae750
PD
426 *clear = build_call_expr (fsflags, 1, old_flags);
427 *update = NULL_TREE;
428}