Bug 59883 - Missed C++ front-end devirtualizations
Summary: Missed C++ front-end devirtualizations
Status: UNCONFIRMED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.9.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2014-01-19 19:09 UTC by Jan Hubicka
Modified: 2016-08-14 11:49 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jan Hubicka 2014-01-19 19:09:21 UTC
I believe the following testcase:
     struct A 
    {
       virtual int foo (void) {return foo();}
    }; 

    struct B {
       struct A a;
    };
    struct A a[7]; 

    int test(void)
    {
      return a[3].foo();
    } 

    int test2(struct B *b)
    {
      return b->a.foo();
    }
ought to get devirtualized by C++ FE based on the fact that the object is contained within an structure or array. (this is related to PR46507)

In the following testcase:
namespace {
  struct A 
  {
    virtual int foo (void) {return 42;}
  };
}
int test(void)
{
  struct A a, *b=&a;
  return b->foo();
}

We can now probably use ipa-devirt's type inheritance graph to work out right away that A is a final class.

And finally:
struct A 
{
   virtual int foo (void) {return foo();}
};
IMO allows devirtualization of self recursive functions
Comment 1 Jan Hubicka 2014-01-29 20:41:31 UTC
The self-recursion testcase is wrong, as can be seen by the following testcase:

#include <stdio.h>
struct A
{
   virtual int foo (void) {return foo()+1;}
};
struct B: public A
{
   virtual int foo (void) {return 1;}
};
main()
{
  struct B b;
  printf("%i\n",b.A::foo());
}

Still, given that recursive virtual functions seems frequent, I wonder if we can't
try to track such explicit calls that breaks the assumption that a virtual function is called only for derived types that do not overwrite it and try to optimize at least for local functions...