Fix folding of memcmp("a", "a", 2) (PR
authorMartin Liska <mliska@suse.cz>
Wed, 9 Nov 2016 14:07:32 +0000 (15:07 +0100)
committerMartin Liska <marxin@gcc.gnu.org>
Wed, 9 Nov 2016 14:07:32 +0000 (14:07 +0000)
* fold-const-call.c (fold_const_call): Fix the folding.
* gcc.dg/tree-ssa/builtins-folding-generic.c (main): Add new
test-case for memcmp.
* gcc.dg/tree-ssa/builtins-folding-gimple.c: Likewise.

From-SVN: r242000

gcc/ChangeLog
gcc/fold-const-call.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/tree-ssa/builtins-folding-generic.c
gcc/testsuite/gcc.dg/tree-ssa/builtins-folding-gimple.c

index cfdf5a5569f5d2146f33ba20ffe5c5025e6b2253..9be098291be53cd5ea000578f7845a8086f97c95 100644 (file)
@@ -1,3 +1,7 @@
+2016-11-09  Martin Liska  <mliska@suse.cz>
+
+       * fold-const-call.c (fold_const_call): Fix the folding.
+
 2016-11-09  Richard Biener  <rguenther@suse.de>
 
        * common.opt (flag_evaluation_order): Remove.
index 1b3a755df3438f9f2c1d8dc2e6ba5c31192811c1..38f9717f139e4c5b1ae41a6e80cf2641676bcecd 100644 (file)
@@ -1506,7 +1506,7 @@ tree
 fold_const_call (combined_fn fn, tree type, tree arg0, tree arg1, tree arg2)
 {
   const char *p0, *p1;
-  size_t s2 = 0;
+  size_t s0, s1, s2 = 0;
   switch (fn)
     {
     case CFN_BUILT_IN_STRNCMP:
@@ -1538,11 +1538,11 @@ fold_const_call (combined_fn fn, tree type, tree arg0, tree arg1, tree arg2)
       }
     case CFN_BUILT_IN_BCMP:
     case CFN_BUILT_IN_MEMCMP:
-      if ((p0 = c_getstr (arg0))
-         && (p1 = c_getstr (arg1))
+      if ((p0 = c_getstr (arg0, &s0))
+         && (p1 = c_getstr (arg1, &s1))
          && host_size_t_cst_p (arg2, &s2)
-         && s2 <= strlen (p0)
-         && s2 <= strlen (p1))
+         && s2 <= s0
+         && s2 <= s1)
        return build_cmp_result (type, memcmp (p0, p1, s2));
       return NULL_TREE;
 
index 5da9220fe713ef0bc47a60a4be093dc8ffb9ee57..5fb53383b5d6780de4292ff0cb4d3ec345d46385 100644 (file)
@@ -1,3 +1,9 @@
+2016-11-09  Martin Liska  <mliska@suse.cz>
+
+       * gcc.dg/tree-ssa/builtins-folding-generic.c (main): Add new
+       test-case for memcmp.
+       * gcc.dg/tree-ssa/builtins-folding-gimple.c: Likewise.
+
 2016-11-09  Thomas Preud'homme  <thomas.preudhomme@arm.com>
 
        PR testsuite/78269
index 175feff7fec9b901a2c6b34c6d56a27e3a139a41..3c7e001691a4b67196b60de805a288001a998e75 100644 (file)
@@ -66,6 +66,10 @@ main (void)
   if (__builtin_strncasecmp ("aab", "aac", 2) != 0)
     __builtin_abort ();
 
+  /* MEMCMP.  */
+  if (__builtin_memcmp ("aaaaa", "aaaaa", 6) != 0)
+    __builtin_abort ();
+
   return 0;
 }
 
@@ -74,3 +78,4 @@ main (void)
 /* { dg-final { scan-tree-dump-not "__builtin_strncmp" "original" } } */
 /* { dg-final { scan-tree-dump-not "__builtin_strncasecmp" "original" } } */
 /* { dg-final { scan-tree-dump-not "__builtin_memchr" "original" } } */
+/* { dg-final { scan-tree-dump-not "__builtin_memcmp" "original" } } */
index 283bd1c70d2f11fa9c77bbe843fc1089f978f801..d08ced5e75865cc5531b0c7ccee1998517bf98ad 100644 (file)
@@ -63,6 +63,7 @@ main (void)
 
   const char *aaaaa = "aaaaa";
   const char *hello = "hello";
+  const char *hello2 = "hello";
   const char *empty = "";
   const char *ab = "ab";
   const char *ba = "ba";
@@ -151,6 +152,10 @@ main (void)
   if (__builtin_strncasecmp (++s2, ++s3+2, 0) != 0 || s2 != s1+1 || s3 != s1+5)
     __builtin_abort();
 
+  /* MEMCMP.  */
+  if (__builtin_memcmp (hello, hello2, 6) != 0)
+    __builtin_abort ();
+
   return 0;
 }
 
@@ -158,4 +163,5 @@ main (void)
 /* { dg-final { scan-tree-dump-not "__builtin_strcasecmp" "optimized" } } */
 /* { dg-final { scan-tree-dump-not "__builtin_strncmp" "optimized" } } */
 /* { dg-final { scan-tree-dump-not "__builtin_memchr" "optimized" } } */
+/* { dg-final { scan-tree-dump-not "__builtin_memcmp" "optimized" } } */
 /* { dg-final { scan-tree-dump-times "__builtin_strncasecmp" 3 "optimized" } } */