<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - [x86] extra cmov in clamp function"
   href="https://llvm.org/bugs/show_bug.cgi?id=29002">29002</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[x86] extra cmov in clamp function
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Backend: X86
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>spatel+llvm@rotateright.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>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</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>