x86: Inline strncmp only with -minline-all-stringops
authorH.J. Lu <hjl.tools@gmail.com>
Wed, 15 Jul 2020 17:34:54 +0000 (10:34 -0700)
committerH.J. Lu <hjl.tools@gmail.com>
Mon, 26 Oct 2020 11:15:47 +0000 (04:15 -0700)
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
gcc/testsuite/gcc.target/i386/pr95458-1.c [new file with mode: 0644]
gcc/testsuite/gcc.target/i386/pr95458-2.c [new file with mode: 0644]

index bd83a8e..19a9f2d 100644 (file)
@@ -7679,7 +7679,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.  */
@@ -7706,17 +7712,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 (file)
index 0000000..231a478
--- /dev/null
@@ -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 (file)
index 0000000..1a62044
--- /dev/null
@@ -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" } } */