[Bug c++/57175] New: NRVO and alignment
glisse at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Sun May 5 13:28:00 GMT 2013
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.
More information about the Gcc-bugs
mailing list