[Bug c++/97474] New: Regression: optimization produces wrong code

sfranzen85 at hotmail dot com gcc-bugzilla@gcc.gnu.org
Sat Oct 17 20:42:35 GMT 2020


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

            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 <iostream>
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 *= 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-that-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.


More information about the Gcc-bugs mailing list