public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Kito Cheng <kito.cheng@sifive.com>
To: gcc-patches@gcc.gnu.org, kito.cheng@gmail.com, jimw@sifive.com,
	jiawei@iscas.ac.cn, cmuellner@ventanamicro.com,
	palmer@dabbelt.com, andrew@sifive.com
Subject: [RFC PATCH 6/8] RISC-V: Use li and rori to load constants.
Date: Thu, 23 Sep 2021 15:57:29 +0800	[thread overview]
Message-ID: <20210923075731.50125-7-kito.cheng@sifive.com> (raw)
In-Reply-To: <20210923075731.50125-1-kito.cheng@sifive.com>

From: Jim Wilson <jimw@sifive.com>

gcc/ChangeLog:

	* config/riscv/riscv.c (riscv_build_integer_1): Build integer
	with rotate.

gcc/testsuite/ChangeLog:

	* gcc.target/riscv/zbb-li-rotr.c: New.
---
 gcc/config/riscv/riscv.c                     | 41 ++++++++++++++++++++
 gcc/testsuite/gcc.target/riscv/zbb-li-rotr.c | 35 +++++++++++++++++
 2 files changed, 76 insertions(+)
 create mode 100644 gcc/testsuite/gcc.target/riscv/zbb-li-rotr.c

diff --git a/gcc/config/riscv/riscv.c b/gcc/config/riscv/riscv.c
index 10f7bd21f8d..66daebbbc8f 100644
--- a/gcc/config/riscv/riscv.c
+++ b/gcc/config/riscv/riscv.c
@@ -461,6 +461,47 @@ riscv_build_integer_1 (struct riscv_integer_op codes[RISCV_MAX_INTEGER_OPS],
 	}
     }
 
+  if (cost > 2 && TARGET_64BIT && TARGET_ZBB)
+    {
+      int leading_ones = clz_hwi (~value);
+      int trailing_ones = ctz_hwi (~value);
+
+      /* If all bits are one except a few that are zero, and the zero bits
+	 are within a range of 11 bits, and at least one of the upper 32-bits
+	 is a zero, then we can generate a constant by loading a small
+	 negative constant and rotating.  */
+      if (leading_ones < 32
+	  && ((64 - leading_ones - trailing_ones) < 12))
+	{
+	  codes[0].code = UNKNOWN;
+	  /* The sign-bit might be zero, so just rotate to be safe.  */
+	  codes[0].value = (((unsigned HOST_WIDE_INT) value >> trailing_ones)
+			    | (value << (64 - trailing_ones)));
+	  codes[1].code = ROTATERT;
+	  codes[1].value = 64 - trailing_ones;
+	  cost = 2;
+	}
+      /* Handle the case where the 11 bit range of zero bits wraps around.  */
+      else
+	{
+	  int upper_trailing_ones = ctz_hwi (~value >> 32);
+	  int lower_leading_ones = clz_hwi (~value << 32);
+
+	  if (upper_trailing_ones < 32 && lower_leading_ones < 32
+	      && ((64 - upper_trailing_ones - lower_leading_ones) < 12))
+	    {
+	      codes[0].code = UNKNOWN;
+	      /* The sign-bit might be zero, so just rotate to be safe.  */
+	      codes[0].value = ((value << (32 - upper_trailing_ones))
+				| ((unsigned HOST_WIDE_INT) value
+				   >> (32 + upper_trailing_ones)));
+	      codes[1].code = ROTATERT;
+	      codes[1].value = 32 - upper_trailing_ones;
+	      cost = 2;
+	    }
+	}
+    }
+
   gcc_assert (cost <= RISCV_MAX_INTEGER_OPS);
   return cost;
 }
diff --git a/gcc/testsuite/gcc.target/riscv/zbb-li-rotr.c b/gcc/testsuite/gcc.target/riscv/zbb-li-rotr.c
new file mode 100644
index 00000000000..03254ed9150
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/zbb-li-rotr.c
@@ -0,0 +1,35 @@
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gc_zbb -mabi=lp64 -O2" } */
+
+long
+li_rori (void)
+{
+  return 0xffff77ffffffffffL;
+}
+
+long
+li_rori_2 (void)
+{
+  return 0x77ffffffffffffffL;
+}
+
+long
+li_rori_3 (void)
+{
+  return 0xfffffffeefffffffL;
+}
+
+long
+li_rori_4 (void)
+{
+  return 0x5ffffffffffffff5L;
+}
+
+long
+li_rori_5 (void)
+{
+  return 0xaffffffffffffffaL;
+}
+
+
+/* { dg-final { scan-assembler-times "rori\t" 5 } } */
-- 
2.33.0


  parent reply	other threads:[~2021-09-23  7:57 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-23  7:57 [RFC PATCH 0/8] RISC-V: Bit-manipulation extension Kito Cheng
2021-09-23  7:57 ` [RFC PATCH 1/8] RISC-V: Minimal support of bitmanip extension Kito Cheng
2021-09-27 11:16   ` Christoph Muellner
2021-10-18  8:48     ` Kito Cheng
2021-10-18 10:10       ` Christoph Muellner
2021-10-18 12:15         ` Kito Cheng
2021-09-23  7:57 ` [RFC PATCH 2/8] RISC-V: Implement instruction patterns for ZBA extension Kito Cheng
2021-09-23  7:57 ` [RFC PATCH 3/8] RISC-V: Cost model for zba extension Kito Cheng
2021-09-23  7:57 ` [RFC PATCH 4/8] RISC-V: Implement instruction patterns for ZBB extension Kito Cheng
2021-09-23  7:57 ` [RFC PATCH 5/8] RISC-V: Cost model for zbb extension Kito Cheng
2021-09-23  7:57 ` Kito Cheng [this message]
2021-09-23  7:57 ` [RFC PATCH 7/8] RISC-V: Implement instruction patterns for ZBS extension Kito Cheng
2021-09-23  7:57 ` [RFC PATCH 8/8] RISC-V: Cost model " Kito Cheng
2021-09-27 11:20 ` [RFC PATCH 0/8] RISC-V: Bit-manipulation extension Christoph Muellner
2021-09-28 22:02   ` Jim Wilson
2021-09-28 22:00 ` Jim Wilson
2021-09-28 22:05   ` Christoph Muellner
2021-09-28 23:40     ` Jim Wilson
2021-10-13 20:22 ` Vineet Gupta
2021-10-18  3:23   ` Kito Cheng
2021-10-25  9:14     ` Kito Cheng

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=20210923075731.50125-7-kito.cheng@sifive.com \
    --to=kito.cheng@sifive.com \
    --cc=andrew@sifive.com \
    --cc=cmuellner@ventanamicro.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jiawei@iscas.ac.cn \
    --cc=jimw@sifive.com \
    --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).