public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "yongxiangng at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug ipa/110057] Missed devirtualization opportunities
Date: Thu, 01 Jun 2023 09:35:00 +0000	[thread overview]
Message-ID: <bug-110057-4-wHoI2A6ykb@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-110057-4@http.gcc.gnu.org/bugzilla/>

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"} } */

  parent reply	other threads:[~2023-06-01  9:35 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-31 10:08 [Bug tree-optimization/110057] New: " yongxiangng at gmail dot com
2023-06-01  0:15 ` [Bug ipa/110057] " pinskia at gcc dot gnu.org
2023-06-01  0:24 ` pinskia at gcc dot gnu.org
2023-06-01  9:35 ` yongxiangng at gmail dot com [this message]
2023-06-01  9:37 ` yongxiangng at gmail dot com
2023-06-01 13:37 ` pinskia at gcc dot gnu.org
2023-06-02  3:43 ` yongxiangng at gmail dot com
2023-06-05  3:11 ` yongxiangng at gmail dot com
2023-06-05  3:13 ` yongxiangng at gmail dot com
2023-07-03  1:09 ` yongxiangng at gmail dot com
2023-07-28 15:40 ` cvs-commit at gcc dot gnu.org
2024-04-12 22:01 ` pinskia at gcc dot gnu.org
2024-05-27  9:29 ` user202729 at protonmail dot com

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-110057-4-wHoI2A6ykb@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).