<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/60173>60173</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Missed optimization opportunity with -~n
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
jeaiii
</td>
</tr>
</table>
<pre>
full example:
https://godbolt.org/z/qeTYh6hcx
clang doesn't optimize the following code as well as both gcc and msvc:
```
unsigned next_a(unsigned n)
{
return -~n % 4 == 0 ? n - 2 : n + 1;
}
```
The problem seems to be that clang doesn't treat `-~n` exactly equivalent to `n + 1` like the other 2 compilers. The following compiles optimally on all 3 and the same as the first example on gcc and msvc
```
unsigned next_b(unsigned n)
{
return (n + 1) % 4 == 0 ? n - 2 : n + 1;
}
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyskz-PozwQxj_N0IwSmYHwp6DYKKJ7u23e6mRgErxnbBYPm80W-9lPkER7F12xxUnIBg8zeuY3fnQI5uSYK9jtYXeI9Cy9n6oX1saYqPHdpTrO1iK_62G0DMkTqAOo29qLjGE5oxqoPvmu8Va2fjoB1R9A9Ss__99nffv-e1JrtTth5zk4oFzQj2IG88EoPePRW-vPxp2w9R2jDnhma5e98dLjqW1Ruw6H8NY-aIFM3Z71c3ZrYx06fpcfGqj4OgEqbzn5_vqCiDixzJPDzadDoB2mCMkBkgMqhKRGhxskhOQJl_AeY0j29yKHvyq4rs894zj5xvKAgXkIKB6bpVkt-IhCJtaCkKnNp4NMLdRbsRfk19m8actOlmzI1F1DptCan1d0XnqekLD1w2gsT2GLzw9E10C4EtfWXtA71NZiskJdigQ9rNTXWZgpyH3yy69_0P8u-ub76IGKe2NU_pshRF2VdGVS6oirOMtTSqkgFfVVuyviri1IdfGRqCyORXrcpbsi6Tju0jyPTEWKEhWTikvKldqmijKmmIo478q0bCBVPGhjt9a-Dculj0wIM1eZivMksrphG1ZbETk-4xoEosVlU7XkbJr5FCBV1gQJX1XEiOXqPxMCd3dzaDHeoR9HP8nsjFzwbKRf7mo0T7Z68KGRfm62rR-A6qXsbduMk3_hVoDqVUwAqlexvwIAAP__5oI0Fw">