public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/111805] New: suboptimal codegen of variant<string, bool>
@ 2023-10-14  5:40 hiraditya at msn dot com
  2023-10-14 15:29 ` [Bug c++/111805] " pinskia at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: hiraditya at msn dot com @ 2023-10-14  5:40 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 111805
           Summary: suboptimal codegen of variant<string, bool>
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: hiraditya at msn dot com
  Target Milestone: ---

#include<variant>
#include<string>

std::string foo() {
    std::variant<std::string, bool> v {"abc"};
    return std::get<0>(v);
}

g++-13.2 -O2 -std=c++20


foo[abi:cxx11]():
        lea     rdx, [rdi+16]
        mov     BYTE PTR [rdi+18], 99
        mov     rax, rdi
        mov     QWORD PTR [rdi], rdx
        mov     edx, 25185
        mov     WORD PTR [rdi+16], dx
        mov     QWORD PTR [rdi+8], 3
        mov     BYTE PTR [rdi+19], 0
        ret


clang++ -O2 -std=c++20

foo():                                # @foo()
        mov     rax, rdi
        mov     byte ptr [rdi], 6
        mov     dword ptr [rdi + 1], 6513249
        ret


https://godbolt.org/z/nTv5rYanM

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

* [Bug c++/111805] suboptimal codegen of variant<string, bool>
  2023-10-14  5:40 [Bug c++/111805] New: suboptimal codegen of variant<string, bool> hiraditya at msn dot com
@ 2023-10-14 15:29 ` pinskia at gcc dot gnu.org
  2023-10-14 15:36 ` [Bug middle-end/111805] " pinskia at gcc dot gnu.org
  2023-10-16  6:48 ` rguenth at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-10-14 15:29 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
The clang assembly you posted does not match the source you posted ...

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

* [Bug middle-end/111805] suboptimal codegen of variant<string, bool>
  2023-10-14  5:40 [Bug c++/111805] New: suboptimal codegen of variant<string, bool> hiraditya at msn dot com
  2023-10-14 15:29 ` [Bug c++/111805] " pinskia at gcc dot gnu.org
@ 2023-10-14 15:36 ` pinskia at gcc dot gnu.org
  2023-10-16  6:48 ` rguenth at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-10-14 15:36 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
           Keywords|                            |missed-optimization
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2023-10-14
          Component|c++                         |middle-end

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Anyways we have:
  __builtin_memcpy (&v.D.38438._M_local_buf, "abc", 3);
  _5 = &MEM[(struct basic_string *)_3(D)].D.38438._M_local_buf;
  MEM[(struct _Alloc_hider *)_3(D)]._M_p = _5;
  __builtin_memcpy (_5, &v.D.38438._M_local_buf, 3);
  MEM[(struct basic_string *)_3(D)]._M_string_length = 3;
  MEM[(char_type &)_3(D) + 19] = 0;

If we able to remove the first memcpy things would be just work I think ...

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

* [Bug middle-end/111805] suboptimal codegen of variant<string, bool>
  2023-10-14  5:40 [Bug c++/111805] New: suboptimal codegen of variant<string, bool> hiraditya at msn dot com
  2023-10-14 15:29 ` [Bug c++/111805] " pinskia at gcc dot gnu.org
  2023-10-14 15:36 ` [Bug middle-end/111805] " pinskia at gcc dot gnu.org
@ 2023-10-16  6:48 ` rguenth at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-10-16  6:48 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Version|unknown                     |13.2.0

--- Comment #3 from Richard Biener <rguenth at gcc dot gnu.org> ---
The memcpy is gone eventually, with a string length of 4 even on GIMPLE.

clang produces

_Z3fooB5cxx11v:                         # @_Z3fooB5cxx11v
        .cfi_startproc
# %bb.0:
        movq    %rdi, %rax
        leaq    16(%rdi), %rcx
        movq    %rcx, (%rdi)
        movl    $6513249, 16(%rdi)              # imm = 0x636261
        movq    $3, 8(%rdi)
        retq

for me and

_Z3foov:                                # @_Z3foov
        .cfi_startproc
# %bb.0:
        movq    %rdi, %rax
        movb    $6, -32(%rsp)
        movl    $6513249, -31(%rsp)             # imm = 0x636261
        movq    -16(%rsp), %rcx
        movq    %rcx, 16(%rdi)
        movzbl  -32(%rsp), %ecx
        movb    %cl, (%rdi)
        movl    -31(%rsp), %ecx
        movl    %ecx, 1(%rdi)
        movq    -27(%rsp), %rcx
        movq    %rcx, 5(%rdi)
        movzwl  -19(%rsp), %ecx
        movw    %cx, 13(%rdi)
        movzbl  -17(%rsp), %ecx
        movb    %cl, 15(%rdi)
        retq

with -stdlib=libc++

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

end of thread, other threads:[~2023-10-16  6:48 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-14  5:40 [Bug c++/111805] New: suboptimal codegen of variant<string, bool> hiraditya at msn dot com
2023-10-14 15:29 ` [Bug c++/111805] " pinskia at gcc dot gnu.org
2023-10-14 15:36 ` [Bug middle-end/111805] " pinskia at gcc dot gnu.org
2023-10-16  6:48 ` rguenth 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).