From patchwork Mon Sep 30 08:04:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Artemiy Volkov X-Patchwork-Id: 1990776 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=synopsys.com header.i=@synopsys.com header.a=rsa-sha256 header.s=pfptdkimsnps header.b=iD+Ukbhy; dkim=pass (2048-bit key; unprotected) header.d=synopsys.com header.i=@synopsys.com header.a=rsa-sha256 header.s=mail header.b=gEVWzxX4; 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 4XHDDD57FMz1xsq for ; Mon, 30 Sep 2024 18:06:20 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8AD3A3851C05 for ; Mon, 30 Sep 2024 08:06:18 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0a-00230701.pphosted.com (mx0a-00230701.pphosted.com [148.163.156.19]) by sourceware.org (Postfix) with ESMTPS id E2BEF385EC0B for ; Mon, 30 Sep 2024 08:04:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E2BEF385EC0B Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=synopsys.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=synopsys.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E2BEF385EC0B Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=148.163.156.19 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727683484; cv=none; b=vrtrchN6TmnmrxQpXMy7qVUWTzBqYsNha8Bfo34WxgVnD3aZzy5zZtCMqtidzv5/tCoQUuhweHhw7AzhzMyXQ68FdFV+Z2cyg4QuTnuIs3cjjXzoNKkUb7StckQTsCfLQwxGCOulxh2lF3A1296UpgBNtJ8DF95PcYevRJH22AI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727683484; c=relaxed/simple; bh=YbNHqy46Qp5bR05FzaFK4MzrJXux0x88+/UgsNUPsic=; h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-ID: MIME-Version; b=Yq8OeAPrAaCEmSP9vnn8frrZbm0Wu2B8on//T11IHSYcufW6DasueaoJB7uHu/7CHDiCVGlYgGsIMpNHxllNS+lKTO0LmI02dykotOWQKwYcm35egErocNYDhb5uB11SAdsaD5yvp0UdS7fiR9VrJgBgUfwSS0/e2Qm74VVA5hc= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0297266.ppops.net [127.0.0.1]) by mx0a-00230701.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 48U5qjTK024194; Mon, 30 Sep 2024 01:04:40 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=synopsys.com; h= cc:content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=pfptdkimsnps; bh=Tjf6DAgx /w6g3xXAwIokU6RJtoMbIvaNIGexE/9DR6k=; b=iD+UkbhygKGya2w14T4uIupr 5L2Jdpbu+TorKyN7duXZzq6DOVggBX8+bx5IuPZad3Ske/OC3cFO5KiIZZ+kYCSq raTbUJvW/Y/2TAM02d/t8tx4NiL8a0/Iza2lt2qamBaqOjZTQ8OMccKS1yjd1/oW UWJBlqDpZ7I13AenDH7FqRhcVdKuEGLrjc72pAjuMVG7avC7t/23gN0O1LTDxnTd EvA8HuACRGbE/Ly/vAKdsJJXFYEUtPtezXWrBqojPzglERIKlfr0iKBrohc/q7si ZRvIS0IngNb6bva7T6LembF7JcaCIbo9PoscdyegbltVEk0T1vGg1RIe6sXmNA== Received: from smtprelay-out1.synopsys.com (smtprelay-out1.synopsys.com [149.117.87.133]) by mx0a-00230701.pphosted.com (PPS) with ESMTPS id 41ybr32p2j-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 30 Sep 2024 01:04:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=synopsys.com; s=mail; t=1727683478; bh=YbNHqy46Qp5bR05FzaFK4MzrJXux0x88+/UgsNUPsic=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gEVWzxX43zpcJODwhiAazRQa6ZTsBI5YoVAOEzdq45koi7tTb2l4i6K4a650OhlHe PU3TdEUqkXBlo5azDmkYgKwPfAh8mw6PjS+YJnH/KVCfPhUoKYbSR/YRiXPUSbXa4I WZezDkzOjFU1jKlt21ubG3tZwazF6ojsPyqku23WD6Y5w++cMAkW2m6jfDBYv52Mzo BIguIQyAHMtepeeWyM/RLdPPmLnq5KjH2yGwYYOI6aPi3bPedmR047Tf2mFVA6FDto rWuQrpb4kBPaRi9Uun+/lDzd9uVeolCfWUrger0QPCsBrkT6sMMASA0voNBiPzEWo0 MPdbG/kOUzh9w== Received: from mailhost.synopsys.com (badc-mailhost1.synopsys.com [10.192.0.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) client-signature RSA-PSS (2048 bits)) (Client CN "mailhost.synopsys.com", Issuer "SNPSica2" (verified OK)) by smtprelay-out1.synopsys.com (Postfix) with ESMTPS id 71DA140349; Mon, 30 Sep 2024 08:04:38 +0000 (UTC) Received: from us01arcgnu3.internal.synopsys.com (us01arcgnu3.internal.synopsys.com [10.194.34.166]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (Client did not present a certificate) by mailhost.synopsys.com (Postfix) with ESMTPSA id C6B41A00AE; Mon, 30 Sep 2024 08:04:37 +0000 (UTC) X-SNPS-Relay: synopsys.com From: Artemiy Volkov To: gcc-patches@gcc.gnu.org Cc: rguenther@suse.de, jeffreyalaw@gmail.com, quic_apinski@quicinc.com, Artemiy Volkov Subject: [PATCH v4 1/4] tree-optimization/116024 - simplify C1-X cmp C2 for UB-on-overflow types Date: Mon, 30 Sep 2024 01:04:05 -0700 Message-ID: <20240930080408.2501963-2-artemiy@synopsys.com> X-Mailer: git-send-email 2.44.2 In-Reply-To: <20240930080408.2501963-1-artemiy@synopsys.com> References: <20240930080408.2501963-1-artemiy@synopsys.com> MIME-Version: 1.0 X-Authority-Analysis: v=2.4 cv=NpODcNdJ c=1 sm=1 tr=0 ts=66fa5b97 cx=c_pps a=t4gDRyhI9k+KZ5gXRQysFQ==:117 a=t4gDRyhI9k+KZ5gXRQysFQ==:17 a=EaEq8P2WXUwA:10 a=qPHU084jO2kA:10 a=jIQo8A4GAAAA:8 a=bXNIPKydyp0ayI4OiMwA:9 a=Lf5xNeLK5dgiOs8hzIjU:22 X-Proofpoint-ORIG-GUID: uTTO9FPQONfnxQp5SbIC274V6r-OwvIu X-Proofpoint-GUID: uTTO9FPQONfnxQp5SbIC274V6r-OwvIu X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.60.29 definitions=2024-09-06_09,2024-09-06_01,2024-09-02_01 X-Proofpoint-Spam-Details: rule=outbound_active_cloned_notspam policy=outbound_active_cloned score=0 lowpriorityscore=0 priorityscore=1501 suspectscore=0 phishscore=0 spamscore=0 mlxscore=0 clxscore=1015 adultscore=0 mlxlogscore=999 malwarescore=0 bulkscore=0 impostorscore=0 classifier=spam authscore=0 adjust=0 reason=mlx scancount=1 engine=8.19.0-2408220000 definitions=main-2409300057 X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, 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 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 Implement a match.pd pattern for C1 - X cmp C2, where C1 and C2 are integer constants and X is of a UB-on-overflow type. The pattern is simplified to X rcmp C1 - C2 by moving X and C2 to the other side of the comparison (with opposite signs). If C1 - C2 happens to overflow, replace the whole expression with either a constant 0 or a constant 1 node, depending on the comparison operator and the sign of the overflow. This transformation allows to occasionally save load-immediate / subtraction instructions, e.g. the following statement: 10 - (int) x <= 9; now compiles to sgt a0,a0,zero instead of li a5,10 sub a0,a5,a0 slti a0,a0,10 on 32-bit RISC-V. Additional examples can be found in the newly added test file. This patch has been bootstrapped and regtested on aarch64, x86_64, and i386, and additionally regtested on riscv32. Existing tests were adjusted where necessary. gcc/ChangeLog: PR tree-optimization/116024 * match.pd: New transformation around integer comparison. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/pr116024.c: New test. * gcc.dg/pr67089-6.c: Adjust. Signed-off-by: Artemiy Volkov --- gcc/match.pd | 26 ++++++++++ gcc/testsuite/gcc.dg/pr67089-6.c | 4 +- gcc/testsuite/gcc.dg/tree-ssa/pr116024.c | 66 ++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr116024.c diff --git a/gcc/match.pd b/gcc/match.pd index e06a812e976..b074f49eebd 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -8996,6 +8996,32 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) } (cmp @0 { res; }))))))))) +/* Invert sign of X in comparisons of the form C1 - X CMP C2. */ + +(for cmp (lt le gt ge eq ne) + rcmp (gt ge lt le eq ne) + (simplify + (cmp (minus INTEGER_CST@0 @1) INTEGER_CST@2) +/* For UB-on-overflow types, simply switch sides for X and C2 + to arrive at X RCMP C1 - C2, handling the case when the latter + expression overflows. */ + (if (!TREE_OVERFLOW (@0) && !TREE_OVERFLOW (@2) + && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@1))) + (with { tree res = int_const_binop (MINUS_EXPR, @0, @2); } + (if (TREE_OVERFLOW (res)) + (switch + (if (cmp == NE_EXPR) + { constant_boolean_node (true, type); }) + (if (cmp == EQ_EXPR) + { constant_boolean_node (false, type); }) + { + bool less = cmp == LE_EXPR || cmp == LT_EXPR; + bool ovf_high = wi::lt_p (wi::to_wide (@0), 0, + TYPE_SIGN (TREE_TYPE (@0))); + constant_boolean_node (less == ovf_high, type); + }) + (rcmp @1 { res; })))))) + /* Canonicalizations of BIT_FIELD_REFs. */ (simplify diff --git a/gcc/testsuite/gcc.dg/pr67089-6.c b/gcc/testsuite/gcc.dg/pr67089-6.c index b59d75b2318..80a33c3f3e2 100644 --- a/gcc/testsuite/gcc.dg/pr67089-6.c +++ b/gcc/testsuite/gcc.dg/pr67089-6.c @@ -57,5 +57,5 @@ T (25, unsigned short, 2U - x, if (r > 2U) foo (0)) T (26, unsigned char, 2U - x, if (r <= 2U) foo (0)) /* { dg-final { scan-tree-dump-times "ADD_OVERFLOW" 16 "widening_mul" { target { i?86-*-* x86_64-*-* } } } } */ -/* { dg-final { scan-tree-dump-times "SUB_OVERFLOW" 11 "widening_mul" { target { { i?86-*-* x86_64-*-* } && { ! ia32 } } } } } */ -/* { dg-final { scan-tree-dump-times "SUB_OVERFLOW" 9 "widening_mul" { target { { i?86-*-* x86_64-*-* } && ia32 } } } } */ +/* { dg-final { scan-tree-dump-times "SUB_OVERFLOW" 9 "widening_mul" { target { { i?86-*-* x86_64-*-* } && { ! ia32 } } } } } */ +/* { dg-final { scan-tree-dump-times "SUB_OVERFLOW" 7 "widening_mul" { target { { i?86-*-* x86_64-*-* } && ia32 } } } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr116024.c b/gcc/testsuite/gcc.dg/tree-ssa/pr116024.c new file mode 100644 index 00000000000..6efa0c2f916 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr116024.c @@ -0,0 +1,66 @@ +/* PR tree-optimization/116024 */ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-forwprop1-details" } */ + +#include +#include + +uint32_t f(void); + +int32_t i1(void) +{ + int32_t l = 10 - (int32_t)f(); + return l <= 9; // f() > 0 +} + +int32_t i1a(void) +{ + int32_t l = 20 - (int32_t)f(); + return l <= INT32_MIN; // return 0 +} + +int32_t i1b(void) +{ + int32_t l = 30 - (int32_t)f(); + return l <= INT32_MIN + 31; // f() == INT32_MAX +} + +int32_t i1c(void) +{ + int32_t l = INT32_MAX - 40 - (int32_t)f(); + return l <= -38; // f() > INT32_MAX - 3 +} + +int32_t i1d(void) +{ + int32_t l = INT32_MAX - 50 - (int32_t)f(); + return l <= INT32_MAX - 1; // f() != -50 +} + +int32_t i1e(void) +{ + int32_t l = INT32_MAX - 60 - (int32_t)f(); + return l != INT32_MAX - 90; // f() != 30 +} + +int32_t i1f(void) +{ + int32_t l = INT32_MIN + 70 - (int32_t)f(); + return l <= INT32_MAX - 2; // return 0 +} + +int32_t i1g(void) +{ + int32_t l = INT32_MAX/2 + 30 - (int32_t)f(); + return l <= INT32_MIN/2 - 30; // return 1 +} + + +/* { dg-final { scan-tree-dump-times "Removing dead stmt:.*?- _" 5 "forwprop1" } } */ +/* { dg-final { scan-tree-dump-times "return 0" 2 "forwprop1" } } */ +/* { dg-final { scan-tree-dump-times "return 1" 1 "forwprop1" } } */ +/* { dg-final { scan-tree-dump-times "gimple_simplified to.* > 0" 1 "forwprop1" } } */ +/* { dg-final { scan-tree-dump-times "gimple_simplified to.* == 2147483647" 1 "forwprop1" } } */ +/* { dg-final { scan-tree-dump-times "gimple_simplified to.* > 2147483644" 1 "forwprop1" } } */ +/* { dg-final { scan-tree-dump-times "gimple_simplified to.* != 4294967246" 1 "forwprop1" } } */ +/* { dg-final { scan-tree-dump-times "gimple_simplified to.* != 30" 1 "forwprop1" } } */