19 auto succ = [&](
const Def* def) {
24 for (
auto target : ex->tuple()->projs())
26 for (
auto arg : term->
args())
39 using Pressure = std::array<size_t, Num_Classes>;
53 static constexpr size_t npos = size_t(-1);
63 const auto&
blocks()
const {
return blocks_; }
65 auto i = lam2block_.find(lam);
66 return i == lam2block_.end() ? nullptr : &blocks_[i->second];
80 void chain(
size_t,
Lam* fun);
83 const DefVec& at(
const App* insn, fe::Vector<DefVec> Block::* vecs)
const {
84 auto [b, i] = insn2block_.find(insn)->second;
85 return (blocks_[b].*vecs)[i];
91 std::unique_ptr<Nest> nest_;
92 fe::Vector<Block> blocks_;
99 return !target.
is_mem(value) && !value->
type()->isa<
Pi>() && target.
cls(value) == cls;
104 if (
auto to = fe::lookup(forward, lam))
return to;
114 for (
const auto& block :
live.blocks()) {
115 for (
const auto& uses : block.uses)
118 for (
auto u : block.term_uses)
138 const Def* raw(
const Def* def);
153 std::optional<size_t>
reg;
169 return f ? fe::lookup(convs, f) :
nullptr;
174 return conv ? conv->clobbers[cls][r] : target.
caller_saved(cls, r);
179 auto term = block.lam->body()->as<
App>();
187template<
class Keep,
class F>
190 auto gen = [&](
const auto& values) {
191 for (
auto v : values)
192 if (keep(v)) alive.emplace(v);
194 auto def = [&](
DefVec ds) {
195 std::erase_if(ds, [&](
const Def* d) {
return !keep(d); });
198 for (
size_t i = 0; i != ds.size(); ++i) {
201 for (
size_t j = i + 1; j != ds.size(); ++j)
207 gen(
live.term_uses(block));
208 for (
auto insn : block.insns | std::views::reverse) {
209 def(
live.defs(insn));
210 gen(
live.uses(insn));
212 def(
live.params(block));
auto args(Projector auto f) const noexcept
const Def * callee() const
nat_t num_vars() noexcept
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
static Lam * isa_mut_basicblock(const Def *d)
const Pi * ret_pi() const
static Lam * isa_mut_returning(const Def *d)
const Def * ret_var()
Yields the Lam::var of the Lam::ret_pi.
Builds a nesting tree for all mutables/binders.
A dependent function type.
Recurseivly rebuilds part of a program into the provided World w.r.t. Rewriter::map.
The machine a native backend compiles for, as far as libmim needs to know it.
The World represents the whole program and manages creation of MimIR nodes (Defs).
Assign(World &world, const Liveness &live, const DefMap< size_t > ®s)
const Def * rewrite(const Def *) final
Visits the closed Cns with a body of a scheduled World for one Target, each before the ones that call...
Liveness of the values of one scheduled function, as a side table.
const auto & blocks() const
In pre-order of the Nest with siblings in topological order, so a block comes after the block it is n...
const Block * block(Lam *lam) const
const DefVec & uses(const App *insn) const
const DefVec & params(const Block &block) const
const Nest::Node * node
Its Node in nest().
fe::Vector< DefVec > defs
size_t parent
The block it is nested in, or npos for the entry.
static constexpr size_t npos
fe::Vector< DefVec > uses
Per instruction.
const DefVec & term_uses(const Block &block) const
The values the jump at the end reads.
fe::Vector< size_t > targets
The blocks its terminator jumps to.
Pressure max_live
Maximum over all program points, per Class; Stack is unbounded.
bool local(const Def *def) const
Is def a parameter of a block or a result of an instruction on a chain, or a projection thereof?
fe::Vector< const App * > insns
The mem.M 0 chain in execution order.
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...
std::array< size_t, Num_Classes > Pressure
const Target & target() const
const Nest & nest() const
The late Nest of the function as it is now.
const DefVec & defs(const App *insn) const
The machine facts of a native backend that the phases of mach need.
static bool is_mem(const Def *def)
Class
The register classes; Stack values live in a spill slot and are unbounded.
Class cls(const Def *def) const
virtual bool caller_saved(Class cls, size_t r) const =0
void for_each_use(const Liveness &live, auto f)
Calls f for each use of a value in live.
bool clobbered(const Target &target, const Conv *conv, Target::Class cls, size_t r)
Does a call of a function with the convention conv, or of an ABI function if there is none,...
fe::Vector< std::optional< size_t > > ParamRegs
The register per parameter of a function with a convention of its own, or none if it is unused.
bool is_value(const Def *value, Liveness::Class cls, const Target &target)
Is value a value of class cls?
size_t num_params(Lam *lam)
The parameters of lam without its return continuation.
const Liveness::Block * call_cont(const Liveness &live, Lam *fun, const Liveness::Block &block)
The block that a call at the end of block returns to, if any.
void interferences(const Liveness &live, const Liveness::Block &block, Keep keep, F f)
Calls f (d, v) for each value v that is live at the definition of d or defined along with it.
std::optional< size_t > reg
void for_each_succ(const App *term, auto f)
Calls f for each block, possibly repeatedly, that the jump term passes control or a return continuati...
Lam * forwarded(const LamMap< Lam * > &forward, Lam *lam)
Where a jump to lam goes once the blocks that forward skips are skipped.
void rewrite_blocks(const Liveness &live, Rewriter &rw)
Rewrites the filter and body of each block of live in place.
fe::Vector< const Liveness::Block * > layout(const Liveness &live, const LamMap< Lam * > &forward={})
The blocks of live in emission order: each block is followed by the one it falls through to,...
const Conv * callee_conv(const LamMap< Conv > &convs, const Liveness::Block &block)
The Conv of the callee that the call at the end of block returns from, if it has one.
fe::Vector< ArgLoc > arg_locs(const Target &target, const Def *arg, size_t n, size_t num, const ParamRegs *conv)
The ArgLocs of the first num of the n arguments in arg under conv, or the ABI if it is nullptr.
std::optional< size_t > stack
ParamRegs param_regs(const Target &target, Lam *fun, const DefMap< size_t > ®s)
Where the known() function fun receives its parameters, given the registers regs that Color assigned ...
Where an argument goes: a register of its class, an incoming stack slot, or neither (memory or a dead...
GIDMap< const Def *, To > DefMap
GIDMap< Lam *, To > LamMap
fe::Vector< const Def * > DefVec
GIDSet< const Def * > DefSet
The convention of a known() function: its param_regs() and the registers a call of it clobbers,...
std::array< fe::Vector< bool >, Target::Num_Classes > clobbers