<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/87854>87854</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
`nuw nsw` not deduced for `add 1` inbounds of range-restricted length
</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>
(Context: I tried adding `assume`s to slice lengths in rust -- https://github.com/rust-lang/rust/pull/122926 -- and was surprised that certain things didn't optimize away that I would have expected to. The example here is basically `if i < slice.len() { do_something_with(i + 1); })`.)
I tried the following: <https://llvm.godbolt.org/z/qM5bnMxxj>
```llvm
define noundef i64 @src(i64 noundef %x.1, i64 noundef %i) unnamed_addr #0 {
start:
%0 = icmp sge i64 %x.1, 0
tail call void @llvm.assume(i1 %0)
%inbounds = icmp ult i64 %i, %x.1
br i1 %inbounds, label %do_something, label %done
do_something:
%next = add i64 %i, 1 ; <-- look here
br label %done
done:
%r = phi i64 [ %next, %do_something ], [-1, %start ]
ret i64 %r
}
```
expecting to see the `add` become `add nuw nsw` because of the range restriction on `%x.1`, but it doesn't.
Alive proof that it would be ok to do so here: <https://alive2.llvm.org/ce/z/3jHXNu>
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx8VEuPozgQ_jXOpRRkTHjkwKGTTLRzmD3tYW8tgwtwj7FZ23TS8-tXNiSTtEYtRQHq8VXVVw_unOw1Yk3yA8lPGz77wdjatcb7sR03jREfNWHV0WiPV0-yF_gO3koUwIWQugdSUO7cPCIpqANvwCnZIijUvR8cSA12dh62Wxi8nxzJXgg7E3bupR_mJmnNSNg5mGwV1_36Tth5mpUi7JwytmdFcOdawIU7cLOdrHQowA_cQ4vWc6nBD1L3DoQUmrDSg5m8HOUvBH7hH4vpd7iYWQkY-DsCXidsfUAxCcA_Q5DwcVIIA1oE6aDhTrZcqY9Qo-x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