<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 when testing for a constant remainder"
href="http://llvm.org/bugs/show_bug.cgi?id=19206">19206</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>Missed optimization when testing for a constant remainder
</td>
</tr>
<tr>
<th>Product</th>
<td>libraries
</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>normal
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>Common Code Generator Code
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>jn@sirrida.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>Consider the expression (x % d) == c where d and c are constants.
For simplicity let us assume that x is unsigned and 0 <= c < d.
Let us further assume that d = a * (1 << b) and a is odd.
Then our expression can be transformed to
rotate_right(x-c, b) * inverse_mul(a) <= (high_value(x) - c) / d .
Example [(x % 250) == 3]:
sub eax,3
ror eax,1
imul eax,eax,0x26e978d5 // multiplicative inverse of 125
cmp eax,17179869 // floor((0xffffffff-3) / 250)
jbe OK
A range check for x can be embedded as well with no additional code.
For signed values a similar transformation is possible.
For more details see my comment on Hacker's Delight
(<a href="http://hackersdelight.org/corres.txt">http://hackersdelight.org/corres.txt</a>) and/or our paper about hashing
(<a href="http://programming.sirrida.de/hashsuper.pdf">http://programming.sirrida.de/hashsuper.pdf</a>).
The current version of Clang / LLVM (clang -O3 -S) translates it to the
following (GCC produces similar code):
movl %edi, %eax
imulq $274877907, %rax, %rax # imm = 0x10624DD3
shrq $36, %rax
imull $250, %eax, %eax
movl %edi, %ecx
subl %eax, %ecx
cmpl $3, %ecx
je OK
Please note that there are 2 multiply operations and one of them produces a
double width result (multiply high).
A first test shows that it is possible to address this optimization with a new
function in InstCombineCompares.cpp in a similar way like FoldICmpDivCst.
See
<a href="http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20140303/207906.html">http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20140303/207906.html</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>