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

commit r17-6556-gfd0911a7aace8da5d62e3530ec0fed0ed25d079e
Author: Shivam Gupta <[email protected]>
Date:   Sat Nov 21 08:07:30 2026 -0700

    [PATCH v3] match.pd: Narrow popcount when operand fits in a narrower type 
[PR125221]
    
    If the value range of a popcount argument shows that it fits in a
    narrower unsigned type, convert the argument to that type and call the
    popcount function for it.  This lets the target use a cheaper popcount,
    for example a 32-bit one for a 64-bit value that is known to be below
    2^32.
    
    The function is chosen from the narrowed type: IFN_POPCOUNT if the
    target supports it for that type, otherwise __builtin_popcount
    (32 bits) or __builtin_popcountll (64 bits).  The builtins are used
    only when the original type is wider than a word and has no popcount of
    its own, since libgcc has no popcount routine narrower than a word on
    most targets.  Wide arguments such as __int128 and _BitInt are
    narrowed to 32, 64 or 128 bits, up to MAX_FIXED_MODE_SIZE.
    
    Changes since v2:
    * Restrict the __builtin_popcount / __builtin_popcountll fallback to
      types wider than a word that have no popcount of their own, as in
      popcount (zext (X)) (addressed sashiko LLM review comment -
      20261115121407.2749144-1-Shivam98.tkg%40gmail.com)
    * Limit popcountll-narrow.c to targets with a popcount optab (aarch64,
      and x86 with -mpopcnt), and scan only for .POPCOUNT.
    * Limit popcountg-narrow-bitint.c to 64-bit aarch64 and x86_64.
    
    Changes since v1:
    * Fix the narrowed call.  v1 reused the matched variant, so
      __builtin_popcountll could receive an unsigned int.  v2 picks
      IFN_POPCOUNT, __builtin_popcount or __builtin_popcountll from the
      narrowed type, as popcount (zext (X)) does.
    * Also narrow to 64 and 128 bits where the target has an
      IFN_POPCOUNT for that type, covering __int128 and _BitInt.
    * Tests: accept both __builtin_popcount and .POPCOUNT, and add
      a scan that no narrowed value reaches __builtin_popcountll. Add
      _BitInt and aarch64 128-bit tests.
    
    Bootstrapped and tested on x86_64-linux-gnu.
    
            PR tree-optimization/125221
    
    gcc/ChangeLog:
    
            * gimple-match-head.cc: Include vr-values.h.
            * match.pd (POPCOUNT): Narrow the argument when its value range
            fits in a narrower unsigned type.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.dg/popcountll-narrow.c: New test.
            * gcc.dg/popcountg-narrow-bitint.c: New test.
            * gcc.target/aarch64/popcountg-narrow-128.c: New test.
    
    Signed-off-by: Shivam Gupta <[email protected]>

Diff:
---
 gcc/gimple-match-head.cc                           |  1 +
 gcc/match.pd                                       | 48 ++++++++++++++
 gcc/testsuite/gcc.dg/popcountg-narrow-bitint.c     | 33 ++++++++++
 gcc/testsuite/gcc.dg/popcountll-narrow.c           | 74 ++++++++++++++++++++++
 .../gcc.target/aarch64/popcountg-narrow-128.c      | 40 ++++++++++++
 5 files changed, 196 insertions(+)

diff --git a/gcc/gimple-match-head.cc b/gcc/gimple-match-head.cc
index c6a0c74eabb7..d079fa2bd4e1 100644
--- a/gcc/gimple-match-head.cc
+++ b/gcc/gimple-match-head.cc
@@ -51,6 +51,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "langhooks.h"
 #include "attribs.h"
 #include "asan.h"
+#include "vr-values.h"
 
 tree do_valueize (tree, tree (*)(tree), bool &);
 tree do_valueize (tree (*)(tree), tree);
diff --git a/gcc/match.pd b/gcc/match.pd
index 8c8cf93e17b0..91dde5d51fae 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -11951,6 +11951,54 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
       (if (cfn == CFN_BUILT_IN_POPCOUNTLL)
        (BUILT_IN_POPCOUNTLL (convert:type0 @0))))))))
 
+/* popcount (X) -> popcount ((narrower unsigned) X) when the range of X
+   fits the narrower type.  */
+(simplify
+ (POPCOUNT @0)
+ (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
+      && TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (unsigned_type_node))
+  (with { combined_fn cfn = CFN_LAST;
+         tree type0 = NULL_TREE;
+         int_range_max vr0;
+         if (gimple_match_range_of_expr (vr0, @0))
+           for (unsigned prec = TYPE_PRECISION (unsigned_type_node);
+                prec < TYPE_PRECISION (TREE_TYPE (@0))
+                && prec <= MAX_FIXED_MODE_SIZE;
+                prec *= 2)
+             if (range_fits_type_p (&vr0, prec, UNSIGNED))
+               {
+                 type0 = build_nonstandard_integer_type (prec, 1);
+                 break;
+               }
+         if (type0
+             && direct_internal_fn_supported_p (IFN_POPCOUNT, type0,
+                                                OPTIMIZE_FOR_BOTH))
+           cfn = CFN_POPCOUNT;
+         if (type0 && cfn == CFN_LAST
+             && TYPE_PRECISION (TREE_TYPE (@0)) > BITS_PER_WORD
+             && !direct_internal_fn_supported_p (IFN_POPCOUNT, TREE_TYPE (@0),
+                                                 OPTIMIZE_FOR_BOTH))
+           {
+             if (TYPE_PRECISION (type0)
+                 == TYPE_PRECISION (unsigned_type_node))
+               {
+                 cfn = CFN_BUILT_IN_POPCOUNT;
+                 type0 = unsigned_type_node;
+               }
+             else if (TYPE_PRECISION (type0)
+                      == TYPE_PRECISION (long_long_unsigned_type_node))
+               {
+                 cfn = CFN_BUILT_IN_POPCOUNTLL;
+                 type0 = long_long_unsigned_type_node;
+               }
+           } }
+   (if (cfn == CFN_POPCOUNT)
+    (IFN_POPCOUNT (convert:type0 @0))
+    (if (cfn == CFN_BUILT_IN_POPCOUNT)
+     (BUILT_IN_POPCOUNT (convert:type0 @0))
+     (if (cfn == CFN_BUILT_IN_POPCOUNTLL)
+      (BUILT_IN_POPCOUNTLL (convert:type0 @0))))))))
+
 /* popcount (X ^ (X - 1)) is the same as ffs(x) when x is nonzero, and
    using ctz+1 will generate better code.  */
 (simplify
diff --git a/gcc/testsuite/gcc.dg/popcountg-narrow-bitint.c 
b/gcc/testsuite/gcc.dg/popcountg-narrow-bitint.c
new file mode 100644
index 000000000000..b576302a3a4c
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/popcountg-narrow-bitint.c
@@ -0,0 +1,33 @@
+/* { dg-do compile { target { { aarch64*-*-* x86_64-*-* } && { bitint575 && 
lp64 } } } } */
+/* { dg-options "-Os -fdump-tree-optimized" } */
+/* { dg-additional-options "-mpopcnt" { target { x86_64-*-* } } } */
+
+/* Narrowed to 32 bits: large _BitInt known to fit in 32 bits.  */
+int f1 (unsigned _BitInt(256) x)
+{
+  if (x > 0xFFFFFFFFu)
+    __builtin_unreachable ();
+  return __builtin_popcountg (x);
+}
+
+/* Narrowed to 64 bits: large _BitInt known to fit in 64 bits.  */
+int f2 (unsigned _BitInt(256) x)
+{
+  if (x > 0xFFFFFFFFFFFFFFFFull)
+    __builtin_unreachable ();
+  return __builtin_popcountg (x);
+}
+
+/* Narrowed to 32 bits: _BitInt(100) known to fit in 32 bits.  */
+int f3 (unsigned _BitInt(100) x)
+{
+  if (x > 0xFFFFFFFFu)
+    __builtin_unreachable ();
+  return __builtin_popcountg (x);
+}
+
+/* All three calls take a narrowed temporary.  */
+/* { dg-final { scan-tree-dump-times {\.POPCOUNT \(_[0-9]+\)} 3 "optimized" } 
} */
+
+/* f1 and f3 narrow to 32 bits; f2 stays 64-bit.  */
+/* { dg-final { scan-tree-dump-times {_[0-9]+ = \(unsigned int\) } 2 
"optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/popcountll-narrow.c 
b/gcc/testsuite/gcc.dg/popcountll-narrow.c
new file mode 100644
index 000000000000..58abf7282091
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/popcountll-narrow.c
@@ -0,0 +1,74 @@
+/* { dg-do compile { target { aarch64*-*-* x86_64-*-* } } } */
+/* { dg-options "-Os -fdump-tree-optimized" } */
+/* { dg-additional-options "-mpopcnt" { target { x86_64-*-* } } } */
+/* { dg-require-effective-target int32plus } */
+
+/* Narrowed: masked value fits in 32 bits.  */
+int func1(unsigned long long x) {
+    return __builtin_popcountll(x & 0x87878787);
+}
+
+/* Narrowed: x >> 32 leaves only 32 bits.  */
+int func2(unsigned long long x) {
+    return __builtin_popcountll(x >> 32);
+}
+
+/* Narrowed: constant shifted right stays below 2^32.  */
+int func3(unsigned char x) {
+    if (x >= 64) {
+      __builtin_unreachable();
+    }
+    return __builtin_popcountll(0x87878787ULL >> x);
+}
+
+/* Narrowed: remainder by a 32-bit constant is below 2^32.  */
+int func4(unsigned long long x) {
+    return __builtin_popcountll(x % 4294967291U);
+}
+
+/* Narrowed: quotient by a constant above 2^32 is below 2^32.  */
+int func5(unsigned long long x) {
+    return __builtin_popcountll(x / 4294967311U);
+}
+
+/* Narrowed: __builtin_unreachable limits x to 32 bits.  */
+int func6(unsigned long long x) {
+    if (x > 0xFFFFFFFFULL) {
+      __builtin_unreachable();
+    }
+    return __builtin_popcountll(x);
+}
+
+/* Narrowed: signed x known to be in [0, 0xFFFFFFFF].  */
+int func7(long long x) {
+    if (x < 0 || x > 0xFFFFFFFFLL)
+      __builtin_unreachable();
+    return __builtin_popcountll(x);
+}
+
+/* Not narrowed: x is in [0, LLONG_MAX], which needs 63 bits.  */
+int func8(long long x) {
+    if (x < 0)
+      __builtin_unreachable();
+    return __builtin_popcountll(x);
+}
+
+/* Not narrowed: no range information.  */
+int func9(unsigned long long x) {
+    return __builtin_popcountll(x);
+}
+
+/* Not narrowed: x <= 2^32 needs 33 bits.  */
+int func10(unsigned long long x) {
+    if (x > 0x100000000ULL)
+      __builtin_unreachable();
+    return __builtin_popcountll(x);
+}
+
+/* The 7 narrowable cases (func1-func7) call a 32-bit .POPCOUNT.  */
+/* { dg-final { scan-tree-dump-times {\.POPCOUNT \(_[0-9]+\)} 7 "optimized" } 
} */
+
+/* Only func8, func9 and func10 keep the long long builtin.  */
+/* { dg-final { scan-tree-dump-times {__builtin_popcountll \(} 3 "optimized" } 
} */
+
+/* { dg-final { scan-tree-dump-not {__builtin_popcountll \(_[0-9]+\)} 
"optimized" } } */
diff --git a/gcc/testsuite/gcc.target/aarch64/popcountg-narrow-128.c 
b/gcc/testsuite/gcc.target/aarch64/popcountg-narrow-128.c
new file mode 100644
index 000000000000..458771716157
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/popcountg-narrow-128.c
@@ -0,0 +1,40 @@
+/* { dg-do compile } */
+/* { dg-options "-Os -fdump-tree-optimized" } */
+
+typedef unsigned __int128 u128;
+
+/* Narrowed to unsigned int: x fits in 32 bits.  */
+int f1 (u128 x)
+{
+  if (x > 0xFFFFFFFFu)
+    __builtin_unreachable ();
+  return __builtin_popcountg (x);
+}
+
+/* Narrowed to unsigned long long: x fits in 64 bits but not 32.  */
+int f2 (u128 x)
+{
+  if (x > 0xFFFFFFFFFFFFFFFFull)
+    __builtin_unreachable ();
+  return __builtin_popcountg (x);
+}
+
+/* Not narrowed: no range information.  */
+int f3 (u128 x)
+{
+  return __builtin_popcountg (x);
+}
+
+/* Not narrowed: x <= 2^64 needs 65 bits.  */
+int f4 (u128 x)
+{
+  if (x > ((u128) 1 << 64))
+    __builtin_unreachable ();
+  return __builtin_popcountg (x);
+}
+
+/* f1 and f2 are narrowed.  */
+/* { dg-final { scan-tree-dump-times {\.POPCOUNT \(_[0-9]+\)} 2 "optimized" } 
} */
+
+/* f3 and f4 keep the 128-bit argument.  */
+/* { dg-final { scan-tree-dump-times {\.POPCOUNT \(x_} 2 "optimized" } } */

Reply via email to