<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 - Missed vector code optimizations - res += X vs res = res + X"
   href="https://bugs.llvm.org/show_bug.cgi?id=47439">47439</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Missed vector code optimizations - res += X vs res = res + X
          </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>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.

<a href="https://godbolt.org/z/6q3h16">https://godbolt.org/z/6q3h16</a></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>