2 * Optimizations for Tiny Code Generator for QEMU
4 * Copyright (c) 2010 Samsung Electronics.
5 * Contributed by Kirill Batuzov <batuzovk@ispras.ru>
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "qemu/osdep.h"
27 #include "tcg/tcg-op.h"
28 #include "tcg-internal.h"
30 #define CASE_OP_32_64(x) \
31 glue(glue(case INDEX_op_, x), _i32): \
32 glue(glue(case INDEX_op_, x), _i64)
34 #define CASE_OP_32_64_VEC(x) \
35 glue(glue(case INDEX_op_, x), _i32): \
36 glue(glue(case INDEX_op_, x), _i64): \
37 glue(glue(case INDEX_op_, x), _vec)
39 typedef struct TempOptInfo
{
47 static inline TempOptInfo
*ts_info(TCGTemp
*ts
)
52 static inline TempOptInfo
*arg_info(TCGArg arg
)
54 return ts_info(arg_temp(arg
));
57 static inline bool ts_is_const(TCGTemp
*ts
)
59 return ts_info(ts
)->is_const
;
62 static inline bool arg_is_const(TCGArg arg
)
64 return ts_is_const(arg_temp(arg
));
67 static inline bool ts_is_copy(TCGTemp
*ts
)
69 return ts_info(ts
)->next_copy
!= ts
;
72 /* Reset TEMP's state, possibly removing the temp for the list of copies. */
73 static void reset_ts(TCGTemp
*ts
)
75 TempOptInfo
*ti
= ts_info(ts
);
76 TempOptInfo
*pi
= ts_info(ti
->prev_copy
);
77 TempOptInfo
*ni
= ts_info(ti
->next_copy
);
79 ni
->prev_copy
= ti
->prev_copy
;
80 pi
->next_copy
= ti
->next_copy
;
87 static void reset_temp(TCGArg arg
)
89 reset_ts(arg_temp(arg
));
92 /* Initialize and activate a temporary. */
93 static void init_ts_info(TCGTempSet
*temps_used
, TCGTemp
*ts
)
95 size_t idx
= temp_idx(ts
);
98 if (test_bit(idx
, temps_used
->l
)) {
101 set_bit(idx
, temps_used
->l
);
105 ti
= tcg_malloc(sizeof(TempOptInfo
));
111 if (ts
->kind
== TEMP_CONST
) {
115 if (TCG_TARGET_REG_BITS
> 32 && ts
->type
== TCG_TYPE_I32
) {
116 /* High bits of a 32-bit quantity are garbage. */
117 ti
->mask
|= ~0xffffffffull
;
120 ti
->is_const
= false;
125 static void init_arg_info(TCGTempSet
*temps_used
, TCGArg arg
)
127 init_ts_info(temps_used
, arg_temp(arg
));
130 static TCGTemp
*find_better_copy(TCGContext
*s
, TCGTemp
*ts
)
134 /* If this is already readonly, we can't do better. */
135 if (temp_readonly(ts
)) {
140 for (i
= ts_info(ts
)->next_copy
; i
!= ts
; i
= ts_info(i
)->next_copy
) {
141 if (temp_readonly(i
)) {
143 } else if (i
->kind
> ts
->kind
) {
144 if (i
->kind
== TEMP_GLOBAL
) {
146 } else if (i
->kind
== TEMP_LOCAL
) {
152 /* If we didn't find a better representation, return the same temp. */
153 return g
? g
: l
? l
: ts
;
156 static bool ts_are_copies(TCGTemp
*ts1
, TCGTemp
*ts2
)
164 if (!ts_is_copy(ts1
) || !ts_is_copy(ts2
)) {
168 for (i
= ts_info(ts1
)->next_copy
; i
!= ts1
; i
= ts_info(i
)->next_copy
) {
177 static bool args_are_copies(TCGArg arg1
, TCGArg arg2
)
179 return ts_are_copies(arg_temp(arg1
), arg_temp(arg2
));
182 static void tcg_opt_gen_mov(TCGContext
*s
, TCGOp
*op
, TCGArg dst
, TCGArg src
)
184 TCGTemp
*dst_ts
= arg_temp(dst
);
185 TCGTemp
*src_ts
= arg_temp(src
);
192 if (ts_are_copies(dst_ts
, src_ts
)) {
193 tcg_op_remove(s
, op
);
198 di
= ts_info(dst_ts
);
199 si
= ts_info(src_ts
);
200 def
= &tcg_op_defs
[op
->opc
];
201 if (def
->flags
& TCG_OPF_VECTOR
) {
202 new_op
= INDEX_op_mov_vec
;
203 } else if (def
->flags
& TCG_OPF_64BIT
) {
204 new_op
= INDEX_op_mov_i64
;
206 new_op
= INDEX_op_mov_i32
;
209 /* TCGOP_VECL and TCGOP_VECE remain unchanged. */
214 if (TCG_TARGET_REG_BITS
> 32 && new_op
== INDEX_op_mov_i32
) {
215 /* High bits of the destination are now garbage. */
216 mask
|= ~0xffffffffull
;
220 if (src_ts
->type
== dst_ts
->type
) {
221 TempOptInfo
*ni
= ts_info(si
->next_copy
);
223 di
->next_copy
= si
->next_copy
;
224 di
->prev_copy
= src_ts
;
225 ni
->prev_copy
= dst_ts
;
226 si
->next_copy
= dst_ts
;
227 di
->is_const
= si
->is_const
;
232 static void tcg_opt_gen_movi(TCGContext
*s
, TCGTempSet
*temps_used
,
233 TCGOp
*op
, TCGArg dst
, uint64_t val
)
235 const TCGOpDef
*def
= &tcg_op_defs
[op
->opc
];
239 if (def
->flags
& TCG_OPF_VECTOR
) {
240 type
= TCGOP_VECL(op
) + TCG_TYPE_V64
;
241 } else if (def
->flags
& TCG_OPF_64BIT
) {
247 /* Convert movi to mov with constant temp. */
248 tv
= tcg_constant_internal(type
, val
);
249 init_ts_info(temps_used
, tv
);
250 tcg_opt_gen_mov(s
, op
, dst
, temp_arg(tv
));
253 static uint64_t do_constant_folding_2(TCGOpcode op
, uint64_t x
, uint64_t y
)
276 case INDEX_op_shl_i32
:
277 return (uint32_t)x
<< (y
& 31);
279 case INDEX_op_shl_i64
:
280 return (uint64_t)x
<< (y
& 63);
282 case INDEX_op_shr_i32
:
283 return (uint32_t)x
>> (y
& 31);
285 case INDEX_op_shr_i64
:
286 return (uint64_t)x
>> (y
& 63);
288 case INDEX_op_sar_i32
:
289 return (int32_t)x
>> (y
& 31);
291 case INDEX_op_sar_i64
:
292 return (int64_t)x
>> (y
& 63);
294 case INDEX_op_rotr_i32
:
295 return ror32(x
, y
& 31);
297 case INDEX_op_rotr_i64
:
298 return ror64(x
, y
& 63);
300 case INDEX_op_rotl_i32
:
301 return rol32(x
, y
& 31);
303 case INDEX_op_rotl_i64
:
304 return rol64(x
, y
& 63);
327 case INDEX_op_clz_i32
:
328 return (uint32_t)x
? clz32(x
) : y
;
330 case INDEX_op_clz_i64
:
331 return x
? clz64(x
) : y
;
333 case INDEX_op_ctz_i32
:
334 return (uint32_t)x
? ctz32(x
) : y
;
336 case INDEX_op_ctz_i64
:
337 return x
? ctz64(x
) : y
;
339 case INDEX_op_ctpop_i32
:
342 case INDEX_op_ctpop_i64
:
345 CASE_OP_32_64(ext8s
):
348 CASE_OP_32_64(ext16s
):
351 CASE_OP_32_64(ext8u
):
354 CASE_OP_32_64(ext16u
):
357 CASE_OP_32_64(bswap16
):
360 CASE_OP_32_64(bswap32
):
363 case INDEX_op_bswap64_i64
:
366 case INDEX_op_ext_i32_i64
:
367 case INDEX_op_ext32s_i64
:
370 case INDEX_op_extu_i32_i64
:
371 case INDEX_op_extrl_i64_i32
:
372 case INDEX_op_ext32u_i64
:
375 case INDEX_op_extrh_i64_i32
:
376 return (uint64_t)x
>> 32;
378 case INDEX_op_muluh_i32
:
379 return ((uint64_t)(uint32_t)x
* (uint32_t)y
) >> 32;
380 case INDEX_op_mulsh_i32
:
381 return ((int64_t)(int32_t)x
* (int32_t)y
) >> 32;
383 case INDEX_op_muluh_i64
:
384 mulu64(&l64
, &h64
, x
, y
);
386 case INDEX_op_mulsh_i64
:
387 muls64(&l64
, &h64
, x
, y
);
390 case INDEX_op_div_i32
:
391 /* Avoid crashing on divide by zero, otherwise undefined. */
392 return (int32_t)x
/ ((int32_t)y
? : 1);
393 case INDEX_op_divu_i32
:
394 return (uint32_t)x
/ ((uint32_t)y
? : 1);
395 case INDEX_op_div_i64
:
396 return (int64_t)x
/ ((int64_t)y
? : 1);
397 case INDEX_op_divu_i64
:
398 return (uint64_t)x
/ ((uint64_t)y
? : 1);
400 case INDEX_op_rem_i32
:
401 return (int32_t)x
% ((int32_t)y
? : 1);
402 case INDEX_op_remu_i32
:
403 return (uint32_t)x
% ((uint32_t)y
? : 1);
404 case INDEX_op_rem_i64
:
405 return (int64_t)x
% ((int64_t)y
? : 1);
406 case INDEX_op_remu_i64
:
407 return (uint64_t)x
% ((uint64_t)y
? : 1);
411 "Unrecognized operation %d in do_constant_folding.\n", op
);
416 static uint64_t do_constant_folding(TCGOpcode op
, uint64_t x
, uint64_t y
)
418 const TCGOpDef
*def
= &tcg_op_defs
[op
];
419 uint64_t res
= do_constant_folding_2(op
, x
, y
);
420 if (!(def
->flags
& TCG_OPF_64BIT
)) {
426 static bool do_constant_folding_cond_32(uint32_t x
, uint32_t y
, TCGCond c
)
434 return (int32_t)x
< (int32_t)y
;
436 return (int32_t)x
>= (int32_t)y
;
438 return (int32_t)x
<= (int32_t)y
;
440 return (int32_t)x
> (int32_t)y
;
454 static bool do_constant_folding_cond_64(uint64_t x
, uint64_t y
, TCGCond c
)
462 return (int64_t)x
< (int64_t)y
;
464 return (int64_t)x
>= (int64_t)y
;
466 return (int64_t)x
<= (int64_t)y
;
468 return (int64_t)x
> (int64_t)y
;
482 static bool do_constant_folding_cond_eq(TCGCond c
)
502 /* Return 2 if the condition can't be simplified, and the result
503 of the condition (0 or 1) if it can */
504 static TCGArg
do_constant_folding_cond(TCGOpcode op
, TCGArg x
,
507 uint64_t xv
= arg_info(x
)->val
;
508 uint64_t yv
= arg_info(y
)->val
;
510 if (arg_is_const(x
) && arg_is_const(y
)) {
511 const TCGOpDef
*def
= &tcg_op_defs
[op
];
512 tcg_debug_assert(!(def
->flags
& TCG_OPF_VECTOR
));
513 if (def
->flags
& TCG_OPF_64BIT
) {
514 return do_constant_folding_cond_64(xv
, yv
, c
);
516 return do_constant_folding_cond_32(xv
, yv
, c
);
518 } else if (args_are_copies(x
, y
)) {
519 return do_constant_folding_cond_eq(c
);
520 } else if (arg_is_const(y
) && yv
== 0) {
533 /* Return 2 if the condition can't be simplified, and the result
534 of the condition (0 or 1) if it can */
535 static TCGArg
do_constant_folding_cond2(TCGArg
*p1
, TCGArg
*p2
, TCGCond c
)
537 TCGArg al
= p1
[0], ah
= p1
[1];
538 TCGArg bl
= p2
[0], bh
= p2
[1];
540 if (arg_is_const(bl
) && arg_is_const(bh
)) {
541 tcg_target_ulong blv
= arg_info(bl
)->val
;
542 tcg_target_ulong bhv
= arg_info(bh
)->val
;
543 uint64_t b
= deposit64(blv
, 32, 32, bhv
);
545 if (arg_is_const(al
) && arg_is_const(ah
)) {
546 tcg_target_ulong alv
= arg_info(al
)->val
;
547 tcg_target_ulong ahv
= arg_info(ah
)->val
;
548 uint64_t a
= deposit64(alv
, 32, 32, ahv
);
549 return do_constant_folding_cond_64(a
, b
, c
);
562 if (args_are_copies(al
, bl
) && args_are_copies(ah
, bh
)) {
563 return do_constant_folding_cond_eq(c
);
568 static bool swap_commutative(TCGArg dest
, TCGArg
*p1
, TCGArg
*p2
)
570 TCGArg a1
= *p1
, a2
= *p2
;
572 sum
+= arg_is_const(a1
);
573 sum
-= arg_is_const(a2
);
575 /* Prefer the constant in second argument, and then the form
576 op a, a, b, which is better handled on non-RISC hosts. */
577 if (sum
> 0 || (sum
== 0 && dest
== a2
)) {
585 static bool swap_commutative2(TCGArg
*p1
, TCGArg
*p2
)
588 sum
+= arg_is_const(p1
[0]);
589 sum
+= arg_is_const(p1
[1]);
590 sum
-= arg_is_const(p2
[0]);
591 sum
-= arg_is_const(p2
[1]);
594 t
= p1
[0], p1
[0] = p2
[0], p2
[0] = t
;
595 t
= p1
[1], p1
[1] = p2
[1], p2
[1] = t
;
601 /* Propagate constants and copies, fold constant expressions. */
602 void tcg_optimize(TCGContext
*s
)
604 int nb_temps
, nb_globals
, i
;
605 TCGOp
*op
, *op_next
, *prev_mb
= NULL
;
606 TCGTempSet temps_used
;
608 /* Array VALS has an element for each temp.
609 If this temp holds a constant then its value is kept in VALS' element.
610 If this temp is a copy of other ones then the other copies are
611 available through the doubly linked circular list. */
613 nb_temps
= s
->nb_temps
;
614 nb_globals
= s
->nb_globals
;
616 memset(&temps_used
, 0, sizeof(temps_used
));
617 for (i
= 0; i
< nb_temps
; ++i
) {
618 s
->temps
[i
].state_ptr
= NULL
;
621 QTAILQ_FOREACH_SAFE(op
, &s
->ops
, link
, op_next
) {
622 uint64_t mask
, partmask
, affected
, tmp
;
623 int nb_oargs
, nb_iargs
;
624 TCGOpcode opc
= op
->opc
;
625 const TCGOpDef
*def
= &tcg_op_defs
[opc
];
627 /* Count the arguments, and initialize the temps that are
629 if (opc
== INDEX_op_call
) {
630 nb_oargs
= TCGOP_CALLO(op
);
631 nb_iargs
= TCGOP_CALLI(op
);
632 for (i
= 0; i
< nb_oargs
+ nb_iargs
; i
++) {
633 TCGTemp
*ts
= arg_temp(op
->args
[i
]);
635 init_ts_info(&temps_used
, ts
);
639 nb_oargs
= def
->nb_oargs
;
640 nb_iargs
= def
->nb_iargs
;
641 for (i
= 0; i
< nb_oargs
+ nb_iargs
; i
++) {
642 init_arg_info(&temps_used
, op
->args
[i
]);
646 /* Do copy propagation */
647 for (i
= nb_oargs
; i
< nb_oargs
+ nb_iargs
; i
++) {
648 TCGTemp
*ts
= arg_temp(op
->args
[i
]);
649 if (ts
&& ts_is_copy(ts
)) {
650 op
->args
[i
] = temp_arg(find_better_copy(s
, ts
));
654 /* For commutative operations make constant second argument */
656 CASE_OP_32_64_VEC(add
):
657 CASE_OP_32_64_VEC(mul
):
658 CASE_OP_32_64_VEC(and):
659 CASE_OP_32_64_VEC(or):
660 CASE_OP_32_64_VEC(xor):
664 CASE_OP_32_64(muluh
):
665 CASE_OP_32_64(mulsh
):
666 swap_commutative(op
->args
[0], &op
->args
[1], &op
->args
[2]);
668 CASE_OP_32_64(brcond
):
669 if (swap_commutative(-1, &op
->args
[0], &op
->args
[1])) {
670 op
->args
[2] = tcg_swap_cond(op
->args
[2]);
673 CASE_OP_32_64(setcond
):
674 if (swap_commutative(op
->args
[0], &op
->args
[1], &op
->args
[2])) {
675 op
->args
[3] = tcg_swap_cond(op
->args
[3]);
678 CASE_OP_32_64(movcond
):
679 if (swap_commutative(-1, &op
->args
[1], &op
->args
[2])) {
680 op
->args
[5] = tcg_swap_cond(op
->args
[5]);
682 /* For movcond, we canonicalize the "false" input reg to match
683 the destination reg so that the tcg backend can implement
684 a "move if true" operation. */
685 if (swap_commutative(op
->args
[0], &op
->args
[4], &op
->args
[3])) {
686 op
->args
[5] = tcg_invert_cond(op
->args
[5]);
690 swap_commutative(op
->args
[0], &op
->args
[2], &op
->args
[4]);
691 swap_commutative(op
->args
[1], &op
->args
[3], &op
->args
[5]);
693 CASE_OP_32_64(mulu2
):
694 CASE_OP_32_64(muls2
):
695 swap_commutative(op
->args
[0], &op
->args
[2], &op
->args
[3]);
697 case INDEX_op_brcond2_i32
:
698 if (swap_commutative2(&op
->args
[0], &op
->args
[2])) {
699 op
->args
[4] = tcg_swap_cond(op
->args
[4]);
702 case INDEX_op_setcond2_i32
:
703 if (swap_commutative2(&op
->args
[1], &op
->args
[3])) {
704 op
->args
[5] = tcg_swap_cond(op
->args
[5]);
711 /* Simplify expressions for "shift/rot r, 0, a => movi r, 0",
712 and "sub r, 0, a => neg r, a" case. */
719 if (arg_is_const(op
->args
[1])
720 && arg_info(op
->args
[1])->val
== 0) {
721 tcg_opt_gen_movi(s
, &temps_used
, op
, op
->args
[0], 0);
725 CASE_OP_32_64_VEC(sub
):
730 if (arg_is_const(op
->args
[2])) {
731 /* Proceed with possible constant folding. */
734 if (opc
== INDEX_op_sub_i32
) {
735 neg_op
= INDEX_op_neg_i32
;
736 have_neg
= TCG_TARGET_HAS_neg_i32
;
737 } else if (opc
== INDEX_op_sub_i64
) {
738 neg_op
= INDEX_op_neg_i64
;
739 have_neg
= TCG_TARGET_HAS_neg_i64
;
740 } else if (TCG_TARGET_HAS_neg_vec
) {
741 TCGType type
= TCGOP_VECL(op
) + TCG_TYPE_V64
;
742 unsigned vece
= TCGOP_VECE(op
);
743 neg_op
= INDEX_op_neg_vec
;
744 have_neg
= tcg_can_emit_vec_op(neg_op
, type
, vece
) > 0;
751 if (arg_is_const(op
->args
[1])
752 && arg_info(op
->args
[1])->val
== 0) {
754 reset_temp(op
->args
[0]);
755 op
->args
[1] = op
->args
[2];
760 CASE_OP_32_64_VEC(xor):
762 if (!arg_is_const(op
->args
[1])
763 && arg_is_const(op
->args
[2])
764 && arg_info(op
->args
[2])->val
== -1) {
770 if (!arg_is_const(op
->args
[1])
771 && arg_is_const(op
->args
[2])
772 && arg_info(op
->args
[2])->val
== 0) {
777 CASE_OP_32_64_VEC(andc
):
778 if (!arg_is_const(op
->args
[2])
779 && arg_is_const(op
->args
[1])
780 && arg_info(op
->args
[1])->val
== -1) {
785 CASE_OP_32_64_VEC(orc
):
787 if (!arg_is_const(op
->args
[2])
788 && arg_is_const(op
->args
[1])
789 && arg_info(op
->args
[1])->val
== 0) {
799 if (def
->flags
& TCG_OPF_VECTOR
) {
800 not_op
= INDEX_op_not_vec
;
801 have_not
= TCG_TARGET_HAS_not_vec
;
802 } else if (def
->flags
& TCG_OPF_64BIT
) {
803 not_op
= INDEX_op_not_i64
;
804 have_not
= TCG_TARGET_HAS_not_i64
;
806 not_op
= INDEX_op_not_i32
;
807 have_not
= TCG_TARGET_HAS_not_i32
;
813 reset_temp(op
->args
[0]);
814 op
->args
[1] = op
->args
[i
];
821 /* Simplify expression for "op r, a, const => mov r, a" cases */
823 CASE_OP_32_64_VEC(add
):
824 CASE_OP_32_64_VEC(sub
):
825 CASE_OP_32_64_VEC(or):
826 CASE_OP_32_64_VEC(xor):
827 CASE_OP_32_64_VEC(andc
):
833 if (!arg_is_const(op
->args
[1])
834 && arg_is_const(op
->args
[2])
835 && arg_info(op
->args
[2])->val
== 0) {
836 tcg_opt_gen_mov(s
, op
, op
->args
[0], op
->args
[1]);
840 CASE_OP_32_64_VEC(and):
841 CASE_OP_32_64_VEC(orc
):
843 if (!arg_is_const(op
->args
[1])
844 && arg_is_const(op
->args
[2])
845 && arg_info(op
->args
[2])->val
== -1) {
846 tcg_opt_gen_mov(s
, op
, op
->args
[0], op
->args
[1]);
854 /* Simplify using known-zero bits. Currently only ops with a single
855 output argument is supported. */
859 CASE_OP_32_64(ext8s
):
860 if ((arg_info(op
->args
[1])->mask
& 0x80) != 0) {
864 CASE_OP_32_64(ext8u
):
867 CASE_OP_32_64(ext16s
):
868 if ((arg_info(op
->args
[1])->mask
& 0x8000) != 0) {
872 CASE_OP_32_64(ext16u
):
875 case INDEX_op_ext32s_i64
:
876 if ((arg_info(op
->args
[1])->mask
& 0x80000000) != 0) {
880 case INDEX_op_ext32u_i64
:
885 mask
= arg_info(op
->args
[2])->mask
;
886 if (arg_is_const(op
->args
[2])) {
888 affected
= arg_info(op
->args
[1])->mask
& ~mask
;
890 mask
= arg_info(op
->args
[1])->mask
& mask
;
893 case INDEX_op_ext_i32_i64
:
894 if ((arg_info(op
->args
[1])->mask
& 0x80000000) != 0) {
898 case INDEX_op_extu_i32_i64
:
899 /* We do not compute affected as it is a size changing op. */
900 mask
= (uint32_t)arg_info(op
->args
[1])->mask
;
904 /* Known-zeros does not imply known-ones. Therefore unless
905 op->args[2] is constant, we can't infer anything from it. */
906 if (arg_is_const(op
->args
[2])) {
907 mask
= ~arg_info(op
->args
[2])->mask
;
910 /* But we certainly know nothing outside args[1] may be set. */
911 mask
= arg_info(op
->args
[1])->mask
;
914 case INDEX_op_sar_i32
:
915 if (arg_is_const(op
->args
[2])) {
916 tmp
= arg_info(op
->args
[2])->val
& 31;
917 mask
= (int32_t)arg_info(op
->args
[1])->mask
>> tmp
;
920 case INDEX_op_sar_i64
:
921 if (arg_is_const(op
->args
[2])) {
922 tmp
= arg_info(op
->args
[2])->val
& 63;
923 mask
= (int64_t)arg_info(op
->args
[1])->mask
>> tmp
;
927 case INDEX_op_shr_i32
:
928 if (arg_is_const(op
->args
[2])) {
929 tmp
= arg_info(op
->args
[2])->val
& 31;
930 mask
= (uint32_t)arg_info(op
->args
[1])->mask
>> tmp
;
933 case INDEX_op_shr_i64
:
934 if (arg_is_const(op
->args
[2])) {
935 tmp
= arg_info(op
->args
[2])->val
& 63;
936 mask
= (uint64_t)arg_info(op
->args
[1])->mask
>> tmp
;
940 case INDEX_op_extrl_i64_i32
:
941 mask
= (uint32_t)arg_info(op
->args
[1])->mask
;
943 case INDEX_op_extrh_i64_i32
:
944 mask
= (uint64_t)arg_info(op
->args
[1])->mask
>> 32;
948 if (arg_is_const(op
->args
[2])) {
949 tmp
= arg_info(op
->args
[2])->val
& (TCG_TARGET_REG_BITS
- 1);
950 mask
= arg_info(op
->args
[1])->mask
<< tmp
;
955 /* Set to 1 all bits to the left of the rightmost. */
956 mask
= -(arg_info(op
->args
[1])->mask
957 & -arg_info(op
->args
[1])->mask
);
960 CASE_OP_32_64(deposit
):
961 mask
= deposit64(arg_info(op
->args
[1])->mask
,
962 op
->args
[3], op
->args
[4],
963 arg_info(op
->args
[2])->mask
);
966 CASE_OP_32_64(extract
):
967 mask
= extract64(arg_info(op
->args
[1])->mask
,
968 op
->args
[2], op
->args
[3]);
969 if (op
->args
[2] == 0) {
970 affected
= arg_info(op
->args
[1])->mask
& ~mask
;
973 CASE_OP_32_64(sextract
):
974 mask
= sextract64(arg_info(op
->args
[1])->mask
,
975 op
->args
[2], op
->args
[3]);
976 if (op
->args
[2] == 0 && (tcg_target_long
)mask
>= 0) {
977 affected
= arg_info(op
->args
[1])->mask
& ~mask
;
983 mask
= arg_info(op
->args
[1])->mask
| arg_info(op
->args
[2])->mask
;
986 case INDEX_op_clz_i32
:
987 case INDEX_op_ctz_i32
:
988 mask
= arg_info(op
->args
[2])->mask
| 31;
991 case INDEX_op_clz_i64
:
992 case INDEX_op_ctz_i64
:
993 mask
= arg_info(op
->args
[2])->mask
| 63;
996 case INDEX_op_ctpop_i32
:
999 case INDEX_op_ctpop_i64
:
1003 CASE_OP_32_64(setcond
):
1004 case INDEX_op_setcond2_i32
:
1008 CASE_OP_32_64(movcond
):
1009 mask
= arg_info(op
->args
[3])->mask
| arg_info(op
->args
[4])->mask
;
1012 CASE_OP_32_64(ld8u
):
1015 CASE_OP_32_64(ld16u
):
1018 case INDEX_op_ld32u_i64
:
1022 CASE_OP_32_64(qemu_ld
):
1024 TCGMemOpIdx oi
= op
->args
[nb_oargs
+ nb_iargs
];
1025 MemOp mop
= get_memop(oi
);
1026 if (!(mop
& MO_SIGN
)) {
1027 mask
= (2ULL << ((8 << (mop
& MO_SIZE
)) - 1)) - 1;
1036 /* 32-bit ops generate 32-bit results. For the result is zero test
1037 below, we can ignore high bits, but for further optimizations we
1038 need to record that the high bits contain garbage. */
1040 if (!(def
->flags
& TCG_OPF_64BIT
)) {
1041 mask
|= ~(tcg_target_ulong
)0xffffffffu
;
1042 partmask
&= 0xffffffffu
;
1043 affected
&= 0xffffffffu
;
1046 if (partmask
== 0) {
1047 tcg_debug_assert(nb_oargs
== 1);
1048 tcg_opt_gen_movi(s
, &temps_used
, op
, op
->args
[0], 0);
1051 if (affected
== 0) {
1052 tcg_debug_assert(nb_oargs
== 1);
1053 tcg_opt_gen_mov(s
, op
, op
->args
[0], op
->args
[1]);
1057 /* Simplify expression for "op r, a, 0 => movi r, 0" cases */
1059 CASE_OP_32_64_VEC(and):
1060 CASE_OP_32_64_VEC(mul
):
1061 CASE_OP_32_64(muluh
):
1062 CASE_OP_32_64(mulsh
):
1063 if (arg_is_const(op
->args
[2])
1064 && arg_info(op
->args
[2])->val
== 0) {
1065 tcg_opt_gen_movi(s
, &temps_used
, op
, op
->args
[0], 0);
1073 /* Simplify expression for "op r, a, a => mov r, a" cases */
1075 CASE_OP_32_64_VEC(or):
1076 CASE_OP_32_64_VEC(and):
1077 if (args_are_copies(op
->args
[1], op
->args
[2])) {
1078 tcg_opt_gen_mov(s
, op
, op
->args
[0], op
->args
[1]);
1086 /* Simplify expression for "op r, a, a => movi r, 0" cases */
1088 CASE_OP_32_64_VEC(andc
):
1089 CASE_OP_32_64_VEC(sub
):
1090 CASE_OP_32_64_VEC(xor):
1091 if (args_are_copies(op
->args
[1], op
->args
[2])) {
1092 tcg_opt_gen_movi(s
, &temps_used
, op
, op
->args
[0], 0);
1100 /* Propagate constants through copy operations and do constant
1101 folding. Constants will be substituted to arguments by register
1102 allocator where needed and possible. Also detect copies. */
1104 CASE_OP_32_64_VEC(mov
):
1105 tcg_opt_gen_mov(s
, op
, op
->args
[0], op
->args
[1]);
1108 case INDEX_op_dup_vec
:
1109 if (arg_is_const(op
->args
[1])) {
1110 tmp
= arg_info(op
->args
[1])->val
;
1111 tmp
= dup_const(TCGOP_VECE(op
), tmp
);
1112 tcg_opt_gen_movi(s
, &temps_used
, op
, op
->args
[0], tmp
);
1117 case INDEX_op_dup2_vec
:
1118 assert(TCG_TARGET_REG_BITS
== 32);
1119 if (arg_is_const(op
->args
[1]) && arg_is_const(op
->args
[2])) {
1120 tcg_opt_gen_movi(s
, &temps_used
, op
, op
->args
[0],
1121 deposit64(arg_info(op
->args
[1])->val
, 32, 32,
1122 arg_info(op
->args
[2])->val
));
1124 } else if (args_are_copies(op
->args
[1], op
->args
[2])) {
1125 op
->opc
= INDEX_op_dup_vec
;
1126 TCGOP_VECE(op
) = MO_32
;
1133 CASE_OP_32_64(ext8s
):
1134 CASE_OP_32_64(ext8u
):
1135 CASE_OP_32_64(ext16s
):
1136 CASE_OP_32_64(ext16u
):
1137 CASE_OP_32_64(ctpop
):
1138 CASE_OP_32_64(bswap16
):
1139 CASE_OP_32_64(bswap32
):
1140 case INDEX_op_bswap64_i64
:
1141 case INDEX_op_ext32s_i64
:
1142 case INDEX_op_ext32u_i64
:
1143 case INDEX_op_ext_i32_i64
:
1144 case INDEX_op_extu_i32_i64
:
1145 case INDEX_op_extrl_i64_i32
:
1146 case INDEX_op_extrh_i64_i32
:
1147 if (arg_is_const(op
->args
[1])) {
1148 tmp
= do_constant_folding(opc
, arg_info(op
->args
[1])->val
, 0);
1149 tcg_opt_gen_movi(s
, &temps_used
, op
, op
->args
[0], tmp
);
1163 CASE_OP_32_64(rotl
):
1164 CASE_OP_32_64(rotr
):
1165 CASE_OP_32_64(andc
):
1168 CASE_OP_32_64(nand
):
1170 CASE_OP_32_64(muluh
):
1171 CASE_OP_32_64(mulsh
):
1173 CASE_OP_32_64(divu
):
1175 CASE_OP_32_64(remu
):
1176 if (arg_is_const(op
->args
[1]) && arg_is_const(op
->args
[2])) {
1177 tmp
= do_constant_folding(opc
, arg_info(op
->args
[1])->val
,
1178 arg_info(op
->args
[2])->val
);
1179 tcg_opt_gen_movi(s
, &temps_used
, op
, op
->args
[0], tmp
);
1186 if (arg_is_const(op
->args
[1])) {
1187 TCGArg v
= arg_info(op
->args
[1])->val
;
1189 tmp
= do_constant_folding(opc
, v
, 0);
1190 tcg_opt_gen_movi(s
, &temps_used
, op
, op
->args
[0], tmp
);
1192 tcg_opt_gen_mov(s
, op
, op
->args
[0], op
->args
[2]);
1198 CASE_OP_32_64(deposit
):
1199 if (arg_is_const(op
->args
[1]) && arg_is_const(op
->args
[2])) {
1200 tmp
= deposit64(arg_info(op
->args
[1])->val
,
1201 op
->args
[3], op
->args
[4],
1202 arg_info(op
->args
[2])->val
);
1203 tcg_opt_gen_movi(s
, &temps_used
, op
, op
->args
[0], tmp
);
1208 CASE_OP_32_64(extract
):
1209 if (arg_is_const(op
->args
[1])) {
1210 tmp
= extract64(arg_info(op
->args
[1])->val
,
1211 op
->args
[2], op
->args
[3]);
1212 tcg_opt_gen_movi(s
, &temps_used
, op
, op
->args
[0], tmp
);
1217 CASE_OP_32_64(sextract
):
1218 if (arg_is_const(op
->args
[1])) {
1219 tmp
= sextract64(arg_info(op
->args
[1])->val
,
1220 op
->args
[2], op
->args
[3]);
1221 tcg_opt_gen_movi(s
, &temps_used
, op
, op
->args
[0], tmp
);
1226 CASE_OP_32_64(extract2
):
1227 if (arg_is_const(op
->args
[1]) && arg_is_const(op
->args
[2])) {
1228 uint64_t v1
= arg_info(op
->args
[1])->val
;
1229 uint64_t v2
= arg_info(op
->args
[2])->val
;
1230 int shr
= op
->args
[3];
1232 if (opc
== INDEX_op_extract2_i64
) {
1233 tmp
= (v1
>> shr
) | (v2
<< (64 - shr
));
1235 tmp
= (int32_t)(((uint32_t)v1
>> shr
) |
1236 ((uint32_t)v2
<< (32 - shr
)));
1238 tcg_opt_gen_movi(s
, &temps_used
, op
, op
->args
[0], tmp
);
1243 CASE_OP_32_64(setcond
):
1244 tmp
= do_constant_folding_cond(opc
, op
->args
[1],
1245 op
->args
[2], op
->args
[3]);
1247 tcg_opt_gen_movi(s
, &temps_used
, op
, op
->args
[0], tmp
);
1252 CASE_OP_32_64(brcond
):
1253 tmp
= do_constant_folding_cond(opc
, op
->args
[0],
1254 op
->args
[1], op
->args
[2]);
1257 memset(&temps_used
, 0, sizeof(temps_used
));
1258 op
->opc
= INDEX_op_br
;
1259 op
->args
[0] = op
->args
[3];
1261 tcg_op_remove(s
, op
);
1267 CASE_OP_32_64(movcond
):
1268 tmp
= do_constant_folding_cond(opc
, op
->args
[1],
1269 op
->args
[2], op
->args
[5]);
1271 tcg_opt_gen_mov(s
, op
, op
->args
[0], op
->args
[4-tmp
]);
1274 if (arg_is_const(op
->args
[3]) && arg_is_const(op
->args
[4])) {
1275 uint64_t tv
= arg_info(op
->args
[3])->val
;
1276 uint64_t fv
= arg_info(op
->args
[4])->val
;
1277 TCGCond cond
= op
->args
[5];
1279 if (fv
== 1 && tv
== 0) {
1280 cond
= tcg_invert_cond(cond
);
1281 } else if (!(tv
== 1 && fv
== 0)) {
1285 op
->opc
= opc
= (opc
== INDEX_op_movcond_i32
1286 ? INDEX_op_setcond_i32
1287 : INDEX_op_setcond_i64
);
1292 case INDEX_op_add2_i32
:
1293 case INDEX_op_sub2_i32
:
1294 if (arg_is_const(op
->args
[2]) && arg_is_const(op
->args
[3])
1295 && arg_is_const(op
->args
[4]) && arg_is_const(op
->args
[5])) {
1296 uint32_t al
= arg_info(op
->args
[2])->val
;
1297 uint32_t ah
= arg_info(op
->args
[3])->val
;
1298 uint32_t bl
= arg_info(op
->args
[4])->val
;
1299 uint32_t bh
= arg_info(op
->args
[5])->val
;
1300 uint64_t a
= ((uint64_t)ah
<< 32) | al
;
1301 uint64_t b
= ((uint64_t)bh
<< 32) | bl
;
1303 TCGOp
*op2
= tcg_op_insert_before(s
, op
, INDEX_op_mov_i32
);
1305 if (opc
== INDEX_op_add2_i32
) {
1313 tcg_opt_gen_movi(s
, &temps_used
, op
, rl
, (int32_t)a
);
1314 tcg_opt_gen_movi(s
, &temps_used
, op2
, rh
, (int32_t)(a
>> 32));
1319 case INDEX_op_mulu2_i32
:
1320 if (arg_is_const(op
->args
[2]) && arg_is_const(op
->args
[3])) {
1321 uint32_t a
= arg_info(op
->args
[2])->val
;
1322 uint32_t b
= arg_info(op
->args
[3])->val
;
1323 uint64_t r
= (uint64_t)a
* b
;
1325 TCGOp
*op2
= tcg_op_insert_before(s
, op
, INDEX_op_mov_i32
);
1329 tcg_opt_gen_movi(s
, &temps_used
, op
, rl
, (int32_t)r
);
1330 tcg_opt_gen_movi(s
, &temps_used
, op2
, rh
, (int32_t)(r
>> 32));
1335 case INDEX_op_brcond2_i32
:
1336 tmp
= do_constant_folding_cond2(&op
->args
[0], &op
->args
[2],
1341 memset(&temps_used
, 0, sizeof(temps_used
));
1342 op
->opc
= INDEX_op_br
;
1343 op
->args
[0] = op
->args
[5];
1346 tcg_op_remove(s
, op
);
1348 } else if ((op
->args
[4] == TCG_COND_LT
1349 || op
->args
[4] == TCG_COND_GE
)
1350 && arg_is_const(op
->args
[2])
1351 && arg_info(op
->args
[2])->val
== 0
1352 && arg_is_const(op
->args
[3])
1353 && arg_info(op
->args
[3])->val
== 0) {
1354 /* Simplify LT/GE comparisons vs zero to a single compare
1355 vs the high word of the input. */
1357 memset(&temps_used
, 0, sizeof(temps_used
));
1358 op
->opc
= INDEX_op_brcond_i32
;
1359 op
->args
[0] = op
->args
[1];
1360 op
->args
[1] = op
->args
[3];
1361 op
->args
[2] = op
->args
[4];
1362 op
->args
[3] = op
->args
[5];
1363 } else if (op
->args
[4] == TCG_COND_EQ
) {
1364 /* Simplify EQ comparisons where one of the pairs
1365 can be simplified. */
1366 tmp
= do_constant_folding_cond(INDEX_op_brcond_i32
,
1367 op
->args
[0], op
->args
[2],
1370 goto do_brcond_false
;
1371 } else if (tmp
== 1) {
1372 goto do_brcond_high
;
1374 tmp
= do_constant_folding_cond(INDEX_op_brcond_i32
,
1375 op
->args
[1], op
->args
[3],
1378 goto do_brcond_false
;
1379 } else if (tmp
!= 1) {
1383 memset(&temps_used
, 0, sizeof(temps_used
));
1384 op
->opc
= INDEX_op_brcond_i32
;
1385 op
->args
[1] = op
->args
[2];
1386 op
->args
[2] = op
->args
[4];
1387 op
->args
[3] = op
->args
[5];
1388 } else if (op
->args
[4] == TCG_COND_NE
) {
1389 /* Simplify NE comparisons where one of the pairs
1390 can be simplified. */
1391 tmp
= do_constant_folding_cond(INDEX_op_brcond_i32
,
1392 op
->args
[0], op
->args
[2],
1395 goto do_brcond_high
;
1396 } else if (tmp
== 1) {
1397 goto do_brcond_true
;
1399 tmp
= do_constant_folding_cond(INDEX_op_brcond_i32
,
1400 op
->args
[1], op
->args
[3],
1404 } else if (tmp
== 1) {
1405 goto do_brcond_true
;
1413 case INDEX_op_setcond2_i32
:
1414 tmp
= do_constant_folding_cond2(&op
->args
[1], &op
->args
[3],
1418 tcg_opt_gen_movi(s
, &temps_used
, op
, op
->args
[0], tmp
);
1419 } else if ((op
->args
[5] == TCG_COND_LT
1420 || op
->args
[5] == TCG_COND_GE
)
1421 && arg_is_const(op
->args
[3])
1422 && arg_info(op
->args
[3])->val
== 0
1423 && arg_is_const(op
->args
[4])
1424 && arg_info(op
->args
[4])->val
== 0) {
1425 /* Simplify LT/GE comparisons vs zero to a single compare
1426 vs the high word of the input. */
1428 reset_temp(op
->args
[0]);
1429 arg_info(op
->args
[0])->mask
= 1;
1430 op
->opc
= INDEX_op_setcond_i32
;
1431 op
->args
[1] = op
->args
[2];
1432 op
->args
[2] = op
->args
[4];
1433 op
->args
[3] = op
->args
[5];
1434 } else if (op
->args
[5] == TCG_COND_EQ
) {
1435 /* Simplify EQ comparisons where one of the pairs
1436 can be simplified. */
1437 tmp
= do_constant_folding_cond(INDEX_op_setcond_i32
,
1438 op
->args
[1], op
->args
[3],
1441 goto do_setcond_const
;
1442 } else if (tmp
== 1) {
1443 goto do_setcond_high
;
1445 tmp
= do_constant_folding_cond(INDEX_op_setcond_i32
,
1446 op
->args
[2], op
->args
[4],
1449 goto do_setcond_high
;
1450 } else if (tmp
!= 1) {
1454 reset_temp(op
->args
[0]);
1455 arg_info(op
->args
[0])->mask
= 1;
1456 op
->opc
= INDEX_op_setcond_i32
;
1457 op
->args
[2] = op
->args
[3];
1458 op
->args
[3] = op
->args
[5];
1459 } else if (op
->args
[5] == TCG_COND_NE
) {
1460 /* Simplify NE comparisons where one of the pairs
1461 can be simplified. */
1462 tmp
= do_constant_folding_cond(INDEX_op_setcond_i32
,
1463 op
->args
[1], op
->args
[3],
1466 goto do_setcond_high
;
1467 } else if (tmp
== 1) {
1468 goto do_setcond_const
;
1470 tmp
= do_constant_folding_cond(INDEX_op_setcond_i32
,
1471 op
->args
[2], op
->args
[4],
1474 goto do_setcond_low
;
1475 } else if (tmp
== 1) {
1476 goto do_setcond_const
;
1485 if (!(tcg_call_flags(op
)
1486 & (TCG_CALL_NO_READ_GLOBALS
| TCG_CALL_NO_WRITE_GLOBALS
))) {
1487 for (i
= 0; i
< nb_globals
; i
++) {
1488 if (test_bit(i
, temps_used
.l
)) {
1489 reset_ts(&s
->temps
[i
]);
1493 goto do_reset_output
;
1497 /* Default case: we know nothing about operation (or were unable
1498 to compute the operation result) so no propagation is done.
1499 We trash everything if the operation is the end of a basic
1500 block, otherwise we only trash the output args. "mask" is
1501 the non-zero bits mask for the first output arg. */
1502 if (def
->flags
& TCG_OPF_BB_END
) {
1503 memset(&temps_used
, 0, sizeof(temps_used
));
1506 for (i
= 0; i
< nb_oargs
; i
++) {
1507 reset_temp(op
->args
[i
]);
1508 /* Save the corresponding known-zero bits mask for the
1509 first output argument (only one supported so far). */
1511 arg_info(op
->args
[i
])->mask
= mask
;
1518 /* Eliminate duplicate and redundant fence instructions. */
1522 /* Merge two barriers of the same type into one,
1523 * or a weaker barrier into a stronger one,
1524 * or two weaker barriers into a stronger one.
1525 * mb X; mb Y => mb X|Y
1526 * mb; strl => mb; st
1527 * ldaq; mb => ld; mb
1528 * ldaq; strl => ld; mb; st
1529 * Other combinations are also merged into a strong
1530 * barrier. This is stricter than specified but for
1531 * the purposes of TCG is better than not optimizing.
1533 prev_mb
->args
[0] |= op
->args
[0];
1534 tcg_op_remove(s
, op
);
1538 /* Opcodes that end the block stop the optimization. */
1539 if ((def
->flags
& TCG_OPF_BB_END
) == 0) {
1543 case INDEX_op_qemu_ld_i32
:
1544 case INDEX_op_qemu_ld_i64
:
1545 case INDEX_op_qemu_st_i32
:
1546 case INDEX_op_qemu_st8_i32
:
1547 case INDEX_op_qemu_st_i64
:
1549 /* Opcodes that touch guest memory stop the optimization. */
1553 } else if (opc
== INDEX_op_mb
) {