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
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)

 

 

AnexoTamanho
Download final.cpp0 bytes
4 posts / novo 0
Último post
Para obter mais informações sobre otimizações de compiladores, consulte Aviso sobre otimizações.

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

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

Regards.

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;
}

 

Anexos: 

AnexoTamanho
Download final_0.cpp405 bytes

Deixar um comentário

Faça login para adicionar um comentário. Não é membro? Inscreva-se hoje mesmo!