From patchwork Wed Aug 2 13:49:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Schulze Frielinghaus X-Patchwork-Id: 1815996 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=WyGJauDo; dkim-atps=neutral Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4RGDCf3gNKz1yYC for ; Thu, 3 Aug 2023 00:01:02 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7E1B93858289 for ; Wed, 2 Aug 2023 14:01:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7E1B93858289 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1690984860; bh=u4xaUWdZy458gACNulMxVEIhrNgX1QOynVIeDvSmF58=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=WyGJauDoBG4gNoyExywkZyHpK5bNdJMkJuMYmCLKHtJ7KgR/GhVmj+hqRIainrzUq m1xWZRgWxNXtB8jBQXe1iavFvtI3qT0cVa3X46ZSd6Jw2neL2g7E2aCicQz3j638Cd ndJV4dc95luvGWxXsrBP1XvtTLdkf6RTSEd97vs8= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) by sourceware.org (Postfix) with ESMTPS id 089703858D1E for ; Wed, 2 Aug 2023 14:00:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 089703858D1E Received: from pps.filterd (m0353726.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 372Dnw1M011605 for ; Wed, 2 Aug 2023 14:00:39 GMT Received: from ppma22.wdc07v.mail.ibm.com (5c.69.3da9.ip4.static.sl-reverse.com [169.61.105.92]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3s7rc78a96-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Wed, 02 Aug 2023 14:00:38 +0000 Received: from pps.filterd (ppma22.wdc07v.mail.ibm.com [127.0.0.1]) by ppma22.wdc07v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 372DT8dw017079 for ; Wed, 2 Aug 2023 13:54:33 GMT Received: from smtprelay06.fra02v.mail.ibm.com ([9.218.2.230]) by ppma22.wdc07v.mail.ibm.com (PPS) with ESMTPS id 3s5dfyd767-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Wed, 02 Aug 2023 13:54:33 +0000 Received: from smtpav06.fra02v.mail.ibm.com (smtpav06.fra02v.mail.ibm.com [10.20.54.105]) by smtprelay06.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 372DsUKT40370616 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 2 Aug 2023 13:54:30 GMT Received: from smtpav06.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 7D65620049; Wed, 2 Aug 2023 13:54:30 +0000 (GMT) Received: from smtpav06.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 55BCC20040; Wed, 2 Aug 2023 13:54:30 +0000 (GMT) Received: from a35lp69.lnxne.boe (unknown [9.152.108.100]) by smtpav06.fra02v.mail.ibm.com (Postfix) with ESMTPS; Wed, 2 Aug 2023 13:54:30 +0000 (GMT) To: gcc-patches@gcc.gnu.org Cc: Stefan Schulze Frielinghaus Subject: [PATCH] PR combine/110867 Fix narrow comparison of memory and constant Date: Wed, 2 Aug 2023 15:49:11 +0200 Message-ID: <20230802134910.2564339-2-stefansf@linux.ibm.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: d6O9B0o_5bx6LSyhFK7B4h91VXwlZJm3 X-Proofpoint-GUID: d6O9B0o_5bx6LSyhFK7B4h91VXwlZJm3 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-08-02_09,2023-08-01_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 adultscore=0 mlxlogscore=999 phishscore=0 suspectscore=0 spamscore=0 lowpriorityscore=0 mlxscore=0 bulkscore=0 impostorscore=0 clxscore=1015 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2306200000 definitions=main-2308020121 X-Spam-Status: No, score=-8.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, 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 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: Stefan Schulze Frielinghaus via Gcc-patches From: Stefan Schulze Frielinghaus Reply-To: Stefan Schulze Frielinghaus Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" In certain cases a constant may not fit into the mode used to perform a comparison. This may be the case for sign-extended constants which are used during an unsigned comparison as e.g. in (set (reg:CC 100 cc) (compare:CC (mem:SI (reg/v/f:SI 115 [ a ]) [1 *a_4(D)+0 S4 A64]) (const_int -2147483648 [0xffffffff80000000]))) Fixed by ensuring that the constant fits into comparison mode. Furthermore, on some targets as e.g. sparc the constant used in a comparison is chopped off before combine which leads to failing test cases (see PR 110869). Fixed by not requiring that the source mode has to be DImode, and excluding sparc from the last two test cases entirely since there the constant cannot be further reduced. According to PR 110867 and 110869 this patch resolves bootstrap problems on armv8l and sparc. While writing this, bootstrap+regtest are still running on x64 and s390x. Assuming they pass, ok for mainline? gcc/ChangeLog: PR combine/110867 * combine.cc (simplify_compare_const): Try the optimization only in case the constant fits into the comparison mode. gcc/testsuite/ChangeLog: PR combine/110869 * gcc.dg/cmp-mem-const-1.c: Relax mode for constant. * gcc.dg/cmp-mem-const-2.c: Relax mode for constant. * gcc.dg/cmp-mem-const-3.c: Relax mode for constant. * gcc.dg/cmp-mem-const-4.c: Relax mode for constant. * gcc.dg/cmp-mem-const-5.c: Exclude sparc since here the constant is already reduced. * gcc.dg/cmp-mem-const-6.c: Exclude sparc since here the constant is already reduced. --- gcc/combine.cc | 4 ++++ gcc/testsuite/gcc.dg/cmp-mem-const-1.c | 2 +- gcc/testsuite/gcc.dg/cmp-mem-const-2.c | 2 +- gcc/testsuite/gcc.dg/cmp-mem-const-3.c | 2 +- gcc/testsuite/gcc.dg/cmp-mem-const-4.c | 2 +- gcc/testsuite/gcc.dg/cmp-mem-const-5.c | 4 ++-- gcc/testsuite/gcc.dg/cmp-mem-const-6.c | 4 ++-- 7 files changed, 12 insertions(+), 8 deletions(-) diff --git a/gcc/combine.cc b/gcc/combine.cc index 0d99fa541c5..e46d202d0a7 100644 --- a/gcc/combine.cc +++ b/gcc/combine.cc @@ -11998,11 +11998,15 @@ simplify_compare_const (enum rtx_code code, machine_mode mode, x0 >= 0x40. */ if ((code == LEU || code == LTU || code == GEU || code == GTU) && is_a (GET_MODE (op0), &int_mode) + && HWI_COMPUTABLE_MODE_P (int_mode) && MEM_P (op0) && !MEM_VOLATILE_P (op0) /* The optimization makes only sense for constants which are big enough so that we have a chance to chop off something at all. */ && (unsigned HOST_WIDE_INT) const_op > 0xff + /* Bail out, if the constant does not fit into INT_MODE. */ + && (unsigned HOST_WIDE_INT) const_op + < ((HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1) << 1) - 1) /* Ensure that we do not overflow during normalization. */ && (code != GTU || (unsigned HOST_WIDE_INT) const_op < HOST_WIDE_INT_M1U)) { diff --git a/gcc/testsuite/gcc.dg/cmp-mem-const-1.c b/gcc/testsuite/gcc.dg/cmp-mem-const-1.c index 263ad98af79..4f21a1ade4a 100644 --- a/gcc/testsuite/gcc.dg/cmp-mem-const-1.c +++ b/gcc/testsuite/gcc.dg/cmp-mem-const-1.c @@ -1,6 +1,6 @@ /* { dg-do compile { target { lp64 } } } */ /* { dg-options "-O1 -fdump-rtl-combine-details" } */ -/* { dg-final { scan-rtl-dump "narrow comparison from mode DI to QI" "combine" } } */ +/* { dg-final { scan-rtl-dump "narrow comparison from mode .I to QI" "combine" } } */ typedef __UINT64_TYPE__ uint64_t; diff --git a/gcc/testsuite/gcc.dg/cmp-mem-const-2.c b/gcc/testsuite/gcc.dg/cmp-mem-const-2.c index a7cc5348295..7b722951594 100644 --- a/gcc/testsuite/gcc.dg/cmp-mem-const-2.c +++ b/gcc/testsuite/gcc.dg/cmp-mem-const-2.c @@ -1,6 +1,6 @@ /* { dg-do compile { target { lp64 } } } */ /* { dg-options "-O1 -fdump-rtl-combine-details" } */ -/* { dg-final { scan-rtl-dump "narrow comparison from mode DI to QI" "combine" } } */ +/* { dg-final { scan-rtl-dump "narrow comparison from mode .I to QI" "combine" } } */ typedef __UINT64_TYPE__ uint64_t; diff --git a/gcc/testsuite/gcc.dg/cmp-mem-const-3.c b/gcc/testsuite/gcc.dg/cmp-mem-const-3.c index 06f80bf72d8..ed5059d3807 100644 --- a/gcc/testsuite/gcc.dg/cmp-mem-const-3.c +++ b/gcc/testsuite/gcc.dg/cmp-mem-const-3.c @@ -1,6 +1,6 @@ /* { dg-do compile { target { lp64 } } } */ /* { dg-options "-O1 -fdump-rtl-combine-details" } */ -/* { dg-final { scan-rtl-dump "narrow comparison from mode DI to HI" "combine" } } */ +/* { dg-final { scan-rtl-dump "narrow comparison from mode .I to HI" "combine" } } */ typedef __UINT64_TYPE__ uint64_t; diff --git a/gcc/testsuite/gcc.dg/cmp-mem-const-4.c b/gcc/testsuite/gcc.dg/cmp-mem-const-4.c index 407999abf7e..23e83372bee 100644 --- a/gcc/testsuite/gcc.dg/cmp-mem-const-4.c +++ b/gcc/testsuite/gcc.dg/cmp-mem-const-4.c @@ -1,6 +1,6 @@ /* { dg-do compile { target { lp64 } } } */ /* { dg-options "-O1 -fdump-rtl-combine-details" } */ -/* { dg-final { scan-rtl-dump "narrow comparison from mode DI to HI" "combine" } } */ +/* { dg-final { scan-rtl-dump "narrow comparison from mode .I to HI" "combine" } } */ typedef __UINT64_TYPE__ uint64_t; diff --git a/gcc/testsuite/gcc.dg/cmp-mem-const-5.c b/gcc/testsuite/gcc.dg/cmp-mem-const-5.c index e16773f5bcf..d266896a25e 100644 --- a/gcc/testsuite/gcc.dg/cmp-mem-const-5.c +++ b/gcc/testsuite/gcc.dg/cmp-mem-const-5.c @@ -1,6 +1,6 @@ -/* { dg-do compile { target { lp64 } } } */ +/* { dg-do compile { target { lp64 } && ! target { sparc*-*-* } } } */ /* { dg-options "-O1 -fdump-rtl-combine-details" } */ -/* { dg-final { scan-rtl-dump "narrow comparison from mode DI to SI" "combine" } } */ +/* { dg-final { scan-rtl-dump "narrow comparison from mode .I to SI" "combine" } } */ typedef __UINT64_TYPE__ uint64_t; diff --git a/gcc/testsuite/gcc.dg/cmp-mem-const-6.c b/gcc/testsuite/gcc.dg/cmp-mem-const-6.c index 8f53b5678bd..68d7a9d0265 100644 --- a/gcc/testsuite/gcc.dg/cmp-mem-const-6.c +++ b/gcc/testsuite/gcc.dg/cmp-mem-const-6.c @@ -1,6 +1,6 @@ -/* { dg-do compile { target { lp64 } } } */ +/* { dg-do compile { target { lp64 } && ! target { sparc*-*-* } } } */ /* { dg-options "-O1 -fdump-rtl-combine-details" } */ -/* { dg-final { scan-rtl-dump "narrow comparison from mode DI to SI" "combine" } } */ +/* { dg-final { scan-rtl-dump "narrow comparison from mode .I to SI" "combine" } } */ typedef __UINT64_TYPE__ uint64_t;