[Bug rtl-optimization/101708] New: Suboptimal codegen when clearing struct fields

cafxx+gcc.gnu.org at strayorange dot com gcc-bugzilla@gcc.gnu.org
Sat Jul 31 23:58:13 GMT 2021


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

            Bug ID: 101708
           Summary: Suboptimal codegen when clearing struct fields
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: cafxx+gcc.gnu.org at strayorange dot com
  Target Milestone: ---

GCC trunk (as well as 11.2) currently seem to be performing suboptimal codegen
in reset2 and reset3, that in theory should yield the same (or very similar to
the) result of reset1:

struct example {
    unsigned a;
    unsigned char b;
    unsigned c:1;
    unsigned d:2;
    unsigned char f; 
    unsigned char g;
    unsigned e;
};

void reset1(struct example *e) {
    e->b = 0,
    e->d = 0,
    e->e = 0,
    e->f = 0,
    e->g = 0;
}

void reset2(struct example *e) {
    *e = (struct example) {
        .a = e->a,
        .c = e->c
    };
}

void reset3(struct example *e) {
    struct example tmp = {
        .a = e->a,
        .c = e->c
    };
    *e = tmp;
}

compiled with -O2 -Wall yields:

reset1:
        and     QWORD PTR [rdi+4], 63744
        ret
reset2:
        movzx   eax, BYTE PTR [rdi+5]
        mov     edx, DWORD PTR [rdi]
        mov     DWORD PTR [rdi+8], 0
        mov     QWORD PTR [rdi], 0
        and     eax, 1
        mov     DWORD PTR [rdi], edx
        mov     BYTE PTR [rdi+5], al
        ret
reset3:
        mov     QWORD PTR [rsp-8], 0
        mov     eax, DWORD PTR [rdi]
        mov     DWORD PTR [rsp-12], eax
        movzx   eax, BYTE PTR [rdi+5]
        and     eax, 1
        mov     BYTE PTR [rsp-7], al
        mov     rax, QWORD PTR [rsp-12]
        mov     QWORD PTR [rdi], rax
        mov     eax, DWORD PTR [rsp-4]
        mov     DWORD PTR [rdi+8], eax
        ret

Godbolt link: https://godbolt.org/z/asr57TWqq

Bug reported at the request of @gnutools:
https://twitter.com/gnutools/status/1421525698515251207

I tentatively selected rtl-optimization as the component, but I'm not familiar
at all with the internals of GCC: feel free to update as needed.


More information about the Gcc-bugs mailing list