Skip to contentSkip to conversation

Improve TCG optimizer

PATCH In review Aurelien Jarno 3 participants 17 messages Sep 7, 2012 – Sep 10, 2012

lore
Share & tools
lore
#0 lore raw

v2 cover Improve TCG optimizer

This patch series improves the TCG optimizer, based on patterns found while executing various guest. The brcond ad setcond constant folding are useful especially useful when they are used to avoid some argument values (e.g. division by 0), and thus can be optimized when this argument is a constant.

This bring around 0.5% improvement on openssl like benchmarks.

Modifications between V1 and V2 following feedback I got:
- In the first patch, account for the liveness analysis time and optimizing pass time separately
- Fixed swith/break in patch 7 to correctly throw an error
- Added patch 9 to make the code more readable
Other patches are unmodified.

Aurelien Jarno (9):
tcg: improve profiler
tcg/optimize: split expression simplification tcg/optimize: simplify or/xor r, a, 0 cases tcg/optimize: simplify and r, a, 0 cases tcg/optimize: simplify shift/rot r, 0, a => movi r, 0 cases tcg/optimize: swap brcond/setcond arguments when possible tcg/optimize: add constant folding for setcond tcg/optimize: add constant folding for brcond tcg/optimize: fix if/else/break coding style

 tcg/optimize.c |  179 +++++++++++++++++++++++++++++++++++++++++++++++++++-----
 tcg/tcg.c      |   12 +++-
 tcg/tcg.h      |    1 +
 3 files changed, 175 insertions(+), 17 deletions(-)

--
1.7.10.4

#1 lore raw

PATCH v2 1/9 tcg: improve profiler

Now that there are two passes of optimization (optimize.c, liveness) there is no point of outputing the statistics of the liveness part only. Update the code to take into account both optimizations.

---
 tcg/tcg.c |   12 +++++++++++-
 tcg/tcg.h |    1 +
 2 files changed, 12 insertions(+), 1 deletion(-)
tcg/tcg.c +11 -1
index 8386b70..a4e7f42 100644
@@ -2059,22 +2059,29 @@ static inline int tcg_gen_code_common(TCGContext *s, uint8_t *gen_code_buf,
     }
 #endif
 
+#ifdef CONFIG_PROFILER
+    s->opt_time -= profile_getclock();
+#endif
+
 #ifdef USE_TCG_OPTIMIZATIONS
     gen_opparam_ptr =
         tcg_optimize(s, gen_opc_ptr, gen_opparam_buf, tcg_op_defs);
 #endif
 
 #ifdef CONFIG_PROFILER
+    s->opt_time += profile_getclock();
     s->la_time -= profile_getclock();
 #endif
+
     tcg_liveness_analysis(s);
+
 #ifdef CONFIG_PROFILER
     s->la_time += profile_getclock();
 #endif
 
 #ifdef DEBUG_DISAS
     if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_OPT))) {
-        qemu_log("OP after liveness analysis:\n");
+        qemu_log("OP after optimization and liveness analysis:\n");
         tcg_dump_ops(s);
         qemu_log("\n");
     }
@@ -2241,6 +2248,9 @@ void tcg_dump_info(FILE *f, fprintf_function cpu_fprintf)
                 (double)s->interm_time / tot * 100.0);
     cpu_fprintf(f, "  gen_code time     %0.1f%%\n", 
                 (double)s->code_time / tot * 100.0);
+    cpu_fprintf(f, "optim./code time    %0.1f%%\n",
+                (double)s->opt_time / (s->code_time ? s->code_time : 1)
+                * 100.0);
     cpu_fprintf(f, "liveness/code time  %0.1f%%\n", 
                 (double)s->la_time / (s->code_time ? s->code_time : 1) * 100.0);
     cpu_fprintf(f, "cpu_restore count   %" PRId64 "\n",
tcg/tcg.h +1 -0
index d710694..7a72729 100644
@@ -382,6 +382,7 @@ struct TCGContext {
     int64_t interm_time;
     int64_t code_time;
     int64_t la_time;
+    int64_t opt_time;
     int64_t restore_count;
     int64_t restore_time;
 #endif
Signature · 1 line
1.7.10.4
#2 lore raw

PATCH v2 2/9 tcg/optimize: split expression simplification

In reply to Aurelien Jarno

Split expression simplification in multiple parts so that a given op can appear multiple times. This patch should not change anything.

---
 tcg/optimize.c |   14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
tcg/optimize.c +13 -1
index 9c65474..63f970d 100644
@@ -322,7 +322,7 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
             break;
         }
 
-        /* Simplify expression if possible. */
+        /* Simplify expression for "op r, a, 0 => mov r, a" cases */
         switch (op) {
         CASE_OP_32_64(add):
         CASE_OP_32_64(sub):
@@ -352,6 +352,12 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
                 continue;
             }
             break;
+        default:
+            break;
+        }
+
+        /* Simplify expression for "op r, a, 0 => movi r, 0" cases */
+        switch (op) {
         CASE_OP_32_64(mul):
             if ((temps[args[2]].state == TCG_TEMP_CONST
                 && temps[args[2]].val == 0)) {
@@ -362,6 +368,12 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
                 continue;
             }
             break;
+        default:
+            break;
+        }
+
+        /* Simplify expression for "op r, a, a => mov r, a" cases */
+        switch (op) {
         CASE_OP_32_64(or):
         CASE_OP_32_64(and):
             if (args[1] == args[2]) {
Signature · 1 line
1.7.10.4
#3 lore raw

PATCH v2 3/9 tcg/optimize: simplify or/xor r, a, 0 cases

In reply to Aurelien Jarno

or/xor r, a, 0 is equivalent to a mov r, a.

---
 tcg/optimize.c |    2 ++
 1 file changed, 2 insertions(+)
tcg/optimize.c +2 -0
index 63f970d..0db849e 100644
@@ -331,6 +331,8 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
         CASE_OP_32_64(sar):
         CASE_OP_32_64(rotl):
         CASE_OP_32_64(rotr):
+        CASE_OP_32_64(or):
+        CASE_OP_32_64(xor):
             if (temps[args[1]].state == TCG_TEMP_CONST) {
                 /* Proceed with possible constant folding. */
                 break;
Signature · 1 line
1.7.10.4
#4 lore raw

PATCH v2 4/9 tcg/optimize: simplify and r, a, 0 cases

In reply to Aurelien Jarno

and r, a, 0 is equivalent to a movi r, 0.

---
 tcg/optimize.c |    1 +
 1 file changed, 1 insertion(+)
tcg/optimize.c +1 -0
index 0db849e..c12cb2b 100644
@@ -360,6 +360,7 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
 
         /* Simplify expression for "op r, a, 0 => movi r, 0" cases */
         switch (op) {
+        CASE_OP_32_64(and):
         CASE_OP_32_64(mul):
             if ((temps[args[2]].state == TCG_TEMP_CONST
                 && temps[args[2]].val == 0)) {
Signature · 1 line
1.7.10.4
#5 lore raw

PATCH v2 5/9 tcg/optimize: simplify shift/rot r, 0, a => movi r, 0 cases

In reply to Aurelien Jarno

shift/rot r, 0, a is equivalent to movi r, 0.

---
 tcg/optimize.c |   20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
tcg/optimize.c +20 -0
index c12cb2b..1698ba3 100644
@@ -322,6 +322,26 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
             break;
         }
 
+        /* Simplify expressions for "shift/rot r, 0, a => movi r, 0" */
+        switch (op) {
+        CASE_OP_32_64(shl):
+        CASE_OP_32_64(shr):
+        CASE_OP_32_64(sar):
+        CASE_OP_32_64(rotl):
+        CASE_OP_32_64(rotr):
+            if (temps[args[1]].state == TCG_TEMP_CONST
+                && temps[args[1]].val == 0) {
+                gen_opc_buf[op_index] = op_to_movi(op);
+                tcg_opt_gen_movi(gen_args, args[0], 0, nb_temps, nb_globals);
+                args += 3;
+                gen_args += 2;
+                continue;
+            }
+            break;
+        default:
+            break;
+        }
+
         /* Simplify expression for "op r, a, 0 => mov r, a" cases */
         switch (op) {
         CASE_OP_32_64(add):
Signature · 1 line
1.7.10.4
#6 lore raw

PATCH v2 6/9 tcg/optimize: swap brcond/setcond arguments when possible

In reply to Aurelien Jarno

brcond and setcond ops are not commutative, but it's easy to compute the new condition after swapping the arguments. Try to always put the constant argument in second position like for commutative ops, to help backends to generate better code.

---
 tcg/optimize.c |   18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
tcg/optimize.c +18 -0
index 1698ba3..7debc8a 100644
@@ -318,6 +318,24 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
                 args[2] = tmp;
             }
             break;
+        CASE_OP_32_64(brcond):
+            if (temps[args[0]].state == TCG_TEMP_CONST
+                && temps[args[1]].state != TCG_TEMP_CONST) {
+                tmp = args[0];
+                args[0] = args[1];
+                args[1] = tmp;
+                args[2] = tcg_swap_cond(args[2]);
+            }
+            break;
+        CASE_OP_32_64(setcond):
+            if (temps[args[1]].state == TCG_TEMP_CONST
+                && temps[args[2]].state != TCG_TEMP_CONST) {
+                tmp = args[1];
+                args[1] = args[2];
+                args[2] = tmp;
+                args[3] = tcg_swap_cond(args[3]);
+            }
+            break;
         default:
             break;
         }
Signature · 1 line
1.7.10.4
#7 lore raw

PATCH v2 7/9 tcg/optimize: add constant folding for setcond

In reply to Aurelien Jarno
---
 tcg/optimize.c |   81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 81 insertions(+)
tcg/optimize.c +81 -0
index 7debc8a..1cb1f36 100644
@@ -267,6 +267,67 @@ static TCGArg do_constant_folding(TCGOpcode op, TCGArg x, TCGArg y)
     return res;
 }
 
+static TCGArg do_constant_folding_cond(TCGOpcode op, TCGArg x,
+                                       TCGArg y, TCGCond c)
+{
+    switch (op_bits(op)) {
+    case 32:
+        switch (c) {
+        case TCG_COND_EQ:
+            return (uint32_t)x == (uint32_t)y;
+        case TCG_COND_NE:
+            return (uint32_t)x != (uint32_t)y;
+        case TCG_COND_LT:
+            return (int32_t)x < (int32_t)y;
+        case TCG_COND_GE:
+            return (int32_t)x >= (int32_t)y;
+        case TCG_COND_LE:
+            return (int32_t)x <= (int32_t)y;
+        case TCG_COND_GT:
+            return (int32_t)x > (int32_t)y;
+        case TCG_COND_LTU:
+            return (uint32_t)x < (uint32_t)y;
+        case TCG_COND_GEU:
+            return (uint32_t)x >= (uint32_t)y;
+        case TCG_COND_LEU:
+            return (uint32_t)x <= (uint32_t)y;
+        case TCG_COND_GTU:
+            return (uint32_t)x > (uint32_t)y;
+        }
+        break;
+    case 64:
+        switch (c) {
+        case TCG_COND_EQ:
+            return (uint64_t)x == (uint64_t)y;
+        case TCG_COND_NE:
+            return (uint64_t)x != (uint64_t)y;
+        case TCG_COND_LT:
+            return (int64_t)x < (int64_t)y;
+        case TCG_COND_GE:
+            return (int64_t)x >= (int64_t)y;
+        case TCG_COND_LE:
+            return (int64_t)x <= (int64_t)y;
+        case TCG_COND_GT:
+            return (int64_t)x > (int64_t)y;
+        case TCG_COND_LTU:
+            return (uint64_t)x < (uint64_t)y;
+        case TCG_COND_GEU:
+            return (uint64_t)x >= (uint64_t)y;
+        case TCG_COND_LEU:
+            return (uint64_t)x <= (uint64_t)y;
+        case TCG_COND_GTU:
+            return (uint64_t)x > (uint64_t)y;
+        }
+        break;
+    }
+
+    fprintf(stderr,
+            "Unrecognized bitness %d or condition %d in "
+            "do_constant_folding_cond.\n", op_bits(op), c);
+    tcg_abort();
+}
+
+
 /* Propagate constants and copies, fold constant expressions. */
 static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
                                     TCGArg *args, TCGOpDef *tcg_op_defs)
@@ -522,6 +583,26 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
                 args += 3;
                 break;
             }
+        CASE_OP_32_64(setcond):
+            if (temps[args[1]].state == TCG_TEMP_CONST
+                && temps[args[2]].state == TCG_TEMP_CONST) {
+                gen_opc_buf[op_index] = op_to_movi(op);
+                tmp = do_constant_folding_cond(op, temps[args[1]].val,
+                                               temps[args[2]].val, args[3]);
+                tcg_opt_gen_movi(gen_args, args[0], tmp, nb_temps, nb_globals);
+                gen_args += 2;
+                args += 4;
+                break;
+            } else {
+                reset_temp(args[0], nb_temps, nb_globals);
+                gen_args[0] = args[0];
+                gen_args[1] = args[1];
+                gen_args[2] = args[2];
+                gen_args[3] = args[3];
+                gen_args += 4;
+                args += 4;
+                break;
+            }
         case INDEX_op_call:
             nb_call_args = (args[0] >> 16) + (args[0] & 0xffff);
             if (!(args[nb_call_args + 1] & (TCG_CALL_CONST | TCG_CALL_PURE))) {
Signature · 1 line
1.7.10.4
#8 lore raw

PATCH v2 8/9 tcg/optimize: add constant folding for brcond

In reply to Aurelien Jarno
---
 tcg/optimize.c |   27 ++++++++++++++++++++++++++-
 1 file changed, 26 insertions(+), 1 deletion(-)
tcg/optimize.c +26 -1
index 1cb1f36..156e8d9 100644
@@ -603,6 +603,32 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
                 args += 4;
                 break;
             }
+        CASE_OP_32_64(brcond):
+            if (temps[args[0]].state == TCG_TEMP_CONST
+                && temps[args[1]].state == TCG_TEMP_CONST) {
+                if (do_constant_folding_cond(op, temps[args[0]].val,
+                                             temps[args[1]].val, args[2])) {
+                    memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info));
+                    gen_opc_buf[op_index] = INDEX_op_br;
+                    gen_args[0] = args[3];
+                    gen_args += 1;
+                    args += 4;
+                } else {
+                    gen_opc_buf[op_index] = INDEX_op_nop;
+                    args += 4;
+                }
+                break;
+            } else {
+                memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info));
+                reset_temp(args[0], nb_temps, nb_globals);
+                gen_args[0] = args[0];
+                gen_args[1] = args[1];
+                gen_args[2] = args[2];
+                gen_args[3] = args[3];
+                gen_args += 4;
+                args += 4;
+                break;
+            }
         case INDEX_op_call:
             nb_call_args = (args[0] >> 16) + (args[0] & 0xffff);
             if (!(args[nb_call_args + 1] & (TCG_CALL_CONST | TCG_CALL_PURE))) {
@@ -624,7 +650,6 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
         case INDEX_op_set_label:
         case INDEX_op_jmp:
         case INDEX_op_br:
-        CASE_OP_32_64(brcond):
             memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info));
             for (i = 0; i < def->nb_args; i++) {
                 *gen_args = *args;
Signature · 1 line
1.7.10.4
#9 lore raw

PATCH v2 9/9 tcg/optimize: fix if/else/break coding style

In reply to Aurelien Jarno

optimizer.c contains some cases were the break is appearing in both the if and the else parts. Fix that by moving it to the outer part. Also move some common code there.

---
 tcg/optimize.c |   34 +++++++++++-----------------------
 1 file changed, 11 insertions(+), 23 deletions(-)
tcg/optimize.c +11 -23
index 156e8d9..fba0ed9 100644
@@ -441,15 +441,14 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
                 if ((temps[args[0]].state == TCG_TEMP_COPY
                     && temps[args[0]].val == args[1])
                     || args[0] == args[1]) {
-                    args += 3;
                     gen_opc_buf[op_index] = INDEX_op_nop;
                 } else {
                     gen_opc_buf[op_index] = op_to_mov(op);
                     tcg_opt_gen_mov(s, gen_args, args[0], args[1],
                                     nb_temps, nb_globals);
                     gen_args += 2;
-                    args += 3;
                 }
+                args += 3;
                 continue;
             }
             break;
@@ -480,15 +479,14 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
         CASE_OP_32_64(and):
             if (args[1] == args[2]) {
                 if (args[1] == args[0]) {
-                    args += 3;
                     gen_opc_buf[op_index] = INDEX_op_nop;
                 } else {
                     gen_opc_buf[op_index] = op_to_mov(op);
                     tcg_opt_gen_mov(s, gen_args, args[0], args[1], nb_temps,
                                     nb_globals);
                     gen_args += 2;
-                    args += 3;
                 }
+                args += 3;
                 continue;
             }
             break;
@@ -538,17 +536,14 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
                 gen_opc_buf[op_index] = op_to_movi(op);
                 tmp = do_constant_folding(op, temps[args[1]].val, 0);
                 tcg_opt_gen_movi(gen_args, args[0], tmp, nb_temps, nb_globals);
-                gen_args += 2;
-                args += 2;
-                break;
             } else {
                 reset_temp(args[0], nb_temps, nb_globals);
                 gen_args[0] = args[0];
                 gen_args[1] = args[1];
-                gen_args += 2;
-                args += 2;
-                break;
             }
+            gen_args += 2;
+            args += 2;
+            break;
         CASE_OP_32_64(add):
         CASE_OP_32_64(sub):
         CASE_OP_32_64(mul):
@@ -572,17 +567,15 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
                                           temps[args[2]].val);
                 tcg_opt_gen_movi(gen_args, args[0], tmp, nb_temps, nb_globals);
                 gen_args += 2;
-                args += 3;
-                break;
             } else {
                 reset_temp(args[0], nb_temps, nb_globals);
                 gen_args[0] = args[0];
                 gen_args[1] = args[1];
                 gen_args[2] = args[2];
                 gen_args += 3;
-                args += 3;
-                break;
             }
+            args += 3;
+            break;
         CASE_OP_32_64(setcond):
             if (temps[args[1]].state == TCG_TEMP_CONST
                 && temps[args[2]].state == TCG_TEMP_CONST) {
@@ -591,8 +584,6 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
                                                temps[args[2]].val, args[3]);
                 tcg_opt_gen_movi(gen_args, args[0], tmp, nb_temps, nb_globals);
                 gen_args += 2;
-                args += 4;
-                break;
             } else {
                 reset_temp(args[0], nb_temps, nb_globals);
                 gen_args[0] = args[0];
@@ -600,9 +591,9 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
                 gen_args[2] = args[2];
                 gen_args[3] = args[3];
                 gen_args += 4;
-                args += 4;
-                break;
             }
+            args += 4;
+            break;
         CASE_OP_32_64(brcond):
             if (temps[args[0]].state == TCG_TEMP_CONST
                 && temps[args[1]].state == TCG_TEMP_CONST) {
@@ -612,12 +603,9 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
                     gen_opc_buf[op_index] = INDEX_op_br;
                     gen_args[0] = args[3];
                     gen_args += 1;
-                    args += 4;
                 } else {
                     gen_opc_buf[op_index] = INDEX_op_nop;
-                    args += 4;
                 }
-                break;
             } else {
                 memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info));
                 reset_temp(args[0], nb_temps, nb_globals);
@@ -626,9 +614,9 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
                 gen_args[2] = args[2];
                 gen_args[3] = args[3];
                 gen_args += 4;
-                args += 4;
-                break;
             }
+            args += 4;
+            break;
         case INDEX_op_call:
             nb_call_args = (args[0] >> 16) + (args[0] & 0xffff);
             if (!(args[nb_call_args + 1] & (TCG_CALL_CONST | TCG_CALL_PURE))) {
Signature · 1 line
1.7.10.4
#10 lore raw
In reply to Aurelien Jarno
Quoted 17 lines · Aurelien Jarno

This patch series improves the TCG optimizer, based on patterns found while executing various guest. The brcond ad setcond constant folding are useful especially useful when they are used to avoid some argument values (e.g. division by 0), and thus can be optimized when this argument is a constant.

This bring around 0.5% improvement on openssl like benchmarks.

Modifications between V1 and V2 following feedback I got:
- In the first patch, account for the liveness analysis time and optimizing pass time separately
- Fixed swith/break in patch 7 to correctly throw an error
- Added patch 9 to make the code more readable
Other patches are unmodified.

Quoted 6 lines

Aurelien Jarno (9):
tcg: improve profiler
tcg/optimize: split expression simplification tcg/optimize: simplify or/xor r, a, 0 cases tcg/optimize: simplify and r, a, 0 cases tcg/optimize: simplify shift/rot r, 0, a => movi r, 0 cases

Aren't the above or/and/shift/rot simplifications (and also for example OR with 0xfffffffff and XOR register by itself) already handled by tcg/tcg-op.h?

Quoted 4 lines

tcg/optimize: swap brcond/setcond arguments when possible tcg/optimize: add constant folding for setcond tcg/optimize: add constant folding for brcond tcg/optimize: fix if/else/break coding style

Otherwise a very nice series.

Quoted 10 lines
 tcg/optimize.c |  179 +++++++++++++++++++++++++++++++++++++++++++++++++++-----
 tcg/tcg.c      |   12 +++-
 tcg/tcg.h      |    1 +
 3 files changed, 175 insertions(+), 17 deletions(-)

--
1.7.10.4

#11 lore raw
Quoted 25 lines · Blue Swirl
Quoted 17 lines · Aurelien Jarno

This patch series improves the TCG optimizer, based on patterns found while executing various guest. The brcond ad setcond constant folding are useful especially useful when they are used to avoid some argument values (e.g. division by 0), and thus can be optimized when this argument is a constant.

This bring around 0.5% improvement on openssl like benchmarks.

Modifications between V1 and V2 following feedback I got:
- In the first patch, account for the liveness analysis time and optimizing pass time separately
- Fixed swith/break in patch 7 to correctly throw an error
- Added patch 9 to make the code more readable
Other patches are unmodified.

Quoted 6 lines

Aurelien Jarno (9):
tcg: improve profiler
tcg/optimize: split expression simplification tcg/optimize: simplify or/xor r, a, 0 cases tcg/optimize: simplify and r, a, 0 cases tcg/optimize: simplify shift/rot r, 0, a => movi r, 0 cases

Quoted 3 lines

Aren't the above or/and/shift/rot simplifications (and also for example OR with 0xfffffffff and XOR register by itself) already handled by tcg/tcg-op.h?

They are handled there when the values are known at decode time. It is not the case when the value are propagated in the TB.

For example, this is optimized in tcg/tcg-op.h:
ori t0, t1, 0

This is not optimized in tcg/tcg-op.h:
movi t2, 0
or t0, t1, t2

Quoted 19 lines
Quoted 4 lines

tcg/optimize: swap brcond/setcond arguments when possible tcg/optimize: add constant folding for setcond tcg/optimize: add constant folding for brcond tcg/optimize: fix if/else/break coding style

Otherwise a very nice series.

Quoted 10 lines
 tcg/optimize.c |  179 +++++++++++++++++++++++++++++++++++++++++++++++++++-----
 tcg/tcg.c      |   12 +++-
 tcg/tcg.h      |    1 +
 3 files changed, 175 insertions(+), 17 deletions(-)

--
1.7.10.4

Signature · 2 lines
Aurelien Jarno                          GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net
#12 lore raw
Quoted 33 lines · Aurelien Jarno
Quoted 25 lines · Blue Swirl
Quoted 17 lines · Aurelien Jarno

This patch series improves the TCG optimizer, based on patterns found while executing various guest. The brcond ad setcond constant folding are useful especially useful when they are used to avoid some argument values (e.g. division by 0), and thus can be optimized when this argument is a constant.

This bring around 0.5% improvement on openssl like benchmarks.

Modifications between V1 and V2 following feedback I got:
- In the first patch, account for the liveness analysis time and optimizing pass time separately
- Fixed swith/break in patch 7 to correctly throw an error
- Added patch 9 to make the code more readable
Other patches are unmodified.

Quoted 6 lines

Aurelien Jarno (9):
tcg: improve profiler
tcg/optimize: split expression simplification tcg/optimize: simplify or/xor r, a, 0 cases tcg/optimize: simplify and r, a, 0 cases tcg/optimize: simplify shift/rot r, 0, a => movi r, 0 cases

Quoted 3 lines

Aren't the above or/and/shift/rot simplifications (and also for example OR with 0xfffffffff and XOR register by itself) already handled by tcg/tcg-op.h?

They are handled there when the values are known at decode time. It is not the case when the value are propagated in the TB.

Quoted 6 lines

For example, this is optimized in tcg/tcg-op.h:
ori t0, t1, 0

This is not optimized in tcg/tcg-op.h:
movi t2, 0
or t0, t1, t2

I see. Does the optimizer pass then make the tcg/tcg-op.h optimization redundant, could we do the optimizations only in optimizer?

Quoted 24 lines
Quoted 20 lines
Quoted 5 lines

tcg/optimize: swap brcond/setcond arguments when possible tcg/optimize: add constant folding for setcond tcg/optimize: add constant folding for brcond tcg/optimize: fix if/else/break coding style

Otherwise a very nice series.

Quoted 10 lines
 tcg/optimize.c |  179 +++++++++++++++++++++++++++++++++++++++++++++++++++-----
 tcg/tcg.c      |   12 +++-
 tcg/tcg.h      |    1 +
 3 files changed, 175 insertions(+), 17 deletions(-)

--
1.7.10.4

--
Aurelien Jarno GPG: 1024D/F1BCDB73
[email protected] http://www.aurel32.net

#13 lore raw
Quoted 37 lines · Blue Swirl
Quoted 36 lines · Aurelien Jarno
Quoted 25 lines · Blue Swirl
Quoted 17 lines · Aurelien Jarno

This patch series improves the TCG optimizer, based on patterns found while executing various guest. The brcond ad setcond constant folding are useful especially useful when they are used to avoid some argument values (e.g. division by 0), and thus can be optimized when this argument is a constant.

This bring around 0.5% improvement on openssl like benchmarks.

Modifications between V1 and V2 following feedback I got:
- In the first patch, account for the liveness analysis time and optimizing pass time separately
- Fixed swith/break in patch 7 to correctly throw an error
- Added patch 9 to make the code more readable
Other patches are unmodified.

Quoted 6 lines

Aurelien Jarno (9):
tcg: improve profiler
tcg/optimize: split expression simplification tcg/optimize: simplify or/xor r, a, 0 cases tcg/optimize: simplify and r, a, 0 cases tcg/optimize: simplify shift/rot r, 0, a => movi r, 0 cases

Quoted 3 lines

Aren't the above or/and/shift/rot simplifications (and also for example OR with 0xfffffffff and XOR register by itself) already handled by tcg/tcg-op.h?

They are handled there when the values are known at decode time. It is not the case when the value are propagated in the TB.

For example, this is optimized in tcg/tcg-op.h:
ori t0, t1, 0

Quoted 6 lines
Quoted 3 lines

This is not optimized in tcg/tcg-op.h:
movi t2, 0
or t0, t1, t2

I see. Does the optimizer pass then make the tcg/tcg-op.h optimization redundant, could we do the optimizations only in optimizer?

Technically yes. In practice it's a good idea to keep simple optimizations in tcg/tcg-op.h, as they cost less in CPU time than when done later.

On the other hand, we can remove such optimizations done in some TCG backends as they won't see this kind of ops anymore.

Quoted 25 lines
Quoted 24 lines
Quoted 20 lines
Quoted 5 lines

tcg/optimize: swap brcond/setcond arguments when possible tcg/optimize: add constant folding for setcond tcg/optimize: add constant folding for brcond tcg/optimize: fix if/else/break coding style

Otherwise a very nice series.

Quoted 10 lines
 tcg/optimize.c |  179 +++++++++++++++++++++++++++++++++++++++++++++++++++-----
 tcg/tcg.c      |   12 +++-
 tcg/tcg.h      |    1 +
 3 files changed, 175 insertions(+), 17 deletions(-)

--
1.7.10.4

--
Aurelien Jarno GPG: 1024D/F1BCDB73
[email protected] http://www.aurel32.net

Signature · 2 lines
Aurelien Jarno	                        GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net
#14 lore raw
Quoted 42 lines · Aurelien Jarno
Quoted 41 lines · Blue Swirl
Quoted 39 lines · Aurelien Jarno
Quoted 25 lines · Blue Swirl
Quoted 17 lines · Aurelien Jarno

This patch series improves the TCG optimizer, based on patterns found while executing various guest. The brcond ad setcond constant folding are useful especially useful when they are used to avoid some argument values (e.g. division by 0), and thus can be optimized when this argument is a constant.

This bring around 0.5% improvement on openssl like benchmarks.

Modifications between V1 and V2 following feedback I got:
- In the first patch, account for the liveness analysis time and optimizing pass time separately
- Fixed swith/break in patch 7 to correctly throw an error
- Added patch 9 to make the code more readable
Other patches are unmodified.

Quoted 6 lines

Aurelien Jarno (9):
tcg: improve profiler
tcg/optimize: split expression simplification tcg/optimize: simplify or/xor r, a, 0 cases tcg/optimize: simplify and r, a, 0 cases tcg/optimize: simplify shift/rot r, 0, a => movi r, 0 cases

Quoted 3 lines

Aren't the above or/and/shift/rot simplifications (and also for example OR with 0xfffffffff and XOR register by itself) already handled by tcg/tcg-op.h?

They are handled there when the values are known at decode time. It is not the case when the value are propagated in the TB.

For example, this is optimized in tcg/tcg-op.h:
ori t0, t1, 0

This is not optimized in tcg/tcg-op.h:
movi t2, 0
or t0, t1, t2

Quoted 6 lines
Quoted 2 lines

I see. Does the optimizer pass then make the tcg/tcg-op.h optimization redundant, could we do the optimizations only in optimizer?

Technically yes. In practice it's a good idea to keep simple optimizations in tcg/tcg-op.h, as they cost less in CPU time than when done later.

OK. Could there be further optimizations based on tcg/tcg-op.h, for example case OR reg, 0xffffffff -> mov reg, 0xffffffff could be rechecked?

Quoted 33 lines

On the other hand, we can remove such optimizations done in some TCG backends as they won't see this kind of ops anymore.

Quoted 21 lines
Quoted 21 lines
Quoted 20 lines
Quoted 5 lines

tcg/optimize: swap brcond/setcond arguments when possible tcg/optimize: add constant folding for setcond tcg/optimize: add constant folding for brcond tcg/optimize: fix if/else/break coding style

Otherwise a very nice series.

Quoted 10 lines
 tcg/optimize.c |  179 +++++++++++++++++++++++++++++++++++++++++++++++++++-----
 tcg/tcg.c      |   12 +++-
 tcg/tcg.h      |    1 +
 3 files changed, 175 insertions(+), 17 deletions(-)

--
1.7.10.4

Quoted 4 lines
Quoted 3 lines

--
Aurelien Jarno GPG: 1024D/F1BCDB73
[email protected] http://www.aurel32.net

--
Aurelien Jarno GPG: 1024D/F1BCDB73
[email protected] http://www.aurel32.net

#15 lore raw
Quoted 46 lines · Blue Swirl
Quoted 45 lines · Aurelien Jarno
Quoted 43 lines · Blue Swirl
Quoted 33 lines · Aurelien Jarno
Quoted 25 lines · Blue Swirl
Quoted 17 lines · Aurelien Jarno

This patch series improves the TCG optimizer, based on patterns found while executing various guest. The brcond ad setcond constant folding are useful especially useful when they are used to avoid some argument values (e.g. division by 0), and thus can be optimized when this argument is a constant.

This bring around 0.5% improvement on openssl like benchmarks.

Modifications between V1 and V2 following feedback I got:
- In the first patch, account for the liveness analysis time and optimizing pass time separately
- Fixed swith/break in patch 7 to correctly throw an error
- Added patch 9 to make the code more readable
Other patches are unmodified.

Quoted 6 lines

Aurelien Jarno (9):
tcg: improve profiler
tcg/optimize: split expression simplification tcg/optimize: simplify or/xor r, a, 0 cases tcg/optimize: simplify and r, a, 0 cases tcg/optimize: simplify shift/rot r, 0, a => movi r, 0 cases

Quoted 3 lines

Aren't the above or/and/shift/rot simplifications (and also for example OR with 0xfffffffff and XOR register by itself) already handled by tcg/tcg-op.h?

They are handled there when the values are known at decode time. It is not the case when the value are propagated in the TB.

Quoted 6 lines

For example, this is optimized in tcg/tcg-op.h:
ori t0, t1, 0

This is not optimized in tcg/tcg-op.h:
movi t2, 0
or t0, t1, t2

I see. Does the optimizer pass then make the tcg/tcg-op.h optimization redundant, could we do the optimizations only in optimizer?

Quoted 8 lines
Quoted 3 lines

Technically yes. In practice it's a good idea to keep simple optimizations in tcg/tcg-op.h, as they cost less in CPU time than when done later.

OK. Could there be further optimizations based on tcg/tcg-op.h, for example case OR reg, 0xffffffff -> mov reg, 0xffffffff could be rechecked?

Yes this is something we can add. That said I based this patch series on instructions I have found while running a few targets (arm, ppc, mips, x86_64) and looking at qemu.log. I haven't seen this one.

Signature · 2 lines
Aurelien Jarno                          GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net
Reviewed-by on the v2 series
#16 lore raw
In reply to Aurelien Jarno
Quoted 10 lines · Aurelien Jarno

Aurelien Jarno (9):
tcg: improve profiler
tcg/optimize: split expression simplification tcg/optimize: simplify or/xor r, a, 0 cases tcg/optimize: simplify and r, a, 0 cases tcg/optimize: simplify shift/rot r, 0, a => movi r, 0 cases tcg/optimize: swap brcond/setcond arguments when possible tcg/optimize: add constant folding for setcond tcg/optimize: add constant folding for brcond tcg/optimize: fix if/else/break coding style

r~