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

            Bug ID: 102490
           Summary: Erroneous optimization of default constexpr operator==
                    of struct with bitfields
           Product: gcc
           Version: 10.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: luc.briand35 at gmail dot com
  Target Milestone: ---

Hello,

For the following code, gcc version 10 and up wrongly optimizes the default
operator==().
This occurs for O1 and up.
Removing the 'constexpr' qualifier fixes everything.
The size of the bitfields doesn't matter.
No warnings are appear with "-Wall -Wextra".


Godbolt link: https://gcc.godbolt.org/z/j4fG3sKze


struct A
{
    unsigned char foo : 1;
    unsigned char bar : 1;

    constexpr bool operator==(const A&) const = default;
};


int main()
{
    A a{}, b{};

    a.bar = 0b1;

    return a == b;
}



With the options "-std=c++2a -O1", the assembly generated is simply:
main:
        mov     eax, 1
        ret



In this similar example, we can see that the generated assembly for the
equality operator ignores the 'bar' bitfield (Godbolt link:
https://gcc.godbolt.org/z/3K75xx1on) :


struct A
{
    unsigned char foo : 3;
    unsigned char bar : 1;

    constexpr bool operator==(const A&) const = default;
};

void change(A& a);

int main()
{
    A a{}, b{};

    change(a);

    return a == b;
}


The assembly for gcc version 10.X and 11.X is a bit different, but have the
same problem.

Reply via email to