https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82858
Bug ID: 82858 Summary: __builtin_add_overflow() generates suboptimal code with unsigned types on x86 Product: gcc Version: 7.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: target Assignee: unassigned at gcc dot gnu.org Reporter: lh_mouse at 126 dot com Target Milestone: --- The following function: ``` unsigned saturated_add(unsigned a, unsigned b){ unsigned c; if(__builtin_add_overflow(a, b, &c)){ return -1; } return c; } ``` , after being compiled with `gcc -O3 -Wall -Wextra -pedantic -pedantic-errors -masm=intel`, results in: ``` saturated_add(unsigned int, unsigned int): add edi, esi jc .L3 mov eax, edi ret .L3: or eax, -1 ret ``` This is suboptimal, as the branch can be simply eliminated as follows: ``` saturated_add(unsigned int, unsigned int): add edi, esi sbb eax, eax // EAX = -1 if CF is set and 0 otherwise. or eax, edi ret ```