https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112657
Bug ID: 112657
Summary: missed optimization: cmove not used with multiple
returns
Product: gcc
Version: 13.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: goon.pri.low at gmail dot com
Target Milestone: ---
This function
int unopt(int c) {
if (c == 14)
return -9;
else
return c;
}
unopt:
mov eax, edi
cmp edi, 14
je .L4
ret
.L4:
mov eax, -9
ret
Should probably be optimized to:
int opt(int c) {
if (c == 14)
c = -9;
return c;
}
opt:
cmp edi, 14
mov eax, -9
cmovne eax, edi
ret
This seems to only really happen when negative numbers are used,
int positive(int c) {
if (c == 14)
return 9;
else
return c;
}
positive:
mov eax, edi
cmp edi, 14
mov edx, 9
cmove eax, edx
ret
Though use of positive values still isn't completely optimized (possibly same
as https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97968)
Also it seems like if the order is reversed:
int reverse(int c) {
if (c != 14)
c = 9120;
return c;
}
reverse:
cmp edi, 14
mov edx, 14
mov eax, 9120
cmove eax, edx
ret
We could use %edi in the cmove and eliminate the 2nd instruction.