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

            Bug ID: 107161
           Summary: gcc doesn't constant fold member if any other member
                    is mutable
           Product: gcc
           Version: 12.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vanyacpp at gmail dot com
  Target Milestone: ---

On this code:

struct mytype
{
    int a;
    mutable int b;
};

extern mytype const p = {1, 2};

int foo()
{
    return p.a + 10;
}

int bar()
{
    return p.b + 10;
}

GCC -O2 generates:
foo():
        mov     eax, DWORD PTR p[rip]
        add     eax, 10
        ret
bar():
        mov     eax, DWORD PTR p[rip+4]
        add     eax, 10
        ret

While clang folds "p.a + 10" into 11:
foo():                                # @foo()
        mov     eax, 11
        ret
bar():                                # @bar()
        mov     eax, dword ptr [rip + p+4]
        add     eax, 10
        ret

I think GCC should do the same.

Reply via email to