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 {
51 static struct tcg_temp_info temps[TCG_MAX_TEMPS];
53 /* Reset TEMP's state to TCG_TEMP_UNDEF. If TEMP only had one copy, remove
54 the copy flag from the left temp. */
55 static void reset_temp(TCGArg temp)
57 if (temps[temp].state == TCG_TEMP_COPY) {
58 if (temps[temp].prev_copy == temps[temp].next_copy) {
59 temps[temps[temp].next_copy].state = TCG_TEMP_UNDEF;
61 temps[temps[temp].next_copy].prev_copy = temps[temp].prev_copy;
62 temps[temps[temp].prev_copy].next_copy = temps[temp].next_copy;
65 temps[temp].state = TCG_TEMP_UNDEF;
68 static int op_bits(TCGOpcode op)
70 const TCGOpDef *def = &tcg_op_defs[op];
71 return def->flags & TCG_OPF_64BIT ? 64 : 32;
74 static TCGOpcode op_to_movi(TCGOpcode op)
76 switch (op_bits(op)) {
78 return INDEX_op_movi_i32;
80 return INDEX_op_movi_i64;
82 fprintf(stderr, "op_to_movi: unexpected return value of "
83 "function op_bits.\n");
88 static TCGArg find_better_copy(TCGContext *s, TCGArg temp)
92 /* If this is already a global, we can't do better. */
93 if (temp < s->nb_globals) {
97 /* Search for a global first. */
98 for (i = temps[temp].next_copy ; i != temp ; i = temps[i].next_copy) {
99 if (i < s->nb_globals) {
104 /* If it is a temp, search for a temp local. */
105 if (!s->temps[temp].temp_local) {
106 for (i = temps[temp].next_copy ; i != temp ; i = temps[i].next_copy) {
107 if (s->temps[i].temp_local) {
113 /* Failure to find a better representation, return the same temp. */
117 static bool temps_are_copies(TCGArg arg1, TCGArg arg2)
125 if (temps[arg1].state != TCG_TEMP_COPY
126 || temps[arg2].state != TCG_TEMP_COPY) {
130 for (i = temps[arg1].next_copy ; i != arg1 ; i = temps[i].next_copy) {
139 static void tcg_opt_gen_mov(TCGContext *s, TCGArg *gen_args,
140 TCGArg dst, TCGArg src)
143 assert(temps[src].state != TCG_TEMP_CONST);
145 if (s->temps[src].type == s->temps[dst].type) {
146 if (temps[src].state != TCG_TEMP_COPY) {
147 temps[src].state = TCG_TEMP_COPY;
148 temps[src].next_copy = src;
149 temps[src].prev_copy = src;
151 temps[dst].state = TCG_TEMP_COPY;
152 temps[dst].next_copy = temps[src].next_copy;
153 temps[dst].prev_copy = src;
154 temps[temps[dst].next_copy].prev_copy = dst;
155 temps[src].next_copy = dst;
162 static void tcg_opt_gen_movi(TCGArg *gen_args, TCGArg dst, TCGArg val)
165 temps[dst].state = TCG_TEMP_CONST;
166 temps[dst].val = val;
171 static TCGOpcode op_to_mov(TCGOpcode op)
173 switch (op_bits(op)) {
175 return INDEX_op_mov_i32;
177 return INDEX_op_mov_i64;
179 fprintf(stderr, "op_to_mov: unexpected return value of "
180 "function op_bits.\n");
185 static TCGArg do_constant_folding_2(TCGOpcode op, TCGArg x, TCGArg y)
206 case INDEX_op_shl_i32:
207 return (uint32_t)x << (uint32_t)y;
209 case INDEX_op_shl_i64:
210 return (uint64_t)x << (uint64_t)y;
212 case INDEX_op_shr_i32:
213 return (uint32_t)x >> (uint32_t)y;
215 case INDEX_op_shr_i64:
216 return (uint64_t)x >> (uint64_t)y;
218 case INDEX_op_sar_i32:
219 return (int32_t)x >> (int32_t)y;
221 case INDEX_op_sar_i64:
222 return (int64_t)x >> (int64_t)y;
224 case INDEX_op_rotr_i32:
225 x = ((uint32_t)x << (32 - y)) | ((uint32_t)x >> y);
228 case INDEX_op_rotr_i64:
229 x = ((uint64_t)x << (64 - y)) | ((uint64_t)x >> y);
232 case INDEX_op_rotl_i32:
233 x = ((uint32_t)x << y) | ((uint32_t)x >> (32 - y));
236 case INDEX_op_rotl_i64:
237 x = ((uint64_t)x << y) | ((uint64_t)x >> (64 - y));
261 CASE_OP_32_64(ext8s):
264 CASE_OP_32_64(ext16s):
267 CASE_OP_32_64(ext8u):
270 CASE_OP_32_64(ext16u):
273 case INDEX_op_ext32s_i64:
276 case INDEX_op_ext32u_i64:
281 "Unrecognized operation %d in do_constant_folding.\n", op);
286 static TCGArg do_constant_folding(TCGOpcode op, TCGArg x, TCGArg y)
288 TCGArg res = do_constant_folding_2(op, x, y);
289 if (op_bits(op) == 32) {
295 static bool do_constant_folding_cond_32(uint32_t x, uint32_t y, TCGCond c)
303 return (int32_t)x < (int32_t)y;
305 return (int32_t)x >= (int32_t)y;
307 return (int32_t)x <= (int32_t)y;
309 return (int32_t)x > (int32_t)y;
323 static bool do_constant_folding_cond_64(uint64_t x, uint64_t y, TCGCond c)
331 return (int64_t)x < (int64_t)y;
333 return (int64_t)x >= (int64_t)y;
335 return (int64_t)x <= (int64_t)y;
337 return (int64_t)x > (int64_t)y;
351 static bool do_constant_folding_cond_eq(TCGCond c)
371 /* Return 2 if the condition can't be simplified, and the result
372 of the condition (0 or 1) if it can */
373 static TCGArg do_constant_folding_cond(TCGOpcode op, TCGArg x,
376 if (temps[x].state == TCG_TEMP_CONST && temps[y].state == TCG_TEMP_CONST) {
377 switch (op_bits(op)) {
379 return do_constant_folding_cond_32(temps[x].val, temps[y].val, c);
381 return do_constant_folding_cond_64(temps[x].val, temps[y].val, c);
385 } else if (temps_are_copies(x, y)) {
386 return do_constant_folding_cond_eq(c);
387 } else if (temps[y].state == TCG_TEMP_CONST && temps[y].val == 0) {
401 /* Return 2 if the condition can't be simplified, and the result
402 of the condition (0 or 1) if it can */
403 static TCGArg do_constant_folding_cond2(TCGArg *p1, TCGArg *p2, TCGCond c)
405 TCGArg al = p1[0], ah = p1[1];
406 TCGArg bl = p2[0], bh = p2[1];
408 if (temps[bl].state == TCG_TEMP_CONST
409 && temps[bh].state == TCG_TEMP_CONST) {
410 uint64_t b = ((uint64_t)temps[bh].val << 32) | (uint32_t)temps[bl].val;
412 if (temps[al].state == TCG_TEMP_CONST
413 && temps[ah].state == TCG_TEMP_CONST) {
415 a = ((uint64_t)temps[ah].val << 32) | (uint32_t)temps[al].val;
416 return do_constant_folding_cond_64(a, b, c);
429 if (temps_are_copies(al, bl) && temps_are_copies(ah, bh)) {
430 return do_constant_folding_cond_eq(c);
435 static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2)
437 TCGArg a1 = *p1, a2 = *p2;
439 sum += temps[a1].state == TCG_TEMP_CONST;
440 sum -= temps[a2].state == TCG_TEMP_CONST;
442 /* Prefer the constant in second argument, and then the form
443 op a, a, b, which is better handled on non-RISC hosts. */
444 if (sum > 0 || (sum == 0 && dest == a2)) {
452 static bool swap_commutative2(TCGArg *p1, TCGArg *p2)
455 sum += temps[p1[0]].state == TCG_TEMP_CONST;
456 sum += temps[p1[1]].state == TCG_TEMP_CONST;
457 sum -= temps[p2[0]].state == TCG_TEMP_CONST;
458 sum -= temps[p2[1]].state == TCG_TEMP_CONST;
461 t = p1[0], p1[0] = p2[0], p2[0] = t;
462 t = p1[1], p1[1] = p2[1], p2[1] = t;
468 /* Propagate constants and copies, fold constant expressions. */
469 static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
470 TCGArg *args, TCGOpDef *tcg_op_defs)
472 int i, nb_ops, op_index, nb_temps, nb_globals, nb_call_args;
478 /* Array VALS has an element for each temp.
479 If this temp holds a constant then its value is kept in VALS' element.
480 If this temp is a copy of other ones then the other copies are
481 available through the doubly linked circular list. */
483 nb_temps = s->nb_temps;
484 nb_globals = s->nb_globals;
485 memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info));
487 nb_ops = tcg_opc_ptr - gen_opc_buf;
489 for (op_index = 0; op_index < nb_ops; op_index++) {
490 op = gen_opc_buf[op_index];
491 def = &tcg_op_defs[op];
492 /* Do copy propagation */
493 if (op == INDEX_op_call) {
494 int nb_oargs = args[0] >> 16;
495 int nb_iargs = args[0] & 0xffff;
496 for (i = nb_oargs + 1; i < nb_oargs + nb_iargs + 1; i++) {
497 if (temps[args[i]].state == TCG_TEMP_COPY) {
498 args[i] = find_better_copy(s, args[i]);
502 for (i = def->nb_oargs; i < def->nb_oargs + def->nb_iargs; i++) {
503 if (temps[args[i]].state == TCG_TEMP_COPY) {
504 args[i] = find_better_copy(s, args[i]);
509 /* For commutative operations make constant second argument */
519 swap_commutative(args[0], &args[1], &args[2]);
521 CASE_OP_32_64(brcond):
522 if (swap_commutative(-1, &args[0], &args[1])) {
523 args[2] = tcg_swap_cond(args[2]);
526 CASE_OP_32_64(setcond):
527 if (swap_commutative(args[0], &args[1], &args[2])) {
528 args[3] = tcg_swap_cond(args[3]);
531 CASE_OP_32_64(movcond):
532 if (swap_commutative(-1, &args[1], &args[2])) {
533 args[5] = tcg_swap_cond(args[5]);
535 /* For movcond, we canonicalize the "false" input reg to match
536 the destination reg so that the tcg backend can implement
537 a "move if true" operation. */
538 if (swap_commutative(args[0], &args[4], &args[3])) {
539 args[5] = tcg_invert_cond(args[5]);
542 case INDEX_op_add2_i32:
543 swap_commutative(args[0], &args[2], &args[4]);
544 swap_commutative(args[1], &args[3], &args[5]);
546 case INDEX_op_mulu2_i32:
547 swap_commutative(args[0], &args[2], &args[3]);
549 case INDEX_op_brcond2_i32:
550 if (swap_commutative2(&args[0], &args[2])) {
551 args[4] = tcg_swap_cond(args[4]);
554 case INDEX_op_setcond2_i32:
555 if (swap_commutative2(&args[1], &args[3])) {
556 args[5] = tcg_swap_cond(args[5]);
563 /* Simplify expressions for "shift/rot r, 0, a => movi r, 0" */
570 if (temps[args[1]].state == TCG_TEMP_CONST
571 && temps[args[1]].val == 0) {
572 gen_opc_buf[op_index] = op_to_movi(op);
573 tcg_opt_gen_movi(gen_args, args[0], 0);
583 /* Simplify expression for "op r, a, 0 => mov r, a" cases */
594 if (temps[args[1]].state == TCG_TEMP_CONST) {
595 /* Proceed with possible constant folding. */
598 if (temps[args[2]].state == TCG_TEMP_CONST
599 && temps[args[2]].val == 0) {
600 if (temps_are_copies(args[0], args[1])) {
601 gen_opc_buf[op_index] = INDEX_op_nop;
603 gen_opc_buf[op_index] = op_to_mov(op);
604 tcg_opt_gen_mov(s, gen_args, args[0], args[1]);
615 /* Simplify expression for "op r, a, 0 => movi r, 0" cases */
619 if ((temps[args[2]].state == TCG_TEMP_CONST
620 && temps[args[2]].val == 0)) {
621 gen_opc_buf[op_index] = op_to_movi(op);
622 tcg_opt_gen_movi(gen_args, args[0], 0);
632 /* Simplify expression for "op r, a, a => mov r, a" cases */
636 if (temps_are_copies(args[1], args[2])) {
637 if (temps_are_copies(args[0], args[1])) {
638 gen_opc_buf[op_index] = INDEX_op_nop;
640 gen_opc_buf[op_index] = op_to_mov(op);
641 tcg_opt_gen_mov(s, gen_args, args[0], args[1]);
652 /* Simplify expression for "op r, a, a => movi r, 0" cases */
656 if (temps_are_copies(args[1], args[2])) {
657 gen_opc_buf[op_index] = op_to_movi(op);
658 tcg_opt_gen_movi(gen_args, args[0], 0);
668 /* Propagate constants through copy operations and do constant
669 folding. Constants will be substituted to arguments by register
670 allocator where needed and possible. Also detect copies. */
673 if (temps_are_copies(args[0], args[1])) {
675 gen_opc_buf[op_index] = INDEX_op_nop;
678 if (temps[args[1]].state != TCG_TEMP_CONST) {
679 tcg_opt_gen_mov(s, gen_args, args[0], args[1]);
684 /* Source argument is constant. Rewrite the operation and
685 let movi case handle it. */
687 gen_opc_buf[op_index] = op;
688 args[1] = temps[args[1]].val;
691 tcg_opt_gen_movi(gen_args, args[0], args[1]);
698 CASE_OP_32_64(ext8s):
699 CASE_OP_32_64(ext8u):
700 CASE_OP_32_64(ext16s):
701 CASE_OP_32_64(ext16u):
702 case INDEX_op_ext32s_i64:
703 case INDEX_op_ext32u_i64:
704 if (temps[args[1]].state == TCG_TEMP_CONST) {
705 gen_opc_buf[op_index] = op_to_movi(op);
706 tmp = do_constant_folding(op, temps[args[1]].val, 0);
707 tcg_opt_gen_movi(gen_args, args[0], tmp);
730 if (temps[args[1]].state == TCG_TEMP_CONST
731 && temps[args[2]].state == TCG_TEMP_CONST) {
732 gen_opc_buf[op_index] = op_to_movi(op);
733 tmp = do_constant_folding(op, temps[args[1]].val,
735 tcg_opt_gen_movi(gen_args, args[0], tmp);
742 CASE_OP_32_64(deposit):
743 if (temps[args[1]].state == TCG_TEMP_CONST
744 && temps[args[2]].state == TCG_TEMP_CONST) {
745 gen_opc_buf[op_index] = op_to_movi(op);
746 tmp = ((1ull << args[4]) - 1);
747 tmp = (temps[args[1]].val & ~(tmp << args[3]))
748 | ((temps[args[2]].val & tmp) << args[3]);
749 tcg_opt_gen_movi(gen_args, args[0], tmp);
756 CASE_OP_32_64(setcond):
757 tmp = do_constant_folding_cond(op, args[1], args[2], args[3]);
759 gen_opc_buf[op_index] = op_to_movi(op);
760 tcg_opt_gen_movi(gen_args, args[0], tmp);
767 CASE_OP_32_64(brcond):
768 tmp = do_constant_folding_cond(op, args[0], args[1], args[2]);
771 memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info));
772 gen_opc_buf[op_index] = INDEX_op_br;
773 gen_args[0] = args[3];
776 gen_opc_buf[op_index] = INDEX_op_nop;
783 CASE_OP_32_64(movcond):
784 tmp = do_constant_folding_cond(op, args[1], args[2], args[5]);
786 if (temps_are_copies(args[0], args[4-tmp])) {
787 gen_opc_buf[op_index] = INDEX_op_nop;
788 } else if (temps[args[4-tmp]].state == TCG_TEMP_CONST) {
789 gen_opc_buf[op_index] = op_to_movi(op);
790 tcg_opt_gen_movi(gen_args, args[0], temps[args[4-tmp]].val);
793 gen_opc_buf[op_index] = op_to_mov(op);
794 tcg_opt_gen_mov(s, gen_args, args[0], args[4-tmp]);
802 case INDEX_op_add2_i32:
803 case INDEX_op_sub2_i32:
804 if (temps[args[2]].state == TCG_TEMP_CONST
805 && temps[args[3]].state == TCG_TEMP_CONST
806 && temps[args[4]].state == TCG_TEMP_CONST
807 && temps[args[5]].state == TCG_TEMP_CONST) {
808 uint32_t al = temps[args[2]].val;
809 uint32_t ah = temps[args[3]].val;
810 uint32_t bl = temps[args[4]].val;
811 uint32_t bh = temps[args[5]].val;
812 uint64_t a = ((uint64_t)ah << 32) | al;
813 uint64_t b = ((uint64_t)bh << 32) | bl;
816 if (op == INDEX_op_add2_i32) {
822 /* We emit the extra nop when we emit the add2/sub2. */
823 assert(gen_opc_buf[op_index + 1] == INDEX_op_nop);
827 gen_opc_buf[op_index] = INDEX_op_movi_i32;
828 gen_opc_buf[++op_index] = INDEX_op_movi_i32;
829 tcg_opt_gen_movi(&gen_args[0], rl, (uint32_t)a);
830 tcg_opt_gen_movi(&gen_args[2], rh, (uint32_t)(a >> 32));
837 case INDEX_op_mulu2_i32:
838 if (temps[args[2]].state == TCG_TEMP_CONST
839 && temps[args[3]].state == TCG_TEMP_CONST) {
840 uint32_t a = temps[args[2]].val;
841 uint32_t b = temps[args[3]].val;
842 uint64_t r = (uint64_t)a * b;
845 /* We emit the extra nop when we emit the mulu2. */
846 assert(gen_opc_buf[op_index + 1] == INDEX_op_nop);
850 gen_opc_buf[op_index] = INDEX_op_movi_i32;
851 gen_opc_buf[++op_index] = INDEX_op_movi_i32;
852 tcg_opt_gen_movi(&gen_args[0], rl, (uint32_t)r);
853 tcg_opt_gen_movi(&gen_args[2], rh, (uint32_t)(r >> 32));
860 case INDEX_op_brcond2_i32:
861 tmp = do_constant_folding_cond2(&args[0], &args[2], args[4]);
864 memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info));
865 gen_opc_buf[op_index] = INDEX_op_br;
866 gen_args[0] = args[5];
869 gen_opc_buf[op_index] = INDEX_op_nop;
871 } else if ((args[4] == TCG_COND_LT || args[4] == TCG_COND_GE)
872 && temps[args[2]].state == TCG_TEMP_CONST
873 && temps[args[3]].state == TCG_TEMP_CONST
874 && temps[args[2]].val == 0
875 && temps[args[3]].val == 0) {
876 /* Simplify LT/GE comparisons vs zero to a single compare
877 vs the high word of the input. */
878 memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info));
879 gen_opc_buf[op_index] = INDEX_op_brcond_i32;
880 gen_args[0] = args[1];
881 gen_args[1] = args[3];
882 gen_args[2] = args[4];
883 gen_args[3] = args[5];
891 case INDEX_op_setcond2_i32:
892 tmp = do_constant_folding_cond2(&args[1], &args[3], args[5]);
894 gen_opc_buf[op_index] = INDEX_op_movi_i32;
895 tcg_opt_gen_movi(gen_args, args[0], tmp);
897 } else if ((args[5] == TCG_COND_LT || args[5] == TCG_COND_GE)
898 && temps[args[3]].state == TCG_TEMP_CONST
899 && temps[args[4]].state == TCG_TEMP_CONST
900 && temps[args[3]].val == 0
901 && temps[args[4]].val == 0) {
902 /* Simplify LT/GE comparisons vs zero to a single compare
903 vs the high word of the input. */
904 gen_opc_buf[op_index] = INDEX_op_setcond_i32;
905 gen_args[0] = args[0];
906 gen_args[1] = args[2];
907 gen_args[2] = args[4];
908 gen_args[3] = args[5];
917 nb_call_args = (args[0] >> 16) + (args[0] & 0xffff);
918 if (!(args[nb_call_args + 1] & (TCG_CALL_CONST | TCG_CALL_PURE))) {
919 for (i = 0; i < nb_globals; i++) {
923 for (i = 0; i < (args[0] >> 16); i++) {
924 reset_temp(args[i + 1]);
926 i = nb_call_args + 3;
937 /* Default case: we know nothing about operation (or were unable
938 to compute the operation result) so no propagation is done.
939 We trash everything if the operation is the end of a basic
940 block, otherwise we only trash the output args. */
941 if (def->flags & TCG_OPF_BB_END) {
942 memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info));
944 for (i = 0; i < def->nb_oargs; i++) {
948 for (i = 0; i < def->nb_args; i++) {
949 gen_args[i] = args[i];
951 args += def->nb_args;
952 gen_args += def->nb_args;
960 TCGArg *tcg_optimize(TCGContext *s, uint16_t *tcg_opc_ptr,
961 TCGArg *args, TCGOpDef *tcg_op_defs)
964 res = tcg_constant_folding(s, tcg_opc_ptr, args, tcg_op_defs);