diff mbox

Aliasing: look through pointer's def stmt

Message ID alpine.DEB.2.02.1310251852480.9786@stedding.saclay.inria.fr
State New
Headers show

Commit Message

Marc Glisse Oct. 25, 2013, 5:02 p.m. UTC
On Fri, 25 Oct 2013, Richard Biener wrote:

> you can followup with handling POINTER_PLUS_EXPR if you like.

Like this? (bootstrap+testsuite on x86_64-unknown-linux-gnu)

2013-10-26  Marc Glisse  <marc.glisse@inria.fr>

gcc/
 	* tree-ssa-alias.c (ao_ref_init_from_ptr_and_size): Look for a
 	POINTER_PLUS_EXPR in the defining statement.

gcc/testsuite/
 	* gcc.dg/tree-ssa/alias-24.c: New file.

Comments

Marc Glisse Oct. 26, 2013, 5:07 p.m. UTC | #1
On Fri, 25 Oct 2013, Marc Glisse wrote:

> On Fri, 25 Oct 2013, Richard Biener wrote:
>
>> you can followup with handling POINTER_PLUS_EXPR if you like.
>
> Like this? (bootstrap+testsuite on x86_64-unknown-linux-gnu)
>
> 2013-10-26  Marc Glisse  <marc.glisse@inria.fr>
>
> gcc/
> 	* tree-ssa-alias.c (ao_ref_init_from_ptr_and_size): Look for a
> 	POINTER_PLUS_EXPR in the defining statement.

This has some issues with the type of the offsets. First, they might 
overflow in some extreme cases (that could probably already happen without 
the patch). But mostly, negative offsets are not supported. There is a 
comment to that effect before ao_ref_init_from_ptr_and_size, and 
ranges_overlap_p takes the offsets as unsigned HOST_WIDE_INT. Currently it 
does indeed seem hard to produce a negative offset there, but handling 
POINTER_PLUS_EXPR (definitely a good thing) would obviously change that.
Richard Biener Oct. 29, 2013, 9:22 a.m. UTC | #2
On Sat, Oct 26, 2013 at 7:07 PM, Marc Glisse <marc.glisse@inria.fr> wrote:
> On Fri, 25 Oct 2013, Marc Glisse wrote:
>
>> On Fri, 25 Oct 2013, Richard Biener wrote:
>>
>>> you can followup with handling POINTER_PLUS_EXPR if you like.
>>
>>
>> Like this? (bootstrap+testsuite on x86_64-unknown-linux-gnu)
>>
>> 2013-10-26  Marc Glisse  <marc.glisse@inria.fr>
>>
>> gcc/
>>         * tree-ssa-alias.c (ao_ref_init_from_ptr_and_size): Look for a
>>         POINTER_PLUS_EXPR in the defining statement.
>
>
> This has some issues with the type of the offsets. First, they might
> overflow in some extreme cases (that could probably already happen without
> the patch). But mostly, negative offsets are not supported. There is a
> comment to that effect before ao_ref_init_from_ptr_and_size, and
> ranges_overlap_p takes the offsets as unsigned HOST_WIDE_INT. Currently it
> does indeed seem hard to produce a negative offset there, but handling
> POINTER_PLUS_EXPR (definitely a good thing) would obviously change that.

For the POINTER_PLUS_EXPR offset argument you should use
int_cst_value () to access it (it will unconditionally sign-extend)
and use host_integerp (..., 0).  That leaves the overflow possibility
in place (and you should multiply by BITS_PER_UNIT) which we
ignore in enough other places similar to this to ignore ...
(side-note: for quite some time on my TODO is to make the
gimple alias-machinery use byte-offsets instead of bit-offsets
which wouldn't cause regressions if we finally lowered bitfield
accesses ...).  A way to not ignore it is to do

  off = double_int_from_tree (ptr-plus-offset);
  off = double_int_sext (off, TYPE_PRECISION (...))
  off = double_int_mul (off, BITS_PER_UNIT);
  if (off.fits_shwi ())
    ...

Richard.

>
> --
> Marc Glisse
diff mbox

Patch

Index: gcc/testsuite/gcc.dg/tree-ssa/alias-24.c
===================================================================
--- gcc/testsuite/gcc.dg/tree-ssa/alias-24.c	(revision 0)
+++ gcc/testsuite/gcc.dg/tree-ssa/alias-24.c	(working copy)
@@ -0,0 +1,13 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+void f (const char *c, int *i)
+{
+  *i = 42;
+  __builtin_memcpy (i + 1, c, sizeof (int));
+  if (*i != 42) __builtin_abort();
+}
+
+/* { dg-final { scan-tree-dump-not "abort" "optimized" } } */
+/* { dg-final { cleanup-tree-dump "optimized" } } */
+

Property changes on: gcc/testsuite/gcc.dg/tree-ssa/alias-24.c
___________________________________________________________________
Added: svn:keywords
## -0,0 +1 ##
+Author Date Id Revision URL
\ No newline at end of property
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Index: gcc/tree-ssa-alias.c
===================================================================
--- gcc/tree-ssa-alias.c	(revision 204071)
+++ gcc/tree-ssa-alias.c	(working copy)
@@ -567,20 +567,28 @@  void
 ao_ref_init_from_ptr_and_size (ao_ref *ref, tree ptr, tree size)
 {
   HOST_WIDE_INT t1, t2;
   ref->ref = NULL_TREE;
   if (TREE_CODE (ptr) == SSA_NAME)
     {
       gimple stmt = SSA_NAME_DEF_STMT (ptr);
       if (gimple_assign_single_p (stmt)
 	  && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
 	ptr = gimple_assign_rhs1 (stmt);
+      else if (is_gimple_assign (stmt)
+	       && gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
+	       && host_integerp (gimple_assign_rhs2 (stmt), 1))
+	{
+	  ao_ref_init_from_ptr_and_size (ref, gimple_assign_rhs1 (stmt), size);
+	  ref->offset += 8 * TREE_INT_CST_LOW (gimple_assign_rhs2 (stmt));
+	  return;
+	}
     }
 
   if (TREE_CODE (ptr) == ADDR_EXPR)
     ref->base = get_ref_base_and_extent (TREE_OPERAND (ptr, 0),
 					 &ref->offset, &t1, &t2);
   else
     {
       ref->base = build2 (MEM_REF, char_type_node,
 			  ptr, null_pointer_node);
       ref->offset = 0;