fold-const.c (fold_binary): Revert removing of index +p PTR
authorRichard Guenther <rguenther@suse.de>
Tue, 21 Aug 2007 09:18:15 +0000 (09:18 +0000)
committerRichard Biener <rguenth@gcc.gnu.org>
Tue, 21 Aug 2007 09:18:15 +0000 (09:18 +0000)
2007-08-21  Richard Guenther  <rguenther@suse.de>

        * fold-const.c (fold_binary): Revert removing of index +p PTR
        * folding.

        * gcc.dg/pointer-arith-10.c: New testcase.

From-SVN: r127660

gcc/ChangeLog
gcc/fold-const.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/pointer-arith-10.c [new file with mode: 0644]

index 8223bdd37976677b20bf5d5eeecfc13672b88c56..aa40fb4d138a221ed287a121086037fd4540cee0 100644 (file)
@@ -1,3 +1,7 @@
+2007-08-21  Richard Guenther  <rguenther@suse.de>
+
+       * fold-const.c (fold_binary): Revert removing of index +p PTR folding.
+
 2007-08-21  Richard Guenther  <rguenther@suse.de>
 
        PR middle-end/33122
index 8faf77d87519a033505d1ba2d1eb8fbb4931cd69..33467eb88f2cdcc77534dadb0bf3e27cbeeb16c4 100644 (file)
@@ -9528,6 +9528,13 @@ fold_binary (enum tree_code code, tree type, tree op0, tree op1)
                                                fold_convert (sizetype, arg1),
                                                fold_convert (sizetype, arg0)));
 
+      /* index +p PTR -> PTR +p index */
+      if (POINTER_TYPE_P (TREE_TYPE (arg1))
+         && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
+        return fold_build2 (POINTER_PLUS_EXPR, type,
+                           fold_convert (type, arg1),
+                           fold_convert (sizetype, arg0));
+
       /* (PTR +p B) +p A -> PTR +p (B + A) */
       if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
        {
index 29500ed8fdf8073f3acdbbc6c9fddaef15c52e88..9f0b0c8584de00ed677e1ccb86144c6060eccdf8 100644 (file)
@@ -1,3 +1,7 @@
+2007-08-21  Richard Guenther  <rguenther@suse.de>
+
+       * gcc.dg/pointer-arith-10.c: New testcase.
+
 2007-08-21  Richard Guenther  <rguenther@suse.de>
 
        PR middle-end/33122
diff --git a/gcc/testsuite/gcc.dg/pointer-arith-10.c b/gcc/testsuite/gcc.dg/pointer-arith-10.c
new file mode 100644 (file)
index 0000000..220891e
--- /dev/null
@@ -0,0 +1,10 @@
+/* { dg-do compile } */
+/* { dg-options "-fdump-tree-original" } */
+
+char *foo(char *p, __SIZE_TYPE__ i)
+{
+  return (char *)i + (__SIZE_TYPE__)p;
+}
+
+/* { dg-final { scan-tree-dump "p +" "original" } } */
+/* { dg-final { cleanup-tree-dump "original" } } */