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

    <tr>
        <th>Summary</th>
        <td>
            [InstCombine] Missed optimization: fold select if the user of its select user indicates the condition
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </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/EDjfon

### Motivating example 

```llvm
define i32 @src(i32 %a, i1 %c1, i1 %c2){
entry:
 %s1 = select i1 %c1, i32 23, i32 45
  %s2 = select i1 %c2, i32 666, i32 %s1
  %s3 = select i1 %c1, i32 789, i32 %s2
  ret i32 %s3
}
```
can be folded to:
```llvm
define i32 @tgt(i32 %a, i1 %c1, i1 %c2){
entry:
  %s2 = select i1 %c2, i32 666, i32 45
  %s3 = select i1 %c1, i32 789, i32 %s2
  ret i32 %s3
}
```

### Real-world motivation

This snippet of IR is derived from [libdeflate/lib/crc32.c@dispatch_crc32](https://github.com/ebiggers/libdeflate/blob/e93127b7e85d8dc65a80ed0ad4883c4b3efbe813/lib/crc32.c#L241) (after O3 pipeline, original IR comes from [llvm-opt-benchmark](https://github.com/dtcxzyw/llvm-opt-benchmark)).
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/rjf9zWM81

**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/eJy0Vdtq5DgQ_Rr1S5HGluy2_eCHJL0NgQkLw8Lu26JLua1Elowkd6bz9YvsvqQzywzLMiCMbqdc5_hUmYeg9xaxJeUDKbcrPsXe-favx_64Ek4d23ujD0hh9M51hN1DH-MYCLsndEfojs-na2MOw9r5PaE7iYTu3gnd_bZ96Zwl2ZZk96cnZcuAZxf1gUdt94Df-DAah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