| Issue |
244086
|
| Summary |
[InstCombine] Missed fold: sdiv (mul X, X+1), 2 -> ashr (mul X, X+1), 1
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
Leo0506
|
`X * (X + 1)` is always even, so `sdiv` by 2 is exact and can become `ashr` by 1.
```llvm
define i32 @src(i32 %x) {
%a = add i32 %x, 1
%m = mul i32 %a, %x
%d = sdiv i32 %m, 2
ret i32 %d
}
define i32 @tgt(i32 %x) {
%a = add i32 %x, 1
%m = mul i32 %a, %x
%d = ashr i32 %m, 1
ret i32 %d
}
```
Alive2 proof: https://alive2.llvm.org/ce/z/UuxeLG
x86-64 (`llc -O3`), current vs. expected:
```asm
leal 1(%rdi), %ecx leal 1(%rdi), %eax
imull %edi, %ecx imull %edi, %eax
movl %ecx, %eax sarl %eax
shrl $31, %eax
addl %ecx, %eax
sarl %eax
```
https://godbolt.org/z/vWEdbThWY
Plain `ashr` is valid for any `%x`; `ashr exact` would additionally need `%x` to be noundef.
Found in real code (abc, `FxchMan`).
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs