<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 - lzcnt causes erroneous code generation (x86_64)"
   href="https://bugs.llvm.org/show_bug.cgi?id=35399">35399</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>lzcnt causes erroneous code generation (x86_64)
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>5.0
          </td>
        </tr>

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

        <tr>
          <th>OS</th>
          <td>Windows NT
          </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>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>michel.bernert@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=19461" name="attach_19461" title="Code compiled with : clang++ lzcnt.cpp -Wall -pedantic -O2 -mlzcnt -S">attachment 19461</a> <a href="attachment.cgi?id=19461&action=edit" title="Code compiled with : clang++ lzcnt.cpp -Wall -pedantic -O2 -mlzcnt -S">[details]</a></span>
Code compiled with : clang++ lzcnt.cpp -Wall -pedantic -O2 -mlzcnt -S

The use of lzcnt has an unexpected behavior, when using the -mlzcnt flag with
optimizations.

Compiling :

uint64_t test64_2(uint64_t x, bool& b1, bool& b2)
{
    uint64_t a = (x == 0 ? 64 : __builtin_clzll(x));
    b1 = (a != 64);
    b2 = !b1;
    return a;
}

Generates :

test64_2(unsigned long, bool&, bool&): # @test64_2(unsigned long, bool&, bool&)
  lzcntq %rdi, %rax
  setae (%rsi)
  setae (%rdx)
  retq

Which sets the two bools to the same value, even though 'b2 = !b1;' and I can't
see any undefined behavior.
This concerns all 16, 32 and 64 bits versions of lzcnt. Omitting -mlzcnt
produces the expected behavior for __builtin_clz versions, using bsr.</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>