<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 - Integer promotion quirk prevents efficient power of 2 division"
href="https://bugs.llvm.org/show_bug.cgi?id=43238">43238</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>Integer promotion quirk prevents efficient power of 2 division
</td>
</tr>
<tr>
<th>Product</th>
<td>clang
</td>
</tr>
<tr>
<th>Version</th>
<td>8.0
</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>LLVM Codegen
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>kim.nilsson@effnet.com
</td>
</tr>
<tr>
<th>CC</th>
<td>llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
</td>
</tr></table>
<p>
<div>
<pre>Created <span class=""><a href="attachment.cgi?id=22476" name="attach_22476" title="Example LLVM IR">attachment 22476</a> <a href="attachment.cgi?id=22476&action=edit" title="Example LLVM IR">[details]</a></span>
Example LLVM IR
For the function,
uint_fast32_t foo_a(uint_fast8_t x) {
const uint_fast32_t q = 1 << x;
return 256 / q;
}
GCC-{8.*,9.*} generates inefficient division instructions for both x86_64 and
ARM. Since 'q' is a power of 2, the result should be a single right shift.
Indeed, if the function is changed to,
uint_fast32_t foo_b(uint_fast8_t x) {
return 256 / (1 << x); // This is, of course, equivalent to (256 >> x)
},
the expected instructions are generated. Counter-intuitively, if the original
function is instead changed slightly to,
uint_fast32_t foo_c(uint_fast8_t x) {
const uint_fast32_t q = (uint_fast32_t)1 << x;
return 256 / q;
},
the expected instructions are once again generated.
See <a href="https://godbolt.org/z/X-It3b">https://godbolt.org/z/X-It3b</a> for examples.
(FWIW, GCC also has this behavior and I've reported the same thing there
<a href="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91680">https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91680</a>)</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>