<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 - Conditional branches are exchanged unoptimally"
   href="https://bugs.llvm.org/show_bug.cgi?id=49336">49336</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Conditional branches are exchanged unoptimally
          </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>Linux
          </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>carrot@google.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>craig.topper@gmail.com, llvm-bugs@lists.llvm.org, llvm-dev@redking.me.uk, pengfei.wang@intel.com, spatel+llvm@rotateright.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>The following code is extracted from zippy. Compile it with -O2

#include <utility>
#define PREDICT_FALSE(x) __builtin_expect((x), 0)

volatile int g;

std::pair<char*, int>
foo(char* ip, char* ip_limit, int op) {
    do {
      char* old_ip = ip; 
      int tag_type = g;
      ip = ip + 1;
      int offset = *old_ip;
      if (offset < 8) {
          ip = ip + 2;
      }   
      int delta = op - offset;
      if (PREDICT_FALSE(delta < 0)) {     
        if (tag_type != 0) {             
            ip = old_ip;
            break;
        }
      }   
      op += 8;
    } while (ip < ip_limit);
  return {ip, op};
}

LLVM generates:

_Z3fooPcS_i:                            # @_Z3fooPcS_i
        .cfi_startproc
# %bb.0:                                # %entry
        movq    %rdi, %rax
        jmp     .LBB0_1
        .p2align        4, 0x90
.LBB0_3:                                # %cleanup
                                        #   in Loop: Header=BB0_1 Depth=1
        movb    %r8b, %cl 
        leaq    (%rax,%rcx,2), %rax
        addq    $1, %rax
        addl    $8, %edx
        cmpq    %rsi, %rax
        jae     .LBB0_4
.LBB0_1:                                # %do.body
                                        # =>This Inner Loop Header: Depth=1
        movl    g(%rip), %r9d
        movsbl  (%rax), %edi
        xorl    %ecx, %ecx
        cmpl    $8, %edi
        setl    %r8b
        testl   %r9d, %r9d              // if (tag_type != 0)
        je      .LBB0_3
# %bb.2:                                # %do.body
                                        #   in Loop: Header=BB0_1 Depth=1
        cmpl    %edi, %edx              // if (PREDICT_FALSE(delta < 0))
        jge     .LBB0_3
.LBB0_4:                                # %do.end
        retq

The condition (tag_type != 0) is unpredictable, but (delta < 0) is predicted as
false, so the unpredictable condition (tag_type != 0) is rarely executed.
But in the generated code, llvm changed the order of the two conditions, so the
unpredictable condition is always executed, causes more branch mis-prediction.</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>