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_movi(TCGContext
*s
, TCGOp
*op
, TCGArg
*args
,
197 TCGArg dst
, TCGArg val
)
199 TCGOpcode new_op
= op_to_movi(op
->opc
);
200 tcg_target_ulong mask
;
205 temps
[dst
].state
= TCG_TEMP_CONST
;
206 temps
[dst
].val
= val
;
208 if (TCG_TARGET_REG_BITS
> 32 && new_op
== INDEX_op_movi_i32
) {
209 /* High bits of the destination are now garbage. */
210 mask
|= ~0xffffffffull
;
212 temps
[dst
].mask
= mask
;
218 static void tcg_opt_gen_mov(TCGContext
*s
, TCGOp
*op
, TCGArg
*args
,
219 TCGArg dst
, TCGArg src
)
221 if (temps_are_copies(dst
, src
)) {
222 tcg_op_remove(s
, op
);
226 if (temps
[src
].state
== TCG_TEMP_CONST
) {
227 tcg_opt_gen_movi(s
, op
, args
, dst
, temps
[src
].val
);
231 TCGOpcode new_op
= op_to_mov(op
->opc
);
232 tcg_target_ulong mask
;
237 mask
= temps
[src
].mask
;
238 if (TCG_TARGET_REG_BITS
> 32 && new_op
== INDEX_op_mov_i32
) {
239 /* High bits of the destination are now garbage. */
240 mask
|= ~0xffffffffull
;
242 temps
[dst
].mask
= mask
;
244 assert(temps
[src
].state
!= TCG_TEMP_CONST
);
246 if (s
->temps
[src
].type
== s
->temps
[dst
].type
) {
247 if (temps
[src
].state
!= TCG_TEMP_COPY
) {
248 temps
[src
].state
= TCG_TEMP_COPY
;
249 temps
[src
].next_copy
= src
;
250 temps
[src
].prev_copy
= src
;
252 temps
[dst
].state
= TCG_TEMP_COPY
;
253 temps
[dst
].next_copy
= temps
[src
].next_copy
;
254 temps
[dst
].prev_copy
= src
;
255 temps
[temps
[dst
].next_copy
].prev_copy
= dst
;
256 temps
[src
].next_copy
= dst
;
263 static TCGArg
do_constant_folding_2(TCGOpcode op
, TCGArg x
, TCGArg y
)
286 case INDEX_op_shl_i32
:
287 return (uint32_t)x
<< (y
& 31);
289 case INDEX_op_shl_i64
:
290 return (uint64_t)x
<< (y
& 63);
292 case INDEX_op_shr_i32
:
293 return (uint32_t)x
>> (y
& 31);
295 case INDEX_op_trunc_shr_i32
:
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_OP_32_64(ext8s
):
341 CASE_OP_32_64(ext16s
):
344 CASE_OP_32_64(ext8u
):
347 CASE_OP_32_64(ext16u
):
350 case INDEX_op_ext32s_i64
:
353 case INDEX_op_ext32u_i64
:
356 case INDEX_op_muluh_i32
:
357 return ((uint64_t)(uint32_t)x
* (uint32_t)y
) >> 32;
358 case INDEX_op_mulsh_i32
:
359 return ((int64_t)(int32_t)x
* (int32_t)y
) >> 32;
361 case INDEX_op_muluh_i64
:
362 mulu64(&l64
, &h64
, x
, y
);
364 case INDEX_op_mulsh_i64
:
365 muls64(&l64
, &h64
, x
, y
);
368 case INDEX_op_div_i32
:
369 /* Avoid crashing on divide by zero, otherwise undefined. */
370 return (int32_t)x
/ ((int32_t)y
? : 1);
371 case INDEX_op_divu_i32
:
372 return (uint32_t)x
/ ((uint32_t)y
? : 1);
373 case INDEX_op_div_i64
:
374 return (int64_t)x
/ ((int64_t)y
? : 1);
375 case INDEX_op_divu_i64
:
376 return (uint64_t)x
/ ((uint64_t)y
? : 1);
378 case INDEX_op_rem_i32
:
379 return (int32_t)x
% ((int32_t)y
? : 1);
380 case INDEX_op_remu_i32
:
381 return (uint32_t)x
% ((uint32_t)y
? : 1);
382 case INDEX_op_rem_i64
:
383 return (int64_t)x
% ((int64_t)y
? : 1);
384 case INDEX_op_remu_i64
:
385 return (uint64_t)x
% ((uint64_t)y
? : 1);
389 "Unrecognized operation %d in do_constant_folding.\n", op
);
394 static TCGArg
do_constant_folding(TCGOpcode op
, TCGArg x
, TCGArg y
)
396 TCGArg res
= do_constant_folding_2(op
, x
, y
);
397 if (op_bits(op
) == 32) {
403 static bool do_constant_folding_cond_32(uint32_t x
, uint32_t y
, TCGCond c
)
411 return (int32_t)x
< (int32_t)y
;
413 return (int32_t)x
>= (int32_t)y
;
415 return (int32_t)x
<= (int32_t)y
;
417 return (int32_t)x
> (int32_t)y
;
431 static bool do_constant_folding_cond_64(uint64_t x
, uint64_t y
, TCGCond c
)
439 return (int64_t)x
< (int64_t)y
;
441 return (int64_t)x
>= (int64_t)y
;
443 return (int64_t)x
<= (int64_t)y
;
445 return (int64_t)x
> (int64_t)y
;
459 static bool do_constant_folding_cond_eq(TCGCond c
)
479 /* Return 2 if the condition can't be simplified, and the result
480 of the condition (0 or 1) if it can */
481 static TCGArg
do_constant_folding_cond(TCGOpcode op
, TCGArg x
,
484 if (temps
[x
].state
== TCG_TEMP_CONST
&& temps
[y
].state
== TCG_TEMP_CONST
) {
485 switch (op_bits(op
)) {
487 return do_constant_folding_cond_32(temps
[x
].val
, temps
[y
].val
, c
);
489 return do_constant_folding_cond_64(temps
[x
].val
, temps
[y
].val
, c
);
493 } else if (temps_are_copies(x
, y
)) {
494 return do_constant_folding_cond_eq(c
);
495 } else if (temps
[y
].state
== TCG_TEMP_CONST
&& temps
[y
].val
== 0) {
509 /* Return 2 if the condition can't be simplified, and the result
510 of the condition (0 or 1) if it can */
511 static TCGArg
do_constant_folding_cond2(TCGArg
*p1
, TCGArg
*p2
, TCGCond c
)
513 TCGArg al
= p1
[0], ah
= p1
[1];
514 TCGArg bl
= p2
[0], bh
= p2
[1];
516 if (temps
[bl
].state
== TCG_TEMP_CONST
517 && temps
[bh
].state
== TCG_TEMP_CONST
) {
518 uint64_t b
= ((uint64_t)temps
[bh
].val
<< 32) | (uint32_t)temps
[bl
].val
;
520 if (temps
[al
].state
== TCG_TEMP_CONST
521 && temps
[ah
].state
== TCG_TEMP_CONST
) {
523 a
= ((uint64_t)temps
[ah
].val
<< 32) | (uint32_t)temps
[al
].val
;
524 return do_constant_folding_cond_64(a
, b
, c
);
537 if (temps_are_copies(al
, bl
) && temps_are_copies(ah
, bh
)) {
538 return do_constant_folding_cond_eq(c
);
543 static bool swap_commutative(TCGArg dest
, TCGArg
*p1
, TCGArg
*p2
)
545 TCGArg a1
= *p1
, a2
= *p2
;
547 sum
+= temps
[a1
].state
== TCG_TEMP_CONST
;
548 sum
-= temps
[a2
].state
== TCG_TEMP_CONST
;
550 /* Prefer the constant in second argument, and then the form
551 op a, a, b, which is better handled on non-RISC hosts. */
552 if (sum
> 0 || (sum
== 0 && dest
== a2
)) {
560 static bool swap_commutative2(TCGArg
*p1
, TCGArg
*p2
)
563 sum
+= temps
[p1
[0]].state
== TCG_TEMP_CONST
;
564 sum
+= temps
[p1
[1]].state
== TCG_TEMP_CONST
;
565 sum
-= temps
[p2
[0]].state
== TCG_TEMP_CONST
;
566 sum
-= temps
[p2
[1]].state
== TCG_TEMP_CONST
;
569 t
= p1
[0], p1
[0] = p2
[0], p2
[0] = t
;
570 t
= p1
[1], p1
[1] = p2
[1], p2
[1] = t
;
576 /* Propagate constants and copies, fold constant expressions. */
577 void tcg_optimize(TCGContext
*s
)
579 int oi
, oi_next
, nb_temps
, nb_globals
;
581 /* Array VALS has an element for each temp.
582 If this temp holds a constant then its value is kept in VALS' element.
583 If this temp is a copy of other ones then the other copies are
584 available through the doubly linked circular list. */
586 nb_temps
= s
->nb_temps
;
587 nb_globals
= s
->nb_globals
;
588 reset_all_temps(nb_temps
);
590 for (oi
= s
->gen_first_op_idx
; oi
>= 0; oi
= oi_next
) {
591 tcg_target_ulong mask
, partmask
, affected
;
592 int nb_oargs
, nb_iargs
, i
;
595 TCGOp
* const op
= &s
->gen_op_buf
[oi
];
596 TCGArg
* const args
= &s
->gen_opparam_buf
[op
->args
];
597 TCGOpcode opc
= op
->opc
;
598 const TCGOpDef
*def
= &tcg_op_defs
[opc
];
601 if (opc
== INDEX_op_call
) {
602 nb_oargs
= op
->callo
;
603 nb_iargs
= op
->calli
;
605 nb_oargs
= def
->nb_oargs
;
606 nb_iargs
= def
->nb_iargs
;
609 /* Do copy propagation */
610 for (i
= nb_oargs
; i
< nb_oargs
+ nb_iargs
; i
++) {
611 if (temps
[args
[i
]].state
== TCG_TEMP_COPY
) {
612 args
[i
] = find_better_copy(s
, args
[i
]);
616 /* For commutative operations make constant second argument */
626 CASE_OP_32_64(muluh
):
627 CASE_OP_32_64(mulsh
):
628 swap_commutative(args
[0], &args
[1], &args
[2]);
630 CASE_OP_32_64(brcond
):
631 if (swap_commutative(-1, &args
[0], &args
[1])) {
632 args
[2] = tcg_swap_cond(args
[2]);
635 CASE_OP_32_64(setcond
):
636 if (swap_commutative(args
[0], &args
[1], &args
[2])) {
637 args
[3] = tcg_swap_cond(args
[3]);
640 CASE_OP_32_64(movcond
):
641 if (swap_commutative(-1, &args
[1], &args
[2])) {
642 args
[5] = tcg_swap_cond(args
[5]);
644 /* For movcond, we canonicalize the "false" input reg to match
645 the destination reg so that the tcg backend can implement
646 a "move if true" operation. */
647 if (swap_commutative(args
[0], &args
[4], &args
[3])) {
648 args
[5] = tcg_invert_cond(args
[5]);
652 swap_commutative(args
[0], &args
[2], &args
[4]);
653 swap_commutative(args
[1], &args
[3], &args
[5]);
655 CASE_OP_32_64(mulu2
):
656 CASE_OP_32_64(muls2
):
657 swap_commutative(args
[0], &args
[2], &args
[3]);
659 case INDEX_op_brcond2_i32
:
660 if (swap_commutative2(&args
[0], &args
[2])) {
661 args
[4] = tcg_swap_cond(args
[4]);
664 case INDEX_op_setcond2_i32
:
665 if (swap_commutative2(&args
[1], &args
[3])) {
666 args
[5] = tcg_swap_cond(args
[5]);
673 /* Simplify expressions for "shift/rot r, 0, a => movi r, 0",
674 and "sub r, 0, a => neg r, a" case. */
681 if (temps
[args
[1]].state
== TCG_TEMP_CONST
682 && temps
[args
[1]].val
== 0) {
683 tcg_opt_gen_movi(s
, op
, args
, args
[0], 0);
692 if (temps
[args
[2]].state
== TCG_TEMP_CONST
) {
693 /* Proceed with possible constant folding. */
696 if (opc
== INDEX_op_sub_i32
) {
697 neg_op
= INDEX_op_neg_i32
;
698 have_neg
= TCG_TARGET_HAS_neg_i32
;
700 neg_op
= INDEX_op_neg_i64
;
701 have_neg
= TCG_TARGET_HAS_neg_i64
;
706 if (temps
[args
[1]].state
== TCG_TEMP_CONST
707 && temps
[args
[1]].val
== 0) {
717 if (temps
[args
[1]].state
!= TCG_TEMP_CONST
718 && temps
[args
[2]].state
== TCG_TEMP_CONST
719 && temps
[args
[2]].val
== -1) {
725 if (temps
[args
[1]].state
!= TCG_TEMP_CONST
726 && temps
[args
[2]].state
== TCG_TEMP_CONST
727 && temps
[args
[2]].val
== 0) {
733 if (temps
[args
[2]].state
!= TCG_TEMP_CONST
734 && temps
[args
[1]].state
== TCG_TEMP_CONST
735 && temps
[args
[1]].val
== -1) {
742 if (temps
[args
[2]].state
!= TCG_TEMP_CONST
743 && temps
[args
[1]].state
== TCG_TEMP_CONST
744 && temps
[args
[1]].val
== 0) {
754 if (def
->flags
& TCG_OPF_64BIT
) {
755 not_op
= INDEX_op_not_i64
;
756 have_not
= TCG_TARGET_HAS_not_i64
;
758 not_op
= INDEX_op_not_i32
;
759 have_not
= TCG_TARGET_HAS_not_i32
;
773 /* Simplify expression for "op r, a, const => mov r, a" cases */
785 if (temps
[args
[1]].state
!= TCG_TEMP_CONST
786 && temps
[args
[2]].state
== TCG_TEMP_CONST
787 && temps
[args
[2]].val
== 0) {
788 tcg_opt_gen_mov(s
, op
, args
, args
[0], args
[1]);
795 if (temps
[args
[1]].state
!= TCG_TEMP_CONST
796 && temps
[args
[2]].state
== TCG_TEMP_CONST
797 && 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 (temps
[args
[2]].state
== TCG_TEMP_CONST
) {
837 affected
= temps
[args
[1]].mask
& ~mask
;
839 mask
= temps
[args
[1]].mask
& mask
;
843 /* Known-zeros does not imply known-ones. Therefore unless
844 args[2] is constant, we can't infer anything from it. */
845 if (temps
[args
[2]].state
== TCG_TEMP_CONST
) {
846 mask
= ~temps
[args
[2]].mask
;
849 /* But we certainly know nothing outside args[1] may be set. */
850 mask
= temps
[args
[1]].mask
;
853 case INDEX_op_sar_i32
:
854 if (temps
[args
[2]].state
== TCG_TEMP_CONST
) {
855 tmp
= temps
[args
[2]].val
& 31;
856 mask
= (int32_t)temps
[args
[1]].mask
>> tmp
;
859 case INDEX_op_sar_i64
:
860 if (temps
[args
[2]].state
== TCG_TEMP_CONST
) {
861 tmp
= temps
[args
[2]].val
& 63;
862 mask
= (int64_t)temps
[args
[1]].mask
>> tmp
;
866 case INDEX_op_shr_i32
:
867 if (temps
[args
[2]].state
== TCG_TEMP_CONST
) {
868 tmp
= temps
[args
[2]].val
& 31;
869 mask
= (uint32_t)temps
[args
[1]].mask
>> tmp
;
872 case INDEX_op_shr_i64
:
873 if (temps
[args
[2]].state
== TCG_TEMP_CONST
) {
874 tmp
= temps
[args
[2]].val
& 63;
875 mask
= (uint64_t)temps
[args
[1]].mask
>> tmp
;
879 case INDEX_op_trunc_shr_i32
:
880 mask
= (uint64_t)temps
[args
[1]].mask
>> args
[2];
884 if (temps
[args
[2]].state
== TCG_TEMP_CONST
) {
885 tmp
= temps
[args
[2]].val
& (TCG_TARGET_REG_BITS
- 1);
886 mask
= temps
[args
[1]].mask
<< tmp
;
891 /* Set to 1 all bits to the left of the rightmost. */
892 mask
= -(temps
[args
[1]].mask
& -temps
[args
[1]].mask
);
895 CASE_OP_32_64(deposit
):
896 mask
= deposit64(temps
[args
[1]].mask
, args
[3], args
[4],
897 temps
[args
[2]].mask
);
902 mask
= temps
[args
[1]].mask
| temps
[args
[2]].mask
;
905 CASE_OP_32_64(setcond
):
906 case INDEX_op_setcond2_i32
:
910 CASE_OP_32_64(movcond
):
911 mask
= temps
[args
[3]].mask
| temps
[args
[4]].mask
;
917 CASE_OP_32_64(ld16u
):
920 case INDEX_op_ld32u_i64
:
924 CASE_OP_32_64(qemu_ld
):
926 TCGMemOpIdx oi
= args
[nb_oargs
+ nb_iargs
];
927 TCGMemOp mop
= get_memop(oi
);
928 if (!(mop
& MO_SIGN
)) {
929 mask
= (2ULL << ((8 << (mop
& MO_SIZE
)) - 1)) - 1;
938 /* 32-bit ops generate 32-bit results. For the result is zero test
939 below, we can ignore high bits, but for further optimizations we
940 need to record that the high bits contain garbage. */
942 if (!(def
->flags
& TCG_OPF_64BIT
)) {
943 mask
|= ~(tcg_target_ulong
)0xffffffffu
;
944 partmask
&= 0xffffffffu
;
945 affected
&= 0xffffffffu
;
949 assert(nb_oargs
== 1);
950 tcg_opt_gen_movi(s
, op
, args
, args
[0], 0);
954 assert(nb_oargs
== 1);
955 tcg_opt_gen_mov(s
, op
, args
, args
[0], args
[1]);
959 /* Simplify expression for "op r, a, 0 => movi r, 0" cases */
963 CASE_OP_32_64(muluh
):
964 CASE_OP_32_64(mulsh
):
965 if ((temps
[args
[2]].state
== TCG_TEMP_CONST
966 && temps
[args
[2]].val
== 0)) {
967 tcg_opt_gen_movi(s
, op
, args
, args
[0], 0);
975 /* Simplify expression for "op r, a, a => mov r, a" cases */
979 if (temps_are_copies(args
[1], args
[2])) {
980 tcg_opt_gen_mov(s
, op
, args
, args
[0], args
[1]);
988 /* Simplify expression for "op r, a, a => movi r, 0" cases */
993 if (temps_are_copies(args
[1], args
[2])) {
994 tcg_opt_gen_movi(s
, op
, args
, args
[0], 0);
1002 /* Propagate constants through copy operations and do constant
1003 folding. Constants will be substituted to arguments by register
1004 allocator where needed and possible. Also detect copies. */
1007 tcg_opt_gen_mov(s
, op
, args
, args
[0], args
[1]);
1009 CASE_OP_32_64(movi
):
1010 tcg_opt_gen_movi(s
, op
, args
, args
[0], args
[1]);
1015 CASE_OP_32_64(ext8s
):
1016 CASE_OP_32_64(ext8u
):
1017 CASE_OP_32_64(ext16s
):
1018 CASE_OP_32_64(ext16u
):
1019 case INDEX_op_ext32s_i64
:
1020 case INDEX_op_ext32u_i64
:
1021 if (temps
[args
[1]].state
== TCG_TEMP_CONST
) {
1022 tmp
= do_constant_folding(opc
, temps
[args
[1]].val
, 0);
1023 tcg_opt_gen_movi(s
, op
, args
, args
[0], tmp
);
1028 case INDEX_op_trunc_shr_i32
:
1029 if (temps
[args
[1]].state
== TCG_TEMP_CONST
) {
1030 tmp
= do_constant_folding(opc
, temps
[args
[1]].val
, args
[2]);
1031 tcg_opt_gen_movi(s
, op
, args
, args
[0], tmp
);
1045 CASE_OP_32_64(rotl
):
1046 CASE_OP_32_64(rotr
):
1047 CASE_OP_32_64(andc
):
1050 CASE_OP_32_64(nand
):
1052 CASE_OP_32_64(muluh
):
1053 CASE_OP_32_64(mulsh
):
1055 CASE_OP_32_64(divu
):
1057 CASE_OP_32_64(remu
):
1058 if (temps
[args
[1]].state
== TCG_TEMP_CONST
1059 && temps
[args
[2]].state
== TCG_TEMP_CONST
) {
1060 tmp
= do_constant_folding(opc
, temps
[args
[1]].val
,
1061 temps
[args
[2]].val
);
1062 tcg_opt_gen_movi(s
, op
, args
, args
[0], tmp
);
1067 CASE_OP_32_64(deposit
):
1068 if (temps
[args
[1]].state
== TCG_TEMP_CONST
1069 && temps
[args
[2]].state
== TCG_TEMP_CONST
) {
1070 tmp
= deposit64(temps
[args
[1]].val
, args
[3], args
[4],
1071 temps
[args
[2]].val
);
1072 tcg_opt_gen_movi(s
, op
, args
, args
[0], tmp
);
1077 CASE_OP_32_64(setcond
):
1078 tmp
= do_constant_folding_cond(opc
, args
[1], args
[2], args
[3]);
1080 tcg_opt_gen_movi(s
, op
, args
, args
[0], tmp
);
1085 CASE_OP_32_64(brcond
):
1086 tmp
= do_constant_folding_cond(opc
, args
[0], args
[1], args
[2]);
1089 reset_all_temps(nb_temps
);
1090 op
->opc
= INDEX_op_br
;
1093 tcg_op_remove(s
, op
);
1099 CASE_OP_32_64(movcond
):
1100 tmp
= do_constant_folding_cond(opc
, args
[1], args
[2], args
[5]);
1102 tcg_opt_gen_mov(s
, op
, args
, args
[0], args
[4-tmp
]);
1107 case INDEX_op_add2_i32
:
1108 case INDEX_op_sub2_i32
:
1109 if (temps
[args
[2]].state
== TCG_TEMP_CONST
1110 && temps
[args
[3]].state
== TCG_TEMP_CONST
1111 && temps
[args
[4]].state
== TCG_TEMP_CONST
1112 && temps
[args
[5]].state
== TCG_TEMP_CONST
) {
1113 uint32_t al
= temps
[args
[2]].val
;
1114 uint32_t ah
= temps
[args
[3]].val
;
1115 uint32_t bl
= temps
[args
[4]].val
;
1116 uint32_t bh
= temps
[args
[5]].val
;
1117 uint64_t a
= ((uint64_t)ah
<< 32) | al
;
1118 uint64_t b
= ((uint64_t)bh
<< 32) | bl
;
1120 TCGOp
*op2
= insert_op_before(s
, op
, INDEX_op_movi_i32
, 2);
1121 TCGArg
*args2
= &s
->gen_opparam_buf
[op2
->args
];
1123 if (opc
== INDEX_op_add2_i32
) {
1131 tcg_opt_gen_movi(s
, op
, args
, rl
, (uint32_t)a
);
1132 tcg_opt_gen_movi(s
, op2
, args2
, rh
, (uint32_t)(a
>> 32));
1134 /* We've done all we need to do with the movi. Skip it. */
1135 oi_next
= op2
->next
;
1140 case INDEX_op_mulu2_i32
:
1141 if (temps
[args
[2]].state
== TCG_TEMP_CONST
1142 && temps
[args
[3]].state
== TCG_TEMP_CONST
) {
1143 uint32_t a
= temps
[args
[2]].val
;
1144 uint32_t b
= temps
[args
[3]].val
;
1145 uint64_t r
= (uint64_t)a
* b
;
1147 TCGOp
*op2
= insert_op_before(s
, op
, INDEX_op_movi_i32
, 2);
1148 TCGArg
*args2
= &s
->gen_opparam_buf
[op2
->args
];
1152 tcg_opt_gen_movi(s
, op
, args
, rl
, (uint32_t)r
);
1153 tcg_opt_gen_movi(s
, op2
, args2
, rh
, (uint32_t)(r
>> 32));
1155 /* We've done all we need to do with the movi. Skip it. */
1156 oi_next
= op2
->next
;
1161 case INDEX_op_brcond2_i32
:
1162 tmp
= do_constant_folding_cond2(&args
[0], &args
[2], args
[4]);
1166 reset_all_temps(nb_temps
);
1167 op
->opc
= INDEX_op_br
;
1171 tcg_op_remove(s
, op
);
1173 } else if ((args
[4] == TCG_COND_LT
|| args
[4] == TCG_COND_GE
)
1174 && temps
[args
[2]].state
== TCG_TEMP_CONST
1175 && temps
[args
[3]].state
== TCG_TEMP_CONST
1176 && temps
[args
[2]].val
== 0
1177 && 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 && temps
[args
[3]].state
== TCG_TEMP_CONST
1240 && temps
[args
[4]].state
== TCG_TEMP_CONST
1241 && temps
[args
[3]].val
== 0
1242 && temps
[args
[4]].val
== 0) {
1243 /* Simplify LT/GE comparisons vs zero to a single compare
1244 vs the high word of the input. */
1246 reset_temp(args
[0]);
1247 temps
[args
[0]].mask
= 1;
1248 op
->opc
= INDEX_op_setcond_i32
;
1252 } else if (args
[5] == TCG_COND_EQ
) {
1253 /* Simplify EQ comparisons where one of the pairs
1254 can be simplified. */
1255 tmp
= do_constant_folding_cond(INDEX_op_setcond_i32
,
1256 args
[1], args
[3], TCG_COND_EQ
);
1258 goto do_setcond_const
;
1259 } else if (tmp
== 1) {
1260 goto do_setcond_high
;
1262 tmp
= do_constant_folding_cond(INDEX_op_setcond_i32
,
1263 args
[2], args
[4], TCG_COND_EQ
);
1265 goto do_setcond_high
;
1266 } else if (tmp
!= 1) {
1270 reset_temp(args
[0]);
1271 temps
[args
[0]].mask
= 1;
1272 op
->opc
= INDEX_op_setcond_i32
;
1275 } else if (args
[5] == TCG_COND_NE
) {
1276 /* Simplify NE comparisons where one of the pairs
1277 can be simplified. */
1278 tmp
= do_constant_folding_cond(INDEX_op_setcond_i32
,
1279 args
[1], args
[3], TCG_COND_NE
);
1281 goto do_setcond_high
;
1282 } else if (tmp
== 1) {
1283 goto do_setcond_const
;
1285 tmp
= do_constant_folding_cond(INDEX_op_setcond_i32
,
1286 args
[2], args
[4], TCG_COND_NE
);
1288 goto do_setcond_low
;
1289 } else if (tmp
== 1) {
1290 goto do_setcond_const
;
1299 if (!(args
[nb_oargs
+ nb_iargs
+ 1]
1300 & (TCG_CALL_NO_READ_GLOBALS
| TCG_CALL_NO_WRITE_GLOBALS
))) {
1301 for (i
= 0; i
< nb_globals
; i
++) {
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
;