没有通过gcc或clang优化函数指针的间接访问-错误或预期的?

Vit*_*meo 6 c++ optimization function-pointers conditional-operator compiler-optimization

给定以下两个功能:

int f() { return 0; }
int g() { return 1; }
Run Code Online (Sandbox Code Playgroud)

以下代码根据布尔值调用其中之一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); }
Run Code Online (Sandbox Code Playgroud)

两者g++ (trunk)clang++ (trunk)-std=c++2a -Ofast -march=native失败来优化下面的代码:

int main(int ac, char**) { return t0(ac & 1); }
Run Code Online (Sandbox Code Playgroud)

产生以下程序集:

main:
  and edi, 1
  mov eax, OFFSET FLAT:f()
  mov edx, OFFSET FLAT:g()
  cmove rax, rdx
  jmp rax
Run Code Online (Sandbox Code Playgroud)

调用t1t2(而不是t0)会产生以下优化的程序集:

main:
        mov     eax, edi
        not     eax
        and     eax, 1
        ret
Run Code Online (Sandbox Code Playgroud)

一切都可以gcc.godbolt.org上实时复制


我感到困惑的是,t0直接调用main不会得到优化,而调用它t2可以。

是否有原因导致调用t0产生的装配与t1或产生的装配不同t2还是这g++ (trunk)和双方都错过了优化机会clang++ (trunk)