llvm / llvm/llvm-project

[CGP] cond ? x / y: x / C should generate branch instead of cmov

Open
#214,962 3 comments 0 reactions 1 assignee Claimed by @L3GiaBao View on GitHub
llvm:codegen missed-optimization
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

https://godbolt.org/z/zsh7YG3c3
```c
#include

int foo(int x, int y, bool k){
return k ? x / y: x / 7;
}
```
clang:
```asm
foo:
mov eax, edi
test edx, edx
mov ecx, 7
cmovne ecx, esi
cdq
idiv ecx
ret
```
gcc:
```asm
"foo":
test dl, dl
mov eax, edi
je .L2
cdq
idiv esi
ret
.L2:
movsxd rdx, edi
sar eax, 31
imul rdx, rdx, -1840700269
shr rdx, 32
add edx, edi
sar edx, 2
sub eax, edx, eax
ret
```

clang missed optimization opportunity of division by a constant. Since division has high latency, the branch cost may not be high.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.