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 TCGOp
*insert_op_before(TCGContext
*s
, TCGOp
*old_op
,
86 TCGOpcode opc
, int nargs
)
88 int oi
= s
->gen_next_op_idx
;
89 int pi
= s
->gen_next_parm_idx
;
90 int prev
= old_op
->prev
;
91 int next
= old_op
- s
->gen_op_buf
;
94 tcg_debug_assert(oi
< OPC_BUF_SIZE
);
95 tcg_debug_assert(pi
+ nargs
<= OPPARAM_BUF_SIZE
);
96 s
->gen_next_op_idx
= oi
+ 1;
97 s
->gen_next_parm_idx
= pi
+ nargs
;
99 new_op
= &s
->gen_op_buf
[oi
];
107 s
->gen_op_buf
[prev
].next
= oi
;
109 s
->gen_first_op_idx
= oi
;
116 static int op_bits(TCGOpcode op
)
118 const TCGOpDef
*def
= &tcg_op_defs
[op
];
119 return def
->flags
& TCG_OPF_64BIT
? 64 : 32;
122 static TCGOpcode
op_to_mov(TCGOpcode op
)
124 switch (op_bits(op
)) {
126 return INDEX_op_mov_i32
;
128 return INDEX_op_mov_i64
;
130 fprintf(stderr
, "op_to_mov: unexpected return value of "
131 "function op_bits.\n");
136 static TCGOpcode
op_to_movi(TCGOpcode op
)
138 switch (op_bits(op
)) {
140 return INDEX_op_movi_i32
;
142 return INDEX_op_movi_i64
;
144 fprintf(stderr
, "op_to_movi: unexpected return value of "
145 "function op_bits.\n");
150 static TCGArg
find_better_copy(TCGContext
*s
, TCGArg temp
)
154 /* If this is already a global, we can't do better. */
155 if (temp
< s
->nb_globals
) {
159 /* Search for a global first. */
160 for (i
= temps
[temp
].next_copy
; i
!= temp
; i
= temps
[i
].next_copy
) {
161 if (i
< s
->nb_globals
) {
166 /* If it is a temp, search for a temp local. */
167 if (!s
->temps
[temp
].temp_local
) {
168 for (i
= temps
[temp
].next_copy
; i
!= temp
; i
= temps
[i
].next_copy
) {
169 if (s
->temps
[i
].temp_local
) {
175 /* Failure to find a better representation, return the same temp. */
179 static bool temps_are_copies(TCGArg arg1
, TCGArg arg2
)
187 if (!temp_is_copy(arg1
) || !temp_is_copy(arg2
)) {
191 for (i
= temps
[arg1
].next_copy
; i
!= arg1
; i
= temps
[i
].next_copy
) {
200 static void tcg_opt_gen_movi(TCGContext
*s
, TCGOp
*op
, TCGArg
*args
,
201 TCGArg dst
, TCGArg val
)
203 TCGOpcode new_op
= op_to_movi(op
->opc
);
204 tcg_target_ulong mask
;
209 temps
[dst
].is_const
= true;
210 temps
[dst
].val
= val
;
212 if (TCG_TARGET_REG_BITS
> 32 && new_op
== INDEX_op_movi_i32
) {
213 /* High bits of the destination are now garbage. */
214 mask
|= ~0xffffffffull
;
216 temps
[dst
].mask
= mask
;
222 static void tcg_opt_gen_mov(TCGContext
*s
, TCGOp
*op
, TCGArg
*args
,
223 TCGArg dst
, TCGArg src
)
225 if (temps_are_copies(dst
, src
)) {
226 tcg_op_remove(s
, op
);
230 TCGOpcode new_op
= op_to_mov(op
->opc
);
231 tcg_target_ulong mask
;
236 mask
= temps
[src
].mask
;
237 if (TCG_TARGET_REG_BITS
> 32 && new_op
== INDEX_op_mov_i32
) {
238 /* High bits of the destination are now garbage. */
239 mask
|= ~0xffffffffull
;
241 temps
[dst
].mask
= mask
;
243 if (s
->temps
[src
].type
== s
->temps
[dst
].type
) {
244 temps
[dst
].next_copy
= temps
[src
].next_copy
;
245 temps
[dst
].prev_copy
= src
;
246 temps
[temps
[dst
].next_copy
].prev_copy
= dst
;
247 temps
[src
].next_copy
= dst
;
248 temps
[dst
].is_const
= temps
[src
].is_const
;
249 temps
[dst
].val
= temps
[src
].val
;
256 static TCGArg
do_constant_folding_2(TCGOpcode op
, TCGArg x
, TCGArg y
)
279 case INDEX_op_shl_i32
:
280 return (uint32_t)x
<< (y
& 31);
282 case INDEX_op_shl_i64
:
283 return (uint64_t)x
<< (y
& 63);
285 case INDEX_op_shr_i32
:
286 return (uint32_t)x
>> (y
& 31);
288 case INDEX_op_shr_i64
:
289 return (uint64_t)x
>> (y
& 63);
291 case INDEX_op_sar_i32
:
292 return (int32_t)x
>> (y
& 31);
294 case INDEX_op_sar_i64
:
295 return (int64_t)x
>> (y
& 63);
297 case INDEX_op_rotr_i32
:
298 return ror32(x
, y
& 31);
300 case INDEX_op_rotr_i64
:
301 return ror64(x
, y
& 63);
303 case INDEX_op_rotl_i32
:
304 return rol32(x
, y
& 31);
306 case INDEX_op_rotl_i64
:
307 return rol64(x
, y
& 63);
330 CASE_OP_32_64(ext8s
):
333 CASE_OP_32_64(ext16s
):
336 CASE_OP_32_64(ext8u
):
339 CASE_OP_32_64(ext16u
):
342 case INDEX_op_ext_i32_i64
:
343 case INDEX_op_ext32s_i64
:
346 case INDEX_op_extu_i32_i64
:
347 case INDEX_op_extrl_i64_i32
:
348 case INDEX_op_ext32u_i64
:
351 case INDEX_op_extrh_i64_i32
:
352 return (uint64_t)x
>> 32;
354 case INDEX_op_muluh_i32
:
355 return ((uint64_t)(uint32_t)x
* (uint32_t)y
) >> 32;
356 case INDEX_op_mulsh_i32
:
357 return ((int64_t)(int32_t)x
* (int32_t)y
) >> 32;
359 case INDEX_op_muluh_i64
:
360 mulu64(&l64
, &h64
, x
, y
);
362 case INDEX_op_mulsh_i64
:
363 muls64(&l64
, &h64
, x
, y
);
366 case INDEX_op_div_i32
:
367 /* Avoid crashing on divide by zero, otherwise undefined. */
368 return (int32_t)x
/ ((int32_t)y
? : 1);
369 case INDEX_op_divu_i32
:
370 return (uint32_t)x
/ ((uint32_t)y
? : 1);
371 case INDEX_op_div_i64
:
372 return (int64_t)x
/ ((int64_t)y
? : 1);
373 case INDEX_op_divu_i64
:
374 return (uint64_t)x
/ ((uint64_t)y
? : 1);
376 case INDEX_op_rem_i32
:
377 return (int32_t)x
% ((int32_t)y
? : 1);
378 case INDEX_op_remu_i32
:
379 return (uint32_t)x
% ((uint32_t)y
? : 1);
380 case INDEX_op_rem_i64
:
381 return (int64_t)x
% ((int64_t)y
? : 1);
382 case INDEX_op_remu_i64
:
383 return (uint64_t)x
% ((uint64_t)y
? : 1);
387 "Unrecognized operation %d in do_constant_folding.\n", op
);
392 static TCGArg
do_constant_folding(TCGOpcode op
, TCGArg x
, TCGArg y
)
394 TCGArg res
= do_constant_folding_2(op
, x
, y
);
395 if (op_bits(op
) == 32) {
401 static bool do_constant_folding_cond_32(uint32_t x
, uint32_t y
, TCGCond c
)
409 return (int32_t)x
< (int32_t)y
;
411 return (int32_t)x
>= (int32_t)y
;
413 return (int32_t)x
<= (int32_t)y
;
415 return (int32_t)x
> (int32_t)y
;
429 static bool do_constant_folding_cond_64(uint64_t x
, uint64_t y
, TCGCond c
)
437 return (int64_t)x
< (int64_t)y
;
439 return (int64_t)x
>= (int64_t)y
;
441 return (int64_t)x
<= (int64_t)y
;
443 return (int64_t)x
> (int64_t)y
;
457 static bool do_constant_folding_cond_eq(TCGCond c
)
477 /* Return 2 if the condition can't be simplified, and the result
478 of the condition (0 or 1) if it can */
479 static TCGArg
do_constant_folding_cond(TCGOpcode op
, TCGArg x
,
482 if (temp_is_const(x
) && temp_is_const(y
)) {
483 switch (op_bits(op
)) {
485 return do_constant_folding_cond_32(temps
[x
].val
, temps
[y
].val
, c
);
487 return do_constant_folding_cond_64(temps
[x
].val
, temps
[y
].val
, c
);
491 } else if (temps_are_copies(x
, y
)) {
492 return do_constant_folding_cond_eq(c
);
493 } else if (temp_is_const(y
) && temps
[y
].val
== 0) {
507 /* Return 2 if the condition can't be simplified, and the result
508 of the condition (0 or 1) if it can */
509 static TCGArg
do_constant_folding_cond2(TCGArg
*p1
, TCGArg
*p2
, TCGCond c
)
511 TCGArg al
= p1
[0], ah
= p1
[1];
512 TCGArg bl
= p2
[0], bh
= p2
[1];
514 if (temp_is_const(bl
) && temp_is_const(bh
)) {
515 uint64_t b
= ((uint64_t)temps
[bh
].val
<< 32) | (uint32_t)temps
[bl
].val
;
517 if (temp_is_const(al
) && temp_is_const(ah
)) {
519 a
= ((uint64_t)temps
[ah
].val
<< 32) | (uint32_t)temps
[al
].val
;
520 return do_constant_folding_cond_64(a
, b
, c
);
533 if (temps_are_copies(al
, bl
) && temps_are_copies(ah
, bh
)) {
534 return do_constant_folding_cond_eq(c
);
539 static bool swap_commutative(TCGArg dest
, TCGArg
*p1
, TCGArg
*p2
)
541 TCGArg a1
= *p1
, a2
= *p2
;
543 sum
+= temp_is_const(a1
);
544 sum
-= temp_is_const(a2
);
546 /* Prefer the constant in second argument, and then the form
547 op a, a, b, which is better handled on non-RISC hosts. */
548 if (sum
> 0 || (sum
== 0 && dest
== a2
)) {
556 static bool swap_commutative2(TCGArg
*p1
, TCGArg
*p2
)
559 sum
+= temp_is_const(p1
[0]);
560 sum
+= temp_is_const(p1
[1]);
561 sum
-= temp_is_const(p2
[0]);
562 sum
-= temp_is_const(p2
[1]);
565 t
= p1
[0], p1
[0] = p2
[0], p2
[0] = t
;
566 t
= p1
[1], p1
[1] = p2
[1], p2
[1] = t
;
572 /* Propagate constants and copies, fold constant expressions. */
573 void tcg_optimize(TCGContext
*s
)
575 int oi
, oi_next
, nb_temps
, nb_globals
;
577 /* Array VALS has an element for each temp.
578 If this temp holds a constant then its value is kept in VALS' element.
579 If this temp is a copy of other ones then the other copies are
580 available through the doubly linked circular list. */
582 nb_temps
= s
->nb_temps
;
583 nb_globals
= s
->nb_globals
;
584 reset_all_temps(nb_temps
);
586 for (oi
= s
->gen_first_op_idx
; oi
>= 0; oi
= oi_next
) {
587 tcg_target_ulong mask
, partmask
, affected
;
588 int nb_oargs
, nb_iargs
, i
;
591 TCGOp
* const op
= &s
->gen_op_buf
[oi
];
592 TCGArg
* const args
= &s
->gen_opparam_buf
[op
->args
];
593 TCGOpcode opc
= op
->opc
;
594 const TCGOpDef
*def
= &tcg_op_defs
[opc
];
598 /* Count the arguments, and initialize the temps that are
600 if (opc
== INDEX_op_call
) {
601 nb_oargs
= op
->callo
;
602 nb_iargs
= op
->calli
;
603 for (i
= 0; i
< nb_oargs
+ nb_iargs
; i
++) {
605 if (tmp
!= TCG_CALL_DUMMY_ARG
) {
610 nb_oargs
= def
->nb_oargs
;
611 nb_iargs
= def
->nb_iargs
;
612 for (i
= 0; i
< nb_oargs
+ nb_iargs
; i
++) {
613 init_temp_info(args
[i
]);
617 /* Do copy propagation */
618 for (i
= nb_oargs
; i
< nb_oargs
+ nb_iargs
; i
++) {
619 if (temp_is_copy(args
[i
])) {
620 args
[i
] = find_better_copy(s
, args
[i
]);
624 /* For commutative operations make constant second argument */
634 CASE_OP_32_64(muluh
):
635 CASE_OP_32_64(mulsh
):
636 swap_commutative(args
[0], &args
[1], &args
[2]);
638 CASE_OP_32_64(brcond
):
639 if (swap_commutative(-1, &args
[0], &args
[1])) {
640 args
[2] = tcg_swap_cond(args
[2]);
643 CASE_OP_32_64(setcond
):
644 if (swap_commutative(args
[0], &args
[1], &args
[2])) {
645 args
[3] = tcg_swap_cond(args
[3]);
648 CASE_OP_32_64(movcond
):
649 if (swap_commutative(-1, &args
[1], &args
[2])) {
650 args
[5] = tcg_swap_cond(args
[5]);
652 /* For movcond, we canonicalize the "false" input reg to match
653 the destination reg so that the tcg backend can implement
654 a "move if true" operation. */
655 if (swap_commutative(args
[0], &args
[4], &args
[3])) {
656 args
[5] = tcg_invert_cond(args
[5]);
660 swap_commutative(args
[0], &args
[2], &args
[4]);
661 swap_commutative(args
[1], &args
[3], &args
[5]);
663 CASE_OP_32_64(mulu2
):
664 CASE_OP_32_64(muls2
):
665 swap_commutative(args
[0], &args
[2], &args
[3]);
667 case INDEX_op_brcond2_i32
:
668 if (swap_commutative2(&args
[0], &args
[2])) {
669 args
[4] = tcg_swap_cond(args
[4]);
672 case INDEX_op_setcond2_i32
:
673 if (swap_commutative2(&args
[1], &args
[3])) {
674 args
[5] = tcg_swap_cond(args
[5]);
681 /* Simplify expressions for "shift/rot r, 0, a => movi r, 0",
682 and "sub r, 0, a => neg r, a" case. */
689 if (temp_is_const(args
[1]) && temps
[args
[1]].val
== 0) {
690 tcg_opt_gen_movi(s
, op
, args
, args
[0], 0);
699 if (temp_is_const(args
[2])) {
700 /* Proceed with possible constant folding. */
703 if (opc
== INDEX_op_sub_i32
) {
704 neg_op
= INDEX_op_neg_i32
;
705 have_neg
= TCG_TARGET_HAS_neg_i32
;
707 neg_op
= INDEX_op_neg_i64
;
708 have_neg
= TCG_TARGET_HAS_neg_i64
;
713 if (temp_is_const(args
[1]) && temps
[args
[1]].val
== 0) {
723 if (!temp_is_const(args
[1])
724 && temp_is_const(args
[2]) && temps
[args
[2]].val
== -1) {
730 if (!temp_is_const(args
[1])
731 && temp_is_const(args
[2]) && temps
[args
[2]].val
== 0) {
737 if (!temp_is_const(args
[2])
738 && temp_is_const(args
[1]) && temps
[args
[1]].val
== -1) {
745 if (!temp_is_const(args
[2])
746 && temp_is_const(args
[1]) && temps
[args
[1]].val
== 0) {
756 if (def
->flags
& TCG_OPF_64BIT
) {
757 not_op
= INDEX_op_not_i64
;
758 have_not
= TCG_TARGET_HAS_not_i64
;
760 not_op
= INDEX_op_not_i32
;
761 have_not
= TCG_TARGET_HAS_not_i32
;
775 /* Simplify expression for "op r, a, const => mov r, a" cases */
787 if (!temp_is_const(args
[1])
788 && temp_is_const(args
[2]) && temps
[args
[2]].val
== 0) {
789 tcg_opt_gen_mov(s
, op
, args
, args
[0], args
[1]);
796 if (!temp_is_const(args
[1])
797 && temp_is_const(args
[2]) && temps
[args
[2]].val
== -1) {
798 tcg_opt_gen_mov(s
, op
, args
, args
[0], args
[1]);
806 /* Simplify using known-zero bits. Currently only ops with a single
807 output argument is supported. */
811 CASE_OP_32_64(ext8s
):
812 if ((temps
[args
[1]].mask
& 0x80) != 0) {
815 CASE_OP_32_64(ext8u
):
818 CASE_OP_32_64(ext16s
):
819 if ((temps
[args
[1]].mask
& 0x8000) != 0) {
822 CASE_OP_32_64(ext16u
):
825 case INDEX_op_ext32s_i64
:
826 if ((temps
[args
[1]].mask
& 0x80000000) != 0) {
829 case INDEX_op_ext32u_i64
:
834 mask
= temps
[args
[2]].mask
;
835 if (temp_is_const(args
[2])) {
837 affected
= temps
[args
[1]].mask
& ~mask
;
839 mask
= temps
[args
[1]].mask
& mask
;
842 case INDEX_op_ext_i32_i64
:
843 if ((temps
[args
[1]].mask
& 0x80000000) != 0) {
846 case INDEX_op_extu_i32_i64
:
847 /* We do not compute affected as it is a size changing op. */
848 mask
= (uint32_t)temps
[args
[1]].mask
;
852 /* Known-zeros does not imply known-ones. Therefore unless
853 args[2] is constant, we can't infer anything from it. */
854 if (temp_is_const(args
[2])) {
855 mask
= ~temps
[args
[2]].mask
;
858 /* But we certainly know nothing outside args[1] may be set. */
859 mask
= temps
[args
[1]].mask
;
862 case INDEX_op_sar_i32
:
863 if (temp_is_const(args
[2])) {
864 tmp
= temps
[args
[2]].val
& 31;
865 mask
= (int32_t)temps
[args
[1]].mask
>> tmp
;
868 case INDEX_op_sar_i64
:
869 if (temp_is_const(args
[2])) {
870 tmp
= temps
[args
[2]].val
& 63;
871 mask
= (int64_t)temps
[args
[1]].mask
>> tmp
;
875 case INDEX_op_shr_i32
:
876 if (temp_is_const(args
[2])) {
877 tmp
= temps
[args
[2]].val
& 31;
878 mask
= (uint32_t)temps
[args
[1]].mask
>> tmp
;
881 case INDEX_op_shr_i64
:
882 if (temp_is_const(args
[2])) {
883 tmp
= temps
[args
[2]].val
& 63;
884 mask
= (uint64_t)temps
[args
[1]].mask
>> tmp
;
888 case INDEX_op_extrl_i64_i32
:
889 mask
= (uint32_t)temps
[args
[1]].mask
;
891 case INDEX_op_extrh_i64_i32
:
892 mask
= (uint64_t)temps
[args
[1]].mask
>> 32;
896 if (temp_is_const(args
[2])) {
897 tmp
= temps
[args
[2]].val
& (TCG_TARGET_REG_BITS
- 1);
898 mask
= temps
[args
[1]].mask
<< tmp
;
903 /* Set to 1 all bits to the left of the rightmost. */
904 mask
= -(temps
[args
[1]].mask
& -temps
[args
[1]].mask
);
907 CASE_OP_32_64(deposit
):
908 mask
= deposit64(temps
[args
[1]].mask
, args
[3], args
[4],
909 temps
[args
[2]].mask
);
914 mask
= temps
[args
[1]].mask
| temps
[args
[2]].mask
;
917 CASE_OP_32_64(setcond
):
918 case INDEX_op_setcond2_i32
:
922 CASE_OP_32_64(movcond
):
923 mask
= temps
[args
[3]].mask
| temps
[args
[4]].mask
;
929 CASE_OP_32_64(ld16u
):
932 case INDEX_op_ld32u_i64
:
936 CASE_OP_32_64(qemu_ld
):
938 TCGMemOpIdx oi
= args
[nb_oargs
+ nb_iargs
];
939 TCGMemOp mop
= get_memop(oi
);
940 if (!(mop
& MO_SIGN
)) {
941 mask
= (2ULL << ((8 << (mop
& MO_SIZE
)) - 1)) - 1;
950 /* 32-bit ops generate 32-bit results. For the result is zero test
951 below, we can ignore high bits, but for further optimizations we
952 need to record that the high bits contain garbage. */
954 if (!(def
->flags
& TCG_OPF_64BIT
)) {
955 mask
|= ~(tcg_target_ulong
)0xffffffffu
;
956 partmask
&= 0xffffffffu
;
957 affected
&= 0xffffffffu
;
961 tcg_debug_assert(nb_oargs
== 1);
962 tcg_opt_gen_movi(s
, op
, args
, args
[0], 0);
966 tcg_debug_assert(nb_oargs
== 1);
967 tcg_opt_gen_mov(s
, op
, args
, args
[0], args
[1]);
971 /* Simplify expression for "op r, a, 0 => movi r, 0" cases */
975 CASE_OP_32_64(muluh
):
976 CASE_OP_32_64(mulsh
):
977 if ((temp_is_const(args
[2]) && temps
[args
[2]].val
== 0)) {
978 tcg_opt_gen_movi(s
, op
, args
, args
[0], 0);
986 /* Simplify expression for "op r, a, a => mov r, a" cases */
990 if (temps_are_copies(args
[1], args
[2])) {
991 tcg_opt_gen_mov(s
, op
, args
, args
[0], args
[1]);
999 /* Simplify expression for "op r, a, a => movi r, 0" cases */
1001 CASE_OP_32_64(andc
):
1004 if (temps_are_copies(args
[1], args
[2])) {
1005 tcg_opt_gen_movi(s
, op
, args
, args
[0], 0);
1013 /* Propagate constants through copy operations and do constant
1014 folding. Constants will be substituted to arguments by register
1015 allocator where needed and possible. Also detect copies. */
1018 tcg_opt_gen_mov(s
, op
, args
, args
[0], args
[1]);
1020 CASE_OP_32_64(movi
):
1021 tcg_opt_gen_movi(s
, op
, args
, args
[0], args
[1]);
1026 CASE_OP_32_64(ext8s
):
1027 CASE_OP_32_64(ext8u
):
1028 CASE_OP_32_64(ext16s
):
1029 CASE_OP_32_64(ext16u
):
1030 case INDEX_op_ext32s_i64
:
1031 case INDEX_op_ext32u_i64
:
1032 case INDEX_op_ext_i32_i64
:
1033 case INDEX_op_extu_i32_i64
:
1034 case INDEX_op_extrl_i64_i32
:
1035 case INDEX_op_extrh_i64_i32
:
1036 if (temp_is_const(args
[1])) {
1037 tmp
= do_constant_folding(opc
, temps
[args
[1]].val
, 0);
1038 tcg_opt_gen_movi(s
, op
, args
, args
[0], tmp
);
1052 CASE_OP_32_64(rotl
):
1053 CASE_OP_32_64(rotr
):
1054 CASE_OP_32_64(andc
):
1057 CASE_OP_32_64(nand
):
1059 CASE_OP_32_64(muluh
):
1060 CASE_OP_32_64(mulsh
):
1062 CASE_OP_32_64(divu
):
1064 CASE_OP_32_64(remu
):
1065 if (temp_is_const(args
[1]) && temp_is_const(args
[2])) {
1066 tmp
= do_constant_folding(opc
, temps
[args
[1]].val
,
1067 temps
[args
[2]].val
);
1068 tcg_opt_gen_movi(s
, op
, args
, args
[0], tmp
);
1073 CASE_OP_32_64(deposit
):
1074 if (temp_is_const(args
[1]) && temp_is_const(args
[2])) {
1075 tmp
= deposit64(temps
[args
[1]].val
, args
[3], args
[4],
1076 temps
[args
[2]].val
);
1077 tcg_opt_gen_movi(s
, op
, args
, args
[0], tmp
);
1082 CASE_OP_32_64(setcond
):
1083 tmp
= do_constant_folding_cond(opc
, args
[1], args
[2], args
[3]);
1085 tcg_opt_gen_movi(s
, op
, args
, args
[0], tmp
);
1090 CASE_OP_32_64(brcond
):
1091 tmp
= do_constant_folding_cond(opc
, args
[0], args
[1], args
[2]);
1094 reset_all_temps(nb_temps
);
1095 op
->opc
= INDEX_op_br
;
1098 tcg_op_remove(s
, op
);
1104 CASE_OP_32_64(movcond
):
1105 tmp
= do_constant_folding_cond(opc
, args
[1], args
[2], args
[5]);
1107 tcg_opt_gen_mov(s
, op
, args
, args
[0], args
[4-tmp
]);
1112 case INDEX_op_add2_i32
:
1113 case INDEX_op_sub2_i32
:
1114 if (temp_is_const(args
[2]) && temp_is_const(args
[3])
1115 && temp_is_const(args
[4]) && temp_is_const(args
[5])) {
1116 uint32_t al
= temps
[args
[2]].val
;
1117 uint32_t ah
= temps
[args
[3]].val
;
1118 uint32_t bl
= temps
[args
[4]].val
;
1119 uint32_t bh
= temps
[args
[5]].val
;
1120 uint64_t a
= ((uint64_t)ah
<< 32) | al
;
1121 uint64_t b
= ((uint64_t)bh
<< 32) | bl
;
1123 TCGOp
*op2
= insert_op_before(s
, op
, INDEX_op_movi_i32
, 2);
1124 TCGArg
*args2
= &s
->gen_opparam_buf
[op2
->args
];
1126 if (opc
== INDEX_op_add2_i32
) {
1134 tcg_opt_gen_movi(s
, op
, args
, rl
, (int32_t)a
);
1135 tcg_opt_gen_movi(s
, op2
, args2
, rh
, (int32_t)(a
>> 32));
1137 /* We've done all we need to do with the movi. Skip it. */
1138 oi_next
= op2
->next
;
1143 case INDEX_op_mulu2_i32
:
1144 if (temp_is_const(args
[2]) && temp_is_const(args
[3])) {
1145 uint32_t a
= temps
[args
[2]].val
;
1146 uint32_t b
= temps
[args
[3]].val
;
1147 uint64_t r
= (uint64_t)a
* b
;
1149 TCGOp
*op2
= insert_op_before(s
, op
, INDEX_op_movi_i32
, 2);
1150 TCGArg
*args2
= &s
->gen_opparam_buf
[op2
->args
];
1154 tcg_opt_gen_movi(s
, op
, args
, rl
, (int32_t)r
);
1155 tcg_opt_gen_movi(s
, op2
, args2
, rh
, (int32_t)(r
>> 32));
1157 /* We've done all we need to do with the movi. Skip it. */
1158 oi_next
= op2
->next
;
1163 case INDEX_op_brcond2_i32
:
1164 tmp
= do_constant_folding_cond2(&args
[0], &args
[2], args
[4]);
1168 reset_all_temps(nb_temps
);
1169 op
->opc
= INDEX_op_br
;
1173 tcg_op_remove(s
, op
);
1175 } else if ((args
[4] == TCG_COND_LT
|| args
[4] == TCG_COND_GE
)
1176 && temp_is_const(args
[2]) && temps
[args
[2]].val
== 0
1177 && temp_is_const(args
[3]) && temps
[args
[3]].val
== 0) {
1178 /* Simplify LT/GE comparisons vs zero to a single compare
1179 vs the high word of the input. */
1181 reset_all_temps(nb_temps
);
1182 op
->opc
= INDEX_op_brcond_i32
;
1187 } else if (args
[4] == TCG_COND_EQ
) {
1188 /* Simplify EQ comparisons where one of the pairs
1189 can be simplified. */
1190 tmp
= do_constant_folding_cond(INDEX_op_brcond_i32
,
1191 args
[0], args
[2], TCG_COND_EQ
);
1193 goto do_brcond_false
;
1194 } else if (tmp
== 1) {
1195 goto do_brcond_high
;
1197 tmp
= do_constant_folding_cond(INDEX_op_brcond_i32
,
1198 args
[1], args
[3], TCG_COND_EQ
);
1200 goto do_brcond_false
;
1201 } else if (tmp
!= 1) {
1205 reset_all_temps(nb_temps
);
1206 op
->opc
= INDEX_op_brcond_i32
;
1210 } else if (args
[4] == TCG_COND_NE
) {
1211 /* Simplify NE comparisons where one of the pairs
1212 can be simplified. */
1213 tmp
= do_constant_folding_cond(INDEX_op_brcond_i32
,
1214 args
[0], args
[2], TCG_COND_NE
);
1216 goto do_brcond_high
;
1217 } else if (tmp
== 1) {
1218 goto do_brcond_true
;
1220 tmp
= do_constant_folding_cond(INDEX_op_brcond_i32
,
1221 args
[1], args
[3], TCG_COND_NE
);
1224 } else if (tmp
== 1) {
1225 goto do_brcond_true
;
1233 case INDEX_op_setcond2_i32
:
1234 tmp
= do_constant_folding_cond2(&args
[1], &args
[3], args
[5]);
1237 tcg_opt_gen_movi(s
, op
, args
, args
[0], tmp
);
1238 } else if ((args
[5] == TCG_COND_LT
|| args
[5] == TCG_COND_GE
)
1239 && temp_is_const(args
[3]) && temps
[args
[3]].val
== 0
1240 && temp_is_const(args
[4]) && temps
[args
[4]].val
== 0) {
1241 /* Simplify LT/GE comparisons vs zero to a single compare
1242 vs the high word of the input. */
1244 reset_temp(args
[0]);
1245 temps
[args
[0]].mask
= 1;
1246 op
->opc
= INDEX_op_setcond_i32
;
1250 } else if (args
[5] == TCG_COND_EQ
) {
1251 /* Simplify EQ comparisons where one of the pairs
1252 can be simplified. */
1253 tmp
= do_constant_folding_cond(INDEX_op_setcond_i32
,
1254 args
[1], args
[3], TCG_COND_EQ
);
1256 goto do_setcond_const
;
1257 } else if (tmp
== 1) {
1258 goto do_setcond_high
;
1260 tmp
= do_constant_folding_cond(INDEX_op_setcond_i32
,
1261 args
[2], args
[4], TCG_COND_EQ
);
1263 goto do_setcond_high
;
1264 } else if (tmp
!= 1) {
1268 reset_temp(args
[0]);
1269 temps
[args
[0]].mask
= 1;
1270 op
->opc
= INDEX_op_setcond_i32
;
1273 } else if (args
[5] == TCG_COND_NE
) {
1274 /* Simplify NE comparisons where one of the pairs
1275 can be simplified. */
1276 tmp
= do_constant_folding_cond(INDEX_op_setcond_i32
,
1277 args
[1], args
[3], TCG_COND_NE
);
1279 goto do_setcond_high
;
1280 } else if (tmp
== 1) {
1281 goto do_setcond_const
;
1283 tmp
= do_constant_folding_cond(INDEX_op_setcond_i32
,
1284 args
[2], args
[4], TCG_COND_NE
);
1286 goto do_setcond_low
;
1287 } else if (tmp
== 1) {
1288 goto do_setcond_const
;
1297 if (!(args
[nb_oargs
+ nb_iargs
+ 1]
1298 & (TCG_CALL_NO_READ_GLOBALS
| TCG_CALL_NO_WRITE_GLOBALS
))) {
1299 for (i
= 0; i
< nb_globals
; i
++) {
1300 if (test_bit(i
, temps_used
.l
)) {
1305 goto do_reset_output
;
1309 /* Default case: we know nothing about operation (or were unable
1310 to compute the operation result) so no propagation is done.
1311 We trash everything if the operation is the end of a basic
1312 block, otherwise we only trash the output args. "mask" is
1313 the non-zero bits mask for the first output arg. */
1314 if (def
->flags
& TCG_OPF_BB_END
) {
1315 reset_all_temps(nb_temps
);
1318 for (i
= 0; i
< nb_oargs
; i
++) {
1319 reset_temp(args
[i
]);
1320 /* Save the corresponding known-zero bits mask for the
1321 first output argument (only one supported so far). */
1323 temps
[args
[i
]].mask
= mask
;