<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </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 - Wrong code generation for CTLZ pattern"
   href="https://bugs.llvm.org/show_bug.cgi?id=37973">37973</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Wrong code generation for CTLZ pattern
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>new-bugs
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </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>new bugs
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>ovmold@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>The motivating example:

int lzcnt(int x) 
{
   int count = 0;
   while (x > 0) {
      count++;
      x = x >> 1;
   }
   return count;
}

int main()
{
   int x = 1;
   int y = lzcnt(x);
   printf("count  = %d\n", y);
   return 0;
}

This code is compiled as: clang test.c -O3 -march=core-avx2

The pattern is recognized and the output looks like: "count  = 1". This is
wrong since the number of leading zeros for x = 1 is 31. 

Inside test.ll we can see:
define dso_local i32 @main() local_unnamed_addr #1 {
entry:
  %call1 = tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds 
                               ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), 
                               i32 1)
  ret i32 0
}

So, in my opinion, the problem is in the result substitution (i32 1 in
@printf), this is the result of lzcnt (number of leading 1 position), and not
quantity of leading zeros.

The same example but with scanf("%d", &x) in the main function produces the
following output for 1 as input: "count  = 32". This output is also wrong.

Inside test.ll we can see:
%1 = load i32, i32* %x, align 4, !tbaa !3
%cmp4.i = icmp sgt i32 %1, 0
%2 = call i32 @llvm.ctlz.i32(i32 %1, i1 false) #5, !range !2
%3 = sub nsw i32 32, %2

Possibly it should be: %3 = sub nsw i32 31, %2</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>