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

            Bug ID: 88425
           Summary: suboptimal code for a<imm?-1:0
           Product: gcc
           Version: 9.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: amonakov at gcc dot gnu.org
  Target Milestone: ---
            Target: x86_64-*-*, i?86-*-*

GCC does not manage to optimize (unsigned)a<b?-1:0 to a cmp-sbb sequence for
x86 when b is an immediate (RTL sees a <= (b-1) comparison instead).

unsigned long baz (unsigned int a)
{
  return a < 123 ? -1ul : 0;
}

Optimal code would be

baz:
        cmpl    123, %edi
        sbbq    %rax, %rax
        ret

but we generate (at -O1, xor/cmp/setbe/neg at -O2+)

baz:
        cmpl    $122, %edi
        setbe   %al
        movzbl  %al, %eax
        negq    %rax
        ret

In the common case the optimization is performed by combine:

unsigned long baz (unsigned int a, unsigned b)
{
  return a < b ? -1ul : 0;
}

baz:
        cmpl    %esi, %edi
        sbbq    %rax, %rax
        ret

Reply via email to