1 /* Tree-based target query functions relating to optabs
2 Copyright (C) 1987-2021 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
25 #include "insn-codes.h"
30 #include "optabs-tree.h"
31 #include "stor-layout.h"
33 /* Return the optab used for computing the operation given by the tree code,
34 CODE and the tree EXP. This function is not always usable (for example, it
35 cannot give complete results for multiplication or division) but probably
36 ought to be relied on more widely throughout the expander. */
38 optab_for_tree_code (enum tree_code code
, const_tree type
,
39 enum optab_subtype subtype
)
51 return one_cmpl_optab
;
56 case MULT_HIGHPART_EXPR
:
57 return TYPE_UNSIGNED (type
) ? umul_highpart_optab
: smul_highpart_optab
;
63 return TYPE_UNSIGNED (type
) ? umod_optab
: smod_optab
;
71 if (TYPE_SATURATING (type
))
72 return TYPE_UNSIGNED (type
) ? usdiv_optab
: ssdiv_optab
;
73 return TYPE_UNSIGNED (type
) ? udiv_optab
: sdiv_optab
;
76 if (TREE_CODE (type
) == VECTOR_TYPE
)
78 if (subtype
== optab_vector
)
79 return TYPE_SATURATING (type
) ? unknown_optab
: vashl_optab
;
81 gcc_assert (subtype
== optab_scalar
);
83 if (TYPE_SATURATING (type
))
84 return TYPE_UNSIGNED (type
) ? usashl_optab
: ssashl_optab
;
88 if (TREE_CODE (type
) == VECTOR_TYPE
)
90 if (subtype
== optab_vector
)
91 return TYPE_UNSIGNED (type
) ? vlshr_optab
: vashr_optab
;
93 gcc_assert (subtype
== optab_scalar
);
95 return TYPE_UNSIGNED (type
) ? lshr_optab
: ashr_optab
;
98 if (TREE_CODE (type
) == VECTOR_TYPE
)
100 if (subtype
== optab_vector
)
103 gcc_assert (subtype
== optab_scalar
);
108 if (TREE_CODE (type
) == VECTOR_TYPE
)
110 if (subtype
== optab_vector
)
113 gcc_assert (subtype
== optab_scalar
);
118 return TYPE_UNSIGNED (type
) ? umax_optab
: smax_optab
;
121 return TYPE_UNSIGNED (type
) ? umin_optab
: smin_optab
;
123 case REALIGN_LOAD_EXPR
:
124 return vec_realign_load_optab
;
127 return TYPE_UNSIGNED (type
) ? usum_widen_optab
: ssum_widen_optab
;
131 if (subtype
== optab_vector_mixed_sign
)
132 return usdot_prod_optab
;
134 return (TYPE_UNSIGNED (type
) ? udot_prod_optab
: sdot_prod_optab
);
138 return TYPE_UNSIGNED (type
) ? usad_optab
: ssad_optab
;
140 case WIDEN_MULT_PLUS_EXPR
:
141 return (TYPE_UNSIGNED (type
)
142 ? (TYPE_SATURATING (type
)
143 ? usmadd_widen_optab
: umadd_widen_optab
)
144 : (TYPE_SATURATING (type
)
145 ? ssmadd_widen_optab
: smadd_widen_optab
));
147 case WIDEN_MULT_MINUS_EXPR
:
148 return (TYPE_UNSIGNED (type
)
149 ? (TYPE_SATURATING (type
)
150 ? usmsub_widen_optab
: umsub_widen_optab
)
151 : (TYPE_SATURATING (type
)
152 ? ssmsub_widen_optab
: smsub_widen_optab
));
154 case VEC_WIDEN_MULT_HI_EXPR
:
155 return (TYPE_UNSIGNED (type
)
156 ? vec_widen_umult_hi_optab
: vec_widen_smult_hi_optab
);
158 case VEC_WIDEN_MULT_LO_EXPR
:
159 return (TYPE_UNSIGNED (type
)
160 ? vec_widen_umult_lo_optab
: vec_widen_smult_lo_optab
);
162 case VEC_WIDEN_MULT_EVEN_EXPR
:
163 return (TYPE_UNSIGNED (type
)
164 ? vec_widen_umult_even_optab
: vec_widen_smult_even_optab
);
166 case VEC_WIDEN_MULT_ODD_EXPR
:
167 return (TYPE_UNSIGNED (type
)
168 ? vec_widen_umult_odd_optab
: vec_widen_smult_odd_optab
);
170 case VEC_WIDEN_LSHIFT_HI_EXPR
:
171 return (TYPE_UNSIGNED (type
)
172 ? vec_widen_ushiftl_hi_optab
: vec_widen_sshiftl_hi_optab
);
174 case VEC_WIDEN_LSHIFT_LO_EXPR
:
175 return (TYPE_UNSIGNED (type
)
176 ? vec_widen_ushiftl_lo_optab
: vec_widen_sshiftl_lo_optab
);
178 case VEC_WIDEN_PLUS_LO_EXPR
:
179 return (TYPE_UNSIGNED (type
)
180 ? vec_widen_uaddl_lo_optab
: vec_widen_saddl_lo_optab
);
182 case VEC_WIDEN_PLUS_HI_EXPR
:
183 return (TYPE_UNSIGNED (type
)
184 ? vec_widen_uaddl_hi_optab
: vec_widen_saddl_hi_optab
);
186 case VEC_WIDEN_MINUS_LO_EXPR
:
187 return (TYPE_UNSIGNED (type
)
188 ? vec_widen_usubl_lo_optab
: vec_widen_ssubl_lo_optab
);
190 case VEC_WIDEN_MINUS_HI_EXPR
:
191 return (TYPE_UNSIGNED (type
)
192 ? vec_widen_usubl_hi_optab
: vec_widen_ssubl_hi_optab
);
194 case VEC_UNPACK_HI_EXPR
:
195 return (TYPE_UNSIGNED (type
)
196 ? vec_unpacku_hi_optab
: vec_unpacks_hi_optab
);
198 case VEC_UNPACK_LO_EXPR
:
199 return (TYPE_UNSIGNED (type
)
200 ? vec_unpacku_lo_optab
: vec_unpacks_lo_optab
);
202 case VEC_UNPACK_FLOAT_HI_EXPR
:
203 /* The signedness is determined from input operand. */
204 return (TYPE_UNSIGNED (type
)
205 ? vec_unpacku_float_hi_optab
: vec_unpacks_float_hi_optab
);
207 case VEC_UNPACK_FLOAT_LO_EXPR
:
208 /* The signedness is determined from input operand. */
209 return (TYPE_UNSIGNED (type
)
210 ? vec_unpacku_float_lo_optab
: vec_unpacks_float_lo_optab
);
212 case VEC_UNPACK_FIX_TRUNC_HI_EXPR
:
213 /* The signedness is determined from output operand. */
214 return (TYPE_UNSIGNED (type
)
215 ? vec_unpack_ufix_trunc_hi_optab
216 : vec_unpack_sfix_trunc_hi_optab
);
218 case VEC_UNPACK_FIX_TRUNC_LO_EXPR
:
219 /* The signedness is determined from output operand. */
220 return (TYPE_UNSIGNED (type
)
221 ? vec_unpack_ufix_trunc_lo_optab
222 : vec_unpack_sfix_trunc_lo_optab
);
224 case VEC_PACK_TRUNC_EXPR
:
225 return vec_pack_trunc_optab
;
227 case VEC_PACK_SAT_EXPR
:
228 return TYPE_UNSIGNED (type
) ? vec_pack_usat_optab
: vec_pack_ssat_optab
;
230 case VEC_PACK_FIX_TRUNC_EXPR
:
231 /* The signedness is determined from output operand. */
232 return (TYPE_UNSIGNED (type
)
233 ? vec_pack_ufix_trunc_optab
: vec_pack_sfix_trunc_optab
);
235 case VEC_PACK_FLOAT_EXPR
:
236 /* The signedness is determined from input operand. */
237 return (TYPE_UNSIGNED (type
)
238 ? vec_packu_float_optab
: vec_packs_float_optab
);
240 case VEC_DUPLICATE_EXPR
:
241 return vec_duplicate_optab
;
243 case VEC_SERIES_EXPR
:
244 return vec_series_optab
;
250 trapv
= INTEGRAL_TYPE_P (type
) && TYPE_OVERFLOW_TRAPS (type
);
253 case POINTER_PLUS_EXPR
:
255 if (TYPE_SATURATING (type
))
256 return TYPE_UNSIGNED (type
) ? usadd_optab
: ssadd_optab
;
257 return trapv
? addv_optab
: add_optab
;
259 case POINTER_DIFF_EXPR
:
261 if (TYPE_SATURATING (type
))
262 return TYPE_UNSIGNED (type
) ? ussub_optab
: sssub_optab
;
263 return trapv
? subv_optab
: sub_optab
;
266 if (TYPE_SATURATING (type
))
267 return TYPE_UNSIGNED (type
) ? usmul_optab
: ssmul_optab
;
268 return trapv
? smulv_optab
: smul_optab
;
271 if (TYPE_SATURATING (type
))
272 return TYPE_UNSIGNED (type
) ? usneg_optab
: ssneg_optab
;
273 return trapv
? negv_optab
: neg_optab
;
276 return trapv
? absv_optab
: abs_optab
;
281 return unknown_optab
;
285 /* Check whether an operation represented by CODE is a 'half' widening operation
286 in which the input vector type has half the number of bits of the output
287 vector type e.g. V8QI->V8HI.
289 This is handled by widening the inputs using NOP_EXPRs then using a
290 non-widening stmt e.g. MINUS_EXPR. RTL fusing converts these to the widening
291 hardware instructions if supported.
293 The more typical case (handled in supportable_widening_operation) is where
294 the input vector type has the same number of bits as the output vector type.
295 In this case half the elements of the input vectors must be processed at a
296 time into respective vector outputs with elements twice as wide i.e. a
297 'hi'/'lo' pair using codes such as VEC_WIDEN_MINUS_HI/LO.
299 Supported widening operations:
306 - CODE1 - The non-widened code, which will be used after the inputs are
307 converted to the wide type. */
309 supportable_half_widening_operation (enum tree_code code
, tree vectype_out
,
310 tree vectype_in
, enum tree_code
*code1
)
313 enum tree_code dummy_code
;
316 gcc_assert (VECTOR_TYPE_P (vectype_out
) && VECTOR_TYPE_P (vectype_in
));
318 m1
= TYPE_MODE (vectype_out
);
319 m2
= TYPE_MODE (vectype_in
);
321 if (!VECTOR_MODE_P (m1
) || !VECTOR_MODE_P (m2
))
324 if (maybe_ne (TYPE_VECTOR_SUBPARTS (vectype_in
),
325 TYPE_VECTOR_SUBPARTS (vectype_out
)))
330 case WIDEN_LSHIFT_EXPR
:
331 *code1
= LSHIFT_EXPR
;
333 case WIDEN_MINUS_EXPR
:
336 case WIDEN_PLUS_EXPR
:
339 case WIDEN_MULT_EXPR
:
346 if (!supportable_convert_operation (NOP_EXPR
, vectype_out
, vectype_in
,
350 op
= optab_for_tree_code (*code1
, vectype_out
, optab_vector
);
351 return (optab_handler (op
, TYPE_MODE (vectype_out
)) != CODE_FOR_nothing
);
354 /* Function supportable_convert_operation
356 Check whether an operation represented by the code CODE is a
357 convert operation that is supported by the target platform in
358 vector form (i.e., when operating on arguments of type VECTYPE_IN
359 producing a result of type VECTYPE_OUT).
361 Convert operations we currently support directly are FIX_TRUNC and FLOAT.
362 This function checks if these operations are supported
363 by the target platform directly (via vector tree-codes).
366 - CODE1 is code of vector operation to be used when
367 vectorizing the operation, if available. */
370 supportable_convert_operation (enum tree_code code
,
371 tree vectype_out
, tree vectype_in
,
372 enum tree_code
*code1
)
377 gcc_assert (VECTOR_TYPE_P (vectype_out
) && VECTOR_TYPE_P (vectype_in
));
379 m1
= TYPE_MODE (vectype_out
);
380 m2
= TYPE_MODE (vectype_in
);
382 if (!VECTOR_MODE_P (m1
) || !VECTOR_MODE_P (m2
))
385 /* First check if we can done conversion directly. */
386 if ((code
== FIX_TRUNC_EXPR
387 && can_fix_p (m1
,m2
,TYPE_UNSIGNED (vectype_out
), &truncp
)
389 || (code
== FLOAT_EXPR
390 && can_float_p (m1
,m2
,TYPE_UNSIGNED (vectype_in
))
391 != CODE_FOR_nothing
))
397 if (GET_MODE_UNIT_PRECISION (m1
) > GET_MODE_UNIT_PRECISION (m2
)
398 && can_extend_p (m1
, m2
, TYPE_UNSIGNED (vectype_in
)))
404 if (GET_MODE_UNIT_PRECISION (m1
) < GET_MODE_UNIT_PRECISION (m2
)
405 && convert_optab_handler (trunc_optab
, m1
, m2
) != CODE_FOR_nothing
)
414 /* Return true iff vec_cmp_optab/vec_cmpu_optab can handle a vector comparison
415 for code CODE, comparing operands of type VALUE_TYPE and producing a result
416 of type MASK_TYPE. */
419 vec_cmp_icode_p (tree value_type
, tree mask_type
, enum tree_code code
)
421 enum rtx_code rcode
= get_rtx_code_1 (code
, TYPE_UNSIGNED (value_type
));
422 if (rcode
== UNKNOWN
)
425 return can_vec_cmp_compare_p (rcode
, TYPE_MODE (value_type
),
426 TYPE_MODE (mask_type
));
429 /* Return true iff vec_cmpeq_optab can handle a vector comparison for code
430 CODE, comparing operands of type VALUE_TYPE and producing a result of type
434 vec_cmp_eq_icode_p (tree value_type
, tree mask_type
, enum tree_code code
)
436 if (code
!= EQ_EXPR
&& code
!= NE_EXPR
)
439 return get_vec_cmp_eq_icode (TYPE_MODE (value_type
), TYPE_MODE (mask_type
))
443 /* Return TRUE if appropriate vector insn is available
444 for vector comparison expr with vector type VALUE_TYPE
445 and resulting mask with MASK_TYPE. */
448 expand_vec_cmp_expr_p (tree value_type
, tree mask_type
, enum tree_code code
)
450 return vec_cmp_icode_p (value_type
, mask_type
, code
)
451 || vec_cmp_eq_icode_p (value_type
, mask_type
, code
);
454 /* Return true iff vcond_optab/vcondu_optab can handle a vector
455 comparison for code CODE, comparing operands of type CMP_OP_TYPE and
456 producing a result of type VALUE_TYPE. */
459 vcond_icode_p (tree value_type
, tree cmp_op_type
, enum tree_code code
)
461 enum rtx_code rcode
= get_rtx_code_1 (code
, TYPE_UNSIGNED (cmp_op_type
));
462 if (rcode
== UNKNOWN
)
465 return can_vcond_compare_p (rcode
, TYPE_MODE (value_type
),
466 TYPE_MODE (cmp_op_type
));
469 /* Return true iff vcondeq_optab can handle a vector comparison for code CODE,
470 comparing operands of type CMP_OP_TYPE and producing a result of type
474 vcond_eq_icode_p (tree value_type
, tree cmp_op_type
, enum tree_code code
)
476 if (code
!= EQ_EXPR
&& code
!= NE_EXPR
)
479 return get_vcond_eq_icode (TYPE_MODE (value_type
), TYPE_MODE (cmp_op_type
))
483 /* Return TRUE iff, appropriate vector insns are available
484 for vector cond expr with vector type VALUE_TYPE and a comparison
485 with operand vector types in CMP_OP_TYPE. */
488 expand_vec_cond_expr_p (tree value_type
, tree cmp_op_type
, enum tree_code code
)
490 machine_mode value_mode
= TYPE_MODE (value_type
);
491 machine_mode cmp_op_mode
= TYPE_MODE (cmp_op_type
);
492 if (VECTOR_BOOLEAN_TYPE_P (cmp_op_type
)
493 && get_vcond_mask_icode (TYPE_MODE (value_type
),
494 TYPE_MODE (cmp_op_type
)) != CODE_FOR_nothing
)
497 if (maybe_ne (GET_MODE_NUNITS (value_mode
), GET_MODE_NUNITS (cmp_op_mode
)))
500 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
501 /* This may happen, for example, if code == SSA_NAME, in which case we
502 cannot be certain whether a vector insn is available. */
505 return vcond_icode_p (value_type
, cmp_op_type
, code
)
506 || vcond_eq_icode_p (value_type
, cmp_op_type
, code
);
509 /* Use the current target and options to initialize
510 TREE_OPTIMIZATION_OPTABS (OPTNODE). */
513 init_tree_optimization_optabs (tree optnode
)
515 /* Quick exit if we have already computed optabs for this target. */
516 if (TREE_OPTIMIZATION_BASE_OPTABS (optnode
) == this_target_optabs
)
519 /* Forget any previous information and set up for the current target. */
520 TREE_OPTIMIZATION_BASE_OPTABS (optnode
) = this_target_optabs
;
521 struct target_optabs
*tmp_optabs
= (struct target_optabs
*)
522 TREE_OPTIMIZATION_OPTABS (optnode
);
524 memset (tmp_optabs
, 0, sizeof (struct target_optabs
));
526 tmp_optabs
= ggc_cleared_alloc
<target_optabs
> ();
528 /* Generate a new set of optabs into tmp_optabs. */
529 init_all_optabs (tmp_optabs
);
531 /* If the optabs changed, record it. */
532 if (memcmp (tmp_optabs
, this_target_optabs
, sizeof (struct target_optabs
)))
533 TREE_OPTIMIZATION_OPTABS (optnode
) = tmp_optabs
;
536 TREE_OPTIMIZATION_OPTABS (optnode
) = NULL
;
537 ggc_free (tmp_optabs
);
541 /* Return TRUE if the target has support for vector right shift of an
542 operand of type TYPE. If OT_TYPE is OPTAB_DEFAULT, check for existence
543 of a shift by either a scalar or a vector. Otherwise, check only
544 for a shift that matches OT_TYPE. */
547 target_supports_op_p (tree type
, enum tree_code code
,
548 enum optab_subtype ot_subtype
)
550 optab ot
= optab_for_tree_code (code
, type
, ot_subtype
);
551 return (ot
!= unknown_optab
552 && optab_handler (ot
, TYPE_MODE (type
)) != CODE_FOR_nothing
);