https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112727
--- Comment #7 from Jakub Jelinek <jakub at gcc dot gnu.org> --- Actually --- gcc/cp/cp-gimplify.cc.jj 2023-12-05 09:06:06.112878408 +0100 +++ gcc/cp/cp-gimplify.cc 2023-12-06 22:32:46.379370223 +0100 @@ -2906,7 +2906,14 @@ cp_fold (tree x, fold_flags_t flags) fold_cache = hash_map<tree, tree>::create_ggc (101); if (tree *cached = fold_cache->get (x)) - return *cached; + { + /* unshare_expr doesn't recurse into SAVE_EXPRs. If SAVE_EXPR's + argument has been folded into a tree invariant, make sure it is + unshared. See PR112727. */ + if (TREE_CODE (x) == SAVE_EXPR && *cached != x) + return unshare_expr (*cached); + return *cached; + } uid_sensitive_constexpr_evaluation_checker c; is what it really fixes.