public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/113718] New: std::bit_cast making the compiler generate unnecessary code.
@ 2024-02-02 11:42 cassio.neri at gmail dot com
  2024-02-04  4:04 ` [Bug c++/113718] " pinskia at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: cassio.neri at gmail dot com @ 2024-02-02 11:42 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113718

            Bug ID: 113718
           Summary: std::bit_cast making the compiler generate unnecessary
                    code.
           Product: gcc
           Version: 13.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: cassio.neri at gmail dot com
  Target Milestone: ---

Consider:

#include <bit>

void f();

auto const p1 = &f;
auto const p2 = std::bit_cast<void(*)()>(&f);

bool a() {
  return p1 == p2;
}

The code emitted for `a` should be the same as-if `return true;` but the usage
of a "no-op" `std::bit_cast` muddies the waters and the compiler generates:

a():
  cmp QWORD PTR p2[rip], OFFSET FLAT:_Z1fv
  sete al
  ret

FWIW: The following changes make the compiler to generate more efficient code:

1. Move `p1` and `p2` inside the body of `a`.
2. Replace `std::bit_cast` with `static_cast`.
3. Remove the cast altogether.

Things get terribly worse if `p1` and `p2` are made `static` and moved inside
the body of `a`.

Given that the compiler can get confused by a "no-op" `std::bit_cast`, I wonder
if it would do the same for more interesting code than this toy example.

https://godbolt.org/z/daWe5Yod8

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug c++/113718] std::bit_cast making the compiler generate unnecessary code.
  2024-02-02 11:42 [Bug tree-optimization/113718] New: std::bit_cast making the compiler generate unnecessary code cassio.neri at gmail dot com
@ 2024-02-04  4:04 ` pinskia at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-02-04  4:04 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113718

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
           Severity|normal                      |enhancement
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=102876,
                   |                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=4131
          Component|tree-optimization           |c++
   Last reconfirmed|                            |2024-02-04
             Status|UNCONFIRMED                 |NEW

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
void f();
static auto const p2 = __builtin_bit_cast(void(*)(), (&f));


Even  causes the front-end to emit a dynamic initializer for p2.

There is definitely 2 issues here, one for the above and one for the original
code.

Note bit_cast is not a const expression due to being a pointer type and that is
what causes the front-end to emit dynamic initializer.

But I wonder if the front-end could try to do it without the dynamic
initializer.

Anyways confirmed.

Note there is another bug about converting dynamic initializers to static
initializers (PR 4131 and PR 102876).

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-02-04  4:04 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-02 11:42 [Bug tree-optimization/113718] New: std::bit_cast making the compiler generate unnecessary code cassio.neri at gmail dot com
2024-02-04  4:04 ` [Bug c++/113718] " pinskia at gcc dot gnu.org

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).