This backport contains 2 patch from gcc main stream tree. The commit id of these patchs list as following in the order of time. 0001-Fix-EXTRACT_LAST_REDUCTION-handling-of-pattern-stmts.patch 9ec35478ccf0f3539988a054b7996278706a7710 0001-Fix-EXTRACT_LAST_REDUCTION-segfault.patch dc176c3ccd6a8cd3f809f3c1549ad00674061eb5 diff -Nurp a/gcc/testsuite/gcc.dg/vect/vect-cond-reduc-6.c b/gcc/testsuite/gcc.dg/vect/vect-cond-reduc-6.c --- a/gcc/testsuite/gcc.dg/vect/vect-cond-reduc-6.c 1969-12-31 19:00:00.000000000 -0500 +++ b/gcc/testsuite/gcc.dg/vect/vect-cond-reduc-6.c 2020-12-14 21:16:26.492000000 -0500 @@ -0,0 +1,10 @@ +/* { dg-do compile } */ + +int +f (int *y) +{ + int res = 0; + for (int i = 0; i < 100; ++i) + res = (y[i] & 1) == 0 && (y[i] < 10) ? res : 1; + return res; +} diff -Nurp a/gcc/tree-vect-stmts.c b/gcc/tree-vect-stmts.c --- a/gcc/tree-vect-stmts.c 2020-12-14 21:15:27.004000000 -0500 +++ b/gcc/tree-vect-stmts.c 2020-12-14 21:16:26.492000000 -0500 @@ -1777,9 +1777,10 @@ vect_finish_stmt_generation_1 (stmt_vec_ stmt_vec_info vect_finish_replace_stmt (stmt_vec_info stmt_info, gimple *vec_stmt) { - gcc_assert (gimple_get_lhs (stmt_info->stmt) == gimple_get_lhs (vec_stmt)); + gimple *scalar_stmt = vect_orig_stmt (stmt_info)->stmt; + gcc_assert (gimple_get_lhs (scalar_stmt) == gimple_get_lhs (vec_stmt)); - gimple_stmt_iterator gsi = gsi_for_stmt (stmt_info->stmt); + gimple_stmt_iterator gsi = gsi_for_stmt (scalar_stmt); gsi_replace (&gsi, vec_stmt, true); return vect_finish_stmt_generation_1 (stmt_info, vec_stmt); @@ -9118,10 +9119,12 @@ vectorizable_condition (stmt_vec_info st if (new_code == ERROR_MARK) must_invert_cmp_result = true; else - cond_code = new_code; + { + cond_code = new_code; + /* Make sure we don't accidentally use the old condition. */ + cond_expr = NULL_TREE; + } } - /* Make sure we don't accidentally use the old condition. */ - cond_expr = NULL_TREE; std::swap (then_clause, else_clause); } @@ -9426,20 +9429,21 @@ vectorizable_condition (stmt_vec_info st vect_finish_stmt_generation (stmt_info, new_stmt, gsi); vec_compare = vec_compare_name; } + gimple *old_stmt = vect_orig_stmt (stmt_info)->stmt; + tree lhs = gimple_get_lhs (old_stmt); gcall *new_stmt = gimple_build_call_internal (IFN_FOLD_EXTRACT_LAST, 3, else_clause, vec_compare, vec_then_clause); - gimple_call_set_lhs (new_stmt, scalar_dest); - SSA_NAME_DEF_STMT (scalar_dest) = new_stmt; - if (stmt_info->stmt == gsi_stmt (*gsi)) + gimple_call_set_lhs (new_stmt, lhs); + SSA_NAME_DEF_STMT (lhs) = new_stmt; + if (old_stmt == gsi_stmt (*gsi)) new_stmt_info = vect_finish_replace_stmt (stmt_info, new_stmt); else { /* In this case we're moving the definition to later in the block. That doesn't matter because the only uses of the lhs are in phi statements. */ - gimple_stmt_iterator old_gsi - = gsi_for_stmt (stmt_info->stmt); + gimple_stmt_iterator old_gsi = gsi_for_stmt (old_stmt); gsi_remove (&old_gsi, true); new_stmt_info = vect_finish_stmt_generation (stmt_info, new_stmt, gsi);