public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Feng Wang <wangfeng@eswincomputing.com>
To: gcc-patches@gcc.gnu.org
Cc: kito.cheng@gmail.com, palmer@dabbelt.com,
	Feng Wang <wangfeng@eswincomputing.com>
Subject: [PATCH] RISC-V: Optimize load memory data in rv64
Date: Fri, 24 Mar 2023 01:52:39 +0000	[thread overview]
Message-ID: <20230324015239.13455-1-wangfeng@eswincomputing.com> (raw)

This patch optimize load one byte or halfword from memory in rv64.
Please refer to the following test case for loading one byte.
int sextb32_memory(int* x)
{ return (*x << 24) >> 24; }

The build flags are "-march=rv64g -mabi=lp64d -O2"
The current compilation results are as follows,

slliw a0,a0,0x18
sraiw a0,a0,0x18
ret

The compilation results after picking this patch are as follows,
lb a0,0(a0)
ret

The iusse is introduced by this patch
"RISC-V: Avoid zero/sign extend for volatile loads. Fix for 97417."
This patch expand
(set (reg:QI/HI/SI target) (mem:QI/HI/SI (address)))
to
(set (reg:DI temp) (zero_extend:DI (mem:QI/HI/SI (address))))
(set (reg:QI/HI/SI target) (subreg:QI/HI/SI (reg:DI temp) 0))
There is no problem with this transformation for QI and HI.
However,it will affect the subsequent combine processing for SI.
So I modified this operation to only take effect for QI and HI.

gcc/ChangeLog:

        * config/riscv/riscv.cc (riscv_legitimize_move):Modify length judgment

gcc/testsuite/ChangeLog:

        * gcc.target/riscv/rv64-load-byte.c: New test.
        * gcc.target/riscv/rv64-load-halfword.c: New test.
---
 gcc/config/riscv/riscv.cc                           | 2 +-
 gcc/testsuite/gcc.target/riscv/rv64-load-byte.c     | 8 ++++++++
 gcc/testsuite/gcc.target/riscv/rv64-load-halfword.c | 8 ++++++++
 3 files changed, 17 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.target/riscv/rv64-load-byte.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/rv64-load-halfword.c

diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc
index 1db12091b5a..4b596c7bb5b 100644
--- a/gcc/config/riscv/riscv.cc
+++ b/gcc/config/riscv/riscv.cc
@@ -2074,7 +2074,7 @@ riscv_legitimize_move (machine_mode mode, rtx dest, rtx src)
        (set (reg:QI target) (subreg:QI (reg:DI temp) 0))
      with auto-sign/zero extend.  */
   if (GET_MODE_CLASS (mode) == MODE_INT
-      && GET_MODE_SIZE (mode).to_constant () < UNITS_PER_WORD
+      && GET_MODE_SIZE (mode).to_constant () < MIN_UNITS_PER_WORD
       && can_create_pseudo_p ()
       && MEM_P (src))
     {
diff --git a/gcc/testsuite/gcc.target/riscv/rv64-load-byte.c b/gcc/testsuite/gcc.target/riscv/rv64-load-byte.c
new file mode 100644
index 00000000000..929aac79993
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/rv64-load-byte.c
@@ -0,0 +1,8 @@
+/* { dg-do compile } */
+/* { dg-options "-march=rv64g -mabi=lp64d -O2" } */
+/* { dg-skip-if "" { *-*-* } { "-O0" "-Og" } } */
+
+int sextb32_memory(int* x)
+{ return (*x << 24) >> 24; }
+
+/* { dg-final { scan-assembler "lb" } } */
diff --git a/gcc/testsuite/gcc.target/riscv/rv64-load-halfword.c b/gcc/testsuite/gcc.target/riscv/rv64-load-halfword.c
new file mode 100644
index 00000000000..94e1bd7e135
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/rv64-load-halfword.c
@@ -0,0 +1,8 @@
+/* { dg-do compile } */
+/* { dg-options "-march=rv64g -mabi=lp64d -O2" } */
+/* { dg-skip-if "" { *-*-* } { "-O0" "-Og" } } */
+
+int sexth32_memory(int* x)
+{ return (*x << 16) >> 16; }
+
+/* { dg-final { scan-assembler "lh" } } */
-- 
2.17.1


                 reply	other threads:[~2023-03-24  1:53 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=20230324015239.13455-1-wangfeng@eswincomputing.com \
    --to=wangfeng@eswincomputing.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=kito.cheng@gmail.com \
    --cc=palmer@dabbelt.com \
    /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).