https://gcc.gnu.org/g:d3e4c092c4f167d3c3ae8aa59d273b0202cef6cf
commit r14-11518-gd3e4c092c4f167d3c3ae8aa59d273b0202cef6cf Author: Richard Biener <rguent...@suse.de> Date: Mon Mar 3 14:12:37 2025 +0100 tree-optimization/119096 - bogus conditional reduction vectorization 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. 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. (cherry picked from commit 10e4107dfcf9fe324d0902f16411a75c596dab91) Diff: --- gcc/testsuite/gcc.dg/vect/pr119096.c | 21 +++++++++++++++++++++ gcc/tree-vect-loop.cc | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/gcc/testsuite/gcc.dg/vect/pr119096.c b/gcc/testsuite/gcc.dg/vect/pr119096.c new file mode 100644 index 000000000000..2c03a5936831 --- /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 95f6d06820f3..75fbcdf46d09 100644 --- a/gcc/tree-vect-loop.cc +++ b/gcc/tree-vect-loop.cc @@ -8660,7 +8660,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]);