https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98255
--- Comment #7 from Martin Jambor <jamborm at gcc dot gnu.org> --- Even our constant folding thinks the unsigned expression wraps around. If I tell SRA to fold the expression if the base is a string_cst, the invalid dereference is avoided. My experiment was (I am not proposing this, but it illustrates the point): diff --git a/gcc/tree-sra.c b/gcc/tree-sra.c index d177f1ba11c..d33c0ab63f7 100644 --- a/gcc/tree-sra.c +++ b/gcc/tree-sra.c @@ -99,6 +99,7 @@ along with GCC; see the file COPYING3. If not see #include "dbgcnt.h" #include "builtins.h" #include "tree-sra.h" +#include "gimple-fold.h" /* Enumeration of all aggregate reductions we can do. */ @@ -1696,6 +1697,14 @@ build_ref_for_model (location_t loc, tree base, HOST_WIDE_INT offset, else { tree res; + if (TREE_CODE (base) == STRING_CST) + { + res = build_ref_for_offset (loc, base, offset, model->reverse, + model->type, gsi, insert_after); + res = fold_const_aggregate_ref (res); + return res; + } + if (model->grp_same_access_path && !TREE_THIS_VOLATILE (base) && (TYPE_ADDR_SPACE (TREE_TYPE (base))