public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "steffen.hirschmann at ipvs dot uni-stuttgart.de" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/95405] New: Unnecessary stores with std::optional
Date: Fri, 29 May 2020 08:38:55 +0000	[thread overview]
Message-ID: <bug-95405-4@http.gcc.gnu.org/bugzilla/> (raw)

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?

             reply	other threads:[~2020-05-29  8:38 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-29  8:38 steffen.hirschmann at ipvs dot uni-stuttgart.de [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-95405-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).