This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [PATCH] Fold (int *)&a + 4 to a[1] using offset_int (PR middle-end/81695)
- From: Marek Polacek <polacek at redhat dot com>
- To: Richard Biener <richard dot guenther at gmail dot com>
- Cc: GCC Patches <gcc-patches at gcc dot gnu dot org>, Richard Biener <rguenther at suse dot de>
- Date: Fri, 4 Aug 2017 11:45:45 +0200
- Subject: Re: [PATCH] Fold (int *)&a + 4 to a[1] using offset_int (PR middle-end/81695)
- Authentication-results: sourceware.org; auth=none
- Authentication-results: ext-mx03.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com
- Authentication-results: ext-mx03.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=polacek at redhat dot com
- Dmarc-filter: OpenDMARC Filter v1.3.2 mx1.redhat.com EFCC478EAA
- References: <20170804083857.GC3397@redhat.com> <CAFiYyc1CRYfEtu1dTuZ=nFPDNpaH3FHK4yQzSLRa2e8Y=m0jgQ@mail.gmail.com>
On Fri, Aug 04, 2017 at 11:08:49AM +0200, Richard Biener wrote:
> On Fri, Aug 4, 2017 at 10:38 AM, Marek Polacek <polacek@redhat.com> wrote:
> > We were crashing because size_binop_loc got operands of different types:
> > sizetype and ssizetype. Richi suggested performing the computation in
> > offset_int which my patch tries to do. Unsure about the sdiv_trunc part,
> > what do I use instead of EXACT_DIV_EXPR?
>
> Use wi::divmod_trunc and check for a zero remainder. If the remainder is not
> zero we may not fold.
>
> Ok with that change.
Thanks, will commit this after the usual testing:
2017-08-04 Marek Polacek <polacek@redhat.com>
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.
diff --git gcc/fold-const.c gcc/fold-const.c
index ed6c289a64b..1f55bee8fc0 100644
--- gcc/fold-const.c
+++ gcc/fold-const.c
@@ -14106,14 +14106,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 gcc/testsuite/gcc.dg/pr81695.c gcc/testsuite/gcc.dg/pr81695.c
index e69de29bb2d..c3452580f1c 100644
--- gcc/testsuite/gcc.dg/pr81695.c
+++ 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));
+}
Marek