Issue 131882
Summary constprop in bitwise operations pessimizes constant selection
Labels new issue
Assignees
Reporter purplesyringa
    [godbolt](https://godbolt.org/z/8Y5xzGWcd)

```cpp
#include <utility>

std::pair<long, long> f(long n) {
    const long C = 0x7ba52da77e55f70b;
    return {n & C, (n * 2) & C};
}
```

LLVM seems dead set on loading two distinct constants because it knows `n * 2` is even, and so the mask can be "simplified" to `0x7ba52da77e55f70a` in one of the cases:

```asm
f(long):
        movabs  rcx, 8909577635224745738
 lea     rax, [rcx + 1]
        and     rax, rdi
        lea     rdx, [rdi + rdi]
        and     rdx, rcx
        ret
```

While sharing the constant would work just fine:

```asm
f(long):
        movabs  rax, 8909577635224745739
        lea     rdx, [rdi + rdi]
        and     rdx, rax
        and     rax, rdi
        ret
```

And LLVM *can* produce this codegen if I [disable constprop of `C`](https://godbolt.org/z/oE1n9PWx1).

Applies to x86-64, aarch64, probably everything else under the sun.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to