On 11/2/20 12:21 PM, Jakub Jelinek via Gcc-patches wrote:
> On Thu, Jul 30, 2020 at 10:16:46AM -0400, Jason Merrill via Gcc-patches wrote:
>>> The following patch adds __builtin_bit_cast builtin, similarly to
>>> clang or MSVC which implement std::bit_cast using such an builtin too.
>> Great!
> Sorry for the long delay.
>
> The following patch implements what you've requested in the review.
> It doesn't deal with the padding bits being (sometimes) well defined, but if
> we e.g. get a new CONSTRUCTOR bit that would request that (and corresponding
> gimplifier change to pre-initialize to all zeros), it could be easily
> adjusted (just for such CONSTRUCTORs memset the whole mask portion
> (total_bytes) and set mask to NULL). If there are other cases where
> such handling is desirable (e.g. I wonder about bit_cast being called on
> non-automatic const variables where their CONSTRUCTOR at least
> implementation-wise would end up being on pre-zeroed .rodata (or .data)
> memory), that can be handled too.
>
> In the previous version I was using next_initializable_field, that is
> something that isn't available in the middle-end, so all I do right now
> is just skip non-FIELD_DECLs, unnamed bit-fields and fields with zero size.
>
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
>
> 2020-11-02 Jakub Jelinek <ja...@redhat.com>
>
> PR libstdc++/93121
> * fold-const.h (native_encode_initializer): Add mask argument
> defaulted to nullptr.
> (find_bitfield_repr_type): Declare.
> * fold-const.c (find_bitfield_repr_type): New function.
> (native_encode_initializer): Add mask argument and support for
> filling it. Handle also some bitfields without integral
> DECL_BIT_FIELD_REPRESENTATIVE.
>
> * c-common.h (enum rid): Add RID_BUILTIN_BIT_CAST.
> * c-common.c (c_common_reswords): Add __builtin_bit_cast.
>
> * cp-tree.h (cp_build_bit_cast): Declare.
> * cp-tree.def (BIT_CAST_EXPR): New tree code.
> * cp-objcp-common.c (names_builtin_p): Handle RID_BUILTIN_BIT_CAST.
> (cp_common_init_ts): Handle BIT_CAST_EXPR.
> * cxx-pretty-print.c (cxx_pretty_printer::postfix_expression):
> Likewise.
> * parser.c (cp_parser_postfix_expression): Handle
> RID_BUILTIN_BIT_CAST.
> * semantics.c (cp_build_bit_cast): New function.
> * tree.c (cp_tree_equal): Handle BIT_CAST_EXPR.
> (cp_walk_subtrees): Likewise.
> * pt.c (tsubst_copy): Likewise.
> * constexpr.c (check_bit_cast_type, cxx_native_interpret_aggregate,
> cxx_eval_bit_cast): New functions.
> (cxx_eval_constant_expression): Handle BIT_CAST_EXPR.
> (potential_constant_expression_1): Likewise.
> * cp-gimplify.c (cp_genericize_r): Likewise.
>
> * g++.dg/cpp2a/bit-cast1.C: New test.
> * g++.dg/cpp2a/bit-cast2.C: New test.
> * g++.dg/cpp2a/bit-cast3.C: New test.
> * g++.dg/cpp2a/bit-cast4.C: New test.
> * g++.dg/cpp2a/bit-cast5.C: New test.
Just looking at the fold-const and c-common bits. Jason is going to
look the the cp/ stuff per today's meeting.
FIrst, do we need to document the new builtin?
> --- gcc/fold-const.c.jj 2020-10-15 15:09:49.079725120 +0200
> +++ gcc/fold-const.c 2020-11-02 17:20:43.784633491 +0100
> @@ -7998,6 +8083,8 @@ native_encode_initializer (tree init, un
> if (ptr)
> memcpy (ptr + (curpos - o), ptr + (pos - o),
> fieldsize);
> + if (mask)
> + memcpy (mask + curpos, mask + pos, fieldsize);
> }
> else if (!native_encode_initializer (val,
> ptr
Presumably we've already determined that the memcpy won't have an
overlap between the source and destination here?
Assuming that's true, then it looks good to me.
jeff