RFA: PATCH to fold_binary for c++/38880

Richard Guenther rguenther@suse.de
Tue Jan 27 09:35:00 GMT 2009


On Mon, 26 Jan 2009, Jason Merrill wrote:

> 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?

      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)))

If op0 and op1 are unstripped this would be a wrong tuple fed into
fold_binary - so I'd rather put a gcc_unreachable () here.

+       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)))

Likewise.  build2 would (should?) abort on both cases.

+       return fold_convert (type, fold_build2 (POINTER_PLUS_EXPR,
+                                               TREE_TYPE (op1),
+                                               op1,
+                                               fold_convert (sizetype,
op0)));


This patch, btw, does not fix the regression of const7.C for me.

Richard.



More information about the Gcc-patches mailing list