From patchwork Thu May 2 21:56:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 1930890 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=quicinc.com header.i=@quicinc.com header.a=rsa-sha256 header.s=qcppdkim1 header.b=RE0oqRCp; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VVnnw6dlqz1ymc for ; Fri, 3 May 2024 07:56:36 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3F2FE3849AEF for ; Thu, 2 May 2024 21:56:35 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) by sourceware.org (Postfix) with ESMTPS id 59DBE385E836 for ; Thu, 2 May 2024 21:56:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 59DBE385E836 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=quicinc.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=quicinc.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 59DBE385E836 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=205.220.168.131 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714686976; cv=none; b=IBRfo6+VWLoZN4AUBNgJmJcFCkGmARAL8fdylvr372GZAcdR0eoLQGBlnxgtv4ucx7iL/Z7dXDLsrPiuju0pMcq6LLkJy+gNa6u7MunBz85cQkX/QMaM5EiBMGWtd3I5VXERejVhhr4svdUdHlnLJ7dqFNoVDdIGWXFYajZAkiQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714686976; c=relaxed/simple; bh=gqTPZeauR02D+sGKZGjCtSSuGi/uLJF9b8A06okgjRI=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=qD0iSANJmIPWdgCzojhKAIpxuO8nD/oO36s77SIa/65TWFHDwujuiezFiBsAhMcirFQCalS0mT9iPFi14uI6QDOeX46veLPThqsIPk9R8bsd74ieGsxmCrFylMno87YxEhrFfPGwQMOQM2d39t5jai5pp1c/iHasWrctkNo3uXQ= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0279867.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 442LP1Dp017161 for ; Thu, 2 May 2024 21:56:12 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h= from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding:content-type; s=qcppdkim1; bh=J516eqX eWe8fei32x4Qlf1Rwkh1MP162VTOSrwPD/bk=; b=RE0oqRCpD0SXvX0u2EYqibr rVlElD3H39mvofsAKgktnx1xrAGUcRyPv1A+stoPJE5ssOciPxh7WkOPfdZS+8Kv QqNQ3vGeH0dGNNmp4WgTjAwjlROrd8bugeYHKNPZZrNf4u4AQ0D33PlFUa+rAj+I PXBZEVEARBuluhHsxdpue/S6LLdtinTzV5QW1bksFoPUIVoOpV7fYy5s2HDVJDcy hDnp0VyrWDmLjooH3gT2M9d38/MaXBWzLydlSrBjAKdj/nMjrDLdYeBRx/hXvgMx IL0SlNpjGQ2YJGV5mjGAkyhzf+U3tP9GaGR+v9s3GLrb4hub+IpSAtepQE2PG9g= = Received: from nasanppmta05.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3xv7pxhm0t-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Thu, 02 May 2024 21:56:12 +0000 (GMT) Received: from nasanex01c.na.qualcomm.com (nasanex01c.na.qualcomm.com [10.45.79.139]) by NASANPPMTA05.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 442LuBuM023665 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Thu, 2 May 2024 21:56:11 GMT Received: from hu-apinski-lv.qualcomm.com (10.49.16.6) by nasanex01c.na.qualcomm.com (10.45.79.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.9; Thu, 2 May 2024 14:56:11 -0700 From: Andrew Pinski To: CC: Andrew Pinski Subject: [PATCH] DCE __cxa_atexit calls where the function is pure/const [PR19661] Date: Thu, 2 May 2024 14:56:00 -0700 Message-ID: <20240502215600.2043902-1-quic_apinski@quicinc.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Originating-IP: [10.49.16.6] X-ClientProxiedBy: nalasex01b.na.qualcomm.com (10.47.209.197) To nasanex01c.na.qualcomm.com (10.45.79.139) X-QCInternal: smtphost X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=5800 signatures=585085 X-Proofpoint-GUID: LRzR-smSB0cjmHB_cZ82jOISpelJWAmk X-Proofpoint-ORIG-GUID: LRzR-smSB0cjmHB_cZ82jOISpelJWAmk X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1011,Hydra:6.0.650,FMLib:17.11.176.26 definitions=2024-05-02_14,2024-05-02_03,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1015 adultscore=0 suspectscore=0 mlxlogscore=999 phishscore=0 lowpriorityscore=0 mlxscore=0 bulkscore=0 spamscore=0 malwarescore=0 impostorscore=0 priorityscore=1501 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2404010003 definitions=main-2405020147 X-Spam-Status: No, score=-13.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org In C++ sometimes you have a deconstructor function which is "empty", like for an example with unions or with arrays. The front-end might not know it is empty either so this should be done on during optimization.o To implement it I added it to DCE where we mark if a statement is necessary or not. Bootstrapped and tested on x86_64-linux-gnu with no regressions. PR tree-optimization/19661 gcc/ChangeLog: * tree-ssa-dce.cc (is_cxa_atexit): New function. (is_removable_cxa_atexit_call): New function. (mark_stmt_if_obviously_necessary): Don't mark removable cxa_at_exit calls. (mark_all_reaching_defs_necessary_1): Likewise. (propagate_necessity): Likewise. gcc/testsuite/ChangeLog: * g++.dg/tree-ssa/cxa_atexit-1.C: New test. * g++.dg/tree-ssa/cxa_atexit-2.C: New test. * g++.dg/tree-ssa/cxa_atexit-3.C: New test. * g++.dg/tree-ssa/cxa_atexit-4.C: New test. * g++.dg/tree-ssa/cxa_atexit-5.C: New test. Signed-off-by: Andrew Pinski --- gcc/testsuite/g++.dg/tree-ssa/cxa_atexit-1.C | 20 +++++++++ gcc/testsuite/g++.dg/tree-ssa/cxa_atexit-2.C | 21 ++++++++++ gcc/testsuite/g++.dg/tree-ssa/cxa_atexit-3.C | 19 +++++++++ gcc/testsuite/g++.dg/tree-ssa/cxa_atexit-4.C | 20 +++++++++ gcc/testsuite/g++.dg/tree-ssa/cxa_atexit-5.C | 39 +++++++++++++++++ gcc/tree-ssa-dce.cc | 44 ++++++++++++++++++++ 6 files changed, 163 insertions(+) create mode 100644 gcc/testsuite/g++.dg/tree-ssa/cxa_atexit-1.C create mode 100644 gcc/testsuite/g++.dg/tree-ssa/cxa_atexit-2.C create mode 100644 gcc/testsuite/g++.dg/tree-ssa/cxa_atexit-3.C create mode 100644 gcc/testsuite/g++.dg/tree-ssa/cxa_atexit-4.C create mode 100644 gcc/testsuite/g++.dg/tree-ssa/cxa_atexit-5.C diff --git a/gcc/testsuite/g++.dg/tree-ssa/cxa_atexit-1.C b/gcc/testsuite/g++.dg/tree-ssa/cxa_atexit-1.C new file mode 100644 index 00000000000..1f5f431c7e4 --- /dev/null +++ b/gcc/testsuite/g++.dg/tree-ssa/cxa_atexit-1.C @@ -0,0 +1,20 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-cddce1-details -fdump-tree-optimized" } */ +// { dg-require-effective-target cxa_atexit } +/* PR tree-optimization/19661 */ + +/* The call to axexit should be removed as A::~A() is a pure/const function call + and there is no visible effect if A::~A() call does not happen. */ + +struct A { + A(); + ~A() {} +}; + +void foo () { + static A a; +} + +/* { dg-final { scan-tree-dump-times "Deleting : __cxxabiv1::__cxa_atexit" 1 "cddce1" } } */ +/* { dg-final { scan-tree-dump-not "__cxa_atexit" "optimized" } } */ + diff --git a/gcc/testsuite/g++.dg/tree-ssa/cxa_atexit-2.C b/gcc/testsuite/g++.dg/tree-ssa/cxa_atexit-2.C new file mode 100644 index 00000000000..4d0656b455c --- /dev/null +++ b/gcc/testsuite/g++.dg/tree-ssa/cxa_atexit-2.C @@ -0,0 +1,21 @@ +/* { dg-do compile { target c++11 } } */ +/* { dg-options "-O2 -fdump-tree-cddce1-details -fdump-tree-optimized" } */ +// { dg-require-effective-target cxa_atexit } +/* PR tree-optimization/19661 */ + +/* The call to axexit should be not removed as A::~A() as it marked with noipa. */ + +struct A { + A(); + ~A(); +}; + +[[gnu::noipa]] A::~A() {} + +void foo () { + static A a; +} + +/* { dg-final { scan-tree-dump-not "Deleting : __cxxabiv1::__cxa_atexit" "cddce1" } } */ +/* { dg-final { scan-tree-dump-times "__cxa_atexit" 1 "optimized" } } */ + diff --git a/gcc/testsuite/g++.dg/tree-ssa/cxa_atexit-3.C b/gcc/testsuite/g++.dg/tree-ssa/cxa_atexit-3.C new file mode 100644 index 00000000000..03a19209661 --- /dev/null +++ b/gcc/testsuite/g++.dg/tree-ssa/cxa_atexit-3.C @@ -0,0 +1,19 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-cddce1-details -fdump-tree-optimized" } */ +// { dg-require-effective-target cxa_atexit } +/* PR tree-optimization/19661 */ + +/* We should not remove the call to atexit as A::~A is unknown. */ + +struct A { + A(); + ~A(); +}; + +void foo () { + static A a; +} + +/* { dg-final { scan-tree-dump-not "Deleting : __cxxabiv1::__cxa_atexit" "cddce1" } } */ +/* { dg-final { scan-tree-dump-times "__cxa_atexit" 1 "optimized" } } */ + diff --git a/gcc/testsuite/g++.dg/tree-ssa/cxa_atexit-4.C b/gcc/testsuite/g++.dg/tree-ssa/cxa_atexit-4.C new file mode 100644 index 00000000000..b85a7efd16b --- /dev/null +++ b/gcc/testsuite/g++.dg/tree-ssa/cxa_atexit-4.C @@ -0,0 +1,20 @@ +/* { dg-do compile { target c++11 } } */ +/* { dg-options "-O2 -fdump-tree-cddce1-details -fdump-tree-optimized -w" } */ +// { dg-require-effective-target cxa_atexit } +/* PR tree-optimization/19661 */ + +/* The call to axexit should be removed as A::~A() is a pure/const function call + and there is no visible effect if A::~A() call does not happen. */ + +struct A { + A(); + [[gnu::pure]] ~A(); +}; + +void foo () { + static A a; +} + +/* { dg-final { scan-tree-dump-times "Deleting : __cxxabiv1::__cxa_atexit" 1 "cddce1" } } */ +/* { dg-final { scan-tree-dump-not "__cxa_atexit" "optimized" } } */ + diff --git a/gcc/testsuite/g++.dg/tree-ssa/cxa_atexit-5.C b/gcc/testsuite/g++.dg/tree-ssa/cxa_atexit-5.C new file mode 100644 index 00000000000..f96395df913 --- /dev/null +++ b/gcc/testsuite/g++.dg/tree-ssa/cxa_atexit-5.C @@ -0,0 +1,39 @@ +/* { dg-do compile { target c++20 } } */ +/* { dg-options "-O2 -fdump-tree-dce2-details -fdump-tree-optimized" } */ +// { dg-require-effective-target cxa_atexit } +/* PR tree-optimization/19661 */ + +/* The call to axexit should be removed as constant_init::~constant_init is a pure/const function call + and there is no visible effect if constant_init::~constant_init() call does not happen. */ +/* This takes until DCE2 as constant_init::~constant_init is not figured out being pure/const until late. */ + +extern "C" int puts(const char*); + +struct A +{ + constexpr A() { } + ~A() { puts("bye"); } +}; + +namespace +{ + struct constant_init + { + union { + A obj; + }; + constexpr constant_init() : obj() { } + + ~constant_init() { /* do nothing, union member is not destroyed */ } + }; + + + // Single-threaded fallback buffer. + constinit constant_init global; +} + +extern "C" A* get() { return &global.obj; } + +/* { dg-final { scan-tree-dump-times "Deleting : __cxxabiv1::__cxa_atexit" 1 "dce2" } } */ +/* { dg-final { scan-tree-dump-not "__cxa_atexit" "optimized" } } */ + diff --git a/gcc/tree-ssa-dce.cc b/gcc/tree-ssa-dce.cc index 636c471d4c8..762218a04fe 100644 --- a/gcc/tree-ssa-dce.cc +++ b/gcc/tree-ssa-dce.cc @@ -124,6 +124,41 @@ keep_all_vdefs_p () return optimize_debug; } +/* True if CALLEE is the function __cxa_atexit. */ + +static inline bool +is_cxa_atexit (const_tree callee) +{ + if (callee != NULL_TREE + && strcmp (IDENTIFIER_POINTER (DECL_NAME (callee)), "__cxa_atexit") == 0) + return true; + return false; +} + +/* True if STMT is a call to __cxa_atexit and the first argument to that + call is a pure/const non looping function decl. */ + +static inline bool +is_removable_cxa_atexit_call (gimple *stmt) +{ + tree callee = gimple_call_fndecl (stmt); + if (!is_cxa_atexit (callee)) + return false; + if (gimple_call_num_args (stmt) != 3) + return false; + tree arg = gimple_call_arg (stmt, 0); + if (TREE_CODE (arg) != ADDR_EXPR) + return false; + arg = TREE_OPERAND (arg, 0); + if (TREE_CODE (arg) != FUNCTION_DECL) + return false; + int flags = flags_from_decl_or_type (arg); + if (flags & ECF_NORETURN) + return false; + return (flags & (ECF_CONST|ECF_PURE)) + && !(flags & ECF_LOOPING_CONST_OR_PURE); +} + /* If STMT is not already marked necessary, mark it, and add it to the worklist if ADD_TO_WORKLIST is true. */ @@ -251,6 +286,10 @@ mark_stmt_if_obviously_necessary (gimple *stmt, bool aggressive) && DECL_IS_REPLACEABLE_OPERATOR_NEW_P (callee)) return; + /* For __cxa_atexit calls, don't mark as necessary right away. */ + if (is_removable_cxa_atexit_call (stmt)) + return; + /* IFN_GOACC_LOOP calls are necessary in that they are used to represent parameter (i.e. step, bound) of a lowered OpenACC partitioned loop. But this kind of partitioned loop might not @@ -626,6 +665,8 @@ mark_all_reaching_defs_necessary_1 (ao_ref *ref ATTRIBUTE_UNUSED, || DECL_IS_OPERATOR_DELETE_P (callee)) && gimple_call_from_new_or_delete (call)) return false; + if (is_removable_cxa_atexit_call (call)) + return false; } if (! gimple_clobber_p (def_stmt)) @@ -930,6 +971,9 @@ propagate_necessity (bool aggressive) && gimple_call_from_new_or_delete (call)) continue; + if (is_removable_cxa_atexit_call (call)) + continue; + /* Calls implicitly load from memory, their arguments in addition may explicitly perform memory loads. */ mark_all_reaching_defs_necessary (call);