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