From patchwork Fri Feb 3 17:50:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xi Ruoyao X-Patchwork-Id: 52600 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp975681wrn; Fri, 3 Feb 2023 09:51:23 -0800 (PST) X-Google-Smtp-Source: AK7set+ARg0Ss/svk4n0cN8t45biP+pCruyuaCktHEeGiXgvIDFCRgUnjltZi3FxUImaZ8XAS1Aq X-Received: by 2002:a17:906:c241:b0:883:3c6e:23eb with SMTP id bl1-20020a170906c24100b008833c6e23ebmr9532317ejb.42.1675446683304; Fri, 03 Feb 2023 09:51:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675446683; cv=none; d=google.com; s=arc-20160816; b=VrRt3IPdczNpOhq83ohWnX5OCKG8TuFofIAoO2kH5KtPS95gV/dbackYRB+gSjLIFT BqzDO/gKf6MbR+Osw8aSUBS4j0h0VNHweAsZlDIQAvZ7KdUUdjLgQxDJpNrn96UPU46I rHoHQQ2BchQJOOgyFC7LKdSSnLbzIJfclcXpqKQS9+iXGCxgf53sH7ms1pDh7D0Um2N3 qhkaE63JjEtNlG9Olk0BkoPVwjHq7wNUmy6ifeS4qj8/1rau8J6+2k+yE0yFM81430QC /n6RZn8r/qxI5W89SFjrZOO3hrHA3KGATUIbgZqI7sK4TVP+SUbZdvniHJQwylTFExzl 5Dmg== 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:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=cYshDcG4wDoCu6Xu5VRExoILd/2jL0lFdGJlQKvBt1E=; b=dGHa5N9xhtqSjfriZspaK1rbC/3rnO7zfao8iwMPQxsLdsViifWieDOGQELkmaHF6I hwUYDv1Rb7PCVfPdZRsYICaXPYxgaAa92qj0jPtcSo/DIfNkM7wiCO5zd+Z54p+33NdN WoKmg8AnbhgSp7gA7v4ds9I+7kLkh0Iot8tOzVvtIWqtHkt1+81yNG4egCbXXIECe2Kx YYVrh92zVkBH9dLyXaIjB+yikRLhB7wCRcL6je6/ff2U/stRPRnPRW3t5gcHiPvJUudT epwY2RBs4WWihIs+eUQGBfO7l6Zfciwq4BUc6NkRvOYvSi+0ntRqa9douVQl0zI4BLlf 3w5g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Ih9RpWBQ; 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id kb8-20020a170907924800b0087761dd9827si3308652ejb.15.2023.02.03.09.51.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Feb 2023 09:51:23 -0800 (PST) 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=Ih9RpWBQ; 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 488983858C3A for ; Fri, 3 Feb 2023 17:51:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 488983858C3A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1675446682; bh=cYshDcG4wDoCu6Xu5VRExoILd/2jL0lFdGJlQKvBt1E=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=Ih9RpWBQQBg20WavcjSrFK2QHtKIA/oixKH2Lj98/iA1t8oeCn5KvHqni47zAQQnm orlQ09uNIE+rgyYoFY/R8o+juRN6Svkbtmdd9Hh2yplHPVlFtrsupBA60aweZz/zq1 6ZrbSBZuHrFo1GGELBvVnfw2fXb533Mt51v1PAC4= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from xry111.site (xry111.site [89.208.246.23]) by sourceware.org (Postfix) with ESMTPS id 3B3453858D20 for ; Fri, 3 Feb 2023 17:50:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3B3453858D20 Received: from xry111-x57s1.. (unknown [IPv6:240e:358:1112:2300:dc73:854d:832e:6]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (Client did not present a certificate) (Authenticated sender: xry111@xry111.site) by xry111.site (Postfix) with ESMTPSA id A96C3668C3; Fri, 3 Feb 2023 12:50:33 -0500 (EST) To: gcc-patches@gcc.gnu.org Cc: WANG Xuerui , Lulu Cheng , Chenghua Xu , Xi Ruoyao Subject: [PATCH] LoongArch: Generate bytepick.[wd] for suitable bit operation pattern Date: Sat, 4 Feb 2023 01:50:22 +0800 Message-Id: <20230203175022.690671-1-xry111@xry111.site> X-Mailer: git-send-email 2.39.1 MIME-Version: 1.0 X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FROM_SUSPICIOUS_NTLD, FROM_SUSPICIOUS_NTLD_FP, GIT_PATCH_0, LIKELY_SPAM_FROM, SPF_HELO_PASS, SPF_PASS, TXREP, T_PDS_OTHER_BAD_TLD 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: Xi Ruoyao via Gcc-patches From: Xi Ruoyao Reply-To: Xi Ruoyao 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?1756833181725154278?= X-GMAIL-MSGID: =?utf-8?q?1756833181725154278?= We can use bytepick.[wd] for a << (8 * x) | b >> (8 * (sizeof(a) - x)) while a and b are uint32_t or uint64_t. This is useful for some cases, for example: https://sourceware.org/pipermail/libc-alpha/2023-February/145203.html Bootstrapped and regtested on loongarch64-linux-gnu. Ok for trunk (now or GCC 14 stage 1)? gcc/ChangeLog: * config/loongarch/loongarch.md (bytepick_w_ashift_amount): New define_int_iterator. (bytepick_d_ashift_amount): Likewise. (bytepick_imm): New define_int_attr. (bytepick_w_lshiftrt_amount): Likewise. (bytepick_d_lshiftrt_amount): Likewise. (bytepick_w_): New define_insn template. (bytepick_w__extend): Likewise. (bytepick_d_): Likewise. (bytepick_w): Remove unused define_insn. (bytepick_d): Likewise. (UNSPEC_BYTEPICK_W): Remove unused unspec. (UNSPEC_BYTEPICK_D): Likewise. * config/loongarch/predicates.md (const_0_to_3_operand): Remove unused define_predicate. (const_0_to_7_operand): Likewise. gcc/testsuite/ChangeLog: * g++.target/loongarch/bytepick.C: New test. --- gcc/config/loongarch/loongarch.md | 60 ++++++++++++++----- gcc/config/loongarch/predicates.md | 8 --- gcc/testsuite/g++.target/loongarch/bytepick.C | 32 ++++++++++ 3 files changed, 77 insertions(+), 23 deletions(-) create mode 100644 gcc/testsuite/g++.target/loongarch/bytepick.C diff --git a/gcc/config/loongarch/loongarch.md b/gcc/config/loongarch/loongarch.md index 0884ec09dfb..3509c3c21c1 100644 --- a/gcc/config/loongarch/loongarch.md +++ b/gcc/config/loongarch/loongarch.md @@ -48,8 +48,6 @@ (define_c_enum "unspec" [ UNSPEC_EH_RETURN ;; Bit operation - UNSPEC_BYTEPICK_W - UNSPEC_BYTEPICK_D UNSPEC_BITREV_4B UNSPEC_BITREV_8B @@ -544,6 +542,27 @@ (define_int_attr lrint_allow_inexact [(UNSPEC_FTINT "1") (UNSPEC_FTINTRM "0") (UNSPEC_FTINTRP "0")]) +;; Iterator and attributes for bytepick.d +(define_int_iterator bytepick_w_ashift_amount [8 16 24]) +(define_int_attr bytepick_w_lshiftrt_amount [(8 "24") + (16 "16") + (24 "8")]) +(define_int_iterator bytepick_d_ashift_amount [8 16 24 32 40 48 56]) +(define_int_attr bytepick_d_lshiftrt_amount [(8 "56") + (16 "48") + (24 "40") + (32 "32") + (40 "24") + (48 "16") + (56 "8")]) +(define_int_attr bytepick_imm [(8 "1") + (16 "2") + (24 "3") + (32 "4") + (40 "5") + (48 "6") + (56 "7")]) + ;; ;; .................... ;; @@ -3364,24 +3383,35 @@ (define_insn "fclass_" [(set_attr "type" "unknown") (set_attr "mode" "")]) -(define_insn "bytepick_w" +(define_insn "bytepick_w_" [(set (match_operand:SI 0 "register_operand" "=r") - (unspec:SI [(match_operand:SI 1 "register_operand" "r") - (match_operand:SI 2 "register_operand" "r") - (match_operand:SI 3 "const_0_to_3_operand" "n")] - UNSPEC_BYTEPICK_W))] + (ior:SI (lshiftrt (match_operand:SI 1 "register_operand" "r") + (const_int )) + (ashift (match_operand:SI 2 "register_operand" "r") + (const_int bytepick_w_ashift_amount))))] "" - "bytepick.w\t%0,%1,%2,%z3" + "bytepick.w\t%0,%1,%2," [(set_attr "mode" "SI")]) -(define_insn "bytepick_d" +(define_insn "bytepick_w__extend" [(set (match_operand:DI 0 "register_operand" "=r") - (unspec:DI [(match_operand:DI 1 "register_operand" "r") - (match_operand:DI 2 "register_operand" "r") - (match_operand:DI 3 "const_0_to_7_operand" "n")] - UNSPEC_BYTEPICK_D))] - "" - "bytepick.d\t%0,%1,%2,%z3" + (sign_extend:DI + (ior:SI (lshiftrt (match_operand:SI 1 "register_operand" "r") + (const_int )) + (ashift (match_operand:SI 2 "register_operand" "r") + (const_int bytepick_w_ashift_amount)))))] + "TARGET_64BIT" + "bytepick.w\t%0,%1,%2," + [(set_attr "mode" "SI")]) + +(define_insn "bytepick_d_" + [(set (match_operand:DI 0 "register_operand" "=r") + (ior:DI (lshiftrt (match_operand:DI 1 "register_operand" "r") + (const_int )) + (ashift (match_operand:DI 2 "register_operand" "r") + (const_int bytepick_d_ashift_amount))))] + "TARGET_64BIT" + "bytepick.d\t%0,%1,%2," [(set_attr "mode" "DI")]) (define_insn "bitrev_4b" diff --git a/gcc/config/loongarch/predicates.md b/gcc/config/loongarch/predicates.md index de0a363e2e4..95140280f1e 100644 --- a/gcc/config/loongarch/predicates.md +++ b/gcc/config/loongarch/predicates.md @@ -91,14 +91,6 @@ (define_predicate "reg_or_1_operand" (ior (match_operand 0 "const_1_operand") (match_operand 0 "register_operand"))) -(define_predicate "const_0_to_3_operand" - (and (match_code "const_int") - (match_test "IN_RANGE (INTVAL (op), 0, 3)"))) - -(define_predicate "const_0_to_7_operand" - (and (match_code "const_int") - (match_test "IN_RANGE (INTVAL (op), 0, 7)"))) - (define_predicate "lu52i_mask_operand" (and (match_code "const_int") (match_test "UINTVAL (op) == 0xfffffffffffff"))) diff --git a/gcc/testsuite/g++.target/loongarch/bytepick.C b/gcc/testsuite/g++.target/loongarch/bytepick.C new file mode 100644 index 00000000000..a39e2fa65b7 --- /dev/null +++ b/gcc/testsuite/g++.target/loongarch/bytepick.C @@ -0,0 +1,32 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -mabi=lp64d" } */ +/* { dg-final { scan-assembler-times "bytepick.w\t\\\$r4,\\\$r5,\\\$r4" 3 } } */ +/* { dg-final { scan-assembler-times "bytepick.d\t\\\$r4,\\\$r5,\\\$r4" 7 } } */ +/* { dg-final { scan-assembler-not "slli.w" } } */ + +template +T +merge (T a, T b) +{ + return a << offs | b >> (8 * sizeof (T) - offs); +} + +using u32 = __UINT32_TYPE__; +using u64 = __UINT64_TYPE__; +using i64 = __INT64_TYPE__; + +template u32 merge (u32, u32); +template u32 merge (u32, u32); +template u32 merge (u32, u32); + +template u64 merge (u64, u64); +template u64 merge (u64, u64); +template u64 merge (u64, u64); +template u64 merge (u64, u64); +template u64 merge (u64, u64); +template u64 merge (u64, u64); +template u64 merge (u64, u64); + +/* we cannot use bytepick for the following cases */ +template i64 merge (i64, i64); +template u64 merge (u64, u64);