https://gcc.gnu.org/g:44fcc1ca11e7ea35dc9fb25a5317346bc1eaf7b2

commit r15-2106-g44fcc1ca11e7ea35dc9fb25a5317346bc1eaf7b2
Author: Eikansh Gupta <quic_eikag...@quicinc.com>
Date:   Wed May 22 23:28:48 2024 +0530

    MATCH: Simplify (a ? x : y) eq/ne (b ? x : y) [PR111150]
    
    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_eikag...@quicinc.com>

Diff:
---
 gcc/match.pd                               | 15 +++++++
 gcc/testsuite/g++.dg/tree-ssa/pr111150.C   | 33 ++++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/pr111150-1.c | 72 ++++++++++++++++++++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/pr111150.c   | 22 +++++++++
 4 files changed, 142 insertions(+)

diff --git a/gcc/match.pd b/gcc/match.pd
index 24a0bbead3e7..5cb399b87180 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -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
index 000000000000..ca02d8dc51e3
--- /dev/null
+++ b/gcc/testsuite/g++.dg/tree-ssa/pr111150.C
@@ -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
index 000000000000..6f4b21ac6bcb
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr111150-1.c
@@ -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
index 000000000000..cf25c5d758c1
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr111150.c
@@ -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" } } */

Reply via email to