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

            Bug ID: 110459
           Summary: Trivial on stack variable was not optimized away
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: antoshkka at gmail dot com
  Target Milestone: ---

Consider the example:

struct array {
    char data[4];
};

auto sample2(char c) {
  array buffer = {c, 0, 0, 0};
  return buffer;
}


With GCC-14 and -O2 it produces the following assembly:

sample2(char):
        xor     eax, eax
        mov     BYTE PTR [rsp-22], 0
        mov     WORD PTR [rsp-24], ax
        mov     eax, DWORD PTR [rsp-24]
        sal     eax, 8
        mov     al, dil
        ret


It could be further optimized to just:

sample2(char):
        movzx   eax, dil
        ret


Godbolt playground: https://godbolt.org/z/nxKhvo3ns

Reply via email to