diff mbox series

[committed] avoid -Wrestrict on structs copies with unknown offset and constant size (PR 86196)

Message ID 6351f73d-a756-3865-7d80-e274b3fe0a15@gmail.com
State New
Headers show
Series [committed] avoid -Wrestrict on structs copies with unknown offset and constant size (PR 86196) | expand

Commit Message

Martin Sebor Dec. 11, 2018, 1:23 a.m. UTC
A fix for pr85753 I committed back in may introduced a subtle
logic error that cause false positives for copies involving
unknown offsets into non-array objects.  The attached patch
corrects this mistake.  I have committed it as obvious in
r266967 after verifying it on x86_64-linux.  The committed patch
is attached.

Martin
diff mbox series

Patch

PR tree-optimization/86196 - Bogus -Wrestrict on memcpy between array elements at unequal indices

gcc/ChangeLog:

	PR tree-optimization/86196
	* gimple-ssa-warn-restrict.c (builtin_memref::builtin_memref): Use
	base size only of arrays.

gcc/testsuite/ChangeLog:

	PR tree-optimization/86196
	* gcc.dg/Wrestrict-18.c: New test.

Index: gcc/gimple-ssa-warn-restrict.c
===================================================================
--- gcc/gimple-ssa-warn-restrict.c	(revision 266963)
+++ gcc/gimple-ssa-warn-restrict.c	(working copy)
@@ -272,15 +272,16 @@  builtin_memref::builtin_memref (tree expr, tree si
 
   offset_int maxoff = maxobjsize;
   tree basetype = TREE_TYPE (base);
-  if (TREE_CODE (basetype) == ARRAY_TYPE
-      && ref
-      && array_at_struct_end_p (ref))
-    ;   /* Use the maximum possible offset for last member arrays.  */
-  else if (tree basesize = TYPE_SIZE_UNIT (basetype))
-    if (TREE_CODE (basesize) == INTEGER_CST)
-      /* Size could be non-constant for a variable-length type such
-	 as a struct with a VLA member (a GCC extension).  */
-      maxoff = wi::to_offset (basesize);
+  if (TREE_CODE (basetype) == ARRAY_TYPE)
+    {
+      if (ref && array_at_struct_end_p (ref))
+	;   /* Use the maximum possible offset for last member arrays.  */
+      else if (tree basesize = TYPE_SIZE_UNIT (basetype))
+	if (TREE_CODE (basesize) == INTEGER_CST)
+	  /* Size could be non-constant for a variable-length type such
+	     as a struct with a VLA member (a GCC extension).  */
+	  maxoff = wi::to_offset (basesize);
+    }
 
   if (offrange[0] >= 0)
     {
Index: gcc/testsuite/gcc.dg/Wrestrict-18.c
===================================================================
--- gcc/testsuite/gcc.dg/Wrestrict-18.c	(nonexistent)
+++ gcc/testsuite/gcc.dg/Wrestrict-18.c	(working copy)
@@ -0,0 +1,37 @@ 
+/* PR tree-optimization/86196 - Bogus -Wrestrict on memcpy between array
+   elements at unequal indices
+   { dg-do compile }
+   { dg-options "-O2 -Wall" } */
+
+typedef __SIZE_TYPE__ size_t;
+
+extern void* memcpy (void*, const void*, size_t);
+
+struct S
+{
+  int n;
+  void * p;
+};
+
+/* Test case submitted in the PR.  */
+
+void pr86196_c0 (struct S * a, size_t n)
+{
+  for (size_t i = 0, j = 0; i != n; ++i)
+    {
+      if (a[i].n == 0)
+	{
+	  if (i != j)
+	    memcpy (&a[j], &a[i], sizeof (struct S));   /* { dg-bogus "\\\[-Wrestrict" } */
+	  ++j;
+	}
+    }
+}
+
+/* Reduced test case.  */
+
+void pr86196_c1 (struct S *a, int i, int j)
+{
+  if (i != j)
+    memcpy (&a[j], &a[i], sizeof (struct S));   /* { dg-bogus "\\\[-Wrestrict" } */
+}