<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: finding closed form of loop doesn't take advantage of assumption"
href="https://bugs.llvm.org/show_bug.cgi?id=41025">41025</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>Missed optimization: finding closed form of loop doesn't take advantage of assumption
</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>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>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>arthur.j.odwyer@gmail.com
</td>
</tr>
<tr>
<th>CC</th>
<td>htmldeveloper@gmail.com, llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
</td>
</tr></table>
<p>
<div>
<pre>Clang trunk does a great job turning this sum-the-area-of-a-trapezoid function
into its closed form. However, the closed form needs a branch to deal with the
possibility that the distance from 'b' to 'e' is negative: in that case, 0
should be returned as a special case.
You can eliminate the special case by adding `__builtin_assume(b < e)` to the
top of the function. This eliminates the branch from the codegen of `f1`.
However, Clang is not currently able to eliminate the branch from the codegen
of `f2`, even though it is identical to `f1` except that f2 uses `i != e` as
the loop termination condition instead of `i < e`.
I don't know if this is a very deep and complicated bug that's not worth
fixing, or just a simple one-line update somewhere. :)
// <a href="https://godbolt.org/z/aWAahD">https://godbolt.org/z/aWAahD</a>
int f1(unsigned b, unsigned e)
{
__builtin_assume( b < e );
int total = 0;
for (unsigned i = b; i < e; ++i) {
total += i;
}
return total;
}
int f2(unsigned b, unsigned e)
{
__builtin_assume( b < e );
int total = 0;
for (unsigned i = b; i != e; ++i) {
total += i;
}
return total;
}
====
_Z2f1jj: # @_Z2f1jj
movl %edi, %ecx
notl %ecx
addl %esi, %ecx
leal 1(%rdi), %eax
imull %ecx, %eax
addl $-2, %esi
subl %edi, %esi
imulq %rcx, %rsi
shrq %rsi
addl %edi, %eax
addl %esi, %eax
retq
_Z2f2jj: # @_Z2f2jj
xorl %eax, %eax
cmpl %esi, %edi
je .LBB1_2 // THIS BRANCH should be unnecessary AFAICT
movl %edi, %ecx
notl %ecx
addl %esi, %ecx
leal 1(%rdi), %eax
imull %ecx, %eax
addl $-2, %esi
subl %edi, %esi
imulq %rcx, %rsi
shrq %rsi
addl %edi, %eax
addl %esi, %eax
.LBB1_2:
retq</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>