bool simplify_impl(Env& env, Vlabel b, size_t i, Simplify simplify) { auto& unit = env.unit; return vmodify(unit, b, i, [&] (Vout& v) { auto& blocks = unit.blocks; auto const nremove = simplify(v); // Update use counts for to-be-removed instructions. for (auto j = i; j < i + nremove; ++j) { visitUses(unit, blocks[b].code[j], [&] (Vreg r) { --env.use_counts[r]; }); } // Update use counts and def instructions for to-be-added instructions. for (auto const& inst : blocks[Vlabel(v)].code) { visitUses(unit, inst, [&] (Vreg r) { if (r >= env.use_counts.size()) { env.use_counts.resize(size_t{r}+1); } ++env.use_counts[r]; }); visitDefs(unit, inst, [&] (Vreg r) { if (r >= env.def_insts.size()) { env.def_insts.resize(size_t{r}+1, Vinstr::nop); } env.def_insts[r] = inst.op; }); } return nremove; }); }
void lower_impl(Vunit& unit, Vlabel b, size_t i, Lower lower) { vmodify(unit, b, i, [&] (Vout& v) { lower(v); return 1; }); }