This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Fix ICE with strlen optimization (PR tree-optimization/50604)


Hi!

The following testcase ICEs on the trunk, as strlen optimization was
assuming memcpy arguments will have expected type (size_type_node),
but they had ssizetype instead.  The following patch fixes it
both in the builtins.c folders that create memcpy and also in the
strlen pass to no longer assume that.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

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.

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

--- gcc/builtins.c.jj	2011-08-26 18:41:44.000000000 +0200
+++ gcc/builtins.c	2011-10-04 09:50:11.000000000 +0200
@@ -8288,7 +8288,8 @@ fold_builtin_strcpy (location_t loc, tre
 	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, tre
   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, tr
   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,
 	      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));
--- gcc/tree-ssa-strlen.c.jj	2011-09-29 14:25:46.000000000 +0200
+++ gcc/tree-ssa-strlen.c	2011-10-04 09:52:50.000000000 +0200
@@ -1297,7 +1297,7 @@ handle_builtin_memcpy (enum built_in_fun
   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)
     {
--- gcc/testsuite/gcc.dg/pr50604.c.jj	2011-10-04 09:55:38.000000000 +0200
+++ gcc/testsuite/gcc.dg/pr50604.c	2011-10-04 09:53:39.000000000 +0200
@@ -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";

	Jakub


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]