]>
git.proxmox.com Git - mirror_qemu.git/blob - tcg/tcg-op-vec.c
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"
21 #include "qemu-common.h"
27 /* Reduce the number of ifdefs below. This assumes that all uses of
28 TCGV_HIGH and TCGV_LOW are properly protected by a conditional that
29 the compiler can eliminate. */
30 #if TCG_TARGET_REG_BITS == 64
31 extern TCGv_i32
TCGV_LOW_link_error(TCGv_i64
);
32 extern TCGv_i32
TCGV_HIGH_link_error(TCGv_i64
);
33 #define TCGV_LOW TCGV_LOW_link_error
34 #define TCGV_HIGH TCGV_HIGH_link_error
38 * Vector optional opcode tracking.
39 * Except for the basic logical operations (and, or, xor), and
40 * data movement (mov, ld, st, dupi), many vector opcodes are
41 * optional and may not be supported on the host. Thank Intel
42 * for the irregularity in their instruction set.
44 * The gvec expanders allow custom vector operations to be composed,
45 * generally via the .fniv callback in the GVecGen* structures. At
46 * the same time, in deciding whether to use this hook we need to
47 * know if the host supports the required operations. This is
48 * presented as an array of opcodes, terminated by 0. Each opcode
49 * is assumed to be expanded with the given VECE.
51 * For debugging, we want to validate this array. Therefore, when
52 * tcg_ctx->vec_opt_opc is non-NULL, the tcg_gen_*_vec expanders
53 * will validate that their opcode is present in the list.
55 #ifdef CONFIG_DEBUG_TCG
56 void tcg_assert_listed_vecop(TCGOpcode op
)
58 const TCGOpcode
*p
= tcg_ctx
->vecop_list
;
65 g_assert_not_reached();
70 bool tcg_can_emit_vecop_list(const TCGOpcode
*list
,
71 TCGType type
, unsigned vece
)
77 for (; *list
; ++list
) {
78 TCGOpcode opc
= *list
;
80 #ifdef CONFIG_DEBUG_TCG
82 case INDEX_op_and_vec
:
84 case INDEX_op_xor_vec
:
85 case INDEX_op_mov_vec
:
86 case INDEX_op_dup_vec
:
87 case INDEX_op_dupi_vec
:
88 case INDEX_op_dup2_vec
:
91 /* These opcodes are mandatory and should not be listed. */
92 g_assert_not_reached();
98 if (tcg_can_emit_vec_op(opc
, type
, vece
)) {
103 * The opcode list is created by front ends based on what they
104 * actually invoke. We must mirror the logic in the routines
105 * below for generic expansions using other opcodes.
108 case INDEX_op_neg_vec
:
109 if (tcg_can_emit_vec_op(INDEX_op_sub_vec
, type
, vece
)) {
121 void vec_gen_2(TCGOpcode opc
, TCGType type
, unsigned vece
, TCGArg r
, TCGArg a
)
123 TCGOp
*op
= tcg_emit_op(opc
);
124 TCGOP_VECL(op
) = type
- TCG_TYPE_V64
;
125 TCGOP_VECE(op
) = vece
;
130 void vec_gen_3(TCGOpcode opc
, TCGType type
, unsigned vece
,
131 TCGArg r
, TCGArg a
, TCGArg b
)
133 TCGOp
*op
= tcg_emit_op(opc
);
134 TCGOP_VECL(op
) = type
- TCG_TYPE_V64
;
135 TCGOP_VECE(op
) = vece
;
141 void vec_gen_4(TCGOpcode opc
, TCGType type
, unsigned vece
,
142 TCGArg r
, TCGArg a
, TCGArg b
, TCGArg c
)
144 TCGOp
*op
= tcg_emit_op(opc
);
145 TCGOP_VECL(op
) = type
- TCG_TYPE_V64
;
146 TCGOP_VECE(op
) = vece
;
153 static void vec_gen_op2(TCGOpcode opc
, unsigned vece
, TCGv_vec r
, TCGv_vec a
)
155 TCGTemp
*rt
= tcgv_vec_temp(r
);
156 TCGTemp
*at
= tcgv_vec_temp(a
);
157 TCGType type
= rt
->base_type
;
159 /* Must enough inputs for the output. */
160 tcg_debug_assert(at
->base_type
>= type
);
161 vec_gen_2(opc
, type
, vece
, temp_arg(rt
), temp_arg(at
));
164 static void vec_gen_op3(TCGOpcode opc
, unsigned vece
,
165 TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
167 TCGTemp
*rt
= tcgv_vec_temp(r
);
168 TCGTemp
*at
= tcgv_vec_temp(a
);
169 TCGTemp
*bt
= tcgv_vec_temp(b
);
170 TCGType type
= rt
->base_type
;
172 /* Must enough inputs for the output. */
173 tcg_debug_assert(at
->base_type
>= type
);
174 tcg_debug_assert(bt
->base_type
>= type
);
175 vec_gen_3(opc
, type
, vece
, temp_arg(rt
), temp_arg(at
), temp_arg(bt
));
178 void tcg_gen_mov_vec(TCGv_vec r
, TCGv_vec a
)
181 vec_gen_op2(INDEX_op_mov_vec
, 0, r
, a
);
185 #define MO_REG (TCG_TARGET_REG_BITS == 64 ? MO_64 : MO_32)
187 static void do_dupi_vec(TCGv_vec r
, unsigned vece
, TCGArg a
)
189 TCGTemp
*rt
= tcgv_vec_temp(r
);
190 vec_gen_2(INDEX_op_dupi_vec
, rt
->base_type
, vece
, temp_arg(rt
), a
);
193 TCGv_vec
tcg_const_zeros_vec(TCGType type
)
195 TCGv_vec ret
= tcg_temp_new_vec(type
);
196 do_dupi_vec(ret
, MO_REG
, 0);
200 TCGv_vec
tcg_const_ones_vec(TCGType type
)
202 TCGv_vec ret
= tcg_temp_new_vec(type
);
203 do_dupi_vec(ret
, MO_REG
, -1);
207 TCGv_vec
tcg_const_zeros_vec_matching(TCGv_vec m
)
209 TCGTemp
*t
= tcgv_vec_temp(m
);
210 return tcg_const_zeros_vec(t
->base_type
);
213 TCGv_vec
tcg_const_ones_vec_matching(TCGv_vec m
)
215 TCGTemp
*t
= tcgv_vec_temp(m
);
216 return tcg_const_ones_vec(t
->base_type
);
219 void tcg_gen_dup64i_vec(TCGv_vec r
, uint64_t a
)
221 if (TCG_TARGET_REG_BITS
== 32 && a
== deposit64(a
, 32, 32, a
)) {
222 do_dupi_vec(r
, MO_32
, a
);
223 } else if (TCG_TARGET_REG_BITS
== 64 || a
== (uint64_t)(int32_t)a
) {
224 do_dupi_vec(r
, MO_64
, a
);
226 TCGv_i64 c
= tcg_const_i64(a
);
227 tcg_gen_dup_i64_vec(MO_64
, r
, c
);
228 tcg_temp_free_i64(c
);
232 void tcg_gen_dup32i_vec(TCGv_vec r
, uint32_t a
)
234 do_dupi_vec(r
, MO_REG
, dup_const(MO_32
, a
));
237 void tcg_gen_dup16i_vec(TCGv_vec r
, uint32_t a
)
239 do_dupi_vec(r
, MO_REG
, dup_const(MO_16
, a
));
242 void tcg_gen_dup8i_vec(TCGv_vec r
, uint32_t a
)
244 do_dupi_vec(r
, MO_REG
, dup_const(MO_8
, a
));
247 void tcg_gen_dupi_vec(unsigned vece
, TCGv_vec r
, uint64_t a
)
249 do_dupi_vec(r
, MO_REG
, dup_const(vece
, a
));
252 void tcg_gen_dup_i64_vec(unsigned vece
, TCGv_vec r
, TCGv_i64 a
)
254 TCGArg ri
= tcgv_vec_arg(r
);
255 TCGTemp
*rt
= arg_temp(ri
);
256 TCGType type
= rt
->base_type
;
258 if (TCG_TARGET_REG_BITS
== 64) {
259 TCGArg ai
= tcgv_i64_arg(a
);
260 vec_gen_2(INDEX_op_dup_vec
, type
, vece
, ri
, ai
);
261 } else if (vece
== MO_64
) {
262 TCGArg al
= tcgv_i32_arg(TCGV_LOW(a
));
263 TCGArg ah
= tcgv_i32_arg(TCGV_HIGH(a
));
264 vec_gen_3(INDEX_op_dup2_vec
, type
, MO_64
, ri
, al
, ah
);
266 TCGArg ai
= tcgv_i32_arg(TCGV_LOW(a
));
267 vec_gen_2(INDEX_op_dup_vec
, type
, vece
, ri
, ai
);
271 void tcg_gen_dup_i32_vec(unsigned vece
, TCGv_vec r
, TCGv_i32 a
)
273 TCGArg ri
= tcgv_vec_arg(r
);
274 TCGArg ai
= tcgv_i32_arg(a
);
275 TCGTemp
*rt
= arg_temp(ri
);
276 TCGType type
= rt
->base_type
;
278 vec_gen_2(INDEX_op_dup_vec
, type
, vece
, ri
, ai
);
281 static void vec_gen_ldst(TCGOpcode opc
, TCGv_vec r
, TCGv_ptr b
, TCGArg o
)
283 TCGArg ri
= tcgv_vec_arg(r
);
284 TCGArg bi
= tcgv_ptr_arg(b
);
285 TCGTemp
*rt
= arg_temp(ri
);
286 TCGType type
= rt
->base_type
;
288 vec_gen_3(opc
, type
, 0, ri
, bi
, o
);
291 void tcg_gen_ld_vec(TCGv_vec r
, TCGv_ptr b
, TCGArg o
)
293 vec_gen_ldst(INDEX_op_ld_vec
, r
, b
, o
);
296 void tcg_gen_st_vec(TCGv_vec r
, TCGv_ptr b
, TCGArg o
)
298 vec_gen_ldst(INDEX_op_st_vec
, r
, b
, o
);
301 void tcg_gen_stl_vec(TCGv_vec r
, TCGv_ptr b
, TCGArg o
, TCGType low_type
)
303 TCGArg ri
= tcgv_vec_arg(r
);
304 TCGArg bi
= tcgv_ptr_arg(b
);
305 TCGTemp
*rt
= arg_temp(ri
);
306 TCGType type
= rt
->base_type
;
308 tcg_debug_assert(low_type
>= TCG_TYPE_V64
);
309 tcg_debug_assert(low_type
<= type
);
310 vec_gen_3(INDEX_op_st_vec
, low_type
, 0, ri
, bi
, o
);
313 void tcg_gen_and_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
315 vec_gen_op3(INDEX_op_and_vec
, 0, r
, a
, b
);
318 void tcg_gen_or_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
320 vec_gen_op3(INDEX_op_or_vec
, 0, r
, a
, b
);
323 void tcg_gen_xor_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
325 vec_gen_op3(INDEX_op_xor_vec
, 0, r
, a
, b
);
328 void tcg_gen_andc_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
330 if (TCG_TARGET_HAS_andc_vec
) {
331 vec_gen_op3(INDEX_op_andc_vec
, 0, r
, a
, b
);
333 TCGv_vec t
= tcg_temp_new_vec_matching(r
);
334 tcg_gen_not_vec(0, t
, b
);
335 tcg_gen_and_vec(0, r
, a
, t
);
336 tcg_temp_free_vec(t
);
340 void tcg_gen_orc_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
342 if (TCG_TARGET_HAS_orc_vec
) {
343 vec_gen_op3(INDEX_op_orc_vec
, 0, r
, a
, b
);
345 TCGv_vec t
= tcg_temp_new_vec_matching(r
);
346 tcg_gen_not_vec(0, t
, b
);
347 tcg_gen_or_vec(0, r
, a
, t
);
348 tcg_temp_free_vec(t
);
352 void tcg_gen_nand_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
354 /* TODO: Add TCG_TARGET_HAS_nand_vec when adding a backend supports it. */
355 tcg_gen_and_vec(0, r
, a
, b
);
356 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 /* TODO: Add TCG_TARGET_HAS_nor_vec when adding a backend supports it. */
362 tcg_gen_or_vec(0, r
, a
, b
);
363 tcg_gen_not_vec(0, r
, r
);
366 void tcg_gen_eqv_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
368 /* TODO: Add TCG_TARGET_HAS_eqv_vec when adding a backend supports it. */
369 tcg_gen_xor_vec(0, r
, a
, b
);
370 tcg_gen_not_vec(0, r
, r
);
373 static bool do_op2(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGOpcode opc
)
375 TCGTemp
*rt
= tcgv_vec_temp(r
);
376 TCGTemp
*at
= tcgv_vec_temp(a
);
377 TCGArg ri
= temp_arg(rt
);
378 TCGArg ai
= temp_arg(at
);
379 TCGType type
= rt
->base_type
;
382 tcg_debug_assert(at
->base_type
>= type
);
383 tcg_assert_listed_vecop(opc
);
384 can
= tcg_can_emit_vec_op(opc
, type
, vece
);
386 vec_gen_2(opc
, type
, vece
, ri
, ai
);
387 } else if (can
< 0) {
388 const TCGOpcode
*hold_list
= tcg_swap_vecop_list(NULL
);
389 tcg_expand_vec_op(opc
, type
, vece
, ri
, ai
);
390 tcg_swap_vecop_list(hold_list
);
397 void tcg_gen_not_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
)
399 if (!TCG_TARGET_HAS_not_vec
|| !do_op2(vece
, r
, a
, INDEX_op_not_vec
)) {
400 TCGv_vec t
= tcg_const_ones_vec_matching(r
);
401 tcg_gen_xor_vec(0, r
, a
, t
);
402 tcg_temp_free_vec(t
);
406 void tcg_gen_neg_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
)
408 const TCGOpcode
*hold_list
;
410 tcg_assert_listed_vecop(INDEX_op_neg_vec
);
411 hold_list
= tcg_swap_vecop_list(NULL
);
413 if (!TCG_TARGET_HAS_neg_vec
|| !do_op2(vece
, r
, a
, INDEX_op_neg_vec
)) {
414 TCGv_vec t
= tcg_const_zeros_vec_matching(r
);
415 tcg_gen_sub_vec(vece
, r
, t
, a
);
416 tcg_temp_free_vec(t
);
418 tcg_swap_vecop_list(hold_list
);
421 static void do_shifti(TCGOpcode opc
, unsigned vece
,
422 TCGv_vec r
, TCGv_vec a
, int64_t i
)
424 TCGTemp
*rt
= tcgv_vec_temp(r
);
425 TCGTemp
*at
= tcgv_vec_temp(a
);
426 TCGArg ri
= temp_arg(rt
);
427 TCGArg ai
= temp_arg(at
);
428 TCGType type
= rt
->base_type
;
431 tcg_debug_assert(at
->base_type
== type
);
432 tcg_debug_assert(i
>= 0 && i
< (8 << vece
));
433 tcg_assert_listed_vecop(opc
);
436 tcg_gen_mov_vec(r
, a
);
440 can
= tcg_can_emit_vec_op(opc
, type
, vece
);
442 vec_gen_3(opc
, type
, vece
, ri
, ai
, i
);
444 /* We leave the choice of expansion via scalar or vector shift
445 to the target. Often, but not always, dupi can feed a vector
446 shift easier than a scalar. */
447 const TCGOpcode
*hold_list
= tcg_swap_vecop_list(NULL
);
448 tcg_debug_assert(can
< 0);
449 tcg_expand_vec_op(opc
, type
, vece
, ri
, ai
, i
);
450 tcg_swap_vecop_list(hold_list
);
454 void tcg_gen_shli_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, int64_t i
)
456 do_shifti(INDEX_op_shli_vec
, vece
, r
, a
, i
);
459 void tcg_gen_shri_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, int64_t i
)
461 do_shifti(INDEX_op_shri_vec
, vece
, r
, a
, i
);
464 void tcg_gen_sari_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, int64_t i
)
466 do_shifti(INDEX_op_sari_vec
, vece
, r
, a
, i
);
469 void tcg_gen_cmp_vec(TCGCond cond
, unsigned vece
,
470 TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
472 TCGTemp
*rt
= tcgv_vec_temp(r
);
473 TCGTemp
*at
= tcgv_vec_temp(a
);
474 TCGTemp
*bt
= tcgv_vec_temp(b
);
475 TCGArg ri
= temp_arg(rt
);
476 TCGArg ai
= temp_arg(at
);
477 TCGArg bi
= temp_arg(bt
);
478 TCGType type
= rt
->base_type
;
481 tcg_debug_assert(at
->base_type
>= type
);
482 tcg_debug_assert(bt
->base_type
>= type
);
483 tcg_assert_listed_vecop(INDEX_op_cmp_vec
);
484 can
= tcg_can_emit_vec_op(INDEX_op_cmp_vec
, type
, vece
);
486 vec_gen_4(INDEX_op_cmp_vec
, type
, vece
, ri
, ai
, bi
, cond
);
488 const TCGOpcode
*hold_list
= tcg_swap_vecop_list(NULL
);
489 tcg_debug_assert(can
< 0);
490 tcg_expand_vec_op(INDEX_op_cmp_vec
, type
, vece
, ri
, ai
, bi
, cond
);
491 tcg_swap_vecop_list(hold_list
);
495 static void do_op3(unsigned vece
, TCGv_vec r
, TCGv_vec a
,
496 TCGv_vec b
, TCGOpcode opc
)
498 TCGTemp
*rt
= tcgv_vec_temp(r
);
499 TCGTemp
*at
= tcgv_vec_temp(a
);
500 TCGTemp
*bt
= tcgv_vec_temp(b
);
501 TCGArg ri
= temp_arg(rt
);
502 TCGArg ai
= temp_arg(at
);
503 TCGArg bi
= temp_arg(bt
);
504 TCGType type
= rt
->base_type
;
507 tcg_debug_assert(at
->base_type
>= type
);
508 tcg_debug_assert(bt
->base_type
>= type
);
509 tcg_assert_listed_vecop(opc
);
510 can
= tcg_can_emit_vec_op(opc
, type
, vece
);
512 vec_gen_3(opc
, type
, vece
, ri
, ai
, bi
);
514 const TCGOpcode
*hold_list
= tcg_swap_vecop_list(NULL
);
515 tcg_debug_assert(can
< 0);
516 tcg_expand_vec_op(opc
, type
, vece
, ri
, ai
, bi
);
517 tcg_swap_vecop_list(hold_list
);
521 void tcg_gen_add_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
523 do_op3(vece
, r
, a
, b
, INDEX_op_add_vec
);
526 void tcg_gen_sub_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
528 do_op3(vece
, r
, a
, b
, INDEX_op_sub_vec
);
531 void tcg_gen_mul_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
533 do_op3(vece
, r
, a
, b
, INDEX_op_mul_vec
);
536 void tcg_gen_ssadd_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
538 do_op3(vece
, r
, a
, b
, INDEX_op_ssadd_vec
);
541 void tcg_gen_usadd_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
543 do_op3(vece
, r
, a
, b
, INDEX_op_usadd_vec
);
546 void tcg_gen_sssub_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
548 do_op3(vece
, r
, a
, b
, INDEX_op_sssub_vec
);
551 void tcg_gen_ussub_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
553 do_op3(vece
, r
, a
, b
, INDEX_op_ussub_vec
);
556 void tcg_gen_smin_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
558 do_op3(vece
, r
, a
, b
, INDEX_op_smin_vec
);
561 void tcg_gen_umin_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
563 do_op3(vece
, r
, a
, b
, INDEX_op_umin_vec
);
566 void tcg_gen_smax_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
568 do_op3(vece
, r
, a
, b
, INDEX_op_smax_vec
);
571 void tcg_gen_umax_vec(unsigned vece
, TCGv_vec r
, TCGv_vec a
, TCGv_vec b
)
573 do_op3(vece
, r
, a
, b
, INDEX_op_umax_vec
);