[llvm-bugs] [Bug 37351] New: Missed optimization for value-init of variable-sized allocation

via llvm-bugs llvm-bugs at lists.llvm.org
Mon May 7 07:41:10 PDT 2018


https://bugs.llvm.org/show_bug.cgi?id=37351

            Bug ID: 37351
           Summary: Missed optimization for value-init of variable-sized
                    allocation
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: C++
          Assignee: unassignedclangbugs at nondot.org
          Reporter: redbeard0531 at gmail.com
                CC: dgregor at apple.com, llvm-bugs at lists.llvm.org

https://godbolt.org/g/6yZEKf (compiled with -O3)


char* variableSize(long n) {
    auto p = new char[n]();
    for (int i = 0; i < n; i++) {
        p[i] = 0xff;
    }
    return p;
}

char* small() {
    return variableSize(8);
}

char* large() {
    return variableSize(10'000);
}


The variableSize() case generates two calls two memset with the second being
conditional on n > 0:

variableSize(long): # @variableSize(long)
  push r14
  push rbx
  push rax
  mov rbx, rdi
  call operator new[](unsigned long)
  mov r14, rax
  xor esi, esi
  mov rdi, rax
  mov rdx, rbx
  call memset
  test rbx, rbx
  jle .LBB0_2
  mov esi, 255
  mov rdi, r14
  mov rdx, rbx
  call memset
.LBB0_2:
  mov rax, r14
  add rsp, 8
  pop rbx
  pop r14
  ret

Note that when clang can see the allocation size it *does* elide the initial
memset:

small(): # @small()
  push rax
  mov edi, 8
  call operator new[](unsigned long)
  mov qword ptr [rax], -1
  pop rcx
  ret
large(): # @large()
  push rbx
  mov edi, 10000
  call operator new[](unsigned long)
  mov rbx, rax
  mov esi, 255
  mov edx, 10000
  mov rdi, rax
  call memset
  mov rax, rbx
  pop rbx
  ret

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20180507/ab863b4e/attachment.html>


More information about the llvm-bugs mailing list