[llvm-bugs] [Bug 47439] New: Missed vector code optimizations - res += X vs res = res + X

via llvm-bugs llvm-bugs at lists.llvm.org
Sun Sep 6 10:16:34 PDT 2020


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

            Bug ID: 47439
           Summary: Missed vector code optimizations - res += X vs res =
                    res + X
           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

typedef float Value;

struct Vec
{

  Vec(Value x=0, Value  y=0, Value  z=0, Value  t=0) :
X(x),Y(y),Z(z),T(t){}
  Vec & operator+=(const Vec & a) {
    X += a.X;
    Y += a.Y;
    Z += a.Z;
    T += a.T;
    return *this;
  }

  Value X;
  Value Y;
  Value Z;
  Value T;
};

inline Vec
operator+(Vec const & a, Vec const & b) {
  return
Vec(a.X+b.X,a.Y+b.Y,a.Z+b.Z,a.T+b.T);
}

inline Vec
operator*(Vec const & a, Value s) {
    return Vec(a.X*s,a.Y*s,a.Z*s,a.T*s);
}

inline Vec
operator*(Value s, Vec const & a) {
  return a*s;
}


void foo(Vec & res, Value s, Vec const & v1, Vec
const & v2) {
  res += s*(v1+v2);
}

void bar(Vec & res, Value s, Vec const & v1, Vec
const & v2) {
  res = res + s*(v1+v2);
}

Clang -Ofast:
foo(Vec&, float, Vec const&, Vec const&): # @foo(Vec&, float, Vec const&, Vec
const&)
  movups xmm1, xmmword ptr [rsi]
  movups xmm2, xmmword ptr [rdx]
  addps xmm2, xmm1
  shufps xmm0, xmm0, 0 # xmm0 = xmm0[0,0,0,0]
  mulps xmm0, xmm2
  movups xmm1, xmmword ptr [rdi]
  addps xmm1, xmm0
  movups xmmword ptr [rdi], xmm1
  ret
bar(Vec&, float, Vec const&, Vec const&): # @bar(Vec&, float, Vec const&, Vec
const&)
  movsd xmm1, qword ptr [rsi] # xmm1 = mem[0],zero
  movsd xmm2, qword ptr [rsi + 8] # xmm2 = mem[0],zero
  movsd xmm3, qword ptr [rdx] # xmm3 = mem[0],zero
  addps xmm3, xmm1
  movsd xmm1, qword ptr [rdx + 8] # xmm1 = mem[0],zero
  addps xmm1, xmm2
  shufps xmm0, xmm0, 0 # xmm0 = xmm0[0,0,0,0]
  mulps xmm3, xmm0
  mulps xmm0, xmm1
  movsd xmm1, qword ptr [rdi] # xmm1 = mem[0],zero
  addps xmm1, xmm3
  movsd xmm2, qword ptr [rdi + 8] # xmm2 = mem[0],zero
  addps xmm2, xmm0
  movlps qword ptr [rdi], xmm1
  movlps qword ptr [rdi + 8], xmm2
  ret


GCC -Ofast:
foo(Vec&, float, Vec const&, Vec const&):
        movups  xmm1, XMMWORD PTR [rsi]
        movups  xmm2, XMMWORD PTR [rdx]
        shufps  xmm0, xmm0, 0
        movups  xmm3, XMMWORD PTR [rdi]
        addps   xmm1, xmm2
        mulps   xmm1, xmm0
        addps   xmm1, xmm3
        movups  XMMWORD PTR [rdi], xmm1
        ret
bar(Vec&, float, Vec const&, Vec const&):
        movups  xmm1, XMMWORD PTR [rsi]
        movups  xmm2, XMMWORD PTR [rdx]
        shufps  xmm0, xmm0, 0
        movups  xmm3, XMMWORD PTR [rdi]
        addps   xmm1, xmm2
        mulps   xmm1, xmm0
        addps   xmm1, xmm3
        movups  XMMWORD PTR [rdi], xmm1
        ret


Function bar should be optimized better and match function foo.

https://godbolt.org/z/6q3h16

-- 
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/20200906/fdddd30f/attachment.html>


More information about the llvm-bugs mailing list