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

            Bug ID: 84527
           Summary: missed optimization for special ternary operation
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ktietz at gcc dot gnu.org
  Target Milestone: ---

The following sample:

int foo(int a, int b)
{
  return a < b ? -1 : 1;
}

gets translated to

...
        xorl    %eax, %eax
        cmpl    %edx, %ecx
        setge   %al
        leal    -1(%rax,%rax), %eax
        ret
...

There would be instead a better representation for such kind of patterns as

... cmpl %ecx, %edx
    sbb  %eax, %eax
    orb  $1, %al
    ret

The missed patterns are 'result = COND ? -1 : VAL' and its permutations.

The same applies to the pattern 'result = COND ? VAL : 0' and its permuations,
which can be expressed as 'CMP a, b; SBB reg, reg; AND $VAL, reg;'

Reply via email to