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-common.h"
28 #include "exec/cpu-common.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 struct tcg_temp_info
{
40 tcg_target_ulong mask
;
43 static struct tcg_temp_info temps
[TCG_MAX_TEMPS
];
44 static TCGTempSet temps_used
;
46 static inline bool temp_is_const(TCGArg arg
)
48 return temps
[arg
].is_const
;
51 static inline bool temp_is_copy(TCGArg arg
)
53 return temps
[arg
].next_copy
!= arg
;
56 /* Reset TEMP's state, possibly removing the temp for the list of copies. */
57 static void reset_temp(TCGArg temp
)
59 temps
[temps
[temp
].next_copy
].prev_copy
= temps
[temp
].prev_copy
;
60 temps
[temps
[temp
].prev_copy
].next_copy
= temps
[temp
].next_copy
;
61 temps
[temp
].next_copy
= temp
;
62 temps
[temp
].prev_copy
= temp
;
63 temps
[temp
].is_const
= false;
64 temps
[temp
].mask
= -1;
67 /* Reset all temporaries, given that there are NB_TEMPS of them. */
68 static void reset_all_temps(int nb_temps
)
70 bitmap_zero(temps_used
.l
, nb_temps
);
73 /* Initialize and activate a temporary. */
74 static void init_temp_info(TCGArg temp
)
76 if (!test_bit(temp
, temps_used
.l
)) {
77 temps
[temp
].next_copy
= temp
;
78 temps
[temp
].prev_copy
= temp
;
79 temps
[temp
].is_const
= false;
80 temps
[temp
].mask
= -1;
81 set_bit(temp
, temps_used
.l
);
85 static int op_bits(TCGOpcode op
)
87 const TCGOpDef
*def
= &tcg_op_defs
[op
];
88 return def
->flags
& TCG_OPF_64BIT
? 64 : 32;
91 static TCGOpcode
op_to_mov(TCGOpcode op
)
93 switch (op_bits(op
)) {
95 return INDEX_op_mov_i32
;
97 return INDEX_op_mov_i64
;
99 fprintf(stderr
, "op_to_mov: unexpected return value of "
100 "function op_bits.\n");
105 static TCGOpcode
op_to_movi(TCGOpcode op
)
107 switch (op_bits(op
)) {
109 return INDEX_op_movi_i32
;
111 return INDEX_op_movi_i64
;
113 fprintf(stderr
, "op_to_movi: unexpected return value of "
114 "function op_bits.\n");
119 static TCGArg
find_better_copy(TCGContext
*s
, TCGArg temp
)
123 /* If this is already a global, we can't do better. */
124 if (temp
< s
->nb_globals
) {
128 /* Search for a global first. */
129 for (i
= temps
[temp
].next_copy
; i
!= temp
; i
= temps
[i
].next_copy
) {
130 if (i
< s
->nb_globals
) {
135 /* If it is a temp, search for a temp local. */
136 if (!s
->temps
[temp
].temp_local
) {
137 for (i
= temps
[temp
].next_copy
; i
!= temp
; i
= temps
[i
].next_copy
) {
138 if (s
->temps
[i
].temp_local
) {
144 /* Failure to find a better representation, return the same temp. */
148 static bool temps_are_copies(TCGArg arg1
, TCGArg arg2
)
156 if (!temp_is_copy(arg1
) || !temp_is_copy(arg2
)) {
160 for (i
= temps
[arg1
].next_copy
; i
!= arg1
; i
= temps
[i
].next_copy
) {
169 static void tcg_opt_gen_movi(TCGContext
*s
, TCGOp
*op
, TCGArg dst
, TCGArg val
)
171 TCGOpcode new_op
= op_to_movi(op
->opc
);
172 tcg_target_ulong mask
;
177 temps
[dst
].is_const
= true;
178 temps
[dst
].val
= val
;
180 if (TCG_TARGET_REG_BITS
> 32 && new_op
== INDEX_op_movi_i32
) {
181 /* High bits of the destination are now garbage. */
182 mask
|= ~0xffffffffull
;
184 temps
[dst
].mask
= mask
;
190 static void tcg_opt_gen_mov(TCGContext
*s
, TCGOp
*op
, TCGArg dst
, TCGArg src
)
192 if (temps_are_copies(dst
, src
)) {
193 tcg_op_remove(s
, op
);
197 TCGOpcode new_op
= op_to_mov(op
->opc
);
198 tcg_target_ulong mask
;
203 mask
= temps
[src
].mask
;
204 if (TCG_TARGET_REG_BITS
> 32 && new_op
== INDEX_op_mov_i32
) {
205 /* High bits of the destination are now garbage. */
206 mask
|= ~0xffffffffull
;
208 temps
[dst
].mask
= mask
;
210 if (s
->temps
[src
].type
== s
->temps
[dst
].type
) {
211 temps
[dst
].next_copy
= temps
[src
].next_copy
;
212 temps
[dst
].prev_copy
= src
;
213 temps
[temps
[dst
].next_copy
].prev_copy
= dst
;
214 temps
[src
].next_copy
= dst
;
215 temps
[dst
].is_const
= temps
[src
].is_const
;
216 temps
[dst
].val
= temps
[src
].val
;
223 static TCGArg
do_constant_folding_2(TCGOpcode op
, TCGArg x
, TCGArg y
)
246 case INDEX_op_shl_i32
:
247 return (uint32_t)x
<< (y
& 31);
249 case INDEX_op_shl_i64
:
250 return (uint64_t)x
<< (y
& 63);
252 case INDEX_op_shr_i32
:
253 return (uint32_t)x
>> (y
& 31);
255 case INDEX_op_shr_i64
:
256 return (uint64_t)x
>> (y
& 63);
258 case INDEX_op_sar_i32
:
259 return (int32_t)x
>> (y
& 31);
261 case INDEX_op_sar_i64
:
262 return (int64_t)x
>> (y
& 63);
264 case INDEX_op_rotr_i32
:
265 return ror32(x
, y
& 31);
267 case INDEX_op_rotr_i64
:
268 return ror64(x
, y
& 63);
270 case INDEX_op_rotl_i32
:
271 return rol32(x
, y
& 31);
273 case INDEX_op_rotl_i64
:
274 return rol64(x
, y
& 63);
297 case INDEX_op_clz_i32
:
298 return (uint32_t)x
? clz32(x
) : y
;
300 case INDEX_op_clz_i64
:
301 return x
? clz64(x
) : y
;
303 case INDEX_op_ctz_i32
:
304 return (uint32_t)x
? ctz32(x
) : y
;
306 case INDEX_op_ctz_i64
:
307 return x
? ctz64(x
) : y
;
309 case INDEX_op_ctpop_i32
:
312 case INDEX_op_ctpop_i64
:
315 CASE_OP_32_64(ext8s
):
318 CASE_OP_32_64(ext16s
):
321 CASE_OP_32_64(ext8u
):
324 CASE_OP_32_64(ext16u
):
327 case INDEX_op_ext_i32_i64
:
328 case INDEX_op_ext32s_i64
:
331 case INDEX_op_extu_i32_i64
:
332 case INDEX_op_extrl_i64_i32
:
333 case INDEX_op_ext32u_i64
:
336 case INDEX_op_extrh_i64_i32
:
337 return (uint64_t)x
>> 32;
339 case INDEX_op_muluh_i32
:
340 return ((uint64_t)(uint32_t)x
* (uint32_t)y
) >> 32;
341 case INDEX_op_mulsh_i32
:
342 return ((int64_t)(int32_t)x
* (int32_t)y
) >> 32;
344 case INDEX_op_muluh_i64
:
345 mulu64(&l64
, &h64
, x
, y
);
347 case INDEX_op_mulsh_i64
:
348 muls64(&l64
, &h64
, x
, y
);
351 case INDEX_op_div_i32
:
352 /* Avoid crashing on divide by zero, otherwise undefined. */
353 return (int32_t)x
/ ((int32_t)y
? : 1);
354 case INDEX_op_divu_i32
:
355 return (uint32_t)x
/ ((uint32_t)y
? : 1);
356 case INDEX_op_div_i64
:
357 return (int64_t)x
/ ((int64_t)y
? : 1);
358 case INDEX_op_divu_i64
:
359 return (uint64_t)x
/ ((uint64_t)y
? : 1);
361 case INDEX_op_rem_i32
:
362 return (int32_t)x
% ((int32_t)y
? : 1);
363 case INDEX_op_remu_i32
:
364 return (uint32_t)x
% ((uint32_t)y
? : 1);
365 case INDEX_op_rem_i64
:
366 return (int64_t)x
% ((int64_t)y
? : 1);
367 case INDEX_op_remu_i64
:
368 return (uint64_t)x
% ((uint64_t)y
? : 1);
372 "Unrecognized operation %d in do_constant_folding.\n", op
);
377 static TCGArg
do_constant_folding(TCGOpcode op
, TCGArg x
, TCGArg y
)
379 TCGArg res
= do_constant_folding_2(op
, x
, y
);
380 if (op_bits(op
) == 32) {
386 static bool do_constant_folding_cond_32(uint32_t x
, uint32_t y
, TCGCond c
)
394 return (int32_t)x
< (int32_t)y
;
396 return (int32_t)x
>= (int32_t)y
;
398 return (int32_t)x
<= (int32_t)y
;
400 return (int32_t)x
> (int32_t)y
;
414 static bool do_constant_folding_cond_64(uint64_t x
, uint64_t y
, TCGCond c
)
422 return (int64_t)x
< (int64_t)y
;
424 return (int64_t)x
>= (int64_t)y
;
426 return (int64_t)x
<= (int64_t)y
;
428 return (int64_t)x
> (int64_t)y
;
442 static bool do_constant_folding_cond_eq(TCGCond c
)
462 /* Return 2 if the condition can't be simplified, and the result
463 of the condition (0 or 1) if it can */
464 static TCGArg
do_constant_folding_cond(TCGOpcode op
, TCGArg x
,
467 if (temp_is_const(x
) && temp_is_const(y
)) {
468 switch (op_bits(op
)) {
470 return do_constant_folding_cond_32(temps
[x
].val
, temps
[y
].val
, c
);
472 return do_constant_folding_cond_64(temps
[x
].val
, temps
[y
].val
, c
);
476 } else if (temps_are_copies(x
, y
)) {
477 return do_constant_folding_cond_eq(c
);
478 } else if (temp_is_const(y
) && temps
[y
].val
== 0) {
491 /* Return 2 if the condition can't be simplified, and the result
492 of the condition (0 or 1) if it can */
493 static TCGArg
do_constant_folding_cond2(TCGArg
*p1
, TCGArg
*p2
, TCGCond c
)
495 TCGArg al
= p1
[0], ah
= p1
[1];
496 TCGArg bl
= p2
[0], bh
= p2
[1];
498 if (temp_is_const(bl
) && temp_is_const(bh
)) {
499 uint64_t b
= ((uint64_t)temps
[bh
].val
<< 32) | (uint32_t)temps
[bl
].val
;
501 if (temp_is_const(al
) && temp_is_const(ah
)) {
503 a
= ((uint64_t)temps
[ah
].val
<< 32) | (uint32_t)temps
[al
].val
;
504 return do_constant_folding_cond_64(a
, b
, c
);
517 if (temps_are_copies(al
, bl
) && temps_are_copies(ah
, bh
)) {
518 return do_constant_folding_cond_eq(c
);
523 static bool swap_commutative(TCGArg dest
, TCGArg
*p1
, TCGArg
*p2
)
525 TCGArg a1
= *p1
, a2
= *p2
;
527 sum
+= temp_is_const(a1
);
528 sum
-= temp_is_const(a2
);
530 /* Prefer the constant in second argument, and then the form
531 op a, a, b, which is better handled on non-RISC hosts. */
532 if (sum
> 0 || (sum
== 0 && dest
== a2
)) {
540 static bool swap_commutative2(TCGArg
*p1
, TCGArg
*p2
)
543 sum
+= temp_is_const(p1
[0]);
544 sum
+= temp_is_const(p1
[1]);
545 sum
-= temp_is_const(p2
[0]);
546 sum
-= temp_is_const(p2
[1]);
549 t
= p1
[0], p1
[0] = p2
[0], p2
[0] = t
;
550 t
= p1
[1], p1
[1] = p2
[1], p2
[1] = t
;
556 /* Propagate constants and copies, fold constant expressions. */
557 void tcg_optimize(TCGContext
*s
)
559 int oi
, oi_next
, nb_temps
, nb_globals
;
560 TCGOp
*prev_mb
= NULL
;
562 /* Array VALS has an element for each temp.
563 If this temp holds a constant then its value is kept in VALS' element.
564 If this temp is a copy of other ones then the other copies are
565 available through the doubly linked circular list. */
567 nb_temps
= s
->nb_temps
;
568 nb_globals
= s
->nb_globals
;
569 reset_all_temps(nb_temps
);
571 for (oi
= s
->gen_op_buf
[0].next
; oi
!= 0; oi
= oi_next
) {
572 tcg_target_ulong mask
, partmask
, affected
;
573 int nb_oargs
, nb_iargs
, i
;
576 TCGOp
* const op
= &s
->gen_op_buf
[oi
];
577 TCGOpcode opc
= op
->opc
;
578 const TCGOpDef
*def
= &tcg_op_defs
[opc
];
582 /* Count the arguments, and initialize the temps that are
584 if (opc
== INDEX_op_call
) {
585 nb_oargs
= op
->callo
;
586 nb_iargs
= op
->calli
;
587 for (i
= 0; i
< nb_oargs
+ nb_iargs
; i
++) {
589 if (tmp
!= TCG_CALL_DUMMY_ARG
) {
594 nb_oargs
= def
->nb_oargs
;
595 nb_iargs
= def
->nb_iargs
;
596 for (i
= 0; i
< nb_oargs
+ nb_iargs
; i
++) {
597 init_temp_info(op
->args
[i
]);
601 /* Do copy propagation */
602 for (i
= nb_oargs
; i
< nb_oargs
+ nb_iargs
; i
++) {
603 if (temp_is_copy(op
->args
[i
])) {
604 op
->args
[i
] = find_better_copy(s
, op
->args
[i
]);
608 /* For commutative operations make constant second argument */
618 CASE_OP_32_64(muluh
):
619 CASE_OP_32_64(mulsh
):
620 swap_commutative(op
->args
[0], &op
->args
[1], &op
->args
[2]);
622 CASE_OP_32_64(brcond
):
623 if (swap_commutative(-1, &op
->args
[0], &op
->args
[1])) {
624 op
->args
[2] = tcg_swap_cond(op
->args
[2]);
627 CASE_OP_32_64(setcond
):
628 if (swap_commutative(op
->args
[0], &op
->args
[1], &op
->args
[2])) {
629 op
->args
[3] = tcg_swap_cond(op
->args
[3]);
632 CASE_OP_32_64(movcond
):
633 if (swap_commutative(-1, &op
->args
[1], &op
->args
[2])) {
634 op
->args
[5] = tcg_swap_cond(op
->args
[5]);
636 /* For movcond, we canonicalize the "false" input reg to match
637 the destination reg so that the tcg backend can implement
638 a "move if true" operation. */
639 if (swap_commutative(op
->args
[0], &op
->args
[4], &op
->args
[3])) {
640 op
->args
[5] = tcg_invert_cond(op
->args
[5]);
644 swap_commutative(op
->args
[0], &op
->args
[2], &op
->args
[4]);
645 swap_commutative(op
->args
[1], &op
->args
[3], &op
->args
[5]);
647 CASE_OP_32_64(mulu2
):
648 CASE_OP_32_64(muls2
):
649 swap_commutative(op
->args
[0], &op
->args
[2], &op
->args
[3]);
651 case INDEX_op_brcond2_i32
:
652 if (swap_commutative2(&op
->args
[0], &op
->args
[2])) {
653 op
->args
[4] = tcg_swap_cond(op
->args
[4]);
656 case INDEX_op_setcond2_i32
:
657 if (swap_commutative2(&op
->args
[1], &op
->args
[3])) {
658 op
->args
[5] = tcg_swap_cond(op
->args
[5]);
665 /* Simplify expressions for "shift/rot r, 0, a => movi r, 0",
666 and "sub r, 0, a => neg r, a" case. */
673 if (temp_is_const(op
->args
[1]) && temps
[op
->args
[1]].val
== 0) {
674 tcg_opt_gen_movi(s
, op
, op
->args
[0], 0);
683 if (temp_is_const(op
->args
[2])) {
684 /* Proceed with possible constant folding. */
687 if (opc
== INDEX_op_sub_i32
) {
688 neg_op
= INDEX_op_neg_i32
;
689 have_neg
= TCG_TARGET_HAS_neg_i32
;
691 neg_op
= INDEX_op_neg_i64
;
692 have_neg
= TCG_TARGET_HAS_neg_i64
;
697 if (temp_is_const(op
->args
[1])
698 && temps
[op
->args
[1]].val
== 0) {
700 reset_temp(op
->args
[0]);
701 op
->args
[1] = op
->args
[2];
708 if (!temp_is_const(op
->args
[1])
709 && temp_is_const(op
->args
[2])
710 && temps
[op
->args
[2]].val
== -1) {
716 if (!temp_is_const(op
->args
[1])
717 && temp_is_const(op
->args
[2])
718 && temps
[op
->args
[2]].val
== 0) {
724 if (!temp_is_const(op
->args
[2])
725 && temp_is_const(op
->args
[1])
726 && temps
[op
->args
[1]].val
== -1) {
733 if (!temp_is_const(op
->args
[2])
734 && temp_is_const(op
->args
[1])
735 && temps
[op
->args
[1]].val
== 0) {
745 if (def
->flags
& TCG_OPF_64BIT
) {
746 not_op
= INDEX_op_not_i64
;
747 have_not
= TCG_TARGET_HAS_not_i64
;
749 not_op
= INDEX_op_not_i32
;
750 have_not
= TCG_TARGET_HAS_not_i32
;
756 reset_temp(op
->args
[0]);
757 op
->args
[1] = op
->args
[i
];
764 /* Simplify expression for "op r, a, const => mov r, a" cases */
776 if (!temp_is_const(op
->args
[1])
777 && temp_is_const(op
->args
[2])
778 && temps
[op
->args
[2]].val
== 0) {
779 tcg_opt_gen_mov(s
, op
, op
->args
[0], op
->args
[1]);
786 if (!temp_is_const(op
->args
[1])
787 && temp_is_const(op
->args
[2])
788 && temps
[op
->args
[2]].val
== -1) {
789 tcg_opt_gen_mov(s
, op
, op
->args
[0], op
->args
[1]);
797 /* Simplify using known-zero bits. Currently only ops with a single
798 output argument is supported. */
802 CASE_OP_32_64(ext8s
):
803 if ((temps
[op
->args
[1]].mask
& 0x80) != 0) {
806 CASE_OP_32_64(ext8u
):
809 CASE_OP_32_64(ext16s
):
810 if ((temps
[op
->args
[1]].mask
& 0x8000) != 0) {
813 CASE_OP_32_64(ext16u
):
816 case INDEX_op_ext32s_i64
:
817 if ((temps
[op
->args
[1]].mask
& 0x80000000) != 0) {
820 case INDEX_op_ext32u_i64
:
825 mask
= temps
[op
->args
[2]].mask
;
826 if (temp_is_const(op
->args
[2])) {
828 affected
= temps
[op
->args
[1]].mask
& ~mask
;
830 mask
= temps
[op
->args
[1]].mask
& mask
;
833 case INDEX_op_ext_i32_i64
:
834 if ((temps
[op
->args
[1]].mask
& 0x80000000) != 0) {
837 case INDEX_op_extu_i32_i64
:
838 /* We do not compute affected as it is a size changing op. */
839 mask
= (uint32_t)temps
[op
->args
[1]].mask
;
843 /* Known-zeros does not imply known-ones. Therefore unless
844 op->args[2] is constant, we can't infer anything from it. */
845 if (temp_is_const(op
->args
[2])) {
846 mask
= ~temps
[op
->args
[2]].mask
;
849 /* But we certainly know nothing outside op->args[1] may be set. */
850 mask
= temps
[op
->args
[1]].mask
;
853 case INDEX_op_sar_i32
:
854 if (temp_is_const(op
->args
[2])) {
855 tmp
= temps
[op
->args
[2]].val
& 31;
856 mask
= (int32_t)temps
[op
->args
[1]].mask
>> tmp
;
859 case INDEX_op_sar_i64
:
860 if (temp_is_const(op
->args
[2])) {
861 tmp
= temps
[op
->args
[2]].val
& 63;
862 mask
= (int64_t)temps
[op
->args
[1]].mask
>> tmp
;
866 case INDEX_op_shr_i32
:
867 if (temp_is_const(op
->args
[2])) {
868 tmp
= temps
[op
->args
[2]].val
& 31;
869 mask
= (uint32_t)temps
[op
->args
[1]].mask
>> tmp
;
872 case INDEX_op_shr_i64
:
873 if (temp_is_const(op
->args
[2])) {
874 tmp
= temps
[op
->args
[2]].val
& 63;
875 mask
= (uint64_t)temps
[op
->args
[1]].mask
>> tmp
;
879 case INDEX_op_extrl_i64_i32
:
880 mask
= (uint32_t)temps
[op
->args
[1]].mask
;
882 case INDEX_op_extrh_i64_i32
:
883 mask
= (uint64_t)temps
[op
->args
[1]].mask
>> 32;
887 if (temp_is_const(op
->args
[2])) {
888 tmp
= temps
[op
->args
[2]].val
& (TCG_TARGET_REG_BITS
- 1);
889 mask
= temps
[op
->args
[1]].mask
<< tmp
;
894 /* Set to 1 all bits to the left of the rightmost. */
895 mask
= -(temps
[op
->args
[1]].mask
& -temps
[op
->args
[1]].mask
);
898 CASE_OP_32_64(deposit
):
899 mask
= deposit64(temps
[op
->args
[1]].mask
, op
->args
[3],
900 op
->args
[4], temps
[op
->args
[2]].mask
);
903 CASE_OP_32_64(extract
):
904 mask
= extract64(temps
[op
->args
[1]].mask
, op
->args
[2], op
->args
[3]);
905 if (op
->args
[2] == 0) {
906 affected
= temps
[op
->args
[1]].mask
& ~mask
;
909 CASE_OP_32_64(sextract
):
910 mask
= sextract64(temps
[op
->args
[1]].mask
,
911 op
->args
[2], op
->args
[3]);
912 if (op
->args
[2] == 0 && (tcg_target_long
)mask
>= 0) {
913 affected
= temps
[op
->args
[1]].mask
& ~mask
;
919 mask
= temps
[op
->args
[1]].mask
| temps
[op
->args
[2]].mask
;
922 case INDEX_op_clz_i32
:
923 case INDEX_op_ctz_i32
:
924 mask
= temps
[op
->args
[2]].mask
| 31;
927 case INDEX_op_clz_i64
:
928 case INDEX_op_ctz_i64
:
929 mask
= temps
[op
->args
[2]].mask
| 63;
932 case INDEX_op_ctpop_i32
:
935 case INDEX_op_ctpop_i64
:
939 CASE_OP_32_64(setcond
):
940 case INDEX_op_setcond2_i32
:
944 CASE_OP_32_64(movcond
):
945 mask
= temps
[op
->args
[3]].mask
| temps
[op
->args
[4]].mask
;
951 CASE_OP_32_64(ld16u
):
954 case INDEX_op_ld32u_i64
:
958 CASE_OP_32_64(qemu_ld
):
960 TCGMemOpIdx oi
= op
->args
[nb_oargs
+ nb_iargs
];
961 TCGMemOp mop
= get_memop(oi
);
962 if (!(mop
& MO_SIGN
)) {
963 mask
= (2ULL << ((8 << (mop
& MO_SIZE
)) - 1)) - 1;
972 /* 32-bit ops generate 32-bit results. For the result is zero test
973 below, we can ignore high bits, but for further optimizations we
974 need to record that the high bits contain garbage. */
976 if (!(def
->flags
& TCG_OPF_64BIT
)) {
977 mask
|= ~(tcg_target_ulong
)0xffffffffu
;
978 partmask
&= 0xffffffffu
;
979 affected
&= 0xffffffffu
;
983 tcg_debug_assert(nb_oargs
== 1);
984 tcg_opt_gen_movi(s
, op
, op
->args
[0], 0);
988 tcg_debug_assert(nb_oargs
== 1);
989 tcg_opt_gen_mov(s
, op
, op
->args
[0], op
->args
[1]);
993 /* Simplify expression for "op r, a, 0 => movi r, 0" cases */
997 CASE_OP_32_64(muluh
):
998 CASE_OP_32_64(mulsh
):
999 if ((temp_is_const(op
->args
[2]) && temps
[op
->args
[2]].val
== 0)) {
1000 tcg_opt_gen_movi(s
, op
, op
->args
[0], 0);
1008 /* Simplify expression for "op r, a, a => mov r, a" cases */
1012 if (temps_are_copies(op
->args
[1], op
->args
[2])) {
1013 tcg_opt_gen_mov(s
, op
, op
->args
[0], op
->args
[1]);
1021 /* Simplify expression for "op r, a, a => movi r, 0" cases */
1023 CASE_OP_32_64(andc
):
1026 if (temps_are_copies(op
->args
[1], op
->args
[2])) {
1027 tcg_opt_gen_movi(s
, op
, op
->args
[0], 0);
1035 /* Propagate constants through copy operations and do constant
1036 folding. Constants will be substituted to arguments by register
1037 allocator where needed and possible. Also detect copies. */
1040 tcg_opt_gen_mov(s
, op
, op
->args
[0], op
->args
[1]);
1042 CASE_OP_32_64(movi
):
1043 tcg_opt_gen_movi(s
, op
, op
->args
[0], op
->args
[1]);
1048 CASE_OP_32_64(ext8s
):
1049 CASE_OP_32_64(ext8u
):
1050 CASE_OP_32_64(ext16s
):
1051 CASE_OP_32_64(ext16u
):
1052 CASE_OP_32_64(ctpop
):
1053 case INDEX_op_ext32s_i64
:
1054 case INDEX_op_ext32u_i64
:
1055 case INDEX_op_ext_i32_i64
:
1056 case INDEX_op_extu_i32_i64
:
1057 case INDEX_op_extrl_i64_i32
:
1058 case INDEX_op_extrh_i64_i32
:
1059 if (temp_is_const(op
->args
[1])) {
1060 tmp
= do_constant_folding(opc
, temps
[op
->args
[1]].val
, 0);
1061 tcg_opt_gen_movi(s
, op
, op
->args
[0], tmp
);
1075 CASE_OP_32_64(rotl
):
1076 CASE_OP_32_64(rotr
):
1077 CASE_OP_32_64(andc
):
1080 CASE_OP_32_64(nand
):
1082 CASE_OP_32_64(muluh
):
1083 CASE_OP_32_64(mulsh
):
1085 CASE_OP_32_64(divu
):
1087 CASE_OP_32_64(remu
):
1088 if (temp_is_const(op
->args
[1]) && temp_is_const(op
->args
[2])) {
1089 tmp
= do_constant_folding(opc
, temps
[op
->args
[1]].val
,
1090 temps
[op
->args
[2]].val
);
1091 tcg_opt_gen_movi(s
, op
, op
->args
[0], tmp
);
1098 if (temp_is_const(op
->args
[1])) {
1099 TCGArg v
= temps
[op
->args
[1]].val
;
1101 tmp
= do_constant_folding(opc
, v
, 0);
1102 tcg_opt_gen_movi(s
, op
, op
->args
[0], tmp
);
1104 tcg_opt_gen_mov(s
, op
, op
->args
[0], op
->args
[2]);
1110 CASE_OP_32_64(deposit
):
1111 if (temp_is_const(op
->args
[1]) && temp_is_const(op
->args
[2])) {
1112 tmp
= deposit64(temps
[op
->args
[1]].val
, op
->args
[3],
1113 op
->args
[4], temps
[op
->args
[2]].val
);
1114 tcg_opt_gen_movi(s
, op
, op
->args
[0], tmp
);
1119 CASE_OP_32_64(extract
):
1120 if (temp_is_const(op
->args
[1])) {
1121 tmp
= extract64(temps
[op
->args
[1]].val
,
1122 op
->args
[2], op
->args
[3]);
1123 tcg_opt_gen_movi(s
, op
, op
->args
[0], tmp
);
1128 CASE_OP_32_64(sextract
):
1129 if (temp_is_const(op
->args
[1])) {
1130 tmp
= sextract64(temps
[op
->args
[1]].val
,
1131 op
->args
[2], op
->args
[3]);
1132 tcg_opt_gen_movi(s
, op
, op
->args
[0], tmp
);
1137 CASE_OP_32_64(setcond
):
1138 tmp
= do_constant_folding_cond(opc
, op
->args
[1],
1139 op
->args
[2], op
->args
[3]);
1141 tcg_opt_gen_movi(s
, op
, op
->args
[0], tmp
);
1146 CASE_OP_32_64(brcond
):
1147 tmp
= do_constant_folding_cond(opc
, op
->args
[0],
1148 op
->args
[1], op
->args
[2]);
1151 reset_all_temps(nb_temps
);
1152 op
->opc
= INDEX_op_br
;
1153 op
->args
[0] = op
->args
[3];
1155 tcg_op_remove(s
, op
);
1161 CASE_OP_32_64(movcond
):
1162 tmp
= do_constant_folding_cond(opc
, op
->args
[1],
1163 op
->args
[2], op
->args
[5]);
1165 tcg_opt_gen_mov(s
, op
, op
->args
[0], op
->args
[4-tmp
]);
1168 if (temp_is_const(op
->args
[3]) && temp_is_const(op
->args
[4])) {
1169 tcg_target_ulong tv
= temps
[op
->args
[3]].val
;
1170 tcg_target_ulong fv
= temps
[op
->args
[4]].val
;
1171 TCGCond cond
= op
->args
[5];
1172 if (fv
== 1 && tv
== 0) {
1173 cond
= tcg_invert_cond(cond
);
1174 } else if (!(tv
== 1 && fv
== 0)) {
1178 op
->opc
= opc
= (opc
== INDEX_op_movcond_i32
1179 ? INDEX_op_setcond_i32
1180 : INDEX_op_setcond_i64
);
1185 case INDEX_op_add2_i32
:
1186 case INDEX_op_sub2_i32
:
1187 if (temp_is_const(op
->args
[2]) && temp_is_const(op
->args
[3])
1188 && temp_is_const(op
->args
[4]) && temp_is_const(op
->args
[5])) {
1189 uint32_t al
= temps
[op
->args
[2]].val
;
1190 uint32_t ah
= temps
[op
->args
[3]].val
;
1191 uint32_t bl
= temps
[op
->args
[4]].val
;
1192 uint32_t bh
= temps
[op
->args
[5]].val
;
1193 uint64_t a
= ((uint64_t)ah
<< 32) | al
;
1194 uint64_t b
= ((uint64_t)bh
<< 32) | bl
;
1196 TCGOp
*op2
= tcg_op_insert_before(s
, op
, INDEX_op_movi_i32
, 2);
1198 if (opc
== INDEX_op_add2_i32
) {
1206 tcg_opt_gen_movi(s
, op
, rl
, (int32_t)a
);
1207 tcg_opt_gen_movi(s
, op2
, rh
, (int32_t)(a
>> 32));
1209 /* We've done all we need to do with the movi. Skip it. */
1210 oi_next
= op2
->next
;
1215 case INDEX_op_mulu2_i32
:
1216 if (temp_is_const(op
->args
[2]) && temp_is_const(op
->args
[3])) {
1217 uint32_t a
= temps
[op
->args
[2]].val
;
1218 uint32_t b
= temps
[op
->args
[3]].val
;
1219 uint64_t r
= (uint64_t)a
* b
;
1221 TCGOp
*op2
= tcg_op_insert_before(s
, op
, INDEX_op_movi_i32
, 2);
1225 tcg_opt_gen_movi(s
, op
, rl
, (int32_t)r
);
1226 tcg_opt_gen_movi(s
, op2
, rh
, (int32_t)(r
>> 32));
1228 /* We've done all we need to do with the movi. Skip it. */
1229 oi_next
= op2
->next
;
1234 case INDEX_op_brcond2_i32
:
1235 tmp
= do_constant_folding_cond2(&op
->args
[0], &op
->args
[2],
1240 reset_all_temps(nb_temps
);
1241 op
->opc
= INDEX_op_br
;
1242 op
->args
[0] = op
->args
[5];
1245 tcg_op_remove(s
, op
);
1247 } else if ((op
->args
[4] == TCG_COND_LT
1248 || op
->args
[4] == TCG_COND_GE
)
1249 && temp_is_const(op
->args
[2])
1250 && temps
[op
->args
[2]].val
== 0
1251 && temp_is_const(op
->args
[3])
1252 && temps
[op
->args
[3]].val
== 0) {
1253 /* Simplify LT/GE comparisons vs zero to a single compare
1254 vs the high word of the input. */
1256 reset_all_temps(nb_temps
);
1257 op
->opc
= INDEX_op_brcond_i32
;
1258 op
->args
[0] = op
->args
[1];
1259 op
->args
[1] = op
->args
[3];
1260 op
->args
[2] = op
->args
[4];
1261 op
->args
[3] = op
->args
[5];
1262 } else if (op
->args
[4] == TCG_COND_EQ
) {
1263 /* Simplify EQ comparisons where one of the pairs
1264 can be simplified. */
1265 tmp
= do_constant_folding_cond(INDEX_op_brcond_i32
,
1266 op
->args
[0], op
->args
[2],
1269 goto do_brcond_false
;
1270 } else if (tmp
== 1) {
1271 goto do_brcond_high
;
1273 tmp
= do_constant_folding_cond(INDEX_op_brcond_i32
,
1274 op
->args
[1], op
->args
[3],
1277 goto do_brcond_false
;
1278 } else if (tmp
!= 1) {
1282 reset_all_temps(nb_temps
);
1283 op
->opc
= INDEX_op_brcond_i32
;
1284 op
->args
[1] = op
->args
[2];
1285 op
->args
[2] = op
->args
[4];
1286 op
->args
[3] = op
->args
[5];
1287 } else if (op
->args
[4] == TCG_COND_NE
) {
1288 /* Simplify NE comparisons where one of the pairs
1289 can be simplified. */
1290 tmp
= do_constant_folding_cond(INDEX_op_brcond_i32
,
1291 op
->args
[0], op
->args
[2],
1294 goto do_brcond_high
;
1295 } else if (tmp
== 1) {
1296 goto do_brcond_true
;
1298 tmp
= do_constant_folding_cond(INDEX_op_brcond_i32
,
1299 op
->args
[1], op
->args
[3],
1303 } else if (tmp
== 1) {
1304 goto do_brcond_true
;
1312 case INDEX_op_setcond2_i32
:
1313 tmp
= do_constant_folding_cond2(&op
->args
[1], &op
->args
[3],
1317 tcg_opt_gen_movi(s
, op
, op
->args
[0], tmp
);
1318 } else if ((op
->args
[5] == TCG_COND_LT
1319 || op
->args
[5] == TCG_COND_GE
)
1320 && temp_is_const(op
->args
[3])
1321 && temps
[op
->args
[3]].val
== 0
1322 && temp_is_const(op
->args
[4])
1323 && temps
[op
->args
[4]].val
== 0) {
1324 /* Simplify LT/GE comparisons vs zero to a single compare
1325 vs the high word of the input. */
1327 reset_temp(op
->args
[0]);
1328 temps
[op
->args
[0]].mask
= 1;
1329 op
->opc
= INDEX_op_setcond_i32
;
1330 op
->args
[1] = op
->args
[2];
1331 op
->args
[2] = op
->args
[4];
1332 op
->args
[3] = op
->args
[5];
1333 } else if (op
->args
[5] == TCG_COND_EQ
) {
1334 /* Simplify EQ comparisons where one of the pairs
1335 can be simplified. */
1336 tmp
= do_constant_folding_cond(INDEX_op_setcond_i32
,
1337 op
->args
[1], op
->args
[3],
1340 goto do_setcond_const
;
1341 } else if (tmp
== 1) {
1342 goto do_setcond_high
;
1344 tmp
= do_constant_folding_cond(INDEX_op_setcond_i32
,
1345 op
->args
[2], op
->args
[4],
1348 goto do_setcond_high
;
1349 } else if (tmp
!= 1) {
1353 reset_temp(op
->args
[0]);
1354 temps
[op
->args
[0]].mask
= 1;
1355 op
->opc
= INDEX_op_setcond_i32
;
1356 op
->args
[2] = op
->args
[3];
1357 op
->args
[3] = op
->args
[5];
1358 } else if (op
->args
[5] == TCG_COND_NE
) {
1359 /* Simplify NE comparisons where one of the pairs
1360 can be simplified. */
1361 tmp
= do_constant_folding_cond(INDEX_op_setcond_i32
,
1362 op
->args
[1], op
->args
[3],
1365 goto do_setcond_high
;
1366 } else if (tmp
== 1) {
1367 goto do_setcond_const
;
1369 tmp
= do_constant_folding_cond(INDEX_op_setcond_i32
,
1370 op
->args
[2], op
->args
[4],
1373 goto do_setcond_low
;
1374 } else if (tmp
== 1) {
1375 goto do_setcond_const
;
1384 if (!(op
->args
[nb_oargs
+ nb_iargs
+ 1]
1385 & (TCG_CALL_NO_READ_GLOBALS
| TCG_CALL_NO_WRITE_GLOBALS
))) {
1386 for (i
= 0; i
< nb_globals
; i
++) {
1387 if (test_bit(i
, temps_used
.l
)) {
1392 goto do_reset_output
;
1396 /* Default case: we know nothing about operation (or were unable
1397 to compute the operation result) so no propagation is done.
1398 We trash everything if the operation is the end of a basic
1399 block, otherwise we only trash the output args. "mask" is
1400 the non-zero bits mask for the first output arg. */
1401 if (def
->flags
& TCG_OPF_BB_END
) {
1402 reset_all_temps(nb_temps
);
1405 for (i
= 0; i
< nb_oargs
; i
++) {
1406 reset_temp(op
->args
[i
]);
1407 /* Save the corresponding known-zero bits mask for the
1408 first output argument (only one supported so far). */
1410 temps
[op
->args
[i
]].mask
= mask
;
1417 /* Eliminate duplicate and redundant fence instructions. */
1421 /* Merge two barriers of the same type into one,
1422 * or a weaker barrier into a stronger one,
1423 * or two weaker barriers into a stronger one.
1424 * mb X; mb Y => mb X|Y
1425 * mb; strl => mb; st
1426 * ldaq; mb => ld; mb
1427 * ldaq; strl => ld; mb; st
1428 * Other combinations are also merged into a strong
1429 * barrier. This is stricter than specified but for
1430 * the purposes of TCG is better than not optimizing.
1432 prev_mb
->args
[0] |= op
->args
[0];
1433 tcg_op_remove(s
, op
);
1437 /* Opcodes that end the block stop the optimization. */
1438 if ((def
->flags
& TCG_OPF_BB_END
) == 0) {
1442 case INDEX_op_qemu_ld_i32
:
1443 case INDEX_op_qemu_ld_i64
:
1444 case INDEX_op_qemu_st_i32
:
1445 case INDEX_op_qemu_st_i64
:
1447 /* Opcodes that touch guest memory stop the optimization. */
1451 } else if (opc
== INDEX_op_mb
) {