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

commit r16-3428-gfa03e49399a8b202b7d6700604c8fbe5ee33474f
Author: Richard Biener <rguent...@suse.de>
Date:   Wed Aug 27 14:40:37 2025 +0200

    Avoid mult pattern if that will break reduction constraints
    
    synth-mult introduces multiple uses of a reduction variable
    in some cases which will ultimatively fail vectorization (or ICE
    with a pending change).  So avoid applying the pattern in such case.
    
            * tree-vect-patterns.cc (vect_synth_mult_by_constant): Avoid
            in cases that introduce multiple uses of reduction operands.
    
    Co-authored-by: Jakub Jelinek <ja...@redhat.com>

Diff:
---
 gcc/tree-vect-patterns.cc | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/gcc/tree-vect-patterns.cc b/gcc/tree-vect-patterns.cc
index 3fffcac4b3aa..65704e5f338c 100644
--- a/gcc/tree-vect-patterns.cc
+++ b/gcc/tree-vect-patterns.cc
@@ -4314,6 +4314,35 @@ vect_synth_mult_by_constant (vec_info *vinfo, tree op, 
tree val,
   if (!possible)
     return NULL;
 
+  if (vect_is_reduction (stmt_vinfo))
+    {
+      int op_uses = alg.op[0] != alg_zero;
+      for (int i = 1; i < alg.ops; i++)
+       switch (alg.op[i])
+         {
+         case alg_add_t_m2:
+         case alg_sub_t_m2:
+           if (synth_shift_p && alg.log[i])
+             return NULL;
+           else
+             op_uses++;
+           break;
+         case alg_add_t2_m:
+         case alg_sub_t2_m:
+           op_uses++;
+           break;
+         default:
+           break;
+         }
+      if (variant == add_variant)
+       op_uses++;
+      /* When we'll synthesize more than a single use of the reduction
+        operand the reduction constraints are violated.  Avoid this
+        situation.  */
+      if (op_uses > 1)
+       return NULL;
+    }
+
   if (!target_supports_mult_synth_alg (&alg, variant, vectype, synth_shift_p))
     return NULL;

Reply via email to