Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.
7956 Discussions

C++11 final keyword & virtual function calls optimization

Richard_G_4
Beginner
515 Views

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
Richard_G_4
Beginner
515 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
QIAOMIN_Q_
New Contributor I
515 Views

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

Regards.

0 Kudos
Richard_G_4
Beginner
515 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
Reply