<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/134028>134028</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Use `range` parameter attributes to fold `sub`+`icmp u*` into `icmp s*`
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
scottmcm
</td>
</tr>
</table>
<pre>
(This example comes from looking at Rust's discriminant code from <https://rust.godbolt.org/z/1138aGqdb>)
Take this input IR:
```llvm
define noundef zeroext i1 @is_foo(i8 noundef range(i8 -1, 5) %x) unnamed_addr {
start:
%0 = sub i8 %x, 2
%1 = zext i8 %0 to i64
%2 = icmp ule i8 %0, 2
%3 = add i64 %1, 1
%_2 = select i1 %2, i64 %3, i64 0
%_0 = icmp eq i64 %_2, 0
ret i1 %_0
}
```
Today, LLVM does simplify it a bunch, getting it down to <https://llvm.godbolt.org/z/G7as7Y6o5>
```llvm
define noundef zeroext i1 @is_foo(i8 noundef range(i8 -1, 5) %x) unnamed_addr #0 {
%0 = add nsw i8 %x, -5
%1 = icmp ult i8 %0, -3
ret i1 %1
}
```
It could do better, though. The range information *was* used to determine the `nsw`, but that doesn't really help the *unsigned* `icmp`.
Specifically, the `range` restriction is enough that it'd be allowed to be just <https://alive2.llvm.org/ce/z/fkVEwL>
```llvm
define noundef zeroext i1 @is_foo(i8 noundef range(i8 -1, 5) %x) unnamed_addr #0 {
start:
%1 = icmp slt i8 %x, 2
ret i1 %1
}
```
Eliminating the need for the `add` altogether.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzEVU1v2zgQ_TX0ZRCDoixZPuigfBUFspdutsCeDEocSWwo0iWHcZJfv6Bkp07bw552AQMyyDdf7z2NZAh6sIg1K65ZcbuSkUbn69A5oqmbVq1TrzUT1eOoA-CLnA4GoXMTBui9m8A496TtAJLgSwzExDaA0qHzetJWWoLOKVygL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