From 4052c05e5b30fee0fb95a51e74e12a56dce29491 Mon Sep 17 00:00:00 2001 From: "H.J. Lu" Date: Wed, 15 Jul 2020 10:34:54 -0700 Subject: [PATCH] x86: Inline strncmp only with -minline-all-stringops 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 --git a/gcc/config/i386/i386-expand.c b/gcc/config/i386/i386-expand.c index bd83a8e..19a9f2d 100644 --- a/gcc/config/i386/i386-expand.c +++ b/gcc/config/i386/i386-expand.c @@ -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 index 0000000..231a478 --- /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 0000000..1a62044 --- /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" } } */ -- 2.7.4