qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Richard Henderson <rth@twiddle.net>
To: qemu-devel@nongnu.org
Cc: claudio.fontana@huawei.com, Richard Henderson <rth@twiddle.net>
Subject: [Qemu-devel] [RFC 11/14] tcg-aarch64: Improve tcg_out_movi
Date: Mon, 12 Aug 2013 11:44:52 -0700	[thread overview]
Message-ID: <1376333095-24385-12-git-send-email-rth@twiddle.net> (raw)
In-Reply-To: <1376333095-24385-1-git-send-email-rth@twiddle.net>

Handle small positive and negative numbers early.  Check for logical
immediates.  Check if using MOVN for the first set helps.

Signed-off-by: Richard Henderson <rth@twiddle.net>
---
 tcg/aarch64/tcg-target.c | 85 +++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 66 insertions(+), 19 deletions(-)

diff --git a/tcg/aarch64/tcg-target.c b/tcg/aarch64/tcg-target.c
index 920c63c..02ab278 100644
--- a/tcg/aarch64/tcg-target.c
+++ b/tcg/aarch64/tcg-target.c
@@ -511,32 +511,79 @@ static inline void tcg_out_movr(TCGContext *s, AArch64Ext ext,
     tcg_out_aimm(s, INSN_ADDI, ext, dest, src, 0);
 }
 
+static inline void tcg_out_movwi(TCGContext *s, AArch64Insn insn,
+                                 AArch64Ext ext, TCGReg rd,
+                                 uint16_t value, int shift)
+{
+    tcg_out32(s, insn | ext | shift << 17 | value << 5 | rd);
+}
+
 static void tcg_out_movi(TCGContext *s, TCGType type, TCGReg rd,
                          tcg_target_long value)
 {
-    AArch64Insn insn = INSN_MOVZ;
+    tcg_target_long valid = (type == TCG_TYPE_I32 ? 0xffffffffull : -1ull);
+    AArch64Insn insn;
+    AArch64Ext ext;
+    int i, wantinv, shift;
+
+    value &= valid;
+    
+    /* Check small positive values.  */
+    if ((value & ~0xffff) == 0) {
+        tcg_out_movwi(s, INSN_MOVZ, E32, rd, value, 0);
+        return;
+    }
+
+    /* Check small negative values.  */
+    if ((~value & valid & ~0xffff) == 0) {
+        tcg_out_movwi(s, INSN_MOVN, EXT(type == TCG_TYPE_I64), rd, ~value, 0);
+        return;
+    }
+        
+    /* Check for bitfield immediates.  */
+    if ((value & ~0xffffffffull) == 0) {
+        i = find_bitmask32(value);
+        ext = E32;
+    } else {
+        i = find_bitmask64(value);
+        ext = E64;
+    }
+    if (i >= 0) {
+        tcg_out32(s, INSN_ORRI | TCG_REG_XZR << 5 | ext
+                  | bitmask_enc[i] << 10 | rd);
+        return;
+    }
 
-    if (type == TCG_TYPE_I32) {
-        value = (uint32_t)value;
+    /* Would it take fewer insns to load the inverse?  */
+    wantinv = 0;
+    for (i = 0; i < 64; i += 16) {
+        if (((value >> i) & 0xffff) == 0) {
+            wantinv--;
+        }
+        if (((~value >> i) & 0xffff) == 0) {
+            wantinv++;
+        }
     }
 
-    /* Construct halfwords of the immediate with MOVZ/MOVK with LSL.
-       Count trailing zeros in 16 bit steps, mapping 64 to 0.  Emit the
-       first MOVZ with the half-word immediate skipping the zeros, with
-       a shift (LSL) equal to this number.  Then all other insns are MOVKs.
-       Zero the processed half-word in the value, continue until empty.
-       We build the final result 16bits at a time with up to 4 instructions,
-       but do not emit instructions for 16bit zero holes. */
-    do {
-        unsigned shift = ctz64(value) & (63 & -16);
-        unsigned half = (value >> shift) & 0xffff;
-        AArch64Ext ext = EXT(shift >= 32);
-
-        tcg_out32(s, insn | ext | shift << 17 | half << 5 | rd);
-
-        insn = INSN_MOVK;
+    if (wantinv > 0) {
+        value = ~value;
+        insn = INSN_MOVN;
+        valid = -1;
+    } else {
+        insn = INSN_MOVZ;
+        valid = 0;
+    }
+
+    /* Perform the first round specially, to handle the inverse.  */
+    shift = ctz64(value) & (63 & -16);
+    tcg_out_movwi(s, insn, ext, rd, value >> shift, shift);
+    value &= ~(0xffffUL << shift);
+
+    while (value) {
+        shift = ctz64(value) & (63 & -16);
+        tcg_out_movwi(s, INSN_MOVK, ext, rd, (value ^ valid) >> shift, shift);
         value &= ~(0xffffUL << shift);
-    } while (value);
+    }
 }
 
 static inline void tcg_out_ldst_r(TCGContext *s,
-- 
1.8.3.1

  parent reply	other threads:[~2013-08-12 18:45 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-08-12 18:44 [Qemu-devel] [RFC 00/14] tcg aarch64 improvements Richard Henderson
2013-08-12 18:44 ` [Qemu-devel] [RFC 01/14] tcg-aarch64: Allow immediate operands to add and sub Richard Henderson
2013-08-12 18:44 ` [Qemu-devel] [RFC 02/14] tcg-aarch64: Allow immediate operands to and, or, xor Richard Henderson
2013-08-12 18:44 ` [Qemu-devel] [RFC 03/14] tcg-aarch64: Allow immediate operands to compare Richard Henderson
2013-08-12 18:44 ` [Qemu-devel] [RFC 04/14] tcg-aarch64: Convert from opcode enums to insn enums Richard Henderson
2013-08-12 18:44 ` [Qemu-devel] [RFC 05/14] tcg-aarch64: Support andc, orc, eqv, not Richard Henderson
2013-08-12 18:44 ` [Qemu-devel] [RFC 06/14] tcg-aarch64: Handle zero as first argument to sub Richard Henderson
2013-08-12 18:44 ` [Qemu-devel] [RFC 07/14] tcg-aarch64: Support movcond Richard Henderson
2013-08-12 18:44 ` [Qemu-devel] [RFC 08/14] tcg-aarch64: Support deposit Richard Henderson
2013-08-12 18:44 ` [Qemu-devel] [RFC 09/14] tcg-aarch64: Support add2, sub2 Richard Henderson
2013-08-12 18:44 ` [Qemu-devel] [RFC 10/14] tcg-aarch64: Support div, mulu2 Richard Henderson
2013-08-12 18:44 ` Richard Henderson [this message]
2013-08-12 18:44 ` [Qemu-devel] [RFC 12/14] tcg-aarch64: Avoid add with zero in tlb load Richard Henderson
2013-08-12 18:44 ` [Qemu-devel] [RFC 13/14] tcg-aarch64: Use adrp in tcg_out_movi Richard Henderson
2013-08-12 18:44 ` [Qemu-devel] [RFC 14/14] tcg-aarch64: Pass return address to load/store helpers directly 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=1376333095-24385-12-git-send-email-rth@twiddle.net \
    --to=rth@twiddle.net \
    --cc=claudio.fontana@huawei.com \
    --cc=qemu-devel@nongnu.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).