]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
match: Fix wrong code due to `(a ? e : f) !=/== (b ? e : f)` patterns [PR116120]
authorAndrew Pinski <quic_apinski@quicinc.com>
Mon, 29 Jul 2024 21:00:13 +0000 (14:00 -0700)
committerAndrew Pinski <quic_apinski@quicinc.com>
Thu, 1 Aug 2024 15:21:15 +0000 (08:21 -0700)
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 */

Also this can't be done if the X can be a NaNs either. Since that changes the value there too.

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.

Changes since v1:
* v2: Fixed the testcase names and fixed dg-run to be `dg-do run`. Added a check for HONORS_NANS too.

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

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_apinski@quicinc.com>
gcc/match.pd
gcc/testsuite/g++.dg/torture/pr116120-1.C [new file with mode: 0644]
gcc/testsuite/g++.dg/torture/pr116120-2.C [new file with mode: 0644]
gcc/testsuite/g++.dg/tree-ssa/pr111150.C
gcc/testsuite/gcc.dg/tree-ssa/pr111150-1.c
gcc/testsuite/gcc.dg/tree-ssa/pr111150.c

index 881a827860f04bb772a34d8e98fa11d94f4f2a2e..c9c8478d2865083ef2c40d27b3a1f86db3035b35 100644 (file)
@@ -5632,21 +5632,28 @@ 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 */
+/* These are only valid if x and y don't have NaNs. */
 (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 (!HONOR_NANS (@1)
+        && 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 (!HONOR_NANS (@1)
+        && 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 (file)
index 0000000..209946f
--- /dev/null
@@ -0,0 +1,32 @@
+// { dg-do 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 (file)
index 0000000..e9fee17
--- /dev/null
@@ -0,0 +1,53 @@
+// { dg-do run }
+// PR tree-optimization/116120
+
+// The optimization for `(a ? x : y) == (b ? x : y)`
+// missed that x and y could be the same value
+// This can't be done if x and y support NaNs since `NaN == NaN` is always false
+// And dominates the whole expression rather than supporting it.
+
+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) __attribute__((noinline));
+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) __attribute__((noinline));
+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;
+  v1sf f;
+
+  /* Test signed 0s. */
+  e = (v1sf){0.0};
+  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();
+
+  /* Test NaNs */
+#if __FLT_HAS_QUIET_NAN__
+  e = (v1sf){__builtin_nanf("")};
+  f = e;
+  /* Test NaNs */
+  r = f1(a,b,c,d,e, f);
+  r1 = f2(a[0], b[0], c[0], d[0], e[0], f[0]);
+  if (r[0] != r1)
+    __builtin_abort();
+#endif
+}
index ac5d3ef15d83aed4c91b65ac93955d281e7f7629..82af8f968789c3be3eacf675818f70ccb2a72200 100644 (file)
@@ -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" } } */
index 6f4b21ac6bcb87508f8bbf8ac22b819262096326..1970f7fe496f6196386ebcabf5d764e6c2f776ad 100644 (file)
@@ -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" } } */
index 568ae9e44b3dea499b84abb3c36b003fe426c4b2..6370be8cfd25a2a31fbdfe600df07747f1ff803a 100644 (file)
@@ -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" } } */