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).
5 This file is part of GCC.
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)
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.
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/>. */
23 #include "coretypes.h"
27 #include "gimple-expr.h"
30 #include "profile-count.h"
33 #include "diagnostic-core.h"
34 #include "stor-layout.h"
36 #include "langhooks.h"
38 /* Macros to create an enumeration identifier for a function prototype. */
39 #define RISCV_FTYPE_NAME1(A, B) RISCV_##A##_FTYPE_##B
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
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. */
54 /* Likewise, but with return type VOID. */
55 RISCV_BUILTIN_DIRECT_NO_TARGET
58 /* Declare an availability predicate for built-in functions. */
59 #define AVAIL(NAME, COND) \
61 riscv_builtin_avail_##NAME (void) \
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. */
72 /* The name of the built-in function. */
75 /* Specifies how the function should be expanded. */
76 enum riscv_builtin_type builtin_type
;
78 /* The function's prototype. */
79 enum riscv_function_type prototype
;
81 /* Whether the function is available. */
82 unsigned int (*avail
) (void);
85 AVAIL (hard_float
, TARGET_HARD_FLOAT
)
87 /* Construct a riscv_builtin_description from the given arguments.
89 INSN is the name of the associated instruction pattern, without the
90 leading CODE_FOR_riscv_.
92 NAME is the name of the function itself, without the leading
95 BUILTIN_TYPE and FUNCTION_TYPE are riscv_builtin_description fields.
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 }
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)
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)
116 /* Argument types. */
117 #define RISCV_ATYPE_VOID void_type_node
118 #define RISCV_ATYPE_USI unsigned_intSI_type_node
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
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
)
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
)];
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
];
138 #define GET_BUILTIN_DECL(CODE) \
139 riscv_builtin_decls[riscv_builtin_decl_index[(CODE)]]
141 /* Return the function type associated with function prototype TYPE. */
144 riscv_build_function_type (enum riscv_function_type type
)
146 static tree types
[(int) RISCV_MAX_FTYPE_MAX
];
148 if (types
[(int) type
] == NULL_TREE
)
151 #define DEF_RISCV_FTYPE(NUM, ARGS) \
152 case RISCV_FTYPE_NAME##NUM ARGS: \
154 = build_function_type_list (RISCV_FTYPE_ATYPES##NUM ARGS, \
157 #include "config/riscv/riscv-ftypes.def"
158 #undef DEF_RISCV_FTYPE
163 return types
[(int) type
];
166 /* Implement TARGET_INIT_BUILTINS. */
169 riscv_init_builtins (void)
171 for (size_t i
= 0; i
< ARRAY_SIZE (riscv_builtins
); i
++)
173 const struct riscv_builtin_description
*d
= &riscv_builtins
[i
];
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
;
184 /* Implement TARGET_BUILTIN_DECL. */
187 riscv_builtin_decl (unsigned int code
, bool initialize_p ATTRIBUTE_UNUSED
)
189 if (code
>= ARRAY_SIZE (riscv_builtins
))
190 return error_mark_node
;
191 return riscv_builtin_decls
[code
];
194 /* Take argument ARGNO from EXP's argument list and convert it into
195 an expand operand. Store the operand in *OP. */
198 riscv_prepare_builtin_arg (struct expand_operand
*op
, tree exp
, unsigned argno
)
200 tree arg
= CALL_EXPR_ARG (exp
, argno
);
201 create_input_operand (op
, expand_normal (arg
), TYPE_MODE (TREE_TYPE (arg
)));
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.
209 Return the target rtx if HAS_TARGET_P, otherwise return const0_rtx. */
212 riscv_expand_builtin_insn (enum insn_code icode
, unsigned int n_ops
,
213 struct expand_operand
*ops
, bool has_target_p
)
215 if (!maybe_expand_insn (icode
, n_ops
, ops
))
217 error ("invalid argument to built-in function");
218 return has_target_p
? gen_reg_rtx (ops
[0].mode
) : const0_rtx
;
221 return has_target_p
? ops
[0].value
: const0_rtx
;
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. */
230 riscv_expand_builtin_direct (enum insn_code icode
, rtx target
, tree exp
,
233 struct expand_operand ops
[MAX_RECOG_OPERANDS
];
235 /* Map any target to operand 0. */
238 create_output_operand (&ops
[opno
++], target
, TYPE_MODE (TREE_TYPE (exp
)));
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
);
246 return riscv_expand_builtin_insn (icode
, opno
, ops
, has_target_p
);
249 /* Implement TARGET_EXPAND_BUILTIN. */
252 riscv_expand_builtin (tree exp
, rtx target
, rtx subtarget ATTRIBUTE_UNUSED
,
253 machine_mode mode ATTRIBUTE_UNUSED
,
254 int ignore ATTRIBUTE_UNUSED
)
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
];
260 switch (d
->builtin_type
)
262 case RISCV_BUILTIN_DIRECT
:
263 return riscv_expand_builtin_direct (d
->icode
, target
, exp
, true);
265 case RISCV_BUILTIN_DIRECT_NO_TARGET
:
266 return riscv_expand_builtin_direct (d
->icode
, target
, exp
, false);
272 /* Implement TARGET_ATOMIC_ASSIGN_EXPAND_FENV. */
275 riscv_atomic_assign_expand_fenv (tree
*hold
, tree
*clear
, tree
*update
)
277 if (!TARGET_HARD_FLOAT
)
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
);
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
);