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
31 #include "qemu-common.h"
34 #define CASE_OP_32_64(x) \
35 glue(glue(case INDEX_op_, x), _i32): \
36 glue(glue(case INDEX_op_, x), _i64)
44 struct tcg_temp_info
{
49 tcg_target_ulong mask
;
52 static struct tcg_temp_info temps
[TCG_MAX_TEMPS
];
54 /* Reset TEMP's state to TCG_TEMP_UNDEF. If TEMP only had one copy, remove
55 the copy flag from the left temp. */
56 static void reset_temp(TCGArg temp
)
58 if (temps
[temp
].state
== TCG_TEMP_COPY
) {
59 if (temps
[temp
].prev_copy
== temps
[temp
].next_copy
) {
60 temps
[temps
[temp
].next_copy
].state
= TCG_TEMP_UNDEF
;
62 temps
[temps
[temp
].next_copy
].prev_copy
= temps
[temp
].prev_copy
;
63 temps
[temps
[temp
].prev_copy
].next_copy
= temps
[temp
].next_copy
;
66 temps
[temp
].state
= TCG_TEMP_UNDEF
;
67 temps
[temp
].mask
= -1;
70 static TCGOp
*insert_op_before(TCGContext
*s
, TCGOp
*old_op
,
71 TCGOpcode opc
, int nargs
)
73 int oi
= s
->gen_next_op_idx
;
74 int pi
= s
->gen_next_parm_idx
;
75 int prev
= old_op
->prev
;
76 int next
= old_op
- s
->gen_op_buf
;
79 tcg_debug_assert(oi
< OPC_BUF_SIZE
);
80 tcg_debug_assert(pi
+ nargs
<= OPPARAM_BUF_SIZE
);
81 s
->gen_next_op_idx
= oi
+ 1;
82 s
->gen_next_parm_idx
= pi
+ nargs
;
84 new_op
= &s
->gen_op_buf
[oi
];
92 s
->gen_op_buf
[prev
].next
= oi
;
94 s
->gen_first_op_idx
= oi
;
101 /* Reset all temporaries, given that there are NB_TEMPS of them. */
102 static void reset_all_temps(int nb_temps
)
105 for (i
= 0; i
< nb_temps
; i
++) {
106 temps
[i
].state
= TCG_TEMP_UNDEF
;
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 TCGArg
find_better_copy(TCGContext
*s
, TCGArg temp
)
149 /* If this is already a global, we can't do better. */
150 if (temp
< s
->nb_globals
) {
154 /* Search for a global first. */
155 for (i
= temps
[temp
].next_copy
; i
!= temp
; i
= temps
[i
].next_copy
) {
156 if (i
< s
->nb_globals
) {
161 /* If it is a temp, search for a temp local. */
162 if (!s
->temps
[temp
].temp_local
) {
163 for (i
= temps
[temp
].next_copy
; i
!= temp
; i
= temps
[i
].next_copy
) {
164 if (s
->temps
[i
].temp_local
) {
170 /* Failure to find a better representation, return the same temp. */
174 static bool temps_are_copies(TCGArg arg1
, TCGArg arg2
)
182 if (temps
[arg1
].state
!= TCG_TEMP_COPY
183 || temps
[arg2
].state
!= TCG_TEMP_COPY
) {
187 for (i
= temps
[arg1
].next_copy
; i
!= arg1
; i
= temps
[i
].next_copy
) {
196 static void tcg_opt_gen_mov(TCGContext
*s
, TCGOp
*op
, TCGArg
*args
,
197 TCGOpcode old_op
, TCGArg dst
, TCGArg src
)
199 TCGOpcode new_op
= op_to_mov(old_op
);
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 assert(temps
[src
].state
!= TCG_TEMP_CONST
);
214 if (s
->temps
[src
].type
== s
->temps
[dst
].type
) {
215 if (temps
[src
].state
!= TCG_TEMP_COPY
) {
216 temps
[src
].state
= TCG_TEMP_COPY
;
217 temps
[src
].next_copy
= src
;
218 temps
[src
].prev_copy
= src
;
220 temps
[dst
].state
= TCG_TEMP_COPY
;
221 temps
[dst
].next_copy
= temps
[src
].next_copy
;
222 temps
[dst
].prev_copy
= src
;
223 temps
[temps
[dst
].next_copy
].prev_copy
= dst
;
224 temps
[src
].next_copy
= dst
;
231 static void tcg_opt_gen_movi(TCGContext
*s
, TCGOp
*op
, TCGArg
*args
,
232 TCGOpcode old_op
, TCGArg dst
, TCGArg val
)
234 TCGOpcode new_op
= op_to_movi(old_op
);
235 tcg_target_ulong mask
;
240 temps
[dst
].state
= TCG_TEMP_CONST
;
241 temps
[dst
].val
= val
;
243 if (TCG_TARGET_REG_BITS
> 32 && new_op
== INDEX_op_mov_i32
) {
244 /* High bits of the destination are now garbage. */
245 mask
|= ~0xffffffffull
;
247 temps
[dst
].mask
= mask
;
253 static TCGArg
do_constant_folding_2(TCGOpcode op
, TCGArg x
, TCGArg y
)
276 case INDEX_op_shl_i32
:
277 return (uint32_t)x
<< (y
& 31);
279 case INDEX_op_shl_i64
:
280 return (uint64_t)x
<< (y
& 63);
282 case INDEX_op_shr_i32
:
283 return (uint32_t)x
>> (y
& 31);
285 case INDEX_op_trunc_shr_i32
:
286 case INDEX_op_shr_i64
:
287 return (uint64_t)x
>> (y
& 63);
289 case INDEX_op_sar_i32
:
290 return (int32_t)x
>> (y
& 31);
292 case INDEX_op_sar_i64
:
293 return (int64_t)x
>> (y
& 63);
295 case INDEX_op_rotr_i32
:
296 return ror32(x
, y
& 31);
298 case INDEX_op_rotr_i64
:
299 return ror64(x
, y
& 63);
301 case INDEX_op_rotl_i32
:
302 return rol32(x
, y
& 31);
304 case INDEX_op_rotl_i64
:
305 return rol64(x
, y
& 63);
328 CASE_OP_32_64(ext8s
):
331 CASE_OP_32_64(ext16s
):
334 CASE_OP_32_64(ext8u
):
337 CASE_OP_32_64(ext16u
):
340 case INDEX_op_ext32s_i64
:
343 case INDEX_op_ext32u_i64
:
346 case INDEX_op_muluh_i32
:
347 return ((uint64_t)(uint32_t)x
* (uint32_t)y
) >> 32;
348 case INDEX_op_mulsh_i32
:
349 return ((int64_t)(int32_t)x
* (int32_t)y
) >> 32;
351 case INDEX_op_muluh_i64
:
352 mulu64(&l64
, &h64
, x
, y
);
354 case INDEX_op_mulsh_i64
:
355 muls64(&l64
, &h64
, x
, y
);
358 case INDEX_op_div_i32
:
359 /* Avoid crashing on divide by zero, otherwise undefined. */
360 return (int32_t)x
/ ((int32_t)y
? : 1);
361 case INDEX_op_divu_i32
:
362 return (uint32_t)x
/ ((uint32_t)y
? : 1);
363 case INDEX_op_div_i64
:
364 return (int64_t)x
/ ((int64_t)y
? : 1);
365 case INDEX_op_divu_i64
:
366 return (uint64_t)x
/ ((uint64_t)y
? : 1);
368 case INDEX_op_rem_i32
:
369 return (int32_t)x
% ((int32_t)y
? : 1);
370 case INDEX_op_remu_i32
:
371 return (uint32_t)x
% ((uint32_t)y
? : 1);
372 case INDEX_op_rem_i64
:
373 return (int64_t)x
% ((int64_t)y
? : 1);
374 case INDEX_op_remu_i64
:
375 return (uint64_t)x
% ((uint64_t)y
? : 1);
379 "Unrecognized operation %d in do_constant_folding.\n", op
);
384 static TCGArg
do_constant_folding(TCGOpcode op
, TCGArg x
, TCGArg y
)
386 TCGArg res
= do_constant_folding_2(op
, x
, y
);
387 if (op_bits(op
) == 32) {
393 static bool do_constant_folding_cond_32(uint32_t x
, uint32_t y
, TCGCond c
)
401 return (int32_t)x
< (int32_t)y
;
403 return (int32_t)x
>= (int32_t)y
;
405 return (int32_t)x
<= (int32_t)y
;
407 return (int32_t)x
> (int32_t)y
;
421 static bool do_constant_folding_cond_64(uint64_t x
, uint64_t y
, TCGCond c
)
429 return (int64_t)x
< (int64_t)y
;
431 return (int64_t)x
>= (int64_t)y
;
433 return (int64_t)x
<= (int64_t)y
;
435 return (int64_t)x
> (int64_t)y
;
449 static bool do_constant_folding_cond_eq(TCGCond c
)
469 /* Return 2 if the condition can't be simplified, and the result
470 of the condition (0 or 1) if it can */
471 static TCGArg
do_constant_folding_cond(TCGOpcode op
, TCGArg x
,
474 if (temps
[x
].state
== TCG_TEMP_CONST
&& temps
[y
].state
== TCG_TEMP_CONST
) {
475 switch (op_bits(op
)) {
477 return do_constant_folding_cond_32(temps
[x
].val
, temps
[y
].val
, c
);
479 return do_constant_folding_cond_64(temps
[x
].val
, temps
[y
].val
, c
);
483 } else if (temps_are_copies(x
, y
)) {
484 return do_constant_folding_cond_eq(c
);
485 } else if (temps
[y
].state
== TCG_TEMP_CONST
&& temps
[y
].val
== 0) {
499 /* Return 2 if the condition can't be simplified, and the result
500 of the condition (0 or 1) if it can */
501 static TCGArg
do_constant_folding_cond2(TCGArg
*p1
, TCGArg
*p2
, TCGCond c
)
503 TCGArg al
= p1
[0], ah
= p1
[1];
504 TCGArg bl
= p2
[0], bh
= p2
[1];
506 if (temps
[bl
].state
== TCG_TEMP_CONST
507 && temps
[bh
].state
== TCG_TEMP_CONST
) {
508 uint64_t b
= ((uint64_t)temps
[bh
].val
<< 32) | (uint32_t)temps
[bl
].val
;
510 if (temps
[al
].state
== TCG_TEMP_CONST
511 && temps
[ah
].state
== TCG_TEMP_CONST
) {
513 a
= ((uint64_t)temps
[ah
].val
<< 32) | (uint32_t)temps
[al
].val
;
514 return do_constant_folding_cond_64(a
, b
, c
);
527 if (temps_are_copies(al
, bl
) && temps_are_copies(ah
, bh
)) {
528 return do_constant_folding_cond_eq(c
);
533 static bool swap_commutative(TCGArg dest
, TCGArg
*p1
, TCGArg
*p2
)
535 TCGArg a1
= *p1
, a2
= *p2
;
537 sum
+= temps
[a1
].state
== TCG_TEMP_CONST
;
538 sum
-= temps
[a2
].state
== TCG_TEMP_CONST
;
540 /* Prefer the constant in second argument, and then the form
541 op a, a, b, which is better handled on non-RISC hosts. */
542 if (sum
> 0 || (sum
== 0 && dest
== a2
)) {
550 static bool swap_commutative2(TCGArg
*p1
, TCGArg
*p2
)
553 sum
+= temps
[p1
[0]].state
== TCG_TEMP_CONST
;
554 sum
+= temps
[p1
[1]].state
== TCG_TEMP_CONST
;
555 sum
-= temps
[p2
[0]].state
== TCG_TEMP_CONST
;
556 sum
-= temps
[p2
[1]].state
== TCG_TEMP_CONST
;
559 t
= p1
[0], p1
[0] = p2
[0], p2
[0] = t
;
560 t
= p1
[1], p1
[1] = p2
[1], p2
[1] = t
;
566 /* Propagate constants and copies, fold constant expressions. */
567 static void tcg_constant_folding(TCGContext
*s
)
569 int oi
, oi_next
, nb_temps
, nb_globals
;
571 /* Array VALS has an element for each temp.
572 If this temp holds a constant then its value is kept in VALS' element.
573 If this temp is a copy of other ones then the other copies are
574 available through the doubly linked circular list. */
576 nb_temps
= s
->nb_temps
;
577 nb_globals
= s
->nb_globals
;
578 reset_all_temps(nb_temps
);
580 for (oi
= s
->gen_first_op_idx
; oi
>= 0; oi
= oi_next
) {
581 tcg_target_ulong mask
, partmask
, affected
;
582 int nb_oargs
, nb_iargs
, i
;
585 TCGOp
* const op
= &s
->gen_op_buf
[oi
];
586 TCGArg
* const args
= &s
->gen_opparam_buf
[op
->args
];
587 TCGOpcode opc
= op
->opc
;
588 const TCGOpDef
*def
= &tcg_op_defs
[opc
];
591 if (opc
== INDEX_op_call
) {
592 nb_oargs
= op
->callo
;
593 nb_iargs
= op
->calli
;
595 nb_oargs
= def
->nb_oargs
;
596 nb_iargs
= def
->nb_iargs
;
599 /* Do copy propagation */
600 for (i
= nb_oargs
; i
< nb_oargs
+ nb_iargs
; i
++) {
601 if (temps
[args
[i
]].state
== TCG_TEMP_COPY
) {
602 args
[i
] = find_better_copy(s
, args
[i
]);
606 /* For commutative operations make constant second argument */
616 CASE_OP_32_64(muluh
):
617 CASE_OP_32_64(mulsh
):
618 swap_commutative(args
[0], &args
[1], &args
[2]);
620 CASE_OP_32_64(brcond
):
621 if (swap_commutative(-1, &args
[0], &args
[1])) {
622 args
[2] = tcg_swap_cond(args
[2]);
625 CASE_OP_32_64(setcond
):
626 if (swap_commutative(args
[0], &args
[1], &args
[2])) {
627 args
[3] = tcg_swap_cond(args
[3]);
630 CASE_OP_32_64(movcond
):
631 if (swap_commutative(-1, &args
[1], &args
[2])) {
632 args
[5] = tcg_swap_cond(args
[5]);
634 /* For movcond, we canonicalize the "false" input reg to match
635 the destination reg so that the tcg backend can implement
636 a "move if true" operation. */
637 if (swap_commutative(args
[0], &args
[4], &args
[3])) {
638 args
[5] = tcg_invert_cond(args
[5]);
642 swap_commutative(args
[0], &args
[2], &args
[4]);
643 swap_commutative(args
[1], &args
[3], &args
[5]);
645 CASE_OP_32_64(mulu2
):
646 CASE_OP_32_64(muls2
):
647 swap_commutative(args
[0], &args
[2], &args
[3]);
649 case INDEX_op_brcond2_i32
:
650 if (swap_commutative2(&args
[0], &args
[2])) {
651 args
[4] = tcg_swap_cond(args
[4]);
654 case INDEX_op_setcond2_i32
:
655 if (swap_commutative2(&args
[1], &args
[3])) {
656 args
[5] = tcg_swap_cond(args
[5]);
663 /* Simplify expressions for "shift/rot r, 0, a => movi r, 0",
664 and "sub r, 0, a => neg r, a" case. */
671 if (temps
[args
[1]].state
== TCG_TEMP_CONST
672 && temps
[args
[1]].val
== 0) {
673 tcg_opt_gen_movi(s
, op
, args
, opc
, args
[0], 0);
682 if (temps
[args
[2]].state
== TCG_TEMP_CONST
) {
683 /* Proceed with possible constant folding. */
686 if (opc
== INDEX_op_sub_i32
) {
687 neg_op
= INDEX_op_neg_i32
;
688 have_neg
= TCG_TARGET_HAS_neg_i32
;
690 neg_op
= INDEX_op_neg_i64
;
691 have_neg
= TCG_TARGET_HAS_neg_i64
;
696 if (temps
[args
[1]].state
== TCG_TEMP_CONST
697 && temps
[args
[1]].val
== 0) {
707 if (temps
[args
[1]].state
!= TCG_TEMP_CONST
708 && temps
[args
[2]].state
== TCG_TEMP_CONST
709 && temps
[args
[2]].val
== -1) {
715 if (temps
[args
[1]].state
!= TCG_TEMP_CONST
716 && temps
[args
[2]].state
== TCG_TEMP_CONST
717 && temps
[args
[2]].val
== 0) {
723 if (temps
[args
[2]].state
!= TCG_TEMP_CONST
724 && temps
[args
[1]].state
== TCG_TEMP_CONST
725 && temps
[args
[1]].val
== -1) {
732 if (temps
[args
[2]].state
!= TCG_TEMP_CONST
733 && temps
[args
[1]].state
== TCG_TEMP_CONST
734 && temps
[args
[1]].val
== 0) {
744 if (def
->flags
& TCG_OPF_64BIT
) {
745 not_op
= INDEX_op_not_i64
;
746 have_not
= TCG_TARGET_HAS_not_i64
;
748 not_op
= INDEX_op_not_i32
;
749 have_not
= TCG_TARGET_HAS_not_i32
;
763 /* Simplify expression for "op r, a, const => mov r, a" cases */
775 if (temps
[args
[1]].state
!= TCG_TEMP_CONST
776 && temps
[args
[2]].state
== TCG_TEMP_CONST
777 && temps
[args
[2]].val
== 0) {
784 if (temps
[args
[1]].state
!= TCG_TEMP_CONST
785 && temps
[args
[2]].state
== TCG_TEMP_CONST
786 && temps
[args
[2]].val
== -1) {
791 if (temps_are_copies(args
[0], args
[1])) {
792 tcg_op_remove(s
, op
);
794 tcg_opt_gen_mov(s
, op
, args
, opc
, args
[0], args
[1]);
801 /* Simplify using known-zero bits. Currently only ops with a single
802 output argument is supported. */
806 CASE_OP_32_64(ext8s
):
807 if ((temps
[args
[1]].mask
& 0x80) != 0) {
810 CASE_OP_32_64(ext8u
):
813 CASE_OP_32_64(ext16s
):
814 if ((temps
[args
[1]].mask
& 0x8000) != 0) {
817 CASE_OP_32_64(ext16u
):
820 case INDEX_op_ext32s_i64
:
821 if ((temps
[args
[1]].mask
& 0x80000000) != 0) {
824 case INDEX_op_ext32u_i64
:
829 mask
= temps
[args
[2]].mask
;
830 if (temps
[args
[2]].state
== TCG_TEMP_CONST
) {
832 affected
= temps
[args
[1]].mask
& ~mask
;
834 mask
= temps
[args
[1]].mask
& mask
;
838 /* Known-zeros does not imply known-ones. Therefore unless
839 args[2] is constant, we can't infer anything from it. */
840 if (temps
[args
[2]].state
== TCG_TEMP_CONST
) {
841 mask
= ~temps
[args
[2]].mask
;
844 /* But we certainly know nothing outside args[1] may be set. */
845 mask
= temps
[args
[1]].mask
;
848 case INDEX_op_sar_i32
:
849 if (temps
[args
[2]].state
== TCG_TEMP_CONST
) {
850 tmp
= temps
[args
[2]].val
& 31;
851 mask
= (int32_t)temps
[args
[1]].mask
>> tmp
;
854 case INDEX_op_sar_i64
:
855 if (temps
[args
[2]].state
== TCG_TEMP_CONST
) {
856 tmp
= temps
[args
[2]].val
& 63;
857 mask
= (int64_t)temps
[args
[1]].mask
>> tmp
;
861 case INDEX_op_shr_i32
:
862 if (temps
[args
[2]].state
== TCG_TEMP_CONST
) {
863 tmp
= temps
[args
[2]].val
& 31;
864 mask
= (uint32_t)temps
[args
[1]].mask
>> tmp
;
867 case INDEX_op_shr_i64
:
868 if (temps
[args
[2]].state
== TCG_TEMP_CONST
) {
869 tmp
= temps
[args
[2]].val
& 63;
870 mask
= (uint64_t)temps
[args
[1]].mask
>> tmp
;
874 case INDEX_op_trunc_shr_i32
:
875 mask
= (uint64_t)temps
[args
[1]].mask
>> args
[2];
879 if (temps
[args
[2]].state
== TCG_TEMP_CONST
) {
880 tmp
= temps
[args
[2]].val
& (TCG_TARGET_REG_BITS
- 1);
881 mask
= temps
[args
[1]].mask
<< tmp
;
886 /* Set to 1 all bits to the left of the rightmost. */
887 mask
= -(temps
[args
[1]].mask
& -temps
[args
[1]].mask
);
890 CASE_OP_32_64(deposit
):
891 mask
= deposit64(temps
[args
[1]].mask
, args
[3], args
[4],
892 temps
[args
[2]].mask
);
897 mask
= temps
[args
[1]].mask
| temps
[args
[2]].mask
;
900 CASE_OP_32_64(setcond
):
901 case INDEX_op_setcond2_i32
:
905 CASE_OP_32_64(movcond
):
906 mask
= temps
[args
[3]].mask
| temps
[args
[4]].mask
;
912 CASE_OP_32_64(ld16u
):
915 case INDEX_op_ld32u_i64
:
919 CASE_OP_32_64(qemu_ld
):
921 TCGMemOp mop
= args
[nb_oargs
+ nb_iargs
];
922 if (!(mop
& MO_SIGN
)) {
923 mask
= (2ULL << ((8 << (mop
& MO_SIZE
)) - 1)) - 1;
932 /* 32-bit ops generate 32-bit results. For the result is zero test
933 below, we can ignore high bits, but for further optimizations we
934 need to record that the high bits contain garbage. */
936 if (!(def
->flags
& TCG_OPF_64BIT
)) {
937 mask
|= ~(tcg_target_ulong
)0xffffffffu
;
938 partmask
&= 0xffffffffu
;
939 affected
&= 0xffffffffu
;
943 assert(nb_oargs
== 1);
944 tcg_opt_gen_movi(s
, op
, args
, opc
, args
[0], 0);
948 assert(nb_oargs
== 1);
949 if (temps_are_copies(args
[0], args
[1])) {
950 tcg_op_remove(s
, op
);
951 } else if (temps
[args
[1]].state
!= TCG_TEMP_CONST
) {
952 tcg_opt_gen_mov(s
, op
, args
, opc
, args
[0], args
[1]);
954 tcg_opt_gen_movi(s
, op
, args
, opc
,
955 args
[0], temps
[args
[1]].val
);
960 /* Simplify expression for "op r, a, 0 => movi r, 0" cases */
964 CASE_OP_32_64(muluh
):
965 CASE_OP_32_64(mulsh
):
966 if ((temps
[args
[2]].state
== TCG_TEMP_CONST
967 && temps
[args
[2]].val
== 0)) {
968 tcg_opt_gen_movi(s
, op
, args
, opc
, args
[0], 0);
976 /* Simplify expression for "op r, a, a => mov r, a" cases */
980 if (temps_are_copies(args
[1], args
[2])) {
981 if (temps_are_copies(args
[0], args
[1])) {
982 tcg_op_remove(s
, op
);
983 } else if (temps
[args
[1]].state
!= TCG_TEMP_CONST
) {
984 tcg_opt_gen_mov(s
, op
, args
, opc
, args
[0], args
[1]);
986 tcg_opt_gen_movi(s
, op
, args
, opc
,
987 args
[0], temps
[args
[1]].val
);
996 /* Simplify expression for "op r, a, a => movi r, 0" cases */
1001 if (temps_are_copies(args
[1], args
[2])) {
1002 tcg_opt_gen_movi(s
, op
, args
, opc
, args
[0], 0);
1010 /* Propagate constants through copy operations and do constant
1011 folding. Constants will be substituted to arguments by register
1012 allocator where needed and possible. Also detect copies. */
1015 if (temps_are_copies(args
[0], args
[1])) {
1016 tcg_op_remove(s
, op
);
1019 if (temps
[args
[1]].state
!= TCG_TEMP_CONST
) {
1020 tcg_opt_gen_mov(s
, op
, args
, opc
, args
[0], args
[1]);
1023 /* Source argument is constant. Rewrite the operation and
1024 let movi case handle it. */
1025 args
[1] = temps
[args
[1]].val
;
1027 CASE_OP_32_64(movi
):
1028 tcg_opt_gen_movi(s
, op
, args
, opc
, args
[0], args
[1]);
1033 CASE_OP_32_64(ext8s
):
1034 CASE_OP_32_64(ext8u
):
1035 CASE_OP_32_64(ext16s
):
1036 CASE_OP_32_64(ext16u
):
1037 case INDEX_op_ext32s_i64
:
1038 case INDEX_op_ext32u_i64
:
1039 if (temps
[args
[1]].state
== TCG_TEMP_CONST
) {
1040 tmp
= do_constant_folding(opc
, temps
[args
[1]].val
, 0);
1041 tcg_opt_gen_movi(s
, op
, args
, opc
, args
[0], tmp
);
1046 case INDEX_op_trunc_shr_i32
:
1047 if (temps
[args
[1]].state
== TCG_TEMP_CONST
) {
1048 tmp
= do_constant_folding(opc
, temps
[args
[1]].val
, args
[2]);
1049 tcg_opt_gen_movi(s
, op
, args
, opc
, args
[0], tmp
);
1063 CASE_OP_32_64(rotl
):
1064 CASE_OP_32_64(rotr
):
1065 CASE_OP_32_64(andc
):
1068 CASE_OP_32_64(nand
):
1070 CASE_OP_32_64(muluh
):
1071 CASE_OP_32_64(mulsh
):
1073 CASE_OP_32_64(divu
):
1075 CASE_OP_32_64(remu
):
1076 if (temps
[args
[1]].state
== TCG_TEMP_CONST
1077 && temps
[args
[2]].state
== TCG_TEMP_CONST
) {
1078 tmp
= do_constant_folding(opc
, temps
[args
[1]].val
,
1079 temps
[args
[2]].val
);
1080 tcg_opt_gen_movi(s
, op
, args
, opc
, args
[0], tmp
);
1085 CASE_OP_32_64(deposit
):
1086 if (temps
[args
[1]].state
== TCG_TEMP_CONST
1087 && temps
[args
[2]].state
== TCG_TEMP_CONST
) {
1088 tmp
= deposit64(temps
[args
[1]].val
, args
[3], args
[4],
1089 temps
[args
[2]].val
);
1090 tcg_opt_gen_movi(s
, op
, args
, opc
, args
[0], tmp
);
1095 CASE_OP_32_64(setcond
):
1096 tmp
= do_constant_folding_cond(opc
, args
[1], args
[2], args
[3]);
1098 tcg_opt_gen_movi(s
, op
, args
, opc
, args
[0], tmp
);
1103 CASE_OP_32_64(brcond
):
1104 tmp
= do_constant_folding_cond(opc
, args
[0], args
[1], args
[2]);
1107 reset_all_temps(nb_temps
);
1108 op
->opc
= INDEX_op_br
;
1111 tcg_op_remove(s
, op
);
1117 CASE_OP_32_64(movcond
):
1118 tmp
= do_constant_folding_cond(opc
, args
[1], args
[2], args
[5]);
1120 if (temps_are_copies(args
[0], args
[4-tmp
])) {
1121 tcg_op_remove(s
, op
);
1122 } else if (temps
[args
[4-tmp
]].state
== TCG_TEMP_CONST
) {
1123 tcg_opt_gen_movi(s
, op
, args
, opc
,
1124 args
[0], temps
[args
[4-tmp
]].val
);
1126 tcg_opt_gen_mov(s
, op
, args
, opc
, args
[0], args
[4-tmp
]);
1132 case INDEX_op_add2_i32
:
1133 case INDEX_op_sub2_i32
:
1134 if (temps
[args
[2]].state
== TCG_TEMP_CONST
1135 && temps
[args
[3]].state
== TCG_TEMP_CONST
1136 && temps
[args
[4]].state
== TCG_TEMP_CONST
1137 && temps
[args
[5]].state
== TCG_TEMP_CONST
) {
1138 uint32_t al
= temps
[args
[2]].val
;
1139 uint32_t ah
= temps
[args
[3]].val
;
1140 uint32_t bl
= temps
[args
[4]].val
;
1141 uint32_t bh
= temps
[args
[5]].val
;
1142 uint64_t a
= ((uint64_t)ah
<< 32) | al
;
1143 uint64_t b
= ((uint64_t)bh
<< 32) | bl
;
1145 TCGOp
*op2
= insert_op_before(s
, op
, INDEX_op_movi_i32
, 2);
1146 TCGArg
*args2
= &s
->gen_opparam_buf
[op2
->args
];
1148 if (opc
== INDEX_op_add2_i32
) {
1156 tcg_opt_gen_movi(s
, op
, args
, opc
, rl
, (uint32_t)a
);
1157 tcg_opt_gen_movi(s
, op2
, args2
, opc
, rh
, (uint32_t)(a
>> 32));
1159 /* We've done all we need to do with the movi. Skip it. */
1160 oi_next
= op2
->next
;
1165 case INDEX_op_mulu2_i32
:
1166 if (temps
[args
[2]].state
== TCG_TEMP_CONST
1167 && temps
[args
[3]].state
== TCG_TEMP_CONST
) {
1168 uint32_t a
= temps
[args
[2]].val
;
1169 uint32_t b
= temps
[args
[3]].val
;
1170 uint64_t r
= (uint64_t)a
* b
;
1172 TCGOp
*op2
= insert_op_before(s
, op
, INDEX_op_movi_i32
, 2);
1173 TCGArg
*args2
= &s
->gen_opparam_buf
[op2
->args
];
1177 tcg_opt_gen_movi(s
, op
, args
, opc
, rl
, (uint32_t)r
);
1178 tcg_opt_gen_movi(s
, op2
, args2
, opc
, rh
, (uint32_t)(r
>> 32));
1180 /* We've done all we need to do with the movi. Skip it. */
1181 oi_next
= op2
->next
;
1186 case INDEX_op_brcond2_i32
:
1187 tmp
= do_constant_folding_cond2(&args
[0], &args
[2], args
[4]);
1191 reset_all_temps(nb_temps
);
1192 op
->opc
= INDEX_op_br
;
1196 tcg_op_remove(s
, op
);
1198 } else if ((args
[4] == TCG_COND_LT
|| args
[4] == TCG_COND_GE
)
1199 && temps
[args
[2]].state
== TCG_TEMP_CONST
1200 && temps
[args
[3]].state
== TCG_TEMP_CONST
1201 && temps
[args
[2]].val
== 0
1202 && temps
[args
[3]].val
== 0) {
1203 /* Simplify LT/GE comparisons vs zero to a single compare
1204 vs the high word of the input. */
1206 reset_all_temps(nb_temps
);
1207 op
->opc
= INDEX_op_brcond_i32
;
1212 } else if (args
[4] == TCG_COND_EQ
) {
1213 /* Simplify EQ comparisons where one of the pairs
1214 can be simplified. */
1215 tmp
= do_constant_folding_cond(INDEX_op_brcond_i32
,
1216 args
[0], args
[2], TCG_COND_EQ
);
1218 goto do_brcond_false
;
1219 } else if (tmp
== 1) {
1220 goto do_brcond_high
;
1222 tmp
= do_constant_folding_cond(INDEX_op_brcond_i32
,
1223 args
[1], args
[3], TCG_COND_EQ
);
1225 goto do_brcond_false
;
1226 } else if (tmp
!= 1) {
1230 reset_all_temps(nb_temps
);
1231 op
->opc
= INDEX_op_brcond_i32
;
1235 } else if (args
[4] == TCG_COND_NE
) {
1236 /* Simplify NE comparisons where one of the pairs
1237 can be simplified. */
1238 tmp
= do_constant_folding_cond(INDEX_op_brcond_i32
,
1239 args
[0], args
[2], TCG_COND_NE
);
1241 goto do_brcond_high
;
1242 } else if (tmp
== 1) {
1243 goto do_brcond_true
;
1245 tmp
= do_constant_folding_cond(INDEX_op_brcond_i32
,
1246 args
[1], args
[3], TCG_COND_NE
);
1249 } else if (tmp
== 1) {
1250 goto do_brcond_true
;
1258 case INDEX_op_setcond2_i32
:
1259 tmp
= do_constant_folding_cond2(&args
[1], &args
[3], args
[5]);
1262 tcg_opt_gen_movi(s
, op
, args
, opc
, args
[0], tmp
);
1263 } else if ((args
[5] == TCG_COND_LT
|| args
[5] == TCG_COND_GE
)
1264 && temps
[args
[3]].state
== TCG_TEMP_CONST
1265 && temps
[args
[4]].state
== TCG_TEMP_CONST
1266 && temps
[args
[3]].val
== 0
1267 && temps
[args
[4]].val
== 0) {
1268 /* Simplify LT/GE comparisons vs zero to a single compare
1269 vs the high word of the input. */
1271 reset_temp(args
[0]);
1272 temps
[args
[0]].mask
= 1;
1273 op
->opc
= INDEX_op_setcond_i32
;
1277 } else if (args
[5] == TCG_COND_EQ
) {
1278 /* Simplify EQ comparisons where one of the pairs
1279 can be simplified. */
1280 tmp
= do_constant_folding_cond(INDEX_op_setcond_i32
,
1281 args
[1], args
[3], TCG_COND_EQ
);
1283 goto do_setcond_const
;
1284 } else if (tmp
== 1) {
1285 goto do_setcond_high
;
1287 tmp
= do_constant_folding_cond(INDEX_op_setcond_i32
,
1288 args
[2], args
[4], TCG_COND_EQ
);
1290 goto do_setcond_high
;
1291 } else if (tmp
!= 1) {
1295 reset_temp(args
[0]);
1296 temps
[args
[0]].mask
= 1;
1297 op
->opc
= INDEX_op_setcond_i32
;
1300 } else if (args
[5] == TCG_COND_NE
) {
1301 /* Simplify NE comparisons where one of the pairs
1302 can be simplified. */
1303 tmp
= do_constant_folding_cond(INDEX_op_setcond_i32
,
1304 args
[1], args
[3], TCG_COND_NE
);
1306 goto do_setcond_high
;
1307 } else if (tmp
== 1) {
1308 goto do_setcond_const
;
1310 tmp
= do_constant_folding_cond(INDEX_op_setcond_i32
,
1311 args
[2], args
[4], TCG_COND_NE
);
1313 goto do_setcond_low
;
1314 } else if (tmp
== 1) {
1315 goto do_setcond_const
;
1324 if (!(args
[nb_oargs
+ nb_iargs
+ 1]
1325 & (TCG_CALL_NO_READ_GLOBALS
| TCG_CALL_NO_WRITE_GLOBALS
))) {
1326 for (i
= 0; i
< nb_globals
; i
++) {
1330 goto do_reset_output
;
1334 /* Default case: we know nothing about operation (or were unable
1335 to compute the operation result) so no propagation is done.
1336 We trash everything if the operation is the end of a basic
1337 block, otherwise we only trash the output args. "mask" is
1338 the non-zero bits mask for the first output arg. */
1339 if (def
->flags
& TCG_OPF_BB_END
) {
1340 reset_all_temps(nb_temps
);
1343 for (i
= 0; i
< nb_oargs
; i
++) {
1344 reset_temp(args
[i
]);
1345 /* Save the corresponding known-zero bits mask for the
1346 first output argument (only one supported so far). */
1348 temps
[args
[i
]].mask
= mask
;
1357 void tcg_optimize(TCGContext
*s
)
1359 tcg_constant_folding(s
);