https://gcc.gnu.org/g:2cb9925f4001b931f43ea64b64adf9dbc1089bfa

commit r16-4465-g2cb9925f4001b931f43ea64b64adf9dbc1089bfa
Author: Richard Biener <[email protected]>
Date:   Fri Oct 17 09:26:25 2025 +0200

    tree-optimization/122301 - fix ICE and improve vectorization of min/max 
reduction
    
    The following fixes another issue with updating of reduc_idx in pattern
    sequences.  But the testcase also shows the pattern in question is
    harmful for vectorization since a reduction path may not contain
    promotions/demotions.  So the already existing but ineffective check
    to guard the pattern is fixed.
    
            PR tree-optimization/122301
            * tree-vect-patterns.cc (vect_recog_over_widening_pattern):
            Fix reduction guard.
            (vect_mark_pattern_stmts): Fix reduction def check.
    
            * gcc.dg/vect/vect-pr122301.c: New testcase.

Diff:
---
 gcc/testsuite/gcc.dg/vect/vect-pr122301.c | 16 ++++++++++++++++
 gcc/tree-vect-patterns.cc                 |  9 ++++++---
 2 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/gcc/testsuite/gcc.dg/vect/vect-pr122301.c 
b/gcc/testsuite/gcc.dg/vect/vect-pr122301.c
new file mode 100644
index 000000000000..acc7aedf1902
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/vect-pr122301.c
@@ -0,0 +1,16 @@
+/* { dg-do compile } */
+
+int get_prev_frame_segid(unsigned char *p, int n)
+{
+  int tem;
+  unsigned seg_id = 8;
+  for (int x = 0; x < n; x++)
+    {
+      int a = seg_id;
+      tem = a < p[x] ? a : p[x];
+      seg_id = tem;
+    }
+  return tem;
+}
+
+/* { dg-final { scan-tree-dump "optimized: loop vectorized" "vect" { target { 
vect_int && { ! vect_no_int_min_max } } } } } */
diff --git a/gcc/tree-vect-patterns.cc b/gcc/tree-vect-patterns.cc
index db30db7b8a5c..a5c721e61535 100644
--- a/gcc/tree-vect-patterns.cc
+++ b/gcc/tree-vect-patterns.cc
@@ -3001,7 +3001,7 @@ vect_recog_over_widening_pattern (vec_info *vinfo,
   tree_code code = gimple_assign_rhs_code (last_stmt);
 
   /* Punt for reductions where we don't handle the type conversions.  */
-  if (STMT_VINFO_DEF_TYPE (last_stmt_info) == vect_reduction_def)
+  if (vect_is_reduction (last_stmt_info))
     return NULL;
 
   /* Keep the first operand of a COND_EXPR as-is: only the other two
@@ -7517,14 +7517,17 @@ vect_mark_pattern_stmts (vec_info *vinfo,
                    break;
                  }
              /* Try harder to find a mid-entry into an earlier pattern
-                sequence.  This means that the initial 'lookfor' was
+                sequence.  Likewise an entry to a stmt skipping a conversion
+                on an input.  This means that the initial 'lookfor' was
                 bogus.  */
              if (!found)
                {
                  for (unsigned i = 0; i < op.num_ops; ++i)
                    if (TREE_CODE (op.ops[i]) == SSA_NAME)
                      if (auto def = vinfo->lookup_def (op.ops[i]))
-                       if (vect_is_reduction (def))
+                       if (vect_is_reduction (def)
+                           || (is_a <gphi *> (def->stmt)
+                               && STMT_VINFO_REDUC_DEF (def) != NULL))
                          {
                            STMT_VINFO_REDUC_IDX (vinfo->lookup_stmt (s)) = i;
                            lookfor = gimple_get_lhs (s);

Reply via email to