public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] aarch64: Improve cost of `a ? {-,}1 : b`
@ 2023-11-27  4:33 Andrew Pinski
  2023-11-27 15:35 ` Richard Sandiford
  0 siblings, 1 reply; 5+ messages in thread
From: Andrew Pinski @ 2023-11-27  4:33 UTC (permalink / raw)
  To: gcc-patches

While looking into PR 112454, I found the cost for
`(if_then_else (cmp) (const_int 1) (reg))` was being recorded as 8
(or `COSTS_N_INSNS (2)`) but it should have been 4 (or `COSTS_N_INSNS (1)`).
This improves the cost by not adding the cost of `(const_int 1)` to
the total cost.

It does not does not fix PR 112454 as that requires other changes to forwprop
the `(const_int 1)` earlier than combine.

Bootstrapped and tested on aarch64-linux-gnu with no regressions.

gcc/ChangeLog:

	* config/aarch64/aarch64.cc (aarch64_if_then_else_costs):
	Don't add the cost of `1` or `-1`.

Signed-off-by: Andrew Pinski <quic_apinski@quicinc.com>
---
 gcc/config/aarch64/aarch64.cc | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/gcc/config/aarch64/aarch64.cc b/gcc/config/aarch64/aarch64.cc
index f6f6f94bf43..63241c5aaa5 100644
--- a/gcc/config/aarch64/aarch64.cc
+++ b/gcc/config/aarch64/aarch64.cc
@@ -11642,9 +11642,16 @@ aarch64_if_then_else_costs (rtx op0, rtx op1, rtx op2, int *cost, bool speed)
 	    /* CSINV/NEG with zero extend + const 0 (*csinv3_uxtw_insn3).  */
 	    op1 = XEXP (inner, 0);
 	}
-
-      *cost += rtx_cost (op1, VOIDmode, IF_THEN_ELSE, 1, speed);
-      *cost += rtx_cost (op2, VOIDmode, IF_THEN_ELSE, 2, speed);
+      if (op2 == constm1_rtx || op2 == const1_rtx)
+	*cost += rtx_cost (op1, VOIDmode, IF_THEN_ELSE, 1, speed);
+      else if (op1 == constm1_rtx || op1 == const1_rtx)
+	*cost += rtx_cost (op2, VOIDmode, IF_THEN_ELSE, 2, speed);
+      else
+	{
+	  *cost += rtx_cost (op1, VOIDmode, IF_THEN_ELSE, 1, speed);
+	  *cost += rtx_cost (op2, VOIDmode, IF_THEN_ELSE, 1, speed);
+	}
+      
       return true;
     }
 
-- 
2.34.1


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] aarch64: Improve cost of `a ? {-,}1 : b`
  2023-11-27  4:33 [PATCH] aarch64: Improve cost of `a ? {-,}1 : b` Andrew Pinski
@ 2023-11-27 15:35 ` Richard Sandiford
  2023-11-27 15:44   ` Richard Sandiford
  2023-11-27 16:11   ` Andrew Pinski (QUIC)
  0 siblings, 2 replies; 5+ messages in thread
From: Richard Sandiford @ 2023-11-27 15:35 UTC (permalink / raw)
  To: Andrew Pinski; +Cc: gcc-patches

Andrew Pinski <quic_apinski@quicinc.com> writes:
> While looking into PR 112454, I found the cost for
> `(if_then_else (cmp) (const_int 1) (reg))` was being recorded as 8
> (or `COSTS_N_INSNS (2)`) but it should have been 4 (or `COSTS_N_INSNS (1)`).
> This improves the cost by not adding the cost of `(const_int 1)` to
> the total cost.
>
> It does not does not fix PR 112454 as that requires other changes to forwprop
> the `(const_int 1)` earlier than combine.
>
> Bootstrapped and tested on aarch64-linux-gnu with no regressions.
>
> gcc/ChangeLog:
>
> 	* config/aarch64/aarch64.cc (aarch64_if_then_else_costs):
> 	Don't add the cost of `1` or `-1`.
>
> Signed-off-by: Andrew Pinski <quic_apinski@quicinc.com>
> ---
>  gcc/config/aarch64/aarch64.cc | 13 ++++++++++---
>  1 file changed, 10 insertions(+), 3 deletions(-)
>
> diff --git a/gcc/config/aarch64/aarch64.cc b/gcc/config/aarch64/aarch64.cc
> index f6f6f94bf43..63241c5aaa5 100644
> --- a/gcc/config/aarch64/aarch64.cc
> +++ b/gcc/config/aarch64/aarch64.cc
> @@ -11642,9 +11642,16 @@ aarch64_if_then_else_costs (rtx op0, rtx op1, rtx op2, int *cost, bool speed)
>  	    /* CSINV/NEG with zero extend + const 0 (*csinv3_uxtw_insn3).  */
>  	    op1 = XEXP (inner, 0);
>  	}
> -
> -      *cost += rtx_cost (op1, VOIDmode, IF_THEN_ELSE, 1, speed);
> -      *cost += rtx_cost (op2, VOIDmode, IF_THEN_ELSE, 2, speed);
> +      if (op2 == constm1_rtx || op2 == const1_rtx)
> +	*cost += rtx_cost (op1, VOIDmode, IF_THEN_ELSE, 1, speed);
> +      else if (op1 == constm1_rtx || op1 == const1_rtx)
> +	*cost += rtx_cost (op2, VOIDmode, IF_THEN_ELSE, 2, speed);

It looks like this is really an extra option on top of the previous
if-else chain, since it only applies when OP1 and OP2 are still the
operands of the if_then_else.  So how about:

      else if (op1 == constm1_rtx || op1 == const1_rtx)
        {
	  /* Use CSINV.  */
	  *cost += rtx_cost (op2, VOIDmode, IF_THEN_ELSE, 2, speed);
	  return true;
        }
      else if (op2 == constm1_rtx || op2 == const1_rtx)
        {
	  /* Use CSINV.  */
	  *cost += rtx_cost (op1, VOIDmode, IF_THEN_ELSE, 1, speed);
	  return true;
        }

leaving the code to fall through to:

      *cost += rtx_cost (op1, VOIDmode, IF_THEN_ELSE, 1, speed);
      *cost += rtx_cost (op2, VOIDmode, IF_THEN_ELSE, 2, speed);
      return true;

as it does currently.  OK in that form if you agree.

Let me know if you don't.  But in that case:

> +      else
> +	{
> +	  *cost += rtx_cost (op1, VOIDmode, IF_THEN_ELSE, 1, speed);
> +	  *cost += rtx_cost (op2, VOIDmode, IF_THEN_ELSE, 1, speed);

should be 2, speed

> +	}
> +      

Thanks,
Richard

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] aarch64: Improve cost of `a ? {-,}1 : b`
  2023-11-27 15:35 ` Richard Sandiford
@ 2023-11-27 15:44   ` Richard Sandiford
  2023-11-27 16:11   ` Andrew Pinski (QUIC)
  1 sibling, 0 replies; 5+ messages in thread
From: Richard Sandiford @ 2023-11-27 15:44 UTC (permalink / raw)
  To: Andrew Pinski; +Cc: gcc-patches

Richard Sandiford <richard.sandiford@arm.com> writes:
> Andrew Pinski <quic_apinski@quicinc.com> writes:
>> While looking into PR 112454, I found the cost for
>> `(if_then_else (cmp) (const_int 1) (reg))` was being recorded as 8
>> (or `COSTS_N_INSNS (2)`) but it should have been 4 (or `COSTS_N_INSNS (1)`).
>> This improves the cost by not adding the cost of `(const_int 1)` to
>> the total cost.
>>
>> It does not does not fix PR 112454 as that requires other changes to forwprop
>> the `(const_int 1)` earlier than combine.
>>
>> Bootstrapped and tested on aarch64-linux-gnu with no regressions.
>>
>> gcc/ChangeLog:
>>
>> 	* config/aarch64/aarch64.cc (aarch64_if_then_else_costs):
>> 	Don't add the cost of `1` or `-1`.
>>
>> Signed-off-by: Andrew Pinski <quic_apinski@quicinc.com>
>> ---
>>  gcc/config/aarch64/aarch64.cc | 13 ++++++++++---
>>  1 file changed, 10 insertions(+), 3 deletions(-)
>>
>> diff --git a/gcc/config/aarch64/aarch64.cc b/gcc/config/aarch64/aarch64.cc
>> index f6f6f94bf43..63241c5aaa5 100644
>> --- a/gcc/config/aarch64/aarch64.cc
>> +++ b/gcc/config/aarch64/aarch64.cc
>> @@ -11642,9 +11642,16 @@ aarch64_if_then_else_costs (rtx op0, rtx op1, rtx op2, int *cost, bool speed)
>>  	    /* CSINV/NEG with zero extend + const 0 (*csinv3_uxtw_insn3).  */
>>  	    op1 = XEXP (inner, 0);
>>  	}
>> -
>> -      *cost += rtx_cost (op1, VOIDmode, IF_THEN_ELSE, 1, speed);
>> -      *cost += rtx_cost (op2, VOIDmode, IF_THEN_ELSE, 2, speed);
>> +      if (op2 == constm1_rtx || op2 == const1_rtx)
>> +	*cost += rtx_cost (op1, VOIDmode, IF_THEN_ELSE, 1, speed);
>> +      else if (op1 == constm1_rtx || op1 == const1_rtx)
>> +	*cost += rtx_cost (op2, VOIDmode, IF_THEN_ELSE, 2, speed);
>
> It looks like this is really an extra option on top of the previous
> if-else chain, since it only applies when OP1 and OP2 are still the
> operands of the if_then_else.  So how about:
>
>       else if (op1 == constm1_rtx || op1 == const1_rtx)
>         {
> 	  /* Use CSINV.  */

eh, of course I meant CSINV or CSINC...

> 	  *cost += rtx_cost (op2, VOIDmode, IF_THEN_ELSE, 2, speed);
> 	  return true;
>         }
>       else if (op2 == constm1_rtx || op2 == const1_rtx)
>         {
> 	  /* Use CSINV.  */
> 	  *cost += rtx_cost (op1, VOIDmode, IF_THEN_ELSE, 1, speed);
> 	  return true;
>         }
>
> leaving the code to fall through to:
>
>       *cost += rtx_cost (op1, VOIDmode, IF_THEN_ELSE, 1, speed);
>       *cost += rtx_cost (op2, VOIDmode, IF_THEN_ELSE, 2, speed);
>       return true;
>
> as it does currently.  OK in that form if you agree.
>
> Let me know if you don't.  But in that case:
>
>> +      else
>> +	{
>> +	  *cost += rtx_cost (op1, VOIDmode, IF_THEN_ELSE, 1, speed);
>> +	  *cost += rtx_cost (op2, VOIDmode, IF_THEN_ELSE, 1, speed);
>
> should be 2, speed
>
>> +	}
>> +      
>
> Thanks,
> Richard

^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: [PATCH] aarch64: Improve cost of `a ? {-,}1 : b`
  2023-11-27 15:35 ` Richard Sandiford
  2023-11-27 15:44   ` Richard Sandiford
@ 2023-11-27 16:11   ` Andrew Pinski (QUIC)
  1 sibling, 0 replies; 5+ messages in thread
From: Andrew Pinski (QUIC) @ 2023-11-27 16:11 UTC (permalink / raw)
  To: Richard Sandiford, Andrew Pinski (QUIC); +Cc: gcc-patches

> -----Original Message-----
> From: Richard Sandiford <richard.sandiford@arm.com>
> Sent: Monday, November 27, 2023 7:35 AM
> To: Andrew Pinski (QUIC) <quic_apinski@quicinc.com>
> Cc: gcc-patches@gcc.gnu.org
> Subject: Re: [PATCH] aarch64: Improve cost of `a ? {-,}1 : b`
> 
> Andrew Pinski <quic_apinski@quicinc.com> writes:
> > While looking into PR 112454, I found the cost for `(if_then_else
> > (cmp) (const_int 1) (reg))` was being recorded as 8 (or `COSTS_N_INSNS
> > (2)`) but it should have been 4 (or `COSTS_N_INSNS (1)`).
> > This improves the cost by not adding the cost of `(const_int 1)` to
> > the total cost.
> >
> > It does not does not fix PR 112454 as that requires other changes to
> > forwprop the `(const_int 1)` earlier than combine.
> >
> > Bootstrapped and tested on aarch64-linux-gnu with no regressions.
> >
> > gcc/ChangeLog:
> >
> > 	* config/aarch64/aarch64.cc (aarch64_if_then_else_costs):
> > 	Don't add the cost of `1` or `-1`.
> >
> > Signed-off-by: Andrew Pinski <quic_apinski@quicinc.com>
> > ---
> >  gcc/config/aarch64/aarch64.cc | 13 ++++++++++---
> >  1 file changed, 10 insertions(+), 3 deletions(-)
> >
> > diff --git a/gcc/config/aarch64/aarch64.cc
> > b/gcc/config/aarch64/aarch64.cc index f6f6f94bf43..63241c5aaa5 100644
> > --- a/gcc/config/aarch64/aarch64.cc
> > +++ b/gcc/config/aarch64/aarch64.cc
> > @@ -11642,9 +11642,16 @@ aarch64_if_then_else_costs (rtx op0, rtx op1,
> rtx op2, int *cost, bool speed)
> >  	    /* CSINV/NEG with zero extend + const 0 (*csinv3_uxtw_insn3).  */
> >  	    op1 = XEXP (inner, 0);
> >  	}
> > -
> > -      *cost += rtx_cost (op1, VOIDmode, IF_THEN_ELSE, 1, speed);
> > -      *cost += rtx_cost (op2, VOIDmode, IF_THEN_ELSE, 2, speed);
> > +      if (op2 == constm1_rtx || op2 == const1_rtx)
> > +	*cost += rtx_cost (op1, VOIDmode, IF_THEN_ELSE, 1, speed);
> > +      else if (op1 == constm1_rtx || op1 == const1_rtx)
> > +	*cost += rtx_cost (op2, VOIDmode, IF_THEN_ELSE, 2, speed);
> 
> It looks like this is really an extra option on top of the previous if-else chain,
> since it only applies when OP1 and OP2 are still the operands of the
> if_then_else.  So how about:
> 
>       else if (op1 == constm1_rtx || op1 == const1_rtx)
>         {
> 	  /* Use CSINV.  */
> 	  *cost += rtx_cost (op2, VOIDmode, IF_THEN_ELSE, 2, speed);
> 	  return true;
>         }
>       else if (op2 == constm1_rtx || op2 == const1_rtx)
>         {
> 	  /* Use CSINV.  */
> 	  *cost += rtx_cost (op1, VOIDmode, IF_THEN_ELSE, 1, speed);
> 	  return true;
>         }
> 
> leaving the code to fall through to:
> 
>       *cost += rtx_cost (op1, VOIDmode, IF_THEN_ELSE, 1, speed);
>       *cost += rtx_cost (op2, VOIDmode, IF_THEN_ELSE, 2, speed);
>       return true;
> 
> as it does currently.  OK in that form if you agree.

Yes I think this is the correct way of implementing this, Let me test it and get back to you.

Thanks,
Andrew

> 
> Let me know if you don't.  But in that case:
> 
> > +      else
> > +	{
> > +	  *cost += rtx_cost (op1, VOIDmode, IF_THEN_ELSE, 1, speed);
> > +	  *cost += rtx_cost (op2, VOIDmode, IF_THEN_ELSE, 1, speed);
> 
> should be 2, speed
> 
> > +	}
> > +
> 
> Thanks,
> Richard

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH] aarch64: Improve cost of `a ? {-,}1 : b`
@ 2023-11-27 23:08 Andrew Pinski
  0 siblings, 0 replies; 5+ messages in thread
From: Andrew Pinski @ 2023-11-27 23:08 UTC (permalink / raw)
  To: gcc-patches

While looking into PR 112454, I found the cost for
`(if_then_else (cmp) (const_int 1) (reg))` was being recorded as 8
(or `COSTS_N_INSNS (2)`) but it should have been 4 (or `COSTS_N_INSNS (1)`).
This improves the cost by not adding the cost of `(const_int 1)` to
the total cost.

It does not does not fully fix PR 112454 as that requires other changes to forwprop
the `(const_int 1)` earlier than combine. Though we do fix the loop case where the
constant was only used once.

Committed as approved after bootstrapped and tested on aarch64-linux-gnu with no regressions.

gcc/ChangeLog:

	* config/aarch64/aarch64.cc (aarch64_if_then_else_costs):
	Handle csinv/csinc case of 1/-1.

gcc/testsuite/ChangeLog:

	* gcc.target/aarch64/csinc-3.c: New test.

Signed-off-by: Andrew Pinski <quic_apinski@quicinc.com>
---
 gcc/config/aarch64/aarch64.cc              | 12 ++++++++++++
 gcc/testsuite/gcc.target/aarch64/csinc-3.c | 10 ++++++++++
 2 files changed, 22 insertions(+)
 create mode 100644 gcc/testsuite/gcc.target/aarch64/csinc-3.c

diff --git a/gcc/config/aarch64/aarch64.cc b/gcc/config/aarch64/aarch64.cc
index b2093430937..4fd8c2de43a 100644
--- a/gcc/config/aarch64/aarch64.cc
+++ b/gcc/config/aarch64/aarch64.cc
@@ -11607,6 +11607,18 @@ aarch64_if_then_else_costs (rtx op0, rtx op1, rtx op2, int *cost, bool speed)
 	    /* CSINV/NEG with zero extend + const 0 (*csinv3_uxtw_insn3).  */
 	    op1 = XEXP (inner, 0);
 	}
+      else if (op1 == constm1_rtx || op1 == const1_rtx)
+	{
+	  /* Use CSINV or CSINC.  */
+	  *cost += rtx_cost (op2, VOIDmode, IF_THEN_ELSE, 2, speed);
+	  return true;
+	}
+      else if (op2 == constm1_rtx || op2 == const1_rtx)
+	{
+	  /* Use CSINV or CSINC.  */
+	  *cost += rtx_cost (op1, VOIDmode, IF_THEN_ELSE, 1, speed);
+	  return true;
+	}
 
       *cost += rtx_cost (op1, VOIDmode, IF_THEN_ELSE, 1, speed);
       *cost += rtx_cost (op2, VOIDmode, IF_THEN_ELSE, 2, speed);
diff --git a/gcc/testsuite/gcc.target/aarch64/csinc-3.c b/gcc/testsuite/gcc.target/aarch64/csinc-3.c
new file mode 100644
index 00000000000..bde131a584e
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/csinc-3.c
@@ -0,0 +1,10 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fno-tree-vectorize" } */
+
+int f(int *a, int n, int *b, int d)
+{
+  for(int i = 0; i < n; i++)
+    b[i] = a[i] == 100 ? 1 : d;
+  /* { dg-final { scan-assembler "csinc\tw\[0-9\].*wzr" } } */
+  return 0;
+}
-- 
2.34.1


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-11-27 23:09 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-27  4:33 [PATCH] aarch64: Improve cost of `a ? {-,}1 : b` Andrew Pinski
2023-11-27 15:35 ` Richard Sandiford
2023-11-27 15:44   ` Richard Sandiford
2023-11-27 16:11   ` Andrew Pinski (QUIC)
2023-11-27 23:08 Andrew Pinski

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 read-only IMAP folder(s) and NNTP newsgroup(s).