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

            Bug ID: 89081
           Summary: [x86] suboptimal code generated for condition
                    expression returning negation
           Product: gcc
           Version: 9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: wojciech_mula at poczta dot onet.pl
  Target Milestone: ---

Let's consider this trivial function:

---clamp.c---
#include <stdint.h>

uint64_t clamp1(int64_t x) {
    return (x < 0) ? -x : 0;
}
---eof---

$ gcc --version
gcc (GCC) 9.0.0 20190117 (experimental)

$ gcc -O3 -march=skylake clamp.c -c -S && cat clamp.s
clamp1:
        movq    %rdi, %rax
        negq    %rax
        movl    $0, %edx
        testq   %rdi, %rdi
        cmovns  %rdx, %rax
        ret

This procedure can be way shorter, like this

clamp1:
    xorq   %rax, %rax     # res = 0
    negq   %rdi           # -x, sets SF
    cmovns %rdi, %rax
    ret

One thing I observed recently when looking at assembly is that GCC
never modifies input registers %rdi or %rsi, always makes their
copies -- -thus the proposed shorter version is not possible.
However, clang modifies these registers, so seems the ABI allows this.

Reply via email to