<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/92966>92966</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Missed optimization for `(a == 0 && b != 0) || (a != 0 && b == 0))`
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
pskrgag
</td>
</tr>
</table>
<pre>
Condition like `(a == 0 && b != 0) || (a != 0 && b == 0))` could be folded into `(a == 0) != (b == 0)`, but llvm misses this optimization.
For following function
```c
int test2(int a, int b)
{
if ((a == 0 && b != 0) || (a != 0 && b == 0))
return 1;
return 0;
}
```
llvm generates
```llvm
define dso_local range(i32 0, 2) i32 @src(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
%3 = icmp eq i32 %0, 0
%4 = icmp ne i32 %1, 0
%5 = and i1 %3, %4
%6 = icmp ne i32 %0, 0
%7 = icmp eq i32 %1, 0
%8 = and i1 %6, %7
%9 = or i1 %5, %8
%10 = zext i1 %9 to i32
ret i32 %10
}
```
While there exists more optimal IR for such function:
```llvm
define dso_local range(i32 0, 2) i32 @dst(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
%3 = icmp eq i32 %0, 0
%4 = icmp eq i32 %1, 0
%5 = xor i1 %3, %4
%6 = zext i1 %5 to i32
ret i32 %6
}
```
Alive shows that such transformation is correct: https://alive2.llvm.org/ce/z/5AUAao
Odd enough that clang performs such folding if `0` is not a constant, but a function argument: https://godbolt.org/z/PsWh1rP8d
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzElU1v4zYTxz8NfRmsQVEWZR10cGIIeA4PuihQ7DGgxJHELkW6JLVJ8-kLUoqjOEnRwxYFDFkif5z_cF5I4b0aDGJNijtSnHdiDqN19cV_d4MYdq2Vf9b31kgVlDWg1XcEwilhRwEkP5P8DBQI44RxaIGwLI0QVgEp70l5Dwu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