[llvm-bugs] [Bug 34797] New: Memory allocation is not omitted in simple case

via llvm-bugs llvm-bugs at lists.llvm.org
Mon Oct 2 02:54:33 PDT 2017


            Bug ID: 34797
           Summary: Memory allocation is not omitted in simple case
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: C++14
          Assignee: unassignedclangbugs at nondot.org
          Reporter: antoshkka at gmail.com
                CC: llvm-bugs at lists.llvm.org

For the following function

unsigned lame_zero(unsigned count) {
    unsigned res = 0;

    // erase `count +` for omitting `call operator new`
    unsigned *p = new unsigned[count + 1]();

    res = p[0]; // or erase this line for omitting `call operator new`

    delete[] p;
    return res;

clang generates calls to `operator new` even in C++1z mode:

lame_zero(unsigned int): # @lame_zero(unsigned int)
  push r14
  push rbx
  push rax
  mov ebx, edi
  inc ebx
  shl rbx, 2
  mov rdi, rbx
  call operator new[](unsigned long)
  mov r14, rax
  xor esi, esi
  mov rdi, r14
  mov rdx, rbx
  call memset
  mov ebx, dword ptr [r14]
  mov rdi, r14
  call operator delete[](void*)
  mov eax, ebx
  add rsp, 8
  pop rbx
  pop r14

This seems suboptimal and affects common code, like std::stable_sort usages:

#include <algorithm>
#include <array>

std::array<unsigned, 6> my_sorted_array() {
    std::array<unsigned, 6> data = {1, 2, 7, 7, 7, 0};

    // Multiple calls to operator new[] are not omitted
    std::stable_sort(data.begin(), data.end());

    return data;

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/20171002/7f7b2549/attachment.html>

More information about the llvm-bugs mailing list