From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id CD8ED3858401; Thu, 1 Jun 2023 09:35:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CD8ED3858401 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1685612101; bh=0Ke4DEalZ9UL/PmzEhKCHQclJ0GXxsa8dpIB/UEMYZE=; h=From:To:Subject:Date:In-Reply-To:References:From; b=VATYnwef6/maFYC5b7g8SPGQW0QRKpOhQAeIXvPUD6qQ6s0R9KDjoKSgIOAttwQ/X TCBuThjY3+zom5DdgF7g1ZukA+dRnHzfWEAvr2UGEX7foa0OpZ+lZrZUGWaV6/pEGL rV2azr9EcHz7LPpf4CVWlhpuplIpdtZ+XChAoah8= From: "yongxiangng at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug ipa/110057] Missed devirtualization opportunities Date: Thu, 01 Jun 2023 09:35:00 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: ipa X-Bugzilla-Version: 14.0 X-Bugzilla-Keywords: missed-optimization 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: Message-ID: In-Reply-To: References: 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 --- Comment #3 from Ng YongXiang --- 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, an= d 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 ther= e'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 ob= ject in vector at compile time */ /* { dg-options "-O3 -fdump-tree-optimized -fno-inline" } */ #include #include #include 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"} } */=