diff mbox series

x86: Inline strncmp only with -minline-all-stringops

Message ID 20200715174227.2173721-1-hjl.tools@gmail.com
State New
Headers show
Series x86: Inline strncmp only with -minline-all-stringops | expand

Commit Message

H.J. Lu July 15, 2020, 5:42 p.m. UTC
Expand strncmp to "repz cmpsb" only with -minline-all-stringops since
"repz cmpsb" can be much slower than strncmp function implemented with
vector instructions, see

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=43052

gcc/

	PR target/95458
	* config/i386/i386-expand.c (ix86_expand_cmpstrn_or_cmpmem):
	Return false for -mno-inline-all-stringops.

gcc/testsuite/

	PR target/95458
	* gcc.target/i386/pr95458-1.c: New test.
	* gcc.target/i386/pr95458-2.c: Likewise.
---
 gcc/config/i386/i386-expand.c             | 19 +++++++------------
 gcc/testsuite/gcc.target/i386/pr95458-1.c | 11 +++++++++++
 gcc/testsuite/gcc.target/i386/pr95458-2.c |  7 +++++++
 3 files changed, 25 insertions(+), 12 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/i386/pr95458-1.c
 create mode 100644 gcc/testsuite/gcc.target/i386/pr95458-2.c
diff mbox series

Patch

diff --git a/gcc/config/i386/i386-expand.c b/gcc/config/i386/i386-expand.c
index a3a62e341b4..04f8c6ef394 100644
--- a/gcc/config/i386/i386-expand.c
+++ b/gcc/config/i386/i386-expand.c
@@ -7650,7 +7650,13 @@  bool
 ix86_expand_cmpstrn_or_cmpmem (rtx result, rtx src1, rtx src2,
 			       rtx length, rtx align, bool is_cmpstrn)
 {
-  if (optimize_insn_for_size_p () && !TARGET_INLINE_ALL_STRINGOPS)
+  /* Expand strncmp and memcmp only with -minline-all-stringops since
+     "repz cmpsb" can be much slower than strncmp and memcmp functions
+     implemented with vector instructions, see
+
+     https://gcc.gnu.org/bugzilla/show_bug.cgi?id=43052
+   */
+  if (!TARGET_INLINE_ALL_STRINGOPS)
     return false;
 
   /* Can't use this if the user has appropriated ecx, esi or edi.  */
@@ -7677,17 +7683,6 @@  ix86_expand_cmpstrn_or_cmpmem (rtx result, rtx src1, rtx src2,
 		    == STRING_CST))))
 	return false;
     }
-  else
-    {
-      /* Expand memcmp to "repz cmpsb" only for -minline-all-stringops
-	 since "repz cmpsb" can be much slower than memcmp function
-	 implemented with vector instructions, see
-
-	 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=43052
-       */
-      if (!TARGET_INLINE_ALL_STRINGOPS)
-	return false;
-    }
 
   rtx addr1 = copy_addr_to_reg (XEXP (src1, 0));
   rtx addr2 = copy_addr_to_reg (XEXP (src2, 0));
diff --git a/gcc/testsuite/gcc.target/i386/pr95458-1.c b/gcc/testsuite/gcc.target/i386/pr95458-1.c
new file mode 100644
index 00000000000..231a4787dce
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr95458-1.c
@@ -0,0 +1,11 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2 -minline-all-stringops" } */
+
+int
+func (char *d, unsigned int l)
+{
+  return __builtin_strncmp (d, "foo", l) ? 1 : 2;
+}
+
+/* { dg-final { scan-assembler-not "call\[\\t \]*_?strncmp" } } */
+/* { dg-final { scan-assembler "cmpsb" } } */
diff --git a/gcc/testsuite/gcc.target/i386/pr95458-2.c b/gcc/testsuite/gcc.target/i386/pr95458-2.c
new file mode 100644
index 00000000000..1a620444770
--- /dev/null
+++ b/gcc/testsuite/gcc.target/i386/pr95458-2.c
@@ -0,0 +1,7 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2 -mno-inline-all-stringops" } */
+
+#include "pr95458-1.c"
+
+/* { dg-final { scan-assembler "call\[\\t \]*_?strncmp" } } */
+/* { dg-final { scan-assembler-not "cmpsb" } } */