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] Fix PR21082 for C++


This patch fixes pr21082 for the trees the C++ frontend generates.
It folds &a[i]-&a[j] to i-j if there are ARRAY_REFs used.

Bootstrapped and tested on x86_64-unknown-linux with no regressions
for all languages except ada and treelang.

Ok for mainline?

Thanks,
Richard.
2005-04-23  Richard Guenther  <rguenth@gcc.gnu.org>

	* fold-const.c: Fold &a[i]-&a[j] to i-j.

	* g++.dg/tree-ssa/pr21082.C: New testcase.


Index: fold-const.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/fold-const.c,v
retrieving revision 1.568
diff -c -3 -p -r1.568 fold-const.c
*** fold-const.c	23 Apr 2005 02:01:49 -0000	1.568
--- fold-const.c	23 Apr 2005 17:57:00 -0000
*************** fold_binary (enum tree_code code, tree t
*** 7841,7847 ****
  	    && ptr_difference_const (arg0, arg1, &diff))
  	  return build_int_cst_type (type, diff);
        }
! 	  
        /* Try replacing &a[i1] - c * i2 with &a[i1 - i2], if c is step
  	 of the array.  Loop optimizer sometimes produce this type of
  	 expressions.  */
--- 7841,7868 ----
  	    && ptr_difference_const (arg0, arg1, &diff))
  	  return build_int_cst_type (type, diff);
        }
! 
!       /* Fold &a[i] - &a[j] to i-j.  */
!       if (TREE_CODE (arg0) == ADDR_EXPR
! 	  && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
! 	  && TREE_CODE (arg1) == ADDR_EXPR
! 	  && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
!         {
! 	  tree aref0 = TREE_OPERAND (arg0, 0);
! 	  tree aref1 = TREE_OPERAND (arg1, 0);
! 	  if (operand_equal_p (TREE_OPERAND (aref0, 0),
! 			       TREE_OPERAND (aref1, 0), 0))
! 	    {
! 	      tree op0 = fold_convert (type, TREE_OPERAND (aref0, 1));
! 	      tree op1 = fold_convert (type, TREE_OPERAND (aref1, 1));
! 	      tree esz = array_ref_element_size (aref0);
! 	      tree diff = build2 (MINUS_EXPR, type, op0, op1);
! 	      return fold_build2 (MULT_EXPR, type, diff,
! 			          fold_convert (type, esz));
! 			          
! 	    }
! 	}
! 
        /* Try replacing &a[i1] - c * i2 with &a[i1 - i2], if c is step
  	 of the array.  Loop optimizer sometimes produce this type of
  	 expressions.  */


/* { dg-do link } */

void link_error();

int a[4];
long b, c;

int main()
{
	if (&a[b] - &a[c] != b - c)
		link_error();
	return 0;
}

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