public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/110057] New: Missed devirtualization opportunities
@ 2023-05-31 10:08 yongxiangng at gmail dot com
  2023-06-01  0:15 ` [Bug ipa/110057] " pinskia at gcc dot gnu.org
                   ` (11 more replies)
  0 siblings, 12 replies; 13+ messages in thread
From: yongxiangng at gmail dot com @ 2023-05-31 10:08 UTC (permalink / raw)
  To: gcc-bugs

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

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2024-05-27  9:29 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-31 10:08 [Bug tree-optimization/110057] New: Missed devirtualization opportunities 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
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

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).