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 "qemu/int128.h"
28 #include "qemu/interval-tree.h"
29 #include "tcg/tcg-op-common.h"
30 #include "tcg-internal.h"
32 #define CASE_OP_32_64(x) \
33 glue(glue(case INDEX_op_, x), _i32): \
34 glue(glue(case INDEX_op_, x), _i64)
36 #define CASE_OP_32_64_VEC(x) \
37 glue(glue(case INDEX_op_, x), _i32): \
38 glue(glue(case INDEX_op_, x), _i64): \
39 glue(glue(case INDEX_op_, x), _vec)
41 typedef struct MemCopyInfo
{
42 IntervalTreeNode itree
;
43 QSIMPLEQ_ENTRY (MemCopyInfo
) next
;
48 typedef struct TempOptInfo
{
52 QSIMPLEQ_HEAD(, MemCopyInfo
) mem_copy
;
54 uint64_t z_mask
; /* mask bit is 0 if and only if value bit is 0 */
55 uint64_t s_mask
; /* a left-aligned mask of clrsb(value) bits. */
58 typedef struct OptContext
{
61 TCGTempSet temps_used
;
63 IntervalTreeRoot mem_copy
;
64 QSIMPLEQ_HEAD(, MemCopyInfo
) mem_free
;
66 /* In flight values from optimization. */
67 uint64_t a_mask
; /* mask bit is 0 iff value identical to first input */
68 uint64_t z_mask
; /* mask bit is 0 iff value bit is 0 */
69 uint64_t s_mask
; /* mask of clrsb(value) bits */
73 /* Calculate the smask for a specific value. */
74 static uint64_t smask_from_value(uint64_t value
)
76 int rep
= clrsb64(value
);
77 return ~(~0ull >> rep
);
81 * Calculate the smask for a given set of known-zeros.
82 * If there are lots of zeros on the left, we can consider the remainder
83 * an unsigned field, and thus the corresponding signed field is one bit
86 static uint64_t smask_from_zmask(uint64_t zmask
)
89 * Only the 0 bits are significant for zmask, thus the msb itself
90 * must be zero, else we have no sign information.
92 int rep
= clz64(zmask
);
97 return ~(~0ull >> rep
);
101 * Recreate a properly left-aligned smask after manipulation.
102 * Some bit-shuffling, particularly shifts and rotates, may
103 * retain sign bits on the left, but may scatter disconnected
104 * sign bits on the right. Retain only what remains to the left.
106 static uint64_t smask_from_smask(int64_t smask
)
108 /* Only the 1 bits are significant for smask */
109 return smask_from_zmask(~smask
);
112 static inline TempOptInfo
*ts_info(TCGTemp
*ts
)
114 return ts
->state_ptr
;
117 static inline TempOptInfo
*arg_info(TCGArg arg
)
119 return ts_info(arg_temp(arg
));
122 static inline bool ts_is_const(TCGTemp
*ts
)
124 return ts_info(ts
)->is_const
;
127 static inline bool ts_is_const_val(TCGTemp
*ts
, uint64_t val
)
129 TempOptInfo
*ti
= ts_info(ts
);
130 return ti
->is_const
&& ti
->val
== val
;
133 static inline bool arg_is_const(TCGArg arg
)
135 return ts_is_const(arg_temp(arg
));
138 static inline bool arg_is_const_val(TCGArg arg
, uint64_t val
)
140 return ts_is_const_val(arg_temp(arg
), val
);
143 static inline bool ts_is_copy(TCGTemp
*ts
)
145 return ts_info(ts
)->next_copy
!= ts
;
148 static TCGTemp
*cmp_better_copy(TCGTemp
*a
, TCGTemp
*b
)
150 return a
->kind
< b
->kind
? b
: a
;
153 /* Initialize and activate a temporary. */
154 static void init_ts_info(OptContext
*ctx
, TCGTemp
*ts
)
156 size_t idx
= temp_idx(ts
);
159 if (test_bit(idx
, ctx
->temps_used
.l
)) {
162 set_bit(idx
, ctx
->temps_used
.l
);
166 ti
= tcg_malloc(sizeof(TempOptInfo
));
172 QSIMPLEQ_INIT(&ti
->mem_copy
);
173 if (ts
->kind
== TEMP_CONST
) {
176 ti
->z_mask
= ts
->val
;
177 ti
->s_mask
= smask_from_value(ts
->val
);
179 ti
->is_const
= false;
185 static MemCopyInfo
*mem_copy_first(OptContext
*ctx
, intptr_t s
, intptr_t l
)
187 IntervalTreeNode
*r
= interval_tree_iter_first(&ctx
->mem_copy
, s
, l
);
188 return r
? container_of(r
, MemCopyInfo
, itree
) : NULL
;
191 static MemCopyInfo
*mem_copy_next(MemCopyInfo
*mem
, intptr_t s
, intptr_t l
)
193 IntervalTreeNode
*r
= interval_tree_iter_next(&mem
->itree
, s
, l
);
194 return r
? container_of(r
, MemCopyInfo
, itree
) : NULL
;
197 static void remove_mem_copy(OptContext
*ctx
, MemCopyInfo
*mc
)
199 TCGTemp
*ts
= mc
->ts
;
200 TempOptInfo
*ti
= ts_info(ts
);
202 interval_tree_remove(&mc
->itree
, &ctx
->mem_copy
);
203 QSIMPLEQ_REMOVE(&ti
->mem_copy
, mc
, MemCopyInfo
, next
);
204 QSIMPLEQ_INSERT_TAIL(&ctx
->mem_free
, mc
, next
);
207 static void remove_mem_copy_in(OptContext
*ctx
, intptr_t s
, intptr_t l
)
210 MemCopyInfo
*mc
= mem_copy_first(ctx
, s
, l
);
214 remove_mem_copy(ctx
, mc
);
218 static void remove_mem_copy_all(OptContext
*ctx
)
220 remove_mem_copy_in(ctx
, 0, -1);
221 tcg_debug_assert(interval_tree_is_empty(&ctx
->mem_copy
));
224 static TCGTemp
*find_better_copy(TCGTemp
*ts
)
228 /* If this is already readonly, we can't do better. */
229 if (temp_readonly(ts
)) {
234 for (i
= ts_info(ts
)->next_copy
; i
!= ts
; i
= ts_info(i
)->next_copy
) {
235 ret
= cmp_better_copy(ret
, i
);
240 static void move_mem_copies(TCGTemp
*dst_ts
, TCGTemp
*src_ts
)
242 TempOptInfo
*si
= ts_info(src_ts
);
243 TempOptInfo
*di
= ts_info(dst_ts
);
246 QSIMPLEQ_FOREACH(mc
, &si
->mem_copy
, next
) {
247 tcg_debug_assert(mc
->ts
== src_ts
);
250 QSIMPLEQ_CONCAT(&di
->mem_copy
, &si
->mem_copy
);
253 /* Reset TEMP's state, possibly removing the temp for the list of copies. */
254 static void reset_ts(OptContext
*ctx
, TCGTemp
*ts
)
256 TempOptInfo
*ti
= ts_info(ts
);
257 TCGTemp
*pts
= ti
->prev_copy
;
258 TCGTemp
*nts
= ti
->next_copy
;
259 TempOptInfo
*pi
= ts_info(pts
);
260 TempOptInfo
*ni
= ts_info(nts
);
262 ni
->prev_copy
= ti
->prev_copy
;
263 pi
->next_copy
= ti
->next_copy
;
266 ti
->is_const
= false;
270 if (!QSIMPLEQ_EMPTY(&ti
->mem_copy
)) {
272 /* Last temp copy being removed, the mem copies die. */
274 QSIMPLEQ_FOREACH(mc
, &ti
->mem_copy
, next
) {
275 interval_tree_remove(&mc
->itree
, &ctx
->mem_copy
);
277 QSIMPLEQ_CONCAT(&ctx
->mem_free
, &ti
->mem_copy
);
279 move_mem_copies(find_better_copy(nts
), ts
);
284 static void reset_temp(OptContext
*ctx
, TCGArg arg
)
286 reset_ts(ctx
, arg_temp(arg
));
289 static void record_mem_copy(OptContext
*ctx
, TCGType type
,
290 TCGTemp
*ts
, intptr_t start
, intptr_t last
)
295 mc
= QSIMPLEQ_FIRST(&ctx
->mem_free
);
297 QSIMPLEQ_REMOVE_HEAD(&ctx
->mem_free
, next
);
299 mc
= tcg_malloc(sizeof(*mc
));
302 memset(mc
, 0, sizeof(*mc
));
303 mc
->itree
.start
= start
;
304 mc
->itree
.last
= last
;
306 interval_tree_insert(&mc
->itree
, &ctx
->mem_copy
);
308 ts
= find_better_copy(ts
);
311 QSIMPLEQ_INSERT_TAIL(&ti
->mem_copy
, mc
, next
);
314 static bool ts_are_copies(TCGTemp
*ts1
, TCGTemp
*ts2
)
322 if (!ts_is_copy(ts1
) || !ts_is_copy(ts2
)) {
326 for (i
= ts_info(ts1
)->next_copy
; i
!= ts1
; i
= ts_info(i
)->next_copy
) {
335 static bool args_are_copies(TCGArg arg1
, TCGArg arg2
)
337 return ts_are_copies(arg_temp(arg1
), arg_temp(arg2
));
340 static TCGTemp
*find_mem_copy_for(OptContext
*ctx
, TCGType type
, intptr_t s
)
344 for (mc
= mem_copy_first(ctx
, s
, s
); mc
; mc
= mem_copy_next(mc
, s
, s
)) {
345 if (mc
->itree
.start
== s
&& mc
->type
== type
) {
346 return find_better_copy(mc
->ts
);
352 static TCGArg
arg_new_constant(OptContext
*ctx
, uint64_t val
)
354 TCGType type
= ctx
->type
;
357 if (type
== TCG_TYPE_I32
) {
361 ts
= tcg_constant_internal(type
, val
);
362 init_ts_info(ctx
, ts
);
367 static TCGArg
arg_new_temp(OptContext
*ctx
)
369 TCGTemp
*ts
= tcg_temp_new_internal(ctx
->type
, TEMP_EBB
);
370 init_ts_info(ctx
, ts
);
374 static bool tcg_opt_gen_mov(OptContext
*ctx
, TCGOp
*op
, TCGArg dst
, TCGArg src
)
376 TCGTemp
*dst_ts
= arg_temp(dst
);
377 TCGTemp
*src_ts
= arg_temp(src
);
382 if (ts_are_copies(dst_ts
, src_ts
)) {
383 tcg_op_remove(ctx
->tcg
, op
);
387 reset_ts(ctx
, dst_ts
);
388 di
= ts_info(dst_ts
);
389 si
= ts_info(src_ts
);
393 new_op
= INDEX_op_mov_i32
;
396 new_op
= INDEX_op_mov_i64
;
401 /* TCGOP_VECL and TCGOP_VECE remain unchanged. */
402 new_op
= INDEX_op_mov_vec
;
405 g_assert_not_reached();
411 di
->z_mask
= si
->z_mask
;
412 di
->s_mask
= si
->s_mask
;
414 if (src_ts
->type
== dst_ts
->type
) {
415 TempOptInfo
*ni
= ts_info(si
->next_copy
);
417 di
->next_copy
= si
->next_copy
;
418 di
->prev_copy
= src_ts
;
419 ni
->prev_copy
= dst_ts
;
420 si
->next_copy
= dst_ts
;
421 di
->is_const
= si
->is_const
;
424 if (!QSIMPLEQ_EMPTY(&si
->mem_copy
)
425 && cmp_better_copy(src_ts
, dst_ts
) == dst_ts
) {
426 move_mem_copies(dst_ts
, src_ts
);
432 static bool tcg_opt_gen_movi(OptContext
*ctx
, TCGOp
*op
,
433 TCGArg dst
, uint64_t val
)
435 /* Convert movi to mov with constant temp. */
436 return tcg_opt_gen_mov(ctx
, op
, dst
, arg_new_constant(ctx
, val
));
439 static uint64_t do_constant_folding_2(TCGOpcode op
, uint64_t x
, uint64_t y
)
453 CASE_OP_32_64_VEC(and):
456 CASE_OP_32_64_VEC(or):
459 CASE_OP_32_64_VEC(xor):
462 case INDEX_op_shl_i32
:
463 return (uint32_t)x
<< (y
& 31);
465 case INDEX_op_shl_i64
:
466 return (uint64_t)x
<< (y
& 63);
468 case INDEX_op_shr_i32
:
469 return (uint32_t)x
>> (y
& 31);
471 case INDEX_op_shr_i64
:
472 return (uint64_t)x
>> (y
& 63);
474 case INDEX_op_sar_i32
:
475 return (int32_t)x
>> (y
& 31);
477 case INDEX_op_sar_i64
:
478 return (int64_t)x
>> (y
& 63);
480 case INDEX_op_rotr_i32
:
481 return ror32(x
, y
& 31);
483 case INDEX_op_rotr_i64
:
484 return ror64(x
, y
& 63);
486 case INDEX_op_rotl_i32
:
487 return rol32(x
, y
& 31);
489 case INDEX_op_rotl_i64
:
490 return rol64(x
, y
& 63);
492 CASE_OP_32_64_VEC(not):
498 CASE_OP_32_64_VEC(andc
):
501 CASE_OP_32_64_VEC(orc
):
504 CASE_OP_32_64_VEC(eqv
):
507 CASE_OP_32_64_VEC(nand
):
510 CASE_OP_32_64_VEC(nor
):
513 case INDEX_op_clz_i32
:
514 return (uint32_t)x
? clz32(x
) : y
;
516 case INDEX_op_clz_i64
:
517 return x
? clz64(x
) : y
;
519 case INDEX_op_ctz_i32
:
520 return (uint32_t)x
? ctz32(x
) : y
;
522 case INDEX_op_ctz_i64
:
523 return x
? ctz64(x
) : y
;
525 case INDEX_op_ctpop_i32
:
528 case INDEX_op_ctpop_i64
:
531 CASE_OP_32_64(ext8s
):
534 CASE_OP_32_64(ext16s
):
537 CASE_OP_32_64(ext8u
):
540 CASE_OP_32_64(ext16u
):
543 CASE_OP_32_64(bswap16
):
545 return y
& TCG_BSWAP_OS
? (int16_t)x
: x
;
547 CASE_OP_32_64(bswap32
):
549 return y
& TCG_BSWAP_OS
? (int32_t)x
: x
;
551 case INDEX_op_bswap64_i64
:
554 case INDEX_op_ext_i32_i64
:
555 case INDEX_op_ext32s_i64
:
558 case INDEX_op_extu_i32_i64
:
559 case INDEX_op_extrl_i64_i32
:
560 case INDEX_op_ext32u_i64
:
563 case INDEX_op_extrh_i64_i32
:
564 return (uint64_t)x
>> 32;
566 case INDEX_op_muluh_i32
:
567 return ((uint64_t)(uint32_t)x
* (uint32_t)y
) >> 32;
568 case INDEX_op_mulsh_i32
:
569 return ((int64_t)(int32_t)x
* (int32_t)y
) >> 32;
571 case INDEX_op_muluh_i64
:
572 mulu64(&l64
, &h64
, x
, y
);
574 case INDEX_op_mulsh_i64
:
575 muls64(&l64
, &h64
, x
, y
);
578 case INDEX_op_div_i32
:
579 /* Avoid crashing on divide by zero, otherwise undefined. */
580 return (int32_t)x
/ ((int32_t)y
? : 1);
581 case INDEX_op_divu_i32
:
582 return (uint32_t)x
/ ((uint32_t)y
? : 1);
583 case INDEX_op_div_i64
:
584 return (int64_t)x
/ ((int64_t)y
? : 1);
585 case INDEX_op_divu_i64
:
586 return (uint64_t)x
/ ((uint64_t)y
? : 1);
588 case INDEX_op_rem_i32
:
589 return (int32_t)x
% ((int32_t)y
? : 1);
590 case INDEX_op_remu_i32
:
591 return (uint32_t)x
% ((uint32_t)y
? : 1);
592 case INDEX_op_rem_i64
:
593 return (int64_t)x
% ((int64_t)y
? : 1);
594 case INDEX_op_remu_i64
:
595 return (uint64_t)x
% ((uint64_t)y
? : 1);
598 g_assert_not_reached();
602 static uint64_t do_constant_folding(TCGOpcode op
, TCGType type
,
603 uint64_t x
, uint64_t y
)
605 uint64_t res
= do_constant_folding_2(op
, x
, y
);
606 if (type
== TCG_TYPE_I32
) {
612 static bool do_constant_folding_cond_32(uint32_t x
, uint32_t y
, TCGCond c
)
620 return (int32_t)x
< (int32_t)y
;
622 return (int32_t)x
>= (int32_t)y
;
624 return (int32_t)x
<= (int32_t)y
;
626 return (int32_t)x
> (int32_t)y
;
639 case TCG_COND_ALWAYS
:
643 g_assert_not_reached();
646 static bool do_constant_folding_cond_64(uint64_t x
, uint64_t y
, TCGCond c
)
654 return (int64_t)x
< (int64_t)y
;
656 return (int64_t)x
>= (int64_t)y
;
658 return (int64_t)x
<= (int64_t)y
;
660 return (int64_t)x
> (int64_t)y
;
673 case TCG_COND_ALWAYS
:
677 g_assert_not_reached();
680 static int do_constant_folding_cond_eq(TCGCond c
)
698 case TCG_COND_ALWAYS
:
702 g_assert_not_reached();
706 * Return -1 if the condition can't be simplified,
707 * and the result of the condition (0 or 1) if it can.
709 static int do_constant_folding_cond(TCGType type
, TCGArg x
,
712 if (arg_is_const(x
) && arg_is_const(y
)) {
713 uint64_t xv
= arg_info(x
)->val
;
714 uint64_t yv
= arg_info(y
)->val
;
718 return do_constant_folding_cond_32(xv
, yv
, c
);
720 return do_constant_folding_cond_64(xv
, yv
, c
);
722 /* Only scalar comparisons are optimizable */
725 } else if (args_are_copies(x
, y
)) {
726 return do_constant_folding_cond_eq(c
);
727 } else if (arg_is_const_val(y
, 0)) {
744 * @dest: TCGArg of the destination argument, or NO_DEST.
745 * @p1: first paired argument
746 * @p2: second paired argument
748 * If *@p1 is a constant and *@p2 is not, swap.
749 * If *@p2 matches @dest, swap.
750 * Return true if a swap was performed.
753 #define NO_DEST temp_arg(NULL)
755 static bool swap_commutative(TCGArg dest
, TCGArg
*p1
, TCGArg
*p2
)
757 TCGArg a1
= *p1
, a2
= *p2
;
759 sum
+= arg_is_const(a1
);
760 sum
-= arg_is_const(a2
);
762 /* Prefer the constant in second argument, and then the form
763 op a, a, b, which is better handled on non-RISC hosts. */
764 if (sum
> 0 || (sum
== 0 && dest
== a2
)) {
772 static bool swap_commutative2(TCGArg
*p1
, TCGArg
*p2
)
775 sum
+= arg_is_const(p1
[0]);
776 sum
+= arg_is_const(p1
[1]);
777 sum
-= arg_is_const(p2
[0]);
778 sum
-= arg_is_const(p2
[1]);
781 t
= p1
[0], p1
[0] = p2
[0], p2
[0] = t
;
782 t
= p1
[1], p1
[1] = p2
[1], p2
[1] = t
;
789 * Return -1 if the condition can't be simplified,
790 * and the result of the condition (0 or 1) if it can.
792 static int do_constant_folding_cond1(OptContext
*ctx
, TCGOp
*op
, TCGArg dest
,
793 TCGArg
*p1
, TCGArg
*p2
, TCGArg
*pcond
)
799 swap
= swap_commutative(dest
, p1
, p2
);
802 *pcond
= cond
= tcg_swap_cond(cond
);
805 r
= do_constant_folding_cond(ctx
->type
, *p1
, *p2
, cond
);
809 if (!is_tst_cond(cond
)) {
814 * TSTNE x,x -> NE x,0
815 * TSTNE x,-1 -> NE x,0
817 if (args_are_copies(*p1
, *p2
) || arg_is_const_val(*p2
, -1)) {
818 *p2
= arg_new_constant(ctx
, 0);
819 *pcond
= tcg_tst_eqne_cond(cond
);
823 /* TSTNE x,sign -> LT x,0 */
824 if (arg_is_const_val(*p2
, (ctx
->type
== TCG_TYPE_I32
825 ? INT32_MIN
: INT64_MIN
))) {
826 *p2
= arg_new_constant(ctx
, 0);
827 *pcond
= tcg_tst_ltge_cond(cond
);
831 /* Expand to AND with a temporary if no backend support. */
832 if (!TCG_TARGET_HAS_tst
) {
833 TCGOpcode and_opc
= (ctx
->type
== TCG_TYPE_I32
834 ? INDEX_op_and_i32
: INDEX_op_and_i64
);
835 TCGOp
*op2
= tcg_op_insert_before(ctx
->tcg
, op
, and_opc
, 3);
836 TCGArg tmp
= arg_new_temp(ctx
);
843 *p2
= arg_new_constant(ctx
, 0);
844 *pcond
= tcg_tst_eqne_cond(cond
);
849 static int do_constant_folding_cond2(OptContext
*ctx
, TCGOp
*op
, TCGArg
*args
)
851 TCGArg al
, ah
, bl
, bh
;
856 swap
= swap_commutative2(args
, args
+ 2);
859 args
[4] = c
= tcg_swap_cond(c
);
867 if (arg_is_const(bl
) && arg_is_const(bh
)) {
868 tcg_target_ulong blv
= arg_info(bl
)->val
;
869 tcg_target_ulong bhv
= arg_info(bh
)->val
;
870 uint64_t b
= deposit64(blv
, 32, 32, bhv
);
872 if (arg_is_const(al
) && arg_is_const(ah
)) {
873 tcg_target_ulong alv
= arg_info(al
)->val
;
874 tcg_target_ulong ahv
= arg_info(ah
)->val
;
875 uint64_t a
= deposit64(alv
, 32, 32, ahv
);
877 r
= do_constant_folding_cond_64(a
, b
, c
);
896 /* TSTNE x,-1 -> NE x,0 */
897 if (b
== -1 && is_tst_cond(c
)) {
898 args
[3] = args
[2] = arg_new_constant(ctx
, 0);
899 args
[4] = tcg_tst_eqne_cond(c
);
903 /* TSTNE x,sign -> LT x,0 */
904 if (b
== INT64_MIN
&& is_tst_cond(c
)) {
905 /* bl must be 0, so copy that to bh */
907 args
[4] = tcg_tst_ltge_cond(c
);
912 if (args_are_copies(al
, bl
) && args_are_copies(ah
, bh
)) {
913 r
= do_constant_folding_cond_eq(c
);
918 /* TSTNE x,x -> NE x,0 */
919 if (is_tst_cond(c
)) {
920 args
[3] = args
[2] = arg_new_constant(ctx
, 0);
921 args
[4] = tcg_tst_eqne_cond(c
);
926 /* Expand to AND with a temporary if no backend support. */
927 if (!TCG_TARGET_HAS_tst
&& is_tst_cond(c
)) {
928 TCGOp
*op1
= tcg_op_insert_before(ctx
->tcg
, op
, INDEX_op_and_i32
, 3);
929 TCGOp
*op2
= tcg_op_insert_before(ctx
->tcg
, op
, INDEX_op_and_i32
, 3);
930 TCGArg t1
= arg_new_temp(ctx
);
931 TCGArg t2
= arg_new_temp(ctx
);
942 args
[3] = args
[2] = arg_new_constant(ctx
, 0);
943 args
[4] = tcg_tst_eqne_cond(c
);
948 static void init_arguments(OptContext
*ctx
, TCGOp
*op
, int nb_args
)
950 for (int i
= 0; i
< nb_args
; i
++) {
951 TCGTemp
*ts
= arg_temp(op
->args
[i
]);
952 init_ts_info(ctx
, ts
);
956 static void copy_propagate(OptContext
*ctx
, TCGOp
*op
,
957 int nb_oargs
, int nb_iargs
)
959 for (int i
= nb_oargs
; i
< nb_oargs
+ nb_iargs
; i
++) {
960 TCGTemp
*ts
= arg_temp(op
->args
[i
]);
961 if (ts_is_copy(ts
)) {
962 op
->args
[i
] = temp_arg(find_better_copy(ts
));
967 static void finish_folding(OptContext
*ctx
, TCGOp
*op
)
969 const TCGOpDef
*def
= &tcg_op_defs
[op
->opc
];
973 * We only optimize extended basic blocks. If the opcode ends a BB
974 * and is not a conditional branch, reset all temp data.
976 if (def
->flags
& TCG_OPF_BB_END
) {
978 if (!(def
->flags
& TCG_OPF_COND_BRANCH
)) {
979 memset(&ctx
->temps_used
, 0, sizeof(ctx
->temps_used
));
980 remove_mem_copy_all(ctx
);
985 nb_oargs
= def
->nb_oargs
;
986 for (i
= 0; i
< nb_oargs
; i
++) {
987 TCGTemp
*ts
= arg_temp(op
->args
[i
]);
990 * Save the corresponding known-zero/sign bits mask for the
991 * first output argument (only one supported so far).
994 ts_info(ts
)->z_mask
= ctx
->z_mask
;
995 ts_info(ts
)->s_mask
= ctx
->s_mask
;
1001 * The fold_* functions return true when processing is complete,
1002 * usually by folding the operation to a constant or to a copy,
1003 * and calling tcg_opt_gen_{mov,movi}. They may do other things,
1004 * like collect information about the value produced, for use in
1005 * optimizing a subsequent operation.
1007 * These first fold_* functions are all helpers, used by other
1008 * folders for more specific operations.
1011 static bool fold_const1(OptContext
*ctx
, TCGOp
*op
)
1013 if (arg_is_const(op
->args
[1])) {
1016 t
= arg_info(op
->args
[1])->val
;
1017 t
= do_constant_folding(op
->opc
, ctx
->type
, t
, 0);
1018 return tcg_opt_gen_movi(ctx
, op
, op
->args
[0], t
);
1023 static bool fold_const2(OptContext
*ctx
, TCGOp
*op
)
1025 if (arg_is_const(op
->args
[1]) && arg_is_const(op
->args
[2])) {
1026 uint64_t t1
= arg_info(op
->args
[1])->val
;
1027 uint64_t t2
= arg_info(op
->args
[2])->val
;
1029 t1
= do_constant_folding(op
->opc
, ctx
->type
, t1
, t2
);
1030 return tcg_opt_gen_movi(ctx
, op
, op
->args
[0], t1
);
1035 static bool fold_commutative(OptContext
*ctx
, TCGOp
*op
)
1037 swap_commutative(op
->args
[0], &op
->args
[1], &op
->args
[2]);
1041 static bool fold_const2_commutative(OptContext
*ctx
, TCGOp
*op
)
1043 swap_commutative(op
->args
[0], &op
->args
[1], &op
->args
[2]);
1044 return fold_const2(ctx
, op
);
1047 static bool fold_masks(OptContext
*ctx
, TCGOp
*op
)
1049 uint64_t a_mask
= ctx
->a_mask
;
1050 uint64_t z_mask
= ctx
->z_mask
;
1051 uint64_t s_mask
= ctx
->s_mask
;
1054 * 32-bit ops generate 32-bit results, which for the purpose of
1055 * simplifying tcg are sign-extended. Certainly that's how we
1056 * represent our constants elsewhere. Note that the bits will
1057 * be reset properly for a 64-bit value when encountering the
1058 * type changing opcodes.
1060 if (ctx
->type
== TCG_TYPE_I32
) {
1061 a_mask
= (int32_t)a_mask
;
1062 z_mask
= (int32_t)z_mask
;
1063 s_mask
|= MAKE_64BIT_MASK(32, 32);
1064 ctx
->z_mask
= z_mask
;
1065 ctx
->s_mask
= s_mask
;
1069 return tcg_opt_gen_movi(ctx
, op
, op
->args
[0], 0);
1072 return tcg_opt_gen_mov(ctx
, op
, op
->args
[0], op
->args
[1]);
1078 * Convert @op to NOT, if NOT is supported by the host.
1079 * Return true f the conversion is successful, which will still
1080 * indicate that the processing is complete.
1082 static bool fold_not(OptContext
*ctx
, TCGOp
*op
);
1083 static bool fold_to_not(OptContext
*ctx
, TCGOp
*op
, int idx
)
1088 switch (ctx
->type
) {
1090 not_op
= INDEX_op_not_i32
;
1091 have_not
= TCG_TARGET_HAS_not_i32
;
1094 not_op
= INDEX_op_not_i64
;
1095 have_not
= TCG_TARGET_HAS_not_i64
;
1100 not_op
= INDEX_op_not_vec
;
1101 have_not
= TCG_TARGET_HAS_not_vec
;
1104 g_assert_not_reached();
1108 op
->args
[1] = op
->args
[idx
];
1109 return fold_not(ctx
, op
);
1114 /* If the binary operation has first argument @i, fold to @i. */
1115 static bool fold_ix_to_i(OptContext
*ctx
, TCGOp
*op
, uint64_t i
)
1117 if (arg_is_const_val(op
->args
[1], i
)) {
1118 return tcg_opt_gen_movi(ctx
, op
, op
->args
[0], i
);
1123 /* If the binary operation has first argument @i, fold to NOT. */
1124 static bool fold_ix_to_not(OptContext
*ctx
, TCGOp
*op
, uint64_t i
)
1126 if (arg_is_const_val(op
->args
[1], i
)) {
1127 return fold_to_not(ctx
, op
, 2);
1132 /* If the binary operation has second argument @i, fold to @i. */
1133 static bool fold_xi_to_i(OptContext
*ctx
, TCGOp
*op
, uint64_t i
)
1135 if (arg_is_const_val(op
->args
[2], i
)) {
1136 return tcg_opt_gen_movi(ctx
, op
, op
->args
[0], i
);
1141 /* If the binary operation has second argument @i, fold to identity. */
1142 static bool fold_xi_to_x(OptContext
*ctx
, TCGOp
*op
, uint64_t i
)
1144 if (arg_is_const_val(op
->args
[2], i
)) {
1145 return tcg_opt_gen_mov(ctx
, op
, op
->args
[0], op
->args
[1]);
1150 /* If the binary operation has second argument @i, fold to NOT. */
1151 static bool fold_xi_to_not(OptContext
*ctx
, TCGOp
*op
, uint64_t i
)
1153 if (arg_is_const_val(op
->args
[2], i
)) {
1154 return fold_to_not(ctx
, op
, 1);
1159 /* If the binary operation has both arguments equal, fold to @i. */
1160 static bool fold_xx_to_i(OptContext
*ctx
, TCGOp
*op
, uint64_t i
)
1162 if (args_are_copies(op
->args
[1], op
->args
[2])) {
1163 return tcg_opt_gen_movi(ctx
, op
, op
->args
[0], i
);
1168 /* If the binary operation has both arguments equal, fold to identity. */
1169 static bool fold_xx_to_x(OptContext
*ctx
, TCGOp
*op
)
1171 if (args_are_copies(op
->args
[1], op
->args
[2])) {
1172 return tcg_opt_gen_mov(ctx
, op
, op
->args
[0], op
->args
[1]);
1178 * These outermost fold_<op> functions are sorted alphabetically.
1180 * The ordering of the transformations should be:
1181 * 1) those that produce a constant
1182 * 2) those that produce a copy
1183 * 3) those that produce information about the result value.
1186 static bool fold_add(OptContext
*ctx
, TCGOp
*op
)
1188 if (fold_const2_commutative(ctx
, op
) ||
1189 fold_xi_to_x(ctx
, op
, 0)) {
1195 /* We cannot as yet do_constant_folding with vectors. */
1196 static bool fold_add_vec(OptContext
*ctx
, TCGOp
*op
)
1198 if (fold_commutative(ctx
, op
) ||
1199 fold_xi_to_x(ctx
, op
, 0)) {
1205 static bool fold_addsub2(OptContext
*ctx
, TCGOp
*op
, bool add
)
1207 bool a_const
= arg_is_const(op
->args
[2]) && arg_is_const(op
->args
[3]);
1208 bool b_const
= arg_is_const(op
->args
[4]) && arg_is_const(op
->args
[5]);
1210 if (a_const
&& b_const
) {
1211 uint64_t al
= arg_info(op
->args
[2])->val
;
1212 uint64_t ah
= arg_info(op
->args
[3])->val
;
1213 uint64_t bl
= arg_info(op
->args
[4])->val
;
1214 uint64_t bh
= arg_info(op
->args
[5])->val
;
1218 if (ctx
->type
== TCG_TYPE_I32
) {
1219 uint64_t a
= deposit64(al
, 32, 32, ah
);
1220 uint64_t b
= deposit64(bl
, 32, 32, bh
);
1228 al
= sextract64(a
, 0, 32);
1229 ah
= sextract64(a
, 32, 32);
1231 Int128 a
= int128_make128(al
, ah
);
1232 Int128 b
= int128_make128(bl
, bh
);
1235 a
= int128_add(a
, b
);
1237 a
= int128_sub(a
, b
);
1240 al
= int128_getlo(a
);
1241 ah
= int128_gethi(a
);
1247 /* The proper opcode is supplied by tcg_opt_gen_mov. */
1248 op2
= tcg_op_insert_before(ctx
->tcg
, op
, 0, 2);
1250 tcg_opt_gen_movi(ctx
, op
, rl
, al
);
1251 tcg_opt_gen_movi(ctx
, op2
, rh
, ah
);
1255 /* Fold sub2 r,x,i to add2 r,x,-i */
1256 if (!add
&& b_const
) {
1257 uint64_t bl
= arg_info(op
->args
[4])->val
;
1258 uint64_t bh
= arg_info(op
->args
[5])->val
;
1260 /* Negate the two parts without assembling and disassembling. */
1264 op
->opc
= (ctx
->type
== TCG_TYPE_I32
1265 ? INDEX_op_add2_i32
: INDEX_op_add2_i64
);
1266 op
->args
[4] = arg_new_constant(ctx
, bl
);
1267 op
->args
[5] = arg_new_constant(ctx
, bh
);
1272 static bool fold_add2(OptContext
*ctx
, TCGOp
*op
)
1274 /* Note that the high and low parts may be independently swapped. */
1275 swap_commutative(op
->args
[0], &op
->args
[2], &op
->args
[4]);
1276 swap_commutative(op
->args
[1], &op
->args
[3], &op
->args
[5]);
1278 return fold_addsub2(ctx
, op
, true);
1281 static bool fold_and(OptContext
*ctx
, TCGOp
*op
)
1285 if (fold_const2_commutative(ctx
, op
) ||
1286 fold_xi_to_i(ctx
, op
, 0) ||
1287 fold_xi_to_x(ctx
, op
, -1) ||
1288 fold_xx_to_x(ctx
, op
)) {
1292 z1
= arg_info(op
->args
[1])->z_mask
;
1293 z2
= arg_info(op
->args
[2])->z_mask
;
1294 ctx
->z_mask
= z1
& z2
;
1297 * Sign repetitions are perforce all identical, whether they are 1 or 0.
1298 * Bitwise operations preserve the relative quantity of the repetitions.
1300 ctx
->s_mask
= arg_info(op
->args
[1])->s_mask
1301 & arg_info(op
->args
[2])->s_mask
;
1304 * Known-zeros does not imply known-ones. Therefore unless
1305 * arg2 is constant, we can't infer affected bits from it.
1307 if (arg_is_const(op
->args
[2])) {
1308 ctx
->a_mask
= z1
& ~z2
;
1311 return fold_masks(ctx
, op
);
1314 static bool fold_andc(OptContext
*ctx
, TCGOp
*op
)
1318 if (fold_const2(ctx
, op
) ||
1319 fold_xx_to_i(ctx
, op
, 0) ||
1320 fold_xi_to_x(ctx
, op
, 0) ||
1321 fold_ix_to_not(ctx
, op
, -1)) {
1325 z1
= arg_info(op
->args
[1])->z_mask
;
1328 * Known-zeros does not imply known-ones. Therefore unless
1329 * arg2 is constant, we can't infer anything from it.
1331 if (arg_is_const(op
->args
[2])) {
1332 uint64_t z2
= ~arg_info(op
->args
[2])->z_mask
;
1333 ctx
->a_mask
= z1
& ~z2
;
1338 ctx
->s_mask
= arg_info(op
->args
[1])->s_mask
1339 & arg_info(op
->args
[2])->s_mask
;
1340 return fold_masks(ctx
, op
);
1343 static bool fold_brcond(OptContext
*ctx
, TCGOp
*op
)
1345 int i
= do_constant_folding_cond1(ctx
, op
, NO_DEST
, &op
->args
[0],
1346 &op
->args
[1], &op
->args
[2]);
1348 tcg_op_remove(ctx
->tcg
, op
);
1352 op
->opc
= INDEX_op_br
;
1353 op
->args
[0] = op
->args
[3];
1358 static bool fold_brcond2(OptContext
*ctx
, TCGOp
*op
)
1364 i
= do_constant_folding_cond2(ctx
, op
, &op
->args
[0]);
1366 label
= op
->args
[5];
1368 goto do_brcond_const
;
1375 * Simplify LT/GE comparisons vs zero to a single compare
1376 * vs the high word of the input.
1378 if (arg_is_const_val(op
->args
[2], 0) &&
1379 arg_is_const_val(op
->args
[3], 0)) {
1380 goto do_brcond_high
;
1389 * Simplify EQ/NE comparisons where one of the pairs
1390 * can be simplified.
1392 i
= do_constant_folding_cond(TCG_TYPE_I32
, op
->args
[0],
1396 goto do_brcond_const
;
1398 goto do_brcond_high
;
1401 i
= do_constant_folding_cond(TCG_TYPE_I32
, op
->args
[1],
1405 goto do_brcond_const
;
1411 case TCG_COND_TSTEQ
:
1412 case TCG_COND_TSTNE
:
1413 if (arg_is_const_val(op
->args
[2], 0)) {
1414 goto do_brcond_high
;
1416 if (arg_is_const_val(op
->args
[3], 0)) {
1425 op
->opc
= INDEX_op_brcond_i32
;
1426 op
->args
[1] = op
->args
[2];
1428 op
->args
[3] = label
;
1429 return fold_brcond(ctx
, op
);
1432 op
->opc
= INDEX_op_brcond_i32
;
1433 op
->args
[0] = op
->args
[1];
1434 op
->args
[1] = op
->args
[3];
1436 op
->args
[3] = label
;
1437 return fold_brcond(ctx
, op
);
1441 tcg_op_remove(ctx
->tcg
, op
);
1444 op
->opc
= INDEX_op_br
;
1445 op
->args
[0] = label
;
1451 static bool fold_bswap(OptContext
*ctx
, TCGOp
*op
)
1453 uint64_t z_mask
, s_mask
, sign
;
1455 if (arg_is_const(op
->args
[1])) {
1456 uint64_t t
= arg_info(op
->args
[1])->val
;
1458 t
= do_constant_folding(op
->opc
, ctx
->type
, t
, op
->args
[2]);
1459 return tcg_opt_gen_movi(ctx
, op
, op
->args
[0], t
);
1462 z_mask
= arg_info(op
->args
[1])->z_mask
;
1465 case INDEX_op_bswap16_i32
:
1466 case INDEX_op_bswap16_i64
:
1467 z_mask
= bswap16(z_mask
);
1470 case INDEX_op_bswap32_i32
:
1471 case INDEX_op_bswap32_i64
:
1472 z_mask
= bswap32(z_mask
);
1475 case INDEX_op_bswap64_i64
:
1476 z_mask
= bswap64(z_mask
);
1480 g_assert_not_reached();
1482 s_mask
= smask_from_zmask(z_mask
);
1484 switch (op
->args
[2] & (TCG_BSWAP_OZ
| TCG_BSWAP_OS
)) {
1488 /* If the sign bit may be 1, force all the bits above to 1. */
1489 if (z_mask
& sign
) {
1495 /* The high bits are undefined: force all bits above the sign to 1. */
1496 z_mask
|= sign
<< 1;
1500 ctx
->z_mask
= z_mask
;
1501 ctx
->s_mask
= s_mask
;
1503 return fold_masks(ctx
, op
);
1506 static bool fold_call(OptContext
*ctx
, TCGOp
*op
)
1508 TCGContext
*s
= ctx
->tcg
;
1509 int nb_oargs
= TCGOP_CALLO(op
);
1510 int nb_iargs
= TCGOP_CALLI(op
);
1513 init_arguments(ctx
, op
, nb_oargs
+ nb_iargs
);
1514 copy_propagate(ctx
, op
, nb_oargs
, nb_iargs
);
1516 /* If the function reads or writes globals, reset temp data. */
1517 flags
= tcg_call_flags(op
);
1518 if (!(flags
& (TCG_CALL_NO_READ_GLOBALS
| TCG_CALL_NO_WRITE_GLOBALS
))) {
1519 int nb_globals
= s
->nb_globals
;
1521 for (i
= 0; i
< nb_globals
; i
++) {
1522 if (test_bit(i
, ctx
->temps_used
.l
)) {
1523 reset_ts(ctx
, &ctx
->tcg
->temps
[i
]);
1528 /* If the function has side effects, reset mem data. */
1529 if (!(flags
& TCG_CALL_NO_SIDE_EFFECTS
)) {
1530 remove_mem_copy_all(ctx
);
1533 /* Reset temp data for outputs. */
1534 for (i
= 0; i
< nb_oargs
; i
++) {
1535 reset_temp(ctx
, op
->args
[i
]);
1538 /* Stop optimizing MB across calls. */
1539 ctx
->prev_mb
= NULL
;
1543 static bool fold_count_zeros(OptContext
*ctx
, TCGOp
*op
)
1547 if (arg_is_const(op
->args
[1])) {
1548 uint64_t t
= arg_info(op
->args
[1])->val
;
1551 t
= do_constant_folding(op
->opc
, ctx
->type
, t
, 0);
1552 return tcg_opt_gen_movi(ctx
, op
, op
->args
[0], t
);
1554 return tcg_opt_gen_mov(ctx
, op
, op
->args
[0], op
->args
[2]);
1557 switch (ctx
->type
) {
1565 g_assert_not_reached();
1567 ctx
->z_mask
= arg_info(op
->args
[2])->z_mask
| z_mask
;
1568 ctx
->s_mask
= smask_from_zmask(ctx
->z_mask
);
1572 static bool fold_ctpop(OptContext
*ctx
, TCGOp
*op
)
1574 if (fold_const1(ctx
, op
)) {
1578 switch (ctx
->type
) {
1580 ctx
->z_mask
= 32 | 31;
1583 ctx
->z_mask
= 64 | 63;
1586 g_assert_not_reached();
1588 ctx
->s_mask
= smask_from_zmask(ctx
->z_mask
);
1592 static bool fold_deposit(OptContext
*ctx
, TCGOp
*op
)
1596 if (arg_is_const(op
->args
[1]) && arg_is_const(op
->args
[2])) {
1597 uint64_t t1
= arg_info(op
->args
[1])->val
;
1598 uint64_t t2
= arg_info(op
->args
[2])->val
;
1600 t1
= deposit64(t1
, op
->args
[3], op
->args
[4], t2
);
1601 return tcg_opt_gen_movi(ctx
, op
, op
->args
[0], t1
);
1604 switch (ctx
->type
) {
1606 and_opc
= INDEX_op_and_i32
;
1609 and_opc
= INDEX_op_and_i64
;
1612 g_assert_not_reached();
1615 /* Inserting a value into zero at offset 0. */
1616 if (arg_is_const_val(op
->args
[1], 0) && op
->args
[3] == 0) {
1617 uint64_t mask
= MAKE_64BIT_MASK(0, op
->args
[4]);
1620 op
->args
[1] = op
->args
[2];
1621 op
->args
[2] = arg_new_constant(ctx
, mask
);
1622 ctx
->z_mask
= mask
& arg_info(op
->args
[1])->z_mask
;
1626 /* Inserting zero into a value. */
1627 if (arg_is_const_val(op
->args
[2], 0)) {
1628 uint64_t mask
= deposit64(-1, op
->args
[3], op
->args
[4], 0);
1631 op
->args
[2] = arg_new_constant(ctx
, mask
);
1632 ctx
->z_mask
= mask
& arg_info(op
->args
[1])->z_mask
;
1636 ctx
->z_mask
= deposit64(arg_info(op
->args
[1])->z_mask
,
1637 op
->args
[3], op
->args
[4],
1638 arg_info(op
->args
[2])->z_mask
);
1642 static bool fold_divide(OptContext
*ctx
, TCGOp
*op
)
1644 if (fold_const2(ctx
, op
) ||
1645 fold_xi_to_x(ctx
, op
, 1)) {
1651 static bool fold_dup(OptContext
*ctx
, TCGOp
*op
)
1653 if (arg_is_const(op
->args
[1])) {
1654 uint64_t t
= arg_info(op
->args
[1])->val
;
1655 t
= dup_const(TCGOP_VECE(op
), t
);
1656 return tcg_opt_gen_movi(ctx
, op
, op
->args
[0], t
);
1661 static bool fold_dup2(OptContext
*ctx
, TCGOp
*op
)
1663 if (arg_is_const(op
->args
[1]) && arg_is_const(op
->args
[2])) {
1664 uint64_t t
= deposit64(arg_info(op
->args
[1])->val
, 32, 32,
1665 arg_info(op
->args
[2])->val
);
1666 return tcg_opt_gen_movi(ctx
, op
, op
->args
[0], t
);
1669 if (args_are_copies(op
->args
[1], op
->args
[2])) {
1670 op
->opc
= INDEX_op_dup_vec
;
1671 TCGOP_VECE(op
) = MO_32
;
1676 static bool fold_eqv(OptContext
*ctx
, TCGOp
*op
)
1678 if (fold_const2_commutative(ctx
, op
) ||
1679 fold_xi_to_x(ctx
, op
, -1) ||
1680 fold_xi_to_not(ctx
, op
, 0)) {
1684 ctx
->s_mask
= arg_info(op
->args
[1])->s_mask
1685 & arg_info(op
->args
[2])->s_mask
;
1689 static bool fold_extract(OptContext
*ctx
, TCGOp
*op
)
1691 uint64_t z_mask_old
, z_mask
;
1692 int pos
= op
->args
[2];
1693 int len
= op
->args
[3];
1695 if (arg_is_const(op
->args
[1])) {
1698 t
= arg_info(op
->args
[1])->val
;
1699 t
= extract64(t
, pos
, len
);
1700 return tcg_opt_gen_movi(ctx
, op
, op
->args
[0], t
);
1703 z_mask_old
= arg_info(op
->args
[1])->z_mask
;
1704 z_mask
= extract64(z_mask_old
, pos
, len
);
1706 ctx
->a_mask
= z_mask_old
^ z_mask
;
1708 ctx
->z_mask
= z_mask
;
1709 ctx
->s_mask
= smask_from_zmask(z_mask
);
1711 return fold_masks(ctx
, op
);
1714 static bool fold_extract2(OptContext
*ctx
, TCGOp
*op
)
1716 if (arg_is_const(op
->args
[1]) && arg_is_const(op
->args
[2])) {
1717 uint64_t v1
= arg_info(op
->args
[1])->val
;
1718 uint64_t v2
= arg_info(op
->args
[2])->val
;
1719 int shr
= op
->args
[3];
1721 if (op
->opc
== INDEX_op_extract2_i64
) {
1725 v1
= (uint32_t)v1
>> shr
;
1726 v2
= (uint64_t)((int32_t)v2
<< (32 - shr
));
1728 return tcg_opt_gen_movi(ctx
, op
, op
->args
[0], v1
| v2
);
1733 static bool fold_exts(OptContext
*ctx
, TCGOp
*op
)
1735 uint64_t s_mask_old
, s_mask
, z_mask
, sign
;
1736 bool type_change
= false;
1738 if (fold_const1(ctx
, op
)) {
1742 z_mask
= arg_info(op
->args
[1])->z_mask
;
1743 s_mask
= arg_info(op
->args
[1])->s_mask
;
1744 s_mask_old
= s_mask
;
1747 CASE_OP_32_64(ext8s
):
1749 z_mask
= (uint8_t)z_mask
;
1751 CASE_OP_32_64(ext16s
):
1753 z_mask
= (uint16_t)z_mask
;
1755 case INDEX_op_ext_i32_i64
:
1758 case INDEX_op_ext32s_i64
:
1760 z_mask
= (uint32_t)z_mask
;
1763 g_assert_not_reached();
1766 if (z_mask
& sign
) {
1769 s_mask
|= sign
<< 1;
1771 ctx
->z_mask
= z_mask
;
1772 ctx
->s_mask
= s_mask
;
1774 ctx
->a_mask
= s_mask
& ~s_mask_old
;
1777 return fold_masks(ctx
, op
);
1780 static bool fold_extu(OptContext
*ctx
, TCGOp
*op
)
1782 uint64_t z_mask_old
, z_mask
;
1783 bool type_change
= false;
1785 if (fold_const1(ctx
, op
)) {
1789 z_mask_old
= z_mask
= arg_info(op
->args
[1])->z_mask
;
1792 CASE_OP_32_64(ext8u
):
1793 z_mask
= (uint8_t)z_mask
;
1795 CASE_OP_32_64(ext16u
):
1796 z_mask
= (uint16_t)z_mask
;
1798 case INDEX_op_extrl_i64_i32
:
1799 case INDEX_op_extu_i32_i64
:
1802 case INDEX_op_ext32u_i64
:
1803 z_mask
= (uint32_t)z_mask
;
1805 case INDEX_op_extrh_i64_i32
:
1810 g_assert_not_reached();
1813 ctx
->z_mask
= z_mask
;
1814 ctx
->s_mask
= smask_from_zmask(z_mask
);
1816 ctx
->a_mask
= z_mask_old
^ z_mask
;
1818 return fold_masks(ctx
, op
);
1821 static bool fold_mb(OptContext
*ctx
, TCGOp
*op
)
1823 /* Eliminate duplicate and redundant fence instructions. */
1826 * Merge two barriers of the same type into one,
1827 * or a weaker barrier into a stronger one,
1828 * or two weaker barriers into a stronger one.
1829 * mb X; mb Y => mb X|Y
1830 * mb; strl => mb; st
1831 * ldaq; mb => ld; mb
1832 * ldaq; strl => ld; mb; st
1833 * Other combinations are also merged into a strong
1834 * barrier. This is stricter than specified but for
1835 * the purposes of TCG is better than not optimizing.
1837 ctx
->prev_mb
->args
[0] |= op
->args
[0];
1838 tcg_op_remove(ctx
->tcg
, op
);
1845 static bool fold_mov(OptContext
*ctx
, TCGOp
*op
)
1847 return tcg_opt_gen_mov(ctx
, op
, op
->args
[0], op
->args
[1]);
1850 static bool fold_movcond(OptContext
*ctx
, TCGOp
*op
)
1855 * Canonicalize the "false" input reg to match the destination reg so
1856 * that the tcg backend can implement a "move if true" operation.
1858 if (swap_commutative(op
->args
[0], &op
->args
[4], &op
->args
[3])) {
1859 op
->args
[5] = tcg_invert_cond(op
->args
[5]);
1862 i
= do_constant_folding_cond1(ctx
, op
, NO_DEST
, &op
->args
[1],
1863 &op
->args
[2], &op
->args
[5]);
1865 return tcg_opt_gen_mov(ctx
, op
, op
->args
[0], op
->args
[4 - i
]);
1868 ctx
->z_mask
= arg_info(op
->args
[3])->z_mask
1869 | arg_info(op
->args
[4])->z_mask
;
1870 ctx
->s_mask
= arg_info(op
->args
[3])->s_mask
1871 & arg_info(op
->args
[4])->s_mask
;
1873 if (arg_is_const(op
->args
[3]) && arg_is_const(op
->args
[4])) {
1874 uint64_t tv
= arg_info(op
->args
[3])->val
;
1875 uint64_t fv
= arg_info(op
->args
[4])->val
;
1876 TCGOpcode opc
, negopc
= 0;
1877 TCGCond cond
= op
->args
[5];
1879 switch (ctx
->type
) {
1881 opc
= INDEX_op_setcond_i32
;
1882 if (TCG_TARGET_HAS_negsetcond_i32
) {
1883 negopc
= INDEX_op_negsetcond_i32
;
1889 opc
= INDEX_op_setcond_i64
;
1890 if (TCG_TARGET_HAS_negsetcond_i64
) {
1891 negopc
= INDEX_op_negsetcond_i64
;
1895 g_assert_not_reached();
1898 if (tv
== 1 && fv
== 0) {
1901 } else if (fv
== 1 && tv
== 0) {
1903 op
->args
[3] = tcg_invert_cond(cond
);
1904 } else if (negopc
) {
1905 if (tv
== -1 && fv
== 0) {
1908 } else if (fv
== -1 && tv
== 0) {
1910 op
->args
[3] = tcg_invert_cond(cond
);
1917 static bool fold_mul(OptContext
*ctx
, TCGOp
*op
)
1919 if (fold_const2(ctx
, op
) ||
1920 fold_xi_to_i(ctx
, op
, 0) ||
1921 fold_xi_to_x(ctx
, op
, 1)) {
1927 static bool fold_mul_highpart(OptContext
*ctx
, TCGOp
*op
)
1929 if (fold_const2_commutative(ctx
, op
) ||
1930 fold_xi_to_i(ctx
, op
, 0)) {
1936 static bool fold_multiply2(OptContext
*ctx
, TCGOp
*op
)
1938 swap_commutative(op
->args
[0], &op
->args
[2], &op
->args
[3]);
1940 if (arg_is_const(op
->args
[2]) && arg_is_const(op
->args
[3])) {
1941 uint64_t a
= arg_info(op
->args
[2])->val
;
1942 uint64_t b
= arg_info(op
->args
[3])->val
;
1948 case INDEX_op_mulu2_i32
:
1949 l
= (uint64_t)(uint32_t)a
* (uint32_t)b
;
1950 h
= (int32_t)(l
>> 32);
1953 case INDEX_op_muls2_i32
:
1954 l
= (int64_t)(int32_t)a
* (int32_t)b
;
1958 case INDEX_op_mulu2_i64
:
1959 mulu64(&l
, &h
, a
, b
);
1961 case INDEX_op_muls2_i64
:
1962 muls64(&l
, &h
, a
, b
);
1965 g_assert_not_reached();
1971 /* The proper opcode is supplied by tcg_opt_gen_mov. */
1972 op2
= tcg_op_insert_before(ctx
->tcg
, op
, 0, 2);
1974 tcg_opt_gen_movi(ctx
, op
, rl
, l
);
1975 tcg_opt_gen_movi(ctx
, op2
, rh
, h
);
1981 static bool fold_nand(OptContext
*ctx
, TCGOp
*op
)
1983 if (fold_const2_commutative(ctx
, op
) ||
1984 fold_xi_to_not(ctx
, op
, -1)) {
1988 ctx
->s_mask
= arg_info(op
->args
[1])->s_mask
1989 & arg_info(op
->args
[2])->s_mask
;
1993 static bool fold_neg_no_const(OptContext
*ctx
, TCGOp
*op
)
1995 /* Set to 1 all bits to the left of the rightmost. */
1996 uint64_t z_mask
= arg_info(op
->args
[1])->z_mask
;
1997 ctx
->z_mask
= -(z_mask
& -z_mask
);
2000 * Because of fold_sub_to_neg, we want to always return true,
2001 * via finish_folding.
2003 finish_folding(ctx
, op
);
2007 static bool fold_neg(OptContext
*ctx
, TCGOp
*op
)
2009 return fold_const1(ctx
, op
) || fold_neg_no_const(ctx
, op
);
2012 static bool fold_nor(OptContext
*ctx
, TCGOp
*op
)
2014 if (fold_const2_commutative(ctx
, op
) ||
2015 fold_xi_to_not(ctx
, op
, 0)) {
2019 ctx
->s_mask
= arg_info(op
->args
[1])->s_mask
2020 & arg_info(op
->args
[2])->s_mask
;
2024 static bool fold_not(OptContext
*ctx
, TCGOp
*op
)
2026 if (fold_const1(ctx
, op
)) {
2030 ctx
->s_mask
= arg_info(op
->args
[1])->s_mask
;
2032 /* Because of fold_to_not, we want to always return true, via finish. */
2033 finish_folding(ctx
, op
);
2037 static bool fold_or(OptContext
*ctx
, TCGOp
*op
)
2039 if (fold_const2_commutative(ctx
, op
) ||
2040 fold_xi_to_x(ctx
, op
, 0) ||
2041 fold_xx_to_x(ctx
, op
)) {
2045 ctx
->z_mask
= arg_info(op
->args
[1])->z_mask
2046 | arg_info(op
->args
[2])->z_mask
;
2047 ctx
->s_mask
= arg_info(op
->args
[1])->s_mask
2048 & arg_info(op
->args
[2])->s_mask
;
2049 return fold_masks(ctx
, op
);
2052 static bool fold_orc(OptContext
*ctx
, TCGOp
*op
)
2054 if (fold_const2(ctx
, op
) ||
2055 fold_xx_to_i(ctx
, op
, -1) ||
2056 fold_xi_to_x(ctx
, op
, -1) ||
2057 fold_ix_to_not(ctx
, op
, 0)) {
2061 ctx
->s_mask
= arg_info(op
->args
[1])->s_mask
2062 & arg_info(op
->args
[2])->s_mask
;
2066 static bool fold_qemu_ld(OptContext
*ctx
, TCGOp
*op
)
2068 const TCGOpDef
*def
= &tcg_op_defs
[op
->opc
];
2069 MemOpIdx oi
= op
->args
[def
->nb_oargs
+ def
->nb_iargs
];
2070 MemOp mop
= get_memop(oi
);
2071 int width
= 8 * memop_size(mop
);
2074 ctx
->s_mask
= MAKE_64BIT_MASK(width
, 64 - width
);
2075 if (!(mop
& MO_SIGN
)) {
2076 ctx
->z_mask
= MAKE_64BIT_MASK(0, width
);
2081 /* Opcodes that touch guest memory stop the mb optimization. */
2082 ctx
->prev_mb
= NULL
;
2086 static bool fold_qemu_st(OptContext
*ctx
, TCGOp
*op
)
2088 /* Opcodes that touch guest memory stop the mb optimization. */
2089 ctx
->prev_mb
= NULL
;
2093 static bool fold_remainder(OptContext
*ctx
, TCGOp
*op
)
2095 if (fold_const2(ctx
, op
) ||
2096 fold_xx_to_i(ctx
, op
, 0)) {
2102 static bool fold_setcond_zmask(OptContext
*ctx
, TCGOp
*op
, bool neg
)
2104 uint64_t a_zmask
, b_val
;
2107 if (!arg_is_const(op
->args
[2])) {
2111 a_zmask
= arg_info(op
->args
[1])->z_mask
;
2112 b_val
= arg_info(op
->args
[2])->val
;
2115 if (ctx
->type
== TCG_TYPE_I32
) {
2116 a_zmask
= (uint32_t)a_zmask
;
2117 b_val
= (uint32_t)b_val
;
2121 * A with only low bits set vs B with high bits set means that A < B.
2123 if (a_zmask
< b_val
) {
2135 return tcg_opt_gen_movi(ctx
, op
, op
->args
[0], neg
? -inv
: inv
);
2142 * A with only lsb set is already boolean.
2145 bool convert
= false;
2153 convert
= (b_val
== 0);
2156 case TCG_COND_TSTEQ
:
2160 case TCG_COND_TSTNE
:
2161 convert
= (b_val
== 1);
2167 TCGOpcode add_opc
, xor_opc
, neg_opc
;
2170 return tcg_opt_gen_mov(ctx
, op
, op
->args
[0], op
->args
[1]);
2173 switch (ctx
->type
) {
2175 add_opc
= INDEX_op_add_i32
;
2176 neg_opc
= INDEX_op_neg_i32
;
2177 xor_opc
= INDEX_op_xor_i32
;
2180 add_opc
= INDEX_op_add_i64
;
2181 neg_opc
= INDEX_op_neg_i64
;
2182 xor_opc
= INDEX_op_xor_i64
;
2185 g_assert_not_reached();
2192 op
->args
[2] = arg_new_constant(ctx
, -1);
2195 op
->args
[2] = arg_new_constant(ctx
, 1);
2204 static void fold_setcond_tst_pow2(OptContext
*ctx
, TCGOp
*op
, bool neg
)
2206 TCGOpcode and_opc
, sub_opc
, xor_opc
, neg_opc
, shr_opc
;
2207 TCGOpcode uext_opc
= 0, sext_opc
= 0;
2208 TCGCond cond
= op
->args
[3];
2209 TCGArg ret
, src1
, src2
;
2215 if (!is_tst_cond(cond
) || !arg_is_const(op
->args
[2])) {
2220 val
= arg_info(src2
)->val
;
2221 if (!is_power_of_2(val
)) {
2226 switch (ctx
->type
) {
2228 and_opc
= INDEX_op_and_i32
;
2229 sub_opc
= INDEX_op_sub_i32
;
2230 xor_opc
= INDEX_op_xor_i32
;
2231 shr_opc
= INDEX_op_shr_i32
;
2232 neg_opc
= INDEX_op_neg_i32
;
2233 if (TCG_TARGET_extract_i32_valid(sh
, 1)) {
2234 uext_opc
= TCG_TARGET_HAS_extract_i32
? INDEX_op_extract_i32
: 0;
2235 sext_opc
= TCG_TARGET_HAS_sextract_i32
? INDEX_op_sextract_i32
: 0;
2239 and_opc
= INDEX_op_and_i64
;
2240 sub_opc
= INDEX_op_sub_i64
;
2241 xor_opc
= INDEX_op_xor_i64
;
2242 shr_opc
= INDEX_op_shr_i64
;
2243 neg_opc
= INDEX_op_neg_i64
;
2244 if (TCG_TARGET_extract_i64_valid(sh
, 1)) {
2245 uext_opc
= TCG_TARGET_HAS_extract_i64
? INDEX_op_extract_i64
: 0;
2246 sext_opc
= TCG_TARGET_HAS_sextract_i64
? INDEX_op_sextract_i64
: 0;
2250 g_assert_not_reached();
2255 inv
= cond
== TCG_COND_TSTEQ
;
2257 if (sh
&& sext_opc
&& neg
&& !inv
) {
2263 } else if (sh
&& uext_opc
) {
2270 op2
= tcg_op_insert_before(ctx
->tcg
, op
, shr_opc
, 3);
2272 op2
->args
[1] = src1
;
2273 op2
->args
[2] = arg_new_constant(ctx
, sh
);
2278 op
->args
[2] = arg_new_constant(ctx
, 1);
2282 op2
= tcg_op_insert_after(ctx
->tcg
, op
, sub_opc
, 3);
2285 op2
->args
[2] = arg_new_constant(ctx
, 1);
2287 op2
= tcg_op_insert_after(ctx
->tcg
, op
, xor_opc
, 3);
2290 op2
->args
[2] = arg_new_constant(ctx
, 1);
2292 op2
= tcg_op_insert_after(ctx
->tcg
, op
, neg_opc
, 2);
2298 static bool fold_setcond(OptContext
*ctx
, TCGOp
*op
)
2300 int i
= do_constant_folding_cond1(ctx
, op
, op
->args
[0], &op
->args
[1],
2301 &op
->args
[2], &op
->args
[3]);
2303 return tcg_opt_gen_movi(ctx
, op
, op
->args
[0], i
);
2306 if (fold_setcond_zmask(ctx
, op
, false)) {
2309 fold_setcond_tst_pow2(ctx
, op
, false);
2312 ctx
->s_mask
= smask_from_zmask(1);
2316 static bool fold_negsetcond(OptContext
*ctx
, TCGOp
*op
)
2318 int i
= do_constant_folding_cond1(ctx
, op
, op
->args
[0], &op
->args
[1],
2319 &op
->args
[2], &op
->args
[3]);
2321 return tcg_opt_gen_movi(ctx
, op
, op
->args
[0], -i
);
2324 if (fold_setcond_zmask(ctx
, op
, true)) {
2327 fold_setcond_tst_pow2(ctx
, op
, true);
2329 /* Value is {0,-1} so all bits are repetitions of the sign. */
2334 static bool fold_setcond2(OptContext
*ctx
, TCGOp
*op
)
2339 i
= do_constant_folding_cond2(ctx
, op
, &op
->args
[1]);
2342 goto do_setcond_const
;
2349 * Simplify LT/GE comparisons vs zero to a single compare
2350 * vs the high word of the input.
2352 if (arg_is_const_val(op
->args
[3], 0) &&
2353 arg_is_const_val(op
->args
[4], 0)) {
2354 goto do_setcond_high
;
2363 * Simplify EQ/NE comparisons where one of the pairs
2364 * can be simplified.
2366 i
= do_constant_folding_cond(TCG_TYPE_I32
, op
->args
[1],
2370 goto do_setcond_const
;
2372 goto do_setcond_high
;
2375 i
= do_constant_folding_cond(TCG_TYPE_I32
, op
->args
[2],
2379 goto do_setcond_const
;
2381 goto do_setcond_low
;
2385 case TCG_COND_TSTEQ
:
2386 case TCG_COND_TSTNE
:
2387 if (arg_is_const_val(op
->args
[3], 0)) {
2388 goto do_setcond_high
;
2390 if (arg_is_const_val(op
->args
[4], 0)) {
2391 goto do_setcond_low
;
2399 op
->args
[2] = op
->args
[3];
2401 op
->opc
= INDEX_op_setcond_i32
;
2402 return fold_setcond(ctx
, op
);
2405 op
->args
[1] = op
->args
[2];
2406 op
->args
[2] = op
->args
[4];
2408 op
->opc
= INDEX_op_setcond_i32
;
2409 return fold_setcond(ctx
, op
);
2413 ctx
->s_mask
= smask_from_zmask(1);
2417 return tcg_opt_gen_movi(ctx
, op
, op
->args
[0], i
);
2420 static bool fold_sextract(OptContext
*ctx
, TCGOp
*op
)
2422 uint64_t z_mask
, s_mask
, s_mask_old
;
2423 int pos
= op
->args
[2];
2424 int len
= op
->args
[3];
2426 if (arg_is_const(op
->args
[1])) {
2429 t
= arg_info(op
->args
[1])->val
;
2430 t
= sextract64(t
, pos
, len
);
2431 return tcg_opt_gen_movi(ctx
, op
, op
->args
[0], t
);
2434 z_mask
= arg_info(op
->args
[1])->z_mask
;
2435 z_mask
= sextract64(z_mask
, pos
, len
);
2436 ctx
->z_mask
= z_mask
;
2438 s_mask_old
= arg_info(op
->args
[1])->s_mask
;
2439 s_mask
= sextract64(s_mask_old
, pos
, len
);
2440 s_mask
|= MAKE_64BIT_MASK(len
, 64 - len
);
2441 ctx
->s_mask
= s_mask
;
2444 ctx
->a_mask
= s_mask
& ~s_mask_old
;
2447 return fold_masks(ctx
, op
);
2450 static bool fold_shift(OptContext
*ctx
, TCGOp
*op
)
2452 uint64_t s_mask
, z_mask
, sign
;
2454 if (fold_const2(ctx
, op
) ||
2455 fold_ix_to_i(ctx
, op
, 0) ||
2456 fold_xi_to_x(ctx
, op
, 0)) {
2460 s_mask
= arg_info(op
->args
[1])->s_mask
;
2461 z_mask
= arg_info(op
->args
[1])->z_mask
;
2463 if (arg_is_const(op
->args
[2])) {
2464 int sh
= arg_info(op
->args
[2])->val
;
2466 ctx
->z_mask
= do_constant_folding(op
->opc
, ctx
->type
, z_mask
, sh
);
2468 s_mask
= do_constant_folding(op
->opc
, ctx
->type
, s_mask
, sh
);
2469 ctx
->s_mask
= smask_from_smask(s_mask
);
2471 return fold_masks(ctx
, op
);
2477 * Arithmetic right shift will not reduce the number of
2478 * input sign repetitions.
2480 ctx
->s_mask
= s_mask
;
2484 * If the sign bit is known zero, then logical right shift
2485 * will not reduced the number of input sign repetitions.
2487 sign
= (s_mask
& -s_mask
) >> 1;
2488 if (sign
&& !(z_mask
& sign
)) {
2489 ctx
->s_mask
= s_mask
;
2499 static bool fold_sub_to_neg(OptContext
*ctx
, TCGOp
*op
)
2504 if (!arg_is_const(op
->args
[1]) || arg_info(op
->args
[1])->val
!= 0) {
2508 switch (ctx
->type
) {
2510 neg_op
= INDEX_op_neg_i32
;
2514 neg_op
= INDEX_op_neg_i64
;
2520 neg_op
= INDEX_op_neg_vec
;
2521 have_neg
= (TCG_TARGET_HAS_neg_vec
&&
2522 tcg_can_emit_vec_op(neg_op
, ctx
->type
, TCGOP_VECE(op
)) > 0);
2525 g_assert_not_reached();
2529 op
->args
[1] = op
->args
[2];
2530 return fold_neg_no_const(ctx
, op
);
2535 /* We cannot as yet do_constant_folding with vectors. */
2536 static bool fold_sub_vec(OptContext
*ctx
, TCGOp
*op
)
2538 if (fold_xx_to_i(ctx
, op
, 0) ||
2539 fold_xi_to_x(ctx
, op
, 0) ||
2540 fold_sub_to_neg(ctx
, op
)) {
2546 static bool fold_sub(OptContext
*ctx
, TCGOp
*op
)
2548 if (fold_const2(ctx
, op
) || fold_sub_vec(ctx
, op
)) {
2552 /* Fold sub r,x,i to add r,x,-i */
2553 if (arg_is_const(op
->args
[2])) {
2554 uint64_t val
= arg_info(op
->args
[2])->val
;
2556 op
->opc
= (ctx
->type
== TCG_TYPE_I32
2557 ? INDEX_op_add_i32
: INDEX_op_add_i64
);
2558 op
->args
[2] = arg_new_constant(ctx
, -val
);
2563 static bool fold_sub2(OptContext
*ctx
, TCGOp
*op
)
2565 return fold_addsub2(ctx
, op
, false);
2568 static bool fold_tcg_ld(OptContext
*ctx
, TCGOp
*op
)
2570 /* We can't do any folding with a load, but we can record bits. */
2572 CASE_OP_32_64(ld8s
):
2573 ctx
->s_mask
= MAKE_64BIT_MASK(8, 56);
2575 CASE_OP_32_64(ld8u
):
2576 ctx
->z_mask
= MAKE_64BIT_MASK(0, 8);
2577 ctx
->s_mask
= MAKE_64BIT_MASK(9, 55);
2579 CASE_OP_32_64(ld16s
):
2580 ctx
->s_mask
= MAKE_64BIT_MASK(16, 48);
2582 CASE_OP_32_64(ld16u
):
2583 ctx
->z_mask
= MAKE_64BIT_MASK(0, 16);
2584 ctx
->s_mask
= MAKE_64BIT_MASK(17, 47);
2586 case INDEX_op_ld32s_i64
:
2587 ctx
->s_mask
= MAKE_64BIT_MASK(32, 32);
2589 case INDEX_op_ld32u_i64
:
2590 ctx
->z_mask
= MAKE_64BIT_MASK(0, 32);
2591 ctx
->s_mask
= MAKE_64BIT_MASK(33, 31);
2594 g_assert_not_reached();
2599 static bool fold_tcg_ld_memcopy(OptContext
*ctx
, TCGOp
*op
)
2605 if (op
->args
[1] != tcgv_ptr_arg(tcg_env
)) {
2611 dst
= arg_temp(op
->args
[0]);
2612 src
= find_mem_copy_for(ctx
, type
, ofs
);
2613 if (src
&& src
->base_type
== type
) {
2614 return tcg_opt_gen_mov(ctx
, op
, temp_arg(dst
), temp_arg(src
));
2618 record_mem_copy(ctx
, type
, dst
, ofs
, ofs
+ tcg_type_size(type
) - 1);
2622 static bool fold_tcg_st(OptContext
*ctx
, TCGOp
*op
)
2624 intptr_t ofs
= op
->args
[2];
2627 if (op
->args
[1] != tcgv_ptr_arg(tcg_env
)) {
2628 remove_mem_copy_all(ctx
);
2636 CASE_OP_32_64(st16
):
2639 case INDEX_op_st32_i64
:
2640 case INDEX_op_st_i32
:
2643 case INDEX_op_st_i64
:
2646 case INDEX_op_st_vec
:
2647 lm1
= tcg_type_size(ctx
->type
) - 1;
2650 g_assert_not_reached();
2652 remove_mem_copy_in(ctx
, ofs
, ofs
+ lm1
);
2656 static bool fold_tcg_st_memcopy(OptContext
*ctx
, TCGOp
*op
)
2662 if (op
->args
[1] != tcgv_ptr_arg(tcg_env
)) {
2663 fold_tcg_st(ctx
, op
);
2667 src
= arg_temp(op
->args
[0]);
2672 * Eliminate duplicate stores of a constant.
2673 * This happens frequently when the target ISA zero-extends.
2675 if (ts_is_const(src
)) {
2676 TCGTemp
*prev
= find_mem_copy_for(ctx
, type
, ofs
);
2678 tcg_op_remove(ctx
->tcg
, op
);
2683 last
= ofs
+ tcg_type_size(type
) - 1;
2684 remove_mem_copy_in(ctx
, ofs
, last
);
2685 record_mem_copy(ctx
, type
, src
, ofs
, last
);
2689 static bool fold_xor(OptContext
*ctx
, TCGOp
*op
)
2691 if (fold_const2_commutative(ctx
, op
) ||
2692 fold_xx_to_i(ctx
, op
, 0) ||
2693 fold_xi_to_x(ctx
, op
, 0) ||
2694 fold_xi_to_not(ctx
, op
, -1)) {
2698 ctx
->z_mask
= arg_info(op
->args
[1])->z_mask
2699 | arg_info(op
->args
[2])->z_mask
;
2700 ctx
->s_mask
= arg_info(op
->args
[1])->s_mask
2701 & arg_info(op
->args
[2])->s_mask
;
2702 return fold_masks(ctx
, op
);
2705 /* Propagate constants and copies, fold constant expressions. */
2706 void tcg_optimize(TCGContext
*s
)
2709 TCGOp
*op
, *op_next
;
2710 OptContext ctx
= { .tcg
= s
};
2712 QSIMPLEQ_INIT(&ctx
.mem_free
);
2714 /* Array VALS has an element for each temp.
2715 If this temp holds a constant then its value is kept in VALS' element.
2716 If this temp is a copy of other ones then the other copies are
2717 available through the doubly linked circular list. */
2719 nb_temps
= s
->nb_temps
;
2720 for (i
= 0; i
< nb_temps
; ++i
) {
2721 s
->temps
[i
].state_ptr
= NULL
;
2724 QTAILQ_FOREACH_SAFE(op
, &s
->ops
, link
, op_next
) {
2725 TCGOpcode opc
= op
->opc
;
2726 const TCGOpDef
*def
;
2729 /* Calls are special. */
2730 if (opc
== INDEX_op_call
) {
2731 fold_call(&ctx
, op
);
2735 def
= &tcg_op_defs
[opc
];
2736 init_arguments(&ctx
, op
, def
->nb_oargs
+ def
->nb_iargs
);
2737 copy_propagate(&ctx
, op
, def
->nb_oargs
, def
->nb_iargs
);
2739 /* Pre-compute the type of the operation. */
2740 if (def
->flags
& TCG_OPF_VECTOR
) {
2741 ctx
.type
= TCG_TYPE_V64
+ TCGOP_VECL(op
);
2742 } else if (def
->flags
& TCG_OPF_64BIT
) {
2743 ctx
.type
= TCG_TYPE_I64
;
2745 ctx
.type
= TCG_TYPE_I32
;
2748 /* Assume all bits affected, no bits known zero, no sign reps. */
2754 * Process each opcode.
2755 * Sorted alphabetically by opcode as much as possible.
2759 done
= fold_add(&ctx
, op
);
2761 case INDEX_op_add_vec
:
2762 done
= fold_add_vec(&ctx
, op
);
2764 CASE_OP_32_64(add2
):
2765 done
= fold_add2(&ctx
, op
);
2767 CASE_OP_32_64_VEC(and):
2768 done
= fold_and(&ctx
, op
);
2770 CASE_OP_32_64_VEC(andc
):
2771 done
= fold_andc(&ctx
, op
);
2773 CASE_OP_32_64(brcond
):
2774 done
= fold_brcond(&ctx
, op
);
2776 case INDEX_op_brcond2_i32
:
2777 done
= fold_brcond2(&ctx
, op
);
2779 CASE_OP_32_64(bswap16
):
2780 CASE_OP_32_64(bswap32
):
2781 case INDEX_op_bswap64_i64
:
2782 done
= fold_bswap(&ctx
, op
);
2786 done
= fold_count_zeros(&ctx
, op
);
2788 CASE_OP_32_64(ctpop
):
2789 done
= fold_ctpop(&ctx
, op
);
2791 CASE_OP_32_64(deposit
):
2792 done
= fold_deposit(&ctx
, op
);
2795 CASE_OP_32_64(divu
):
2796 done
= fold_divide(&ctx
, op
);
2798 case INDEX_op_dup_vec
:
2799 done
= fold_dup(&ctx
, op
);
2801 case INDEX_op_dup2_vec
:
2802 done
= fold_dup2(&ctx
, op
);
2804 CASE_OP_32_64_VEC(eqv
):
2805 done
= fold_eqv(&ctx
, op
);
2807 CASE_OP_32_64(extract
):
2808 done
= fold_extract(&ctx
, op
);
2810 CASE_OP_32_64(extract2
):
2811 done
= fold_extract2(&ctx
, op
);
2813 CASE_OP_32_64(ext8s
):
2814 CASE_OP_32_64(ext16s
):
2815 case INDEX_op_ext32s_i64
:
2816 case INDEX_op_ext_i32_i64
:
2817 done
= fold_exts(&ctx
, op
);
2819 CASE_OP_32_64(ext8u
):
2820 CASE_OP_32_64(ext16u
):
2821 case INDEX_op_ext32u_i64
:
2822 case INDEX_op_extu_i32_i64
:
2823 case INDEX_op_extrl_i64_i32
:
2824 case INDEX_op_extrh_i64_i32
:
2825 done
= fold_extu(&ctx
, op
);
2827 CASE_OP_32_64(ld8s
):
2828 CASE_OP_32_64(ld8u
):
2829 CASE_OP_32_64(ld16s
):
2830 CASE_OP_32_64(ld16u
):
2831 case INDEX_op_ld32s_i64
:
2832 case INDEX_op_ld32u_i64
:
2833 done
= fold_tcg_ld(&ctx
, op
);
2835 case INDEX_op_ld_i32
:
2836 case INDEX_op_ld_i64
:
2837 case INDEX_op_ld_vec
:
2838 done
= fold_tcg_ld_memcopy(&ctx
, op
);
2841 CASE_OP_32_64(st16
):
2842 case INDEX_op_st32_i64
:
2843 done
= fold_tcg_st(&ctx
, op
);
2845 case INDEX_op_st_i32
:
2846 case INDEX_op_st_i64
:
2847 case INDEX_op_st_vec
:
2848 done
= fold_tcg_st_memcopy(&ctx
, op
);
2851 done
= fold_mb(&ctx
, op
);
2853 CASE_OP_32_64_VEC(mov
):
2854 done
= fold_mov(&ctx
, op
);
2856 CASE_OP_32_64(movcond
):
2857 done
= fold_movcond(&ctx
, op
);
2860 done
= fold_mul(&ctx
, op
);
2862 CASE_OP_32_64(mulsh
):
2863 CASE_OP_32_64(muluh
):
2864 done
= fold_mul_highpart(&ctx
, op
);
2866 CASE_OP_32_64(muls2
):
2867 CASE_OP_32_64(mulu2
):
2868 done
= fold_multiply2(&ctx
, op
);
2870 CASE_OP_32_64_VEC(nand
):
2871 done
= fold_nand(&ctx
, op
);
2874 done
= fold_neg(&ctx
, op
);
2876 CASE_OP_32_64_VEC(nor
):
2877 done
= fold_nor(&ctx
, op
);
2879 CASE_OP_32_64_VEC(not):
2880 done
= fold_not(&ctx
, op
);
2882 CASE_OP_32_64_VEC(or):
2883 done
= fold_or(&ctx
, op
);
2885 CASE_OP_32_64_VEC(orc
):
2886 done
= fold_orc(&ctx
, op
);
2888 case INDEX_op_qemu_ld_a32_i32
:
2889 case INDEX_op_qemu_ld_a64_i32
:
2890 case INDEX_op_qemu_ld_a32_i64
:
2891 case INDEX_op_qemu_ld_a64_i64
:
2892 case INDEX_op_qemu_ld_a32_i128
:
2893 case INDEX_op_qemu_ld_a64_i128
:
2894 done
= fold_qemu_ld(&ctx
, op
);
2896 case INDEX_op_qemu_st8_a32_i32
:
2897 case INDEX_op_qemu_st8_a64_i32
:
2898 case INDEX_op_qemu_st_a32_i32
:
2899 case INDEX_op_qemu_st_a64_i32
:
2900 case INDEX_op_qemu_st_a32_i64
:
2901 case INDEX_op_qemu_st_a64_i64
:
2902 case INDEX_op_qemu_st_a32_i128
:
2903 case INDEX_op_qemu_st_a64_i128
:
2904 done
= fold_qemu_st(&ctx
, op
);
2907 CASE_OP_32_64(remu
):
2908 done
= fold_remainder(&ctx
, op
);
2910 CASE_OP_32_64(rotl
):
2911 CASE_OP_32_64(rotr
):
2915 done
= fold_shift(&ctx
, op
);
2917 CASE_OP_32_64(setcond
):
2918 done
= fold_setcond(&ctx
, op
);
2920 CASE_OP_32_64(negsetcond
):
2921 done
= fold_negsetcond(&ctx
, op
);
2923 case INDEX_op_setcond2_i32
:
2924 done
= fold_setcond2(&ctx
, op
);
2926 CASE_OP_32_64(sextract
):
2927 done
= fold_sextract(&ctx
, op
);
2930 done
= fold_sub(&ctx
, op
);
2932 case INDEX_op_sub_vec
:
2933 done
= fold_sub_vec(&ctx
, op
);
2935 CASE_OP_32_64(sub2
):
2936 done
= fold_sub2(&ctx
, op
);
2938 CASE_OP_32_64_VEC(xor):
2939 done
= fold_xor(&ctx
, op
);
2946 finish_folding(&ctx
, op
);