https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52339
--- Comment #6 from Jakub Jelinek <jakub at gcc dot gnu.org> --- Though, walking the whole tree to find them in tree_invariant_p_1 would result in bad compile time complexity, because e.g. skip_simple_arithmetic can call that on both operands of binary expression etc. So, perhaps check it in save_expr? - if (tree_invariant_p_1 (inner)) + if (tree_invariant_p_1 (inner) + && !walk_tree_without_duplicates (&expr, contains_indirect_refs, NULL)) return expr; ?