public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: David Faust <dfaust@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r13-4566] bpf: add define_insn for bswap
Date: Thu,  8 Dec 2022 18:50:59 +0000 (GMT)	[thread overview]
Message-ID: <20221208185059.A607C3890860@sourceware.org> (raw)

https://gcc.gnu.org/g:5ddfe794405a514824382e96b1047d3195af23cc

commit r13-4566-g5ddfe794405a514824382e96b1047d3195af23cc
Author: David Faust <david.faust@oracle.com>
Date:   Thu Dec 8 10:08:22 2022 -0800

    bpf: add define_insn for bswap
    
    The eBPF architecture provides 'end[be,le]' instructions for endianness
    swapping. Add a define_insn for bswap<mode>2 to use them instaed of
    falling back on a libcall.
    
    gcc/
    
            * config/bpf/bpf.md (bswap<mode>2): New define_insn.
    
    gcc/testsuite/
    
            * gcc.target/bpf/bswap-1.c: New test.

Diff:
---
 gcc/config/bpf/bpf.md                  | 17 +++++++++++++++++
 gcc/testsuite/gcc.target/bpf/bswap-1.c | 23 +++++++++++++++++++++++
 2 files changed, 40 insertions(+)

diff --git a/gcc/config/bpf/bpf.md b/gcc/config/bpf/bpf.md
index a28021aef26..22a133f1c79 100644
--- a/gcc/config/bpf/bpf.md
+++ b/gcc/config/bpf/bpf.md
@@ -341,6 +341,23 @@
   "rsh<msuffix>\t%0,%2"
   [(set_attr "type" "<mtype>")])
 
+;;;; Endianness conversion
+
+(define_mode_iterator BSM [HI SI DI])
+(define_mode_attr endmode [(HI "16") (SI "32") (DI "64")])
+
+(define_insn "bswap<BSM:mode>2"
+  [(set (match_operand:BSM 0 "register_operand"            "=r")
+        (bswap:BSM (match_operand:BSM 1 "register_operand" " r")))]
+  ""
+{
+  if (TARGET_BIG_ENDIAN)
+    return "endle\t%0, <endmode>";
+  else
+    return "endbe\t%0, <endmode>";
+}
+  [(set_attr "type" "end")])
+
 ;;;; Conditional branches
 
 ;; The eBPF jump instructions use 64-bit arithmetic when evaluating
diff --git a/gcc/testsuite/gcc.target/bpf/bswap-1.c b/gcc/testsuite/gcc.target/bpf/bswap-1.c
new file mode 100644
index 00000000000..4748143ada5
--- /dev/null
+++ b/gcc/testsuite/gcc.target/bpf/bswap-1.c
@@ -0,0 +1,23 @@
+/* { dg-do compile } */
+/* { dg-options "-mlittle-endian" } */
+
+unsigned short in16 = 0x1234U;
+unsigned int   in32 = 0x12345678U;
+unsigned long  in64 = 0x123456789abcdef0ULL;
+
+unsigned short out16 = 0;
+unsigned int   out32 = 0;
+unsigned long  out64 = 0;
+
+int foo (void)
+{
+  out16 = __builtin_bswap16 (in16);
+  out32 = __builtin_bswap32 (in32);
+  out64 = __builtin_bswap64 (in64);
+
+  return 0;
+}
+
+/* { dg-final { scan-assembler "endbe\t%r., 16" } } */
+/* { dg-final { scan-assembler "endbe\t%r., 32" } } */
+/* { dg-final { scan-assembler "endbe\t%r., 64" } } */

                 reply	other threads:[~2022-12-08 18:50 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221208185059.A607C3890860@sourceware.org \
    --to=dfaust@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).