public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/102490] New: Erroneous optimization of default constexpr operator== of struct with bitfields
@ 2021-09-26 11:31 luc.briand35 at gmail dot com
  2021-09-27 10:16 ` [Bug c++/102490] " jakub at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: luc.briand35 at gmail dot com @ 2021-09-26 11:31 UTC (permalink / raw)
  To: gcc-bugs

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.

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

end of thread, other threads:[~2022-11-03 21:18 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-26 11:31 [Bug c++/102490] New: Erroneous optimization of default constexpr operator== of struct with bitfields luc.briand35 at gmail dot com
2021-09-27 10:16 ` [Bug c++/102490] " jakub at gcc dot gnu.org
2021-09-27 11:33 ` jakub at gcc dot gnu.org
2021-09-27 11:37 ` pinskia at gcc dot gnu.org
2021-09-27 12:38 ` jakub at gcc dot gnu.org
2021-10-05 17:15 ` jakub at gcc dot gnu.org
2021-10-06  2:41 ` cvs-commit at gcc dot gnu.org
2022-11-03 21:18 ` ppalka 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).