[llvm-bugs] [Bug 31175] New: [AArch64] unnecessary cmp instruction with subtract and conditional select

via llvm-bugs llvm-bugs at lists.llvm.org
Sun Nov 27 15:56:40 PST 2016


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

            Bug ID: 31175
           Summary: [AArch64] unnecessary cmp instruction with subtract
                    and conditional select
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: Common Code Generator Code
          Assignee: unassignedbugs at nondot.org
          Reporter: spatel+llvm at rotateright.com
                CC: llvm-bugs at lists.llvm.org
    Classification: Unclassified

; ret = (x-y) > 0 ? x-y : 0
define i32 @max(i32 %x, i32 %y) {
  %sub = sub nsw i32 %x, %y
  %cmp = icmp sgt i32 %sub, 0
  %sel = select i1 %cmp, i32 %sub, i32 0
  ret i32 %sel
}


Using AArch64 as the target because that shows the problem, but this is likely
a bug for any target that has a subtract that sets comparison flags:

$ ./llc -o - max.ll -mtriple=aarch64 
    sub        w8, w0, w1  <--- could have used 'subs'
    cmp        w8, #0      <--- and eliminated this cmp
    csel    w0, w8, wzr, gt
    ret


This IR equivalent shows the better codegen:

define i32 @max_better(i32 %x, i32 %y) {
  %sub = sub nsw i32 %x, %y
  %cmp = icmp sgt i32 %x, %y
  %sel = select i1 %cmp, i32 %sub, i32 0
  ret i32 %sel
}

$ ./llc -o - max.ll -mtriple=aarch64
    subs        w8, w0, w1
    csel    w0, w8, wzr, gt
    ret

-- 
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/20161127/fb05f6db/attachment.html>


More information about the llvm-bugs mailing list