[Bug c/122731] Adding __builtin_bswapg for all types that is a multiple of 2 bytes (16 bits)
cvs-commit at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Fri Jun 5 08:41:36 GMT 2026
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122731
--- Comment #2 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:
https://gcc.gnu.org/g:9ab08d664545bcea9164040e806385369fdaaf0e
commit r17-1367-g9ab08d664545bcea9164040e806385369fdaaf0e
Author: Jakub Jelinek <jakub@redhat.com>
Date: Fri Jun 5 10:39:55 2026 +0200
c, c++, bitintlower: Add __builtin_{bswap,bitreverse}g type-generic
builtins [PR122731]
Clang has added recently type-generic versions of __builtin_bswap{16,32,64}
and __builtin_bitreverse{8,16,32,64} builtins.
The following patch adds them to GCC as well.
Unlike the other __builtin_*g type-generic builtins, these are different in
that the return type is not fixed (usually int or unsigned int), but is the
same as the argument type, so these can't be handled as normal builtins
and are handled in the C and C++ parsers instead.
For consistency with the other __builtin_*g builtins, these only accept
unsigned INTEGER_TYPE (or for C BITINT_TYPE) - users can use various ways
to request unsigned variant of arbitrary types. For __builtin_bswapg
additionally this requires the type to have precision which is a multiple
of
8 (unlike clang, which for some strange reason requires multiple of 16).
2026-06-05 Jakub Jelinek <jakub@redhat.com>
PR c/122731
gcc/
* doc/extend.texi (__builtin_bswapg, __builtin_bitreverseg):
Document.
* internal-fn.def (DEF_INTERNAL_INTSZ_FN): Define.
(DEF_INTERNAL_INTSZ_EXT_FN): Define.
(BSWAP): New internal fn.
(BITREVERSE): Likewise.
* internal-fn.h (expand_BSWAP, expand_BITREVERSE): Declare.
* internal-fn.cc (DEF_INTERNAL_INTSZ_EXT_FN): Define.
(expand_BSWAP, expand_BITREVERSE): New functions.
* builtins.h (fold_build_builtin_bswapg_bitreverseg): Declare.
* builtins.cc (fold_build_builtin_bswapg_bitreverseg): New
function.
* wide-int.h (wi::bswap): Fix up docs that only multiple of 8 bits
is required.
* gimple-match-exports.cc (build_call_internal): Handle IFN_BSWAP
and IFN_BITREVERSE.
* gencfn-macros.cc (internal_fn_intsz_names): New variable.
(intsz_suffixes): Likewise.
(suffix_lists): Add intsz_suffixes.
(main): Handle internal_fn_intsz_names.
* match.pd (BSWAP, BITREVERSE): Remove.
(popcount(bswap(x)) is popcount(x)): Use BSWAP BITREVERSE
op lists.
* tree-ssa-phiopt.cc (empty_bb_or_one_feeding_into_p): Use
CASE_CFN_BSWAP and CASE_CFN_BITREVERSE.
(cond_removal_in_builtin_zero_pattern): Likewise.
* fold-const-call.cc (fold_const_call_ss): Likewise.
* fold-const.cc (tree_call_nonnegative_p): Likewise.
* gimple-lower-bitint.cc (struct bitint_large_huge): Declare
lower_bswap_bitreverse method.
(bitint_large_huge::lower_bswap_bitreverse): New method.
(bitint_large_huge::lower_call): Call it.
(build_bitint_stmt_ssa_conflicts): Set muldiv_p for IFN_BSWAP
and IFN_BITREVERSE.
gcc/c-family/
* c-common.h (enum rid): Add RID_BUILTIN_BSWAPG and
RID_BUILTIN_BITREVERSEG.
* c-common.cc (c_common_reswords): Add __builtin_bswapg
and __builtin_bitreverseg.
gcc/c/
* c-parser.cc (c_parser_postfix_expression): Parse
__builtin_bswapg and __builtin_bitreverseg.
gcc/cp/
* parser.cc (cp_parser_postfix_expression): Parse
__builtin_bswapg and __builtin_bitreverseg.
* typeck.cc (build_x_bswapg_bitreverseg): New function.
* cp-tree.h (build_x_bswapg_bitreverseg): Declare.
* pt.cc (tsubst_expr): Handle IFN_BSWAP and IFN_BITREVERSE.
* constexpr.cc (cxx_eval_internal_function): Likewise.
(potential_constant_expression_1): Likewise.
* cp-gimplify.cc (cp_gimplify_expr): Likewise.
gcc/testsuite/
* c-c++-common/builtin-bswapg-1.c: New test.
* c-c++-common/builtin-bswapg-2.c: New test.
* c-c++-common/builtin-bswapg-3.c: New test.
* c-c++-common/builtin-bitreverseg-1.c: New test.
* c-c++-common/builtin-bitreverseg-2.c: New test.
Reviewed-by: Richard Biener <rguenth@suse.de>
Reviewed-by: Joseph Myers <josmyers@redhat.com>
Reviewed-by: Jason Merrill <jason@redhat.com>
More information about the Gcc-bugs
mailing list