http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57993
--- Comment #6 from Bill Schmidt <wschmidt at gcc dot gnu.org> --- Here's the patch I'm currently testing, which corrects the problem for this test case. We'll see how it does on regressions. Index: gcc/gimple-ssa-strength-reduction.c =================================================================== --- gcc/gimple-ssa-strength-reduction.c (revision 201267) +++ gcc/gimple-ssa-strength-reduction.c (working copy) @@ -2179,6 +2179,18 @@ phi_add_costs (gimple phi, slsr_cand_t c, int one_ int cost = 0; slsr_cand_t phi_cand = base_cand_from_table (gimple_phi_result (phi)); + /* If we work our way back to a phi that isn't dominated by the hidden + basis, this isn't a candidate for replacement. Indicate this by + returning an unreasonably high cost. It's not easy to detect + these situations when determining the basis, so we defer the + decision until now. */ + basic_block phi_bb = gimple_bb (phi_cand->cand_stmt); + slsr_cand_t basis = lookup_cand (c->basis); + basic_block basis_bb = gimple_bb (basis->cand_stmt); + + if (phi_bb == basis_bb || !dominated_by_p (CDI_DOMINATORS, phi_bb, basis_bb)) + return COST_INFINITE; + for (i = 0; i < gimple_phi_num_args (phi); i++) { tree arg = gimple_phi_arg_def (phi, i);