Debian Codegraph @

gcc/config/riscv/riscv-builtins.cc

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