on ARRAY_REFs sign-extend offsets only from sizetype's precision [PR98255]
authorJakub Jelinek <jakub@redhat.com>
Fri, 22 Jan 2021 10:42:03 +0000 (11:42 +0100)
committerJakub Jelinek <jakub@redhat.com>
Fri, 22 Jan 2021 10:42:03 +0000 (11:42 +0100)
commite287a2a11d7958e5d9f7c6172e59cc83495e393a
treeffc177370b8b199ee6f381b0ac3f1b0009ea58d4
parentfd61ca67f97acc5462d02676380af92329c37bb2
on ARRAY_REFs sign-extend offsets only from sizetype's precision [PR98255]

As discussed in the PR, the problem here is that the routines changed in
this patch sign extend the difference of index and low_bound from the
precision of the index, so e.g. when index is unsigned int and contains
value -2U, we treat it as index -2 rather than 0x00000000fffffffeU on 64-bit
arches.
On the other hand, get_inner_reference which is used during expansion, does:
            if (! integer_zerop (low_bound))
              index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
                                   index, low_bound);

            offset = size_binop (PLUS_EXPR, offset,
                                 size_binop (MULT_EXPR,
                                             fold_convert (sizetype, index),
                                             unit_size));
which effectively requires that either low_bound is constant 0 and then
index in ARRAY_REFs can be arbitrary type which is then sign or zero
extended to sizetype, or low_bound is something else and then index and
low_bound must have compatible types and it is still converted afterwards to
sizetype and from there then a few lines later:
expr.c-  if (poly_int_tree_p (offset))
expr.c-    {
expr.c:      poly_offset_int tem = wi::sext (wi::to_poly_offset (offset),
expr.c-                               TYPE_PRECISION (sizetype));
The following patch makes those routines match what get_inner_reference is
doing.

2021-01-22  Jakub Jelinek  <jakub@redhat.com>

PR tree-optimization/98255
* tree-dfa.c (get_ref_base_and_extent): For ARRAY_REFs, sign
extend index - low_bound from sizetype's precision rather than index
precision.
(get_addr_base_and_unit_offset_1): Likewise.
* tree-ssa-sccvn.c (ao_ref_init_from_vn_reference): Likewise.
* gimple-fold.c (fold_const_aggregate_ref_1): Likewise.

* gcc.dg/pr98255.c: New test.
gcc/gimple-fold.c
gcc/testsuite/gcc.dg/pr98255.c [new file with mode: 0644]
gcc/tree-dfa.c
gcc/tree-ssa-sccvn.c