https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83171
Bug ID: 83171 Summary: std::bitset::count not inlining __popcountdi2 Product: gcc Version: 7.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: lectem at gmail dot com Target Milestone: --- Even if the size of the std::bitset is smaller than a long long, the compiler still emits a call to __popcountdi2 to count the number of bits set to 1. This is unnecessary for example if the size of the bitset is 8. I believe the issue lies with the fact that __popcountdi2 is not inlined/optimized, even when using -O3. Clang (-O2, using libstdc++) and MSVC(/O2, Visual 2017) seem to handle this correctly. Here is a link to the compiler explorer test : https://godbolt.org/g/QTWgdb The code used in the test : //------------------------------------------ #include <bitset> #include <stdint.h> size_t foo(uint32_t value) { std::bitset<8> bset = value; return bset.count(); } //------------------------------------------ gcc -v Target: x86_64-linux-gnu Configured with: ../gcc-7.2.0/configure --prefix /root/staging --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --disable-multilib --disable-bootstrap --disable-multiarch --with-arch-32=i586 --enable-clocale=gnu --enable-languages=c,c++,go,fortran --enable-ld=yes --enable-gold=yes --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-linker-build-id --enable-lto --enable-plugins --enable-threads=posix --with-pkgversion=GCC-Explorer-Build Thread model: posix gcc version 7.2.0 (GCC-Explorer-Build)