From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0b-0016f401.pphosted.com (mx0a-0016f401.pphosted.com [67.231.148.174]) by sourceware.org (Postfix) with ESMTPS id 853C73858D28 for ; Tue, 1 Aug 2023 00:05:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 853C73858D28 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=marvell.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=marvell.com Received: from pps.filterd (m0045849.ppops.net [127.0.0.1]) by mx0a-0016f401.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 36VFthYF021997 for ; Mon, 31 Jul 2023 17:05:22 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=marvell.com; h=from : to : cc : subject : date : message-id : mime-version : content-transfer-encoding : content-type; s=pfpt0220; bh=CKZUjK20ep9qxbsudAeX7+BF/oXVUowxiZy+5fnrLhY=; b=GCIuutmm4iXeK6N01iJIIXffTG3NVnUW0gw4PbtpEo5LM9oXZzo3nN5iSl9XrEg2qA7w RneD+NjQqZ8ESyBxAocTyWWz8OAVhIDdx60qH4aF5zmvLUiIZoG4nMCNP/CcUNxEWPbe VXuDu9YzpzNyW/npESKDcTsBv5ScEFC8lsao+1qh2AhwaTJ/GVG/CsaLjWhkcX8q1O7z F6LDL8fJ8GuohMQVmGgng7dlpNj+riumj45lC99gBNdxFL6o7BMKiEFSc5UtSNh+UHXj tVpEdug23deKyG5mk302h/+S+e8sPy2gs6fjxpaBTf0VV/HRLmeryyfRdxNhQQFTD12a WA== Received: from dc5-exch02.marvell.com ([199.233.59.182]) by mx0a-0016f401.pphosted.com (PPS) with ESMTPS id 3s6du0a4cw-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Mon, 31 Jul 2023 17:05:21 -0700 Received: from DC5-EXCH02.marvell.com (10.69.176.39) by DC5-EXCH02.marvell.com (10.69.176.39) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Mon, 31 Jul 2023 17:05:20 -0700 Received: from maili.marvell.com (10.69.176.80) by DC5-EXCH02.marvell.com (10.69.176.39) with Microsoft SMTP Server id 15.0.1497.48 via Frontend Transport; Mon, 31 Jul 2023 17:05:20 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id 6E6035B6928; Mon, 31 Jul 2023 17:05:19 -0700 (PDT) From: Andrew Pinski To: CC: Andrew Pinski Subject: [COMMITTEDv2] Fix PR 93044: extra cast is not removed Date: Mon, 31 Jul 2023 17:05:07 -0700 Message-ID: <20230801000507.2147978-1-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Proofpoint-GUID: Ycc4vjik3k_Rs0aTYp0_jNH6rMwp8Kv6 X-Proofpoint-ORIG-GUID: Ycc4vjik3k_Rs0aTYp0_jNH6rMwp8Kv6 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.957,Hydra:6.0.591,FMLib:17.11.176.26 definitions=2023-07-31_16,2023-07-31_02,2023-05-22_02 X-Spam-Status: No, score=-14.6 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_LOW,SPF_HELO_NONE,SPF_PASS,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 List-Id: In this case we are not removing convert to a bigger size back to the same size (or smaller) if signedness does not match. For an example: ``` signed char _1; ... _1 = *a_4(D); b_5 = (short unsigned int) _1; _2 = (unsigned char) b_5; ``` The inner cast is not needed and can be removed but was not. The match pattern for removing the extra cast is overly complex so decided to add a new case for rather than trying to modify the current if statement here. Committed as approved. Bootstrapped and tested on x86_64-linux-gnu with no regressions. gcc/ChangeLog: * match.pd (nested int casts): A truncation (to the same size or smaller) can always remove the inner cast. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/cast-1.c: New test. * gcc.dg/tree-ssa/cast-2.c: New test. --- gcc/match.pd | 10 ++++++++++ gcc/testsuite/gcc.dg/tree-ssa/cast-1.c | 12 ++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/cast-2.c | 12 ++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/cast-1.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/cast-2.c diff --git a/gcc/match.pd b/gcc/match.pd index 74f0a84f31d..cfd6ea08807 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -4340,6 +4340,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) && ! (final_ptr && inside_prec != inter_prec)) (ocvt @0)) + /* `(outer:M)(inter:N) a:O` + can be converted to `(outer:M) a` + if M <= O && N >= O. No matter what signedness of the casts, + as the final is either a truncation from the original or just + a sign change of the type. */ + (if (inside_int && inter_int && final_int + && final_prec <= inside_prec + && inter_prec >= inside_prec) + (convert @0)) + /* A truncation to an unsigned type (a zero-extension) should be canonicalized as bitwise and of a mask. */ (if (GIMPLE /* PR70366: doing this in GENERIC breaks -Wconversion. */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/cast-1.c b/gcc/testsuite/gcc.dg/tree-ssa/cast-1.c new file mode 100644 index 00000000000..0f33ab58b3e --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/cast-1.c @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-optimized" } */ + + +void f(signed char *a, unsigned char *c) +{ + unsigned short b = *a; + *c = ((unsigned char)b); +} + + +/* { dg-final { scan-tree-dump-not "\\(short unsigned int\\)" "optimized"} } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/cast-2.c b/gcc/testsuite/gcc.dg/tree-ssa/cast-2.c new file mode 100644 index 00000000000..d665e924831 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/cast-2.c @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-optimized" } */ + + +void f(signed short *a, unsigned char *c) +{ + unsigned long b = *a; + *c = ((unsigned char)b); +} + + +/* { dg-final { scan-tree-dump-not "\\(long unsigned int\\)" "optimized"} } */ -- 2.31.1