public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH PR45098] Disallow NULL pointer in pointer arithmetic
@ 2011-06-16  6:42 Tom de Vries
  2011-06-16  6:51 ` Tom de Vries
                   ` (2 more replies)
  0 siblings, 3 replies; 22+ messages in thread
From: Tom de Vries @ 2011-06-16  6:42 UTC (permalink / raw)
  To: Zdenek Dvorak; +Cc: gcc-patches

[-- Attachment #1: Type: text/plain, Size: 1590 bytes --]

Hi,

Consider the following example.

extern unsigned int foo (int*) __attribute__((pure));
unsigned int
tr (int array[], int n)
{
  unsigned int i;
  unsigned int sum = 0;
  for (i = 0; i < n; i++)
    sum += foo (&array[i]);
  return sum;
}

For 32-bit pointers, the analysis in infer_loop_bounds_from_pointer_arith
currently concludes that the range of valid &array[i] is &array[0x0] to
&array[0x3fffffff], meaning 0x40000000 distinct values.
This implies that i < n is executed at most 0x40000001 times, and i < n
cannot be eliminated by an 32-bit iterator with step 4, since that one has
only 0x40000000 distinct values.

The patch reasons that NULL cannot be used or produced by pointer
arithmetic, and that we can exclude the possibility of the NULL pointer in the
range. So the range of valid &array[i] is &array[0] to &array[0x3ffffffe],
meaning 0x3fffffff distinct values.
This implies that i < n is executed at most 0x40000000 times and i < n can be
eliminated.

The patch implements this new limitation by changing the (low, high, step)
triplet in infer_loop_bounds_from_pointer_arith from (0x0, 0xffffffff, 0x4)
to (0x4, 0xffffffff, 0x4).

I'm not too happy about the test for C-like language: ptrdiff_type_node !=
NULL_TREE, but I'm not sure how else to test for this.

Bootstrapped and reg-tested on x86_64.

I will sent the adapted test cases in a separate email.

OK for trunk?

Thanks,
- Tom

2011-06-15  Tom de Vries  <tom@codesourcery.com>

	PR target/45098
	* tree-ssa-loop-niter.c (infer_loop_bounds_from_pointer_arith): Disallow
	NULL pointer for pointer arithmetic.

[-- Attachment #2: 14_pr45098-null.patch --]
[-- Type: text/x-patch, Size: 998 bytes --]

diff -u gcc/tree-ssa-loop-niter.c (working copy) gcc/tree-ssa-loop-niter.c (working copy)
--- gcc/tree-ssa-loop-niter.c (working copy)
+++ gcc/tree-ssa-loop-niter.c (working copy)
@@ -2875,6 +2875,16 @@
   low = lower_bound_in_type (type, type);
   high = upper_bound_in_type (type, type);
 
+  /* In C, pointer arithmetic p + 1 cannot use a NULL pointer, and p - 1 cannot
+     produce a NULL pointer.  The contrary would mean NULL points to an object,
+     while NULL is supposed to compare unequal with the address of all objects.
+     Furthermore, p + 1 cannot produce a NULL pointer and p - 1 cannot use a
+     NULL pointer since that would mean wrapping, which we assume here not to
+     happen.  So, we can exclude NULL from the valid range of pointer
+     arithmetic.  */
+  if (ptrdiff_type_node != NULL_TREE && int_cst_value (low) == 0)
+    low = fold_build2 (PLUS_EXPR, TREE_TYPE (low), low, step);
+
   record_nonwrapping_iv (loop, base, step, stmt, low, high, false, true);
 }
 

^ permalink raw reply	[flat|nested] 22+ messages in thread

end of thread, other threads:[~2011-06-20 13:29 UTC | newest]

Thread overview: 22+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-06-16  6:42 [PATCH PR45098] Disallow NULL pointer in pointer arithmetic Tom de Vries
2011-06-16  6:51 ` Tom de Vries
2011-06-16  7:34 ` Zdenek Dvorak
2011-06-16 12:22   ` Tom de Vries
2011-06-16 15:33     ` Zdenek Dvorak
2011-06-16 15:42       ` Richard Guenther
2011-06-16 15:54         ` Zdenek Dvorak
2011-06-16 18:10           ` Tom de Vries
2011-06-16 22:03 ` Jeff Law
2011-06-17 10:44   ` Tom de Vries
2011-06-17 10:56     ` Richard Guenther
2011-06-17 10:57       ` Zdenek Dvorak
2011-06-17 11:13         ` Richard Guenther
2011-06-17 11:22           ` Zdenek Dvorak
2011-06-17 13:01             ` Richard Guenther
2011-06-17 14:57               ` Zdenek Dvorak
2011-06-17 18:24                 ` Jeff Law
2011-06-20 11:06                   ` Richard Guenther
2011-06-20 12:26                     ` Zdenek Dvorak
2011-06-20 12:41                       ` Zdenek Dvorak
2011-06-20 13:29                         ` Richard Guenther
2011-06-20 13:35                           ` Michael Matz

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).