public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/107161] New: gcc doesn't constant fold member if any other member is mutable
@ 2022-10-05 17:33 vanyacpp at gmail dot com
  2022-10-05 17:34 ` [Bug c++/107161] " pinskia at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: vanyacpp at gmail dot com @ 2022-10-05 17:33 UTC (permalink / raw)
  To: gcc-bugs

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.

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

* [Bug c++/107161] gcc doesn't constant fold member if any other member is mutable
  2022-10-05 17:33 [Bug c++/107161] New: gcc doesn't constant fold member if any other member is mutable vanyacpp at gmail dot com
@ 2022-10-05 17:34 ` pinskia at gcc dot gnu.org
  2022-10-06 10:46 ` rguenth at gcc dot gnu.org
  2022-10-06 12:39 ` vanyacpp at gmail dot com
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-10-05 17:34 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
           Keywords|                            |missed-optimization

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

* [Bug c++/107161] gcc doesn't constant fold member if any other member is mutable
  2022-10-05 17:33 [Bug c++/107161] New: gcc doesn't constant fold member if any other member is mutable vanyacpp at gmail dot com
  2022-10-05 17:34 ` [Bug c++/107161] " pinskia at gcc dot gnu.org
@ 2022-10-06 10:46 ` rguenth at gcc dot gnu.org
  2022-10-06 12:39 ` vanyacpp at gmail dot com
  2 siblings, 0 replies; 4+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-10-06 10:46 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
GCC relies on 'p' being in .rodata for the folding which of course cannot be
the case with a mutable member.  Note the frontend clears the 'const' from the
declaration because of the mutable member and we have no way to distinguish
constant from non-constant members here.

Do constexpr/consteval work in such circumstances?

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

* [Bug c++/107161] gcc doesn't constant fold member if any other member is mutable
  2022-10-05 17:33 [Bug c++/107161] New: gcc doesn't constant fold member if any other member is mutable vanyacpp at gmail dot com
  2022-10-05 17:34 ` [Bug c++/107161] " pinskia at gcc dot gnu.org
  2022-10-06 10:46 ` rguenth at gcc dot gnu.org
@ 2022-10-06 12:39 ` vanyacpp at gmail dot com
  2 siblings, 0 replies; 4+ messages in thread
From: vanyacpp at gmail dot com @ 2022-10-06 12:39 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Ivan Sorokin <vanyacpp at gmail dot com> ---
> Do constexpr/consteval work in such circumstances?

Yes, constexpr works for variables like "p.a":

extern constexpr mytype p = {1, 2};

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

foo():
        mov     eax, 11
        ret

https://godbolt.org/z/K9a69E4ar

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

end of thread, other threads:[~2022-10-06 12:39 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-05 17:33 [Bug c++/107161] New: gcc doesn't constant fold member if any other member is mutable vanyacpp at gmail dot com
2022-10-05 17:34 ` [Bug c++/107161] " pinskia at gcc dot gnu.org
2022-10-06 10:46 ` rguenth at gcc dot gnu.org
2022-10-06 12:39 ` vanyacpp at gmail dot com

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).