https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128459
--- Comment #1 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The master branch has been updated by Jakub Jelinek <[email protected]>: https://gcc.gnu.org/g:9e45414bb92937ce7b9495ecbf437de3b9ba5abf commit r17-6716-g9e45414bb92937ce7b9495ecbf437de3b9ba5abf Author: Jakub Jelinek <[email protected]> Date: Thu Nov 26 09:20:21 2026 +0100 c, c++, middle-end, i386, libstdc++: Introduce __builtin_bit{compress,expand}g builtins [PR128459] 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). 2026-11-26 Jakub Jelinek <[email protected]> PR middle-end/128459 * doc/extend.texi (__builtin_bitcompressg, __builtin_bitexpandg): Document. * doc/ifn.texi (IFN_BSWAP, IFN_BITREVERSE, IFN_CLMUL, IFN_CLMUL_WIDEN, IFN_BITCOMPRESS, IFN_BITEXPAND): Document. * doc/md.texi (bitcompress@var{m}3, bitexpand@var{m}3): 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. Reviewed-by: Richard Biener <[email protected]> Reviewed-by: Uros Bizjak <[email protected]> Reviewed-by: Jonathan Wakely <[email protected]> Reviewed-by: Jason Merrill <[email protected]> Reviewed-by: Joseph Myers <[email protected]>
