<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 - Regression with builtin_expect over multiple predicates"
   href="https://bugs.llvm.org/show_bug.cgi?id=33336">33336</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Regression with builtin_expect over multiple predicates
          </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>normal
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>Common Code Generator Code
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>zamsden@cloudera.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=18584" name="attach_18584" title="Reproducing case">attachment 18584</a> <a href="attachment.cgi?id=18584&action=edit" title="Reproducing case">[details]</a></span>
Reproducing case

Testing using an UNLIKELY macro built around builtin_expect found a regression
from 3.7 -> 3.8 that is still present in the 4.0 and likely trunk (untested)
branches of llvm.  The unlikely condition is not propagated to both
sub-expressions when combined with an OR clause.  Using multiple invocations of
such a macro gives the expected result.

Apparently the branch probabilities are no longer being propagated correctly to
child expressions.

I bisected llvm and clang in parallel on release_37 and release_38 branches to
find the responsible change and found this change to be responsible.  Seems
entirely plausible as it introduces a new branch:

    [JumpThreading] Split select that has constant conditions coming from the
PHI node

    Look for PHI/Select in the same BB of the form

    bb:
      %p = phi [false, %bb1], [true, %bb2], [false, %bb3], [true, %bb4], ...
      %s = select p, trueval, falseval

    And expand the select into a branch structure. This later enables
    jump-threading over bb in this pass.

    Using the similar approach of SimplifyCFG::FoldCondBranchOnPHI(), unfold
    select if the associated PHI has at least one constant.  If the unfolded
    select is not jump-threaded, it will be folded again in the later
    optimizations.

    git-svn-id: <a href="https://llvm.org/svn/llvm-project/llvm/trunk@257198">https://llvm.org/svn/llvm-project/llvm/trunk@257198</a>
91177308-0d34-0410-b5e6-96231b3b80d


Attached code which generates a reproduction - you can see the sub-optimal
branch generated in the asm output.  Let me know if you want IR output as well,
but this reproduces quite easily.  I copied my reproducing case verbatim, there
is almost certainly a more minimal case that doesn't involve floating point,
but this works reliably - Godbolt link: <a href="https://godbolt.org/g/vBwwQU">https://godbolt.org/g/vBwwQU</a></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>