This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: C++ 'extern inline' magic possible?


"Kevin P. Fleming" <kpfleming@digium.com> writes:

> On a slightly related note... I've seen a couple of pages (but not the
> GCC manual's 'inlining' page) mention that it's possible for virtual
> member functions to be inlined if they are inlinable (either defined
> in the class declaration or the function declaration is visible and
> marked 'inline') and the call to them is a 'direct virtual function
> call'. This last term doesn't make any sense to me, and based on my
> understanding of vtables I don't see how a virtual member function
> could *ever* be inlined unless it was called in a non-virtual fashion
> (i.e. calling it as "Foo::bar()" when Foo is the class making the call
> or one of its bases). In any other situation, the compiler cannot know
> at compile time which member function would actually be called. Does
> this sound like a correct statement?

The direct virtual function call is indeed the call to Foo::bar().

However, there are other cases where the compiler can inline a virtual
function call.  If the compiler can see the use of the new operator
which created the object, then it knows the exact virtual table which
the object is using, and so can inline any virtual call.  That is a
fairly common though obviously somewhat limited case.  In gcc these
sorts of optimizations are implemented by -fdevirtualize, which is
enabled by default at -O2.

More subtly, the compiler can use profiling feedback to see which
virtual function is being called at any specific point in the code.  In
many programs a specific call site will almost always call a specific
virtual function implementation.  When the profiling feedback indicates
that, the compiler can insert a test for the expected virtual function.
If the test succeeds, the compiler can inline the call.  If the test
fails, the compiler can generate a normal virtual function call.  I
don't know off-hand whether gcc implements this optimization.

Ian


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]