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.
This commit is contained in:
Richard Biener 2025-10-17 09:26:25 +02:00
parent 6883d51304
commit 2cb9925f40
2 changed files with 22 additions and 3 deletions

View File

@ -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 } } } } } */

View File

@ -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);