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

            Bug ID: 108516
           Summary: Useless movzx instruction emitted when loading 8 bits
                    from 24 bit struct
           Product: gcc
           Version: 12.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jzwinck at gmail dot com
  Target Milestone: ---

This code produces an extra instruction in GCC 11 and 12 (but not 10 or
before):

    #include <stdint.h>

    struct S
    {
        uint8_t e1;
        uint8_t e2;
        uint8_t e3;
    };

    uint32_t f2(S s) { return s.e2; }

The generated code is:

        mov     eax, edi
        movzx   eax, ah
        movzx   eax, al
        ret

The movzx from "al" is useless: it zeros the high 24 bits of eax which are
known to be zero after the prior movzx.  GCC 10 and earlier do not emit the
useless instruction, and neither do GCC 11 or 12 if the struct contains 4 bytes
instead of 3.

Demo: https://godbolt.org/z/Wajo86GfM

Reply via email to