<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 - Missed optimization for value-init of variable-sized allocation"
href="https://bugs.llvm.org/show_bug.cgi?id=37351">37351</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>Missed optimization for value-init of variable-sized allocation
</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++
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>redbeard0531@gmail.com
</td>
</tr>
<tr>
<th>CC</th>
<td>dgregor@apple.com, llvm-bugs@lists.llvm.org
</td>
</tr></table>
<p>
<div>
<pre><a href="https://godbolt.org/g/6yZEKf">https://godbolt.org/g/6yZEKf</a> (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</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>