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)
311 CASE_OP_32_64_VEC(and):
314 CASE_OP_32_64_VEC(or):
317 CASE_OP_32_64_VEC(xor):
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);
350 CASE_OP_32_64_VEC(not):
356 CASE_OP_32_64_VEC(andc):
359 CASE_OP_32_64_VEC(orc):
362 CASE_OP_32_64_VEC(eqv):
365 CASE_OP_32_64_VEC(nand):
368 CASE_OP_32_64_VEC(nor):
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 if (arg_is_const(x) && arg_is_const(y)) {
556 uint64_t xv = arg_info(x)->val;
557 uint64_t yv = arg_info(y)->val;
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) && arg_info(y)->val == 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_commutative(OptContext *ctx, TCGOp *op)
756 swap_commutative(op->args[0], &op->args[1], &op->args[2]);
760 static bool fold_const2_commutative(OptContext *ctx, TCGOp *op)
762 swap_commutative(op->args[0], &op->args[1], &op->args[2]);
763 return fold_const2(ctx, op);
766 static bool fold_masks(OptContext *ctx, TCGOp *op)
768 uint64_t a_mask = ctx->a_mask;
769 uint64_t z_mask = ctx->z_mask;
770 uint64_t s_mask = ctx->s_mask;
773 * 32-bit ops generate 32-bit results, which for the purpose of
774 * simplifying tcg are sign-extended. Certainly that's how we
775 * represent our constants elsewhere. Note that the bits will
776 * be reset properly for a 64-bit value when encountering the
777 * type changing opcodes.
779 if (ctx->type == TCG_TYPE_I32) {
780 a_mask = (int32_t)a_mask;
781 z_mask = (int32_t)z_mask;
782 s_mask |= MAKE_64BIT_MASK(32, 32);
783 ctx->z_mask = z_mask;
784 ctx->s_mask = s_mask;
788 return tcg_opt_gen_movi(ctx, op, op->args[0], 0);
791 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]);
797 * Convert @op to NOT, if NOT is supported by the host.
798 * Return true f the conversion is successful, which will still
799 * indicate that the processing is complete.
801 static bool fold_not(OptContext *ctx, TCGOp *op);
802 static bool fold_to_not(OptContext *ctx, TCGOp *op, int idx)
809 not_op = INDEX_op_not_i32;
810 have_not = TCG_TARGET_HAS_not_i32;
813 not_op = INDEX_op_not_i64;
814 have_not = TCG_TARGET_HAS_not_i64;
819 not_op = INDEX_op_not_vec;
820 have_not = TCG_TARGET_HAS_not_vec;
823 g_assert_not_reached();
827 op->args[1] = op->args[idx];
828 return fold_not(ctx, op);
833 /* If the binary operation has first argument @i, fold to @i. */
834 static bool fold_ix_to_i(OptContext *ctx, TCGOp *op, uint64_t i)
836 if (arg_is_const(op->args[1]) && arg_info(op->args[1])->val == i) {
837 return tcg_opt_gen_movi(ctx, op, op->args[0], i);
842 /* If the binary operation has first argument @i, fold to NOT. */
843 static bool fold_ix_to_not(OptContext *ctx, TCGOp *op, uint64_t i)
845 if (arg_is_const(op->args[1]) && arg_info(op->args[1])->val == i) {
846 return fold_to_not(ctx, op, 2);
851 /* If the binary operation has second argument @i, fold to @i. */
852 static bool fold_xi_to_i(OptContext *ctx, TCGOp *op, uint64_t i)
854 if (arg_is_const(op->args[2]) && arg_info(op->args[2])->val == i) {
855 return tcg_opt_gen_movi(ctx, op, op->args[0], i);
860 /* If the binary operation has second argument @i, fold to identity. */
861 static bool fold_xi_to_x(OptContext *ctx, TCGOp *op, uint64_t i)
863 if (arg_is_const(op->args[2]) && arg_info(op->args[2])->val == i) {
864 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]);
869 /* If the binary operation has second argument @i, fold to NOT. */
870 static bool fold_xi_to_not(OptContext *ctx, TCGOp *op, uint64_t i)
872 if (arg_is_const(op->args[2]) && arg_info(op->args[2])->val == i) {
873 return fold_to_not(ctx, op, 1);
878 /* If the binary operation has both arguments equal, fold to @i. */
879 static bool fold_xx_to_i(OptContext *ctx, TCGOp *op, uint64_t i)
881 if (args_are_copies(op->args[1], op->args[2])) {
882 return tcg_opt_gen_movi(ctx, op, op->args[0], i);
887 /* If the binary operation has both arguments equal, fold to identity. */
888 static bool fold_xx_to_x(OptContext *ctx, TCGOp *op)
890 if (args_are_copies(op->args[1], op->args[2])) {
891 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]);
897 * These outermost fold_<op> functions are sorted alphabetically.
899 * The ordering of the transformations should be:
900 * 1) those that produce a constant
901 * 2) those that produce a copy
902 * 3) those that produce information about the result value.
905 static bool fold_add(OptContext *ctx, TCGOp *op)
907 if (fold_const2_commutative(ctx, op) ||
908 fold_xi_to_x(ctx, op, 0)) {
914 /* We cannot as yet do_constant_folding with vectors. */
915 static bool fold_add_vec(OptContext *ctx, TCGOp *op)
917 if (fold_commutative(ctx, op) ||
918 fold_xi_to_x(ctx, op, 0)) {
924 static bool fold_addsub2(OptContext *ctx, TCGOp *op, bool add)
926 if (arg_is_const(op->args[2]) && arg_is_const(op->args[3]) &&
927 arg_is_const(op->args[4]) && arg_is_const(op->args[5])) {
928 uint64_t al = arg_info(op->args[2])->val;
929 uint64_t ah = arg_info(op->args[3])->val;
930 uint64_t bl = arg_info(op->args[4])->val;
931 uint64_t bh = arg_info(op->args[5])->val;
935 if (ctx->type == TCG_TYPE_I32) {
936 uint64_t a = deposit64(al, 32, 32, ah);
937 uint64_t b = deposit64(bl, 32, 32, bh);
945 al = sextract64(a, 0, 32);
946 ah = sextract64(a, 32, 32);
948 Int128 a = int128_make128(al, ah);
949 Int128 b = int128_make128(bl, bh);
952 a = int128_add(a, b);
954 a = int128_sub(a, b);
957 al = int128_getlo(a);
958 ah = int128_gethi(a);
964 /* The proper opcode is supplied by tcg_opt_gen_mov. */
965 op2 = tcg_op_insert_before(ctx->tcg, op, 0);
967 tcg_opt_gen_movi(ctx, op, rl, al);
968 tcg_opt_gen_movi(ctx, op2, rh, ah);
974 static bool fold_add2(OptContext *ctx, TCGOp *op)
976 /* Note that the high and low parts may be independently swapped. */
977 swap_commutative(op->args[0], &op->args[2], &op->args[4]);
978 swap_commutative(op->args[1], &op->args[3], &op->args[5]);
980 return fold_addsub2(ctx, op, true);
983 static bool fold_and(OptContext *ctx, TCGOp *op)
987 if (fold_const2_commutative(ctx, op) ||
988 fold_xi_to_i(ctx, op, 0) ||
989 fold_xi_to_x(ctx, op, -1) ||
990 fold_xx_to_x(ctx, op)) {
994 z1 = arg_info(op->args[1])->z_mask;
995 z2 = arg_info(op->args[2])->z_mask;
996 ctx->z_mask = z1 & z2;
999 * Sign repetitions are perforce all identical, whether they are 1 or 0.
1000 * Bitwise operations preserve the relative quantity of the repetitions.
1002 ctx->s_mask = arg_info(op->args[1])->s_mask
1003 & arg_info(op->args[2])->s_mask;
1006 * Known-zeros does not imply known-ones. Therefore unless
1007 * arg2 is constant, we can't infer affected bits from it.
1009 if (arg_is_const(op->args[2])) {
1010 ctx->a_mask = z1 & ~z2;
1013 return fold_masks(ctx, op);
1016 static bool fold_andc(OptContext *ctx, TCGOp *op)
1020 if (fold_const2(ctx, op) ||
1021 fold_xx_to_i(ctx, op, 0) ||
1022 fold_xi_to_x(ctx, op, 0) ||
1023 fold_ix_to_not(ctx, op, -1)) {
1027 z1 = arg_info(op->args[1])->z_mask;
1030 * Known-zeros does not imply known-ones. Therefore unless
1031 * arg2 is constant, we can't infer anything from it.
1033 if (arg_is_const(op->args[2])) {
1034 uint64_t z2 = ~arg_info(op->args[2])->z_mask;
1035 ctx->a_mask = z1 & ~z2;
1040 ctx->s_mask = arg_info(op->args[1])->s_mask
1041 & arg_info(op->args[2])->s_mask;
1042 return fold_masks(ctx, op);
1045 static bool fold_brcond(OptContext *ctx, TCGOp *op)
1047 TCGCond cond = op->args[2];
1050 if (swap_commutative(NO_DEST, &op->args[0], &op->args[1])) {
1051 op->args[2] = cond = tcg_swap_cond(cond);
1054 i = do_constant_folding_cond(ctx->type, op->args[0], op->args[1], cond);
1056 tcg_op_remove(ctx->tcg, op);
1060 op->opc = INDEX_op_br;
1061 op->args[0] = op->args[3];
1066 static bool fold_brcond2(OptContext *ctx, TCGOp *op)
1068 TCGCond cond = op->args[4];
1069 TCGArg label = op->args[5];
1072 if (swap_commutative2(&op->args[0], &op->args[2])) {
1073 op->args[4] = cond = tcg_swap_cond(cond);
1076 i = do_constant_folding_cond2(&op->args[0], &op->args[2], cond);
1078 goto do_brcond_const;
1085 * Simplify LT/GE comparisons vs zero to a single compare
1086 * vs the high word of the input.
1088 if (arg_is_const(op->args[2]) && arg_info(op->args[2])->val == 0 &&
1089 arg_is_const(op->args[3]) && arg_info(op->args[3])->val == 0) {
1090 goto do_brcond_high;
1099 * Simplify EQ/NE comparisons where one of the pairs
1100 * can be simplified.
1102 i = do_constant_folding_cond(TCG_TYPE_I32, op->args[0],
1106 goto do_brcond_const;
1108 goto do_brcond_high;
1111 i = do_constant_folding_cond(TCG_TYPE_I32, op->args[1],
1115 goto do_brcond_const;
1117 op->opc = INDEX_op_brcond_i32;
1118 op->args[1] = op->args[2];
1120 op->args[3] = label;
1129 op->opc = INDEX_op_brcond_i32;
1130 op->args[0] = op->args[1];
1131 op->args[1] = op->args[3];
1133 op->args[3] = label;
1138 tcg_op_remove(ctx->tcg, op);
1141 op->opc = INDEX_op_br;
1142 op->args[0] = label;
1148 static bool fold_bswap(OptContext *ctx, TCGOp *op)
1150 uint64_t z_mask, s_mask, sign;
1152 if (arg_is_const(op->args[1])) {
1153 uint64_t t = arg_info(op->args[1])->val;
1155 t = do_constant_folding(op->opc, ctx->type, t, op->args[2]);
1156 return tcg_opt_gen_movi(ctx, op, op->args[0], t);
1159 z_mask = arg_info(op->args[1])->z_mask;
1162 case INDEX_op_bswap16_i32:
1163 case INDEX_op_bswap16_i64:
1164 z_mask = bswap16(z_mask);
1167 case INDEX_op_bswap32_i32:
1168 case INDEX_op_bswap32_i64:
1169 z_mask = bswap32(z_mask);
1172 case INDEX_op_bswap64_i64:
1173 z_mask = bswap64(z_mask);
1177 g_assert_not_reached();
1179 s_mask = smask_from_zmask(z_mask);
1181 switch (op->args[2] & (TCG_BSWAP_OZ | TCG_BSWAP_OS)) {
1185 /* If the sign bit may be 1, force all the bits above to 1. */
1186 if (z_mask & sign) {
1192 /* The high bits are undefined: force all bits above the sign to 1. */
1193 z_mask |= sign << 1;
1197 ctx->z_mask = z_mask;
1198 ctx->s_mask = s_mask;
1200 return fold_masks(ctx, op);
1203 static bool fold_call(OptContext *ctx, TCGOp *op)
1205 TCGContext *s = ctx->tcg;
1206 int nb_oargs = TCGOP_CALLO(op);
1207 int nb_iargs = TCGOP_CALLI(op);
1210 init_arguments(ctx, op, nb_oargs + nb_iargs);
1211 copy_propagate(ctx, op, nb_oargs, nb_iargs);
1213 /* If the function reads or writes globals, reset temp data. */
1214 flags = tcg_call_flags(op);
1215 if (!(flags & (TCG_CALL_NO_READ_GLOBALS | TCG_CALL_NO_WRITE_GLOBALS))) {
1216 int nb_globals = s->nb_globals;
1218 for (i = 0; i < nb_globals; i++) {
1219 if (test_bit(i, ctx->temps_used.l)) {
1220 reset_ts(&ctx->tcg->temps[i]);
1225 /* Reset temp data for outputs. */
1226 for (i = 0; i < nb_oargs; i++) {
1227 reset_temp(op->args[i]);
1230 /* Stop optimizing MB across calls. */
1231 ctx->prev_mb = NULL;
1235 static bool fold_count_zeros(OptContext *ctx, TCGOp *op)
1239 if (arg_is_const(op->args[1])) {
1240 uint64_t t = arg_info(op->args[1])->val;
1243 t = do_constant_folding(op->opc, ctx->type, t, 0);
1244 return tcg_opt_gen_movi(ctx, op, op->args[0], t);
1246 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[2]);
1249 switch (ctx->type) {
1257 g_assert_not_reached();
1259 ctx->z_mask = arg_info(op->args[2])->z_mask | z_mask;
1260 ctx->s_mask = smask_from_zmask(ctx->z_mask);
1264 static bool fold_ctpop(OptContext *ctx, TCGOp *op)
1266 if (fold_const1(ctx, op)) {
1270 switch (ctx->type) {
1272 ctx->z_mask = 32 | 31;
1275 ctx->z_mask = 64 | 63;
1278 g_assert_not_reached();
1280 ctx->s_mask = smask_from_zmask(ctx->z_mask);
1284 static bool fold_deposit(OptContext *ctx, TCGOp *op)
1286 if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) {
1287 uint64_t t1 = arg_info(op->args[1])->val;
1288 uint64_t t2 = arg_info(op->args[2])->val;
1290 t1 = deposit64(t1, op->args[3], op->args[4], t2);
1291 return tcg_opt_gen_movi(ctx, op, op->args[0], t1);
1294 ctx->z_mask = deposit64(arg_info(op->args[1])->z_mask,
1295 op->args[3], op->args[4],
1296 arg_info(op->args[2])->z_mask);
1300 static bool fold_divide(OptContext *ctx, TCGOp *op)
1302 if (fold_const2(ctx, op) ||
1303 fold_xi_to_x(ctx, op, 1)) {
1309 static bool fold_dup(OptContext *ctx, TCGOp *op)
1311 if (arg_is_const(op->args[1])) {
1312 uint64_t t = arg_info(op->args[1])->val;
1313 t = dup_const(TCGOP_VECE(op), t);
1314 return tcg_opt_gen_movi(ctx, op, op->args[0], t);
1319 static bool fold_dup2(OptContext *ctx, TCGOp *op)
1321 if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) {
1322 uint64_t t = deposit64(arg_info(op->args[1])->val, 32, 32,
1323 arg_info(op->args[2])->val);
1324 return tcg_opt_gen_movi(ctx, op, op->args[0], t);
1327 if (args_are_copies(op->args[1], op->args[2])) {
1328 op->opc = INDEX_op_dup_vec;
1329 TCGOP_VECE(op) = MO_32;
1334 static bool fold_eqv(OptContext *ctx, TCGOp *op)
1336 if (fold_const2_commutative(ctx, op) ||
1337 fold_xi_to_x(ctx, op, -1) ||
1338 fold_xi_to_not(ctx, op, 0)) {
1342 ctx->s_mask = arg_info(op->args[1])->s_mask
1343 & arg_info(op->args[2])->s_mask;
1347 static bool fold_extract(OptContext *ctx, TCGOp *op)
1349 uint64_t z_mask_old, z_mask;
1350 int pos = op->args[2];
1351 int len = op->args[3];
1353 if (arg_is_const(op->args[1])) {
1356 t = arg_info(op->args[1])->val;
1357 t = extract64(t, pos, len);
1358 return tcg_opt_gen_movi(ctx, op, op->args[0], t);
1361 z_mask_old = arg_info(op->args[1])->z_mask;
1362 z_mask = extract64(z_mask_old, pos, len);
1364 ctx->a_mask = z_mask_old ^ z_mask;
1366 ctx->z_mask = z_mask;
1367 ctx->s_mask = smask_from_zmask(z_mask);
1369 return fold_masks(ctx, op);
1372 static bool fold_extract2(OptContext *ctx, TCGOp *op)
1374 if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) {
1375 uint64_t v1 = arg_info(op->args[1])->val;
1376 uint64_t v2 = arg_info(op->args[2])->val;
1377 int shr = op->args[3];
1379 if (op->opc == INDEX_op_extract2_i64) {
1383 v1 = (uint32_t)v1 >> shr;
1384 v2 = (uint64_t)((int32_t)v2 << (32 - shr));
1386 return tcg_opt_gen_movi(ctx, op, op->args[0], v1 | v2);
1391 static bool fold_exts(OptContext *ctx, TCGOp *op)
1393 uint64_t s_mask_old, s_mask, z_mask, sign;
1394 bool type_change = false;
1396 if (fold_const1(ctx, op)) {
1400 z_mask = arg_info(op->args[1])->z_mask;
1401 s_mask = arg_info(op->args[1])->s_mask;
1402 s_mask_old = s_mask;
1405 CASE_OP_32_64(ext8s):
1407 z_mask = (uint8_t)z_mask;
1409 CASE_OP_32_64(ext16s):
1411 z_mask = (uint16_t)z_mask;
1413 case INDEX_op_ext_i32_i64:
1416 case INDEX_op_ext32s_i64:
1418 z_mask = (uint32_t)z_mask;
1421 g_assert_not_reached();
1424 if (z_mask & sign) {
1427 s_mask |= sign << 1;
1429 ctx->z_mask = z_mask;
1430 ctx->s_mask = s_mask;
1432 ctx->a_mask = s_mask & ~s_mask_old;
1435 return fold_masks(ctx, op);
1438 static bool fold_extu(OptContext *ctx, TCGOp *op)
1440 uint64_t z_mask_old, z_mask;
1441 bool type_change = false;
1443 if (fold_const1(ctx, op)) {
1447 z_mask_old = z_mask = arg_info(op->args[1])->z_mask;
1450 CASE_OP_32_64(ext8u):
1451 z_mask = (uint8_t)z_mask;
1453 CASE_OP_32_64(ext16u):
1454 z_mask = (uint16_t)z_mask;
1456 case INDEX_op_extrl_i64_i32:
1457 case INDEX_op_extu_i32_i64:
1460 case INDEX_op_ext32u_i64:
1461 z_mask = (uint32_t)z_mask;
1463 case INDEX_op_extrh_i64_i32:
1468 g_assert_not_reached();
1471 ctx->z_mask = z_mask;
1472 ctx->s_mask = smask_from_zmask(z_mask);
1474 ctx->a_mask = z_mask_old ^ z_mask;
1476 return fold_masks(ctx, op);
1479 static bool fold_mb(OptContext *ctx, TCGOp *op)
1481 /* Eliminate duplicate and redundant fence instructions. */
1484 * Merge two barriers of the same type into one,
1485 * or a weaker barrier into a stronger one,
1486 * or two weaker barriers into a stronger one.
1487 * mb X; mb Y => mb X|Y
1488 * mb; strl => mb; st
1489 * ldaq; mb => ld; mb
1490 * ldaq; strl => ld; mb; st
1491 * Other combinations are also merged into a strong
1492 * barrier. This is stricter than specified but for
1493 * the purposes of TCG is better than not optimizing.
1495 ctx->prev_mb->args[0] |= op->args[0];
1496 tcg_op_remove(ctx->tcg, op);
1503 static bool fold_mov(OptContext *ctx, TCGOp *op)
1505 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[1]);
1508 static bool fold_movcond(OptContext *ctx, TCGOp *op)
1510 TCGCond cond = op->args[5];
1513 if (swap_commutative(NO_DEST, &op->args[1], &op->args[2])) {
1514 op->args[5] = cond = tcg_swap_cond(cond);
1517 * Canonicalize the "false" input reg to match the destination reg so
1518 * that the tcg backend can implement a "move if true" operation.
1520 if (swap_commutative(op->args[0], &op->args[4], &op->args[3])) {
1521 op->args[5] = cond = tcg_invert_cond(cond);
1524 i = do_constant_folding_cond(ctx->type, op->args[1], op->args[2], cond);
1526 return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[4 - i]);
1529 ctx->z_mask = arg_info(op->args[3])->z_mask
1530 | arg_info(op->args[4])->z_mask;
1531 ctx->s_mask = arg_info(op->args[3])->s_mask
1532 & arg_info(op->args[4])->s_mask;
1534 if (arg_is_const(op->args[3]) && arg_is_const(op->args[4])) {
1535 uint64_t tv = arg_info(op->args[3])->val;
1536 uint64_t fv = arg_info(op->args[4])->val;
1539 switch (ctx->type) {
1541 opc = INDEX_op_setcond_i32;
1544 opc = INDEX_op_setcond_i64;
1547 g_assert_not_reached();
1550 if (tv == 1 && fv == 0) {
1553 } else if (fv == 1 && tv == 0) {
1555 op->args[3] = tcg_invert_cond(cond);
1561 static bool fold_mul(OptContext *ctx, TCGOp *op)
1563 if (fold_const2(ctx, op) ||
1564 fold_xi_to_i(ctx, op, 0) ||
1565 fold_xi_to_x(ctx, op, 1)) {
1571 static bool fold_mul_highpart(OptContext *ctx, TCGOp *op)
1573 if (fold_const2_commutative(ctx, op) ||
1574 fold_xi_to_i(ctx, op, 0)) {
1580 static bool fold_multiply2(OptContext *ctx, TCGOp *op)
1582 swap_commutative(op->args[0], &op->args[2], &op->args[3]);
1584 if (arg_is_const(op->args[2]) && arg_is_const(op->args[3])) {
1585 uint64_t a = arg_info(op->args[2])->val;
1586 uint64_t b = arg_info(op->args[3])->val;
1592 case INDEX_op_mulu2_i32:
1593 l = (uint64_t)(uint32_t)a * (uint32_t)b;
1594 h = (int32_t)(l >> 32);
1597 case INDEX_op_muls2_i32:
1598 l = (int64_t)(int32_t)a * (int32_t)b;
1602 case INDEX_op_mulu2_i64:
1603 mulu64(&l, &h, a, b);
1605 case INDEX_op_muls2_i64:
1606 muls64(&l, &h, a, b);
1609 g_assert_not_reached();
1615 /* The proper opcode is supplied by tcg_opt_gen_mov. */
1616 op2 = tcg_op_insert_before(ctx->tcg, op, 0);
1618 tcg_opt_gen_movi(ctx, op, rl, l);
1619 tcg_opt_gen_movi(ctx, op2, rh, h);
1625 static bool fold_nand(OptContext *ctx, TCGOp *op)
1627 if (fold_const2_commutative(ctx, op) ||
1628 fold_xi_to_not(ctx, op, -1)) {
1632 ctx->s_mask = arg_info(op->args[1])->s_mask
1633 & arg_info(op->args[2])->s_mask;
1637 static bool fold_neg(OptContext *ctx, TCGOp *op)
1641 if (fold_const1(ctx, op)) {
1645 /* Set to 1 all bits to the left of the rightmost. */
1646 z_mask = arg_info(op->args[1])->z_mask;
1647 ctx->z_mask = -(z_mask & -z_mask);
1650 * Because of fold_sub_to_neg, we want to always return true,
1651 * via finish_folding.
1653 finish_folding(ctx, op);
1657 static bool fold_nor(OptContext *ctx, TCGOp *op)
1659 if (fold_const2_commutative(ctx, op) ||
1660 fold_xi_to_not(ctx, op, 0)) {
1664 ctx->s_mask = arg_info(op->args[1])->s_mask
1665 & arg_info(op->args[2])->s_mask;
1669 static bool fold_not(OptContext *ctx, TCGOp *op)
1671 if (fold_const1(ctx, op)) {
1675 ctx->s_mask = arg_info(op->args[1])->s_mask;
1677 /* Because of fold_to_not, we want to always return true, via finish. */
1678 finish_folding(ctx, op);
1682 static bool fold_or(OptContext *ctx, TCGOp *op)
1684 if (fold_const2_commutative(ctx, op) ||
1685 fold_xi_to_x(ctx, op, 0) ||
1686 fold_xx_to_x(ctx, op)) {
1690 ctx->z_mask = arg_info(op->args[1])->z_mask
1691 | arg_info(op->args[2])->z_mask;
1692 ctx->s_mask = arg_info(op->args[1])->s_mask
1693 & arg_info(op->args[2])->s_mask;
1694 return fold_masks(ctx, op);
1697 static bool fold_orc(OptContext *ctx, TCGOp *op)
1699 if (fold_const2(ctx, op) ||
1700 fold_xx_to_i(ctx, op, -1) ||
1701 fold_xi_to_x(ctx, op, -1) ||
1702 fold_ix_to_not(ctx, op, 0)) {
1706 ctx->s_mask = arg_info(op->args[1])->s_mask
1707 & arg_info(op->args[2])->s_mask;
1711 static bool fold_qemu_ld(OptContext *ctx, TCGOp *op)
1713 const TCGOpDef *def = &tcg_op_defs[op->opc];
1714 MemOpIdx oi = op->args[def->nb_oargs + def->nb_iargs];
1715 MemOp mop = get_memop(oi);
1716 int width = 8 * memop_size(mop);
1719 ctx->s_mask = MAKE_64BIT_MASK(width, 64 - width);
1720 if (!(mop & MO_SIGN)) {
1721 ctx->z_mask = MAKE_64BIT_MASK(0, width);
1726 /* Opcodes that touch guest memory stop the mb optimization. */
1727 ctx->prev_mb = NULL;
1731 static bool fold_qemu_st(OptContext *ctx, TCGOp *op)
1733 /* Opcodes that touch guest memory stop the mb optimization. */
1734 ctx->prev_mb = NULL;
1738 static bool fold_remainder(OptContext *ctx, TCGOp *op)
1740 if (fold_const2(ctx, op) ||
1741 fold_xx_to_i(ctx, op, 0)) {
1747 static bool fold_setcond(OptContext *ctx, TCGOp *op)
1749 TCGCond cond = op->args[3];
1752 if (swap_commutative(op->args[0], &op->args[1], &op->args[2])) {
1753 op->args[3] = cond = tcg_swap_cond(cond);
1756 i = do_constant_folding_cond(ctx->type, op->args[1], op->args[2], cond);
1758 return tcg_opt_gen_movi(ctx, op, op->args[0], i);
1762 ctx->s_mask = smask_from_zmask(1);
1766 static bool fold_setcond2(OptContext *ctx, TCGOp *op)
1768 TCGCond cond = op->args[5];
1771 if (swap_commutative2(&op->args[1], &op->args[3])) {
1772 op->args[5] = cond = tcg_swap_cond(cond);
1775 i = do_constant_folding_cond2(&op->args[1], &op->args[3], cond);
1777 goto do_setcond_const;
1784 * Simplify LT/GE comparisons vs zero to a single compare
1785 * vs the high word of the input.
1787 if (arg_is_const(op->args[3]) && arg_info(op->args[3])->val == 0 &&
1788 arg_is_const(op->args[4]) && arg_info(op->args[4])->val == 0) {
1789 goto do_setcond_high;
1798 * Simplify EQ/NE comparisons where one of the pairs
1799 * can be simplified.
1801 i = do_constant_folding_cond(TCG_TYPE_I32, op->args[1],
1805 goto do_setcond_const;
1807 goto do_setcond_high;
1810 i = do_constant_folding_cond(TCG_TYPE_I32, op->args[2],
1814 goto do_setcond_const;
1816 op->args[2] = op->args[3];
1818 op->opc = INDEX_op_setcond_i32;
1827 op->args[1] = op->args[2];
1828 op->args[2] = op->args[4];
1830 op->opc = INDEX_op_setcond_i32;
1835 ctx->s_mask = smask_from_zmask(1);
1839 return tcg_opt_gen_movi(ctx, op, op->args[0], i);
1842 static bool fold_sextract(OptContext *ctx, TCGOp *op)
1844 uint64_t z_mask, s_mask, s_mask_old;
1845 int pos = op->args[2];
1846 int len = op->args[3];
1848 if (arg_is_const(op->args[1])) {
1851 t = arg_info(op->args[1])->val;
1852 t = sextract64(t, pos, len);
1853 return tcg_opt_gen_movi(ctx, op, op->args[0], t);
1856 z_mask = arg_info(op->args[1])->z_mask;
1857 z_mask = sextract64(z_mask, pos, len);
1858 ctx->z_mask = z_mask;
1860 s_mask_old = arg_info(op->args[1])->s_mask;
1861 s_mask = sextract64(s_mask_old, pos, len);
1862 s_mask |= MAKE_64BIT_MASK(len, 64 - len);
1863 ctx->s_mask = s_mask;
1866 ctx->a_mask = s_mask & ~s_mask_old;
1869 return fold_masks(ctx, op);
1872 static bool fold_shift(OptContext *ctx, TCGOp *op)
1874 uint64_t s_mask, z_mask, sign;
1876 if (fold_const2(ctx, op) ||
1877 fold_ix_to_i(ctx, op, 0) ||
1878 fold_xi_to_x(ctx, op, 0)) {
1882 s_mask = arg_info(op->args[1])->s_mask;
1883 z_mask = arg_info(op->args[1])->z_mask;
1885 if (arg_is_const(op->args[2])) {
1886 int sh = arg_info(op->args[2])->val;
1888 ctx->z_mask = do_constant_folding(op->opc, ctx->type, z_mask, sh);
1890 s_mask = do_constant_folding(op->opc, ctx->type, s_mask, sh);
1891 ctx->s_mask = smask_from_smask(s_mask);
1893 return fold_masks(ctx, op);
1899 * Arithmetic right shift will not reduce the number of
1900 * input sign repetitions.
1902 ctx->s_mask = s_mask;
1906 * If the sign bit is known zero, then logical right shift
1907 * will not reduced the number of input sign repetitions.
1909 sign = (s_mask & -s_mask) >> 1;
1910 if (!(z_mask & sign)) {
1911 ctx->s_mask = s_mask;
1921 static bool fold_sub_to_neg(OptContext *ctx, TCGOp *op)
1926 if (!arg_is_const(op->args[1]) || arg_info(op->args[1])->val != 0) {
1930 switch (ctx->type) {
1932 neg_op = INDEX_op_neg_i32;
1933 have_neg = TCG_TARGET_HAS_neg_i32;
1936 neg_op = INDEX_op_neg_i64;
1937 have_neg = TCG_TARGET_HAS_neg_i64;
1942 neg_op = INDEX_op_neg_vec;
1943 have_neg = (TCG_TARGET_HAS_neg_vec &&
1944 tcg_can_emit_vec_op(neg_op, ctx->type, TCGOP_VECE(op)) > 0);
1947 g_assert_not_reached();
1951 op->args[1] = op->args[2];
1952 return fold_neg(ctx, op);
1957 /* We cannot as yet do_constant_folding with vectors. */
1958 static bool fold_sub_vec(OptContext *ctx, TCGOp *op)
1960 if (fold_xx_to_i(ctx, op, 0) ||
1961 fold_xi_to_x(ctx, op, 0) ||
1962 fold_sub_to_neg(ctx, op)) {
1968 static bool fold_sub(OptContext *ctx, TCGOp *op)
1970 return fold_const2(ctx, op) || fold_sub_vec(ctx, op);
1973 static bool fold_sub2(OptContext *ctx, TCGOp *op)
1975 return fold_addsub2(ctx, op, false);
1978 static bool fold_tcg_ld(OptContext *ctx, TCGOp *op)
1980 /* We can't do any folding with a load, but we can record bits. */
1982 CASE_OP_32_64(ld8s):
1983 ctx->s_mask = MAKE_64BIT_MASK(8, 56);
1985 CASE_OP_32_64(ld8u):
1986 ctx->z_mask = MAKE_64BIT_MASK(0, 8);
1987 ctx->s_mask = MAKE_64BIT_MASK(9, 55);
1989 CASE_OP_32_64(ld16s):
1990 ctx->s_mask = MAKE_64BIT_MASK(16, 48);
1992 CASE_OP_32_64(ld16u):
1993 ctx->z_mask = MAKE_64BIT_MASK(0, 16);
1994 ctx->s_mask = MAKE_64BIT_MASK(17, 47);
1996 case INDEX_op_ld32s_i64:
1997 ctx->s_mask = MAKE_64BIT_MASK(32, 32);
1999 case INDEX_op_ld32u_i64:
2000 ctx->z_mask = MAKE_64BIT_MASK(0, 32);
2001 ctx->s_mask = MAKE_64BIT_MASK(33, 31);
2004 g_assert_not_reached();
2009 static bool fold_xor(OptContext *ctx, TCGOp *op)
2011 if (fold_const2_commutative(ctx, op) ||
2012 fold_xx_to_i(ctx, op, 0) ||
2013 fold_xi_to_x(ctx, op, 0) ||
2014 fold_xi_to_not(ctx, op, -1)) {
2018 ctx->z_mask = arg_info(op->args[1])->z_mask
2019 | arg_info(op->args[2])->z_mask;
2020 ctx->s_mask = arg_info(op->args[1])->s_mask
2021 & arg_info(op->args[2])->s_mask;
2022 return fold_masks(ctx, op);
2025 /* Propagate constants and copies, fold constant expressions. */
2026 void tcg_optimize(TCGContext *s)
2029 TCGOp *op, *op_next;
2030 OptContext ctx = { .tcg = s };
2032 /* Array VALS has an element for each temp.
2033 If this temp holds a constant then its value is kept in VALS' element.
2034 If this temp is a copy of other ones then the other copies are
2035 available through the doubly linked circular list. */
2037 nb_temps = s->nb_temps;
2038 for (i = 0; i < nb_temps; ++i) {
2039 s->temps[i].state_ptr = NULL;
2042 QTAILQ_FOREACH_SAFE(op, &s->ops, link, op_next) {
2043 TCGOpcode opc = op->opc;
2044 const TCGOpDef *def;
2047 /* Calls are special. */
2048 if (opc == INDEX_op_call) {
2049 fold_call(&ctx, op);
2053 def = &tcg_op_defs[opc];
2054 init_arguments(&ctx, op, def->nb_oargs + def->nb_iargs);
2055 copy_propagate(&ctx, op, def->nb_oargs, def->nb_iargs);
2057 /* Pre-compute the type of the operation. */
2058 if (def->flags & TCG_OPF_VECTOR) {
2059 ctx.type = TCG_TYPE_V64 + TCGOP_VECL(op);
2060 } else if (def->flags & TCG_OPF_64BIT) {
2061 ctx.type = TCG_TYPE_I64;
2063 ctx.type = TCG_TYPE_I32;
2066 /* Assume all bits affected, no bits known zero, no sign reps. */
2072 * Process each opcode.
2073 * Sorted alphabetically by opcode as much as possible.
2077 done = fold_add(&ctx, op);
2079 case INDEX_op_add_vec:
2080 done = fold_add_vec(&ctx, op);
2082 CASE_OP_32_64(add2):
2083 done = fold_add2(&ctx, op);
2085 CASE_OP_32_64_VEC(and):
2086 done = fold_and(&ctx, op);
2088 CASE_OP_32_64_VEC(andc):
2089 done = fold_andc(&ctx, op);
2091 CASE_OP_32_64(brcond):
2092 done = fold_brcond(&ctx, op);
2094 case INDEX_op_brcond2_i32:
2095 done = fold_brcond2(&ctx, op);
2097 CASE_OP_32_64(bswap16):
2098 CASE_OP_32_64(bswap32):
2099 case INDEX_op_bswap64_i64:
2100 done = fold_bswap(&ctx, op);
2104 done = fold_count_zeros(&ctx, op);
2106 CASE_OP_32_64(ctpop):
2107 done = fold_ctpop(&ctx, op);
2109 CASE_OP_32_64(deposit):
2110 done = fold_deposit(&ctx, op);
2113 CASE_OP_32_64(divu):
2114 done = fold_divide(&ctx, op);
2116 case INDEX_op_dup_vec:
2117 done = fold_dup(&ctx, op);
2119 case INDEX_op_dup2_vec:
2120 done = fold_dup2(&ctx, op);
2122 CASE_OP_32_64_VEC(eqv):
2123 done = fold_eqv(&ctx, op);
2125 CASE_OP_32_64(extract):
2126 done = fold_extract(&ctx, op);
2128 CASE_OP_32_64(extract2):
2129 done = fold_extract2(&ctx, op);
2131 CASE_OP_32_64(ext8s):
2132 CASE_OP_32_64(ext16s):
2133 case INDEX_op_ext32s_i64:
2134 case INDEX_op_ext_i32_i64:
2135 done = fold_exts(&ctx, op);
2137 CASE_OP_32_64(ext8u):
2138 CASE_OP_32_64(ext16u):
2139 case INDEX_op_ext32u_i64:
2140 case INDEX_op_extu_i32_i64:
2141 case INDEX_op_extrl_i64_i32:
2142 case INDEX_op_extrh_i64_i32:
2143 done = fold_extu(&ctx, op);
2145 CASE_OP_32_64(ld8s):
2146 CASE_OP_32_64(ld8u):
2147 CASE_OP_32_64(ld16s):
2148 CASE_OP_32_64(ld16u):
2149 case INDEX_op_ld32s_i64:
2150 case INDEX_op_ld32u_i64:
2151 done = fold_tcg_ld(&ctx, op);
2154 done = fold_mb(&ctx, op);
2156 CASE_OP_32_64_VEC(mov):
2157 done = fold_mov(&ctx, op);
2159 CASE_OP_32_64(movcond):
2160 done = fold_movcond(&ctx, op);
2163 done = fold_mul(&ctx, op);
2165 CASE_OP_32_64(mulsh):
2166 CASE_OP_32_64(muluh):
2167 done = fold_mul_highpart(&ctx, op);
2169 CASE_OP_32_64(muls2):
2170 CASE_OP_32_64(mulu2):
2171 done = fold_multiply2(&ctx, op);
2173 CASE_OP_32_64_VEC(nand):
2174 done = fold_nand(&ctx, op);
2177 done = fold_neg(&ctx, op);
2179 CASE_OP_32_64_VEC(nor):
2180 done = fold_nor(&ctx, op);
2182 CASE_OP_32_64_VEC(not):
2183 done = fold_not(&ctx, op);
2185 CASE_OP_32_64_VEC(or):
2186 done = fold_or(&ctx, op);
2188 CASE_OP_32_64_VEC(orc):
2189 done = fold_orc(&ctx, op);
2191 case INDEX_op_qemu_ld_i32:
2192 case INDEX_op_qemu_ld_i64:
2193 done = fold_qemu_ld(&ctx, op);
2195 case INDEX_op_qemu_st_i32:
2196 case INDEX_op_qemu_st8_i32:
2197 case INDEX_op_qemu_st_i64:
2198 done = fold_qemu_st(&ctx, op);
2201 CASE_OP_32_64(remu):
2202 done = fold_remainder(&ctx, op);
2204 CASE_OP_32_64(rotl):
2205 CASE_OP_32_64(rotr):
2209 done = fold_shift(&ctx, op);
2211 CASE_OP_32_64(setcond):
2212 done = fold_setcond(&ctx, op);
2214 case INDEX_op_setcond2_i32:
2215 done = fold_setcond2(&ctx, op);
2217 CASE_OP_32_64(sextract):
2218 done = fold_sextract(&ctx, op);
2221 done = fold_sub(&ctx, op);
2223 case INDEX_op_sub_vec:
2224 done = fold_sub_vec(&ctx, op);
2226 CASE_OP_32_64(sub2):
2227 done = fold_sub2(&ctx, op);
2229 CASE_OP_32_64_VEC(xor):
2230 done = fold_xor(&ctx, op);
2237 finish_folding(&ctx, op);