https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96272
--- Comment #3 from Uroš Bizjak <ubizjak at gmail dot com> ---
(In reply to Jakub Jelinek from comment #2)
> Well, it needs the addition too, so I think this can't be done in match.pd,
> but would need to be done in some other pass (not sure which, perhaps
> phiopt?).
No, I was referring to the first step of the optimization. The converted source
would read something like:
unsigned
bar (unsigned a, unsigned b)
{
int dummy;
if (__builtin_uadd_overflow (a, b, &dummy))
return UINT_MAX;
return a + b;
}
The RTL CSE pass is able to eliminate one addition, resulting in:
bar:
addl %esi, %edi
jc .L5
movl %edi, %eax
ret
.L5:
orl $-1, %eax
ret
Eventually, some tree pass could convert the above source to:
unsigned
bar (unsigned a, unsigned b)
{
unsigned res;
if (__builtin_uadd_overflow (a, b, &res))
return UINT_MAX;
return res;
}
which results in:
bar:
addl %esi, %edi
movl $-1, %eax
cmovnc %edi, %eax
ret