public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/102704] New: NRVO for throw expression
@ 2021-10-12 13:39 vanyacpp at gmail dot com
  2021-10-13  6:22 ` [Bug c++/102704] " rguenth at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: vanyacpp at gmail dot com @ 2021-10-12 13:39 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 102704
           Summary: NRVO for throw expression
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vanyacpp at gmail dot com
  Target Milestone: ---

Consider this code:

struct mytype
{
    mytype();
    mytype(mytype const&);
    mytype(mytype&&);
};

void test()
{
    mytype e;
    throw e;
}

Currently for function test() GCC generates the following sequence of calls
(pseudocode):

    char e[sizeof(mytype)];
    mytype_default_ctor(e);
    p = __cxa_allocate_exception();
    mytype_move_ctor(p, e);
    __cxa_throw(p);

I believe a trick similar to NRVO for returns can be made here. When a variable
meets NRVO criteria, compiler can remove the local variable and replace it with
a storage allocated by __cxa_allocate_exception. Here what I believe can be
generated:

    p = __cxa_allocate_exception();
    mytype_default_ctor(p);
    __cxa_throw(p);

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

* [Bug c++/102704] NRVO for throw expression
  2021-10-12 13:39 [Bug c++/102704] New: NRVO for throw expression vanyacpp at gmail dot com
@ 2021-10-13  6:22 ` rguenth at gcc dot gnu.org
  2021-10-13  8:43 ` redi at gcc dot gnu.org
  2021-10-13  8:47 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: rguenth at gcc dot gnu.org @ 2021-10-13  6:22 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |missed-optimization

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
I think that's going to be quite difficult to implement despite looking "easy"
for the testcase at hand.

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

* [Bug c++/102704] NRVO for throw expression
  2021-10-12 13:39 [Bug c++/102704] New: NRVO for throw expression vanyacpp at gmail dot com
  2021-10-13  6:22 ` [Bug c++/102704] " rguenth at gcc dot gnu.org
@ 2021-10-13  8:43 ` redi at gcc dot gnu.org
  2021-10-13  8:47 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: redi at gcc dot gnu.org @ 2021-10-13  8:43 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2021-10-13

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
[class.copy.elision] p1:
This elision of copy/move operations, called copy elision, is permitted in the
following circumstances (which may be combined to eliminate multiple copies):
-- in a return statement [...]
-- in a throw-expression (7.6.18), when the operand is the name of a
non-volatile object with automatic storage duration (other than a function or
catch-clause parameter) that belongs to a scope that does not contain the
innermost enclosing compound-statement associated with a try-block (if there is
one), the copy/move operation can be omitted by constructing the object
directly into the exception object
-- in a coroutine [...]
-- when the exception-declaration of an exception handler [...]


The slightly confusing "does not contain the innermost enclosing
compound-statement" means you can't elide if the throw is in a try-block and
the variable was defined outside it.

So this cannot be elided:

void f()
{
  mytype x;
  try {
    throw x;
  } catch (int) {
  }
}

But this can be:

void g()
{
  try {
    mytype x;
    throw x;
  } catch (int) {
  }
}

And the example in comment 0 can be.

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

* [Bug c++/102704] NRVO for throw expression
  2021-10-12 13:39 [Bug c++/102704] New: NRVO for throw expression vanyacpp at gmail dot com
  2021-10-13  6:22 ` [Bug c++/102704] " rguenth at gcc dot gnu.org
  2021-10-13  8:43 ` redi at gcc dot gnu.org
@ 2021-10-13  8:47 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-10-13  8:47 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement

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

end of thread, other threads:[~2021-10-13  8:47 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-12 13:39 [Bug c++/102704] New: NRVO for throw expression vanyacpp at gmail dot com
2021-10-13  6:22 ` [Bug c++/102704] " rguenth at gcc dot gnu.org
2021-10-13  8:43 ` redi at gcc dot gnu.org
2021-10-13  8:47 ` 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).