MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
regalloc.h
Go to the documentation of this file.
1#pragma once
2
3#include <optional>
4
5#include <mim/plugin.h>
6
9
10namespace mim::plug::mach {
11
12/// Prints the Liveness of every function to `-X <backend>:live=<file>` or stdout; a debugging and testing aid.
13class Live : public FunPhase {
14public:
16
17private:
18 void visit(Lam*) final;
19};
20
21/// Spills and reloads until at most `k` values of each register class are live at every point of a scheduled function.
22/// This is Belady's MIN per block with global next-use distances (Braun & Hack, CC 2009), summarized per block.
23/// A jump reads its target's distances and tells the target which arguments it passes in registers.
24/// A value is spilled right after its definition, and only if it is reloaded somewhere or flows into a memory φ.
25/// A parameter that is not in a register at the entry of its block becomes a memory φ of the type Target::spill_type.
26/// Expects the output of Close: a reload is a new version within its block, and a jump passes the current one.
27/// A block nested in the one block that jumps to it starts from the versions at the end of that block.
28/// Across a call of a function with a Conv in @p convs, only its clobber set is lost, else the caller-saved registers.
29/// `k` is `-X <backend>:k=<n>` or the number of allocatable registers of a class.
30void spill_fun(const FunPhase&, Lam* fun, const LamMap<Conv>& convs, std::optional<size_t> k);
31
32/// Copies fixed and live tied operands by a jump right before their instruction, keeping the graph chordal (Hack).
33/// Likewise, the live fixed results of an instruction go to a fresh continuation right after it.
34/// Returns whether it changed anything, which makes @p live stale.
35bool constrain_fun(const Liveness& live);
36
37/// Assigns a register to every value by one PBQP per function and register class (Buchwald, Zwinkau, Bersch, CC 2011).
38/// The result is in the graph: every use of a value `v` in register `r` becomes Target::in.
39/// Expects the output of spill_fun() and constrain_fun(), so the interference graph is chordal.
40/// Removes chain instructions whose results are all dead, so its instruction implies the register of any unused result.
41/// A checker verifies the assignment on a fresh Liveness.
42/// A known() function gets a Conv in @p convs, which its callers, colored after it, follow instead of the ABI.
43void color_fun(const FunPhase&, Lam* fun, Liveness live, LamMap<Conv>& convs, std::ostream* dot);
44
45/// The `k` of spill_fun(): `-X <backend>:k=<n>`, if given.
46std::optional<size_t> arg_k(Driver&, const Target&);
47
48/// spill_fun() alone, with the ABI at every call; a testing aid.
49class Spilling : public FunPhase {
50public:
54
55private:
56 void visit(Lam*) final;
57
58 std::optional<size_t> k_;
59};
60
61/// constrain_fun() alone; a testing aid.
62class Constrain : public FunPhase {
63public:
65
66private:
67 void visit(Lam*) final;
68};
69
70/// spill_fun(), constrain_fun(), and color_fun() for one function after the other, bottom-up.
71/// So a caller's spiller already knows the clobber set of a known() callee.
72class RegAlloc : public FunPhase {
73public:
77
78private:
79 void visit(Lam*) final;
80 /// The stream of `-X <backend>:color-dot=<file>`, if requested.
81 std::ostream* dot();
82
83 std::optional<size_t> k_;
84 Out dot_;
85 LamMap<Conv> convs_;
86};
87
88} // namespace mim::plug::mach
Some "global" variables needed all over the place.
Definition driver.h:65
A function.
Definition lam.h:91
A file name from the command line and the stream to write to;.
Definition plugin.h:100
flags_t annex() const
Definition phase.h:64
Driver & driver()
Definition phase.h:61
World & world()
Definition phase.h:60
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:43
constrain_fun() alone; a testing aid.
Definition regalloc.h:62
void visit(Lam *) final
FunPhase(World &world, flags_t annex, const Target &target)
Definition phase.h:57
Visits the closed Cns with a body of a scheduled World for one Target, each before the ones that call...
Definition phase.h:55
FunPhase(World &world, flags_t annex, const Target &target)
Definition phase.h:57
const Target & target() const
Definition phase.h:61
Prints the Liveness of every function to -X <backend>:live=<file> or stdout; a debugging and testing ...
Definition regalloc.h:13
void visit(Lam *) final
Definition live.cpp:20
FunPhase(World &world, flags_t annex, const Target &target)
Definition phase.h:57
Liveness of the values of one scheduled function, as a side table.
Definition liveness.h:35
void visit(Lam *) final
Definition color.cpp:445
RegAlloc(World &world, flags_t annex, const Target &target)
Definition regalloc.h:74
void visit(Lam *) final
Definition spilling.cpp:513
Spilling(World &world, flags_t annex, const Target &target)
Definition regalloc.h:51
The machine facts of a native backend that the phases of mach need.
Definition target.h:45
The mach Plugin
Definition asm.h:15
std::optional< size_t > arg_k(Driver &, const Target &)
The k of spill_fun(): -X <backend>:k=<n>, if given.
Definition spilling.cpp:499
void spill_fun(const FunPhase &, Lam *fun, const LamMap< Conv > &convs, std::optional< size_t > k)
Spills and reloads until at most k values of each register class are live at every point of a schedul...
Definition spilling.cpp:507
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
void color_fun(const FunPhase &, Lam *fun, Liveness live, LamMap< Conv > &convs, std::ostream *dot)
Assigns a register to every value by one PBQP per function and register class (Buchwald,...
Definition color.cpp:425
u64 flags_t
Definition types.h:39
GIDMap< Lam *, To > LamMap
Definition lam.h:210