From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 142F33858D20; Wed, 31 May 2023 10:08:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 142F33858D20 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1685527706; bh=J/uUFAQvsRGgY2iv955FmaBJ8nhSZPaNoz/k5q4WMpE=; h=From:To:Subject:Date:From; b=JdERHWxKD+W4DEb/8g++urCvsW039hheLUqjMBiXhfMyLj7YQ4WfhckLLRKPheMr3 C3HXs/HMRbSlkeP2YtuyhCHonmVAIQNSkuWRfdMj/FvUJMG+xqpXiqKcule4SuEtOa dDZSZAns70upcTe423nQg9cX84Q1Cf9H6aSoht6g= From: "yongxiangng at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug tree-optimization/110057] New: Missed devirtualization opportunities Date: Wed, 31 May 2023 10:08:25 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: tree-optimization X-Bugzilla-Version: 14.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: yongxiangng at gmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D110057 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-stdvectort= pop-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 arra= y, the virtual destructor is invoked even though it should be quite clear to t= he 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 ob= ject in vector at compile time */ /* { dg-options "-O3 -fdump-tree-ssa" } */ #include using std::vector; class A { public: virtual ~A() { } }; class B : public A { public: virtual ~B() { } }; int main() { vector arr; // B b[40]; return 0; } /* { dg-final { scan-tree-dump-times "OBJ_TYPE_REF" 0 "ssa"} } */=