llvm / llvm/llvm-project

[SimplifyCFG] a > b ? a / b : b / a should be optimized to max(a, b) / min(a, b)

Open
#214,956 4 comments 0 reactions 1 assignee Claimed by @lodha1503 View on GitHub
llvm:transforms missed-optimization
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

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.