public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Roger Sayle <sayle@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r14-177] [xstormy16] Update xstormy16_rtx_costs.
Date: Sun, 23 Apr 2023 09:37:44 +0000 (GMT)	[thread overview]
Message-ID: <20230423093744.CF1863858C50@sourceware.org> (raw)

https://gcc.gnu.org/g:8ffff5e9de2fa8e2845c8045941afbb1e8638aed

commit r14-177-g8ffff5e9de2fa8e2845c8045941afbb1e8638aed
Author: Roger Sayle <roger@nextmovesoftware.com>
Date:   Sun Apr 23 10:35:53 2023 +0100

    [xstormy16] Update xstormy16_rtx_costs.
    
    This patch provides an improved rtx_costs target hook on xstormy16.
    The current implementation has the unfortunate property that it claims
    that zero_extendhisi2 is very cheap, even though the machine description
    doesn't provide that instruction/pattern.  Doh!  Rewriting the
    xstormy16_rtx_costs function has additional benefits, including
    making more use of the (short) "mul" instruction when optimizing
    for size with -Os.
    
    2023-04-23  Roger Sayle  <roger@nextmovesoftware.com>
    
    gcc/ChangeLog
            * config/stormy16/stormy16.cc (xstormy16_rtx_costs): Rewrite to
            provide reasonable values for common arithmetic operations and
            immediate operands (in several machine modes).
    
    gcc/testsuite/ChangeLog
            * gcc.target/xstormy16/mulhi.c: New test case.

Diff:
---
 gcc/config/stormy16/stormy16.cc            | 166 +++++++++++++++++++++++++++--
 gcc/testsuite/gcc.target/xstormy16/mulhi.c |   8 ++
 2 files changed, 163 insertions(+), 11 deletions(-)

diff --git a/gcc/config/stormy16/stormy16.cc b/gcc/config/stormy16/stormy16.cc
index 647b72c4538..98f87fa8251 100644
--- a/gcc/config/stormy16/stormy16.cc
+++ b/gcc/config/stormy16/stormy16.cc
@@ -72,19 +72,23 @@ static GTY(()) section *bss100_section;
    scanned.  In either case, *TOTAL contains the cost result.  */
 
 static bool
-xstormy16_rtx_costs (rtx x, machine_mode mode ATTRIBUTE_UNUSED,
+xstormy16_rtx_costs (rtx x, machine_mode mode,
 		     int outer_code ATTRIBUTE_UNUSED,
-		     int opno ATTRIBUTE_UNUSED, int *total,
-		     bool speed ATTRIBUTE_UNUSED)
+		     int opno ATTRIBUTE_UNUSED, int *total, bool speed_p)
 {
-  int code = GET_CODE (x);
+  rtx_code code = GET_CODE (x);
 
   switch (code)
     {
     case CONST_INT:
-      if (INTVAL (x) < 16 && INTVAL (x) >= 0)
-        *total = COSTS_N_INSNS (1) / 2;
-      else if (INTVAL (x) < 256 && INTVAL (x) >= 0)
+      if (mode == SImode)
+	{
+	  HOST_WIDE_INT lo_word = INTVAL (x) & 0xffff;
+	  HOST_WIDE_INT hi_word = INTVAL (x) >> 16;
+	  *total = COSTS_N_INSNS (IN_RANGE (lo_word, 0, 255) ? 1 : 2);
+	  *total += COSTS_N_INSNS (IN_RANGE (hi_word, 0, 255) ? 1 : 2);
+	}
+      else if (mode == QImode || IN_RANGE(INTVAL (x), 0, 255))
 	*total = COSTS_N_INSNS (1);
       else
 	*total = COSTS_N_INSNS (2);
@@ -97,12 +101,152 @@ xstormy16_rtx_costs (rtx x, machine_mode mode ATTRIBUTE_UNUSED,
       *total = COSTS_N_INSNS (2);
       return true;
 
+    case PLUS:
+    case MINUS:
+      if (mode == SImode)
+	{
+	  if (CONST_INT_P (XEXP (x, 1)))
+	    {
+	      HOST_WIDE_INT lo_word = INTVAL (XEXP (x, 1)) & 0xffff;
+	      HOST_WIDE_INT hi_word = INTVAL (XEXP (x, 1)) >> 16;
+	      if (IN_RANGE (lo_word, 0, 16))
+		*total = COSTS_N_INSNS (1);
+	      else
+		*total = COSTS_N_INSNS (2);
+	      if (IN_RANGE (hi_word, 0, 16))
+		*total += COSTS_N_INSNS (1);
+	      else
+		*total += COSTS_N_INSNS (2);
+	    }
+	  else
+	    {
+	      *total = COSTS_N_INSNS (2);
+	      *total += rtx_cost (XEXP (x, 1), mode, code, 0, speed_p);
+	    }
+	  *total += rtx_cost (XEXP (x, 0), mode, code, 0, speed_p);
+	  return true;
+	}
+      else
+	{
+	  if (CONST_INT_P (XEXP (x, 1)))
+	    {
+	      if (IN_RANGE (INTVAL (XEXP (x, 1)), 0, 16))
+		*total = COSTS_N_INSNS (1);
+	      else
+		*total = COSTS_N_INSNS (2);
+	    }
+	  else
+	    {
+	      *total = COSTS_N_INSNS (1);
+	      *total += rtx_cost (XEXP (x, 1), mode, code, 0, speed_p);
+	    }
+	  *total += rtx_cost (XEXP (x, 0), mode, code, 0, speed_p);
+	  return true;
+	}
+      return false;
+
     case MULT:
-      *total = COSTS_N_INSNS (35 + 6);
-      return true;
+      if (mode == QImode)
+        *total = COSTS_N_INSNS (speed_p ? 18 + 5 : 6);
+      else if (mode == SImode)
+	*total = COSTS_N_INSNS (speed_p ? 3 * 18 + 14 : 17);
+      else 
+        *total = COSTS_N_INSNS (speed_p ? 18 + 3 : 4);
+      return false;
+
     case DIV:
-      *total = COSTS_N_INSNS (51 - 6);
-      return true;
+    case MOD:
+      if (mode == QImode)
+        *total = COSTS_N_INSNS (speed_p ? 19 + 6 : 7);
+      else if (mode == SImode)
+	*total = COSTS_N_INSNS (speed_p ? 100 : 7);
+      else
+        *total = COSTS_N_INSNS (speed_p ? 19 + 3 : 4);
+      return false;
+
+    case UDIV:
+    case UMOD:
+      if (mode == QImode)
+        *total = COSTS_N_INSNS (speed_p ? 18 + 7 : 8);
+      else if (mode == SImode)
+	*total = COSTS_N_INSNS (speed_p ? 100 : 7);
+      else
+        *total = COSTS_N_INSNS (speed_p ? 18 + 3 : 4);
+      return false;
+
+    case ASHIFT:
+    case ASHIFTRT:
+    case LSHIFTRT:
+      if (REG_P (XEXP (x, 0))
+	  && CONST_INT_P (XEXP (x, 1)))
+	{
+	  if (mode == HImode)
+	    {
+	      /* asr/shl/shr.  */
+	      *total = COSTS_N_INSNS (1);
+	      return true;
+	    }
+	  else if (mode == QImode)
+	    {
+	      /* (shl+shr)+shr.  */
+	      *total = COSTS_N_INSNS (3);
+	      return true;
+	    }
+	  else if (mode == SImode)
+	    {
+	      if (IN_RANGE (INTVAL (XEXP (x, 1)), 16, 31))
+		*total = COSTS_N_INSNS (3);
+	      else
+	        *total = COSTS_N_INSNS (5);
+	      return true;
+	    }
+	}
+      return false;
+
+    case ZERO_EXTEND:
+      if (mode == HImode)
+	{
+	  if (GET_MODE (XEXP (x, 0)) == QImode)
+	    /* shl+shr.  */
+	    *total = COSTS_N_INSNS (2);
+	}
+      else if (mode == SImode)
+	{
+	  if (GET_MODE (XEXP (x, 0)) == HImode)
+	    /* mov+mov.  */
+	    *total = COSTS_N_INSNS (2);
+	  else if (GET_MODE (XEXP (x, 0)) == QImode)
+	    /* mov+shl+shr+mov.  */
+	    *total = COSTS_N_INSNS (4);
+	}
+      return false;
+
+    case SIGN_EXTEND:
+      if (mode == HImode)
+	{
+	  if (GET_MODE (XEXP (x, 0)) == QImode)
+	    /* cbw.  */
+	    *total = COSTS_N_INSNS (1);
+	}
+      else if (mode == SImode)
+	{
+	  if (GET_MODE (XEXP (x, 0)) == HImode)
+	    /* mov+asr.  */
+	    *total = COSTS_N_INSNS (2);
+	  else if (GET_MODE (XEXP (x, 0)) == QImode)
+	    /* mov+shl+shr+mov.  */
+	    *total = COSTS_N_INSNS (3);
+	}
+      return false;
+
+    case SET:
+      if (REG_P (XEXP (x, 0)))
+	{
+	  if (!REG_P (XEXP (x, 1)))
+	    *total = rtx_cost (XEXP (x, 1), mode, SET, 1, speed_p);
+	  return true;
+	}
+      return false;
 
     default:
       return false;
diff --git a/gcc/testsuite/gcc.target/xstormy16/mulhi.c b/gcc/testsuite/gcc.target/xstormy16/mulhi.c
new file mode 100644
index 00000000000..885f1453b6a
--- /dev/null
+++ b/gcc/testsuite/gcc.target/xstormy16/mulhi.c
@@ -0,0 +1,8 @@
+/* { dg-do compile } */
+/* { dg-options "-Os" } */
+unsigned short foo(unsigned short x)
+{
+  return x*91;
+}
+
+/* { dg-final { scan-assembler "mul" } } */

                 reply	other threads:[~2023-04-23  9:37 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=20230423093744.CF1863858C50@sourceware.org \
    --to=sayle@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).