C++11 final keyword & virtual function calls optimization

C++11 final keyword & virtual function calls optimization

Should making a derived class "final" cause calls to its virtual member functions to be replaced with static function calls? (Assuming the pointer type of your instance is the final type.) I tested the attached code with gcc & icc, it looks like icc14.0.1 doesn't make this optimization. 

Repro :

icc14 final.cpp -std=c++11 -o final.S.icc14 -S -O3 && icc14 final.cpp -std=c++11 -o final.S.icc14F -S -O3 -DFINAL=final

gcc47 final.cpp -std=c++11 -o final.S.gcc47 -S -O3 && gcc47 final.cpp -std=c++11 -o final.S.gcc47F -S -O3 -DFINAL=final

diff final.S.icc14 final.S.icc14F

diff final.S.gcc47 final.S.gcc47F

Output :

$ diff final.S.icc14 final.S.icc14F
< # mark_description "-std=c++11 -o final.S.icc14 -S -O3";
> # mark_description "-std=c++11 -o final.S.icc14F -S -O3 -DFINAL=final";
$ diff final.S.gcc47 final.S.gcc47F
<       xorl    %edi, %edi
<       movb    $8, %dil
>       movl    $8, %edi
<       movq    %rax, %rdi
<       movq    (%rdi), %rax
<       call    *16(%rax)



Downloadtext/x-c++src final.cpp0 bytes
4 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Hi intel,

Please can you implement this optimization for the next version of icc. It is potentially useful optimization when you are calling virtual member functions of a template class argument. GCC has already implemented this optimization, so I would expect GCC's C++11 performance to beat ICC in this case.


Thanks ,but your final.cpp is 0 bytes .Can you please check this file ?


Attached final.cpp, and pasted below :

#include <stdlib.h>

using namespace std;

class Base {
public :
 virtual ~Base() {}
 virtual int foo() { return 1; }

#ifndef FINAL
#define FINAL
//#define FINAL final

class Derived FINAL : Base {
public :
 virtual ~Derived() {}
 virtual int foo() { return 2; }

int main(int ac, const char **av) {
  Derived * d = (rand()==9999) ? NULL : new Derived();
        int x = d->foo();
        return 0;



Downloadtext/x-c++src final_0.cpp405 bytes

Leave a Comment

Please sign in to add a comment. Not a member? Join today