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)



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



Download final_0.cpp405 bytes

Deixar um comentário

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