]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
MATCH: Simplify (a ? x : y) eq/ne (b ? x : y) [PR111150]
authorEikansh Gupta <quic_eikagupt@quicinc.com>
Wed, 22 May 2024 17:58:48 +0000 (23:28 +0530)
committerAndrew Pinski <quic_apinski@quicinc.com>
Wed, 17 Jul 2024 16:58:11 +0000 (09:58 -0700)
This patch adds match pattern for `(a ? x : y) eq/ne (b ? x : y)`.
In forwprop1 pass, depending on the type of `a` and `b`, GCC produces
`vec_cond` or `cond_expr`. Based on the observation that `(x != y)` is
TRUE, the pattern can be optimized to produce `(a^b ? TRUE : FALSE)`.

The patch adds match pattern for a, b:
(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) ? TRUE  : FALSE
(a ? x : y) == (b ? y : x) --> (a^b) ? FALSE : TRUE

PR tree-optimization/111150

gcc/ChangeLog:

* match.pd (`(a ? x : y) eq/ne (b ? x : y)`): New pattern.
(`(a ? x : y) eq/ne (b ? y : x)`): New pattern.

gcc/testsuite/ChangeLog:

* gcc.dg/tree-ssa/pr111150.c: New test.
* gcc.dg/tree-ssa/pr111150-1.c: New test.
* g++.dg/tree-ssa/pr111150.C: New test.

Signed-off-by: Eikansh Gupta <quic_eikagupt@quicinc.com>
gcc/match.pd
gcc/testsuite/g++.dg/tree-ssa/pr111150.C [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/pr111150-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/pr111150.c [new file with mode: 0644]

index 24a0bbead3e7669dbc344b366474176968a7d6d3..5cb399b87180edd03764b57539babf683eebc94c 100644 (file)
@@ -5586,6 +5586,21 @@ 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 */
+(for cnd (cond vec_cond)
+ (for eqne (eq ne)
+  (simplify
+   (eqne:c (cnd @0 @1 @2) (cnd @3 @1 @2))
+    (cnd (bit_xor @0 @3) { 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))
+    (cnd (bit_xor @0 @3) { constant_boolean_node (eqne != NE_EXPR, type); }
+     { constant_boolean_node (eqne == NE_EXPR, type); }))))
+
 /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask
    types are compatible.  */
 (simplify
diff --git a/gcc/testsuite/g++.dg/tree-ssa/pr111150.C b/gcc/testsuite/g++.dg/tree-ssa/pr111150.C
new file mode 100644 (file)
index 0000000..ca02d8d
--- /dev/null
@@ -0,0 +1,33 @@
+/* PR tree-optimization/111150 */
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-forwprop1" } */
+typedef int v4si __attribute((__vector_size__(4 * sizeof(int))));
+
+/* Before the patch, VEC_COND_EXPR was generated for each statement in the
+   function. This resulted in 3 VEC_COND_EXPR. */
+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);
+}
+
+v4si f2_(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);
+}
+
+v4si f3_(v4si a, v4si b, v4si c, v4si d, v4si e, v4si f) {
+  v4si X = a == b ? e : f;
+  v4si Y = c == d ? f : e;
+  return (X != Y);
+}
+
+v4si f4_(v4si a, v4si b, v4si c, v4si d, v4si e, v4si f) {
+  v4si X = a == b ? e : f;
+  v4si Y = c == d ? f : e;
+  return (X == Y);
+}
+
+/* For each testcase, should produce only one VEC_COND_EXPR for X^Y. */
+/* { dg-final { scan-tree-dump-times " VEC_COND_EXPR " 4 "forwprop1" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr111150-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr111150-1.c
new file mode 100644 (file)
index 0000000..6f4b21a
--- /dev/null
@@ -0,0 +1,72 @@
+/* PR tree-optimization/111150 */
+/* { dg-do compile } */
+/* { dg-options "-O1 -fgimple -fdump-tree-forwprop1-raw" } */
+
+/* Checks if pattern (X ? e : f) == (Y ? e : f) gets optimized. */
+__GIMPLE()
+_Bool f1_(int a, int b, int c, int d, int e, int f) {
+  _Bool X;
+  _Bool Y;
+  _Bool t;
+  int t1;
+  int t2;
+  X = a == b;
+  Y = c == d;
+  /* Before the patch cond_expr was generated for these 2 statements. */
+  t1 = X ? e : f;
+  t2 = Y ? e : f;
+  t = t1 == t2;
+  return t;
+}
+
+/* Checks if pattern (X ? e : f) != (Y ? e : f) gets optimized. */
+__GIMPLE()
+_Bool f2_(int a, int b, int c, int d, int e, int f) {
+  _Bool X;
+  _Bool Y;
+  _Bool t;
+  int t1;
+  int t2;
+  X = a == b;
+  Y = c == d;
+  t1 = X ? e : f;
+  t2 = Y ? e : f;
+  t = t1 != t2;
+  return t;
+}
+
+/* Checks if pattern (X ? e : f) == (Y ? f : e) gets optimized. */
+__GIMPLE()
+_Bool f3_(int a, int b, int c, int d, int e, int f) {
+  _Bool X;
+  _Bool Y;
+  _Bool t;
+  int t1;
+  int t2;
+  X = a == b;
+  Y = c == d;
+  t1 = X ? e : f;
+  t2 = Y ? f : e;
+  t = t1 == t2;
+  return t;
+}
+
+/* Checks if pattern (X ? e : f) != (Y ? f : e) gets optimized. */
+__GIMPLE()
+_Bool f4_(int a, int b, int c, int d, int e, int f) {
+  _Bool X;
+  _Bool Y;
+  _Bool t;
+  int t1;
+  int t2;
+  X = a == b;
+  Y = c == d;
+  t1 = X ? e : f;
+  t2 = Y ? f : e;
+  t = t1 != t2;
+  return t;
+}
+
+/* Should generate one bit_xor_expr for each testcase. */
+/* { dg-final { scan-tree-dump-not "cond_expr, "  "forwprop1" } } */
+/* { dg-final { scan-tree-dump-times "bit_xor_expr, " 4 "forwprop1" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr111150.c b/gcc/testsuite/gcc.dg/tree-ssa/pr111150.c
new file mode 100644 (file)
index 0000000..cf25c5d
--- /dev/null
@@ -0,0 +1,22 @@
+/* PR tree-optimization/111150 */
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-forwprop1" } */
+
+typedef int v4si __attribute((__vector_size__(4 * sizeof(int))));
+
+/* Before the patch, VEC_COND_EXPR was generated for each statement in the
+   function. This resulted in 3 VEC_COND_EXPR. */
+v4si f1_(v4si a, v4si b, v4si c, v4si d) {
+  v4si X = a == b;
+  v4si Y = c == d;
+  return (X != Y);
+}
+
+v4si f2_(v4si a, v4si b, v4si c, v4si d) {
+  v4si X = a == b;
+  v4si Y = c == d;
+  return (X == Y);
+}
+
+/* For each testcase, should produce only one VEC_COND_EXPR for X^Y. */
+/* { dg-final { scan-tree-dump-times " VEC_COND_EXPR " 2 "forwprop1" } } */