This is the first of a series of enabling patches to make 
POINTER_PLUS_EXPR not forcefully take a sizetype offset
(I'm still no 100% what requirements I will end up implementing,
but the first goal is to have less TYPE_IS_SIZETYPE types).

This patch removes the (T *)index +p (int)PTR -> PTR +p index
folding.  We shouldn't change what the user specified as
the pointer base as we can't be sure we don't mess up here,
considering

int foo(int *p, uintptr_t o)
{
  return *((uintptr_t)p + (int *)o);
}
int main ()
{
  int res = 0;
  return foo((int *)0, (uintptr_t)&res);
}

if the o argument in foo is really the offset then the
C code is invoking undefined behavior as you may not do
anything with an integer which you converted to a pointer
other than converting it back.

Bootstrapped and tested on x86_64-unknown-linux-gnu.

Richard.

2011-07-07  Richard Guenther  <rguent...@suse.de>

        * fold-const.c (fold_binary_loc): Remove index +p PTR -> PTR +p index
        folding.

Index: gcc/fold-const.c
===================================================================
--- gcc/fold-const.c    (revision 175920)
+++ gcc/fold-const.c    (working copy)
@@ -9484,13 +9484,6 @@ fold_binary_loc (location_t loc,
                                              fold_convert_loc (loc, sizetype,
                                                                arg0)));
 
-      /* index +p PTR -> PTR +p index */
-      if (POINTER_TYPE_P (TREE_TYPE (arg1))
-         && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
-        return fold_build2_loc (loc, POINTER_PLUS_EXPR, type,
-                           fold_convert_loc (loc, type, arg1),
-                           fold_convert_loc (loc, sizetype, arg0));
-
       /* (PTR +p B) +p A -> PTR +p (B + A) */
       if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
        {

Reply via email to