https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112508
--- Comment #4 from Richard Biener <rguenth at gcc dot gnu.org> --- We are already doing /* Verify whether the candidate is hot for LOOP. Only do store motion if the candidate's profile count is hot. Statement in cold BB shouldn't be moved out of it's loop_father. */ if (!for_all_locs_in_loop (loop, ref, ref_in_loop_hot_body (loop))) return false; but /* Check the coldest loop between loop L and innermost loop. If there is one cold loop between L and INNER_LOOP, store motion can be performed, otherwise no cold loop means no store motion. get_coldest_out_loop also handles cases when l is inner_loop. */ bool ref_in_loop_hot_body::operator () (mem_ref_loc *loc) { basic_block curr_bb = gimple_bb (loc->stmt); class loop *inner_loop = curr_bb->loop_father; return get_coldest_out_loop (l, inner_loop, curr_bb); } checks that there's a good place to move a store to but it doesn't verify whether there's a store that's likely to be executed in its contained loop. The for_all_locs_in_loop is also happy if _any_ of the stores for the var can be moved to a colder place, likely the intent was to check that for all of the stores (though it's unlikely to differ in simple cases). This all doesn't distinguish between always and not always executed refs.