https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81695

--- Comment #6 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
I hope this is what you had in mind:

--- a/gcc/fold-const.c
+++ b/gcc/fold-const.c
@@ -14106,14 +14106,17 @@ 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));
+         off = wi::sdiv_trunc (off, el_sz);
+         off = off + wi::to_offset (min);
+         op01 = wide_int_to_tree (type, off);
+         return build4_loc (loc, ARRAY_REF, type, op00, op01,
+                    NULL_TREE, NULL_TREE);
+       }
        }
    }
     }

Reply via email to