From patchwork Thu May 16 23:12:42 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Carlini X-Patchwork-Id: 1100785 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-500961-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=oracle.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="RR8Ne7YD"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=oracle.com header.i=@oracle.com header.b="sWnhgNdp"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 454nJw4v3hz9s7h for ; Fri, 17 May 2019 09:13:02 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :subject:to:cc:message-id:date:mime-version:content-type; q=dns; s=default; b=jJoxuyrzwVYaEQP6cbTt2JNL8m1HIif3UewJgMNP9nGtrwb28X /HIHlwIU8ft8UAOHurMwc5s7DHQOqvqiW6TmWpp9XwHwY7H34COsgvZYnUYrhlcK 5qJub/9GX3qviyKBIZGUD2kviSmU3J25ASoaWex/MsxvZ8ELa+QWjCMpo= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :subject:to:cc:message-id:date:mime-version:content-type; s= default; bh=Y0R46arEdY0doWBm6Eiw8k89n5A=; b=RR8Ne7YDjNikNDVN+hFo J1UFnHceFCQk7faUSF9Y6ngc/LyEGcCpHKwIhcFZ3Qfh5WIk8BKlEppU6l+OhN0j vDUqkrrZN92dBMjySx7n87eXoQBrqMLOv845PacKIbuAwaIunGdtaJWV44NQPnyY UOlWFlrvdbIUIWI8Yz4PWDk= Received: (qmail 56605 invoked by alias); 16 May 2019 23:12:55 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 56597 invoked by uid 89); 16 May 2019 23:12:55 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-9.5 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, SPF_HELO_PASS autolearn=ham version=3.3.1 spammy=UD:oracle.com, HX-Languages-Length:3034, UD:x.foo, xfoo X-HELO: userp2120.oracle.com Received: from userp2120.oracle.com (HELO userp2120.oracle.com) (156.151.31.85) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 16 May 2019 23:12:53 +0000 Received: from pps.filterd (userp2120.oracle.com [127.0.0.1]) by userp2120.oracle.com (8.16.0.27/8.16.0.27) with SMTP id x4GN90aY177431; Thu, 16 May 2019 23:12:51 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : subject : to : cc : message-id : date : mime-version : content-type; s=corp-2018-07-02; bh=Rv1SPX0WbpgfRrPMIOMhNi7BbpLbgt/eh2zp5p+7CP8=; b=sWnhgNdpsb4JodR5Ysty95KFmcKGWtp11O/3JwarBd0gTOzy4kDlGDOoOM1t3wxbzu0O TTD+03Ii162Jff57bl/9deSvhuws3wR6RQFwQQpOHIVHFlLtDRIqdBx9/ZRAOlTOJvp+ cvLqeDPWCJoZnj2SywxjO7ygi3+sQKHadQWlYVXU02nvv2h4/RzcNGhMnAlFH9Vj+irh qWbQS1LnyHKNbpLb5XPKVpmK+rekCYwIwDobDdgAauwn1E0Gzanf76N08nDwEk14mqy4 lDcO1jllL+oic4qVcf9XxeWeY2QpcsOg/C+1jFTa4d3xYb2788FnOGh8uhqNb72ibM7p cg== Received: from userp3030.oracle.com (userp3030.oracle.com [156.151.31.80]) by userp2120.oracle.com with ESMTP id 2sdq1qxdth-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 16 May 2019 23:12:51 +0000 Received: from pps.filterd (userp3030.oracle.com [127.0.0.1]) by userp3030.oracle.com (8.16.0.27/8.16.0.27) with SMTP id x4GNCL4C055900; Thu, 16 May 2019 23:12:51 GMT Received: from userv0121.oracle.com (userv0121.oracle.com [156.151.31.72]) by userp3030.oracle.com with ESMTP id 2sgkx4c4h1-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 16 May 2019 23:12:50 +0000 Received: from abhmp0008.oracle.com (abhmp0008.oracle.com [141.146.116.14]) by userv0121.oracle.com (8.14.4/8.13.8) with ESMTP id x4GNCoK0010324; Thu, 16 May 2019 23:12:50 GMT Received: from [192.168.1.4] (/79.16.32.200) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Thu, 16 May 2019 16:12:49 -0700 From: Paolo Carlini Subject: [C++ Patch] PR 67184 ("Missed optimization with C++11 final specifier") To: "gcc-patches@gcc.gnu.org" Cc: Jason Merrill Message-ID: <67378ced-6d34-2617-d933-2adba3872ae4@oracle.com> Date: Fri, 17 May 2019 01:12:42 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 X-IsSubscribed: yes Hi, when Roberto Agostino and I implemented the front-end devirtualization of final overriders we missed this case, where it comes from the base. It seems to me that by way of access_path the existing approach can be neatly extended. Tested x86_64-linux. Thanks, Paolo. /////////////////////// /cp 2019-05-17 Paolo Carlini PR c++/67184 PR c++/69445 * call.c (build_over_call): Devirtualize when the final overrider comes from the base. /testsuite 2019-05-17 Paolo Carlini PR c++/67184 PR c++/69445 * g++.dg/other/final3.C: New. * g++.dg/other/final4.C: Likewise. * g++.dg/other/final5.C: Likewise. Index: cp/call.c =================================================================== --- cp/call.c (revision 271296) +++ cp/call.c (working copy) @@ -8241,7 +8241,7 @@ build_over_call (struct z_candidate *cand, int fla /* See if the function member or the whole class type is declared final and the call can be devirtualized. */ if (DECL_FINAL_P (fn) - || CLASSTYPE_FINAL (TYPE_METHOD_BASETYPE (TREE_TYPE (fn)))) + || CLASSTYPE_FINAL (TREE_TYPE (cand->access_path))) flags |= LOOKUP_NONVIRTUAL; /* [class.mfct.nonstatic]: If a nonstatic member function of a class Index: testsuite/g++.dg/other/final3.C =================================================================== --- testsuite/g++.dg/other/final3.C (nonexistent) +++ testsuite/g++.dg/other/final3.C (working copy) @@ -0,0 +1,26 @@ +// PR c++/67184 +// { dg-do compile { target c++11 } } +// { dg-options "-fdump-tree-original" } + +struct V { + virtual void foo(); +}; + +struct wV final : V { +}; + +struct oV final : V { + void foo(); +}; + +void call(wV& x) +{ + x.foo(); +} + +void call(oV& x) +{ + x.foo(); +} + +// { dg-final { scan-tree-dump-times "OBJ_TYPE_REF" 0 "original" } } Index: testsuite/g++.dg/other/final4.C =================================================================== --- testsuite/g++.dg/other/final4.C (nonexistent) +++ testsuite/g++.dg/other/final4.C (working copy) @@ -0,0 +1,16 @@ +// PR c++/67184 +// { dg-do compile { target c++11 } } +// { dg-options "-fdump-tree-original" } + +struct B +{ + virtual void operator()(); + virtual operator int(); + virtual int operator++(); +}; + +struct D final : B { }; + +void foo(D& d) { d(); int t = d; ++d; } + +// { dg-final { scan-tree-dump-times "OBJ_TYPE_REF" 0 "original" } } Index: testsuite/g++.dg/other/final5.C =================================================================== --- testsuite/g++.dg/other/final5.C (nonexistent) +++ testsuite/g++.dg/other/final5.C (working copy) @@ -0,0 +1,19 @@ +// PR c++/69445 +// { dg-do compile { target c++11 } } +// { dg-options "-fdump-tree-original" } + +struct Base { + virtual void foo() const = 0; + virtual void bar() const {} +}; + +struct C final : Base { + void foo() const { } +}; + +void func(const C & c) { + c.bar(); + c.foo(); +} + +// { dg-final { scan-tree-dump-times "OBJ_TYPE_REF" 0 "original" } }