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
*args
,
170 TCGArg dst
, TCGArg val
)
172 TCGOpcode new_op
= op_to_movi(op
->opc
);
173 tcg_target_ulong mask
;
178 temps
[dst
].is_const
= true;
179 temps
[dst
].val
= val
;
181 if (TCG_TARGET_REG_BITS
> 32 && new_op
== INDEX_op_movi_i32
) {
182 /* High bits of the destination are now garbage. */
183 mask
|= ~0xffffffffull
;
185 temps
[dst
].mask
= mask
;
191 static void tcg_opt_gen_mov(TCGContext
*s
, TCGOp
*op
, TCGArg
*args
,
192 TCGArg dst
, TCGArg src
)
194 if (temps_are_copies(dst
, src
)) {
195 tcg_op_remove(s
, op
);
199 TCGOpcode new_op
= op_to_mov(op
->opc
);
200 tcg_target_ulong mask
;
205 mask
= temps
[src
].mask
;
206 if (TCG_TARGET_REG_BITS
> 32 && new_op
== INDEX_op_mov_i32
) {
207 /* High bits of the destination are now garbage. */
208 mask
|= ~0xffffffffull
;
210 temps
[dst
].mask
= mask
;
212 if (s
->temps
[src
].type
== s
->temps
[dst
].type
) {
213 temps
[dst
].next_copy
= temps
[src
].next_copy
;
214 temps
[dst
].prev_copy
= src
;
215 temps
[temps
[dst
].next_copy
].prev_copy
= dst
;
216 temps
[src
].next_copy
= dst
;
217 temps
[dst
].is_const
= temps
[src
].is_const
;
218 temps
[dst
].val
= temps
[src
].val
;
225 static TCGArg
do_constant_folding_2(TCGOpcode op
, TCGArg x
, TCGArg y
)
248 case INDEX_op_shl_i32
:
249 return (uint32_t)x
<< (y
& 31);
251 case INDEX_op_shl_i64
:
252 return (uint64_t)x
<< (y
& 63);
254 case INDEX_op_shr_i32
:
255 return (uint32_t)x
>> (y
& 31);
257 case INDEX_op_shr_i64
:
258 return (uint64_t)x
>> (y
& 63);
260 case INDEX_op_sar_i32
:
261 return (int32_t)x
>> (y
& 31);
263 case INDEX_op_sar_i64
:
264 return (int64_t)x
>> (y
& 63);
266 case INDEX_op_rotr_i32
:
267 return ror32(x
, y
& 31);
269 case INDEX_op_rotr_i64
:
270 return ror64(x
, y
& 63);
272 case INDEX_op_rotl_i32
:
273 return rol32(x
, y
& 31);
275 case INDEX_op_rotl_i64
:
276 return rol64(x
, y
& 63);
299 case INDEX_op_clz_i32
:
300 return (uint32_t)x
? clz32(x
) : y
;
302 case INDEX_op_clz_i64
:
303 return x
? clz64(x
) : y
;
305 case INDEX_op_ctz_i32
:
306 return (uint32_t)x
? ctz32(x
) : y
;
308 case INDEX_op_ctz_i64
:
309 return x
? ctz64(x
) : y
;
311 CASE_OP_32_64(ext8s
):
314 CASE_OP_32_64(ext16s
):
317 CASE_OP_32_64(ext8u
):
320 CASE_OP_32_64(ext16u
):
323 case INDEX_op_ext_i32_i64
:
324 case INDEX_op_ext32s_i64
:
327 case INDEX_op_extu_i32_i64
:
328 case INDEX_op_extrl_i64_i32
:
329 case INDEX_op_ext32u_i64
:
332 case INDEX_op_extrh_i64_i32
:
333 return (uint64_t)x
>> 32;
335 case INDEX_op_muluh_i32
:
336 return ((uint64_t)(uint32_t)x
* (uint32_t)y
) >> 32;
337 case INDEX_op_mulsh_i32
:
338 return ((int64_t)(int32_t)x
* (int32_t)y
) >> 32;
340 case INDEX_op_muluh_i64
:
341 mulu64(&l64
, &h64
, x
, y
);
343 case INDEX_op_mulsh_i64
:
344 muls64(&l64
, &h64
, x
, y
);
347 case INDEX_op_div_i32
:
348 /* Avoid crashing on divide by zero, otherwise undefined. */
349 return (int32_t)x
/ ((int32_t)y
? : 1);
350 case INDEX_op_divu_i32
:
351 return (uint32_t)x
/ ((uint32_t)y
? : 1);
352 case INDEX_op_div_i64
:
353 return (int64_t)x
/ ((int64_t)y
? : 1);
354 case INDEX_op_divu_i64
:
355 return (uint64_t)x
/ ((uint64_t)y
? : 1);
357 case INDEX_op_rem_i32
:
358 return (int32_t)x
% ((int32_t)y
? : 1);
359 case INDEX_op_remu_i32
:
360 return (uint32_t)x
% ((uint32_t)y
? : 1);
361 case INDEX_op_rem_i64
:
362 return (int64_t)x
% ((int64_t)y
? : 1);
363 case INDEX_op_remu_i64
:
364 return (uint64_t)x
% ((uint64_t)y
? : 1);
368 "Unrecognized operation %d in do_constant_folding.\n", op
);
373 static TCGArg
do_constant_folding(TCGOpcode op
, TCGArg x
, TCGArg y
)
375 TCGArg res
= do_constant_folding_2(op
, x
, y
);
376 if (op_bits(op
) == 32) {
382 static bool do_constant_folding_cond_32(uint32_t x
, uint32_t y
, TCGCond c
)
390 return (int32_t)x
< (int32_t)y
;
392 return (int32_t)x
>= (int32_t)y
;
394 return (int32_t)x
<= (int32_t)y
;
396 return (int32_t)x
> (int32_t)y
;
410 static bool do_constant_folding_cond_64(uint64_t x
, uint64_t y
, TCGCond c
)
418 return (int64_t)x
< (int64_t)y
;
420 return (int64_t)x
>= (int64_t)y
;
422 return (int64_t)x
<= (int64_t)y
;
424 return (int64_t)x
> (int64_t)y
;
438 static bool do_constant_folding_cond_eq(TCGCond c
)
458 /* Return 2 if the condition can't be simplified, and the result
459 of the condition (0 or 1) if it can */
460 static TCGArg
do_constant_folding_cond(TCGOpcode op
, TCGArg x
,
463 if (temp_is_const(x
) && temp_is_const(y
)) {
464 switch (op_bits(op
)) {
466 return do_constant_folding_cond_32(temps
[x
].val
, temps
[y
].val
, c
);
468 return do_constant_folding_cond_64(temps
[x
].val
, temps
[y
].val
, c
);
472 } else if (temps_are_copies(x
, y
)) {
473 return do_constant_folding_cond_eq(c
);
474 } else if (temp_is_const(y
) && temps
[y
].val
== 0) {
487 /* Return 2 if the condition can't be simplified, and the result
488 of the condition (0 or 1) if it can */
489 static TCGArg
do_constant_folding_cond2(TCGArg
*p1
, TCGArg
*p2
, TCGCond c
)
491 TCGArg al
= p1
[0], ah
= p1
[1];
492 TCGArg bl
= p2
[0], bh
= p2
[1];
494 if (temp_is_const(bl
) && temp_is_const(bh
)) {
495 uint64_t b
= ((uint64_t)temps
[bh
].val
<< 32) | (uint32_t)temps
[bl
].val
;
497 if (temp_is_const(al
) && temp_is_const(ah
)) {
499 a
= ((uint64_t)temps
[ah
].val
<< 32) | (uint32_t)temps
[al
].val
;
500 return do_constant_folding_cond_64(a
, b
, c
);
513 if (temps_are_copies(al
, bl
) && temps_are_copies(ah
, bh
)) {
514 return do_constant_folding_cond_eq(c
);
519 static bool swap_commutative(TCGArg dest
, TCGArg
*p1
, TCGArg
*p2
)
521 TCGArg a1
= *p1
, a2
= *p2
;
523 sum
+= temp_is_const(a1
);
524 sum
-= temp_is_const(a2
);
526 /* Prefer the constant in second argument, and then the form
527 op a, a, b, which is better handled on non-RISC hosts. */
528 if (sum
> 0 || (sum
== 0 && dest
== a2
)) {
536 static bool swap_commutative2(TCGArg
*p1
, TCGArg
*p2
)
539 sum
+= temp_is_const(p1
[0]);
540 sum
+= temp_is_const(p1
[1]);
541 sum
-= temp_is_const(p2
[0]);
542 sum
-= temp_is_const(p2
[1]);
545 t
= p1
[0], p1
[0] = p2
[0], p2
[0] = t
;
546 t
= p1
[1], p1
[1] = p2
[1], p2
[1] = t
;
552 /* Propagate constants and copies, fold constant expressions. */
553 void tcg_optimize(TCGContext
*s
)
555 int oi
, oi_next
, nb_temps
, nb_globals
;
556 TCGArg
*prev_mb_args
= NULL
;
558 /* Array VALS has an element for each temp.
559 If this temp holds a constant then its value is kept in VALS' element.
560 If this temp is a copy of other ones then the other copies are
561 available through the doubly linked circular list. */
563 nb_temps
= s
->nb_temps
;
564 nb_globals
= s
->nb_globals
;
565 reset_all_temps(nb_temps
);
567 for (oi
= s
->gen_op_buf
[0].next
; oi
!= 0; oi
= oi_next
) {
568 tcg_target_ulong mask
, partmask
, affected
;
569 int nb_oargs
, nb_iargs
, i
;
572 TCGOp
* const op
= &s
->gen_op_buf
[oi
];
573 TCGArg
* const args
= &s
->gen_opparam_buf
[op
->args
];
574 TCGOpcode opc
= op
->opc
;
575 const TCGOpDef
*def
= &tcg_op_defs
[opc
];
579 /* Count the arguments, and initialize the temps that are
581 if (opc
== INDEX_op_call
) {
582 nb_oargs
= op
->callo
;
583 nb_iargs
= op
->calli
;
584 for (i
= 0; i
< nb_oargs
+ nb_iargs
; i
++) {
586 if (tmp
!= TCG_CALL_DUMMY_ARG
) {
591 nb_oargs
= def
->nb_oargs
;
592 nb_iargs
= def
->nb_iargs
;
593 for (i
= 0; i
< nb_oargs
+ nb_iargs
; i
++) {
594 init_temp_info(args
[i
]);
598 /* Do copy propagation */
599 for (i
= nb_oargs
; i
< nb_oargs
+ nb_iargs
; i
++) {
600 if (temp_is_copy(args
[i
])) {
601 args
[i
] = find_better_copy(s
, args
[i
]);
605 /* For commutative operations make constant second argument */
615 CASE_OP_32_64(muluh
):
616 CASE_OP_32_64(mulsh
):
617 swap_commutative(args
[0], &args
[1], &args
[2]);
619 CASE_OP_32_64(brcond
):
620 if (swap_commutative(-1, &args
[0], &args
[1])) {
621 args
[2] = tcg_swap_cond(args
[2]);
624 CASE_OP_32_64(setcond
):
625 if (swap_commutative(args
[0], &args
[1], &args
[2])) {
626 args
[3] = tcg_swap_cond(args
[3]);
629 CASE_OP_32_64(movcond
):
630 if (swap_commutative(-1, &args
[1], &args
[2])) {
631 args
[5] = tcg_swap_cond(args
[5]);
633 /* For movcond, we canonicalize the "false" input reg to match
634 the destination reg so that the tcg backend can implement
635 a "move if true" operation. */
636 if (swap_commutative(args
[0], &args
[4], &args
[3])) {
637 args
[5] = tcg_invert_cond(args
[5]);
641 swap_commutative(args
[0], &args
[2], &args
[4]);
642 swap_commutative(args
[1], &args
[3], &args
[5]);
644 CASE_OP_32_64(mulu2
):
645 CASE_OP_32_64(muls2
):
646 swap_commutative(args
[0], &args
[2], &args
[3]);
648 case INDEX_op_brcond2_i32
:
649 if (swap_commutative2(&args
[0], &args
[2])) {
650 args
[4] = tcg_swap_cond(args
[4]);
653 case INDEX_op_setcond2_i32
:
654 if (swap_commutative2(&args
[1], &args
[3])) {
655 args
[5] = tcg_swap_cond(args
[5]);
662 /* Simplify expressions for "shift/rot r, 0, a => movi r, 0",
663 and "sub r, 0, a => neg r, a" case. */
670 if (temp_is_const(args
[1]) && temps
[args
[1]].val
== 0) {
671 tcg_opt_gen_movi(s
, op
, args
, args
[0], 0);
680 if (temp_is_const(args
[2])) {
681 /* Proceed with possible constant folding. */
684 if (opc
== INDEX_op_sub_i32
) {
685 neg_op
= INDEX_op_neg_i32
;
686 have_neg
= TCG_TARGET_HAS_neg_i32
;
688 neg_op
= INDEX_op_neg_i64
;
689 have_neg
= TCG_TARGET_HAS_neg_i64
;
694 if (temp_is_const(args
[1]) && temps
[args
[1]].val
== 0) {
704 if (!temp_is_const(args
[1])
705 && temp_is_const(args
[2]) && temps
[args
[2]].val
== -1) {
711 if (!temp_is_const(args
[1])
712 && temp_is_const(args
[2]) && temps
[args
[2]].val
== 0) {
718 if (!temp_is_const(args
[2])
719 && temp_is_const(args
[1]) && temps
[args
[1]].val
== -1) {
726 if (!temp_is_const(args
[2])
727 && temp_is_const(args
[1]) && temps
[args
[1]].val
== 0) {
737 if (def
->flags
& TCG_OPF_64BIT
) {
738 not_op
= INDEX_op_not_i64
;
739 have_not
= TCG_TARGET_HAS_not_i64
;
741 not_op
= INDEX_op_not_i32
;
742 have_not
= TCG_TARGET_HAS_not_i32
;
756 /* Simplify expression for "op r, a, const => mov r, a" cases */
768 if (!temp_is_const(args
[1])
769 && temp_is_const(args
[2]) && temps
[args
[2]].val
== 0) {
770 tcg_opt_gen_mov(s
, op
, args
, args
[0], args
[1]);
777 if (!temp_is_const(args
[1])
778 && temp_is_const(args
[2]) && temps
[args
[2]].val
== -1) {
779 tcg_opt_gen_mov(s
, op
, args
, args
[0], args
[1]);
787 /* Simplify using known-zero bits. Currently only ops with a single
788 output argument is supported. */
792 CASE_OP_32_64(ext8s
):
793 if ((temps
[args
[1]].mask
& 0x80) != 0) {
796 CASE_OP_32_64(ext8u
):
799 CASE_OP_32_64(ext16s
):
800 if ((temps
[args
[1]].mask
& 0x8000) != 0) {
803 CASE_OP_32_64(ext16u
):
806 case INDEX_op_ext32s_i64
:
807 if ((temps
[args
[1]].mask
& 0x80000000) != 0) {
810 case INDEX_op_ext32u_i64
:
815 mask
= temps
[args
[2]].mask
;
816 if (temp_is_const(args
[2])) {
818 affected
= temps
[args
[1]].mask
& ~mask
;
820 mask
= temps
[args
[1]].mask
& mask
;
823 case INDEX_op_ext_i32_i64
:
824 if ((temps
[args
[1]].mask
& 0x80000000) != 0) {
827 case INDEX_op_extu_i32_i64
:
828 /* We do not compute affected as it is a size changing op. */
829 mask
= (uint32_t)temps
[args
[1]].mask
;
833 /* Known-zeros does not imply known-ones. Therefore unless
834 args[2] is constant, we can't infer anything from it. */
835 if (temp_is_const(args
[2])) {
836 mask
= ~temps
[args
[2]].mask
;
839 /* But we certainly know nothing outside args[1] may be set. */
840 mask
= temps
[args
[1]].mask
;
843 case INDEX_op_sar_i32
:
844 if (temp_is_const(args
[2])) {
845 tmp
= temps
[args
[2]].val
& 31;
846 mask
= (int32_t)temps
[args
[1]].mask
>> tmp
;
849 case INDEX_op_sar_i64
:
850 if (temp_is_const(args
[2])) {
851 tmp
= temps
[args
[2]].val
& 63;
852 mask
= (int64_t)temps
[args
[1]].mask
>> tmp
;
856 case INDEX_op_shr_i32
:
857 if (temp_is_const(args
[2])) {
858 tmp
= temps
[args
[2]].val
& 31;
859 mask
= (uint32_t)temps
[args
[1]].mask
>> tmp
;
862 case INDEX_op_shr_i64
:
863 if (temp_is_const(args
[2])) {
864 tmp
= temps
[args
[2]].val
& 63;
865 mask
= (uint64_t)temps
[args
[1]].mask
>> tmp
;
869 case INDEX_op_extrl_i64_i32
:
870 mask
= (uint32_t)temps
[args
[1]].mask
;
872 case INDEX_op_extrh_i64_i32
:
873 mask
= (uint64_t)temps
[args
[1]].mask
>> 32;
877 if (temp_is_const(args
[2])) {
878 tmp
= temps
[args
[2]].val
& (TCG_TARGET_REG_BITS
- 1);
879 mask
= temps
[args
[1]].mask
<< tmp
;
884 /* Set to 1 all bits to the left of the rightmost. */
885 mask
= -(temps
[args
[1]].mask
& -temps
[args
[1]].mask
);
888 CASE_OP_32_64(deposit
):
889 mask
= deposit64(temps
[args
[1]].mask
, args
[3], args
[4],
890 temps
[args
[2]].mask
);
893 CASE_OP_32_64(extract
):
894 mask
= extract64(temps
[args
[1]].mask
, args
[2], args
[3]);
896 affected
= temps
[args
[1]].mask
& ~mask
;
899 CASE_OP_32_64(sextract
):
900 mask
= sextract64(temps
[args
[1]].mask
, args
[2], args
[3]);
901 if (args
[2] == 0 && (tcg_target_long
)mask
>= 0) {
902 affected
= temps
[args
[1]].mask
& ~mask
;
908 mask
= temps
[args
[1]].mask
| temps
[args
[2]].mask
;
911 case INDEX_op_clz_i32
:
912 case INDEX_op_ctz_i32
:
913 mask
= temps
[args
[2]].mask
| 31;
916 case INDEX_op_clz_i64
:
917 case INDEX_op_ctz_i64
:
918 mask
= temps
[args
[2]].mask
| 63;
921 CASE_OP_32_64(setcond
):
922 case INDEX_op_setcond2_i32
:
926 CASE_OP_32_64(movcond
):
927 mask
= temps
[args
[3]].mask
| temps
[args
[4]].mask
;
933 CASE_OP_32_64(ld16u
):
936 case INDEX_op_ld32u_i64
:
940 CASE_OP_32_64(qemu_ld
):
942 TCGMemOpIdx oi
= args
[nb_oargs
+ nb_iargs
];
943 TCGMemOp mop
= get_memop(oi
);
944 if (!(mop
& MO_SIGN
)) {
945 mask
= (2ULL << ((8 << (mop
& MO_SIZE
)) - 1)) - 1;
954 /* 32-bit ops generate 32-bit results. For the result is zero test
955 below, we can ignore high bits, but for further optimizations we
956 need to record that the high bits contain garbage. */
958 if (!(def
->flags
& TCG_OPF_64BIT
)) {
959 mask
|= ~(tcg_target_ulong
)0xffffffffu
;
960 partmask
&= 0xffffffffu
;
961 affected
&= 0xffffffffu
;
965 tcg_debug_assert(nb_oargs
== 1);
966 tcg_opt_gen_movi(s
, op
, args
, args
[0], 0);
970 tcg_debug_assert(nb_oargs
== 1);
971 tcg_opt_gen_mov(s
, op
, args
, args
[0], args
[1]);
975 /* Simplify expression for "op r, a, 0 => movi r, 0" cases */
979 CASE_OP_32_64(muluh
):
980 CASE_OP_32_64(mulsh
):
981 if ((temp_is_const(args
[2]) && temps
[args
[2]].val
== 0)) {
982 tcg_opt_gen_movi(s
, op
, args
, args
[0], 0);
990 /* Simplify expression for "op r, a, a => mov r, a" cases */
994 if (temps_are_copies(args
[1], args
[2])) {
995 tcg_opt_gen_mov(s
, op
, args
, args
[0], args
[1]);
1003 /* Simplify expression for "op r, a, a => movi r, 0" cases */
1005 CASE_OP_32_64(andc
):
1008 if (temps_are_copies(args
[1], args
[2])) {
1009 tcg_opt_gen_movi(s
, op
, args
, args
[0], 0);
1017 /* Propagate constants through copy operations and do constant
1018 folding. Constants will be substituted to arguments by register
1019 allocator where needed and possible. Also detect copies. */
1022 tcg_opt_gen_mov(s
, op
, args
, args
[0], args
[1]);
1024 CASE_OP_32_64(movi
):
1025 tcg_opt_gen_movi(s
, op
, args
, args
[0], args
[1]);
1030 CASE_OP_32_64(ext8s
):
1031 CASE_OP_32_64(ext8u
):
1032 CASE_OP_32_64(ext16s
):
1033 CASE_OP_32_64(ext16u
):
1034 case INDEX_op_ext32s_i64
:
1035 case INDEX_op_ext32u_i64
:
1036 case INDEX_op_ext_i32_i64
:
1037 case INDEX_op_extu_i32_i64
:
1038 case INDEX_op_extrl_i64_i32
:
1039 case INDEX_op_extrh_i64_i32
:
1040 if (temp_is_const(args
[1])) {
1041 tmp
= do_constant_folding(opc
, temps
[args
[1]].val
, 0);
1042 tcg_opt_gen_movi(s
, op
, args
, args
[0], tmp
);
1056 CASE_OP_32_64(rotl
):
1057 CASE_OP_32_64(rotr
):
1058 CASE_OP_32_64(andc
):
1061 CASE_OP_32_64(nand
):
1063 CASE_OP_32_64(muluh
):
1064 CASE_OP_32_64(mulsh
):
1066 CASE_OP_32_64(divu
):
1068 CASE_OP_32_64(remu
):
1069 if (temp_is_const(args
[1]) && temp_is_const(args
[2])) {
1070 tmp
= do_constant_folding(opc
, temps
[args
[1]].val
,
1071 temps
[args
[2]].val
);
1072 tcg_opt_gen_movi(s
, op
, args
, args
[0], tmp
);
1079 if (temp_is_const(args
[1])) {
1080 TCGArg v
= temps
[args
[1]].val
;
1082 tmp
= do_constant_folding(opc
, v
, 0);
1083 tcg_opt_gen_movi(s
, op
, args
, args
[0], tmp
);
1085 tcg_opt_gen_mov(s
, op
, args
, args
[0], args
[2]);
1091 CASE_OP_32_64(deposit
):
1092 if (temp_is_const(args
[1]) && temp_is_const(args
[2])) {
1093 tmp
= deposit64(temps
[args
[1]].val
, args
[3], args
[4],
1094 temps
[args
[2]].val
);
1095 tcg_opt_gen_movi(s
, op
, args
, args
[0], tmp
);
1100 CASE_OP_32_64(extract
):
1101 if (temp_is_const(args
[1])) {
1102 tmp
= extract64(temps
[args
[1]].val
, args
[2], args
[3]);
1103 tcg_opt_gen_movi(s
, op
, args
, args
[0], tmp
);
1108 CASE_OP_32_64(sextract
):
1109 if (temp_is_const(args
[1])) {
1110 tmp
= sextract64(temps
[args
[1]].val
, args
[2], args
[3]);
1111 tcg_opt_gen_movi(s
, op
, args
, args
[0], tmp
);
1116 CASE_OP_32_64(setcond
):
1117 tmp
= do_constant_folding_cond(opc
, args
[1], args
[2], args
[3]);
1119 tcg_opt_gen_movi(s
, op
, args
, args
[0], tmp
);
1124 CASE_OP_32_64(brcond
):
1125 tmp
= do_constant_folding_cond(opc
, args
[0], args
[1], args
[2]);
1128 reset_all_temps(nb_temps
);
1129 op
->opc
= INDEX_op_br
;
1132 tcg_op_remove(s
, op
);
1138 CASE_OP_32_64(movcond
):
1139 tmp
= do_constant_folding_cond(opc
, args
[1], args
[2], args
[5]);
1141 tcg_opt_gen_mov(s
, op
, args
, args
[0], args
[4-tmp
]);
1144 if (temp_is_const(args
[3]) && temp_is_const(args
[4])) {
1145 tcg_target_ulong tv
= temps
[args
[3]].val
;
1146 tcg_target_ulong fv
= temps
[args
[4]].val
;
1147 TCGCond cond
= args
[5];
1148 if (fv
== 1 && tv
== 0) {
1149 cond
= tcg_invert_cond(cond
);
1150 } else if (!(tv
== 1 && fv
== 0)) {
1154 op
->opc
= opc
= (opc
== INDEX_op_movcond_i32
1155 ? INDEX_op_setcond_i32
1156 : INDEX_op_setcond_i64
);
1161 case INDEX_op_add2_i32
:
1162 case INDEX_op_sub2_i32
:
1163 if (temp_is_const(args
[2]) && temp_is_const(args
[3])
1164 && temp_is_const(args
[4]) && temp_is_const(args
[5])) {
1165 uint32_t al
= temps
[args
[2]].val
;
1166 uint32_t ah
= temps
[args
[3]].val
;
1167 uint32_t bl
= temps
[args
[4]].val
;
1168 uint32_t bh
= temps
[args
[5]].val
;
1169 uint64_t a
= ((uint64_t)ah
<< 32) | al
;
1170 uint64_t b
= ((uint64_t)bh
<< 32) | bl
;
1172 TCGOp
*op2
= tcg_op_insert_before(s
, op
, INDEX_op_movi_i32
, 2);
1173 TCGArg
*args2
= &s
->gen_opparam_buf
[op2
->args
];
1175 if (opc
== INDEX_op_add2_i32
) {
1183 tcg_opt_gen_movi(s
, op
, args
, rl
, (int32_t)a
);
1184 tcg_opt_gen_movi(s
, op2
, args2
, rh
, (int32_t)(a
>> 32));
1186 /* We've done all we need to do with the movi. Skip it. */
1187 oi_next
= op2
->next
;
1192 case INDEX_op_mulu2_i32
:
1193 if (temp_is_const(args
[2]) && temp_is_const(args
[3])) {
1194 uint32_t a
= temps
[args
[2]].val
;
1195 uint32_t b
= temps
[args
[3]].val
;
1196 uint64_t r
= (uint64_t)a
* b
;
1198 TCGOp
*op2
= tcg_op_insert_before(s
, op
, INDEX_op_movi_i32
, 2);
1199 TCGArg
*args2
= &s
->gen_opparam_buf
[op2
->args
];
1203 tcg_opt_gen_movi(s
, op
, args
, rl
, (int32_t)r
);
1204 tcg_opt_gen_movi(s
, op2
, args2
, rh
, (int32_t)(r
>> 32));
1206 /* We've done all we need to do with the movi. Skip it. */
1207 oi_next
= op2
->next
;
1212 case INDEX_op_brcond2_i32
:
1213 tmp
= do_constant_folding_cond2(&args
[0], &args
[2], args
[4]);
1217 reset_all_temps(nb_temps
);
1218 op
->opc
= INDEX_op_br
;
1222 tcg_op_remove(s
, op
);
1224 } else if ((args
[4] == TCG_COND_LT
|| args
[4] == TCG_COND_GE
)
1225 && temp_is_const(args
[2]) && temps
[args
[2]].val
== 0
1226 && temp_is_const(args
[3]) && temps
[args
[3]].val
== 0) {
1227 /* Simplify LT/GE comparisons vs zero to a single compare
1228 vs the high word of the input. */
1230 reset_all_temps(nb_temps
);
1231 op
->opc
= INDEX_op_brcond_i32
;
1236 } else if (args
[4] == TCG_COND_EQ
) {
1237 /* Simplify EQ comparisons where one of the pairs
1238 can be simplified. */
1239 tmp
= do_constant_folding_cond(INDEX_op_brcond_i32
,
1240 args
[0], args
[2], TCG_COND_EQ
);
1242 goto do_brcond_false
;
1243 } else if (tmp
== 1) {
1244 goto do_brcond_high
;
1246 tmp
= do_constant_folding_cond(INDEX_op_brcond_i32
,
1247 args
[1], args
[3], TCG_COND_EQ
);
1249 goto do_brcond_false
;
1250 } else if (tmp
!= 1) {
1254 reset_all_temps(nb_temps
);
1255 op
->opc
= INDEX_op_brcond_i32
;
1259 } else if (args
[4] == TCG_COND_NE
) {
1260 /* Simplify NE comparisons where one of the pairs
1261 can be simplified. */
1262 tmp
= do_constant_folding_cond(INDEX_op_brcond_i32
,
1263 args
[0], args
[2], TCG_COND_NE
);
1265 goto do_brcond_high
;
1266 } else if (tmp
== 1) {
1267 goto do_brcond_true
;
1269 tmp
= do_constant_folding_cond(INDEX_op_brcond_i32
,
1270 args
[1], args
[3], TCG_COND_NE
);
1273 } else if (tmp
== 1) {
1274 goto do_brcond_true
;
1282 case INDEX_op_setcond2_i32
:
1283 tmp
= do_constant_folding_cond2(&args
[1], &args
[3], args
[5]);
1286 tcg_opt_gen_movi(s
, op
, args
, args
[0], tmp
);
1287 } else if ((args
[5] == TCG_COND_LT
|| args
[5] == TCG_COND_GE
)
1288 && temp_is_const(args
[3]) && temps
[args
[3]].val
== 0
1289 && temp_is_const(args
[4]) && temps
[args
[4]].val
== 0) {
1290 /* Simplify LT/GE comparisons vs zero to a single compare
1291 vs the high word of the input. */
1293 reset_temp(args
[0]);
1294 temps
[args
[0]].mask
= 1;
1295 op
->opc
= INDEX_op_setcond_i32
;
1299 } else if (args
[5] == TCG_COND_EQ
) {
1300 /* Simplify EQ comparisons where one of the pairs
1301 can be simplified. */
1302 tmp
= do_constant_folding_cond(INDEX_op_setcond_i32
,
1303 args
[1], args
[3], TCG_COND_EQ
);
1305 goto do_setcond_const
;
1306 } else if (tmp
== 1) {
1307 goto do_setcond_high
;
1309 tmp
= do_constant_folding_cond(INDEX_op_setcond_i32
,
1310 args
[2], args
[4], TCG_COND_EQ
);
1312 goto do_setcond_high
;
1313 } else if (tmp
!= 1) {
1317 reset_temp(args
[0]);
1318 temps
[args
[0]].mask
= 1;
1319 op
->opc
= INDEX_op_setcond_i32
;
1322 } else if (args
[5] == TCG_COND_NE
) {
1323 /* Simplify NE comparisons where one of the pairs
1324 can be simplified. */
1325 tmp
= do_constant_folding_cond(INDEX_op_setcond_i32
,
1326 args
[1], args
[3], TCG_COND_NE
);
1328 goto do_setcond_high
;
1329 } else if (tmp
== 1) {
1330 goto do_setcond_const
;
1332 tmp
= do_constant_folding_cond(INDEX_op_setcond_i32
,
1333 args
[2], args
[4], TCG_COND_NE
);
1335 goto do_setcond_low
;
1336 } else if (tmp
== 1) {
1337 goto do_setcond_const
;
1346 if (!(args
[nb_oargs
+ nb_iargs
+ 1]
1347 & (TCG_CALL_NO_READ_GLOBALS
| TCG_CALL_NO_WRITE_GLOBALS
))) {
1348 for (i
= 0; i
< nb_globals
; i
++) {
1349 if (test_bit(i
, temps_used
.l
)) {
1354 goto do_reset_output
;
1358 /* Default case: we know nothing about operation (or were unable
1359 to compute the operation result) so no propagation is done.
1360 We trash everything if the operation is the end of a basic
1361 block, otherwise we only trash the output args. "mask" is
1362 the non-zero bits mask for the first output arg. */
1363 if (def
->flags
& TCG_OPF_BB_END
) {
1364 reset_all_temps(nb_temps
);
1367 for (i
= 0; i
< nb_oargs
; i
++) {
1368 reset_temp(args
[i
]);
1369 /* Save the corresponding known-zero bits mask for the
1370 first output argument (only one supported so far). */
1372 temps
[args
[i
]].mask
= mask
;
1379 /* Eliminate duplicate and redundant fence instructions. */
1383 /* Merge two barriers of the same type into one,
1384 * or a weaker barrier into a stronger one,
1385 * or two weaker barriers into a stronger one.
1386 * mb X; mb Y => mb X|Y
1387 * mb; strl => mb; st
1388 * ldaq; mb => ld; mb
1389 * ldaq; strl => ld; mb; st
1390 * Other combinations are also merged into a strong
1391 * barrier. This is stricter than specified but for
1392 * the purposes of TCG is better than not optimizing.
1394 prev_mb_args
[0] |= args
[0];
1395 tcg_op_remove(s
, op
);
1399 /* Opcodes that end the block stop the optimization. */
1400 if ((def
->flags
& TCG_OPF_BB_END
) == 0) {
1404 case INDEX_op_qemu_ld_i32
:
1405 case INDEX_op_qemu_ld_i64
:
1406 case INDEX_op_qemu_st_i32
:
1407 case INDEX_op_qemu_st_i64
:
1409 /* Opcodes that touch guest memory stop the optimization. */
1410 prev_mb_args
= NULL
;
1413 } else if (opc
== INDEX_op_mb
) {
1414 prev_mb_args
= args
;