Playing with devirtualization in g++4.9

Markus Trippelsdorf markus@trippelsdorf.de
Thu Apr 3 12:46:00 GMT 2014


On 2014.04.02 at 18:49 +0200, Robert Matusewicz wrote:
> 
> I found that g++4.9.0 have -fdevirtualize switch and wanted to play with
> that feature for a while. I wrote simple, non-trivial program to check
> how g++ will behave in obvious case:
> 
> ==== SOURCE BEGIN ====
> #include <iostream>
> 
> class B final
> {
> public:
>     virtual void test() { std::cout << "Test" << std::endl;  }
> };
> 
> int main()
> {
>     B test;
>     test.test();
>     return 0;
> }
> ==== SOURCE ENDS ====
> 
> I compiled this code with
> 
> g++ -std=c++11 -fdevirtualize simple1.cpp
> 
> and then objdumped symbols:
> 
> objdump -t a.out | c++filt  | grep vtable
> 
> and noticed that vtable is present (I would expect it will be removed)
> 00000000006012c0  w    O .bss	0000000000000058              vtable for
> __cxxabiv1::__class_type_info@@CXXABI_1.3
> 0000000000400b20  w    O .rodata	0000000000000018              vtable for B
> 
> 
> I found some examples in testsuite (opt/ and ipa/) but for those
> examples I checked, I can "reproduce" devirtualization.
> 
> Is there a documentation somewhere to see what is the current status of
> devirtualiztion in g++ 4.9? I mean, beside the source code. Or maybe
> someone could explain me why this case can't be devirtualized?

The best documentation is a series of blog posts from Honza, who
implemented the devirtualization machinery:

http://hubicka.blogspot.com/2014/01/devirtualization-in-c-part-1.html
http://hubicka.blogspot.com/2014/01/devirtualization-in-c-part-2-low-level.html
http://hubicka.blogspot.com/2014/02/devirtualization-in-c-part-3-building.html
http://hubicka.blogspot.com/2014/02/devirtualization-in-c-part-4-analyzing.html

As for your example, if you replace std::endl with "\n" gcc will
devirtualize the member function.

-- 
Markus



More information about the Gcc-help mailing list