This patch reduces code size by disabling the performance optimized,
"const_variant" of shift library functions when optimization for size is
enabled.

For the following program, the below code size reduction is observed:
  long a;

  int
  main (void)
  {
    a = a >> 4;
    return 0;
  }

With shift patch 2:
   text    data     bss     dec     hex filename
    522      12      22     556     22c a.out
New patch:
   text    data     bss     dec     hex filename
    474      12      22     508     1fc a.out

Ok for trunk?
>From 894b6809822ba3a3a1bab3750abe29e03f2a3ad6 Mon Sep 17 00:00:00 2001
From: Jozef Lawrynowicz <joze...@mittosystems.com>
Date: Mon, 13 May 2019 17:52:19 +0100
Subject: [PATCH 3/4] MSP430: Do not use the performance optimized variant of a
 shift by constant amount when optimizing for size

gcc/ChangeLog

2019-06-04  Jozef Lawrynowicz  <joze...@mittosystems.com>

	* config/msp430/msp430.md (ashlhi3): Use the const_variant of shift
	library functions only when not optimizing for size.
	(ashlsi3): Likewise.
	(ashrhi3): Likewise.
	(ashrsi3): Likewise.
	(lshrhi3): Likewise.
	(lshrsi3): Likewise.

gcc/testsuite/ChangeLog

2019-06-04  Jozef Lawrynowicz  <joze...@mittosystems.com>

	* gcc.target/msp430/size-optimized-shifts.c: New test.
---
 gcc/config/msp430/msp430.md                   | 15 ++++++-----
 .../gcc.target/msp430/size-optimized-shifts.c | 26 +++++++++++++++++++
 2 files changed, 35 insertions(+), 6 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/msp430/size-optimized-shifts.c

diff --git a/gcc/config/msp430/msp430.md b/gcc/config/msp430/msp430.md
index 58c1f4edc9c..76296a2f317 100644
--- a/gcc/config/msp430/msp430.md
+++ b/gcc/config/msp430/msp430.md
@@ -769,7 +769,10 @@
 	     && INTVAL (operands[2]) == 1)
       emit_insn (gen_slli_1 (operands[0], operands[1]));
     else		 
-      msp430_expand_helper (operands, \"__mspabi_slli\", true);
+      /* The const variants of mspabi shifts have larger code size than the
+	 generic version, so use the generic version if optimizing for
+	 size.  */
+      msp430_expand_helper (operands, \"__mspabi_slli\", !optimize_size);
     DONE;
   }
 )
@@ -815,7 +818,7 @@
 	(ashift:SI (match_operand:SI 1 "general_operand")
 		   (match_operand:SI 2 "general_operand")))]
   ""
-  "msp430_expand_helper (operands, \"__mspabi_slll\", true);
+  "msp430_expand_helper (operands, \"__mspabi_slll\", !optimize_size);
    DONE;"
 )
 
@@ -842,7 +845,7 @@
 	     && INTVAL (operands[2]) == 1)
       emit_insn (gen_srai_1 (operands[0], operands[1]));
     else		 
-       msp430_expand_helper (operands, \"__mspabi_srai\", true);
+       msp430_expand_helper (operands, \"__mspabi_srai\", !optimize_size);
    DONE;
    }
 )
@@ -904,7 +907,7 @@
 	(ashiftrt:SI (match_operand:SI 1 "general_operand")
 		     (match_operand:SI 2 "general_operand")))]
   ""
-  "msp430_expand_helper (operands, \"__mspabi_sral\", true);
+  "msp430_expand_helper (operands, \"__mspabi_sral\", !optimize_size);
    DONE;"
 )
 
@@ -931,7 +934,7 @@
 	     && INTVAL (operands[2]) == 1)
       emit_insn (gen_srli_1 (operands[0], operands[1]));
     else		 
-      msp430_expand_helper (operands, \"__mspabi_srli\", true);
+      msp430_expand_helper (operands, \"__mspabi_srli\", !optimize_size);
     DONE;
   }
 )
@@ -983,7 +986,7 @@
 	(lshiftrt:SI (match_operand:SI 1 "general_operand")
 		     (match_operand:SI 2 "general_operand")))]
   ""
-  "msp430_expand_helper (operands, \"__mspabi_srll\", true);
+  "msp430_expand_helper (operands, \"__mspabi_srll\", !optimize_size);
    DONE;"
 )
 
diff --git a/gcc/testsuite/gcc.target/msp430/size-optimized-shifts.c b/gcc/testsuite/gcc.target/msp430/size-optimized-shifts.c
new file mode 100644
index 00000000000..be9509b86cc
--- /dev/null
+++ b/gcc/testsuite/gcc.target/msp430/size-optimized-shifts.c
@@ -0,0 +1,26 @@
+/* { dg-do compile } */
+/* { dg-options "-Os" } */
+/* { dg-final { scan-assembler-not "__mspabi_sral_4" } } */
+/* { dg-final { scan-assembler-not "__mspabi_srll_4" } } */
+/* { dg-final { scan-assembler-not "__mspabi_slll_4" } } */
+/* { dg-final { scan-assembler "__mspabi_sral" } } */
+/* { dg-final { scan-assembler "__mspabi_srll" } } */
+/* { dg-final { scan-assembler "__mspabi_slll" } } */
+
+/* Ensure that SImode shifts by a constant amount do not use the const_variant
+   of the shift library code when optimizing for size.  */
+
+long a;
+long b;
+long c;
+long d;
+unsigned long e;
+unsigned long f;
+
+void
+foo (void)
+{
+  a = b >> 4;
+  c = d << 4;
+  e = f >> 4;
+}
-- 
2.17.1

Reply via email to