The following fixes an omission in bool pattern detection that
makes it fail when check_bool_pattern fails for COND_EXPR.  That's
not what it should do, instead it should still pattern recog
to var != 0 even if no further adjustments to the def chain are
necessary when var is not a mask already.

Bootstrapped and tested on x86_64-unknown-linux-gnu, queued for stage1.

There's another piece of the PR not yet fixed.

2022-02-21  Richard Biener  <rguent...@suse.de>

        PR tree-optimization/104595
        * tree-vect-patterns.c (vect_recog_bool_pattern): For
        COND_EXPR do not fail if check_bool_pattern returns false.

        * gcc.dg/vect/pr104595.c: New testcase.
---
 gcc/testsuite/gcc.dg/vect/pr104595.c | 24 ++++++++++++++++++++++++
 gcc/tree-vect-patterns.cc            | 16 ++++++++--------
 2 files changed, 32 insertions(+), 8 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/vect/pr104595.c

diff --git a/gcc/testsuite/gcc.dg/vect/pr104595.c 
b/gcc/testsuite/gcc.dg/vect/pr104595.c
new file mode 100644
index 00000000000..bb7d79aa69f
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/pr104595.c
@@ -0,0 +1,24 @@
+/* { dg-do compile } */
+/* { dg-require-effective-target vect_condition } */
+
+#define N 256
+typedef char T;
+extern T a[N];
+extern T b[N];
+extern T c[N];
+extern _Bool pb[N];
+extern char pc[N];
+
+void predicate_by_bool()
+{
+  for (int i = 0; i < N; i++)
+    c[i] = pb[i] ? a[i] : b[i];
+}
+
+void predicate_by_char()
+{
+  for (int i = 0; i < N; i++)
+    c[i] = pc[i] ? a[i] : b[i];
+}
+
+/* { dg-final { scan-tree-dump-times "vectorized 1 loops" 2 "vect" } } */
diff --git a/gcc/tree-vect-patterns.cc b/gcc/tree-vect-patterns.cc
index 217bdfd7045..8c61eb965a6 100644
--- a/gcc/tree-vect-patterns.cc
+++ b/gcc/tree-vect-patterns.cc
@@ -4450,18 +4450,18 @@ vect_recog_bool_pattern (vec_info *vinfo,
       if (get_vectype_for_scalar_type (vinfo, type) == NULL_TREE)
        return NULL;
 
-      if (!check_bool_pattern (var, vinfo, bool_stmts))
+      if (check_bool_pattern (var, vinfo, bool_stmts))
+       var = adjust_bool_stmts (vinfo, bool_stmts, type, stmt_vinfo);
+      else if (integer_type_for_mask (var, vinfo))
        return NULL;
 
-      rhs = adjust_bool_stmts (vinfo, bool_stmts, type, stmt_vinfo);
-
       lhs = vect_recog_temp_ssa_var (TREE_TYPE (lhs), NULL);
       pattern_stmt 
-         = gimple_build_assign (lhs, COND_EXPR,
-                                build2 (NE_EXPR, boolean_type_node,
-                                        rhs, build_int_cst (type, 0)),
-                                gimple_assign_rhs2 (last_stmt),
-                                gimple_assign_rhs3 (last_stmt));
+       = gimple_build_assign (lhs, COND_EXPR,
+                              build2 (NE_EXPR, boolean_type_node,
+                                      var, build_int_cst (TREE_TYPE (var), 0)),
+                              gimple_assign_rhs2 (last_stmt),
+                              gimple_assign_rhs3 (last_stmt));
       *type_out = vectype;
       vect_pattern_detected ("vect_recog_bool_pattern", last_stmt);
 
-- 
2.34.1

Reply via email to