public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "cfsteefel at arista dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/110648] New: Missed optimization for small returned optional leads to redundant memory accesses
Date: Wed, 12 Jul 2023 21:27:37 +0000	[thread overview]
Message-ID: <bug-110648-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 110648
           Summary: Missed optimization for small returned optional leads
                    to redundant memory accesses
           Product: gcc
           Version: 11.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: cfsteefel at arista dot com
  Target Milestone: ---

The following code 

#include <optional>

std::optional< int >
foo( int x ) {
    return 1;
}

produces x86_64 assembly which does two stores into the stack, and then a load
into rax, rather than simply operating directly on rax.

i.e.:
foo(int):
        mov     DWORD PTR [rsp-8], 1
        mov     BYTE PTR [rsp-4], 1
        mov     rax, QWORD PTR [rsp-8]
        ret

clang produces much more direct code:
foo(int):                                # @foo(int)
        movabs  rax, 4294967297
        ret

Since the returned value is always returned in rax as the optional is small
enough (less than two registers wide), there is no reason for the memory
accesses here.

The code can be improved by naming the returned object, but this breaks down
again if there are any conditionals, i.e.:
std::optional< int >
foo( int x ) {
    std::optional< int > ret = 1;
    return ret;
}

produces better code, but there is no way to get this better code once a branch
is introduced, i.e.

std::optional< int >
foo( int x ) {
    std::optional< int > ret = 1;
    if ( x < 1 ) {
      return std::nullopt;
    }
    return ret;
}

The same applies with godbolts' trunk version of gcc, as well as gcc11.3.

             reply	other threads:[~2023-07-12 21:27 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-12 21:27 cfsteefel at arista dot com [this message]
2023-07-12 21:34 ` [Bug c++/110648] " 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-110648-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).