Re: [PATCH] MATCH: [PR110937/PR100798] (a ? ~b : b) should be optimized to b ^ -(a)

2023-08-11 Thread Christophe Lyon via Gcc-patches
On Thu, 10 Aug 2023 at 20:52, Andrew Pinski  wrote:

> On Thu, Aug 10, 2023 at 6:39 AM Christophe Lyon via Gcc-patches
>  wrote:
> >
> > Hi Andrew,
> >
> >
> > On Wed, 9 Aug 2023 at 21:20, Andrew Pinski via Gcc-patches <
> > gcc-patches@gcc.gnu.org> wrote:
> >
> > > This adds a simple match pattern for this case.
> > > I noticed it a couple of different places.
> > > One while I was looking at code generation of a parser and
> > > also while I was looking at locations where bitwise_inverted_equal_p
> > > should be used more.
> > >
> > > Committed as approved after bootstrapped and tested on x86_64-linux-gnu
> > > with no regressions.
> > >
> > > PR tree-optimization/110937
> > > PR tree-optimization/100798
> > >
> > > gcc/ChangeLog:
> > >
> > > * match.pd (`a ? ~b : b`): Handle this
> > > case.
> > >
> > > gcc/testsuite/ChangeLog:
> > >
> > > * gcc.dg/tree-ssa/bool-14.c: New test.
> > > * gcc.dg/tree-ssa/bool-15.c: New test.
> > > * gcc.dg/tree-ssa/phi-opt-33.c: New test.
> > > * gcc.dg/tree-ssa/20030709-2.c: Update testcase
> > > so `a ? -1 : 0` is not used to hit the match
> > > pattern.
> > >
> >
> > Our CI noticed that your patch introduced regressions as follows on
> aarch64:
> >
> >  Running gcc:gcc.target/aarch64/aarch64.exp ...
> > FAIL: gcc.target/aarch64/cond_op_imm_1.c scan-assembler csinv\tw[0-9]*.*
> > FAIL: gcc.target/aarch64/cond_op_imm_1.c scan-assembler csinv\tx[0-9]*.*
> >
> > Running gcc:gcc.target/aarch64/sve/aarch64-sve.exp ...
> > FAIL: gcc.target/aarch64/sve/cond_unary_5.c scan-assembler-not \\tmov\\tz
> > FAIL: gcc.target/aarch64/sve/cond_unary_5.c scan-assembler-times
> > \\tneg\\tz[0-9]+\\.b, p[0-7]/m, 3
> > FAIL: gcc.target/aarch64/sve/cond_unary_5.c scan-assembler-times
> > \\tneg\\tz[0-9]+\\.h, p[0-7]/m, 2
> > FAIL: gcc.target/aarch64/sve/cond_unary_5.c scan-assembler-times
> > \\tneg\\tz[0-9]+\\.s, p[0-7]/m, 1
> > FAIL: gcc.target/aarch64/sve/cond_unary_5.c scan-assembler-times
> > \\tnot\\tz[0-9]+\\.b, p[0-7]/m, 3
> > FAIL: gcc.target/aarch64/sve/cond_unary_5.c scan-assembler-times
> > \\tnot\\tz[0-9]+\\.h, p[0-7]/m, 2
> > FAIL: gcc.target/aarch64/sve/cond_unary_5.c scan-assembler-times
> > \\tnot\\tz[0-9]+\\.s, p[0-7]/m, 1
> >
> > Hopefully you'll just need to update the testcases (I didn't check
> > manually, I think you can easily reproduce this on aarch64?)
>
> I have a few ideas of how to fix this properly inside isel without
> changing the testcases. I will start working on that starting
> tomorrow.
> In the meantime can you file a bug report? So we don't lose track of
> the regression?
>
> Hi Andrew,

Sure, I've just filed:  https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110986

Thanks,

Christophe

Thanks,
> Andrew
>
> >
> > Thanks,
> >
> > Christophe
> >
> >
> >
> >
> > > ---
> > >  gcc/match.pd   | 14 ++
> > >  gcc/testsuite/gcc.dg/tree-ssa/20030709-2.c |  5 +++--
> > >  gcc/testsuite/gcc.dg/tree-ssa/bool-14.c| 15 +++
> > >  gcc/testsuite/gcc.dg/tree-ssa/bool-15.c| 18 ++
> > >  gcc/testsuite/gcc.dg/tree-ssa/phi-opt-33.c | 13 +
> > >  5 files changed, 63 insertions(+), 2 deletions(-)
> > >  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bool-14.c
> > >  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bool-15.c
> > >  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-33.c
> > >
> > > diff --git a/gcc/match.pd b/gcc/match.pd
> > > index 9b4819e5be7..fc630b63563 100644
> > > --- a/gcc/match.pd
> > > +++ b/gcc/match.pd
> > > @@ -6460,6 +6460,20 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
> > >(if (cmp == NE_EXPR)
> > > { constant_boolean_node (true, type); })))
> > >
> > > +#if GIMPLE
> > > +/* a?~t:t -> (-(a))^t */
> > > +(simplify
> > > + (cond @0 @1 @2)
> > > + (if (INTEGRAL_TYPE_P (type)
> > > +  && bitwise_inverted_equal_p (@1, @2))
> > > +  (with {
> > > +auto prec = TYPE_PRECISION (type);
> > > +auto unsign = TYPE_UNSIGNED (type);
> > > +tree inttype = build_nonstandard_integer_type (prec, unsign);
> > > +   }
> > > +   (convert (bit_xor (negate (convert:inttype @0)) (convert:inttype
> > > @2))
> > > +#endif
> > > +
> > >  /* Simplify pointer equality compares using PTA.  */
> > >  (for neeq (ne eq)
> > >   (simplify
> > > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/20030709-2.c
> > > b/gcc/testsuite/gcc.dg/tree-ssa/20030709-2.c
> > > index 5009cd69cfe..78938f919d4 100644
> > > --- a/gcc/testsuite/gcc.dg/tree-ssa/20030709-2.c
> > > +++ b/gcc/testsuite/gcc.dg/tree-ssa/20030709-2.c
> > > @@ -29,15 +29,16 @@ union tree_node
> > >  };
> > >  int make_decl_rtl (tree, int);
> > >  void *
> > > -get_alias_set (t)
> > > +get_alias_set (t, t1)
> > >   tree t;
> > > + void *t1;
> > >  {
> > >long set;
> > >if (t->decl.rtl)
> > >  return (t->decl.rtl->fld[1].rtmem
> > > ? 0
> > > : (((t->decl.rtl ? t->decl.rtl: 

Re: [PATCH] MATCH: [PR110937/PR100798] (a ? ~b : b) should be optimized to b ^ -(a)

2023-08-10 Thread Andrew Pinski via Gcc-patches
On Thu, Aug 10, 2023 at 6:39 AM Christophe Lyon via Gcc-patches
 wrote:
>
> Hi Andrew,
>
>
> On Wed, 9 Aug 2023 at 21:20, Andrew Pinski via Gcc-patches <
> gcc-patches@gcc.gnu.org> wrote:
>
> > This adds a simple match pattern for this case.
> > I noticed it a couple of different places.
> > One while I was looking at code generation of a parser and
> > also while I was looking at locations where bitwise_inverted_equal_p
> > should be used more.
> >
> > Committed as approved after bootstrapped and tested on x86_64-linux-gnu
> > with no regressions.
> >
> > PR tree-optimization/110937
> > PR tree-optimization/100798
> >
> > gcc/ChangeLog:
> >
> > * match.pd (`a ? ~b : b`): Handle this
> > case.
> >
> > gcc/testsuite/ChangeLog:
> >
> > * gcc.dg/tree-ssa/bool-14.c: New test.
> > * gcc.dg/tree-ssa/bool-15.c: New test.
> > * gcc.dg/tree-ssa/phi-opt-33.c: New test.
> > * gcc.dg/tree-ssa/20030709-2.c: Update testcase
> > so `a ? -1 : 0` is not used to hit the match
> > pattern.
> >
>
> Our CI noticed that your patch introduced regressions as follows on aarch64:
>
>  Running gcc:gcc.target/aarch64/aarch64.exp ...
> FAIL: gcc.target/aarch64/cond_op_imm_1.c scan-assembler csinv\tw[0-9]*.*
> FAIL: gcc.target/aarch64/cond_op_imm_1.c scan-assembler csinv\tx[0-9]*.*
>
> Running gcc:gcc.target/aarch64/sve/aarch64-sve.exp ...
> FAIL: gcc.target/aarch64/sve/cond_unary_5.c scan-assembler-not \\tmov\\tz
> FAIL: gcc.target/aarch64/sve/cond_unary_5.c scan-assembler-times
> \\tneg\\tz[0-9]+\\.b, p[0-7]/m, 3
> FAIL: gcc.target/aarch64/sve/cond_unary_5.c scan-assembler-times
> \\tneg\\tz[0-9]+\\.h, p[0-7]/m, 2
> FAIL: gcc.target/aarch64/sve/cond_unary_5.c scan-assembler-times
> \\tneg\\tz[0-9]+\\.s, p[0-7]/m, 1
> FAIL: gcc.target/aarch64/sve/cond_unary_5.c scan-assembler-times
> \\tnot\\tz[0-9]+\\.b, p[0-7]/m, 3
> FAIL: gcc.target/aarch64/sve/cond_unary_5.c scan-assembler-times
> \\tnot\\tz[0-9]+\\.h, p[0-7]/m, 2
> FAIL: gcc.target/aarch64/sve/cond_unary_5.c scan-assembler-times
> \\tnot\\tz[0-9]+\\.s, p[0-7]/m, 1
>
> Hopefully you'll just need to update the testcases (I didn't check
> manually, I think you can easily reproduce this on aarch64?)

I have a few ideas of how to fix this properly inside isel without
changing the testcases. I will start working on that starting
tomorrow.
In the meantime can you file a bug report? So we don't lose track of
the regression?

Thanks,
Andrew

>
> Thanks,
>
> Christophe
>
>
>
>
> > ---
> >  gcc/match.pd   | 14 ++
> >  gcc/testsuite/gcc.dg/tree-ssa/20030709-2.c |  5 +++--
> >  gcc/testsuite/gcc.dg/tree-ssa/bool-14.c| 15 +++
> >  gcc/testsuite/gcc.dg/tree-ssa/bool-15.c| 18 ++
> >  gcc/testsuite/gcc.dg/tree-ssa/phi-opt-33.c | 13 +
> >  5 files changed, 63 insertions(+), 2 deletions(-)
> >  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bool-14.c
> >  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bool-15.c
> >  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-33.c
> >
> > diff --git a/gcc/match.pd b/gcc/match.pd
> > index 9b4819e5be7..fc630b63563 100644
> > --- a/gcc/match.pd
> > +++ b/gcc/match.pd
> > @@ -6460,6 +6460,20 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
> >(if (cmp == NE_EXPR)
> > { constant_boolean_node (true, type); })))
> >
> > +#if GIMPLE
> > +/* a?~t:t -> (-(a))^t */
> > +(simplify
> > + (cond @0 @1 @2)
> > + (if (INTEGRAL_TYPE_P (type)
> > +  && bitwise_inverted_equal_p (@1, @2))
> > +  (with {
> > +auto prec = TYPE_PRECISION (type);
> > +auto unsign = TYPE_UNSIGNED (type);
> > +tree inttype = build_nonstandard_integer_type (prec, unsign);
> > +   }
> > +   (convert (bit_xor (negate (convert:inttype @0)) (convert:inttype
> > @2))
> > +#endif
> > +
> >  /* Simplify pointer equality compares using PTA.  */
> >  (for neeq (ne eq)
> >   (simplify
> > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/20030709-2.c
> > b/gcc/testsuite/gcc.dg/tree-ssa/20030709-2.c
> > index 5009cd69cfe..78938f919d4 100644
> > --- a/gcc/testsuite/gcc.dg/tree-ssa/20030709-2.c
> > +++ b/gcc/testsuite/gcc.dg/tree-ssa/20030709-2.c
> > @@ -29,15 +29,16 @@ union tree_node
> >  };
> >  int make_decl_rtl (tree, int);
> >  void *
> > -get_alias_set (t)
> > +get_alias_set (t, t1)
> >   tree t;
> > + void *t1;
> >  {
> >long set;
> >if (t->decl.rtl)
> >  return (t->decl.rtl->fld[1].rtmem
> > ? 0
> > : (((t->decl.rtl ? t->decl.rtl: (make_decl_rtl (t, 0),
> > t->decl.rtl)))->fld[1]).rtmem);
> > -  return (void*)-1;
> > +  return t1;
> >  }
> >
> >  /* There should be precisely one load of ->decl.rtl.  If there is
> > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bool-14.c
> > b/gcc/testsuite/gcc.dg/tree-ssa/bool-14.c
> > new file mode 100644
> > index 000..0149380a63b
> > --- /dev/null
> > +++ b/gcc/testsuite/gcc.dg/tree-ssa/bool-14.c
> > @@ -0,0 +1,15 @@
> > 

Re: [PATCH] MATCH: [PR110937/PR100798] (a ? ~b : b) should be optimized to b ^ -(a)

2023-08-10 Thread Christophe Lyon via Gcc-patches
Hi Andrew,


On Wed, 9 Aug 2023 at 21:20, Andrew Pinski via Gcc-patches <
gcc-patches@gcc.gnu.org> wrote:

> This adds a simple match pattern for this case.
> I noticed it a couple of different places.
> One while I was looking at code generation of a parser and
> also while I was looking at locations where bitwise_inverted_equal_p
> should be used more.
>
> Committed as approved after bootstrapped and tested on x86_64-linux-gnu
> with no regressions.
>
> PR tree-optimization/110937
> PR tree-optimization/100798
>
> gcc/ChangeLog:
>
> * match.pd (`a ? ~b : b`): Handle this
> case.
>
> gcc/testsuite/ChangeLog:
>
> * gcc.dg/tree-ssa/bool-14.c: New test.
> * gcc.dg/tree-ssa/bool-15.c: New test.
> * gcc.dg/tree-ssa/phi-opt-33.c: New test.
> * gcc.dg/tree-ssa/20030709-2.c: Update testcase
> so `a ? -1 : 0` is not used to hit the match
> pattern.
>

Our CI noticed that your patch introduced regressions as follows on aarch64:

 Running gcc:gcc.target/aarch64/aarch64.exp ...
FAIL: gcc.target/aarch64/cond_op_imm_1.c scan-assembler csinv\tw[0-9]*.*
FAIL: gcc.target/aarch64/cond_op_imm_1.c scan-assembler csinv\tx[0-9]*.*

Running gcc:gcc.target/aarch64/sve/aarch64-sve.exp ...
FAIL: gcc.target/aarch64/sve/cond_unary_5.c scan-assembler-not \\tmov\\tz
FAIL: gcc.target/aarch64/sve/cond_unary_5.c scan-assembler-times
\\tneg\\tz[0-9]+\\.b, p[0-7]/m, 3
FAIL: gcc.target/aarch64/sve/cond_unary_5.c scan-assembler-times
\\tneg\\tz[0-9]+\\.h, p[0-7]/m, 2
FAIL: gcc.target/aarch64/sve/cond_unary_5.c scan-assembler-times
\\tneg\\tz[0-9]+\\.s, p[0-7]/m, 1
FAIL: gcc.target/aarch64/sve/cond_unary_5.c scan-assembler-times
\\tnot\\tz[0-9]+\\.b, p[0-7]/m, 3
FAIL: gcc.target/aarch64/sve/cond_unary_5.c scan-assembler-times
\\tnot\\tz[0-9]+\\.h, p[0-7]/m, 2
FAIL: gcc.target/aarch64/sve/cond_unary_5.c scan-assembler-times
\\tnot\\tz[0-9]+\\.s, p[0-7]/m, 1

Hopefully you'll just need to update the testcases (I didn't check
manually, I think you can easily reproduce this on aarch64?)

Thanks,

Christophe




> ---
>  gcc/match.pd   | 14 ++
>  gcc/testsuite/gcc.dg/tree-ssa/20030709-2.c |  5 +++--
>  gcc/testsuite/gcc.dg/tree-ssa/bool-14.c| 15 +++
>  gcc/testsuite/gcc.dg/tree-ssa/bool-15.c| 18 ++
>  gcc/testsuite/gcc.dg/tree-ssa/phi-opt-33.c | 13 +
>  5 files changed, 63 insertions(+), 2 deletions(-)
>  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bool-14.c
>  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bool-15.c
>  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-33.c
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 9b4819e5be7..fc630b63563 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -6460,6 +6460,20 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>(if (cmp == NE_EXPR)
> { constant_boolean_node (true, type); })))
>
> +#if GIMPLE
> +/* a?~t:t -> (-(a))^t */
> +(simplify
> + (cond @0 @1 @2)
> + (if (INTEGRAL_TYPE_P (type)
> +  && bitwise_inverted_equal_p (@1, @2))
> +  (with {
> +auto prec = TYPE_PRECISION (type);
> +auto unsign = TYPE_UNSIGNED (type);
> +tree inttype = build_nonstandard_integer_type (prec, unsign);
> +   }
> +   (convert (bit_xor (negate (convert:inttype @0)) (convert:inttype
> @2))
> +#endif
> +
>  /* Simplify pointer equality compares using PTA.  */
>  (for neeq (ne eq)
>   (simplify
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/20030709-2.c
> b/gcc/testsuite/gcc.dg/tree-ssa/20030709-2.c
> index 5009cd69cfe..78938f919d4 100644
> --- a/gcc/testsuite/gcc.dg/tree-ssa/20030709-2.c
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/20030709-2.c
> @@ -29,15 +29,16 @@ union tree_node
>  };
>  int make_decl_rtl (tree, int);
>  void *
> -get_alias_set (t)
> +get_alias_set (t, t1)
>   tree t;
> + void *t1;
>  {
>long set;
>if (t->decl.rtl)
>  return (t->decl.rtl->fld[1].rtmem
> ? 0
> : (((t->decl.rtl ? t->decl.rtl: (make_decl_rtl (t, 0),
> t->decl.rtl)))->fld[1]).rtmem);
> -  return (void*)-1;
> +  return t1;
>  }
>
>  /* There should be precisely one load of ->decl.rtl.  If there is
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bool-14.c
> b/gcc/testsuite/gcc.dg/tree-ssa/bool-14.c
> new file mode 100644
> index 000..0149380a63b
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/bool-14.c
> @@ -0,0 +1,15 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -fdump-tree-optimized-raw" } */
> +/* PR tree-optimization/110937 */
> +
> +_Bool f2(_Bool a, _Bool b)
> +{
> +if (a)
> +  return !b;
> +return b;
> +}
> +
> +/* We should be able to remove the conditional and convert it to an xor.
> */
> +/* { dg-final { scan-tree-dump-not "gimple_cond " "optimized" } } */
> +/* { dg-final { scan-tree-dump-not "gimple_phi " "optimized" } } */
> +/* { dg-final { scan-tree-dump-times "bit_xor_expr, " 1 "optimized" } } */
> diff --git 

[PATCH] MATCH: [PR110937/PR100798] (a ? ~b : b) should be optimized to b ^ -(a)

2023-08-09 Thread Andrew Pinski via Gcc-patches
This adds a simple match pattern for this case.
I noticed it a couple of different places.
One while I was looking at code generation of a parser and
also while I was looking at locations where bitwise_inverted_equal_p
should be used more.

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

PR tree-optimization/110937
PR tree-optimization/100798

gcc/ChangeLog:

* match.pd (`a ? ~b : b`): Handle this
case.

gcc/testsuite/ChangeLog:

* gcc.dg/tree-ssa/bool-14.c: New test.
* gcc.dg/tree-ssa/bool-15.c: New test.
* gcc.dg/tree-ssa/phi-opt-33.c: New test.
* gcc.dg/tree-ssa/20030709-2.c: Update testcase
so `a ? -1 : 0` is not used to hit the match
pattern.
---
 gcc/match.pd   | 14 ++
 gcc/testsuite/gcc.dg/tree-ssa/20030709-2.c |  5 +++--
 gcc/testsuite/gcc.dg/tree-ssa/bool-14.c| 15 +++
 gcc/testsuite/gcc.dg/tree-ssa/bool-15.c| 18 ++
 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-33.c | 13 +
 5 files changed, 63 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bool-14.c
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bool-15.c
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-33.c

diff --git a/gcc/match.pd b/gcc/match.pd
index 9b4819e5be7..fc630b63563 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -6460,6 +6460,20 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
   (if (cmp == NE_EXPR)
{ constant_boolean_node (true, type); })))
 
+#if GIMPLE
+/* a?~t:t -> (-(a))^t */
+(simplify
+ (cond @0 @1 @2)
+ (if (INTEGRAL_TYPE_P (type)
+  && bitwise_inverted_equal_p (@1, @2))
+  (with {
+auto prec = TYPE_PRECISION (type);
+auto unsign = TYPE_UNSIGNED (type);
+tree inttype = build_nonstandard_integer_type (prec, unsign);
+   }
+   (convert (bit_xor (negate (convert:inttype @0)) (convert:inttype @2))
+#endif
+
 /* Simplify pointer equality compares using PTA.  */
 (for neeq (ne eq)
  (simplify
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/20030709-2.c 
b/gcc/testsuite/gcc.dg/tree-ssa/20030709-2.c
index 5009cd69cfe..78938f919d4 100644
--- a/gcc/testsuite/gcc.dg/tree-ssa/20030709-2.c
+++ b/gcc/testsuite/gcc.dg/tree-ssa/20030709-2.c
@@ -29,15 +29,16 @@ union tree_node
 };
 int make_decl_rtl (tree, int);
 void *
-get_alias_set (t)
+get_alias_set (t, t1)
  tree t;
+ void *t1;
 {
   long set;
   if (t->decl.rtl)
 return (t->decl.rtl->fld[1].rtmem 
? 0
: (((t->decl.rtl ? t->decl.rtl: (make_decl_rtl (t, 0), 
t->decl.rtl)))->fld[1]).rtmem);
-  return (void*)-1;
+  return t1;
 }
 
 /* There should be precisely one load of ->decl.rtl.  If there is
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bool-14.c 
b/gcc/testsuite/gcc.dg/tree-ssa/bool-14.c
new file mode 100644
index 000..0149380a63b
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/bool-14.c
@@ -0,0 +1,15 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized-raw" } */
+/* PR tree-optimization/110937 */
+
+_Bool f2(_Bool a, _Bool b)
+{
+if (a)
+  return !b;
+return b;
+}
+
+/* We should be able to remove the conditional and convert it to an xor. */
+/* { dg-final { scan-tree-dump-not "gimple_cond " "optimized" } } */
+/* { dg-final { scan-tree-dump-not "gimple_phi " "optimized" } } */
+/* { dg-final { scan-tree-dump-times "bit_xor_expr, " 1 "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bool-15.c 
b/gcc/testsuite/gcc.dg/tree-ssa/bool-15.c
new file mode 100644
index 000..1f496663863
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/bool-15.c
@@ -0,0 +1,18 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized-raw" } */
+/* PR tree-optimization/110937 */
+
+_Bool f2(int x, int y, int w, int z)
+{
+  _Bool a = x == y;
+  _Bool b = w == z;
+  if (a)
+return !b;
+  return b;
+}
+
+/* We should be able to remove the conditional and convert it to an xor. */
+/* { dg-final { scan-tree-dump-not "gimple_cond " "optimized" } } */
+/* { dg-final { scan-tree-dump-not "gimple_phi " "optimized" } } */
+/* { dg-final { scan-tree-dump-not "ne_expr, " "optimized" } } */
+/* { dg-final { scan-tree-dump-times "bit_xor_expr, " 1 "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-33.c 
b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-33.c
new file mode 100644
index 000..b79fe44187a
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-33.c
@@ -0,0 +1,13 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized-raw" } */
+/* PR tree-optimization/100798 */
+
+int f(int a, int t)
+{
+  return (a=='s' ? ~t : t);
+}
+
+/* This should be convert into t^-(a=='s').  */
+/* { dg-final { scan-tree-dump-times "bit_xor_expr, " 1 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "negate_expr, " 1 "optimized" } } */
+/* { dg-final { scan-tree-dump-not "bit_not_expr, " "optimized" } } 

Re: [PATCH] MATCH: [PR110937/PR100798] (a ? ~b : b) should be optimized to b ^ -(a)

2023-08-08 Thread Andrew Pinski via Gcc-patches
On Tue, Aug 8, 2023 at 12:44 AM Richard Biener via Gcc-patches
 wrote:
>
> On Tue, Aug 8, 2023 at 2:55 AM Andrew Pinski via Gcc-patches
>  wrote:
> >
> > This adds a simple match pattern for this case.
> > I noticed it a couple of different places.
> > One while I was looking at code generation of a parser and
> > also while I was looking at locations where bitwise_inverted_equal_p
> > should be used more.
> >
> > OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.
> >
> > PR tree-optimization/110937
> > PR tree-optimization/100798
> >
> > gcc/ChangeLog:
> >
> > * match.pd (`a ? ~b : b`): Handle this
> > case.
> >
> > gcc/testsuite/ChangeLog:
> >
> > * gcc.dg/tree-ssa/bool-14.c: New test.
> > * gcc.dg/tree-ssa/bool-15.c: New test.
> > * gcc.dg/tree-ssa/phi-opt-33.c: New test.
> > * gcc.dg/tree-ssa/20030709-2.c: Update testcase
> > so `a ? -1 : 0` is not used to hit the match
> > pattern.
> > ---
> >  gcc/match.pd   | 13 +
> >  gcc/testsuite/gcc.dg/tree-ssa/20030709-2.c |  5 +++--
> >  gcc/testsuite/gcc.dg/tree-ssa/bool-14.c| 15 +++
> >  gcc/testsuite/gcc.dg/tree-ssa/bool-15.c| 18 ++
> >  gcc/testsuite/gcc.dg/tree-ssa/phi-opt-33.c | 13 +
> >  5 files changed, 62 insertions(+), 2 deletions(-)
> >  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bool-14.c
> >  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bool-15.c
> >  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-33.c
> >
> > diff --git a/gcc/match.pd b/gcc/match.pd
> > index 9b4819e5be7..f887c517c81 100644
> > --- a/gcc/match.pd
> > +++ b/gcc/match.pd
> > @@ -6460,6 +6460,19 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
> >(if (cmp == NE_EXPR)
> > { constant_boolean_node (true, type); })))
> >
> > +#if GIMPLE
> > +/* a?~t:t -> (-(a))^t */
> > +(simplify
> > + (cond @0 @1 @2)
> > + (if (bitwise_inverted_equal_p (@1, @2))
>
> I'm not sure if that can ever match a not INTEGRAL_TYPE_P
> but we can have vector typed @1 and @2 and then the
> TYPE_PRECISION ask below would be wrong.  So can you
> add
>
>   INTEGRAL_TYPE_P (type)
>   && bitwise_in...
>
> if only for clarity?
>
> > +  (with {
> > +auto prec = TYPE_PRECISION (type);
> > +auto unsign = TYPE_UNSIGNED (type);
> > +tree inttype = build_nonstandard_integer_type (prec, unsign);
> > +   }
> > +   (convert (bit_xor (negate (convert:inttype @0)) (convert:inttype 
> > @2))
>
> so we don't get to know which of @1 or @2 is "simpler" (the not
> explicitely inverted
> operand), I suppose that's the disadvantage of using bitwise_inverted_equal_p.
> I'll note that if you make bitwise_inverted_equal_p a match you'd need a :c on
> the 'cond' but otherwise complexity would be the same as match patterns are 
> not
> "inlined".

Right, The disadvantage is definitely not knowing which is "simpler".
And I found a testcase which shows that but I suspect we can fix that.
```
int f(int a, int t)
{
  int t1 = ~t;
 return (a=='s' ? t : t1);
}
```
Basically we are missing transforming:
~(-(cast)(cmp)) into -(cast)(cmp`)
Filed as PR 110949 .

>
> In any case, OK with the INTEGRAL_TYPE_P check.
Will update the patch and commit it after a bootstrap/test.

Thanks,
Andrew

>
> Thanks,
> Richard.
>
> > +#endif
> > +
> >  /* Simplify pointer equality compares using PTA.  */
> >  (for neeq (ne eq)
> >   (simplify
> > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/20030709-2.c 
> > b/gcc/testsuite/gcc.dg/tree-ssa/20030709-2.c
> > index 5009cd69cfe..78938f919d4 100644
> > --- a/gcc/testsuite/gcc.dg/tree-ssa/20030709-2.c
> > +++ b/gcc/testsuite/gcc.dg/tree-ssa/20030709-2.c
> > @@ -29,15 +29,16 @@ union tree_node
> >  };
> >  int make_decl_rtl (tree, int);
> >  void *
> > -get_alias_set (t)
> > +get_alias_set (t, t1)
> >   tree t;
> > + void *t1;
> >  {
> >long set;
> >if (t->decl.rtl)
> >  return (t->decl.rtl->fld[1].rtmem
> > ? 0
> > : (((t->decl.rtl ? t->decl.rtl: (make_decl_rtl (t, 0), 
> > t->decl.rtl)))->fld[1]).rtmem);
> > -  return (void*)-1;
> > +  return t1;
> >  }
> >
> >  /* There should be precisely one load of ->decl.rtl.  If there is
> > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bool-14.c 
> > b/gcc/testsuite/gcc.dg/tree-ssa/bool-14.c
> > new file mode 100644
> > index 000..0149380a63b
> > --- /dev/null
> > +++ b/gcc/testsuite/gcc.dg/tree-ssa/bool-14.c
> > @@ -0,0 +1,15 @@
> > +/* { dg-do compile } */
> > +/* { dg-options "-O2 -fdump-tree-optimized-raw" } */
> > +/* PR tree-optimization/110937 */
> > +
> > +_Bool f2(_Bool a, _Bool b)
> > +{
> > +if (a)
> > +  return !b;
> > +return b;
> > +}
> > +
> > +/* We should be able to remove the conditional and convert it to an xor. */
> > +/* { dg-final { scan-tree-dump-not "gimple_cond " "optimized" } } */
> > +/* { dg-final { scan-tree-dump-not "gimple_phi " "optimized" } } 

Re: [PATCH] MATCH: [PR110937/PR100798] (a ? ~b : b) should be optimized to b ^ -(a)

2023-08-08 Thread Richard Biener via Gcc-patches
On Tue, Aug 8, 2023 at 2:55 AM Andrew Pinski via Gcc-patches
 wrote:
>
> This adds a simple match pattern for this case.
> I noticed it a couple of different places.
> One while I was looking at code generation of a parser and
> also while I was looking at locations where bitwise_inverted_equal_p
> should be used more.
>
> OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.
>
> PR tree-optimization/110937
> PR tree-optimization/100798
>
> gcc/ChangeLog:
>
> * match.pd (`a ? ~b : b`): Handle this
> case.
>
> gcc/testsuite/ChangeLog:
>
> * gcc.dg/tree-ssa/bool-14.c: New test.
> * gcc.dg/tree-ssa/bool-15.c: New test.
> * gcc.dg/tree-ssa/phi-opt-33.c: New test.
> * gcc.dg/tree-ssa/20030709-2.c: Update testcase
> so `a ? -1 : 0` is not used to hit the match
> pattern.
> ---
>  gcc/match.pd   | 13 +
>  gcc/testsuite/gcc.dg/tree-ssa/20030709-2.c |  5 +++--
>  gcc/testsuite/gcc.dg/tree-ssa/bool-14.c| 15 +++
>  gcc/testsuite/gcc.dg/tree-ssa/bool-15.c| 18 ++
>  gcc/testsuite/gcc.dg/tree-ssa/phi-opt-33.c | 13 +
>  5 files changed, 62 insertions(+), 2 deletions(-)
>  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bool-14.c
>  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bool-15.c
>  create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-33.c
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 9b4819e5be7..f887c517c81 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -6460,6 +6460,19 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>(if (cmp == NE_EXPR)
> { constant_boolean_node (true, type); })))
>
> +#if GIMPLE
> +/* a?~t:t -> (-(a))^t */
> +(simplify
> + (cond @0 @1 @2)
> + (if (bitwise_inverted_equal_p (@1, @2))

I'm not sure if that can ever match a not INTEGRAL_TYPE_P
but we can have vector typed @1 and @2 and then the
TYPE_PRECISION ask below would be wrong.  So can you
add

  INTEGRAL_TYPE_P (type)
  && bitwise_in...

if only for clarity?

> +  (with {
> +auto prec = TYPE_PRECISION (type);
> +auto unsign = TYPE_UNSIGNED (type);
> +tree inttype = build_nonstandard_integer_type (prec, unsign);
> +   }
> +   (convert (bit_xor (negate (convert:inttype @0)) (convert:inttype @2))

so we don't get to know which of @1 or @2 is "simpler" (the not
explicitely inverted
operand), I suppose that's the disadvantage of using bitwise_inverted_equal_p.
I'll note that if you make bitwise_inverted_equal_p a match you'd need a :c on
the 'cond' but otherwise complexity would be the same as match patterns are not
"inlined".

In any case, OK with the INTEGRAL_TYPE_P check.

Thanks,
Richard.

> +#endif
> +
>  /* Simplify pointer equality compares using PTA.  */
>  (for neeq (ne eq)
>   (simplify
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/20030709-2.c 
> b/gcc/testsuite/gcc.dg/tree-ssa/20030709-2.c
> index 5009cd69cfe..78938f919d4 100644
> --- a/gcc/testsuite/gcc.dg/tree-ssa/20030709-2.c
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/20030709-2.c
> @@ -29,15 +29,16 @@ union tree_node
>  };
>  int make_decl_rtl (tree, int);
>  void *
> -get_alias_set (t)
> +get_alias_set (t, t1)
>   tree t;
> + void *t1;
>  {
>long set;
>if (t->decl.rtl)
>  return (t->decl.rtl->fld[1].rtmem
> ? 0
> : (((t->decl.rtl ? t->decl.rtl: (make_decl_rtl (t, 0), 
> t->decl.rtl)))->fld[1]).rtmem);
> -  return (void*)-1;
> +  return t1;
>  }
>
>  /* There should be precisely one load of ->decl.rtl.  If there is
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bool-14.c 
> b/gcc/testsuite/gcc.dg/tree-ssa/bool-14.c
> new file mode 100644
> index 000..0149380a63b
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/bool-14.c
> @@ -0,0 +1,15 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -fdump-tree-optimized-raw" } */
> +/* PR tree-optimization/110937 */
> +
> +_Bool f2(_Bool a, _Bool b)
> +{
> +if (a)
> +  return !b;
> +return b;
> +}
> +
> +/* We should be able to remove the conditional and convert it to an xor. */
> +/* { dg-final { scan-tree-dump-not "gimple_cond " "optimized" } } */
> +/* { dg-final { scan-tree-dump-not "gimple_phi " "optimized" } } */
> +/* { dg-final { scan-tree-dump-times "bit_xor_expr, " 1 "optimized" } } */
> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bool-15.c 
> b/gcc/testsuite/gcc.dg/tree-ssa/bool-15.c
> new file mode 100644
> index 000..1f496663863
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/tree-ssa/bool-15.c
> @@ -0,0 +1,18 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -fdump-tree-optimized-raw" } */
> +/* PR tree-optimization/110937 */
> +
> +_Bool f2(int x, int y, int w, int z)
> +{
> +  _Bool a = x == y;
> +  _Bool b = w == z;
> +  if (a)
> +return !b;
> +  return b;
> +}
> +
> +/* We should be able to remove the conditional and convert it to an xor. */
> +/* { dg-final { scan-tree-dump-not "gimple_cond " 

[PATCH] MATCH: [PR110937/PR100798] (a ? ~b : b) should be optimized to b ^ -(a)

2023-08-07 Thread Andrew Pinski via Gcc-patches
This adds a simple match pattern for this case.
I noticed it a couple of different places.
One while I was looking at code generation of a parser and
also while I was looking at locations where bitwise_inverted_equal_p
should be used more.

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

PR tree-optimization/110937
PR tree-optimization/100798

gcc/ChangeLog:

* match.pd (`a ? ~b : b`): Handle this
case.

gcc/testsuite/ChangeLog:

* gcc.dg/tree-ssa/bool-14.c: New test.
* gcc.dg/tree-ssa/bool-15.c: New test.
* gcc.dg/tree-ssa/phi-opt-33.c: New test.
* gcc.dg/tree-ssa/20030709-2.c: Update testcase
so `a ? -1 : 0` is not used to hit the match
pattern.
---
 gcc/match.pd   | 13 +
 gcc/testsuite/gcc.dg/tree-ssa/20030709-2.c |  5 +++--
 gcc/testsuite/gcc.dg/tree-ssa/bool-14.c| 15 +++
 gcc/testsuite/gcc.dg/tree-ssa/bool-15.c| 18 ++
 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-33.c | 13 +
 5 files changed, 62 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bool-14.c
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bool-15.c
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-33.c

diff --git a/gcc/match.pd b/gcc/match.pd
index 9b4819e5be7..f887c517c81 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -6460,6 +6460,19 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
   (if (cmp == NE_EXPR)
{ constant_boolean_node (true, type); })))
 
+#if GIMPLE
+/* a?~t:t -> (-(a))^t */
+(simplify
+ (cond @0 @1 @2)
+ (if (bitwise_inverted_equal_p (@1, @2))
+  (with {
+auto prec = TYPE_PRECISION (type);
+auto unsign = TYPE_UNSIGNED (type);
+tree inttype = build_nonstandard_integer_type (prec, unsign);
+   }
+   (convert (bit_xor (negate (convert:inttype @0)) (convert:inttype @2))
+#endif
+
 /* Simplify pointer equality compares using PTA.  */
 (for neeq (ne eq)
  (simplify
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/20030709-2.c 
b/gcc/testsuite/gcc.dg/tree-ssa/20030709-2.c
index 5009cd69cfe..78938f919d4 100644
--- a/gcc/testsuite/gcc.dg/tree-ssa/20030709-2.c
+++ b/gcc/testsuite/gcc.dg/tree-ssa/20030709-2.c
@@ -29,15 +29,16 @@ union tree_node
 };
 int make_decl_rtl (tree, int);
 void *
-get_alias_set (t)
+get_alias_set (t, t1)
  tree t;
+ void *t1;
 {
   long set;
   if (t->decl.rtl)
 return (t->decl.rtl->fld[1].rtmem 
? 0
: (((t->decl.rtl ? t->decl.rtl: (make_decl_rtl (t, 0), 
t->decl.rtl)))->fld[1]).rtmem);
-  return (void*)-1;
+  return t1;
 }
 
 /* There should be precisely one load of ->decl.rtl.  If there is
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bool-14.c 
b/gcc/testsuite/gcc.dg/tree-ssa/bool-14.c
new file mode 100644
index 000..0149380a63b
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/bool-14.c
@@ -0,0 +1,15 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized-raw" } */
+/* PR tree-optimization/110937 */
+
+_Bool f2(_Bool a, _Bool b)
+{
+if (a)
+  return !b;
+return b;
+}
+
+/* We should be able to remove the conditional and convert it to an xor. */
+/* { dg-final { scan-tree-dump-not "gimple_cond " "optimized" } } */
+/* { dg-final { scan-tree-dump-not "gimple_phi " "optimized" } } */
+/* { dg-final { scan-tree-dump-times "bit_xor_expr, " 1 "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bool-15.c 
b/gcc/testsuite/gcc.dg/tree-ssa/bool-15.c
new file mode 100644
index 000..1f496663863
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/bool-15.c
@@ -0,0 +1,18 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized-raw" } */
+/* PR tree-optimization/110937 */
+
+_Bool f2(int x, int y, int w, int z)
+{
+  _Bool a = x == y;
+  _Bool b = w == z;
+  if (a)
+return !b;
+  return b;
+}
+
+/* We should be able to remove the conditional and convert it to an xor. */
+/* { dg-final { scan-tree-dump-not "gimple_cond " "optimized" } } */
+/* { dg-final { scan-tree-dump-not "gimple_phi " "optimized" } } */
+/* { dg-final { scan-tree-dump-not "ne_expr, " "optimized" } } */
+/* { dg-final { scan-tree-dump-times "bit_xor_expr, " 1 "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-33.c 
b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-33.c
new file mode 100644
index 000..b79fe44187a
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-33.c
@@ -0,0 +1,13 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized-raw" } */
+/* PR tree-optimization/100798 */
+
+int f(int a, int t)
+{
+  return (a=='s' ? ~t : t);
+}
+
+/* This should be convert into t^-(a=='s').  */
+/* { dg-final { scan-tree-dump-times "bit_xor_expr, " 1 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "negate_expr, " 1 "optimized" } } */
+/* { dg-final { scan-tree-dump-not "bit_not_expr, " "optimized" } } */
-- 
2.31.1