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

            Bug ID: 98335
           Summary: [9/10/11 Regression] Poor code generation for partial
                    struct initialization
           Product: gcc
           Version: 9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: mcolavita at fb dot com
  Target Milestone: ---

Consider the following code:

  struct Data {
    char a;
    int b;
  };

  char c;

  Data val(int idx) {
    return { c };
  }

On x86-64 (with sizeof(char) = 1 and sizeof(int) = 4), val() can be implemented
with a single mov to %rax. With -O3, g++ produces the following inefficient
output:

    xorl        %eax, %eax
    movb        $0, -18(%rsp)
    movabsq     $72057594037927935, %rdx
    movw        %ax, -20(%rsp)
    movl        $0, -24(%rsp)
    andq        -24(%rsp), %rdx
    movq        %rdx, %rax
    salq        $8, %rax
    movb        c(%rip), %al
    ret

Similar outputs are seen for any level of optimization above O0 on GCC 9, 10,
and 11. The bug is not present in GCC 8.

Reasonable code is generated if the second field of the struct is explicitly
initialized to a constant, either in the struct definition or the initializer.

Reply via email to