diff mbox series

[committed] correct handling of anti-ranges in -Warray-bounds for built-ins (PR 83446)

Message ID 355f9955-012c-f21d-b76c-20ef501f1cdf@gmail.com
State New
Headers show
Series [committed] correct handling of anti-ranges in -Warray-bounds for built-ins (PR 83446) | expand

Commit Message

Martin Sebor Dec. 18, 2017, 3:37 a.m. UTC
I checked in the attached patch to restore i686 bootstrap due
to a -Warray-bounds false positive triggered by the -Wrestrict
enhancement committed yesterday in r255758.

Martin
diff mbox series

Patch

PR bootstrap/83446 - Bootstrap failed on i686

gcc/testsuite/ChangeLog:

	PR bootstrap/83446
	* c-c++-common/Warray-bounds-3.c: Adjust.
	* gcc.dg/Warray-bounds-25.c: New test.

gcc/ChangeLog:

	PR bootstrap/83446
	* gimple-ssa-warn-restrict.c
	(builtin_memref::offset_out_of_bounds): Correct the handling of
	anti-ranges.

Index: gcc/gimple-ssa-warn-restrict.c
===================================================================
--- gcc/gimple-ssa-warn-restrict.c	(revision 255771)
+++ gcc/gimple-ssa-warn-restrict.c	(working copy)
@@ -413,7 +413,9 @@  builtin_memref::offset_out_of_bounds (int strict,
 
   if (DECL_P (base) && TREE_CODE (TREE_TYPE (base)) == ARRAY_TYPE)
     {
-      if (offrng[1] < offrng[0])
+      /* Check for offset in an anti-range with a negative lower bound.
+	 For such a range, consider only the non-negative subrange.  */
+      if (offrng[1] < offrng[0] && offrng[1] < 0)
   	offrng[1] = maxobjsize;
     }
 
Index: gcc/testsuite/c-c++-common/Warray-bounds-3.c
===================================================================
--- gcc/testsuite/c-c++-common/Warray-bounds-3.c	(revision 255771)
+++ gcc/testsuite/c-c++-common/Warray-bounds-3.c	(working copy)
@@ -123,8 +123,11 @@  void test_memcpy_bounds_anti_range (char *d, const
      (yet).  */
   T (char, 9, a, a + SAR ( 1,  6), 3);   /* { dg-warning "forming offset \\\[9, 0] is out of the bounds \\\[0, 9] of object " "memcpy" { xfail *-*-* } } */
 
-  T (char, 9, a, a + SAR ( 2,  6), 3);   /* { dg-warning "forming offset 10 is out of the bounds \\\[0, 9] of object " "memcpy" } */
-  T (char, 9, a, a + SAR ( 3,  6), 3);   /* { dg-warning "forming offset 10 is out of the bounds \\\[0, 9] of object " "memcpy" } */
+  /* The range of offsets is the union of [0, 1] and [7, PTRDIFF_MAX]
+     of which the first subrange is valid and thus no warming for memcpy
+     is issued.  Similarly for the next test.  */
+  T (char, 9, a, a + SAR ( 2,  6), 3);
+  T (char, 9, a, a + SAR ( 3,  6), 3);
 
   T (char, 9, a, a + SAR (-1,  7), 3);   /* { dg-warning "forming offset \\\[10, 11] is out of the bounds \\\[0, 9] of object " "memcpy" } */
   T (char, 9, a, a + SAR (-2,  8), 3);   /* { dg-warning "forming offset \\\[10, 12] is out of the bounds \\\[0, 9] of object " "memcpy" } */
Index: gcc/testsuite/gcc.dg/Warray-bounds-25.c
===================================================================
--- gcc/testsuite/gcc.dg/Warray-bounds-25.c	(nonexistent)
+++ gcc/testsuite/gcc.dg/Warray-bounds-25.c	(working copy)
@@ -0,0 +1,33 @@ 
+/* PR tree-optimization/83446 - Bootstrap failed on i686
+   { dg-do compile }
+   { dg-options "-O2 -Warray-bounds" } */
+
+char a[4];
+
+void f0i (void *d, int n)
+{
+  if (n < 0) n = 0;
+
+  __builtin_memcpy (d, a + sizeof a - n, n);
+}
+
+void f0L (void *d, long n)
+{
+  if (n < 0) n = 0;
+
+  __builtin_memcpy (d, a + sizeof a - n, n);
+}
+
+void f0u (void *d, unsigned n)
+{
+  if (n < 0) n = 1;
+
+  __builtin_memcpy (d, a + sizeof a - n, n);   /* { dg-bogus "\\\[-Warray-bounds" } */
+}
+
+void f1lu (void *d, unsigned long n)
+{
+  if (n < 1) n = 1;
+
+  __builtin_memcpy (d, a + sizeof a - n, n);   /* { dg-bogus "\\\[-Warray-bounds" } */
+}