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]

RFA: PATCH to fold_binary for c++/38880


This patch largely reverts Richard's change to fold_binary for 38835 and 36227, but changes the code to check the types of the original operands rather than the STRIP_NOPS-ed operands. This avoids the problem of treating a pointer cast to an integer as still being a pointer but restores the ability to fold pointer+integer properly.

Richard, thoughts?

Jason
2009-01-26  Jason Merrill  <jason@redhat.com>

	PR c++/38880
	* fold-const.c (fold_binary): Restore PLUS_EXPR ->
	POINTER_PLUS_EXPR folding, but check unstripped operand types.
	* g++.dg/init/const7.C: Remove XFAIL.

Index: fold-const.c
===================================================================
*** fold-const.c	(revision 143499)
--- fold-const.c	(working copy)
*************** fold_binary (enum tree_code code, tree t
*** 9864,9869 ****
--- 9864,9886 ----
        return NULL_TREE;
  
      case PLUS_EXPR:
+       /* For POINTER_PLUS_EXPR folding we check the types of the original
+ 	 operands rather than the stripped operands because overflow
+ 	 semantics are different.  */
+       /* PTR + INT -> (INT)(PTR p+ INT) */
+       if (POINTER_TYPE_P (TREE_TYPE (op0))
+ 	  && INTEGRAL_TYPE_P (TREE_TYPE (op1)))
+ 	return fold_convert (type, fold_build2 (POINTER_PLUS_EXPR,
+ 						TREE_TYPE (op0),
+ 						op0,
+ 						fold_convert (sizetype, op1)));
+       /* INT + PTR -> (INT)(PTR p+ INT) */
+       if (POINTER_TYPE_P (TREE_TYPE (op1))
+ 	  && INTEGRAL_TYPE_P (TREE_TYPE (op0)))
+ 	return fold_convert (type, fold_build2 (POINTER_PLUS_EXPR,
+ 						TREE_TYPE (op1),
+ 						op1,
+ 						fold_convert (sizetype, op0)));
        /* A + (-B) -> A - B */
        if (TREE_CODE (arg1) == NEGATE_EXPR)
  	return fold_build2 (MINUS_EXPR, type,
Index: testsuite/g++.dg/init/const7.C
===================================================================
*** testsuite/g++.dg/init/const7.C	(revision 143499)
--- testsuite/g++.dg/init/const7.C	(working copy)
*************** 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" { xfail *-*-* } } }
  // { 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" } }
  // { dg-final { cleanup-tree-dump "gimple" } }

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