[SimplifyCFG] a > b ? a / b : b / a should be optimized to max(a, b) / min(a, b)
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
```c
#include
int foo(int a, int b){
return a > b ? a / b : b / a;
}
int bar(int a, int b){
return __builtin_elementwise_max(a,b) / __builtin_elementwise_min(a,b);
}
```
```asm
foo:
cmp edi, esi
jle .LBB0_2
mov eax, edi
cdq
idiv esi
ret
.LBB0_2:
mov eax, esi
cdq
idiv edi
ret
bar:
cmp edi, esi
cmovg eax, esi, edi
cmovl esi, edi
cdq
idiv esi
ret
```
```asm
foo:
cbge w1, w0, .LBB0_2
sdiv w0, w0, w1
ret
.LBB0_2:
sdiv w0, w1, w0
ret
bar:
smax w8, w0, w1
smin w9, w0, w1
sdiv w0, w8, w9
ret
```
https://godbolt.org/z/eM6qM976Y
Contributor guide
Assessment
This issue has not been assessed yet.