https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128659
Bug ID: 128659
Summary: [14/15/16/17 Regression] wrong code at -O1: y = g (y)
with a const by-value struct parameter, inliner maps
both the parameter and the return value to y
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Keywords: wrong-code
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: lzy20000419 at outlook dot com
Target Milestone: ---
Fails with trunk (gcc 17.0.0 20261111, a2a11abfad35) and with 12.1.0 through
16.2.0; 9.5.0 through 11.5.0 work.
Target: x86_64-pc-linux-gnu (also -m32).
struct S { long a, b, c, d; };
static inline S g (const S p)
{
return S{p.b, p.a, p.d, p.c};
}
int main ()
{
S y = { 1, 2, 3, 4 };
y = g (y);
if (y.a != 2 || y.b != 1 || y.c != 4 || y.d != 3)
__builtin_abort ();
return 0;
}
g++ -O1 t.C && ./a.out
Observed: abort, y is {2, 2, 4, 4}. Expected: exit 0, since g works on its own
copy of y and must return {2, 1, 4, 3}.
-O2, -O3 and -Og fail too. -O0 and -O1 -fno-inline work.
Godbolt (trunk, -O1 vs -O1 -fno-inline): https://godbolt.org/z/YY7hbo3xc
After einline, y = g (y) becomes
_14 = y.b; y.a = _14;
_15 = y.a; y.b = _15; <- reads the y.a just written
expand_call_inline makes two mappings that are each fine on their own:
1. setup_one_parameter maps the readonly, non-addressable parameter p directly
to the argument y, without a copy (since r12-434, "Reuse non-gimple_reg
variable for inlining").
2. declare_return_variable reuses the call LHS y as the RESULT_DECL.
Stores to <retval> then go to y, and later reads of p see the new values.
Possible fix: do not substitute a readonly parameter directly when its argument
is the base of the call LHS.
Related: PR100453 and PR111873 come from the same r12-434 change but are SRA
problems. This case involves no SRA.
Disclaimer: This test case was found with LLM-assisted targeted testing, and
the report was written with LLM assistance.