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"
29 #include "qemu-common.h"
32 #define CASE_OP_32_64(x) \
33 glue(glue(case INDEX_op_, x), _i32): \
34 glue(glue(case INDEX_op_, x), _i64)
36 struct tcg_temp_info
{
41 tcg_target_ulong mask
;
44 static struct tcg_temp_info temps
[TCG_MAX_TEMPS
];
45 static TCGTempSet temps_used
;
47 static inline bool temp_is_const(TCGArg arg
)
49 return temps
[arg
].is_const
;
52 static inline bool temp_is_copy(TCGArg arg
)
54 return temps
[arg
].next_copy
!= arg
;
57 /* Reset TEMP's state, possibly removing the temp for the list of copies. */
58 static void reset_temp(TCGArg temp
)
60 temps
[temps
[temp
].next_copy
].prev_copy
= temps
[temp
].prev_copy
;
61 temps
[temps
[temp
].prev_copy
].next_copy
= temps
[temp
].next_copy
;
62 temps
[temp
].next_copy
= temp
;
63 temps
[temp
].prev_copy
= temp
;
64 temps
[temp
].is_const
= false;
65 temps
[temp
].mask
= -1;
68 /* Reset all temporaries, given that there are NB_TEMPS of them. */
69 static void reset_all_temps(int nb_temps
)
71 bitmap_zero(temps_used
.l
, nb_temps
);
74 /* Initialize and activate a temporary. */
75 static void init_temp_info(TCGArg temp
)
77 if (!test_bit(temp
, temps_used
.l
)) {
78 temps
[temp
].next_copy
= temp
;
79 temps
[temp
].prev_copy
= temp
;
80 temps
[temp
].is_const
= false;
81 temps
[temp
].mask
= -1;
82 set_bit(temp
, temps_used
.l
);
86 static TCGOp
*insert_op_before(TCGContext
*s
, TCGOp
*old_op
,
87 TCGOpcode opc
, int nargs
)
89 int oi
= s
->gen_next_op_idx
;
90 int pi
= s
->gen_next_parm_idx
;
91 int prev
= old_op
->prev
;
92 int next
= old_op
- s
->gen_op_buf
;
95 tcg_debug_assert(oi
< OPC_BUF_SIZE
);
96 tcg_debug_assert(pi
+ nargs
<= OPPARAM_BUF_SIZE
);
97 s
->gen_next_op_idx
= oi
+ 1;
98 s
->gen_next_parm_idx
= pi
+ nargs
;
100 new_op
= &s
->gen_op_buf
[oi
];
108 s
->gen_op_buf
[prev
].next
= oi
;
110 s
->gen_first_op_idx
= oi
;
117 static int op_bits(TCGOpcode op
)
119 const TCGOpDef
*def
= &tcg_op_defs
[op
];
120 return def
->flags
& TCG_OPF_64BIT
? 64 : 32;
123 static TCGOpcode
op_to_mov(TCGOpcode op
)
125 switch (op_bits(op
)) {
127 return INDEX_op_mov_i32
;
129 return INDEX_op_mov_i64
;
131 fprintf(stderr
, "op_to_mov: unexpected return value of "
132 "function op_bits.\n");
137 static TCGOpcode
op_to_movi(TCGOpcode op
)
139 switch (op_bits(op
)) {
141 return INDEX_op_movi_i32
;
143 return INDEX_op_movi_i64
;
145 fprintf(stderr
, "op_to_movi: unexpected return value of "
146 "function op_bits.\n");
151 static TCGArg
find_better_copy(TCGContext
*s
, TCGArg temp
)
155 /* If this is already a global, we can't do better. */
156 if (temp
< s
->nb_globals
) {
160 /* Search for a global first. */
161 for (i
= temps
[temp
].next_copy
; i
!= temp
; i
= temps
[i
].next_copy
) {
162 if (i
< s
->nb_globals
) {
167 /* If it is a temp, search for a temp local. */
168 if (!s
->temps
[temp
].temp_local
) {
169 for (i
= temps
[temp
].next_copy
; i
!= temp
; i
= temps
[i
].next_copy
) {
170 if (s
->temps
[i
].temp_local
) {
176 /* Failure to find a better representation, return the same temp. */
180 static bool temps_are_copies(TCGArg arg1
, TCGArg arg2
)
188 if (!temp_is_copy(arg1
) || !temp_is_copy(arg2
)) {
192 for (i
= temps
[arg1
].next_copy
; i
!= arg1
; i
= temps
[i
].next_copy
) {
201 static void tcg_opt_gen_movi(TCGContext
*s
, TCGOp
*op
, TCGArg
*args
,
202 TCGArg dst
, TCGArg val
)
204 TCGOpcode new_op
= op_to_movi(op
->opc
);
205 tcg_target_ulong mask
;
210 temps
[dst
].is_const
= true;
211 temps
[dst
].val
= val
;
213 if (TCG_TARGET_REG_BITS
> 32 && new_op
== INDEX_op_movi_i32
) {
214 /* High bits of the destination are now garbage. */
215 mask
|= ~0xffffffffull
;
217 temps
[dst
].mask
= mask
;
223 static void tcg_opt_gen_mov(TCGContext
*s
, TCGOp
*op
, TCGArg
*args
,
224 TCGArg dst
, TCGArg src
)
226 if (temps_are_copies(dst
, src
)) {
227 tcg_op_remove(s
, op
);
231 TCGOpcode new_op
= op_to_mov(op
->opc
);
232 tcg_target_ulong mask
;
237 mask
= temps
[src
].mask
;
238 if (TCG_TARGET_REG_BITS
> 32 && new_op
== INDEX_op_mov_i32
) {
239 /* High bits of the destination are now garbage. */
240 mask
|= ~0xffffffffull
;
242 temps
[dst
].mask
= mask
;
244 if (s
->temps
[src
].type
== s
->temps
[dst
].type
) {
245 temps
[dst
].next_copy
= temps
[src
].next_copy
;
246 temps
[dst
].prev_copy
= src
;
247 temps
[temps
[dst
].next_copy
].prev_copy
= dst
;
248 temps
[src
].next_copy
= dst
;
249 temps
[dst
].is_const
= temps
[src
].is_const
;
250 temps
[dst
].val
= temps
[src
].val
;
257 static TCGArg
do_constant_folding_2(TCGOpcode op
, TCGArg x
, TCGArg y
)
280 case INDEX_op_shl_i32
:
281 return (uint32_t)x
<< (y
& 31);
283 case INDEX_op_shl_i64
:
284 return (uint64_t)x
<< (y
& 63);
286 case INDEX_op_shr_i32
:
287 return (uint32_t)x
>> (y
& 31);
289 case INDEX_op_shr_i64
:
290 return (uint64_t)x
>> (y
& 63);
292 case INDEX_op_sar_i32
:
293 return (int32_t)x
>> (y
& 31);
295 case INDEX_op_sar_i64
:
296 return (int64_t)x
>> (y
& 63);
298 case INDEX_op_rotr_i32
:
299 return ror32(x
, y
& 31);
301 case INDEX_op_rotr_i64
:
302 return ror64(x
, y
& 63);
304 case INDEX_op_rotl_i32
:
305 return rol32(x
, y
& 31);
307 case INDEX_op_rotl_i64
:
308 return rol64(x
, y
& 63);
331 CASE_OP_32_64(ext8s
):
334 CASE_OP_32_64(ext16s
):
337 CASE_OP_32_64(ext8u
):
340 CASE_OP_32_64(ext16u
):
343 case INDEX_op_ext_i32_i64
:
344 case INDEX_op_ext32s_i64
:
347 case INDEX_op_extu_i32_i64
:
348 case INDEX_op_extrl_i64_i32
:
349 case INDEX_op_ext32u_i64
:
352 case INDEX_op_extrh_i64_i32
:
353 return (uint64_t)x
>> 32;
355 case INDEX_op_muluh_i32
:
356 return ((uint64_t)(uint32_t)x
* (uint32_t)y
) >> 32;
357 case INDEX_op_mulsh_i32
:
358 return ((int64_t)(int32_t)x
* (int32_t)y
) >> 32;
360 case INDEX_op_muluh_i64
:
361 mulu64(&l64
, &h64
, x
, y
);
363 case INDEX_op_mulsh_i64
:
364 muls64(&l64
, &h64
, x
, y
);
367 case INDEX_op_div_i32
:
368 /* Avoid crashing on divide by zero, otherwise undefined. */
369 return (int32_t)x
/ ((int32_t)y
? : 1);
370 case INDEX_op_divu_i32
:
371 return (uint32_t)x
/ ((uint32_t)y
? : 1);
372 case INDEX_op_div_i64
:
373 return (int64_t)x
/ ((int64_t)y
? : 1);
374 case INDEX_op_divu_i64
:
375 return (uint64_t)x
/ ((uint64_t)y
? : 1);
377 case INDEX_op_rem_i32
:
378 return (int32_t)x
% ((int32_t)y
? : 1);
379 case INDEX_op_remu_i32
:
380 return (uint32_t)x
% ((uint32_t)y
? : 1);
381 case INDEX_op_rem_i64
:
382 return (int64_t)x
% ((int64_t)y
? : 1);
383 case INDEX_op_remu_i64
:
384 return (uint64_t)x
% ((uint64_t)y
? : 1);
388 "Unrecognized operation %d in do_constant_folding.\n", op
);
393 static TCGArg
do_constant_folding(TCGOpcode op
, TCGArg x
, TCGArg y
)
395 TCGArg res
= do_constant_folding_2(op
, x
, y
);
396 if (op_bits(op
) == 32) {
402 static bool do_constant_folding_cond_32(uint32_t x
, uint32_t y
, TCGCond c
)
410 return (int32_t)x
< (int32_t)y
;
412 return (int32_t)x
>= (int32_t)y
;
414 return (int32_t)x
<= (int32_t)y
;
416 return (int32_t)x
> (int32_t)y
;
430 static bool do_constant_folding_cond_64(uint64_t x
, uint64_t y
, TCGCond c
)
438 return (int64_t)x
< (int64_t)y
;
440 return (int64_t)x
>= (int64_t)y
;
442 return (int64_t)x
<= (int64_t)y
;
444 return (int64_t)x
> (int64_t)y
;
458 static bool do_constant_folding_cond_eq(TCGCond c
)
478 /* Return 2 if the condition can't be simplified, and the result
479 of the condition (0 or 1) if it can */
480 static TCGArg
do_constant_folding_cond(TCGOpcode op
, TCGArg x
,
483 if (temp_is_const(x
) && temp_is_const(y
)) {
484 switch (op_bits(op
)) {
486 return do_constant_folding_cond_32(temps
[x
].val
, temps
[y
].val
, c
);
488 return do_constant_folding_cond_64(temps
[x
].val
, temps
[y
].val
, c
);
492 } else if (temps_are_copies(x
, y
)) {
493 return do_constant_folding_cond_eq(c
);
494 } else if (temp_is_const(y
) && temps
[y
].val
== 0) {
508 /* Return 2 if the condition can't be simplified, and the result
509 of the condition (0 or 1) if it can */
510 static TCGArg
do_constant_folding_cond2(TCGArg
*p1
, TCGArg
*p2
, TCGCond c
)
512 TCGArg al
= p1
[0], ah
= p1
[1];
513 TCGArg bl
= p2
[0], bh
= p2
[1];
515 if (temp_is_const(bl
) && temp_is_const(bh
)) {
516 uint64_t b
= ((uint64_t)temps
[bh
].val
<< 32) | (uint32_t)temps
[bl
].val
;
518 if (temp_is_const(al
) && temp_is_const(ah
)) {
520 a
= ((uint64_t)temps
[ah
].val
<< 32) | (uint32_t)temps
[al
].val
;
521 return do_constant_folding_cond_64(a
, b
, c
);
534 if (temps_are_copies(al
, bl
) && temps_are_copies(ah
, bh
)) {
535 return do_constant_folding_cond_eq(c
);
540 static bool swap_commutative(TCGArg dest
, TCGArg
*p1
, TCGArg
*p2
)
542 TCGArg a1
= *p1
, a2
= *p2
;
544 sum
+= temp_is_const(a1
);
545 sum
-= temp_is_const(a2
);
547 /* Prefer the constant in second argument, and then the form
548 op a, a, b, which is better handled on non-RISC hosts. */
549 if (sum
> 0 || (sum
== 0 && dest
== a2
)) {
557 static bool swap_commutative2(TCGArg
*p1
, TCGArg
*p2
)
560 sum
+= temp_is_const(p1
[0]);
561 sum
+= temp_is_const(p1
[1]);
562 sum
-= temp_is_const(p2
[0]);
563 sum
-= temp_is_const(p2
[1]);
566 t
= p1
[0], p1
[0] = p2
[0], p2
[0] = t
;
567 t
= p1
[1], p1
[1] = p2
[1], p2
[1] = t
;
573 /* Propagate constants and copies, fold constant expressions. */
574 void tcg_optimize(TCGContext
*s
)
576 int oi
, oi_next
, nb_temps
, nb_globals
;
578 /* Array VALS has an element for each temp.
579 If this temp holds a constant then its value is kept in VALS' element.
580 If this temp is a copy of other ones then the other copies are
581 available through the doubly linked circular list. */
583 nb_temps
= s
->nb_temps
;
584 nb_globals
= s
->nb_globals
;
585 reset_all_temps(nb_temps
);
587 for (oi
= s
->gen_first_op_idx
; oi
>= 0; oi
= oi_next
) {
588 tcg_target_ulong mask
, partmask
, affected
;
589 int nb_oargs
, nb_iargs
, i
;
592 TCGOp
* const op
= &s
->gen_op_buf
[oi
];
593 TCGArg
* const args
= &s
->gen_opparam_buf
[op
->args
];
594 TCGOpcode opc
= op
->opc
;
595 const TCGOpDef
*def
= &tcg_op_defs
[opc
];
599 /* Count the arguments, and initialize the temps that are
601 if (opc
== INDEX_op_call
) {
602 nb_oargs
= op
->callo
;
603 nb_iargs
= op
->calli
;
604 for (i
= 0; i
< nb_oargs
+ nb_iargs
; i
++) {
606 if (tmp
!= TCG_CALL_DUMMY_ARG
) {
611 nb_oargs
= def
->nb_oargs
;
612 nb_iargs
= def
->nb_iargs
;
613 for (i
= 0; i
< nb_oargs
+ nb_iargs
; i
++) {
614 init_temp_info(args
[i
]);
618 /* Do copy propagation */
619 for (i
= nb_oargs
; i
< nb_oargs
+ nb_iargs
; i
++) {
620 if (temp_is_copy(args
[i
])) {
621 args
[i
] = find_better_copy(s
, args
[i
]);
625 /* For commutative operations make constant second argument */
635 CASE_OP_32_64(muluh
):
636 CASE_OP_32_64(mulsh
):
637 swap_commutative(args
[0], &args
[1], &args
[2]);
639 CASE_OP_32_64(brcond
):
640 if (swap_commutative(-1, &args
[0], &args
[1])) {
641 args
[2] = tcg_swap_cond(args
[2]);
644 CASE_OP_32_64(setcond
):
645 if (swap_commutative(args
[0], &args
[1], &args
[2])) {
646 args
[3] = tcg_swap_cond(args
[3]);
649 CASE_OP_32_64(movcond
):
650 if (swap_commutative(-1, &args
[1], &args
[2])) {
651 args
[5] = tcg_swap_cond(args
[5]);
653 /* For movcond, we canonicalize the "false" input reg to match
654 the destination reg so that the tcg backend can implement
655 a "move if true" operation. */
656 if (swap_commutative(args
[0], &args
[4], &args
[3])) {
657 args
[5] = tcg_invert_cond(args
[5]);
661 swap_commutative(args
[0], &args
[2], &args
[4]);
662 swap_commutative(args
[1], &args
[3], &args
[5]);
664 CASE_OP_32_64(mulu2
):
665 CASE_OP_32_64(muls2
):
666 swap_commutative(args
[0], &args
[2], &args
[3]);
668 case INDEX_op_brcond2_i32
:
669 if (swap_commutative2(&args
[0], &args
[2])) {
670 args
[4] = tcg_swap_cond(args
[4]);
673 case INDEX_op_setcond2_i32
:
674 if (swap_commutative2(&args
[1], &args
[3])) {
675 args
[5] = tcg_swap_cond(args
[5]);
682 /* Simplify expressions for "shift/rot r, 0, a => movi r, 0",
683 and "sub r, 0, a => neg r, a" case. */
690 if (temp_is_const(args
[1]) && temps
[args
[1]].val
== 0) {
691 tcg_opt_gen_movi(s
, op
, args
, args
[0], 0);
700 if (temp_is_const(args
[2])) {
701 /* Proceed with possible constant folding. */
704 if (opc
== INDEX_op_sub_i32
) {
705 neg_op
= INDEX_op_neg_i32
;
706 have_neg
= TCG_TARGET_HAS_neg_i32
;
708 neg_op
= INDEX_op_neg_i64
;
709 have_neg
= TCG_TARGET_HAS_neg_i64
;
714 if (temp_is_const(args
[1]) && temps
[args
[1]].val
== 0) {
724 if (!temp_is_const(args
[1])
725 && temp_is_const(args
[2]) && temps
[args
[2]].val
== -1) {
731 if (!temp_is_const(args
[1])
732 && temp_is_const(args
[2]) && temps
[args
[2]].val
== 0) {
738 if (!temp_is_const(args
[2])
739 && temp_is_const(args
[1]) && temps
[args
[1]].val
== -1) {
746 if (!temp_is_const(args
[2])
747 && temp_is_const(args
[1]) && temps
[args
[1]].val
== 0) {
757 if (def
->flags
& TCG_OPF_64BIT
) {
758 not_op
= INDEX_op_not_i64
;
759 have_not
= TCG_TARGET_HAS_not_i64
;
761 not_op
= INDEX_op_not_i32
;
762 have_not
= TCG_TARGET_HAS_not_i32
;
776 /* Simplify expression for "op r, a, const => mov r, a" cases */
788 if (!temp_is_const(args
[1])
789 && temp_is_const(args
[2]) && temps
[args
[2]].val
== 0) {
790 tcg_opt_gen_mov(s
, op
, args
, args
[0], args
[1]);
797 if (!temp_is_const(args
[1])
798 && temp_is_const(args
[2]) && temps
[args
[2]].val
== -1) {
799 tcg_opt_gen_mov(s
, op
, args
, args
[0], args
[1]);
807 /* Simplify using known-zero bits. Currently only ops with a single
808 output argument is supported. */
812 CASE_OP_32_64(ext8s
):
813 if ((temps
[args
[1]].mask
& 0x80) != 0) {
816 CASE_OP_32_64(ext8u
):
819 CASE_OP_32_64(ext16s
):
820 if ((temps
[args
[1]].mask
& 0x8000) != 0) {
823 CASE_OP_32_64(ext16u
):
826 case INDEX_op_ext32s_i64
:
827 if ((temps
[args
[1]].mask
& 0x80000000) != 0) {
830 case INDEX_op_ext32u_i64
:
835 mask
= temps
[args
[2]].mask
;
836 if (temp_is_const(args
[2])) {
838 affected
= temps
[args
[1]].mask
& ~mask
;
840 mask
= temps
[args
[1]].mask
& mask
;
843 case INDEX_op_ext_i32_i64
:
844 if ((temps
[args
[1]].mask
& 0x80000000) != 0) {
847 case INDEX_op_extu_i32_i64
:
848 /* We do not compute affected as it is a size changing op. */
849 mask
= (uint32_t)temps
[args
[1]].mask
;
853 /* Known-zeros does not imply known-ones. Therefore unless
854 args[2] is constant, we can't infer anything from it. */
855 if (temp_is_const(args
[2])) {
856 mask
= ~temps
[args
[2]].mask
;
859 /* But we certainly know nothing outside args[1] may be set. */
860 mask
= temps
[args
[1]].mask
;
863 case INDEX_op_sar_i32
:
864 if (temp_is_const(args
[2])) {
865 tmp
= temps
[args
[2]].val
& 31;
866 mask
= (int32_t)temps
[args
[1]].mask
>> tmp
;
869 case INDEX_op_sar_i64
:
870 if (temp_is_const(args
[2])) {
871 tmp
= temps
[args
[2]].val
& 63;
872 mask
= (int64_t)temps
[args
[1]].mask
>> tmp
;
876 case INDEX_op_shr_i32
:
877 if (temp_is_const(args
[2])) {
878 tmp
= temps
[args
[2]].val
& 31;
879 mask
= (uint32_t)temps
[args
[1]].mask
>> tmp
;
882 case INDEX_op_shr_i64
:
883 if (temp_is_const(args
[2])) {
884 tmp
= temps
[args
[2]].val
& 63;
885 mask
= (uint64_t)temps
[args
[1]].mask
>> tmp
;
889 case INDEX_op_extrl_i64_i32
:
890 mask
= (uint32_t)temps
[args
[1]].mask
;
892 case INDEX_op_extrh_i64_i32
:
893 mask
= (uint64_t)temps
[args
[1]].mask
>> 32;
897 if (temp_is_const(args
[2])) {
898 tmp
= temps
[args
[2]].val
& (TCG_TARGET_REG_BITS
- 1);
899 mask
= temps
[args
[1]].mask
<< tmp
;
904 /* Set to 1 all bits to the left of the rightmost. */
905 mask
= -(temps
[args
[1]].mask
& -temps
[args
[1]].mask
);
908 CASE_OP_32_64(deposit
):
909 mask
= deposit64(temps
[args
[1]].mask
, args
[3], args
[4],
910 temps
[args
[2]].mask
);
915 mask
= temps
[args
[1]].mask
| temps
[args
[2]].mask
;
918 CASE_OP_32_64(setcond
):
919 case INDEX_op_setcond2_i32
:
923 CASE_OP_32_64(movcond
):
924 mask
= temps
[args
[3]].mask
| temps
[args
[4]].mask
;
930 CASE_OP_32_64(ld16u
):
933 case INDEX_op_ld32u_i64
:
937 CASE_OP_32_64(qemu_ld
):
939 TCGMemOpIdx oi
= args
[nb_oargs
+ nb_iargs
];
940 TCGMemOp mop
= get_memop(oi
);
941 if (!(mop
& MO_SIGN
)) {
942 mask
= (2ULL << ((8 << (mop
& MO_SIZE
)) - 1)) - 1;
951 /* 32-bit ops generate 32-bit results. For the result is zero test
952 below, we can ignore high bits, but for further optimizations we
953 need to record that the high bits contain garbage. */
955 if (!(def
->flags
& TCG_OPF_64BIT
)) {
956 mask
|= ~(tcg_target_ulong
)0xffffffffu
;
957 partmask
&= 0xffffffffu
;
958 affected
&= 0xffffffffu
;
962 tcg_debug_assert(nb_oargs
== 1);
963 tcg_opt_gen_movi(s
, op
, args
, args
[0], 0);
967 tcg_debug_assert(nb_oargs
== 1);
968 tcg_opt_gen_mov(s
, op
, args
, args
[0], args
[1]);
972 /* Simplify expression for "op r, a, 0 => movi r, 0" cases */
976 CASE_OP_32_64(muluh
):
977 CASE_OP_32_64(mulsh
):
978 if ((temp_is_const(args
[2]) && temps
[args
[2]].val
== 0)) {
979 tcg_opt_gen_movi(s
, op
, args
, args
[0], 0);
987 /* Simplify expression for "op r, a, a => mov r, a" cases */
991 if (temps_are_copies(args
[1], args
[2])) {
992 tcg_opt_gen_mov(s
, op
, args
, args
[0], args
[1]);
1000 /* Simplify expression for "op r, a, a => movi r, 0" cases */
1002 CASE_OP_32_64(andc
):
1005 if (temps_are_copies(args
[1], args
[2])) {
1006 tcg_opt_gen_movi(s
, op
, args
, args
[0], 0);
1014 /* Propagate constants through copy operations and do constant
1015 folding. Constants will be substituted to arguments by register
1016 allocator where needed and possible. Also detect copies. */
1019 tcg_opt_gen_mov(s
, op
, args
, args
[0], args
[1]);
1021 CASE_OP_32_64(movi
):
1022 tcg_opt_gen_movi(s
, op
, args
, args
[0], args
[1]);
1027 CASE_OP_32_64(ext8s
):
1028 CASE_OP_32_64(ext8u
):
1029 CASE_OP_32_64(ext16s
):
1030 CASE_OP_32_64(ext16u
):
1031 case INDEX_op_ext32s_i64
:
1032 case INDEX_op_ext32u_i64
:
1033 case INDEX_op_ext_i32_i64
:
1034 case INDEX_op_extu_i32_i64
:
1035 case INDEX_op_extrl_i64_i32
:
1036 case INDEX_op_extrh_i64_i32
:
1037 if (temp_is_const(args
[1])) {
1038 tmp
= do_constant_folding(opc
, temps
[args
[1]].val
, 0);
1039 tcg_opt_gen_movi(s
, op
, args
, args
[0], tmp
);
1053 CASE_OP_32_64(rotl
):
1054 CASE_OP_32_64(rotr
):
1055 CASE_OP_32_64(andc
):
1058 CASE_OP_32_64(nand
):
1060 CASE_OP_32_64(muluh
):
1061 CASE_OP_32_64(mulsh
):
1063 CASE_OP_32_64(divu
):
1065 CASE_OP_32_64(remu
):
1066 if (temp_is_const(args
[1]) && temp_is_const(args
[2])) {
1067 tmp
= do_constant_folding(opc
, temps
[args
[1]].val
,
1068 temps
[args
[2]].val
);
1069 tcg_opt_gen_movi(s
, op
, args
, args
[0], tmp
);
1074 CASE_OP_32_64(deposit
):
1075 if (temp_is_const(args
[1]) && temp_is_const(args
[2])) {
1076 tmp
= deposit64(temps
[args
[1]].val
, args
[3], args
[4],
1077 temps
[args
[2]].val
);
1078 tcg_opt_gen_movi(s
, op
, args
, args
[0], tmp
);
1083 CASE_OP_32_64(setcond
):
1084 tmp
= do_constant_folding_cond(opc
, args
[1], args
[2], args
[3]);
1086 tcg_opt_gen_movi(s
, op
, args
, args
[0], tmp
);
1091 CASE_OP_32_64(brcond
):
1092 tmp
= do_constant_folding_cond(opc
, args
[0], args
[1], args
[2]);
1095 reset_all_temps(nb_temps
);
1096 op
->opc
= INDEX_op_br
;
1099 tcg_op_remove(s
, op
);
1105 CASE_OP_32_64(movcond
):
1106 tmp
= do_constant_folding_cond(opc
, args
[1], args
[2], args
[5]);
1108 tcg_opt_gen_mov(s
, op
, args
, args
[0], args
[4-tmp
]);
1113 case INDEX_op_add2_i32
:
1114 case INDEX_op_sub2_i32
:
1115 if (temp_is_const(args
[2]) && temp_is_const(args
[3])
1116 && temp_is_const(args
[4]) && temp_is_const(args
[5])) {
1117 uint32_t al
= temps
[args
[2]].val
;
1118 uint32_t ah
= temps
[args
[3]].val
;
1119 uint32_t bl
= temps
[args
[4]].val
;
1120 uint32_t bh
= temps
[args
[5]].val
;
1121 uint64_t a
= ((uint64_t)ah
<< 32) | al
;
1122 uint64_t b
= ((uint64_t)bh
<< 32) | bl
;
1124 TCGOp
*op2
= insert_op_before(s
, op
, INDEX_op_movi_i32
, 2);
1125 TCGArg
*args2
= &s
->gen_opparam_buf
[op2
->args
];
1127 if (opc
== INDEX_op_add2_i32
) {
1135 tcg_opt_gen_movi(s
, op
, args
, rl
, (int32_t)a
);
1136 tcg_opt_gen_movi(s
, op2
, args2
, rh
, (int32_t)(a
>> 32));
1138 /* We've done all we need to do with the movi. Skip it. */
1139 oi_next
= op2
->next
;
1144 case INDEX_op_mulu2_i32
:
1145 if (temp_is_const(args
[2]) && temp_is_const(args
[3])) {
1146 uint32_t a
= temps
[args
[2]].val
;
1147 uint32_t b
= temps
[args
[3]].val
;
1148 uint64_t r
= (uint64_t)a
* b
;
1150 TCGOp
*op2
= insert_op_before(s
, op
, INDEX_op_movi_i32
, 2);
1151 TCGArg
*args2
= &s
->gen_opparam_buf
[op2
->args
];
1155 tcg_opt_gen_movi(s
, op
, args
, rl
, (int32_t)r
);
1156 tcg_opt_gen_movi(s
, op2
, args2
, rh
, (int32_t)(r
>> 32));
1158 /* We've done all we need to do with the movi. Skip it. */
1159 oi_next
= op2
->next
;
1164 case INDEX_op_brcond2_i32
:
1165 tmp
= do_constant_folding_cond2(&args
[0], &args
[2], args
[4]);
1169 reset_all_temps(nb_temps
);
1170 op
->opc
= INDEX_op_br
;
1174 tcg_op_remove(s
, op
);
1176 } else if ((args
[4] == TCG_COND_LT
|| args
[4] == TCG_COND_GE
)
1177 && temp_is_const(args
[2]) && temps
[args
[2]].val
== 0
1178 && temp_is_const(args
[3]) && temps
[args
[3]].val
== 0) {
1179 /* Simplify LT/GE comparisons vs zero to a single compare
1180 vs the high word of the input. */
1182 reset_all_temps(nb_temps
);
1183 op
->opc
= INDEX_op_brcond_i32
;
1188 } else if (args
[4] == TCG_COND_EQ
) {
1189 /* Simplify EQ comparisons where one of the pairs
1190 can be simplified. */
1191 tmp
= do_constant_folding_cond(INDEX_op_brcond_i32
,
1192 args
[0], args
[2], TCG_COND_EQ
);
1194 goto do_brcond_false
;
1195 } else if (tmp
== 1) {
1196 goto do_brcond_high
;
1198 tmp
= do_constant_folding_cond(INDEX_op_brcond_i32
,
1199 args
[1], args
[3], TCG_COND_EQ
);
1201 goto do_brcond_false
;
1202 } else if (tmp
!= 1) {
1206 reset_all_temps(nb_temps
);
1207 op
->opc
= INDEX_op_brcond_i32
;
1211 } else if (args
[4] == TCG_COND_NE
) {
1212 /* Simplify NE comparisons where one of the pairs
1213 can be simplified. */
1214 tmp
= do_constant_folding_cond(INDEX_op_brcond_i32
,
1215 args
[0], args
[2], TCG_COND_NE
);
1217 goto do_brcond_high
;
1218 } else if (tmp
== 1) {
1219 goto do_brcond_true
;
1221 tmp
= do_constant_folding_cond(INDEX_op_brcond_i32
,
1222 args
[1], args
[3], TCG_COND_NE
);
1225 } else if (tmp
== 1) {
1226 goto do_brcond_true
;
1234 case INDEX_op_setcond2_i32
:
1235 tmp
= do_constant_folding_cond2(&args
[1], &args
[3], args
[5]);
1238 tcg_opt_gen_movi(s
, op
, args
, args
[0], tmp
);
1239 } else if ((args
[5] == TCG_COND_LT
|| args
[5] == TCG_COND_GE
)
1240 && temp_is_const(args
[3]) && temps
[args
[3]].val
== 0
1241 && temp_is_const(args
[4]) && temps
[args
[4]].val
== 0) {
1242 /* Simplify LT/GE comparisons vs zero to a single compare
1243 vs the high word of the input. */
1245 reset_temp(args
[0]);
1246 temps
[args
[0]].mask
= 1;
1247 op
->opc
= INDEX_op_setcond_i32
;
1251 } else if (args
[5] == TCG_COND_EQ
) {
1252 /* Simplify EQ comparisons where one of the pairs
1253 can be simplified. */
1254 tmp
= do_constant_folding_cond(INDEX_op_setcond_i32
,
1255 args
[1], args
[3], TCG_COND_EQ
);
1257 goto do_setcond_const
;
1258 } else if (tmp
== 1) {
1259 goto do_setcond_high
;
1261 tmp
= do_constant_folding_cond(INDEX_op_setcond_i32
,
1262 args
[2], args
[4], TCG_COND_EQ
);
1264 goto do_setcond_high
;
1265 } else if (tmp
!= 1) {
1269 reset_temp(args
[0]);
1270 temps
[args
[0]].mask
= 1;
1271 op
->opc
= INDEX_op_setcond_i32
;
1274 } else if (args
[5] == TCG_COND_NE
) {
1275 /* Simplify NE comparisons where one of the pairs
1276 can be simplified. */
1277 tmp
= do_constant_folding_cond(INDEX_op_setcond_i32
,
1278 args
[1], args
[3], TCG_COND_NE
);
1280 goto do_setcond_high
;
1281 } else if (tmp
== 1) {
1282 goto do_setcond_const
;
1284 tmp
= do_constant_folding_cond(INDEX_op_setcond_i32
,
1285 args
[2], args
[4], TCG_COND_NE
);
1287 goto do_setcond_low
;
1288 } else if (tmp
== 1) {
1289 goto do_setcond_const
;
1298 if (!(args
[nb_oargs
+ nb_iargs
+ 1]
1299 & (TCG_CALL_NO_READ_GLOBALS
| TCG_CALL_NO_WRITE_GLOBALS
))) {
1300 for (i
= 0; i
< nb_globals
; i
++) {
1301 if (test_bit(i
, temps_used
.l
)) {
1306 goto do_reset_output
;
1310 /* Default case: we know nothing about operation (or were unable
1311 to compute the operation result) so no propagation is done.
1312 We trash everything if the operation is the end of a basic
1313 block, otherwise we only trash the output args. "mask" is
1314 the non-zero bits mask for the first output arg. */
1315 if (def
->flags
& TCG_OPF_BB_END
) {
1316 reset_all_temps(nb_temps
);
1319 for (i
= 0; i
< nb_oargs
; i
++) {
1320 reset_temp(args
[i
]);
1321 /* Save the corresponding known-zero bits mask for the
1322 first output argument (only one supported so far). */
1324 temps
[args
[i
]].mask
= mask
;