<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][SSE] Slow sqrt libcall branch due to dependency on sqrtsd result"
   href="https://llvm.org/bugs/show_bug.cgi?id=31455">31455</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[X86][SSE] Slow sqrt libcall branch due to dependency on sqrtsd result
          </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>Windows NT
          </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>llvm-dev@redking.me.uk
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>craig.topper@gmail.com, llvm-bugs@lists.llvm.org, mkuper@google.com, spatel+llvm@rotateright.com
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>#include <math.h>
double sqrt_add(double a, double b) {
  return sqrt(a) + b;
}


define double @sqrt_add(double, double) {
  %3 = tail call double @sqrt(double %0)
  %4 = fadd double %3, %1
  ret double %4
}
declare double @sqrt(double)

Found while looking at c-ray performance (PR22657), llvm tests for an
non-finite result of the (slow) sqrtsd instruction to decide whether to
fallback on the sqrt libcall, while gcc tests the input for a negative or zero
input, giving the branch logic more time to work.

llvm (3.9) output:

sqrt_add(double, double):                          # @sqrt_add(double, double)
        vmovapd %xmm0, %xmm2
        vxorps  %xmm0, %xmm0, %xmm0
        vsqrtsd %xmm2, %xmm0, %xmm0
        vucomisd        %xmm0, %xmm0
        jnp     .LBB0_2
        pushq   %rax
        vmovapd %xmm2, %xmm0
        vmovsd  %xmm1, (%rsp)           # 8-byte Spill
        callq   sqrt
        vmovsd  (%rsp), %xmm1           # 8-byte Reload
        addq    $8, %rsp
.LBB0_2:
        vaddsd  %xmm1, %xmm0, %xmm0
        retq

gcc (7 snapshot) output:

sqrt_add(double, double):
        vsqrtsd %xmm0, %xmm3, %xmm3
        subq    $24, %rsp
        vxorpd  %xmm2, %xmm2, %xmm2
        vucomisd        %xmm0, %xmm2
        vmovsd  %xmm3, (%rsp)
        jbe     .L2
        vmovsd  %xmm1, 8(%rsp)
        call    sqrt
        vmovsd  8(%rsp), %xmm1
.L2:
        vaddsd  (%rsp), %xmm1, %xmm0
        addq    $24, %rsp
        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>