From patchwork Wed May 8 03:24:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 1932770 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=quicinc.com header.i=@quicinc.com header.a=rsa-sha256 header.s=qcppdkim1 header.b=nZQmlTpI; 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 4VZ0rw50B6z1ydW for ; Wed, 8 May 2024 13:25:19 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 911363858C50 for ; Wed, 8 May 2024 03:25:17 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) by sourceware.org (Postfix) with ESMTPS id C8C953858D1E for ; Wed, 8 May 2024 03:24:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C8C953858D1E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=quicinc.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=quicinc.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C8C953858D1E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=205.220.168.131 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715138699; cv=none; b=KI94zr/UYMJrXrmJSK/Hwy9aTDQyZeUqgJJA0lcvU5MN1uQWaCEFpI0dTmqYi3wDvW9tSgC0ZzBMMfwKFh0+He9IC44wwbyKxXUdvBghKXdRVCrwvl0vDdJdx6tDrxio3dMm4JX2CogxuE0Z9a8qt+zj3XjynRCdzV/lxwiP0Qw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715138699; c=relaxed/simple; bh=/6m+GjJ0gWmu/0gdig1mTbL/hG4eAYEDzDevINcNRsE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=JzwoUNf3axuGjV/H0cNasv5gX1EU1+K+CVgN5fPi3TUSSBm2m0ugywB1SqFsplYXPPnNWAiN1fS9E2xEM0UWLrqdnMCps90jjQwcj2YnFu+YbbjwuOU7bgigGbdW84DuFrs4Nfxi/0G7lMXMYPSus2J8KgazboP3yFhkwjufhu0= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0279866.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 4482KuLo004061 for ; Wed, 8 May 2024 03:24:56 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h= from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding:content-type; s=qcppdkim1; bh=kvGS37t VOCsphWFRvCKXXjSX1C4u6bHuZcgfedDruwA=; b=nZQmlTpIuBZX6L3Tu++Mu8Z XqJL7pZBA7ThYnkAHfXwTwShfdTZGi9nQ1d8z3WyaVzSo4hqK1TBcH3UYijzu2jZ 3DWlvmB4HthbkdDc5rTqJidNM5Xie5CAUQhochJqZLYuR0ilSKcVOxfgkNzVpKT3 l23AcOnw88SJwHnCBLL8yKxKF+qTqBkcuCn/MQwclCo3l7mwphSjNjWfRgn+Dmpu IHD7izhw1M7Yh6DQ9CiJLCkpzJXLj8k+l+zBM2wB/5aK/IA3HwET4+nwZuOnoAnW OSf4xjfbqxqsuwA9kNQH0dCBEdzhMaNSGSbP6yKkdI1bNulXsHZCRvl999C1S7Q= = Received: from nasanppmta02.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3xysg8rux5-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Wed, 08 May 2024 03:24:56 +0000 (GMT) Received: from nasanex01c.na.qualcomm.com (nasanex01c.na.qualcomm.com [10.45.79.139]) by NASANPPMTA02.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 4483Ot83005547 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Wed, 8 May 2024 03:24:55 GMT Received: from hu-apinski-lv.qualcomm.com (10.49.16.6) by nasanex01c.na.qualcomm.com (10.45.79.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.9; Tue, 7 May 2024 20:24:55 -0700 From: Andrew Pinski To: CC: Andrew Pinski Subject: [PATCH] match: `a CMP nonnegative ? a : ABS` simplified to just `ABS` [PR112392] Date: Tue, 7 May 2024 20:24:44 -0700 Message-ID: <20240508032444.573884-1-quic_apinski@quicinc.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Originating-IP: [10.49.16.6] X-ClientProxiedBy: nalasex01b.na.qualcomm.com (10.47.209.197) To nasanex01c.na.qualcomm.com (10.45.79.139) X-QCInternal: smtphost X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=5800 signatures=585085 X-Proofpoint-GUID: v4b9fuhNooZrc-r1fSum-9V3EZCJAgm4 X-Proofpoint-ORIG-GUID: v4b9fuhNooZrc-r1fSum-9V3EZCJAgm4 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.650,FMLib:17.11.176.26 definitions=2024-05-07_16,2024-05-06_02,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 phishscore=0 adultscore=0 mlxscore=0 suspectscore=0 mlxlogscore=575 lowpriorityscore=0 impostorscore=0 priorityscore=1501 bulkscore=0 spamscore=0 malwarescore=0 clxscore=1015 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2405010000 definitions=main-2405080024 X-Spam-Status: No, score=-13.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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 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 We can optimize `a == nonnegative ? a : ABS`, `a > nonnegative ? a : ABS` and `a >= nonnegative ? a : ABS` into `ABS`. This allows removal of some extra comparison and extra conditional moves in some cases. I don't remember where I had found though but it is simple to add so let's add it. Bootstrapped and tested on x86_64-linux-gnu with no regressions. Note I have a secondary pattern for the equal case as either a or nonnegative could be used. PR tree-optimization/112392 gcc/ChangeLog: * match.pd (`x CMP nonnegative ? x : ABS`): New pattern; where CMP is ==, > and >=. (`x CMP nonnegative@y ? y : ABS`): New pattern. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/phi-opt-41.c: New test. Signed-off-by: Andrew Pinski --- gcc/match.pd | 15 ++++++++++ gcc/testsuite/gcc.dg/tree-ssa/phi-opt-41.c | 34 ++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-41.c diff --git a/gcc/match.pd b/gcc/match.pd index 03a03c31233..07e743ae464 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -5876,6 +5876,21 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (convert (absu:utype @0))) @3)))) +/* X > Positive ? X : ABS(X) -> ABS(X) */ +/* X >= Positive ? X : ABS(X) -> ABS(X) */ +/* X == Positive ? X : ABS(X) -> ABS(X) */ +(for cmp (eq gt ge) + (simplify + (cond (cmp:c @0 tree_expr_nonnegative_p@1) @0 (abs@3 @0)) + (if (INTEGRAL_TYPE_P (type)) + @3))) + +/* X == Positive ? Positive : ABS(X) -> ABS(X) */ +(simplify + (cond (eq:c @0 tree_expr_nonnegative_p@1) @1 (abs@3 @0)) + (if (INTEGRAL_TYPE_P (type)) + @3)) + /* (X + 1) > Y ? -X : 1 simplifies to X >= Y ? -X : 1 when X is unsigned, as when X + 1 overflows, X is -1, so -X == 1. */ (simplify diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-41.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-41.c new file mode 100644 index 00000000000..9774e283a7b --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-41.c @@ -0,0 +1,34 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-phiopt1" } */ +/* PR tree-optimization/112392 */ + +int feq_1(int a, unsigned char b) +{ + int absb = b; + if (a == absb) return absb; + return a > 0 ? a : -a; +} +int feq_2(int a, unsigned char b) +{ + int absb = b; + if (a == absb) return a; + return a > 0 ? a : -a; +} + +int fgt(int a, unsigned char b) +{ + int absb = b; + if (a > absb) return a; + return a > 0 ? a : -a; +} + +int fge(int a, unsigned char b) +{ + int absb = b; + if (a >= absb) return a; + return a > 0 ? a : -a; +} + + +/* { dg-final { scan-tree-dump-not "if " "phiopt1" } } */ +/* { dg-final { scan-tree-dump-times "ABS_EXPR <" 4 "phiopt1" } } */