From patchwork Wed Sep 18 06:21:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eikansh Gupta X-Patchwork-Id: 1986691 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=hVtaQTA0; 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 4X7pVt17rlz1y1t for ; Wed, 18 Sep 2024 16:23:17 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 34BD23858D34 for ; Wed, 18 Sep 2024 06:23:15 +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 E599F3858D26 for ; Wed, 18 Sep 2024 06:22:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E599F3858D26 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 E599F3858D26 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=1726640560; cv=none; b=KPSnK+MFCGZh1KA02TX4lxlYpjTNQW95uRtGIDEhKCF5L2smsZnB+B4gYaNYYwLt6zecqgVh8P916Yx7RiQxSItPlLbkqNhNpTR4JW03GIYB5kRxM2f1UIAmChlMvSTqKjEFT+EfVC0agN0RREA7JKCRYlUa/+j21gH5gweV5zw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1726640560; c=relaxed/simple; bh=vW36eNCeS7S8WIcktdgeO7webSh9OmZB+hk0XQk7t3k=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=so5YCw7b1T123QGlA8P3Uwn78mYNcj9d3nHci7wDzIojm4g61YWO15brN5Ppf9pgz5V7XgAZmwbQoiZU8JqlxYUSwAkQV7VxZVhHKj5ctlHjArYr94yyhvkRKXiCnFUhpSkxgYARvf5M5VrRavHgrc7JuEioJ+4tIIGMPN7p0CM= 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 48HIbYPQ010049 for ; Wed, 18 Sep 2024 06:22:36 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h= cc:content-type:date:from:message-id:mime-version:subject:to; s= qcppdkim1; bh=cP9lDPXueqiAYMg++ZWIV/7iNYpVyQa4inW8mfax3pA=; b=hV taQTA0aODC4xFVlQYcWbL0ZUoPUfkQuugQ6Pb4uRWy7nWnFUV/iFB9Mk1DM2DpSl 4oUyFkhxaX/D4zwYIsx6DmVd47bYHBER4FSKRcHAKRT2Y4qtYFrgy72EIb3253Pt PjspmSaTNF1YEbZTHnBf33R2kbWjqavleaLN+VRrIcT2tmpfNL93EzIhe/6vXe1q lNoxF7N37Er0xd60L684zld93PkFqJbgewoH/nfv2lJhy399GK4XyIZtSNtChYpq ulRIQR5YHuvZtXP6ghPQVLRMoohtlyM4IMloN2HTI/TAohAEyVeoi4y3qpUicMbD W9SMREVBPVNGCO1tpdJg== Received: from nalasppmta03.qualcomm.com (Global_NAT1.qualcomm.com [129.46.96.20]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 41n4jds0tt-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Wed, 18 Sep 2024 06:22:36 +0000 (GMT) Received: from nalasex01b.na.qualcomm.com (nalasex01b.na.qualcomm.com [10.47.209.197]) by NALASPPMTA03.qualcomm.com (8.18.1.2/8.18.1.2) with ESMTPS id 48I6MZlp010495 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Wed, 18 Sep 2024 06:22:35 GMT Received: from hu-eikagupt-hyd.qualcomm.com (10.80.80.8) by nalasex01b.na.qualcomm.com (10.47.209.197) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.9; Tue, 17 Sep 2024 23:22:34 -0700 From: Eikansh Gupta To: CC: Eikansh Gupta Subject: [PATCH] MATCH: Simplify `a rrotate (32-b) -> a lrotate b` [PR109906] Date: Wed, 18 Sep 2024 11:51:57 +0530 Message-ID: <20240918062157.29060-1-quic_eikagupt@quicinc.com> X-Mailer: git-send-email 2.17.1 MIME-Version: 1.0 X-Originating-IP: [10.80.80.8] X-ClientProxiedBy: nasanex01b.na.qualcomm.com (10.46.141.250) To nalasex01b.na.qualcomm.com (10.47.209.197) X-QCInternal: smtphost X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=5800 signatures=585085 X-Proofpoint-ORIG-GUID: uBhef-KBgOTFLJWKKNgLmg2pT2vBTAWr X-Proofpoint-GUID: uBhef-KBgOTFLJWKKNgLmg2pT2vBTAWr 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_notspam policy=outbound score=0 phishscore=0 impostorscore=0 clxscore=1011 adultscore=0 mlxscore=0 suspectscore=0 malwarescore=0 priorityscore=1501 spamscore=0 bulkscore=0 lowpriorityscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2408220000 definitions=main-2409180036 X-Spam-Status: No, score=-13.8 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 The pattern `a rrotate (32-b)` should be optimized to `a lrotate b`. The same is also true for `a lrotate (32-b)`. It can be optimized to `a rrotate b`. This patch adds following patterns: a rrotate (32-b) -> a lrotate b a lrotate (32-b) -> a rrotate b PR tree-optimization/109906 gcc/ChangeLog: * match.pd (a rrotate (32-b) -> a lrotate b): New pattern (a lrotate (32-b) -> a rrotate b): New pattern gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/pr109906.c: New test. --- gcc/match.pd | 10 ++++++ gcc/testsuite/gcc.dg/tree-ssa/pr109906.c | 40 ++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr109906.c diff --git a/gcc/match.pd b/gcc/match.pd index 5566c0e4c41..77cb3f8060a 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -4759,6 +4759,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) build_int_cst (TREE_TYPE (@1), element_precision (type)), @1); })) +/* a rrotate (32-b) -> a lrotate b */ +/* a lrotate (32-b) -> a rrotate b */ +(for rotate (lrotate rrotate) + (simplify + (rotate @0 (minus INTEGER_CST@1 @2)) + (if (TYPE_PRECISION (TREE_TYPE (@0)) == wi::to_wide (@1)) + (if (rotate == RROTATE_EXPR) + (lrotate @0 @2) + (rrotate @0 @2))))) + /* Turn (a OP c1) OP c2 into a OP (c1+c2). */ (for op (lrotate rrotate rshift lshift) (simplify diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr109906.c b/gcc/testsuite/gcc.dg/tree-ssa/pr109906.c new file mode 100644 index 00000000000..fe576d7ce3a --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr109906.c @@ -0,0 +1,40 @@ +/* PR tree-optimization/109906 */ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-optimized-raw" } */ + +/* Implementation of rotate right operation */ +static inline +unsigned rrotate(unsigned x, int t) +{ + if (t >= 32) __builtin_unreachable(); + unsigned tl = x >> (t); + unsigned th = x << (32-t); + return tl | th; +} + +/* Here rotate left is achieved by doing rotate right by (32 - x) */ +unsigned rotateleft(unsigned t, int x) +{ + return rrotate (t, 32-x); +} + +/* Implementation of rotate left operation */ +static inline +unsigned lrotate(unsigned x, int t) +{ + if (t >= 32) __builtin_unreachable(); + unsigned tl = x << (t); + unsigned th = x >> (32-t); + return tl | th; +} + +/* Here rotate right is achieved by doing rotate left by (32 - x) */ +unsigned rotateright(unsigned t, int x) +{ + return lrotate (t, 32-x); +} + +/* Shouldn't have instruction for (32 - x). */ +/* { dg-final { scan-tree-dump-not "minus_expr" "optimized" } } */ +/* { dg-final { scan-tree-dump "rrotate_expr" "optimized" } } */ +/* { dg-final { scan-tree-dump "lrotate_expr" "optimized" } } */