aboutsummaryrefslogtreecommitdiff
path: root/gcc/fold-const.c
diff options
context:
space:
mode:
authorRichard Guenther <rguenth@gcc.gnu.org>2005-04-23 21:34:40 +0000
committerRichard Biener <rguenth@gcc.gnu.org>2005-04-23 21:34:40 +0000
commit75cf42ccffa6a661cce421e0d9eb0e0689d75aae (patch)
treead181a15c3fa2379b1bb6a0ffc0864bace316ee0 /gcc/fold-const.c
parentbe1b5cba3527dececdb66c6c356260500a040345 (diff)
downloadgcc-75cf42ccffa6a661cce421e0d9eb0e0689d75aae.zip
gcc-75cf42ccffa6a661cce421e0d9eb0e0689d75aae.tar.gz
gcc-75cf42ccffa6a661cce421e0d9eb0e0689d75aae.tar.bz2
re PR middle-end/21082 (&a[b] - &a[c] is not folded to b - c)
2005-04-23 Richard Guenther <rguenth@gcc.gnu.org> PR middle-end/21082 * fold-const.c: Fold &a[i]-&a[j] to i-j. * g++.dg/tree-ssa/pr21082.C: New testcase. From-SVN: r98636
Diffstat (limited to 'gcc/fold-const.c')
-rw-r--r--gcc/fold-const.c23
1 files changed, 22 insertions, 1 deletions
diff --git a/gcc/fold-const.c b/gcc/fold-const.c
index e928025..a75d1a2 100644
--- a/gcc/fold-const.c
+++ b/gcc/fold-const.c
@@ -7841,7 +7841,28 @@ fold_binary (enum tree_code code, tree type, tree op0, tree op1)
&& ptr_difference_const (arg0, arg1, &diff))
return build_int_cst_type (type, diff);
}
-
+
+ /* Fold &a[i] - &a[j] to i-j. */
+ if (TREE_CODE (arg0) == ADDR_EXPR
+ && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
+ && TREE_CODE (arg1) == ADDR_EXPR
+ && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
+ {
+ tree aref0 = TREE_OPERAND (arg0, 0);
+ tree aref1 = TREE_OPERAND (arg1, 0);
+ if (operand_equal_p (TREE_OPERAND (aref0, 0),
+ TREE_OPERAND (aref1, 0), 0))
+ {
+ tree op0 = fold_convert (type, TREE_OPERAND (aref0, 1));
+ tree op1 = fold_convert (type, TREE_OPERAND (aref1, 1));
+ tree esz = array_ref_element_size (aref0);
+ tree diff = build2 (MINUS_EXPR, type, op0, op1);
+ return fold_build2 (MULT_EXPR, type, diff,
+ fold_convert (type, esz));
+
+ }
+ }
+
/* Try replacing &a[i1] - c * i2 with &a[i1 - i2], if c is step
of the array. Loop optimizer sometimes produce this type of
expressions. */