[gcc r17-2750] tree-optimization/126404 - CTZ pattern destroying reductions

Richard Biener via Gcc-cvs <[email protected]>
Newsgroups gmane.comp.gcc.cvs
Message-ID <[email protected]>
https://gcc.gnu.org/g:a97f2ef0d90d46144b39a8c9ea670b52dd13233a

commit r17-2750-ga97f2ef0d90d46144b39a8c9ea670b52dd13233a
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.

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 e0fbe2521635..c57e215be577 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)
       {
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.