[PATCH] c, c++, middle-end, i386, libstdc++: Introduce __builtin_bit{compress,expand}g builtins [PR128459]

Jonathan Wakely jwakely@redhat.com
Mon Nov 9 12:22:56 GMT 2026


On Thu, 5 Nov 2026 at 13:58, Jakub Jelinek <jakub@redhat.com> wrote:
>
> Hi!
>
> The following patch on top of the __builtin_clmulg patch
> https://gcc.gnu.org/pipermail/gcc-patches/2026-October/736969.html
> implements builtins to optimize the recently added std::bit_compress
> and std::bit_expand function templates.
> Similarly to the reasons for __builtin_clmulg, various targets have
> instructions to support this (x86 BMI2 pext/pdep, SVE2 has bext/bdep,
> RISC-V has something, ...) and even when not, e.g. some parts of
> the code can be optimized using __builtin_clmulg if implemented
> in hardware (otherwise it would be pessimization rather than optimization).
>
> So far lightly tested on the new tests and libstdc++ bit.permute tests,
> ok for trunk if it passes full bootstrap/regtest?
>
> 2026-11-05  Jakub Jelinek  <jakub@redhat.com>
>
>         PR middle-end/128459
>         * doc/extend.texi (__builtin_bitcompressg, __builtin_bitexpandg):
>         Document.
>         * builtins.h (fold_build_builtin_bitcompressg_bitexpandg): Declare.
>         * fold-const-call.cc (fold_const_call_sss): Handle IFN_BITCOMPRESS
>         and IFN_BITEXPAND.
>         * internal-fn.h (expand_BITCOMPRESS, expand_BITEXPAND): Declare.
>         * fold-const.cc (tree_call_nonnegative_p): Handle IFN_BITCOMPRESS
>         and IFN_BITEXPAND.
>         * wide-int.cc (wi::bit_compress, wi::bit_expand): New functions.
>         * wide-int.h (wi::bit_compress, wi::bit_expand): Declare.
>         * builtins.cc (fold_build_builtin_bitcompressg_bitexpandg): New
>         function.
>         * internal-fn.def (BITCOMPRESS, BITEXPAND): New internal functions.
>         * internal-fn.cc (expand_BITCOMPRESS, expand_BITEXPAND): New functions.
>         * optabs.def (bitcompress_optab, bitexpand_optab): New optabs.
>         * gimple-lower-bitint.cc (bitint_large_huge::lower_stmt): Handle
>         store temporarily without vdef in SSA form.
>         (lower_bitcompress_bitexpand): New function.
>         (gimple_lower_bitint): Lower IFN_BITCOMPRESS and IFN_BITEXPAND calls
>         before lowering.
>         * match.pd (.BITCOMPRESS, .BITEXPAND): New simplifications.
>         * config/i386/i386.md (bitexpand<mode>3, bitcompress<mode>3): New
>         BMI2 define_expand patterns.
> gcc/c-family/
>         * c-common.h (enum rid): Add RID_BUILTIN_BITCOMPRESSG and
>         RID_BUILTIN_BITEXPANDG enumerators.
>         * c-common.cc (c_common_reswords): Add __builtin_bitcompressg and
>         __builtin_bitexpandg entries.
> gcc/c/
>         * c-parser.cc (c_parser_postfix_expression): Handle
>         RID_BUILTIN_BITCOMPRESSG and RID_BUILTIN_BITEXPANDG.
>         * c-decl.cc (names_builtin_p): Likewise.
> gcc/cp/
>         * parser.cc (cp_parser_postfix_expression): Handle
>         RID_BUILTIN_BITCOMPRESSG and RID_BUILTIN_BITEXPANDG.
>         * typeck.cc (build_x_bitcompressg_bitexpandg): New function.
>         * cp-tree.h (build_x_bitcompressg_bitexpandg): Declare.
>         * pt.cc (tsubst_expr): Handle IFN_BITCOMPRESS and IFN_BITEXPAND.
>         * mangle.cc (write_expression): Likewise.
>         * cp-objcp-common.cc (names_builtin_p): Handle
>         RID_BUILTIN_BITCOMPRESSG and RID_BUILTIN_BITEXPANDG.
>         * cp-gimplify.cc (cp_gimplify_expr): Handle IFN_BITCOMPRESS and
>         IFN_BITEXPAND.
>         * constexpr.cc (cxx_eval_internal_function): Likewise.
>         (potential_constant_expression_1): Likewise.
> gcc/testsuite/
>         * c-c++-common/builtin-bitcompressg-1.c: New test.
>         * c-c++-common/builtin-bitcompressg-2.c: New test.
>         * c-c++-common/builtin-bitexpandg-1.c: New test.
>         * c-c++-common/builtin-bitexpandg-2.c: New test.
>         * gcc.target/i386/bmi2-builtin-bitcompressg-1.c: New test.
>         * gcc.target/i386/bmi2-builtin-bitcompressg-2.c: New test.
>         * gcc.target/i386/bmi2-builtin-bitexpandg-1.c: New test.
>         * gcc.target/i386/bmi2-builtin-bitexpandg-2.c: New test.
>         * gcc.target/i386/pclmul-builtin-bitcompressg-1.c: New test.
>         * gcc.target/i386/pclmul-builtin-bitexpandg-1.c: New test.
> libstdc++-v3/
>         * include/std/bit (std::bit_compress): Use __builtin_bitcompressg
>         if available.
>         (std::bit_expand): Use __builtin_bitexpandg if available.
>


The library part is OK



More information about the Libstdc++ mailing list