[llvm-bugs] [Bug 52167] New: Failure to optimize negative abs properly

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Oct 13 12:21:52 PDT 2021


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

            Bug ID: 52167
           Summary: Failure to optimize negative abs properly
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Scalar Optimizations
          Assignee: unassignedbugs at nondot.org
          Reporter: gabravier at gmail.com
                CC: llvm-bugs at lists.llvm.org

long long abs_1(long long i)
{
  return (i > 0) ? -i : i;
}

This is "optimized" by LLVM to `return -llabs(i);`. Instead, it should be
possible to optimize it to avoid the negation by just changing the preceding
compare to the opposite one (as is already done in this example, and that LLVM
currently "optimizes" to have the current output). This is done by GCC, but not
by LLVM.

For comparison, LLVM outputs this on AMD64:

abs_1(long long):
  mov rax, rdi
  neg rax
  cmovl rax, rdi
  neg rax
  ret

GCC outputs this:

abs_1(long long):
  mov rax, rdi
  neg rax
  cmovns rax, rdi
  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/20211013/e0baa9fc/attachment.html>


More information about the llvm-bugs mailing list