Issue 244101
Summary [InstCombine] Missed optimization: (2 << X) | -(2 << X) -> -2 << X
Labels new issue
Assignees
Reporter Leo0506
    `2 << X` is a power of two, and the single set bit of a power of two is also set in its negation, so `P | -P == -P`; and `-(2 << X)` is `-2 << X`.

```llvm
define i32 @src(i32 %x) {
  %p = shl i32 2, %x
  %n = sub i32 0, %p
  %r = or i32 %p, %n
  ret i32 %r
}

define i32 @tgt(i32 %x) {
  %r = shl i32 -2, %x
  ret i32 %r
}
```
https://alive2.llvm.org/ce/z/MPu6ar

x86-64 (`llc -O3`), current vs. expected:
```asm
movl  %edi, %ecx             movl  %edi, %ecx
movl  $2, %edx               movl  $-2, %eax
shll  %cl, %edx shll  %cl, %eax
movl  %edx, %eax
negl  %eax
orl   %edx, %eax
```
https://godbolt.org/z/rjrTY8bPM

Found in real code (boost, dlmalloc).

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to