]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fix load/store bias handling for extractlast.
authorJuergen Christ <jchrist@linux.ibm.com>
Mon, 1 Sep 2025 09:29:45 +0000 (11:29 +0200)
committerJuergen Christ <jchrist@linux.ibm.com>
Wed, 10 Sep 2025 13:57:14 +0000 (15:57 +0200)
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 <jchrist@linux.ibm.com>
gcc/testsuite/gcc.dg/vect/nodump-extractlast-1.c [new file with mode: 0644]
gcc/tree-vect-loop.cc

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 (file)
index 0000000..980ac3e
--- /dev/null
@@ -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;
+}
index 446c4e3b3db66900ba4b4c75ecdf6459f754a259..ae999dbd071411da140ab35411c36b84e3221793 100644 (file)
@@ -10029,10 +10029,12 @@ vectorizable_live_operation_1 (loop_vec_info loop_vinfo, basic_block exit_bb,
     {
       /* Emit:
 
-        SCALAR_RES = VEC_EXTRACT <VEC_LHS, LEN + BIAS - 1>
+        SCALAR_RES = VEC_EXTRACT <VEC_LHS, LEN - (BIAS + 1)>
 
-        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 <VEC_LHS, LEN + BIAS - 1>.  */
+      /* SCALAR_RES = VEC_EXTRACT <VEC_LHS, LEN - (BIAS + 1)>.  */
       tree scalar_res
        = gimple_build (&stmts, CFN_VEC_EXTRACT, TREE_TYPE (vectype),
                        vec_lhs_phi, last_index);