public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "barry.revzin at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/95383] New: Poor codegen when constructing a trivial Optional
Date: Thu, 28 May 2020 13:56:16 +0000	[thread overview]
Message-ID: <bug-95383-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 95383
           Summary: Poor codegen when constructing a trivial Optional
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: barry.revzin at gmail dot com
  Target Milestone: ---

Here is a complete example:

struct nullopt_t {} inline constexpr nullopt{};

template <typename T>
struct Optional {
    struct Empty { };
    union {
        Empty _;
        T value;
    };
    bool engaged;

    Optional(nullopt_t) : _(), engaged(false) { }
    Optional(T v) : value(v), engaged(true) { }
};

Optional<int> foo(bool b) {
    if (b) {
        return 42;
    }
    return nullopt;
}

Optional here is a valid implementation strategy for trivial types, like int.
You can see some codegen examples here: https://godbolt.org/z/KwuWzB

gcc 10.1 -O2 emits:

foo(bool):
        test    dil, dil
        mov     eax, 0
        movabs  rdx, 4294967338
        cmovne  rax, rdx
        ret

gcc 10.1 -O3 is worse:

foo(bool):
        xor     eax, eax
        mov     ecx, 42
        test    dil, dil
        cmovne  rdx, rcx
        mov     ecx, 1
        cmovne  rax, rcx
        movabs  rcx, -1095216660481
        and     rdx, rcx
        sal     rax, 32
        or      rax, rdx
        ret

gcc trunk (as of today) -O2 is the same as this bad -O3 version.

clang 10, on the other hand, on -O2 or -O3, emits:

foo(bool):                                # @foo(bool)
        shl     rdi, 32
        lea     rax, [rdi + 42]
        ret

which is much better. 

Using std::optional instead of this Optional (https://godbolt.org/z/By-fYx) for
comparison, clang emits the same code as above. gcc 10 -O3 emits a branch:

foo(bool):
        xor     eax, eax
        test    dil, dil
        je      .L2
        mov     DWORD PTR [rsp-8], 42
        mov     eax, 1
.L2:
        mov     BYTE PTR [rsp-4], al
        mov     rax, QWORD PTR [rsp-8]
        ret

             reply	other threads:[~2020-05-28 13:56 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-28 13:56 barry.revzin at gmail dot com [this message]
2020-05-29  6:21 ` [Bug c++/95383] " rguenth at gcc dot gnu.org
2021-12-17  5:33 ` [Bug middle-end/95383] " 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-95383-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).