[gcc r16-9458] tree-optimization/126404 - CTZ pattern destroying reductions

Richard Biener via Gcc-cvs <[email protected]> Thu, 30 Jul 2026 08:39:27 +0000 (GMT)
Newsgroups gmane.comp.gcc.cvs
Message-ID <[email protected]>
https://gcc.gnu.org/g:762b68f15e6442aca835f1bb6da5e483f4ad0c1c

commit r16-9458-g762b68f15e6442aca835f1bb6da5e483f4ad0c1c
Author: Richard Biener <[email protected]>
Date:   Tue Jul 28 14:21:15 2026 +0200

    tree-optimization/126404 - CTZ pattern destroying reductions
    
    CTZ pattern recognition can recurse through a temporary pattern
    stmt but that will fail to be detected as part of a reduction
    because the setup is partial.  This causes the reduction chain
    to break due to multiple uses.  Avoid recursing in case the original
    pattern is for a reduction.
    
            PR tree-optimization/126404
            * tree-vect-patterns.cc (vect_recog_popcount_clz_ctz_ffs_pattern):
            Avoid recursing to vect_recog_ctz_ffs_pattern with a reduction.
    
            * gcc.dg/vect/vect-pr126404.c: Adjust.
    
    (cherry picked from commit a97f2ef0d90d46144b39a8c9ea670b52dd13233a)

Diff:
---
 gcc/testsuite/gcc.dg/vect/vect-pr126404.c | 1 +
 gcc/tree-vect-patterns.cc                 | 5 +++++
 2 files changed, 6 insertions(+)

diff --git a/gcc/testsuite/gcc.dg/vect/vect-pr126404.c b/gcc/testsuite/gcc.dg/vect/vect-pr126404.c
index 4657d34baf38..ea7187c0c447 100644
--- a/gcc/testsuite/gcc.dg/vect/vect-pr126404.c
+++ b/gcc/testsuite/gcc.dg/vect/vect-pr126404.c
@@ -1,4 +1,5 @@
 /* { dg-do compile } */
+/* { dg-additional-options "-march=x86-64-v4" { target { x86_64-*-* i?86-*-* } } } */
 
 unsigned ff(int g7)
 {
diff --git a/gcc/tree-vect-patterns.cc b/gcc/tree-vect-patterns.cc
index d1f36fd87c62..7c6a4722f4ee 100644
--- a/gcc/tree-vect-patterns.cc
+++ b/gcc/tree-vect-patterns.cc
@@ -2258,6 +2258,11 @@ vect_recog_popcount_clz_ctz_ffs_pattern (vec_info *vinfo,
 
   bool supported
     = direct_internal_fn_supported_p (ifn, vec_type, OPTIMIZE_FOR_SPEED);
+  /* We cannot rely on the reduction check in vect_recog_ctz_ffs_pattern
+     because we do not set up the temporary pattern stmt finally for
+     the recursive analysis.  */
+  if (!supported && vect_is_reduction (stmt_vinfo))
+    return NULL;
   if (!supported)
     switch (ifn)
       {