<html>
    <head>
      <base href="https://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 --- - CGP generates inefficient branch for double selects"
   href="https://llvm.org/bugs/show_bug.cgi?id=29095">29095</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>CGP generates inefficient branch for double selects
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </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>LLVM Codegen
          </td>
        </tr>

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

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

        <tr>
          <th>CC</th>
          <td>davidxl@google.com, llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>int*
rbsearch(int c, int *t, int n, int ne)
{
        int *p;
        int m;

        while(n > 1) {
                m = n >> 1;
                p = t + m*ne;
                if(__builtin_expect(c >= p[0], true)) {
                        t = p;
                        n = n-m;
                } else
                        n = m;
        }
        return t;
}

For this test case, building with clang -O2, the generated x86 code is has
something like:

        cmpl    %edi, %r9d
        jle     .LBB0_3
# BB#2:
        movl    %r10d, %edx
        jmp     .LBB0_4
        .p2align        4, 0x90
.LBB0_3:
        subl    %r10d, %edx
.LBB0_4:
        cmpl    %edi, %r9d
        jg      .LBB0_6

Note that the two cmpl instructions are redundant. The problem is gone if the
code is compiled with -mllvm -phi-node-folding-threshold=0

The issue is that CGP generates 2 conditional branches (with same condition)
for 2 select instructions.</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>