* [PATCH] fold-const: Handle NON_LVALUE_EXPR in native_encode_initializer [PR114537]
@ 2024-04-04 7:36 Jakub Jelinek
2024-04-04 8:07 ` Richard Biener
0 siblings, 1 reply; 2+ messages in thread
From: Jakub Jelinek @ 2024-04-04 7:36 UTC (permalink / raw)
To: Richard Biener, Jason Merrill; +Cc: gcc-patches
Hi!
The following testcase is incorrectly rejected. The problem is that
for bit-fields native_encode_initializer expects the corresponding
CONSTRUCTOR elt value must be INTEGER_CST, but that isn't the case
here, it is wrapped into NON_LVALUE_EXPR by maybe_wrap_with_location.
We could STRIP_ANY_LOCATION_WRAPPER as well, but as all we are looking for
is INTEGER_CST inside, just looking through NON_LVALUE_EXPR seems easier.
Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
2024-04-04 Jakub Jelinek <jakub@redhat.com>
PR c++/114537
* fold-const.cc (native_encode_initializer): Look through
NON_LVALUE_EXPR if val is INTEGER_CST.
* g++.dg/cpp2a/bit-cast16.C: New test.
--- gcc/fold-const.cc.jj 2024-03-26 11:21:31.996860739 +0100
+++ gcc/fold-const.cc 2024-04-03 16:59:05.747297410 +0200
@@ -8601,6 +8601,8 @@ native_encode_initializer (tree init, un
if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
return 0;
+ if (TREE_CODE (val) == NON_LVALUE_EXPR)
+ val = TREE_OPERAND (val, 0);
if (TREE_CODE (val) != INTEGER_CST)
return 0;
--- gcc/testsuite/g++.dg/cpp2a/bit-cast16.C.jj 2024-04-03 17:06:46.720974426 +0200
+++ gcc/testsuite/g++.dg/cpp2a/bit-cast16.C 2024-04-03 17:06:40.233063410 +0200
@@ -0,0 +1,16 @@
+// PR c++/114537
+// { dg-do compile { target c++20 } }
+
+namespace std {
+template<typename T, typename F>
+constexpr T
+bit_cast (const F& f) noexcept
+{
+ return __builtin_bit_cast (T, f);
+}
+}
+
+struct A { signed char b : 1 = 0; signed char c : 7 = 0; };
+struct D { unsigned char e; };
+constexpr unsigned char f = std::bit_cast<D> (A{}).e;
+static_assert (f == 0);
Jakub
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] fold-const: Handle NON_LVALUE_EXPR in native_encode_initializer [PR114537]
2024-04-04 7:36 [PATCH] fold-const: Handle NON_LVALUE_EXPR in native_encode_initializer [PR114537] Jakub Jelinek
@ 2024-04-04 8:07 ` Richard Biener
0 siblings, 0 replies; 2+ messages in thread
From: Richard Biener @ 2024-04-04 8:07 UTC (permalink / raw)
To: Jakub Jelinek; +Cc: Jason Merrill, gcc-patches
On Thu, 4 Apr 2024, Jakub Jelinek wrote:
> Hi!
>
> The following testcase is incorrectly rejected. The problem is that
> for bit-fields native_encode_initializer expects the corresponding
> CONSTRUCTOR elt value must be INTEGER_CST, but that isn't the case
> here, it is wrapped into NON_LVALUE_EXPR by maybe_wrap_with_location.
> We could STRIP_ANY_LOCATION_WRAPPER as well, but as all we are looking for
> is INTEGER_CST inside, just looking through NON_LVALUE_EXPR seems easier.
>
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
OK.
Richard.
> 2024-04-04 Jakub Jelinek <jakub@redhat.com>
>
> PR c++/114537
> * fold-const.cc (native_encode_initializer): Look through
> NON_LVALUE_EXPR if val is INTEGER_CST.
>
> * g++.dg/cpp2a/bit-cast16.C: New test.
>
> --- gcc/fold-const.cc.jj 2024-03-26 11:21:31.996860739 +0100
> +++ gcc/fold-const.cc 2024-04-03 16:59:05.747297410 +0200
> @@ -8601,6 +8601,8 @@ native_encode_initializer (tree init, un
> if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
> return 0;
>
> + if (TREE_CODE (val) == NON_LVALUE_EXPR)
> + val = TREE_OPERAND (val, 0);
> if (TREE_CODE (val) != INTEGER_CST)
> return 0;
>
> --- gcc/testsuite/g++.dg/cpp2a/bit-cast16.C.jj 2024-04-03 17:06:46.720974426 +0200
> +++ gcc/testsuite/g++.dg/cpp2a/bit-cast16.C 2024-04-03 17:06:40.233063410 +0200
> @@ -0,0 +1,16 @@
> +// PR c++/114537
> +// { dg-do compile { target c++20 } }
> +
> +namespace std {
> +template<typename T, typename F>
> +constexpr T
> +bit_cast (const F& f) noexcept
> +{
> + return __builtin_bit_cast (T, f);
> +}
> +}
> +
> +struct A { signed char b : 1 = 0; signed char c : 7 = 0; };
> +struct D { unsigned char e; };
> +constexpr unsigned char f = std::bit_cast<D> (A{}).e;
> +static_assert (f == 0);
>
> Jakub
>
>
--
Richard Biener <rguenther@suse.de>
SUSE Software Solutions Germany GmbH,
Frankenstrasse 146, 90461 Nuernberg, Germany;
GF: Ivo Totev, Andrew McDonald, Werner Knoblich; (HRB 36809, AG Nuernberg)
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-04-04 8:07 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-04 7:36 [PATCH] fold-const: Handle NON_LVALUE_EXPR in native_encode_initializer [PR114537] Jakub Jelinek
2024-04-04 8:07 ` Richard Biener
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).