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 inline struct tcg_temp_info
*ts_info(TCGTemp
*ts
)
48 static inline struct tcg_temp_info
*arg_info(TCGArg arg
)
50 return ts_info(arg_temp(arg
));
53 static inline bool ts_is_const(TCGTemp
*ts
)
55 return ts_info(ts
)->is_const
;
58 static inline bool arg_is_const(TCGArg arg
)
60 return ts_is_const(arg_temp(arg
));
63 static inline bool ts_is_copy(TCGTemp
*ts
)
65 return ts_info(ts
)->next_copy
!= ts
;
68 /* Reset TEMP's state, possibly removing the temp for the list of copies. */
69 static void reset_ts(TCGTemp
*ts
)
71 struct tcg_temp_info
*ti
= ts_info(ts
);
72 struct tcg_temp_info
*pi
= ts_info(ti
->prev_copy
);
73 struct tcg_temp_info
*ni
= ts_info(ti
->next_copy
);
75 ni
->prev_copy
= ti
->prev_copy
;
76 pi
->next_copy
= ti
->next_copy
;
83 static void reset_temp(TCGArg arg
)
85 reset_ts(arg_temp(arg
));
88 /* Initialize and activate a temporary. */
89 static void init_ts_info(struct tcg_temp_info
*infos
,
90 TCGTempSet
*temps_used
, TCGTemp
*ts
)
92 size_t idx
= temp_idx(ts
);
93 if (!test_bit(idx
, temps_used
->l
)) {
94 struct tcg_temp_info
*ti
= &infos
[idx
];
101 set_bit(idx
, temps_used
->l
);
105 static void init_arg_info(struct tcg_temp_info
*infos
,
106 TCGTempSet
*temps_used
, TCGArg arg
)
108 init_ts_info(infos
, temps_used
, arg_temp(arg
));
111 static int op_bits(TCGOpcode op
)
113 const TCGOpDef
*def
= &tcg_op_defs
[op
];
114 return def
->flags
& TCG_OPF_64BIT
? 64 : 32;
117 static TCGOpcode
op_to_mov(TCGOpcode op
)
119 switch (op_bits(op
)) {
121 return INDEX_op_mov_i32
;
123 return INDEX_op_mov_i64
;
125 fprintf(stderr
, "op_to_mov: unexpected return value of "
126 "function op_bits.\n");
131 static TCGOpcode
op_to_movi(TCGOpcode op
)
133 switch (op_bits(op
)) {
135 return INDEX_op_movi_i32
;
137 return INDEX_op_movi_i64
;
139 fprintf(stderr
, "op_to_movi: unexpected return value of "
140 "function op_bits.\n");
145 static TCGTemp
*find_better_copy(TCGContext
*s
, TCGTemp
*ts
)
149 /* If this is already a global, we can't do better. */
150 if (ts
->temp_global
) {
154 /* Search for a global first. */
155 for (i
= ts_info(ts
)->next_copy
; i
!= ts
; i
= ts_info(i
)->next_copy
) {
156 if (i
->temp_global
) {
161 /* If it is a temp, search for a temp local. */
162 if (!ts
->temp_local
) {
163 for (i
= ts_info(ts
)->next_copy
; i
!= ts
; i
= ts_info(i
)->next_copy
) {
164 if (ts
->temp_local
) {
170 /* Failure to find a better representation, return the same temp. */
174 static bool ts_are_copies(TCGTemp
*ts1
, TCGTemp
*ts2
)
182 if (!ts_is_copy(ts1
) || !ts_is_copy(ts2
)) {
186 for (i
= ts_info(ts1
)->next_copy
; i
!= ts1
; i
= ts_info(i
)->next_copy
) {
195 static bool args_are_copies(TCGArg arg1
, TCGArg arg2
)
197 return ts_are_copies(arg_temp(arg1
), arg_temp(arg2
));
200 static void tcg_opt_gen_movi(TCGContext
*s
, TCGOp
*op
, TCGArg dst
, TCGArg val
)
202 TCGOpcode new_op
= op_to_movi(op
->opc
);
203 tcg_target_ulong mask
;
204 struct tcg_temp_info
*di
= arg_info(dst
);
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
;
222 static void tcg_opt_gen_mov(TCGContext
*s
, TCGOp
*op
, TCGArg dst
, TCGArg src
)
224 TCGTemp
*dst_ts
= arg_temp(dst
);
225 TCGTemp
*src_ts
= arg_temp(src
);
226 struct tcg_temp_info
*di
;
227 struct tcg_temp_info
*si
;
228 tcg_target_ulong mask
;
231 if (ts_are_copies(dst_ts
, src_ts
)) {
232 tcg_op_remove(s
, op
);
237 di
= ts_info(dst_ts
);
238 si
= ts_info(src_ts
);
239 new_op
= op_to_mov(op
->opc
);
246 if (TCG_TARGET_REG_BITS
> 32 && new_op
== INDEX_op_mov_i32
) {
247 /* High bits of the destination are now garbage. */
248 mask
|= ~0xffffffffull
;
252 if (src_ts
->type
== dst_ts
->type
) {
253 struct tcg_temp_info
*ni
= ts_info(si
->next_copy
);
255 di
->next_copy
= si
->next_copy
;
256 di
->prev_copy
= src_ts
;
257 ni
->prev_copy
= dst_ts
;
258 si
->next_copy
= dst_ts
;
259 di
->is_const
= si
->is_const
;
264 static TCGArg
do_constant_folding_2(TCGOpcode op
, TCGArg x
, TCGArg y
)
287 case INDEX_op_shl_i32
:
288 return (uint32_t)x
<< (y
& 31);
290 case INDEX_op_shl_i64
:
291 return (uint64_t)x
<< (y
& 63);
293 case INDEX_op_shr_i32
:
294 return (uint32_t)x
>> (y
& 31);
296 case INDEX_op_shr_i64
:
297 return (uint64_t)x
>> (y
& 63);
299 case INDEX_op_sar_i32
:
300 return (int32_t)x
>> (y
& 31);
302 case INDEX_op_sar_i64
:
303 return (int64_t)x
>> (y
& 63);
305 case INDEX_op_rotr_i32
:
306 return ror32(x
, y
& 31);
308 case INDEX_op_rotr_i64
:
309 return ror64(x
, y
& 63);
311 case INDEX_op_rotl_i32
:
312 return rol32(x
, y
& 31);
314 case INDEX_op_rotl_i64
:
315 return rol64(x
, y
& 63);
338 case INDEX_op_clz_i32
:
339 return (uint32_t)x
? clz32(x
) : y
;
341 case INDEX_op_clz_i64
:
342 return x
? clz64(x
) : y
;
344 case INDEX_op_ctz_i32
:
345 return (uint32_t)x
? ctz32(x
) : y
;
347 case INDEX_op_ctz_i64
:
348 return x
? ctz64(x
) : y
;
350 case INDEX_op_ctpop_i32
:
353 case INDEX_op_ctpop_i64
:
356 CASE_OP_32_64(ext8s
):
359 CASE_OP_32_64(ext16s
):
362 CASE_OP_32_64(ext8u
):
365 CASE_OP_32_64(ext16u
):
368 case INDEX_op_ext_i32_i64
:
369 case INDEX_op_ext32s_i64
:
372 case INDEX_op_extu_i32_i64
:
373 case INDEX_op_extrl_i64_i32
:
374 case INDEX_op_ext32u_i64
:
377 case INDEX_op_extrh_i64_i32
:
378 return (uint64_t)x
>> 32;
380 case INDEX_op_muluh_i32
:
381 return ((uint64_t)(uint32_t)x
* (uint32_t)y
) >> 32;
382 case INDEX_op_mulsh_i32
:
383 return ((int64_t)(int32_t)x
* (int32_t)y
) >> 32;
385 case INDEX_op_muluh_i64
:
386 mulu64(&l64
, &h64
, x
, y
);
388 case INDEX_op_mulsh_i64
:
389 muls64(&l64
, &h64
, x
, y
);
392 case INDEX_op_div_i32
:
393 /* Avoid crashing on divide by zero, otherwise undefined. */
394 return (int32_t)x
/ ((int32_t)y
? : 1);
395 case INDEX_op_divu_i32
:
396 return (uint32_t)x
/ ((uint32_t)y
? : 1);
397 case INDEX_op_div_i64
:
398 return (int64_t)x
/ ((int64_t)y
? : 1);
399 case INDEX_op_divu_i64
:
400 return (uint64_t)x
/ ((uint64_t)y
? : 1);
402 case INDEX_op_rem_i32
:
403 return (int32_t)x
% ((int32_t)y
? : 1);
404 case INDEX_op_remu_i32
:
405 return (uint32_t)x
% ((uint32_t)y
? : 1);
406 case INDEX_op_rem_i64
:
407 return (int64_t)x
% ((int64_t)y
? : 1);
408 case INDEX_op_remu_i64
:
409 return (uint64_t)x
% ((uint64_t)y
? : 1);
413 "Unrecognized operation %d in do_constant_folding.\n", op
);
418 static TCGArg
do_constant_folding(TCGOpcode op
, TCGArg x
, TCGArg y
)
420 TCGArg res
= do_constant_folding_2(op
, x
, y
);
421 if (op_bits(op
) == 32) {
427 static bool do_constant_folding_cond_32(uint32_t x
, uint32_t y
, TCGCond c
)
435 return (int32_t)x
< (int32_t)y
;
437 return (int32_t)x
>= (int32_t)y
;
439 return (int32_t)x
<= (int32_t)y
;
441 return (int32_t)x
> (int32_t)y
;
455 static bool do_constant_folding_cond_64(uint64_t x
, uint64_t y
, TCGCond c
)
463 return (int64_t)x
< (int64_t)y
;
465 return (int64_t)x
>= (int64_t)y
;
467 return (int64_t)x
<= (int64_t)y
;
469 return (int64_t)x
> (int64_t)y
;
483 static bool do_constant_folding_cond_eq(TCGCond c
)
503 /* Return 2 if the condition can't be simplified, and the result
504 of the condition (0 or 1) if it can */
505 static TCGArg
do_constant_folding_cond(TCGOpcode op
, TCGArg x
,
508 tcg_target_ulong xv
= arg_info(x
)->val
;
509 tcg_target_ulong yv
= arg_info(y
)->val
;
510 if (arg_is_const(x
) && arg_is_const(y
)) {
511 switch (op_bits(op
)) {
513 return do_constant_folding_cond_32(xv
, yv
, c
);
515 return do_constant_folding_cond_64(xv
, yv
, c
);
519 } else if (args_are_copies(x
, y
)) {
520 return do_constant_folding_cond_eq(c
);
521 } else if (arg_is_const(y
) && yv
== 0) {
534 /* Return 2 if the condition can't be simplified, and the result
535 of the condition (0 or 1) if it can */
536 static TCGArg
do_constant_folding_cond2(TCGArg
*p1
, TCGArg
*p2
, TCGCond c
)
538 TCGArg al
= p1
[0], ah
= p1
[1];
539 TCGArg bl
= p2
[0], bh
= p2
[1];
541 if (arg_is_const(bl
) && arg_is_const(bh
)) {
542 tcg_target_ulong blv
= arg_info(bl
)->val
;
543 tcg_target_ulong bhv
= arg_info(bh
)->val
;
544 uint64_t b
= deposit64(blv
, 32, 32, bhv
);
546 if (arg_is_const(al
) && arg_is_const(ah
)) {
547 tcg_target_ulong alv
= arg_info(al
)->val
;
548 tcg_target_ulong ahv
= arg_info(ah
)->val
;
549 uint64_t a
= deposit64(alv
, 32, 32, ahv
);
550 return do_constant_folding_cond_64(a
, b
, c
);
563 if (args_are_copies(al
, bl
) && args_are_copies(ah
, bh
)) {
564 return do_constant_folding_cond_eq(c
);
569 static bool swap_commutative(TCGArg dest
, TCGArg
*p1
, TCGArg
*p2
)
571 TCGArg a1
= *p1
, a2
= *p2
;
573 sum
+= arg_is_const(a1
);
574 sum
-= arg_is_const(a2
);
576 /* Prefer the constant in second argument, and then the form
577 op a, a, b, which is better handled on non-RISC hosts. */
578 if (sum
> 0 || (sum
== 0 && dest
== a2
)) {
586 static bool swap_commutative2(TCGArg
*p1
, TCGArg
*p2
)
589 sum
+= arg_is_const(p1
[0]);
590 sum
+= arg_is_const(p1
[1]);
591 sum
-= arg_is_const(p2
[0]);
592 sum
-= arg_is_const(p2
[1]);
595 t
= p1
[0], p1
[0] = p2
[0], p2
[0] = t
;
596 t
= p1
[1], p1
[1] = p2
[1], p2
[1] = t
;
602 /* Propagate constants and copies, fold constant expressions. */
603 void tcg_optimize(TCGContext
*s
)
605 int oi
, oi_next
, nb_temps
, nb_globals
;
606 TCGOp
*prev_mb
= NULL
;
607 struct tcg_temp_info
*infos
;
608 TCGTempSet temps_used
;
610 /* Array VALS has an element for each temp.
611 If this temp holds a constant then its value is kept in VALS' element.
612 If this temp is a copy of other ones then the other copies are
613 available through the doubly linked circular list. */
615 nb_temps
= s
->nb_temps
;
616 nb_globals
= s
->nb_globals
;
617 bitmap_zero(temps_used
.l
, nb_temps
);
618 infos
= tcg_malloc(sizeof(struct tcg_temp_info
) * nb_temps
);
620 for (oi
= s
->gen_op_buf
[0].next
; oi
!= 0; oi
= oi_next
) {
621 tcg_target_ulong mask
, partmask
, affected
;
622 int nb_oargs
, nb_iargs
, i
;
625 TCGOp
* const op
= &s
->gen_op_buf
[oi
];
626 TCGOpcode opc
= op
->opc
;
627 const TCGOpDef
*def
= &tcg_op_defs
[opc
];
631 /* Count the arguments, and initialize the temps that are
633 if (opc
== INDEX_op_call
) {
634 nb_oargs
= op
->callo
;
635 nb_iargs
= op
->calli
;
636 for (i
= 0; i
< nb_oargs
+ nb_iargs
; i
++) {
637 TCGTemp
*ts
= arg_temp(op
->args
[i
]);
639 init_ts_info(infos
, &temps_used
, ts
);
643 nb_oargs
= def
->nb_oargs
;
644 nb_iargs
= def
->nb_iargs
;
645 for (i
= 0; i
< nb_oargs
+ nb_iargs
; i
++) {
646 init_arg_info(infos
, &temps_used
, op
->args
[i
]);
650 /* Do copy propagation */
651 for (i
= nb_oargs
; i
< nb_oargs
+ nb_iargs
; i
++) {
652 TCGTemp
*ts
= arg_temp(op
->args
[i
]);
653 if (ts
&& ts_is_copy(ts
)) {
654 op
->args
[i
] = temp_arg(find_better_copy(s
, ts
));
658 /* For commutative operations make constant second argument */
668 CASE_OP_32_64(muluh
):
669 CASE_OP_32_64(mulsh
):
670 swap_commutative(op
->args
[0], &op
->args
[1], &op
->args
[2]);
672 CASE_OP_32_64(brcond
):
673 if (swap_commutative(-1, &op
->args
[0], &op
->args
[1])) {
674 op
->args
[2] = tcg_swap_cond(op
->args
[2]);
677 CASE_OP_32_64(setcond
):
678 if (swap_commutative(op
->args
[0], &op
->args
[1], &op
->args
[2])) {
679 op
->args
[3] = tcg_swap_cond(op
->args
[3]);
682 CASE_OP_32_64(movcond
):
683 if (swap_commutative(-1, &op
->args
[1], &op
->args
[2])) {
684 op
->args
[5] = tcg_swap_cond(op
->args
[5]);
686 /* For movcond, we canonicalize the "false" input reg to match
687 the destination reg so that the tcg backend can implement
688 a "move if true" operation. */
689 if (swap_commutative(op
->args
[0], &op
->args
[4], &op
->args
[3])) {
690 op
->args
[5] = tcg_invert_cond(op
->args
[5]);
694 swap_commutative(op
->args
[0], &op
->args
[2], &op
->args
[4]);
695 swap_commutative(op
->args
[1], &op
->args
[3], &op
->args
[5]);
697 CASE_OP_32_64(mulu2
):
698 CASE_OP_32_64(muls2
):
699 swap_commutative(op
->args
[0], &op
->args
[2], &op
->args
[3]);
701 case INDEX_op_brcond2_i32
:
702 if (swap_commutative2(&op
->args
[0], &op
->args
[2])) {
703 op
->args
[4] = tcg_swap_cond(op
->args
[4]);
706 case INDEX_op_setcond2_i32
:
707 if (swap_commutative2(&op
->args
[1], &op
->args
[3])) {
708 op
->args
[5] = tcg_swap_cond(op
->args
[5]);
715 /* Simplify expressions for "shift/rot r, 0, a => movi r, 0",
716 and "sub r, 0, a => neg r, a" case. */
723 if (arg_is_const(op
->args
[1])
724 && arg_info(op
->args
[1])->val
== 0) {
725 tcg_opt_gen_movi(s
, op
, op
->args
[0], 0);
734 if (arg_is_const(op
->args
[2])) {
735 /* Proceed with possible constant folding. */
738 if (opc
== INDEX_op_sub_i32
) {
739 neg_op
= INDEX_op_neg_i32
;
740 have_neg
= TCG_TARGET_HAS_neg_i32
;
742 neg_op
= INDEX_op_neg_i64
;
743 have_neg
= TCG_TARGET_HAS_neg_i64
;
748 if (arg_is_const(op
->args
[1])
749 && arg_info(op
->args
[1])->val
== 0) {
751 reset_temp(op
->args
[0]);
752 op
->args
[1] = op
->args
[2];
759 if (!arg_is_const(op
->args
[1])
760 && arg_is_const(op
->args
[2])
761 && arg_info(op
->args
[2])->val
== -1) {
767 if (!arg_is_const(op
->args
[1])
768 && arg_is_const(op
->args
[2])
769 && arg_info(op
->args
[2])->val
== 0) {
775 if (!arg_is_const(op
->args
[2])
776 && arg_is_const(op
->args
[1])
777 && arg_info(op
->args
[1])->val
== -1) {
784 if (!arg_is_const(op
->args
[2])
785 && arg_is_const(op
->args
[1])
786 && arg_info(op
->args
[1])->val
== 0) {
796 if (def
->flags
& TCG_OPF_64BIT
) {
797 not_op
= INDEX_op_not_i64
;
798 have_not
= TCG_TARGET_HAS_not_i64
;
800 not_op
= INDEX_op_not_i32
;
801 have_not
= TCG_TARGET_HAS_not_i32
;
807 reset_temp(op
->args
[0]);
808 op
->args
[1] = op
->args
[i
];
815 /* Simplify expression for "op r, a, const => mov r, a" cases */
827 if (!arg_is_const(op
->args
[1])
828 && arg_is_const(op
->args
[2])
829 && arg_info(op
->args
[2])->val
== 0) {
830 tcg_opt_gen_mov(s
, op
, op
->args
[0], op
->args
[1]);
837 if (!arg_is_const(op
->args
[1])
838 && arg_is_const(op
->args
[2])
839 && arg_info(op
->args
[2])->val
== -1) {
840 tcg_opt_gen_mov(s
, op
, op
->args
[0], op
->args
[1]);
848 /* Simplify using known-zero bits. Currently only ops with a single
849 output argument is supported. */
853 CASE_OP_32_64(ext8s
):
854 if ((arg_info(op
->args
[1])->mask
& 0x80) != 0) {
857 CASE_OP_32_64(ext8u
):
860 CASE_OP_32_64(ext16s
):
861 if ((arg_info(op
->args
[1])->mask
& 0x8000) != 0) {
864 CASE_OP_32_64(ext16u
):
867 case INDEX_op_ext32s_i64
:
868 if ((arg_info(op
->args
[1])->mask
& 0x80000000) != 0) {
871 case INDEX_op_ext32u_i64
:
876 mask
= arg_info(op
->args
[2])->mask
;
877 if (arg_is_const(op
->args
[2])) {
879 affected
= arg_info(op
->args
[1])->mask
& ~mask
;
881 mask
= arg_info(op
->args
[1])->mask
& mask
;
884 case INDEX_op_ext_i32_i64
:
885 if ((arg_info(op
->args
[1])->mask
& 0x80000000) != 0) {
888 case INDEX_op_extu_i32_i64
:
889 /* We do not compute affected as it is a size changing op. */
890 mask
= (uint32_t)arg_info(op
->args
[1])->mask
;
894 /* Known-zeros does not imply known-ones. Therefore unless
895 op->args[2] is constant, we can't infer anything from it. */
896 if (arg_is_const(op
->args
[2])) {
897 mask
= ~arg_info(op
->args
[2])->mask
;
900 /* But we certainly know nothing outside args[1] may be set. */
901 mask
= arg_info(op
->args
[1])->mask
;
904 case INDEX_op_sar_i32
:
905 if (arg_is_const(op
->args
[2])) {
906 tmp
= arg_info(op
->args
[2])->val
& 31;
907 mask
= (int32_t)arg_info(op
->args
[1])->mask
>> tmp
;
910 case INDEX_op_sar_i64
:
911 if (arg_is_const(op
->args
[2])) {
912 tmp
= arg_info(op
->args
[2])->val
& 63;
913 mask
= (int64_t)arg_info(op
->args
[1])->mask
>> tmp
;
917 case INDEX_op_shr_i32
:
918 if (arg_is_const(op
->args
[2])) {
919 tmp
= arg_info(op
->args
[2])->val
& 31;
920 mask
= (uint32_t)arg_info(op
->args
[1])->mask
>> tmp
;
923 case INDEX_op_shr_i64
:
924 if (arg_is_const(op
->args
[2])) {
925 tmp
= arg_info(op
->args
[2])->val
& 63;
926 mask
= (uint64_t)arg_info(op
->args
[1])->mask
>> tmp
;
930 case INDEX_op_extrl_i64_i32
:
931 mask
= (uint32_t)arg_info(op
->args
[1])->mask
;
933 case INDEX_op_extrh_i64_i32
:
934 mask
= (uint64_t)arg_info(op
->args
[1])->mask
>> 32;
938 if (arg_is_const(op
->args
[2])) {
939 tmp
= arg_info(op
->args
[2])->val
& (TCG_TARGET_REG_BITS
- 1);
940 mask
= arg_info(op
->args
[1])->mask
<< tmp
;
945 /* Set to 1 all bits to the left of the rightmost. */
946 mask
= -(arg_info(op
->args
[1])->mask
947 & -arg_info(op
->args
[1])->mask
);
950 CASE_OP_32_64(deposit
):
951 mask
= deposit64(arg_info(op
->args
[1])->mask
,
952 op
->args
[3], op
->args
[4],
953 arg_info(op
->args
[2])->mask
);
956 CASE_OP_32_64(extract
):
957 mask
= extract64(arg_info(op
->args
[1])->mask
,
958 op
->args
[2], op
->args
[3]);
959 if (op
->args
[2] == 0) {
960 affected
= arg_info(op
->args
[1])->mask
& ~mask
;
963 CASE_OP_32_64(sextract
):
964 mask
= sextract64(arg_info(op
->args
[1])->mask
,
965 op
->args
[2], op
->args
[3]);
966 if (op
->args
[2] == 0 && (tcg_target_long
)mask
>= 0) {
967 affected
= arg_info(op
->args
[1])->mask
& ~mask
;
973 mask
= arg_info(op
->args
[1])->mask
| arg_info(op
->args
[2])->mask
;
976 case INDEX_op_clz_i32
:
977 case INDEX_op_ctz_i32
:
978 mask
= arg_info(op
->args
[2])->mask
| 31;
981 case INDEX_op_clz_i64
:
982 case INDEX_op_ctz_i64
:
983 mask
= arg_info(op
->args
[2])->mask
| 63;
986 case INDEX_op_ctpop_i32
:
989 case INDEX_op_ctpop_i64
:
993 CASE_OP_32_64(setcond
):
994 case INDEX_op_setcond2_i32
:
998 CASE_OP_32_64(movcond
):
999 mask
= arg_info(op
->args
[3])->mask
| arg_info(op
->args
[4])->mask
;
1002 CASE_OP_32_64(ld8u
):
1005 CASE_OP_32_64(ld16u
):
1008 case INDEX_op_ld32u_i64
:
1012 CASE_OP_32_64(qemu_ld
):
1014 TCGMemOpIdx oi
= op
->args
[nb_oargs
+ nb_iargs
];
1015 TCGMemOp mop
= get_memop(oi
);
1016 if (!(mop
& MO_SIGN
)) {
1017 mask
= (2ULL << ((8 << (mop
& MO_SIZE
)) - 1)) - 1;
1026 /* 32-bit ops generate 32-bit results. For the result is zero test
1027 below, we can ignore high bits, but for further optimizations we
1028 need to record that the high bits contain garbage. */
1030 if (!(def
->flags
& TCG_OPF_64BIT
)) {
1031 mask
|= ~(tcg_target_ulong
)0xffffffffu
;
1032 partmask
&= 0xffffffffu
;
1033 affected
&= 0xffffffffu
;
1036 if (partmask
== 0) {
1037 tcg_debug_assert(nb_oargs
== 1);
1038 tcg_opt_gen_movi(s
, op
, op
->args
[0], 0);
1041 if (affected
== 0) {
1042 tcg_debug_assert(nb_oargs
== 1);
1043 tcg_opt_gen_mov(s
, op
, op
->args
[0], op
->args
[1]);
1047 /* Simplify expression for "op r, a, 0 => movi r, 0" cases */
1051 CASE_OP_32_64(muluh
):
1052 CASE_OP_32_64(mulsh
):
1053 if (arg_is_const(op
->args
[2])
1054 && arg_info(op
->args
[2])->val
== 0) {
1055 tcg_opt_gen_movi(s
, op
, op
->args
[0], 0);
1063 /* Simplify expression for "op r, a, a => mov r, a" cases */
1067 if (args_are_copies(op
->args
[1], op
->args
[2])) {
1068 tcg_opt_gen_mov(s
, op
, op
->args
[0], op
->args
[1]);
1076 /* Simplify expression for "op r, a, a => movi r, 0" cases */
1078 CASE_OP_32_64(andc
):
1081 if (args_are_copies(op
->args
[1], op
->args
[2])) {
1082 tcg_opt_gen_movi(s
, op
, op
->args
[0], 0);
1090 /* Propagate constants through copy operations and do constant
1091 folding. Constants will be substituted to arguments by register
1092 allocator where needed and possible. Also detect copies. */
1095 tcg_opt_gen_mov(s
, op
, op
->args
[0], op
->args
[1]);
1097 CASE_OP_32_64(movi
):
1098 tcg_opt_gen_movi(s
, op
, op
->args
[0], op
->args
[1]);
1103 CASE_OP_32_64(ext8s
):
1104 CASE_OP_32_64(ext8u
):
1105 CASE_OP_32_64(ext16s
):
1106 CASE_OP_32_64(ext16u
):
1107 CASE_OP_32_64(ctpop
):
1108 case INDEX_op_ext32s_i64
:
1109 case INDEX_op_ext32u_i64
:
1110 case INDEX_op_ext_i32_i64
:
1111 case INDEX_op_extu_i32_i64
:
1112 case INDEX_op_extrl_i64_i32
:
1113 case INDEX_op_extrh_i64_i32
:
1114 if (arg_is_const(op
->args
[1])) {
1115 tmp
= do_constant_folding(opc
, arg_info(op
->args
[1])->val
, 0);
1116 tcg_opt_gen_movi(s
, op
, op
->args
[0], tmp
);
1130 CASE_OP_32_64(rotl
):
1131 CASE_OP_32_64(rotr
):
1132 CASE_OP_32_64(andc
):
1135 CASE_OP_32_64(nand
):
1137 CASE_OP_32_64(muluh
):
1138 CASE_OP_32_64(mulsh
):
1140 CASE_OP_32_64(divu
):
1142 CASE_OP_32_64(remu
):
1143 if (arg_is_const(op
->args
[1]) && arg_is_const(op
->args
[2])) {
1144 tmp
= do_constant_folding(opc
, arg_info(op
->args
[1])->val
,
1145 arg_info(op
->args
[2])->val
);
1146 tcg_opt_gen_movi(s
, op
, op
->args
[0], tmp
);
1153 if (arg_is_const(op
->args
[1])) {
1154 TCGArg v
= arg_info(op
->args
[1])->val
;
1156 tmp
= do_constant_folding(opc
, v
, 0);
1157 tcg_opt_gen_movi(s
, op
, op
->args
[0], tmp
);
1159 tcg_opt_gen_mov(s
, op
, op
->args
[0], op
->args
[2]);
1165 CASE_OP_32_64(deposit
):
1166 if (arg_is_const(op
->args
[1]) && arg_is_const(op
->args
[2])) {
1167 tmp
= deposit64(arg_info(op
->args
[1])->val
,
1168 op
->args
[3], op
->args
[4],
1169 arg_info(op
->args
[2])->val
);
1170 tcg_opt_gen_movi(s
, op
, op
->args
[0], tmp
);
1175 CASE_OP_32_64(extract
):
1176 if (arg_is_const(op
->args
[1])) {
1177 tmp
= extract64(arg_info(op
->args
[1])->val
,
1178 op
->args
[2], op
->args
[3]);
1179 tcg_opt_gen_movi(s
, op
, op
->args
[0], tmp
);
1184 CASE_OP_32_64(sextract
):
1185 if (arg_is_const(op
->args
[1])) {
1186 tmp
= sextract64(arg_info(op
->args
[1])->val
,
1187 op
->args
[2], op
->args
[3]);
1188 tcg_opt_gen_movi(s
, op
, op
->args
[0], tmp
);
1193 CASE_OP_32_64(setcond
):
1194 tmp
= do_constant_folding_cond(opc
, op
->args
[1],
1195 op
->args
[2], op
->args
[3]);
1197 tcg_opt_gen_movi(s
, op
, op
->args
[0], tmp
);
1202 CASE_OP_32_64(brcond
):
1203 tmp
= do_constant_folding_cond(opc
, op
->args
[0],
1204 op
->args
[1], op
->args
[2]);
1207 bitmap_zero(temps_used
.l
, nb_temps
);
1208 op
->opc
= INDEX_op_br
;
1209 op
->args
[0] = op
->args
[3];
1211 tcg_op_remove(s
, op
);
1217 CASE_OP_32_64(movcond
):
1218 tmp
= do_constant_folding_cond(opc
, op
->args
[1],
1219 op
->args
[2], op
->args
[5]);
1221 tcg_opt_gen_mov(s
, op
, op
->args
[0], op
->args
[4-tmp
]);
1224 if (arg_is_const(op
->args
[3]) && arg_is_const(op
->args
[4])) {
1225 tcg_target_ulong tv
= arg_info(op
->args
[3])->val
;
1226 tcg_target_ulong fv
= arg_info(op
->args
[4])->val
;
1227 TCGCond cond
= op
->args
[5];
1228 if (fv
== 1 && tv
== 0) {
1229 cond
= tcg_invert_cond(cond
);
1230 } else if (!(tv
== 1 && fv
== 0)) {
1234 op
->opc
= opc
= (opc
== INDEX_op_movcond_i32
1235 ? INDEX_op_setcond_i32
1236 : INDEX_op_setcond_i64
);
1241 case INDEX_op_add2_i32
:
1242 case INDEX_op_sub2_i32
:
1243 if (arg_is_const(op
->args
[2]) && arg_is_const(op
->args
[3])
1244 && arg_is_const(op
->args
[4]) && arg_is_const(op
->args
[5])) {
1245 uint32_t al
= arg_info(op
->args
[2])->val
;
1246 uint32_t ah
= arg_info(op
->args
[3])->val
;
1247 uint32_t bl
= arg_info(op
->args
[4])->val
;
1248 uint32_t bh
= arg_info(op
->args
[5])->val
;
1249 uint64_t a
= ((uint64_t)ah
<< 32) | al
;
1250 uint64_t b
= ((uint64_t)bh
<< 32) | bl
;
1252 TCGOp
*op2
= tcg_op_insert_before(s
, op
, INDEX_op_movi_i32
, 2);
1254 if (opc
== INDEX_op_add2_i32
) {
1262 tcg_opt_gen_movi(s
, op
, rl
, (int32_t)a
);
1263 tcg_opt_gen_movi(s
, op2
, rh
, (int32_t)(a
>> 32));
1265 /* We've done all we need to do with the movi. Skip it. */
1266 oi_next
= op2
->next
;
1271 case INDEX_op_mulu2_i32
:
1272 if (arg_is_const(op
->args
[2]) && arg_is_const(op
->args
[3])) {
1273 uint32_t a
= arg_info(op
->args
[2])->val
;
1274 uint32_t b
= arg_info(op
->args
[3])->val
;
1275 uint64_t r
= (uint64_t)a
* b
;
1277 TCGOp
*op2
= tcg_op_insert_before(s
, op
, INDEX_op_movi_i32
, 2);
1281 tcg_opt_gen_movi(s
, op
, rl
, (int32_t)r
);
1282 tcg_opt_gen_movi(s
, op2
, rh
, (int32_t)(r
>> 32));
1284 /* We've done all we need to do with the movi. Skip it. */
1285 oi_next
= op2
->next
;
1290 case INDEX_op_brcond2_i32
:
1291 tmp
= do_constant_folding_cond2(&op
->args
[0], &op
->args
[2],
1296 bitmap_zero(temps_used
.l
, nb_temps
);
1297 op
->opc
= INDEX_op_br
;
1298 op
->args
[0] = op
->args
[5];
1301 tcg_op_remove(s
, op
);
1303 } else if ((op
->args
[4] == TCG_COND_LT
1304 || op
->args
[4] == TCG_COND_GE
)
1305 && arg_is_const(op
->args
[2])
1306 && arg_info(op
->args
[2])->val
== 0
1307 && arg_is_const(op
->args
[3])
1308 && arg_info(op
->args
[3])->val
== 0) {
1309 /* Simplify LT/GE comparisons vs zero to a single compare
1310 vs the high word of the input. */
1312 bitmap_zero(temps_used
.l
, nb_temps
);
1313 op
->opc
= INDEX_op_brcond_i32
;
1314 op
->args
[0] = op
->args
[1];
1315 op
->args
[1] = op
->args
[3];
1316 op
->args
[2] = op
->args
[4];
1317 op
->args
[3] = op
->args
[5];
1318 } else if (op
->args
[4] == TCG_COND_EQ
) {
1319 /* Simplify EQ comparisons where one of the pairs
1320 can be simplified. */
1321 tmp
= do_constant_folding_cond(INDEX_op_brcond_i32
,
1322 op
->args
[0], op
->args
[2],
1325 goto do_brcond_false
;
1326 } else if (tmp
== 1) {
1327 goto do_brcond_high
;
1329 tmp
= do_constant_folding_cond(INDEX_op_brcond_i32
,
1330 op
->args
[1], op
->args
[3],
1333 goto do_brcond_false
;
1334 } else if (tmp
!= 1) {
1338 bitmap_zero(temps_used
.l
, nb_temps
);
1339 op
->opc
= INDEX_op_brcond_i32
;
1340 op
->args
[1] = op
->args
[2];
1341 op
->args
[2] = op
->args
[4];
1342 op
->args
[3] = op
->args
[5];
1343 } else if (op
->args
[4] == TCG_COND_NE
) {
1344 /* Simplify NE comparisons where one of the pairs
1345 can be simplified. */
1346 tmp
= do_constant_folding_cond(INDEX_op_brcond_i32
,
1347 op
->args
[0], op
->args
[2],
1350 goto do_brcond_high
;
1351 } else if (tmp
== 1) {
1352 goto do_brcond_true
;
1354 tmp
= do_constant_folding_cond(INDEX_op_brcond_i32
,
1355 op
->args
[1], op
->args
[3],
1359 } else if (tmp
== 1) {
1360 goto do_brcond_true
;
1368 case INDEX_op_setcond2_i32
:
1369 tmp
= do_constant_folding_cond2(&op
->args
[1], &op
->args
[3],
1373 tcg_opt_gen_movi(s
, op
, op
->args
[0], tmp
);
1374 } else if ((op
->args
[5] == TCG_COND_LT
1375 || op
->args
[5] == TCG_COND_GE
)
1376 && arg_is_const(op
->args
[3])
1377 && arg_info(op
->args
[3])->val
== 0
1378 && arg_is_const(op
->args
[4])
1379 && arg_info(op
->args
[4])->val
== 0) {
1380 /* Simplify LT/GE comparisons vs zero to a single compare
1381 vs the high word of the input. */
1383 reset_temp(op
->args
[0]);
1384 arg_info(op
->args
[0])->mask
= 1;
1385 op
->opc
= INDEX_op_setcond_i32
;
1386 op
->args
[1] = op
->args
[2];
1387 op
->args
[2] = op
->args
[4];
1388 op
->args
[3] = op
->args
[5];
1389 } else if (op
->args
[5] == TCG_COND_EQ
) {
1390 /* Simplify EQ comparisons where one of the pairs
1391 can be simplified. */
1392 tmp
= do_constant_folding_cond(INDEX_op_setcond_i32
,
1393 op
->args
[1], op
->args
[3],
1396 goto do_setcond_const
;
1397 } else if (tmp
== 1) {
1398 goto do_setcond_high
;
1400 tmp
= do_constant_folding_cond(INDEX_op_setcond_i32
,
1401 op
->args
[2], op
->args
[4],
1404 goto do_setcond_high
;
1405 } else if (tmp
!= 1) {
1409 reset_temp(op
->args
[0]);
1410 arg_info(op
->args
[0])->mask
= 1;
1411 op
->opc
= INDEX_op_setcond_i32
;
1412 op
->args
[2] = op
->args
[3];
1413 op
->args
[3] = op
->args
[5];
1414 } else if (op
->args
[5] == TCG_COND_NE
) {
1415 /* Simplify NE comparisons where one of the pairs
1416 can be simplified. */
1417 tmp
= do_constant_folding_cond(INDEX_op_setcond_i32
,
1418 op
->args
[1], op
->args
[3],
1421 goto do_setcond_high
;
1422 } else if (tmp
== 1) {
1423 goto do_setcond_const
;
1425 tmp
= do_constant_folding_cond(INDEX_op_setcond_i32
,
1426 op
->args
[2], op
->args
[4],
1429 goto do_setcond_low
;
1430 } else if (tmp
== 1) {
1431 goto do_setcond_const
;
1440 if (!(op
->args
[nb_oargs
+ nb_iargs
+ 1]
1441 & (TCG_CALL_NO_READ_GLOBALS
| TCG_CALL_NO_WRITE_GLOBALS
))) {
1442 for (i
= 0; i
< nb_globals
; i
++) {
1443 if (test_bit(i
, temps_used
.l
)) {
1444 reset_ts(&s
->temps
[i
]);
1448 goto do_reset_output
;
1452 /* Default case: we know nothing about operation (or were unable
1453 to compute the operation result) so no propagation is done.
1454 We trash everything if the operation is the end of a basic
1455 block, otherwise we only trash the output args. "mask" is
1456 the non-zero bits mask for the first output arg. */
1457 if (def
->flags
& TCG_OPF_BB_END
) {
1458 bitmap_zero(temps_used
.l
, nb_temps
);
1461 for (i
= 0; i
< nb_oargs
; i
++) {
1462 reset_temp(op
->args
[i
]);
1463 /* Save the corresponding known-zero bits mask for the
1464 first output argument (only one supported so far). */
1466 arg_info(op
->args
[i
])->mask
= mask
;
1473 /* Eliminate duplicate and redundant fence instructions. */
1477 /* Merge two barriers of the same type into one,
1478 * or a weaker barrier into a stronger one,
1479 * or two weaker barriers into a stronger one.
1480 * mb X; mb Y => mb X|Y
1481 * mb; strl => mb; st
1482 * ldaq; mb => ld; mb
1483 * ldaq; strl => ld; mb; st
1484 * Other combinations are also merged into a strong
1485 * barrier. This is stricter than specified but for
1486 * the purposes of TCG is better than not optimizing.
1488 prev_mb
->args
[0] |= op
->args
[0];
1489 tcg_op_remove(s
, op
);
1493 /* Opcodes that end the block stop the optimization. */
1494 if ((def
->flags
& TCG_OPF_BB_END
) == 0) {
1498 case INDEX_op_qemu_ld_i32
:
1499 case INDEX_op_qemu_ld_i64
:
1500 case INDEX_op_qemu_st_i32
:
1501 case INDEX_op_qemu_st_i64
:
1503 /* Opcodes that touch guest memory stop the optimization. */
1507 } else if (opc
== INDEX_op_mb
) {