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 memmove to memcpy folding (PR middle-end/71874)


Hi!

As mentioned in the PR and discussed on IRC, get_ref_base_and_extent
can return size != maxsize or maxsize -1 and then we really can't trust
the offset for the purposes we want.  So this patch instead uses a different
function that just computes the base and offset if the offset is constant;
we don't really care about the access size.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk/6.2,
for 5.5 the 4.9 patch applied to gimple-fold.c instead of bultins.c and for
4.9.4 the attached patch?

2016-07-19  Jakub Jelinek  <jakub@redhat.com>

	PR middle-end/71874
	* gimple-fold.c (fold_builtin_memory_op): Use
	get_addr_base_and_unit_offset instead of get_ref_base_and_extent.

	* g++.dg/torture/pr71874.C: New test.

--- gcc/gimple-fold.c.jj	2016-07-19 15:24:42.870004660 +0200
+++ gcc/gimple-fold.c	2016-07-19 15:26:55.449343027 +0200
@@ -796,22 +796,21 @@ gimple_fold_builtin_memory_op (gimple_st
 	    {
 	      tree src_base, dest_base, fn;
 	      HOST_WIDE_INT src_offset = 0, dest_offset = 0;
-	      HOST_WIDE_INT size = -1;
-	      HOST_WIDE_INT maxsize = -1;
-	      bool reverse;
+	      HOST_WIDE_INT maxsize;
 
 	      srcvar = TREE_OPERAND (src, 0);
-	      src_base = get_ref_base_and_extent (srcvar, &src_offset,
-						  &size, &maxsize, &reverse);
+	      src_base = get_addr_base_and_unit_offset (srcvar, &src_offset);
+	      if (src_base == NULL)
+		src_base = srcvar;
 	      destvar = TREE_OPERAND (dest, 0);
-	      dest_base = get_ref_base_and_extent (destvar, &dest_offset,
-						   &size, &maxsize, &reverse);
+	      dest_base = get_addr_base_and_unit_offset (destvar,
+							 &dest_offset);
+	      if (dest_base == NULL)
+		dest_base = destvar;
 	      if (tree_fits_uhwi_p (len))
 		maxsize = tree_to_uhwi (len);
 	      else
 		maxsize = -1;
-	      src_offset /= BITS_PER_UNIT;
-	      dest_offset /= BITS_PER_UNIT;
 	      if (SSA_VAR_P (src_base)
 		  && SSA_VAR_P (dest_base))
 		{
--- gcc/testsuite/g++.dg/torture/pr71874.C.jj	2016-07-19 15:23:37.097832377 +0200
+++ gcc/testsuite/g++.dg/torture/pr71874.C	2016-07-19 15:23:37.097832377 +0200
@@ -0,0 +1,12 @@
+// PR middle-end/71874
+// { dg-do run }
+
+int
+main ()
+{
+  char str[] = "abcdefghijklmnopqrstuvwxyzABCDEF";
+  __builtin_memmove (str + 20, str + 15, 11);
+  if (__builtin_strcmp (str, "abcdefghijklmnopqrstpqrstuvwxyzF") != 0)
+    __builtin_abort ();
+  return 0;
+}

	Jakub

Attachment: gcc49-pr71874.patch
Description: Text document


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