re PR middle-end/78809 (Inline strcmp with small constant strings)
authorQing Zhao <qing.zhao@oracle.com>
Fri, 17 Nov 2017 05:32:05 +0000 (05:32 +0000)
committerJeff Law <law@gcc.gnu.org>
Fri, 17 Nov 2017 05:32:05 +0000 (22:32 -0700)
2017-11-15  Qing Zhao <qing.zhao@oracle.com>

PR middle-end/78809
* gimple-fold.c (gimple_fold_builtin_string_compare): Add handling
of replacing call to strncmp with corresponding call to strcmp when
meeting conditions.

PR middle-end/78809
* gcc.dg/strcmpopt_1.c: New test.

From-SVN: r254856

gcc/ChangeLog
gcc/gimple-fold.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/strcmpopt_1.c [new file with mode: 0644]

index da8a9cea33b41a2042a08443a9e111324ee8654e..9352b48ccf3d1e223754cc9e1d4ff204486571b8 100644 (file)
@@ -1,3 +1,10 @@
+2017-11-15  Qing Zhao <qing.zhao@oracle.com>
+
+       PR middle-end/78809
+       * gimple-fold.c (gimple_fold_builtin_string_compare): Add handling
+       of replacing call to strncmp with corresponding call to strcmp when
+       meeting conditions.
+
 2017-11-17  Sergey Shalnov  <Sergey.Shalnov@intel.com>
 
        * config/i386/x86-tune.def (X86_TUNE_AVX256_OPTIMAL): Add tuning
index adb6f3baf933445d1a559e4b39014f355eb66402..1ed63833b732aa019a60c0ec6d499b4ac873a7ac 100644 (file)
@@ -2258,6 +2258,21 @@ gimple_fold_builtin_string_compare (gimple_stmt_iterator *gsi)
       return true;
     }
 
+  /* If length is larger than the length of one constant string, 
+     replace strncmp with corresponding strcmp */ 
+  if (fcode == BUILT_IN_STRNCMP 
+      && length > 0
+      && ((p2 && (size_t) length > strlen (p2)) 
+          || (p1 && (size_t) length > strlen (p1))))
+    {
+      tree fn = builtin_decl_implicit (BUILT_IN_STRCMP);
+      if (!fn)
+        return false;
+      gimple *repl = gimple_build_call (fn, 2, str1, str2);
+      replace_call_with_call_and_fold (gsi, repl);
+      return true;
+    }
+
   return false;
 }
 
index f9221894efd569a161d156edaf45bd7730bf0364..d597fe606ccb4349cd6ecb14af69091d7a3989c6 100644 (file)
@@ -1,3 +1,8 @@
+2017-11-15  Qing Zhao  <qing.zhao@oracle.com <mailto:qing.zhao@oracle.com>>
+
+       PR middle-end/78809
+       * gcc.dg/strcmpopt_1.c: New test.
+
 2017-11-16  Joseph Myers  <joseph@codesourcery.com>
 
        * gcc.dg/c18-version-1.c, gcc.dg/c18-version-2.c: New tests.
diff --git a/gcc/testsuite/gcc.dg/strcmpopt_1.c b/gcc/testsuite/gcc.dg/strcmpopt_1.c
new file mode 100644 (file)
index 0000000..40596a2
--- /dev/null
@@ -0,0 +1,28 @@
+/* { dg-do run } */
+/* { dg-options "-fdump-tree-gimple" } */
+
+#include <string.h>
+#include <stdlib.h>
+
+int cmp1 (char *p)
+{
+  return strncmp (p, "fis", 4);
+}
+int cmp2 (char *q)
+{
+  return strncmp ("fis", q, 4);
+}
+
+int main ()
+{
+
+  char *p = "fish";
+  char *q = "fis\0";
+
+  if (cmp1 (p) == 0 || cmp2 (q) != 0)
+    abort ();
+
+  return 0;
+}
+
+/* { dg-final { scan-tree-dump-times "strcmp \\(" 2 "gimple" } } */