https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127864
Bug ID: 127864
Summary: i386: Missed optimization for val ?: 1
Product: gcc
Version: 16.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: target
Assignee: unassigned at gcc dot gnu.org
Reporter: pali at kernel dot org
Target Milestone: ---
i386 gcc is missing optimization for the integer "val ?: 1" pattern.
This pattern is used for example inside the longjmp() implementation
because longjmp() never returns zero when jumping back to the setjmp caller.
Take the following example:
https://godbolt.org/z/jreaxP943
int test(int val) { return val ?: 1; }
gcc -m32 -march=i386 -O3
test:
mov eax, DWORD PTR [esp+4]
test eax, eax
jne .L1
mov eax, 1
.L1:
ret
clang -m32 -march=i386 -O3
test:
mov eax, dword ptr [esp + 4]
cmp eax, 1
adc eax, 0
ret
gcc output is not optimal as it contains jne branch instruction.
clang output is without branching and contains less instructions.