Re: [PATCH] Remove bogus PLUS_EXPR -> POINTER_PLUS_EXPR folding

Richard Guenther <[email protected]>
Newsgroups gmane.comp.gcc.patches,gmane.comp.gcc.java.devel
Message-ID <[email protected]>
On Fri, 16 Jan 2009, Andrew Haley wrote:

> OK.
> 
> Andrew.

Thanks.  This is what I applied.

Richard.

2009-01-16  Richard Guenther  <[email protected]>

	PR tree-optimization/38835
	PR middle-end/36227
	* fold-const.c (fold_binary): Remove PTR + INT -> (INT)(PTR p+ INT)
	and INT + PTR -> (INT)(PTR p+ INT) folding.
	* tree-ssa-address.c (create_mem_ref): Properly use POINTER_PLUS_EXPR.

	java/
	* builtins.c (build_addr_sum): Use POINTER_PLUS_EXPR.

	* gcc.c-torture/execute/pr36227.c: New testcase.
	* gcc.dg/tree-ssa/foldaddr-1.c: XFAIL.
	* g++.dg/init/const7.C: Likewise.

Index: gcc/fold-const.c
===================================================================
*** gcc/fold-const.c.orig	2009-01-16 18:07:26.000000000 +0100
--- gcc/fold-const.c	2009-01-16 20:11:49.000000000 +0100
*************** fold_binary (enum tree_code code, tree t
*** 9864,9883 ****
        return NULL_TREE;
  
      case PLUS_EXPR:
-       /* PTR + INT -> (INT)(PTR p+ INT) */
-       if (POINTER_TYPE_P (TREE_TYPE (arg0))
- 	  && INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
- 	return fold_convert (type, fold_build2 (POINTER_PLUS_EXPR,
- 						TREE_TYPE (arg0),
- 						arg0,
- 						fold_convert (sizetype, arg1)));
-       /* INT + PTR -> (INT)(PTR p+ INT) */
-       if (POINTER_TYPE_P (TREE_TYPE (arg1))
- 	  && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
- 	return fold_convert (type, fold_build2 (POINTER_PLUS_EXPR,
- 						TREE_TYPE (arg1),
- 						arg1,
- 						fold_convert (sizetype, arg0)));
        /* A + (-B) -> A - B */
        if (TREE_CODE (arg1) == NEGATE_EXPR)
  	return fold_build2 (MINUS_EXPR, type,
--- 9864,9869 ----
Index: gcc/testsuite/gcc.dg/tree-ssa/foldaddr-1.c
===================================================================
*** gcc/testsuite/gcc.dg/tree-ssa/foldaddr-1.c.orig	2009-01-16 18:06:51.000000000 +0100
--- gcc/testsuite/gcc.dg/tree-ssa/foldaddr-1.c	2009-01-16 20:11:49.000000000 +0100
*************** int foo(char *b)
*** 11,16 ****
  /* Folding should have determined that the two addresses were
     not identical and thus collapsed the function into a trivial
     "return 0".  */
! /* { dg-final { scan-tree-dump-times "return 0" 1 "original"} } */
  /* { dg-final { cleanup-tree-dump "original" } } */
  
--- 11,16 ----
  /* Folding should have determined that the two addresses were
     not identical and thus collapsed the function into a trivial
     "return 0".  */
! /* { dg-final { scan-tree-dump-times "return 0" 1 "original" { xfail *-*-* } } */
  /* { dg-final { cleanup-tree-dump "original" } } */
  
Index: gcc/tree-ssa-address.c
===================================================================
*** gcc/tree-ssa-address.c.orig	2009-01-16 18:06:51.000000000 +0100
--- gcc/tree-ssa-address.c	2009-01-16 20:11:49.000000000 +0100
*************** create_mem_ref (gimple_stmt_iterator *gs
*** 619,627 ****
  	    {
  	      atype = TREE_TYPE (tmp);
  	      parts.base = force_gimple_operand_gsi (gsi,
! 			fold_build2 (PLUS_EXPR, atype,
! 				     fold_convert (atype, parts.base),
! 				     tmp),
  			true, NULL_TREE, true, GSI_SAME_STMT);
  	    }
  	  else
--- 619,627 ----
  	    {
  	      atype = TREE_TYPE (tmp);
  	      parts.base = force_gimple_operand_gsi (gsi,
! 			fold_build2 (POINTER_PLUS_EXPR, atype,
! 				     tmp,
! 				     fold_convert (sizetype, parts.base)),
  			true, NULL_TREE, true, GSI_SAME_STMT);
  	    }
  	  else
Index: gcc/java/builtins.c
===================================================================
*** gcc/java/builtins.c.orig	2009-01-16 18:06:51.000000000 +0100
--- gcc/java/builtins.c	2009-01-16 20:11:49.000000000 +0100
*************** static tree
*** 265,273 ****
  build_addr_sum (tree type, tree addr, tree offset)
  {
    tree ptr_type = build_pointer_type (type);
!   return  fold_build2 (PLUS_EXPR, 
! 		       ptr_type, 
! 		       fold_convert (ptr_type, addr), offset);
  }
  
  /* Make sure that this-arg is non-NULL.  This is a security check.  */
--- 265,274 ----
  build_addr_sum (tree type, tree addr, tree offset)
  {
    tree ptr_type = build_pointer_type (type);
!   return fold_build2 (POINTER_PLUS_EXPR,
! 		      ptr_type,
! 		      fold_convert (ptr_type, addr),
! 		      fold_convert (sizetype, offset));
  }
  
  /* Make sure that this-arg is non-NULL.  This is a security check.  */
Index: gcc/testsuite/g++.dg/init/const7.C
===================================================================
*** gcc/testsuite/g++.dg/init/const7.C.orig	2009-01-16 18:06:51.000000000 +0100
--- gcc/testsuite/g++.dg/init/const7.C	2009-01-16 20:11:49.000000000 +0100
*************** short offsets[1] = {
*** 9,13 ****
  // This ensures that we get a dump whether or not the bug is present.
  void fn() { }
  
! // { dg-final { scan-tree-dump-not "initialization"  "gimple" } }
  // { dg-final { cleanup-tree-dump "gimple" } }
--- 9,13 ----
  // This ensures that we get a dump whether or not the bug is present.
  void fn() { }
  
! // { dg-final { scan-tree-dump-not "initialization"  "gimple" { xfail *-*-* } } }
  // { dg-final { cleanup-tree-dump "gimple" } }
Index: gcc/testsuite/gcc.c-torture/execute/pr36227.c
===================================================================
*** /dev/null	1970-01-01 00:00:00.000000000 +0000
--- gcc/testsuite/gcc.c-torture/execute/pr36227.c	2009-01-16 20:12:24.000000000 +0100
***************
*** 0 ****
--- 1,15 ----
+ #include <stdint.h>
+ extern void abort (void);
+ int main()
+ {
+   int i = 1;
+   int *p = &i;
+   uintptr_t iptr;
+ 
+   iptr = (uintptr_t)p - (uintptr_t)&iptr;
+   p = (int *)((uintptr_t)&iptr + iptr);
+   if (*p != 1)
+     abort ();
+   return 0;
+ }
+
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.