[libcxx-commits] [libcxx] [libc++] Use _BitInt and __builtin_popcountg in bitset::count() (PR #160679)

via libcxx-commits libcxx-commits at lists.llvm.org
Thu Sep 25 05:22:32 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: Nikolas Klauser (philnik777)

<details>
<summary>Changes</summary>

This has multiple benefits:
1) The compiler has to do way less work to figure out things fold into a simple `popcount`, improving compile times quite a bit
2) The compiler inlines better, since the compile doesn't have to do complicated optimizations to get to the same point. Looking at the pipeline, it seems that without this, LLVM has to go all the way to GVN to get to the same code as there is after the first InstCombine pass with this change.

Currently this applies only to `bitset`s with at most 64 bits, but that is by far the most common case.

---
Full diff: https://github.com/llvm/llvm-project/pull/160679.diff


1 Files Affected:

- (modified) libcxx/include/bitset (+8-1) 


``````````diff
diff --git a/libcxx/include/bitset b/libcxx/include/bitset
index e2b46154ae730..d8bb938456104 100644
--- a/libcxx/include/bitset
+++ b/libcxx/include/bitset
@@ -867,7 +867,14 @@ bitset<_Size>::to_string(char __zero, char __one) const {
 
 template <size_t _Size>
 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 size_t bitset<_Size>::count() const _NOEXCEPT {
-  return static_cast<size_t>(std::count(__base::__make_iter(0), __base::__make_iter(_Size), true));
+#ifdef _LIBCPP_COMPILER_CLANG_BASED
+  if constexpr (_Size <= __base::__bits_per_word) {
+    return __builtin_popcountg(static_cast<unsigned _BitInt(_Size)>(__base::__first_));
+  } else
+#endif
+  {
+    return static_cast<size_t>(std::count(__base::__make_iter(0), __base::__make_iter(_Size), true));
+  }
 }
 
 template <size_t _Size>

``````````

</details>


https://github.com/llvm/llvm-project/pull/160679


More information about the libcxx-commits mailing list