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:
   0x0000000000001040 <+0>:     endbr64 
   0x0000000000001044 <+4>:     lea    0x2d75(%rip),%rdi        # 0x3dc0
<_ZN12_GLOBAL__N_1L1bE>
   0x000000000000104b <+11>:    jmp    0x1150
<_ZNK12_GLOBAL__N_13bar7getInt1Ev>


clang-12 output:
Dump of assembler code for function main:
   0x0000000000401110 <+0>:     mov    $0x1,%eax
   0x0000000000401115 <+5>:     ret

Reply via email to