14 const App* untied =
nullptr;
15 fe::Vector<std::pair<size_t, std::optional<size_t>>> pre;
16 fe::Vector<std::pair<size_t, size_t>> post;
18using Splits = fe::Vector<Split>;
21 const auto& target =
live.target();
23 auto after =
block.live_out;
24 for (
auto v :
live.term_uses(block))
27 auto term =
block.lam->body()->as<
App>();
29 for (
size_t j = 0, n = term->num_args(); j != n; ++j)
30 phis.emplace(term->arg(n, j), k->var(n, j));
32 for (
size_t i =
block.insns.size(); i-- != 0;) {
33 auto insn =
block.insns[i];
34 auto c = target.constraint(insn);
35 auto phi =
c.tied &&
c.results.empty() ? fe::lookup(phis,
Target::result(insn, 0)) : nullptr;
36 if (
c.tied && (after.contains(
c.ops[*
c.tied]) || phi))
37 if (
auto untied = target.untie(insn, after, phi))
c = target.constraint(res[i].untied = untied);
38 res[i].pre =
copies(c, [&](
const Def* v) {
return after.contains(v); });
39 for (
auto [j, reg] :
c.results)
40 if (after.contains(
Target::result(insn, j))) res[i].post.emplace_back(j, reg);
41 for (
auto d :
live.defs(insn))
43 for (
auto u :
live.uses(insn))
52 const auto& t =
live.target();
53 auto all = fe::Vector<Splits>();
54 for (
const auto& block :
live.blocks())
55 all.emplace_back(splits(
live, block));
56 if (std::ranges::all_of(all | std::views::join,
57 [](
const auto& s) {
return !s.untied && s.pre.empty() && s.post.empty(); }))
60 auto& w =
live.blocks().front().lam->world();
63 for (
size_t b = 0; b !=
live.blocks().size(); ++b) {
64 const auto& block =
live.blocks()[b];
66 auto filter = lam->filter();
67 auto term = lam->body()->as<
App>();
71 auto finish = [&](
const Def* body) {
73 seg->
set(w.lit_ff(), body);
74 else if (body != lam->body())
78 for (
size_t i = 0; i != block.insns.size(); ++i) {
79 auto insn = block.insns[i];
80 auto src = all[b][i].untied ? all[b][i].untied : insn;
81 auto subst =
Subst(t, w, map, m);
82 auto ops = t.operands(src);
84 op = subst.rewrite(op);
86 if (
const auto&
cps = all[b][i].pre; !
cps.empty()) {
87 auto dom =
DefVec{m->type()};
89 for (
auto [j, _] :
cps) {
90 dom.emplace_back(ops[j]->type());
91 args.emplace_back(ops[j]);
93 auto pre = w.mut_con(dom)->set(lam->dbg());
94 finish(w.app(pre, w.tuple(args)));
99 for (
size_t k = 0;
auto [j, reg] :
cps) {
100 auto x = pre->var(n, ++k)->set(ops[j]->sym());
105 auto new_app = t.rebuild(src, m, ops);
107 m = t.is_mem(new_app) ? new_app : new_app->proj(2, 0);
110 if (
const auto& post = all[b][i].post; !post.empty()) {
111 auto dom =
DefVec{m->type()};
113 for (
auto [j, reg] : post) {
115 dom.emplace_back(v->type());
118 auto next = w.mut_con(dom)->set(lam->dbg());
119 finish(w.app(next, w.tuple(args)));
124 for (
size_t k = 0;
auto [j, _] : post)
128 finish(
Subst(t, w, map, m).rewrite_jump(term));
Def * set(size_t i, const Def *)
Successively set from left to right.
const Def * var(nat_t a, nat_t i) noexcept
Def * unset()
Unsets all Def::ops; works even, if not set at all or only partially set.
static Lam * isa_mut_basicblock(const Def *d)
Liveness of the values of one scheduled function, as a side table.
Liveness(const FunPhase &phase, Lam *fun, bool solve=false)
Bails unless the blocks of the function fun are closed; with solve, it iterates to the fixed point in...
Subst(const Target &target, World &world, const Def2Def &map, const Def *m, const LamMap< Lam * > *lams=nullptr)
static const Def * result(const App *insn, size_t j)
The j-th projection of the non-memory result of insn.
const Def * mem_var(Lam *lam)
Returns the memory argument of a function if it has one.
fe::Vector< std::pair< size_t, std::optional< size_t > > > copies(const Constraint &c, auto live_after)
The fixed operands with their registers, and the tied one with no register if it is live_after.
bool constrain_fun(const Liveness &live)
Copies fixed and live tied operands by a jump right before their instruction, keeping the graph chord...
fe::Vector< Split > Splits
A bracketing of a matrix chain, innermost node first.
DefMap< const Def * > Def2Def
fe::Vector< const Def * > DefVec