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

            Bug ID: 65220
           Summary: Regression: integer division in stack alignment for
                    VLA allocation
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: fweimer at redhat dot com

This sample program:

int f(void *);

void g(void)
{
  unsigned size = 128;
  while (1)
    {
      unsigned buf[size];
      if (f(buf))
    break;
      size *= 2;
    }
}

results in the following x86_64 machine code:

g:
.LFB0:
    .cfi_startproc
    pushq    %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    movq    %rsp, %rbp
    .cfi_def_cfa_register 6
    pushq    %r13
    pushq    %r12
    pushq    %rbx
    .cfi_offset 13, -24
    .cfi_offset 12, -32
    .cfi_offset 3, -40
    movl    $128, %r13d
    movl    $16, %ebx
    subq    $8, %rsp
    jmp    .L3
    .p2align 4,,10
    .p2align 3
.L6:
    addl    %r13d, %r13d
    movq    %r12, %rsp
.L3:
    movl    %r13d, %eax
    xorl    %edx, %edx
    movq    %rsp, %r12
    leaq    18(,%rax,4), %rax
    divq    %rbx
    salq    $4, %rax
    subq    %rax, %rsp
    movq    %rsp, %rdi
    call    f
    testl    %eax, %eax
    je    .L6
    leaq    -24(%rbp), %rsp
    popq    %rbx
    popq    %r12
    popq    %r13
    popq    %rbp
    .cfi_def_cfa 7, 8
    ret
    .cfi_endproc

Note the divq instruction.  This happens with GCC 4.8, 4.9 and current trunk
(r221002).  I see no reason why GCC couldn't emit a shift instead of a
division.  GCC 4.4 generated slightly different code, and at least it had no
divq instruction.

This happens with i386 as well.

Reply via email to