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

commit r17-6050-gde18b2843bc644849374f21228f332fc938cf56d
Author: Kyrylo Tkachov <[email protected]>
Date:   Tue Nov 3 06:26:23 2026 -0800

    aarch64: Pass unsigned arguments to .CLZ and .POPCOUNT for vclz and vcnt
    
    The vclz, vclzq, vcnt and vcntq intrinsics fold to .CLZ and .POPCOUNT.
    For the signed variants the argument of the call is a signed vector,
    but .CLZ and .POPCOUNT are defined only for unsigned arguments.  For
    
      int8x8_t f (int8x8_t a) { return vclz_s8 (a); }
    
    the fold gives
    
      _2 = .CLZ (a_1(D));
    
    View-convert a signed argument to the unsigned vector type.  The fold
    now gives
    
      _2 = VIEW_CONVERT_EXPR<vector(8) unsigned char>(a_1(D));
      _3 = .CLZ (_2);
    
    vcnt_s8 and vcntq_s8 give the same with .POPCOUNT.  The generated code
    does not change.
    
    Bootstrapped and tested on aarch64-none-linux-gnu and x86_64-linux.
    
    gcc/ChangeLog:
    
            * config/aarch64/aarch64-neon-builtins-base.cc (gimple_ifn::fold):
            View-convert a signed argument of .CLZ and .POPCOUNT to unsigned.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.target/aarch64/neon/bit_count_unsigned.c: New test.
    
    Signed-off-by: Kyrylo Tkachov <[email protected]>

Diff:
---
 gcc/config/aarch64/aarch64-neon-builtins-base.cc          |  5 +++++
 .../gcc.target/aarch64/neon/bit_count_unsigned.c          | 15 +++++++++++++++
 2 files changed, 20 insertions(+)

diff --git a/gcc/config/aarch64/aarch64-neon-builtins-base.cc 
b/gcc/config/aarch64/aarch64-neon-builtins-base.cc
index bd9ae6dc2897..a20490775a99 100644
--- a/gcc/config/aarch64/aarch64-neon-builtins-base.cc
+++ b/gcc/config/aarch64/aarch64-neon-builtins-base.cc
@@ -659,6 +659,11 @@ public:
        /* Convert V1DI/V1DF arguments to corresponding scalar modes.  */
        if (scalar_lhs && one_element_vector_type_p (TREE_TYPE (arg)))
          arg = f.force_val (build_cast (TREE_TYPE (lhs_type), arg));
+       /* .CLZ and .POPCOUNT take an unsigned argument.  */
+       else if ((m_ifn == IFN_CLZ || m_ifn == IFN_POPCOUNT)
+                && !TYPE_UNSIGNED (TREE_TYPE (arg)))
+         arg = f.force_val (build_cast (unsigned_type_for (TREE_TYPE (arg)),
+                                        arg));
        args.safe_push (arg);
       }
 
diff --git a/gcc/testsuite/gcc.target/aarch64/neon/bit_count_unsigned.c 
b/gcc/testsuite/gcc.target/aarch64/neon/bit_count_unsigned.c
new file mode 100644
index 000000000000..d7164b75c7ad
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/neon/bit_count_unsigned.c
@@ -0,0 +1,15 @@
+/* { dg-do compile } */
+/* { dg-additional-options "-O2 -fdump-tree-optimized" } */
+
+/* The signed vcnt and vclz intrinsics count an unsigned vector.  */
+
+#include <arm_neon.h>
+
+int8x8_t f1 (int8x8_t a) { return vcnt_s8 (a); }
+int8x16_t f2 (int8x16_t a) { return vcntq_s8 (a); }
+int8x8_t f3 (int8x8_t a) { return vclz_s8 (a); }
+int32x4_t f4 (int32x4_t a) { return vclzq_s32 (a); }
+
+/* { dg-final { scan-tree-dump-times {\.POPCOUNT \(_} 2 "optimized" } } */
+/* { dg-final { scan-tree-dump-times {\.CLZ \(_} 2 "optimized" } } */
+/* { dg-final { scan-tree-dump-not {\.(POPCOUNT|CLZ) \(a_} "optimized" } } */

Reply via email to