https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109845
Bug ID: 109845
Summary: Addition overflow/carry flag unnecessarily put in a
temporary register
Product: gcc
Version: 13.1.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: rtl-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: chfast at gmail dot com
Target Milestone: ---
When we have an addition and an overflow check and the overflow flag is
combined with some other condition the codegen may generate variant when the
overflow flag is temporary register.
unsigned s = y + z;
_Bool ov = s < y;
if (x || ov)
return;
This produces
add esi, edx
setc al
test edi, edi
jne .L1
test eax, eax
jne .L1
while it could be
add esi, edx
jc .L6
test edi, edi
jne .L6
There are easy workaround to the C code which make the assembly optimal:
1. Change the order of checks
if (ov || x)
2. Split if into two
if (x)
return;
if (ov)
return;
https://godbolt.org/z/rxsrnhPdc