From patchwork Wed Sep 18 06:45:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eikansh Gupta X-Patchwork-Id: 1986692 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=X1PFK7+5; 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 4X7q186h8nz1y1g for ; Wed, 18 Sep 2024 16:46:03 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8EECE3858C31 for ; Wed, 18 Sep 2024 06:46:00 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) by sourceware.org (Postfix) with ESMTPS id 8C9753858D34 for ; Wed, 18 Sep 2024 06:45:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8C9753858D34 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 8C9753858D34 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=205.220.180.131 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1726641944; cv=none; b=CVchnISvpIbIF82WlaZcTJVG4fBTL8KIIb5PPX2rPs+lUAA17e2R3IPO80StlHBjeIhby09Hpbrh6yIVQnXHe5zHBpahCFP/7hZByjn/XzVI80UlphSYMgv9Nz9+ydLycePT5bDioJVWX2Kb5RpXnGxWm19DmAxHdnxvnc1SedQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1726641944; c=relaxed/simple; bh=I2YA5vFvGNxRTd/nR9ejLJ2zQ0drPrnlCZtkxkCBn/k=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=KyOarOVa1G+eglqxqaZOe67fFeUM9xrvBCgmbCUIlYXpZtJfmHKnwoYcrpqhJgTj3Q8O7rS60QyueX5D7aATX+Rd7f90gR20LzK4hh1t/GH3JFvT1jwlClTVYBxEhJPTrMG5599f9LxxyIP75y8DmlfedW2indnQIWpc+aQgFHA= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0279868.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 48HIWbXK014104 for ; Wed, 18 Sep 2024 06:45:42 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=U53VAClSAy7x1uki4Z2pHh3HiZ3aEu/Hpk1NcUFj1s0=; b=X1 PFK7+5f4DeTKVsUzH/W++R0Yoe9KTTVqxnJ/LDM9kH5mhLuZqdnlSrn3OC9SaT3x 1ycKO6D0diSeRnq2v/kfQ1nVwn/P9abuZhW9aMxsyV9f/LucOvviQ4xtLEOTNViY xOmOwvwIgFb2OTAnIhSua7/RUyL4cdpC2BsS9njmJyqHOO6WbPKCCA9LhgeyLna+ kPPsbFhuyyBP4AigRIERj1mp8bk5wja6/vrJnvLmi17kqtGEud/Qg3bDAg9YaT5E vuKZCTbmDpJmP68HEU7eXB6C1C+N9Dz3Bz1AYcgM3GcutmW2jOnIGAmNnDoPNFLU G/9UKmovSNSIRgejD/1g== Received: from nalasppmta05.qualcomm.com (Global_NAT1.qualcomm.com [129.46.96.20]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 41n4jes2de-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Wed, 18 Sep 2024 06:45:41 +0000 (GMT) Received: from nalasex01b.na.qualcomm.com (nalasex01b.na.qualcomm.com [10.47.209.197]) by NALASPPMTA05.qualcomm.com (8.18.1.2/8.18.1.2) with ESMTPS id 48I6jehG020241 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Wed, 18 Sep 2024 06:45:40 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:45:39 -0700 From: Eikansh Gupta To: CC: Eikansh Gupta Subject: [PATCH v2] MATCH: Simplify `a rrotate (32-b) -> a lrotate b` [PR109906] Date: Wed, 18 Sep 2024 12:15:17 +0530 Message-ID: <20240918064517.3593-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: nasanex01a.na.qualcomm.com (10.52.223.231) 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: e_81aKdx5NhGMwJOoi21EVC7lGnK40OV X-Proofpoint-GUID: e_81aKdx5NhGMwJOoi21EVC7lGnK40OV 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 clxscore=1015 mlxscore=0 malwarescore=0 suspectscore=0 priorityscore=1501 mlxlogscore=999 adultscore=0 impostorscore=0 spamscore=0 lowpriorityscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2408220000 definitions=main-2409180039 X-Spam-Status: No, score=-13.9 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 | 9 ++++++ gcc/testsuite/gcc.dg/tree-ssa/pr109906.c | 40 ++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr109906.c diff --git a/gcc/match.pd b/gcc/match.pd index 5566c0e4c41..b67ffce9586 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -4759,6 +4759,15 @@ 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) + orotate (rrotate lrotate) + (simplify + (rotate @0 (minus INTEGER_CST@1 @2)) + (if (TYPE_PRECISION (TREE_TYPE (@0)) == wi::to_wide (@1)) + (orotate @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" } } */