qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: richard.henderson@linaro.org
Subject: [PATCH v2] tcg/i386: convert add/sub of 128 to sub/add of -128
Date: Thu, 28 Dec 2023 13:05:14 +0100	[thread overview]
Message-ID: <20231228120514.70205-1-pbonzini@redhat.com> (raw)

Extend the existing conditional that generates INC/DEC, to also swap an
ADD for a SUB and vice versa when the immediate is 128.  This facilitates
using OPC_ARITH_EvIb instead of OPC_ARITH_EvIz.

Best reviewed with "git diff -b".

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 tcg/i386/tcg-target.c.inc | 40 +++++++++++++++++++++++----------------
 1 file changed, 24 insertions(+), 16 deletions(-)

diff --git a/tcg/i386/tcg-target.c.inc b/tcg/i386/tcg-target.c.inc
index a83f8aab304..1791b959738 100644
--- a/tcg/i386/tcg-target.c.inc
+++ b/tcg/i386/tcg-target.c.inc
@@ -1316,23 +1316,31 @@ static void tgen_arithi(TCGContext *s, int c, int r0,
         c &= 7;
     }
 
-    /* ??? While INC is 2 bytes shorter than ADDL $1, they also induce
-       partial flags update stalls on Pentium4 and are not recommended
-       by current Intel optimization manuals.  */
-    if (!cf && (c == ARITH_ADD || c == ARITH_SUB) && (val == 1 || val == -1)) {
-        int is_inc = (c == ARITH_ADD) ^ (val < 0);
-        if (TCG_TARGET_REG_BITS == 64) {
-            /* The single-byte increment encodings are re-tasked as the
-               REX prefixes.  Use the MODRM encoding.  */
-            tcg_out_modrm(s, OPC_GRP5 + rexw,
-                          (is_inc ? EXT5_INC_Ev : EXT5_DEC_Ev), r0);
-        } else {
-            tcg_out8(s, (is_inc ? OPC_INC_r32 : OPC_DEC_r32) + r0);
+    if (!cf && (c == ARITH_ADD || c == ARITH_SUB)) {
+        /* ??? While INC is 2 bytes shorter than ADDL $1, they also induce
+           partial flags update stalls on Pentium4 and are not recommended
+           by current Intel optimization manuals.  */
+        if (val == 1 || val == -1) {
+            int is_inc = (c == ARITH_ADD) ^ (val < 0);
+            if (TCG_TARGET_REG_BITS == 64) {
+                /* The single-byte increment encodings are re-tasked as the
+                   REX prefixes.  Use the MODRM encoding.  */
+                tcg_out_modrm(s, OPC_GRP5 + rexw,
+                              (is_inc ? EXT5_INC_Ev : EXT5_DEC_Ev), r0);
+            } else {
+                tcg_out8(s, (is_inc ? OPC_INC_r32 : OPC_DEC_r32) + r0);
+            }
+            return;
         }
-        return;
-    }
-
-    if (c == ARITH_AND) {
+        if (val == 128) {
+            /*
+             * Facilitate using an 8-bit immediate.  Carry is inverted
+             * by this transformation, so do it only if cf == 0.
+             */
+            c ^= ARITH_ADD ^ ARITH_SUB;
+            val = -128;
+        }
+    } else if (c == ARITH_AND) {
         if (TCG_TARGET_REG_BITS == 64) {
             if (val == 0xffffffffu) {
                 tcg_out_ext32u(s, r0, r0);
-- 
2.43.0



             reply	other threads:[~2023-12-28 12:06 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-28 12:05 Paolo Bonzini [this message]
2024-01-01  1:15 ` [PATCH v2] tcg/i386: convert add/sub of 128 to sub/add of -128 Richard Henderson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231228120514.70205-1-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=richard.henderson@linaro.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).