]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Backport r272993
authorMartin Liska <mliska@suse.cz>
Thu, 4 Jul 2019 09:03:56 +0000 (11:03 +0200)
committerMartin Liska <marxin@gcc.gnu.org>
Thu, 4 Jul 2019 09:03:56 +0000 (09:03 +0000)
2019-07-04  Martin Liska  <mliska@suse.cz>

Backport from mainline
2019-07-03  Martin Liska  <mliska@suse.cz>

PR tree-optimization/90892
* builtins.c (inline_expand_builtin_string_cmp): Handle '\0'
in string constants.
2019-07-04  Martin Liska  <mliska@suse.cz>

Backport from mainline
2019-07-03  Martin Liska  <mliska@suse.cz>

PR tree-optimization/90892
* gcc.dg/pr90892.c: New test.

From-SVN: r273074

gcc/ChangeLog
gcc/builtins.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/pr90892.c [new file with mode: 0644]

index ae633153aa8fb1f4c60a6f97f8a689376534e30e..82010817b6aeacb64beea62ae557338ead8d6644 100644 (file)
@@ -1,3 +1,12 @@
+2019-07-04  Martin Liska  <mliska@suse.cz>
+
+       Backport from mainline
+       2019-07-03  Martin Liska  <mliska@suse.cz>
+
+       PR tree-optimization/90892
+       * builtins.c (inline_expand_builtin_string_cmp): Handle '\0'
+       in string constants.
+
 2019-07-04  Martin Liska  <mliska@suse.cz>
 
        Backport from mainline
index d37d73fc4a0fdd9ff0883ea3adfb385775a8a684..9bcb310c0150e216d1585b0ba7be1569bf44e46e 100644 (file)
@@ -7124,8 +7124,19 @@ inline_expand_builtin_string_cmp (tree exp, rtx target)
     return NULL_RTX;
 
   /* For strncmp, if the length is not a const, not qualify.  */
-  if (is_ncmp && !tree_fits_uhwi_p (len3_tree))
-    return NULL_RTX;
+  if (is_ncmp)
+    {
+      if (!tree_fits_uhwi_p (len3_tree))
+       return NULL_RTX;
+      else
+       len3 = tree_to_uhwi (len3_tree);
+    }
+
+  if (src_str1 != NULL)
+    len1 = strnlen (src_str1, len1) + 1;
+
+  if (src_str2 != NULL)
+    len2 = strnlen (src_str2, len2) + 1;
 
   int const_str_n = 0;
   if (!len1)
@@ -7140,7 +7151,7 @@ inline_expand_builtin_string_cmp (tree exp, rtx target)
   gcc_checking_assert (const_str_n > 0);
   length = (const_str_n == 1) ? len1 : len2;
 
-  if (is_ncmp && (len3 = tree_to_uhwi (len3_tree)) < length)
+  if (is_ncmp && len3 < length)
     length = len3;
 
   /* If the length of the comparision is larger than the threshold,
index 2a42f3c15b066341338808a642d6682b9e72de0d..07b2fa79dbf57365cee02bbb97fbea03fd4c9ee7 100644 (file)
@@ -1,3 +1,11 @@
+2019-07-04  Martin Liska  <mliska@suse.cz>
+
+       Backport from mainline
+       2019-07-03  Martin Liska  <mliska@suse.cz>
+
+       PR tree-optimization/90892
+       * gcc.dg/pr90892.c: New test.
+
 2019-07-04  Martin Liska  <mliska@suse.cz>
 
        Backport from mainline
diff --git a/gcc/testsuite/gcc.dg/pr90892.c b/gcc/testsuite/gcc.dg/pr90892.c
new file mode 100644 (file)
index 0000000..e4b5310
--- /dev/null
@@ -0,0 +1,14 @@
+/* PR tree-optimization/90892 */
+/* { dg-do run } */
+/* { dg-options "-O2" } */
+
+const char *a = "A\0b";
+
+int
+main()
+{
+  if (__builtin_strncmp(a, "A\0", 2) != 0)
+    __builtin_abort ();
+
+  return 0;
+}