https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110057

            Bug ID: 110057
           Summary: Missed devirtualization opportunities
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: yongxiangng at gmail dot com
  Target Milestone: ---

There are some missed devirtualization opportunities involving vectors. The
issue is discussed in the stackoverflow here.

https://stackoverflow.com/questions/73257739/missed-optimization-stdvectortpop-back-not-qualifying-destructor-call/

Because we know the type of the objects in the vector (they have to be _Tp),
there is no need to invoke the virtual methods and we can devirtualize them. I
believe this applies not just to the destructor, but also other member
functions.

This missed optimization does not only affect vector. When creating an array,
the virtual destructor is invoked even though it should be quite clear to the
compiler that it can resolve the type of the object in the array at compile
time.

There is a possibility of using placement new and "spoofing" the type
https://godbolt.org/z/3MdMsdKha (thanks Jonathan Wakely), but I'm not sure if
this is legal c++ and if this applies to raw arrays

I think this might be a problem with the IPA component, I might have
mislabelled the issue because there isn't an option for IPA. Please let me know
if this makes sense or I have some misconception.

A sample program that shows this behavior is below.

/* { dg-do run } */
/* Virtual calls should be devirtualized because we know dynamic type of object
in vector at compile time */
/* { dg-options "-O3 -fdump-tree-ssa"  } */

#include <vector>

using std::vector;

class A {
public:
    virtual ~A()
    {

    }
};

class B  : public A
{
public:
    virtual ~B()
    {

    }
};

int main()
{
    vector<B> arr;
//    B b[40];
    return 0;
}

/* { dg-final { scan-tree-dump-times "OBJ_TYPE_REF" 0 "ssa"} } */

Reply via email to