aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Pinski <quic_apinski@quicinc.com>2024-07-29 14:00:13 -0700
committerAndrew Pinski <quic_apinski@quicinc.com>2024-08-01 08:21:15 -0700
commitc5ccdfdcab0b24afba2a661af861bec1d63f0595 (patch)
treefccac517669580e2891b17bfdea0bf55723073e5
parent88fc730efdd31cc4bf13932ff3cc5b097a5eb23c (diff)
downloadgcc-c5ccdfdcab0b24afba2a661af861bec1d63f0595.zip
gcc-c5ccdfdcab0b24afba2a661af861bec1d63f0595.tar.gz
gcc-c5ccdfdcab0b24afba2a661af861bec1d63f0595.tar.bz2
match: Fix wrong code due to `(a ? e : f) !=/== (b ? e : f)` patterns [PR116120]
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>
-rw-r--r--gcc/match.pd23
-rw-r--r--gcc/testsuite/g++.dg/torture/pr116120-1.C32
-rw-r--r--gcc/testsuite/g++.dg/torture/pr116120-2.C53
-rw-r--r--gcc/testsuite/g++.dg/tree-ssa/pr111150.C10
-rw-r--r--gcc/testsuite/gcc.dg/tree-ssa/pr111150-1.c9
-rw-r--r--gcc/testsuite/gcc.dg/tree-ssa/pr111150.c1
6 files changed, 120 insertions, 8 deletions
diff --git a/gcc/match.pd b/gcc/match.pd
index 881a827..c9c8478 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -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
index 0000000..209946f
--- /dev/null
+++ b/gcc/testsuite/g++.dg/torture/pr116120-1.C
@@ -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
index 0000000..e9fee17
--- /dev/null
+++ b/gcc/testsuite/g++.dg/torture/pr116120-2.C
@@ -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
+}
diff --git a/gcc/testsuite/g++.dg/tree-ssa/pr111150.C b/gcc/testsuite/g++.dg/tree-ssa/pr111150.C
index ac5d3ef..82af8f9 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 6f4b21a..1970f7f 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 568ae9e..6370be8 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" } } */