<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/155187>155187</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Optimization: If (x & y) == y then (x - y) == (x xor y) == (x & ~y)
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          Explorer09
      </td>
    </tr>
</table>

<pre>
    This is an optimization feature request.

For unsigned integers `x` and `y`, if it's checked in a conditional that `(x & y) == y`, then the three expressions `(x - y)`, `(x xor y)` and `(x & ~y)` should equal to each other. The compiler should be able to emit any of the three forms in assembly, looking at which takes the least clock cycles or which is the smallest in code size.

Note that Clang is already able to optimize the condition `(x & y) == y` to `(~x & y) == 0`.

```c
#include <stdlib.h>
unsigned int test1(unsigned int x, unsigned int y) {
    if ((x & y) == y)
        return x - y;
    abort();
}
unsigned int test2(unsigned int x, unsigned int y) {
    if ((x & y) == y)
 return x ^ y;
    abort();
}
unsigned int test3(unsigned int x, unsigned int y) {
    if ((x & y) == y)
        return x & ~y;
 abort();
}
```
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJy8VMuu2zYQ_ZrRZnANitRzoYVz7zXQTbvJD1DU2GRNiQ5JJXYW-fZCL9dGc7so0BiGHzNnZg7PUEeGYE4DUQP5J8jfEjlG7Xzzfr1Y58mzOmldd2s-axPQBJQDuks0vfkuo3EDHknG0RN6-jJSiDtge2D7g_M4DnPjDs0Q6UQ-IBTsCgVDOXTT7xsUDPgrmiOaCLwMqDSp81yBEpUbOjPNkBajlhFneHVF4AXegNcI4g3EG259oqZh-sCoPRHS9eIpBOOGcC99mQtX_Ba8Or-FN2r3OT-2RNButB3Sl3Gi45Ck0uiiJr_Dz5pQuf5iLPkN2BLK1tIM7U1EOdzQHR_oHZ3vw3zSEKhv7W2iZJ07m-GEMuI3bZTGKM8U5ipLMkRU1qkzqpuyFND5FWUWSOiltRTi1FW5jjCY77Ru5HcXaZHx1crhNK_SepLd7c5z3SvNve7q_6vsU9mS__FPAIOCrdMnzPxW0x8uzKDs2BGCeA2xs6bdaRDvwPaPlwYjhZgCr56C10mnp8gys_wEbI-I03UCXn1Amdcranp5iqMfcLkWYquXrfNx7lAvQSjffsaM_x_M7pQgf_8vpMSvkGt9MBZyHzC7bzzpGtHVopYJNWmZF6zgGReJbpjoUqXyOhWyPMq0zIpWlGXOGRN5p5hKTMMZz1nFs7RmJRO7KmXU1ll2rFVWSVlBxqiXxu6s_drvnD8lJoSRmjTP06pMrGzJhtnXOB_oG85Z4HyyOd9MRS_teAqQMWtCDH-3iSZaav548DkQe_xtVuonMi3O82AwW-bJXp6Cd29JRm8bHeMlgNgDPwA_nEzUY7tTrgd-mDitXy8X7_4kFYEf5pME4If1qF8b_lcAAAD__zHOuLE">