From patchwork Tue Apr 25 20:20:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roger Sayle X-Patchwork-Id: 87539 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp3649717vqo; Tue, 25 Apr 2023 13:21:56 -0700 (PDT) X-Google-Smtp-Source: AKy350ZVuD4pN8ue3Yocs2vtW0vwFqsdN38qa6gTzOT8T0j8onEnrB/7/HVl1DN6u4eMiTDgRv4/ X-Received: by 2002:a17:906:f24c:b0:933:1134:be1e with SMTP id gy12-20020a170906f24c00b009331134be1emr13900763ejb.53.1682454116101; Tue, 25 Apr 2023 13:21:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682454116; cv=none; d=google.com; s=arc-20160816; b=BWc0znNW+Jtuv9+50ydW+dvHJeLIBHL+sKtSIigcyc0LKx8e2Nk+VWtxqFE53MZ3p4 irjz2PkM6X68Ko20p6XWgeP8Ufuy4vcM25P4jr+21ivf9qcpiRl4w0E+OOELjv4+DGqT CFr6QLK9uPyQ+LB8Rb/92X9hZr5qsL0CAXoUYUDi5whrUvLJQ+i53otnDDpu880YK5je 7ykX/+0HW8LgpI+cRRTBgGo/iMbXYQXzV2yyF1mVOPWKQM9vGa0S3VV2PqlKjrf247Qs vODLlLISph9v6dkdFLAT4fZH087pHCxFE33H8OLxXUUWqrWOayGuIMHIImRGjNNI6jFu AVwA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-language:thread-index :mime-version:message-id:date:subject:cc:to:from:dkim-signature :dmarc-filter:delivered-to; bh=OYzXtxJA8gFAe9dm+Cuw7x8kSt50QF9QZ96RyO8d/KE=; b=o7q5CfpSZ7BbI7puqZpv/IH2+ghu1dkfMNmEg+Ol+iFNPe8aJSPBZqM8vNgJECpqo3 ErzrpdaC5Hw5v2hf58xn3wfd9/frNTU5iImPt6rEq/b77FqF7ZFiAJFZo8Q+fg5N/9nH ou0d4cyqsFYgq4iXOSc+qyrmvWQu9iWK3quFy9NCJUFsuo26fS5K5+nCGCEq1QUrQE9q RON1E/Bu2KcrkMZDjPRd+te7BIhHPk52Zliq7P7jgJPLyBnz9FQ8tWhMZHcMT+wg8Ch4 Zu3W7jCQeyFTnT4P3l1jz9FeFR6c2whMMFfyDmnWWBSdnRi2OpeKANanZN2T11yJO5vJ 3lzQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@nextmovesoftware.com header.s=default header.b=ZOsYS9GR; 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" Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id ci14-20020a170906c34e00b0094ee8b8d52esi10402265ejb.469.2023.04.25.13.21.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Apr 2023 13:21:56 -0700 (PDT) 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=fail header.i=@nextmovesoftware.com header.s=default header.b=ZOsYS9GR; 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" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7E12A385701A for ; Tue, 25 Apr 2023 20:21:18 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from server.nextmovesoftware.com (server.nextmovesoftware.com [162.254.253.69]) by sourceware.org (Postfix) with ESMTPS id CD0D83858417 for ; Tue, 25 Apr 2023 20:20:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CD0D83858417 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=nextmovesoftware.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=nextmovesoftware.com DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=nextmovesoftware.com; s=default; h=Content-Type:MIME-Version:Message-ID: Date:Subject:Cc:To:From:Sender:Reply-To:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=OYzXtxJA8gFAe9dm+Cuw7x8kSt50QF9QZ96RyO8d/KE=; b=ZOsYS9GRESJx0lq+OLl/1Rl3rW H7AuCGsfmt6FBkStmYo/aYMeK1ufIeGC2b2hlJOkkDf6c4VkCRLr1lS9DsiDHfk824luLvcfkeanU Wm1EAXhj8v53/zWsWyBip6QXxAGg3Iv6CMWcwbqOzJEARWmlgYNU7uO73RCdeLQtUDt1HsgElj5pW ZRrH1YBEPZehqh3Z9XVjrDCu/mtJYJ8x7yBjtbec7Ux8ZXNwPeXloPJVIqLFI13yfM/gefqU2RzXx Q4+czh7HZUC4u4t1c4K/aTjk3JA4QLBSHx3uny0sKvNzGO4ZrDnocxeWry7cgzl5wBQDd3TvvT5Re ueFxGVbw==; Received: from host86-169-41-81.range86-169.btcentralplus.com ([86.169.41.81]:64063 helo=Dell) by server.nextmovesoftware.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1prP9h-000713-0C; Tue, 25 Apr 2023 16:20:53 -0400 From: "Roger Sayle" To: "'GCC Patches'" Cc: "'Jeff Law'" Subject: [xstormy16 PATCH] Add support for byte and word swapping instructions. Date: Tue, 25 Apr 2023 21:20:50 +0100 Message-ID: <019e01d977b3$6ea3cc20$4beb6460$@nextmovesoftware.com> MIME-Version: 1.0 X-Mailer: Microsoft Outlook 16.0 Thread-Index: Adl3snGvnb4IZb/MSSG8ifJD+oc7BQ== Content-Language: en-gb X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - server.nextmovesoftware.com X-AntiAbuse: Original Domain - gcc.gnu.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - nextmovesoftware.com X-Get-Message-Sender-Via: server.nextmovesoftware.com: authenticated_id: roger@nextmovesoftware.com X-Authenticated-Sender: server.nextmovesoftware.com: roger@nextmovesoftware.com X-Source: X-Source-Args: X-Source-Dir: X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_BARRACUDACENTRAL, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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: , 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?1764181007252495360?= X-GMAIL-MSGID: =?utf-8?q?1764181007252495360?= This patch adds support for xstormy16's swpb (swap bytes) and swpw (swap words) instructions. The most obvious application of these to implement the __builtin_bswap16 and __builtin_bswap32 intrinsics. Currently, __builtin_bswap16 is implemented as: foo: mov r7,r2 shl r7,#8 shr r2,#8 or r2,r7 ret but with this patch becomes: foo: swpb r2 ret Likewise, __builtin_bswap32 now becomes: foo: swpb r2 | swpb r3 | swpw r2,r3 ret Finally, the swpw instruction on its own can be used to exchange two word mode registers without a temporary, so a new pattern and peephole2 have been added to catch this. As described in the PR rtl-optimization/106518, register allocation can (in theory) be more efficient on targets that provide a swap/exchange instruction. The slightly unusual swap naming matches that used in i386.md. This patch has been tested by building a cross-compiler to xstormy16-elf from x86_64-pc-linux-gnu, and confirming the new test cases pass. Ok for mainline? 2024-04-25 Roger Sayle gcc/ChangeLog * config/stormy16/stormy16.md (bswaphi2): New define_insn. (bswapsi2): New define_insn. (swaphi): New define_insn to exchange two registers (swpw). (define_peephole2): Recognize exchange of registers as swaphi. gcc/testsuite/ChangeLog * gcc.target/xstormy16/bswap16.c: New test case. * gcc.target/xstormy16/bswap32.c: Likewise. * gcc.target/xstormy16/swpb.c: Likewise. * gcc.target/xstormy16/swpw-1.c: Likewise. * gcc.target/xstormy16/swpw-2.c: Likewise. Thanks in advance, Roger diff --git a/gcc/config/stormy16/stormy16.md b/gcc/config/stormy16/stormy16.md index fd52588..4c2cff2 100644 --- a/gcc/config/stormy16/stormy16.md +++ b/gcc/config/stormy16/stormy16.md @@ -1265,3 +1265,39 @@ "bp %1,#7,%l0" [(set_attr "length" "4") (set_attr "psw_operand" "nop")]) + +(define_insn "bswaphi2" + [(set (match_operand:HI 0 "register_operand" "=r") + (bswap:HI (match_operand:HI 1 "register_operand" "0")))] + "" + "swpb %0") + +(define_insn "bswapsi2" + [(set (match_operand:SI 0 "register_operand" "=r") + (bswap:SI (match_operand:SI 1 "register_operand" "0")))] + "" + "swpb %0 | swpb %h0 | swpw %0,%h0" + [(set_attr "length" "6")]) + +(define_insn "swaphi" + [(set (match_operand:HI 0 "register_operand" "+r") + (match_operand:HI 1 "register_operand" "+r")) + (set (match_dup 1) + (match_dup 0))] + "" + "swpw %0,%1") + +(define_peephole2 + [(set (match_operand:HI 0 "register_operand") + (match_operand:HI 1 "register_operand")) + (set (match_dup 1) + (match_operand:HI 2 "register_operand")) + (set (match_dup 2) + (match_dup 0))] + "REGNO (operands[0]) != REGNO (operands[1]) + && REGNO (operands[0]) != REGNO (operands[2]) + && REGNO (operands[1]) != REGNO (operands[2]) + && peep2_reg_dead_p (3, operands[0])" + [(parallel [(set (match_dup 2) (match_dup 1)) + (set (match_dup 1) (match_dup 2))])]) + diff --git a/gcc/testsuite/gcc.target/xstormy16/bswap16.c b/gcc/testsuite/gcc.target/xstormy16/bswap16.c new file mode 100644 index 0000000..cf6795f --- /dev/null +++ b/gcc/testsuite/gcc.target/xstormy16/bswap16.c @@ -0,0 +1,9 @@ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + +unsigned short foo(unsigned short x) +{ + return __builtin_bswap16 (x); +} + +/* { dg-final { scan-assembler "swpb r2" } } */ diff --git a/gcc/testsuite/gcc.target/xstormy16/bswap32.c b/gcc/testsuite/gcc.target/xstormy16/bswap32.c new file mode 100644 index 0000000..3287e4d --- /dev/null +++ b/gcc/testsuite/gcc.target/xstormy16/bswap32.c @@ -0,0 +1,9 @@ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + +unsigned long foo(unsigned long x) +{ + return __builtin_bswap32 (x); +} + +/* { dg-final { scan-assembler "swpb" } } */ diff --git a/gcc/testsuite/gcc.target/xstormy16/swpb.c b/gcc/testsuite/gcc.target/xstormy16/swpb.c new file mode 100644 index 0000000..8ea8ff7 --- /dev/null +++ b/gcc/testsuite/gcc.target/xstormy16/swpb.c @@ -0,0 +1,9 @@ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + +unsigned short foo(unsigned short x) +{ + return (x>>8) | (x<<8); +} + +/* { dg-final { scan-assembler "swpb r2" } } */ diff --git a/gcc/testsuite/gcc.target/xstormy16/swpw-1.c b/gcc/testsuite/gcc.target/xstormy16/swpw-1.c new file mode 100644 index 0000000..bde540d --- /dev/null +++ b/gcc/testsuite/gcc.target/xstormy16/swpw-1.c @@ -0,0 +1,8 @@ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + +void ext(int x, int y); + +void foo(int x, int y) { ext(y,x); } + +/* { dg-final { scan-assembler "swpw r3,r2" } } */ diff --git a/gcc/testsuite/gcc.target/xstormy16/swpw-2.c b/gcc/testsuite/gcc.target/xstormy16/swpw-2.c new file mode 100644 index 0000000..b8d637b --- /dev/null +++ b/gcc/testsuite/gcc.target/xstormy16/swpw-2.c @@ -0,0 +1,14 @@ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + +void ext(int x, int y); + +void foo(int x, int y) +{ + int t1 = x ^ y; + int t2 = t1 ^ x; + int t3 = t1 ^ y; + ext(t2,t3); +} + +/* { dg-final { scan-assembler "swpw r3,r2" } } */