<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </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 --- - [CodeGen] suboptimal code for ffs()"
   href="https://llvm.org/bugs/show_bug.cgi?id=31399">31399</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[CodeGen] suboptimal code for ffs()
          </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>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Common Code Generator Code
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>davide@freebsd.org
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>filcab@gmail.com, llvm-bugs@lists.llvm.org, llvm-dev@redking.me.uk, simon.f.whittaker@gmail.com
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>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)</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>