Skip to contentSkip to conversation

Double-word tcg/optimize improvements

PATCH Superseded Richard Henderson 3 participants 21 messages Sep 27, 2012 – Oct 1, 2012

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#1 lore raw

PATCH 1/7 tcg: Split out swap_commutative as a subroutine

In reply to Richard Henderson

Reduces code duplication and prefers

movcond d, c1, c2, const, s
to
movcond d, c1, c2, s, const

---
 tcg/optimize.c | 54 ++++++++++++++++++++++--------------------------------
 1 file changed, 22 insertions(+), 32 deletions(-)
tcg/optimize.c +22 -32
index 35532a1..55f2a24 100644
@@ -382,6 +382,21 @@ static TCGArg do_constant_folding_cond(TCGOpcode op, TCGArg x,
     tcg_abort();
 }
 
+static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2)
+{
+    TCGArg a1 = *p1, a2 = *p2;
+    /* Prefer the constant in second argument, and then the form
+       op a, a, b, which is better handled on non-RISC hosts. */
+    if (temps[a1].state == TCG_TEMP_CONST
+        || (dest != (TCGArg)-1 && dest == a2
+            && temps[a2].state != TCG_TEMP_CONST)) {
+        *p1 = a2;
+        *p2 = a1;
+        return true;
+    }
+    return false;
+}
+
 /* 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)
@@ -391,7 +406,6 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
     const TCGOpDef *def;
     TCGArg *gen_args;
     TCGArg tmp;
-    TCGCond cond;
 
     /* Array VALS has an element for each temp.
        If this temp holds a constant then its value is kept in VALS' element.
@@ -434,52 +448,28 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
         CASE_OP_32_64(eqv):
         CASE_OP_32_64(nand):
         CASE_OP_32_64(nor):
-            /* Prefer the constant in second argument, and then the form
-               op a, a, b, which is better handled on non-RISC hosts. */
-            if (temps[args[1]].state == TCG_TEMP_CONST || (args[0] == args[2]
-                && temps[args[2]].state != TCG_TEMP_CONST)) {
-                tmp = args[1];
-                args[1] = args[2];
-                args[2] = tmp;
-            }
+            swap_commutative(args[0], &args[1], &args[2]);
             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;
+            if (swap_commutative(-1, &args[0], &args[1])) {
                 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;
+            if (swap_commutative(args[0], &args[1], &args[2])) {
                 args[3] = tcg_swap_cond(args[3]);
             }
             break;
         CASE_OP_32_64(movcond):
-            cond = args[5];
-            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;
-                cond = tcg_swap_cond(cond);
+            if (swap_commutative(-1, &args[1], &args[2])) {
+                args[5] = tcg_swap_cond(args[5]);
             }
             /* For movcond, we canonicalize the "false" input reg to match
                the destination reg so that the tcg backend can implement
                a "move if true" operation.  */
-            if (args[0] == args[3]) {
-                tmp = args[3];
-                args[3] = args[4];
-                args[4] = tmp;
-                cond = tcg_invert_cond(cond);
+            if (swap_commutative(args[0], &args[4], &args[3])) {
+                args[5] = tcg_invert_cond(args[5]);
             }
-            args[5] = cond;
         default:
             break;
         }
Signature · 1 line
1.7.11.4
#0 lore raw

cover Double-word tcg/optimize improvements

In another thread we talked about doing the brcond2 -> brcond optimization generically for LT/GTE comparisons vs zero. That's patch 4 of this series. In addition, it seemed trivial to go ahead and apply the existing code we have for fully optimizing constant comparisons.

Tested host i686, guest alpha. Where even the final patch activates during the boot rom. (FYI, implver is sorta like cpuid, querying cpu revision.)

0xfffffc0000003748:  implver    t0
0xfffffc000000374c:  cmpeq      t0,0x1,t1
0xfffffc0000003750:  bne        t1,0x40000003784

OP:
---- 0xfffffc0000003748
movi_i32 ir1_0,$0x2
movi_i32 ir1_1,$0x0

---- 0xfffffc000000374c
movi_i32 tmp0,$0x1
movi_i32 tmp1,$0x0
setcond2_i32 ir2_0,ir1_0,ir1_1,tmp0,tmp1,eq
movi_i32 ir2_1,$0x0

---- 0xfffffc0000003750
mov_i32 tmp0,ir2_0
mov_i32 tmp1,ir2_1
movi_i32 tmp2,$0x0
movi_i32 tmp3,$0x0
brcond2_i32 tmp0,tmp1,tmp2,tmp3,ne,$0x0
...

OP after optimization and liveness analysis:
---- 0xfffffc0000003748
movi_i32 ir1_0,$0x2
movi_i32 ir1_1,$0x0

---- 0xfffffc000000374c
nopn $0x2,$0x2
nopn $0x2,$0x2
movi_i32 ir2_0,$0x0
movi_i32 ir2_1,$0x0

---- 0xfffffc0000003750
nopn $0x2,$0x2
nopn $0x2,$0x2
nopn $0x2,$0x2
nopn $0x2,$0x2
nop
...

r~

Richard Henderson (7):
tcg: Split out swap_commutative as a subroutine
tcg: Optimize add2 + sub2
tcg: Swap commutative double-word comparisons
tcg: Optimize double-word comparisons against zero
tcg: Split out subroutines from do_constant_folding_cond
tcg: Tidy brcond optimization
tcg: Do constant folding on double-word comparisons

 tcg/optimize.c | 366 +++++++++++++++++++++++++++++++++++++++++----------------
 1 file changed, 267 insertions(+), 99 deletions(-)
Signature · 1 line
1.7.11.4
#3 lore raw

PATCH 2/7 tcg: Optimize add2 + sub2

We can't do complete constant folding because we lack "mov2", or the ability to insert opcodes in the stream. But we can at least canonicalize add2 operand ordering and simplify add2 to add when the lowpart adds a constant 0.

---
 tcg/optimize.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
tcg/optimize.c +31 -0
index 55f2a24..004c336 100644
@@ -470,6 +470,11 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
             if (swap_commutative(args[0], &args[4], &args[3])) {
                 args[5] = tcg_invert_cond(args[5]);
             }
+            break;
+        case INDEX_op_add2_i32:
+            swap_commutative(args[0], &args[2], &args[4]);
+            swap_commutative(args[1], &args[3], &args[5]);
+            break;
         default:
             break;
         }
@@ -522,6 +527,32 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
                 continue;
             }
             break;
+        case INDEX_op_add2_i32:
+        case INDEX_op_sub2_i32:
+            /* Simplify op rl, rh, al, ah, 0, bh => op rh, ah, bh.
+               The zero implies there will be no carry into the high part.
+               But only when rl == al, since we can't insert the extra move
+               that would be required.  */
+            if (temps[args[4]].state == TCG_TEMP_CONST
+                && temps[args[4]].val == 0
+                && temps_are_copies(args[0], args[2])) {
+                if (temps[args[5]].state == TCG_TEMP_CONST
+                    && temps[args[5]].val == 0
+                    && temps_are_copies(args[1], args[3])) {
+                    gen_opc_buf[op_index] = INDEX_op_nop;
+                } else {
+                    gen_opc_buf[op_index] = (op == INDEX_op_add2_i32
+                                             ? INDEX_op_add_i32
+                                             : INDEX_op_sub_i32);
+                    args[0] = args[1];
+                    args[1] = args[3];
+                    args[2] = args[5];
+                    gen_args += 3;
+                }
+                args += 6;
+                continue;
+            }
+            break;
         default:
             break;
         }
Signature · 1 line
1.7.11.4
#12 lore raw

PATCH 4/7 tcg: Optimize double-word comparisons against zero

In reply to Richard Henderson
---
 tcg/optimize.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)
tcg/optimize.c +51 -0
index d39926e..c972e4f 100644
@@ -799,6 +799,57 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
             }
             args += 6;
             break;
+        case INDEX_op_brcond2_i32:
+            /* Simplify LT/GE comparisons vs zero to a single compare
+               vs the high word of the input.  */
+            if ((args[4] == TCG_COND_LT || args[4] == TCG_COND_GE)
+                && temps[args[2]].state == TCG_TEMP_CONST
+                && temps[args[3]].state == TCG_TEMP_CONST
+                && temps[args[2]].val == 0
+                && temps[args[2]].val == 0) {
+                gen_opc_buf[op_index] = INDEX_op_brcond_i32;
+                args[0] = args[1];
+                args[1] = args[3];
+                args[2] = args[4];
+                args[3] = args[5];
+                gen_args += 4;
+            } else {
+                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];
+                gen_args[5] = args[5];
+                gen_args += 6;
+            }
+            memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info));
+            args += 6;
+            break;
+        case INDEX_op_setcond2_i32:
+            /* Simplify LT/GE comparisons vs zero to a single compare
+               vs the high word of the input.  */
+            if ((args[5] == TCG_COND_LT || args[5] == TCG_COND_GE)
+                && temps[args[3]].state == TCG_TEMP_CONST
+                && temps[args[4]].state == TCG_TEMP_CONST
+                && temps[args[3]].val == 0
+                && temps[args[4]].val == 0) {
+                gen_opc_buf[op_index] = INDEX_op_setcond_i32;
+                args[1] = args[2];
+                args[2] = args[4];
+                args[3] = args[5];
+                gen_args += 4;
+            } else {
+                reset_temp(args[0]);
+                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];
+                gen_args[5] = args[5];
+                gen_args += 6;
+            }
+            args += 6;
+            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.11.4
#19 lore raw

PATCH 7/7 tcg: Do constant folding on double-word comparisons

In reply to Richard Henderson
---
 tcg/optimize.c | 134 ++++++++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 94 insertions(+), 40 deletions(-)
tcg/optimize.c +94 -40
index dfac877..f6a16fd 100644
@@ -398,6 +398,40 @@ static TCGArg do_constant_folding_cond(TCGOpcode op, TCGArg x,
     }
 }
 
+/* Return 2 if the condition can't be simplified, and the result
+   of the condition (0 or 1) if it can */
+static TCGArg do_constant_folding_cond2(TCGArg *p1, TCGArg *p2, TCGCond c)
+{
+    TCGArg al = p1[0], ah = p1[1];
+    TCGArg bl = p2[0], bh = p2[1];
+
+    if (temps[bl].state == TCG_TEMP_CONST
+        && temps[bh].state == TCG_TEMP_CONST) {
+        uint64_t b = ((uint64_t)temps[bh].val << 32) | (uint32_t)temps[bl].val;
+
+        if (temps[al].state == TCG_TEMP_CONST
+            && temps[ah].state == TCG_TEMP_CONST) {
+            uint64_t a;
+            a = ((uint64_t)temps[ah].val << 32) | (uint32_t)temps[al].val;
+            return do_constant_folding_cond_64(a, b, c);
+        }
+        if (b == 0) {
+            switch (c) {
+            case TCG_COND_LTU:
+                return 0;
+            case TCG_COND_GEU:
+                return 1;
+            default:
+                break;
+            }
+        }
+    }
+    if (temps_are_copies(al, bl) && temps_are_copies(ah, bh)) {
+        return do_constant_folding_cond_eq(c);
+    }
+    return 2;
+}
+
 static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2)
 {
     TCGArg a1 = *p1, a2 = *p2;
@@ -816,53 +850,73 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
             args += 6;
             break;
         case INDEX_op_brcond2_i32:
-            /* Simplify LT/GE comparisons vs zero to a single compare
-               vs the high word of the input.  */
-            if ((args[4] == TCG_COND_LT || args[4] == TCG_COND_GE)
-                && temps[args[2]].state == TCG_TEMP_CONST
-                && temps[args[3]].state == TCG_TEMP_CONST
-                && temps[args[2]].val == 0
-                && temps[args[2]].val == 0) {
-                gen_opc_buf[op_index] = INDEX_op_brcond_i32;
-                args[0] = args[1];
-                args[1] = args[3];
-                args[2] = args[4];
-                args[3] = args[5];
-                gen_args += 4;
+            tmp = do_constant_folding_cond2(&args[0], &args[2], args[4]);
+            if (tmp != 2) {
+                if (tmp) {
+                    gen_opc_buf[op_index] = INDEX_op_br;
+                    gen_args[0] = args[5];
+                    gen_args += 1;
+                } else {
+                    gen_opc_buf[op_index] = INDEX_op_nop;
+                    args += 6;
+                    break;
+                }
             } else {
-                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];
-                gen_args[5] = args[5];
-                gen_args += 6;
+                /* Simplify LT/GE comparisons vs zero to a single compare
+                   vs the high word of the input.  */
+                if ((args[4] == TCG_COND_LT || args[4] == TCG_COND_GE)
+                    && temps[args[2]].state == TCG_TEMP_CONST
+                    && temps[args[3]].state == TCG_TEMP_CONST
+                    && temps[args[2]].val == 0
+                    && temps[args[2]].val == 0) {
+                    gen_opc_buf[op_index] = INDEX_op_brcond_i32;
+                    args[0] = args[1];
+                    args[1] = args[3];
+                    args[2] = args[4];
+                    args[3] = args[5];
+                    gen_args += 4;
+                } else {
+                    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];
+                    gen_args[5] = args[5];
+                    gen_args += 6;
+                }
             }
             memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info));
             args += 6;
             break;
         case INDEX_op_setcond2_i32:
-            /* Simplify LT/GE comparisons vs zero to a single compare
-               vs the high word of the input.  */
-            if ((args[5] == TCG_COND_LT || args[5] == TCG_COND_GE)
-                && temps[args[3]].state == TCG_TEMP_CONST
-                && temps[args[4]].state == TCG_TEMP_CONST
-                && temps[args[3]].val == 0
-                && temps[args[4]].val == 0) {
-                gen_opc_buf[op_index] = INDEX_op_setcond_i32;
-                args[1] = args[2];
-                args[2] = args[4];
-                args[3] = args[5];
-                gen_args += 4;
+            tmp = do_constant_folding_cond2(&args[1], &args[3], args[5]);
+            if (tmp != 2) {
+                gen_opc_buf[op_index] = INDEX_op_movi_i32;
+                tcg_opt_gen_movi(gen_args, args[0], tmp);
+                gen_args += 2;
             } else {
-                reset_temp(args[0]);
-                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];
-                gen_args[5] = args[5];
-                gen_args += 6;
+                /* Simplify LT/GE comparisons vs zero to a single compare
+                   vs the high word of the input.  */
+                if ((args[5] == TCG_COND_LT || args[5] == TCG_COND_GE)
+                    && temps[args[3]].state == TCG_TEMP_CONST
+                    && temps[args[4]].state == TCG_TEMP_CONST
+                    && temps[args[3]].val == 0
+                    && temps[args[4]].val == 0) {
+                    gen_opc_buf[op_index] = INDEX_op_setcond_i32;
+                    args[1] = args[2];
+                    args[2] = args[4];
+                    args[3] = args[5];
+                    gen_args += 4;
+                } else {
+                    reset_temp(args[0]);
+                    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];
+                    gen_args[5] = args[5];
+                    gen_args += 6;
+                }
             }
             args += 6;
             break;
Signature · 1 line
1.7.11.4
#17 lore raw

PATCH 6/7 tcg: Tidy brcond optimization

In reply to Richard Henderson

Do the memset once. Don't reset_temp before doing so.

---
 tcg/optimize.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tcg/optimize.c +3 -3
index c1881fa..dfac877 100644
@@ -771,22 +771,22 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
             tmp = do_constant_folding_cond(op, args[0], args[1], args[2]);
             if (tmp != 2) {
                 if (tmp) {
-                    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;
                 } 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]);
                 gen_args[0] = args[0];
                 gen_args[1] = args[1];
                 gen_args[2] = args[2];
                 gen_args[3] = args[3];
                 gen_args += 4;
             }
+            memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info));
             args += 4;
             break;
         CASE_OP_32_64(movcond):
Signature · 1 line
1.7.11.4
#10 lore raw

PATCH 3/7 tcg: Swap commutative double-word comparisons

In reply to Richard Henderson
---
 tcg/optimize.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
tcg/optimize.c +26 -0
index 004c336..d39926e 100644
@@ -397,6 +397,22 @@ static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2)
     return false;
 }
 
+static bool swap_commutative2(TCGArg *p1, TCGArg *p2)
+{
+    int sum = 0;
+    sum += temps[p1[0]].state == TCG_TEMP_CONST;
+    sum += temps[p1[1]].state == TCG_TEMP_CONST;
+    sum -= temps[p2[0]].state == TCG_TEMP_CONST;
+    sum -= temps[p2[1]].state == TCG_TEMP_CONST;
+    if (sum > 0) {
+        TCGArg t;
+        t = p1[0], p1[0] = p2[0], p2[0] = t;
+        t = p1[1], p1[1] = p2[1], p2[1] = t;
+        return true;
+    }
+    return false;
+}
+
 /* 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)
@@ -475,6 +491,16 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
             swap_commutative(args[0], &args[2], &args[4]);
             swap_commutative(args[1], &args[3], &args[5]);
             break;
+        case INDEX_op_brcond2_i32:
+            if (swap_commutative2(&args[0], &args[2])) {
+                args[4] = tcg_swap_cond(args[4]);
+            }
+            break;
+        case INDEX_op_setcond2_i32:
+            if (swap_commutative2(&args[1], &args[3])) {
+                args[5] = tcg_swap_cond(args[5]);
+            }
+            break;
         default:
             break;
         }
Signature · 1 line
1.7.11.4
#15 lore raw

PATCH 5/7 tcg: Split out subroutines from do_constant_folding_cond

In reply to Richard Henderson

We can re-use these for implementing double-word folding.

---
 tcg/optimize.c | 146 ++++++++++++++++++++++++++++++++-------------------------
 1 file changed, 81 insertions(+), 65 deletions(-)
tcg/optimize.c +81 -65
index c972e4f..c1881fa 100644
@@ -292,6 +292,82 @@ static TCGArg do_constant_folding(TCGOpcode op, TCGArg x, TCGArg y)
     return res;
 }
 
+static bool do_constant_folding_cond_32(uint32_t x, uint32_t y, TCGCond c)
+{
+    switch (c) {
+    case TCG_COND_EQ:
+        return x == y;
+    case TCG_COND_NE:
+        return x != 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 x < y;
+    case TCG_COND_GEU:
+        return x >= y;
+    case TCG_COND_LEU:
+        return x <= y;
+    case TCG_COND_GTU:
+        return x > y;
+    default:
+        tcg_abort();
+    }
+}
+
+static bool do_constant_folding_cond_64(uint64_t x, uint64_t y, TCGCond c)
+{
+    switch (c) {
+    case TCG_COND_EQ:
+        return x == y;
+    case TCG_COND_NE:
+        return x != 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 x < y;
+    case TCG_COND_GEU:
+        return x >= y;
+    case TCG_COND_LEU:
+        return x <= y;
+    case TCG_COND_GTU:
+        return x > y;
+    default:
+        tcg_abort();
+    }
+}
+
+static bool do_constant_folding_cond_eq(TCGCond c)
+{
+    switch (c) {
+    case TCG_COND_GT:
+    case TCG_COND_LTU:
+    case TCG_COND_LT:
+    case TCG_COND_GTU:
+    case TCG_COND_NE:
+        return 0;
+    case TCG_COND_GE:
+    case TCG_COND_GEU:
+    case TCG_COND_LE:
+    case TCG_COND_LEU:
+    case TCG_COND_EQ:
+        return 1;
+    default:
+        tcg_abort();
+    }
+}
+
 /* Return 2 if the condition can't be simplified, and the result
    of the condition (0 or 1) if it can */
 static TCGArg do_constant_folding_cond(TCGOpcode op, TCGArg x,
@@ -300,69 +376,14 @@ static TCGArg do_constant_folding_cond(TCGOpcode op, TCGArg x,
     if (temps[x].state == TCG_TEMP_CONST && temps[y].state == TCG_TEMP_CONST) {
         switch (op_bits(op)) {
         case 32:
-            switch (c) {
-            case TCG_COND_EQ:
-                return (uint32_t)temps[x].val == (uint32_t)temps[y].val;
-            case TCG_COND_NE:
-                return (uint32_t)temps[x].val != (uint32_t)temps[y].val;
-            case TCG_COND_LT:
-                return (int32_t)temps[x].val < (int32_t)temps[y].val;
-            case TCG_COND_GE:
-                return (int32_t)temps[x].val >= (int32_t)temps[y].val;
-            case TCG_COND_LE:
-                return (int32_t)temps[x].val <= (int32_t)temps[y].val;
-            case TCG_COND_GT:
-                return (int32_t)temps[x].val > (int32_t)temps[y].val;
-            case TCG_COND_LTU:
-                return (uint32_t)temps[x].val < (uint32_t)temps[y].val;
-            case TCG_COND_GEU:
-                return (uint32_t)temps[x].val >= (uint32_t)temps[y].val;
-            case TCG_COND_LEU:
-                return (uint32_t)temps[x].val <= (uint32_t)temps[y].val;
-            case TCG_COND_GTU:
-                return (uint32_t)temps[x].val > (uint32_t)temps[y].val;
-            }
-            break;
+            return do_constant_folding_cond_32(temps[x].val, temps[y].val, c);
         case 64:
-            switch (c) {
-            case TCG_COND_EQ:
-                return (uint64_t)temps[x].val == (uint64_t)temps[y].val;
-            case TCG_COND_NE:
-                return (uint64_t)temps[x].val != (uint64_t)temps[y].val;
-            case TCG_COND_LT:
-                return (int64_t)temps[x].val < (int64_t)temps[y].val;
-            case TCG_COND_GE:
-                return (int64_t)temps[x].val >= (int64_t)temps[y].val;
-            case TCG_COND_LE:
-                return (int64_t)temps[x].val <= (int64_t)temps[y].val;
-            case TCG_COND_GT:
-                return (int64_t)temps[x].val > (int64_t)temps[y].val;
-            case TCG_COND_LTU:
-                return (uint64_t)temps[x].val < (uint64_t)temps[y].val;
-            case TCG_COND_GEU:
-                return (uint64_t)temps[x].val >= (uint64_t)temps[y].val;
-            case TCG_COND_LEU:
-                return (uint64_t)temps[x].val <= (uint64_t)temps[y].val;
-            case TCG_COND_GTU:
-                return (uint64_t)temps[x].val > (uint64_t)temps[y].val;
-            }
-            break;
+            return do_constant_folding_cond_64(temps[x].val, temps[y].val, c);
+        default:
+            tcg_abort();
         }
     } else if (temps_are_copies(x, y)) {
-        switch (c) {
-        case TCG_COND_GT:
-        case TCG_COND_LTU:
-        case TCG_COND_LT:
-        case TCG_COND_GTU:
-        case TCG_COND_NE:
-            return 0;
-        case TCG_COND_GE:
-        case TCG_COND_GEU:
-        case TCG_COND_LE:
-        case TCG_COND_LEU:
-        case TCG_COND_EQ:
-            return 1;
-        }
+        return do_constant_folding_cond_eq(c);
     } else if (temps[y].state == TCG_TEMP_CONST && temps[y].val == 0) {
         switch (c) {
         case TCG_COND_LTU:
@@ -375,11 +396,6 @@ static TCGArg do_constant_folding_cond(TCGOpcode op, TCGArg x,
     } else {
         return 2;
     }
-
-    fprintf(stderr,
-            "Unrecognized bitness %d or condition %d in "
-            "do_constant_folding_cond.\n", op_bits(op), c);
-    tcg_abort();
 }
 
 static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2)
Signature · 1 line
1.7.11.4
Reviewed-by on v1 1/7
#2 lore raw

Re: [Qemu-devel] [PATCH 1/7] tcg: Split out swap_commutative as a subroutine

In reply to Richard Henderson
Quoted 95 lines · Richard Henderson

Reduces code duplication and prefers

movcond d, c1, c2, const, s
to
movcond d, c1, c2, s, const

---
 tcg/optimize.c | 54 ++++++++++++++++++++++--------------------------------
 1 file changed, 22 insertions(+), 32 deletions(-)
tcg/optimize.c +20 -28
index 35532a1..55f2a24 100644
@@ -382,6 +382,21 @@ static TCGArg do_constant_folding_cond(TCGOpcode op, TCGArg x,
     tcg_abort();
 }
 
+static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2)
+{
+    TCGArg a1 = *p1, a2 = *p2;
+    /* Prefer the constant in second argument, and then the form
+       op a, a, b, which is better handled on non-RISC hosts. */
+    if (temps[a1].state == TCG_TEMP_CONST
+        || (dest != (TCGArg)-1 && dest == a2
+            && temps[a2].state != TCG_TEMP_CONST)) {
+        *p1 = a2;
+        *p2 = a1;
+        return true;
+    }
+    return false;
+}
+
 /* 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)
@@ -391,7 +406,6 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
     const TCGOpDef *def;
     TCGArg *gen_args;
     TCGArg tmp;
-    TCGCond cond;
 
     /* Array VALS has an element for each temp.
        If this temp holds a constant then its value is kept in VALS' element.
@@ -434,52 +448,28 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
         CASE_OP_32_64(eqv):
         CASE_OP_32_64(nand):
         CASE_OP_32_64(nor):
-            /* Prefer the constant in second argument, and then the form
-               op a, a, b, which is better handled on non-RISC hosts. */
-            if (temps[args[1]].state == TCG_TEMP_CONST || (args[0] == args[2]
-                && temps[args[2]].state != TCG_TEMP_CONST)) {
-                tmp = args[1];
-                args[1] = args[2];
-                args[2] = tmp;
-            }
+            swap_commutative(args[0], &args[1], &args[2]);
             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;
+            if (swap_commutative(-1, &args[0], &args[1])) {
                 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;
+            if (swap_commutative(args[0], &args[1], &args[2])) {
                 args[3] = tcg_swap_cond(args[3]);
             }
             break;
         CASE_OP_32_64(movcond):
-            cond = args[5];
-            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;
-                cond = tcg_swap_cond(cond);
+            if (swap_commutative(-1, &args[1], &args[2])) {
+                args[5] = tcg_swap_cond(args[5]);
             }
             /* For movcond, we canonicalize the "false" input reg to match
                the destination reg so that the tcg backend can implement
                a "move if true" operation.  */
-            if (args[0] == args[3]) {
-                tmp = args[3];

tcg/optimize.c static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr, · line 475

Quoted 10 lines
tcg/optimize.c +2 -4
-                args[3] = args[4];
-                args[4] = tmp;
-                cond = tcg_invert_cond(cond);
+            if (swap_commutative(args[0], &args[4], &args[3])) {
+                args[5] = tcg_invert_cond(args[5]);
             }
-            args[5] = cond;
         default:
             break;
         }
Signature · 2 lines
Aurelien Jarno                          GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net
#4 lore raw

Re: [Qemu-devel] [PATCH 2/7] tcg: Optimize add2 + sub2

In reply to Richard Henderson
Quoted 62 lines · Richard Henderson

We can't do complete constant folding because we lack "mov2", or the ability to insert opcodes in the stream. But we can at least canonicalize add2 operand ordering and simplify add2 to add when the lowpart adds a constant 0.

---
 tcg/optimize.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
tcg/optimize.c +31 -0
index 55f2a24..004c336 100644
@@ -470,6 +470,11 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
             if (swap_commutative(args[0], &args[4], &args[3])) {
                 args[5] = tcg_invert_cond(args[5]);
             }
+            break;
+        case INDEX_op_add2_i32:
+            swap_commutative(args[0], &args[2], &args[4]);
+            swap_commutative(args[1], &args[3], &args[5]);
+            break;
         default:
             break;
         }
@@ -522,6 +527,32 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
                 continue;
             }
             break;
+        case INDEX_op_add2_i32:
+        case INDEX_op_sub2_i32:
+            /* Simplify op rl, rh, al, ah, 0, bh => op rh, ah, bh.
+               The zero implies there will be no carry into the high part.
+               But only when rl == al, since we can't insert the extra move
+               that would be required.  */
+            if (temps[args[4]].state == TCG_TEMP_CONST
+                && temps[args[4]].val == 0
+                && temps_are_copies(args[0], args[2])) {
+                if (temps[args[5]].state == TCG_TEMP_CONST
+                    && temps[args[5]].val == 0
+                    && temps_are_copies(args[1], args[3])) {
+                    gen_opc_buf[op_index] = INDEX_op_nop;
+                } else {
+                    gen_opc_buf[op_index] = (op == INDEX_op_add2_i32
+                                             ? INDEX_op_add_i32
+                                             : INDEX_op_sub_i32);
+                    args[0] = args[1];
+                    args[1] = args[3];
+                    args[2] = args[5];
+                    gen_args += 3;
+                }
+                args += 6;
+                continue;
+            }
+            break;
         default:
             break;
         }
Signature · 1 line
1.7.11.4

I understand that we can't easily insert an instruction, so the limitation comes from here, but is it really something happening often? Doing an optimization has a CPU cost, so if it is not used often, it might be worse than without.

Signature · 2 lines
Aurelien Jarno                          GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net
Reviewed-by on v1 3/7
#11 lore raw

Re: [Qemu-devel] [PATCH 3/7] tcg: Swap commutative double-word comparisons

In reply to Richard Henderson
Quoted 39 lines · Richard Henderson
---
 tcg/optimize.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
tcg/optimize.c +19 -0
index 004c336..d39926e 100644
@@ -397,6 +397,22 @@ static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2)
     return false;
 }
 
+static bool swap_commutative2(TCGArg *p1, TCGArg *p2)
+{
+    int sum = 0;
+    sum += temps[p1[0]].state == TCG_TEMP_CONST;
+    sum += temps[p1[1]].state == TCG_TEMP_CONST;
+    sum -= temps[p2[0]].state == TCG_TEMP_CONST;
+    sum -= temps[p2[1]].state == TCG_TEMP_CONST;
+    if (sum > 0) {
+        TCGArg t;
+        t = p1[0], p1[0] = p2[0], p2[0] = t;
+        t = p1[1], p1[1] = p2[1], p2[1] = t;
+        return true;
+    }
+    return false;
+}
+
 /* 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)
@@ -475,6 +491,16 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
             swap_commutative(args[0], &args[2], &args[4]);
             swap_commutative(args[1], &args[3], &args[5]);
             break;
+        case INDEX_op_brcond2_i32:
+            if (swap_commutative2(&args[0], &args[2])) {
+                args[4] = tcg_swap_cond(args[4]);

tcg/optimize.c static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr, · line 506

Quoted 10 lines
tcg/optimize.c +7 -0
+            }
+            break;
+        case INDEX_op_setcond2_i32:
+            if (swap_commutative2(&args[1], &args[3])) {
+                args[5] = tcg_swap_cond(args[5]);
+            }
+            break;
         default:
             break;
         }
Signature · 2 lines
Aurelien Jarno                          GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net
#5 lore raw

Re: [Qemu-devel] [PATCH 2/7] tcg: Optimize add2 + sub2

In reply to Aurelien Jarno
Quoted 2 lines · Aurelien Jarno

I understand that we can't easily insert an instruction, so the limitation comes from here, but is it really something happening often?

It will certainly appear sometimes. E.g. s390x has an add immediate instruction that does exactly: r1 += imm16 << 32.

Or did you mean specifically the full constant being folded? That would happen quite a bit more often. That you can see with most any 64-bit RISC guest when they attempt to generate a constant from addition primitives instead of logical primitives.

For a 32-bit host, we've already decomposed logical primitives to 32-bit operations. And we can constant-fold through all of those. But when addition comes into play, we can't constant-fold through add2.

r~

#8 lore raw

Re: [Qemu-devel] [PATCH 2/7] tcg: Optimize add2 + sub2

In reply to Richard Henderson
Quoted 4 lines · Richard Henderson

We can't do complete constant folding because we lack "mov2", or the ability to insert opcodes in the stream. But we can at least canonicalize add2 operand ordering and simplify add2 to add when the lowpart adds a constant 0.

Couldn't we introduce add2_part1 and add2_part2, the latter being nop for architectures that don't need it?

Quoted 59 lines
---
 tcg/optimize.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
tcg/optimize.c +31 -1
index 55f2a24..004c336 100644
@@ -470,6 +470,11 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
             if (swap_commutative(args[0], &args[4], &args[3])) {
                 args[5] = tcg_invert_cond(args[5]);
             }
+            break;
+        case INDEX_op_add2_i32:
+            swap_commutative(args[0], &args[2], &args[4]);
+            swap_commutative(args[1], &args[3], &args[5]);
+            break;
         default:
             break;
         }
@@ -522,6 +527,32 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
                 continue;
             }
             break;
+        case INDEX_op_add2_i32:
+        case INDEX_op_sub2_i32:
+            /* Simplify op rl, rh, al, ah, 0, bh => op rh, ah, bh.
+               The zero implies there will be no carry into the high part.
+               But only when rl == al, since we can't insert the extra move
+               that would be required.  */
+            if (temps[args[4]].state == TCG_TEMP_CONST
+                && temps[args[4]].val == 0
+                && temps_are_copies(args[0], args[2])) {
+                if (temps[args[5]].state == TCG_TEMP_CONST
+                    && temps[args[5]].val == 0
+                    && temps_are_copies(args[1], args[3])) {
+                    gen_opc_buf[op_index] = INDEX_op_nop;
+                } else {
+                    gen_opc_buf[op_index] = (op == INDEX_op_add2_i32
+                                             ? INDEX_op_add_i32
+                                             : INDEX_op_sub_i32);
+                    args[0] = args[1];
+                    args[1] = args[3];
+                    args[2] = args[5];
+                    gen_args += 3;
+                }
+                args += 6;
+                continue;
+            }
+            break;
         default:
             break;
         }
--

1.7.11.4

#6 lore raw

Re: [Qemu-devel] [PATCH 2/7] tcg: Optimize add2 + sub2

In reply to Richard Henderson
Quoted 7 lines · Richard Henderson
Quoted 2 lines · Aurelien Jarno

I understand that we can't easily insert an instruction, so the limitation comes from here, but is it really something happening often?

It will certainly appear sometimes. E.g. s390x has an add immediate instruction that does exactly: r1 += imm16 << 32.

Quoted 9 lines

Or did you mean specifically the full constant being folded? That would happen quite a bit more often. That you can see with most any 64-bit RISC guest when they attempt to generate a constant from addition primitives instead of logical primitives.

For a 32-bit host, we've already decomposed logical primitives to 32-bit operations. And we can constant-fold through all of those. But when addition comes into play, we can't constant-fold through add2.

I tried this patch on an i386 host running an x86_64 target, but it even fails to start seabios, there is probably a wrong logic somewhere in the patch.

For the first add2 that seemed to have work correctly, this patch optimized 0.2% of them. I am not sure it worth it as is.

I think optimizing add2, and in general all *2 ops is a good idea, but we should be able to do more agressive optimization. Maybe, a bit like Blue was suggesting, add2 should always be followed by a nop, so we can do more optimizations?

Signature · 2 lines
Aurelien Jarno                          GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net
#9 lore raw

Re: [Qemu-devel] [PATCH 2/7] tcg: Optimize add2 + sub2

In reply to Blue Swirl
Quoted 6 lines · Blue Swirl
Quoted 4 lines

We can't do complete constant folding because we lack "mov2",

Quoted 3 lines

or the ability to insert opcodes in the stream. But we can at least canonicalize add2 operand ordering and simplify add2 to add when the lowpart adds a constant 0.

Couldn't we introduce add2_part1 and add2_part2, the latter being nop for architectures that don't need it?

Possibly. It certainly would be easy to model these as addcc + addx on targets like sparc where CC never gets clobbered during moves.

I'm a bit worried about i386 though, since loading 0 wants to use xor and clobber the flags. We could possibly work around this by taking care of the constant loading for add2_part2 manually. E.g.

{ INDEX_op_add2_part2, { "r", "ri", "ri" } }

  if (args[2] == args[0] && !const_args[2]) {
    // swap arg1 arg2
  }
  if (const_args[1]) {
    mov $args[1], args[0]
  } else {
    mov args[1], args[0]
  }
  adcl args[2], args[0]

which means that tcg_out_movi will not have to be called in between. It's all a bit fragile though.

That said, I do wonder if having a synthetic mov2{rr,ri,ii} opcodes isn't just easier. That could be broken up into two moves by tcg.c without the backends having to care about it.

r~

#7 lore raw

Re: [Qemu-devel] [PATCH 2/7] tcg: Optimize add2 + sub2

In reply to Aurelien Jarno
Quoted 2 lines · Aurelien Jarno

For the first add2 that seemed to have work correctly, this patch optimized 0.2% of them. I am not sure it worth it as is.

You're probably right.

Quoted 4 lines

I think optimizing add2, and in general all *2 ops is a good idea, but we should be able to do more agressive optimization. Maybe, a bit like Blue was suggesting, add2 should always be followed by a nop, so we can do more optimizations?

Adding an extra nop sounds like a better idea than add2_part[12]. And it's probably easier than adding mov2 opcodes -- one little assert inside the optimizer and we're golden.

r~

#13 lore raw

Re: [Qemu-devel] [PATCH 4/7] tcg: Optimize double-word comparisons against zero

In reply to Richard Henderson
Quoted 11 lines · Richard Henderson
---
 tcg/optimize.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)
tcg/optimize.c +0 -0
index d39926e..c972e4f 100644
@@ -799,6 +799,57 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
             }

tcg/optimize.c static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr, · line 809

Quoted 10 lines
tcg/optimize.c +8 -0
             args += 6;
             break;
+        case INDEX_op_brcond2_i32:
+            /* Simplify LT/GE comparisons vs zero to a single compare
+               vs the high word of the input.  */
+            if ((args[4] == TCG_COND_LT || args[4] == TCG_COND_GE)
+                && temps[args[2]].state == TCG_TEMP_CONST
+                && temps[args[3]].state == TCG_TEMP_CONST
+                && temps[args[2]].val == 0
+                && temps[args[2]].val == 0) {

The value comparison there is wrong, probably copy & paste issue. I wonder how it could work.

Quoted 16 lines
tcg/optimize.c +16 -0
+                gen_opc_buf[op_index] = INDEX_op_brcond_i32;
+                args[0] = args[1];
+                args[1] = args[3];
+                args[2] = args[4];
+                args[3] = args[5];
+                gen_args += 4;
+            } else {
+                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];
+                gen_args[5] = args[5];
+                gen_args += 6;
+            }
+            memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info));

tcg/optimize.c static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr, · line 835

Quoted 10 lines
tcg/optimize.c +10 -0
+            args += 6;
+            break;
+        case INDEX_op_setcond2_i32:
+            /* Simplify LT/GE comparisons vs zero to a single compare
+               vs the high word of the input.  */
+            if ((args[5] == TCG_COND_LT || args[5] == TCG_COND_GE)
+                && temps[args[3]].state == TCG_TEMP_CONST
+                && temps[args[4]].state == TCG_TEMP_CONST
+                && temps[args[3]].val == 0
+                && temps[args[4]].val == 0) {

Here it is fine.

Quoted 10 lines
tcg/optimize.c +10 -0
+                gen_opc_buf[op_index] = INDEX_op_setcond_i32;
+                args[1] = args[2];
+                args[2] = args[4];
+                args[3] = args[5];
+                gen_args += 4;
+            } else {
+                reset_temp(args[0]);
+                gen_args[0] = args[0];
+                gen_args[1] = args[1];
+                gen_args[2] = args[2];

tcg/optimize.c static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr, · line 855

Quoted 10 lines
tcg/optimize.c +7 -0
+                gen_args[3] = args[3];
+                gen_args[4] = args[4];
+                gen_args[5] = args[5];
+                gen_args += 6;
+            }
+            args += 6;
+            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))) {

While it's a nice optimization to have, one that seems to happen a lot more often is the two high parts being equal. It happens when the guest is working on (u)int32_t.

Signature · 2 lines
Aurelien Jarno                          GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net
Reviewed-by on v1 5/7
#16 lore raw

Re: [Qemu-devel] [PATCH 5/7] tcg: Split out subroutines from do_constant_folding_cond

In reply to Richard Henderson
Quoted 171 lines · Richard Henderson

We can re-use these for implementing double-word folding.

---
 tcg/optimize.c | 146 ++++++++++++++++++++++++++++++++-------------------------
 1 file changed, 81 insertions(+), 65 deletions(-)
tcg/optimize.c +81 -60
index c972e4f..c1881fa 100644
@@ -292,6 +292,82 @@ static TCGArg do_constant_folding(TCGOpcode op, TCGArg x, TCGArg y)
     return res;
 }
 
+static bool do_constant_folding_cond_32(uint32_t x, uint32_t y, TCGCond c)
+{
+    switch (c) {
+    case TCG_COND_EQ:
+        return x == y;
+    case TCG_COND_NE:
+        return x != 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 x < y;
+    case TCG_COND_GEU:
+        return x >= y;
+    case TCG_COND_LEU:
+        return x <= y;
+    case TCG_COND_GTU:
+        return x > y;
+    default:
+        tcg_abort();
+    }
+}
+
+static bool do_constant_folding_cond_64(uint64_t x, uint64_t y, TCGCond c)
+{
+    switch (c) {
+    case TCG_COND_EQ:
+        return x == y;
+    case TCG_COND_NE:
+        return x != 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 x < y;
+    case TCG_COND_GEU:
+        return x >= y;
+    case TCG_COND_LEU:
+        return x <= y;
+    case TCG_COND_GTU:
+        return x > y;
+    default:
+        tcg_abort();
+    }
+}
+
+static bool do_constant_folding_cond_eq(TCGCond c)
+{
+    switch (c) {
+    case TCG_COND_GT:
+    case TCG_COND_LTU:
+    case TCG_COND_LT:
+    case TCG_COND_GTU:
+    case TCG_COND_NE:
+        return 0;
+    case TCG_COND_GE:
+    case TCG_COND_GEU:
+    case TCG_COND_LE:
+    case TCG_COND_LEU:
+    case TCG_COND_EQ:
+        return 1;
+    default:
+        tcg_abort();
+    }
+}
+
 /* Return 2 if the condition can't be simplified, and the result
    of the condition (0 or 1) if it can */
 static TCGArg do_constant_folding_cond(TCGOpcode op, TCGArg x,
@@ -300,69 +376,14 @@ static TCGArg do_constant_folding_cond(TCGOpcode op, TCGArg x,
     if (temps[x].state == TCG_TEMP_CONST && temps[y].state == TCG_TEMP_CONST) {
         switch (op_bits(op)) {
         case 32:
-            switch (c) {
-            case TCG_COND_EQ:
-                return (uint32_t)temps[x].val == (uint32_t)temps[y].val;
-            case TCG_COND_NE:
-                return (uint32_t)temps[x].val != (uint32_t)temps[y].val;
-            case TCG_COND_LT:
-                return (int32_t)temps[x].val < (int32_t)temps[y].val;
-            case TCG_COND_GE:
-                return (int32_t)temps[x].val >= (int32_t)temps[y].val;
-            case TCG_COND_LE:
-                return (int32_t)temps[x].val <= (int32_t)temps[y].val;
-            case TCG_COND_GT:
-                return (int32_t)temps[x].val > (int32_t)temps[y].val;
-            case TCG_COND_LTU:
-                return (uint32_t)temps[x].val < (uint32_t)temps[y].val;
-            case TCG_COND_GEU:
-                return (uint32_t)temps[x].val >= (uint32_t)temps[y].val;
-            case TCG_COND_LEU:
-                return (uint32_t)temps[x].val <= (uint32_t)temps[y].val;
-            case TCG_COND_GTU:
-                return (uint32_t)temps[x].val > (uint32_t)temps[y].val;
-            }
-            break;
+            return do_constant_folding_cond_32(temps[x].val, temps[y].val, c);
         case 64:
-            switch (c) {
-            case TCG_COND_EQ:
-                return (uint64_t)temps[x].val == (uint64_t)temps[y].val;
-            case TCG_COND_NE:
-                return (uint64_t)temps[x].val != (uint64_t)temps[y].val;
-            case TCG_COND_LT:
-                return (int64_t)temps[x].val < (int64_t)temps[y].val;
-            case TCG_COND_GE:
-                return (int64_t)temps[x].val >= (int64_t)temps[y].val;
-            case TCG_COND_LE:
-                return (int64_t)temps[x].val <= (int64_t)temps[y].val;
-            case TCG_COND_GT:
-                return (int64_t)temps[x].val > (int64_t)temps[y].val;
-            case TCG_COND_LTU:
-                return (uint64_t)temps[x].val < (uint64_t)temps[y].val;
-            case TCG_COND_GEU:
-                return (uint64_t)temps[x].val >= (uint64_t)temps[y].val;
-            case TCG_COND_LEU:
-                return (uint64_t)temps[x].val <= (uint64_t)temps[y].val;
-            case TCG_COND_GTU:
-                return (uint64_t)temps[x].val > (uint64_t)temps[y].val;
-            }
-            break;
+            return do_constant_folding_cond_64(temps[x].val, temps[y].val, c);
+        default:
+            tcg_abort();
         }
     } else if (temps_are_copies(x, y)) {
-        switch (c) {
-        case TCG_COND_GT:
-        case TCG_COND_LTU:
-        case TCG_COND_LT:
-        case TCG_COND_GTU:
-        case TCG_COND_NE:
-            return 0;
-        case TCG_COND_GE:
-        case TCG_COND_GEU:
-        case TCG_COND_LE:
-        case TCG_COND_LEU:
-        case TCG_COND_EQ:
-            return 1;
-        }
+        return do_constant_folding_cond_eq(c);
     } else if (temps[y].state == TCG_TEMP_CONST && temps[y].val == 0) {
         switch (c) {
         case TCG_COND_LTU:
@@ -375,11 +396,6 @@ static TCGArg do_constant_folding_cond(TCGOpcode op, TCGArg x,
     } else {

tcg/optimize.c static TCGArg do_constant_folding_cond(TCGOpcode op, TCGArg x, · line 401

Quoted 10 lines
tcg/optimize.c +0 -5
         return 2;
     }
-
-    fprintf(stderr,
-            "Unrecognized bitness %d or condition %d in "
-            "do_constant_folding_cond.\n", op_bits(op), c);
-    tcg_abort();
 }
 
 static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2)
Signature · 2 lines
Aurelien Jarno                          GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net
#14 lore raw

Re: [Qemu-devel] [PATCH 4/7] tcg: Optimize double-word comparisons against zero

In reply to Aurelien Jarno
Quoted 3 lines · Aurelien Jarno

While it's a nice optimization to have, one that seems to happen a lot more often is the two high parts being equal. It happens when the guest is working on (u)int32_t.

It depends on what target you're looking at. For alpha guest, all branches are comparisons vs zero, so LT/GE happens with some regularity.

r~

Reviewed-by on v1 6/7
#18 lore raw

Re: [Qemu-devel] [PATCH 6/7] tcg: Tidy brcond optimization

In reply to Richard Henderson
Quoted 40 lines · Richard Henderson

Do the memset once. Don't reset_temp before doing so.

---
 tcg/optimize.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tcg/optimize.c +3 -3
index c1881fa..dfac877 100644
@@ -771,22 +771,22 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
             tmp = do_constant_folding_cond(op, args[0], args[1], args[2]);
             if (tmp != 2) {
                 if (tmp) {
-                    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;
                 } 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]);
                 gen_args[0] = args[0];
                 gen_args[1] = args[1];
                 gen_args[2] = args[2];
                 gen_args[3] = args[3];
                 gen_args += 4;
             }
+            memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info));
             args += 4;
             break;
         CASE_OP_32_64(movcond):
Signature · 1 line
1.7.11.4

Removing the useless reset_temp() is indeed something to do. I am not so sure that factorizing the memset() and putting a break in the nop case is easier to read. Nevertheless:

Signature · 2 lines
Aurelien Jarno                          GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net
Reviewed-by on v1 7/7
#20 lore raw

Re: [Qemu-devel] [PATCH 7/7] tcg: Do constant folding on double-word comparisons

In reply to Richard Henderson
Quoted 154 lines · Richard Henderson
---
 tcg/optimize.c | 134 ++++++++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 94 insertions(+), 40 deletions(-)
tcg/optimize.c +87 -40
index dfac877..f6a16fd 100644
@@ -398,6 +398,40 @@ static TCGArg do_constant_folding_cond(TCGOpcode op, TCGArg x,
     }
 }
 
+/* Return 2 if the condition can't be simplified, and the result
+   of the condition (0 or 1) if it can */
+static TCGArg do_constant_folding_cond2(TCGArg *p1, TCGArg *p2, TCGCond c)
+{
+    TCGArg al = p1[0], ah = p1[1];
+    TCGArg bl = p2[0], bh = p2[1];
+
+    if (temps[bl].state == TCG_TEMP_CONST
+        && temps[bh].state == TCG_TEMP_CONST) {
+        uint64_t b = ((uint64_t)temps[bh].val << 32) | (uint32_t)temps[bl].val;
+
+        if (temps[al].state == TCG_TEMP_CONST
+            && temps[ah].state == TCG_TEMP_CONST) {
+            uint64_t a;
+            a = ((uint64_t)temps[ah].val << 32) | (uint32_t)temps[al].val;
+            return do_constant_folding_cond_64(a, b, c);
+        }
+        if (b == 0) {
+            switch (c) {
+            case TCG_COND_LTU:
+                return 0;
+            case TCG_COND_GEU:
+                return 1;
+            default:
+                break;
+            }
+        }
+    }
+    if (temps_are_copies(al, bl) && temps_are_copies(ah, bh)) {
+        return do_constant_folding_cond_eq(c);
+    }
+    return 2;
+}
+
 static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2)
 {
     TCGArg a1 = *p1, a2 = *p2;
@@ -816,53 +850,73 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
             args += 6;
             break;
         case INDEX_op_brcond2_i32:
-            /* Simplify LT/GE comparisons vs zero to a single compare
-               vs the high word of the input.  */
-            if ((args[4] == TCG_COND_LT || args[4] == TCG_COND_GE)
-                && temps[args[2]].state == TCG_TEMP_CONST
-                && temps[args[3]].state == TCG_TEMP_CONST
-                && temps[args[2]].val == 0
-                && temps[args[2]].val == 0) {
-                gen_opc_buf[op_index] = INDEX_op_brcond_i32;
-                args[0] = args[1];
-                args[1] = args[3];
-                args[2] = args[4];
-                args[3] = args[5];
-                gen_args += 4;
+            tmp = do_constant_folding_cond2(&args[0], &args[2], args[4]);
+            if (tmp != 2) {
+                if (tmp) {
+                    gen_opc_buf[op_index] = INDEX_op_br;
+                    gen_args[0] = args[5];
+                    gen_args += 1;
+                } else {
+                    gen_opc_buf[op_index] = INDEX_op_nop;
+                    args += 6;
+                    break;
+                }
             } else {
-                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];
-                gen_args[5] = args[5];
-                gen_args += 6;
+                /* Simplify LT/GE comparisons vs zero to a single compare
+                   vs the high word of the input.  */
+                if ((args[4] == TCG_COND_LT || args[4] == TCG_COND_GE)
+                    && temps[args[2]].state == TCG_TEMP_CONST
+                    && temps[args[3]].state == TCG_TEMP_CONST
+                    && temps[args[2]].val == 0
+                    && temps[args[2]].val == 0) {
+                    gen_opc_buf[op_index] = INDEX_op_brcond_i32;
+                    args[0] = args[1];
+                    args[1] = args[3];
+                    args[2] = args[4];
+                    args[3] = args[5];
+                    gen_args += 4;
+                } else {
+                    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];
+                    gen_args[5] = args[5];
+                    gen_args += 6;
+                }
             }
             memset(temps, 0, nb_temps * sizeof(struct tcg_temp_info));
             args += 6;
             break;
         case INDEX_op_setcond2_i32:
-            /* Simplify LT/GE comparisons vs zero to a single compare
-               vs the high word of the input.  */
-            if ((args[5] == TCG_COND_LT || args[5] == TCG_COND_GE)
-                && temps[args[3]].state == TCG_TEMP_CONST
-                && temps[args[4]].state == TCG_TEMP_CONST
-                && temps[args[3]].val == 0
-                && temps[args[4]].val == 0) {
-                gen_opc_buf[op_index] = INDEX_op_setcond_i32;
-                args[1] = args[2];
-                args[2] = args[4];
-                args[3] = args[5];
-                gen_args += 4;
+            tmp = do_constant_folding_cond2(&args[1], &args[3], args[5]);
+            if (tmp != 2) {
+                gen_opc_buf[op_index] = INDEX_op_movi_i32;
+                tcg_opt_gen_movi(gen_args, args[0], tmp);
+                gen_args += 2;
             } else {
-                reset_temp(args[0]);
-                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];
-                gen_args[5] = args[5];
-                gen_args += 6;
+                /* Simplify LT/GE comparisons vs zero to a single compare
+                   vs the high word of the input.  */
+                if ((args[5] == TCG_COND_LT || args[5] == TCG_COND_GE)
+                    && temps[args[3]].state == TCG_TEMP_CONST
+                    && temps[args[4]].state == TCG_TEMP_CONST
+                    && temps[args[3]].val == 0
+                    && temps[args[4]].val == 0) {
+                    gen_opc_buf[op_index] = INDEX_op_setcond_i32;
+                    args[1] = args[2];
+                    args[2] = args[4];
+                    args[3] = args[5];
+                    gen_args += 4;
+                } else {
+                    reset_temp(args[0]);
+                    gen_args[0] = args[0];

tcg/optimize.c static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr, · line 922

Quoted 10 lines
tcg/optimize.c +7 -0
+                    gen_args[1] = args[1];
+                    gen_args[2] = args[2];
+                    gen_args[3] = args[3];
+                    gen_args[4] = args[4];
+                    gen_args[5] = args[5];
+                    gen_args += 6;
+                }
             }
             args += 6;
             break;
Signature · 2 lines
Aurelien Jarno                          GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net