MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
constrain.cpp
Go to the documentation of this file.
1#include <ranges>
2
6
7namespace mim::plug::mach {
8
9namespace {
10
11/// Per instruction, its Target::untie()d replacement, the copies() before it, and the fixed results that stay live
12/// after it.
13struct Split {
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;
17};
18using Splits = fe::Vector<Split>;
19
20Splits splits(const Liveness& live, const Liveness::Block& block) {
21 const auto& target = live.target();
22 auto res = Splits(block.insns.size());
23 auto after = block.live_out;
24 for (auto v : live.term_uses(block))
25 after.emplace(v);
26 auto phis = Def2Def();
27 auto term = block.lam->body()->as<App>();
28 if (auto k = Lam::isa_mut_basicblock(term->callee()); k && live.block(k))
29 for (size_t j = 0, n = term->num_args(); j != n; ++j)
30 phis.emplace(term->arg(n, j), k->var(n, j));
31
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))
42 after.erase(d);
43 for (auto u : live.uses(insn))
44 after.emplace(u);
45 }
46 return res;
47}
48
49} // namespace
50
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(); }))
58 return false;
59
60 auto& w = live.blocks().front().lam->world();
61 auto map = Def2Def();
62 // A block comes after the block it is nested in, so a value is rebuilt before any block nested in its own.
63 for (size_t b = 0; b != live.blocks().size(); ++b) {
64 const auto& block = live.blocks()[b];
65 auto lam = block.lam;
66 auto filter = lam->filter();
67 auto term = lam->body()->as<App>();
68 auto seg = lam;
69 auto m = core::mem_var(lam);
70
71 auto finish = [&](const Def* body) {
72 if (seg != lam)
73 seg->set(w.lit_ff(), body);
74 else if (body != lam->body())
75 lam->unset()->set(filter, body);
76 };
77
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);
83 for (auto& op : ops)
84 op = subst.rewrite(op);
85
86 if (const auto& cps = all[b][i].pre; !cps.empty()) {
87 auto dom = DefVec{m->type()};
88 auto args = DefVec{m};
89 for (auto [j, _] : cps) {
90 dom.emplace_back(ops[j]->type());
91 args.emplace_back(ops[j]);
92 }
93 auto pre = w.mut_con(dom)->set(lam->dbg());
94 finish(w.app(pre, w.tuple(args)));
95
96 auto n = dom.size();
97 seg = pre;
98 m = pre->var(n, 0);
99 for (size_t k = 0; auto [j, reg] : cps) {
100 auto x = pre->var(n, ++k)->set(ops[j]->sym());
101 ops[j] = reg ? t.in(Target::Gpr, *reg, x) : x;
102 }
103 }
104
105 auto new_app = t.rebuild(src, m, ops);
106 map[insn] = new_app;
107 m = t.is_mem(new_app) ? new_app : new_app->proj(2, 0);
108
109 // A fixed result leaves its register right away, so its own live range is short.
110 if (const auto& post = all[b][i].post; !post.empty()) {
111 auto dom = DefVec{m->type()};
112 auto args = DefVec{m};
113 for (auto [j, reg] : post) {
114 auto v = Target::result(new_app, j);
115 dom.emplace_back(v->type());
116 args.emplace_back(t.in(Target::Gpr, reg, v));
117 }
118 auto next = w.mut_con(dom)->set(lam->dbg());
119 finish(w.app(next, w.tuple(args)));
120
121 auto n = dom.size();
122 seg = next;
123 m = next->var(n, 0);
124 for (size_t k = 0; auto [j, _] : post)
125 map[Target::result(insn, j)] = next->var(n, ++k);
126 }
127 }
128 finish(Subst(t, w, map, m).rewrite_jump(term));
129 }
130 return true;
131}
132
133void Constrain::visit(Lam* fun) { constrain_fun(Liveness(*this, fun)); }
134
135} // namespace mim::plug::mach
Base class for all Defs.
Definition def.h:285
Def * set(size_t i, const Def *)
Successively set from left to right.
Definition def.cpp:188
const Def * var(nat_t a, nat_t i) noexcept
Definition def.h:488
Def * unset()
Unsets all Def::ops; works even, if not set at all or only partially set.
Definition def.cpp:205
A function.
Definition lam.h:91
static Lam * isa_mut_basicblock(const Def *d)
Definition lam.h:130
void visit(Lam *) final
Liveness of the values of one scheduled function, as a side table.
Definition liveness.h:35
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...
Definition liveness.cpp:11
Subst(const Target &target, World &world, const Def2Def &map, const Def *m, const LamMap< Lam * > *lams=nullptr)
Definition subst.h:21
static const Def * result(const App *insn, size_t j)
The j-th projection of the non-memory result of insn.
Definition target.h:153
const Def * mem_var(Lam *lam)
Returns the memory argument of a function if it has one.
Definition core.h:162
The CPS Plugin
Definition cps.h:8
The mach Plugin
Definition asm.h:15
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.
Definition target.h:27
bool constrain_fun(const Liveness &live)
Copies fixed and live tied operands by a jump right before their instruction, keeping the graph chord...
Definition constrain.cpp:51
fe::Vector< Split > Splits
A bracketing of a matrix chain, innermost node first.
Definition reassoc.h:18
DefMap< const Def * > Def2Def
Definition def.h:93
fe::Vector< const Def * > DefVec
Definition def.h:96
@ App
Definition def.h:123