<html>
<head>
<base href="https://bugs.llvm.org/">
</head>
<body><table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Bug ID</th>
<td><a class="bz_bug_link
bz_status_NEW "
title="NEW - Memory allocation is not omitted in simple case"
href="https://bugs.llvm.org/show_bug.cgi?id=34797">34797</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>Memory allocation is not omitted in simple case
</td>
</tr>
<tr>
<th>Product</th>
<td>clang
</td>
</tr>
<tr>
<th>Version</th>
<td>trunk
</td>
</tr>
<tr>
<th>Hardware</th>
<td>PC
</td>
</tr>
<tr>
<th>OS</th>
<td>Linux
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>enhancement
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>C++14
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>antoshkka@gmail.com
</td>
</tr>
<tr>
<th>CC</th>
<td>llvm-bugs@lists.llvm.org
</td>
</tr></table>
<p>
<div>
<pre>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
ret
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;
}</pre>
</div>
</p>
<hr>
<span>You are receiving this mail because:</span>
<ul>
<li>You are on the CC list for the bug.</li>
</ul>
</body>
</html>