From 4bbd661e9937d46cc66182423f1946761a4f31c9 Mon Sep 17 00:00:00 2001
From: Richard Sandiford <richard.sandiford@arm.com>
Date: Sun, 29 Dec 2019 09:27:43 +0000
Subject: [PATCH] Check for a supported comparison when using
 EXTRACT_LAST_REDUCTION

The EXTRACT_LAST_REDUCTION handling needs to generate a separate
comparison instruction that feeds the vector mask argument of the
IFN_EXTRACT_LAST call.  We weren't checking whether that comparison
was supported, leading to an ICE on the testcase.

2019-12-29  Richard Sandiford  <richard.sandiford@arm.com>

gcc/
	* tree-vect-stmts.c (vectorizable_condition): For extract-last
	reductions, check that the target supports the required comparison
	operation.

gcc/testsuite/
	* gcc.dg/vect/vect-cond-12.c: New test.

From-SVN: r279752
---
 gcc/ChangeLog                            |  6 ++++++
 gcc/testsuite/ChangeLog                  |  4 ++++
 gcc/testsuite/gcc.dg/vect/vect-cond-12.c | 14 ++++++++++++++
 gcc/tree-vect-stmts.c                    | 10 ++++++++++
 4 files changed, 34 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/vect/vect-cond-12.c

diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 922e4048a785..fc7af987cdf5 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,9 @@
+2019-12-29  Richard Sandiford  <richard.sandiford@arm.com>
+
+	* tree-vect-stmts.c (vectorizable_condition): For extract-last
+	reductions, check that the target supports the required comparison
+	operation.
+
 2019-12-27  Richard Sandiford  <richard.sandiford@arm.com>
 
 	* config/aarch64/iterators.md (V_INT_CONTAINER): Fix VNx2SF entry.
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index eec8b0b91dcb..2e0f36410df9 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,7 @@
+2019-12-29  Richard Sandiford  <richard.sandiford@arm.com>
+
+	* gcc.dg/vect/vect-cond-12.c: New test.
+
 2019-12-27  Richard Sandiford  <richard.sandiford@arm.com>
 
 	* gcc.target/aarch64/sve/mixed_size_11.c: New test.
diff --git a/gcc/testsuite/gcc.dg/vect/vect-cond-12.c b/gcc/testsuite/gcc.dg/vect/vect-cond-12.c
new file mode 100644
index 000000000000..1add96afe450
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/vect-cond-12.c
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+
+int
+f (int *x, short *y)
+{
+  int res = 100;
+  for (int i = 0; i < 40; ++i)
+    {
+      if (y[i] > 1)
+	res = x[i];
+      x[i] += y[i];
+    }
+  return res;
+}
diff --git a/gcc/tree-vect-stmts.c b/gcc/tree-vect-stmts.c
index 706da5b4e920..feb0b7eb7b3c 100644
--- a/gcc/tree-vect-stmts.c
+++ b/gcc/tree-vect-stmts.c
@@ -10081,6 +10081,16 @@ vectorizable_condition (stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
       cond_code = SSA_NAME;
     }
 
+  if (TREE_CODE_CLASS (cond_code) == tcc_comparison
+      && reduction_type == EXTRACT_LAST_REDUCTION
+      && !expand_vec_cmp_expr_p (comp_vectype, vec_cmp_type, cond_code))
+    {
+      if (dump_enabled_p ())
+	dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
+			 "reduction comparison operation not supported.\n");
+      return false;
+    }
+
   if (!vec_stmt)
     {
       if (bitop1 != NOP_EXPR)
-- 
GitLab