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

            Bug ID: 117341
           Summary: GCC 13.2.0 and 14.2.0 generate wrong code with -O3
           Product: gcc
           Version: 14.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: newsigma at 163 dot com
  Target Milestone: ---

Hi,

It seems GCC 13.2.0 and 14.2.0 generate wrong code in my case, while GCC 9.5.0
works fine.

GCC version: 13.2.0 and 14.2.0 have this bug, while 9.5.0 not
System: Ubuntu 24.04
GCC Build Option: Use gcc installed from apt. Detailed options are lengthy

Here is a minimal reproducer, simply copy them into a main.cpp.
Build command: gcc main.cpp -O3 -o main
```
#include <iostream>
#include <vector>

class Array {
    double arr[1];
public:
    Array() = default;
    Array(const Array&) = default;
    Array(Array&&) noexcept = default;
    ~Array() = default;
    /* Operators */
    [[nodiscard]] double& operator[](int index) { return arr[index]; }
    [[nodiscard]] const double& operator[](int index) const { return
arr[index]; }
    /* Operations */
    void swap(Array& __restrict array) noexcept {
        for (int i = 0; i < 1; ++i)
            std::swap(arr[i], array[i]);
    }
};

class Vector : public Array {};

int main() {
    std::vector<Vector> solution;
    solution.resize(4);
    solution[0][0] = 1;

    auto func = [](const Vector& y) -> Vector { return y; };
    for (int i = 0; i < 3; ++i) {
        const Vector& v = solution[i];
        Vector sum{};
        const double delta = func(v)[0] * 0;
        sum[0] = v[0] + delta;
        solution[i + 1].swap(sum);
    }

    for (auto e : solution)
        std::cout << e[0] << ' ';
    std::cout << std::endl;
    return 0;
}
```
Expected output(GCC 9.5.0): 1, 1, 1, 1
Wrong output(GCC 13.2.0 and 14.2.0): 1, 1, 0, 0

If I was wrong, please inform me. Thanks you.

Reply via email to