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