Patchwork [1/n] Do not force sizetype for POINTER_PLUS_EXPR

mail settings
Submitter Richard Guenther
Date July 7, 2011, 11:07 a.m.
Message ID <alpine.LNX.2.00.1107071301010.810@zhemvz.fhfr.qr>
Download mbox | patch
Permalink /patch/103638/
State New
Headers show


Richard Guenther - July 7, 2011, 11:07 a.m.
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,

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.


2011-07-07  Richard Guenther  <>

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


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,
-      /* 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)