]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/config/riscv/riscv-builtins.c
Update copyright years.
[thirdparty/gcc.git] / gcc / config / riscv / riscv-builtins.c
CommitLineData
09cae750 1/* Subroutines used for expanding RISC-V builtins.
7adcbafe 2 Copyright (C) 2011-2022 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"
37#include "expr.h"
38#include "langhooks.h"
39
40/* Macros to create an enumeration identifier for a function prototype. */
33d9794b 41#define RISCV_FTYPE_NAME0(A) RISCV_##A##_FTYPE
09cae750
PD
42#define RISCV_FTYPE_NAME1(A, B) RISCV_##A##_FTYPE_##B
43
44/* Classifies the prototype of a built-in function. */
45enum riscv_function_type {
46#define DEF_RISCV_FTYPE(NARGS, LIST) RISCV_FTYPE_NAME##NARGS LIST,
47#include "config/riscv/riscv-ftypes.def"
48#undef DEF_RISCV_FTYPE
49 RISCV_MAX_FTYPE_MAX
50};
51
52/* Specifies how a built-in function should be converted into rtl. */
53enum riscv_builtin_type {
54 /* The function corresponds directly to an .md pattern. */
55 RISCV_BUILTIN_DIRECT,
56
57 /* Likewise, but with return type VOID. */
58 RISCV_BUILTIN_DIRECT_NO_TARGET
59};
60
61/* Declare an availability predicate for built-in functions. */
62#define AVAIL(NAME, COND) \
63 static unsigned int \
64 riscv_builtin_avail_##NAME (void) \
65 { \
66 return (COND); \
67 }
68
69/* This structure describes a single built-in function. */
70struct riscv_builtin_description {
71 /* The code of the main .md file instruction. See riscv_builtin_type
72 for more information. */
73 enum insn_code icode;
74
75 /* The name of the built-in function. */
76 const char *name;
77
78 /* Specifies how the function should be expanded. */
79 enum riscv_builtin_type builtin_type;
80
81 /* The function's prototype. */
82 enum riscv_function_type prototype;
83
84 /* Whether the function is available. */
85 unsigned int (*avail) (void);
86};
87
88AVAIL (hard_float, TARGET_HARD_FLOAT)
89
90/* Construct a riscv_builtin_description from the given arguments.
91
92 INSN is the name of the associated instruction pattern, without the
93 leading CODE_FOR_riscv_.
94
95 NAME is the name of the function itself, without the leading
96 "__builtin_riscv_".
97
98 BUILTIN_TYPE and FUNCTION_TYPE are riscv_builtin_description fields.
99
100 AVAIL is the name of the availability predicate, without the leading
101 riscv_builtin_avail_. */
102#define RISCV_BUILTIN(INSN, NAME, BUILTIN_TYPE, FUNCTION_TYPE, AVAIL) \
103 { CODE_FOR_riscv_ ## INSN, "__builtin_riscv_" NAME, \
104 BUILTIN_TYPE, FUNCTION_TYPE, riscv_builtin_avail_ ## AVAIL }
105
106/* Define __builtin_riscv_<INSN>, which is a RISCV_BUILTIN_DIRECT function
107 mapped to instruction CODE_FOR_riscv_<INSN>, FUNCTION_TYPE and AVAIL
108 are as for RISCV_BUILTIN. */
109#define DIRECT_BUILTIN(INSN, FUNCTION_TYPE, AVAIL) \
110 RISCV_BUILTIN (INSN, #INSN, RISCV_BUILTIN_DIRECT, FUNCTION_TYPE, AVAIL)
111
112/* Define __builtin_riscv_<INSN>, which is a RISCV_BUILTIN_DIRECT_NO_TARGET
113 function mapped to instruction CODE_FOR_riscv_<INSN>, FUNCTION_TYPE
114 and AVAIL are as for RISCV_BUILTIN. */
115#define DIRECT_NO_TARGET_BUILTIN(INSN, FUNCTION_TYPE, AVAIL) \
116 RISCV_BUILTIN (INSN, #INSN, RISCV_BUILTIN_DIRECT_NO_TARGET, \
117 FUNCTION_TYPE, AVAIL)
118
119/* Argument types. */
120#define RISCV_ATYPE_VOID void_type_node
121#define RISCV_ATYPE_USI unsigned_intSI_type_node
122
123/* RISCV_FTYPE_ATYPESN takes N RISCV_FTYPES-like type codes and lists
124 their associated RISCV_ATYPEs. */
33d9794b
KC
125#define RISCV_FTYPE_ATYPES0(A) \
126 RISCV_ATYPE_##A
09cae750
PD
127#define RISCV_FTYPE_ATYPES1(A, B) \
128 RISCV_ATYPE_##A, RISCV_ATYPE_##B
129
130static const struct riscv_builtin_description riscv_builtins[] = {
33d9794b 131 DIRECT_BUILTIN (frflags, RISCV_USI_FTYPE, hard_float),
09cae750
PD
132 DIRECT_NO_TARGET_BUILTIN (fsflags, RISCV_VOID_FTYPE_USI, hard_float)
133};
134
135/* Index I is the function declaration for riscv_builtins[I], or null if the
136 function isn't defined on this target. */
137static GTY(()) tree riscv_builtin_decls[ARRAY_SIZE (riscv_builtins)];
138
139/* Get the index I of the function declaration for riscv_builtin_decls[I]
140 using the instruction code or return null if not defined for the target. */
141static GTY(()) int riscv_builtin_decl_index[NUM_INSN_CODES];
142
143#define GET_BUILTIN_DECL(CODE) \
144 riscv_builtin_decls[riscv_builtin_decl_index[(CODE)]]
145
146/* Return the function type associated with function prototype TYPE. */
147
148static tree
149riscv_build_function_type (enum riscv_function_type type)
150{
151 static tree types[(int) RISCV_MAX_FTYPE_MAX];
152
153 if (types[(int) type] == NULL_TREE)
154 switch (type)
155 {
156#define DEF_RISCV_FTYPE(NUM, ARGS) \
157 case RISCV_FTYPE_NAME##NUM ARGS: \
158 types[(int) type] \
159 = build_function_type_list (RISCV_FTYPE_ATYPES##NUM ARGS, \
160 NULL_TREE); \
161 break;
162#include "config/riscv/riscv-ftypes.def"
163#undef DEF_RISCV_FTYPE
164 default:
165 gcc_unreachable ();
166 }
167
168 return types[(int) type];
169}
170
171/* Implement TARGET_INIT_BUILTINS. */
172
173void
174riscv_init_builtins (void)
175{
176 for (size_t i = 0; i < ARRAY_SIZE (riscv_builtins); i++)
177 {
178 const struct riscv_builtin_description *d = &riscv_builtins[i];
179 if (d->avail ())
180 {
181 tree type = riscv_build_function_type (d->prototype);
182 riscv_builtin_decls[i]
183 = add_builtin_function (d->name, type, i, BUILT_IN_MD, NULL, NULL);
184 riscv_builtin_decl_index[d->icode] = i;
185 }
186 }
187}
188
189/* Implement TARGET_BUILTIN_DECL. */
190
191tree
192riscv_builtin_decl (unsigned int code, bool initialize_p ATTRIBUTE_UNUSED)
193{
194 if (code >= ARRAY_SIZE (riscv_builtins))
195 return error_mark_node;
196 return riscv_builtin_decls[code];
197}
198
199/* Take argument ARGNO from EXP's argument list and convert it into
200 an expand operand. Store the operand in *OP. */
201
202static void
203riscv_prepare_builtin_arg (struct expand_operand *op, tree exp, unsigned argno)
204{
205 tree arg = CALL_EXPR_ARG (exp, argno);
206 create_input_operand (op, expand_normal (arg), TYPE_MODE (TREE_TYPE (arg)));
207}
208
209/* Expand instruction ICODE as part of a built-in function sequence.
210 Use the first NOPS elements of OPS as the instruction's operands.
211 HAS_TARGET_P is true if operand 0 is a target; it is false if the
212 instruction has no target.
213
214 Return the target rtx if HAS_TARGET_P, otherwise return const0_rtx. */
215
216static rtx
217riscv_expand_builtin_insn (enum insn_code icode, unsigned int n_ops,
218 struct expand_operand *ops, bool has_target_p)
219{
220 if (!maybe_expand_insn (icode, n_ops, ops))
221 {
222 error ("invalid argument to built-in function");
223 return has_target_p ? gen_reg_rtx (ops[0].mode) : const0_rtx;
224 }
225
226 return has_target_p ? ops[0].value : const0_rtx;
227}
228
229/* Expand a RISCV_BUILTIN_DIRECT or RISCV_BUILTIN_DIRECT_NO_TARGET function;
230 HAS_TARGET_P says which. EXP is the CALL_EXPR that calls the function
231 and ICODE is the code of the associated .md pattern. TARGET, if nonnull,
232 suggests a good place to put the result. */
233
234static rtx
235riscv_expand_builtin_direct (enum insn_code icode, rtx target, tree exp,
236 bool has_target_p)
237{
238 struct expand_operand ops[MAX_RECOG_OPERANDS];
239
240 /* Map any target to operand 0. */
241 int opno = 0;
242 if (has_target_p)
243 create_output_operand (&ops[opno++], target, TYPE_MODE (TREE_TYPE (exp)));
244
245 /* Map the arguments to the other operands. */
246 gcc_assert (opno + call_expr_nargs (exp)
247 == insn_data[icode].n_generator_args);
248 for (int argno = 0; argno < call_expr_nargs (exp); argno++)
249 riscv_prepare_builtin_arg (&ops[opno++], exp, argno);
250
251 return riscv_expand_builtin_insn (icode, opno, ops, has_target_p);
252}
253
254/* Implement TARGET_EXPAND_BUILTIN. */
255
256rtx
257riscv_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
258 machine_mode mode ATTRIBUTE_UNUSED,
259 int ignore ATTRIBUTE_UNUSED)
260{
261 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
4d732405 262 unsigned int fcode = DECL_MD_FUNCTION_CODE (fndecl);
09cae750
PD
263 const struct riscv_builtin_description *d = &riscv_builtins[fcode];
264
265 switch (d->builtin_type)
266 {
267 case RISCV_BUILTIN_DIRECT:
268 return riscv_expand_builtin_direct (d->icode, target, exp, true);
269
270 case RISCV_BUILTIN_DIRECT_NO_TARGET:
271 return riscv_expand_builtin_direct (d->icode, target, exp, false);
272 }
273
274 gcc_unreachable ();
275}
276
277/* Implement TARGET_ATOMIC_ASSIGN_EXPAND_FENV. */
278
279void
280riscv_atomic_assign_expand_fenv (tree *hold, tree *clear, tree *update)
281{
282 if (!TARGET_HARD_FLOAT)
283 return;
284
285 tree frflags = GET_BUILTIN_DECL (CODE_FOR_riscv_frflags);
286 tree fsflags = GET_BUILTIN_DECL (CODE_FOR_riscv_fsflags);
287 tree old_flags = create_tmp_var_raw (RISCV_ATYPE_USI);
288
b4ace720
JJ
289 *hold = build4 (TARGET_EXPR, RISCV_ATYPE_USI, old_flags,
290 build_call_expr (frflags, 0), NULL_TREE, NULL_TREE);
09cae750
PD
291 *clear = build_call_expr (fsflags, 1, old_flags);
292 *update = NULL_TREE;
293}