Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
44 Views

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
3c3
< # 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
94d93
<       xorl    %edi, %edi
97c96
<       movb    $8, %dil
---
>       movl    $8, %edi
99d97
<       movq    %rax, %rdi
102,103d99
<       movq    (%rdi), %rax
<       call    *16(%rax)

 

 

0 Kudos
3 Replies
Highlighted
Beginner
44 Views

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

0 Kudos
Highlighted
New Contributor I
44 Views

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

Regards.

0 Kudos
Highlighted
Beginner
44 Views

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
#endif
//#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;
}

 

0 Kudos