public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Playing with devirtualization in g++4.9
@ 2014-04-02 16:49 Robert Matusewicz
  2014-04-02 17:04 ` Jonathan Wakely
  2014-04-03 12:46 ` Markus Trippelsdorf
  0 siblings, 2 replies; 4+ messages in thread
From: Robert Matusewicz @ 2014-04-02 16:49 UTC (permalink / raw)
  To: gcc-help

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

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?

Best regards
Robert
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.14 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIcBAEBAgAGBQJTPD+dAAoJEATzNIO2M4RN7KUP/RwDVzk+yGdDT9Lhl7bHxCpu
57AFNybReEB0kiqYB0znkagpZZ2pumVF2lKGY9cT48f88fcR20nSq2sy0M6Y+a8D
v+GbH7cuTPg1wfDe3mJogKUMCCuax23FHetsf6PXkRW72V+TI+PROtlEIDLM7iGs
dvezN75dzKCyS6avhJymJg8bIheylBzgp6wnX1VyLOcRiqVoViHPPVQc7JAMaE+s
RW1wupE3pWAWevYGdWpPEgjd02+8/nQgz8OsT9aVCyZw3+txtEpNE+CHgw3S2xNd
aDNpWUlfWIS6d9gmT9hIbk40PkMBrigFktN7eqcOs6glaQhP0dfyaMy69TiKABKs
bGONJQcNQ8x+Vqn2MfHeM+jRjd1XneT+av6nx8AOO/lfjX41vQMMZQ9ZysN3aIj7
zLxnsk/pbuqa8mjVzt/fmO0IXCop7mH16ZKE6W74GHhh9BQDG8urIlCIIYZ04Qbp
mKuQFaGfrUF20OxXpgkvNM0jzhNUT5h5mSrgqeJ3A4LINXksdBC+VhMP+9QUvQ5i
9aNBGpfSQK7svCjeDVezgZhA7TmmLZlZQfT6bYIAhWw8rR7OByXriMrlm+IrxsjG
t6vdgxrn4DTOWYrCTh+okY89cK7BhgzVN4zFLGRE3hE5k8DhroT1RwEoY9v2JaGc
SNEbtSf7FNpBRWRPZjF0
=l2Ey
-----END PGP SIGNATURE-----

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Playing with devirtualization in g++4.9
  2014-04-02 16:49 Playing with devirtualization in g++4.9 Robert Matusewicz
@ 2014-04-02 17:04 ` Jonathan Wakely
  2014-04-02 17:05   ` Jonathan Wakely
  2014-04-03 12:46 ` Markus Trippelsdorf
  1 sibling, 1 reply; 4+ messages in thread
From: Jonathan Wakely @ 2014-04-02 17:04 UTC (permalink / raw)
  To: Robert Matusewicz; +Cc: gcc-help

On 2 April 2014 17:49, 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

You can't turn on individual optimisations with just -fxxx switches.

If you don't enable optimisation with -On for n > 0 then no
optimisations happen, so you need at least -O -fdevirtualisation

> 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 don't know whether devirtualisation will actually stop the vtable
being emitted into the executable, or whether it just avoids
indirecting through the vtable for the call.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Playing with devirtualization in g++4.9
  2014-04-02 17:04 ` Jonathan Wakely
@ 2014-04-02 17:05   ` Jonathan Wakely
  0 siblings, 0 replies; 4+ messages in thread
From: Jonathan Wakely @ 2014-04-02 17:05 UTC (permalink / raw)
  To: Robert Matusewicz; +Cc: gcc-help

On 2 April 2014 18:04, Jonathan Wakely wrote:
> If you don't enable optimisation with -On for n > 0 then no
> optimisations happen, so you need at least -O -fdevirtualisation

Stupid fingers! That should be -fdevirtualize obviously

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Playing with devirtualization in g++4.9
  2014-04-02 16:49 Playing with devirtualization in g++4.9 Robert Matusewicz
  2014-04-02 17:04 ` Jonathan Wakely
@ 2014-04-03 12:46 ` Markus Trippelsdorf
  1 sibling, 0 replies; 4+ messages in thread
From: Markus Trippelsdorf @ 2014-04-03 12:46 UTC (permalink / raw)
  To: Robert Matusewicz; +Cc: gcc-help

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2014-04-03 12:46 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-04-02 16:49 Playing with devirtualization in g++4.9 Robert Matusewicz
2014-04-02 17:04 ` Jonathan Wakely
2014-04-02 17:05   ` Jonathan Wakely
2014-04-03 12:46 ` Markus Trippelsdorf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).