<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/91619>91619</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Missed optimization in bounds analysis in unsigned arithmetic that cannot underflow
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
davidben
</td>
</tr>
</table>
<pre>
Clang doesn't seem to be able to optimize the following out:
```
bool f(size_t a, size_t b) {
__builtin_assume(a <= b);
__builtin_assume(b <= PTRDIFF_MAX);
// This can be deleted, because:
// 1. 0 <= a <= b implies that b - a <= b
// 2. b - a <= b <= PTRDIFF_MAX
return b - a <= PTRDIFF_MAX;
}
```
https://godbolt.org/z/vzE7qb7Gb
I ran into this while exploring what combination of `__builtin_assume`s would dispatch some unnecessary assertions in `std::string_view`. (A separate bug while I'll file after this.)
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJxsU09vuzgQ_TTmMioyJoFw4JA2y6qHSqtVD3uLbDwEr4ydZexkm0__k2nSf79ICAb5zcx7bzySyBwcYsvWj2y9y2QMo59bLU9GK3SZ8vqtfbLSHUB7JMdEHYAQJwgeFIJUFlPoj8FM5oIQRoTBW-vPxh3Ax8DKLeM7xm_vil-f5Vd5b2FgYkPmgvsAkoknuMaKiQZY_fiOhP1eRWODcXtJFCdkYiOBlU-s3C1QVt6QAPfA6gb-6_Xv3XPX7V-2_3xLY6JjooPX0RD00iV5Gi0G1ImUwl5Gwg85qcs1A6DIgd_Kf5ICMx2tQYIwygAKHr6c3Ski8h-Ye4Q_0mYMcXbfM74Cb7JYvbvr_BjCkZKapf3Ba-VtyP18YKK7MNGdLn_U_6n6T_V1es8wSwfGBQ8h2XQejUXA_4_Wz2ne5yS095MyTgbjHfgBWMV_m0bFCc4-Wg3a0FGGfgTyE0J0DnskkvMbSCKcUxEC41IVCjrxLbcUUrP9yeCZVTwHJjZbIDzKWQYEFQ9XXs9M1NbCkGI5BJwX0jkTTabbUjdlIzNsi7pYi1qIWmRjKzSXVb0ZlOw3iqu62KzWstRVhVVdqaHITCu4WPE1b4qqLHidc7lWWGyGvqlwpYuCrThO0tjc2tOU_MwMUcS2KaqiyaxUaGnZNiEcnmE5ZEKk5ZvblPOg4oHYiltDgT6rBBMsti-GCPVt3d49Ng6Uj04TSCftG5nFsOiWzdYgZxPGCYPp3-9hL53zAaLTOA_Wn7M42_bHbTBhjCrv_cRElxhcPw_H2f-LfWCiW3gTE92i61cAAAD__ziuT5E">