[Bug tree-optimization/96272] Failure to optimize overflow check

ubizjak at gmail dot com gcc-bugzilla@gcc.gnu.org
Mon Nov 23 08:07:57 GMT 2020


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96272

--- Comment #4 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?).

Maybe I was not too clear. Please consider this testcase:

--cut here--
#include <limits.h>
#include <stdio.h>

int
foo (unsigned a, unsigned b)
{
  return a > UINT_MAX - b;
}

int
bar (unsigned a, unsigned b)
{
  int dummy;

  return __builtin_uadd_overflow (a, b, &dummy);
}
--cut here--

This results in (-O2):

foo:
        notl    %esi
        xorl    %eax, %eax
        cmpl    %edi, %esi
        setb    %al
        ret

bar:
        xorl    %eax, %eax
        addl    %esi, %edi
        setc    %al
        ret

So, if it is possible to transform the comparison via the following
equivalence:

a > UINT_MAX - b  == (a + b) > UINT_MAX

to a __builtin_uadd_overflow, then at least on x86 it is possible to produce
much better code.


More information about the Gcc-bugs mailing list