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
  • [Bug tree-optimization/113718] N... cassio.neri at gmail dot com via Gcc-bugs

Reply via email to