From beb53eef62044955be724ae56ab3dbe93b3eb764 Mon Sep 17 00:00:00 2001 From: mpolacek Date: Fri, 4 Aug 2017 11:28:04 +0000 Subject: [PATCH] PR middle-end/81695 * fold-const.c (fold_indirect_ref_1): For ((int *)&a + 4 -> a[1], perform the computation in offset_int. * gcc.dg/pr81695.c: New test. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@250871 138bc75d-0d04-0410-961f-82ee72b054a4 --- gcc/ChangeLog | 6 ++++++ gcc/fold-const.c | 23 +++++++++++++++-------- gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/gcc.dg/pr81695.c | 11 +++++++++++ 4 files changed, 37 insertions(+), 8 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/pr81695.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index c0d747dbcf2b..811c9878d43e 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2017-08-04 Marek Polacek + + PR middle-end/81695 + * fold-const.c (fold_indirect_ref_1): For ((int *)&a + 4 -> a[1], + perform the computation in offset_int. + 2017-08-04 Richard Sandiford PR tree-optimization/81136 diff --git a/gcc/fold-const.c b/gcc/fold-const.c index 53428b89454e..d563ba767668 100644 --- a/gcc/fold-const.c +++ b/gcc/fold-const.c @@ -14107,14 +14107,21 @@ fold_indirect_ref_1 (location_t loc, tree type, tree op0) && type == TREE_TYPE (op00type)) { tree type_domain = TYPE_DOMAIN (op00type); - tree min_val = size_zero_node; - if (type_domain && TYPE_MIN_VALUE (type_domain)) - min_val = TYPE_MIN_VALUE (type_domain); - op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01, - TYPE_SIZE_UNIT (type)); - op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val); - return build4_loc (loc, ARRAY_REF, type, op00, op01, - NULL_TREE, NULL_TREE); + tree min = TYPE_MIN_VALUE (type_domain); + if (min && TREE_CODE (min) == INTEGER_CST) + { + offset_int off = wi::to_offset (op01); + offset_int el_sz = wi::to_offset (TYPE_SIZE_UNIT (type)); + offset_int remainder; + off = wi::divmod_trunc (off, el_sz, SIGNED, &remainder); + if (remainder == 0) + { + off = off + wi::to_offset (min); + op01 = wide_int_to_tree (sizetype, off); + return build4_loc (loc, ARRAY_REF, type, op00, op01, + NULL_TREE, NULL_TREE); + } + } } } } diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 23f9c8fd1660..fbb1fba0f5f8 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2017-08-04 Marek Polacek + + PR middle-end/81695 + * gcc.dg/pr81695.c: New test. + 2017-08-04 Richard Sandiford PR tree-optimization/81136 diff --git a/gcc/testsuite/gcc.dg/pr81695.c b/gcc/testsuite/gcc.dg/pr81695.c new file mode 100644 index 000000000000..c3452580f1c0 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr81695.c @@ -0,0 +1,11 @@ +/* PR middle-end/81695 */ +/* { dg-do compile } */ +/* { dg-options "" } */ + +int z[] = { }; + +int +main (void) +{ + __builtin_printf ("%d\n", *(z + 1)); +} -- 2.47.2