public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] match: Simplify `a != C1 ? abs(a) : C2` when C2 == abs(C1) [PR111957]
@ 2023-10-25  3:37 Andrew Pinski
  2023-10-26  9:24 ` Richard Biener
  0 siblings, 1 reply; 4+ messages in thread
From: Andrew Pinski @ 2023-10-25  3:37 UTC (permalink / raw)
  To: gcc-patches; +Cc: Andrew Pinski

This adds a match pattern for `a != C1 ? abs(a) : C2` which gets simplified
to `abs(a)`. if C1 was originally *_MIN then change it over to use absu instead
of abs.

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

	PR tree-optimization/111957

gcc/ChangeLog:

	* match.pd (`a != C1 ? abs(a) : C2`): New pattern.

gcc/testsuite/ChangeLog:

	* gcc.dg/tree-ssa/phi-opt-40.c: New test.
---
 gcc/match.pd                               | 10 +++++++++
 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-40.c | 25 ++++++++++++++++++++++
 2 files changed, 35 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-40.c

diff --git a/gcc/match.pd b/gcc/match.pd
index 5df04ebba77..370ee35de52 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -5622,6 +5622,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
  (if (wi::eq_p (wi::bit_not (wi::to_wide (@1)), wi::to_wide (@2)))
   @3))
 
+/* X != C1 ? abs(X) : C2 simplifies to abs(x) when abs(C1) == C2. */
+(for op (abs absu)
+ (simplify
+  (cond (ne @0 INTEGER_CST@1) (op@3 @0) INTEGER_CST@2)
+  (if (wi::abs (wi::to_wide (@1)) == wi::to_wide (@2))
+   (if (op != ABSU_EXPR && wi::only_sign_bit_p (wi::to_wide (@1)))
+    (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
+     (convert (absu:utype @0)))
+    @3))))
+
 /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when
    X is unsigned, as when X + 1 overflows, X is -1, so -X == 1.  */
 (simplify
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-40.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-40.c
new file mode 100644
index 00000000000..a9011ce97fb
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-40.c
@@ -0,0 +1,25 @@
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-phiopt" } */
+/* PR tree-optimization/111957 */
+
+int f(int a)
+{
+  if (a)
+    return a > 0 ? a : -a;
+  return 0;
+}
+
+int f1(int x)
+{
+  int intmin = (-1u >> 1);
+  intmin = -intmin - 1;
+  if (x != intmin)
+    return x > 0 ? x : -x;
+  return intmin;
+}
+
+/* { dg-final { scan-tree-dump-times "if " 1 "phiopt1" } } */
+/* { dg-final { scan-tree-dump-not "if " "phiopt2" } } */
+/* { dg-final { scan-tree-dump-times "ABS_EXPR <" 2 "phiopt1" } } */
+/* { dg-final { scan-tree-dump-times "ABS_EXPR <" 1 "phiopt2" } } */
+/* { dg-final { scan-tree-dump-times "ABSU_EXPR <" 1 "phiopt2" } } */
-- 
2.34.1


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

* Re: [PATCH] match: Simplify `a != C1 ? abs(a) : C2` when C2 == abs(C1) [PR111957]
  2023-10-25  3:37 [PATCH] match: Simplify `a != C1 ? abs(a) : C2` when C2 == abs(C1) [PR111957] Andrew Pinski
@ 2023-10-26  9:24 ` Richard Biener
  2023-10-26 12:21   ` Andrew Pinski
  0 siblings, 1 reply; 4+ messages in thread
From: Richard Biener @ 2023-10-26  9:24 UTC (permalink / raw)
  To: Andrew Pinski; +Cc: gcc-patches

On Wed, Oct 25, 2023 at 5:37 AM Andrew Pinski <pinskia@gmail.com> wrote:
>
> This adds a match pattern for `a != C1 ? abs(a) : C2` which gets simplified
> to `abs(a)`. if C1 was originally *_MIN then change it over to use absu instead
> of abs.
>
> Bootstrapped and tested on x86_64-linux-gnu with no regressions.
>
>         PR tree-optimization/111957
>
> gcc/ChangeLog:
>
>         * match.pd (`a != C1 ? abs(a) : C2`): New pattern.
>
> gcc/testsuite/ChangeLog:
>
>         * gcc.dg/tree-ssa/phi-opt-40.c: New test.
> ---
>  gcc/match.pd                               | 10 +++++++++
>  gcc/testsuite/gcc.dg/tree-ssa/phi-opt-40.c | 25 ++++++++++++++++++++++
>  2 files changed, 35 insertions(+)
>  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-40.c
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 5df04ebba77..370ee35de52 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -5622,6 +5622,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>   (if (wi::eq_p (wi::bit_not (wi::to_wide (@1)), wi::to_wide (@2)))
>    @3))
>
> +/* X != C1 ? abs(X) : C2 simplifies to abs(x) when abs(C1) == C2. */
> +(for op (abs absu)
> + (simplify
> +  (cond (ne @0 INTEGER_CST@1) (op@3 @0) INTEGER_CST@2)
> +  (if (wi::abs (wi::to_wide (@1)) == wi::to_wide (@2))

Why not use

  (cond (ne @0 INTEGER_CST@1) (op@3 @0) @1)

?  OK with that change.

Richard.

> +   (if (op != ABSU_EXPR && wi::only_sign_bit_p (wi::to_wide (@1)))
> +    (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
> +     (convert (absu:utype @0)))
> +    @3))))
> +
>  /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when
>     X is unsigned, as when X + 1 overflows, X is -1, so -X == 1.  */
>  (simplify
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-40.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-40.c
> new file mode 100644
> index 00000000000..a9011ce97fb
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-40.c
> @@ -0,0 +1,25 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O1 -fdump-tree-phiopt" } */
> +/* PR tree-optimization/111957 */
> +
> +int f(int a)
> +{
> +  if (a)
> +    return a > 0 ? a : -a;
> +  return 0;
> +}
> +
> +int f1(int x)
> +{
> +  int intmin = (-1u >> 1);
> +  intmin = -intmin - 1;
> +  if (x != intmin)
> +    return x > 0 ? x : -x;
> +  return intmin;
> +}
> +
> +/* { dg-final { scan-tree-dump-times "if " 1 "phiopt1" } } */
> +/* { dg-final { scan-tree-dump-not "if " "phiopt2" } } */
> +/* { dg-final { scan-tree-dump-times "ABS_EXPR <" 2 "phiopt1" } } */
> +/* { dg-final { scan-tree-dump-times "ABS_EXPR <" 1 "phiopt2" } } */
> +/* { dg-final { scan-tree-dump-times "ABSU_EXPR <" 1 "phiopt2" } } */
> --
> 2.34.1
>

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

* Re: [PATCH] match: Simplify `a != C1 ? abs(a) : C2` when C2 == abs(C1) [PR111957]
  2023-10-26  9:24 ` Richard Biener
@ 2023-10-26 12:21   ` Andrew Pinski
  2023-10-26 13:53     ` Richard Biener
  0 siblings, 1 reply; 4+ messages in thread
From: Andrew Pinski @ 2023-10-26 12:21 UTC (permalink / raw)
  To: Richard Biener; +Cc: gcc-patches

On Thu, Oct 26, 2023 at 2:24 AM Richard Biener
<richard.guenther@gmail.com> wrote:
>
> On Wed, Oct 25, 2023 at 5:37 AM Andrew Pinski <pinskia@gmail.com> wrote:
> >
> > This adds a match pattern for `a != C1 ? abs(a) : C2` which gets simplified
> > to `abs(a)`. if C1 was originally *_MIN then change it over to use absu instead
> > of abs.
> >
> > Bootstrapped and tested on x86_64-linux-gnu with no regressions.
> >
> >         PR tree-optimization/111957
> >
> > gcc/ChangeLog:
> >
> >         * match.pd (`a != C1 ? abs(a) : C2`): New pattern.
> >
> > gcc/testsuite/ChangeLog:
> >
> >         * gcc.dg/tree-ssa/phi-opt-40.c: New test.
> > ---
> >  gcc/match.pd                               | 10 +++++++++
> >  gcc/testsuite/gcc.dg/tree-ssa/phi-opt-40.c | 25 ++++++++++++++++++++++
> >  2 files changed, 35 insertions(+)
> >  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-40.c
> >
> > diff --git a/gcc/match.pd b/gcc/match.pd
> > index 5df04ebba77..370ee35de52 100644
> > --- a/gcc/match.pd
> > +++ b/gcc/match.pd
> > @@ -5622,6 +5622,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
> >   (if (wi::eq_p (wi::bit_not (wi::to_wide (@1)), wi::to_wide (@2)))
> >    @3))
> >
> > +/* X != C1 ? abs(X) : C2 simplifies to abs(x) when abs(C1) == C2. */
> > +(for op (abs absu)
> > + (simplify
> > +  (cond (ne @0 INTEGER_CST@1) (op@3 @0) INTEGER_CST@2)
> > +  (if (wi::abs (wi::to_wide (@1)) == wi::to_wide (@2))
>
> Why not use
>
>   (cond (ne @0 INTEGER_CST@1) (op@3 @0) @1)

Because that does not work for:
`a != -1 ? abs(a) : -1`
We want to match -1 and 1 here (in that order).
It just happens 0 and INT_MIN have the same value as their abs and
matches but we could expand it to match all others too.

Thanks,
Andrew

>
> ?  OK with that change.
>
> Richard.
>
> > +   (if (op != ABSU_EXPR && wi::only_sign_bit_p (wi::to_wide (@1)))
> > +    (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
> > +     (convert (absu:utype @0)))
> > +    @3))))
> > +
> >  /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when
> >     X is unsigned, as when X + 1 overflows, X is -1, so -X == 1.  */
> >  (simplify
> > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-40.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-40.c
> > new file mode 100644
> > index 00000000000..a9011ce97fb
> > --- /dev/null
> > +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-40.c
> > @@ -0,0 +1,25 @@
> > +/* { dg-do compile } */
> > +/* { dg-options "-O1 -fdump-tree-phiopt" } */
> > +/* PR tree-optimization/111957 */
> > +
> > +int f(int a)
> > +{
> > +  if (a)
> > +    return a > 0 ? a : -a;
> > +  return 0;
> > +}
> > +
> > +int f1(int x)
> > +{
> > +  int intmin = (-1u >> 1);
> > +  intmin = -intmin - 1;
> > +  if (x != intmin)
> > +    return x > 0 ? x : -x;
> > +  return intmin;
> > +}
> > +
> > +/* { dg-final { scan-tree-dump-times "if " 1 "phiopt1" } } */
> > +/* { dg-final { scan-tree-dump-not "if " "phiopt2" } } */
> > +/* { dg-final { scan-tree-dump-times "ABS_EXPR <" 2 "phiopt1" } } */
> > +/* { dg-final { scan-tree-dump-times "ABS_EXPR <" 1 "phiopt2" } } */
> > +/* { dg-final { scan-tree-dump-times "ABSU_EXPR <" 1 "phiopt2" } } */
> > --
> > 2.34.1
> >

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

* Re: [PATCH] match: Simplify `a != C1 ? abs(a) : C2` when C2 == abs(C1) [PR111957]
  2023-10-26 12:21   ` Andrew Pinski
@ 2023-10-26 13:53     ` Richard Biener
  0 siblings, 0 replies; 4+ messages in thread
From: Richard Biener @ 2023-10-26 13:53 UTC (permalink / raw)
  To: Andrew Pinski; +Cc: gcc-patches



> Am 26.10.2023 um 14:21 schrieb Andrew Pinski <pinskia@gmail.com>:
> 
> On Thu, Oct 26, 2023 at 2:24 AM Richard Biener
> <richard.guenther@gmail.com> wrote:
>> 
>>> On Wed, Oct 25, 2023 at 5:37 AM Andrew Pinski <pinskia@gmail.com> wrote:
>>> 
>>> This adds a match pattern for `a != C1 ? abs(a) : C2` which gets simplified
>>> to `abs(a)`. if C1 was originally *_MIN then change it over to use absu instead
>>> of abs.
>>> 
>>> Bootstrapped and tested on x86_64-linux-gnu with no regressions.
>>> 
>>>        PR tree-optimization/111957
>>> 
>>> gcc/ChangeLog:
>>> 
>>>        * match.pd (`a != C1 ? abs(a) : C2`): New pattern.
>>> 
>>> gcc/testsuite/ChangeLog:
>>> 
>>>        * gcc.dg/tree-ssa/phi-opt-40.c: New test.
>>> ---
>>> gcc/match.pd                               | 10 +++++++++
>>> gcc/testsuite/gcc.dg/tree-ssa/phi-opt-40.c | 25 ++++++++++++++++++++++
>>> 2 files changed, 35 insertions(+)
>>> create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-40.c
>>> 
>>> diff --git a/gcc/match.pd b/gcc/match.pd
>>> index 5df04ebba77..370ee35de52 100644
>>> --- a/gcc/match.pd
>>> +++ b/gcc/match.pd
>>> @@ -5622,6 +5622,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>>>  (if (wi::eq_p (wi::bit_not (wi::to_wide (@1)), wi::to_wide (@2)))
>>>   @3))
>>> 
>>> +/* X != C1 ? abs(X) : C2 simplifies to abs(x) when abs(C1) == C2. */
>>> +(for op (abs absu)
>>> + (simplify
>>> +  (cond (ne @0 INTEGER_CST@1) (op@3 @0) INTEGER_CST@2)
>>> +  (if (wi::abs (wi::to_wide (@1)) == wi::to_wide (@2))
>> 
>> Why not use
>> 
>>  (cond (ne @0 INTEGER_CST@1) (op@3 @0) @1)
> 
> Because that does not work for:
> `a != -1 ? abs(a) : -1`
> We want to match -1 and 1 here (in that order).
> It just happens 0 and INT_MIN have the same value as their abs and
> matches but we could expand it to match all others too.

Ah, I missed the wi::not.  The original patch is OK.

Richard 

> Thanks,
> Andrew
> 
>> 
>> ?  OK with that change.
>> 
>> Richard.
>> 
>>> +   (if (op != ABSU_EXPR && wi::only_sign_bit_p (wi::to_wide (@1)))
>>> +    (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
>>> +     (convert (absu:utype @0)))
>>> +    @3))))
>>> +
>>> /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when
>>>    X is unsigned, as when X + 1 overflows, X is -1, so -X == 1.  */
>>> (simplify
>>> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-40.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-40.c
>>> new file mode 100644
>>> index 00000000000..a9011ce97fb
>>> --- /dev/null
>>> +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-40.c
>>> @@ -0,0 +1,25 @@
>>> +/* { dg-do compile } */
>>> +/* { dg-options "-O1 -fdump-tree-phiopt" } */
>>> +/* PR tree-optimization/111957 */
>>> +
>>> +int f(int a)
>>> +{
>>> +  if (a)
>>> +    return a > 0 ? a : -a;
>>> +  return 0;
>>> +}
>>> +
>>> +int f1(int x)
>>> +{
>>> +  int intmin = (-1u >> 1);
>>> +  intmin = -intmin - 1;
>>> +  if (x != intmin)
>>> +    return x > 0 ? x : -x;
>>> +  return intmin;
>>> +}
>>> +
>>> +/* { dg-final { scan-tree-dump-times "if " 1 "phiopt1" } } */
>>> +/* { dg-final { scan-tree-dump-not "if " "phiopt2" } } */
>>> +/* { dg-final { scan-tree-dump-times "ABS_EXPR <" 2 "phiopt1" } } */
>>> +/* { dg-final { scan-tree-dump-times "ABS_EXPR <" 1 "phiopt2" } } */
>>> +/* { dg-final { scan-tree-dump-times "ABSU_EXPR <" 1 "phiopt2" } } */
>>> --
>>> 2.34.1
>>> 

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

end of thread, other threads:[~2023-10-26 13:54 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-25  3:37 [PATCH] match: Simplify `a != C1 ? abs(a) : C2` when C2 == abs(C1) [PR111957] Andrew Pinski
2023-10-26  9:24 ` Richard Biener
2023-10-26 12:21   ` Andrew Pinski
2023-10-26 13:53     ` 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).