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>
(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
--- /dev/null
+/* 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" } } */
--- /dev/null
+/* 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" } } */
--- /dev/null
+/* 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" } } */