https://gcc.gnu.org/g:ee5e63a6531defa646b995592f577c05ed288d63

commit r17-4643-gee5e63a6531defa646b995592f577c05ed288d63
Author: Richard Biener <[email protected]>
Date:   Fri Sep 25 10:36:12 2026 +0200

    tree-optimization/127600 - bogus INTEGER_INDUC_COND_REDUCTION detection
    
    After we introduced additional induction kinds we failed to make sure
    integer cond reductions are unaffected.
    
            PR tree-optimization/127600
            * tree-vect-loop.cc (vect_is_simple_iv_evolution): Explicitly
            initialize STMT_VINFO_LOOP_PHI_EVOLUTION_TYPE.
            (is_nonwrapping_integer_induction): Bail out for alternate
            induction types.
    
            * gcc.dg/vect/vect-cond-reduc-7.c: New testcase.

Diff:
---
 gcc/testsuite/gcc.dg/vect/vect-cond-reduc-7.c | 45 +++++++++++++++++++++++++++
 gcc/tree-vect-loop.cc                         |  7 ++++-
 2 files changed, 51 insertions(+), 1 deletion(-)

diff --git a/gcc/testsuite/gcc.dg/vect/vect-cond-reduc-7.c 
b/gcc/testsuite/gcc.dg/vect/vect-cond-reduc-7.c
new file mode 100644
index 000000000000..abaa3693aff1
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/vect-cond-reduc-7.c
@@ -0,0 +1,45 @@
+/* PR1276000 */
+
+#include "tree-vect.h"
+
+extern void abort (void) __attribute__ ((noreturn));
+
+int __attribute__((noipa))
+f (int *a, int n)
+{
+  int last = -99;
+  int j = 1;
+  for (int i = 0; i < n; i++)
+    {
+      if (a[i])
+        last = j;
+      j = -j;
+    }
+  return last;
+}
+
+int
+main (void)
+{
+  check_vect ();
+
+  int a[16] = { 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
+  if (f (a, 16) != 1)
+    abort ();
+
+  int b[16] = { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
+  if (f (b, 16) != -1)
+    abort ();
+
+  int c[16] = { 0 };
+  if (f (c, 16) != -99)
+    abort ();
+
+  int d[16] = { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
+  if (f (d, 16) != 1)
+    abort ();
+
+  return 0;
+}
+
+/* { dg-final { scan-tree-dump-not "condition expression based on integer 
induction." "vect" } } */
diff --git a/gcc/tree-vect-loop.cc b/gcc/tree-vect-loop.cc
index 99dda5b1e005..f692ead2bacc 100644
--- a/gcc/tree-vect-loop.cc
+++ b/gcc/tree-vect-loop.cc
@@ -198,6 +198,7 @@ vect_is_simple_iv_evolution (unsigned loop_nb, tree 
access_fn,
 
   STMT_VINFO_LOOP_PHI_EVOLUTION_BASE_UNCHANGED (stmt_info) = init_expr;
   STMT_VINFO_LOOP_PHI_EVOLUTION_PART (stmt_info) = step_expr;
+  STMT_VINFO_LOOP_PHI_EVOLUTION_TYPE (stmt_info) = vect_step_op_add;
 
   if (TREE_CODE (step_expr) != INTEGER_CST
       && (TREE_CODE (step_expr) != SSA_NAME
@@ -6592,7 +6593,7 @@ vectorize_fold_left_reduction (loop_vec_info loop_vinfo,
 
 /* Function is_nonwrapping_integer_induction.
 
-   Check if STMT_VINO (which is part of loop LOOP) both increments and
+   Check if STMT_VINFO (which is part of loop LOOP) both increments and
    does not cause overflow.  */
 
 static bool
@@ -6605,6 +6606,10 @@ is_nonwrapping_integer_induction (stmt_vec_info 
stmt_vinfo, class loop *loop)
   widest_int ni, max_loop_value, lhs_max;
   wi::overflow_type overflow = wi::OVF_NONE;
 
+  /* Make sure this is a regular induction.  */
+  if (STMT_VINFO_LOOP_PHI_EVOLUTION_TYPE (stmt_vinfo) != vect_step_op_add)
+    return false;
+
   /* Make sure the loop is integer based.  */
   if (TREE_CODE (base) != INTEGER_CST
       || TREE_CODE (step) != INTEGER_CST)

Reply via email to