Patchwork [5/6] tcg-i386: Simplify brcond2.

login
register
mail settings
Submitter Richard Henderson
Date Dec. 17, 2009, 6:38 p.m.
Message ID <38131b3a03ff4872a19c52b5c042dd7d77341b75.1261078375.git.rth@twiddle.net>
Download mbox | patch
Permalink /patch/41340/
State New
Headers show

Comments

Richard Henderson - Dec. 17, 2009, 6:38 p.m.
Split out tcg_out_cond from tcg_out_brcond.  Add "small" arguments
to all branch functions for completeness.  Unify all the calls to
generate branches within brcond2 and pass on the small flag.

Signed-off-by: Richard Henderson <rth@twiddle.net>
---
 tcg/i386/tcg-target.c |   87 ++++++++++++++++++++++--------------------------
 1 files changed, 40 insertions(+), 47 deletions(-)
Laurent Desnogues - Dec. 18, 2009, 11:40 a.m.
On Thu, Dec 17, 2009 at 7:38 PM, Richard Henderson <rth@twiddle.net> wrote:
> Split out tcg_out_cond from tcg_out_brcond.  Add "small" arguments
> to all branch functions for completeness.  Unify all the calls to
> generate branches within brcond2 and pass on the small flag.
>
> Signed-off-by: Richard Henderson <rth@twiddle.net>
> ---
>  tcg/i386/tcg-target.c |   87 ++++++++++++++++++++++--------------------------
>  1 files changed, 40 insertions(+), 47 deletions(-)
>
> diff --git a/tcg/i386/tcg-target.c b/tcg/i386/tcg-target.c
> index cc3d28f..f7b2416 100644
> --- a/tcg/i386/tcg-target.c
> +++ b/tcg/i386/tcg-target.c
> @@ -355,9 +355,8 @@ static void tcg_out_jxx(TCGContext *s, int opc, int label_index, int small)
>     }
>  }
>
> -static void tcg_out_brcond(TCGContext *s, int cond,
> -                           TCGArg arg1, TCGArg arg2, int const_arg2,
> -                           int label_index)
> +static void tcg_out_cond(TCGContext *s, int cond,
> +                         TCGArg arg1, TCGArg arg2, int const_arg2)
>  {
>     if (const_arg2) {
>         if (arg2 == 0) {
> @@ -369,68 +368,61 @@ static void tcg_out_brcond(TCGContext *s, int cond,
>     } else {
>         tcg_out_modrm(s, 0x01 | (ARITH_CMP << 3), arg2, arg1);
>     }
> -    tcg_out_jxx(s, tcg_cond_to_jcc[cond], label_index, 0);
> +}
> +
> +static void tcg_out_brcond(TCGContext *s, int cond,
> +                           TCGArg arg1, TCGArg arg2, int const_arg2,
> +                           int label_index, int small)
> +{
> +    tcg_out_cond(s, cond, arg1, arg2, const_arg2);
> +    tcg_out_jxx(s, tcg_cond_to_jcc[cond], label_index, small);
>  }
>
>  /* XXX: we implement it at the target level to avoid having to
>    handle cross basic blocks temporaries */
> -static void tcg_out_brcond2(TCGContext *s,
> -                            const TCGArg *args, const int *const_args)
> +static void tcg_out_brcond2(TCGContext *s, const TCGArg *args,
> +                            const int *const_args, int small)
>  {
> -    int label_next;
> -    label_next = gen_new_label();
> -    switch(args[4]) {
> +    int label_next = gen_new_label();
> +    int label_dest = args[5];
> +    int cond = args[4], c1, c2, c3;
> +
> +    switch (cond) {
>     case TCG_COND_EQ:
> -        tcg_out_brcond(s, TCG_COND_NE, args[0], args[2], const_args[2], label_next);
> -        tcg_out_brcond(s, TCG_COND_EQ, args[1], args[3], const_args[3], args[5]);
> +        c1 = -1, c2 = TCG_COND_NE, c3 = TCG_COND_EQ;
>         break;
>     case TCG_COND_NE:
> -        tcg_out_brcond(s, TCG_COND_NE, args[0], args[2], const_args[2], args[5]);
> -        tcg_out_brcond(s, TCG_COND_NE, args[1], args[3], const_args[3], args[5]);
> +        c1 = TCG_COND_NE, c2 = -1, c3 = TCG_COND_NE;
>         break;
>     case TCG_COND_LT:
> -        tcg_out_brcond(s, TCG_COND_LT, args[1], args[3], const_args[3], args[5]);
> -        tcg_out_jxx(s, JCC_JNE, label_next, 1);
> -        tcg_out_brcond(s, TCG_COND_LTU, args[0], args[2], const_args[2], args[5]);
> -        break;
> -    case TCG_COND_LE:
> -        tcg_out_brcond(s, TCG_COND_LT, args[1], args[3], const_args[3], args[5]);
> -        tcg_out_jxx(s, JCC_JNE, label_next, 1);
> -        tcg_out_brcond(s, TCG_COND_LEU, args[0], args[2], const_args[2], args[5]);
> -        break;
> -    case TCG_COND_GT:
> -        tcg_out_brcond(s, TCG_COND_GT, args[1], args[3], const_args[3], args[5]);
> -        tcg_out_jxx(s, JCC_JNE, label_next, 1);
> -        tcg_out_brcond(s, TCG_COND_GTU, args[0], args[2], const_args[2], args[5]);
> -        break;
> -    case TCG_COND_GE:
> -        tcg_out_brcond(s, TCG_COND_GT, args[1], args[3], const_args[3], args[5]);
> -        tcg_out_jxx(s, JCC_JNE, label_next, 1);
> -        tcg_out_brcond(s, TCG_COND_GEU, args[0], args[2], const_args[2], args[5]);
> -        break;
>     case TCG_COND_LTU:
> -        tcg_out_brcond(s, TCG_COND_LTU, args[1], args[3], const_args[3], args[5]);
> -        tcg_out_jxx(s, JCC_JNE, label_next, 1);
> -        tcg_out_brcond(s, TCG_COND_LTU, args[0], args[2], const_args[2], args[5]);
> +        c1 = cond, c2 = TCG_COND_NE, c3 = TCG_COND_LTU;
>         break;
> +    case TCG_COND_LE:
>     case TCG_COND_LEU:
> -        tcg_out_brcond(s, TCG_COND_LTU, args[1], args[3], const_args[3], args[5]);
> -        tcg_out_jxx(s, JCC_JNE, label_next, 1);
> -        tcg_out_brcond(s, TCG_COND_LEU, args[0], args[2], const_args[2], args[5]);
> +        c1 = cond, c2 = TCG_COND_NE, c3 = TCG_COND_LEU;
>         break;
> +    case TCG_COND_GT:
>     case TCG_COND_GTU:
> -        tcg_out_brcond(s, TCG_COND_GTU, args[1], args[3], const_args[3], args[5]);
> -        tcg_out_jxx(s, JCC_JNE, label_next, 1);
> -        tcg_out_brcond(s, TCG_COND_GTU, args[0], args[2], const_args[2], args[5]);
> +        c1 = cond, c2 = TCG_COND_NE, c3 = TCG_COND_GTU;
>         break;
> +    case TCG_COND_GE:
>     case TCG_COND_GEU:
> -        tcg_out_brcond(s, TCG_COND_GTU, args[1], args[3], const_args[3], args[5]);
> -        tcg_out_jxx(s, JCC_JNE, label_next, 1);
> -        tcg_out_brcond(s, TCG_COND_GEU, args[0], args[2], const_args[2], args[5]);
> +        c1 = cond, c2 = TCG_COND_NE, c3 = TCG_COND_GEU;
>         break;
>     default:
> -        tcg_abort();
> +        tcg_abort ();

This is unwanted :-)

> +    }
> +
> +    tcg_out_cond(s, cond, args[1], args[3], const_args[3]);
> +    if (c1 != -1) {
> +        tcg_out_jxx(s, tcg_cond_to_jcc[c1], label_dest, small);
>     }
> +    if (c2 != -1) {
> +        tcg_out_jxx(s, tcg_cond_to_jcc[c2], label_next, 1);
> +    }
> +    tcg_out_brcond(s, c3, args[0], args[2], const_args[2], label_dest, small);
> +
>     tcg_out_label(s, label_next, (tcg_target_long)s->code_ptr);
>  }

I'm not sure I really like that rewrite, I find it hard to read.
Convince me it's better :-)


Laurent

> @@ -1058,10 +1050,11 @@ static inline void tcg_out_op(TCGContext *s, int opc,
>             tcg_out_modrm(s, 0x01 | (ARITH_SBB << 3), args[5], args[1]);
>         break;
>     case INDEX_op_brcond_i32:
> -        tcg_out_brcond(s, args[2], args[0], args[1], const_args[1], args[3]);
> +        tcg_out_brcond(s, args[2], args[0], args[1], const_args[1],
> +                       args[3], 0);
>         break;
>     case INDEX_op_brcond2_i32:
> -        tcg_out_brcond2(s, args, const_args);
> +        tcg_out_brcond2(s, args, const_args, 0);
>         break;
>
>     case INDEX_op_bswap16_i32:
> --
> 1.6.5.2
>
>
Richard Henderson - Dec. 18, 2009, 5:45 p.m.
On 12/18/2009 03:40 AM, Laurent Desnogues wrote:
> I'm not sure I really like that rewrite, I find it hard to read.
> Convince me it's better :-)

Would it be more obvious if I used a table instead of a switch?
The code size reduction is

    Num:    Value  Size Type    Bind   Vis      Ndx Name

before:
     40: 000039a0   793 FUNC    LOCAL  DEFAULT    1 tcg_out_brcond2
after:
     40: 00003a50   412 FUNC    LOCAL  DEFAULT    1 tcg_out_brcond2
table-ized:
     40: 00003a50   224 FUNC    LOCAL  DEFAULT    1 tcg_out_brcond2

But I'm not married to the patch; let's drop it if there's a question of 
it holding up the rest of the series.


r~

Patch

diff --git a/tcg/i386/tcg-target.c b/tcg/i386/tcg-target.c
index cc3d28f..f7b2416 100644
--- a/tcg/i386/tcg-target.c
+++ b/tcg/i386/tcg-target.c
@@ -355,9 +355,8 @@  static void tcg_out_jxx(TCGContext *s, int opc, int label_index, int small)
     }
 }
 
-static void tcg_out_brcond(TCGContext *s, int cond, 
-                           TCGArg arg1, TCGArg arg2, int const_arg2,
-                           int label_index)
+static void tcg_out_cond(TCGContext *s, int cond,
+                         TCGArg arg1, TCGArg arg2, int const_arg2)
 {
     if (const_arg2) {
         if (arg2 == 0) {
@@ -369,68 +368,61 @@  static void tcg_out_brcond(TCGContext *s, int cond,
     } else {
         tcg_out_modrm(s, 0x01 | (ARITH_CMP << 3), arg2, arg1);
     }
-    tcg_out_jxx(s, tcg_cond_to_jcc[cond], label_index, 0);
+}
+
+static void tcg_out_brcond(TCGContext *s, int cond,
+                           TCGArg arg1, TCGArg arg2, int const_arg2,
+                           int label_index, int small)
+{
+    tcg_out_cond(s, cond, arg1, arg2, const_arg2);
+    tcg_out_jxx(s, tcg_cond_to_jcc[cond], label_index, small);
 }
 
 /* XXX: we implement it at the target level to avoid having to
    handle cross basic blocks temporaries */
-static void tcg_out_brcond2(TCGContext *s,
-                            const TCGArg *args, const int *const_args)
+static void tcg_out_brcond2(TCGContext *s, const TCGArg *args,
+                            const int *const_args, int small)
 {
-    int label_next;
-    label_next = gen_new_label();
-    switch(args[4]) {
+    int label_next = gen_new_label();
+    int label_dest = args[5];
+    int cond = args[4], c1, c2, c3;
+
+    switch (cond) {
     case TCG_COND_EQ:
-        tcg_out_brcond(s, TCG_COND_NE, args[0], args[2], const_args[2], label_next);
-        tcg_out_brcond(s, TCG_COND_EQ, args[1], args[3], const_args[3], args[5]);
+        c1 = -1, c2 = TCG_COND_NE, c3 = TCG_COND_EQ;
         break;
     case TCG_COND_NE:
-        tcg_out_brcond(s, TCG_COND_NE, args[0], args[2], const_args[2], args[5]);
-        tcg_out_brcond(s, TCG_COND_NE, args[1], args[3], const_args[3], args[5]);
+        c1 = TCG_COND_NE, c2 = -1, c3 = TCG_COND_NE;
         break;
     case TCG_COND_LT:
-        tcg_out_brcond(s, TCG_COND_LT, args[1], args[3], const_args[3], args[5]);
-        tcg_out_jxx(s, JCC_JNE, label_next, 1);
-        tcg_out_brcond(s, TCG_COND_LTU, args[0], args[2], const_args[2], args[5]);
-        break;
-    case TCG_COND_LE:
-        tcg_out_brcond(s, TCG_COND_LT, args[1], args[3], const_args[3], args[5]);
-        tcg_out_jxx(s, JCC_JNE, label_next, 1);
-        tcg_out_brcond(s, TCG_COND_LEU, args[0], args[2], const_args[2], args[5]);
-        break;
-    case TCG_COND_GT:
-        tcg_out_brcond(s, TCG_COND_GT, args[1], args[3], const_args[3], args[5]);
-        tcg_out_jxx(s, JCC_JNE, label_next, 1);
-        tcg_out_brcond(s, TCG_COND_GTU, args[0], args[2], const_args[2], args[5]);
-        break;
-    case TCG_COND_GE:
-        tcg_out_brcond(s, TCG_COND_GT, args[1], args[3], const_args[3], args[5]);
-        tcg_out_jxx(s, JCC_JNE, label_next, 1);
-        tcg_out_brcond(s, TCG_COND_GEU, args[0], args[2], const_args[2], args[5]);
-        break;
     case TCG_COND_LTU:
-        tcg_out_brcond(s, TCG_COND_LTU, args[1], args[3], const_args[3], args[5]);
-        tcg_out_jxx(s, JCC_JNE, label_next, 1);
-        tcg_out_brcond(s, TCG_COND_LTU, args[0], args[2], const_args[2], args[5]);
+        c1 = cond, c2 = TCG_COND_NE, c3 = TCG_COND_LTU;
         break;
+    case TCG_COND_LE:
     case TCG_COND_LEU:
-        tcg_out_brcond(s, TCG_COND_LTU, args[1], args[3], const_args[3], args[5]);
-        tcg_out_jxx(s, JCC_JNE, label_next, 1);
-        tcg_out_brcond(s, TCG_COND_LEU, args[0], args[2], const_args[2], args[5]);
+        c1 = cond, c2 = TCG_COND_NE, c3 = TCG_COND_LEU;
         break;
+    case TCG_COND_GT:
     case TCG_COND_GTU:
-        tcg_out_brcond(s, TCG_COND_GTU, args[1], args[3], const_args[3], args[5]);
-        tcg_out_jxx(s, JCC_JNE, label_next, 1);
-        tcg_out_brcond(s, TCG_COND_GTU, args[0], args[2], const_args[2], args[5]);
+        c1 = cond, c2 = TCG_COND_NE, c3 = TCG_COND_GTU;
         break;
+    case TCG_COND_GE:
     case TCG_COND_GEU:
-        tcg_out_brcond(s, TCG_COND_GTU, args[1], args[3], const_args[3], args[5]);
-        tcg_out_jxx(s, JCC_JNE, label_next, 1);
-        tcg_out_brcond(s, TCG_COND_GEU, args[0], args[2], const_args[2], args[5]);
+        c1 = cond, c2 = TCG_COND_NE, c3 = TCG_COND_GEU;
         break;
     default:
-        tcg_abort();
+        tcg_abort ();
+    }
+
+    tcg_out_cond(s, cond, args[1], args[3], const_args[3]);
+    if (c1 != -1) {
+        tcg_out_jxx(s, tcg_cond_to_jcc[c1], label_dest, small);
     }
+    if (c2 != -1) {
+        tcg_out_jxx(s, tcg_cond_to_jcc[c2], label_next, 1);
+    }
+    tcg_out_brcond(s, c3, args[0], args[2], const_args[2], label_dest, small);
+
     tcg_out_label(s, label_next, (tcg_target_long)s->code_ptr);
 }
 
@@ -1058,10 +1050,11 @@  static inline void tcg_out_op(TCGContext *s, int opc,
             tcg_out_modrm(s, 0x01 | (ARITH_SBB << 3), args[5], args[1]);
         break;
     case INDEX_op_brcond_i32:
-        tcg_out_brcond(s, args[2], args[0], args[1], const_args[1], args[3]);
+        tcg_out_brcond(s, args[2], args[0], args[1], const_args[1],
+                       args[3], 0);
         break;
     case INDEX_op_brcond2_i32:
-        tcg_out_brcond2(s, args, const_args);
+        tcg_out_brcond2(s, args, const_args, 0);
         break;
 
     case INDEX_op_bswap16_i32: