<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/63321>63321</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
`(-1 + b) & x` → `select b, 0, x` should work for `!range {0, 2}` too, not just for `i1`
</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>
Original rust demonstration: <https://rust.godbolt.org/z/Y7n96orba>
LLVM today will simplify <https://llvm.godbolt.org/z/4jbjorjoe>
```llvm
%rhs = zext i1 %first_take to i64
%mask = add i64 -1, %rhs
%0 = and i64 %step, %mask
```
to
```llvm
%0 = select i1 %first_take, i64 0, i64 %step
```
However, if that `i1` instead comes from a `load i8` with `{0, 2}` range metadata, it no longer can: <https://llvm.godbolt.org/z/jevoe764x>
```llvm
%0 = load i8, ptr %first_take, align 1, !range !1, !noundef !2
%_5 = trunc i8 %0 to i1
%rhs = zext i1 %_5 to i64
%mask = add i64 -1, %rhs
%_6 = load i64, ptr %step, align 8
%1 = and i64 %_6, %mask
```
stays as just
```llvm
%0 = load i8, ptr %first_take, align 1, !range !0, !noundef !1
%rhs = zext i8 %0 to i64
%mask = add nsw i64 %rhs, -1
%_6 = load i64, ptr %step, align 8
%1 = and i64 %mask, %_6
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0VE2PozgQ_TXmUurIGDBw4NAfE-1hVntbaU-RAQNOGzuyi05nfv3KJtnJdvpDWu1IEQH7-VXVq-cS3qvRSNmQ4oEUT4lYcLKu8Z1FnLs5aW1_av5walRGaHCLR-jlbI1HJ1BZQ7J7INnjhHjwJLsnbEvYNsA2o-1bq3Fj3UjY9gdh279KU3PrWkGyb4Q-EXq_Pr9___N3QNuLExyV1uDVfNBqON0Sa_0yv0Oc79u9dXsrfxJzuv7CiXUJgLDCTR5I9gQ_5CuCSsPSoJzHHYpnCWhB8fwKPgv_HPGi78MW3KWEPZ6JrnB0BZkVRFjhUR7OyMDxJqn1E-3nua6kXmrZ3eYayEMsenm5BH0v0vr8zR7li3TxwAA4CQTCqUoJp6CMRyl66OwsPQzOziDCrraiB1UFyFHhFJZI-RCDMlI-hXUnzChhlih6gSKyIxgL2ppROujE-x75oJV7-WJlyfPXL1u5ynNJkD3CAd2tRkKr0cC5a-maK2HpZcHYxfRyCK_sintXRHJ0i-lAVWu44I70cy_tiv_uoR2_KojnVxVdzLTWUl2dSd_6bse_cp1HcfIgPOwXj79QYXqr8MfiXSn8sXbGHy9lBuXYY1Dyf9YvyrYquOOf3KWkb7K-zmqRyCblFa9SWtdlMjWs5SzPukLk6cDKvKh4yau8lwUr87pqaaIaRllGeVpQnvGs3FA-5Fkt85qXRdqxmuRUzkLpTbwh1o2J8n6RDc8yliZatFL7OK0ZM_IIcZMwFoa3a8KZu3YZPcmpVh79TxZUqGUT6mDVXXDrA7SE1UAYh9dwkck3RipOahZu-XnstEGM2MoI8ZNddA9H655hsC6Og396_mYuoLXh01iMVrvg47xJFqebf8-DUeG0tJvOzufhcP67Ozi7lx0Sto2lesK2UYq_AwAA___9Wd1p">