]>
Commit | Line | Data |
---|---|---|
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 | ||
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 | ||
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. */ | |
45 | enum 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. */ | |
53 | enum 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. */ | |
70 | struct 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 | ||
88 | AVAIL (hard_float, TARGET_HARD_FLOAT) | |
89 | ||
3df3ca90 S |
90 | |
91 | AVAIL (clean32, TARGET_ZICBOM && !TARGET_64BIT) | |
92 | AVAIL (clean64, TARGET_ZICBOM && TARGET_64BIT) | |
93 | AVAIL (flush32, TARGET_ZICBOM && !TARGET_64BIT) | |
94 | AVAIL (flush64, TARGET_ZICBOM && TARGET_64BIT) | |
95 | AVAIL (inval32, TARGET_ZICBOM && !TARGET_64BIT) | |
96 | AVAIL (inval64, TARGET_ZICBOM && TARGET_64BIT) | |
97 | AVAIL (zero32, TARGET_ZICBOZ && !TARGET_64BIT) | |
98 | AVAIL (zero64, TARGET_ZICBOZ && TARGET_64BIT) | |
99 | AVAIL (prefetchi32, TARGET_ZICBOP && !TARGET_64BIT) | |
100 | AVAIL (prefetchi64, TARGET_ZICBOP && TARGET_64BIT) | |
101 | ||
09cae750 PD |
102 | /* Construct a riscv_builtin_description from the given arguments. |
103 | ||
104 | INSN is the name of the associated instruction pattern, without the | |
105 | leading CODE_FOR_riscv_. | |
106 | ||
107 | NAME is the name of the function itself, without the leading | |
108 | "__builtin_riscv_". | |
109 | ||
110 | BUILTIN_TYPE and FUNCTION_TYPE are riscv_builtin_description fields. | |
111 | ||
112 | AVAIL is the name of the availability predicate, without the leading | |
113 | riscv_builtin_avail_. */ | |
114 | #define RISCV_BUILTIN(INSN, NAME, BUILTIN_TYPE, FUNCTION_TYPE, AVAIL) \ | |
115 | { CODE_FOR_riscv_ ## INSN, "__builtin_riscv_" NAME, \ | |
116 | BUILTIN_TYPE, FUNCTION_TYPE, riscv_builtin_avail_ ## AVAIL } | |
117 | ||
118 | /* Define __builtin_riscv_<INSN>, which is a RISCV_BUILTIN_DIRECT function | |
119 | mapped to instruction CODE_FOR_riscv_<INSN>, FUNCTION_TYPE and AVAIL | |
120 | are as for RISCV_BUILTIN. */ | |
121 | #define DIRECT_BUILTIN(INSN, FUNCTION_TYPE, AVAIL) \ | |
122 | RISCV_BUILTIN (INSN, #INSN, RISCV_BUILTIN_DIRECT, FUNCTION_TYPE, AVAIL) | |
123 | ||
124 | /* Define __builtin_riscv_<INSN>, which is a RISCV_BUILTIN_DIRECT_NO_TARGET | |
125 | function mapped to instruction CODE_FOR_riscv_<INSN>, FUNCTION_TYPE | |
126 | and AVAIL are as for RISCV_BUILTIN. */ | |
127 | #define DIRECT_NO_TARGET_BUILTIN(INSN, FUNCTION_TYPE, AVAIL) \ | |
128 | RISCV_BUILTIN (INSN, #INSN, RISCV_BUILTIN_DIRECT_NO_TARGET, \ | |
129 | FUNCTION_TYPE, AVAIL) | |
130 | ||
131 | /* Argument types. */ | |
132 | #define RISCV_ATYPE_VOID void_type_node | |
133 | #define RISCV_ATYPE_USI unsigned_intSI_type_node | |
3df3ca90 S |
134 | #define RISCV_ATYPE_SI intSI_type_node |
135 | #define RISCV_ATYPE_DI intDI_type_node | |
970b03c0 | 136 | #define RISCV_ATYPE_VOID_PTR ptr_type_node |
09cae750 PD |
137 | |
138 | /* RISCV_FTYPE_ATYPESN takes N RISCV_FTYPES-like type codes and lists | |
139 | their associated RISCV_ATYPEs. */ | |
33d9794b KC |
140 | #define RISCV_FTYPE_ATYPES0(A) \ |
141 | RISCV_ATYPE_##A | |
09cae750 PD |
142 | #define RISCV_FTYPE_ATYPES1(A, B) \ |
143 | RISCV_ATYPE_##A, RISCV_ATYPE_##B | |
144 | ||
145 | static const struct riscv_builtin_description riscv_builtins[] = { | |
3df3ca90 S |
146 | #include "riscv-cmo.def" |
147 | ||
33d9794b | 148 | DIRECT_BUILTIN (frflags, RISCV_USI_FTYPE, hard_float), |
09cae750 PD |
149 | DIRECT_NO_TARGET_BUILTIN (fsflags, RISCV_VOID_FTYPE_USI, hard_float) |
150 | }; | |
151 | ||
152 | /* Index I is the function declaration for riscv_builtins[I], or null if the | |
153 | function isn't defined on this target. */ | |
154 | static GTY(()) tree riscv_builtin_decls[ARRAY_SIZE (riscv_builtins)]; | |
155 | ||
156 | /* Get the index I of the function declaration for riscv_builtin_decls[I] | |
157 | using the instruction code or return null if not defined for the target. */ | |
158 | static GTY(()) int riscv_builtin_decl_index[NUM_INSN_CODES]; | |
159 | ||
160 | #define GET_BUILTIN_DECL(CODE) \ | |
161 | riscv_builtin_decls[riscv_builtin_decl_index[(CODE)]] | |
162 | ||
163 | /* Return the function type associated with function prototype TYPE. */ | |
164 | ||
165 | static tree | |
166 | riscv_build_function_type (enum riscv_function_type type) | |
167 | { | |
168 | static tree types[(int) RISCV_MAX_FTYPE_MAX]; | |
169 | ||
170 | if (types[(int) type] == NULL_TREE) | |
171 | switch (type) | |
172 | { | |
173 | #define DEF_RISCV_FTYPE(NUM, ARGS) \ | |
174 | case RISCV_FTYPE_NAME##NUM ARGS: \ | |
175 | types[(int) type] \ | |
176 | = build_function_type_list (RISCV_FTYPE_ATYPES##NUM ARGS, \ | |
177 | NULL_TREE); \ | |
178 | break; | |
179 | #include "config/riscv/riscv-ftypes.def" | |
180 | #undef DEF_RISCV_FTYPE | |
181 | default: | |
182 | gcc_unreachable (); | |
183 | } | |
184 | ||
185 | return types[(int) type]; | |
186 | } | |
187 | ||
188 | /* Implement TARGET_INIT_BUILTINS. */ | |
189 | ||
190 | void | |
191 | riscv_init_builtins (void) | |
192 | { | |
193 | for (size_t i = 0; i < ARRAY_SIZE (riscv_builtins); i++) | |
194 | { | |
195 | const struct riscv_builtin_description *d = &riscv_builtins[i]; | |
196 | if (d->avail ()) | |
197 | { | |
198 | tree type = riscv_build_function_type (d->prototype); | |
199 | riscv_builtin_decls[i] | |
200 | = add_builtin_function (d->name, type, i, BUILT_IN_MD, NULL, NULL); | |
201 | riscv_builtin_decl_index[d->icode] = i; | |
202 | } | |
203 | } | |
204 | } | |
205 | ||
206 | /* Implement TARGET_BUILTIN_DECL. */ | |
207 | ||
208 | tree | |
209 | riscv_builtin_decl (unsigned int code, bool initialize_p ATTRIBUTE_UNUSED) | |
210 | { | |
211 | if (code >= ARRAY_SIZE (riscv_builtins)) | |
212 | return error_mark_node; | |
213 | return riscv_builtin_decls[code]; | |
214 | } | |
215 | ||
216 | /* Take argument ARGNO from EXP's argument list and convert it into | |
217 | an expand operand. Store the operand in *OP. */ | |
218 | ||
219 | static void | |
220 | riscv_prepare_builtin_arg (struct expand_operand *op, tree exp, unsigned argno) | |
221 | { | |
222 | tree arg = CALL_EXPR_ARG (exp, argno); | |
223 | create_input_operand (op, expand_normal (arg), TYPE_MODE (TREE_TYPE (arg))); | |
224 | } | |
225 | ||
226 | /* Expand instruction ICODE as part of a built-in function sequence. | |
227 | Use the first NOPS elements of OPS as the instruction's operands. | |
228 | HAS_TARGET_P is true if operand 0 is a target; it is false if the | |
229 | instruction has no target. | |
230 | ||
231 | Return the target rtx if HAS_TARGET_P, otherwise return const0_rtx. */ | |
232 | ||
233 | static rtx | |
234 | riscv_expand_builtin_insn (enum insn_code icode, unsigned int n_ops, | |
235 | struct expand_operand *ops, bool has_target_p) | |
236 | { | |
237 | if (!maybe_expand_insn (icode, n_ops, ops)) | |
238 | { | |
239 | error ("invalid argument to built-in function"); | |
240 | return has_target_p ? gen_reg_rtx (ops[0].mode) : const0_rtx; | |
241 | } | |
242 | ||
243 | return has_target_p ? ops[0].value : const0_rtx; | |
244 | } | |
245 | ||
246 | /* Expand a RISCV_BUILTIN_DIRECT or RISCV_BUILTIN_DIRECT_NO_TARGET function; | |
247 | HAS_TARGET_P says which. EXP is the CALL_EXPR that calls the function | |
248 | and ICODE is the code of the associated .md pattern. TARGET, if nonnull, | |
249 | suggests a good place to put the result. */ | |
250 | ||
251 | static rtx | |
252 | riscv_expand_builtin_direct (enum insn_code icode, rtx target, tree exp, | |
253 | bool has_target_p) | |
254 | { | |
255 | struct expand_operand ops[MAX_RECOG_OPERANDS]; | |
256 | ||
257 | /* Map any target to operand 0. */ | |
258 | int opno = 0; | |
259 | if (has_target_p) | |
260 | create_output_operand (&ops[opno++], target, TYPE_MODE (TREE_TYPE (exp))); | |
261 | ||
262 | /* Map the arguments to the other operands. */ | |
263 | gcc_assert (opno + call_expr_nargs (exp) | |
264 | == insn_data[icode].n_generator_args); | |
265 | for (int argno = 0; argno < call_expr_nargs (exp); argno++) | |
266 | riscv_prepare_builtin_arg (&ops[opno++], exp, argno); | |
267 | ||
268 | return riscv_expand_builtin_insn (icode, opno, ops, has_target_p); | |
269 | } | |
270 | ||
271 | /* Implement TARGET_EXPAND_BUILTIN. */ | |
272 | ||
273 | rtx | |
274 | riscv_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED, | |
275 | machine_mode mode ATTRIBUTE_UNUSED, | |
276 | int ignore ATTRIBUTE_UNUSED) | |
277 | { | |
278 | tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0); | |
4d732405 | 279 | unsigned int fcode = DECL_MD_FUNCTION_CODE (fndecl); |
09cae750 PD |
280 | const struct riscv_builtin_description *d = &riscv_builtins[fcode]; |
281 | ||
282 | switch (d->builtin_type) | |
283 | { | |
284 | case RISCV_BUILTIN_DIRECT: | |
285 | return riscv_expand_builtin_direct (d->icode, target, exp, true); | |
286 | ||
287 | case RISCV_BUILTIN_DIRECT_NO_TARGET: | |
288 | return riscv_expand_builtin_direct (d->icode, target, exp, false); | |
289 | } | |
290 | ||
291 | gcc_unreachable (); | |
292 | } | |
293 | ||
294 | /* Implement TARGET_ATOMIC_ASSIGN_EXPAND_FENV. */ | |
295 | ||
296 | void | |
297 | riscv_atomic_assign_expand_fenv (tree *hold, tree *clear, tree *update) | |
298 | { | |
299 | if (!TARGET_HARD_FLOAT) | |
300 | return; | |
301 | ||
302 | tree frflags = GET_BUILTIN_DECL (CODE_FOR_riscv_frflags); | |
303 | tree fsflags = GET_BUILTIN_DECL (CODE_FOR_riscv_fsflags); | |
304 | tree old_flags = create_tmp_var_raw (RISCV_ATYPE_USI); | |
305 | ||
b4ace720 JJ |
306 | *hold = build4 (TARGET_EXPR, RISCV_ATYPE_USI, old_flags, |
307 | build_call_expr (frflags, 0), NULL_TREE, NULL_TREE); | |
09cae750 PD |
308 | *clear = build_call_expr (fsflags, 1, old_flags); |
309 | *update = NULL_TREE; | |
310 | } |