From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id E537C3857C4E; Sat, 17 Oct 2020 20:42:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E537C3857C4E From: "sfranzen85 at hotmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/97474] New: Regression: optimization produces wrong code Date: Sat, 17 Oct 2020 20:42:35 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 11.0 X-Bugzilla-Keywords: wrong-code X-Bugzilla-Severity: normal X-Bugzilla-Who: sfranzen85 at hotmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status keywords bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 17 Oct 2020 20:42:36 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D97474 Bug ID: 97474 Summary: Regression: optimization produces wrong code Product: gcc Version: 11.0 Status: UNCONFIRMED Keywords: wrong-code Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: sfranzen85 at hotmail dot com Target Milestone: --- While searching for something different I found the following example code demonstrating a possible gcc bug: --- #include using std::cout; struct A { int a; int& b; A(int x) : a(x), b(a) {} A(const A& other) : a(other.a), b(a) {} A() : a(0), b(a) {} }; int foo(A a) { a.a *=3D a.b; return a.b; } int main() { A a(3); cout << foo(a) << '\n'; return 0; } --- (Source: https://stackoverflow.com/questions/62853805/why-does-modifying-a-field-tha= t-is-referenced-by-another-variable-lead-to-unexpe) I was unable to find a related bug report, hence this one. Wrong output (3 instead of 9) is produced with -O1 or higher, since gcc version 6.4, as mentioned in a comment. Indeed gcc trunk on godbolt still generates faulty code: https://godbolt.org/z/TcedxE.=