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

            Bug ID: 117545
           Summary: Addition of two integers incorrectly "optimized" to 16
                    SIMD instructions instead of a single "lea"
           Product: gcc
           Version: 14.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: svetli97 at gmail dot com
  Target Milestone: ---

In some cases passing two numbers to a function as a struct, adding them, and
returning the result causes the compiler to "optimize" the code to a complex
sequence of 16 SIMD instructions instead of a single "lea" as it does in the
other similar examples provided. The same bug happens for both C and C++
equivalent code. I also tested it with clang for reference and the bug doesn't
occur with that compiler.

The code that causes the problem in C++
(also at https://godbolt.org/z/orxa86KMW):

#include<cstdint>
struct S {
    uint64_t a;
    uint64_t b;
};

uint64_t add(uint64_t a, uint64_t b) {
    return a + b;
}

uint32_t add2(S vals) {
    return vals.a + vals.b;
}

uint64_t add3(S vals) {
    return vals.a + vals.b;
}

int main() {
    return 0;
}
// end of example

The functions "add" and "add2" are compiled to a single "lea" instruction, as
expected. But the function "add3" gets optimized to 16 SIMD instructions. I
suppose there is an error with evaluating the cost of certain instructions?

This bug occurs for gcc version 14 but not for 13. And only for optimization
levels -O2 and -O3, but not for -O1.

Reply via email to