public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/57971] New: Improve copy elision when returning structs by value
@ 2013-07-24 19:29 scovich at gmail dot com
  2015-01-10 18:38 ` [Bug c++/57971] " gcc-bugzilla at contacts dot eelis.net
  2024-03-17 19:24 ` pinskia at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: scovich at gmail dot com @ 2013-07-24 19:29 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57971

            Bug ID: 57971
           Summary: Improve copy elision when returning structs by value
           Product: gcc
           Version: 4.8.1
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: scovich at gmail dot com

Hi all,

In the testcase below, bar() and baz() perform copy elision as expected, but
blah() does not, in spite of its being functionally identical to baz():

#include <cstdio>
struct foo {
    foo() { printf("make\n"); }
    foo(foo const &) { printf("copy\n"); }
    void frob() { printf("frob\n"); }
};

foo bar(bool) {
    foo f;
    f.frob();
    return f;
}
foo baz(bool mknew) {
    if (mknew)
        return foo();
    return bar(mknew);
}
foo blah(bool mknew) {
    if (mknew)
        return foo();
    foo f = bar(mknew);
    return f;
}

int main() {
    printf("*** bar ***\n");
    bar(false);
    printf("*** baz ***\n");
    baz(false);
    printf("*** blah ***\n");
    blah(false);
}

Output is:

$ g++ -Wall bug.cpp && ./a.out
*** bar ***
make
frob
*** baz ***
make
frob
*** blah ***
make
frob
copy

I assume that bar() and baz() exploit the named and unnamed return value
optimizations, respectively, but blah() is missed because it needs both
optimizations together.


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

* [Bug c++/57971] Improve copy elision when returning structs by value
  2013-07-24 19:29 [Bug c++/57971] New: Improve copy elision when returning structs by value scovich at gmail dot com
@ 2015-01-10 18:38 ` gcc-bugzilla at contacts dot eelis.net
  2024-03-17 19:24 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: gcc-bugzilla at contacts dot eelis.net @ 2015-01-10 18:38 UTC (permalink / raw)
  To: gcc-bugs

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

Eelis <gcc-bugzilla at contacts dot eelis.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |gcc-bugzilla at contacts dot eelis
                   |                            |.net

--- Comment #1 from Eelis <gcc-bugzilla at contacts dot eelis.net> ---
struct foo { foo(); foo(foo const &); };

foo bar();

foo fast(bool b) {
    if (b) return foo();
    return bar();
}

/* produces (with -O3):

_Z4fastb:
.LFB0:
    testb    %sil, %sil
    pushq    %rbx
    movq    %rdi, %rbx
    jne    .L6
    call    _Z3barv
    movq    %rbx, %rax
    popq    %rbx
    ret
    .p2align 4,,10
    .p2align 3
.L6:
    call    _ZN3fooC1Ev
    movq    %rbx, %rax
    popq    %rbx
    ret
*/

foo slow(bool b) {
    if (b) return foo();
    foo f = bar();
    return f;
}

/* produces (with -O3):

_Z4slowb:
.LFB1:
    pushq    %rbx
    movq    %rdi, %rbx
    subq    $16, %rsp
    testb    %sil, %sil
    jne    .L11
    leaq    15(%rsp), %rdi
    call    _Z3barv
    leaq    15(%rsp), %rsi
    movq    %rbx, %rdi
    call    _ZN3fooC1ERKS_
    addq    $16, %rsp
    movq    %rbx, %rax
    popq    %rbx
    ret
    .p2align 4,,10
    .p2align 3
.L11:
    call    _ZN3fooC1Ev
    addq    $16, %rsp
    movq    %rbx, %rax
    popq    %rbx
    ret
*/


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

* [Bug c++/57971] Improve copy elision when returning structs by value
  2013-07-24 19:29 [Bug c++/57971] New: Improve copy elision when returning structs by value scovich at gmail dot com
  2015-01-10 18:38 ` [Bug c++/57971] " gcc-bugzilla at contacts dot eelis.net
@ 2024-03-17 19:24 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-03-17 19:24 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2024-03-17

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed. clang is able to optimize this case.

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

end of thread, other threads:[~2024-03-17 19:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-07-24 19:29 [Bug c++/57971] New: Improve copy elision when returning structs by value scovich at gmail dot com
2015-01-10 18:38 ` [Bug c++/57971] " gcc-bugzilla at contacts dot eelis.net
2024-03-17 19:24 ` pinskia 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).