https://gcc.gnu.org/g:39a6434dcc6d7af501459d8a6104c3ea22e3acc4

commit r17-5428-g39a6434dcc6d7af501459d8a6104c3ea22e3acc4
Author: Reshma Roy <[email protected]>
Date:   Thu Oct 8 11:15:57 2026 +0530

    Fix incorrect identification of POPCOUNT pattern [PR127748]
    
    r17-489-g8ca1e887847e2f captured the low half of the final add as
    @step_4 and the shifted temporary as @step4.  Those are different
    captures, so (other & 0xffff) + (x >> 16) was folded to
    .POPCOUNT(x) and the other operand was dropped. Use @step4 for
    both operands so the masked value and the shifted value must
    be the same temporary.
    
    gcc/ChangeLog:
            PR tree-optimization/127748
            * match.pd: Fix incorrect POPCOUNT identification in the first 
32-bit
            Hacker's Delight matcher.
    
    gcc/testsuite/ChangeLog:
            * gcc.dg/tree-ssa/popcount11.c: New test.

Diff:
---
 gcc/match.pd                               |  2 +-
 gcc/testsuite/gcc.dg/tree-ssa/popcount11.c | 15 +++++++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/gcc/match.pd b/gcc/match.pd
index 2bb64104aa38..79e7d774da45 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -12005,7 +12005,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 
   (simplify
      (plus:c
-       (bit_and @step_4 INTEGER_CST@9)
+       (bit_and @step4 INTEGER_CST@9)
        (rshift
         (plus:c@step4
           (bit_and @step3 INTEGER_CST@7)
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/popcount11.c 
b/gcc/testsuite/gcc.dg/tree-ssa/popcount11.c
new file mode 100644
index 000000000000..ad4dc7d21ac5
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/popcount11.c
@@ -0,0 +1,15 @@
+/* { dg-do compile } */
+/* { dg-require-effective-target popcount } */
+/* { dg-require-effective-target int32plus } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+unsigned neg(unsigned uword, unsigned other)
+{
+  uword = (uword & 0x55555555u) + ((uword >> 1) & 0x55555555u);
+  uword = (uword & 0x33333333u) + ((uword >> 2) & 0x33333333u);
+  uword = (uword & 0x0f0f0f0fu) + ((uword >> 4) & 0x0f0f0f0fu);
+  uword = (uword & 0x00ff00ffu) + ((uword >> 8) & 0x00ff00ffu);
+  return (other & 0x0000ffffu) + (uword >> 16);
+}
+
+/* { dg-final { scan-tree-dump-not "\.POPCOUNT" "optimized" } } */

Reply via email to