<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 - Missed optimization opportunity when returning function pointers based on run-time boolean"
   href="https://bugs.llvm.org/show_bug.cgi?id=41980">41980</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Missed optimization opportunity when returning function pointers based on run-time boolean
          </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>Windows NT
          </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>-New Bugs
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>vittorio.romeo@outlook.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>htmldeveloper@gmail.com, llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Given the following two functions:

    int f() { return 0; }
    int g() { return 1; }

And the following code to invoke one of them depending on a boolean `b`:

    int t0(bool b) { return (b ? &f : &g)(); }
    int t1(bool b) { return b ? f() : g(); }
    int t2(bool b) { return b ? t0(true) : t0(false); }

Both `g++ (trunk)` and `clang++ (trunk)` with `-std=c++2a -Ofast -march=native`
fail to optimize the following code:

    int main(int ac, char**) { return t0(ac & 1); }

Producing the following assembly:

<span class="quote">>     main:
>       and edi, 1
>       mov eax, OFFSET FLAT:f()
>       mov edx, OFFSET FLAT:g()
>       cmove rax, rdx
>       jmp rax
> </span >


Invoking `t1` or `t2` (instead of `t0`) produces the following optimized
assembly:

<span class="quote">>     main:
>             mov     eax, edi
>             not     eax
>             and     eax, 1
>             ret</span >


Everything can be reproduced live on **gcc.godbolt.org**:
<a href="https://godbolt.org/z/gh7270">https://godbolt.org/z/gh7270</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>