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] Remove bogus PLUS_EXPR -> POINTER_PLUS_EXPR folding


This removes a bogus folding that causes miscompiles of libstdc++
testcases with the ext_pointer class (PR38835) and non-field-sensitive 
PTA (field-sensitive PTA is too dumb).  I probably anticipated the
problem in PR36227 which should be fixed with that patch as well.

Fold changes

   return <retval> = (const struct _Fwd_list_node_base *) ((long unsigned int) this + (long unsigned int) ((const struct _Relative_pointer_impl *) this)->_M_diff);

to

   (const struct _Fwd_list_node_base *) this p+ (long unsigned int) ((const struct _Relative_pointer_impl *) this)->_M_diff

which makes PTA correctly conclude that 'this' and the result of the
POINTER_PLUS_EXPR point to the same object.  Which they do not - the source
used addition in (long unsigned int) to avoid this issue and the resulting
pointer points somewhere else.

Fixed by removing the folding.  This also fixes -Wsystem-header warnings
and miscompiles for the libstdc++ testsuite.

Bootstrap and regtest on x86_64-unknown-linux-gnu ongoing.

Richard.

2009-01-16  Richard Guenther  <rguenther@suse.de>

	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.

	* gcc.dg/tree-ssa/foldaddr-1.c: XFAIL.

Index: gcc/fold-const.c
===================================================================
*** gcc/fold-const.c	(revision 143429)
--- gcc/fold-const.c	(working copy)
*************** 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	(revision 143429)
--- gcc/testsuite/gcc.dg/tree-ssa/foldaddr-1.c	(working copy)
*************** 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	(revision 143429)
--- gcc/tree-ssa-address.c	(working copy)
*************** 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 Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]