https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128113
Bug ID: 128113
Summary: [17 Regression] c ? a & K : a one instruction longer
on aarch64 (-O2, -Os) and x86-64 (-O2)
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: rtl-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: yuki.tanaka at defaultcase dot de
Target Milestone: ---
int a (int a, int c) { return c ? (a & 0xf0) : a; }
int b (int a, int c) { if (c == 0) a &= 7; return a; }
GCC 17 emits one instruction more for both functions than GCC 16.1 on aarch64
at -O2 and -Os and on x86-64 at -O2.
>From Compiler Explorer:
aarch64, -O2 and -Os (same output for both options)
GCC 16.1:
a:
and w2, w0, 240
cmp w1, 0
csel w0, w2, w0, ne
ret
b:
and w2, w0, 7
cmp w1, 0
csel w0, w2, w0, eq
ret
GCC 17:
a:
cmp w1, 0
csel w1, w0, wzr, eq
and w0, w0, 240
orr w0, w0, w1
ret
b:
cmp w1, 0
csel w1, w0, wzr, ne
and w0, w0, 7
orr w0, w0, w1
ret
x86-64, -O2
GCC 16.2:
"a":
mov eax, edi
and eax, 240
test esi, esi
cmove eax, edi
ret
"b":
mov eax, edi
and eax, 7
test esi, esi
cmovne eax, edi
ret
GCC 17:
"a":
xor eax, eax
test esi, esi
cmove eax, edi
and edi, 240
or eax, edi
ret
"b":
xor eax, eax
test esi, esi
cmovne eax, edi
and edi, 7
or eax, edi
ret