Skip to contentSkip to conversation

target/i386: optimize string operations

PATCH In review Paolo Bonzini 2 participants 33 messages Dec 15, 2024

lore
Share & tools
lore
#0 lore raw

cover target/i386: optimize string operations

This started as an "easy" fix for RF handling in string instructions. I then realized how broken repz_opt is (patch 5) in that it was optimizing for the wrong case; and that redoing the optimization would make the RF handling basically free.

On a microbenchmark running x86-on-x86 user-mode emulation, stos and movs execute about 40% less instruction and about 60% less branches. Performance is very variable, because it is limited by memory bandwidth and because the out-of-order processor does a great job of scheduling all the useless instructions executed by the older code; but the microbenchmark results seem to improve by 10-15%.

Paolo

Paolo Bonzini (13):
target/i386: inline gen_jcc into sole caller
target/i386: remove trailing 1 from gen_{j,cmov,set}cc1 target/i386: unify REP and REPZ/REPNZ generation target/i386: unify choice between single and repeated string
instructions
target/i386: reorganize ops emitted by do_gen_rep, drop repz_opt target/i386: tcg: move gen_set/reset_* earlier in the file target/i386: fix RF handling for string instructions target/i386: make cc_op handling more explicit for repeated string
instructions.
target/i386: do not use gen_op_jz_ecx for repeated string operations target/i386: optimize CX handling in repeated string operations target/i386: execute multiple REP/REPZ iterations without leaving TB target/i386: pull computation of string update value out of loop target/i386: avoid using s->tmp0 for add to implicit registers

 target/i386/tcg/translate.c | 342 +++++++++++++++++++++---------------
 target/i386/tcg/emit.c.inc  |  56 ++----
 2 files changed, 219 insertions(+), 179 deletions(-)
Signature · 1 line
2.47.1
#1 lore raw

PATCH 1/13 target/i386: inline gen_jcc into sole caller

The code of gen_Jcc is very similar to gen_LOOP* and gen_JCXZ, but this is hidden by gen_jcc.

---
 target/i386/tcg/translate.c | 8 --------
 target/i386/tcg/emit.c.inc  | 6 +++++-
 2 files changed, 5 insertions(+), 9 deletions(-)
target/i386/tcg/translate.c +0 -8
index 5d729e68c98..298842d46b6 100644
@@ -1846,14 +1846,6 @@ static void gen_conditional_jump_labels(DisasContext *s, target_long diff,
     gen_jmp_rel(s, s->dflag, diff, 0);
 }
 
-static void gen_jcc(DisasContext *s, int b, int diff)
-{
-    TCGLabel *l1 = gen_new_label();
-
-    gen_jcc1(s, b, l1);
-    gen_conditional_jump_labels(s, diff, NULL, l1);
-}
-
 static void gen_cmovcc1(DisasContext *s, int b, TCGv dest, TCGv src)
 {
     CCPrepare cc = gen_prepare_cc(s, b, NULL);
target/i386/tcg/emit.c.inc +5 -1
index 785ff63f2ac..8d18407facf 100644
@@ -2276,8 +2276,12 @@ static void gen_IRET(DisasContext *s, X86DecodedInsn *decode)
 
 static void gen_Jcc(DisasContext *s, X86DecodedInsn *decode)
 {
+    TCGLabel *taken = gen_new_label();
+
     gen_bnd_jmp(s);
-    gen_jcc(s, decode->b & 0xf, decode->immediate);
+    gen_jcc1(s, decode->b & 0xf, taken);
+    gen_conditional_jump_labels(s, decode->immediate, NULL, taken);
+
 }
 
 static void gen_JCXZ(DisasContext *s, X86DecodedInsn *decode)
Signature · 1 line
2.47.1
Reviewed-by on v1 1/13
#2 lore raw

Re: [PATCH 01/13] target/i386: inline gen_jcc into sole caller

Quoted 34 lines · Paolo Bonzini

The code of gen_Jcc is very similar to gen_LOOP* and gen_JCXZ, but this is hidden by gen_jcc.

---
  target/i386/tcg/translate.c | 8 --------
  target/i386/tcg/emit.c.inc  | 6 +++++-
  2 files changed, 5 insertions(+), 9 deletions(-)
target/i386/tcg/translate.c +0 -8
index 5d729e68c98..298842d46b6 100644
@@ -1846,14 +1846,6 @@ static void gen_conditional_jump_labels(DisasContext *s, target_long diff,
      gen_jmp_rel(s, s->dflag, diff, 0);
  }
 
-static void gen_jcc(DisasContext *s, int b, int diff)
-{
-    TCGLabel *l1 = gen_new_label();
-
-    gen_jcc1(s, b, l1);
-    gen_conditional_jump_labels(s, diff, NULL, l1);
-}
-
  static void gen_cmovcc1(DisasContext *s, int b, TCGv dest, TCGv src)
  {
      CCPrepare cc = gen_prepare_cc(s, b, NULL);
target/i386/tcg/emit.c.inc +0 -0
index 785ff63f2ac..8d18407facf 100644
@@ -2276,8 +2276,12 @@ static void gen_IRET(DisasContext *s, X86DecodedInsn *decode)
 

target/i386/tcg/emit.c.inc static void gen_IRET(DisasContext *s, X86DecodedInsn *decode) · line 2285

Quoted 10 lines
target/i386/tcg/emit.c.inc +5 -1
  static void gen_Jcc(DisasContext *s, X86DecodedInsn *decode)
  {
+    TCGLabel *taken = gen_new_label();
+
      gen_bnd_jmp(s);
-    gen_jcc(s, decode->b & 0xf, decode->immediate);
+    gen_jcc1(s, decode->b & 0xf, taken);
+    gen_conditional_jump_labels(s, decode->immediate, NULL, taken);
+
  }

Watch the extra blank line. Otherwise,

r~

#3 lore raw

PATCH 2/13 target/i386: remove trailing 1 from gen_{j, cmov, set}cc1

In reply to Paolo Bonzini

This is not needed anymore now that gen_jcc has been eliminated (merged into the similarly-named gen_Jcc, where the uppercase letter gives away that it is an emission function).

---
 target/i386/tcg/translate.c | 14 +++++++-------
 target/i386/tcg/emit.c.inc  | 10 +++++-----
 2 files changed, 12 insertions(+), 12 deletions(-)
target/i386/tcg/translate.c +7 -7
index 298842d46b6..99e989d4b22 100644
@@ -1147,7 +1147,7 @@ static CCPrepare gen_prepare_cc(DisasContext *s, int b, TCGv reg)
     return cc;
 }
 
-static void gen_setcc1(DisasContext *s, int b, TCGv reg)
+static void gen_setcc(DisasContext *s, int b, TCGv reg)
 {
     CCPrepare cc = gen_prepare_cc(s, b, reg);
 
@@ -1169,12 +1169,12 @@ static void gen_setcc1(DisasContext *s, int b, TCGv reg)
 
 static inline void gen_compute_eflags_c(DisasContext *s, TCGv reg)
 {
-    gen_setcc1(s, JCC_B << 1, reg);
+    gen_setcc(s, JCC_B << 1, reg);
 }
 
 /* generate a conditional jump to label 'l1' according to jump opcode
    value 'b'. In the fast case, T0 is guaranteed not to be used. */
-static inline void gen_jcc1_noeob(DisasContext *s, int b, TCGLabel *l1)
+static inline void gen_jcc_noeob(DisasContext *s, int b, TCGLabel *l1)
 {
     CCPrepare cc = gen_prepare_cc(s, b, NULL);
 
@@ -1189,7 +1189,7 @@ static inline void gen_jcc1_noeob(DisasContext *s, int b, TCGLabel *l1)
    value 'b'. In the fast case, T0 is guaranteed not to be used.
    One or both of the branches will call gen_jmp_rel, so ensure
    cc_op is clean.  */
-static inline void gen_jcc1(DisasContext *s, int b, TCGLabel *l1)
+static inline void gen_jcc(DisasContext *s, int b, TCGLabel *l1)
 {
     CCPrepare cc = gen_prepare_cc(s, b, NULL);
 
@@ -1336,7 +1336,7 @@ static void gen_repz_nz(DisasContext *s, MemOp ot,
     l2 = gen_jz_ecx_string(s);
     fn(s, ot);
     gen_op_add_reg_im(s, s->aflag, R_ECX, -1);
-    gen_jcc1(s, (JCC_Z << 1) | (nz ^ 1), l2);
+    gen_jcc(s, (JCC_Z << 1) | (nz ^ 1), l2);
     if (s->repz_opt) {
         gen_op_jz_ecx(s, l2);
     }
@@ -1846,7 +1846,7 @@ static void gen_conditional_jump_labels(DisasContext *s, target_long diff,
     gen_jmp_rel(s, s->dflag, diff, 0);
 }
 
-static void gen_cmovcc1(DisasContext *s, int b, TCGv dest, TCGv src)
+static void gen_cmovcc(DisasContext *s, int b, TCGv dest, TCGv src)
 {
     CCPrepare cc = gen_prepare_cc(s, b, NULL);
 
@@ -2855,7 +2855,7 @@ static void gen_x87(DisasContext *s, X86DecodedInsn *decode)
                 }
                 op1 = fcmov_cc[op & 3] | (((op >> 3) & 1) ^ 1);
                 l1 = gen_new_label();
-                gen_jcc1_noeob(s, op1, l1);
+                gen_jcc_noeob(s, op1, l1);
                 gen_helper_fmov_ST0_STN(tcg_env,
                                         tcg_constant_i32(opreg));
                 gen_set_label(l1);
target/i386/tcg/emit.c.inc +5 -5
index 8d18407facf..a05ba019026 100644
@@ -1613,7 +1613,7 @@ static void gen_CMC(DisasContext *s, X86DecodedInsn *decode)
 
 static void gen_CMOVcc(DisasContext *s, X86DecodedInsn *decode)
 {
-    gen_cmovcc1(s, decode->b & 0xf, s->T0, s->T1);
+    gen_cmovcc(s, decode->b & 0xf, s->T0, s->T1);
 }
 
 static void gen_CMPccXADD(DisasContext *s, X86DecodedInsn *decode)
@@ -2279,7 +2279,7 @@ static void gen_Jcc(DisasContext *s, X86DecodedInsn *decode)
     TCGLabel *taken = gen_new_label();
 
     gen_bnd_jmp(s);
-    gen_jcc1(s, decode->b & 0xf, taken);
+    gen_jcc(s, decode->b & 0xf, taken);
     gen_conditional_jump_labels(s, decode->immediate, NULL, taken);
 
 }
@@ -2431,7 +2431,7 @@ static void gen_LOOPE(DisasContext *s, X86DecodedInsn *decode)
     gen_update_cc_op(s);
     gen_op_add_reg_im(s, s->aflag, R_ECX, -1);
     gen_op_jz_ecx(s, not_taken);
-    gen_jcc1(s, (JCC_Z << 1), taken); /* jz taken */
+    gen_jcc(s, (JCC_Z << 1), taken); /* jz taken */
     gen_conditional_jump_labels(s, decode->immediate, not_taken, taken);
 }
 
@@ -2443,7 +2443,7 @@ static void gen_LOOPNE(DisasContext *s, X86DecodedInsn *decode)
     gen_update_cc_op(s);
     gen_op_add_reg_im(s, s->aflag, R_ECX, -1);
     gen_op_jz_ecx(s, not_taken);
-    gen_jcc1(s, (JCC_Z << 1) | 1, taken); /* jnz taken */
+    gen_jcc(s, (JCC_Z << 1) | 1, taken); /* jnz taken */
     gen_conditional_jump_labels(s, decode->immediate, not_taken, taken);
 }
 
@@ -3868,7 +3868,7 @@ static void gen_SCAS(DisasContext *s, X86DecodedInsn *decode)
 
 static void gen_SETcc(DisasContext *s, X86DecodedInsn *decode)
 {
-    gen_setcc1(s, decode->b & 0xf, s->T0);
+    gen_setcc(s, decode->b & 0xf, s->T0);
 }
 
 static void gen_SFENCE(DisasContext *s, X86DecodedInsn *decode)
Signature · 1 line
2.47.1
Reviewed-by on v1 2/13
#4 lore raw

Re: [PATCH 02/13] target/i386: remove trailing 1 from gen_{j,cmov,set}cc1

Quoted 9 lines · Paolo Bonzini

This is not needed anymore now that gen_jcc has been eliminated (merged into the similarly-named gen_Jcc, where the uppercase letter gives away that it is an emission function).

---
  target/i386/tcg/translate.c | 14 +++++++-------
  target/i386/tcg/emit.c.inc  | 10 +++++-----
  2 files changed, 12 insertions(+), 12 deletions(-)

r~

#5 lore raw

PATCH 3/13 target/i386: unify REP and REPZ/REPNZ generation

In reply to Paolo Bonzini

It only differs in a single call to gen_jcc, so use a "bool" argument to distinguish the two cases; do not duplicate code.

---
 target/i386/tcg/translate.c | 39 +++++++++++++++++++------------------
 1 file changed, 20 insertions(+), 19 deletions(-)
target/i386/tcg/translate.c +20 -19
index 99e989d4b22..8bf6aa1fcf6 100644
@@ -1309,14 +1309,18 @@ static void gen_outs(DisasContext *s, MemOp ot)
     gen_bpt_io(s, s->tmp2_i32, ot);
 }
 
-/* Generate jumps to current or next instruction */
-static void gen_repz(DisasContext *s, MemOp ot,
-                     void (*fn)(DisasContext *s, MemOp ot))
+static void do_gen_rep(DisasContext *s, MemOp ot,
+                       void (*fn)(DisasContext *s, MemOp ot),
+                       bool is_repz_nz)
 {
     TCGLabel *l2;
     l2 = gen_jz_ecx_string(s);
     fn(s, ot);
     gen_op_add_reg_im(s, s->aflag, R_ECX, -1);
+    if (is_repz_nz) {
+        int nz = (s->prefix & PREFIX_REPNZ) ? 1 : 0;
+        gen_jcc(s, (JCC_Z << 1) | (nz ^ 1), l2);
+    }
     /*
      * A loop would cause two single step exceptions if ECX = 1
      * before rep string_insn
@@ -1324,28 +1328,25 @@ static void gen_repz(DisasContext *s, MemOp ot,
     if (s->repz_opt) {
         gen_op_jz_ecx(s, l2);
     }
+    /*
+     * For CMPS/SCAS there is no need to set CC_OP_DYNAMIC: only one iteration
+     * is done at a time, so the translation block ends unconditionally after
+     * this instruction and there is no control flow junction.
+     */
     gen_jmp_rel_csize(s, -cur_insn_len(s), 0);
 }
 
+static void gen_repz(DisasContext *s, MemOp ot,
+                     void (*fn)(DisasContext *s, MemOp ot))
+
+{
+    do_gen_rep(s, ot, fn, false);
+}
+
 static void gen_repz_nz(DisasContext *s, MemOp ot,
                         void (*fn)(DisasContext *s, MemOp ot))
 {
-    TCGLabel *l2;
-    int nz = (s->prefix & PREFIX_REPNZ) ? 1 : 0;
-
-    l2 = gen_jz_ecx_string(s);
-    fn(s, ot);
-    gen_op_add_reg_im(s, s->aflag, R_ECX, -1);
-    gen_jcc(s, (JCC_Z << 1) | (nz ^ 1), l2);
-    if (s->repz_opt) {
-        gen_op_jz_ecx(s, l2);
-    }
-    /*
-     * Only one iteration is done at a time, so the translation
-     * block ends unconditionally after this instruction and there
-     * is no control flow junction - no need to set CC_OP_DYNAMIC.
-     */
-    gen_jmp_rel_csize(s, -cur_insn_len(s), 0);
+    do_gen_rep(s, ot, fn, true);
 }
 
 static void gen_helper_fp_arith_ST0_FT0(int op)
Signature · 1 line
2.47.1
Reviewed-by on v1 3/13
#6 lore raw

Re: [PATCH 03/13] target/i386: unify REP and REPZ/REPNZ generation

Quoted 7 lines · Paolo Bonzini

It only differs in a single call to gen_jcc, so use a "bool" argument to distinguish the two cases; do not duplicate code.

---
  target/i386/tcg/translate.c | 39 +++++++++++++++++++------------------
  1 file changed, 20 insertions(+), 19 deletions(-)

r~

#7 lore raw

PATCH 4/13 target/i386: unify choice between single and repeated string instructions

In reply to Paolo Bonzini

The same "if" is present in all generator functions for string instructions. Push it inside gen_repz() and gen_repz_nz() instead.

---
 target/i386/tcg/translate.c | 12 +++++++++--
 target/i386/tcg/emit.c.inc  | 42 +++++++------------------------------
 2 files changed, 17 insertions(+), 37 deletions(-)
target/i386/tcg/translate.c +10 -2
index 8bf6aa1fcf6..63a39d9f15a 100644
@@ -1340,13 +1340,21 @@ static void gen_repz(DisasContext *s, MemOp ot,
                      void (*fn)(DisasContext *s, MemOp ot))
 
 {
-    do_gen_rep(s, ot, fn, false);
+    if (s->prefix & (PREFIX_REPZ | PREFIX_REPNZ)) {
+        do_gen_rep(s, ot, fn, false);
+    } else {
+        fn(s, ot);
+    }
 }
 
 static void gen_repz_nz(DisasContext *s, MemOp ot,
                         void (*fn)(DisasContext *s, MemOp ot))
 {
-    do_gen_rep(s, ot, fn, true);
+    if (s->prefix & (PREFIX_REPZ | PREFIX_REPNZ)) {
+        do_gen_rep(s, ot, fn, true);
+    } else {
+        fn(s, ot);
+    }
 }
 
 static void gen_helper_fp_arith_ST0_FT0(int op)
target/i386/tcg/emit.c.inc +7 -35

84 lines not on this page · raw message

Signature · 1 line
2.47.1
#8 lore raw

Re: [PATCH 04/13] target/i386: unify choice between single and repeated string instructions

Quoted 25 lines · Paolo Bonzini

The same "if" is present in all generator functions for string instructions. Push it inside gen_repz() and gen_repz_nz() instead.

---
  target/i386/tcg/translate.c | 12 +++++++++--
  target/i386/tcg/emit.c.inc  | 42 +++++++------------------------------
  2 files changed, 17 insertions(+), 37 deletions(-)
target/i386/tcg/translate.c +5 -1
index 8bf6aa1fcf6..63a39d9f15a 100644
@@ -1340,13 +1340,21 @@ static void gen_repz(DisasContext *s, MemOp ot,
                       void (*fn)(DisasContext *s, MemOp ot))
 
  {
-    do_gen_rep(s, ot, fn, false);
+    if (s->prefix & (PREFIX_REPZ | PREFIX_REPNZ)) {
+        do_gen_rep(s, ot, fn, false);
+    } else {
+        fn(s, ot);
+    }
  }
 

target/i386/tcg/translate.c static void gen_repz(DisasContext *s, MemOp ot, · line 1358

Quoted 10 lines
target/i386/tcg/translate.c +5 -1
  static void gen_repz_nz(DisasContext *s, MemOp ot,
                          void (*fn)(DisasContext *s, MemOp ot))
  {
-    do_gen_rep(s, ot, fn, true);
+    if (s->prefix & (PREFIX_REPZ | PREFIX_REPNZ)) {
+        do_gen_rep(s, ot, fn, true);
+    } else {
+        fn(s, ot);
+    }
  }

Why not push it into do_gen_rep?

r~

#9 lore raw

Re: [PATCH 04/13] target/i386: unify choice between single and repeated string instructions

Il dom 15 dic 2024, 15:10 Richard Henderson <[email protected]> ha scritto:

Quoted 27 lines · Paolo Bonzini
Quoted 1 line

The same "if" is present in all generator functions for string

instructions.

Quoted 24 lines

Push it inside gen_repz() and gen_repz_nz() instead.

---
  target/i386/tcg/translate.c | 12 +++++++++--
  target/i386/tcg/emit.c.inc  | 42 +++++++------------------------------
  2 files changed, 17 insertions(+), 37 deletions(-)
target/i386/tcg/translate.c +5 -1
index 8bf6aa1fcf6..63a39d9f15a 100644
@@ -1340,13 +1340,21 @@ static void gen_repz(DisasContext *s, MemOp ot,
                       void (*fn)(DisasContext *s, MemOp ot))
 
  {
-    do_gen_rep(s, ot, fn, false);
+    if (s->prefix & (PREFIX_REPZ | PREFIX_REPNZ)) {
+        do_gen_rep(s, ot, fn, false);
+    } else {
+        fn(s, ot);
+    }
  }
 
Quoted 13 lines

target/i386/tcg/translate.c static void gen_repz(DisasContext *s, MemOp ot, · line 1358

Quoted 10 lines
target/i386/tcg/translate.c +5 -1
  static void gen_repz_nz(DisasContext *s, MemOp ot,
                          void (*fn)(DisasContext *s, MemOp ot))
  {
-    do_gen_rep(s, ot, fn, true);
+    if (s->prefix & (PREFIX_REPZ | PREFIX_REPNZ)) {
+        do_gen_rep(s, ot, fn, true);
+    } else {
+        fn(s, ot);
+    }
  }

Why not push it into do_gen_rep?

Just because do_gen_rep is already complicated enough by the end of the series.

Paolo

Quoted 3 lines

r~

#10 lore raw

PATCH 5/13 target/i386: reorganize ops emitted by do_gen_rep, drop repz_opt

In reply to Paolo Bonzini

The condition for optimizing repeat instruction is more or less the opposite of what you imagine: almost always the string instruction was _not_ optimized and optimizing the loop relied on goto_tb. This is obviously not great for performance, due to the cost of the exit-to-main-loop check, but also wrong. In fact, after expanding dc->jmp_opt and simplifying "!!x" to "x", the condition for looping used to be:

   ((cflags & CF_NO_GOTO_TB) ||
    (flags & (HF_RF_MASK | HF_TF_MASK | HF_INHIBIT_IRQ_MASK))) && !(cflags & CF_USE_ICOUNT)

In other words, setting aside RF (it requires special handling for REP instructions and it was completely missing), repeat instruction were being optimized if TF or inhibit IRQ flags were set. This is certainly wrong for TF, because string instructions trap after every execution, and probably for interrupt shadow too.

Get rid of repz_opt completely. The next patches will reintroduce the optimization, applying it in the common case instead of the unlikely and wrong one.

While at it, place the CX/ECX/RCX=0 case is at the end of the function, which saves a label and is clearer when reading the generated ops. For clarity, mark the cc_op explicitly as DYNAMIC even if at the end of the translation block; the cc_op can come from either the previous instruction or the string instruction, and currently we rely on a gen_update_cc_op() that is hidden in the bowels of gen_jcc() to spill cc_op and mark it clean.

---
 target/i386/tcg/translate.c | 60 ++++++++-----------------------------
 1 file changed, 13 insertions(+), 47 deletions(-)
target/i386/tcg/translate.c +13 -47
index 63a39d9f15a..3732d05d5f5 100644
@@ -112,7 +112,6 @@ typedef struct DisasContext {
 #endif
     bool vex_w; /* used by AVX even on 32-bit processors */
     bool jmp_opt; /* use direct block chaining for direct jumps */
-    bool repz_opt; /* optimize jumps within repz instructions */
     bool cc_op_dirty;
 
     CCOp cc_op;  /* current CC operation */
@@ -1205,23 +1204,6 @@ static inline void gen_jcc(DisasContext *s, int b, TCGLabel *l1)
     }
 }
 
-/* XXX: does not work with gdbstub "ice" single step - not a
-   serious problem.  The caller can jump to the returned label
-   to stop the REP but, if the flags have changed, it has to call
-   gen_update_cc_op before doing so.  */
-static TCGLabel *gen_jz_ecx_string(DisasContext *s)
-{
-    TCGLabel *l1 = gen_new_label();
-    TCGLabel *l2 = gen_new_label();
-
-    gen_update_cc_op(s);
-    gen_op_jnz_ecx(s, l1);
-    gen_set_label(l2);
-    gen_jmp_rel_csize(s, 0, 1);
-    gen_set_label(l1);
-    return l2;
-}
-
 static void gen_stos(DisasContext *s, MemOp ot)
 {
     gen_string_movl_A0_EDI(s);
@@ -1313,27 +1295,25 @@ static void do_gen_rep(DisasContext *s, MemOp ot,
                        void (*fn)(DisasContext *s, MemOp ot),
                        bool is_repz_nz)
 {
-    TCGLabel *l2;
-    l2 = gen_jz_ecx_string(s);
+    TCGLabel *done = gen_new_label();
+
+    gen_update_cc_op(s);
+    gen_op_jz_ecx(s, done);
+
     fn(s, ot);
     gen_op_add_reg_im(s, s->aflag, R_ECX, -1);
     if (is_repz_nz) {
         int nz = (s->prefix & PREFIX_REPNZ) ? 1 : 0;
-        gen_jcc(s, (JCC_Z << 1) | (nz ^ 1), l2);
+        gen_jcc(s, (JCC_Z << 1) | (nz ^ 1), done);
     }
-    /*
-     * A loop would cause two single step exceptions if ECX = 1
-     * before rep string_insn
-     */
-    if (s->repz_opt) {
-        gen_op_jz_ecx(s, l2);
-    }
-    /*
-     * For CMPS/SCAS there is no need to set CC_OP_DYNAMIC: only one iteration
-     * is done at a time, so the translation block ends unconditionally after
-     * this instruction and there is no control flow junction.
-     */
+
+    /* Go to the main loop but reenter the same instruction.  */
     gen_jmp_rel_csize(s, -cur_insn_len(s), 0);
+
+    /* CX/ECX/RCX is zero, or REPZ/REPNZ broke the repetition.  */
+    gen_set_label(done);
+    set_cc_op(s, CC_OP_DYNAMIC);
+    gen_jmp_rel_csize(s, 0, 1);
 }
 
 static void gen_repz(DisasContext *s, MemOp ot,
@@ -3664,20 +3644,6 @@ static void i386_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cpu)
     dc->cpuid_xsave_features = env->features[FEAT_XSAVE];
     dc->jmp_opt = !((cflags & CF_NO_GOTO_TB) ||
                     (flags & (HF_RF_MASK | HF_TF_MASK | HF_INHIBIT_IRQ_MASK)));
-    /*
-     * If jmp_opt, we want to handle each string instruction individually.
-     * For icount also disable repz optimization so that each iteration
-     * is accounted separately.
-     *
-     * FIXME: this is messy; it makes REP string instructions a lot less
-     * efficient than they should be and it gets in the way of correct
-     * handling of RF (interrupts or traps arriving after any iteration
-     * of a repeated string instruction but the last should set RF to 1).
-     * Perhaps it would be more efficient if REP string instructions were
-     * always at the beginning of the TB, or even their own TB?  That
-     * would even allow accounting up to 64k iterations at once for icount.
-     */
-    dc->repz_opt = !dc->jmp_opt && !(cflags & CF_USE_ICOUNT);
 
     dc->T0 = tcg_temp_new();
     dc->T1 = tcg_temp_new();
Signature · 1 line
2.47.1
Reviewed-by on v1 5/13
#11 lore raw

Re: [PATCH 05/13] target/i386: reorganize ops emitted by do_gen_rep, drop repz_opt

Quoted 29 lines · Paolo Bonzini

The condition for optimizing repeat instruction is more or less the opposite of what you imagine: almost always the string instruction was_not_ optimized and optimizing the loop relied on goto_tb. This is obviously not great for performance, due to the cost of the exit-to-main-loop check, but also wrong. In fact, after expanding dc->jmp_opt and simplifying "!!x" to "x", the condition for looping used to be:

    ((cflags & CF_NO_GOTO_TB) ||
     (flags & (HF_RF_MASK | HF_TF_MASK | HF_INHIBIT_IRQ_MASK))) && !(cflags & CF_USE_ICOUNT)

In other words, setting aside RF (it requires special handling for REP instructions and it was completely missing), repeat instruction were being optimized if TF or inhibit IRQ flags were set. This is certainly wrong for TF, because string instructions trap after every execution, and probably for interrupt shadow too.

Get rid of repz_opt completely. The next patches will reintroduce the optimization, applying it in the common case instead of the unlikely and wrong one.

While at it, place the CX/ECX/RCX=0 case is at the end of the function, which saves a label and is clearer when reading the generated ops. For clarity, mark the cc_op explicitly as DYNAMIC even if at the end of the translation block; the cc_op can come from either the previous instruction or the string instruction, and currently we rely on a gen_update_cc_op() that is hidden in the bowels of gen_jcc() to spill cc_op and mark it clean.

Quoted 4 lines
---
  target/i386/tcg/translate.c | 60 ++++++++-----------------------------
  1 file changed, 13 insertions(+), 47 deletions(-)

It might have been clearer inlining gen_jz_ecx_string as a separate step, but no need to do that now. Yes, this is much clearer code generation.

r~

#12 lore raw

PATCH 6/13 target/i386: tcg: move gen_set/reset_* earlier in the file

In reply to Paolo Bonzini

Allow using them in the code that translates REP/REPZ, without forward declarations.

---
 target/i386/tcg/translate.c | 80 ++++++++++++++++++-------------------
 1 file changed, 40 insertions(+), 40 deletions(-)
target/i386/tcg/translate.c +40 -40
index 3732d05d5f5..0fe7ed4ec8d 100644
@@ -724,6 +724,46 @@ static inline void gen_op_jnz_ecx(DisasContext *s, TCGLabel *label1)
     gen_op_j_ecx(s, TCG_COND_NE, label1);
 }
 
+static void gen_set_hflag(DisasContext *s, uint32_t mask)
+{
+    if ((s->flags & mask) == 0) {
+        TCGv_i32 t = tcg_temp_new_i32();
+        tcg_gen_ld_i32(t, tcg_env, offsetof(CPUX86State, hflags));
+        tcg_gen_ori_i32(t, t, mask);
+        tcg_gen_st_i32(t, tcg_env, offsetof(CPUX86State, hflags));
+        s->flags |= mask;
+    }
+}
+
+static void gen_reset_hflag(DisasContext *s, uint32_t mask)
+{
+    if (s->flags & mask) {
+        TCGv_i32 t = tcg_temp_new_i32();
+        tcg_gen_ld_i32(t, tcg_env, offsetof(CPUX86State, hflags));
+        tcg_gen_andi_i32(t, t, ~mask);
+        tcg_gen_st_i32(t, tcg_env, offsetof(CPUX86State, hflags));
+        s->flags &= ~mask;
+    }
+}
+
+static void gen_set_eflags(DisasContext *s, target_ulong mask)
+{
+    TCGv t = tcg_temp_new();
+
+    tcg_gen_ld_tl(t, tcg_env, offsetof(CPUX86State, eflags));
+    tcg_gen_ori_tl(t, t, mask);
+    tcg_gen_st_tl(t, tcg_env, offsetof(CPUX86State, eflags));
+}
+
+static void gen_reset_eflags(DisasContext *s, target_ulong mask)
+{
+    TCGv t = tcg_temp_new();
+
+    tcg_gen_ld_tl(t, tcg_env, offsetof(CPUX86State, eflags));
+    tcg_gen_andi_tl(t, t, ~mask);
+    tcg_gen_st_tl(t, tcg_env, offsetof(CPUX86State, eflags));
+}
+
 static void gen_helper_in_func(MemOp ot, TCGv v, TCGv_i32 n)
 {
     switch (ot) {
@@ -2083,46 +2123,6 @@ static void gen_interrupt(DisasContext *s, uint8_t intno)
     s->base.is_jmp = DISAS_NORETURN;
 }
 
-static void gen_set_hflag(DisasContext *s, uint32_t mask)
-{
-    if ((s->flags & mask) == 0) {
-        TCGv_i32 t = tcg_temp_new_i32();
-        tcg_gen_ld_i32(t, tcg_env, offsetof(CPUX86State, hflags));
-        tcg_gen_ori_i32(t, t, mask);
-        tcg_gen_st_i32(t, tcg_env, offsetof(CPUX86State, hflags));
-        s->flags |= mask;
-    }
-}
-
-static void gen_reset_hflag(DisasContext *s, uint32_t mask)
-{
-    if (s->flags & mask) {
-        TCGv_i32 t = tcg_temp_new_i32();
-        tcg_gen_ld_i32(t, tcg_env, offsetof(CPUX86State, hflags));
-        tcg_gen_andi_i32(t, t, ~mask);
-        tcg_gen_st_i32(t, tcg_env, offsetof(CPUX86State, hflags));
-        s->flags &= ~mask;
-    }
-}
-
-static void gen_set_eflags(DisasContext *s, target_ulong mask)
-{
-    TCGv t = tcg_temp_new();
-
-    tcg_gen_ld_tl(t, tcg_env, offsetof(CPUX86State, eflags));
-    tcg_gen_ori_tl(t, t, mask);
-    tcg_gen_st_tl(t, tcg_env, offsetof(CPUX86State, eflags));
-}
-
-static void gen_reset_eflags(DisasContext *s, target_ulong mask)
-{
-    TCGv t = tcg_temp_new();
-
-    tcg_gen_ld_tl(t, tcg_env, offsetof(CPUX86State, eflags));
-    tcg_gen_andi_tl(t, t, ~mask);
-    tcg_gen_st_tl(t, tcg_env, offsetof(CPUX86State, eflags));
-}
-
 /* Clear BND registers during legacy branches.  */
 static void gen_bnd_jmp(DisasContext *s)
 {
Signature · 1 line
2.47.1
Reviewed-by on v1 6/13
#13 lore raw

Re: [PATCH 06/13] target/i386: tcg: move gen_set/reset_* earlier in the file

Quoted 7 lines · Paolo Bonzini

Allow using them in the code that translates REP/REPZ, without forward declarations.

---
  target/i386/tcg/translate.c | 80 ++++++++++++++++++-------------------
  1 file changed, 40 insertions(+), 40 deletions(-)

r~

#14 lore raw

PATCH 7/13 target/i386: fix RF handling for string instructions

In reply to Paolo Bonzini

RF must be set on traps and interrupts from a string instruction, except if they occur after the last iteration. Ensure it is set before giving the main loop a chance to execute.

---
 target/i386/tcg/translate.c | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)
target/i386/tcg/translate.c +21 -1
index 0fe7ed4ec8d..4ff0ccf71cb 100644
@@ -1336,6 +1336,14 @@ static void do_gen_rep(DisasContext *s, MemOp ot,
                        bool is_repz_nz)
 {
     TCGLabel *done = gen_new_label();
+    bool had_rf = s->flags & HF_RF_MASK;
+
+    /*
+     * Even if EFLAGS.RF was set on entry (such as if we're on the second or
+     * later iteration and an exception or interrupt happened), force gen_eob()
+     * not to clear the flag.  We do that ourselves after the last iteration.
+     */
+    s->flags &= ~HF_RF_MASK;
 
     gen_update_cc_op(s);
     gen_op_jz_ecx(s, done);
@@ -1347,12 +1355,24 @@ static void do_gen_rep(DisasContext *s, MemOp ot,
         gen_jcc(s, (JCC_Z << 1) | (nz ^ 1), done);
     }
 
+    /*
+     * Traps or interrupts set RF_MASK if they happen after any iteration
+     * but the last.  Set it here before giving the main loop a chance to
+     * execute.  (For faults, seg_helper.c sets the flag as usual).
+     */
+    if (!had_rf) {
+        gen_set_eflags(s, RF_MASK);
+    }
+
     /* Go to the main loop but reenter the same instruction.  */
     gen_jmp_rel_csize(s, -cur_insn_len(s), 0);
 
     /* CX/ECX/RCX is zero, or REPZ/REPNZ broke the repetition.  */
     gen_set_label(done);
     set_cc_op(s, CC_OP_DYNAMIC);
+    if (had_rf) {
+        gen_reset_eflags(s, RF_MASK);
+    }
     gen_jmp_rel_csize(s, 0, 1);
 }
 
@@ -2157,7 +2177,7 @@ gen_eob(DisasContext *s, int mode)
         gen_set_hflag(s, HF_INHIBIT_IRQ_MASK);
     }
 
-    if (s->base.tb->flags & HF_RF_MASK) {
+    if (s->flags & HF_RF_MASK) {
         gen_reset_eflags(s, RF_MASK);
     }
     if (mode == DISAS_EOB_RECHECK_TF) {
Signature · 1 line
2.47.1
Reviewed-by on v1 7/13
#15 lore raw

Re: [PATCH 07/13] target/i386: fix RF handling for string instructions

Quoted 8 lines · Paolo Bonzini

RF must be set on traps and interrupts from a string instruction, except if they occur after the last iteration. Ensure it is set before giving the main loop a chance to execute.

---
  target/i386/tcg/translate.c | 22 +++++++++++++++++++++-
  1 file changed, 21 insertions(+), 1 deletion(-)

r~

#16 lore raw

PATCH 8/13 target/i386: make cc_op handling more explicit for repeated string instructions.

In reply to Paolo Bonzini

Since the cost of gen_update_cc_op() must be paid anyway, it's easier to place them manually and not rely on spilling that is buried under multiple levels of function calls.

And since cc_op will have been spilled at the point of a fault, just make the whole insn CC_OP_DYNAMIC. Once repz_opt is reintroduced, a fault could happen either before or after the first execution of CMPS/SCAS, and CC_OP_DYNAMIC sidesteps the complicated matter of what x86_restore_state_to_opc would do.

---
 target/i386/tcg/translate.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
target/i386/tcg/translate.c +16 -1
index 4ff0ccf71cb..4b53a47000e 100644
@@ -1345,14 +1345,29 @@ static void do_gen_rep(DisasContext *s, MemOp ot,
      */
     s->flags &= ~HF_RF_MASK;
 
+    /*
+     * For CMPS/SCAS, the CC_OP after a memory fault could come from either
+     * the previous instruction or the string instruction; but because we
+     * arrange to keep CC_OP up to date all the time, just mark the whole
+     * insn as CC_OP_DYNAMIC.
+     *
+     * It's not a problem to do this even for instructions that do not
+     * modify the flags, so do it unconditionally.
+     */
     gen_update_cc_op(s);
+    tcg_set_insn_start_param(s->base.insn_start, 1, CC_OP_DYNAMIC);
+
+    /* Any iteration at all?  */
     gen_op_jz_ecx(s, done);
 
     fn(s, ot);
     gen_op_add_reg_im(s, s->aflag, R_ECX, -1);
+    gen_update_cc_op(s);
+
+    /* Leave if REP condition fails.  */
     if (is_repz_nz) {
         int nz = (s->prefix & PREFIX_REPNZ) ? 1 : 0;
-        gen_jcc(s, (JCC_Z << 1) | (nz ^ 1), done);
+        gen_jcc_noeob(s, (JCC_Z << 1) | (nz ^ 1), done);
     }
 
     /*
Signature · 1 line
2.47.1
#17 lore raw

Re: [PATCH 08/13] target/i386: make cc_op handling more explicit for repeated string instructions.

target/i386/tcg/translate.c static void do_gen_rep(DisasContext *s, MemOp ot, · line 1370

Quoted 9 lines · Paolo Bonzini
      fn(s, ot);
      gen_op_add_reg_im(s, s->aflag, R_ECX, -1);
target/i386/tcg/translate.c +4 -1
+    gen_update_cc_op(s);
+
+    /* Leave if REP condition fails.  */
      if (is_repz_nz) {
          int nz = (s->prefix & PREFIX_REPNZ) ? 1 : 0;
-        gen_jcc(s, (JCC_Z << 1) | (nz ^ 1), done);
+        gen_jcc_noeob(s, (JCC_Z << 1) | (nz ^ 1), done);

The comment in gen_jcc would still seem to apply:

    CCPrepare cc = gen_prepare_cc(s, b, NULL);

    /*
     * Note that this must be _after_ gen_prepare_cc, because it
     * can change the cc_op from CC_OP_DYNAMIC to CC_OP_EFLAGS!
     */
    gen_update_cc_op(s);

via any path through gen_prepare_cc that reaches gen_compute_eflags.

However! Because this is JCC_Z, we will never call gen_compute_eflags, we will always go through the gen_prepare_eflags_z, which doesn't have the same problem.

This subtlety deserves a comment and maybe an assert. Perhaps

    gen_jcc_noeob(...);
    assert(!s->cc_op_dirty);

r~

#18 lore raw

Re: [PATCH 08/13] target/i386: make cc_op handling more explicit for repeated string instructions.

Il dom 15 dic 2024, 15:45 Richard Henderson <[email protected]> ha scritto:

Quoted 21 lines · Paolo Bonzini

target/i386/tcg/translate.c static void do_gen_rep(DisasContext *s, MemOp ot, · line 1370

Quoted 9 lines
      fn(s, ot);
      gen_op_add_reg_im(s, s->aflag, R_ECX, -1);
target/i386/tcg/translate.c +4 -1
+    gen_update_cc_op(s);
+
+    /* Leave if REP condition fails.  */
      if (is_repz_nz) {
          int nz = (s->prefix & PREFIX_REPNZ) ? 1 : 0;
-        gen_jcc(s, (JCC_Z << 1) | (nz ^ 1), done);
+        gen_jcc_noeob(s, (JCC_Z << 1) | (nz ^ 1), done);

The comment in gen_jcc would still seem to apply:

     CCPrepare cc = gen_prepare_cc(s, b, NULL);

     /*
      * Note that this must be _after_ gen_prepare_cc, because it
      * can change the cc_op from CC_OP_DYNAMIC to CC_OP_EFLAGS!
      */
     gen_update_cc_op(s);
Quoted 6 lines

via any path through gen_prepare_cc that reaches gen_compute_eflags.

However! Because this is JCC_Z, we will never call gen_compute_eflags, we will always go
through the gen_prepare_eflags_z, which doesn't have the same problem.

Or more simply, fn(s, ot) must have left CC_OP_SUBx in cc_op.

This subtlety deserves a comment and maybe an assert. Perhaps

Quoted 4 lines
     gen_jcc_noeob(...);
     assert(!s->cc_op_dirty);

Either that or an assert(s->cc_op != CC_OP_DYNAMIC) before the call to gen_jcc_noeob().

Paolo

Quoted 4 lines

r~

#19 lore raw

Re: [PATCH 08/13] target/i386: make cc_op handling more explicit for repeated string instructions.

Quoted 31 lines · Paolo Bonzini

Il dom 15 dic 2024, 15:45 Richard Henderson <[email protected] <mailto:[email protected]>> ha scritto:

    On 12/15/24 03:06, Paolo Bonzini wrote:
     >       fn(s, ot);
     >       gen_op_add_reg_im(s, s->aflag, R_ECX, -1);
     > +    gen_update_cc_op(s);
     > +
     > +    /* Leave if REP condition fails.  */
     >       if (is_repz_nz) {
     >           int nz = (s->prefix & PREFIX_REPNZ) ? 1 : 0;
     > -        gen_jcc(s, (JCC_Z << 1) | (nz ^ 1), done);
     > +        gen_jcc_noeob(s, (JCC_Z << 1) | (nz ^ 1), done);

    The comment in gen_jcc would still seem to apply:

          CCPrepare cc = gen_prepare_cc(s, b, NULL);

          /*
           * Note that this must be _after_ gen_prepare_cc, because it
           * can change the cc_op from CC_OP_DYNAMIC to CC_OP_EFLAGS!
           */
          gen_update_cc_op(s);

    via any path through gen_prepare_cc that reaches gen_compute_eflags.

    However!  Because this is JCC_Z, we will never call gen_compute_eflags, we will always go
    through the gen_prepare_eflags_z, which doesn't have the same problem.
Quoted 1 line

Or more simply, fn(s, ot) must have left CC_OP_SUBx in cc_op.

No, even CC_OP_SUBx can (and in this case, will) goto slow_jcc. My correctness analysis is strictly based on JCC_Z.

Quoted 7 lines
    This subtlety deserves a comment and maybe an assert.  Perhaps

          gen_jcc_noeob(...);
          assert(!s->cc_op_dirty);

Either that or an assert(s->cc_op != CC_OP_DYNAMIC) before the call to gen_jcc_noeob().

No, not before gen_jcc_noeob, since that's where any buggy change would occur.

r~

#20 lore raw

PATCH 9/13 target/i386: do not use gen_op_jz_ecx for repeated string operations

In reply to Paolo Bonzini

Explicitly generate a TSTEQ branch (which is optimized to NE x,0 if possible). This does not make much sense yet, but later we will add more checks and some will use a temporary to check on the decremented value of CX/ECX/RCX; it will be clearer for all checks to share the same logic using TSTEQ(reg, cx_mask).

---
 target/i386/tcg/translate.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
target/i386/tcg/translate.c +2 -1
index 4b53a47000e..8ef938372d5 100644
@@ -1336,6 +1336,7 @@ static void do_gen_rep(DisasContext *s, MemOp ot,
                        bool is_repz_nz)
 {
     TCGLabel *done = gen_new_label();
+    target_ulong cx_mask = MAKE_64BIT_MASK(0, 8 << s->aflag);
     bool had_rf = s->flags & HF_RF_MASK;
 
     /*
@@ -1358,7 +1359,7 @@ static void do_gen_rep(DisasContext *s, MemOp ot,
     tcg_set_insn_start_param(s->base.insn_start, 1, CC_OP_DYNAMIC);
 
     /* Any iteration at all?  */
-    gen_op_jz_ecx(s, done);
+    tcg_gen_brcondi_tl(TCG_COND_TSTEQ, cpu_regs[R_ECX], cx_mask, done);
 
     fn(s, ot);
     gen_op_add_reg_im(s, s->aflag, R_ECX, -1);
Signature · 1 line
2.47.1
Reviewed-by on v1 9/13
#21 lore raw

Re: [PATCH 09/13] target/i386: do not use gen_op_jz_ecx for repeated string operations

Quoted 23 lines · Paolo Bonzini

Explicitly generate a TSTEQ branch (which is optimized to NE x,0 if possible). This does not make much sense yet, but later we will add more checks and some will use a temporary to check on the decremented value of CX/ECX/RCX; it will be clearer for all checks to share the same logic using TSTEQ(reg, cx_mask).

---
  target/i386/tcg/translate.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
target/i386/tcg/translate.c +1 -0
index 4b53a47000e..8ef938372d5 100644
@@ -1336,6 +1336,7 @@ static void do_gen_rep(DisasContext *s, MemOp ot,
                         bool is_repz_nz)
  {
      TCGLabel *done = gen_new_label();
+    target_ulong cx_mask = MAKE_64BIT_MASK(0, 8 << s->aflag);
      bool had_rf = s->flags & HF_RF_MASK;
 
      /*

target/i386/tcg/translate.c static void do_gen_rep(DisasContext *s, MemOp ot, · line 1365

Quoted 8 lines
target/i386/tcg/translate.c +1 -1
@@ -1358,7 +1359,7 @@ static void do_gen_rep(DisasContext *s, MemOp ot,
      tcg_set_insn_start_param(s->base.insn_start, 1, CC_OP_DYNAMIC);
 
      /* Any iteration at all?  */
-    gen_op_jz_ecx(s, done);
+    tcg_gen_brcondi_tl(TCG_COND_TSTEQ, cpu_regs[R_ECX], cx_mask, done);
 
      fn(s, ot);
      gen_op_add_reg_im(s, s->aflag, R_ECX, -1);

gen_op_jz_ecx should become unused now.

And maybe gen_op_jnz_ecx went unused a few patches ago, when gen_jz_ecx_string got merged? Anyway, I think there are some dead inlines which clang would Werror about.

With those fixed,

r~

#22 lore raw

Re: [PATCH 09/13] target/i386: do not use gen_op_jz_ecx for repeated string operations

Il dom 15 dic 2024, 15:50 Richard Henderson <[email protected]> ha scritto:

Quoted 5 lines

gen_op_jz_ecx should become unused now.

And maybe gen_op_jnz_ecx went unused a few patches ago, when gen_jz_ecx_string got merged?

IIRC LOOP{,Z,NZ} use both of them. I can inline them as a follow-up.

Paolo

Anyway, I think there are some dead inlines which clang would Werror about.

Quoted 8 lines

With those fixed,

r~

#23 lore raw

PATCH 10/13 target/i386: optimize CX handling in repeated string operations

In reply to Paolo Bonzini

In a repeated string operation, CX/ECX will be decremented until it is 0 but never underflow. Use this observation to avoid a deposit or zero-extend operation if the address size of the operation is smaller than MO_TL.

As in the previous patch, the patch is structured to include some preparatory work for subsequent changes. In particular, introducing cx_next prepares for when ECX will be decremented *before* calling fn(s, ot), and therefore cannot yet be written back to cpu_regs.

---
 target/i386/tcg/translate.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
target/i386/tcg/translate.c +14 -1
index 8ef938372d5..2ea8a418612 100644
@@ -1337,6 +1337,7 @@ static void do_gen_rep(DisasContext *s, MemOp ot,
 {
     TCGLabel *done = gen_new_label();
     target_ulong cx_mask = MAKE_64BIT_MASK(0, 8 << s->aflag);
+    TCGv cx_next = tcg_temp_new();
     bool had_rf = s->flags & HF_RF_MASK;
 
     /*
@@ -1362,7 +1363,19 @@ static void do_gen_rep(DisasContext *s, MemOp ot,
     tcg_gen_brcondi_tl(TCG_COND_TSTEQ, cpu_regs[R_ECX], cx_mask, done);
 
     fn(s, ot);
-    gen_op_add_reg_im(s, s->aflag, R_ECX, -1);
+
+    tcg_gen_subi_tl(cx_next, cpu_regs[R_ECX], 1);
+
+    /*
+     * Write back cx_next to CX/ECX/RCX.  There can be no carry, so zero
+     * extend if needed but do not do expensive deposit operations.
+     */
+#ifdef TARGET_X86_64
+    if (s->aflag == MO_32) {
+        tcg_gen_ext32u_tl(cx_next, cx_next);
+    }
+#endif
+    tcg_gen_mov_tl(cpu_regs[R_ECX], cx_next);
     gen_update_cc_op(s);
 
     /* Leave if REP condition fails.  */
Signature · 1 line
2.47.1
Reviewed-by on v1 10/13
#24 lore raw

Re: [PATCH 10/13] target/i386: optimize CX handling in repeated string operations

Quoted 10 lines · Paolo Bonzini

In a repeated string operation, CX/ECX will be decremented until it is 0 but never underflow. Use this observation to avoid a deposit or zero-extend operation if the address size of the operation is smaller than MO_TL.

As in the previous patch, the patch is structured to include some preparatory work for subsequent changes. In particular, introducing cx_next prepares for when ECX will be decremented*before* calling fn(s, ot), and therefore cannot yet be written back to cpu_regs.

Quoted 4 lines
---
  target/i386/tcg/translate.c | 15 ++++++++++++++-
  1 file changed, 14 insertions(+), 1 deletion(-)

r~

#25 lore raw

PATCH 11/13 target/i386: execute multiple REP/REPZ iterations without leaving TB

In reply to Paolo Bonzini

Use a TCG loop so that it is not necessary to go through the setup steps of REP and through the I/O check on every iteration. Interestingly, this is not a particularly effective optimization on its own, though it avoids the cost of correct RF emulation that was added in the previous patch. The main benefit lies in allowing the hoisting of loop invariants outside the loop, which will happen separately.

The loop exits when the low 16 bits of CX/ECX/RCX are zero (so generally speaking the string operation runs in 65536 iteration batches) to give the main loop an opportunity to pick up interrupts.

---
 target/i386/tcg/translate.c | 55 +++++++++++++++++++++++++++++++++----
 1 file changed, 49 insertions(+), 6 deletions(-)
target/i386/tcg/translate.c +49 -6
index 2ea8a418612..e0f9f7748bc 100644
@@ -1331,13 +1331,28 @@ static void gen_outs(DisasContext *s, MemOp ot)
     gen_bpt_io(s, s->tmp2_i32, ot);
 }
 
+#define REP_MAX 65535
+
 static void do_gen_rep(DisasContext *s, MemOp ot,
                        void (*fn)(DisasContext *s, MemOp ot),
                        bool is_repz_nz)
 {
+    TCGLabel *last = gen_new_label();
+    TCGLabel *loop = gen_new_label();
     TCGLabel *done = gen_new_label();
+
     target_ulong cx_mask = MAKE_64BIT_MASK(0, 8 << s->aflag);
     TCGv cx_next = tcg_temp_new();
+
+    /*
+     * Check if we must translate a single iteration only.  Normally, HF_RF_MASK
+     * would also limit translation blocks to one instruction, so that gen_eob
+     * can reset the flag; here however RF is set throughout the repetition, so
+     * we can plow through until CX/ECX/RCX is zero.
+     */
+    bool can_loop =
+        (!(tb_cflags(s->base.tb) & (CF_USE_ICOUNT | CF_SINGLE_STEP))
+	 && !(s->flags & (HF_TF_MASK | HF_INHIBIT_IRQ_MASK)));
     bool had_rf = s->flags & HF_RF_MASK;
 
     /*
@@ -1362,19 +1377,29 @@ static void do_gen_rep(DisasContext *s, MemOp ot,
     /* Any iteration at all?  */
     tcg_gen_brcondi_tl(TCG_COND_TSTEQ, cpu_regs[R_ECX], cx_mask, done);
 
-    fn(s, ot);
-
-    tcg_gen_subi_tl(cx_next, cpu_regs[R_ECX], 1);
-
     /*
-     * Write back cx_next to CX/ECX/RCX.  There can be no carry, so zero
-     * extend if needed but do not do expensive deposit operations.
+     * From now on we operate on the value of CX/ECX/RCX that will be written
+     * back, which is stored in cx_next.  There can be no carry, so we can zero
+     * extend here if needed and not do any expensive deposit operations later.
      */
+    tcg_gen_subi_tl(cx_next, cpu_regs[R_ECX], 1);
 #ifdef TARGET_X86_64
     if (s->aflag == MO_32) {
         tcg_gen_ext32u_tl(cx_next, cx_next);
+        cx_mask = ~0;
     }
 #endif
+
+    /*
+     * The last iteration is handled outside the loop, so that cx_next
+     * can never underflow.
+     */
+    if (can_loop) {
+        tcg_gen_brcondi_tl(TCG_COND_TSTEQ, cx_next, cx_mask, last);
+    }
+
+    gen_set_label(loop);
+    fn(s, ot);
     tcg_gen_mov_tl(cpu_regs[R_ECX], cx_next);
     gen_update_cc_op(s);
 
@@ -1384,6 +1409,12 @@ static void do_gen_rep(DisasContext *s, MemOp ot,
         gen_jcc_noeob(s, (JCC_Z << 1) | (nz ^ 1), done);
     }
 
+    if (can_loop) {
+        tcg_gen_subi_tl(cx_next, cpu_regs[R_ECX], 1);
+        tcg_gen_brcondi_tl(TCG_COND_TSTNE, cx_next, REP_MAX, loop);
+        tcg_gen_brcondi_tl(TCG_COND_TSTEQ, cx_next, cx_mask, last);
+    }
+
     /*
      * Traps or interrupts set RF_MASK if they happen after any iteration
      * but the last.  Set it here before giving the main loop a chance to
@@ -1396,6 +1427,18 @@ static void do_gen_rep(DisasContext *s, MemOp ot,
     /* Go to the main loop but reenter the same instruction.  */
     gen_jmp_rel_csize(s, -cur_insn_len(s), 0);
 
+    if (can_loop) {
+        /*
+         * The last iteration needs no conditional jump, even if is_repz_nz,
+         * because the repeats are ending anyway.
+         */
+        gen_set_label(last);
+        set_cc_op(s, CC_OP_DYNAMIC);
+        fn(s, ot);
+        tcg_gen_mov_tl(cpu_regs[R_ECX], cx_next);
+        gen_update_cc_op(s);
+    }
+
     /* CX/ECX/RCX is zero, or REPZ/REPNZ broke the repetition.  */
     gen_set_label(done);
     set_cc_op(s, CC_OP_DYNAMIC);
Signature · 1 line
2.47.1
Reviewed-by on v1 11/13
#26 lore raw

Re: [PATCH 11/13] target/i386: execute multiple REP/REPZ iterations without leaving TB

target/i386/tcg/translate.c static void do_gen_rep(DisasContext *s, MemOp ot, · line 1401

Quoted 10 lines · Paolo Bonzini
target/i386/tcg/translate.c +10 -0
+
+    /*
+     * The last iteration is handled outside the loop, so that cx_next
+     * can never underflow.
+     */
+    if (can_loop) {
+        tcg_gen_brcondi_tl(TCG_COND_TSTEQ, cx_next, cx_mask, last);
+    }
+
+    gen_set_label(loop);

I know unused labels will get eliminated, but clearer if you emit the label within the IF as well.

target/i386/tcg/translate.c static void do_gen_rep(DisasContext *s, MemOp ot, · line 1413

Quoted 6 lines
target/i386/tcg/translate.c +2 -0
@@ -1384,6 +1409,12 @@ static void do_gen_rep(DisasContext *s, MemOp ot,
          gen_jcc_noeob(s, (JCC_Z << 1) | (nz ^ 1), done);
      }
 
+    if (can_loop) {
+        tcg_gen_subi_tl(cx_next, cpu_regs[R_ECX], 1);

Since we've just written back cx_next to ECX, this is the same as cx_next -= 1, yes?

Anyway,

r~

#27 lore raw

Re: [PATCH 11/13] target/i386: execute multiple REP/REPZ iterations without leaving TB

Il dom 15 dic 2024, 16:07 Richard Henderson <[email protected]> ha scritto:

Quoted 11 lines

Review static void do_gen_rep(DisasContext *s, MemOp ot,

Quoted 1 line
(quoted diff) +0 -0
@@ -1384,6 +1409,12 @@ static void do_gen_rep(DisasContext *s, MemOp ot,
          gen_jcc_noeob(s, (JCC_Z << 1) | (nz ^ 1), done);

target/i386/tcg/translate.c static void do_gen_rep(DisasContext *s, MemOp ot, · line 1413

Quoted 4 lines
target/i386/tcg/translate.c +2 -0
      }
 
+    if (can_loop) {
+        tcg_gen_subi_tl(cx_next, cpu_regs[R_ECX], 1);

Since we've just written back cx_next to ECX, this is the same as cx_next -= 1, yes?

Yeah, I wanted to make cx_next die at the assignment to ECX but it probably does not make a difference to generated code.

Paolo

Quoted 9 lines

Anyway,

r~

#28 lore raw

Re: [PATCH 11/13] target/i386: execute multiple REP/REPZ iterations without leaving TB

Quoted 15 lines · Paolo Bonzini

Il dom 15 dic 2024, 16:07 Richard Henderson <[email protected] <mailto:[email protected]>> ha scritto:

     > @@ -1384,6 +1409,12 @@ static void do_gen_rep(DisasContext *s, MemOp ot,

     >           gen_jcc_noeob(s, (JCC_Z << 1) | (nz ^ 1), done);
     >       }
     >
     > +    if (can_loop) {
     > +        tcg_gen_subi_tl(cx_next, cpu_regs[R_ECX], 1);

    Since we've just written back cx_next to ECX, this is the same as cx_next -= 1, yes?
Quoted 1 line

Yeah, I wanted to make cx_next die at the assignment to ECX but it probably does not make a difference to generated code.

Not really. It would only make a difference if cx_next was never live outside the EBB. But it is live across the branches to LOOP and LAST.

What might make a difference is to use the knowledge of known values in ECX, but less usage of cx_next itself. Let cx_next die at the two

+ tcg_gen_brcondi_tl(TCG_COND_TSTEQ, cx_next, cx_mask, last);

by repeating the subtraction when updating ECX, i.e.

- tcg_gen_mov_tl(cpu_regs[R_ECX], cx_next);
+ tcg_gen_subi_tl(cpu_regs[R_ECX], cpu_regs[R_ECX], 1);

This would avoid spilling cx_next to the stack.

There's a the ext32u to place somewhere.

I guess you can't hoist outside the loop before the first invocation of FN, due to the fault path. To eliminate it from the main loop you'd have to unroll once.

// no iteration
brcond tsteq ecx, mask, done

sub cxnext, ecx, 1
brcond tsteq cxnext, mask, last

// first iteration
fn
sub ecx, ecx, 1
extu ecx, ecx

sub cxnext, ecx, 1
brcond eq cxnext, 0, last

// subsequent iterations, ecx now known zero-extended.
loop:
fn
sub ecx, ecx, 1

sub cxnext, ecx, 1
brcond tstne, cxnext, max, loop
brcond eq cxnext, 0, last

etc. It doesn't seem worthwhile to eliminate one ext32u, which will almost certainly be scheduled into the noise.

r~

#29 lore raw

PATCH 12/13 target/i386: pull computation of string update value out of loop

In reply to Paolo Bonzini

This is a common operation that is executed many times in rep movs or rep stos loops. It can improve performance by several percentage points.

---
 target/i386/tcg/translate.c | 54 ++++++++++++++++++-------------------
 1 file changed, 26 insertions(+), 28 deletions(-)
target/i386/tcg/translate.c +26 -28
index e0f9f7748bc..4b652cc23e1 100644
@@ -831,16 +831,13 @@ static bool gen_check_io(DisasContext *s, MemOp ot, TCGv_i32 port,
 #endif
 }
 
-static void gen_movs(DisasContext *s, MemOp ot)
+static void gen_movs(DisasContext *s, MemOp ot, TCGv dshift)
 {
-    TCGv dshift;
-
     gen_string_movl_A0_ESI(s);
     gen_op_ld_v(s, ot, s->T0, s->A0);
     gen_string_movl_A0_EDI(s);
     gen_op_st_v(s, ot, s->T0, s->A0);
 
-    dshift = gen_compute_Dshift(s, ot);
     gen_op_add_reg(s, s->aflag, R_ESI, dshift);
     gen_op_add_reg(s, s->aflag, R_EDI, dshift);
 }
@@ -1244,22 +1241,22 @@ static inline void gen_jcc(DisasContext *s, int b, TCGLabel *l1)
     }
 }
 
-static void gen_stos(DisasContext *s, MemOp ot)
+static void gen_stos(DisasContext *s, MemOp ot, TCGv dshift)
 {
     gen_string_movl_A0_EDI(s);
     gen_op_st_v(s, ot, s->T0, s->A0);
-    gen_op_add_reg(s, s->aflag, R_EDI, gen_compute_Dshift(s, ot));
+    gen_op_add_reg(s, s->aflag, R_EDI, dshift);
 }
 
-static void gen_lods(DisasContext *s, MemOp ot)
+static void gen_lods(DisasContext *s, MemOp ot, TCGv dshift)
 {
     gen_string_movl_A0_ESI(s);
     gen_op_ld_v(s, ot, s->T0, s->A0);
     gen_op_mov_reg_v(s, ot, R_EAX, s->T0);
-    gen_op_add_reg(s, s->aflag, R_ESI, gen_compute_Dshift(s, ot));
+    gen_op_add_reg(s, s->aflag, R_ESI, dshift);
 }
 
-static void gen_scas(DisasContext *s, MemOp ot)
+static void gen_scas(DisasContext *s, MemOp ot, TCGv dshift)
 {
     gen_string_movl_A0_EDI(s);
     gen_op_ld_v(s, ot, s->T1, s->A0);
@@ -1268,13 +1265,11 @@ static void gen_scas(DisasContext *s, MemOp ot)
     tcg_gen_sub_tl(cpu_cc_dst, s->T0, s->T1);
     set_cc_op(s, CC_OP_SUBB + ot);
 
-    gen_op_add_reg(s, s->aflag, R_EDI, gen_compute_Dshift(s, ot));
+    gen_op_add_reg(s, s->aflag, R_EDI, dshift);
 }
 
-static void gen_cmps(DisasContext *s, MemOp ot)
+static void gen_cmps(DisasContext *s, MemOp ot, TCGv dshift)
 {
-    TCGv dshift;
-
     gen_string_movl_A0_EDI(s);
     gen_op_ld_v(s, ot, s->T1, s->A0);
     gen_string_movl_A0_ESI(s);
@@ -1284,7 +1279,6 @@ static void gen_cmps(DisasContext *s, MemOp ot)
     tcg_gen_sub_tl(cpu_cc_dst, s->T0, s->T1);
     set_cc_op(s, CC_OP_SUBB + ot);
 
-    dshift = gen_compute_Dshift(s, ot);
     gen_op_add_reg(s, s->aflag, R_ESI, dshift);
     gen_op_add_reg(s, s->aflag, R_EDI, dshift);
 }
@@ -1303,7 +1297,7 @@ static void gen_bpt_io(DisasContext *s, TCGv_i32 t_port, int ot)
     }
 }
 
-static void gen_ins(DisasContext *s, MemOp ot)
+static void gen_ins(DisasContext *s, MemOp ot, TCGv dshift)
 {
     gen_string_movl_A0_EDI(s);
     /* Note: we must do this dummy write first to be restartable in
@@ -1314,11 +1308,11 @@ static void gen_ins(DisasContext *s, MemOp ot)
     tcg_gen_andi_i32(s->tmp2_i32, s->tmp2_i32, 0xffff);
     gen_helper_in_func(ot, s->T0, s->tmp2_i32);
     gen_op_st_v(s, ot, s->T0, s->A0);
-    gen_op_add_reg(s, s->aflag, R_EDI, gen_compute_Dshift(s, ot));
+    gen_op_add_reg(s, s->aflag, R_EDI, dshift);
     gen_bpt_io(s, s->tmp2_i32, ot);
 }
 
-static void gen_outs(DisasContext *s, MemOp ot)
+static void gen_outs(DisasContext *s, MemOp ot, TCGv dshift)
 {
     gen_string_movl_A0_ESI(s);
     gen_op_ld_v(s, ot, s->T0, s->A0);
@@ -1327,14 +1321,14 @@ static void gen_outs(DisasContext *s, MemOp ot)
     tcg_gen_andi_i32(s->tmp2_i32, s->tmp2_i32, 0xffff);
     tcg_gen_trunc_tl_i32(s->tmp3_i32, s->T0);
     gen_helper_out_func(ot, s->tmp2_i32, s->tmp3_i32);
-    gen_op_add_reg(s, s->aflag, R_ESI, gen_compute_Dshift(s, ot));
+    gen_op_add_reg(s, s->aflag, R_ESI, dshift);
     gen_bpt_io(s, s->tmp2_i32, ot);
 }
 
 #define REP_MAX 65535
 
-static void do_gen_rep(DisasContext *s, MemOp ot,
-                       void (*fn)(DisasContext *s, MemOp ot),
+static void do_gen_rep(DisasContext *s, MemOp ot, TCGv dshift,
+                       void (*fn)(DisasContext *s, MemOp ot, TCGv dshift),
                        bool is_repz_nz)
 {
     TCGLabel *last = gen_new_label();
@@ -1399,7 +1393,7 @@ static void do_gen_rep(DisasContext *s, MemOp ot,
     }
 
     gen_set_label(loop);
-    fn(s, ot);
+    fn(s, ot, dshift);
     tcg_gen_mov_tl(cpu_regs[R_ECX], cx_next);
     gen_update_cc_op(s);
 
@@ -1434,7 +1428,7 @@ static void do_gen_rep(DisasContext *s, MemOp ot,
          */
         gen_set_label(last);
         set_cc_op(s, CC_OP_DYNAMIC);
-        fn(s, ot);
+        fn(s, ot, dshift);
         tcg_gen_mov_tl(cpu_regs[R_ECX], cx_next);
         gen_update_cc_op(s);
     }
@@ -1449,23 +1443,27 @@ static void do_gen_rep(DisasContext *s, MemOp ot,
 }
 
 static void gen_repz(DisasContext *s, MemOp ot,
-                     void (*fn)(DisasContext *s, MemOp ot))
+                     void (*fn)(DisasContext *s, MemOp ot, TCGv dshift))
 
 {
+    TCGv dshift = gen_compute_Dshift(s, ot);
+
     if (s->prefix & (PREFIX_REPZ | PREFIX_REPNZ)) {
-        do_gen_rep(s, ot, fn, false);
+        do_gen_rep(s, ot, dshift, fn, false);
     } else {
-        fn(s, ot);
+        fn(s, ot, dshift);
     }
 }
 
 static void gen_repz_nz(DisasContext *s, MemOp ot,
-                        void (*fn)(DisasContext *s, MemOp ot))
+                        void (*fn)(DisasContext *s, MemOp ot, TCGv dshift))
 {
+    TCGv dshift = gen_compute_Dshift(s, ot);
+
     if (s->prefix & (PREFIX_REPZ | PREFIX_REPNZ)) {
-        do_gen_rep(s, ot, fn, true);
+        do_gen_rep(s, ot, dshift, fn, true);
     } else {
-        fn(s, ot);
+        fn(s, ot, dshift);
     }
 }
 
Signature · 1 line
2.47.1
#30 lore raw

Re: [PATCH 12/13] target/i386: pull computation of string update value out of loop

Quoted 166 lines · Paolo Bonzini

This is a common operation that is executed many times in rep movs or rep stos loops. It can improve performance by several percentage points.

---
  target/i386/tcg/translate.c | 54 ++++++++++++++++++-------------------
  1 file changed, 26 insertions(+), 28 deletions(-)
target/i386/tcg/translate.c +23 -26
index e0f9f7748bc..4b652cc23e1 100644
@@ -831,16 +831,13 @@ static bool gen_check_io(DisasContext *s, MemOp ot, TCGv_i32 port,
  #endif
  }
 
-static void gen_movs(DisasContext *s, MemOp ot)
+static void gen_movs(DisasContext *s, MemOp ot, TCGv dshift)
  {
-    TCGv dshift;
-
      gen_string_movl_A0_ESI(s);
      gen_op_ld_v(s, ot, s->T0, s->A0);
      gen_string_movl_A0_EDI(s);
      gen_op_st_v(s, ot, s->T0, s->A0);
 
-    dshift = gen_compute_Dshift(s, ot);
      gen_op_add_reg(s, s->aflag, R_ESI, dshift);
      gen_op_add_reg(s, s->aflag, R_EDI, dshift);
  }
@@ -1244,22 +1241,22 @@ static inline void gen_jcc(DisasContext *s, int b, TCGLabel *l1)
      }
  }
 
-static void gen_stos(DisasContext *s, MemOp ot)
+static void gen_stos(DisasContext *s, MemOp ot, TCGv dshift)
  {
      gen_string_movl_A0_EDI(s);
      gen_op_st_v(s, ot, s->T0, s->A0);
-    gen_op_add_reg(s, s->aflag, R_EDI, gen_compute_Dshift(s, ot));
+    gen_op_add_reg(s, s->aflag, R_EDI, dshift);
  }
 
-static void gen_lods(DisasContext *s, MemOp ot)
+static void gen_lods(DisasContext *s, MemOp ot, TCGv dshift)
  {
      gen_string_movl_A0_ESI(s);
      gen_op_ld_v(s, ot, s->T0, s->A0);
      gen_op_mov_reg_v(s, ot, R_EAX, s->T0);
-    gen_op_add_reg(s, s->aflag, R_ESI, gen_compute_Dshift(s, ot));
+    gen_op_add_reg(s, s->aflag, R_ESI, dshift);
  }
 
-static void gen_scas(DisasContext *s, MemOp ot)
+static void gen_scas(DisasContext *s, MemOp ot, TCGv dshift)
  {
      gen_string_movl_A0_EDI(s);
      gen_op_ld_v(s, ot, s->T1, s->A0);
@@ -1268,13 +1265,11 @@ static void gen_scas(DisasContext *s, MemOp ot)
      tcg_gen_sub_tl(cpu_cc_dst, s->T0, s->T1);
      set_cc_op(s, CC_OP_SUBB + ot);
 
-    gen_op_add_reg(s, s->aflag, R_EDI, gen_compute_Dshift(s, ot));
+    gen_op_add_reg(s, s->aflag, R_EDI, dshift);
  }
 
-static void gen_cmps(DisasContext *s, MemOp ot)
+static void gen_cmps(DisasContext *s, MemOp ot, TCGv dshift)
  {
-    TCGv dshift;
-
      gen_string_movl_A0_EDI(s);
      gen_op_ld_v(s, ot, s->T1, s->A0);
      gen_string_movl_A0_ESI(s);
@@ -1284,7 +1279,6 @@ static void gen_cmps(DisasContext *s, MemOp ot)
      tcg_gen_sub_tl(cpu_cc_dst, s->T0, s->T1);
      set_cc_op(s, CC_OP_SUBB + ot);
 
-    dshift = gen_compute_Dshift(s, ot);
      gen_op_add_reg(s, s->aflag, R_ESI, dshift);
      gen_op_add_reg(s, s->aflag, R_EDI, dshift);
  }
@@ -1303,7 +1297,7 @@ static void gen_bpt_io(DisasContext *s, TCGv_i32 t_port, int ot)
      }
  }
 
-static void gen_ins(DisasContext *s, MemOp ot)
+static void gen_ins(DisasContext *s, MemOp ot, TCGv dshift)
  {
      gen_string_movl_A0_EDI(s);
      /* Note: we must do this dummy write first to be restartable in
@@ -1314,11 +1308,11 @@ static void gen_ins(DisasContext *s, MemOp ot)
      tcg_gen_andi_i32(s->tmp2_i32, s->tmp2_i32, 0xffff);
      gen_helper_in_func(ot, s->T0, s->tmp2_i32);
      gen_op_st_v(s, ot, s->T0, s->A0);
-    gen_op_add_reg(s, s->aflag, R_EDI, gen_compute_Dshift(s, ot));
+    gen_op_add_reg(s, s->aflag, R_EDI, dshift);
      gen_bpt_io(s, s->tmp2_i32, ot);
  }
 
-static void gen_outs(DisasContext *s, MemOp ot)
+static void gen_outs(DisasContext *s, MemOp ot, TCGv dshift)
  {
      gen_string_movl_A0_ESI(s);
      gen_op_ld_v(s, ot, s->T0, s->A0);
@@ -1327,14 +1321,14 @@ static void gen_outs(DisasContext *s, MemOp ot)
      tcg_gen_andi_i32(s->tmp2_i32, s->tmp2_i32, 0xffff);
      tcg_gen_trunc_tl_i32(s->tmp3_i32, s->T0);
      gen_helper_out_func(ot, s->tmp2_i32, s->tmp3_i32);
-    gen_op_add_reg(s, s->aflag, R_ESI, gen_compute_Dshift(s, ot));
+    gen_op_add_reg(s, s->aflag, R_ESI, dshift);
      gen_bpt_io(s, s->tmp2_i32, ot);
  }
 
  #define REP_MAX 65535
 
-static void do_gen_rep(DisasContext *s, MemOp ot,
-                       void (*fn)(DisasContext *s, MemOp ot),
+static void do_gen_rep(DisasContext *s, MemOp ot, TCGv dshift,
+                       void (*fn)(DisasContext *s, MemOp ot, TCGv dshift),
                         bool is_repz_nz)
  {
      TCGLabel *last = gen_new_label();
@@ -1399,7 +1393,7 @@ static void do_gen_rep(DisasContext *s, MemOp ot,
      }
 
      gen_set_label(loop);
-    fn(s, ot);
+    fn(s, ot, dshift);
      tcg_gen_mov_tl(cpu_regs[R_ECX], cx_next);
      gen_update_cc_op(s);
 
@@ -1434,7 +1428,7 @@ static void do_gen_rep(DisasContext *s, MemOp ot,
           */
          gen_set_label(last);
          set_cc_op(s, CC_OP_DYNAMIC);
-        fn(s, ot);
+        fn(s, ot, dshift);
          tcg_gen_mov_tl(cpu_regs[R_ECX], cx_next);
          gen_update_cc_op(s);
      }
@@ -1449,23 +1443,27 @@ static void do_gen_rep(DisasContext *s, MemOp ot,
  }
 
  static void gen_repz(DisasContext *s, MemOp ot,
-                     void (*fn)(DisasContext *s, MemOp ot))
+                     void (*fn)(DisasContext *s, MemOp ot, TCGv dshift))
 
  {
+    TCGv dshift = gen_compute_Dshift(s, ot);
+
      if (s->prefix & (PREFIX_REPZ | PREFIX_REPNZ)) {
-        do_gen_rep(s, ot, fn, false);
+        do_gen_rep(s, ot, dshift, fn, false);
      } else {
-        fn(s, ot);
+        fn(s, ot, dshift);
      }
  }
 
  static void gen_repz_nz(DisasContext *s, MemOp ot,
-                        void (*fn)(DisasContext *s, MemOp ot))
+                        void (*fn)(DisasContext *s, MemOp ot, TCGv dshift))
  {
+    TCGv dshift = gen_compute_Dshift(s, ot);

target/i386/tcg/translate.c static void do_gen_rep(DisasContext *s, MemOp ot, · line 1469

Quoted 10 lines
target/i386/tcg/translate.c +3 -2
+
      if (s->prefix & (PREFIX_REPZ | PREFIX_REPNZ)) {
-        do_gen_rep(s, ot, fn, true);
+        do_gen_rep(s, ot, dshift, fn, true);
      } else {
-        fn(s, ot);
+        fn(s, ot, dshift);
      }
  }
 

Still not a fan of the repetition. I think this could easily be

static void do_gen_rep(DisasContext *s, MemOp ot,
void (*fn)(DisasContext *s, MemOp ot, TCGv dshift), bool is_repz_nz)

{
    TCGv dshift = gen_compute_Dshift(s, ot);
    TCGLabel *last, *loop, *done;
    target_ulong cx_mask;
    TCGv cx_next;
    bool can_loop, had_rf;

    if (!(s->prefix & (REPZ | REPNZ))) {
        fn(s, ot, dshift);
        return;
    }

    done = gen_new_label();
    can_loop = ...
    everything else.
}

Or, if you prefer, use two functions gen_repz_nz{0,1}.

r~

#31 lore raw

PATCH 13/13 target/i386: avoid using s->tmp0 for add to implicit registers

In reply to Paolo Bonzini

For updates to implicit registers (RCX in LOOP instructions, RSI or RDI in string instructions, or the stack pointer) do the add directly using the registers (with no temporary) if 32-bit or 64-bit, or use a temporary created for the occasion if 16-bit. This is more efficient and removes move instructions for the MO_TL case.

---
 target/i386/tcg/translate.c | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)
target/i386/tcg/translate.c +15 -8
index 4b652cc23e1..8de506927b0 100644
@@ -504,17 +504,24 @@ static inline void gen_op_jmp_v(DisasContext *s, TCGv dest)
     s->pc_save = -1;
 }
 
+static inline void gen_op_add_reg(DisasContext *s, MemOp size, int reg, TCGv val)
+{
+    /* Using cpu_regs[reg] does not work for xH registers.  */
+    assert(size >= MO_16);
+    if (size == MO_16) {
+        TCGv temp = tcg_temp_new();
+        tcg_gen_add_tl(temp, cpu_regs[reg], val);
+        gen_op_mov_reg_v(s, size, reg, temp);
+    } else {
+        tcg_gen_add_tl(cpu_regs[reg], cpu_regs[reg], val);
+        tcg_gen_ext_tl(cpu_regs[reg], cpu_regs[reg], size);
+    }
+}
+
 static inline
 void gen_op_add_reg_im(DisasContext *s, MemOp size, int reg, int32_t val)
 {
-    tcg_gen_addi_tl(s->tmp0, cpu_regs[reg], val);
-    gen_op_mov_reg_v(s, size, reg, s->tmp0);
-}
-
-static inline void gen_op_add_reg(DisasContext *s, MemOp size, int reg, TCGv val)
-{
-    tcg_gen_add_tl(s->tmp0, cpu_regs[reg], val);
-    gen_op_mov_reg_v(s, size, reg, s->tmp0);
+    gen_op_add_reg(s, size, reg, tcg_constant_tl(val));
 }
 
 static inline void gen_op_ld_v(DisasContext *s, int idx, TCGv t0, TCGv a0)
Signature · 1 line
2.47.1
Reviewed-by on v1 13/13
#32 lore raw

Re: [PATCH 13/13] target/i386: avoid using s->tmp0 for add to implicit registers

Quoted 16 lines · Paolo Bonzini

For updates to implicit registers (RCX in LOOP instructions, RSI or RDI in string instructions, or the stack pointer) do the add directly using the registers (with no temporary) if 32-bit or 64-bit, or use a temporary created for the occasion if 16-bit. This is more efficient and removes move instructions for the MO_TL case.

---
  target/i386/tcg/translate.c | 23 +++++++++++++++--------
  1 file changed, 15 insertions(+), 8 deletions(-)
target/i386/tcg/translate.c +0 -0
index 4b652cc23e1..8de506927b0 100644

target/i386/tcg/translate.c static inline void gen_op_jmp_v(DisasContext *s, TCGv dest) · line 507

Quoted 4 lines
target/i386/tcg/translate.c +1 -0
@@ -504,17 +504,24 @@ static inline void gen_op_jmp_v(DisasContext *s, TCGv dest)
      s->pc_save = -1;
  }
 
+static inline void gen_op_add_reg(DisasContext *s, MemOp size, int reg, TCGv val)

Drop the inline. Otherwise, yay!

r~