On Wed, Jan 3, 2018 at 5:31 PM, Marek Polacek <pola...@redhat.com> wrote: > Here we are crashing because cxx_fold_indirect_ref got a POINTER_PLUS_EXPR > with offset > signed HOST_WIDE_INT and we tried to convert it to sHWI. > > The matching code in fold_indirect_ref_1 uses uHWIs so I've followed suit. > But that code now also uses poly_uint64 and I'm not sure if any of the > constexpr.c code should use it, too. But this patch fixes the ICE.
POINTER_PLUS_EXPR offets are to be interpreted as signed (ptrdiff_t) so using uhwi and then performing an unsigned division is wrong code. See mem_ref_offset how to deal with this (ugh - poly-ints...). Basically you have to force the thing to signed. Like just use HOST_WIDE_INT offset = TREE_INT_CST_LOW (op01); Richard. > Bootstrapped/regtested on x86_64-linux, ok for trunk/7? > > 2018-01-03 Marek Polacek <pola...@redhat.com> > > PR c++/83659 > * constexpr.c (cxx_fold_indirect_ref): Use unsigned HOST_WIDE_INT > when computing offsets. > > * g++.dg/torture/pr83659.C: New test. > > diff --git gcc/cp/constexpr.c gcc/cp/constexpr.c > index 1aeacd51810..cf7c994b381 100644 > --- gcc/cp/constexpr.c > +++ gcc/cp/constexpr.c > @@ -3109,9 +3109,10 @@ cxx_fold_indirect_ref (location_t loc, tree type, tree > op0, bool *empty_base) > && (same_type_ignoring_top_level_qualifiers_p > (type, TREE_TYPE (op00type)))) > { > - HOST_WIDE_INT offset = tree_to_shwi (op01); > + unsigned HOST_WIDE_INT offset = tree_to_uhwi (op01); > tree part_width = TYPE_SIZE (type); > - unsigned HOST_WIDE_INT part_widthi = tree_to_shwi > (part_width)/BITS_PER_UNIT; > + unsigned HOST_WIDE_INT part_widthi > + = tree_to_uhwi (part_width) / BITS_PER_UNIT; > unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT; > tree index = bitsize_int (indexi); > > diff --git gcc/testsuite/g++.dg/torture/pr83659.C > gcc/testsuite/g++.dg/torture/pr83659.C > index e69de29bb2d..d9f709bb520 100644 > --- gcc/testsuite/g++.dg/torture/pr83659.C > +++ gcc/testsuite/g++.dg/torture/pr83659.C > @@ -0,0 +1,11 @@ > +// PR c++/83659 > +// { dg-do compile } > + > +typedef int V __attribute__ ((__vector_size__ (16))); > +V a; > + > +int > +main () > +{ > + reinterpret_cast <int *> (&a)[-1] += 1; > +} > > Marek