<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 - [X86] Instruction reordering prevents EFLAGS folding"
   href="https://bugs.llvm.org/show_bug.cgi?id=33362">33362</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[X86] Instruction reordering prevents EFLAGS folding
          </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>Windows NT
          </td>
        </tr>

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

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>Backend: X86
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>llvm-dev@redking.me.uk
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>andrea.dibiagio@gmail.com, davide@freebsd.org, filcab@gmail.com, llvm-bugs@lists.llvm.org, spatel+llvm@rotateright.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre><a href="https://godbolt.org/g/l1T1da">https://godbolt.org/g/l1T1da</a>

Swapping 2 instructions over prevents the folding of an EFLAGS result,
requiring an additional comparison to regenerate them. Ideally we'd recognise
which instructions are actually using the EFLAGS result:

define i32 @clz_i128(i64, i64)  {
  %3 = tail call i64 @llvm.ctlz.i64(i64 %1, i1 false)
  %4 = tail call i64 @llvm.ctlz.i64(i64 %0, i1 false)
  %5 = icmp ne i64 %0, 0
  %6 = select i1 %5, i64 0, i64 %3
  %7 = add nuw nsw i64 %6, %4
  %8 = trunc i64 %7 to i32
  ret i32 %8
}
define i32 @clz_i128_swap(i64, i64)  {
  %3 = tail call i64 @llvm.ctlz.i64(i64 %0, i1 false)    <-- SWAP
  %4 = tail call i64 @llvm.ctlz.i64(i64 %1, i1 false)    <-- SWAP
  %5 = icmp ne i64 %0, 0
  %6 = select i1 %5, i64 0, i64 %4
  %7 = add nuw nsw i64 %6, %3
  %8 = trunc i64 %7 to i32
  ret i32 %8
}
declare i64 @llvm.ctlz.i64(i64, i1)

clz_i128:
        lzcntq  %rsi, %rcx
        xorl    %edx, %edx
        lzcntq  %rdi, %rax
        cmovael %edx, %ecx
        addl    %ecx, %eax
        retq
clz_i128_swap:
        lzcntq  %rdi, %rax
        lzcntq  %rsi, %rcx
        xorl    %edx, %edx
        testq   %rdi, %rdi    <-- EXTRA TEST
        cmovnel %edx, %ecx
        addl    %ecx, %eax
        retq</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>