Skip to contentSkip to conversation

target/arm, cris, mips: optimize "swap bytes within words"

PATCH In review Aurelien Jarno 3 participants 12 messages May 16, 2017 – May 23, 2017

lore
Share & tools
lore
#0 lore raw

cover target/arm, cris, mips: optimize "swap bytes within words"

This patchset optimizes the "swap bytes within words" instructions on the arm, cris and mips targets. It all started with the patchset from Philippe Mathieu-Daudé optimizing TCG code by using the extract op. Looking at the patch I have found that the aarch64 rev16 function can be optimized even more. Richard Henderson then suggested an even more optimized version.

Aurelien Jarno (4):
target/arm: optimize aarch32 rev16
target/arm: simplify and optimize aarch64 rev16
target/cris: optimize swap
target/mips: optimize WSBH, DSBH and DSHD

 target/arm/translate-a64.c | 24 ++++++------------------
 target/arm/translate.c     |  6 ++++--
 target/cris/translate.c    | 15 +++++++--------
 target/mips/translate.c    | 18 ++++++++++++------
 4 files changed, 29 insertions(+), 34 deletions(-)
Signature · 1 line
2.11.0
#1 lore raw

PATCH 1/4 target/arm: optimize aarch32 rev16

Use the same mask to avoid having to load two different constants, as suggested by Richard Henderson.

---
 target/arm/translate.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
target/arm/translate.c +4 -2
index 0b5a0bca06..5becb2bb89 100644
@@ -339,11 +339,13 @@ static void gen_smul_dual(TCGv_i32 a, TCGv_i32 b)
 static void gen_rev16(TCGv_i32 var)
 {
     TCGv_i32 tmp = tcg_temp_new_i32();
+    TCGv_i32 mask = tcg_const_i32(0x00ff00ff);
     tcg_gen_shri_i32(tmp, var, 8);
-    tcg_gen_andi_i32(tmp, tmp, 0x00ff00ff);
+    tcg_gen_and_i32(tmp, tmp, mask);
+    tcg_gen_and_i32(var, var, mask);
     tcg_gen_shli_i32(var, var, 8);
-    tcg_gen_andi_i32(var, var, 0xff00ff00);
     tcg_gen_or_i32(var, var, tmp);
+    tcg_temp_free_i32(mask);
     tcg_temp_free_i32(tmp);
 }
 
Signature · 1 line
2.11.0
#7 lore raw

PATCH 3/4 target/cris: optimize swap

In reply to Aurelien Jarno

Use the same mask to avoid having to load two different constants, as suggest by Richard Henderson. Also use one less temp.

---
 target/cris/translate.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
target/cris/translate.c +7 -8
index 0ee05ca02d..103b214233 100644
@@ -433,20 +433,19 @@ static inline void t_gen_subx_carry(DisasContext *dc, TCGv d)
    T0 = ((T0 << 8) & 0xff00ff00) | ((T0 >> 8) & 0x00ff00ff)  */
 static inline void t_gen_swapb(TCGv d, TCGv s)
 {
-    TCGv t, org_s;
+    TCGv t, m;
 
     t = tcg_temp_new();
-    org_s = tcg_temp_new();
+    m = tcg_const_tl(0x00ff00ff);
 
     /* d and s may refer to the same object.  */
-    tcg_gen_mov_tl(org_s, s);
-    tcg_gen_shli_tl(t, org_s, 8);
-    tcg_gen_andi_tl(d, t, 0xff00ff00);
-    tcg_gen_shri_tl(t, org_s, 8);
-    tcg_gen_andi_tl(t, t, 0x00ff00ff);
+    tcg_gen_shri_tl(t, s, 8);
+    tcg_gen_and_tl(t, t, m);
+    tcg_gen_and_tl(d, s, m);
+    tcg_gen_shli_tl(d, d, 8);
     tcg_gen_or_tl(d, d, t);
+    tcg_temp_free(m);
     tcg_temp_free(t);
-    tcg_temp_free(org_s);
 }
 
 /* Swap the halfwords of the s operand.  */
Signature · 1 line
2.11.0
#10 lore raw

PATCH 4/4 target/mips: optimize WSBH, DSBH and DSHD

In reply to Aurelien Jarno

Use the same mask to avoid having to load two different constants, as suggested by Richard Henderson.

---
 target/mips/translate.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
target/mips/translate.c +12 -6
index 3022f349cb..c71eed498c 100644
@@ -4572,12 +4572,14 @@ static void gen_bshfl (DisasContext *ctx, uint32_t op2, int rt, int rd)
     case OPC_WSBH:
         {
             TCGv t1 = tcg_temp_new();
+            TCGv t2 = tcg_const_tl(0x00FF00FF);
 
             tcg_gen_shri_tl(t1, t0, 8);
-            tcg_gen_andi_tl(t1, t1, 0x00FF00FF);
+            tcg_gen_and_tl(t1, t1, t2);
+            tcg_gen_and_tl(t0, t0, t2);
             tcg_gen_shli_tl(t0, t0, 8);
-            tcg_gen_andi_tl(t0, t0, ~0x00FF00FF);
             tcg_gen_or_tl(t0, t0, t1);
+            tcg_temp_free(t2);
             tcg_temp_free(t1);
             tcg_gen_ext32s_tl(cpu_gpr[rd], t0);
         }
@@ -4592,27 +4594,31 @@ static void gen_bshfl (DisasContext *ctx, uint32_t op2, int rt, int rd)
     case OPC_DSBH:
         {
             TCGv t1 = tcg_temp_new();
+            TCGv t2 = tcg_const_tl(0x00FF00FF00FF00FFULL);
 
             tcg_gen_shri_tl(t1, t0, 8);
-            tcg_gen_andi_tl(t1, t1, 0x00FF00FF00FF00FFULL);
+            tcg_gen_and_tl(t1, t1, t2);
+            tcg_gen_and_tl(t0, t0, t2);
             tcg_gen_shli_tl(t0, t0, 8);
-            tcg_gen_andi_tl(t0, t0, ~0x00FF00FF00FF00FFULL);
             tcg_gen_or_tl(cpu_gpr[rd], t0, t1);
+            tcg_temp_free(t2);
             tcg_temp_free(t1);
         }
         break;
     case OPC_DSHD:
         {
             TCGv t1 = tcg_temp_new();
+            TCGv t2 = tcg_const_tl(0x0000FFFF0000FFFFULL);
 
             tcg_gen_shri_tl(t1, t0, 16);
-            tcg_gen_andi_tl(t1, t1, 0x0000FFFF0000FFFFULL);
+            tcg_gen_and_tl(t1, t1, t2);
+            tcg_gen_and_tl(t0, t0, t2);
             tcg_gen_shli_tl(t0, t0, 16);
-            tcg_gen_andi_tl(t0, t0, ~0x0000FFFF0000FFFFULL);
             tcg_gen_or_tl(t0, t0, t1);
             tcg_gen_shri_tl(t1, t0, 32);
             tcg_gen_shli_tl(t0, t0, 32);
             tcg_gen_or_tl(cpu_gpr[rd], t0, t1);
+            tcg_temp_free(t2);
             tcg_temp_free(t1);
         }
         break;
Signature · 1 line
2.11.0
#4 lore raw

PATCH 2/4 target/arm: simplify and optimize aarch64 rev16

In reply to Aurelien Jarno

Instead of byteswapping individual 16-bit words one by one, work on the whole register at the same time using shifts and mask. This is the same strategy than the aarch32 version of rev16 and is much more efficient in the case sf=1.

---
 target/arm/translate-a64.c | 24 ++++++------------------
 1 file changed, 6 insertions(+), 18 deletions(-)
target/arm/translate-a64.c +6 -18
index 24de30d92c..ed15d21655 100644
@@ -4035,24 +4035,12 @@ static void handle_rev16(DisasContext *s, unsigned int sf,
     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
 
-    tcg_gen_andi_i64(tcg_tmp, tcg_rn, 0xffff);
-    tcg_gen_bswap16_i64(tcg_rd, tcg_tmp);
-
-    tcg_gen_shri_i64(tcg_tmp, tcg_rn, 16);
-    tcg_gen_andi_i64(tcg_tmp, tcg_tmp, 0xffff);
-    tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
-    tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 16, 16);
-
-    if (sf) {
-        tcg_gen_shri_i64(tcg_tmp, tcg_rn, 32);
-        tcg_gen_andi_i64(tcg_tmp, tcg_tmp, 0xffff);
-        tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
-        tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 32, 16);
-
-        tcg_gen_shri_i64(tcg_tmp, tcg_rn, 48);
-        tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
-        tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 48, 16);
-    }
+    TCGv mask = tcg_const_i64(sf ? 0x00ff00ff00ff00ffull : 0x00ff00ff);
+    tcg_gen_shri_i64(tcg_tmp, tcg_rn, 8);
+    tcg_gen_and_i64(tcg_rd, tcg_rn, mask);
+    tcg_gen_and_i64(tcg_tmp, tcg_tmp, mask);
+    tcg_gen_shli_i64(tcg_rd, tcg_rd, 8);
+    tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_tmp);
 
     tcg_temp_free_i64(tcg_tmp);
 }
Signature · 1 line
2.11.0
Reviewed-by on v1 1/4
#2 lore raw

Re: [Qemu-arm] [PATCH 1/4] target/arm: optimize aarch32 rev16

In reply to Aurelien Jarno

Hi Aurelien,

Quoted 3 lines · Aurelien Jarno

Use the same mask to avoid having to load two different constants, as

suggested by Richard Henderson.

What about

Quoted 1 line
Quoted 25 lines
---
 target/arm/translate.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
target/arm/translate.c +4 -2
index 0b5a0bca06..5becb2bb89 100644
@@ -339,11 +339,13 @@ static void gen_smul_dual(TCGv_i32 a, TCGv_i32 b)
 static void gen_rev16(TCGv_i32 var)
 {
     TCGv_i32 tmp = tcg_temp_new_i32();
+    TCGv_i32 mask = tcg_const_i32(0x00ff00ff);
     tcg_gen_shri_i32(tmp, var, 8);
-    tcg_gen_andi_i32(tmp, tmp, 0x00ff00ff);
+    tcg_gen_and_i32(tmp, tmp, mask);
+    tcg_gen_and_i32(var, var, mask);
     tcg_gen_shli_i32(var, var, 8);
-    tcg_gen_andi_i32(var, var, 0xff00ff00);
     tcg_gen_or_i32(var, var, tmp);
+    tcg_temp_free_i32(mask);
     tcg_temp_free_i32(tmp);
 }
 
Reviewed-by on v1 2/4
#5 lore raw

Re: [Qemu-arm] [PATCH 2/4] target/arm: simplify and optimize aarch64 rev16

In reply to Aurelien Jarno
Quoted 6 lines · Aurelien Jarno

Instead of byteswapping individual 16-bit words one by one, work on the whole register at the same time using shifts and mask. This is the same strategy than the aarch32 version of rev16 and is much more efficient in the case sf=1.

Quoted 40 lines
---
 target/arm/translate-a64.c | 24 ++++++------------------
 1 file changed, 6 insertions(+), 18 deletions(-)
target/arm/translate-a64.c +6 -18
index 24de30d92c..ed15d21655 100644
@@ -4035,24 +4035,12 @@ static void handle_rev16(DisasContext *s, unsigned int sf,
     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
 
-    tcg_gen_andi_i64(tcg_tmp, tcg_rn, 0xffff);
-    tcg_gen_bswap16_i64(tcg_rd, tcg_tmp);
-
-    tcg_gen_shri_i64(tcg_tmp, tcg_rn, 16);
-    tcg_gen_andi_i64(tcg_tmp, tcg_tmp, 0xffff);
-    tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
-    tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 16, 16);
-
-    if (sf) {
-        tcg_gen_shri_i64(tcg_tmp, tcg_rn, 32);
-        tcg_gen_andi_i64(tcg_tmp, tcg_tmp, 0xffff);
-        tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
-        tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 32, 16);
-
-        tcg_gen_shri_i64(tcg_tmp, tcg_rn, 48);
-        tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
-        tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 48, 16);
-    }
+    TCGv mask = tcg_const_i64(sf ? 0x00ff00ff00ff00ffull : 0x00ff00ff);
+    tcg_gen_shri_i64(tcg_tmp, tcg_rn, 8);
+    tcg_gen_and_i64(tcg_rd, tcg_rn, mask);
+    tcg_gen_and_i64(tcg_tmp, tcg_tmp, mask);
+    tcg_gen_shli_i64(tcg_rd, tcg_rd, 8);
+    tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_tmp);
 
     tcg_temp_free_i64(tcg_tmp);
 }
Reviewed-by on v1 3/4
#8 lore raw

Re: [Qemu-devel] [PATCH 3/4] target/cris: optimize swap

In reply to Aurelien Jarno
Quoted 22 lines · Aurelien Jarno

Use the same mask to avoid having to load two different constants, as suggest by Richard Henderson. Also use one less temp.

---
 target/cris/translate.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
target/cris/translate.c +2 -2
index 0ee05ca02d..103b214233 100644
@@ -433,20 +433,19 @@ static inline void t_gen_subx_carry(DisasContext *dc, TCGv d)
    T0 = ((T0 << 8) & 0xff00ff00) | ((T0 >> 8) & 0x00ff00ff)  */
 static inline void t_gen_swapb(TCGv d, TCGv s)
 {
-    TCGv t, org_s;
+    TCGv t, m;
 
     t = tcg_temp_new();
-    org_s = tcg_temp_new();
+    m = tcg_const_tl(0x00ff00ff);

target/cris/translate.c static inline void t_gen_subx_carry(DisasContext *dc, TCGv d) · line 444

Quoted 10 lines
target/cris/translate.c +3 -5
 
     /* d and s may refer to the same object.  */
-    tcg_gen_mov_tl(org_s, s);
-    tcg_gen_shli_tl(t, org_s, 8);
-    tcg_gen_andi_tl(d, t, 0xff00ff00);
-    tcg_gen_shri_tl(t, org_s, 8);
-    tcg_gen_andi_tl(t, t, 0x00ff00ff);
+    tcg_gen_shri_tl(t, s, 8);
+    tcg_gen_and_tl(t, t, m);
+    tcg_gen_and_tl(d, s, m);

Eventually add a comment /* set d 0xff00ff00 */

Anyway,

Quoted 9 lines
target/cris/translate.c +2 -1
+    tcg_gen_shli_tl(d, d, 8);
     tcg_gen_or_tl(d, d, t);
+    tcg_temp_free(m);
     tcg_temp_free(t);
-    tcg_temp_free(org_s);
 }
 
 /* Swap the halfwords of the s operand.  */
Reviewed-by on v1 2/4
#6 lore raw

Re: [Qemu-arm] [Qemu-devel] [PATCH 2/4] target/arm: simplify and optimize aarch64 rev16

In reply to Aurelien Jarno
Quoted 9 lines · Aurelien Jarno

Instead of byteswapping individual 16-bit words one by one, work on the whole register at the same time using shifts and mask. This is the same strategy than the aarch32 version of rev16 and is much more efficient in the case sf=1.

---
  target/arm/translate-a64.c | 24 ++++++------------------
  1 file changed, 6 insertions(+), 18 deletions(-)

r~

Reviewed-by on v1 3/4
#9 lore raw

Re: [Qemu-devel] [PATCH 3/4] target/cris: optimize swap

In reply to Aurelien Jarno
Quoted 7 lines · Aurelien Jarno

Use the same mask to avoid having to load two different constants, as suggest by Richard Henderson. Also use one less temp.

---
  target/cris/translate.c | 15 +++++++--------
  1 file changed, 7 insertions(+), 8 deletions(-)

r~

Reviewed-by on v1 4/4
#11 lore raw

Re: [Qemu-devel] [PATCH 4/4] target/mips: optimize WSBH, DSBH and DSHD

In reply to Aurelien Jarno
Quoted 7 lines · Aurelien Jarno

Use the same mask to avoid having to load two different constants, as suggested by Richard Henderson.

---
  target/mips/translate.c | 18 ++++++++++++------
  1 file changed, 12 insertions(+), 6 deletions(-)

r~