[Bug rtl-optimization/88425] New: suboptimal code for a<imm?-1:0

amonakov at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Sun Dec 9 19:33:00 GMT 2018


            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

        cmpl    123, %edi
        sbbq    %rax, %rax

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

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

In the common case the optimization is performed by combine:

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

        cmpl    %esi, %edi
        sbbq    %rax, %rax

More information about the Gcc-bugs mailing list