https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56049
--- Comment #23 from Richard Biener <rguenth at gcc dot gnu.org> --- So we are _nearly_ there on trunk. LIM has improved up to the point the only blocker is mismatching alias-sets in mem_ref_hasher::equal and the case in question for this PR is even handled correctly because refs arrive in the correct order and the one we'd chose has the ref-all case we'd have to canonicalize to anyways. So the following works (and can be slightly enhanced). Index: gcc/tree-ssa-loop-im.c =================================================================== --- gcc/tree-ssa-loop-im.c (revision 270366) +++ gcc/tree-ssa-loop-im.c (working copy) @@ -178,7 +178,15 @@ mem_ref_hasher::equal (const im_mem_ref && known_eq (mem1->mem.size, obj2->size) && known_eq (mem1->mem.max_size, obj2->max_size) && mem1->mem.volatile_p == obj2->volatile_p - && mem1->mem.ref_alias_set == obj2->ref_alias_set + && (mem1->mem.ref_alias_set == obj2->ref_alias_set + /* We are not canonicalizing alias-sets but for the + special-case we didn't canonicalize yet and the + incoming ref is a alias-set zero MEM we pick + the correct one already. */ + || (!mem1->ref_canonical + && (TREE_CODE (obj2->ref) == MEM_REF + || TREE_CODE (obj2->ref) == TARGET_MEM_REF) + && obj2->ref_alias_set == 0)) && types_compatible_p (TREE_TYPE (mem1->mem.ref), TREE_TYPE (obj2->ref))); else