This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/83384] New: Optimize heap allocation as stack allocation


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

            Bug ID: 83384
           Summary: Optimize heap allocation as stack allocation
           Product: gcc
           Version: 8.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: zamazan4ik at tut dot by
  Target Milestone: ---

gcc(trunk) with '--std=c++17 -O3' for this code:


int test(int a)
{
    int res = 0;
    int* i = new int;

    for(*i = 0; *i < a; *i = *i + 1)
    {
        res += *i;
    }

    return res;
}


generates this:


test(int):
        push    rbx
        mov     ebx, edi
        mov     edi, 4
        call    operator new(unsigned long)
        test    ebx, ebx
        jle     .L8
        lea     eax, [rbx-1]
        cmp     eax, 17
        jbe     .L9
        mov     edx, ebx
        movdqa  xmm1, XMMWORD PTR .LC0[rip]
        xor     eax, eax
        pxor    xmm0, xmm0
        movdqa  xmm2, XMMWORD PTR .LC1[rip]
        shr     edx, 2
.L5:
        add     eax, 1
        paddd   xmm0, xmm1
        paddd   xmm1, xmm2
        cmp     eax, edx
        jne     .L5
        movdqa  xmm1, xmm0
        mov     edx, ebx
        psrldq  xmm1, 8
        and     edx, -4
        paddd   xmm0, xmm1
        movdqa  xmm1, xmm0
        psrldq  xmm1, 4
        paddd   xmm0, xmm1
        movd    eax, xmm0
        cmp     ebx, edx
        je      .L1
.L7:
        add     eax, edx
        add     edx, 1
        cmp     ebx, edx
        jg      .L7
.L1:
        pop     rbx
        ret
.L8:
        xor     eax, eax
        pop     rbx
        ret
.L9:
        xor     eax, eax
        xor     edx, edx
        jmp     .L7
.LC0:
        .long   0
        .long   1
        .long   2
        .long   3
.LC1:
        .long   4
        .long   4
        .long   4
        .long   4


clang(trunk) with '--std=c++17 -O3':


test(int): # @test(int)
  test edi, edi
  jle .LBB0_1
  lea eax, [rdi - 1]
  lea ecx, [rdi - 2]
  imul rcx, rax
  shr rcx
  lea eax, [rcx + rdi]
  add eax, -1
  ret
.LBB0_1:
  xor eax, eax
  ret



>From C++14 compiler can remove heap allocation and use allocation on stack.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]