public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/95405] New: Unnecessary stores with std::optional
@ 2020-05-29  8:38 steffen.hirschmann at ipvs dot uni-stuttgart.de
  2021-03-07  7:32 ` [Bug rtl-optimization/95405] " pinskia at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: steffen.hirschmann at ipvs dot uni-stuttgart.de @ 2020-05-29  8:38 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 95405
           Summary: Unnecessary stores with std::optional
           Product: gcc
           Version: 10.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: steffen.hirschmann at ipvs dot uni-stuttgart.de
  Target Milestone: ---

I posted this to the gcc-help mailing list a few days ago
(https://gcc.gnu.org/pipermail/gcc-help/2020-May/138978.html).

GCC produces stores that don't seem to be required for std::optional.

Code:
--------
#include <optional>
std::optional<long> foo();
long bar()
{
    auto r = foo();
    if (r)
        return *r;
    else
        return 0L;
}
--------

What gcc 10.1 with -std=c++17 -O3 produces is:
bar():
        sub     rsp, 24
        call    foo()
        mov     QWORD PTR [rsp+8], rdx
        cmp     BYTE PTR [rsp+8], 0
        mov     QWORD PTR [rsp], rax
        mov     rax, QWORD PTR [rsp]
        jne     .L1
        xor     eax, eax
.L1:
        add     rsp, 24
        ret

(see: https://godbolt.org/z/uHE6QB)

I don't understand the stores (and loads) after the call to foo. They
don't seem necessary to me.


Marc Glisse pointed out
(https://gcc.gnu.org/pipermail/gcc-help/2020-May/138982.html) that the first
pair of store/load seems to be a tuning choice and can be removed with the
correct tuning flags.


What I expected is:
        mov     QWORD PTR [rsp+8], rdx
        cmp     BYTE PTR [rsp+8], 0
should be a compare/test directly of dl.

And:
        mov     QWORD PTR [rsp], rax
        mov     rax, QWORD PTR [rsp]
is not present at all.

Can someone explain this behavior? Shouldn't the optimizer produce what I
pointed out?

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

end of thread, other threads:[~2023-07-12 21:35 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-29  8:38 [Bug c++/95405] New: Unnecessary stores with std::optional steffen.hirschmann at ipvs dot uni-stuttgart.de
2021-03-07  7:32 ` [Bug rtl-optimization/95405] " pinskia at gcc dot gnu.org
2021-06-06 15:30 ` gabravier at gmail dot com
2021-06-06 18:07 ` glisse at gcc dot gnu.org
2021-06-06 18:11 ` gabravier at gmail dot com
2021-06-06 18:58 ` glisse at gcc dot gnu.org
2023-07-12 21:34 ` pinskia at gcc dot gnu.org
2023-07-12 21:35 ` pinskia at gcc dot gnu.org
2023-07-12 21:35 ` pinskia at gcc dot gnu.org
2023-07-12 21:35 ` pinskia 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).