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

commit r17-5474-gd4b9bcaca3f490f777b7815a396df434ba027d9b
Author: Avinash Jayakar <[email protected]>
Date:   Fri Dec 5 15:12:52 2025 +0530

    rs6000: Emit better code for parity in DImode [PR119468]
    
    This patch aims to fix PR119468 by emitting better code for parity
    builtin. Currently the following code:
    
    bool parity(unsigned long long l)
    {
        return __builtin_parityll(l);
    }
    with O2 flag and even if the target supports popcntd emits,
      popcntb 3,3
      prtyd 3,3
      extsw 3,3
    
    With this patch, if there is support for popcntd (2.06) instruction, we
    emit
      popcntd 3,3
      rldicl 3,3,0,63
    Making use of the existing instruction and reducing the latency compared
    to existing code generated.
    
    2026-10-22  Avinash Jayakar  <[email protected]>
    
    gcc/ChangeLog:
            PR target/119468
            * config/rs6000/rs6000.cc (rs6000_emit_parity): Add code to generate
            popcntd if available.
    
    gcc/testsuite/ChangeLog:
            PR target/119468
            * gcc.target/powerpc/pr119468.c: New test.
    
    Reviewed-by: Michael Meissner <[email protected]>

Diff:
---
 gcc/config/rs6000/rs6000.cc                 |  8 ++++++++
 gcc/testsuite/gcc.target/powerpc/pr119468.c | 16 ++++++++++++++++
 2 files changed, 24 insertions(+)

diff --git a/gcc/config/rs6000/rs6000.cc b/gcc/config/rs6000/rs6000.cc
index a819b2ece9e5..2002abb96bac 100644
--- a/gcc/config/rs6000/rs6000.cc
+++ b/gcc/config/rs6000/rs6000.cc
@@ -23454,6 +23454,14 @@ rs6000_emit_parity (rtx dst, rtx src)
 
   tmp = gen_reg_rtx (mode);
 
+  /* Use ISA 2.06 insn popcntd if we can. */
+  if (mode == DImode && TARGET_POPCNTD)
+    {
+      emit_insn (gen_popcntddi2 (tmp, src));
+      emit_insn (gen_anddi3 (dst, tmp, const1_rtx));
+      return;
+    }
+
   /* Use the PPC ISA 2.05 prtyw/prtyd instruction if we can.  */
   if (TARGET_CMPB)
     {
diff --git a/gcc/testsuite/gcc.target/powerpc/pr119468.c 
b/gcc/testsuite/gcc.target/powerpc/pr119468.c
new file mode 100644
index 000000000000..f43319210b7c
--- /dev/null
+++ b/gcc/testsuite/gcc.target/powerpc/pr119468.c
@@ -0,0 +1,16 @@
+/* { dg-do compile { target { powerpc*-*-* && lp64 } } } */
+/* { dg-options "-O2 -mdejagnu-cpu=power7" } */
+
+bool foo(unsigned long long x)
+{
+  return __builtin_parityll(x);
+}
+
+bool parity2(unsigned long long l)
+{
+  return __builtin_popcountll(l) & 1;
+}
+
+/* { dg-final { scan-assembler-times "popcntd" 2 } } */
+/* { dg-final { scan-assembler-not "popcntb" } } */
+/* { dg-final { scan-assembler-times "rldicl" 2 } } */

Reply via email to