diff options
| author | rguenth <rguenth@138bc75d-0d04-0410-961f-82ee72b054a4> | 2005-04-23 21:34:40 +0000 |
|---|---|---|
| committer | rguenth <rguenth@138bc75d-0d04-0410-961f-82ee72b054a4> | 2005-04-23 21:34:40 +0000 |
| commit | d4e7fe89e71e63cc25c26e6652911dc59591d54c (patch) | |
| tree | ad181a15c3fa2379b1bb6a0ffc0864bace316ee0 | |
| parent | 1962cebaa32153a93d9eb93d120caa1b789d09d4 (diff) | |
| download | ppe42-gcc-d4e7fe89e71e63cc25c26e6652911dc59591d54c.tar.gz ppe42-gcc-d4e7fe89e71e63cc25c26e6652911dc59591d54c.zip | |
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.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@98636 138bc75d-0d04-0410-961f-82ee72b054a4
| -rw-r--r-- | gcc/ChangeLog | 5 | ||||
| -rw-r--r-- | gcc/fold-const.c | 23 | ||||
| -rw-r--r-- | gcc/testsuite/ChangeLog | 5 | ||||
| -rw-r--r-- | gcc/testsuite/g++.dg/tree-ssa/pr21082.C | 13 |
4 files changed, 45 insertions, 1 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 8d7e1564271..e0a065b7695 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,8 @@ +2005-04-23 Richard Guenther <rguenth@gcc.gnu.org> + + PR middle-end/21082 + * fold-const.c: Fold &a[i]-&a[j] to i-j. + 2005-04-23 Zdenek Dvorak <dvorakz@suse.cz> * tree-ssa-loop-niter.c (tree_simplify_using_condition): Expand simple diff --git a/gcc/fold-const.c b/gcc/fold-const.c index e928025700b..a75d1a25063 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. */ diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 819bb052c05..48e1d3078bd 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2005-04-23 Richard Guenther <rguenth@gcc.gnu.org> + + PR middle-end/21082 + * g++.dg/tree-ssa/pr21082.C: New testcase. + 2005-04-23 Zdenek Dvorak <dvorakz@suse.cz> * gcc.dg/vect/vect-99.c: New test. diff --git a/gcc/testsuite/g++.dg/tree-ssa/pr21082.C b/gcc/testsuite/g++.dg/tree-ssa/pr21082.C new file mode 100644 index 00000000000..dab630fa503 --- /dev/null +++ b/gcc/testsuite/g++.dg/tree-ssa/pr21082.C @@ -0,0 +1,13 @@ +/* { dg-do link } */ + +void link_error(); + +int a[4]; +long b, c; + +int main() +{ + if (&a[b] - &a[c] != b - c) + link_error(); + return 0; +} |

