From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from loongson.cn (mail.loongson.cn [114.242.206.163]) by sourceware.org (Postfix) with ESMTP id A23323858D1E for ; Mon, 6 Feb 2023 13:07:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A23323858D1E Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=loongson.cn Received: from loongson.cn (unknown [10.20.4.52]) by gateway (Coremail) with SMTP id _____8CxPuuo++BjfCsPAA--.29446S3; Mon, 06 Feb 2023 21:07:53 +0800 (CST) Received: from [10.20.4.52] (unknown [10.20.4.52]) by localhost.localdomain (Coremail) with SMTP id AQAAf8Cxmb2l++BjGuMqAA--.17052S2; Mon, 06 Feb 2023 21:07:49 +0800 (CST) Subject: Re: [PATCH] LoongArch: Generate bytepick.[wd] for suitable bit operation pattern To: Xi Ruoyao , gcc-patches@gcc.gnu.org Cc: WANG Xuerui , Chenghua Xu References: <20230203175022.690671-1-xry111@xry111.site> From: Lulu Cheng Message-ID: <046c8fce-99b8-17aa-e0e2-d3a17430ccc7@loongson.cn> Date: Mon, 6 Feb 2023 21:07:49 +0800 User-Agent: Mozilla/5.0 (X11; Linux mips64; rv:68.0) Gecko/20100101 Thunderbird/68.7.0 MIME-Version: 1.0 In-Reply-To: <20230203175022.690671-1-xry111@xry111.site> Content-Type: text/plain; charset=gbk; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US X-CM-TRANSID:AQAAf8Cxmb2l++BjGuMqAA--.17052S2 X-CM-SenderInfo: xfkh0wpoxo3qxorr0wxvrqhubq/ X-Coremail-Antispam: 1Uk129KBjvJXoW3JrW5CryrZr4rKr4rCryUZFb_yoWxur48pa 97G3W5Gr48XFs2g34qkay3Xws8JrnrCFyjvFZ3K3sFkw1UXw1Igw18CFySv3WUZw4Fyw4x ZFW8Ww13ur43G3DanT9S1TB71UUUUUDqnTZGkaVYY2UrUUUUj1kv1TuYvTs0mT0YCTnIWj qI5I8CrVACY4xI64kE6c02F40Ex7xfYxn0WfASr-VFAUDa7-sFnT9fnUUIcSsGvfJTRUUU bxAYFVCjjxCrM7AC8VAFwI0_Jr0_Gr1l1xkIjI8I6I8E6xAIw20EY4v20xvaj40_Wr0E3s 1l1IIY67AEw4v_Jr0_Jr4l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxSw2x7M28EF7xv wVC0I7IYx2IY67AKxVWUCVW8JwA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxVW8JVWxJwA2z4 x0Y4vEx4A2jsIE14v26r4UJVWxJr1l84ACjcxK6I8E87Iv6xkF7I0E14v26F4UJVW0owAS 0I0E0xvYzxvE52x082IY62kv0487Mc804VCY07AIYIkI8VC2zVCFFI0UMc02F40EFcxC0V AKzVAqx4xG6I80ewAv7VC0I7IYx2IY67AKxVWUXVWUAwAv7VC2z280aVAFwI0_Jr0_Gr1l Ox8S6xCaFVCjc4AY6r1j6r4UM4x0Y48IcVAKI48JMxk0xIA0c2IEe2xFo4CEbIxvr21l42 xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWU GwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r126r1DMIIYrxkI7VAKI4 8JMIIF0xvE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v26r1j6r4U MIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I 8E87Iv6xkF7I0E14v26r1j6r4UYxBIdaVFxhVjvjDU0xZFpf9x07UNvtZUUUUU= X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00,GIT_PATCH_0,KAM_DMARC_STATUS,MIME_CHARSET_FARAWAY,NICE_REPLY_A,SPF_HELO_PASS,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 List-Id: ÔÚ 2023/2/4 ÉÏÎç1:50, Xi Ruoyao дµÀ: > 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)? LGTM! I think this change is not big and can be merged into the trunk branch. Thanks! > 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);