public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/57175] New: NRVO and alignment
@ 2013-05-05 13:28 glisse at gcc dot gnu.org
  2013-05-05 13:30 ` [Bug c++/57175] " paolo.carlini at oracle dot com
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: glisse at gcc dot gnu.org @ 2013-05-05 13:28 UTC (permalink / raw)
  To: gcc-bugs


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

             Bug #: 57175
           Summary: NRVO and alignment
    Classification: Unclassified
           Product: gcc
           Version: 4.9.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: glisse@gcc.gnu.org


Hello,

in typeck.c (check_return_expr), before applying the NRV optimization, there is
a check: DECL_ALIGN (retval) >= DECL_ALIGN (result)

It seems to me that this check is backwards and should be <= instead (compare
with tree_nrv in tree-nrv.c which seems correct).

#include <iostream>
struct A {
  A(){std::cerr<<"A()\n";}
  ~A(){std::cerr<<"~A()\n";}
  A(A&&){std::cerr<<"A(A&&)\n";}
  A(A const&){std::cerr<<"A(A const&)\n";}
};
A f(){
  alignas(32) A x;
  return x;
}
int main(){
  f();
}

If I understand NRVO properly, main reserves memory for the return value of f
and passes f the address of that memory. f then decides to use that memory
directly for x instead of allocating new memory that would have to be copied
later. So the return memory needs to be at least as aligned as x, not the
reverse (I think retval corresponds to x here).

Current g++ does the elision on this code. If I change >= to <=, it inhibits
the elision.


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

end of thread, other threads:[~2013-05-20  7:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-05-05 13:28 [Bug c++/57175] New: NRVO and alignment glisse at gcc dot gnu.org
2013-05-05 13:30 ` [Bug c++/57175] " paolo.carlini at oracle dot com
2013-05-05 23:34 ` paolo.carlini at oracle dot com
2013-05-20  7:52 ` glisse 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).