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 /* Reset all temporaries, given that there are NB_TEMPS of them. */
71 static void reset_all_temps(int nb_temps
)
74 for (i
= 0; i
< nb_temps
; i
++) {
75 temps
[i
].state
= TCG_TEMP_UNDEF
;
80 static int op_bits(TCGOpcode op
)
82 const TCGOpDef
*def
= &tcg_op_defs
[op
];
83 return def
->flags
& TCG_OPF_64BIT
? 64 : 32;
86 static TCGOpcode
op_to_movi(TCGOpcode op
)
88 switch (op_bits(op
)) {
90 return INDEX_op_movi_i32
;
92 return INDEX_op_movi_i64
;
94 fprintf(stderr
, "op_to_movi: unexpected return value of "
95 "function op_bits.\n");
100 static TCGArg
find_better_copy(TCGContext
*s
, TCGArg temp
)
104 /* If this is already a global, we can't do better. */
105 if (temp
< s
->nb_globals
) {
109 /* Search for a global first. */
110 for (i
= temps
[temp
].next_copy
; i
!= temp
; i
= temps
[i
].next_copy
) {
111 if (i
< s
->nb_globals
) {
116 /* If it is a temp, search for a temp local. */
117 if (!s
->temps
[temp
].temp_local
) {
118 for (i
= temps
[temp
].next_copy
; i
!= temp
; i
= temps
[i
].next_copy
) {
119 if (s
->temps
[i
].temp_local
) {
125 /* Failure to find a better representation, return the same temp. */
129 static bool temps_are_copies(TCGArg arg1
, TCGArg arg2
)
137 if (temps
[arg1
].state
!= TCG_TEMP_COPY
138 || temps
[arg2
].state
!= TCG_TEMP_COPY
) {
142 for (i
= temps
[arg1
].next_copy
; i
!= arg1
; i
= temps
[i
].next_copy
) {
151 static void tcg_opt_gen_mov(TCGContext
*s
, TCGArg
*gen_args
,
152 TCGArg dst
, TCGArg src
)
155 temps
[dst
].mask
= temps
[src
].mask
;
156 assert(temps
[src
].state
!= TCG_TEMP_CONST
);
158 if (s
->temps
[src
].type
== s
->temps
[dst
].type
) {
159 if (temps
[src
].state
!= TCG_TEMP_COPY
) {
160 temps
[src
].state
= TCG_TEMP_COPY
;
161 temps
[src
].next_copy
= src
;
162 temps
[src
].prev_copy
= src
;
164 temps
[dst
].state
= TCG_TEMP_COPY
;
165 temps
[dst
].next_copy
= temps
[src
].next_copy
;
166 temps
[dst
].prev_copy
= src
;
167 temps
[temps
[dst
].next_copy
].prev_copy
= dst
;
168 temps
[src
].next_copy
= dst
;
175 static void tcg_opt_gen_movi(TCGArg
*gen_args
, TCGArg dst
, TCGArg val
)
178 temps
[dst
].state
= TCG_TEMP_CONST
;
179 temps
[dst
].val
= val
;
180 temps
[dst
].mask
= val
;
185 static TCGOpcode
op_to_mov(TCGOpcode op
)
187 switch (op_bits(op
)) {
189 return INDEX_op_mov_i32
;
191 return INDEX_op_mov_i64
;
193 fprintf(stderr
, "op_to_mov: unexpected return value of "
194 "function op_bits.\n");
199 static TCGArg
do_constant_folding_2(TCGOpcode op
, TCGArg x
, TCGArg y
)
222 case INDEX_op_shl_i32
:
223 return (uint32_t)x
<< (uint32_t)y
;
225 case INDEX_op_shl_i64
:
226 return (uint64_t)x
<< (uint64_t)y
;
228 case INDEX_op_shr_i32
:
229 return (uint32_t)x
>> (uint32_t)y
;
231 case INDEX_op_shr_i64
:
232 return (uint64_t)x
>> (uint64_t)y
;
234 case INDEX_op_sar_i32
:
235 return (int32_t)x
>> (int32_t)y
;
237 case INDEX_op_sar_i64
:
238 return (int64_t)x
>> (int64_t)y
;
240 case INDEX_op_rotr_i32
:
241 x
= ((uint32_t)x
<< (32 - y
)) | ((uint32_t)x
>> y
);
244 case INDEX_op_rotr_i64
:
245 x
= ((uint64_t)x
<< (64 - y
)) | ((uint64_t)x
>> y
);
248 case INDEX_op_rotl_i32
:
249 x
= ((uint32_t)x
<< y
) | ((uint32_t)x
>> (32 - y
));
252 case INDEX_op_rotl_i64
:
253 x
= ((uint64_t)x
<< y
) | ((uint64_t)x
>> (64 - y
));
277 CASE_OP_32_64(ext8s
):
280 CASE_OP_32_64(ext16s
):
283 CASE_OP_32_64(ext8u
):
286 CASE_OP_32_64(ext16u
):
289 case INDEX_op_ext32s_i64
:
292 case INDEX_op_ext32u_i64
:
295 case INDEX_op_muluh_i32
:
296 return ((uint64_t)(uint32_t)x
* (uint32_t)y
) >> 32;
297 case INDEX_op_mulsh_i32
:
298 return ((int64_t)(int32_t)x
* (int32_t)y
) >> 32;
300 case INDEX_op_muluh_i64
:
301 mulu64(&l64
, &h64
, x
, y
);
303 case INDEX_op_mulsh_i64
:
304 muls64(&l64
, &h64
, x
, y
);
307 case INDEX_op_div_i32
:
308 /* Avoid crashing on divide by zero, otherwise undefined. */
309 return (int32_t)x
/ ((int32_t)y
? : 1);
310 case INDEX_op_divu_i32
:
311 return (uint32_t)x
/ ((uint32_t)y
? : 1);
312 case INDEX_op_div_i64
:
313 return (int64_t)x
/ ((int64_t)y
? : 1);
314 case INDEX_op_divu_i64
:
315 return (uint64_t)x
/ ((uint64_t)y
? : 1);
317 case INDEX_op_rem_i32
:
318 return (int32_t)x
% ((int32_t)y
? : 1);
319 case INDEX_op_remu_i32
:
320 return (uint32_t)x
% ((uint32_t)y
? : 1);
321 case INDEX_op_rem_i64
:
322 return (int64_t)x
% ((int64_t)y
? : 1);
323 case INDEX_op_remu_i64
:
324 return (uint64_t)x
% ((uint64_t)y
? : 1);
328 "Unrecognized operation %d in do_constant_folding.\n", op
);
333 static TCGArg
do_constant_folding(TCGOpcode op
, TCGArg x
, TCGArg y
)
335 TCGArg res
= do_constant_folding_2(op
, x
, y
);
336 if (op_bits(op
) == 32) {
342 static bool do_constant_folding_cond_32(uint32_t x
, uint32_t y
, TCGCond c
)
350 return (int32_t)x
< (int32_t)y
;
352 return (int32_t)x
>= (int32_t)y
;
354 return (int32_t)x
<= (int32_t)y
;
356 return (int32_t)x
> (int32_t)y
;
370 static bool do_constant_folding_cond_64(uint64_t x
, uint64_t y
, TCGCond c
)
378 return (int64_t)x
< (int64_t)y
;
380 return (int64_t)x
>= (int64_t)y
;
382 return (int64_t)x
<= (int64_t)y
;
384 return (int64_t)x
> (int64_t)y
;
398 static bool do_constant_folding_cond_eq(TCGCond c
)
418 /* Return 2 if the condition can't be simplified, and the result
419 of the condition (0 or 1) if it can */
420 static TCGArg
do_constant_folding_cond(TCGOpcode op
, TCGArg x
,
423 if (temps
[x
].state
== TCG_TEMP_CONST
&& temps
[y
].state
== TCG_TEMP_CONST
) {
424 switch (op_bits(op
)) {
426 return do_constant_folding_cond_32(temps
[x
].val
, temps
[y
].val
, c
);
428 return do_constant_folding_cond_64(temps
[x
].val
, temps
[y
].val
, c
);
432 } else if (temps_are_copies(x
, y
)) {
433 return do_constant_folding_cond_eq(c
);
434 } else if (temps
[y
].state
== TCG_TEMP_CONST
&& temps
[y
].val
== 0) {
448 /* Return 2 if the condition can't be simplified, and the result
449 of the condition (0 or 1) if it can */
450 static TCGArg
do_constant_folding_cond2(TCGArg
*p1
, TCGArg
*p2
, TCGCond c
)
452 TCGArg al
= p1
[0], ah
= p1
[1];
453 TCGArg bl
= p2
[0], bh
= p2
[1];
455 if (temps
[bl
].state
== TCG_TEMP_CONST
456 && temps
[bh
].state
== TCG_TEMP_CONST
) {
457 uint64_t b
= ((uint64_t)temps
[bh
].val
<< 32) | (uint32_t)temps
[bl
].val
;
459 if (temps
[al
].state
== TCG_TEMP_CONST
460 && temps
[ah
].state
== TCG_TEMP_CONST
) {
462 a
= ((uint64_t)temps
[ah
].val
<< 32) | (uint32_t)temps
[al
].val
;
463 return do_constant_folding_cond_64(a
, b
, c
);
476 if (temps_are_copies(al
, bl
) && temps_are_copies(ah
, bh
)) {
477 return do_constant_folding_cond_eq(c
);
482 static bool swap_commutative(TCGArg dest
, TCGArg
*p1
, TCGArg
*p2
)
484 TCGArg a1
= *p1
, a2
= *p2
;
486 sum
+= temps
[a1
].state
== TCG_TEMP_CONST
;
487 sum
-= temps
[a2
].state
== TCG_TEMP_CONST
;
489 /* Prefer the constant in second argument, and then the form
490 op a, a, b, which is better handled on non-RISC hosts. */
491 if (sum
> 0 || (sum
== 0 && dest
== a2
)) {
499 static bool swap_commutative2(TCGArg
*p1
, TCGArg
*p2
)
502 sum
+= temps
[p1
[0]].state
== TCG_TEMP_CONST
;
503 sum
+= temps
[p1
[1]].state
== TCG_TEMP_CONST
;
504 sum
-= temps
[p2
[0]].state
== TCG_TEMP_CONST
;
505 sum
-= temps
[p2
[1]].state
== TCG_TEMP_CONST
;
508 t
= p1
[0], p1
[0] = p2
[0], p2
[0] = t
;
509 t
= p1
[1], p1
[1] = p2
[1], p2
[1] = t
;
515 /* Propagate constants and copies, fold constant expressions. */
516 static TCGArg
*tcg_constant_folding(TCGContext
*s
, uint16_t *tcg_opc_ptr
,
517 TCGArg
*args
, TCGOpDef
*tcg_op_defs
)
519 int i
, nb_ops
, op_index
, nb_temps
, nb_globals
, nb_call_args
;
520 tcg_target_ulong mask
, affected
;
526 /* Array VALS has an element for each temp.
527 If this temp holds a constant then its value is kept in VALS' element.
528 If this temp is a copy of other ones then the other copies are
529 available through the doubly linked circular list. */
531 nb_temps
= s
->nb_temps
;
532 nb_globals
= s
->nb_globals
;
533 reset_all_temps(nb_temps
);
535 nb_ops
= tcg_opc_ptr
- s
->gen_opc_buf
;
537 for (op_index
= 0; op_index
< nb_ops
; op_index
++) {
538 op
= s
->gen_opc_buf
[op_index
];
539 def
= &tcg_op_defs
[op
];
540 /* Do copy propagation */
541 if (op
== INDEX_op_call
) {
542 int nb_oargs
= args
[0] >> 16;
543 int nb_iargs
= args
[0] & 0xffff;
544 for (i
= nb_oargs
+ 1; i
< nb_oargs
+ nb_iargs
+ 1; i
++) {
545 if (temps
[args
[i
]].state
== TCG_TEMP_COPY
) {
546 args
[i
] = find_better_copy(s
, args
[i
]);
550 for (i
= def
->nb_oargs
; i
< def
->nb_oargs
+ def
->nb_iargs
; i
++) {
551 if (temps
[args
[i
]].state
== TCG_TEMP_COPY
) {
552 args
[i
] = find_better_copy(s
, args
[i
]);
557 /* For commutative operations make constant second argument */
567 CASE_OP_32_64(muluh
):
568 CASE_OP_32_64(mulsh
):
569 swap_commutative(args
[0], &args
[1], &args
[2]);
571 CASE_OP_32_64(brcond
):
572 if (swap_commutative(-1, &args
[0], &args
[1])) {
573 args
[2] = tcg_swap_cond(args
[2]);
576 CASE_OP_32_64(setcond
):
577 if (swap_commutative(args
[0], &args
[1], &args
[2])) {
578 args
[3] = tcg_swap_cond(args
[3]);
581 CASE_OP_32_64(movcond
):
582 if (swap_commutative(-1, &args
[1], &args
[2])) {
583 args
[5] = tcg_swap_cond(args
[5]);
585 /* For movcond, we canonicalize the "false" input reg to match
586 the destination reg so that the tcg backend can implement
587 a "move if true" operation. */
588 if (swap_commutative(args
[0], &args
[4], &args
[3])) {
589 args
[5] = tcg_invert_cond(args
[5]);
593 swap_commutative(args
[0], &args
[2], &args
[4]);
594 swap_commutative(args
[1], &args
[3], &args
[5]);
596 CASE_OP_32_64(mulu2
):
597 CASE_OP_32_64(muls2
):
598 swap_commutative(args
[0], &args
[2], &args
[3]);
600 case INDEX_op_brcond2_i32
:
601 if (swap_commutative2(&args
[0], &args
[2])) {
602 args
[4] = tcg_swap_cond(args
[4]);
605 case INDEX_op_setcond2_i32
:
606 if (swap_commutative2(&args
[1], &args
[3])) {
607 args
[5] = tcg_swap_cond(args
[5]);
614 /* Simplify expressions for "shift/rot r, 0, a => movi r, 0",
615 and "sub r, 0, a => neg r, a" case. */
622 if (temps
[args
[1]].state
== TCG_TEMP_CONST
623 && temps
[args
[1]].val
== 0) {
624 s
->gen_opc_buf
[op_index
] = op_to_movi(op
);
625 tcg_opt_gen_movi(gen_args
, args
[0], 0);
636 if (temps
[args
[2]].state
== TCG_TEMP_CONST
) {
637 /* Proceed with possible constant folding. */
640 if (op
== INDEX_op_sub_i32
) {
641 neg_op
= INDEX_op_neg_i32
;
642 have_neg
= TCG_TARGET_HAS_neg_i32
;
644 neg_op
= INDEX_op_neg_i64
;
645 have_neg
= TCG_TARGET_HAS_neg_i64
;
650 if (temps
[args
[1]].state
== TCG_TEMP_CONST
651 && temps
[args
[1]].val
== 0) {
652 s
->gen_opc_buf
[op_index
] = neg_op
;
654 gen_args
[0] = args
[0];
655 gen_args
[1] = args
[2];
666 /* Simplify expression for "op r, a, 0 => mov r, a" cases */
677 if (temps
[args
[1]].state
== TCG_TEMP_CONST
) {
678 /* Proceed with possible constant folding. */
681 if (temps
[args
[2]].state
== TCG_TEMP_CONST
682 && temps
[args
[2]].val
== 0) {
683 if (temps_are_copies(args
[0], args
[1])) {
684 s
->gen_opc_buf
[op_index
] = INDEX_op_nop
;
686 s
->gen_opc_buf
[op_index
] = op_to_mov(op
);
687 tcg_opt_gen_mov(s
, gen_args
, args
[0], args
[1]);
698 /* Simplify using known-zero bits */
702 CASE_OP_32_64(ext8s
):
703 if ((temps
[args
[1]].mask
& 0x80) != 0) {
706 CASE_OP_32_64(ext8u
):
709 CASE_OP_32_64(ext16s
):
710 if ((temps
[args
[1]].mask
& 0x8000) != 0) {
713 CASE_OP_32_64(ext16u
):
716 case INDEX_op_ext32s_i64
:
717 if ((temps
[args
[1]].mask
& 0x80000000) != 0) {
720 case INDEX_op_ext32u_i64
:
725 mask
= temps
[args
[2]].mask
;
726 if (temps
[args
[2]].state
== TCG_TEMP_CONST
) {
728 affected
= temps
[args
[1]].mask
& ~mask
;
730 mask
= temps
[args
[1]].mask
& mask
;
734 if (temps
[args
[2]].state
== TCG_TEMP_CONST
) {
735 mask
= ((tcg_target_long
)temps
[args
[1]].mask
736 >> temps
[args
[2]].val
);
741 if (temps
[args
[2]].state
== TCG_TEMP_CONST
) {
742 mask
= temps
[args
[1]].mask
>> temps
[args
[2]].val
;
747 if (temps
[args
[2]].state
== TCG_TEMP_CONST
) {
748 mask
= temps
[args
[1]].mask
<< temps
[args
[2]].val
;
753 /* Set to 1 all bits to the left of the rightmost. */
754 mask
= -(temps
[args
[1]].mask
& -temps
[args
[1]].mask
);
757 CASE_OP_32_64(deposit
):
758 tmp
= ((1ull << args
[4]) - 1);
759 mask
= ((temps
[args
[1]].mask
& ~(tmp
<< args
[3]))
760 | ((temps
[args
[2]].mask
& tmp
) << args
[3]));
765 mask
= temps
[args
[1]].mask
| temps
[args
[2]].mask
;
768 CASE_OP_32_64(setcond
):
772 CASE_OP_32_64(movcond
):
773 mask
= temps
[args
[3]].mask
| temps
[args
[4]].mask
;
781 assert(def
->nb_oargs
== 1);
782 s
->gen_opc_buf
[op_index
] = op_to_movi(op
);
783 tcg_opt_gen_movi(gen_args
, args
[0], 0);
784 args
+= def
->nb_oargs
+ def
->nb_iargs
+ def
->nb_cargs
;
789 assert(def
->nb_oargs
== 1);
790 if (temps_are_copies(args
[0], args
[1])) {
791 s
->gen_opc_buf
[op_index
] = INDEX_op_nop
;
792 } else if (temps
[args
[1]].state
!= TCG_TEMP_CONST
) {
793 s
->gen_opc_buf
[op_index
] = op_to_mov(op
);
794 tcg_opt_gen_mov(s
, gen_args
, args
[0], args
[1]);
797 s
->gen_opc_buf
[op_index
] = op_to_movi(op
);
798 tcg_opt_gen_movi(gen_args
, args
[0], temps
[args
[1]].val
);
801 args
+= def
->nb_iargs
+ 1;
805 /* Simplify expression for "op r, a, 0 => movi r, 0" cases */
809 CASE_OP_32_64(muluh
):
810 CASE_OP_32_64(mulsh
):
811 if ((temps
[args
[2]].state
== TCG_TEMP_CONST
812 && temps
[args
[2]].val
== 0)) {
813 s
->gen_opc_buf
[op_index
] = op_to_movi(op
);
814 tcg_opt_gen_movi(gen_args
, args
[0], 0);
824 /* Simplify expression for "op r, a, a => mov r, a" cases */
828 if (temps_are_copies(args
[1], args
[2])) {
829 if (temps_are_copies(args
[0], args
[1])) {
830 s
->gen_opc_buf
[op_index
] = INDEX_op_nop
;
832 s
->gen_opc_buf
[op_index
] = op_to_mov(op
);
833 tcg_opt_gen_mov(s
, gen_args
, args
[0], args
[1]);
844 /* Simplify expression for "op r, a, a => movi r, 0" cases */
848 if (temps_are_copies(args
[1], args
[2])) {
849 s
->gen_opc_buf
[op_index
] = op_to_movi(op
);
850 tcg_opt_gen_movi(gen_args
, args
[0], 0);
860 /* Propagate constants through copy operations and do constant
861 folding. Constants will be substituted to arguments by register
862 allocator where needed and possible. Also detect copies. */
865 if (temps_are_copies(args
[0], args
[1])) {
867 s
->gen_opc_buf
[op_index
] = INDEX_op_nop
;
870 if (temps
[args
[1]].state
!= TCG_TEMP_CONST
) {
871 tcg_opt_gen_mov(s
, gen_args
, args
[0], args
[1]);
876 /* Source argument is constant. Rewrite the operation and
877 let movi case handle it. */
879 s
->gen_opc_buf
[op_index
] = op
;
880 args
[1] = temps
[args
[1]].val
;
883 tcg_opt_gen_movi(gen_args
, args
[0], args
[1]);
890 CASE_OP_32_64(ext8s
):
891 CASE_OP_32_64(ext8u
):
892 CASE_OP_32_64(ext16s
):
893 CASE_OP_32_64(ext16u
):
894 case INDEX_op_ext32s_i64
:
895 case INDEX_op_ext32u_i64
:
896 if (temps
[args
[1]].state
== TCG_TEMP_CONST
) {
897 s
->gen_opc_buf
[op_index
] = op_to_movi(op
);
898 tmp
= do_constant_folding(op
, temps
[args
[1]].val
, 0);
899 tcg_opt_gen_movi(gen_args
, args
[0], tmp
);
922 CASE_OP_32_64(muluh
):
923 CASE_OP_32_64(mulsh
):
928 if (temps
[args
[1]].state
== TCG_TEMP_CONST
929 && temps
[args
[2]].state
== TCG_TEMP_CONST
) {
930 s
->gen_opc_buf
[op_index
] = op_to_movi(op
);
931 tmp
= do_constant_folding(op
, temps
[args
[1]].val
,
933 tcg_opt_gen_movi(gen_args
, args
[0], tmp
);
940 CASE_OP_32_64(deposit
):
941 if (temps
[args
[1]].state
== TCG_TEMP_CONST
942 && temps
[args
[2]].state
== TCG_TEMP_CONST
) {
943 s
->gen_opc_buf
[op_index
] = op_to_movi(op
);
944 tmp
= ((1ull << args
[4]) - 1);
945 tmp
= (temps
[args
[1]].val
& ~(tmp
<< args
[3]))
946 | ((temps
[args
[2]].val
& tmp
) << args
[3]);
947 tcg_opt_gen_movi(gen_args
, args
[0], tmp
);
954 CASE_OP_32_64(setcond
):
955 tmp
= do_constant_folding_cond(op
, args
[1], args
[2], args
[3]);
957 s
->gen_opc_buf
[op_index
] = op_to_movi(op
);
958 tcg_opt_gen_movi(gen_args
, args
[0], tmp
);
965 CASE_OP_32_64(brcond
):
966 tmp
= do_constant_folding_cond(op
, args
[0], args
[1], args
[2]);
969 reset_all_temps(nb_temps
);
970 s
->gen_opc_buf
[op_index
] = INDEX_op_br
;
971 gen_args
[0] = args
[3];
974 s
->gen_opc_buf
[op_index
] = INDEX_op_nop
;
981 CASE_OP_32_64(movcond
):
982 tmp
= do_constant_folding_cond(op
, args
[1], args
[2], args
[5]);
984 if (temps_are_copies(args
[0], args
[4-tmp
])) {
985 s
->gen_opc_buf
[op_index
] = INDEX_op_nop
;
986 } else if (temps
[args
[4-tmp
]].state
== TCG_TEMP_CONST
) {
987 s
->gen_opc_buf
[op_index
] = op_to_movi(op
);
988 tcg_opt_gen_movi(gen_args
, args
[0], temps
[args
[4-tmp
]].val
);
991 s
->gen_opc_buf
[op_index
] = op_to_mov(op
);
992 tcg_opt_gen_mov(s
, gen_args
, args
[0], args
[4-tmp
]);
1000 case INDEX_op_add2_i32
:
1001 case INDEX_op_sub2_i32
:
1002 if (temps
[args
[2]].state
== TCG_TEMP_CONST
1003 && temps
[args
[3]].state
== TCG_TEMP_CONST
1004 && temps
[args
[4]].state
== TCG_TEMP_CONST
1005 && temps
[args
[5]].state
== TCG_TEMP_CONST
) {
1006 uint32_t al
= temps
[args
[2]].val
;
1007 uint32_t ah
= temps
[args
[3]].val
;
1008 uint32_t bl
= temps
[args
[4]].val
;
1009 uint32_t bh
= temps
[args
[5]].val
;
1010 uint64_t a
= ((uint64_t)ah
<< 32) | al
;
1011 uint64_t b
= ((uint64_t)bh
<< 32) | bl
;
1014 if (op
== INDEX_op_add2_i32
) {
1020 /* We emit the extra nop when we emit the add2/sub2. */
1021 assert(s
->gen_opc_buf
[op_index
+ 1] == INDEX_op_nop
);
1025 s
->gen_opc_buf
[op_index
] = INDEX_op_movi_i32
;
1026 s
->gen_opc_buf
[++op_index
] = INDEX_op_movi_i32
;
1027 tcg_opt_gen_movi(&gen_args
[0], rl
, (uint32_t)a
);
1028 tcg_opt_gen_movi(&gen_args
[2], rh
, (uint32_t)(a
>> 32));
1035 case INDEX_op_mulu2_i32
:
1036 if (temps
[args
[2]].state
== TCG_TEMP_CONST
1037 && temps
[args
[3]].state
== TCG_TEMP_CONST
) {
1038 uint32_t a
= temps
[args
[2]].val
;
1039 uint32_t b
= temps
[args
[3]].val
;
1040 uint64_t r
= (uint64_t)a
* b
;
1043 /* We emit the extra nop when we emit the mulu2. */
1044 assert(s
->gen_opc_buf
[op_index
+ 1] == INDEX_op_nop
);
1048 s
->gen_opc_buf
[op_index
] = INDEX_op_movi_i32
;
1049 s
->gen_opc_buf
[++op_index
] = INDEX_op_movi_i32
;
1050 tcg_opt_gen_movi(&gen_args
[0], rl
, (uint32_t)r
);
1051 tcg_opt_gen_movi(&gen_args
[2], rh
, (uint32_t)(r
>> 32));
1058 case INDEX_op_brcond2_i32
:
1059 tmp
= do_constant_folding_cond2(&args
[0], &args
[2], args
[4]);
1062 reset_all_temps(nb_temps
);
1063 s
->gen_opc_buf
[op_index
] = INDEX_op_br
;
1064 gen_args
[0] = args
[5];
1067 s
->gen_opc_buf
[op_index
] = INDEX_op_nop
;
1069 } else if ((args
[4] == TCG_COND_LT
|| args
[4] == TCG_COND_GE
)
1070 && temps
[args
[2]].state
== TCG_TEMP_CONST
1071 && temps
[args
[3]].state
== TCG_TEMP_CONST
1072 && temps
[args
[2]].val
== 0
1073 && temps
[args
[3]].val
== 0) {
1074 /* Simplify LT/GE comparisons vs zero to a single compare
1075 vs the high word of the input. */
1076 reset_all_temps(nb_temps
);
1077 s
->gen_opc_buf
[op_index
] = INDEX_op_brcond_i32
;
1078 gen_args
[0] = args
[1];
1079 gen_args
[1] = args
[3];
1080 gen_args
[2] = args
[4];
1081 gen_args
[3] = args
[5];
1089 case INDEX_op_setcond2_i32
:
1090 tmp
= do_constant_folding_cond2(&args
[1], &args
[3], args
[5]);
1092 s
->gen_opc_buf
[op_index
] = INDEX_op_movi_i32
;
1093 tcg_opt_gen_movi(gen_args
, args
[0], tmp
);
1095 } else if ((args
[5] == TCG_COND_LT
|| args
[5] == TCG_COND_GE
)
1096 && temps
[args
[3]].state
== TCG_TEMP_CONST
1097 && temps
[args
[4]].state
== TCG_TEMP_CONST
1098 && temps
[args
[3]].val
== 0
1099 && temps
[args
[4]].val
== 0) {
1100 /* Simplify LT/GE comparisons vs zero to a single compare
1101 vs the high word of the input. */
1102 s
->gen_opc_buf
[op_index
] = INDEX_op_setcond_i32
;
1103 reset_temp(args
[0]);
1104 gen_args
[0] = args
[0];
1105 gen_args
[1] = args
[2];
1106 gen_args
[2] = args
[4];
1107 gen_args
[3] = args
[5];
1116 nb_call_args
= (args
[0] >> 16) + (args
[0] & 0xffff);
1117 if (!(args
[nb_call_args
+ 1] & (TCG_CALL_NO_READ_GLOBALS
|
1118 TCG_CALL_NO_WRITE_GLOBALS
))) {
1119 for (i
= 0; i
< nb_globals
; i
++) {
1123 for (i
= 0; i
< (args
[0] >> 16); i
++) {
1124 reset_temp(args
[i
+ 1]);
1126 i
= nb_call_args
+ 3;
1137 /* Default case: we know nothing about operation (or were unable
1138 to compute the operation result) so no propagation is done.
1139 We trash everything if the operation is the end of a basic
1140 block, otherwise we only trash the output args. "mask" is
1141 the non-zero bits mask for the first output arg. */
1142 if (def
->flags
& TCG_OPF_BB_END
) {
1143 reset_all_temps(nb_temps
);
1145 for (i
= 0; i
< def
->nb_oargs
; i
++) {
1146 reset_temp(args
[i
]);
1149 for (i
= 0; i
< def
->nb_args
; i
++) {
1150 gen_args
[i
] = args
[i
];
1152 args
+= def
->nb_args
;
1153 gen_args
+= def
->nb_args
;
1161 TCGArg
*tcg_optimize(TCGContext
*s
, uint16_t *tcg_opc_ptr
,
1162 TCGArg
*args
, TCGOpDef
*tcg_op_defs
)
1165 res
= tcg_constant_folding(s
, tcg_opc_ptr
, args
, tcg_op_defs
);