title: Function pointer indirection not optimized by gcc or clang - bug or intended?
body: <p>Given the following two functions:</p>
  
  <pre><code>int f() { return 0; }
  int g() { return 1; }
  </code></pre>
  
  <p>And the following code to invoke one of them depending on a boolean <code>b</code>:</p>
  
  <pre><code>int t0(bool b) { return (b ? &amp;f : &amp;g)(); }
  int t1(bool b) { return b ? f() : g(); }
  int t2(bool b) { return b ? t0(true) : t0(false); }
  </code></pre>
  
  <p>Both <code>g++ (trunk)</code> and <code>clang++ (trunk)</code> with <code>-std=c++2a -Ofast -march=native</code> fail to optimize the following code:</p>
  
  <pre><code>int main(int ac, char**) { return t0(ac &amp; 1); }
  </code></pre>
  
  <p>Producing the following assembly:</p>
  
  <blockquote>
  <pre><code>main:
    and edi, 1
    mov eax, OFFSET FLAT:f()
    mov edx, OFFSET FLAT:g()
    cmove rax, rdx
    jmp rax
  </code></pre>
  </blockquote>
  
  <p>Invoking <code>t1</code> or <code>t2</code> (instead of <code>t0</code>) produces the following optimized assembly:</p>
  
  <blockquote>
  <pre><code>main:
          mov     eax, edi
          not     eax
          and     eax, 1
          ret
  </code></pre>
  </blockquote>
  
  <p>Everything can be reproduced <a href="https://godbolt.org/z/gh7270" rel="noreferrer">live on <strong>gcc.godbolt.org</strong></a>.</p>
  
  <hr>
  
  <p>I find it puzzling that invoking <code>t0</code> directly in <code>main</code> doesn't get optimized, while invoking it in <code>t2</code> is OK. </p>
  
  <p><strong>Is there a reason why invoking <code>t0</code> does not produce the same assembly as <code>t1</code> or <code>t2</code>?</strong> Or is this a missed optimization opportunity by both <code>g++ (trunk)</code> and <code>clang++ (trunk)</code>?</p>