<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 - Suoptimal multiplications with -Ofast"
   href="https://bugs.llvm.org/show_bug.cgi?id=42681">42681</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Suoptimal multiplications with -Ofast
          </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>Scalar Optimizations
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>david.bolvansky@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>#include <math.h>

double foo(double x, unsigned int n) {
  double result = 0.0;
  unsigned int i = 1;
  double numerator = 1.0;
  double denumerator = 1.0;
  x = 1.0 - x;
  while (i <= n) {
    numerator = numerator * ((x - 1) / x);
    result -= numerator / denumerator;
    denumerator++;
    i++;
  }

  return result;
}

Clang-8 -Ofast

foo(double, unsigned int):                               # @foo(double,
unsigned int)
        test    edi, edi
        je      .LBB0_1
        movsd   xmm2, qword ptr [rip + .LCPI0_0] # xmm2 = mem[0],zero
        movapd  xmm1, xmm0
        subsd   xmm1, xmm2
        divsd   xmm0, xmm1
        xorpd   xmm1, xmm1
        mov     eax, 1
        movapd  xmm3, xmm2
        movapd  xmm4, xmm2
.LBB0_3:                                # =>This Inner Loop Header: Depth=1
        mulsd   xmm4, xmm0
        movapd  xmm5, xmm4
        divsd   xmm5, xmm3
        subsd   xmm1, xmm5
        addsd   xmm3, xmm2
        add     eax, 1
        cmp     eax, edi
        jbe     .LBB0_3
        movapd  xmm0, xmm1
        ret
.LBB0_1:
        xorps   xmm1, xmm1
        movaps  xmm0, xmm1
        ret


Clang 9/10 -Ofast
foo(double, unsigned int):                               # @foo(double,
unsigned int)
        test    edi, edi
        je      .LBB0_1
        movsd   xmm2, qword ptr [rip + .LCPI0_0] # xmm2 = mem[0],zero
        movapd  xmm1, xmm2
        subsd   xmm1, xmm0
        movapd  xmm3, xmm2
        divsd   xmm3, xmm1
        xorpd   xmm1, xmm1
        mov     eax, 1
        movapd  xmm4, xmmword ptr [rip + .LCPI0_1] # xmm4 = [-0.0E+0,-0.0E+0]
        movapd  xmm5, xmm2
        movapd  xmm6, xmm2
.LBB0_3:                                # =>This Inner Loop Header: Depth=1
        mulsd   xmm6, xmm0
        mulsd   xmm6, xmm3
        movapd  xmm7, xmm6
        xorpd   xmm7, xmm4
        divsd   xmm6, xmm5
        addsd   xmm1, xmm6
        addsd   xmm5, xmm2
        add     eax, 1
        movapd  xmm6, xmm7
        cmp     eax, edi
        jbe     .LBB0_3
        movapd  xmm0, xmm1
        ret
.LBB0_1:
        xorps   xmm1, xmm1
        movaps  xmm0, xmm1
        ret


The loop body with new Clang looks worse .. 2x mulsd, 2x addsd..

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

Clang 8
  %12 = fmul fast double %10, %5, !dbg !392
  %13 = fdiv fast double %12, %9, !dbg !394
  %14 = fsub fast double %8, %13, !dbg !395

Clang 9
  %12 = fmul fast double %10, %0, !dbg !384
  %13 = fmul fast double %12, %6
  %14 = fsub fast double -0.000000e+00, %13
  %15 = fdiv fast double %14, %9, !dbg !386
  %16 = fsub fast double %8, %15, !dbg !387

So some pattern probably pessimized this code..</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>