<html>
<head>
<base href="http://llvm.org/bugs/" />
</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 of shifts into roll/rorl when avoiding undefined behavior"
href="http://llvm.org/bugs/show_bug.cgi?id=17332">17332</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>Missed optimization of shifts into roll/rorl when avoiding undefined behavior
</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>All
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>normal
</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>jonathan.sauer@gmx.de
</td>
</tr>
<tr>
<th>CC</th>
<td>llvmbugs@cs.uiuc.edu
</td>
</tr>
<tr>
<th>Classification</th>
<td>Unclassified
</td>
</tr></table>
<p>
<div>
<pre>Created <span class=""><a href="attachment.cgi?id=11268" name="attach_11268" title="Disassembly of first rol/ror (no masking, roll and rorl)">attachment 11268</a> <a href="attachment.cgi?id=11268&action=edit" title="Disassembly of first rol/ror (no masking, roll and rorl)">[details]</a></span>
Disassembly of first rol/ror (no masking, roll and rorl)
The following two functions rotate an unsigned integer to the left and the
right, respectively:
unsigned int rol(unsigned int value, unsigned int amount)
{
return (value << amount) | (value >> (32 - amount));
}
unsigned int ror(unsigned int value, unsigned int amount)
{
return (value >> amount) | (value << (32 - amount));
}
On x86_64, clang r191183 with -O2 compiles them into a rol/ror instruction
(disassembly attached):
% ~/LLVM/build/Release+Asserts/bin/clang++ -S -O2 -o - clang.cpp
Unfortunately the code results in undefined behavior when <amount> is zero, as
then <value> gets shifted by 32 bits which, assuming <unsigned int> has a size
of 32 bits, is undefined behavior according to ยง5.8p1:
| The behavior is undefined if the right operand is negative, or greater than
| or equal to the length in bits of the promoted left operand
Of course it's easy to fix the functions by masking the lower five bits of
<amount>, which in this case results in no functionality change:
unsigned int rol(unsigned int value, unsigned int amount)
{
return (value << amount) | (value >> ((32 - amount) & 31));
}
unsigned int ror(unsigned int value, unsigned int amount)
{
return (value >> amount) | (value << ((32 - amount) & 31));
}
This however does not result in a rol/ror instruction (disassembly attached);
instead the code is compiled into two shifts, a negation and an or.
Now I'm a bit stuck between a rock and a hard place: On the one hand I want to
avoid undefined behavior (especially when using clang's UB sanitizer), on the
other hand I'd like clang to generate the optimum code.</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>