public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Generated assembly code for returning std::optional<uint32_t>
@ 2021-10-28  7:10 ☂Josh Chia (謝任中)
  2021-10-28  9:55 ` Jonathan Wakely
  0 siblings, 1 reply; 3+ messages in thread
From: ☂Josh Chia (謝任中) @ 2021-10-28  7:10 UTC (permalink / raw)
  To: gcc-help

Under -O3 -std=c++17, the assembly code generated by gcc 11.2 for some
std::optional C++ code seems less optimal than I expect; it is writing the
content of optional<uint64_t> return values to both the stack and to the
RAX register. What am I missing? I was under the impression that return
values are put in RAX if they can fit. Why does gcc have to write the
return value to both the stack and RAX instead of just one of them? Can the
writing to the stack be avoided?

foo() & bar() return optional<uint32_t> and demonstrate this problem. baz()
returns int32_t and does not have the problem. bun(), which calls foo() and
bar(), accesses the optional<uint32_t> return value through RAX and not
through the stack, so it is evident that the stack value is redundant.

https://godbolt.org/z/Ef14n8EcM

C++ code:
#include <cstdint>
#include <optional>

using namespace std;

optional<uint32_t> foo() {
return 111;
}

optional<uint32_t> bar() {
return nullopt;
}

uint32_t baz() {
return 111;
}

bool bun(bool x) {
auto f = x ? foo : bar;
return f().has_value();
}

Generated assembly code:
foo():
mov DWORD PTR [rsp-8], 111
mov BYTE PTR [rsp-4], 1
mov rax, QWORD PTR [rsp-8]
ret
bar():
mov BYTE PTR [rsp-4], 0
mov rax, QWORD PTR [rsp-8]
ret
baz():
mov eax, 111
ret
bun(bool):
sub rsp, 24
mov edx, OFFSET FLAT:bar()
test dil, dil
mov eax, OFFSET FLAT:foo()
cmove rax, rdx
call rax
add rsp, 24
shr rax, 32
ret

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

* Re: Generated assembly code for returning std::optional<uint32_t>
  2021-10-28  7:10 Generated assembly code for returning std::optional<uint32_t> ☂Josh Chia (謝任中)
@ 2021-10-28  9:55 ` Jonathan Wakely
  2021-10-28 10:54   ` ☂Josh Chia (謝任中)
  0 siblings, 1 reply; 3+ messages in thread
From: Jonathan Wakely @ 2021-10-28  9:55 UTC (permalink / raw)
  To: ☂Josh Chia (謝任中); +Cc: gcc-help

On Thu, 28 Oct 2021 at 08:11, ☂Josh Chia (謝任中) via Gcc-help
<gcc-help@gcc.gnu.org> wrote:
>
> Under -O3 -std=c++17, the assembly code generated by gcc 11.2 for some
> std::optional C++ code seems less optimal than I expect; it is writing the
> content of optional<uint64_t> return values to both the stack and to the
> RAX register. What am I missing? I was under the impression that return
> values are put in RAX if they can fit. Why does gcc have to write the
> return value to both the stack and RAX instead of just one of them? Can the
> writing to the stack be avoided?

Is this https://gcc.gnu.org/PR101326 ?

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

* Re: Generated assembly code for returning std::optional<uint32_t>
  2021-10-28  9:55 ` Jonathan Wakely
@ 2021-10-28 10:54   ` ☂Josh Chia (謝任中)
  0 siblings, 0 replies; 3+ messages in thread
From: ☂Josh Chia (謝任中) @ 2021-10-28 10:54 UTC (permalink / raw)
  To: Jonathan Wakely; +Cc: gcc-help

Yes, it is. Thanks.

On Thu, Oct 28, 2021 at 5:55 PM Jonathan Wakely <jwakely.gcc@gmail.com>
wrote:

> On Thu, 28 Oct 2021 at 08:11, ☂Josh Chia (謝任中) via Gcc-help
> <gcc-help@gcc.gnu.org> wrote:
> >
> > Under -O3 -std=c++17, the assembly code generated by gcc 11.2 for some
> > std::optional C++ code seems less optimal than I expect; it is writing
> the
> > content of optional<uint64_t> return values to both the stack and to the
> > RAX register. What am I missing? I was under the impression that return
> > values are put in RAX if they can fit. Why does gcc have to write the
> > return value to both the stack and RAX instead of just one of them? Can
> the
> > writing to the stack be avoided?
>
> Is this https://gcc.gnu.org/PR101326 ?
>

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

end of thread, other threads:[~2021-10-28 10:54 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-28  7:10 Generated assembly code for returning std::optional<uint32_t> ☂Josh Chia (謝任中)
2021-10-28  9:55 ` Jonathan Wakely
2021-10-28 10:54   ` ☂Josh Chia (謝任中)

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