<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/96690>96690</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[InstCombine] Optimize multiple compares using `xor`
</td>
</tr>
<tr>
<th>Labels</th>
<td>
llvm:instcombine,
missed-optimization
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
Kmeakin
</td>
</tr>
</table>
<pre>
[This comment](https://github.com/rust-lang/rust/blob/c290e9de32e8ba6a673ef125fde40eadd395d170/library/core/src/char/convert.rs#L232) in the Rust stdlib explains how `(x >= 0x110000) | (x >= 0xD800 && x < 0xE000)` can be computed as `((x ^ 0xD800) - 0x800) >= (0x110000 - 0x800)`, which saves an instruction in LLVM-IR and tends to produce shorter assembly as well. It would be nice if LLVM could do this transformation automatically. I'm not sure how to generalize it to other constants though.
[godbolt](https://godbolt.org/z/z9GaK7MbY)
[alive](https://alive2.llvm.org/ce/z/brMDvK)
```c
#include <stdbool.h>
#include <stdint.h>
typedef uint32_t u32;
bool src(u32 x) { return (x >= 0x110000) | (x >= 0xD800 && x < 0xE000); }
bool tgt(u32 x) { return ((x ^ 0xD800) - 0x800) >= (0x110000 - 0x800); }
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVE1vIzcM_TXyhbAhU56vgw-bOC6CJCiwKAr0VGhGtEetRjIkynHy6wuNvbspkPbSArZM8YmPEvlMnZI9eqKtqO5EtVvozGOI26eJ9J_WL_pg3gr0y2gTDGGayLOodgLbkfmUhPoicC9wf7Q85n41hEngPubES6f98WYL3Pcu9AL3A3aSOkMKqe11retG0WGN1cHQRpI2RnWVWTdS4N7ZPur4VoJCJIH7FIeyGXWcff5MkVcxCVTPqFBgB9YDjwRfc2JIbJztgS4np61PMIZXELUU2F5AqAehdiAv67WUUpZQ0dzD37FdKyUIrAXWUNz3IC8P19OiljBoDz2VkpwykwGdbvRXlurhRlHIlyAvN_NGL7D9lv0DOhPcw-tohxGSPlMC7cH6xDEPbEOx4fn515fl41fQ3gCTNwk4wCkGkweCNIbIFEGnRFPv3sq1Xsm5FTwyvIbsTLm0twOBPcxUMMxeE4BLizlqnw4hTnrOpzOHYg7aubcVPApsJvCBIeVIc005wJE8Re3sO4Hl4gg8UoQh-MTacwIeQz6OKxByJ-SX21rdHYPpg_tcTldoFWLR0Hv5dj_pp-al_63U6RuDdvZMn8XPAK6cO083joFuRH182Z2ffrDU8voZbntU1g8uGyo9T2z6ENxqLH37HLeeP8Lzym8nMnSAbD0r_J0hKxTq7uORQguzpNusEC5XEd5BJM7Rw_-jU3UHotl9SMhH_ueE_0W5HzN9L-nCbJXpVKcXtF03605tKqy7xbhda304DGqjZVsZlFivNSkzVBts1q1ZbxZ2ixI3ssZq3VUb7FZNW-mmHZpDZ5QxbS82kiZt3fcWL2xKmbZdXXdy4XRPLs0jDbGcEOpL-RsNYeqtJ4Eo8F4gTjYlMstwYjvZ91nyBat2i7gtYcs-H5PYSGcTpx-p2LKbB-ajT3x_46x28POVh2DKju3JXceDjpQgJ-uPZUJcQiyVydFt_2WCzne-_ixPMfxBQxmi8xOTwP31lect_hUAAP__ccSzvA">