public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Only allow (copysign x, NEG_CONST) -> (fneg (fabs x)) simplification for constant folding [PR112483]
@ 2023-11-14  9:59 Xi Ruoyao
  2023-11-14 10:44 ` Richard Biener
  0 siblings, 1 reply; 4+ messages in thread
From: Xi Ruoyao @ 2023-11-14  9:59 UTC (permalink / raw)
  To: gcc-patches
  Cc: chenglulu, i, xuchenghua, Tamar Christina, tschwinge,
	Roger Sayle, Andrew Pinski

From: Andrew Pinski <pinskia@gmail.com>

On targets with native copysign instructions, (copysign x, -1) is
usually more efficient than (fneg (fabs x)).  Since r14-5284, in the
middle end we always optimize (fneg (fabs x)) to (copysign x, -1), not
vice versa.  If the target does not support native fcopysign,
expand_COPYSIGN will expand it as (fneg (fabs x)) anyway.

gcc/ChangeLog:

	PR rtl-optimization/112483
	* simplify-rtx.cc (simplify_binary_operation_1) <case COPYSIGN>:
	Call simplify_unary_operation for NEG instead of
	simplify_gen_unary.
---

[xry111]:

Following Andrew's suggestion, I bootstrapped and regtested this
patch on loongarch64-linux-gnu.  Now with

	float t(float x)
	{
	  return __builtin_copysignf(x, -0.1234);
	}

It correctly generates

	pcalau12i	$r12,%pc_hi20(.LC0)
	fld.s	$f1,$r12,%pc_lo12(.LC0)
	fcopysign.s	$f0,$f0,$f1
	jr	$r1

instead of the de-optimized fabs.s and fneg.s

Ok for trunk?

 gcc/simplify-rtx.cc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/gcc/simplify-rtx.cc b/gcc/simplify-rtx.cc
index 2d2e5a3c1ca..f3745d86aea 100644
--- a/gcc/simplify-rtx.cc
+++ b/gcc/simplify-rtx.cc
@@ -4392,7 +4392,7 @@ simplify_ashift:
 	  real_convert (&f1, mode, CONST_DOUBLE_REAL_VALUE (trueop1));
 	  rtx tmp = simplify_gen_unary (ABS, mode, op0, mode);
 	  if (REAL_VALUE_NEGATIVE (f1))
-	    tmp = simplify_gen_unary (NEG, mode, tmp, mode);
+	    tmp = simplify_unary_operation (NEG, mode, tmp, mode);
 	  return tmp;
 	}
       if (GET_CODE (op0) == NEG || GET_CODE (op0) == ABS)
-- 
2.42.1


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

* Re: [PATCH] Only allow (copysign x, NEG_CONST) -> (fneg (fabs x)) simplification for constant folding [PR112483]
  2023-11-14  9:59 [PATCH] Only allow (copysign x, NEG_CONST) -> (fneg (fabs x)) simplification for constant folding [PR112483] Xi Ruoyao
@ 2023-11-14 10:44 ` Richard Biener
  2023-11-14 21:14   ` Xi Ruoyao
  0 siblings, 1 reply; 4+ messages in thread
From: Richard Biener @ 2023-11-14 10:44 UTC (permalink / raw)
  To: Xi Ruoyao
  Cc: gcc-patches, chenglulu, i, xuchenghua, Tamar Christina,
	tschwinge, Roger Sayle, Andrew Pinski

On Tue, Nov 14, 2023 at 11:04 AM Xi Ruoyao <xry111@xry111.site> wrote:
>
> From: Andrew Pinski <pinskia@gmail.com>
>
> On targets with native copysign instructions, (copysign x, -1) is
> usually more efficient than (fneg (fabs x)).  Since r14-5284, in the
> middle end we always optimize (fneg (fabs x)) to (copysign x, -1), not
> vice versa.  If the target does not support native fcopysign,
> expand_COPYSIGN will expand it as (fneg (fabs x)) anyway.
>
> gcc/ChangeLog:
>
>         PR rtl-optimization/112483
>         * simplify-rtx.cc (simplify_binary_operation_1) <case COPYSIGN>:
>         Call simplify_unary_operation for NEG instead of
>         simplify_gen_unary.
> ---
>
> [xry111]:
>
> Following Andrew's suggestion, I bootstrapped and regtested this
> patch on loongarch64-linux-gnu.  Now with
>
>         float t(float x)
>         {
>           return __builtin_copysignf(x, -0.1234);
>         }
>
> It correctly generates
>
>         pcalau12i       $r12,%pc_hi20(.LC0)
>         fld.s   $f1,$r12,%pc_lo12(.LC0)
>         fcopysign.s     $f0,$f0,$f1
>         jr      $r1
>
> instead of the de-optimized fabs.s and fneg.s
>
> Ok for trunk?
>
>  gcc/simplify-rtx.cc | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/gcc/simplify-rtx.cc b/gcc/simplify-rtx.cc
> index 2d2e5a3c1ca..f3745d86aea 100644
> --- a/gcc/simplify-rtx.cc
> +++ b/gcc/simplify-rtx.cc
> @@ -4392,7 +4392,7 @@ simplify_ashift:
>           real_convert (&f1, mode, CONST_DOUBLE_REAL_VALUE (trueop1));
>           rtx tmp = simplify_gen_unary (ABS, mode, op0, mode);
>           if (REAL_VALUE_NEGATIVE (f1))
> -           tmp = simplify_gen_unary (NEG, mode, tmp, mode);
> +           tmp = simplify_unary_operation (NEG, mode, tmp, mode);

shouldn't that be when either the ABS or the NEG simplify?  And I wonder
when that happens - I suppose when op0 is CONST_DOUBLE only?

>           return tmp;
>         }
>        if (GET_CODE (op0) == NEG || GET_CODE (op0) == ABS)
> --
> 2.42.1
>

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

* Re: [PATCH] Only allow (copysign x, NEG_CONST) -> (fneg (fabs x)) simplification for constant folding [PR112483]
  2023-11-14 10:44 ` Richard Biener
@ 2023-11-14 21:14   ` Xi Ruoyao
  2023-11-15 11:41     ` Richard Biener
  0 siblings, 1 reply; 4+ messages in thread
From: Xi Ruoyao @ 2023-11-14 21:14 UTC (permalink / raw)
  To: Richard Biener
  Cc: gcc-patches, chenglulu, i, xuchenghua, Tamar Christina,
	tschwinge, Roger Sayle, Andrew Pinski

On Tue, 2023-11-14 at 11:44 +0100, Richard Biener wrote:
> > diff --git a/gcc/simplify-rtx.cc b/gcc/simplify-rtx.cc
> > index 2d2e5a3c1ca..f3745d86aea 100644
> > --- a/gcc/simplify-rtx.cc
> > +++ b/gcc/simplify-rtx.cc
> > @@ -4392,7 +4392,7 @@ simplify_ashift:
> >            real_convert (&f1, mode, CONST_DOUBLE_REAL_VALUE (trueop1));
> >            rtx tmp = simplify_gen_unary (ABS, mode, op0, mode);
> >            if (REAL_VALUE_NEGATIVE (f1))
> > -           tmp = simplify_gen_unary (NEG, mode, tmp, mode);
> > +           tmp = simplify_unary_operation (NEG, mode, tmp, mode);
> >           return tmp;
> >         }
> 
> shouldn't that be when either the ABS or the NEG simplify?

Simplify (copysign x, POSTIVE_CONST) to (abs x) is an optimization.  So
for a positive f1, tmp will just be (abs x) and we return it.

> And I wonder when that happens - I suppose when op0 is CONST_DOUBLE only?

Yes, it's Andrew's intention.

-- 
Xi Ruoyao <xry111@xry111.site>
School of Aerospace Science and Technology, Xidian University

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

* Re: [PATCH] Only allow (copysign x, NEG_CONST) -> (fneg (fabs x)) simplification for constant folding [PR112483]
  2023-11-14 21:14   ` Xi Ruoyao
@ 2023-11-15 11:41     ` Richard Biener
  0 siblings, 0 replies; 4+ messages in thread
From: Richard Biener @ 2023-11-15 11:41 UTC (permalink / raw)
  To: Xi Ruoyao
  Cc: gcc-patches, chenglulu, i, xuchenghua, Tamar Christina,
	tschwinge, Roger Sayle, Andrew Pinski

On Tue, Nov 14, 2023 at 10:14 PM Xi Ruoyao <xry111@xry111.site> wrote:
>
> On Tue, 2023-11-14 at 11:44 +0100, Richard Biener wrote:
> > > diff --git a/gcc/simplify-rtx.cc b/gcc/simplify-rtx.cc
> > > index 2d2e5a3c1ca..f3745d86aea 100644
> > > --- a/gcc/simplify-rtx.cc
> > > +++ b/gcc/simplify-rtx.cc
> > > @@ -4392,7 +4392,7 @@ simplify_ashift:
> > >            real_convert (&f1, mode, CONST_DOUBLE_REAL_VALUE (trueop1));
> > >            rtx tmp = simplify_gen_unary (ABS, mode, op0, mode);
> > >            if (REAL_VALUE_NEGATIVE (f1))
> > > -           tmp = simplify_gen_unary (NEG, mode, tmp, mode);
> > > +           tmp = simplify_unary_operation (NEG, mode, tmp, mode);
> > >           return tmp;
> > >         }
> >
> > shouldn't that be when either the ABS or the NEG simplify?
>
> Simplify (copysign x, POSTIVE_CONST) to (abs x) is an optimization.  So
> for a positive f1, tmp will just be (abs x) and we return it.

Ah, OK.

> > And I wonder when that happens - I suppose when op0 is CONST_DOUBLE only?
>
> Yes, it's Andrew's intention.

The patch is fine then.

Richard.

> --
> Xi Ruoyao <xry111@xry111.site>
> School of Aerospace Science and Technology, Xidian University

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

end of thread, other threads:[~2023-11-15 11:44 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-14  9:59 [PATCH] Only allow (copysign x, NEG_CONST) -> (fneg (fabs x)) simplification for constant folding [PR112483] Xi Ruoyao
2023-11-14 10:44 ` Richard Biener
2023-11-14 21:14   ` Xi Ruoyao
2023-11-15 11:41     ` Richard Biener

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).