<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/79266>79266</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[InstCombine] Missed optimization: fold `((select C1, C2) | A) ^ C3` to `(select C1^C3, C2^C3) | A)`
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
XChy
</td>
</tr>
</table>
<pre>
Alive2 proof: https://alive2.llvm.org/ce/z/oEmX9u
### Motivating example
```llvm
define i32 @src(i32 %a, i1 %c) {
entry:
%s = select i1 %c, i32 0, i32 4
%shl = shl i32 %a, 4
%or = or disjoint i32 %s, %shl
%xor = xor i32 %or, 4
ret i32 %xor
}
```
can be folded to
```llvm
define i32 @tgt(i32 %a, i1 %c) {
entry:
%s = select i1 %c, i32 4, i32 0
%shl = shl i32 %a, 4
%or = or disjoint i32 %s, %shl
ret i32 %or
}
```
Since `or disjoint` here can be regarded as `xor`, `((select C1, C2) | A) ^ C3` can be `(select C1, C2) ^ C3 ^ A)` and then we get `(select C1^C3, C2^C3) | A)`.
### Real-world motivation
This snippet of IR is derived from [zstd/lib/compress/zstd_compress_literals.c@ZSTD_compressRleLiteralsBlock](https://github.com/facebook/zstd/blob/1a860c8737f42b60e4e80b70d987df8583c977b8/lib/compress/zstd_compress_literals.c#L213) (after O3 pipeline).
The example above is a reduced version. If you're interested in the original suboptimal IR and optimal IR, contact me to get them, please.
**Let me know if you can confirm that it's an optimization opportunity, thanks.**
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0VV1v4ygU_TXk5aoRuf7MQx7apJEqdbRSZx5G-zLC5jpmisECkn78-hXYbdLZ0a4q7UqWAXPOueZwLwjv1cEQbVhxw4rdQhxDb93m-7Z_WTRWvmyutToRwuis7Vh2DX0Io2fZNcM9w71Is0utT8PSugPDfUsM968M9_Z2-L4-Mr5j_H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