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

--- Comment #3 from Ng YongXiang <yongxiangng at gmail dot com> ---
I'm giving the example of an array for now, because gcc treatment of the
destructor is inconsistent and depends on the length of the array. Clang on the
other hand is able to devirtualize the destructor in the array no matter the
length of the array.

Virtual call

https://godbolt.org/z/f33Gh5EGM

Devirtualized call

https://godbolt.org/z/jPz3Ka1cd

We know it is devirtualized because the destructor of the derived object is not
called and the compiler assumes that the items in the array are all Base
objects.

So currently, gcc does perform this kind of "devirtualization" (I wouldn't
really call it a devirtualization for an array, because it is similar to
declaring Derived d; on the stack), but it is dependent on array length, and I
think we should make it devirtualized for all length of array.

I changed my testing to dump tree optimized like below (let me know if there's
issues with the example because I am not familiar with hacking gcc).

/* { 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-optimized -fno-inline"  } */

#include <vector>
#include <iostream>
#include <memory>

using std::vector;

class A
{
public:
    virtual ~A()
    {
    }
};

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

int main()
{
    B b[3];
    return 0;
}

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

Reply via email to