From: Juergen Christ Date: Mon, 1 Sep 2025 09:29:45 +0000 (+0200) Subject: Fix load/store bias handling for extractlast. X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=64abb12aacbb64eb17e906edc24a788575f4103d;p=thirdparty%2Fgcc.git Fix load/store bias handling for extractlast. The length returned by vect_get_loop_len is REALLEN + BIAS, but was assumed to be REALLEN - BIAS. If BIAS is -1, this leads to wrong code. gcc/ChangeLog: * tree-vect-loop.cc (vectorizable_live_operation_1): Fix load/store bias handling. gcc/testsuite/ChangeLog: * gcc.dg/vect/nodump-extractlast-1.c: New test. Signed-off-by: Juergen Christ --- diff --git a/gcc/testsuite/gcc.dg/vect/nodump-extractlast-1.c b/gcc/testsuite/gcc.dg/vect/nodump-extractlast-1.c new file mode 100644 index 00000000000..980ac3e4218 --- /dev/null +++ b/gcc/testsuite/gcc.dg/vect/nodump-extractlast-1.c @@ -0,0 +1,21 @@ +/* Check for a bung in the treatment of LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS when + using VEC_EXTRACT. */ +/* { dg-require-effective-target vect_int } */ + +char c = 4; + +__attribute__((noipa)) +int +foo () +{ + for ( ; c > 3; c -= 3); + return c - 1; +} + +int +main () +{ + if (foo() != 0) + __builtin_abort(); + return 0; +} diff --git a/gcc/tree-vect-loop.cc b/gcc/tree-vect-loop.cc index 446c4e3b3db..ae999dbd071 100644 --- a/gcc/tree-vect-loop.cc +++ b/gcc/tree-vect-loop.cc @@ -10029,10 +10029,12 @@ vectorizable_live_operation_1 (loop_vec_info loop_vinfo, basic_block exit_bb, { /* Emit: - SCALAR_RES = VEC_EXTRACT + SCALAR_RES = VEC_EXTRACT - where VEC_LHS is the vectorized live-out result and MASK is - the loop mask for the final iteration. */ + where VEC_LHS is the vectorized live-out result, LEN is the length of + the vector, BIAS is the load-store bias. The bias should not be used + at all since we are not using load/store operations, but LEN will be + REALLEN + BIAS, so subtract it to get to the correct position. */ gcc_assert (SLP_TREE_LANES (slp_node) == 1); gimple_seq tem = NULL; gimple_stmt_iterator gsi = gsi_last (tem); @@ -10041,18 +10043,18 @@ vectorizable_live_operation_1 (loop_vec_info loop_vinfo, basic_block exit_bb, 1, vectype, 0, 1); gimple_seq_add_seq (&stmts, tem); - /* BIAS - 1. */ + /* BIAS + 1. */ signed char biasval = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo); - tree bias_minus_one - = int_const_binop (MINUS_EXPR, + tree bias_plus_one + = int_const_binop (PLUS_EXPR, build_int_cst (TREE_TYPE (len), biasval), build_one_cst (TREE_TYPE (len))); - /* LAST_INDEX = LEN + (BIAS - 1). */ - tree last_index = gimple_build (&stmts, PLUS_EXPR, TREE_TYPE (len), - len, bias_minus_one); + /* LAST_INDEX = LEN - (BIAS + 1). */ + tree last_index = gimple_build (&stmts, MINUS_EXPR, TREE_TYPE (len), + len, bias_plus_one); - /* SCALAR_RES = VEC_EXTRACT . */ + /* SCALAR_RES = VEC_EXTRACT . */ tree scalar_res = gimple_build (&stmts, CFN_VEC_EXTRACT, TREE_TYPE (vectype), vec_lhs_phi, last_index);