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 "tcg/tcg-op.h"
29 #include "tcg-internal.h"
31 #define CASE_OP_32_64(x) \
32 glue(glue(case INDEX_op_, x), _i32): \
33 glue(glue(case INDEX_op_, x), _i64)
35 #define CASE_OP_32_64_VEC(x) \
36 glue(glue(case INDEX_op_, x), _i32): \
37 glue(glue(case INDEX_op_, x), _i64): \
38 glue(glue(case INDEX_op_, x), _vec)
40 typedef struct TempOptInfo
{
45 uint64_t z_mask
; /* mask bit is 0 if and only if value bit is 0 */
46 uint64_t s_mask
; /* a left-aligned mask of clrsb(value) bits. */
49 typedef struct OptContext
{
52 TCGTempSet temps_used
;
54 /* In flight values from optimization. */
55 uint64_t a_mask
; /* mask bit is 0 iff value identical to first input */
56 uint64_t z_mask
; /* mask bit is 0 iff value bit is 0 */
57 uint64_t s_mask
; /* mask of clrsb(value) bits */
61 /* Calculate the smask for a specific value. */
62 static uint64_t smask_from_value(uint64_t value
)
64 int rep
= clrsb64(value
);
65 return ~(~0ull >> rep
);
69 * Calculate the smask for a given set of known-zeros.
70 * If there are lots of zeros on the left, we can consider the remainder
71 * an unsigned field, and thus the corresponding signed field is one bit
74 static uint64_t smask_from_zmask(uint64_t zmask
)
77 * Only the 0 bits are significant for zmask, thus the msb itself
78 * must be zero, else we have no sign information.
80 int rep
= clz64(zmask
);
85 return ~(~0ull >> rep
);
89 * Recreate a properly left-aligned smask after manipulation.
90 * Some bit-shuffling, particularly shifts and rotates, may
91 * retain sign bits on the left, but may scatter disconnected
92 * sign bits on the right. Retain only what remains to the left.
94 static uint64_t smask_from_smask(int64_t smask
)
96 /* Only the 1 bits are significant for smask */
97 return smask_from_zmask(~smask
);
100 static inline TempOptInfo
*ts_info(TCGTemp
*ts
)
102 return ts
->state_ptr
;
105 static inline TempOptInfo
*arg_info(TCGArg arg
)
107 return ts_info(arg_temp(arg
));
110 static inline bool ts_is_const(TCGTemp
*ts
)
112 return ts_info(ts
)->is_const
;
115 static inline bool arg_is_const(TCGArg arg
)
117 return ts_is_const(arg_temp(arg
));
120 static inline bool ts_is_copy(TCGTemp
*ts
)
122 return ts_info(ts
)->next_copy
!= ts
;
125 /* Reset TEMP's state, possibly removing the temp for the list of copies. */
126 static void reset_ts(TCGTemp
*ts
)
128 TempOptInfo
*ti
= ts_info(ts
);
129 TempOptInfo
*pi
= ts_info(ti
->prev_copy
);
130 TempOptInfo
*ni
= ts_info(ti
->next_copy
);
132 ni
->prev_copy
= ti
->prev_copy
;
133 pi
->next_copy
= ti
->next_copy
;
136 ti
->is_const
= false;
141 static void reset_temp(TCGArg arg
)
143 reset_ts(arg_temp(arg
));
146 /* Initialize and activate a temporary. */
147 static void init_ts_info(OptContext
*ctx
, TCGTemp
*ts
)
149 size_t idx
= temp_idx(ts
);
152 if (test_bit(idx
, ctx
->temps_used
.l
)) {
155 set_bit(idx
, ctx
->temps_used
.l
);
159 ti
= tcg_malloc(sizeof(TempOptInfo
));
165 if (ts
->kind
== TEMP_CONST
) {
168 ti
->z_mask
= ts
->val
;
169 ti
->s_mask
= smask_from_value(ts
->val
);
171 ti
->is_const
= false;
177 static TCGTemp
*find_better_copy(TCGContext
*s
, TCGTemp
*ts
)
181 /* If this is already readonly, we can't do better. */
182 if (temp_readonly(ts
)) {
187 for (i
= ts_info(ts
)->next_copy
; i
!= ts
; i
= ts_info(i
)->next_copy
) {
188 if (temp_readonly(i
)) {
190 } else if (i
->kind
> ts
->kind
) {
191 if (i
->kind
== TEMP_GLOBAL
) {
193 } else if (i
->kind
== TEMP_LOCAL
) {
199 /* If we didn't find a better representation, return the same temp. */
200 return g
? g
: l
? l
: ts
;
203 static bool ts_are_copies(TCGTemp
*ts1
, TCGTemp
*ts2
)
211 if (!ts_is_copy(ts1
) || !ts_is_copy(ts2
)) {
215 for (i
= ts_info(ts1
)->next_copy
; i
!= ts1
; i
= ts_info(i
)->next_copy
) {
224 static bool args_are_copies(TCGArg arg1
, TCGArg arg2
)
226 return ts_are_copies(arg_temp(arg1
), arg_temp(arg2
));
229 static bool tcg_opt_gen_mov(OptContext
*ctx
, TCGOp
*op
, TCGArg dst
, TCGArg src
)
231 TCGTemp
*dst_ts
= arg_temp(dst
);
232 TCGTemp
*src_ts
= arg_temp(src
);
237 if (ts_are_copies(dst_ts
, src_ts
)) {
238 tcg_op_remove(ctx
->tcg
, op
);
243 di
= ts_info(dst_ts
);
244 si
= ts_info(src_ts
);
248 new_op
= INDEX_op_mov_i32
;
251 new_op
= INDEX_op_mov_i64
;
256 /* TCGOP_VECL and TCGOP_VECE remain unchanged. */
257 new_op
= INDEX_op_mov_vec
;
260 g_assert_not_reached();
266 di
->z_mask
= si
->z_mask
;
267 di
->s_mask
= si
->s_mask
;
269 if (src_ts
->type
== dst_ts
->type
) {
270 TempOptInfo
*ni
= ts_info(si
->next_copy
);
272 di
->next_copy
= si
->next_copy
;
273 di
->prev_copy
= src_ts
;
274 ni
->prev_copy
= dst_ts
;
275 si
->next_copy
= dst_ts
;
276 di
->is_const
= si
->is_const
;
282 static bool tcg_opt_gen_movi(OptContext
*ctx
, TCGOp
*op
,
283 TCGArg dst
, uint64_t val
)
287 if (ctx
->type
== TCG_TYPE_I32
) {
291 /* Convert movi to mov with constant temp. */
292 tv
= tcg_constant_internal(ctx
->type
, val
);
293 init_ts_info(ctx
, tv
);
294 return tcg_opt_gen_mov(ctx
, op
, dst
, temp_arg(tv
));
297 static uint64_t do_constant_folding_2(TCGOpcode op
, uint64_t x
, uint64_t y
)
320 case INDEX_op_shl_i32
:
321 return (uint32_t)x
<< (y
& 31);
323 case INDEX_op_shl_i64
:
324 return (uint64_t)x
<< (y
& 63);
326 case INDEX_op_shr_i32
:
327 return (uint32_t)x
>> (y
& 31);
329 case INDEX_op_shr_i64
:
330 return (uint64_t)x
>> (y
& 63);
332 case INDEX_op_sar_i32
:
333 return (int32_t)x
>> (y
& 31);
335 case INDEX_op_sar_i64
:
336 return (int64_t)x
>> (y
& 63);
338 case INDEX_op_rotr_i32
:
339 return ror32(x
, y
& 31);
341 case INDEX_op_rotr_i64
:
342 return ror64(x
, y
& 63);
344 case INDEX_op_rotl_i32
:
345 return rol32(x
, y
& 31);
347 case INDEX_op_rotl_i64
:
348 return rol64(x
, y
& 63);
371 case INDEX_op_clz_i32
:
372 return (uint32_t)x
? clz32(x
) : y
;
374 case INDEX_op_clz_i64
:
375 return x
? clz64(x
) : y
;
377 case INDEX_op_ctz_i32
:
378 return (uint32_t)x
? ctz32(x
) : y
;
380 case INDEX_op_ctz_i64
:
381 return x
? ctz64(x
) : y
;
383 case INDEX_op_ctpop_i32
:
386 case INDEX_op_ctpop_i64
:
389 CASE_OP_32_64(ext8s
):
392 CASE_OP_32_64(ext16s
):
395 CASE_OP_32_64(ext8u
):
398 CASE_OP_32_64(ext16u
):
401 CASE_OP_32_64(bswap16
):
403 return y
& TCG_BSWAP_OS
? (int16_t)x
: x
;
405 CASE_OP_32_64(bswap32
):
407 return y
& TCG_BSWAP_OS
? (int32_t)x
: x
;
409 case INDEX_op_bswap64_i64
:
412 case INDEX_op_ext_i32_i64
:
413 case INDEX_op_ext32s_i64
:
416 case INDEX_op_extu_i32_i64
:
417 case INDEX_op_extrl_i64_i32
:
418 case INDEX_op_ext32u_i64
:
421 case INDEX_op_extrh_i64_i32
:
422 return (uint64_t)x
>> 32;
424 case INDEX_op_muluh_i32
:
425 return ((uint64_t)(uint32_t)x
* (uint32_t)y
) >> 32;
426 case INDEX_op_mulsh_i32
:
427 return ((int64_t)(int32_t)x
* (int32_t)y
) >> 32;
429 case INDEX_op_muluh_i64
:
430 mulu64(&l64
, &h64
, x
, y
);
432 case INDEX_op_mulsh_i64
:
433 muls64(&l64
, &h64
, x
, y
);
436 case INDEX_op_div_i32
:
437 /* Avoid crashing on divide by zero, otherwise undefined. */
438 return (int32_t)x
/ ((int32_t)y
? : 1);
439 case INDEX_op_divu_i32
:
440 return (uint32_t)x
/ ((uint32_t)y
? : 1);
441 case INDEX_op_div_i64
:
442 return (int64_t)x
/ ((int64_t)y
? : 1);
443 case INDEX_op_divu_i64
:
444 return (uint64_t)x
/ ((uint64_t)y
? : 1);
446 case INDEX_op_rem_i32
:
447 return (int32_t)x
% ((int32_t)y
? : 1);
448 case INDEX_op_remu_i32
:
449 return (uint32_t)x
% ((uint32_t)y
? : 1);
450 case INDEX_op_rem_i64
:
451 return (int64_t)x
% ((int64_t)y
? : 1);
452 case INDEX_op_remu_i64
:
453 return (uint64_t)x
% ((uint64_t)y
? : 1);
457 "Unrecognized operation %d in do_constant_folding.\n", op
);
462 static uint64_t do_constant_folding(TCGOpcode op
, TCGType type
,
463 uint64_t x
, uint64_t y
)
465 uint64_t res
= do_constant_folding_2(op
, x
, y
);
466 if (type
== TCG_TYPE_I32
) {
472 static bool do_constant_folding_cond_32(uint32_t x
, uint32_t y
, TCGCond c
)
480 return (int32_t)x
< (int32_t)y
;
482 return (int32_t)x
>= (int32_t)y
;
484 return (int32_t)x
<= (int32_t)y
;
486 return (int32_t)x
> (int32_t)y
;
500 static bool do_constant_folding_cond_64(uint64_t x
, uint64_t y
, TCGCond c
)
508 return (int64_t)x
< (int64_t)y
;
510 return (int64_t)x
>= (int64_t)y
;
512 return (int64_t)x
<= (int64_t)y
;
514 return (int64_t)x
> (int64_t)y
;
528 static bool do_constant_folding_cond_eq(TCGCond c
)
549 * Return -1 if the condition can't be simplified,
550 * and the result of the condition (0 or 1) if it can.
552 static int do_constant_folding_cond(TCGType type
, TCGArg x
,
555 uint64_t xv
= arg_info(x
)->val
;
556 uint64_t yv
= arg_info(y
)->val
;
558 if (arg_is_const(x
) && arg_is_const(y
)) {
561 return do_constant_folding_cond_32(xv
, yv
, c
);
563 return do_constant_folding_cond_64(xv
, yv
, c
);
565 /* Only scalar comparisons are optimizable */
568 } else if (args_are_copies(x
, y
)) {
569 return do_constant_folding_cond_eq(c
);
570 } else if (arg_is_const(y
) && yv
== 0) {
584 * Return -1 if the condition can't be simplified,
585 * and the result of the condition (0 or 1) if it can.
587 static int do_constant_folding_cond2(TCGArg
*p1
, TCGArg
*p2
, TCGCond c
)
589 TCGArg al
= p1
[0], ah
= p1
[1];
590 TCGArg bl
= p2
[0], bh
= p2
[1];
592 if (arg_is_const(bl
) && arg_is_const(bh
)) {
593 tcg_target_ulong blv
= arg_info(bl
)->val
;
594 tcg_target_ulong bhv
= arg_info(bh
)->val
;
595 uint64_t b
= deposit64(blv
, 32, 32, bhv
);
597 if (arg_is_const(al
) && arg_is_const(ah
)) {
598 tcg_target_ulong alv
= arg_info(al
)->val
;
599 tcg_target_ulong ahv
= arg_info(ah
)->val
;
600 uint64_t a
= deposit64(alv
, 32, 32, ahv
);
601 return do_constant_folding_cond_64(a
, b
, c
);
614 if (args_are_copies(al
, bl
) && args_are_copies(ah
, bh
)) {
615 return do_constant_folding_cond_eq(c
);
622 * @dest: TCGArg of the destination argument, or NO_DEST.
623 * @p1: first paired argument
624 * @p2: second paired argument
626 * If *@p1 is a constant and *@p2 is not, swap.
627 * If *@p2 matches @dest, swap.
628 * Return true if a swap was performed.
631 #define NO_DEST temp_arg(NULL)
633 static bool swap_commutative(TCGArg dest
, TCGArg
*p1
, TCGArg
*p2
)
635 TCGArg a1
= *p1
, a2
= *p2
;
637 sum
+= arg_is_const(a1
);
638 sum
-= arg_is_const(a2
);
640 /* Prefer the constant in second argument, and then the form
641 op a, a, b, which is better handled on non-RISC hosts. */
642 if (sum
> 0 || (sum
== 0 && dest
== a2
)) {
650 static bool swap_commutative2(TCGArg
*p1
, TCGArg
*p2
)
653 sum
+= arg_is_const(p1
[0]);
654 sum
+= arg_is_const(p1
[1]);
655 sum
-= arg_is_const(p2
[0]);
656 sum
-= arg_is_const(p2
[1]);
659 t
= p1
[0], p1
[0] = p2
[0], p2
[0] = t
;
660 t
= p1
[1], p1
[1] = p2
[1], p2
[1] = t
;
666 static void init_arguments(OptContext
*ctx
, TCGOp
*op
, int nb_args
)
668 for (int i
= 0; i
< nb_args
; i
++) {
669 TCGTemp
*ts
= arg_temp(op
->args
[i
]);
671 init_ts_info(ctx
, ts
);
676 static void copy_propagate(OptContext
*ctx
, TCGOp
*op
,
677 int nb_oargs
, int nb_iargs
)
679 TCGContext
*s
= ctx
->tcg
;
681 for (int i
= nb_oargs
; i
< nb_oargs
+ nb_iargs
; i
++) {
682 TCGTemp
*ts
= arg_temp(op
->args
[i
]);
683 if (ts
&& ts_is_copy(ts
)) {
684 op
->args
[i
] = temp_arg(find_better_copy(s
, ts
));
689 static void finish_folding(OptContext
*ctx
, TCGOp
*op
)
691 const TCGOpDef
*def
= &tcg_op_defs
[op
->opc
];
695 * For an opcode that ends a BB, reset all temp data.
696 * We do no cross-BB optimization.
698 if (def
->flags
& TCG_OPF_BB_END
) {
699 memset(&ctx
->temps_used
, 0, sizeof(ctx
->temps_used
));
704 nb_oargs
= def
->nb_oargs
;
705 for (i
= 0; i
< nb_oargs
; i
++) {
706 TCGTemp
*ts
= arg_temp(op
->args
[i
]);
709 * Save the corresponding known-zero/sign bits mask for the
710 * first output argument (only one supported so far).
713 ts_info(ts
)->z_mask
= ctx
->z_mask
;
714 ts_info(ts
)->s_mask
= ctx
->s_mask
;
720 * The fold_* functions return true when processing is complete,
721 * usually by folding the operation to a constant or to a copy,
722 * and calling tcg_opt_gen_{mov,movi}. They may do other things,
723 * like collect information about the value produced, for use in
724 * optimizing a subsequent operation.
726 * These first fold_* functions are all helpers, used by other
727 * folders for more specific operations.
730 static bool fold_const1(OptContext
*ctx
, TCGOp
*op
)
732 if (arg_is_const(op
->args
[1])) {
735 t
= arg_info(op
->args
[1])->val
;
736 t
= do_constant_folding(op
->opc
, ctx
->type
, t
, 0);
737 return tcg_opt_gen_movi(ctx
, op
, op
->args
[0], t
);
742 static bool fold_const2(OptContext
*ctx
, TCGOp
*op
)
744 if (arg_is_const(op
->args
[1]) && arg_is_const(op
->args
[2])) {
745 uint64_t t1
= arg_info(op
->args
[1])->val
;
746 uint64_t t2
= arg_info(op
->args
[2])->val
;
748 t1
= do_constant_folding(op
->opc
, ctx
->type
, t1
, t2
);
749 return tcg_opt_gen_movi(ctx
, op
, op
->args
[0], t1
);
754 static bool fold_const2_commutative(OptContext
*ctx
, TCGOp
*op
)
756 swap_commutative(op
->args
[0], &op
->args
[1], &op
->args
[2]);
757 return fold_const2(ctx
, op
);
760 static bool fold_masks(OptContext
*ctx
, TCGOp
*op
)
762 uint64_t a_mask
= ctx
->a_mask
;
763 uint64_t z_mask
= ctx
->z_mask
;
764 uint64_t s_mask
= ctx
->s_mask
;
767 * 32-bit ops generate 32-bit results, which for the purpose of
768 * simplifying tcg are sign-extended. Certainly that's how we
769 * represent our constants elsewhere. Note that the bits will
770 * be reset properly for a 64-bit value when encountering the
771 * type changing opcodes.
773 if (ctx
->type
== TCG_TYPE_I32
) {
774 a_mask
= (int32_t)a_mask
;
775 z_mask
= (int32_t)z_mask
;
776 s_mask
|= MAKE_64BIT_MASK(32, 32);
777 ctx
->z_mask
= z_mask
;
778 ctx
->s_mask
= s_mask
;
782 return tcg_opt_gen_movi(ctx
, op
, op
->args
[0], 0);
785 return tcg_opt_gen_mov(ctx
, op
, op
->args
[0], op
->args
[1]);
791 * Convert @op to NOT, if NOT is supported by the host.
792 * Return true f the conversion is successful, which will still
793 * indicate that the processing is complete.
795 static bool fold_not(OptContext
*ctx
, TCGOp
*op
);
796 static bool fold_to_not(OptContext
*ctx
, TCGOp
*op
, int idx
)
803 not_op
= INDEX_op_not_i32
;
804 have_not
= TCG_TARGET_HAS_not_i32
;
807 not_op
= INDEX_op_not_i64
;
808 have_not
= TCG_TARGET_HAS_not_i64
;
813 not_op
= INDEX_op_not_vec
;
814 have_not
= TCG_TARGET_HAS_not_vec
;
817 g_assert_not_reached();
821 op
->args
[1] = op
->args
[idx
];
822 return fold_not(ctx
, op
);
827 /* If the binary operation has first argument @i, fold to @i. */
828 static bool fold_ix_to_i(OptContext
*ctx
, TCGOp
*op
, uint64_t i
)
830 if (arg_is_const(op
->args
[1]) && arg_info(op
->args
[1])->val
== i
) {
831 return tcg_opt_gen_movi(ctx
, op
, op
->args
[0], i
);
836 /* If the binary operation has first argument @i, fold to NOT. */
837 static bool fold_ix_to_not(OptContext
*ctx
, TCGOp
*op
, uint64_t i
)
839 if (arg_is_const(op
->args
[1]) && arg_info(op
->args
[1])->val
== i
) {
840 return fold_to_not(ctx
, op
, 2);
845 /* If the binary operation has second argument @i, fold to @i. */
846 static bool fold_xi_to_i(OptContext
*ctx
, TCGOp
*op
, uint64_t i
)
848 if (arg_is_const(op
->args
[2]) && arg_info(op
->args
[2])->val
== i
) {
849 return tcg_opt_gen_movi(ctx
, op
, op
->args
[0], i
);
854 /* If the binary operation has second argument @i, fold to identity. */
855 static bool fold_xi_to_x(OptContext
*ctx
, TCGOp
*op
, uint64_t i
)
857 if (arg_is_const(op
->args
[2]) && arg_info(op
->args
[2])->val
== i
) {
858 return tcg_opt_gen_mov(ctx
, op
, op
->args
[0], op
->args
[1]);
863 /* If the binary operation has second argument @i, fold to NOT. */
864 static bool fold_xi_to_not(OptContext
*ctx
, TCGOp
*op
, uint64_t i
)
866 if (arg_is_const(op
->args
[2]) && arg_info(op
->args
[2])->val
== i
) {
867 return fold_to_not(ctx
, op
, 1);
872 /* If the binary operation has both arguments equal, fold to @i. */
873 static bool fold_xx_to_i(OptContext
*ctx
, TCGOp
*op
, uint64_t i
)
875 if (args_are_copies(op
->args
[1], op
->args
[2])) {
876 return tcg_opt_gen_movi(ctx
, op
, op
->args
[0], i
);
881 /* If the binary operation has both arguments equal, fold to identity. */
882 static bool fold_xx_to_x(OptContext
*ctx
, TCGOp
*op
)
884 if (args_are_copies(op
->args
[1], op
->args
[2])) {
885 return tcg_opt_gen_mov(ctx
, op
, op
->args
[0], op
->args
[1]);
891 * These outermost fold_<op> functions are sorted alphabetically.
893 * The ordering of the transformations should be:
894 * 1) those that produce a constant
895 * 2) those that produce a copy
896 * 3) those that produce information about the result value.
899 static bool fold_add(OptContext
*ctx
, TCGOp
*op
)
901 if (fold_const2_commutative(ctx
, op
) ||
902 fold_xi_to_x(ctx
, op
, 0)) {
908 static bool fold_addsub2(OptContext
*ctx
, TCGOp
*op
, bool add
)
910 if (arg_is_const(op
->args
[2]) && arg_is_const(op
->args
[3]) &&
911 arg_is_const(op
->args
[4]) && arg_is_const(op
->args
[5])) {
912 uint64_t al
= arg_info(op
->args
[2])->val
;
913 uint64_t ah
= arg_info(op
->args
[3])->val
;
914 uint64_t bl
= arg_info(op
->args
[4])->val
;
915 uint64_t bh
= arg_info(op
->args
[5])->val
;
919 if (ctx
->type
== TCG_TYPE_I32
) {
920 uint64_t a
= deposit64(al
, 32, 32, ah
);
921 uint64_t b
= deposit64(bl
, 32, 32, bh
);
929 al
= sextract64(a
, 0, 32);
930 ah
= sextract64(a
, 32, 32);
932 Int128 a
= int128_make128(al
, ah
);
933 Int128 b
= int128_make128(bl
, bh
);
936 a
= int128_add(a
, b
);
938 a
= int128_sub(a
, b
);
941 al
= int128_getlo(a
);
942 ah
= int128_gethi(a
);
948 /* The proper opcode is supplied by tcg_opt_gen_mov. */
949 op2
= tcg_op_insert_before(ctx
->tcg
, op
, 0);
951 tcg_opt_gen_movi(ctx
, op
, rl
, al
);
952 tcg_opt_gen_movi(ctx
, op2
, rh
, ah
);
958 static bool fold_add2(OptContext
*ctx
, TCGOp
*op
)
960 /* Note that the high and low parts may be independently swapped. */
961 swap_commutative(op
->args
[0], &op
->args
[2], &op
->args
[4]);
962 swap_commutative(op
->args
[1], &op
->args
[3], &op
->args
[5]);
964 return fold_addsub2(ctx
, op
, true);
967 static bool fold_and(OptContext
*ctx
, TCGOp
*op
)
971 if (fold_const2_commutative(ctx
, op
) ||
972 fold_xi_to_i(ctx
, op
, 0) ||
973 fold_xi_to_x(ctx
, op
, -1) ||
974 fold_xx_to_x(ctx
, op
)) {
978 z1
= arg_info(op
->args
[1])->z_mask
;
979 z2
= arg_info(op
->args
[2])->z_mask
;
980 ctx
->z_mask
= z1
& z2
;
983 * Sign repetitions are perforce all identical, whether they are 1 or 0.
984 * Bitwise operations preserve the relative quantity of the repetitions.
986 ctx
->s_mask
= arg_info(op
->args
[1])->s_mask
987 & arg_info(op
->args
[2])->s_mask
;
990 * Known-zeros does not imply known-ones. Therefore unless
991 * arg2 is constant, we can't infer affected bits from it.
993 if (arg_is_const(op
->args
[2])) {
994 ctx
->a_mask
= z1
& ~z2
;
997 return fold_masks(ctx
, op
);
1000 static bool fold_andc(OptContext
*ctx
, TCGOp
*op
)
1004 if (fold_const2(ctx
, op
) ||
1005 fold_xx_to_i(ctx
, op
, 0) ||
1006 fold_xi_to_x(ctx
, op
, 0) ||
1007 fold_ix_to_not(ctx
, op
, -1)) {
1011 z1
= arg_info(op
->args
[1])->z_mask
;
1014 * Known-zeros does not imply known-ones. Therefore unless
1015 * arg2 is constant, we can't infer anything from it.
1017 if (arg_is_const(op
->args
[2])) {
1018 uint64_t z2
= ~arg_info(op
->args
[2])->z_mask
;
1019 ctx
->a_mask
= z1
& ~z2
;
1024 ctx
->s_mask
= arg_info(op
->args
[1])->s_mask
1025 & arg_info(op
->args
[2])->s_mask
;
1026 return fold_masks(ctx
, op
);
1029 static bool fold_brcond(OptContext
*ctx
, TCGOp
*op
)
1031 TCGCond cond
= op
->args
[2];
1034 if (swap_commutative(NO_DEST
, &op
->args
[0], &op
->args
[1])) {
1035 op
->args
[2] = cond
= tcg_swap_cond(cond
);
1038 i
= do_constant_folding_cond(ctx
->type
, op
->args
[0], op
->args
[1], cond
);
1040 tcg_op_remove(ctx
->tcg
, op
);
1044 op
->opc
= INDEX_op_br
;
1045 op
->args
[0] = op
->args
[3];
1050 static bool fold_brcond2(OptContext
*ctx
, TCGOp
*op
)
1052 TCGCond cond
= op
->args
[4];
1053 TCGArg label
= op
->args
[5];
1056 if (swap_commutative2(&op
->args
[0], &op
->args
[2])) {
1057 op
->args
[4] = cond
= tcg_swap_cond(cond
);
1060 i
= do_constant_folding_cond2(&op
->args
[0], &op
->args
[2], cond
);
1062 goto do_brcond_const
;
1069 * Simplify LT/GE comparisons vs zero to a single compare
1070 * vs the high word of the input.
1072 if (arg_is_const(op
->args
[2]) && arg_info(op
->args
[2])->val
== 0 &&
1073 arg_is_const(op
->args
[3]) && arg_info(op
->args
[3])->val
== 0) {
1074 goto do_brcond_high
;
1083 * Simplify EQ/NE comparisons where one of the pairs
1084 * can be simplified.
1086 i
= do_constant_folding_cond(TCG_TYPE_I32
, op
->args
[0],
1090 goto do_brcond_const
;
1092 goto do_brcond_high
;
1095 i
= do_constant_folding_cond(TCG_TYPE_I32
, op
->args
[1],
1099 goto do_brcond_const
;
1101 op
->opc
= INDEX_op_brcond_i32
;
1102 op
->args
[1] = op
->args
[2];
1104 op
->args
[3] = label
;
1113 op
->opc
= INDEX_op_brcond_i32
;
1114 op
->args
[0] = op
->args
[1];
1115 op
->args
[1] = op
->args
[3];
1117 op
->args
[3] = label
;
1122 tcg_op_remove(ctx
->tcg
, op
);
1125 op
->opc
= INDEX_op_br
;
1126 op
->args
[0] = label
;
1132 static bool fold_bswap(OptContext
*ctx
, TCGOp
*op
)
1134 uint64_t z_mask
, s_mask
, sign
;
1136 if (arg_is_const(op
->args
[1])) {
1137 uint64_t t
= arg_info(op
->args
[1])->val
;
1139 t
= do_constant_folding(op
->opc
, ctx
->type
, t
, op
->args
[2]);
1140 return tcg_opt_gen_movi(ctx
, op
, op
->args
[0], t
);
1143 z_mask
= arg_info(op
->args
[1])->z_mask
;
1146 case INDEX_op_bswap16_i32
:
1147 case INDEX_op_bswap16_i64
:
1148 z_mask
= bswap16(z_mask
);
1151 case INDEX_op_bswap32_i32
:
1152 case INDEX_op_bswap32_i64
:
1153 z_mask
= bswap32(z_mask
);
1156 case INDEX_op_bswap64_i64
:
1157 z_mask
= bswap64(z_mask
);
1161 g_assert_not_reached();
1163 s_mask
= smask_from_zmask(z_mask
);
1165 switch (op
->args
[2] & (TCG_BSWAP_OZ
| TCG_BSWAP_OS
)) {
1169 /* If the sign bit may be 1, force all the bits above to 1. */
1170 if (z_mask
& sign
) {
1176 /* The high bits are undefined: force all bits above the sign to 1. */
1177 z_mask
|= sign
<< 1;
1181 ctx
->z_mask
= z_mask
;
1182 ctx
->s_mask
= s_mask
;
1184 return fold_masks(ctx
, op
);
1187 static bool fold_call(OptContext
*ctx
, TCGOp
*op
)
1189 TCGContext
*s
= ctx
->tcg
;
1190 int nb_oargs
= TCGOP_CALLO(op
);
1191 int nb_iargs
= TCGOP_CALLI(op
);
1194 init_arguments(ctx
, op
, nb_oargs
+ nb_iargs
);
1195 copy_propagate(ctx
, op
, nb_oargs
, nb_iargs
);
1197 /* If the function reads or writes globals, reset temp data. */
1198 flags
= tcg_call_flags(op
);
1199 if (!(flags
& (TCG_CALL_NO_READ_GLOBALS
| TCG_CALL_NO_WRITE_GLOBALS
))) {
1200 int nb_globals
= s
->nb_globals
;
1202 for (i
= 0; i
< nb_globals
; i
++) {
1203 if (test_bit(i
, ctx
->temps_used
.l
)) {
1204 reset_ts(&ctx
->tcg
->temps
[i
]);
1209 /* Reset temp data for outputs. */
1210 for (i
= 0; i
< nb_oargs
; i
++) {
1211 reset_temp(op
->args
[i
]);
1214 /* Stop optimizing MB across calls. */
1215 ctx
->prev_mb
= NULL
;
1219 static bool fold_count_zeros(OptContext
*ctx
, TCGOp
*op
)
1223 if (arg_is_const(op
->args
[1])) {
1224 uint64_t t
= arg_info(op
->args
[1])->val
;
1227 t
= do_constant_folding(op
->opc
, ctx
->type
, t
, 0);
1228 return tcg_opt_gen_movi(ctx
, op
, op
->args
[0], t
);
1230 return tcg_opt_gen_mov(ctx
, op
, op
->args
[0], op
->args
[2]);
1233 switch (ctx
->type
) {
1241 g_assert_not_reached();
1243 ctx
->z_mask
= arg_info(op
->args
[2])->z_mask
| z_mask
;
1244 ctx
->s_mask
= smask_from_zmask(ctx
->z_mask
);
1248 static bool fold_ctpop(OptContext
*ctx
, TCGOp
*op
)
1250 if (fold_const1(ctx
, op
)) {
1254 switch (ctx
->type
) {
1256 ctx
->z_mask
= 32 | 31;
1259 ctx
->z_mask
= 64 | 63;
1262 g_assert_not_reached();
1264 ctx
->s_mask
= smask_from_zmask(ctx
->z_mask
);
1268 static bool fold_deposit(OptContext
*ctx
, TCGOp
*op
)
1270 if (arg_is_const(op
->args
[1]) && arg_is_const(op
->args
[2])) {
1271 uint64_t t1
= arg_info(op
->args
[1])->val
;
1272 uint64_t t2
= arg_info(op
->args
[2])->val
;
1274 t1
= deposit64(t1
, op
->args
[3], op
->args
[4], t2
);
1275 return tcg_opt_gen_movi(ctx
, op
, op
->args
[0], t1
);
1278 ctx
->z_mask
= deposit64(arg_info(op
->args
[1])->z_mask
,
1279 op
->args
[3], op
->args
[4],
1280 arg_info(op
->args
[2])->z_mask
);
1284 static bool fold_divide(OptContext
*ctx
, TCGOp
*op
)
1286 if (fold_const2(ctx
, op
) ||
1287 fold_xi_to_x(ctx
, op
, 1)) {
1293 static bool fold_dup(OptContext
*ctx
, TCGOp
*op
)
1295 if (arg_is_const(op
->args
[1])) {
1296 uint64_t t
= arg_info(op
->args
[1])->val
;
1297 t
= dup_const(TCGOP_VECE(op
), t
);
1298 return tcg_opt_gen_movi(ctx
, op
, op
->args
[0], t
);
1303 static bool fold_dup2(OptContext
*ctx
, TCGOp
*op
)
1305 if (arg_is_const(op
->args
[1]) && arg_is_const(op
->args
[2])) {
1306 uint64_t t
= deposit64(arg_info(op
->args
[1])->val
, 32, 32,
1307 arg_info(op
->args
[2])->val
);
1308 return tcg_opt_gen_movi(ctx
, op
, op
->args
[0], t
);
1311 if (args_are_copies(op
->args
[1], op
->args
[2])) {
1312 op
->opc
= INDEX_op_dup_vec
;
1313 TCGOP_VECE(op
) = MO_32
;
1318 static bool fold_eqv(OptContext
*ctx
, TCGOp
*op
)
1320 if (fold_const2_commutative(ctx
, op
) ||
1321 fold_xi_to_x(ctx
, op
, -1) ||
1322 fold_xi_to_not(ctx
, op
, 0)) {
1326 ctx
->s_mask
= arg_info(op
->args
[1])->s_mask
1327 & arg_info(op
->args
[2])->s_mask
;
1331 static bool fold_extract(OptContext
*ctx
, TCGOp
*op
)
1333 uint64_t z_mask_old
, z_mask
;
1334 int pos
= op
->args
[2];
1335 int len
= op
->args
[3];
1337 if (arg_is_const(op
->args
[1])) {
1340 t
= arg_info(op
->args
[1])->val
;
1341 t
= extract64(t
, pos
, len
);
1342 return tcg_opt_gen_movi(ctx
, op
, op
->args
[0], t
);
1345 z_mask_old
= arg_info(op
->args
[1])->z_mask
;
1346 z_mask
= extract64(z_mask_old
, pos
, len
);
1348 ctx
->a_mask
= z_mask_old
^ z_mask
;
1350 ctx
->z_mask
= z_mask
;
1351 ctx
->s_mask
= smask_from_zmask(z_mask
);
1353 return fold_masks(ctx
, op
);
1356 static bool fold_extract2(OptContext
*ctx
, TCGOp
*op
)
1358 if (arg_is_const(op
->args
[1]) && arg_is_const(op
->args
[2])) {
1359 uint64_t v1
= arg_info(op
->args
[1])->val
;
1360 uint64_t v2
= arg_info(op
->args
[2])->val
;
1361 int shr
= op
->args
[3];
1363 if (op
->opc
== INDEX_op_extract2_i64
) {
1367 v1
= (uint32_t)v1
>> shr
;
1368 v2
= (uint64_t)((int32_t)v2
<< (32 - shr
));
1370 return tcg_opt_gen_movi(ctx
, op
, op
->args
[0], v1
| v2
);
1375 static bool fold_exts(OptContext
*ctx
, TCGOp
*op
)
1377 uint64_t s_mask_old
, s_mask
, z_mask
, sign
;
1378 bool type_change
= false;
1380 if (fold_const1(ctx
, op
)) {
1384 z_mask
= arg_info(op
->args
[1])->z_mask
;
1385 s_mask
= arg_info(op
->args
[1])->s_mask
;
1386 s_mask_old
= s_mask
;
1389 CASE_OP_32_64(ext8s
):
1391 z_mask
= (uint8_t)z_mask
;
1393 CASE_OP_32_64(ext16s
):
1395 z_mask
= (uint16_t)z_mask
;
1397 case INDEX_op_ext_i32_i64
:
1400 case INDEX_op_ext32s_i64
:
1402 z_mask
= (uint32_t)z_mask
;
1405 g_assert_not_reached();
1408 if (z_mask
& sign
) {
1411 s_mask
|= sign
<< 1;
1413 ctx
->z_mask
= z_mask
;
1414 ctx
->s_mask
= s_mask
;
1416 ctx
->a_mask
= s_mask
& ~s_mask_old
;
1419 return fold_masks(ctx
, op
);
1422 static bool fold_extu(OptContext
*ctx
, TCGOp
*op
)
1424 uint64_t z_mask_old
, z_mask
;
1425 bool type_change
= false;
1427 if (fold_const1(ctx
, op
)) {
1431 z_mask_old
= z_mask
= arg_info(op
->args
[1])->z_mask
;
1434 CASE_OP_32_64(ext8u
):
1435 z_mask
= (uint8_t)z_mask
;
1437 CASE_OP_32_64(ext16u
):
1438 z_mask
= (uint16_t)z_mask
;
1440 case INDEX_op_extrl_i64_i32
:
1441 case INDEX_op_extu_i32_i64
:
1444 case INDEX_op_ext32u_i64
:
1445 z_mask
= (uint32_t)z_mask
;
1447 case INDEX_op_extrh_i64_i32
:
1452 g_assert_not_reached();
1455 ctx
->z_mask
= z_mask
;
1456 ctx
->s_mask
= smask_from_zmask(z_mask
);
1458 ctx
->a_mask
= z_mask_old
^ z_mask
;
1460 return fold_masks(ctx
, op
);
1463 static bool fold_mb(OptContext
*ctx
, TCGOp
*op
)
1465 /* Eliminate duplicate and redundant fence instructions. */
1468 * Merge two barriers of the same type into one,
1469 * or a weaker barrier into a stronger one,
1470 * or two weaker barriers into a stronger one.
1471 * mb X; mb Y => mb X|Y
1472 * mb; strl => mb; st
1473 * ldaq; mb => ld; mb
1474 * ldaq; strl => ld; mb; st
1475 * Other combinations are also merged into a strong
1476 * barrier. This is stricter than specified but for
1477 * the purposes of TCG is better than not optimizing.
1479 ctx
->prev_mb
->args
[0] |= op
->args
[0];
1480 tcg_op_remove(ctx
->tcg
, op
);
1487 static bool fold_mov(OptContext
*ctx
, TCGOp
*op
)
1489 return tcg_opt_gen_mov(ctx
, op
, op
->args
[0], op
->args
[1]);
1492 static bool fold_movcond(OptContext
*ctx
, TCGOp
*op
)
1494 TCGCond cond
= op
->args
[5];
1497 if (swap_commutative(NO_DEST
, &op
->args
[1], &op
->args
[2])) {
1498 op
->args
[5] = cond
= tcg_swap_cond(cond
);
1501 * Canonicalize the "false" input reg to match the destination reg so
1502 * that the tcg backend can implement a "move if true" operation.
1504 if (swap_commutative(op
->args
[0], &op
->args
[4], &op
->args
[3])) {
1505 op
->args
[5] = cond
= tcg_invert_cond(cond
);
1508 i
= do_constant_folding_cond(ctx
->type
, op
->args
[1], op
->args
[2], cond
);
1510 return tcg_opt_gen_mov(ctx
, op
, op
->args
[0], op
->args
[4 - i
]);
1513 ctx
->z_mask
= arg_info(op
->args
[3])->z_mask
1514 | arg_info(op
->args
[4])->z_mask
;
1515 ctx
->s_mask
= arg_info(op
->args
[3])->s_mask
1516 & arg_info(op
->args
[4])->s_mask
;
1518 if (arg_is_const(op
->args
[3]) && arg_is_const(op
->args
[4])) {
1519 uint64_t tv
= arg_info(op
->args
[3])->val
;
1520 uint64_t fv
= arg_info(op
->args
[4])->val
;
1523 switch (ctx
->type
) {
1525 opc
= INDEX_op_setcond_i32
;
1528 opc
= INDEX_op_setcond_i64
;
1531 g_assert_not_reached();
1534 if (tv
== 1 && fv
== 0) {
1537 } else if (fv
== 1 && tv
== 0) {
1539 op
->args
[3] = tcg_invert_cond(cond
);
1545 static bool fold_mul(OptContext
*ctx
, TCGOp
*op
)
1547 if (fold_const2(ctx
, op
) ||
1548 fold_xi_to_i(ctx
, op
, 0) ||
1549 fold_xi_to_x(ctx
, op
, 1)) {
1555 static bool fold_mul_highpart(OptContext
*ctx
, TCGOp
*op
)
1557 if (fold_const2_commutative(ctx
, op
) ||
1558 fold_xi_to_i(ctx
, op
, 0)) {
1564 static bool fold_multiply2(OptContext
*ctx
, TCGOp
*op
)
1566 swap_commutative(op
->args
[0], &op
->args
[2], &op
->args
[3]);
1568 if (arg_is_const(op
->args
[2]) && arg_is_const(op
->args
[3])) {
1569 uint64_t a
= arg_info(op
->args
[2])->val
;
1570 uint64_t b
= arg_info(op
->args
[3])->val
;
1576 case INDEX_op_mulu2_i32
:
1577 l
= (uint64_t)(uint32_t)a
* (uint32_t)b
;
1578 h
= (int32_t)(l
>> 32);
1581 case INDEX_op_muls2_i32
:
1582 l
= (int64_t)(int32_t)a
* (int32_t)b
;
1586 case INDEX_op_mulu2_i64
:
1587 mulu64(&l
, &h
, a
, b
);
1589 case INDEX_op_muls2_i64
:
1590 muls64(&l
, &h
, a
, b
);
1593 g_assert_not_reached();
1599 /* The proper opcode is supplied by tcg_opt_gen_mov. */
1600 op2
= tcg_op_insert_before(ctx
->tcg
, op
, 0);
1602 tcg_opt_gen_movi(ctx
, op
, rl
, l
);
1603 tcg_opt_gen_movi(ctx
, op2
, rh
, h
);
1609 static bool fold_nand(OptContext
*ctx
, TCGOp
*op
)
1611 if (fold_const2_commutative(ctx
, op
) ||
1612 fold_xi_to_not(ctx
, op
, -1)) {
1616 ctx
->s_mask
= arg_info(op
->args
[1])->s_mask
1617 & arg_info(op
->args
[2])->s_mask
;
1621 static bool fold_neg(OptContext
*ctx
, TCGOp
*op
)
1625 if (fold_const1(ctx
, op
)) {
1629 /* Set to 1 all bits to the left of the rightmost. */
1630 z_mask
= arg_info(op
->args
[1])->z_mask
;
1631 ctx
->z_mask
= -(z_mask
& -z_mask
);
1634 * Because of fold_sub_to_neg, we want to always return true,
1635 * via finish_folding.
1637 finish_folding(ctx
, op
);
1641 static bool fold_nor(OptContext
*ctx
, TCGOp
*op
)
1643 if (fold_const2_commutative(ctx
, op
) ||
1644 fold_xi_to_not(ctx
, op
, 0)) {
1648 ctx
->s_mask
= arg_info(op
->args
[1])->s_mask
1649 & arg_info(op
->args
[2])->s_mask
;
1653 static bool fold_not(OptContext
*ctx
, TCGOp
*op
)
1655 if (fold_const1(ctx
, op
)) {
1659 ctx
->s_mask
= arg_info(op
->args
[1])->s_mask
;
1661 /* Because of fold_to_not, we want to always return true, via finish. */
1662 finish_folding(ctx
, op
);
1666 static bool fold_or(OptContext
*ctx
, TCGOp
*op
)
1668 if (fold_const2_commutative(ctx
, op
) ||
1669 fold_xi_to_x(ctx
, op
, 0) ||
1670 fold_xx_to_x(ctx
, op
)) {
1674 ctx
->z_mask
= arg_info(op
->args
[1])->z_mask
1675 | arg_info(op
->args
[2])->z_mask
;
1676 ctx
->s_mask
= arg_info(op
->args
[1])->s_mask
1677 & arg_info(op
->args
[2])->s_mask
;
1678 return fold_masks(ctx
, op
);
1681 static bool fold_orc(OptContext
*ctx
, TCGOp
*op
)
1683 if (fold_const2(ctx
, op
) ||
1684 fold_xx_to_i(ctx
, op
, -1) ||
1685 fold_xi_to_x(ctx
, op
, -1) ||
1686 fold_ix_to_not(ctx
, op
, 0)) {
1690 ctx
->s_mask
= arg_info(op
->args
[1])->s_mask
1691 & arg_info(op
->args
[2])->s_mask
;
1695 static bool fold_qemu_ld(OptContext
*ctx
, TCGOp
*op
)
1697 const TCGOpDef
*def
= &tcg_op_defs
[op
->opc
];
1698 MemOpIdx oi
= op
->args
[def
->nb_oargs
+ def
->nb_iargs
];
1699 MemOp mop
= get_memop(oi
);
1700 int width
= 8 * memop_size(mop
);
1703 ctx
->s_mask
= MAKE_64BIT_MASK(width
, 64 - width
);
1704 if (!(mop
& MO_SIGN
)) {
1705 ctx
->z_mask
= MAKE_64BIT_MASK(0, width
);
1710 /* Opcodes that touch guest memory stop the mb optimization. */
1711 ctx
->prev_mb
= NULL
;
1715 static bool fold_qemu_st(OptContext
*ctx
, TCGOp
*op
)
1717 /* Opcodes that touch guest memory stop the mb optimization. */
1718 ctx
->prev_mb
= NULL
;
1722 static bool fold_remainder(OptContext
*ctx
, TCGOp
*op
)
1724 if (fold_const2(ctx
, op
) ||
1725 fold_xx_to_i(ctx
, op
, 0)) {
1731 static bool fold_setcond(OptContext
*ctx
, TCGOp
*op
)
1733 TCGCond cond
= op
->args
[3];
1736 if (swap_commutative(op
->args
[0], &op
->args
[1], &op
->args
[2])) {
1737 op
->args
[3] = cond
= tcg_swap_cond(cond
);
1740 i
= do_constant_folding_cond(ctx
->type
, op
->args
[1], op
->args
[2], cond
);
1742 return tcg_opt_gen_movi(ctx
, op
, op
->args
[0], i
);
1746 ctx
->s_mask
= smask_from_zmask(1);
1750 static bool fold_setcond2(OptContext
*ctx
, TCGOp
*op
)
1752 TCGCond cond
= op
->args
[5];
1755 if (swap_commutative2(&op
->args
[1], &op
->args
[3])) {
1756 op
->args
[5] = cond
= tcg_swap_cond(cond
);
1759 i
= do_constant_folding_cond2(&op
->args
[1], &op
->args
[3], cond
);
1761 goto do_setcond_const
;
1768 * Simplify LT/GE comparisons vs zero to a single compare
1769 * vs the high word of the input.
1771 if (arg_is_const(op
->args
[3]) && arg_info(op
->args
[3])->val
== 0 &&
1772 arg_is_const(op
->args
[4]) && arg_info(op
->args
[4])->val
== 0) {
1773 goto do_setcond_high
;
1782 * Simplify EQ/NE comparisons where one of the pairs
1783 * can be simplified.
1785 i
= do_constant_folding_cond(TCG_TYPE_I32
, op
->args
[1],
1789 goto do_setcond_const
;
1791 goto do_setcond_high
;
1794 i
= do_constant_folding_cond(TCG_TYPE_I32
, op
->args
[2],
1798 goto do_setcond_const
;
1800 op
->args
[2] = op
->args
[3];
1802 op
->opc
= INDEX_op_setcond_i32
;
1811 op
->args
[1] = op
->args
[2];
1812 op
->args
[2] = op
->args
[4];
1814 op
->opc
= INDEX_op_setcond_i32
;
1819 ctx
->s_mask
= smask_from_zmask(1);
1823 return tcg_opt_gen_movi(ctx
, op
, op
->args
[0], i
);
1826 static bool fold_sextract(OptContext
*ctx
, TCGOp
*op
)
1828 uint64_t z_mask
, s_mask
, s_mask_old
;
1829 int pos
= op
->args
[2];
1830 int len
= op
->args
[3];
1832 if (arg_is_const(op
->args
[1])) {
1835 t
= arg_info(op
->args
[1])->val
;
1836 t
= sextract64(t
, pos
, len
);
1837 return tcg_opt_gen_movi(ctx
, op
, op
->args
[0], t
);
1840 z_mask
= arg_info(op
->args
[1])->z_mask
;
1841 z_mask
= sextract64(z_mask
, pos
, len
);
1842 ctx
->z_mask
= z_mask
;
1844 s_mask_old
= arg_info(op
->args
[1])->s_mask
;
1845 s_mask
= sextract64(s_mask_old
, pos
, len
);
1846 s_mask
|= MAKE_64BIT_MASK(len
, 64 - len
);
1847 ctx
->s_mask
= s_mask
;
1850 ctx
->a_mask
= s_mask
& ~s_mask_old
;
1853 return fold_masks(ctx
, op
);
1856 static bool fold_shift(OptContext
*ctx
, TCGOp
*op
)
1858 uint64_t s_mask
, z_mask
, sign
;
1860 if (fold_const2(ctx
, op
) ||
1861 fold_ix_to_i(ctx
, op
, 0) ||
1862 fold_xi_to_x(ctx
, op
, 0)) {
1866 s_mask
= arg_info(op
->args
[1])->s_mask
;
1867 z_mask
= arg_info(op
->args
[1])->z_mask
;
1869 if (arg_is_const(op
->args
[2])) {
1870 int sh
= arg_info(op
->args
[2])->val
;
1872 ctx
->z_mask
= do_constant_folding(op
->opc
, ctx
->type
, z_mask
, sh
);
1874 s_mask
= do_constant_folding(op
->opc
, ctx
->type
, s_mask
, sh
);
1875 ctx
->s_mask
= smask_from_smask(s_mask
);
1877 return fold_masks(ctx
, op
);
1883 * Arithmetic right shift will not reduce the number of
1884 * input sign repetitions.
1886 ctx
->s_mask
= s_mask
;
1890 * If the sign bit is known zero, then logical right shift
1891 * will not reduced the number of input sign repetitions.
1893 sign
= (s_mask
& -s_mask
) >> 1;
1894 if (!(z_mask
& sign
)) {
1895 ctx
->s_mask
= s_mask
;
1905 static bool fold_sub_to_neg(OptContext
*ctx
, TCGOp
*op
)
1910 if (!arg_is_const(op
->args
[1]) || arg_info(op
->args
[1])->val
!= 0) {
1914 switch (ctx
->type
) {
1916 neg_op
= INDEX_op_neg_i32
;
1917 have_neg
= TCG_TARGET_HAS_neg_i32
;
1920 neg_op
= INDEX_op_neg_i64
;
1921 have_neg
= TCG_TARGET_HAS_neg_i64
;
1926 neg_op
= INDEX_op_neg_vec
;
1927 have_neg
= (TCG_TARGET_HAS_neg_vec
&&
1928 tcg_can_emit_vec_op(neg_op
, ctx
->type
, TCGOP_VECE(op
)) > 0);
1931 g_assert_not_reached();
1935 op
->args
[1] = op
->args
[2];
1936 return fold_neg(ctx
, op
);
1941 static bool fold_sub(OptContext
*ctx
, TCGOp
*op
)
1943 if (fold_const2(ctx
, op
) ||
1944 fold_xx_to_i(ctx
, op
, 0) ||
1945 fold_xi_to_x(ctx
, op
, 0) ||
1946 fold_sub_to_neg(ctx
, op
)) {
1952 static bool fold_sub2(OptContext
*ctx
, TCGOp
*op
)
1954 return fold_addsub2(ctx
, op
, false);
1957 static bool fold_tcg_ld(OptContext
*ctx
, TCGOp
*op
)
1959 /* We can't do any folding with a load, but we can record bits. */
1961 CASE_OP_32_64(ld8s
):
1962 ctx
->s_mask
= MAKE_64BIT_MASK(8, 56);
1964 CASE_OP_32_64(ld8u
):
1965 ctx
->z_mask
= MAKE_64BIT_MASK(0, 8);
1966 ctx
->s_mask
= MAKE_64BIT_MASK(9, 55);
1968 CASE_OP_32_64(ld16s
):
1969 ctx
->s_mask
= MAKE_64BIT_MASK(16, 48);
1971 CASE_OP_32_64(ld16u
):
1972 ctx
->z_mask
= MAKE_64BIT_MASK(0, 16);
1973 ctx
->s_mask
= MAKE_64BIT_MASK(17, 47);
1975 case INDEX_op_ld32s_i64
:
1976 ctx
->s_mask
= MAKE_64BIT_MASK(32, 32);
1978 case INDEX_op_ld32u_i64
:
1979 ctx
->z_mask
= MAKE_64BIT_MASK(0, 32);
1980 ctx
->s_mask
= MAKE_64BIT_MASK(33, 31);
1983 g_assert_not_reached();
1988 static bool fold_xor(OptContext
*ctx
, TCGOp
*op
)
1990 if (fold_const2_commutative(ctx
, op
) ||
1991 fold_xx_to_i(ctx
, op
, 0) ||
1992 fold_xi_to_x(ctx
, op
, 0) ||
1993 fold_xi_to_not(ctx
, op
, -1)) {
1997 ctx
->z_mask
= arg_info(op
->args
[1])->z_mask
1998 | arg_info(op
->args
[2])->z_mask
;
1999 ctx
->s_mask
= arg_info(op
->args
[1])->s_mask
2000 & arg_info(op
->args
[2])->s_mask
;
2001 return fold_masks(ctx
, op
);
2004 /* Propagate constants and copies, fold constant expressions. */
2005 void tcg_optimize(TCGContext
*s
)
2008 TCGOp
*op
, *op_next
;
2009 OptContext ctx
= { .tcg
= s
};
2011 /* Array VALS has an element for each temp.
2012 If this temp holds a constant then its value is kept in VALS' element.
2013 If this temp is a copy of other ones then the other copies are
2014 available through the doubly linked circular list. */
2016 nb_temps
= s
->nb_temps
;
2017 for (i
= 0; i
< nb_temps
; ++i
) {
2018 s
->temps
[i
].state_ptr
= NULL
;
2021 QTAILQ_FOREACH_SAFE(op
, &s
->ops
, link
, op_next
) {
2022 TCGOpcode opc
= op
->opc
;
2023 const TCGOpDef
*def
;
2026 /* Calls are special. */
2027 if (opc
== INDEX_op_call
) {
2028 fold_call(&ctx
, op
);
2032 def
= &tcg_op_defs
[opc
];
2033 init_arguments(&ctx
, op
, def
->nb_oargs
+ def
->nb_iargs
);
2034 copy_propagate(&ctx
, op
, def
->nb_oargs
, def
->nb_iargs
);
2036 /* Pre-compute the type of the operation. */
2037 if (def
->flags
& TCG_OPF_VECTOR
) {
2038 ctx
.type
= TCG_TYPE_V64
+ TCGOP_VECL(op
);
2039 } else if (def
->flags
& TCG_OPF_64BIT
) {
2040 ctx
.type
= TCG_TYPE_I64
;
2042 ctx
.type
= TCG_TYPE_I32
;
2045 /* Assume all bits affected, no bits known zero, no sign reps. */
2051 * Process each opcode.
2052 * Sorted alphabetically by opcode as much as possible.
2055 CASE_OP_32_64_VEC(add
):
2056 done
= fold_add(&ctx
, op
);
2058 CASE_OP_32_64(add2
):
2059 done
= fold_add2(&ctx
, op
);
2061 CASE_OP_32_64_VEC(and):
2062 done
= fold_and(&ctx
, op
);
2064 CASE_OP_32_64_VEC(andc
):
2065 done
= fold_andc(&ctx
, op
);
2067 CASE_OP_32_64(brcond
):
2068 done
= fold_brcond(&ctx
, op
);
2070 case INDEX_op_brcond2_i32
:
2071 done
= fold_brcond2(&ctx
, op
);
2073 CASE_OP_32_64(bswap16
):
2074 CASE_OP_32_64(bswap32
):
2075 case INDEX_op_bswap64_i64
:
2076 done
= fold_bswap(&ctx
, op
);
2080 done
= fold_count_zeros(&ctx
, op
);
2082 CASE_OP_32_64(ctpop
):
2083 done
= fold_ctpop(&ctx
, op
);
2085 CASE_OP_32_64(deposit
):
2086 done
= fold_deposit(&ctx
, op
);
2089 CASE_OP_32_64(divu
):
2090 done
= fold_divide(&ctx
, op
);
2092 case INDEX_op_dup_vec
:
2093 done
= fold_dup(&ctx
, op
);
2095 case INDEX_op_dup2_vec
:
2096 done
= fold_dup2(&ctx
, op
);
2099 done
= fold_eqv(&ctx
, op
);
2101 CASE_OP_32_64(extract
):
2102 done
= fold_extract(&ctx
, op
);
2104 CASE_OP_32_64(extract2
):
2105 done
= fold_extract2(&ctx
, op
);
2107 CASE_OP_32_64(ext8s
):
2108 CASE_OP_32_64(ext16s
):
2109 case INDEX_op_ext32s_i64
:
2110 case INDEX_op_ext_i32_i64
:
2111 done
= fold_exts(&ctx
, op
);
2113 CASE_OP_32_64(ext8u
):
2114 CASE_OP_32_64(ext16u
):
2115 case INDEX_op_ext32u_i64
:
2116 case INDEX_op_extu_i32_i64
:
2117 case INDEX_op_extrl_i64_i32
:
2118 case INDEX_op_extrh_i64_i32
:
2119 done
= fold_extu(&ctx
, op
);
2121 CASE_OP_32_64(ld8s
):
2122 CASE_OP_32_64(ld8u
):
2123 CASE_OP_32_64(ld16s
):
2124 CASE_OP_32_64(ld16u
):
2125 case INDEX_op_ld32s_i64
:
2126 case INDEX_op_ld32u_i64
:
2127 done
= fold_tcg_ld(&ctx
, op
);
2130 done
= fold_mb(&ctx
, op
);
2132 CASE_OP_32_64_VEC(mov
):
2133 done
= fold_mov(&ctx
, op
);
2135 CASE_OP_32_64(movcond
):
2136 done
= fold_movcond(&ctx
, op
);
2139 done
= fold_mul(&ctx
, op
);
2141 CASE_OP_32_64(mulsh
):
2142 CASE_OP_32_64(muluh
):
2143 done
= fold_mul_highpart(&ctx
, op
);
2145 CASE_OP_32_64(muls2
):
2146 CASE_OP_32_64(mulu2
):
2147 done
= fold_multiply2(&ctx
, op
);
2149 CASE_OP_32_64(nand
):
2150 done
= fold_nand(&ctx
, op
);
2153 done
= fold_neg(&ctx
, op
);
2156 done
= fold_nor(&ctx
, op
);
2158 CASE_OP_32_64_VEC(not):
2159 done
= fold_not(&ctx
, op
);
2161 CASE_OP_32_64_VEC(or):
2162 done
= fold_or(&ctx
, op
);
2164 CASE_OP_32_64_VEC(orc
):
2165 done
= fold_orc(&ctx
, op
);
2167 case INDEX_op_qemu_ld_i32
:
2168 case INDEX_op_qemu_ld_i64
:
2169 done
= fold_qemu_ld(&ctx
, op
);
2171 case INDEX_op_qemu_st_i32
:
2172 case INDEX_op_qemu_st8_i32
:
2173 case INDEX_op_qemu_st_i64
:
2174 done
= fold_qemu_st(&ctx
, op
);
2177 CASE_OP_32_64(remu
):
2178 done
= fold_remainder(&ctx
, op
);
2180 CASE_OP_32_64(rotl
):
2181 CASE_OP_32_64(rotr
):
2185 done
= fold_shift(&ctx
, op
);
2187 CASE_OP_32_64(setcond
):
2188 done
= fold_setcond(&ctx
, op
);
2190 case INDEX_op_setcond2_i32
:
2191 done
= fold_setcond2(&ctx
, op
);
2193 CASE_OP_32_64(sextract
):
2194 done
= fold_sextract(&ctx
, op
);
2196 CASE_OP_32_64_VEC(sub
):
2197 done
= fold_sub(&ctx
, op
);
2199 CASE_OP_32_64(sub2
):
2200 done
= fold_sub2(&ctx
, op
);
2202 CASE_OP_32_64_VEC(xor):
2203 done
= fold_xor(&ctx
, op
);
2210 finish_folding(&ctx
, op
);