<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 - __builtin_assume causes a call to a function marked as __attribute__((const)) or ((pure)) to be needlessly propagated"
   href="https://bugs.llvm.org/show_bug.cgi?id=43010">43010</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>__builtin_assume causes a call to a function marked as __attribute__((const)) or ((pure)) to be needlessly propagated
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </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>Scalar Optimizations
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>mike.k@digitalcarbide.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>In a situation when a call to an external __attribute__((const)) or
__attribute__((pure)) is encapsulated in a __builtin_assume, and then called,
the function is still called despite having no side-effects and the result
value being known.

extern int foo() __attribute__((const));
extern void oof();

void bar() {
    __builtin_assume(foo() == 1);

    if (foo() == 1) {
        oof();
    }
}

results in the following x86-64 assembly with -03:

bar(): # @bar()
  push rax
  call foo()
  pop rax
  jmp oof() # TAILCALL

The `call foo()`, as stated, is unnecessary.

If the code is refactored to use a temporary variable for the result, as such:

extern int foo() __attribute__((const));
extern void oof();

void bar() {
    int foo_result = foo();
    __builtin_assume(foo_result == 1);

    if (foo_result == 1) {
        oof();
    }
}

It then produces the expected output:

bar(): # @bar()
  jmp oof() # TAILCALL</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>