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

            Bug ID: 89163
           Summary: Missed optimization: sar and shr equivalent for
                    non-negative numbers
           Product: gcc
           Version: 8.2.1
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: tspiteri at ieee dot org
  Target Milestone: ---

For the minimized test case below, shift is equivalent to same_as, because shr
and sar are equivalent for non-negative numbers. The generated code is not the
same.

int shift(int i) {
        return i >= 0 ? (unsigned)i >> 8 : i >> 8;
}
int same_as(int i) {
        return i >> 8;
}

shift:
        movl    %edi, %edx
        movl    %edi, %eax
        shrl    $8, %edx
        sarl    $8, %eax
        testl   %edi, %edi
        cmovns  %edx, %eax
        ret

same_as:
        movl    %edi, %eax
        sarl    $8, %eax
        ret

Reply via email to