[llvm-bugs] [Bug 34210] New: Extra moves due to failure to commute sub+add to sub+sub

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Aug 16 05:56:18 PDT 2017


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

            Bug ID: 34210
           Summary: Extra moves due to failure to commute sub+add to
                    sub+sub
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Backend: X86
          Assignee: unassignedbugs at nondot.org
          Reporter: llvm-dev at redking.me.uk
                CC: llvm-bugs at lists.llvm.org, spatel+llvm at rotateright.com

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

-- 
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/20170816/858adf18/attachment.html>


More information about the llvm-bugs mailing list