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

    <tr>
        <th>Summary</th>
        <td>
            (a & C0) != 0 || (a & C1) != 0 Doesn't Always Become (a & (C0 | C1)) != 0
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          Geotale
      </td>
    </tr>
</table>

<pre>
    Example code:
```rust
#[no_mangle]
pub fn src(a: u8) -> u8 {
    1 + ((a & 4 != 0 || a & 1 != 0) as u8)
}

#[no_mangle]
pub fn tgt(a: u8) -> u8 {
    1 + ((a & 5 != 0) as u8)
}
```
Resulting in the IR:
```llvm
define noundef i8 @src(i8 noundef %a) unnamed_addr {
start:
  %_4 = and i8 %a, 4
 %0 = icmp eq i8 %_4, 0
  %1 = and i8 %a, 1
  %2 = add nuw nsw i8 %1, 1
  %_3.sroa.0.0 = select i1 %0, i8 %2, i8 2
  ret i8 %_3.sroa.0.0
}

define noundef i8 @tgt(i8 noundef %a) unnamed_addr {
start:
  %_4 = and i8 %a, 5
  %_3.not = icmp eq i8 %_4, 0
  %_0 = select i1 %_3.not, i8 1, i8 2
  ret i8 %_0
}
```
This specific pattern is never caught, but the cause for it is due to the original comparisons lying on different branches which InstCombine didn't account for
In many situations, missing this optimization causes multiple unnecessary branches to occur (the code I was testing with did `(a & !3) + 4 * ((a & 2 != 0 && (a & 4 != 0 || a & 1 != 0)) as u8)`, which similarly never gets the chance to optimize this away due to the separate branches, but they remain on different branches the entire time!!)

Tested with nightly Rustc on Godbolt
Godbolt: https://godbolt.org/z/K5GP5dGq1
Alive2: https://alive2.llvm.org/ce/z/evhsnx
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVU9v47gP_TTKhWhgy3FjH3xI00lR_C4_FHMPFIm2tbCljCg1k_n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