Patchwork tcg-optimize: Fold sub r,0,x to neg r,x

login
register
mail settings
Submitter Richard Henderson
Date Feb. 19, 2013, 7:03 p.m.
Message ID <1361300601-12649-1-git-send-email-rth@twiddle.net>
Download mbox | patch
Permalink /patch/221854/
State New
Headers show

Comments

Richard Henderson - Feb. 19, 2013, 7:03 p.m.
Signed-off-by: Richard Henderson <rth@twiddle.net>
---
 tcg/optimize.c | 33 ++++++++++++++++++++++++++++++++-
 1 file changed, 32 insertions(+), 1 deletion(-)

I noticed this while looking at other dumps.  This seems like it
should be considered independently of the other patch series.

r~
Blue Swirl - Feb. 23, 2013, 4:43 p.m.
On Tue, Feb 19, 2013 at 7:03 PM, Richard Henderson <rth@twiddle.net> wrote:
> Signed-off-by: Richard Henderson <rth@twiddle.net>
> ---
>  tcg/optimize.c | 33 ++++++++++++++++++++++++++++++++-
>  1 file changed, 32 insertions(+), 1 deletion(-)
>
> I noticed this while looking at other dumps.  This seems like it
> should be considered independently of the other patch series.

This breaks Sparc32 boot, only a few first instructions are executed.

>
> r~
>
>
> diff --git a/tcg/optimize.c b/tcg/optimize.c
> index 973d2d6..7c172c5 100644
> --- a/tcg/optimize.c
> +++ b/tcg/optimize.c
> @@ -575,7 +575,8 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
>              break;
>          }
>
> -        /* Simplify expressions for "shift/rot r, 0, a => movi r, 0" */
> +        /* Simplify expressions for "shift/rot r, 0, a => movi r, 0",
> +           and "sub r, 0, a => neg r, a" case.  */
>          switch (op) {
>          CASE_OP_32_64(shl):
>          CASE_OP_32_64(shr):
> @@ -591,6 +592,36 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
>                  continue;
>              }
>              break;
> +        CASE_OP_32_64(sub):
> +            {
> +                TCGOpcode neg_op;
> +                bool have_neg;
> +
> +                if (temps[args[2]].state == TCG_TEMP_CONST) {
> +                    /* Proceed with possible constant folding. */
> +                    break;
> +                }
> +                if (op == INDEX_op_sub_i32) {
> +                    neg_op = INDEX_op_neg_i32;
> +                    have_neg = TCG_TARGET_HAS_neg_i32;
> +                } else {
> +                    neg_op = INDEX_op_neg_i64;
> +                    have_neg = TCG_TARGET_HAS_neg_i64;
> +                }
> +                if (!have_neg) {
> +                    break;
> +                }
> +                if (temps[args[1]].state == TCG_TEMP_CONST
> +                    && temps[args[1]].val == 0) {
> +                    s->gen_opc_buf[op_index] = neg_op;
> +                    gen_args[0] = args[0];
> +                    gen_args[1] = args[2];
> +                    args += 3;
> +                    gen_args += 2;
> +                    continue;
> +                }
> +            }
> +            break;
>          default:
>              break;
>          }
> --
> 1.8.1.2
>
Richard Henderson - March 20, 2013, 12:13 a.m.
On 2013-02-23 08:43, Blue Swirl wrote:
>> Signed-off-by: Richard Henderson<rth@twiddle.net>
>> >---
>> >  tcg/optimize.c | 33 ++++++++++++++++++++++++++++++++-
>> >  1 file changed, 32 insertions(+), 1 deletion(-)
>> >
>> >I noticed this while looking at other dumps.  This seems like it
>> >should be considered independently of the other patch series.
> This breaks Sparc32 boot, only a few first instructions are executed.
>

I can't replicate any failure with the sparc-test image.
Can you expand on a method to replicate this?


r~
Blue Swirl - March 20, 2013, 8:59 p.m.
On Wed, Mar 20, 2013 at 12:13 AM, Richard Henderson <rth@twiddle.net> wrote:
> On 2013-02-23 08:43, Blue Swirl wrote:
>>>
>>> Signed-off-by: Richard Henderson<rth@twiddle.net>
>>> >---
>>> >  tcg/optimize.c | 33 ++++++++++++++++++++++++++++++++-
>>> >  1 file changed, 32 insertions(+), 1 deletion(-)
>>> >
>>> >I noticed this while looking at other dumps.  This seems like it
>>> >should be considered independently of the other patch series.
>>
>> This breaks Sparc32 boot, only a few first instructions are executed.
>>
>
> I can't replicate any failure with the sparc-test image.
> Can you expand on a method to replicate this?

It was just with qemu-system-sparc -d in_asm. Unpatched can get to
OpenBIOS prompt, patched does not leave boot mode, PC always < 0x6000.

>
>
> r~

Patch

diff --git a/tcg/optimize.c b/tcg/optimize.c
index 973d2d6..7c172c5 100644
--- a/tcg/optimize.c
+++ b/tcg/optimize.c
@@ -575,7 +575,8 @@  static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
             break;
         }
 
-        /* Simplify expressions for "shift/rot r, 0, a => movi r, 0" */
+        /* Simplify expressions for "shift/rot r, 0, a => movi r, 0",
+           and "sub r, 0, a => neg r, a" case.  */
         switch (op) {
         CASE_OP_32_64(shl):
         CASE_OP_32_64(shr):
@@ -591,6 +592,36 @@  static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,
                 continue;
             }
             break;
+        CASE_OP_32_64(sub):
+            {
+                TCGOpcode neg_op;
+                bool have_neg;
+
+                if (temps[args[2]].state == TCG_TEMP_CONST) {
+                    /* Proceed with possible constant folding. */
+                    break;
+                }
+                if (op == INDEX_op_sub_i32) {
+                    neg_op = INDEX_op_neg_i32;
+                    have_neg = TCG_TARGET_HAS_neg_i32;
+                } else {
+                    neg_op = INDEX_op_neg_i64;
+                    have_neg = TCG_TARGET_HAS_neg_i64;
+                }
+                if (!have_neg) {
+                    break;
+                }
+                if (temps[args[1]].state == TCG_TEMP_CONST
+                    && temps[args[1]].val == 0) {
+                    s->gen_opc_buf[op_index] = neg_op;
+                    gen_args[0] = args[0];
+                    gen_args[1] = args[2];
+                    args += 3;
+                    gen_args += 2;
+                    continue;
+                }
+            }
+            break;
         default:
             break;
         }