From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0b-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) by sourceware.org (Postfix) with ESMTPS id 81BB83858D32 for ; Thu, 10 Aug 2023 14:56:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 81BB83858D32 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=linux.ibm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linux.ibm.com Received: from pps.filterd (m0353722.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 37AEnUnQ002187 for ; Thu, 10 Aug 2023 14:56:12 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : mime-version : content-transfer-encoding; s=pp1; bh=TmITtGxaAcbkqROtUJY4tXbGYnBCxDroOR/SLmyfFpk=; b=jxNV1/RiBEpo1mcV2LqRoL5S3jYzDRLxm6BqC9gIZC0wJWFPtqxh7y5H9hoHWioXMT9y Pc1tFvQbpet1ZdNlgeRL8EoRzXETnqXMQQpAzIWO4/wfvoascpOTt54TKmPg8hye1cAU 7yxgRksAu85r2shpaMf/lBG5HvN5Ilv65k1I8V6JCky7YDx7+wATEaVTQ7ITpl2UdfIb Ev4+gtkLo5W7zVAZ88Dc3G0oD1JhKGMv3F5fO3+xBqJKN+rjEwAnw3stsBXjDEqWlPKG NdUYjfikOScbxaWv+zBbT/1sSfutknps7dDmzKV4CtaNXR01O2S4mB2r+SGr1KChiCFo QQ== Received: from ppma12.dal12v.mail.ibm.com (dc.9e.1632.ip4.static.sl-reverse.com [50.22.158.220]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3sd20dr59j-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Thu, 10 Aug 2023 14:56:11 +0000 Received: from pps.filterd (ppma12.dal12v.mail.ibm.com [127.0.0.1]) by ppma12.dal12v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 37ADKooV006656 for ; Thu, 10 Aug 2023 14:56:10 GMT Received: from smtprelay01.fra02v.mail.ibm.com ([9.218.2.227]) by ppma12.dal12v.mail.ibm.com (PPS) with ESMTPS id 3sa0rtjv92-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Thu, 10 Aug 2023 14:56:10 +0000 Received: from smtpav01.fra02v.mail.ibm.com (smtpav01.fra02v.mail.ibm.com [10.20.54.100]) by smtprelay01.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 37AEu7cM11862634 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 10 Aug 2023 14:56:07 GMT Received: from smtpav01.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 85B2A20043; Thu, 10 Aug 2023 14:56:07 +0000 (GMT) Received: from smtpav01.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 6E1F220040; Thu, 10 Aug 2023 14:56:07 +0000 (GMT) Received: from a8345010.lnxne.boe (unknown [9.152.108.100]) by smtpav01.fra02v.mail.ibm.com (Postfix) with ESMTPS; Thu, 10 Aug 2023 14:56:07 +0000 (GMT) From: Stefan Schulze Frielinghaus To: gcc-patches@gcc.gnu.org Cc: Stefan Schulze Frielinghaus Subject: [PATCH] rtl-optimization/110939 Really fix narrow comparison of memory and constant Date: Thu, 10 Aug 2023 15:04:03 +0200 Message-ID: <20230810130402.752335-2-stefansf@linux.ibm.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-TM-AS-GCONF: 00 X-Proofpoint-GUID: 1q0yt-3MTWKPD1nx4L_CXtsiOT2vXuQA X-Proofpoint-ORIG-GUID: 1q0yt-3MTWKPD1nx4L_CXtsiOT2vXuQA X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.957,Hydra:6.0.591,FMLib:17.11.176.26 definitions=2023-08-10_11,2023-08-10_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 malwarescore=0 clxscore=1015 priorityscore=1501 lowpriorityscore=0 mlxlogscore=607 spamscore=0 mlxscore=0 impostorscore=0 phishscore=0 suspectscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2306200000 definitions=main-2308100124 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 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 the former fix in commit 41ef5a34161356817807be3a2e51fbdbe575ae85 I completely missed the fact that the normal form of a generated constant for a mode with fewer bits than in HOST_WIDE_INT is a sign extended version of the actual constant. This even holds true for unsigned constants. Fixed by masking out the upper bits for the incoming constant and sign extending the resulting unsigned constant. Bootstrapped and regtested on x64 and s390x. Ok for mainline? While reading existing optimizations in combine I stumbled across two optimizations where either my intuition about the representation of unsigned integers via a const_int rtx is wrong, which then in turn would probably also mean that this patch is wrong, or that the optimizations are missed sometimes. In other words in the following I would assume that the upper bits are masked out: diff --git a/gcc/combine.cc b/gcc/combine.cc index 468b7fde911..80c4ff0fbaf 100644 --- a/gcc/combine.cc +++ b/gcc/combine.cc @@ -11923,7 +11923,7 @@ simplify_compare_const (enum rtx_code code, machine_mode mode, /* (unsigned) < 0x80000000 is equivalent to >= 0. */ else if (is_a (mode, &int_mode) && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT - && ((unsigned HOST_WIDE_INT) const_op + && (((unsigned HOST_WIDE_INT) const_op & GET_MODE_MASK (int_mode)) == HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1))) { const_op = 0; @@ -11962,7 +11962,7 @@ simplify_compare_const (enum rtx_code code, machine_mode mode, /* (unsigned) >= 0x80000000 is equivalent to < 0. */ else if (is_a (mode, &int_mode) && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT - && ((unsigned HOST_WIDE_INT) const_op + && (((unsigned HOST_WIDE_INT) const_op & GET_MODE_MASK (int_mode)) == HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1))) { const_op = 0; For example, while bootstrapping on x64 the optimization is missed since a LTU comparison in QImode is done and the constant equals 0xffffffffffffff80. Sorry for inlining another patch, but I would really like to make sure that my understanding is correct, now, before I come up with another patch. Thus it would be great if someone could shed some light on this. gcc/ChangeLog: * combine.cc (simplify_compare_const): Properly handle unsigned constants while narrowing comparison of memory and constants. --- gcc/combine.cc | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/gcc/combine.cc b/gcc/combine.cc index e46d202d0a7..468b7fde911 100644 --- a/gcc/combine.cc +++ b/gcc/combine.cc @@ -12003,14 +12003,15 @@ simplify_compare_const (enum rtx_code code, machine_mode mode, && !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) + && ((unsigned HOST_WIDE_INT) const_op & GET_MODE_MASK (int_mode)) > 0xff /* Ensure that we do not overflow during normalization. */ - && (code != GTU || (unsigned HOST_WIDE_INT) const_op < HOST_WIDE_INT_M1U)) + && (code != GTU + || ((unsigned HOST_WIDE_INT) const_op & GET_MODE_MASK (int_mode)) + < HOST_WIDE_INT_M1U) + && trunc_int_for_mode (const_op, int_mode) == const_op) { - unsigned HOST_WIDE_INT n = (unsigned HOST_WIDE_INT) const_op; + unsigned HOST_WIDE_INT n + = (unsigned HOST_WIDE_INT) const_op & GET_MODE_MASK (int_mode); enum rtx_code adjusted_code; /* Normalize code to either LEU or GEU. */ @@ -12051,15 +12052,15 @@ simplify_compare_const (enum rtx_code code, machine_mode mode, HOST_WIDE_INT_PRINT_HEX ") to (MEM %s " HOST_WIDE_INT_PRINT_HEX ").\n", GET_MODE_NAME (int_mode), GET_MODE_NAME (narrow_mode_iter), GET_RTX_NAME (code), - (unsigned HOST_WIDE_INT)const_op, GET_RTX_NAME (adjusted_code), - n); + (unsigned HOST_WIDE_INT) const_op & GET_MODE_MASK (int_mode), + GET_RTX_NAME (adjusted_code), n); } poly_int64 offset = (BYTES_BIG_ENDIAN ? 0 : (GET_MODE_SIZE (int_mode) - GET_MODE_SIZE (narrow_mode_iter))); *pop0 = adjust_address_nv (op0, narrow_mode_iter, offset); - *pop1 = GEN_INT (n); + *pop1 = gen_int_mode (n, narrow_mode_iter); return adjusted_code; } } -- 2.41.0