diff mbox series

[xstormy16] Update xstormy16_rtx_costs.

Message ID 007101d97538$e0734790$a159d6b0$@nextmovesoftware.com
State New
Headers show
Series [xstormy16] Update xstormy16_rtx_costs. | expand

Commit Message

Roger Sayle April 22, 2023, 4:38 p.m. UTC
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.

This patch has been tested by building a cross-compiler to xstormy-elf
on x86_64-pc-linux-gnu, and confirming that the new test case passes
with "make -k check-gcc".  Many thanks to Jeff Law for additional
testing.  Ok for mainline?


2023-04-22  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.


Roger
--

Comments

Jeff Law April 22, 2023, 10:09 p.m. UTC | #1
On 4/22/23 10:38, Roger Sayle wrote:
> 
> 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.
> 
> This patch has been tested by building a cross-compiler to xstormy-elf
> on x86_64-pc-linux-gnu, and confirming that the new test case passes
> with "make -k check-gcc".  Many thanks to Jeff Law for additional
> testing.  Ok for mainline?
> 
> 
> 2023-04-22  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.
GIven this only affects costing and we don't have a lot of activity/use 
of the xstormy16 port, no objections go ahead and commit.  If there is 
fallout from the 3 patches we'll see it ~24hrs after you commit the changes.

Thanks,
jeff
diff mbox series

Patch

diff --git a/gcc/config/stormy16/stormy16.cc b/gcc/config/stormy16/stormy16.cc
index 1ed619a..e6d6fbd 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 0000000..885f145
--- /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" } } */