<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 - Extra moves due to failure to commute sub+add to sub+sub"
   href="https://bugs.llvm.org/show_bug.cgi?id=34210">34210</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Extra moves due to failure to commute sub+add to sub+sub
          </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>Windows NT
          </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>llvm-dev@redking.me.uk
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, spatel+llvm@rotateright.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>int foo(int x, int y, int *diff)
{
    *diff += y - x;
    return y;
}

define i32 @foo(i32, i32 returned, i32* nocapture) local_unnamed_addr #0 {
  %4 = sub i32 %1, %0
  %5 = load i32, i32* %2
  %6 = add nsw i32 %4, %5
  store i32 %6, i32* %2
  ret i32 %1
}

foo:
  movl %esi, %eax
  subl %edi, %eax
  addl %eax, (%rdx)
  movl %esi, %eax
  retq

But we could avoid the extra mov by performing:

foo:
  subl %esi, %edi
  subl %edi, (%rdx)
  movl %esi, %eax
  retq

Which would be equivalent to:

int bar(int x, int y, int *diff)
{
    *diff -= x - y;
    return y;
}

Which annoyingly gets canonicalized to something very similar:

define i32 @bar(i32, i32 returned, i32* nocapture) local_unnamed_addr #0 {
  %4 = load i32, i32* %2
  %5 = sub i32 %1, %0
  %6 = add i32 %5, %4
  store i32 %6, i32* %2
  ret i32 %1
}

bar:
  movl %esi, %eax
  subl %edi, %eax
  addl %eax, (%rdx)
  movl %esi, %eax
  retq


I'm assuming because add is more commutable.......</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>