public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/58487] New: Missed return value optimization
@ 2013-09-20 19:38 jm at bourguet dot org
  2021-06-28 16:36 ` [Bug c++/58487] " antoshkka at gmail dot com
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: jm at bourguet dot org @ 2013-09-20 19:38 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 58487
           Summary: Missed return value optimization
           Product: gcc
           Version: 4.8.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jm at bourguet dot org

$ cat rva.cpp
#include <iostream>

class rva
{
public:
    rva() { std::cout << "Default construction\n"; }
    rva(rva const&) { std::cout << "Copy construction\n"; }
    rva& operator=(rva const&) { std::cout << "Assignation\n"; }
    ~rva() { std::cout << "Destruction\n"; }
};

rva f(int i) {
    if (i == 0) {
        rva result;
        return result;
    } else {
        return rva();
    }
}

int main()
{
    { std::cout << "f(0)\n"; f(0); }
    { std::cout << "\nf(1)\n"; f(1); }
    { std::cout << "\ng(0)\n"; g(0); }
    { std::cout << "\ng(1)\n"; g(1); }
    return 0;
}
$ g++-4.8.1 --version
g++-4.8.1 (GCC) 4.8.1
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ g++-4.8.1 rva.cpp  
$ ./a.out
f(0)
Default construction
Copy construction
Destruction
Destruction

f(1)
Default construction
Destruction


It seems to me that for f(0) the copy construction could be avoided.


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

* [Bug c++/58487] Missed return value optimization
  2013-09-20 19:38 [Bug c++/58487] New: Missed return value optimization jm at bourguet dot org
@ 2021-06-28 16:36 ` antoshkka at gmail dot com
  2021-11-19 20:37 ` gcc_bugzilla at axeitado dot com
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: antoshkka at gmail dot com @ 2021-06-28 16:36 UTC (permalink / raw)
  To: gcc-bugs

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

Antony Polukhin <antoshkka at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |antoshkka at gmail dot com

--- Comment #3 from Antony Polukhin <antoshkka at gmail dot com> ---
Minimized example, move constructor should not be called:

struct A {
  A() = default;
  A(A&&);
};

A test() {
  if (true) {
    A a;
    return a;
  } else {
    return A{};
  }
}


Godbolt playground: https://godbolt.org/z/4Pzq83WWY

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

* [Bug c++/58487] Missed return value optimization
  2013-09-20 19:38 [Bug c++/58487] New: Missed return value optimization jm at bourguet dot org
  2021-06-28 16:36 ` [Bug c++/58487] " antoshkka at gmail dot com
@ 2021-11-19 20:37 ` gcc_bugzilla at axeitado dot com
  2023-06-06 16:11 ` jason at gcc dot gnu.org
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: gcc_bugzilla at axeitado dot com @ 2021-11-19 20:37 UTC (permalink / raw)
  To: gcc-bugs

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

Óscar Fuentes <gcc_bugzilla at axeitado dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |gcc_bugzilla at axeitado dot com

--- Comment #4 from Óscar Fuentes <gcc_bugzilla at axeitado dot com> ---
Isn't this PR a trivial variation of PR53637 ?

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

* [Bug c++/58487] Missed return value optimization
  2013-09-20 19:38 [Bug c++/58487] New: Missed return value optimization jm at bourguet dot org
  2021-06-28 16:36 ` [Bug c++/58487] " antoshkka at gmail dot com
  2021-11-19 20:37 ` gcc_bugzilla at axeitado dot com
@ 2023-06-06 16:11 ` jason at gcc dot gnu.org
  2023-06-07  1:33 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: jason at gcc dot gnu.org @ 2023-06-06 16:11 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Jason Merrill <jason at gcc dot gnu.org> ---
*** Bug 96004 has been marked as a duplicate of this bug. ***

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

* [Bug c++/58487] Missed return value optimization
  2013-09-20 19:38 [Bug c++/58487] New: Missed return value optimization jm at bourguet dot org
                   ` (2 preceding siblings ...)
  2023-06-06 16:11 ` jason at gcc dot gnu.org
@ 2023-06-07  1:33 ` cvs-commit at gcc dot gnu.org
  2023-06-07 22:08 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-06-07  1:33 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The trunk branch has been updated by Jason Merrill <jason@gcc.gnu.org>:

https://gcc.gnu.org/g:2ae5384d457b9c67586de012816dfc71a6943164

commit r14-1594-g2ae5384d457b9c67586de012816dfc71a6943164
Author: Jason Merrill <jason@redhat.com>
Date:   Tue Jun 6 12:46:26 2023 -0400

    c++: Add -Wnrvo

    While looking at PRs about cases where we don't perform the named return
    value optimization, it occurred to me that it might be useful to have a
    warning for that.

    This does not fix PR58487, but might be interesting to people watching it.

            PR c++/58487

    gcc/c-family/ChangeLog:

            * c.opt: Add -Wnrvo.

    gcc/ChangeLog:

            * doc/invoke.texi: Document it.

    gcc/cp/ChangeLog:

            * typeck.cc (want_nrvo_p): New.
            (check_return_expr): Handle -Wnrvo.

    gcc/testsuite/ChangeLog:

            * g++.dg/opt/nrv25.C: New test.

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

* [Bug c++/58487] Missed return value optimization
  2013-09-20 19:38 [Bug c++/58487] New: Missed return value optimization jm at bourguet dot org
                   ` (3 preceding siblings ...)
  2023-06-07  1:33 ` cvs-commit at gcc dot gnu.org
@ 2023-06-07 22:08 ` cvs-commit at gcc dot gnu.org
  2023-06-09 15:42 ` cvs-commit at gcc dot gnu.org
  2023-12-19 20:45 ` jason at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-06-07 22:08 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The trunk branch has been updated by Jason Merrill <jason@gcc.gnu.org>:

https://gcc.gnu.org/g:28db36e2cfca1b7106adc8d371600fa3a325c4e2

commit r14-1624-g28db36e2cfca1b7106adc8d371600fa3a325c4e2
Author: Jason Merrill <jason@redhat.com>
Date:   Wed Jun 7 05:15:02 2023 -0400

    c++: allow NRV and non-NRV returns [PR58487]

    Now that we support NRV from an inner block, we can also support non-NRV
    returns from other blocks, since once the NRV is out of scope a later
return
    expression can't possibly alias it.

    This fixes 58487 and half-fixes 53637: now one of the returns is elided,
but
    not the other.

    Fixing the remaining xfails in these testcases will require a very
different
    approach, probably involving a full tree/block walk from finalize_nrv, and
    check_return_expr only adding to a list of potential return variables.

            PR c++/58487
            PR c++/53637

    gcc/cp/ChangeLog:

            * cp-tree.h (INIT_EXPR_NRV_P): New.
            * semantics.cc (finalize_nrv_r): Check it.
            * name-lookup.h (decl_in_scope_p): Declare.
            * name-lookup.cc (decl_in_scope_p): New.
            * typeck.cc (check_return_expr): Allow non-NRV
            returns if the NRV is no longer in scope.

    gcc/testsuite/ChangeLog:

            * g++.dg/opt/nrv26.C: New test.
            * g++.dg/opt/nrv26a.C: New test.
            * g++.dg/opt/nrv27.C: New test.

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

* [Bug c++/58487] Missed return value optimization
  2013-09-20 19:38 [Bug c++/58487] New: Missed return value optimization jm at bourguet dot org
                   ` (4 preceding siblings ...)
  2023-06-07 22:08 ` cvs-commit at gcc dot gnu.org
@ 2023-06-09 15:42 ` cvs-commit at gcc dot gnu.org
  2023-12-19 20:45 ` jason at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-06-09 15:42 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The trunk branch has been updated by Jason Merrill <jason@gcc.gnu.org>:

https://gcc.gnu.org/g:953bbeaeff050f4d0b670568a587aa1ce82ed711

commit r14-1660-g953bbeaeff050f4d0b670568a587aa1ce82ed711
Author: Jason Merrill <jason@redhat.com>
Date:   Fri Jun 9 10:37:35 2023 -0400

    c++: fix 32-bit spaceship failures [PR110185]

    Various spaceship tests failed after r14-1624.  This turned out to be
    because the comparison category classes return in memory on 32-bit targets,
    and the synthesized operator<=> looks something like

    if (auto v = a.x <=> b.x, v == 0); else return v;
    if (auto v = a.y <=> b.y, v == 0); else return v;
    etc.

    so check_return_expr was trying to do NRVO for all the 'v' variables, and
    now on subsequent returns we check to see if the previous NRV is still in
    scope.  But the NRVs didn't have names, so looking up name bindings
crashed.
    Fixed both by giving 'v' a name so we can NRVO the first one, and fixing
the
    test to give up if the old NRV has no name.

            PR c++/110185
            PR c++/58487

    gcc/cp/ChangeLog:

            * method.cc (build_comparison_op): Give retval a name.
            * typeck.cc (check_return_expr): Fix for nameless variables.

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

* [Bug c++/58487] Missed return value optimization
  2013-09-20 19:38 [Bug c++/58487] New: Missed return value optimization jm at bourguet dot org
                   ` (5 preceding siblings ...)
  2023-06-09 15:42 ` cvs-commit at gcc dot gnu.org
@ 2023-12-19 20:45 ` jason at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: jason at gcc dot gnu.org @ 2023-12-19 20:45 UTC (permalink / raw)
  To: gcc-bugs

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

Jason Merrill <jason at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jason at gcc dot gnu.org
   Target Milestone|---                         |14.0
         Resolution|---                         |FIXED
             Status|NEW                         |RESOLVED

--- Comment #9 from Jason Merrill <jason at gcc dot gnu.org> ---
Fixed for GCC 14.

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

end of thread, other threads:[~2023-12-19 20:45 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-09-20 19:38 [Bug c++/58487] New: Missed return value optimization jm at bourguet dot org
2021-06-28 16:36 ` [Bug c++/58487] " antoshkka at gmail dot com
2021-11-19 20:37 ` gcc_bugzilla at axeitado dot com
2023-06-06 16:11 ` jason at gcc dot gnu.org
2023-06-07  1:33 ` cvs-commit at gcc dot gnu.org
2023-06-07 22:08 ` cvs-commit at gcc dot gnu.org
2023-06-09 15:42 ` cvs-commit at gcc dot gnu.org
2023-12-19 20:45 ` jason 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).