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

            Bug ID: 125619
           Summary: Missed optimization with comparing struct with bit
                    field
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: dr.xiaosa at gmail dot com
  Target Milestone: ---

GCC generate bad code when doing field-wise comparison of struct with bit field
member on x86-64 target. Given:

===
struct test0 {
    std::uint16_t a:7;
    std::uint16_t b:2;
    std::uint16_t c:5;
    std::uint16_t d:2;

    friend constexpr bool operator==(const test0& lhs, const test0& rhs) =
default;
};

bool comp0(test0 a, test0 b) noexcept {
    return a == b;
}
===

GCC now produces:

===
"comp0(test0, test0)":
        mov     edx, edi
        xor     eax, eax
        xor     edx, esi
        test    dx, 511
        je      .L5
        ret
.L5:
        mov     edx, esi
        mov     ecx, edi
        movzx   eax, dh
        xor     al, ch
        cmp     al, 1
        setbe   al
        ret
===

Full test cases in C++:
https://godbolt.org/z/f18edz9KP

Reply via email to