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

            Bug ID: 114176
           Summary: Failure to optimize out useless stack spill when array
                    is present in union
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gabravier at gmail dot com
  Target Milestone: ---

struct Vec3
{
    union {
        float v[3];
        float x, y, z;
    };
};

float Vec3Dot(struct Vec3 vec1, struct Vec3 vec2)
{
    return vec1.x;
}

on x86-64, with -O3, GCC outputs:

Vec3Dot:
  movq QWORD PTR [rsp-16], xmm0
  movss xmm0, DWORD PTR [rsp-16]
  ret

LLVM instead outputs this:

Vec3Dot:
  ret

Stack spilling here seems to occur for no reason when the `float v[3];`
declaration is present.

This looks like an RTL issue to me as the final optimized tree pass looks the
same with or without the `float v[3];` declaration being present, and the issue
seems to be present on multiple targets, though to different degrees: only on
x86-64 have I seen it actually result in a stack spill (on e.g. AArch64 and
RISC-V, it only makes GCC outputs unnecessary instructions to adjust `sp` but
does not actually spill the value).

Reply via email to