public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/51571] New: No named return value optimization while adding a dummy scope
@ 2011-12-15 16:44 prasoonsaurav.nit at gmail dot com
  2011-12-15 18:26 ` [Bug c++/51571] " prasoonsaurav.nit at gmail dot com
                   ` (13 more replies)
  0 siblings, 14 replies; 15+ messages in thread
From: prasoonsaurav.nit at gmail dot com @ 2011-12-15 16:44 UTC (permalink / raw)
  To: gcc-bugs

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

             Bug #: 51571
           Summary: No named return value optimization while adding a
                    dummy scope
    Classification: Unclassified
           Product: gcc
           Version: 4.6.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: prasoonsaurav.nit@gmail.com


Simple code snippet

#include <iostream>
int global;
struct A
{
   A(){}
   A(const A&x){
       ++global;
   }
   ~A(){}
};
A foo()
{  
     A a;
     return a;  
}
int main()
{
   A x = foo();
   std::cout << global;
}
Output : 0

When the definition of foo is changed to

A foo()
{ 
  { 
     A a;
     return a;  
  }
}
I get 1 as the output i.e copy c-tor gets called once.

Compiler is not optimizing the call to the copy c-tor in this case.


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

* [Bug c++/51571] No named return value optimization while adding a dummy scope
  2011-12-15 16:44 [Bug c++/51571] New: No named return value optimization while adding a dummy scope prasoonsaurav.nit at gmail dot com
@ 2011-12-15 18:26 ` prasoonsaurav.nit at gmail dot com
  2011-12-15 22:08 ` pinskia at gcc dot gnu.org
                   ` (12 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: prasoonsaurav.nit at gmail dot com @ 2011-12-15 18:26 UTC (permalink / raw)
  To: gcc-bugs

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

Prasoon <prasoonsaurav.nit at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |critical


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

* [Bug c++/51571] No named return value optimization while adding a dummy scope
  2011-12-15 16:44 [Bug c++/51571] New: No named return value optimization while adding a dummy scope prasoonsaurav.nit at gmail dot com
  2011-12-15 18:26 ` [Bug c++/51571] " prasoonsaurav.nit at gmail dot com
@ 2011-12-15 22:08 ` pinskia at gcc dot gnu.org
  2011-12-18 11:03 ` paolo.carlini at oracle dot com
                   ` (11 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: pinskia at gcc dot gnu.org @ 2011-12-15 22:08 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |missed-optimization
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2011-12-15
     Ever Confirmed|0                           |1
      Known to fail|                            |4.2.3, 4.7.0
           Severity|critical                    |normal

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> 2011-12-15 21:59:31 UTC ---
Confirmed, this is just a missed optimization and not very critical really.


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

* [Bug c++/51571] No named return value optimization while adding a dummy scope
  2011-12-15 16:44 [Bug c++/51571] New: No named return value optimization while adding a dummy scope prasoonsaurav.nit at gmail dot com
  2011-12-15 18:26 ` [Bug c++/51571] " prasoonsaurav.nit at gmail dot com
  2011-12-15 22:08 ` pinskia at gcc dot gnu.org
@ 2011-12-18 11:03 ` paolo.carlini at oracle dot com
  2013-06-02  7:36 ` guillaume.melquiond at inria dot fr
                   ` (10 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: paolo.carlini at oracle dot com @ 2011-12-18 11:03 UTC (permalink / raw)
  To: gcc-bugs

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

Paolo Carlini <paolo.carlini at oracle dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jason at gcc dot gnu.org

--- Comment #2 from Paolo Carlini <paolo.carlini at oracle dot com> 2011-12-18 10:55:08 UTC ---
Adding Jason, I seem to remember he did NRVO on trees.


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

* [Bug c++/51571] No named return value optimization while adding a dummy scope
  2011-12-15 16:44 [Bug c++/51571] New: No named return value optimization while adding a dummy scope prasoonsaurav.nit at gmail dot com
                   ` (2 preceding siblings ...)
  2011-12-18 11:03 ` paolo.carlini at oracle dot com
@ 2013-06-02  7:36 ` guillaume.melquiond at inria dot fr
  2015-02-04 22:40 ` marc at kdab dot com
                   ` (9 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: guillaume.melquiond at inria dot fr @ 2013-06-02  7:36 UTC (permalink / raw)
  To: gcc-bugs

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

Guillaume Melquiond <guillaume.melquiond at inria dot fr> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |guillaume.melquiond at inria dot f
                   |                            |r

--- Comment #3 from Guillaume Melquiond <guillaume.melquiond at inria dot fr> ---
I have recently encountered a similar problem, but in a much more general case.

struct A {
    A(int);
    A(A const &);
    ~A();
};

A f(bool b)
{
    if (b) return A(0);
    A a(1);
    return a;
}

All the return statements dominated by variable "a" return "a", so its
construction should happen in-place, hence eliding copy-construction and
destruction. Unfortunately, this is not what happens with g++ 4.8.0.

Interestingly enough, if one uninlines the code by hand, g++ actually generates
the optimal code, so it is possible though cumbersome to work around the missed
optimization:

inline A f2()
{
    A a(1);
    return a;
}

A f1(bool b)
{
    if (b) return A(0);
        return f2();
}

produces

A f1(bool) (bool b)
{
  <bb 2>:
  if (b_2(D) != 0)
    goto <bb 3>;
  else
    goto <bb 4>;

  <bb 3>:
  A::A (_4(D), 0);
  goto <bb 5>;

  <bb 4>:
  A::A (_4(D), 1);

  <bb 5>:
  return _4(D);
}


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

* [Bug c++/51571] No named return value optimization while adding a dummy scope
  2011-12-15 16:44 [Bug c++/51571] New: No named return value optimization while adding a dummy scope prasoonsaurav.nit at gmail dot com
                   ` (3 preceding siblings ...)
  2013-06-02  7:36 ` guillaume.melquiond at inria dot fr
@ 2015-02-04 22:40 ` marc at kdab dot com
  2020-07-05  9:44 ` glisse at gcc dot gnu.org
                   ` (8 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: marc at kdab dot com @ 2015-02-04 22:40 UTC (permalink / raw)
  To: gcc-bugs

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

marc at kdab dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |marc at kdab dot com

--- Comment #4 from marc at kdab dot com ---
I would like to strongly oppose the notion that this "just a missed
optimisation and not very critical, really".

NRVO is not just "an optimisation". It's actually one that is explcitly
permitted to change observable behaviour of the program and it's extremely
powerful.

And it it _required_ for performant C++. Just try to return a std::vector by
value to see the importance of this optimisation. This is not missed
optimisation. This is premature pessimisation.

You could just as well stop all optimisation work for the C++ frontend until
this is implemented, because any other optimisation effords are dwarfed by the
overhead when NRVO is expected by the developer but not applied.

Please make this a top priority. Every C++ program will benefit both in text
size and in runtime performance - dramatically.


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

* [Bug c++/51571] No named return value optimization while adding a dummy scope
  2011-12-15 16:44 [Bug c++/51571] New: No named return value optimization while adding a dummy scope prasoonsaurav.nit at gmail dot com
                   ` (4 preceding siblings ...)
  2015-02-04 22:40 ` marc at kdab dot com
@ 2020-07-05  9:44 ` glisse at gcc dot gnu.org
  2020-10-26 16:15 ` mpolacek at gcc dot gnu.org
                   ` (7 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: glisse at gcc dot gnu.org @ 2020-07-05  9:44 UTC (permalink / raw)
  To: gcc-bugs

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

Marc Glisse <glisse at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |b7.10110111 at gmail dot com

--- Comment #6 from Marc Glisse <glisse at gcc dot gnu.org> ---
*** Bug 96065 has been marked as a duplicate of this bug. ***

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

* [Bug c++/51571] No named return value optimization while adding a dummy scope
  2011-12-15 16:44 [Bug c++/51571] New: No named return value optimization while adding a dummy scope prasoonsaurav.nit at gmail dot com
                   ` (5 preceding siblings ...)
  2020-07-05  9:44 ` glisse at gcc dot gnu.org
@ 2020-10-26 16:15 ` mpolacek at gcc dot gnu.org
  2020-10-26 16:16 ` mpolacek at gcc dot gnu.org
                   ` (6 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2020-10-26 16:15 UTC (permalink / raw)
  To: gcc-bugs

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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |barry.revzin at gmail dot com

--- Comment #7 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
*** Bug 97562 has been marked as a duplicate of this bug. ***

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

* [Bug c++/51571] No named return value optimization while adding a dummy scope
  2011-12-15 16:44 [Bug c++/51571] New: No named return value optimization while adding a dummy scope prasoonsaurav.nit at gmail dot com
                   ` (6 preceding siblings ...)
  2020-10-26 16:15 ` mpolacek at gcc dot gnu.org
@ 2020-10-26 16:16 ` mpolacek at gcc dot gnu.org
  2023-06-06 16:12 ` jason at gcc dot gnu.org
                   ` (5 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2020-10-26 16:16 UTC (permalink / raw)
  To: gcc-bugs

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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mpolacek at gcc dot gnu.org

--- Comment #8 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
See bug 97562 for a nice testcase.

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

* [Bug c++/51571] No named return value optimization while adding a dummy scope
  2011-12-15 16:44 [Bug c++/51571] New: No named return value optimization while adding a dummy scope prasoonsaurav.nit at gmail dot com
                   ` (7 preceding siblings ...)
  2020-10-26 16:16 ` mpolacek at gcc dot gnu.org
@ 2023-06-06 16:12 ` jason at gcc dot gnu.org
  2023-06-07  1:33 ` cvs-commit at gcc dot gnu.org
                   ` (4 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: jason at gcc dot gnu.org @ 2023-06-06 16:12 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jason at gcc dot gnu.org
             Status|NEW                         |ASSIGNED
           Assignee|unassigned at gcc dot gnu.org      |jason at gcc dot gnu.org

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

* [Bug c++/51571] No named return value optimization while adding a dummy scope
  2011-12-15 16:44 [Bug c++/51571] New: No named return value optimization while adding a dummy scope prasoonsaurav.nit at gmail dot com
                   ` (8 preceding siblings ...)
  2023-06-06 16:12 ` jason at gcc dot gnu.org
@ 2023-06-07  1:33 ` cvs-commit at gcc dot gnu.org
  2023-06-07  1:35 ` jason at gcc dot gnu.org
                   ` (3 subsequent siblings)
  13 siblings, 0 replies; 15+ 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=51571

--- Comment #9 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:7e0b65b239c3a0d68ce94896b236b03de666ffd6

commit r14-1593-g7e0b65b239c3a0d68ce94896b236b03de666ffd6
Author: Jason Merrill <jason@redhat.com>
Date:   Sun Jun 4 12:09:11 2023 -0400

    c++: enable NRVO from inner block [PR51571]

    Our implementation of the named return value optimization has been limited
    to variables declared in the outermost block of the function, to avoid
    needing to handle the case where the variable needs to be destroyed due to
    going out of scope.  PR92407 pointed out a case we were missing, where the
    variable goes out of scope due to a goto and we were failing to destroy it.

    It occurred to me that this problem is the flip side of PR33799, where we
    need to be sure to destroy the return value if a cleanup throws on return;
    here we want to avoid destroying the return value when exiting the
    variable's scope on return.  We can use the same flag to indicate to both
    cleanups that we're returning.

    This implements the guaranteed copy elision specified by P2025 (which is
not
    yet part of the draft standard).

            PR c++/51571
            PR c++/92407

    gcc/cp/ChangeLog:

            * decl.cc (finish_function): Simplify NRV handling.
            * except.cc (maybe_set_retval_sentinel): Also set if NRV.
            (maybe_splice_retval_cleanup): Don't add the cleanup region
            if we don't need it.
            * semantics.cc (nrv_data): Add simple field.
            (finalize_nrv): Set it.
            (finalize_nrv_r): Check it and retval sentinel.
            * cp-tree.h (finalize_nrv): Adjust declaration.
            * typeck.cc (check_return_expr): Remove named_labels check.

    gcc/testsuite/ChangeLog:

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

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

* [Bug c++/51571] No named return value optimization while adding a dummy scope
  2011-12-15 16:44 [Bug c++/51571] New: No named return value optimization while adding a dummy scope prasoonsaurav.nit at gmail dot com
                   ` (9 preceding siblings ...)
  2023-06-07  1:33 ` cvs-commit at gcc dot gnu.org
@ 2023-06-07  1:35 ` jason at gcc dot gnu.org
  2023-06-07 20:41 ` jason at gcc dot gnu.org
                   ` (2 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: jason at gcc dot gnu.org @ 2023-06-07  1:35 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |14.0

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

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

* [Bug c++/51571] No named return value optimization while adding a dummy scope
  2011-12-15 16:44 [Bug c++/51571] New: No named return value optimization while adding a dummy scope prasoonsaurav.nit at gmail dot com
                   ` (10 preceding siblings ...)
  2023-06-07  1:35 ` jason at gcc dot gnu.org
@ 2023-06-07 20:41 ` jason at gcc dot gnu.org
  2023-10-17 10:29 ` rguenth at gcc dot gnu.org
  2023-12-19 19:50 ` jason at gcc dot gnu.org
  13 siblings, 0 replies; 15+ messages in thread
From: jason at gcc dot gnu.org @ 2023-06-07 20:41 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #11 from Jason Merrill <jason at gcc dot gnu.org> ---
(In reply to CVS Commits from comment #9)
>     This implements the guaranteed copy elision specified by P2025

Or not; I just noticed that P2025 also requires a fix for PR53637.

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

* [Bug c++/51571] No named return value optimization while adding a dummy scope
  2011-12-15 16:44 [Bug c++/51571] New: No named return value optimization while adding a dummy scope prasoonsaurav.nit at gmail dot com
                   ` (11 preceding siblings ...)
  2023-06-07 20:41 ` jason at gcc dot gnu.org
@ 2023-10-17 10:29 ` rguenth at gcc dot gnu.org
  2023-12-19 19:50 ` jason at gcc dot gnu.org
  13 siblings, 0 replies; 15+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-10-17 10:29 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|14.0                        |---

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

* [Bug c++/51571] No named return value optimization while adding a dummy scope
  2011-12-15 16:44 [Bug c++/51571] New: No named return value optimization while adding a dummy scope prasoonsaurav.nit at gmail dot com
                   ` (12 preceding siblings ...)
  2023-10-17 10:29 ` rguenth at gcc dot gnu.org
@ 2023-12-19 19:50 ` jason at gcc dot gnu.org
  13 siblings, 0 replies; 15+ messages in thread
From: jason at gcc dot gnu.org @ 2023-12-19 19:50 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |14.0
             Status|ASSIGNED                    |RESOLVED
         Resolution|---                         |FIXED

--- Comment #12 from Jason Merrill <jason at gcc dot gnu.org> ---
But this particular issue is indeed fixed for 14.

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

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

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-12-15 16:44 [Bug c++/51571] New: No named return value optimization while adding a dummy scope prasoonsaurav.nit at gmail dot com
2011-12-15 18:26 ` [Bug c++/51571] " prasoonsaurav.nit at gmail dot com
2011-12-15 22:08 ` pinskia at gcc dot gnu.org
2011-12-18 11:03 ` paolo.carlini at oracle dot com
2013-06-02  7:36 ` guillaume.melquiond at inria dot fr
2015-02-04 22:40 ` marc at kdab dot com
2020-07-05  9:44 ` glisse at gcc dot gnu.org
2020-10-26 16:15 ` mpolacek at gcc dot gnu.org
2020-10-26 16:16 ` mpolacek at gcc dot gnu.org
2023-06-06 16:12 ` jason at gcc dot gnu.org
2023-06-07  1:33 ` cvs-commit at gcc dot gnu.org
2023-06-07  1:35 ` jason at gcc dot gnu.org
2023-06-07 20:41 ` jason at gcc dot gnu.org
2023-10-17 10:29 ` rguenth at gcc dot gnu.org
2023-12-19 19:50 ` 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).