<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </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] repeated memory references for infinity checks"
   href="https://bugs.llvm.org/show_bug.cgi?id=43611">43611</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[X86] repeated memory references for infinity checks
          </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>Linux
          </td>
        </tr>

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

        <tr>
          <th>Severity</th>
          <td>enhancement
          </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>andres@anarazel.de
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>craig.topper@gmail.com, llvm-bugs@lists.llvm.org, llvm-dev@redking.me.uk, spatel+llvm@rotateright.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Hi,

LLVM, for x64, does generate repeated memory references when checking the
results of floating point math for infinity. Clang generates IR like:
  %add = fadd double %a, %b
  %0 = tail call double @llvm.fabs.f64(double %add) #3
  %cmpinf = fcmp oeq double %0, 0x7FF0000000000000
  br i1 %cmpinf, label %if.then, label %if.end, !prof !2
...
if.end:                                           ; preds = %entry
  %add2 = fadd double %add, %c
  %1 = tail call double @llvm.fabs.f64(double %add2) #3
  %cmpinf3 = fcmp oeq double %1, 0x7FF0000000000000
  br i1 %cmpinf3, label %if.then9, label %if.end10, !prof !2
...

for 
...
  sum += b;
  if (unlikely(__builtin_isinf(sum)))
    overflowerror();

  sum += c;
  if (unlikely(__builtin_isinf(sum)))
    overflowerror();
...

which end up with instructions that reference the NaN/+Inf constants multiple
times:
  vaddsd %xmm1, %xmm0, %xmm0
  vandpd .LCPI0_0(%rip), %xmm0, %xmm1
  vucomisd .LCPI0_1(%rip), %xmm1
  jae .LBB0_5
  vaddsd %xmm2, %xmm0, %xmm0
  vandpd .LCPI0_0(%rip), %xmm0, %xmm1
  vucomisd .LCPI0_1(%rip), %xmm1
  jae .LBB0_5
...
rather than moving them into a register once.

Here's a godbolt link showing the issue:
<a href="https://godbolt.org/z/MnGGMU">https://godbolt.org/z/MnGGMU</a>

Interestingly llvm generates slightly better code for single precision floats,
moving the NaN constant into a register, but continuing to reference the
positive +Inf from memory.

Thanks to LebedevRI on #llvm, here are the links to llvm-mca analyses for
double/single precision, comparing with GCC generated code (although part of
the cost difference are due to gcc using subq/addq $8, %rsp, and clang using
pushq/popq, and I'm not sure that's accurately costed).  Interestingly gcc
generates similar code to clang for single precision code.

<a href="https://godbolt.org/z/_2q9cc">https://godbolt.org/z/_2q9cc</a>
<a href="https://godbolt.org/z/nytVTG">https://godbolt.org/z/nytVTG</a>


What I originally was wishing for was that some pass would recognize that the
isinf statements are redundant, and remove all but the last, but that's a
separate issue. In my case the repeated additions are only apparent after IPO.


- Andres</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>