<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 --- - __builtin_assume pessimization"
   href="https://llvm.org/bugs/show_bug.cgi?id=25476">25476</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>__builtin_assume pessimization
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>3.7
          </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>Backend: X86
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>matt@godbolt.org
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Consider the code: [c.f. <a href="https://goo.gl/7VB27y">https://goo.gl/7VB27y</a> ]

--
enum class Side { Bid, Ask };
struct Foo { int a; int b; };

int test(Side side, const Foo &foo) {
  if (side == Side::Bid) return foo.a;
  return foo.b;
}

int test_with_unreachable(Side side, const Foo &foo) {
  if (side == Side::Bid) return foo.a;
  if (side == Side::Ask) return foo.b;
  __builtin_unreachable();
}

int test_with_assume(Side side, const Foo &foo) {
  __builtin_assume(side == Side::Bid || side == Side::Ask);
  if (side == Side::Bid) return foo.a;
  return foo.b;
}

int test_with_assume2(Side side, const Foo &foo) {
  if (side == Side::Bid) return foo.a;
  __builtin_assume(side == Side::Ask);
  return foo.b;
}
--

One might expect the generated code to basically be the same, but the _assume2
case generates a branch instead of the conditional moves emitted elsewhere.

e.g.
--
test(Side, Foo const&):                     # @test(Side, Foo const&)
    lea    rax, [rsi + 4]
    test    edi, edi
    cmove    rax, rsi
    mov    eax, dword ptr [rax]
    ret
--

vs

--
test_with_assume2(Side, Foo const&):       # @test_with_assume2(Side, Foo
const&)
    test    edi, edi
    je    .LBB3_2
    add    rsi, 4
.LBB3_2:
    mov    eax, dword ptr [rsi]
    ret
--

This would appear to be a pessimization.

Arguably given the __builtin_assumption that the Side is either 0 or 1, one
might hope the following code could be generated:

--
mov eax, dword ptr [rsi+edx*4]
ret
--

GCC has a similar bug with __builtin_unreachable
(<a href="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68274">https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68274</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>