When we vectorize a .COND_ADD reduction and apply the single-use-def
cycle optimization we can end up chosing the wrong else value for
subsequent .COND_ADD.  The following rectifies this.

Bootstrap and regtest running on x86_64-unknown-linux-gnu.

        PR tree-optimization/119096
        * tree-vect-loop.cc (vect_transform_reduction): Use the
        correct else value for .COND_fn.

        * gcc.dg/vect/pr119096.c: New testcase.
---
 gcc/testsuite/gcc.dg/vect/pr119096.c | 21 +++++++++++++++++++++
 gcc/tree-vect-loop.cc                |  2 +-
 2 files changed, 22 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.dg/vect/pr119096.c

diff --git a/gcc/testsuite/gcc.dg/vect/pr119096.c 
b/gcc/testsuite/gcc.dg/vect/pr119096.c
new file mode 100644
index 00000000000..2c03a593683
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/pr119096.c
@@ -0,0 +1,21 @@
+#include "tree-vect.h"
+
+long __attribute__((noipa))
+sum(int* A, int* B)
+{
+        long total = 0;
+        for(int j = 0; j < 16; j++)
+                if((A[j] > 0) & (B[j] > 0))
+                        total += (long)A[j];
+        return total;
+}
+int main()
+{
+  int A[16] = { 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1 };
+  int B[16] = { };
+  check_vect ();
+  if (sum (A, B) != 0)
+    abort ();
+  return 0;
+}
+
diff --git a/gcc/tree-vect-loop.cc b/gcc/tree-vect-loop.cc
index dc15b955aad..52533623cab 100644
--- a/gcc/tree-vect-loop.cc
+++ b/gcc/tree-vect-loop.cc
@@ -9064,7 +9064,7 @@ vect_transform_reduction (loop_vec_info loop_vinfo,
            new_stmt = gimple_build_call_internal (internal_fn (code),
                                                   op.num_ops,
                                                   vop[0], vop[1], vop[2],
-                                                  vop[1]);
+                                                  vop[reduc_index]);
          else
            new_stmt = gimple_build_assign (vec_dest, tree_code (op.code),
                                            vop[0], vop[1], vop[2]);
-- 
2.43.0

Reply via email to