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

            Bug ID: 67089
           Summary: [4.8/4.9/5/6 Regression] Integer overflow checks not
                    optimized on x86/x86_64
           Product: gcc
           Version: 5.2.1
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: m.mukovnikov at gmail dot com
  Target Milestone: ---

Starting from 4.8.3, gcc does not optimize integer overflow and underflow
checks inserting a redundant cmp instruction in both 32-bit and 64-bit modes.
Having git-bisected the revisions, I found out r204088 (by pr58779) had added
such behavior.

--------------- example ---------------
extern void underflow(void) __attribute__((noreturn));
unsigned sub(unsigned a, unsigned b)
{
    unsigned r = a - b;
    if (r > a) underflow();
    return r;
}
------------ gcc-4.8.2 -O1 ------------
sub(unsigned int, unsigned int):
    movl    %edi, %eax
    subl    %esi, %eax /* CF is set on overflow */
    jae .L4            /* jae = jnb = jnc = 73h */
    subq    $8, %rsp
    call    underflow()
.L4:
    rep; ret
------------ gcc-4.8.3 -O1 ------------
sub(unsigned int, unsigned int):
    movl    %edi, %eax
    subl    %esi, %eax
    cmpl    %eax, %edi  /* absolutely redundant */
    jnb .L4
    subq    $8, %rsp
    call    underflow()
.L4:
    rep ret
---------------------------------------

Reply via email to