From patchwork Tue Sep 6 09:42:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robin Dapp X-Patchwork-Id: 1038 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5044:0:0:0:0:0 with SMTP id h4csp549657wrt; Tue, 6 Sep 2022 02:43:26 -0700 (PDT) X-Google-Smtp-Source: AA6agR6GgFAdDB24jnHtiIIvPG+15w0g7RNgbHvaanhq+om054bks7ghkLMNVGe6105vqiHRUCCo X-Received: by 2002:a05:6402:268d:b0:43d:b9d0:9efc with SMTP id w13-20020a056402268d00b0043db9d09efcmr47802921edd.92.1662457406537; Tue, 06 Sep 2022 02:43:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662457406; cv=none; d=google.com; s=arc-20160816; b=mNvXrylFVbnoGXQX4Di3b/TCW6FWidYpSEQgtBPF/UtbujFaRU+qPeH0Pg6hHJI1Bj axdtIYB76a0C/UZrZrdQ8fANWr5BbMG7rHWains0mSYvbXkgd1T80szJK1yWJsmfTpRH nffw+LsCe0S63cMSncwu2jAZm3X4GnzIRSTkZ6X1Y5eyBq28XrwsburvK0ThOdrfhmrm F+H5sujgtd5hUODHm+5DFaq+k5Kd4Bz3EWkM/ADjnp2uIdbqP+VMAqIKGAsiJKF8TGV7 Ebt44Btros/+ebSicFAfBNL5pRcoU8SgDDKIApDDKUh0JpqU6ShaDs2D2Vy3UOFIM4/Q G75w== 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:subject:to:content-language:user-agent :mime-version:date:message-id:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=aSO1XkPi8X6irofbzQVYK7Cvw3KjKy7VTEu0LRmDBrA=; b=ClFzWRpkPI5d88CiYDV9WkY2rD6peENBOpy29jGLMejzMI5xFathhRpWMj03gqJsDh hfEkLUmmzdfjfhyndg1q27DXoC/yPIGy7kkBcrJF4SE5X48PwOzwREL4t4ueDkb1ynb6 3tU7ki5ti6C2lNFghfLgYwi8ESDEhJBzRoK5rEuQLYTwbwVfinpo0SHYUShO1K5zeVVW 01AoiH6b9oLT+K66kDjyeTgFg7yBlciQw34vpOmV98AZrpfPKlL5x56adbpyFP57HDNP uJkXRI/bK9XC1ozvbKFcMgKRj0rW86IPv+qepbK+eWJuQA/SexCMI69F6it51H5gZC0m eoNg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=M300A7Zk; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id hb16-20020a170907161000b0073d6276b00fsi11558946ejc.704.2022.09.06.02.43.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Sep 2022 02:43:26 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=M300A7Zk; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 89CC3385AE58 for ; Tue, 6 Sep 2022 09:43:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 89CC3385AE58 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1662457405; bh=aSO1XkPi8X6irofbzQVYK7Cvw3KjKy7VTEu0LRmDBrA=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=M300A7ZkO01ildIEpNVwVKb6vNADuStgdM6vTphwDZR8hj/ogxLqNgnm6CX09Zfci KamQHPxSxrhCXjSxI4h+eGY1MMMYTVQas+OUgUnKIHmd5xNucF9vdqj5iCdv8Q+hPH l874MmNL2HvYOFMJQ93iyvpuyswpwEJAQNmh2bzQ= 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 3B69F38582AD for ; Tue, 6 Sep 2022 09:42:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 3B69F38582AD Received: from pps.filterd (m0187473.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 2869Sj3B033946 for ; Tue, 6 Sep 2022 09:42:41 GMT Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3je3m1rjvq-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 06 Sep 2022 09:42:40 +0000 Received: from m0187473.ppops.net (m0187473.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 2869ToVv039604 for ; Tue, 6 Sep 2022 09:42:40 GMT Received: from ppma04ams.nl.ibm.com (63.31.33a9.ip4.static.sl-reverse.com [169.51.49.99]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3je3m1rjuh-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 06 Sep 2022 09:42:40 +0000 Received: from pps.filterd (ppma04ams.nl.ibm.com [127.0.0.1]) by ppma04ams.nl.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 2869e0pO001670; Tue, 6 Sep 2022 09:42:37 GMT Received: from b06avi18626390.portsmouth.uk.ibm.com (b06avi18626390.portsmouth.uk.ibm.com [9.149.26.192]) by ppma04ams.nl.ibm.com with ESMTP id 3jbxj8ujc6-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 06 Sep 2022 09:42:37 +0000 Received: from d06av25.portsmouth.uk.ibm.com (d06av25.portsmouth.uk.ibm.com [9.149.105.61]) by b06avi18626390.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 2869d7dT39846256 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 6 Sep 2022 09:39:07 GMT Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 607C511C052; Tue, 6 Sep 2022 09:42:35 +0000 (GMT) Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 2977E11C04C; Tue, 6 Sep 2022 09:42:35 +0000 (GMT) Received: from [9.171.63.5] (unknown [9.171.63.5]) by d06av25.portsmouth.uk.ibm.com (Postfix) with ESMTPS; Tue, 6 Sep 2022 09:42:35 +0000 (GMT) Message-ID: Date: Tue, 6 Sep 2022 11:42:34 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.12.0 Content-Language: en-US To: GCC Patches , Richard Biener , pinskia@gmail.com Subject: [PATCH] expand: Convert cst - x into cst xor x. X-TM-AS-GCONF: 00 X-Proofpoint-GUID: 9ui4CIm9jCNzrI8U4ZjhkzqzSqoMqT0a X-Proofpoint-ORIG-GUID: KtZn0WnFGo6WbOSEsOkImtxrbS8lq7dO X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.895,Hydra:6.0.528,FMLib:17.11.122.1 definitions=2022-09-06_05,2022-09-05_03,2022-06-22_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 lowpriorityscore=0 mlxlogscore=999 bulkscore=0 malwarescore=0 suspectscore=0 mlxscore=0 priorityscore=1501 spamscore=0 phishscore=0 impostorscore=0 clxscore=1015 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2207270000 definitions=main-2209060045 X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_MSPIKE_H2, 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: Robin Dapp via Gcc-patches From: Robin Dapp Reply-To: Robin Dapp Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1743212937566919630?= X-GMAIL-MSGID: =?utf-8?q?1743212937566919630?= Hi, posting this separately from PR91213 now. I wrote an s390 test and most likely it could also be done for x86 which will give it broader coverage. Depending on the backend it might be better to convert cst - x into cst xor x if cst + 1 is a power of two and 0 <= x <= cst. This patch compares both sequences and emits the less expensive one. Does this look like a viable approach? Bootstrapped and regtested on s390[x], waited with x86 tests until a first round of comments. Regards Robin gcc/ChangeLog: PR middle-end/91213 * expr.cc (expand_expr_real_2): Call new function. (maybe_optimize_cst_sub): New function. * expr.h (maybe_optimize_cst_sub): Define. gcc/testsuite/ChangeLog: * gcc.target/s390/cst-minus-var.c: New test. --- gcc/expr.cc | 79 +++++++++++++++++++ gcc/expr.h | 2 + gcc/testsuite/gcc.target/s390/cst-minus-var.c | 55 +++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 gcc/testsuite/gcc.target/s390/cst-minus-var.c + return 65535 - a; +} diff --git a/gcc/expr.cc b/gcc/expr.cc index 80bb1b8a4c5b..80f25720d7b6 100644 --- a/gcc/expr.cc +++ b/gcc/expr.cc @@ -9397,6 +9397,21 @@ expand_expr_real_2 (sepops ops, rtx target, machine_mode tmode, return simplify_gen_binary (MINUS, mode, op0, op1); } + /* Convert const - A to const xor A if integer_pow2p (const + 1) + and 0 <= A <= const. */ + if (code == MINUS_EXPR + && SCALAR_INT_MODE_P (mode) + && TREE_CODE (treeop0) == INTEGER_CST + && TREE_CODE (TREE_TYPE (treeop1)) == INTEGER_TYPE + && wi::exact_log2 (wi::to_widest (treeop0) + 1) != -1) + { + rtx res = maybe_optimize_cst_sub (code, treeop0, treeop1, + mode, unsignedp, type, + target, subtarget); + if (res) + return res; + } + /* No sense saving up arithmetic to be done if it's all in the wrong mode to form part of an address. And force_operand won't know whether to sign-extend or @@ -12692,6 +12707,70 @@ maybe_optimize_mod_cmp (enum tree_code code, tree *arg0, tree *arg1) return code == EQ_EXPR ? LE_EXPR : GT_EXPR; } +/* Convert const - A to const xor A if integer_pow2p (const + 1) + and 0 <= A <= const. */ + +rtx +maybe_optimize_cst_sub (enum tree_code code, tree treeop0, tree treeop1, + machine_mode mode, int unsignedp, tree type, + rtx target, rtx subtarget) +{ + gcc_checking_assert (code == MINUS_EXPR); + gcc_checking_assert (SCALAR_INT_MODE_P (mode)); + gcc_checking_assert (TREE_CODE (treeop0) == INTEGER_CST); + gcc_checking_assert (TREE_CODE (TREE_TYPE (treeop1)) == INTEGER_TYPE); + gcc_checking_assert (wi::exact_log2 (wi::to_widest (treeop0) + 1) != -1); + + if (!optimize) + return NULL_RTX; + + optab this_optab; + rtx op0, op1; + + if (wi::leu_p (tree_nonzero_bits (treeop1), tree_nonzero_bits (treeop0))) + { + expand_operands (treeop0, treeop1, subtarget, &op0, &op1, + EXPAND_NORMAL); + bool speed_p = optimize_insn_for_speed_p (); + do_pending_stack_adjust (); + start_sequence (); + this_optab = optab_for_tree_code (MINUS_EXPR, type, + optab_default); + rtx subi = expand_binop (mode, this_optab, op0, op1, target, + unsignedp, OPTAB_LIB_WIDEN); + + rtx_insn *sub_insns = get_insns (); + end_sequence (); + start_sequence (); + this_optab = optab_for_tree_code (BIT_XOR_EXPR, type, + optab_default); + rtx xori = expand_binop (mode, this_optab, op0, op1, target, + unsignedp, OPTAB_LIB_WIDEN); + rtx_insn *xor_insns = get_insns (); + end_sequence (); + unsigned sub_cost = seq_cost (sub_insns, speed_p); + unsigned xor_cost = seq_cost (xor_insns, speed_p); + /* If costs are the same then use as tie breaker the other other + factor. */ + if (sub_cost == xor_cost) + { + sub_cost = seq_cost (sub_insns, !speed_p); + xor_cost = seq_cost (xor_insns, !speed_p); + } + + if (sub_cost <= xor_cost) + { + emit_insn (sub_insns); + return subi; + } + + emit_insn (xor_insns); + return xori; + } + + return NULL_RTX; +} + /* Optimize x - y < 0 into x < 0 if x - y has undefined overflow. */ void diff --git a/gcc/expr.h b/gcc/expr.h index 08b59b8d869a..43ea11042d26 100644 --- a/gcc/expr.h +++ b/gcc/expr.h @@ -326,6 +326,8 @@ extern tree string_constant (tree, tree *, tree *, tree *); extern tree byte_representation (tree, tree *, tree *, tree *); extern enum tree_code maybe_optimize_mod_cmp (enum tree_code, tree *, tree *); +extern rtx maybe_optimize_cst_sub (enum tree_code, tree, tree, + machine_mode, int, tree , rtx, rtx); extern void maybe_optimize_sub_cmp_0 (enum tree_code, tree *, tree *); /* Two different ways of generating switch statements. */ diff --git a/gcc/testsuite/gcc.target/s390/cst-minus-var.c b/gcc/testsuite/gcc.target/s390/cst-minus-var.c new file mode 100644 index 000000000000..c713624a9784 --- /dev/null +++ b/gcc/testsuite/gcc.target/s390/cst-minus-var.c @@ -0,0 +1,55 @@ +/* Check that we can convert const - x to const xor x if + const + 1 is a power of two and 0 <= x <= const. */ + +/* { dg-do compile } */ +/* { dg-options "-O2 -mzarch" } */ +/* { dg-final { scan-assembler-times 8 "xr\t" { target { ! 390_z10_hw } } } } */ +/* { dg-final { scan-assembler-times 8 "xilf\t" { target { s390_z10_hw } } } } */ + +unsigned long long foo (unsigned long long a) +{ + if (a > 1) __builtin_unreachable(); + return 1 - a; +} + +unsigned long long bar (unsigned long long a) +{ + if (a > 65535) __builtin_unreachable(); + return 65535 - a; +} + +unsigned long long baz (unsigned long long a) +{ + if (a > 4294967295) __builtin_unreachable(); + return 4294967295 - a; +} + +int fooi (int a) +{ + if (a > 127 || a < 0) __builtin_unreachable(); + return 127 - a; +} + +int bari (int a) +{ + if (a > 65535 || a < 0) __builtin_unreachable(); + return 65535 - a; +} + +long bazl (long a) +{ + if (a > 4294967295 || a < 0) __builtin_unreachable(); + return 4294967295 - a; +} + +short foos (short a) +{ + if (a > 31 || a < 0) __builtin_unreachable(); + return 31 - a; +} + +short bars (int a) +{ + if (a > 65535 || a < 0) __builtin_unreachable();