From patchwork Mon Mar 11 05:41:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: HAO CHEN GUI X-Patchwork-Id: 1910323 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=ibm.com header.i=@ibm.com header.a=rsa-sha256 header.s=pp1 header.b=brdiC130; dkim-atps=neutral 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=patchwork.ozlabs.org) 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 (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TtQdG44Cfz1yX8 for ; Mon, 11 Mar 2024 16:41:52 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6B71C3856090 for ; Mon, 11 Mar 2024 05:41:48 +0000 (GMT) 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 84E2F3858422 for ; Mon, 11 Mar 2024 05:41:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 84E2F3858422 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 ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 84E2F3858422 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=148.163.156.1 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710135686; cv=none; b=sMReJUPs8Oq+Pdi7JGNNiivg7JsARyruzRX7jAGq7Uli7OvLJWB8c1KdO/XQzSW8G5y7Lw2EOhLSY2/EYC3TXYPyiEj5820ltTdTA1nmelA94jJuSbNwyOjmDqmCemA0nOR/Z5O7ETv5YGgw7fcZhWuS5VHMdKtjzooKEGmSj4E= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710135686; c=relaxed/simple; bh=OB/ze1W4h8tJWZFa9XZbSk8kKi7BHmIlP/azwkMa/Hc=; h=DKIM-Signature:Message-ID:Date:To:From:Subject:MIME-Version; b=oxW8aEWLj/nObTxrDFccgYMBkoIo5O7SAKYG+PUVHJsr7KEGLL0sbZHLtv/fWJAC2nprsjTiwSlYkjYb3WZeXdQvc4eukKiBbZTdPwmng8EfamctPPvvVixcz2VO8w21xMebhILru4N7XNlZVwUX3R7sMaaGxxZv5mHVj5PCrAk= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0353729.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 42B523fx032163; Mon, 11 Mar 2024 05:41:22 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=message-id : date : to : cc : from : subject : content-type : content-transfer-encoding : mime-version; s=pp1; bh=+aS30Qx+EHnOMGonaMeK2Qj9B3A4oBqi+aHytqNEQnk=; b=brdiC130tV5ipDcRk67zCqoxqMqoGYLBpVwRglu+DMsgFF3Y4bvTB83GqNfl1z7ChGs1 jMpcyHjTjPGbCQnbxDV6aVWizsMwHKTCrGF8yzKGZqx4EN+2+TUz/AKxmZcy+RGvaXGP gl3qfBuvVkGwNE6I5LXx4vZ3kHgvrhSojswQ1Z9ziYRjEnGtSVbuxnMhESbdi0qJeyH4 cG3nXFW1Ebiz/VYcK+YNel1JEVDmhq5p3v6k+4syK+Tjjz0Gb37aXVipDmom//et6bYn i+wpBNprEIPN4LaMdTJ7FJ4NEXRb6PEkMbWWybS9CaptEwrUKPc5CkhJSV4yK5++XEq8 zw== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3wsuexrgmm-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 11 Mar 2024 05:41:21 +0000 Received: from m0353729.ppops.net (m0353729.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 42B5F8Eq029145; Mon, 11 Mar 2024 05:41:21 GMT Received: from ppma21.wdc07v.mail.ibm.com (5b.69.3da9.ip4.static.sl-reverse.com [169.61.105.91]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3wsuexrgma-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 11 Mar 2024 05:41:21 +0000 Received: from pps.filterd (ppma21.wdc07v.mail.ibm.com [127.0.0.1]) by ppma21.wdc07v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 42B3KCbU015028; Mon, 11 Mar 2024 05:41:20 GMT Received: from smtprelay07.fra02v.mail.ibm.com ([9.218.2.229]) by ppma21.wdc07v.mail.ibm.com (PPS) with ESMTPS id 3ws33nemfq-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 11 Mar 2024 05:41:20 +0000 Received: from smtpav03.fra02v.mail.ibm.com (smtpav03.fra02v.mail.ibm.com [10.20.54.102]) by smtprelay07.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 42B5fExD40042950 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 11 Mar 2024 05:41:16 GMT Received: from smtpav03.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 4BD6020043; Mon, 11 Mar 2024 05:41:14 +0000 (GMT) Received: from smtpav03.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 520D920040; Mon, 11 Mar 2024 05:41:12 +0000 (GMT) Received: from [9.200.56.49] (unknown [9.200.56.49]) by smtpav03.fra02v.mail.ibm.com (Postfix) with ESMTP; Mon, 11 Mar 2024 05:41:11 +0000 (GMT) Message-ID: Date: Mon, 11 Mar 2024 13:41:10 +0800 User-Agent: Mozilla Thunderbird Content-Language: en-US To: gcc-patches Cc: Segher Boessenkool , David , "Kewen.Lin" , Peter Bergner From: HAO CHEN GUI Subject: [PATCH, RFC] combine: Don't truncate const operand of AND if it's no benefits X-TM-AS-GCONF: 00 X-Proofpoint-GUID: xDkJuUpGX7Hdb06iQCsrFe6OVr-baQeO X-Proofpoint-ORIG-GUID: FKrPttGyuKO29k0EJheJF09aFdf90xjr X-Proofpoint-UnRewURL: 0 URL was un-rewritten MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1011,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-03-11_02,2024-03-06_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 impostorscore=0 suspectscore=0 spamscore=0 bulkscore=0 phishscore=0 clxscore=1015 lowpriorityscore=0 malwarescore=0 mlxscore=0 adultscore=0 priorityscore=1501 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2311290000 definitions=main-2403110042 X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_MSPIKE_H4, 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.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 Hi, This patch tries to fix the problem when a canonical form doesn't benefit on a specific target. The const operand of AND is and with the nonzero bits of another operand in combine pass. It's a canonical form, but it's no benefits for the target which has rotate and mask insns. As the mask is truncated, it can't match the insn conditions which it originally matches. For example, the following insn condition checks the sum of two AND masks. When one of the mask is truncated, the condition breaks. (define_insn "*rotlsi3_insert_5" [(set (match_operand:SI 0 "gpc_reg_operand" "=r,r") (ior:SI (and:SI (match_operand:SI 1 "gpc_reg_operand" "0,r") (match_operand:SI 2 "const_int_operand" "n,n")) (and:SI (match_operand:SI 3 "gpc_reg_operand" "r,0") (match_operand:SI 4 "const_int_operand" "n,n"))))] "rs6000_is_valid_mask (operands[2], NULL, NULL, SImode) && UINTVAL (operands[2]) != 0 && UINTVAL (operands[4]) != 0 && UINTVAL (operands[2]) + UINTVAL (operands[4]) + 1 == 0" ... This patch tries to fix the problem by comparing the rtx cost. If another operand (varop) is not changed and rtx cost with new mask is not less than the original one, the mask is restored to original one. I'm not sure if comparison of rtx cost here is proper. The outer code is unknown and I suppose it as "SET". Also the rtx cost might not be accurate. From my understanding, the canonical forms should always benefit as it can't be undo in combine pass. Do we have a perfect solution for this kind of issues? Looking forward for your advice. Another similar issues for canonical forms. Whether the widen mode for lshiftrt is always good? https://gcc.gnu.org/pipermail/gcc-patches/2023-July/624852.html Thanks Gui Haochen ChangeLog Combine: Don't truncate const operand of AND if it's no benefits In combine pass, the canonical form is to turn off all bits in the constant that are know to already be zero for AND. /* Turn off all bits in the constant that are known to already be zero. Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS which is tested below. */ constop &= nonzero; But it doesn't benefit when the target has rotate and mask insert insns. The AND mask is truncated and lost its information. Thus it can't match the insn conditions. For example, the following insn condition checks the sum of two AND masks. (define_insn "*rotlsi3_insert_5" [(set (match_operand:SI 0 "gpc_reg_operand" "=r,r") (ior:SI (and:SI (match_operand:SI 1 "gpc_reg_operand" "0,r") (match_operand:SI 2 "const_int_operand" "n,n")) (and:SI (match_operand:SI 3 "gpc_reg_operand" "r,0") (match_operand:SI 4 "const_int_operand" "n,n"))))] "rs6000_is_valid_mask (operands[2], NULL, NULL, SImode) && UINTVAL (operands[2]) != 0 && UINTVAL (operands[4]) != 0 && UINTVAL (operands[2]) + UINTVAL (operands[4]) + 1 == 0" ... This patch restores the const operand of AND if the another operand is not optimized and the truncated const operand doesn't save the rtx cost. gcc/ * combine.cc (simplify_and_const_int_1): Restore the const operand of AND if varop is not optimized and the rtx cost of the new const operand is not reduced. gcc/testsuite/ * gcc.target/powerpc/rlwimi-0.c: Reduced total number of insns and adjust the number of rotate and mask insns. * gcc.target/powerpc/rlwimi-1.c: Likewise. * gcc.target/powerpc/rlwimi-2.c: Likewise. patch.diff diff --git a/gcc/combine.cc b/gcc/combine.cc index a4479f8d836..16ff09ea854 100644 --- a/gcc/combine.cc +++ b/gcc/combine.cc @@ -10161,8 +10161,23 @@ simplify_and_const_int_1 (scalar_int_mode mode, rtx varop, if (constop == nonzero) return varop; - if (varop == orig_varop && constop == orig_constop) - return NULL_RTX; + if (varop == orig_varop) + { + if (constop == orig_constop) + return NULL_RTX; + else + { + rtx tmp = simplify_gen_binary (AND, mode, varop, + gen_int_mode (constop, mode)); + rtx orig = simplify_gen_binary (AND, mode, varop, + gen_int_mode (orig_constop, mode)); + if (set_src_cost (tmp, mode, optimize_this_for_speed_p) + < set_src_cost (orig, mode, optimize_this_for_speed_p)) + return tmp; + else + return NULL_RTX; + } + } /* Otherwise, return an AND. */ return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode)); diff --git a/gcc/testsuite/gcc.target/powerpc/rlwimi-0.c b/gcc/testsuite/gcc.target/powerpc/rlwimi-0.c index 961be199901..d9dd4419f1d 100644 --- a/gcc/testsuite/gcc.target/powerpc/rlwimi-0.c +++ b/gcc/testsuite/gcc.target/powerpc/rlwimi-0.c @@ -2,15 +2,15 @@ /* { dg-options "-O2" } */ /* { dg-final { scan-assembler-times {(?n)^\s+[a-z]} 16485 { target ilp32 } } } */ -/* { dg-final { scan-assembler-times {(?n)^\s+[a-z]} 19909 { target lp64 } } } */ +/* { dg-final { scan-assembler-times {(?n)^\s+[a-z]} 19780 { target lp64 } } } */ /* { dg-final { scan-assembler-times {(?n)^\s+blr} 6750 } } */ /* { dg-final { scan-assembler-times {(?n)^\s+mr} 3007 { target ilp32 } } } */ -/* { dg-final { scan-assembler-times {(?n)^\s+mr} 11 { target lp64 } } } */ -/* { dg-final { scan-assembler-times {(?n)^\s+rldicl} 6420 { target lp64 } } } */ +/* { dg-final { scan-assembler-times {(?n)^\s+mr} 140 { target lp64 } } } */ +/* { dg-final { scan-assembler-times {(?n)^\s+rldicl} 6162 { target lp64 } } } */ /* { dg-final { scan-assembler-times {(?n)^\s+rlwimi} 6420 { target ilp32 } } } */ -/* { dg-final { scan-assembler-times {(?n)^\s+rldimi} 310 { target lp64 } } } */ -/* { dg-final { scan-assembler-times {(?n)^\s+rlwimi} 6110 { target lp64 } } } */ +/* { dg-final { scan-assembler-times {(?n)^\s+rldimi} 366 { target lp64 } } } */ +/* { dg-final { scan-assembler-times {(?n)^\s+rlwimi} 6054 { target lp64 } } } */ /* { dg-final { scan-assembler-times {(?n)^\s+rotlwi} 308 } } */ diff --git a/gcc/testsuite/gcc.target/powerpc/rlwimi-1.c b/gcc/testsuite/gcc.target/powerpc/rlwimi-1.c index e2c607b1d45..d955fc792d9 100644 --- a/gcc/testsuite/gcc.target/powerpc/rlwimi-1.c +++ b/gcc/testsuite/gcc.target/powerpc/rlwimi-1.c @@ -2,15 +2,15 @@ /* { dg-options "-O2" } */ /* { dg-final { scan-assembler-times {(?n)^\s+[a-z]} 13977 { target ilp32 } } } */ -/* { dg-final { scan-assembler-times {(?n)^\s+[a-z]} 20217 { target lp64 } } } */ +/* { dg-final { scan-assembler-times {(?n)^\s+[a-z]} 20189 { target lp64 } } } */ /* { dg-final { scan-assembler-times {(?n)^\s+blr} 6750 } } */ /* { dg-final { scan-assembler-times {(?n)^\s+mr} 499 { target ilp32 } } } */ -/* { dg-final { scan-assembler-times {(?n)^\s+mr} 11 { target lp64 } } } */ -/* { dg-final { scan-assembler-times {(?n)^\s+rldicl} 6728 { target lp64 } } } */ +/* { dg-final { scan-assembler-times {(?n)^\s+mr} 39 { target lp64 } } } */ +/* { dg-final { scan-assembler-times {(?n)^\s+rldicl} 6672 { target lp64 } } } */ /* { dg-final { scan-assembler-times {(?n)^\s+rlwimi} 1404 { target ilp32 } } } */ -/* { dg-final { scan-assembler-times {(?n)^\s+rldimi} 134 { target lp64 } } } */ -/* { dg-final { scan-assembler-times {(?n)^\s+rlwimi} 1270 { target lp64 } } } */ +/* { dg-final { scan-assembler-times {(?n)^\s+rldimi} 190 { target lp64 } } } */ +/* { dg-final { scan-assembler-times {(?n)^\s+rlwimi} 1214 { target lp64 } } } */ /* { dg-final { scan-assembler-times {(?n)^\s+mulli} 5324 } } */ diff --git a/gcc/testsuite/gcc.target/powerpc/rlwimi-2.c b/gcc/testsuite/gcc.target/powerpc/rlwimi-2.c index bafa371db73..9abfd7abf08 100644 --- a/gcc/testsuite/gcc.target/powerpc/rlwimi-2.c +++ b/gcc/testsuite/gcc.target/powerpc/rlwimi-2.c @@ -2,14 +2,14 @@ /* { dg-options "-O2" } */ /* { dg-final { scan-assembler-times {(?n)^\s+[a-z]} 14121 { target ilp32 } } } */ -/* { dg-final { scan-assembler-times {(?n)^\s+[a-z]} 20217 { target lp64 } } } */ +/* { dg-final { scan-assembler-times {(?n)^\s+[a-z]} 19622 { target lp64 } } } */ /* { dg-final { scan-assembler-times {(?n)^\s+blr} 6750 } } */ /* { dg-final { scan-assembler-times {(?n)^\s+mr} 643 { target ilp32 } } } */ -/* { dg-final { scan-assembler-times {(?n)^\s+mr} 11 { target lp64 } } } */ -/* { dg-final { scan-assembler-times {(?n)^\s+rldicl} 7790 { target lp64 } } } */ +/* { dg-final { scan-assembler-times {(?n)^\s+mr} 658 { target lp64 } } } */ +/* { dg-final { scan-assembler-times {(?n)^\s+rldicl} 5460 { target lp64 } } } */ /* { dg-final { scan-assembler-times {(?n)^\s+rlwimi} 1692 { target ilp32 } } } */ -/* { dg-final { scan-assembler-times {(?n)^\s+rlwimi} 1666 { target lp64 } } } */ +/* { dg-final { scan-assembler-times {(?n)^\s+rlwimi} 1610 { target lp64 } } } */ /* { dg-final { scan-assembler-times {(?n)^\s+mulli} 5036 } } */