From patchwork Fri Aug 12 14:26:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Hubicka X-Patchwork-Id: 56708 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9637C3858C2F for ; Fri, 12 Aug 2022 14:27:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9637C3858C2F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1660314446; bh=ZhWBb/6iJEK0RD7hQmVFMjT4ZG5m4KSSPvIHoyFJFzk=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=iACR9e2oQRReVlevor9q7VeT5nuxUHbZtSeCRzzMVKGYdILdCcR9+DgNNd+oXnUUQ s/WL7tmmH+jLgM3Z8E+MtTkgJa9XvWFjEl+tuQsU8Pt3iKyddWYlDt3lo6n0vQyYbP CqD+ufatyNinSaNE+aBO5ganwMS/EmWbvNdykkYE= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from nikam.ms.mff.cuni.cz (nikam.ms.mff.cuni.cz [195.113.20.16]) by sourceware.org (Postfix) with ESMTPS id 3A2853858D28 for ; Fri, 12 Aug 2022 14:26:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 3A2853858D28 Received: by nikam.ms.mff.cuni.cz (Postfix, from userid 16202) id D3A4A284A5A; Fri, 12 Aug 2022 16:26:55 +0200 (CEST) Date: Fri, 12 Aug 2022 16:26:55 +0200 To: gcc-patches@gcc.gnu.org Subject: Fix invalid devirtualization when combining final keyword and anonymous types Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jan Hubicka via Gcc-patches From: Jan Hubicka Reply-To: Jan Hubicka Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Hi, this patch fixes a wrong code issue where we incorrectly devirtualize to __builtin_unreachable. The problem occurs in combination of anonymous namespaces and final keyword used on methods. We do two optimizations here 1) when reacing final method we cut the search for possible new targets 2) if the type is anonymous we detect whether it is ever instatiated by looking if its vtable is referred to. Now this goes wrong when thre is an anonymous type with final method that is not instantiated while its derived type is. So if 1 triggers we need to make 2 to look for vtables of all derived types as done by this patch. Bootstrpaped/regtested x86_64-linux, comitted. Honza gcc/ChangeLog: 2022-08-10 Jan Hubicka PR middle-end/106057 * ipa-devirt.cc (type_or_derived_type_possibly_instantiated_p): New function. (possible_polymorphic_call_targets): Use it. gcc/testsuite/ChangeLog: 2022-08-10 Jan Hubicka PR middle-end/106057 * g++.dg/tree-ssa/pr101839.C: New test. diff --git a/gcc/ipa-devirt.cc b/gcc/ipa-devirt.cc index 412ca14f66b..265d07bb354 100644 --- a/gcc/ipa-devirt.cc +++ b/gcc/ipa-devirt.cc @@ -285,6 +285,19 @@ type_possibly_instantiated_p (tree t) return vnode && vnode->definition; } +/* Return true if T or type derived from T may have instance. */ + +static bool +type_or_derived_type_possibly_instantiated_p (odr_type t) +{ + if (type_possibly_instantiated_p (t->type)) + return true; + for (auto derived : t->derived_types) + if (type_or_derived_type_possibly_instantiated_p (derived)) + return true; + return false; +} + /* Hash used to unify ODR types based on their mangled name and for anonymous namespace types. */ @@ -3172,6 +3185,7 @@ possible_polymorphic_call_targets (tree otr_type, { odr_type speculative_outer_type; bool speculation_complete = true; + bool check_derived_types = false; /* First insert target from type itself and check if it may have derived types. */ @@ -3190,8 +3204,12 @@ possible_polymorphic_call_targets (tree otr_type, to walk derivations. */ if (target && DECL_FINAL_P (target)) context.speculative_maybe_derived_type = false; - if (type_possibly_instantiated_p (speculative_outer_type->type)) - maybe_record_node (nodes, target, &inserted, can_refer, &speculation_complete); + if (check_derived_types + ? type_or_derived_type_possibly_instantiated_p + (speculative_outer_type) + : type_possibly_instantiated_p (speculative_outer_type->type)) + maybe_record_node (nodes, target, &inserted, can_refer, + &speculation_complete); if (binfo) matched_vtables.add (BINFO_VTABLE (binfo)); @@ -3212,6 +3230,7 @@ possible_polymorphic_call_targets (tree otr_type, if (!speculative || !nodes.length ()) { + bool check_derived_types = false; /* First see virtual method of type itself. */ binfo = get_binfo_at_offset (TYPE_BINFO (outer_type->type), context.offset, otr_type); @@ -3229,16 +3248,18 @@ possible_polymorphic_call_targets (tree otr_type, if (target && DECL_CXX_DESTRUCTOR_P (target)) context.maybe_in_construction = false; - if (target) + /* In the case we get complete method, we don't need + to walk derivations. */ + if (target && DECL_FINAL_P (target)) { - /* In the case we get complete method, we don't need - to walk derivations. */ - if (DECL_FINAL_P (target)) - context.maybe_derived_type = false; + check_derived_types = true; + context.maybe_derived_type = false; } /* If OUTER_TYPE is abstract, we know we are not seeing its instance. */ - if (type_possibly_instantiated_p (outer_type->type)) + if (check_derived_types + ? type_or_derived_type_possibly_instantiated_p (outer_type) + : type_possibly_instantiated_p (outer_type->type)) maybe_record_node (nodes, target, &inserted, can_refer, &complete); else skipped = true; diff --git a/gcc/testsuite/g++.dg/tree-ssa/pr101839.C b/gcc/testsuite/g++.dg/tree-ssa/pr101839.C new file mode 100644 index 00000000000..bb7b61cad43 --- /dev/null +++ b/gcc/testsuite/g++.dg/tree-ssa/pr101839.C @@ -0,0 +1,53 @@ +// { dg-do run } +// { dg-options "-O2 -fdump-tree-optimized" } +// { dg-require-effective-target c++11 } + +#include +#include +#include +namespace { + struct Buf { + char * buf; int a{0}; int b{0}; + Buf(char * b) : buf(b) { } + void add(int v) { + ::memcpy(buf, &v, sizeof(v)); + a += sizeof(v); + b += sizeof(v); + } + }; + struct A { + virtual void fill(Buf &buf) { + buf.add(type()); + buf.add(type()); + } + virtual ~A() {} + virtual int type() = 0; + }; + struct BA : A { + void fill(Buf &buf) { + A::fill(buf); + buf.add(type()); + buf.add(type()); + } + int type() final { + return 1; + } + }; + struct CBA final : BA { + }; + struct CA final : A { + ::std::map m; + int type() final { + return 2; + } + }; +} +int main(int argc, char ** ) { + char d[1024]; + CBA cba; + Buf buf(d); + cba.fill(buf); + CA ca; + return 0; +} +// { dg-final { scan-tree-dump-not "__builtin_unreachable" "optimized" } }