<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 when a constant is a power of two"
href="https://bugs.llvm.org/show_bug.cgi?id=42257">42257</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>Missed optimization when a constant is a power of two
</td>
</tr>
<tr>
<th>Product</th>
<td>new-bugs
</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>new bugs
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>jameshamm1995@gmail.com
</td>
</tr>
<tr>
<th>CC</th>
<td>htmldeveloper@gmail.com, llvm-bugs@lists.llvm.org
</td>
</tr></table>
<p>
<div>
<pre>The following function is correctly treated as a no-op by clang(trunk, -Ofast
--march=skylake)
bool zero(bool b) {
int x = 5;
return (x * b) + (-x * b);
}
=>
zero(bool):
xor eax, eax
ret
Messing around with this function on compiler explorer
(<a href="https://godbolt.org/z/grc5ZF">https://godbolt.org/z/grc5ZF</a>), I noticed when the constant is set to a power
of two (where the power is not 0 or 1) or the negative of a power of two, the
code is no longer optimized away.
bool zero(bool b) {
int x = 4;
return (x * b) + (-x * b);
}
=>
zero(bool):
shl edi, 2
cmp edi, edi ;; Always sets the ZF
setne al ;; ZF is set, so al is always 0
ret
This missed optimization still happens when the return type is an int, and the
missed optimization is not obvious from the assembly
(<a href="https://godbolt.org/z/ZEwONp">https://godbolt.org/z/ZEwONp</a>).
// Completely optimized
int zero(bool b) {
int x = 18;
return (x * b) + (-x * b);
}
=>
zero(bool):
xor eax, eax
ret
vs
// Missing opportunity
int zero(bool b) {
int x = 4;
return (x * b) + (-x * b);
}
=>
zero(bool):
mov eax, edi
shl eax, 2
xor dil, 1
movzx ecx, dil
lea eax, [rax + 4*rcx]
add eax, -4
ret
The problem disappears if the function parameter is widened,
int zero(int b) {
int x = 4;
return (x * b) + (-x * b);
}
or if the intermediate values are close to overflowing
(<a href="https://godbolt.org/z/yHPjz2">https://godbolt.org/z/yHPjz2</a>),
int zero(bool b) {
int x = 1 << 31;
return (x * b) + (-x * b);
}
or (as a comment on stackoverflow pointed out
<a href="https://stackoverflow.com/questions/56416747/missed-optimization-in-clang-only-on-powers-of-two#comment99429730_56416747">https://stackoverflow.com/questions/56416747/missed-optimization-in-clang-only-on-powers-of-two#comment99429730_56416747</a>)
if the value is not known.
int zero(bool b, int x) {
return (x * b) + (-x * b);
}
All compile down to
zero(int):
xor eax, eax
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>