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

    <tr>
        <th>Summary</th>
        <td>
            Optimization: (x != C) comparison can utilize (x ^ C) result
        </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 opportunity that can benefit RISC-V, MIPS and other processors that don't use condition codes for their compare-and-branch instructions.

```c

#include <stdlib.h>
unsigned int func1_a(unsigned int x) {
    if (x != 0x555)
 return (x ^ 0x555);
    abort();
}

unsigned int func1_c(unsigned int x) {
    unsigned int tmp = x ^ 0x555;
    __asm__ ("" : "+r" (tmp));
 if (tmp == 0)
        abort();
    return tmp;
}

unsigned int func2_a(unsigned int x) {
    if (x != 0x555)
        return (x - 0x555);
 abort();
}

unsigned int func2_c(unsigned int x) {
    unsigned int tmp = x - 0x555;
    __asm__ ("" : "+r" (tmp));
    if (tmp == 0)
 abort();
    return tmp;
}

```

The example code above can be tested in Compiler Explorer (godbolt.org). (The target architecture I mostly concern here is `riscv32`.)

`func1_c()` and `func2_c()` are the results I expected for `func1_a()` and `func2_a()` to optimize to when using the `-Os` option. Clang 19 is already able to recognize the `func2` case (`x != 0x555` and `x - 0x555 != 0`) but misses on the `func1` case (`(x ^ 0x555) != 0`).

(Note: I also [reported this in GCC](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119178).)
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJysVU1v4zYQ_TX0ZWCBoizZOuiQ2HGRQ7tFd9GrQVFjiQVFCvxInP31BSnFSbxbLLatIcDikPPmzdPMkDsne43YkPKelIcVD34wtnm4TMpYtLRetaZ7ab4M0oF0wDWYyctRfuVemriYjPVBS_8CfuAeBNfQosaz9PDH4-f9-k_C9vDr4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