2 * Tiny Code Generator for QEMU
4 * Copyright (c) 2018 Linaro, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
22 #include "tcg/tcg-temp-internal.h"
23 #include "tcg/tcg-op.h"
24 #include "tcg/tcg-mo.h"
25 #include "tcg-internal.h"
28 /* Reduce the number of ifdefs below. This assumes that all uses of
29 TCGV_HIGH and TCGV_LOW are properly protected by a conditional that
30 the compiler can eliminate. */
31 #if TCG_TARGET_REG_BITS == 64
32 extern TCGv_i32
TCGV_LOW_link_error(TCGv_i64
);
33 extern TCGv_i32
TCGV_HIGH_link_error(TCGv_i64
);
34 #define TCGV_LOW TCGV_LOW_link_error
35 #define TCGV_HIGH TCGV_HIGH_link_error
39 * Vector optional opcode tracking.
40 * Except for the basic logical operations (and, or, xor), and
41 * data movement (mov, ld, st, dupi), many vector opcodes are
42 * optional and may not be supported on the host. Thank Intel
43 * for the irregularity in their instruction set.
45 * The gvec expanders allow custom vector operations to be composed,
46 * generally via the .fniv callback in the GVecGen* structures. At
47 * the same time, in deciding whether to use this hook we need to
48 * know if the host supports the required operations. This is
49 * presented as an array of opcodes, terminated by 0. Each opcode
50 * is assumed to be expanded with the given VECE.
52 * For debugging, we want to validate this array. Therefore, when
53 * tcg_ctx->vec_opt_opc is non-NULL, the tcg_gen_*_vec expanders
54 * will validate that their opcode is present in the list.
56 #ifdef CONFIG_DEBUG_TCG
57 void tcg_assert_listed_vecop(TCGOpcode op
)
59 const TCGOpcode
*p
= tcg_ctx
->vecop_list
;
66 g_assert_not_reached();
71 bool tcg_can_emit_vecop_list(const TCGOpcode
*list
,
72 TCGType type
, unsigned vece
)
78 for (; *list
; ++list
) {
79 TCGOpcode opc
= *list
;
81 #ifdef CONFIG_DEBUG_TCG
83 case INDEX_op_and_vec
:
85 case INDEX_op_xor_vec
:
86 case INDEX_op_mov_vec
:
87 case INDEX_op_dup_vec
:
88 case INDEX_op_dup2_vec
:
91 case INDEX_op_bitsel_vec
:
92 /* These opcodes are mandatory and should not be listed. */
93 g_assert_not_reached();
94 case INDEX_op_not_vec
:
95 /* These opcodes have generic expansions using the above. */
96 g_assert_not_reached();
102 if (tcg_can_emit_vec_op(opc
, type
, vece
)) {
107 * The opcode list is created by front ends based on what they
108 * actually invoke. We must mirror the logic in the routines
109 * below for generic expansions using other opcodes.
112 case INDEX_op_neg_vec
:
113 if (tcg_can_emit_vec_op(INDEX_op_sub_vec
, type
, vece
)) {
117 case INDEX_op_abs_vec
:
118 if (tcg_can_emit_vec_op(INDEX_op_sub_vec
, type
, vece
)
119 && (tcg_can_emit_vec_op(INDEX_op_smax_vec
, type
, vece
) > 0
120 || tcg_can_emit_vec_op(INDEX_op_sari_vec
, type
, vece
) > 0
121 || tcg_can_emit_vec_op(INDEX_op_cmp_vec
, type
, vece
))) {
125 case INDEX_op_usadd_vec
:
126 if (tcg_can_emit_vec_op(INDEX_op_umin_vec
, type
, vece
) ||
127 tcg_can_emit_vec_op(INDEX_op_cmp_vec
, type
, vece
)) {
131 case INDEX_op_ussub_vec
:
132 if (tcg_can_emit_vec_op(INDEX_op_umax_vec
, type
, vece
) ||
133 tcg_can_emit_vec_op(INDEX_op_cmp_vec
, type
, vece
)) {
137 case INDEX_op_cmpsel_vec
:
138 case INDEX_op_smin_vec
:
139 case INDEX_op_smax_vec
:
140 case INDEX_op_umin_vec
:
141 case INDEX_op_umax_vec
:
142 if (tcg_can_emit_vec_op(INDEX_op_cmp_vec
, type
, vece
)) {
154 void vec_gen_2(TCGOpcode opc
, TCGType type
, unsigned vece
, TCGArg r
, TCGArg a
)
156 TCGOp
*op
= tcg_emit_op(opc
, 2);
157 TCGOP_VECL(op
) = type
- TCG_TYPE_V64
;
158 TCGOP_VECE(op
) = vece
;
163 void vec_gen_3(TCGOpcode opc
, TCGType type
, unsigned vece
,
164 TCGArg r
, TCGArg a
, TCGArg b
)
166 TCGOp
*op
= tcg_emit_op(opc
, 3);
167 TCGOP_VECL(op
) = type
- TCG_TYPE_V64
;
168 TCGOP_VECE(op
) = vece
;
174 void vec_gen_4(TCGOpcode opc
, TCGType type
, unsigned vece
,
175 TCGArg r
, TCGArg a
, TCGArg b
, TCGArg c
)
177 TCGOp
*op
= tcg_emit_op(opc
, 4);
178 TCGOP_VECL(op
) = type
- TCG_TYPE_V64
;
179 TCGOP_VECE(op
) = vece
;
186 static void vec_gen_6(TCGOpcode opc
, TCGType type
, unsigned vece
, TCGArg r
,
187 TCGArg a
, TCGArg b
, TCGArg c
, TCGArg d
, TCGArg e
)
189 TCGOp
*op
= tcg_emit_op(opc
, 6);
190 TCGOP_VECL(op
) = type
- TCG_TYPE_V64
;
191 TCGOP_VECE(op
) = vece
;
200 static void vec_gen_op2(TCGOpcode opc
, unsigned vece
, TCGv_vec r
, TCGv_vec a
)
202 TCGTemp
*rt
= tcgv_vec_temp(r
);
203 TCGTemp
*at
= tcgv_vec_temp(a
);
204 TCGType type
= rt
->base_type
;
206 /* Must enough inputs for the output. */
207 tcg_debug_assert(at
->base_type
>= type
);
208 vec_gen_2(opc
, type
, vece
, temp_arg(rt
), temp_arg(at
));
211 static void vec_gen_op3(TCGOpcode opc
, unsigned vece
,
212 TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
214 TCGTemp
*rt
= tcgv_vec_temp(r
);
215 TCGTemp
*at
= tcgv_vec_temp(a
);
216 TCGTemp
*bt
= tcgv_vec_temp(b
);
217 TCGType type
= rt
->base_type
;
219 /* Must enough inputs for the output. */
220 tcg_debug_assert(at
->base_type
>= type
);
221 tcg_debug_assert(bt
->base_type
>= type
);
222 vec_gen_3(opc
, type
, vece
, temp_arg(rt
), temp_arg(at
), temp_arg(bt
));
225 void tcg_gen_mov_vec(TCGv_vec r
, TCGv_vec a
)
228 vec_gen_op2(INDEX_op_mov_vec
, 0, r
, a
);
232 void tcg_gen_dupi_vec(unsigned vece
, TCGv_vec r
, uint64_t a
)
234 TCGTemp
*rt
= tcgv_vec_temp(r
);
235 tcg_gen_mov_vec(r
, tcg_constant_vec(rt
->base_type
, vece
, a
));
238 void tcg_gen_dup_i64_vec(unsigned vece
, TCGv_vec r
, TCGv_i64 a
)
240 TCGArg ri
= tcgv_vec_arg(r
);
241 TCGTemp
*rt
= arg_temp(ri
);
242 TCGType type
= rt
->base_type
;
244 if (TCG_TARGET_REG_BITS
== 64) {
245 TCGArg ai
= tcgv_i64_arg(a
);
246 vec_gen_2(INDEX_op_dup_vec
, type
, vece
, ri
, ai
);
247 } else if (vece
== MO_64
) {
248 TCGArg al
= tcgv_i32_arg(TCGV_LOW(a
));
249 TCGArg ah
= tcgv_i32_arg(TCGV_HIGH(a
));
250 vec_gen_3(INDEX_op_dup2_vec
, type
, MO_64
, ri
, al
, ah
);
252 TCGArg ai
= tcgv_i32_arg(TCGV_LOW(a
));
253 vec_gen_2(INDEX_op_dup_vec
, type
, vece
, ri
, ai
);
257 void tcg_gen_dup_i32_vec(unsigned vece
, TCGv_vec r
, TCGv_i32 a
)
259 TCGArg ri
= tcgv_vec_arg(r
);
260 TCGArg ai
= tcgv_i32_arg(a
);
261 TCGTemp
*rt
= arg_temp(ri
);
262 TCGType type
= rt
->base_type
;
264 vec_gen_2(INDEX_op_dup_vec
, type
, vece
, ri
, ai
);
267 void tcg_gen_dup_mem_vec(unsigned vece
, TCGv_vec r
, TCGv_ptr b
,
270 TCGArg ri
= tcgv_vec_arg(r
);
271 TCGArg bi
= tcgv_ptr_arg(b
);
272 TCGTemp
*rt
= arg_temp(ri
);
273 TCGType type
= rt
->base_type
;
275 vec_gen_3(INDEX_op_dupm_vec
, type
, vece
, ri
, bi
, ofs
);
278 static void vec_gen_ldst(TCGOpcode opc
, TCGv_vec r
, TCGv_ptr b
, TCGArg o
)
280 TCGArg ri
= tcgv_vec_arg(r
);
281 TCGArg bi
= tcgv_ptr_arg(b
);
282 TCGTemp
*rt
= arg_temp(ri
);
283 TCGType type
= rt
->base_type
;
285 vec_gen_3(opc
, type
, 0, ri
, bi
, o
);
288 void tcg_gen_ld_vec(TCGv_vec r
, TCGv_ptr b
, TCGArg o
)
290 vec_gen_ldst(INDEX_op_ld_vec
, r
, b
, o
);
293 void tcg_gen_st_vec(TCGv_vec r
, TCGv_ptr b
, TCGArg o
)
295 vec_gen_ldst(INDEX_op_st_vec
, r
, b
, o
);
298 void tcg_gen_stl_vec(TCGv_vec r
, TCGv_ptr b
, TCGArg o
, TCGType low_type
)
300 TCGArg ri
= tcgv_vec_arg(r
);
301 TCGArg bi
= tcgv_ptr_arg(b
);
302 TCGTemp
*rt
= arg_temp(ri
);
303 TCGType type
= rt
->base_type
;
305 tcg_debug_assert(low_type
>= TCG_TYPE_V64
);
306 tcg_debug_assert(low_type
<= type
);
307 vec_gen_3(INDEX_op_st_vec
, low_type
, 0, ri
, bi
, o
);
310 void tcg_gen_and_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
312 vec_gen_op3(INDEX_op_and_vec
, 0, r
, a
, b
);
315 void tcg_gen_or_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
317 vec_gen_op3(INDEX_op_or_vec
, 0, r
, a
, b
);
320 void tcg_gen_xor_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
322 vec_gen_op3(INDEX_op_xor_vec
, 0, r
, a
, b
);
325 void tcg_gen_andc_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
327 if (TCG_TARGET_HAS_andc_vec
) {
328 vec_gen_op3(INDEX_op_andc_vec
, 0, r
, a
, b
);
330 TCGv_vec t
= tcg_temp_new_vec_matching(r
);
331 tcg_gen_not_vec(0, t
, b
);
332 tcg_gen_and_vec(0, r
, a
, t
);
333 tcg_temp_free_vec(t
);
337 void tcg_gen_orc_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
339 if (TCG_TARGET_HAS_orc_vec
) {
340 vec_gen_op3(INDEX_op_orc_vec
, 0, r
, a
, b
);
342 TCGv_vec t
= tcg_temp_new_vec_matching(r
);
343 tcg_gen_not_vec(0, t
, b
);
344 tcg_gen_or_vec(0, r
, a
, t
);
345 tcg_temp_free_vec(t
);
349 void tcg_gen_nand_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
351 if (TCG_TARGET_HAS_nand_vec
) {
352 vec_gen_op3(INDEX_op_nand_vec
, 0, r
, a
, b
);
354 tcg_gen_and_vec(0, r
, a
, b
);
355 tcg_gen_not_vec(0, r
, r
);
359 void tcg_gen_nor_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
361 if (TCG_TARGET_HAS_nor_vec
) {
362 vec_gen_op3(INDEX_op_nor_vec
, 0, r
, a
, b
);
364 tcg_gen_or_vec(0, r
, a
, b
);
365 tcg_gen_not_vec(0, r
, r
);
369 void tcg_gen_eqv_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
371 if (TCG_TARGET_HAS_eqv_vec
) {
372 vec_gen_op3(INDEX_op_eqv_vec
, 0, r
, a
, b
);
374 tcg_gen_xor_vec(0, r
, a
, b
);
375 tcg_gen_not_vec(0, r
, r
);
379 static bool do_op2(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGOpcode opc
)
381 TCGTemp
*rt
= tcgv_vec_temp(r
);
382 TCGTemp
*at
= tcgv_vec_temp(a
);
383 TCGArg ri
= temp_arg(rt
);
384 TCGArg ai
= temp_arg(at
);
385 TCGType type
= rt
->base_type
;
388 tcg_debug_assert(at
->base_type
>= type
);
389 tcg_assert_listed_vecop(opc
);
390 can
= tcg_can_emit_vec_op(opc
, type
, vece
);
392 vec_gen_2(opc
, type
, vece
, ri
, ai
);
393 } else if (can
< 0) {
394 const TCGOpcode
*hold_list
= tcg_swap_vecop_list(NULL
);
395 tcg_expand_vec_op(opc
, type
, vece
, ri
, ai
);
396 tcg_swap_vecop_list(hold_list
);
403 void tcg_gen_not_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
)
405 const TCGOpcode
*hold_list
= tcg_swap_vecop_list(NULL
);
407 if (!TCG_TARGET_HAS_not_vec
|| !do_op2(vece
, r
, a
, INDEX_op_not_vec
)) {
408 tcg_gen_xor_vec(0, r
, a
, tcg_constant_vec_matching(r
, 0, -1));
410 tcg_swap_vecop_list(hold_list
);
413 void tcg_gen_neg_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
)
415 const TCGOpcode
*hold_list
;
417 tcg_assert_listed_vecop(INDEX_op_neg_vec
);
418 hold_list
= tcg_swap_vecop_list(NULL
);
420 if (!TCG_TARGET_HAS_neg_vec
|| !do_op2(vece
, r
, a
, INDEX_op_neg_vec
)) {
421 tcg_gen_sub_vec(vece
, r
, tcg_constant_vec_matching(r
, vece
, 0), a
);
423 tcg_swap_vecop_list(hold_list
);
426 void tcg_gen_abs_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
)
428 const TCGOpcode
*hold_list
;
430 tcg_assert_listed_vecop(INDEX_op_abs_vec
);
431 hold_list
= tcg_swap_vecop_list(NULL
);
433 if (!do_op2(vece
, r
, a
, INDEX_op_abs_vec
)) {
434 TCGType type
= tcgv_vec_temp(r
)->base_type
;
435 TCGv_vec t
= tcg_temp_new_vec(type
);
437 tcg_debug_assert(tcg_can_emit_vec_op(INDEX_op_sub_vec
, type
, vece
));
438 if (tcg_can_emit_vec_op(INDEX_op_smax_vec
, type
, vece
) > 0) {
439 tcg_gen_neg_vec(vece
, t
, a
);
440 tcg_gen_smax_vec(vece
, r
, a
, t
);
442 if (tcg_can_emit_vec_op(INDEX_op_sari_vec
, type
, vece
) > 0) {
443 tcg_gen_sari_vec(vece
, t
, a
, (8 << vece
) - 1);
445 tcg_gen_cmp_vec(TCG_COND_LT
, vece
, t
, a
,
446 tcg_constant_vec(type
, vece
, 0));
448 tcg_gen_xor_vec(vece
, r
, a
, t
);
449 tcg_gen_sub_vec(vece
, r
, r
, t
);
452 tcg_temp_free_vec(t
);
454 tcg_swap_vecop_list(hold_list
);
457 static void do_shifti(TCGOpcode opc
, unsigned vece
,
458 TCGv_vec r
, TCGv_vec a
, int64_t i
)
460 TCGTemp
*rt
= tcgv_vec_temp(r
);
461 TCGTemp
*at
= tcgv_vec_temp(a
);
462 TCGArg ri
= temp_arg(rt
);
463 TCGArg ai
= temp_arg(at
);
464 TCGType type
= rt
->base_type
;
467 tcg_debug_assert(at
->base_type
== type
);
468 tcg_debug_assert(i
>= 0 && i
< (8 << vece
));
469 tcg_assert_listed_vecop(opc
);
472 tcg_gen_mov_vec(r
, a
);
476 can
= tcg_can_emit_vec_op(opc
, type
, vece
);
478 vec_gen_3(opc
, type
, vece
, ri
, ai
, i
);
480 /* We leave the choice of expansion via scalar or vector shift
481 to the target. Often, but not always, dupi can feed a vector
482 shift easier than a scalar. */
483 const TCGOpcode
*hold_list
= tcg_swap_vecop_list(NULL
);
484 tcg_debug_assert(can
< 0);
485 tcg_expand_vec_op(opc
, type
, vece
, ri
, ai
, i
);
486 tcg_swap_vecop_list(hold_list
);
490 void tcg_gen_shli_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, int64_t i
)
492 do_shifti(INDEX_op_shli_vec
, vece
, r
, a
, i
);
495 void tcg_gen_shri_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, int64_t i
)
497 do_shifti(INDEX_op_shri_vec
, vece
, r
, a
, i
);
500 void tcg_gen_sari_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, int64_t i
)
502 do_shifti(INDEX_op_sari_vec
, vece
, r
, a
, i
);
505 void tcg_gen_rotli_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, int64_t i
)
507 do_shifti(INDEX_op_rotli_vec
, vece
, r
, a
, i
);
510 void tcg_gen_rotri_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, int64_t i
)
512 int bits
= 8 << vece
;
513 tcg_debug_assert(i
>= 0 && i
< bits
);
514 do_shifti(INDEX_op_rotli_vec
, vece
, r
, a
, -i
& (bits
- 1));
517 void tcg_gen_cmp_vec(TCGCond cond
, unsigned vece
,
518 TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
520 TCGTemp
*rt
= tcgv_vec_temp(r
);
521 TCGTemp
*at
= tcgv_vec_temp(a
);
522 TCGTemp
*bt
= tcgv_vec_temp(b
);
523 TCGArg ri
= temp_arg(rt
);
524 TCGArg ai
= temp_arg(at
);
525 TCGArg bi
= temp_arg(bt
);
526 TCGType type
= rt
->base_type
;
529 tcg_debug_assert(at
->base_type
>= type
);
530 tcg_debug_assert(bt
->base_type
>= type
);
531 tcg_assert_listed_vecop(INDEX_op_cmp_vec
);
532 can
= tcg_can_emit_vec_op(INDEX_op_cmp_vec
, type
, vece
);
534 vec_gen_4(INDEX_op_cmp_vec
, type
, vece
, ri
, ai
, bi
, cond
);
536 const TCGOpcode
*hold_list
= tcg_swap_vecop_list(NULL
);
537 tcg_debug_assert(can
< 0);
538 tcg_expand_vec_op(INDEX_op_cmp_vec
, type
, vece
, ri
, ai
, bi
, cond
);
539 tcg_swap_vecop_list(hold_list
);
543 static bool do_op3(unsigned vece
, TCGv_vec r
, TCGv_vec a
,
544 TCGv_vec b
, TCGOpcode opc
)
546 TCGTemp
*rt
= tcgv_vec_temp(r
);
547 TCGTemp
*at
= tcgv_vec_temp(a
);
548 TCGTemp
*bt
= tcgv_vec_temp(b
);
549 TCGArg ri
= temp_arg(rt
);
550 TCGArg ai
= temp_arg(at
);
551 TCGArg bi
= temp_arg(bt
);
552 TCGType type
= rt
->base_type
;
555 tcg_debug_assert(at
->base_type
>= type
);
556 tcg_debug_assert(bt
->base_type
>= type
);
557 tcg_assert_listed_vecop(opc
);
558 can
= tcg_can_emit_vec_op(opc
, type
, vece
);
560 vec_gen_3(opc
, type
, vece
, ri
, ai
, bi
);
561 } else if (can
< 0) {
562 const TCGOpcode
*hold_list
= tcg_swap_vecop_list(NULL
);
563 tcg_expand_vec_op(opc
, type
, vece
, ri
, ai
, bi
);
564 tcg_swap_vecop_list(hold_list
);
571 static void do_op3_nofail(unsigned vece
, TCGv_vec r
, TCGv_vec a
,
572 TCGv_vec b
, TCGOpcode opc
)
574 bool ok
= do_op3(vece
, r
, a
, b
, opc
);
575 tcg_debug_assert(ok
);
578 void tcg_gen_add_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
580 do_op3_nofail(vece
, r
, a
, b
, INDEX_op_add_vec
);
583 void tcg_gen_sub_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
585 do_op3_nofail(vece
, r
, a
, b
, INDEX_op_sub_vec
);
588 void tcg_gen_mul_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
590 do_op3_nofail(vece
, r
, a
, b
, INDEX_op_mul_vec
);
593 void tcg_gen_ssadd_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
595 do_op3_nofail(vece
, r
, a
, b
, INDEX_op_ssadd_vec
);
598 void tcg_gen_usadd_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
600 if (!do_op3(vece
, r
, a
, b
, INDEX_op_usadd_vec
)) {
601 const TCGOpcode
*hold_list
= tcg_swap_vecop_list(NULL
);
602 TCGv_vec t
= tcg_temp_new_vec_matching(r
);
604 /* usadd(a, b) = min(a, ~b) + b */
605 tcg_gen_not_vec(vece
, t
, b
);
606 tcg_gen_umin_vec(vece
, t
, t
, a
);
607 tcg_gen_add_vec(vece
, r
, t
, b
);
609 tcg_temp_free_vec(t
);
610 tcg_swap_vecop_list(hold_list
);
614 void tcg_gen_sssub_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
616 do_op3_nofail(vece
, r
, a
, b
, INDEX_op_sssub_vec
);
619 void tcg_gen_ussub_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
621 if (!do_op3(vece
, r
, a
, b
, INDEX_op_ussub_vec
)) {
622 const TCGOpcode
*hold_list
= tcg_swap_vecop_list(NULL
);
623 TCGv_vec t
= tcg_temp_new_vec_matching(r
);
625 /* ussub(a, b) = max(a, b) - b */
626 tcg_gen_umax_vec(vece
, t
, a
, b
);
627 tcg_gen_sub_vec(vece
, r
, t
, b
);
629 tcg_temp_free_vec(t
);
630 tcg_swap_vecop_list(hold_list
);
634 static void do_minmax(unsigned vece
, TCGv_vec r
, TCGv_vec a
,
635 TCGv_vec b
, TCGOpcode opc
, TCGCond cond
)
637 if (!do_op3(vece
, r
, a
, b
, opc
)) {
638 const TCGOpcode
*hold_list
= tcg_swap_vecop_list(NULL
);
639 tcg_gen_cmpsel_vec(cond
, vece
, r
, a
, b
, a
, b
);
640 tcg_swap_vecop_list(hold_list
);
644 void tcg_gen_smin_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
646 do_minmax(vece
, r
, a
, b
, INDEX_op_smin_vec
, TCG_COND_LT
);
649 void tcg_gen_umin_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
651 do_minmax(vece
, r
, a
, b
, INDEX_op_umin_vec
, TCG_COND_LTU
);
654 void tcg_gen_smax_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
656 do_minmax(vece
, r
, a
, b
, INDEX_op_smax_vec
, TCG_COND_GT
);
659 void tcg_gen_umax_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
661 do_minmax(vece
, r
, a
, b
, INDEX_op_umax_vec
, TCG_COND_GTU
);
664 void tcg_gen_shlv_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
666 do_op3_nofail(vece
, r
, a
, b
, INDEX_op_shlv_vec
);
669 void tcg_gen_shrv_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
671 do_op3_nofail(vece
, r
, a
, b
, INDEX_op_shrv_vec
);
674 void tcg_gen_sarv_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
676 do_op3_nofail(vece
, r
, a
, b
, INDEX_op_sarv_vec
);
679 void tcg_gen_rotlv_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
681 do_op3_nofail(vece
, r
, a
, b
, INDEX_op_rotlv_vec
);
684 void tcg_gen_rotrv_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
686 do_op3_nofail(vece
, r
, a
, b
, INDEX_op_rotrv_vec
);
689 static void do_shifts(unsigned vece
, TCGv_vec r
, TCGv_vec a
,
690 TCGv_i32 s
, TCGOpcode opc
)
692 TCGTemp
*rt
= tcgv_vec_temp(r
);
693 TCGTemp
*at
= tcgv_vec_temp(a
);
694 TCGTemp
*st
= tcgv_i32_temp(s
);
695 TCGArg ri
= temp_arg(rt
);
696 TCGArg ai
= temp_arg(at
);
697 TCGArg si
= temp_arg(st
);
698 TCGType type
= rt
->base_type
;
701 tcg_debug_assert(at
->base_type
>= type
);
702 tcg_assert_listed_vecop(opc
);
703 can
= tcg_can_emit_vec_op(opc
, type
, vece
);
705 vec_gen_3(opc
, type
, vece
, ri
, ai
, si
);
706 } else if (can
< 0) {
707 const TCGOpcode
*hold_list
= tcg_swap_vecop_list(NULL
);
708 tcg_expand_vec_op(opc
, type
, vece
, ri
, ai
, si
);
709 tcg_swap_vecop_list(hold_list
);
711 g_assert_not_reached();
715 void tcg_gen_shls_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_i32 b
)
717 do_shifts(vece
, r
, a
, b
, INDEX_op_shls_vec
);
720 void tcg_gen_shrs_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_i32 b
)
722 do_shifts(vece
, r
, a
, b
, INDEX_op_shrs_vec
);
725 void tcg_gen_sars_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_i32 b
)
727 do_shifts(vece
, r
, a
, b
, INDEX_op_sars_vec
);
730 void tcg_gen_rotls_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_i32 s
)
732 do_shifts(vece
, r
, a
, s
, INDEX_op_rotls_vec
);
735 void tcg_gen_bitsel_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
,
736 TCGv_vec b
, TCGv_vec c
)
738 TCGTemp
*rt
= tcgv_vec_temp(r
);
739 TCGTemp
*at
= tcgv_vec_temp(a
);
740 TCGTemp
*bt
= tcgv_vec_temp(b
);
741 TCGTemp
*ct
= tcgv_vec_temp(c
);
742 TCGType type
= rt
->base_type
;
744 tcg_debug_assert(at
->base_type
>= type
);
745 tcg_debug_assert(bt
->base_type
>= type
);
746 tcg_debug_assert(ct
->base_type
>= type
);
748 if (TCG_TARGET_HAS_bitsel_vec
) {
749 vec_gen_4(INDEX_op_bitsel_vec
, type
, MO_8
,
750 temp_arg(rt
), temp_arg(at
), temp_arg(bt
), temp_arg(ct
));
752 TCGv_vec t
= tcg_temp_new_vec(type
);
753 tcg_gen_and_vec(MO_8
, t
, a
, b
);
754 tcg_gen_andc_vec(MO_8
, r
, c
, a
);
755 tcg_gen_or_vec(MO_8
, r
, r
, t
);
756 tcg_temp_free_vec(t
);
760 void tcg_gen_cmpsel_vec(TCGCond cond
, unsigned vece
, TCGv_vec r
,
761 TCGv_vec a
, TCGv_vec b
, TCGv_vec c
, TCGv_vec d
)
763 TCGTemp
*rt
= tcgv_vec_temp(r
);
764 TCGTemp
*at
= tcgv_vec_temp(a
);
765 TCGTemp
*bt
= tcgv_vec_temp(b
);
766 TCGTemp
*ct
= tcgv_vec_temp(c
);
767 TCGTemp
*dt
= tcgv_vec_temp(d
);
768 TCGArg ri
= temp_arg(rt
);
769 TCGArg ai
= temp_arg(at
);
770 TCGArg bi
= temp_arg(bt
);
771 TCGArg ci
= temp_arg(ct
);
772 TCGArg di
= temp_arg(dt
);
773 TCGType type
= rt
->base_type
;
774 const TCGOpcode
*hold_list
;
777 tcg_debug_assert(at
->base_type
>= type
);
778 tcg_debug_assert(bt
->base_type
>= type
);
779 tcg_debug_assert(ct
->base_type
>= type
);
780 tcg_debug_assert(dt
->base_type
>= type
);
782 tcg_assert_listed_vecop(INDEX_op_cmpsel_vec
);
783 hold_list
= tcg_swap_vecop_list(NULL
);
784 can
= tcg_can_emit_vec_op(INDEX_op_cmpsel_vec
, type
, vece
);
787 vec_gen_6(INDEX_op_cmpsel_vec
, type
, vece
, ri
, ai
, bi
, ci
, di
, cond
);
788 } else if (can
< 0) {
789 tcg_expand_vec_op(INDEX_op_cmpsel_vec
, type
, vece
,
790 ri
, ai
, bi
, ci
, di
, cond
);
792 TCGv_vec t
= tcg_temp_new_vec(type
);
793 tcg_gen_cmp_vec(cond
, vece
, t
, a
, b
);
794 tcg_gen_bitsel_vec(vece
, r
, t
, c
, d
);
795 tcg_temp_free_vec(t
);
797 tcg_swap_vecop_list(hold_list
);