<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/131882>131882</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
constprop in bitwise operations pessimizes constant selection
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
purplesyringa
</td>
</tr>
</table>
<pre>
[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.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJysVE1v4zYQ_TX0ZRBDGlpfBx0cJy4KbIGeGvRIkWOLG5oUOFTs5NcXlJ1N2t0Ci6IGAZKex8eZ90ZUzPboiXpR3YvqYaXmNIbYT3OcHPFrtP6oVkMwrxlwDGYILonqQWA7pjSxkFuBe4H7W2gd4lHg_k3gvv2zurz98qSNwE4U2zzq4jr0NOUtSuu1mw2BkLs5WWfTq5CPVzAnk8nldlI2CrlzwR8F7mCZ5SMcBLZ5DV5gB6K5F8UWAEAHz2lBwQ6EfIDi0gyqQqOahqrq0BSDkO_gSGmOPp_2ILCGXb5BYJt3W8CFefm7ebgeyotPhVxT_fLlj9-AiU4MhpQBpgTBgwvKWH-EdA5gLCfrdbqmp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