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

            Bug ID: 116574
           Summary: GCC generates redundant moves in simple vector
                    addition
           Product: gcc
           Version: 14.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: bernardo at bernardosulzbach dot com
  Target Milestone: ---

When given

  #include <array>
  using V = std::array<float, 2>;
  V add(V v1, V v2) { return {v1[0] + v2[0], v1[1] + v2[1]}; }

GCC 14.2 with -Os produces
    add(std::array<float, 2ul>, std::array<float, 2ul>):
        movq    rax, xmm0
        movq    rdx, xmm1
        addss   xmm0, xmm1
        shr     rax, 32
        shr     rdx, 32
        movd    xmm3, edx
        movd    xmm2, eax
        addss   xmm2, xmm3
        movd    edx, xmm0
        movd    eax, xmm2
        sal     rax, 32
        or      rdx, rax
        movq    xmm0, rdx
        ret

and with -O2 or -O3 produces
    add(std::array<float, 2ul>, std::array<float, 2ul>):
        movq    xmm1, xmm1
        movq    xmm0, xmm0
        addps   xmm0, xmm1
        ret

while Clang always produces
    add(std::array<float, 2ul>, std::array<float, 2ul>):
        addps   xmm0, xmm1
        ret

as I'd expect.

Compiler Explorer (godbolt.org) link (for convenience, it doesn't have anything
I didn't write here): https://godbolt.org/z/chsqqjPPx.

The -Os version should probably not generate so much code, and I think the
`movq {x}, {x}` in the -O2/-O3 version are redundant.

I picked C++ as the component, because I don't know what component to blame for
this.

Reply via email to