<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 with redundant overflow checks"
href="https://bugs.llvm.org/show_bug.cgi?id=40845">40845</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>Missed optimization with redundant overflow checks
</td>
</tr>
<tr>
<th>Product</th>
<td>libraries
</td>
</tr>
<tr>
<th>Version</th>
<td>trunk
</td>
</tr>
<tr>
<th>Hardware</th>
<td>All
</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>Scalar Optimizations
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>alex.gaynor@gmail.com
</td>
</tr>
<tr>
<th>CC</th>
<td>llvm-bugs@lists.llvm.org
</td>
</tr></table>
<p>
<div>
<pre>Godbolt link: <a href="https://godbolt.org/z/h2lkGE">https://godbolt.org/z/h2lkGE</a>
The subtraction can be known not to overflow because it's derived from an
overflow checked addition. This was reduced/converted to C from this rust bug:
<a href="https://github.com/rust-lang/rust/issues/58692">https://github.com/rust-lang/rust/issues/58692</a>
One commenter suggested they thought the issue was in how GVN handled overflow
checked operations.
Code:
#include <stdlib.h>
extern void panic1();
extern void panic2();
extern void panic3();
size_t f(size_t a, size_t b) {
size_t x;
if (!__builtin_add_overflow(a, b, &x)) {
panic1();
__builtin_unreachable();
}
size_t r;
if (!__builtin_sub_overflow(x, a, &r)) {
panic2();
__builtin_unreachable();
}
if (r < 0) {
panic3();
__builtin_unreachable();
}
return x;
}
Assembly:
f(unsigned long, unsigned long): # @f(unsigned
long, unsigned long)
push rax
mov rax, rsi
add rax, rdi
jae .LBB0_3
cmp rax, rdi
jae .LBB0_4
pop rcx
ret
.LBB0_3:
call panic1()
.LBB0_4:
call panic2()</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>