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: 129850 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f41:0:b0:3e4:2afc:c1 with SMTP id v1csp477014vqx; Wed, 2 Aug 2023 07:01:24 -0700 (PDT) X-Google-Smtp-Source: APBJJlGv7mfLjiyDDQlWeIl7yLSk+S+Lp7865Lczl3pF6wCgLdi5yh8eD0vY1AQFOI8h0E6iAs+T X-Received: by 2002:a17:906:7495:b0:965:6075:d100 with SMTP id e21-20020a170906749500b009656075d100mr4836154ejl.39.1690984884575; Wed, 02 Aug 2023 07:01:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690984884; cv=none; d=google.com; s=arc-20160816; b=euOrpgAST2NhmqLlYZuHRKeOg0DPum3CLDTr+aLFvhlblJv5uKFUwO6+3hwZqL6v1L sFki69SfSihaB3w0h6PV9TodBuWF6lMJOZLMQx09P5eI8gKgl02On83pjUkokC6ubhI5 Wcz+aDQMIcedcYGaMtZ5ulsaFsuBHRBBacChGWUXfXpoS7AAlOEwntu+Mg0PMFwzsk/0 +RFSy7oIYeMX7nrWKjU+paol02rU9L+kdXpF6L0nifizAc2jtxslDRZiHg9tNLH5YuKK byE9mgwGdH3dhfol+EhJTqUjQdKB0zKUj0+C34AFr3UJLllmcHabZol4cq3dTA+M/juK eEmQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=u4xaUWdZy458gACNulMxVEIhrNgX1QOynVIeDvSmF58=; fh=HMaSBIaXHQWtzxUV9NgXQx0dxb10mZXjfEfy0AoTSJY=; b=WGBWKlosbR/4TGjsCjcrj4nML9aYzCw/tnKVNU0uiDqGtWNUIhV7FJYp7iv59kQ3vN OM05XvjzBTv7jHHsQl978f8dFkdTLo34vmli5OML4K/Ypyfsv1GdrUjWDu3WBE3mURBs XL3HiZAZdWMuD3ixKo3HiOyT8PlWFeTW/tHoF/8VVBHrmrVnMArfy+bcljWnCyzYkn1Z XlV8FAUyGTRnX1/UBmDyX99Tk9qzTyhM0h7YBx+sLbAPf8ErvibiAS05027TFyaxMUWu FyY+gZF+c1chxNofPuRpBQLfy1VY1iQFmW2rd0Utj37iDmhdqIqPi27by/r35oZtA+pS 1U3w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Kf088fYp; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id gl23-20020a170906e0d700b0099bd1a1d025si7031953ejb.199.2023.08.02.07.01.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Aug 2023 07:01:24 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Kf088fYp; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3E4383858C2B for ; Wed, 2 Aug 2023 14:01:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3E4383858C2B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1690984883; bh=u4xaUWdZy458gACNulMxVEIhrNgX1QOynVIeDvSmF58=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=Kf088fYpqOsRjBIB37uigYYr3oGJoDFLtPRZTxvm1pe2NArZUhHtn8bHRnhp1X60M jBQrXs3MPLS8RsN0uGEzuPchJL3ZXYG7OwQXmL7V8lIwoYfLTgd2T2XrsJ3CngN7l9 OMjsQeuGDjONGDoi1WY/rUBJRbQJWBIiVlILBUVw= 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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773126166398267555 X-GMAIL-MSGID: 1773126166398267555 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;