<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/70374>70374</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Missed optimization opportunity in (z >= 0) && (x >= y + z) && (x >= y);
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
krtab
</td>
</tr>
</table>
<pre>
Hi!
I'm not too familiar LLVM. I was investigating this rustc issue https://github.com/rust-lang/rust/issues/82801 and I realized that in the following Rust code, the last (x >= y) check is not optimized away:
```rust
#![feature(unchecked_math)]
#[no_mangle]
pub unsafe fn foo(x: u32, y : u32, z: u32) -> bool {
(x >= y.unchecked_add(z)) && (x >= y)
}
```
I wrote what I believe is an equivalent C code, using the fact that overflow is UB on signed integers
```C
bool foo(int x, int y, int z) {
return (z >= 0) && (x >= y + z) && (x >= y);
}
```
but using clang trunk I get
```asm
foo: # @foo
test edx, edx
setns cl
add edx, esi
cmp edx, edi
setle dl
cmp edi, esi
setge al
and al, cl
and al, dl
ret
```
whereas GCC properly removes the last comparison
```asm
foo:
xor eax, eax
test edx, edx
js .L1
add edx, esi
cmp edx, edi
setle al
.L1:
ret
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVcFy4jgQ_Rpx6Qply2DwwYcAwy5VmctW7V6nZKttayJLXqkNga_fkk0IkGQmh3WlIlntfnrvtVAL71VtEHM2X7H5ZiJ6aqzLnx2JYlJYecz_VIzHLNqw6HH8v2N80YKxBGQtVKJVWgkHT0__fJ_CDg7CgzJ79KRqQcrUQI3y4HpPJSjve4SGqPMseWR8y_i2VtT0xbS0LePb8NmDFqY-zxnfDjme8e2SL6MYhJGwA4dCqxNKoEYQKAPUIFRWa3sIW_7Ve4LSSmR8PYS08ASML1-AJd9YsoEj4xmUDZbPoPygxnak2gFTHMQx0LsSzdJo_BtIjUs8Cc7MVxUK6h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