<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/85833>85833</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[InstCombine] Missed optimization: fold chained selects with exclusive condition but one identical arm
</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/ZkjFRM
### Motivating example
```llvm
define ptr @src(i32 %level, ptr %level.addr) local_unnamed_addr #0 {
entry:
%c1 = icmp eq i32 %level, 0
%c2 = icmp eq i32 %level, 1
%s1 = select i1 %c1, ptr %level.addr, ptr null
%s2 = select i1 %c2, ptr null, ptr %s1
ret ptr %s2
}
```
can be folded to:
```llvm
define ptr @tgt(i32 %level, ptr %level.addr) local_unnamed_addr #0 {
entry:
%cond = icmp eq i32 %level, 0
%s1 = select i1 %cond, ptr %level.addr, ptr null
ret ptr %s1
}
```
### Real-world motivation
This snippet of IR is derived from [openssl/crypto/shlib/cmp_util.c@OSSL_CMP_print_to_bio](https://github.com/openssl/openssl/blob/dc9bc6c8e1bd329ead703417a2235ab3e97557ec/crypto/cmp/cmp_util.c#L127) (after O3 pipeline).
The example above is a reduced version. If you're interested in the original suboptimal IR and optimal IR, see also: https://godbolt.org/z/qrh916jj8
**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/eJy0Vc1u4zYQfhr6MoghUf8HH5IYBgIk2CLbQ9GLQZEjaxKK1JKUs96nLyi5SZxu070UICTNcPRx5psfCu_pYBA3rLhhxXYlptBbt_njtj-tWqtOm2tNR-QwOms7ll1DH8LoWXbN-I7xnZh311ofh7V1B8Z3Ehnf_WB89-fz0-7xgSVbllyfnzxbFjzYQEcRyBwAv4th1AgXhmWyrIi7qBR2ZBDG4IDliXeS8Zoy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