https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104597
--- Comment #2 from m.cencora at gmail dot com ---
Similarly when indirect call is a result of virtual function call, gcc cannot
optimize it, while clang can:
// main.cpp
struct foo
{
virtual int getInt0() const = 0;
virtual int getInt1() const = 0;
};
const foo& getFooInstance();
namespace
{
int test()
{
auto& foo = getFooInstance();
return foo.getInt1();
}
}
int main()
{
return test();
}
// lib1.cpp
struct foo
{
virtual int getInt0() const = 0;
virtual int getInt1() const = 0;
};
namespace
{
struct bar final : foo
{
int getInt0() const override
{
return 0;
}
int getInt1() const override
{
return 1;
}
};
constexpr bar b;
}
const foo& getFooInstance()
{
return b;
}
gcc-11 output:
Dump of assembler code for function main:
0x1040 <+0>: endbr64
0x1044 <+4>: lea0x2d75(%rip),%rdi# 0x3dc0
<_ZN12_GLOBAL__N_1L1bE>
0x104b <+11>:jmp0x1150
<_ZNK12_GLOBAL__N_13bar7getInt1Ev>
clang-12 output:
Dump of assembler code for function main:
0x00401110 <+0>: mov$0x1,%eax
0x00401115 <+5>: ret