<html>
    <head>
      <base href="http://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 --- - bit-scan-reverse / count-leading-zeros loop not recognized"
   href="http://llvm.org/bugs/show_bug.cgi?id=17130">17130</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>bit-scan-reverse / count-leading-zeros loop not recognized
          </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>Scalar Optimizations
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>kkhoo@perfwizard.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>This is very similar to <a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - bit-scan-forward / count-trailing-zeros loop not recognized"
   href="show_bug.cgi?id=17128">bug 17128</a> (count trailing zeros), but I'm filing a
separate bug just in case the solution involves some differences in
implementation in the compiler.

On the surface, this one maps to existing hardware instructions (Power =
ctlzw/d, ARM = clz) more easily than count-trailing-zeros. x86 has bsr/lzcnt as
well as bsf/tzcnt.

Here's a test file with a couple of implementations of count-leading-zeros.
Currently, llvm recognizes that both functions have bit-testing loops ('bt'
instructions is generated), but doesn't know that they can be simplified to
count-leading-zeros:

$ ./clang -v
clang version 3.4 (trunk 189776)
Target: x86_64-apple-darwin11.4.2
Thread model: posix

$ cat lzcnt.c 
int lzcnt(int x) {
    int count = 0;
    int i = 31;
    while (i>=0 && (((x >> i) & 0x1) == 0)) {
        i--;
        count++;
    }
    return count;
}

int lzcnt2(int x) {
    int count = 0;
    int i;
    for (i=0; i<32; i++) {
        if ((x & (1 << (31-i))) != 0) break;
        count++;
    }
    return count;
}


$ ./clang -S -O3 -fomit-frame-pointer -march=core-avx2 -o /dev/stdout lzcnt.c 
    .section    __TEXT,__text,regular,pure_instructions
    .globl    _lzcnt
    .align    4, 0x90
_lzcnt:                                 ## @lzcnt
    .cfi_startproc
## BB#0:                                ## %entry
    xorl    %eax, %eax
    movl    $32, %ecx
    .align    4, 0x90
LBB0_1:                                 ## %land.rhs
                                        ## =>This Inner Loop Header: Depth=1
    decl    %ecx
    btl    %ecx, %edi
    jb    LBB0_3
## BB#2:                                ## %while.body
                                        ##   in Loop: Header=BB0_1 Depth=1
    incl    %eax
    testl    %ecx, %ecx
    jg    LBB0_1
LBB0_3:                                 ## %while.end
    ret
    .cfi_endproc

    .globl    _lzcnt2
    .align    4, 0x90
_lzcnt2:                                ## @lzcnt2
    .cfi_startproc
## BB#0:                                ## %entry
    xorl    %eax, %eax
    movl    $31, %ecx
    .align    4, 0x90
LBB1_1:                                 ## %for.body
                                        ## =>This Inner Loop Header: Depth=1
    btl    %ecx, %edi
    jb    LBB1_3
## BB#2:                                ## %if.end
                                        ##   in Loop: Header=BB1_1 Depth=1
    decl    %ecx
    incl    %eax
    cmpl    $32, %eax
    jl    LBB1_1
LBB1_3:                                 ## %for.end
    ret
    .cfi_endproc</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>