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] More address forward propagation and ARRAY_REF re-construction


This patch makes sure we re-construct array accesses from

  tmp_1 = &a[0] + off_1;

by folding such stmt during forwprop (and then possibly continuing
to propagate the result, &a[off'_2] in this case).  To make this
work fold_stmt (or rather maybe_fold_stmt_addition) needs to handle
the above case in a similar way as forwprops
forward_propagate_addr_into_variable_array_index, so this patch
extends it to do that.

Together with this patch the next one I am about to send will
finally fix the C frontend oddity of creating non-GIMPLE &ARRAY
addresses.

I'm currently bootstrapping and testing both patches together
but will try this one alone and commit it if it succeeds.

Richard.

2009-04-06  Richard Guenther  <rguenther@suse.de>

	* tree-ssa-ccp.c (maybe_fold_stmt_addition): Move non-constant
	indices into an array reference if possible.
	* tree-ssa-forwprop.c (tree_ssa_forward_propagate_single_use_vars):
	Fold POINTER_PLUS_EXPR statements with invariant address.

	* gcc.dg/tree-ssa/ssa-ccp-25.c: New testcase.
	* gcc.dg/tree-ssa/ssa-ccp-26.c: Likewise.

Index: gcc/tree-ssa-ccp.c
===================================================================
*** gcc/tree-ssa-ccp.c.orig	2009-04-06 16:17:46.000000000 +0200
--- gcc/tree-ssa-ccp.c	2009-04-06 16:17:55.000000000 +0200
*************** maybe_fold_stmt_addition (tree res_type,
*** 2094,2107 ****
    tree ptd_type;
    tree t;
  
-   /* It had better be a constant.  */
-   if (TREE_CODE (op1) != INTEGER_CST)
-     return NULL_TREE;
    /* The first operand should be an ADDR_EXPR.  */
    if (TREE_CODE (op0) != ADDR_EXPR)
      return NULL_TREE;
    op0 = TREE_OPERAND (op0, 0);
  
    /* If the first operand is an ARRAY_REF, expand it so that we can fold
       the offset into it.  */
    while (TREE_CODE (op0) == ARRAY_REF)
--- 2094,2140 ----
    tree ptd_type;
    tree t;
  
    /* The first operand should be an ADDR_EXPR.  */
    if (TREE_CODE (op0) != ADDR_EXPR)
      return NULL_TREE;
    op0 = TREE_OPERAND (op0, 0);
  
+   /* It had better be a constant.  */
+   if (TREE_CODE (op1) != INTEGER_CST)
+     {
+       /* Or op0 should now be A[0] and the non-constant offset defined
+ 	 via a multiplication by the array element size.  */
+       if (TREE_CODE (op0) == ARRAY_REF
+ 	  && integer_zerop (TREE_OPERAND (op0, 1))
+ 	  && TREE_CODE (op1) == SSA_NAME
+ 	  && host_integerp (TYPE_SIZE_UNIT (TREE_TYPE (op0)), 1))
+ 	{
+ 	  gimple offset_def = SSA_NAME_DEF_STMT (op1);
+ 	  if (!is_gimple_assign (offset_def))
+ 	    return NULL_TREE;
+ 
+ 	  if (gimple_assign_rhs_code (offset_def) == MULT_EXPR
+ 	      && TREE_CODE (gimple_assign_rhs2 (offset_def)) == INTEGER_CST
+ 	      && tree_int_cst_equal (gimple_assign_rhs2 (offset_def),
+ 				     TYPE_SIZE_UNIT (TREE_TYPE (op0))))
+ 	    return build1 (ADDR_EXPR, res_type,
+ 			   build4 (ARRAY_REF, TREE_TYPE (op0),
+ 				   TREE_OPERAND (op0, 0),
+ 				   gimple_assign_rhs1 (offset_def),
+ 				   TREE_OPERAND (op0, 2),
+ 				   TREE_OPERAND (op0, 3)));
+ 	  else if (integer_onep (TYPE_SIZE_UNIT (TREE_TYPE (op0)))
+ 		   && gimple_assign_rhs_code (offset_def) != MULT_EXPR)
+ 	    return build1 (ADDR_EXPR, res_type,
+ 			   build4 (ARRAY_REF, TREE_TYPE (op0),
+ 				   TREE_OPERAND (op0, 0),
+ 				   op1,
+ 				   TREE_OPERAND (op0, 2),
+ 				   TREE_OPERAND (op0, 3)));
+ 	}
+       return NULL_TREE;
+     }
+ 
    /* If the first operand is an ARRAY_REF, expand it so that we can fold
       the offset into it.  */
    while (TREE_CODE (op0) == ARRAY_REF)
Index: gcc/testsuite/gcc.dg/tree-ssa/ssa-ccp-25.c
===================================================================
*** /dev/null	1970-01-01 00:00:00.000000000 +0000
--- gcc/testsuite/gcc.dg/tree-ssa/ssa-ccp-25.c	2009-04-06 16:17:55.000000000 +0200
***************
*** 0 ****
--- 1,14 ----
+ /* { dg-do compile } */
+ /* { dg-options "-O -fdump-tree-ccp1 -fdump-tree-forwprop1" } */
+ 
+ int a[256];
+ int foo(int i)
+ {
+   int *p = &a[0];
+   return *(p + i);
+ }
+ 
+ /* { dg-final { scan-tree-dump "&a\\\[D\\\." "ccp1" } } */
+ /* { dg-final { scan-tree-dump "= a\\\[D\\\." "forwprop1" } } */
+ /* { dg-final { cleanup-tree-dump "ccp1" } } */
+ /* { dg-final { cleanup-tree-dump "forwprop1" } } */
Index: gcc/tree-ssa-forwprop.c
===================================================================
*** gcc/tree-ssa-forwprop.c.orig	2009-04-06 16:18:15.000000000 +0200
--- gcc/tree-ssa-forwprop.c	2009-04-06 16:46:31.000000000 +0200
*************** tree_ssa_forward_propagate_single_use_va
*** 1256,1261 ****
--- 1256,1270 ----
  		  else
  		    gsi_next (&gsi);
  		}
+ 	      else if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
+ 		       && is_gimple_min_invariant (rhs))
+ 		{
+ 		  /* Make sure to fold &a[0] + off_1 here.  */
+ 		  fold_stmt_inplace (stmt);
+ 		  update_stmt (stmt);
+ 		  if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
+ 		    gsi_next (&gsi);
+ 		}
  	      else if ((gimple_assign_rhs_code (stmt) == BIT_NOT_EXPR
  		        || gimple_assign_rhs_code (stmt) == NEGATE_EXPR)
  		       && TREE_CODE (rhs) == SSA_NAME)
Index: gcc/testsuite/gcc.dg/tree-ssa/ssa-ccp-26.c
===================================================================
*** /dev/null	1970-01-01 00:00:00.000000000 +0000
--- gcc/testsuite/gcc.dg/tree-ssa/ssa-ccp-26.c	2009-04-06 16:18:56.000000000 +0200
***************
*** 0 ****
--- 1,11 ----
+ /* { dg-do compile } */
+ /* { dg-options "-O -fdump-tree-forwprop1" } */
+ 
+ int a[256];
+ int foo(int i)
+ {
+   return (a + 1)[i];
+ }
+ 
+ /* { dg-final { scan-tree-dump "= a\\\[D\\\." "forwprop1" } } */
+ /* { dg-final { cleanup-tree-dump "forwprop1" } } */


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