Skip to contentSkip to conversation

tcg/i386: Improvements to deposit

PATCH In review Richard Henderson 3 participants 11 messages Aug 16, 2023 – Aug 21, 2023

lore
Share & tools
lore
#0 lore raw

cover tcg/i386: Improvements to deposit

Richard Henderson (3):
tcg/i386: Drop BYTEH deposits for 64-bit
tcg: Fold deposit with zero to and
tcg/i386: Allow immediate as input to deposit_*

 tcg/i386/tcg-target-con-set.h |  2 +-
 tcg/i386/tcg-target-con-str.h |  1 -
 tcg/i386/tcg-target.h         |  4 ++--
 tcg/optimize.c                | 35 +++++++++++++++++++++++++++++++++++
 tcg/i386/tcg-target.c.inc     | 31 ++++++++++++++++++++++++-------
 5 files changed, 62 insertions(+), 11 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/3 tcg/i386: Drop BYTEH deposits for 64-bit

It is more useful to allow low-part deposits into all registers than to restrict allocation for high-byte deposits.

---
 tcg/i386/tcg-target-con-set.h | 2 +-
 tcg/i386/tcg-target-con-str.h | 1 -
 tcg/i386/tcg-target.h         | 4 ++--
 tcg/i386/tcg-target.c.inc     | 7 +++----
 4 files changed, 6 insertions(+), 8 deletions(-)
tcg/i386/tcg-target-con-set.h +1 -1
index 5ea3a292f0..3949d49538 100644
@@ -33,7 +33,7 @@ C_O1_I1(r, q)
 C_O1_I1(r, r)
 C_O1_I1(x, r)
 C_O1_I1(x, x)
-C_O1_I2(Q, 0, Q)
+C_O1_I2(q, 0, q)
 C_O1_I2(q, r, re)
 C_O1_I2(r, 0, ci)
 C_O1_I2(r, 0, r)
tcg/i386/tcg-target-con-str.h +0 -1
index 24e6bcb80d..95a30e58cd 100644
@@ -19,7 +19,6 @@ REGS('D', 1u << TCG_REG_EDI)
 REGS('r', ALL_GENERAL_REGS)
 REGS('x', ALL_VECTOR_REGS)
 REGS('q', ALL_BYTEL_REGS)     /* regs that can be used as a byte operand */
-REGS('Q', ALL_BYTEH_REGS)     /* regs with a second byte (e.g. %ah) */
 REGS('L', ALL_GENERAL_REGS & ~SOFTMMU_RESERVE_REGS)  /* qemu_ld/st */
 REGS('s', ALL_BYTEL_REGS & ~SOFTMMU_RESERVE_REGS)    /* qemu_st8_i32 data */
 
tcg/i386/tcg-target.h +2 -2
index 2a2e3fffa8..30cce01ca4 100644
@@ -227,8 +227,8 @@ typedef enum {
 #define TCG_TARGET_HAS_cmpsel_vec       -1
 
 #define TCG_TARGET_deposit_i32_valid(ofs, len) \
-    (((ofs) == 0 && (len) == 8) || ((ofs) == 8 && (len) == 8) || \
-     ((ofs) == 0 && (len) == 16))
+    (((ofs) == 0 && ((len) == 8 || (len) == 16)) || \
+     (TCG_TARGET_REG_BITS == 32 && (ofs) == 8 && (len) == 8))
 #define TCG_TARGET_deposit_i64_valid    TCG_TARGET_deposit_i32_valid
 
 /* Check for the possibility of high-byte extraction and, for 64-bit,
tcg/i386/tcg-target.c.inc +3 -4
index a6b2eae995..ba40dd0f4d 100644
@@ -144,7 +144,6 @@ static TCGReg tcg_target_call_oarg_reg(TCGCallReturnKind kind, int slot)
 # define TCG_REG_L1 TCG_REG_EDX
 #endif
 
-#define ALL_BYTEH_REGS         0x0000000fu
 #if TCG_TARGET_REG_BITS == 64
 # define ALL_GENERAL_REGS      0x0000ffffu
 # define ALL_VECTOR_REGS       0xffff0000u
@@ -152,7 +151,7 @@ static TCGReg tcg_target_call_oarg_reg(TCGCallReturnKind kind, int slot)
 #else
 # define ALL_GENERAL_REGS      0x000000ffu
 # define ALL_VECTOR_REGS       0x00ff0000u
-# define ALL_BYTEL_REGS        ALL_BYTEH_REGS
+# define ALL_BYTEL_REGS        0x0000000fu
 #endif
 #ifdef CONFIG_SOFTMMU
 # define SOFTMMU_RESERVE_REGS  ((1 << TCG_REG_L0) | (1 << TCG_REG_L1))
@@ -2752,7 +2751,7 @@ static inline void tcg_out_op(TCGContext *s, TCGOpcode opc,
         if (args[3] == 0 && args[4] == 8) {
             /* load bits 0..7 */
             tcg_out_modrm(s, OPC_MOVB_EvGv | P_REXB_R | P_REXB_RM, a2, a0);
-        } else if (args[3] == 8 && args[4] == 8) {
+        } else if (TCG_TARGET_REG_BITS == 32 && args[3] == 8 && args[4] == 8) {
             /* load bits 8..15 */
             tcg_out_modrm(s, OPC_MOVB_EvGv, a2, a0 + 4);
         } else if (args[3] == 0 && args[4] == 16) {
@@ -3312,7 +3311,7 @@ static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op)
 
     case INDEX_op_deposit_i32:
     case INDEX_op_deposit_i64:
-        return C_O1_I2(Q, 0, Q);
+        return C_O1_I2(q, 0, q);
 
     case INDEX_op_setcond_i32:
     case INDEX_op_setcond_i64:
Signature · 1 line
2.34.1
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] tcg/i386: Drop BYTEH deposits for 64-bit

Quoted 13 lines · Richard Henderson

It is more useful to allow low-part deposits into all registers than to restrict allocation for high-byte deposits.

#define TCG_TARGET_deposit_i32_valid(ofs, len) \

tcg/i386/tcg-target.h +2 -2
-    (((ofs) == 0 && (len) == 8) || ((ofs) == 8 && (len) == 8) || \
-     ((ofs) == 0 && (len) == 16))
+    (((ofs) == 0 && ((len) == 8 || (len) == 16)) || \
+     (TCG_TARGET_REG_BITS == 32 && (ofs) == 8 && (len) == 8))
 #define TCG_TARGET_deposit_i64_valid    TCG_TARGET_deposit_i32_valid

tcg/i386/tcg-target.h static inline void tcg_out_op(TCGContext *s, TCGOpcode opc, · line 2754

Quoted 5 lines
tcg/i386/tcg-target.h +1 -1
@@ -2752,7 +2751,7 @@ static inline void tcg_out_op(TCGContext *s, TCGOpcode opc,
         if (args[3] == 0 && args[4] == 8) {
             /* load bits 0..7 */
             tcg_out_modrm(s, OPC_MOVB_EvGv | P_REXB_R | P_REXB_RM, a2, a0);
-        } else if (args[3] == 8 && args[4] == 8) {
+        } else if (TCG_TARGET_REG_BITS == 32 && args[3] == 8 && args[4] == 8) {

Should we assert(TCG_TARGET_REG_BITS == 32) rather than making it part of the condition? If I understand the change to the deposit_i32_valid macro above, we should never get here with 8, 8 if TCG_TARGET_REG_BITS is 64.

tcg/i386/tcg-target.c.inc static inline void tcg_out_op(TCGContext *s, TCGOpcode opc, · line 2757

Quoted 3 lines
tcg/i386/tcg-target.c.inc +0 -0
             /* load bits 8..15 */
             tcg_out_modrm(s, OPC_MOVB_EvGv, a2, a0 + 4);
         } else if (args[3] == 0 && args[4] == 16) {

Otherwise

thanks
-- PMM

#3 lore raw

Re: [PATCH 1/3] tcg/i386: Drop BYTEH deposits for 64-bit

Quoted 14 lines · Peter Maydell
Quoted 10 lines · Richard Henderson

It is more useful to allow low-part deposits into all registers than to restrict allocation for high-byte deposits.

  #define TCG_TARGET_deposit_i32_valid(ofs, len) \
tcg/i386/tcg-target.h +2 -2
-    (((ofs) == 0 && (len) == 8) || ((ofs) == 8 && (len) == 8) || \
-     ((ofs) == 0 && (len) == 16))
+    (((ofs) == 0 && ((len) == 8 || (len) == 16)) || \
+     (TCG_TARGET_REG_BITS == 32 && (ofs) == 8 && (len) == 8))
  #define TCG_TARGET_deposit_i64_valid    TCG_TARGET_deposit_i32_valid
Quoted 9 lines

tcg/i386/tcg-target.c.inc static inline void tcg_out_op(TCGContext *s, TCGOpcode opc, · line 2754

Quoted 6 lines
tcg/i386/tcg-target.c.inc +1 -1
@@ -2752,7 +2751,7 @@ static inline void tcg_out_op(TCGContext *s, TCGOpcode opc,
          if (args[3] == 0 && args[4] == 8) {
              /* load bits 0..7 */
              tcg_out_modrm(s, OPC_MOVB_EvGv | P_REXB_R | P_REXB_RM, a2, a0);
-        } else if (args[3] == 8 && args[4] == 8) {
+        } else if (TCG_TARGET_REG_BITS == 32 && args[3] == 8 && args[4] == 8) {

Should we assert(TCG_TARGET_REG_BITS == 32) rather than making it part of the condition?

The if/else chain ends in g_assert_not_reached().

Quoted 2 lines

If I understand the change to the deposit_i32_valid macro above, we should never get here with 8, 8 if TCG_TARGET_REG_BITS is 64.

Correct.

r~

#4 lore raw

PATCH 2/3 tcg: Fold deposit with zero to and

In reply to Richard Henderson

Inserting a zero into a value, or inserting a value into zero at offset 0 my be implemented with AND.

---
 tcg/optimize.c | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
tcg/optimize.c +35 -0
index d2156367a3..956114b631 100644
@@ -1279,6 +1279,8 @@ static bool fold_ctpop(OptContext *ctx, TCGOp *op)
 
 static bool fold_deposit(OptContext *ctx, TCGOp *op)
 {
+    TCGOpcode and_opc;
+
     if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) {
         uint64_t t1 = arg_info(op->args[1])->val;
         uint64_t t2 = arg_info(op->args[2])->val;
@@ -1287,6 +1289,39 @@ static bool fold_deposit(OptContext *ctx, TCGOp *op)
         return tcg_opt_gen_movi(ctx, op, op->args[0], t1);
     }
 
+    switch (ctx->type) {
+    case TCG_TYPE_I32:
+        and_opc = INDEX_op_and_i32;
+        break;
+    case TCG_TYPE_I64:
+        and_opc = INDEX_op_and_i64;
+        break;
+    default:
+        g_assert_not_reached();
+    }
+
+    if (arg_is_const(op->args[1])
+        && arg_info(op->args[1])->val == 0
+        && op->args[3] == 0) {
+        uint64_t mask = MAKE_64BIT_MASK(0, op->args[4]);
+
+        op->opc = and_opc;
+        op->args[1] = op->args[2];
+        op->args[2] = temp_arg(tcg_constant_internal(ctx->type, mask));
+        ctx->z_mask = mask & arg_info(op->args[1])->z_mask;
+        return false;
+    }
+
+    if (arg_is_const(op->args[2])
+        && arg_info(op->args[2])->val == 0) {
+        uint64_t mask = deposit64(-1, op->args[3], op->args[4], 0);
+
+        op->opc = and_opc;
+        op->args[2] = temp_arg(tcg_constant_internal(ctx->type, mask));
+        ctx->z_mask = mask & arg_info(op->args[1])->z_mask;
+        return false;
+    }
+
     ctx->z_mask = deposit64(arg_info(op->args[1])->z_mask,
                             op->args[3], op->args[4],
                             arg_info(op->args[2])->z_mask);
Signature · 1 line
2.34.1
#5 lore raw

Re: [PATCH 2/3] tcg: Fold deposit with zero to and

Quoted 31 lines · Richard Henderson

Inserting a zero into a value, or inserting a value into zero at offset 0 my be implemented with AND.

---
 tcg/optimize.c | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
tcg/optimize.c +7 -0
index d2156367a3..956114b631 100644
@@ -1279,6 +1279,8 @@ static bool fold_ctpop(OptContext *ctx, TCGOp *op)
 
 static bool fold_deposit(OptContext *ctx, TCGOp *op)
 {
+    TCGOpcode and_opc;
+
     if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) {
         uint64_t t1 = arg_info(op->args[1])->val;
         uint64_t t2 = arg_info(op->args[2])->val;
@@ -1287,6 +1289,39 @@ static bool fold_deposit(OptContext *ctx, TCGOp *op)
         return tcg_opt_gen_movi(ctx, op, op->args[0], t1);
     }
 
+    switch (ctx->type) {
+    case TCG_TYPE_I32:
+        and_opc = INDEX_op_and_i32;
+        break;
+    case TCG_TYPE_I64:

tcg/optimize.c static bool fold_deposit(OptContext *ctx, TCGOp *op) · line 1306

Quoted 10 lines
tcg/optimize.c +10 -0
+        and_opc = INDEX_op_and_i64;
+        break;
+    default:
+        g_assert_not_reached();
+    }
+
+    if (arg_is_const(op->args[1])
+        && arg_info(op->args[1])->val == 0
+        && op->args[3] == 0) {
+        uint64_t mask = MAKE_64BIT_MASK(0, op->args[4]);

The docs for the TCG deposit op don't say what the restrictions on the immediate args are, but this will be UB for QEMU if args[4] is 0. Have we already sanitized those somewhere?

Quoted 12 lines
tcg/optimize.c +12 -0
+
+        op->opc = and_opc;
+        op->args[1] = op->args[2];
+        op->args[2] = temp_arg(tcg_constant_internal(ctx->type, mask));
+        ctx->z_mask = mask & arg_info(op->args[1])->z_mask;
+        return false;
+    }
+
+    if (arg_is_const(op->args[2])
+        && arg_info(op->args[2])->val == 0) {
+        uint64_t mask = deposit64(-1, op->args[3], op->args[4], 0);
+

tcg/optimize.c static bool fold_deposit(OptContext *ctx, TCGOp *op)

Quoted 10 lines
tcg/optimize.c +6 -1
+        op->opc = and_opc;
+        op->args[2] = temp_arg(tcg_constant_internal(ctx->type, mask));
+        ctx->z_mask = mask & arg_info(op->args[1])->z_mask;
+        return false;
+    }
+
     ctx->z_mask = deposit64(arg_info(op->args[1])->z_mask,
                             op->args[3], op->args[4],
                             arg_info(op->args[2])->z_mask);
--

thanks
-- PMM

#6 lore raw

Re: [PATCH 2/3] tcg: Fold deposit with zero to and

Quoted 8 lines · Peter Maydell

tcg/optimize.c static bool fold_deposit(OptContext *ctx, TCGOp *op) · line 1306

Quoted 4 lines
tcg/optimize.c +4 -0
+    if (arg_is_const(op->args[1])
+        && arg_info(op->args[1])->val == 0
+        && op->args[3] == 0) {
+        uint64_t mask = MAKE_64BIT_MASK(0, op->args[4]);

The docs for the TCG deposit op don't say what the restrictions on the immediate args are, but this will be UB for QEMU if args[4] is 0. Have we already sanitized those somewhere?

tcg_gen_deposit_{i32,i64} do so.

r~

Reviewed-by
#7 lore raw

Re: [PATCH 2/3] tcg: Fold deposit with zero to and

Quoted 7 lines · Richard Henderson
Quoted 5 lines · Peter Maydell
Quoted 4 lines
tcg/optimize.c +4 -0
+    if (arg_is_const(op->args[1])
+        && arg_info(op->args[1])->val == 0
+        && op->args[3] == 0) {
+        uint64_t mask = MAKE_64BIT_MASK(0, op->args[4]);
Quoted 5 lines
Quoted 3 lines

The docs for the TCG deposit op don't say what the restrictions on the immediate args are, but this will be UB for QEMU if args[4] is 0. Have we already sanitized those somewhere?

tcg_gen_deposit_{i32,i64} do so.

Cool.

thanks
-- PMM

Reviewed-by on v1 2/3
#8 lore raw

Re: [PATCH 2/3] tcg: Fold deposit with zero to and

In reply to Richard Henderson
Quoted 2 lines · Richard Henderson

Inserting a zero into a value, or inserting a value into zero at offset 0 my be implemented with AND.

Typo "may".

Quoted 26 lines
---
  tcg/optimize.c | 35 +++++++++++++++++++++++++++++++++++
  1 file changed, 35 insertions(+)
tcg/optimize.c +6 -0
index d2156367a3..956114b631 100644
@@ -1279,6 +1279,8 @@ static bool fold_ctpop(OptContext *ctx, TCGOp *op)
 
  static bool fold_deposit(OptContext *ctx, TCGOp *op)
  {
+    TCGOpcode and_opc;
+
      if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) {
          uint64_t t1 = arg_info(op->args[1])->val;
          uint64_t t2 = arg_info(op->args[2])->val;
@@ -1287,6 +1289,39 @@ static bool fold_deposit(OptContext *ctx, TCGOp *op)
          return tcg_opt_gen_movi(ctx, op, op->args[0], t1);
      }
 
+    switch (ctx->type) {
+    case TCG_TYPE_I32:
+        and_opc = INDEX_op_and_i32;
+        break;

tcg/optimize.c static bool fold_deposit(OptContext *ctx, TCGOp *op) · line 1305

Quoted 10 lines
tcg/optimize.c +10 -0
+    case TCG_TYPE_I64:
+        and_opc = INDEX_op_and_i64;
+        break;
+    default:
+        g_assert_not_reached();
+    }
+
+    if (arg_is_const(op->args[1])
+        && arg_info(op->args[1])->val == 0
+        && op->args[3] == 0) {
           /* Inserting a value into zero at offset 0. */

tcg/optimize.c static bool fold_deposit(OptContext *ctx, TCGOp *op) · line 1316

Quoted 11 lines
tcg/optimize.c +11 -0
+        uint64_t mask = MAKE_64BIT_MASK(0, op->args[4]);
+
+        op->opc = and_opc;
+        op->args[1] = op->args[2];
+        op->args[2] = temp_arg(tcg_constant_internal(ctx->type, mask));
+        ctx->z_mask = mask & arg_info(op->args[1])->z_mask;
+        return false;
+    }
+
+    if (arg_is_const(op->args[2])
+        && arg_info(op->args[2])->val == 0) {
           /* Inserting a zero into a value. */

tcg/optimize.c static bool fold_deposit(OptContext *ctx, TCGOp *op) · line 1323

Quoted 7 lines
tcg/optimize.c +7 -0
+        uint64_t mask = deposit64(-1, op->args[3], op->args[4], 0);
+
+        op->opc = and_opc;
+        op->args[2] = temp_arg(tcg_constant_internal(ctx->type, mask));
+        ctx->z_mask = mask & arg_info(op->args[1])->z_mask;
+        return false;
+    }
#9 lore raw

PATCH 3/3 tcg/i386: Allow immediate as input to deposit_*

In reply to Richard Henderson

We can use MOVB and MOVW with an immediate just as easily as with a register input.

---
 tcg/i386/tcg-target-con-set.h |  2 +-
 tcg/i386/tcg-target.c.inc     | 26 ++++++++++++++++++++++----
 2 files changed, 23 insertions(+), 5 deletions(-)
tcg/i386/tcg-target-con-set.h +1 -1
index 3949d49538..7d00a7dde8 100644
@@ -33,7 +33,7 @@ C_O1_I1(r, q)
 C_O1_I1(r, r)
 C_O1_I1(x, r)
 C_O1_I1(x, x)
-C_O1_I2(q, 0, q)
+C_O1_I2(q, 0, qi)
 C_O1_I2(q, r, re)
 C_O1_I2(r, 0, ci)
 C_O1_I2(r, 0, r)
tcg/i386/tcg-target.c.inc +22 -4
index ba40dd0f4d..3045b56002 100644
@@ -276,6 +276,7 @@ static bool tcg_target_const_match(int64_t val, TCGType type, int ct)
 #define OPC_MOVL_GvEv	(0x8b)		/* loads, more or less */
 #define OPC_MOVB_EvIz   (0xc6)
 #define OPC_MOVL_EvIz	(0xc7)
+#define OPC_MOVB_Ib     (0xb0)
 #define OPC_MOVL_Iv     (0xb8)
 #define OPC_MOVBE_GyMy  (0xf0 | P_EXT38)
 #define OPC_MOVBE_MyGy  (0xf1 | P_EXT38)
@@ -2750,13 +2751,30 @@ static inline void tcg_out_op(TCGContext *s, TCGOpcode opc,
     OP_32_64(deposit):
         if (args[3] == 0 && args[4] == 8) {
             /* load bits 0..7 */
-            tcg_out_modrm(s, OPC_MOVB_EvGv | P_REXB_R | P_REXB_RM, a2, a0);
+            if (const_a2) {
+                tcg_out_opc(s, OPC_MOVB_Ib | P_REXB_RM | LOWREGMASK(a0),
+                            0, a0, 0);
+                tcg_out8(s, a2);
+            } else {
+                tcg_out_modrm(s, OPC_MOVB_EvGv | P_REXB_R | P_REXB_RM, a2, a0);
+            }
         } else if (TCG_TARGET_REG_BITS == 32 && args[3] == 8 && args[4] == 8) {
             /* load bits 8..15 */
-            tcg_out_modrm(s, OPC_MOVB_EvGv, a2, a0 + 4);
+            if (const_a2) {
+                tcg_out8(s, OPC_MOVB_Ib + a0 + 4);
+                tcg_out8(s, a2);
+            } else {
+                tcg_out_modrm(s, OPC_MOVB_EvGv, a2, a0 + 4);
+            }
         } else if (args[3] == 0 && args[4] == 16) {
             /* load bits 0..15 */
-            tcg_out_modrm(s, OPC_MOVL_EvGv | P_DATA16, a2, a0);
+            if (const_a2) {
+                tcg_out_opc(s, OPC_MOVL_Iv | P_DATA16 | LOWREGMASK(a0),
+                            0, a0, 0);
+                tcg_out16(s, a2);
+            } else {
+                tcg_out_modrm(s, OPC_MOVL_EvGv | P_DATA16, a2, a0);
+            }
         } else {
             g_assert_not_reached();
         }
@@ -3311,7 +3329,7 @@ static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op)
 
     case INDEX_op_deposit_i32:
     case INDEX_op_deposit_i64:
-        return C_O1_I2(q, 0, q);
+        return C_O1_I2(q, 0, qi);
 
     case INDEX_op_setcond_i32:
     case INDEX_op_setcond_i64:
Signature · 1 line
2.34.1