Repository: rust
Issue: Optimizer regression in Rust nightly
Labels: A-LLVM,P-medium,T-compiler,regression-from-stable-to-stable,C-bug,llvm-fixed-upstream

<!--
Thank you for filing a regression report! 🐛 A regression is something that changed between versions of Rust but was not supposed to.

Please provide a short summary of the regression, along with any information you feel is relevant to replicate it.
-->

### Code

I tried this code:

```rust
const MASK: usize = 1;

#[inline(never)]
pub fn test1(a1: usize, a2: usize) -> bool {
    (a1 & !MASK) == (a2 & !MASK) && (a1 & MASK) == (a2 & MASK)
}

#[inline(never)]
pub fn test2(a1: usize, a2: usize) -> bool {
    (a1 & MASK) == (a2 & MASK) && (a1 & !MASK) == (a2 & !MASK)
}
```

I expected to see this happen: Each function is optimized to a simple equality comparison.

Instead, this happened: On nightly, `test1()` is not optimized correctly. Tested locally with AArch64, but Godbolt suggests analogous results for x86-64.

### Version it worked on

<!--
Provide the most recent version this worked on, for example:

It most recently worked on: Rust 1.47
-->

It most recently worked on:
```
rustc 1.81.0 (eeb90cda1 2024-09-04)
binary: rustc
commit-hash: eeb90cda1969383f56a2637cbd3037bdf598841c
commit-date: 2024-09-04
host: aarch64-apple-darwin
release: 1.81.0
LLVM version: 18.1.7
```

### Version with regression

<!--
Provide the version you are using that has the regression.
-->

`rustc --version --verbose`:
```
rustc 1.83.0-nightly (06bb8364a 2024-10-01)
binary: rustc
commit-hash: 06bb8364aaffefb0ce67e5f5445e66ec99c1f66e
commit-date: 2024-10-01
host: aarch64-apple-darwin
release: 1.83.0-nightly
LLVM version: 19.1.0
```

<!--
Did the compiler crash? If so, please provide a backtrace.
-->

### Backtrace
N/A

### Demo

Godbolt, default settings (x86_64-unknown-linux-gnu): https://rust.godbolt.org/z/K8PnTo1cn

On aarch64-apple-darwin macOS 15.0, using a blank new Cargo project with `lib.rs` exactly as above:

```
$ cargo build --release && otool -tvV target/release/libmask_opt_regression_test.rlib
    Finished `release` profile [optimized] target(s) in 0.00s
Archive : target/release/libmask_opt_regression_test.rlib
target/release/libmask_opt_regression_test.rlib(lib.rmeta):
(__TEXT,__text) section
target/release/libmask_opt_regression_test.rlib(mask_opt_regression_test-bad7f2fccccc562a.mask_opt_regression_test.cb8f71a1c9276c63-cgu.0.rcgu.o):
(__TEXT,__text) section
__ZN24mask_opt_regression_test5test117h89b10ffee17a747dE:
0000000000000000        cmp     x0, x1
0000000000000004        cset    w0, eq
0000000000000008        ret
```

```
$ cargo +nightly build --release && otool -tvV target/release/libmask_opt_regression_test.rlib
    Finished `release` profile [optimized] target(s) in 0.18s
Archive : target/release/libmask_opt_regression_test.rlib
target/release/libmask_opt_regression_test.rlib(lib.rmeta):
(__TEXT,__text) section
target/release/libmask_opt_regression_test.rlib(mask_opt_regression_test-f6c3b03d41968e3f.mask_opt_regression_test.62739398bfe23ba6-cgu.0.rcgu.o):
(__TEXT,__text) section
__ZN24mask_opt_regression_test5test117h2d14c7accc517b7cE:
0000000000000000        eor     x8, x1, x0
0000000000000004        cmp     x8, #0x2
0000000000000008        cset    w9, lo
000000000000000c        eor     w8, w8, #0x1
0000000000000010        and     w0, w9, w8
0000000000000014        ret
__ZN24mask_opt_regression_test5test217hb31fa9ddc54caea8E:
0000000000000018        cmp     x0, x1
000000000000001c        cset    w0, eq
0000000000000020        ret
```

---

<!--
If you know when this regression occurred, please add a line like below, replacing `{channel}` with one of stable, beta, or nightly.

@rustbot modify labels: +regression-from-stable-to-{channel} -regression-untriaged
-->

@rustbot modify labels: +regression-from-stable-to-nightly -regression-untriaged