[llvm-bugs] [Bug 42681] New: Suoptimal multiplications with -Ofast

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Jul 18 16:15:43 PDT 2019


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

            Bug ID: 42681
           Summary: Suoptimal multiplications with -Ofast
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Scalar Optimizations
          Assignee: unassignedbugs at nondot.org
          Reporter: david.bolvansky at gmail.com
                CC: llvm-bugs at lists.llvm.org

#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..

https://godbolt.org/z/zZRxbE

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..

-- 
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/20190718/8750f481/attachment.html>


More information about the llvm-bugs mailing list