Patchwork [for-next,2/8] tcg-i386: Try pc-relative lea for constant formation

login
register
mail settings
Submitter Richard Henderson
Date Aug. 5, 2013, 6:07 p.m.
Message ID <1375726045-20797-3-git-send-email-rth@twiddle.net>
Download mbox | patch
Permalink /patch/264757/
State New
Headers show

Comments

Richard Henderson - Aug. 5, 2013, 6:07 p.m.
Use a 7 byte lea before the ultimate 10 byte movq.

Signed-off-by: Richard Henderson <rth@twiddle.net>
---
 tcg/i386/tcg-target.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)
Aurelien Jarno - Aug. 15, 2013, 3:54 p.m.
On Mon, Aug 05, 2013 at 08:07:19AM -1000, Richard Henderson wrote:
> Use a 7 byte lea before the ultimate 10 byte movq.
> 
> Signed-off-by: Richard Henderson <rth@twiddle.net>
> ---
>  tcg/i386/tcg-target.c | 19 ++++++++++++++++---
>  1 file changed, 16 insertions(+), 3 deletions(-)
> 
> diff --git a/tcg/i386/tcg-target.c b/tcg/i386/tcg-target.c
> index 841bd75..456bd9e 100644
> --- a/tcg/i386/tcg-target.c
> +++ b/tcg/i386/tcg-target.c
> @@ -541,19 +541,32 @@ static inline void tcg_out_mov(TCGContext *s, TCGType type,
>  static void tcg_out_movi(TCGContext *s, TCGType type,
>                           TCGReg ret, tcg_target_long arg)
>  {
> +    tcg_target_long diff;
> +
>      if (arg == 0) {
>          tgen_arithr(s, ARITH_XOR, ret, ret);
>          return;
>      } else if (arg == (uint32_t)arg || type == TCG_TYPE_I32) {
>          tcg_out_opc(s, OPC_MOVL_Iv + LOWREGMASK(ret), 0, ret, 0);
>          tcg_out32(s, arg);
> +        return;
>      } else if (arg == (int32_t)arg) {
>          tcg_out_modrm(s, OPC_MOVL_EvIz + P_REXW, 0, ret);
>          tcg_out32(s, arg);
> -    } else {
> -        tcg_out_opc(s, OPC_MOVL_Iv + P_REXW + LOWREGMASK(ret), 0, ret, 0);
> -        tcg_out64(s, arg);
> +        return;
>      }

Now that all the else parts end up with a return, it would improve
readability to remove them and keep only the ifs. 

> +
> +    /* Try a 7 byte pc-relative lea before the 10 byte movq.  */
> +    diff = arg - ((tcg_target_long)s->code_ptr + 7);
> +    if (diff == (int32_t)diff) {
> +        tcg_out_opc(s, OPC_LEA | P_REXW, ret, 0, 0);
> +        tcg_out8(s, (LOWREGMASK(ret) << 3) | 5);
> +        tcg_out32(s, diff);
> +        return;
> +    }
> +
> +    tcg_out_opc(s, OPC_MOVL_Iv + P_REXW + LOWREGMASK(ret), 0, ret, 0);
> +    tcg_out64(s, arg);
>  }
>  
>  static inline void tcg_out_pushi(TCGContext *s, tcg_target_long val)

Otherwise it looks good.

Patch

diff --git a/tcg/i386/tcg-target.c b/tcg/i386/tcg-target.c
index 841bd75..456bd9e 100644
--- a/tcg/i386/tcg-target.c
+++ b/tcg/i386/tcg-target.c
@@ -541,19 +541,32 @@  static inline void tcg_out_mov(TCGContext *s, TCGType type,
 static void tcg_out_movi(TCGContext *s, TCGType type,
                          TCGReg ret, tcg_target_long arg)
 {
+    tcg_target_long diff;
+
     if (arg == 0) {
         tgen_arithr(s, ARITH_XOR, ret, ret);
         return;
     } else if (arg == (uint32_t)arg || type == TCG_TYPE_I32) {
         tcg_out_opc(s, OPC_MOVL_Iv + LOWREGMASK(ret), 0, ret, 0);
         tcg_out32(s, arg);
+        return;
     } else if (arg == (int32_t)arg) {
         tcg_out_modrm(s, OPC_MOVL_EvIz + P_REXW, 0, ret);
         tcg_out32(s, arg);
-    } else {
-        tcg_out_opc(s, OPC_MOVL_Iv + P_REXW + LOWREGMASK(ret), 0, ret, 0);
-        tcg_out64(s, arg);
+        return;
     }
+
+    /* Try a 7 byte pc-relative lea before the 10 byte movq.  */
+    diff = arg - ((tcg_target_long)s->code_ptr + 7);
+    if (diff == (int32_t)diff) {
+        tcg_out_opc(s, OPC_LEA | P_REXW, ret, 0, 0);
+        tcg_out8(s, (LOWREGMASK(ret) << 3) | 5);
+        tcg_out32(s, diff);
+        return;
+    }
+
+    tcg_out_opc(s, OPC_MOVL_Iv + P_REXW + LOWREGMASK(ret), 0, ret, 0);
+    tcg_out64(s, arg);
 }
 
 static inline void tcg_out_pushi(TCGContext *s, tcg_target_long val)