[llvm-bugs] [Bug 31399] New: [CodeGen] suboptimal code for ffs()

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Dec 15 15:25:40 PST 2016


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

            Bug ID: 31399
           Summary: [CodeGen] suboptimal code for ffs()
           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: davide at freebsd.org
                CC: filcab at gmail.com, llvm-bugs at lists.llvm.org,
                    llvm-dev at redking.me.uk, simon.f.whittaker at gmail.com
    Classification: Unclassified

Given 

#include <strings.h>
int f(int x) {
  return ffs(x) + 1;
}

compiled with -O3, clang 3.9.0 generates

f(int):                                  # @f(int)
        bsfl    %edi, %ecx
        addl    $2, %ecx
        testl   %edi, %edi
        movl    $1, %eax
        cmovnel %ecx, %eax
        retq

while gcc 6.2 generates:

f(int):
        bsf     eax, edi
        mov     edx, -1
        cmove   eax, edx
        add     eax, 2
        ret

We could save a test instruction. I don't think it matters a lot in practice,
but maybe a fun exercise if somebody wants to take a look (cc:ing Simon W. who
was looking for small codegen bugs to fix).

(Note that we lower ffs() to (x != 0) ? llvm.cttz(x) + 1 : 0 in
SimplifyLibCalls.cpp)

-- 
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/20161215/ba2bc916/attachment.html>


More information about the llvm-bugs mailing list