public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] MATCH: Improve zero_one_valued_p for cases without range information
@ 2023-09-15  1:08 Andrew Pinski
  2023-09-15  6:27 ` Richard Biener
  0 siblings, 1 reply; 3+ messages in thread
From: Andrew Pinski @ 2023-09-15  1:08 UTC (permalink / raw)
  To: gcc-patches; +Cc: Andrew Pinski

I noticed we sometimes lose range information in forwprop due to a few
match and simplify patterns optimizing away casts. So the easier way
to these cases is to add a match for zero_one_valued_p wich mathes
a cast from another zero_one_valued_p.
This also adds the case of `x & zero_one_valued_p` as being zero_one_valued_p
which allows catching more cases too.

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

gcc/ChangeLog:

	* match.pd (zero_one_valued_p): Match a cast from a zero_one_valued_p.
	Also match `a & zero_one_valued_p` too.

gcc/testsuite/ChangeLog:

	* gcc.dg/tree-ssa/bool-13.c: Update testcase as we now do
	the MIN/MAX during forwprop1.
---
 gcc/match.pd                            | 10 ++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/bool-13.c | 15 +++++----------
 2 files changed, 15 insertions(+), 10 deletions(-)

diff --git a/gcc/match.pd b/gcc/match.pd
index 97db0eb5f25..39c9c81966a 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -2181,6 +2181,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
       && (TYPE_UNSIGNED (type)
 	  || TYPE_PRECISION (type) > 1))))
 
+/* (a&1) is always [0,1] too. This is useful again when
+   the range is not known. */
+(match zero_one_valued_p
+ (bit_and:c@0 @1 zero_one_valued_p))
+
+/* A conversion from an zero_one_valued_p is still a [0,1].
+   This is useful when the range of a variable is not known */
+(match zero_one_valued_p
+ (convert@0 zero_one_valued_p))
+
 /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 }.  */
 (simplify
  (mult zero_one_valued_p@0 zero_one_valued_p@1)
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bool-13.c b/gcc/testsuite/gcc.dg/tree-ssa/bool-13.c
index 438f15a484a..de8c99a7727 100644
--- a/gcc/testsuite/gcc.dg/tree-ssa/bool-13.c
+++ b/gcc/testsuite/gcc.dg/tree-ssa/bool-13.c
@@ -1,5 +1,5 @@
 /* { dg-do compile } */
-/* { dg-options "-O1 -fdump-tree-optimized -fdump-tree-original -fdump-tree-phiopt1 -fdump-tree-forwprop2" } */
+/* { dg-options "-O1 -fdump-tree-optimized -fdump-tree-original -fdump-tree-forwprop1 -fdump-tree-forwprop2" } */
 #define bool _Bool
 int maxbool(bool ab, bool bb)
 {
@@ -22,15 +22,10 @@ int minbool(bool ab, bool bb)
 /* { dg-final { scan-tree-dump-times "MIN_EXPR" 1 "original" } } */
 /* { dg-final { scan-tree-dump-times "if " 0 "original" } } */
 
-/* PHI-OPT1 should have kept it as min/max. */
-/* { dg-final { scan-tree-dump-times "MAX_EXPR" 1 "phiopt1" } } */
-/* { dg-final { scan-tree-dump-times "MIN_EXPR" 1 "phiopt1" } } */
-/* { dg-final { scan-tree-dump-times "if " 0 "phiopt1" } } */
-
-/* Forwprop2 (after ccp) will convert it into &\| */
-/* { dg-final { scan-tree-dump-times "MAX_EXPR" 0 "forwprop2" } } */
-/* { dg-final { scan-tree-dump-times "MIN_EXPR" 0 "forwprop2" } } */
-/* { dg-final { scan-tree-dump-times "if " 0 "forwprop2" } } */
+/* Forwprop1 will convert it into &\| as we can detect that the arguments are one_zero. */
+/* { dg-final { scan-tree-dump-times "MAX_EXPR" 0 "forwprop1" } } */
+/* { dg-final { scan-tree-dump-times "MIN_EXPR" 0 "forwprop1" } } */
+/* { dg-final { scan-tree-dump-times "if " 0 "forwprop1" } } */
 
 /* By optimize there should be no min/max nor if  */
 /* { dg-final { scan-tree-dump-times "MAX_EXPR" 0 "optimized" } } */
-- 
2.31.1


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

* Re: [PATCH] MATCH: Improve zero_one_valued_p for cases without range information
  2023-09-15  1:08 [PATCH] MATCH: Improve zero_one_valued_p for cases without range information Andrew Pinski
@ 2023-09-15  6:27 ` Richard Biener
  2023-09-15 14:58   ` Andrew Pinski
  0 siblings, 1 reply; 3+ messages in thread
From: Richard Biener @ 2023-09-15  6:27 UTC (permalink / raw)
  To: Andrew Pinski; +Cc: gcc-patches

On Fri, Sep 15, 2023 at 3:09 AM Andrew Pinski via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> I noticed we sometimes lose range information in forwprop due to a few
> match and simplify patterns optimizing away casts. So the easier way
> to these cases is to add a match for zero_one_valued_p wich mathes
> a cast from another zero_one_valued_p.
> This also adds the case of `x & zero_one_valued_p` as being zero_one_valued_p
> which allows catching more cases too.
>
> OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.

OK.

I wonder if it would make a difference if we'd enable ranger unconditionally
in forwprop (maybe with -O2+), currently it gets enabled sometimes only.

Richard.

> gcc/ChangeLog:
>
>         * match.pd (zero_one_valued_p): Match a cast from a zero_one_valued_p.
>         Also match `a & zero_one_valued_p` too.
>
> gcc/testsuite/ChangeLog:
>
>         * gcc.dg/tree-ssa/bool-13.c: Update testcase as we now do
>         the MIN/MAX during forwprop1.
> ---
>  gcc/match.pd                            | 10 ++++++++++
>  gcc/testsuite/gcc.dg/tree-ssa/bool-13.c | 15 +++++----------
>  2 files changed, 15 insertions(+), 10 deletions(-)
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 97db0eb5f25..39c9c81966a 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -2181,6 +2181,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>        && (TYPE_UNSIGNED (type)
>           || TYPE_PRECISION (type) > 1))))
>
> +/* (a&1) is always [0,1] too. This is useful again when
> +   the range is not known. */
> +(match zero_one_valued_p
> + (bit_and:c@0 @1 zero_one_valued_p))
> +
> +/* A conversion from an zero_one_valued_p is still a [0,1].
> +   This is useful when the range of a variable is not known */
> +(match zero_one_valued_p
> + (convert@0 zero_one_valued_p))
> +
>  /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 }.  */
>  (simplify
>   (mult zero_one_valued_p@0 zero_one_valued_p@1)
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bool-13.c b/gcc/testsuite/gcc.dg/tree-ssa/bool-13.c
> index 438f15a484a..de8c99a7727 100644
> --- a/gcc/testsuite/gcc.dg/tree-ssa/bool-13.c
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/bool-13.c
> @@ -1,5 +1,5 @@
>  /* { dg-do compile } */
> -/* { dg-options "-O1 -fdump-tree-optimized -fdump-tree-original -fdump-tree-phiopt1 -fdump-tree-forwprop2" } */
> +/* { dg-options "-O1 -fdump-tree-optimized -fdump-tree-original -fdump-tree-forwprop1 -fdump-tree-forwprop2" } */
>  #define bool _Bool
>  int maxbool(bool ab, bool bb)
>  {
> @@ -22,15 +22,10 @@ int minbool(bool ab, bool bb)
>  /* { dg-final { scan-tree-dump-times "MIN_EXPR" 1 "original" } } */
>  /* { dg-final { scan-tree-dump-times "if " 0 "original" } } */
>
> -/* PHI-OPT1 should have kept it as min/max. */
> -/* { dg-final { scan-tree-dump-times "MAX_EXPR" 1 "phiopt1" } } */
> -/* { dg-final { scan-tree-dump-times "MIN_EXPR" 1 "phiopt1" } } */
> -/* { dg-final { scan-tree-dump-times "if " 0 "phiopt1" } } */
> -
> -/* Forwprop2 (after ccp) will convert it into &\| */
> -/* { dg-final { scan-tree-dump-times "MAX_EXPR" 0 "forwprop2" } } */
> -/* { dg-final { scan-tree-dump-times "MIN_EXPR" 0 "forwprop2" } } */
> -/* { dg-final { scan-tree-dump-times "if " 0 "forwprop2" } } */
> +/* Forwprop1 will convert it into &\| as we can detect that the arguments are one_zero. */
> +/* { dg-final { scan-tree-dump-times "MAX_EXPR" 0 "forwprop1" } } */
> +/* { dg-final { scan-tree-dump-times "MIN_EXPR" 0 "forwprop1" } } */
> +/* { dg-final { scan-tree-dump-times "if " 0 "forwprop1" } } */
>
>  /* By optimize there should be no min/max nor if  */
>  /* { dg-final { scan-tree-dump-times "MAX_EXPR" 0 "optimized" } } */
> --
> 2.31.1
>

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

* Re: [PATCH] MATCH: Improve zero_one_valued_p for cases without range information
  2023-09-15  6:27 ` Richard Biener
@ 2023-09-15 14:58   ` Andrew Pinski
  0 siblings, 0 replies; 3+ messages in thread
From: Andrew Pinski @ 2023-09-15 14:58 UTC (permalink / raw)
  To: Richard Biener; +Cc: Andrew Pinski, gcc-patches

On Thu, Sep 14, 2023 at 11:28 PM Richard Biener via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> On Fri, Sep 15, 2023 at 3:09 AM Andrew Pinski via Gcc-patches
> <gcc-patches@gcc.gnu.org> wrote:
> >
> > I noticed we sometimes lose range information in forwprop due to a few
> > match and simplify patterns optimizing away casts. So the easier way
> > to these cases is to add a match for zero_one_valued_p wich mathes
> > a cast from another zero_one_valued_p.
> > This also adds the case of `x & zero_one_valued_p` as being zero_one_valued_p
> > which allows catching more cases too.
> >
> > OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.
>
> OK.
>
> I wonder if it would make a difference if we'd enable ranger unconditionally
> in forwprop (maybe with -O2+), currently it gets enabled sometimes only.

I was thinking about that though currently zero_one_valued_p only uses
the global ranger info via tree_nonzero_bits but I have patches to use
the local one too.

Thanks,
Andrew

>
> Richard.
>
> > gcc/ChangeLog:
> >
> >         * match.pd (zero_one_valued_p): Match a cast from a zero_one_valued_p.
> >         Also match `a & zero_one_valued_p` too.
> >
> > gcc/testsuite/ChangeLog:
> >
> >         * gcc.dg/tree-ssa/bool-13.c: Update testcase as we now do
> >         the MIN/MAX during forwprop1.
> > ---
> >  gcc/match.pd                            | 10 ++++++++++
> >  gcc/testsuite/gcc.dg/tree-ssa/bool-13.c | 15 +++++----------
> >  2 files changed, 15 insertions(+), 10 deletions(-)
> >
> > diff --git a/gcc/match.pd b/gcc/match.pd
> > index 97db0eb5f25..39c9c81966a 100644
> > --- a/gcc/match.pd
> > +++ b/gcc/match.pd
> > @@ -2181,6 +2181,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
> >        && (TYPE_UNSIGNED (type)
> >           || TYPE_PRECISION (type) > 1))))
> >
> > +/* (a&1) is always [0,1] too. This is useful again when
> > +   the range is not known. */
> > +(match zero_one_valued_p
> > + (bit_and:c@0 @1 zero_one_valued_p))
> > +
> > +/* A conversion from an zero_one_valued_p is still a [0,1].
> > +   This is useful when the range of a variable is not known */
> > +(match zero_one_valued_p
> > + (convert@0 zero_one_valued_p))
> > +
> >  /* Transform { 0 or 1 } * { 0 or 1 } into { 0 or 1 } & { 0 or 1 }.  */
> >  (simplify
> >   (mult zero_one_valued_p@0 zero_one_valued_p@1)
> > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bool-13.c b/gcc/testsuite/gcc.dg/tree-ssa/bool-13.c
> > index 438f15a484a..de8c99a7727 100644
> > --- a/gcc/testsuite/gcc.dg/tree-ssa/bool-13.c
> > +++ b/gcc/testsuite/gcc.dg/tree-ssa/bool-13.c
> > @@ -1,5 +1,5 @@
> >  /* { dg-do compile } */
> > -/* { dg-options "-O1 -fdump-tree-optimized -fdump-tree-original -fdump-tree-phiopt1 -fdump-tree-forwprop2" } */
> > +/* { dg-options "-O1 -fdump-tree-optimized -fdump-tree-original -fdump-tree-forwprop1 -fdump-tree-forwprop2" } */
> >  #define bool _Bool
> >  int maxbool(bool ab, bool bb)
> >  {
> > @@ -22,15 +22,10 @@ int minbool(bool ab, bool bb)
> >  /* { dg-final { scan-tree-dump-times "MIN_EXPR" 1 "original" } } */
> >  /* { dg-final { scan-tree-dump-times "if " 0 "original" } } */
> >
> > -/* PHI-OPT1 should have kept it as min/max. */
> > -/* { dg-final { scan-tree-dump-times "MAX_EXPR" 1 "phiopt1" } } */
> > -/* { dg-final { scan-tree-dump-times "MIN_EXPR" 1 "phiopt1" } } */
> > -/* { dg-final { scan-tree-dump-times "if " 0 "phiopt1" } } */
> > -
> > -/* Forwprop2 (after ccp) will convert it into &\| */
> > -/* { dg-final { scan-tree-dump-times "MAX_EXPR" 0 "forwprop2" } } */
> > -/* { dg-final { scan-tree-dump-times "MIN_EXPR" 0 "forwprop2" } } */
> > -/* { dg-final { scan-tree-dump-times "if " 0 "forwprop2" } } */
> > +/* Forwprop1 will convert it into &\| as we can detect that the arguments are one_zero. */
> > +/* { dg-final { scan-tree-dump-times "MAX_EXPR" 0 "forwprop1" } } */
> > +/* { dg-final { scan-tree-dump-times "MIN_EXPR" 0 "forwprop1" } } */
> > +/* { dg-final { scan-tree-dump-times "if " 0 "forwprop1" } } */
> >
> >  /* By optimize there should be no min/max nor if  */
> >  /* { dg-final { scan-tree-dump-times "MAX_EXPR" 0 "optimized" } } */
> > --
> > 2.31.1
> >

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

end of thread, other threads:[~2023-09-15 14:58 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-15  1:08 [PATCH] MATCH: Improve zero_one_valued_p for cases without range information Andrew Pinski
2023-09-15  6:27 ` Richard Biener
2023-09-15 14:58   ` 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).