[llvm-bugs] [Bug 29002] New: [x86] extra cmov in clamp function

via llvm-bugs llvm-bugs at lists.llvm.org
Tue Aug 16 07:36:56 PDT 2016


https://llvm.org/bugs/show_bug.cgi?id=29002

            Bug ID: 29002
           Summary: [x86] extra cmov in clamp function
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: Backend: X86
          Assignee: unassignedbugs at nondot.org
          Reporter: spatel+llvm at rotateright.com
                CC: llvm-bugs at lists.llvm.org
    Classification: Unclassified

void clamp(const float *src, short *dst) {
    int tmp = (int)((*src) * 32767.0f);
    if (tmp > 32767) tmp = 32767;
    if (tmp < -32768) tmp = -32768;
    *dst = tmp;
}

Or as IR:

define void @clamp(float* %src, i16* %dst) {
  %ld = load float, float* %src, align 4
  %mul = fmul float %ld, 3.276700e+04
  %conv = fptosi float %mul to i32
  %cmp = icmp sgt i32 %conv, 32767
  %sel1 = select i1 %cmp, i32 32767, i32 %conv
  %cmp2 = icmp slt i32 %sel1, -32768
  %sel2 = select i1 %cmp2, i32 -32768, i32 %sel1
  %conv6 = trunc i32 %sel2 to i16
  store i16 %conv6, i16* %dst, align 2
  ret void
}

Somehow, 2 select instructions became 3 cmovs:

$ ./llc -o - clampdown.ll
...
_clamp:                                 ## @clamp
    movss    (%rdi), %xmm0           ## xmm0 = mem[0],zero,zero,zero
    mulss    LCPI0_0(%rip), %xmm0
    cvttss2si    %xmm0, %eax
    cmpl    $32767, %eax            ## imm = 0x7FFF
    movl    $32767, %ecx            ## imm = 0x7FFF
    cmovlel    %eax, %ecx
    movw    $32767, %dx             ## imm = 0x7FFF
    cmovlew    %ax, %dx
    cmpl    $-32768, %ecx           ## imm = 0x8000
    movw    $-32768, %ax            ## imm = 0x8000
    cmovgew    %dx, %ax
    movw    %ax, (%rsi)
    retq


gcc 6.1 does:

foo(float const*, short*):
        movss   .LC0(%rip), %xmm0
        movl    $-32768, %edx
        mulss   (%rdi), %xmm0
        cvttss2si       %xmm0, %eax
        cmpl    $-32768, %eax
        cmovl   %edx, %eax
        movl    $32767, %edx
        cmpl    $32767, %eax
        cmovg   %edx, %eax
        movw    %ax, (%rsi)
        ret

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20160816/6c0166f8/attachment.html>


More information about the llvm-bugs mailing list