[PATCH] target/i386/tcg: fix SETCC(zu) optimization

Paolo Bonzini posted 1 patch 1 week, 6 days ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20261109185654.1068614-1-pbonzini@redhat.com
Maintainers: Paolo Bonzini <pbonzini@redhat.com>, Richard Henderson <richard.henderson@linaro.org>
target/i386/tcg/decode-new.c.inc | 3 ---
target/i386/tcg/emit.c.inc       | 8 ++++++++
2 files changed, 8 insertions(+), 3 deletions(-)
[PATCH] target/i386/tcg: fix SETCC(zu) optimization
Posted by Paolo Bonzini 1 week, 6 days ago
With EVEX.ND=1, SETcc zero extends its result to the whole register, which
validate_evex_apx() requests with EXT_Unsigned.  QEMU is trying to
avoid the extension by changing the size of the operand, but it sets
a field of the row that is unused, because op1 is None.

Do it in gen_SETcc() instead.  The result of setcc is 0 or 1, so writing
the whole register has the same effect as the zero extension, and
gen_writeback() skips the extension.
as bytes.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 target/i386/tcg/decode-new.c.inc | 3 ---
 target/i386/tcg/emit.c.inc       | 8 ++++++++
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/target/i386/tcg/decode-new.c.inc b/target/i386/tcg/decode-new.c.inc
index b51b2f1ae9d..f14851d0143 100644
--- a/target/i386/tcg/decode-new.c.inc
+++ b/target/i386/tcg/decode-new.c.inc
@@ -2107,9 +2107,6 @@ static void decode_EVEX4cc(DisasContext *s, CPUX86State *env, X86OpEntry *entry,
 
     if (s->prefix & PREFIX_REPNZ) {
         *entry = setcc;
-        if (EVEX_APX_ND(s)) {
-            entry->s1 = X86_SIZE_q; /* optimization for zu */
-        }
     } else {
         *entry = (EVEX_APX_ND(s) ? cfcmov_nd1 : cfcmov_nd0)[EVEX_APX_NF(s)][mod == 3];
     }
diff --git a/target/i386/tcg/emit.c.inc b/target/i386/tcg/emit.c.inc
index b3029f6fbbe..49d33223f41 100644
--- a/target/i386/tcg/emit.c.inc
+++ b/target/i386/tcg/emit.c.inc
@@ -4136,6 +4136,14 @@ static void gen_SCAS(DisasContext *s, X86DecodedInsn *decode)
 static void gen_SETcc(DisasContext *s, X86DecodedInsn *decode)
 {
     gen_setcc(s, decode->b & 0xf, s->T0);
+
+    /*
+     * With APX ZU, the written value requests zero extension.  Since T0 is
+     * 0 or 1, writing the whole register is the same and saves the extension.
+     */
+    if (decode->op[0].ext == X86_EXT_Unsigned && !decode->op[0].has_ea) {
+        decode->op[0].ot = MO_TL;
+    }
 }
 
 static void gen_SFENCE(DisasContext *s, X86DecodedInsn *decode)
-- 
2.56.0
Re: [PATCH] target/i386/tcg: fix SETCC(zu) optimization
Posted by Richard Henderson 1 week, 2 days ago
On 11/9/26 11:56, Paolo Bonzini wrote:
> With EVEX.ND=1, SETcc zero extends its result to the whole register, which
> validate_evex_apx() requests with EXT_Unsigned.  QEMU is trying to
> avoid the extension by changing the size of the operand, but it sets
> a field of the row that is unused, because op1 is None.
> 
> Do it in gen_SETcc() instead.  The result of setcc is 0 or 1, so writing
> the whole register has the same effect as the zero extension, and
> gen_writeback() skips the extension.
> as bytes.
> 
> Signed-off-by: Paolo Bonzini<pbonzini@redhat.com>
> ---
>   target/i386/tcg/decode-new.c.inc | 3 ---
>   target/i386/tcg/emit.c.inc       | 8 ++++++++
>   2 files changed, 8 insertions(+), 3 deletions(-)

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

r~