]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/config/riscv/riscv-builtins.cc
RISC-V: Implement ZKSH and ZKSED extensions
[thirdparty/gcc.git] / gcc / config / riscv / riscv-builtins.cc
1 /* Subroutines used for expanding RISC-V builtins.
2 Copyright (C) 2011-2023 Free Software Foundation, Inc.
3 Contributed by Andrew Waterman (andrew@sifive.com).
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
42 /* Macros to create an enumeration identifier for a function prototype. */
43 #define RISCV_FTYPE_NAME0(A) RISCV_##A##_FTYPE
44 #define RISCV_FTYPE_NAME1(A, B) RISCV_##A##_FTYPE_##B
45 #define RISCV_FTYPE_NAME2(A, B, C) RISCV_##A##_FTYPE_##B##_##C
46 #define RISCV_FTYPE_NAME3(A, B, C, D) RISCV_##A##_FTYPE_##B##_##C##_##D
47
48 /* Classifies the prototype of a built-in function. */
49 enum riscv_function_type {
50 #define DEF_RISCV_FTYPE(NARGS, LIST) RISCV_FTYPE_NAME##NARGS LIST,
51 #include "config/riscv/riscv-ftypes.def"
52 #undef DEF_RISCV_FTYPE
53 RISCV_MAX_FTYPE_MAX
54 };
55
56 /* Specifies how a built-in function should be converted into rtl. */
57 enum riscv_builtin_type {
58 /* The function corresponds directly to an .md pattern. */
59 RISCV_BUILTIN_DIRECT,
60
61 /* Likewise, but with return type VOID. */
62 RISCV_BUILTIN_DIRECT_NO_TARGET
63 };
64
65 /* Declare an availability predicate for built-in functions. */
66 #define AVAIL(NAME, COND) \
67 static unsigned int \
68 riscv_builtin_avail_##NAME (void) \
69 { \
70 return (COND); \
71 }
72
73 /* This structure describes a single built-in function. */
74 struct riscv_builtin_description {
75 /* The code of the main .md file instruction. See riscv_builtin_type
76 for more information. */
77 enum insn_code icode;
78
79 /* The name of the built-in function. */
80 const char *name;
81
82 /* Specifies how the function should be expanded. */
83 enum riscv_builtin_type builtin_type;
84
85 /* The function's prototype. */
86 enum riscv_function_type prototype;
87
88 /* Whether the function is available. */
89 unsigned int (*avail) (void);
90 };
91
92 AVAIL (hard_float, TARGET_HARD_FLOAT || TARGET_ZFINX)
93 AVAIL (clean32, TARGET_ZICBOM && !TARGET_64BIT)
94 AVAIL (clean64, TARGET_ZICBOM && TARGET_64BIT)
95 AVAIL (flush32, TARGET_ZICBOM && !TARGET_64BIT)
96 AVAIL (flush64, TARGET_ZICBOM && TARGET_64BIT)
97 AVAIL (inval32, TARGET_ZICBOM && !TARGET_64BIT)
98 AVAIL (inval64, TARGET_ZICBOM && TARGET_64BIT)
99 AVAIL (zero32, TARGET_ZICBOZ && !TARGET_64BIT)
100 AVAIL (zero64, TARGET_ZICBOZ && TARGET_64BIT)
101 AVAIL (prefetchi32, TARGET_ZICBOP && !TARGET_64BIT)
102 AVAIL (prefetchi64, TARGET_ZICBOP && TARGET_64BIT)
103 AVAIL (crypto_zbkb32, TARGET_ZBKB && !TARGET_64BIT)
104 AVAIL (crypto_zbkb64, TARGET_ZBKB && TARGET_64BIT)
105 AVAIL (crypto_zbkc32, TARGET_ZBKC && !TARGET_64BIT)
106 AVAIL (crypto_zbkc64, TARGET_ZBKC && TARGET_64BIT)
107 AVAIL (crypto_zbkx32, TARGET_ZBKX && !TARGET_64BIT)
108 AVAIL (crypto_zbkx64, TARGET_ZBKX && TARGET_64BIT)
109 AVAIL (crypto_zknd32, TARGET_ZKND && !TARGET_64BIT)
110 AVAIL (crypto_zknd64, TARGET_ZKND && TARGET_64BIT)
111 AVAIL (crypto_zkne32, TARGET_ZKNE && !TARGET_64BIT)
112 AVAIL (crypto_zkne64, TARGET_ZKNE && TARGET_64BIT)
113 AVAIL (crypto_zkne_or_zknd, (TARGET_ZKNE || TARGET_ZKND) && TARGET_64BIT)
114 AVAIL (crypto_zknh32, TARGET_ZKNH && !TARGET_64BIT)
115 AVAIL (crypto_zknh64, TARGET_ZKNH && TARGET_64BIT)
116 AVAIL (crypto_zksh32, TARGET_ZKSH && !TARGET_64BIT)
117 AVAIL (crypto_zksh64, TARGET_ZKSH && TARGET_64BIT)
118 AVAIL (crypto_zksed32, TARGET_ZKSED && !TARGET_64BIT)
119 AVAIL (crypto_zksed64, TARGET_ZKSED && TARGET_64BIT)
120 AVAIL (always, (!0))
121
122 /* Construct a riscv_builtin_description from the given arguments.
123
124 INSN is the name of the associated instruction pattern, without the
125 leading CODE_FOR_riscv_.
126
127 NAME is the name of the function itself, without the leading
128 "__builtin_riscv_".
129
130 BUILTIN_TYPE and FUNCTION_TYPE are riscv_builtin_description fields.
131
132 AVAIL is the name of the availability predicate, without the leading
133 riscv_builtin_avail_. */
134 #define RISCV_BUILTIN(INSN, NAME, BUILTIN_TYPE, FUNCTION_TYPE, AVAIL) \
135 { CODE_FOR_riscv_ ## INSN, "__builtin_riscv_" NAME, \
136 BUILTIN_TYPE, FUNCTION_TYPE, riscv_builtin_avail_ ## AVAIL }
137
138 /* Define __builtin_riscv_<INSN>, which is a RISCV_BUILTIN_DIRECT function
139 mapped to instruction CODE_FOR_riscv_<INSN>, FUNCTION_TYPE and AVAIL
140 are as for RISCV_BUILTIN. */
141 #define DIRECT_BUILTIN(INSN, FUNCTION_TYPE, AVAIL) \
142 RISCV_BUILTIN (INSN, #INSN, RISCV_BUILTIN_DIRECT, FUNCTION_TYPE, AVAIL)
143
144 /* Define __builtin_riscv_<INSN>, which is a RISCV_BUILTIN_DIRECT_NO_TARGET
145 function mapped to instruction CODE_FOR_riscv_<INSN>, FUNCTION_TYPE
146 and AVAIL are as for RISCV_BUILTIN. */
147 #define DIRECT_NO_TARGET_BUILTIN(INSN, FUNCTION_TYPE, AVAIL) \
148 RISCV_BUILTIN (INSN, #INSN, RISCV_BUILTIN_DIRECT_NO_TARGET, \
149 FUNCTION_TYPE, AVAIL)
150
151 /* Argument types. */
152 #define RISCV_ATYPE_VOID void_type_node
153 #define RISCV_ATYPE_USI unsigned_intSI_type_node
154 #define RISCV_ATYPE_QI intQI_type_node
155 #define RISCV_ATYPE_HI intHI_type_node
156 #define RISCV_ATYPE_SI intSI_type_node
157 #define RISCV_ATYPE_DI intDI_type_node
158 #define RISCV_ATYPE_VOID_PTR ptr_type_node
159
160 /* RISCV_FTYPE_ATYPESN takes N RISCV_FTYPES-like type codes and lists
161 their associated RISCV_ATYPEs. */
162 #define RISCV_FTYPE_ATYPES0(A) \
163 RISCV_ATYPE_##A
164 #define RISCV_FTYPE_ATYPES1(A, B) \
165 RISCV_ATYPE_##A, RISCV_ATYPE_##B
166 #define RISCV_FTYPE_ATYPES2(A, B, C) \
167 RISCV_ATYPE_##A, RISCV_ATYPE_##B, RISCV_ATYPE_##C
168 #define RISCV_FTYPE_ATYPES3(A, B, C, D) \
169 RISCV_ATYPE_##A, RISCV_ATYPE_##B, RISCV_ATYPE_##C, RISCV_ATYPE_##D
170
171 static const struct riscv_builtin_description riscv_builtins[] = {
172 #include "riscv-cmo.def"
173 #include "riscv-scalar-crypto.def"
174
175 DIRECT_BUILTIN (frflags, RISCV_USI_FTYPE, hard_float),
176 DIRECT_NO_TARGET_BUILTIN (fsflags, RISCV_VOID_FTYPE_USI, hard_float),
177 DIRECT_NO_TARGET_BUILTIN (pause, RISCV_VOID_FTYPE, always),
178 };
179
180 /* Index I is the function declaration for riscv_builtins[I], or null if the
181 function isn't defined on this target. */
182 static GTY(()) tree riscv_builtin_decls[ARRAY_SIZE (riscv_builtins)];
183
184 /* Get the index I of the function declaration for riscv_builtin_decls[I]
185 using the instruction code or return null if not defined for the target. */
186 static GTY(()) int riscv_builtin_decl_index[NUM_INSN_CODES];
187
188 #define GET_BUILTIN_DECL(CODE) \
189 riscv_builtin_decls[riscv_builtin_decl_index[(CODE)]]
190
191 tree riscv_float16_type_node = NULL_TREE;
192
193 /* Return the function type associated with function prototype TYPE. */
194
195 static tree
196 riscv_build_function_type (enum riscv_function_type type)
197 {
198 static tree types[(int) RISCV_MAX_FTYPE_MAX];
199
200 if (types[(int) type] == NULL_TREE)
201 switch (type)
202 {
203 #define DEF_RISCV_FTYPE(NUM, ARGS) \
204 case RISCV_FTYPE_NAME##NUM ARGS: \
205 types[(int) type] \
206 = build_function_type_list (RISCV_FTYPE_ATYPES##NUM ARGS, \
207 NULL_TREE); \
208 break;
209 #include "config/riscv/riscv-ftypes.def"
210 #undef DEF_RISCV_FTYPE
211 default:
212 gcc_unreachable ();
213 }
214
215 return types[(int) type];
216 }
217
218 static void
219 riscv_init_builtin_types (void)
220 {
221 /* Provide the _Float16 type and float16_type_node if needed. */
222 if (!float16_type_node)
223 {
224 riscv_float16_type_node = make_node (REAL_TYPE);
225 TYPE_PRECISION (riscv_float16_type_node) = 16;
226 SET_TYPE_MODE (riscv_float16_type_node, HFmode);
227 layout_type (riscv_float16_type_node);
228 }
229 else
230 riscv_float16_type_node = float16_type_node;
231
232 if (!maybe_get_identifier ("_Float16"))
233 lang_hooks.types.register_builtin_type (riscv_float16_type_node,
234 "_Float16");
235 }
236
237 /* Implement TARGET_INIT_BUILTINS. */
238
239 void
240 riscv_init_builtins (void)
241 {
242 riscv_init_builtin_types ();
243 riscv_vector::init_builtins ();
244
245 for (size_t i = 0; i < ARRAY_SIZE (riscv_builtins); i++)
246 {
247 const struct riscv_builtin_description *d = &riscv_builtins[i];
248 if (d->avail ())
249 {
250 tree type = riscv_build_function_type (d->prototype);
251 riscv_builtin_decls[i]
252 = add_builtin_function (d->name, type,
253 (i << RISCV_BUILTIN_SHIFT)
254 + RISCV_BUILTIN_GENERAL,
255 BUILT_IN_MD, NULL, NULL);
256 riscv_builtin_decl_index[d->icode] = i;
257 }
258 }
259 }
260
261 /* Implement TARGET_BUILTIN_DECL. */
262
263 tree
264 riscv_builtin_decl (unsigned int code, bool initialize_p ATTRIBUTE_UNUSED)
265 {
266 unsigned int subcode = code >> RISCV_BUILTIN_SHIFT;
267 switch (code & RISCV_BUILTIN_CLASS)
268 {
269 case RISCV_BUILTIN_GENERAL:
270 if (subcode >= ARRAY_SIZE (riscv_builtins))
271 return error_mark_node;
272 return riscv_builtin_decls[subcode];
273
274 case RISCV_BUILTIN_VECTOR:
275 return riscv_vector::builtin_decl (subcode, initialize_p);
276 }
277 return error_mark_node;
278 }
279
280 /* Take argument ARGNO from EXP's argument list and convert it into
281 an expand operand. Store the operand in *OP. */
282
283 static void
284 riscv_prepare_builtin_arg (struct expand_operand *op, tree exp, unsigned argno)
285 {
286 tree arg = CALL_EXPR_ARG (exp, argno);
287 create_input_operand (op, expand_normal (arg), TYPE_MODE (TREE_TYPE (arg)));
288 }
289
290 /* Expand instruction ICODE as part of a built-in function sequence.
291 Use the first NOPS elements of OPS as the instruction's operands.
292 HAS_TARGET_P is true if operand 0 is a target; it is false if the
293 instruction has no target.
294
295 Return the target rtx if HAS_TARGET_P, otherwise return const0_rtx. */
296
297 static rtx
298 riscv_expand_builtin_insn (enum insn_code icode, unsigned int n_ops,
299 struct expand_operand *ops, bool has_target_p)
300 {
301 if (!maybe_expand_insn (icode, n_ops, ops))
302 {
303 error ("invalid argument to built-in function");
304 return has_target_p ? gen_reg_rtx (ops[0].mode) : const0_rtx;
305 }
306
307 return has_target_p ? ops[0].value : const0_rtx;
308 }
309
310 /* Expand a RISCV_BUILTIN_DIRECT or RISCV_BUILTIN_DIRECT_NO_TARGET function;
311 HAS_TARGET_P says which. EXP is the CALL_EXPR that calls the function
312 and ICODE is the code of the associated .md pattern. TARGET, if nonnull,
313 suggests a good place to put the result. */
314
315 static rtx
316 riscv_expand_builtin_direct (enum insn_code icode, rtx target, tree exp,
317 bool has_target_p)
318 {
319 struct expand_operand ops[MAX_RECOG_OPERANDS];
320
321 /* Map any target to operand 0. */
322 int opno = 0;
323 if (has_target_p)
324 create_output_operand (&ops[opno++], target, TYPE_MODE (TREE_TYPE (exp)));
325
326 /* Map the arguments to the other operands. */
327 gcc_assert (opno + call_expr_nargs (exp)
328 == insn_data[icode].n_generator_args);
329 for (int argno = 0; argno < call_expr_nargs (exp); argno++)
330 riscv_prepare_builtin_arg (&ops[opno++], exp, argno);
331
332 return riscv_expand_builtin_insn (icode, opno, ops, has_target_p);
333 }
334
335 /* Implement TARGET_EXPAND_BUILTIN. */
336
337 rtx
338 riscv_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
339 machine_mode mode ATTRIBUTE_UNUSED,
340 int ignore ATTRIBUTE_UNUSED)
341 {
342 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
343 unsigned int fcode = DECL_MD_FUNCTION_CODE (fndecl);
344 unsigned int subcode = fcode >> RISCV_BUILTIN_SHIFT;
345 switch (fcode & RISCV_BUILTIN_CLASS)
346 {
347 case RISCV_BUILTIN_VECTOR:
348 return riscv_vector::expand_builtin (subcode, exp, target);
349 case RISCV_BUILTIN_GENERAL: {
350 const struct riscv_builtin_description *d = &riscv_builtins[subcode];
351
352 switch (d->builtin_type)
353 {
354 case RISCV_BUILTIN_DIRECT:
355 return riscv_expand_builtin_direct (d->icode, target, exp, true);
356
357 case RISCV_BUILTIN_DIRECT_NO_TARGET:
358 return riscv_expand_builtin_direct (d->icode, target, exp, false);
359 }
360 }
361 }
362
363 gcc_unreachable ();
364 }
365
366 /* Implement TARGET_ATOMIC_ASSIGN_EXPAND_FENV. */
367
368 void
369 riscv_atomic_assign_expand_fenv (tree *hold, tree *clear, tree *update)
370 {
371 if (!(TARGET_HARD_FLOAT || TARGET_ZFINX))
372 return;
373
374 tree frflags = GET_BUILTIN_DECL (CODE_FOR_riscv_frflags);
375 tree fsflags = GET_BUILTIN_DECL (CODE_FOR_riscv_fsflags);
376 tree old_flags = create_tmp_var_raw (RISCV_ATYPE_USI);
377
378 *hold = build4 (TARGET_EXPR, RISCV_ATYPE_USI, old_flags,
379 build_call_expr (frflags, 0), NULL_TREE, NULL_TREE);
380 *clear = build_call_expr (fsflags, 1, old_flags);
381 *update = NULL_TREE;
382 }