re PR middle-end/50604 (verify_gimple failed: type mismatch in binary expression)
authorJakub Jelinek <jakub@redhat.com>
Tue, 4 Oct 2011 15:25:53 +0000 (17:25 +0200)
committerJakub Jelinek <jakub@gcc.gnu.org>
Tue, 4 Oct 2011 15:25:53 +0000 (17:25 +0200)
PR tree-optimization/50604
* builtins.c (fold_builtin_strcpy, fold_builtin_stpcpy,
fold_builtin_strncpy, fold_builtin_stxcpy_chk): Ensure
last argument to memcpy has size_type_node type instead of
ssizetype.
* tree-ssa-strlen.c (handle_builtin_memcpy): Use size_type_node
instead of TREE_TYPE (len) as type for newlen.

* gcc.dg/pr50604.c: New test.

From-SVN: r179508

gcc/ChangeLog
gcc/builtins.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/pr50604.c [new file with mode: 0644]
gcc/tree-ssa-strlen.c

index b6113219d6b8fd673b2372b5b586577fb8a38f41..2f4d9a6fbe1785735c29efd47aaaaf49fcaa4f47 100644 (file)
@@ -1,5 +1,13 @@
 2011-10-04  Jakub Jelinek  <jakub@redhat.com>
 
+       PR tree-optimization/50604
+       * builtins.c (fold_builtin_strcpy, fold_builtin_stpcpy,
+       fold_builtin_strncpy, fold_builtin_stxcpy_chk): Ensure
+       last argument to memcpy has size_type_node type instead of
+       ssizetype.
+       * tree-ssa-strlen.c (handle_builtin_memcpy): Use size_type_node
+       instead of TREE_TYPE (len) as type for newlen.
+
        PR tree-optimization/50522
        * tree-ssa-alias.c (ptr_deref_may_alias_decl_p): Don't test
        TYPE_RESTRICT.
index b79ce6f4a7479fb7dba887d53de05c923009eb1c..3055927f369c928c87c42d06f1c1d8c2ec47e8d5 100644 (file)
@@ -8288,7 +8288,8 @@ fold_builtin_strcpy (location_t loc, tree fndecl, tree dest, tree src, tree len)
        return NULL_TREE;
     }
 
-  len = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
+  len = fold_convert_loc (loc, size_type_node, len);
+  len = size_binop_loc (loc, PLUS_EXPR, len, build_int_cst (size_type_node, 1));
   return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
                           build_call_expr_loc (loc, fn, 3, dest, src, len));
 }
@@ -8319,7 +8320,9 @@ fold_builtin_stpcpy (location_t loc, tree fndecl, tree dest, tree src)
   if (!fn)
     return NULL_TREE;
 
-  lenp1 = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
+  lenp1 = size_binop_loc (loc, PLUS_EXPR,
+                         fold_convert_loc (loc, size_type_node, len),
+                         build_int_cst (size_type_node, 1));
   /* We use dest twice in building our expression.  Save it from
      multiple expansions.  */
   dest = builtin_save_expr (dest);
@@ -8375,6 +8378,8 @@ fold_builtin_strncpy (location_t loc, tree fndecl, tree dest,
   fn = implicit_built_in_decls[BUILT_IN_MEMCPY];
   if (!fn)
     return NULL_TREE;
+
+  len = fold_convert_loc (loc, size_type_node, len);
   return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
                           build_call_expr_loc (loc, fn, 3, dest, src, len));
 }
@@ -12127,7 +12132,9 @@ fold_builtin_stxcpy_chk (location_t loc, tree fndecl, tree dest,
              if (!fn)
                return NULL_TREE;
 
-             len = size_binop_loc (loc, PLUS_EXPR, len, ssize_int (1));
+             len = fold_convert_loc (loc, size_type_node, len);
+             len = size_binop_loc (loc, PLUS_EXPR, len,
+                                   build_int_cst (size_type_node, 1));
              return fold_convert_loc (loc, TREE_TYPE (TREE_TYPE (fndecl)),
                                       build_call_expr_loc (loc, fn, 4,
                                                        dest, src, len, size));
index baa2b6308a21babfc1b7598c131f3b7b1d98bd8d..3d1372fe443a33766070c34e42d3ddf3364775ca 100644 (file)
@@ -1,3 +1,8 @@
+2011-10-04  Jakub Jelinek  <jakub@redhat.com>
+
+       PR tree-optimization/50604
+       * gcc.dg/pr50604.c: New test.
+
 2011-10-04  Rainer Orth  <ro@CeBiTec.Uni-Bielefeld.DE>
 
        PR tree-optimization/49662
diff --git a/gcc/testsuite/gcc.dg/pr50604.c b/gcc/testsuite/gcc.dg/pr50604.c
new file mode 100644 (file)
index 0000000..1ab465a
--- /dev/null
@@ -0,0 +1,19 @@
+/* PR tree-optimization/50604 */
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+
+#include "strlenopt.h"
+
+typedef char T;
+extern const T s[];
+
+void
+foo (T *x)
+{
+  char *r = malloc (strlen (x));
+  strcpy (r, s);
+  strcat (r, x);
+  strcat (r, "/");
+}
+
+const T s[] = "abcdefghijklmnopq";
index 71cefaaabc875a9988d23e3069d3a3c19fff848a..b2ee2f96f44b34c18a7e4b68228a10f48e6da808 100644 (file)
@@ -1297,7 +1297,7 @@ handle_builtin_memcpy (enum built_in_function bcode, gimple_stmt_iterator *gsi)
   if (si != NULL)
     newlen = si->length;
   else
-    newlen = build_int_cst (TREE_TYPE (len), ~idx);
+    newlen = build_int_cst (size_type_node, ~idx);
   oldlen = NULL_TREE;
   if (olddsi != NULL)
     {