[PATCH] Fix forward propagating of &A[cst]

Richard Guenther rguenther@suse.de
Sun Mar 29 17:21:00 GMT 2009


Forward propagating of &A[cst] has some issues, first, propagating
into ptr +p CST is non-functional because we pass the wrong tree to
maybe_fold_stmt_addition which expects the address, not the array access,
second if it triggers we need to unshare the resulting tree to not
ICE later.

Second, we can propagate &A[cst] for any constant cst in the ptr +p CST
case, just not in the ptr +p OFF case.

Fixed with the following, which also adds testcases for both fixed
cases.

Bootstrap and regtest running on x86_64-unknown-linux-gnu, I will apply
this once it succeeded.

Richard.

2009-03-29  Richard Guenther  <rguenther@suse.de>

	* tree-ssa-forwprop.c (forward_propagate_addr_expr_1): Properly
	propagate addresses of array references.

	* gcc.dg/tree-ssa/forwprop-11.c: New testcase.

Index: gcc/testsuite/gcc.dg/tree-ssa/forwprop-11.c
===================================================================
*** gcc/testsuite/gcc.dg/tree-ssa/forwprop-11.c	(revision 0)
--- gcc/testsuite/gcc.dg/tree-ssa/forwprop-11.c	(revision 0)
***************
*** 0 ****
--- 1,19 ----
+ /* { dg-do compile } */
+ /* { dg-options "-O -fdump-tree-forwprop1" } */
+ 
+ int f(int *p, int n)
+ {
+   int (*a)[n] = (int (*)[n])p;
+   int *q = &(*a)[0];
+   return q[1];
+ }
+ 
+ int g(int *p, int n)
+ {
+   int (*a)[n] = (int (*)[n])p;
+   int *q = &(*a)[2];
+   return q[-1];
+ }
+ 
+ /* { dg-final { scan-tree-dump-times "= \\\(\\\*a_..\\\)\\\[1\\\];" 2 "forwprop1" } } */
+ /* { dg-final { cleanup-tree-dump "forwprop1" } } */
Index: gcc/tree-ssa-forwprop.c
===================================================================
*** gcc/tree-ssa-forwprop.c	(revision 145233)
--- gcc/tree-ssa-forwprop.c	(working copy)
*************** forward_propagate_addr_expr_1 (tree name
*** 828,846 ****
    array_ref = TREE_OPERAND (def_rhs, 0);
    if (TREE_CODE (array_ref) != ARRAY_REF
        || TREE_CODE (TREE_TYPE (TREE_OPERAND (array_ref, 0))) != ARRAY_TYPE
!       || !integer_zerop (TREE_OPERAND (array_ref, 1)))
      return false;
  
    rhs2 = gimple_assign_rhs2 (use_stmt);
!   /* Try to optimize &x[0] p+ C where C is a multiple of the size
!      of the elements in X into &x[C/element size].  */
    if (TREE_CODE (rhs2) == INTEGER_CST)
      {
        tree new_rhs = maybe_fold_stmt_addition (gimple_expr_type (use_stmt),
! 					       array_ref, rhs2);
        if (new_rhs)
  	{
! 	  gimple_assign_set_rhs_from_tree (use_stmt_gsi, new_rhs);
  	  use_stmt = gsi_stmt (*use_stmt_gsi);
  	  update_stmt (use_stmt);
  	  tidy_after_forward_propagate_addr (use_stmt);
--- 828,847 ----
    array_ref = TREE_OPERAND (def_rhs, 0);
    if (TREE_CODE (array_ref) != ARRAY_REF
        || TREE_CODE (TREE_TYPE (TREE_OPERAND (array_ref, 0))) != ARRAY_TYPE
!       || TREE_CODE (TREE_OPERAND (array_ref, 1)) != INTEGER_CST)
      return false;
  
    rhs2 = gimple_assign_rhs2 (use_stmt);
!   /* Try to optimize &x[C1] p+ C2 where C2 is a multiple of the size
!      of the elements in X into &x[C1 + C2/element size].  */
    if (TREE_CODE (rhs2) == INTEGER_CST)
      {
        tree new_rhs = maybe_fold_stmt_addition (gimple_expr_type (use_stmt),
! 					       def_rhs, rhs2);
        if (new_rhs)
  	{
! 	  gimple_assign_set_rhs_from_tree (use_stmt_gsi,
! 					   unshare_expr (new_rhs));
  	  use_stmt = gsi_stmt (*use_stmt_gsi);
  	  update_stmt (use_stmt);
  	  tidy_after_forward_propagate_addr (use_stmt);
*************** forward_propagate_addr_expr_1 (tree name
*** 853,858 ****
--- 854,860 ----
       array elements, then the result is converted into the proper
       type for the arithmetic.  */
    if (TREE_CODE (rhs2) == SSA_NAME
+       && integer_zerop (TREE_OPERAND (array_ref, 1))
        && useless_type_conversion_p (TREE_TYPE (name), TREE_TYPE (def_rhs))
        /* Avoid problems with IVopts creating PLUS_EXPRs with a
  	 different type than their operands.  */



More information about the Gcc-patches mailing list