[gcc r17-2786] tree-optimization/126457 - robustify reduction analysis

Richard Biener via Gcc-cvs <[email protected]> Wed, 29 Jul 2026 11:33:23 +0000 (GMT)
Newsgroups gmane.comp.gcc.cvs
Message-ID <[email protected]>
https://gcc.gnu.org/g:3e6ac9b8d87704323df1f12214c29725c4e5eb9d

commit r17-2786-g3e6ac9b8d87704323df1f12214c29725c4e5eb9d
Author: Richard Biener <[email protected]>
Date:   Wed Jul 29 11:32:48 2026 +0200

    tree-optimization/126457 - robustify reduction analysis
    
    The following adds an early out for unsupported reduction operations
    to avoid ICEing when the assumption that the GIMPLE stmt operand
    number matches the SLP operand number breaks, as is for .CLZ with
    two operands.
    
            PR tree-optimization/126457
            * tree-vect-loop.cc (vectorizable_reduction): Reject
            operations where not all operands correspond to a SLP
            child early.
    
            * gcc.dg/vect/vect-pr126457.c: New testcase.

Diff:
---
 gcc/testsuite/gcc.dg/vect/vect-pr126457.c | 16 ++++++++++++++++
 gcc/tree-vect-loop.cc                     | 10 ++++++++++
 2 files changed, 26 insertions(+)

diff --git a/gcc/testsuite/gcc.dg/vect/vect-pr126457.c b/gcc/testsuite/gcc.dg/vect/vect-pr126457.c
new file mode 100644
index 000000000000..be441b304260
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/vect-pr126457.c
@@ -0,0 +1,16 @@
+/* { dg-do compile } */
+
+int g6, g24;
+_Bool f18_c6;
+void f18()
+{
+    int a1;
+    int bb16 = 607229;
+lbl_sw5:
+    bb16 = __builtin_clz(bb16);
+    f18_c6 = a1;
+    a1 = g24;
+    if (f18_c6) goto lbl_sw5;
+    g6 = __builtin_parity(bb16);
+    return;
+}
diff --git a/gcc/tree-vect-loop.cc b/gcc/tree-vect-loop.cc
index a9335ed68bbf..aff02a9ccae9 100644
--- a/gcc/tree-vect-loop.cc
+++ b/gcc/tree-vect-loop.cc
@@ -7208,6 +7208,16 @@ vectorizable_reduction (loop_vec_info loop_vinfo,
       return false;
     }
 
+  /* We'll verify the reduction operation only later - avoid
+     all operations that mismatch on the number of SLP children.  */
+  if (op.num_ops != SLP_TREE_CHILDREN (slp_for_stmt_info).length ())
+    {
+      if (dump_enabled_p ())
+	dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
+			 "unsupported reduction operation.\n");
+      return false;
+    }
+
   /* All uses but the last are expected to be defined in the loop.
      The last use is the reduction variable.  In case of nested cycle this
      assumption is not true: we use reduc_index to record the index of the