2 * Optimizations for Tiny Code Generator for QEMU
4 * Copyright (c) 2010 Samsung Electronics.
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:
243 case INDEX_op_rotr_i64:
246 case INDEX_op_rotl_i32:
249 case INDEX_op_rotl_i64:
273 CASE_OP_32_64(ext8s):
276 CASE_OP_32_64(ext16s):
279 CASE_OP_32_64(ext8u):
282 CASE_OP_32_64(ext16u):
285 case INDEX_op_ext32s_i64:
288 case INDEX_op_ext32u_i64:
291 case INDEX_op_muluh_i32:
292 return ((uint64_t)(uint32_t)x * (uint32_t)y) >> 32;
293 case INDEX_op_mulsh_i32:
294 return ((int64_t)(int32_t)x * (int32_t)y) >> 32;
296 case INDEX_op_muluh_i64:
297 mulu64(&l64, &h64, x, y);
299 case INDEX_op_mulsh_i64:
300 muls64(&l64, &h64, x, y);
303 case INDEX_op_div_i32:
304 /* Avoid crashing on divide by zero, otherwise undefined. */
305 return (int32_t)x / ((int32_t)y ? : 1);
306 case INDEX_op_divu_i32:
307 return (uint32_t)x / ((uint32_t)y ? : 1);
308 case INDEX_op_div_i64:
309 return (int64_t)x / ((int64_t)y ? : 1);
310 case INDEX_op_divu_i64:
311 return (uint64_t)x / ((uint64_t)y ? : 1);
313 case INDEX_op_rem_i32:
314 return (int32_t)x % ((int32_t)y ? : 1);
315 case INDEX_op_remu_i32:
316 return (uint32_t)x % ((uint32_t)y ? : 1);
317 case INDEX_op_rem_i64:
318 return (int64_t)x % ((int64_t)y ? : 1);
319 case INDEX_op_remu_i64:
320 return (uint64_t)x % ((uint64_t)y ? : 1);
324 "Unrecognized operation %d in do_constant_folding.\n", op);
329 static TCGArg do_constant_folding(TCGOpcode op, TCGArg x, TCGArg y)
331 TCGArg res = do_constant_folding_2(op, x, y);
332 if (op_bits(op) == 32) {
338 static bool do_constant_folding_cond_32(uint32_t x, uint32_t y, TCGCond c)
346 return (int32_t)x < (int32_t)y;
348 return (int32_t)x >= (int32_t)y;
350 return (int32_t)x <= (int32_t)y;
352 return (int32_t)x > (int32_t)y;
366 static bool do_constant_folding_cond_64(uint64_t x, uint64_t y, TCGCond c)
374 return (int64_t)x < (int64_t)y;
376 return (int64_t)x >= (int64_t)y;
378 return (int64_t)x <= (int64_t)y;
380 return (int64_t)x > (int64_t)y;
394 static bool do_constant_folding_cond_eq(TCGCond c)
414 /* Return 2 if the condition can't be simplified, and the result
415 of the condition (0 or 1) if it can */
416 static TCGArg do_constant_folding_cond(TCGOpcode op, TCGArg x,
419 if (temps[x].state == TCG_TEMP_CONST && temps[y].state == TCG_TEMP_CONST) {
420 switch (op_bits(op)) {
422 return do_constant_folding_cond_32(temps[x].val, temps[y].val, c);
424 return do_constant_folding_cond_64(temps[x].val, temps[y].val, c);
428 } else if (temps_are_copies(x, y)) {
429 return do_constant_folding_cond_eq(c);
430 } else if (temps[y].state == TCG_TEMP_CONST && temps[y].val == 0) {
444 /* Return 2 if the condition can't be simplified, and the result
445 of the condition (0 or 1) if it can */
446 static TCGArg do_constant_folding_cond2(TCGArg *p1, TCGArg *p2, TCGCond c)
448 TCGArg al = p1[0], ah = p1[1];
449 TCGArg bl = p2[0], bh = p2[1];
451 if (temps[bl].state == TCG_TEMP_CONST
452 && temps[bh].state == TCG_TEMP_CONST) {
453 uint64_t b = ((uint64_t)temps[bh].val << 32) | (uint32_t)temps[bl].val;
455 if (temps[al].state == TCG_TEMP_CONST
456 && temps[ah].state == TCG_TEMP_CONST) {
458 a = ((uint64_t)temps[ah].val << 32) | (uint32_t)temps[al].val;
459 return do_constant_folding_cond_64(a, b, c);
472 if (temps_are_copies(al, bl) && temps_are_copies(ah, bh)) {
473 return do_constant_folding_cond_eq(c);
478 static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2)
480 TCGArg a1 = *p1, a2 = *p2;
482 sum += temps[a1].state == TCG_TEMP_CONST;
483 sum -= temps[a2].state == TCG_TEMP_CONST;
485 /* Prefer the constant in second argument, and then the form
486 op a, a, b, which is better handled on non-RISC hosts. */
487 if (sum > 0 || (sum == 0 && dest == a2)) {
495 static bool swap_commutative2(TCGArg *p1, TCGArg *p2)
498 sum += temps[p1[0]].state == TCG_TEMP_CONST;
499 sum += temps[p1[1]].state == TCG_TEMP_CONST;
500 sum -= temps[p2[0]].state == TCG_TEMP_CONST;
501 sum -= temps[p2[1]].state == TCG_TEMP_CONST;
504 t = p1[0], p1[0] = p2[0], p2[0] = t;
505 t = p1[1], p1[1] = p2[1], p2[1] = t;
511 /* Propagate constants and copies, fold constant expressions. */
512 static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
513 TCGArg *args, TCGOpDef *tcg_op_defs)
515 int i, nb_ops, op_index, nb_temps, nb_globals, nb_call_args;
516 tcg_target_ulong mask, affected;
522 /* Array VALS has an element for each temp.
523 If this temp holds a constant then its value is kept in VALS' element.
524 If this temp is a copy of other ones then the other copies are
525 available through the doubly linked circular list. */
527 nb_temps = s->nb_temps;
528 nb_globals = s->nb_globals;
529 reset_all_temps(nb_temps);
531 nb_ops = tcg_opc_ptr - s->gen_opc_buf;
533 for (op_index = 0; op_index < nb_ops; op_index++) {
534 op = s->gen_opc_buf[op_index];
535 def = &tcg_op_defs[op];
536 /* Do copy propagation */
537 if (op == INDEX_op_call) {
538 int nb_oargs = args[0] >> 16;
539 int nb_iargs = args[0] & 0xffff;
540 for (i = nb_oargs + 1; i < nb_oargs + nb_iargs + 1; i++) {
541 if (temps[args[i]].state == TCG_TEMP_COPY) {
542 args[i] = find_better_copy(s, args[i]);
546 for (i = def->nb_oargs; i < def->nb_oargs + def->nb_iargs; i++) {
547 if (temps[args[i]].state == TCG_TEMP_COPY) {
548 args[i] = find_better_copy(s, args[i]);
553 /* For commutative operations make constant second argument */
563 CASE_OP_32_64(muluh):
564 CASE_OP_32_64(mulsh):
565 swap_commutative(args[0], &args[1], &args[2]);
567 CASE_OP_32_64(brcond):
568 if (swap_commutative(-1, &args[0], &args[1])) {
569 args[2] = tcg_swap_cond(args[2]);
572 CASE_OP_32_64(setcond):
573 if (swap_commutative(args[0], &args[1], &args[2])) {
574 args[3] = tcg_swap_cond(args[3]);
577 CASE_OP_32_64(movcond):
578 if (swap_commutative(-1, &args[1], &args[2])) {
579 args[5] = tcg_swap_cond(args[5]);
581 /* For movcond, we canonicalize the "false" input reg to match
582 the destination reg so that the tcg backend can implement
583 a "move if true" operation. */
584 if (swap_commutative(args[0], &args[4], &args[3])) {
585 args[5] = tcg_invert_cond(args[5]);
589 swap_commutative(args[0], &args[2], &args[4]);
590 swap_commutative(args[1], &args[3], &args[5]);
592 CASE_OP_32_64(mulu2):
593 CASE_OP_32_64(muls2):
594 swap_commutative(args[0], &args[2], &args[3]);
596 case INDEX_op_brcond2_i32:
597 if (swap_commutative2(&args[0], &args[2])) {
598 args[4] = tcg_swap_cond(args[4]);
601 case INDEX_op_setcond2_i32:
602 if (swap_commutative2(&args[1], &args[3])) {
603 args[5] = tcg_swap_cond(args[5]);
610 /* Simplify expressions for "shift/rot r, 0, a => movi r, 0",
611 and "sub r, 0, a => neg r, a" case. */
618 if (temps[args[1]].state == TCG_TEMP_CONST
619 && temps[args[1]].val == 0) {
620 s->gen_opc_buf[op_index] = op_to_movi(op);
621 tcg_opt_gen_movi(gen_args, args[0], 0);
632 if (temps[args[2]].state == TCG_TEMP_CONST) {
633 /* Proceed with possible constant folding. */
636 if (op == INDEX_op_sub_i32) {
637 neg_op = INDEX_op_neg_i32;
638 have_neg = TCG_TARGET_HAS_neg_i32;
640 neg_op = INDEX_op_neg_i64;
641 have_neg = TCG_TARGET_HAS_neg_i64;
646 if (temps[args[1]].state == TCG_TEMP_CONST
647 && temps[args[1]].val == 0) {
648 s->gen_opc_buf[op_index] = neg_op;
650 gen_args[0] = args[0];
651 gen_args[1] = args[2];
662 /* Simplify expression for "op r, a, 0 => mov r, a" cases */
673 if (temps[args[1]].state == TCG_TEMP_CONST) {
674 /* Proceed with possible constant folding. */
677 if (temps[args[2]].state == TCG_TEMP_CONST
678 && temps[args[2]].val == 0) {
679 if (temps_are_copies(args[0], args[1])) {
680 s->gen_opc_buf[op_index] = INDEX_op_nop;
682 s->gen_opc_buf[op_index] = op_to_mov(op);
683 tcg_opt_gen_mov(s, gen_args, args[0], args[1]);
694 /* Simplify using known-zero bits */
698 CASE_OP_32_64(ext8s):
699 if ((temps[args[1]].mask & 0x80) != 0) {
702 CASE_OP_32_64(ext8u):
705 CASE_OP_32_64(ext16s):
706 if ((temps[args[1]].mask & 0x8000) != 0) {
709 CASE_OP_32_64(ext16u):
712 case INDEX_op_ext32s_i64:
713 if ((temps[args[1]].mask & 0x80000000) != 0) {
716 case INDEX_op_ext32u_i64:
721 mask = temps[args[2]].mask;
722 if (temps[args[2]].state == TCG_TEMP_CONST) {
724 affected = temps[args[1]].mask & ~mask;
726 mask = temps[args[1]].mask & mask;
729 case INDEX_op_sar_i32:
730 if (temps[args[2]].state == TCG_TEMP_CONST) {
731 mask = (int32_t)temps[args[1]].mask >> temps[args[2]].val;
734 case INDEX_op_sar_i64:
735 if (temps[args[2]].state == TCG_TEMP_CONST) {
736 mask = (int64_t)temps[args[1]].mask >> temps[args[2]].val;
740 case INDEX_op_shr_i32:
741 if (temps[args[2]].state == TCG_TEMP_CONST) {
742 mask = (uint32_t)temps[args[1]].mask >> temps[args[2]].val;
745 case INDEX_op_shr_i64:
746 if (temps[args[2]].state == TCG_TEMP_CONST) {
747 mask = (uint64_t)temps[args[1]].mask >> temps[args[2]].val;
752 if (temps[args[2]].state == TCG_TEMP_CONST) {
753 mask = temps[args[1]].mask << temps[args[2]].val;
758 /* Set to 1 all bits to the left of the rightmost. */
759 mask = -(temps[args[1]].mask & -temps[args[1]].mask);
762 CASE_OP_32_64(deposit):
763 tmp = ((1ull << args[4]) - 1);
764 mask = ((temps[args[1]].mask & ~(tmp << args[3]))
765 | ((temps[args[2]].mask & tmp) << args[3]));
770 mask = temps[args[1]].mask | temps[args[2]].mask;
773 CASE_OP_32_64(setcond):
777 CASE_OP_32_64(movcond):
778 mask = temps[args[3]].mask | temps[args[4]].mask;
786 assert(def->nb_oargs == 1);
787 s->gen_opc_buf[op_index] = op_to_movi(op);
788 tcg_opt_gen_movi(gen_args, args[0], 0);
789 args += def->nb_oargs + def->nb_iargs + def->nb_cargs;
794 assert(def->nb_oargs == 1);
795 if (temps_are_copies(args[0], args[1])) {
796 s->gen_opc_buf[op_index] = INDEX_op_nop;
797 } else if (temps[args[1]].state != TCG_TEMP_CONST) {
798 s->gen_opc_buf[op_index] = op_to_mov(op);
799 tcg_opt_gen_mov(s, gen_args, args[0], args[1]);
802 s->gen_opc_buf[op_index] = op_to_movi(op);
803 tcg_opt_gen_movi(gen_args, args[0], temps[args[1]].val);
806 args += def->nb_iargs + 1;
810 /* Simplify expression for "op r, a, 0 => movi r, 0" cases */
814 CASE_OP_32_64(muluh):
815 CASE_OP_32_64(mulsh):
816 if ((temps[args[2]].state == TCG_TEMP_CONST
817 && temps[args[2]].val == 0)) {
818 s->gen_opc_buf[op_index] = op_to_movi(op);
819 tcg_opt_gen_movi(gen_args, args[0], 0);
829 /* Simplify expression for "op r, a, a => mov r, a" cases */
833 if (temps_are_copies(args[1], args[2])) {
834 if (temps_are_copies(args[0], args[1])) {
835 s->gen_opc_buf[op_index] = INDEX_op_nop;
837 s->gen_opc_buf[op_index] = op_to_mov(op);
838 tcg_opt_gen_mov(s, gen_args, args[0], args[1]);
849 /* Simplify expression for "op r, a, a => movi r, 0" cases */
853 if (temps_are_copies(args[1], args[2])) {
854 s->gen_opc_buf[op_index] = op_to_movi(op);
855 tcg_opt_gen_movi(gen_args, args[0], 0);
865 /* Propagate constants through copy operations and do constant
866 folding. Constants will be substituted to arguments by register
867 allocator where needed and possible. Also detect copies. */
870 if (temps_are_copies(args[0], args[1])) {
872 s->gen_opc_buf[op_index] = INDEX_op_nop;
875 if (temps[args[1]].state != TCG_TEMP_CONST) {
876 tcg_opt_gen_mov(s, gen_args, args[0], args[1]);
881 /* Source argument is constant. Rewrite the operation and
882 let movi case handle it. */
884 s->gen_opc_buf[op_index] = op;
885 args[1] = temps[args[1]].val;
888 tcg_opt_gen_movi(gen_args, args[0], args[1]);
895 CASE_OP_32_64(ext8s):
896 CASE_OP_32_64(ext8u):
897 CASE_OP_32_64(ext16s):
898 CASE_OP_32_64(ext16u):
899 case INDEX_op_ext32s_i64:
900 case INDEX_op_ext32u_i64:
901 if (temps[args[1]].state == TCG_TEMP_CONST) {
902 s->gen_opc_buf[op_index] = op_to_movi(op);
903 tmp = do_constant_folding(op, temps[args[1]].val, 0);
904 tcg_opt_gen_movi(gen_args, args[0], tmp);
927 CASE_OP_32_64(muluh):
928 CASE_OP_32_64(mulsh):
933 if (temps[args[1]].state == TCG_TEMP_CONST
934 && temps[args[2]].state == TCG_TEMP_CONST) {
935 s->gen_opc_buf[op_index] = op_to_movi(op);
936 tmp = do_constant_folding(op, temps[args[1]].val,
938 tcg_opt_gen_movi(gen_args, args[0], tmp);
945 CASE_OP_32_64(deposit):
946 if (temps[args[1]].state == TCG_TEMP_CONST
947 && temps[args[2]].state == TCG_TEMP_CONST) {
948 s->gen_opc_buf[op_index] = op_to_movi(op);
949 tmp = ((1ull << args[4]) - 1);
950 tmp = (temps[args[1]].val & ~(tmp << args[3]))
951 | ((temps[args[2]].val & tmp) << args[3]);
952 tcg_opt_gen_movi(gen_args, args[0], tmp);
959 CASE_OP_32_64(setcond):
960 tmp = do_constant_folding_cond(op, args[1], args[2], args[3]);
962 s->gen_opc_buf[op_index] = op_to_movi(op);
963 tcg_opt_gen_movi(gen_args, args[0], tmp);
970 CASE_OP_32_64(brcond):
971 tmp = do_constant_folding_cond(op, args[0], args[1], args[2]);
974 reset_all_temps(nb_temps);
975 s->gen_opc_buf[op_index] = INDEX_op_br;
976 gen_args[0] = args[3];
979 s->gen_opc_buf[op_index] = INDEX_op_nop;
986 CASE_OP_32_64(movcond):
987 tmp = do_constant_folding_cond(op, args[1], args[2], args[5]);
989 if (temps_are_copies(args[0], args[4-tmp])) {
990 s->gen_opc_buf[op_index] = INDEX_op_nop;
991 } else if (temps[args[4-tmp]].state == TCG_TEMP_CONST) {
992 s->gen_opc_buf[op_index] = op_to_movi(op);
993 tcg_opt_gen_movi(gen_args, args[0], temps[args[4-tmp]].val);
996 s->gen_opc_buf[op_index] = op_to_mov(op);
997 tcg_opt_gen_mov(s, gen_args, args[0], args[4-tmp]);
1005 case INDEX_op_add2_i32:
1006 case INDEX_op_sub2_i32:
1007 if (temps[args[2]].state == TCG_TEMP_CONST
1008 && temps[args[3]].state == TCG_TEMP_CONST
1009 && temps[args[4]].state == TCG_TEMP_CONST
1010 && temps[args[5]].state == TCG_TEMP_CONST) {
1011 uint32_t al = temps[args[2]].val;
1012 uint32_t ah = temps[args[3]].val;
1013 uint32_t bl = temps[args[4]].val;
1014 uint32_t bh = temps[args[5]].val;
1015 uint64_t a = ((uint64_t)ah << 32) | al;
1016 uint64_t b = ((uint64_t)bh << 32) | bl;
1019 if (op == INDEX_op_add2_i32) {
1025 /* We emit the extra nop when we emit the add2/sub2. */
1026 assert(s->gen_opc_buf[op_index + 1] == INDEX_op_nop);
1030 s->gen_opc_buf[op_index] = INDEX_op_movi_i32;
1031 s->gen_opc_buf[++op_index] = INDEX_op_movi_i32;
1032 tcg_opt_gen_movi(&gen_args[0], rl, (uint32_t)a);
1033 tcg_opt_gen_movi(&gen_args[2], rh, (uint32_t)(a >> 32));
1040 case INDEX_op_mulu2_i32:
1041 if (temps[args[2]].state == TCG_TEMP_CONST
1042 && temps[args[3]].state == TCG_TEMP_CONST) {
1043 uint32_t a = temps[args[2]].val;
1044 uint32_t b = temps[args[3]].val;
1045 uint64_t r = (uint64_t)a * b;
1048 /* We emit the extra nop when we emit the mulu2. */
1049 assert(s->gen_opc_buf[op_index + 1] == INDEX_op_nop);
1053 s->gen_opc_buf[op_index] = INDEX_op_movi_i32;
1054 s->gen_opc_buf[++op_index] = INDEX_op_movi_i32;
1055 tcg_opt_gen_movi(&gen_args[0], rl, (uint32_t)r);
1056 tcg_opt_gen_movi(&gen_args[2], rh, (uint32_t)(r >> 32));
1063 case INDEX_op_brcond2_i32:
1064 tmp = do_constant_folding_cond2(&args[0], &args[2], args[4]);
1067 reset_all_temps(nb_temps);
1068 s->gen_opc_buf[op_index] = INDEX_op_br;
1069 gen_args[0] = args[5];
1072 s->gen_opc_buf[op_index] = INDEX_op_nop;
1074 } else if ((args[4] == TCG_COND_LT || args[4] == TCG_COND_GE)
1075 && temps[args[2]].state == TCG_TEMP_CONST
1076 && temps[args[3]].state == TCG_TEMP_CONST
1077 && temps[args[2]].val == 0
1078 && temps[args[3]].val == 0) {
1079 /* Simplify LT/GE comparisons vs zero to a single compare
1080 vs the high word of the input. */
1081 reset_all_temps(nb_temps);
1082 s->gen_opc_buf[op_index] = INDEX_op_brcond_i32;
1083 gen_args[0] = args[1];
1084 gen_args[1] = args[3];
1085 gen_args[2] = args[4];
1086 gen_args[3] = args[5];
1094 case INDEX_op_setcond2_i32:
1095 tmp = do_constant_folding_cond2(&args[1], &args[3], args[5]);
1097 s->gen_opc_buf[op_index] = INDEX_op_movi_i32;
1098 tcg_opt_gen_movi(gen_args, args[0], tmp);
1100 } else if ((args[5] == TCG_COND_LT || args[5] == TCG_COND_GE)
1101 && temps[args[3]].state == TCG_TEMP_CONST
1102 && temps[args[4]].state == TCG_TEMP_CONST
1103 && temps[args[3]].val == 0
1104 && temps[args[4]].val == 0) {
1105 /* Simplify LT/GE comparisons vs zero to a single compare
1106 vs the high word of the input. */
1107 s->gen_opc_buf[op_index] = INDEX_op_setcond_i32;
1108 reset_temp(args[0]);
1109 gen_args[0] = args[0];
1110 gen_args[1] = args[2];
1111 gen_args[2] = args[4];
1112 gen_args[3] = args[5];
1121 nb_call_args = (args[0] >> 16) + (args[0] & 0xffff);
1122 if (!(args[nb_call_args + 1] & (TCG_CALL_NO_READ_GLOBALS |
1123 TCG_CALL_NO_WRITE_GLOBALS))) {
1124 for (i = 0; i < nb_globals; i++) {
1128 for (i = 0; i < (args[0] >> 16); i++) {
1129 reset_temp(args[i + 1]);
1131 i = nb_call_args + 3;
1142 /* Default case: we know nothing about operation (or were unable
1143 to compute the operation result) so no propagation is done.
1144 We trash everything if the operation is the end of a basic
1145 block, otherwise we only trash the output args. "mask" is
1146 the non-zero bits mask for the first output arg. */
1147 if (def->flags & TCG_OPF_BB_END) {
1148 reset_all_temps(nb_temps);
1150 for (i = 0; i < def->nb_oargs; i++) {
1151 reset_temp(args[i]);
1154 for (i = 0; i < def->nb_args; i++) {
1155 gen_args[i] = args[i];
1157 args += def->nb_args;
1158 gen_args += def->nb_args;
1166 TCGArg *tcg_optimize(TCGContext *s, uint16_t *tcg_opc_ptr,
1167 TCGArg *args, TCGOpDef *tcg_op_defs)
1170 res = tcg_constant_folding(s, tcg_opc_ptr, args, tcg_op_defs);