On Wed, Jul 31, 2024 at 5:05 AM Richard Biener
<richard.guent...@gmail.com> wrote:
>
> On Tue, Jul 30, 2024 at 5:26 PM Andrew Pinski <quic_apin...@quicinc.com> 
> wrote:
> >
> > When this pattern was converted from being only dealing with 0/-1, we 
> > missed that if `e == f` is true
> > then the optimization is wrong and needs an extra check for that.
> >
> > This changes the patterns to be:
> > /* (a ? x : y) != (b ? x : y) --> (a^b & (x != y)) ? TRUE  : FALSE */
> > /* (a ? x : y) == (b ? x : y) --> (a^b & (x != y)) ? FALSE : TRUE  */
> > /* (a ? x : y) != (b ? y : x) --> (a^b | (x == y)) ? FALSE : TRUE  */
> > /* (a ? x : y) == (b ? y : x) --> (a^b | (x == y)) ? TRUE  : FALSE */
> >
> > This still produces better code than the original case and in many cases (x 
> > != y) will
> > still reduce to either false or true.
> >
> > With this change we also need to make sure `a`, `b` and the resulting types 
> > are all
> > the same for the same reason as the previous patch.
> >
> > I updated (well added) to the testcases to make sure there are the right 
> > amount of
> > comparisons left.
> >
> > Bootstrapped and tested on x86_64-linux-gnu with no regressions.
>
> OK

I found a few issues with this version of the patch in the end dealing
with NaNs and because the testcase was not actually running it was not
spotted before.
I am testing a new version of the patch and will submit it after it
finishes testing.

Thanks,
Andrew

>
> >         PR tree-optimization/116120
> >
> > gcc/ChangeLog:
> >
> >         * match.pd (`(a ? x : y) eq/ne (b ? x : y)`): Add test for `x != y`
> >         in result.
> >         (`(a ? x : y) eq/ne (b ? y : x)`): Add test for `x == y` in result.
> >
> > gcc/testsuite/ChangeLog:
> >
> >         * g++.dg/tree-ssa/pr111150.C: Add extra checks on the test.
> >         * gcc.dg/tree-ssa/pr111150-1.c: Likewise.
> >         * gcc.dg/tree-ssa/pr111150.c: Likewise.
> >         * g++.dg/torture/pr116120-1.c: New test.
> >         * g++.dg/torture/pr116120-2.c: New test.
> >
> > Signed-off-by: Andrew Pinski <quic_apin...@quicinc.com>
> > ---
> >  gcc/match.pd                               | 20 ++++++++-----
> >  gcc/testsuite/g++.dg/torture/pr116120-1.c  | 32 ++++++++++++++++++++
> >  gcc/testsuite/g++.dg/torture/pr116120-2.c  | 35 ++++++++++++++++++++++
> >  gcc/testsuite/g++.dg/tree-ssa/pr111150.C   | 10 +++++++
> >  gcc/testsuite/gcc.dg/tree-ssa/pr111150-1.c |  9 ++++++
> >  gcc/testsuite/gcc.dg/tree-ssa/pr111150.c   |  1 +
> >  6 files changed, 99 insertions(+), 8 deletions(-)
> >  create mode 100644 gcc/testsuite/g++.dg/torture/pr116120-1.c
> >  create mode 100644 gcc/testsuite/g++.dg/torture/pr116120-2.c
> >
> > diff --git a/gcc/match.pd b/gcc/match.pd
> > index 881a827860f..4d3ee578371 100644
> > --- a/gcc/match.pd
> > +++ b/gcc/match.pd
> > @@ -5632,21 +5632,25 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
> >    (vec_cond (bit_and (bit_not @0) @1) @2 @3)))
> >  #endif
> >
> > -/* (a ? x : y) != (b ? x : y) --> (a^b) ? TRUE  : FALSE */
> > -/* (a ? x : y) == (b ? x : y) --> (a^b) ? FALSE : TRUE  */
> > -/* (a ? x : y) != (b ? y : x) --> (a^b) ? FALSE : TRUE  */
> > -/* (a ? x : y) == (b ? y : x) --> (a^b) ? TRUE  : FALSE */
> > +/* (a ? x : y) != (b ? x : y) --> (a^b & (x != y)) ? TRUE  : FALSE */
> > +/* (a ? x : y) == (b ? x : y) --> (a^b & (x != y)) ? FALSE : TRUE  */
> > +/* (a ? x : y) != (b ? y : x) --> (a^b | (x == y)) ? FALSE : TRUE  */
> > +/* (a ? x : y) == (b ? y : x) --> (a^b | (x == y)) ? TRUE  : FALSE */
> >  (for cnd (cond vec_cond)
> >   (for eqne (eq ne)
> >    (simplify
> >     (eqne:c (cnd @0 @1 @2) (cnd @3 @1 @2))
> > -    (if (types_match (TREE_TYPE (@0), TREE_TYPE (@3)))
> > -     (cnd (bit_xor @0 @3) { constant_boolean_node (eqne == NE_EXPR, type); 
> > }
> > +    (if (types_match (TREE_TYPE (@0), TREE_TYPE (@3))
> > +         && types_match (type, TREE_TYPE (@0)))
> > +     (cnd (bit_and (bit_xor @0 @3) (ne:type @1 @2))
> > +      { constant_boolean_node (eqne == NE_EXPR, type); }
> >        { constant_boolean_node (eqne != NE_EXPR, type); })))
> >    (simplify
> >     (eqne:c (cnd @0 @1 @2) (cnd @3 @2 @1))
> > -    (if (types_match (TREE_TYPE (@0), TREE_TYPE (@3)))
> > -     (cnd (bit_xor @0 @3) { constant_boolean_node (eqne != NE_EXPR, type); 
> > }
> > +    (if (types_match (TREE_TYPE (@0), TREE_TYPE (@3))
> > +         && types_match (type, TREE_TYPE (@0)))
> > +     (cnd (bit_ior (bit_xor @0 @3) (eq:type @1 @2))
> > +      { constant_boolean_node (eqne != NE_EXPR, type); }
> >        { constant_boolean_node (eqne == NE_EXPR, type); })))))
> >
> >  /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
> > diff --git a/gcc/testsuite/g++.dg/torture/pr116120-1.c 
> > b/gcc/testsuite/g++.dg/torture/pr116120-1.c
> > new file mode 100644
> > index 00000000000..cffb7fbdc5b
> > --- /dev/null
> > +++ b/gcc/testsuite/g++.dg/torture/pr116120-1.c
> > @@ -0,0 +1,32 @@
> > +// { dg-run }
> > +// PR tree-optimization/116120
> > +
> > +// The optimization for `(a ? x : y) != (b ? x : y)`
> > +// missed that x and y could be the same value.
> > +
> > +typedef int v4si __attribute((__vector_size__(1 * sizeof(int))));
> > +v4si f1(v4si a, v4si b, v4si c, v4si d, v4si e, v4si f) {
> > +  v4si X = a == b ? e : f;
> > +  v4si Y = c == d ? e : f;
> > +  return (X != Y); // ~(X == Y ? -1 : 0) (x ^ Y)
> > +}
> > +
> > +int f2(int a, int b, int c, int d, int e, int f) {
> > +  int X = a == b ? e : f;
> > +  int Y = c == d ? e : f;
> > +  return (X != Y) ? -1 : 0; // ~(X == Y ? -1 : 0) (x ^ Y)
> > +}
> > +
> > +int main()
> > +{
> > +  v4si a = {0};
> > +  v4si b = {0}; // a == b, true
> > +  v4si c = {2};
> > +  v4si d = {3}; // c == b, false
> > +  v4si e = {0};
> > +  v4si f = e;
> > +  v4si r = f1(a,b,c,d,e, f);
> > +  int r1 = f2(a[0], b[0], c[0], d[0], e[0], f[0]);
> > +  if (r[0] != r1)
> > +    __builtin_abort();
> > +}
> > diff --git a/gcc/testsuite/g++.dg/torture/pr116120-2.c 
> > b/gcc/testsuite/g++.dg/torture/pr116120-2.c
> > new file mode 100644
> > index 00000000000..997e0d1bfc6
> > --- /dev/null
> > +++ b/gcc/testsuite/g++.dg/torture/pr116120-2.c
> > @@ -0,0 +1,35 @@
> > +// { dg-run }
> > +// PR tree-optimization/116120
> > +
> > +// The optimization for `(a ? x : y) == (b ? x : y)`
> > +// missed that x and y could be the same value (or rather `x == y` could 
> > be always false, e.g. NaNs).
> > +
> > +typedef int v1si __attribute((__vector_size__(1 * sizeof(int))));
> > +typedef float v1sf __attribute((__vector_size__(1 * sizeof(float))));
> > +
> > +v1si f1(v1si a, v1si b, v1si c, v1si d, v1sf e, v1sf f) {
> > +  v1sf X = a == b ? e : f;
> > +  v1sf Y = c == d ? f : e;
> > +  return (X == Y); // ~(X == Y ? -1 : 0) (x ^ Y)
> > +}
> > +
> > +int f2(int a, int b, int c, int d, float e, float f) {
> > +  float X = a == b ? e : f;
> > +  float Y = c == d ? f : e;
> > +  return (X == Y) ? -1 : 0; // ~(X == Y ? -1 : 0) (x ^ Y)
> > +}
> > +
> > +int main()
> > +{
> > +  v1si a = {0};
> > +  v1si b = {0}; // a == b, true
> > +  v1si c = {2};
> > +  v1si d = {3}; // c == b, false
> > +  v1sf e = {__builtin_nanf("")};
> > +  v1sf f = e;
> > +  v1si r = f1(a,b,c,d,e, f);
> > +  int r1 = f2(a[0], b[0], c[0], d[0], e[0], f[0]);
> > +  if (r[0] != r1)
> > +    __builtin_abort();
> > +  __builtin_printf ("%d == %d.\n", r[0], r1);
> > +}
> > diff --git a/gcc/testsuite/g++.dg/tree-ssa/pr111150.C 
> > b/gcc/testsuite/g++.dg/tree-ssa/pr111150.C
> > index ac5d3ef15d8..82af8f96878 100644
> > --- a/gcc/testsuite/g++.dg/tree-ssa/pr111150.C
> > +++ b/gcc/testsuite/g++.dg/tree-ssa/pr111150.C
> > @@ -32,3 +32,13 @@ v4si f4_(v4si a, v4si b, v4si c, v4si d, v4si e, v4si f) 
> > {
> >
> >  /* For each testcase, should produce only one VEC_COND_EXPR for X^Y. */
> >  /* { dg-final { scan-tree-dump-times " VEC_COND_EXPR " 4 "forwprop1" } } */
> > +/* 2 IOR, one each for f1 and f2.
> > +   2 AND, one each for f3 and f4. */
> > +/* { dg-final { scan-tree-dump-times " & " 2 "forwprop1" } } */
> > +/* { dg-final { scan-tree-dump-times " \\| " 2 "forwprop1" } } */
> > +/* { dg-final { scan-tree-dump-times " \\^ " 4 "forwprop1" } } */
> > +/* 8 eq comparisons from each of `a == b`/`c == d`.
> > +   2 more to check that `e == f`
> > +   2 ne comparisons to check that `e != f`.   */
> > +/* { dg-final { scan-tree-dump-times " == " 10 "forwprop1" } } */
> > +/* { dg-final { scan-tree-dump-times " != " 2 "forwprop1" } } */
> > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr111150-1.c 
> > b/gcc/testsuite/gcc.dg/tree-ssa/pr111150-1.c
> > index 6f4b21ac6bc..1970f7fe496 100644
> > --- a/gcc/testsuite/gcc.dg/tree-ssa/pr111150-1.c
> > +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr111150-1.c
> > @@ -69,4 +69,13 @@ _Bool f4_(int a, int b, int c, int d, int e, int f) {
> >
> >  /* Should generate one bit_xor_expr for each testcase. */
> >  /* { dg-final { scan-tree-dump-not "cond_expr, "  "forwprop1" } } */
> > +/* 2 IOR, one each for f1 and f2.
> > +   2 AND, one each for f3 and f4. */
> > +/* { dg-final { scan-tree-dump-times "bit_ior_expr, " 2  "forwprop1" } } */
> > +/* { dg-final { scan-tree-dump-times "bit_and_expr, " 2  "forwprop1" } } */
> >  /* { dg-final { scan-tree-dump-times "bit_xor_expr, " 4 "forwprop1" } } */
> > +/* 8 eq comparisons from each of `a == b`/`c == d`.
> > +   2 more to check that `e == f`
> > +   2 ne comparisons to check that `e != f`.   */
> > +/* { dg-final { scan-tree-dump-times "<ne_expr, " 2 "forwprop1" } } */
> > +/* { dg-final { scan-tree-dump-times "<eq_expr, " 10 "forwprop1" } } */
> > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr111150.c 
> > b/gcc/testsuite/gcc.dg/tree-ssa/pr111150.c
> > index 568ae9e44b3..6370be8cfd2 100644
> > --- a/gcc/testsuite/gcc.dg/tree-ssa/pr111150.c
> > +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr111150.c
> > @@ -20,3 +20,4 @@ v4si f2_(v4si a, v4si b, v4si c, v4si d) {
> >
> >  /* For each testcase, should produce only one VEC_COND_EXPR for X^Y. */
> >  /* { dg-final { scan-tree-dump-times " VEC_COND_EXPR " 2 "forwprop1" } } */
> > +/* { dg-final { scan-tree-dump-times " == " 4 "forwprop1" } } */
> > --
> > 2.43.0
> >

Reply via email to