MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
target.h
Go to the documentation of this file.
1#pragma once
2
3#include <optional>
4
5#include <mim/driver.h>
6#include <mim/target.h>
7
10#include <mim/plug/mem/mem.h>
11
12#include "mim/plug/mach/mach.h"
13
14namespace mim::plug::mach {
15
16/// What an instruction on the chain prescribes about the registers of its operands; registers are hardware encodings.
17struct Constraint {
18 DefVec ops; ///< See Target::operands.
19 fe::Vector<std::pair<size_t, size_t>> fixed; ///< Non-literal operands that have to be in a given register.
20 fe::Vector<std::pair<size_t, size_t>> results; ///< Results, see Target::result, that land in a given register.
21 std::optional<size_t> tied; ///< The operand whose register the (only) result takes.
22 fe::Vector<size_t> clobbers; ///< Registers written while the operands still occupy theirs.
23};
24
25/// The fixed operands with their registers, and the tied one with no register if it is @p live_after.
26/// With clobbers, also every other operand with no register, since it must not sit in a clobbered register.
27inline fe::Vector<std::pair<size_t, std::optional<size_t>>> copies(const Constraint& c, auto live_after) {
28 auto res = fe::Vector<std::pair<size_t, std::optional<size_t>>>();
29 for (auto [j, r] : c.fixed)
30 res.emplace_back(j, r);
31 auto copied = [&](size_t j) { return std::ranges::any_of(res, [j](auto p) { return p.first == j; }); };
32 if (c.tied && !copied(*c.tied) && live_after(c.ops[*c.tied])) res.emplace_back(*c.tied, std::nullopt);
33 if (!c.clobbers.empty())
34 for (size_t j = 0; j != c.ops.size(); ++j)
35 if (!Lit::isa(c.ops[j]) && !copied(j)) res.emplace_back(j, std::nullopt);
36 return res;
37}
38
39/// The register per parameter of a function with a convention of its own, or none if it is unused.
40using ParamRegs = fe::Vector<std::optional<size_t>>;
41
42/// The machine facts of a native backend that the phases of `mach` need.
43/// A value's class is `Flags` for `mach.Flags`, `Stack` for `mach.Spill T`, `Fpr` for `math.F pe`, and `Gpr` otherwise.
44/// A backend derives from it and adds its instructions, register file, ABI, and constraints.
45class Target : public mim::Target {
46public:
47 /// The register classes; `Stack` values live in a spill slot and are unbounded.
48 enum Class : size_t { Gpr, Fpr, Flags, Stack, Num_Classes };
49 /// @p plugin is the backend's plugin id.
50 Target(std::string_view name, plugin_t plugin)
51 : mim::Target(name)
52 , plugin_(plugin) {}
53
54 /// The Target of the one loaded backend; every backend's Target is a mach::Target.
55 static const Target& of(Driver& driver) { return static_cast<const Target&>(driver.target()); }
56
57 nat_t ptr_size() const override { return 8; }
58
59 /// @name Object Format
60 ///@{
61 /// Is the host that the emitted assembly is assembled and linked on ELF, not Mach-O?
62 static constexpr bool is_elf() {
63#ifdef __APPLE__
64 return false;
65#else
66 return true;
67#endif
68 }
69 /// The prefix that the object format adds to a C symbol.
70 static constexpr std::string_view global_prefix() { return is_elf() ? "" : "_"; }
71 /// The prefix of an assembler-local label.
72 static constexpr std::string_view local_prefix() { return is_elf() ? ".L" : "L"; }
73 ///@}
74
75 /// @name Register File and ABI
76 /// Registers are hardware encodings within their Class.
77 ///@{
78 virtual size_t num_regs(Class cls) const = 0;
79 /// Is @p r never available to register allocation?
80 virtual bool reserved(Class cls, size_t r) const = 0;
81 virtual bool caller_saved(Class cls, size_t r) const = 0;
82 virtual size_t num_arg_regs(Class cls) const = 0;
83 /// The register of the @p i-th argument of class @p cls.
84 virtual size_t arg_reg(Class cls, size_t i) const = 0;
85 virtual size_t ret_reg(Class cls) const = 0;
86 /// The number of registers of @p cls that register allocation may assign.
87 size_t num_allocatable(Class cls) const {
88 size_t n = 0;
89 for (size_t r = 0, e = num_regs(cls); r != e; ++r)
90 n += !reserved(cls, r);
91 return n;
92 }
93 /// The number of those that a call may clobber.
94 size_t num_caller_saved(Class cls) const {
95 size_t n = 0;
96 for (size_t r = 0, e = num_regs(cls); r != e; ++r)
97 n += !reserved(cls, r) && caller_saved(cls, r);
98 return n;
99 }
100 ///@}
101
102 /// @name Instructions
103 ///@{
104 /// The register class of the value @p def, from its type.
105 Class cls(const Def* def) const {
106 auto type = def->type();
107 if (Axm::isa<mach::Flags>(type)) return Flags;
108 if (Axm::isa<Spill>(type)) return Stack;
109 return math::isa_f(type) ? Fpr : Gpr;
110 }
111 /// Is @p app an instruction of this backend, other than `mach.sched`, that belongs on a `mem.M 0` chain?
112 /// By default, an application of an axiom of the backend or of `mach` that is no `Pi` and no `mach.in`.
113 virtual bool is_machine_insn(const App* app) const {
114 auto axm = app->axm();
115 return axm && (axm->plugin() == plugin_ || axm->plugin() == Plugin_Id) && !app->type()->isa<Pi>()
116 && !Axm::isa<mach::in>(app) && !is_view(app);
117 }
118 /// Is @p app an instruction that belongs on a `mem.M 0` chain?
119 bool is_insn(const App* app) const { return Axm::isa<sched>(app) || is_machine_insn(app); }
120 /// Is @p app an instruction that takes the `mem.M 0` it continues as its first argument?
121 /// `mach.slot`; a backend adds its loads and stores.
122 virtual bool is_mem_insn(const App* app) const { return Axm::isa<slot>(app); }
123 /// May @p insn go if nothing uses its results, e.g. a load?
124 /// A `mach.sched` and `mach.slot`; a backend adds its loads.
125 virtual bool is_removable(const App* insn) const { return Axm::isa<sched>(insn) || Axm::isa<slot>(insn); }
126 /// The register constraint of @p insn; by default, it has none.
127 /// Its operands are values, so an operand `zext d v` is `v`.
128 virtual Constraint constraint(const App* insn) const {
129 auto res = Constraint();
130 res.ops = operands(insn);
131 for (auto& op : res.ops)
132 op = unview(op);
133 return res;
134 }
135 /// An equivalent of @p insn that needs no copy of its tied operand if the values @p after the instruction hold it,
136 /// or that ties the parameter @p phi which its result replaces at the block's jump; none if there is no better one.
137 virtual const App* untie(const App*, const DefSet& /*after*/, const Def* /*phi*/) const { return nullptr; }
138 /// Does the literal @p lit fit a hole of type `Want::Imm`?
139 virtual bool fits_imm(const Def*) const { return true; }
140 /// Is @p axm an operation that instruction selection must have replaced?
141 /// An axiom of `core`, `mem`, or `math`, or `mach.load`/`mach.store`.
142 static bool is_lowered(const Axm* axm) {
143 auto p = axm->plugin();
144 return p == core::Plugin_Id || p == mem::Plugin_Id || p == math::Plugin_Id
145 || axm->flags() == Annex::Base<load> || axm->flags() == Annex::Base<store>;
146 }
147 /// The operands of @p insn: the argument of a `sched`'s instruction, otherwise the arguments including the memory.
148 static DefVec operands(const App* insn) {
149 auto ops = Axm::isa<sched>(insn) ? insn->arg()->projs() : insn->args();
150 return DefVec(ops.begin(), ops.end());
151 }
152 /// The @p j-th projection of the non-memory result of @p insn.
153 static const Def* result(const App* insn, size_t j) {
154 auto res = insn->proj(2, 1);
155 return res->proj(res->num_projs(), j);
156 }
157 /// @p insn continuing @p m with @p ops instead of its operands.
158 static const App* rebuild(const App* insn, const Def* m, const DefVec& ops) {
159 auto& w = insn->world();
160 if (Axm::isa<sched>(insn)) return w.app(w.app(insn->callee()->as<App>()->callee(), m), w.tuple(ops))->as<App>();
161 auto args = ops;
162 args[0] = m;
163 return w.app(insn->callee(), w.tuple(args))->as<App>();
164 }
165 ///@}
166
167 /// @name Spilling
168 ///@{
169 /// The instruction `f` of `sched f m x` that spills a value of @p type.
170 static const Def* spill(const Def* type) { return type->world().app(type->world().annex<mach::spill>(), type); }
171 /// The instruction that reloads a value of @p type from its spill slot.
172 static const Def* reload(const Def* type) { return type->world().app(type->world().annex<mach::reload>(), type); }
173 /// The type of the spill slot of a value of @p type.
174 static const Def* spill_type(const Def* type) { return type->world().app(type->world().annex<Spill>(), type); }
175 ///@}
176
177 /// @name Registers
178 ///@{
179 /// Is @p def a register operand `in r v`?
180 static bool is_in(const Def* def) { return Axm::isa<mach::in>(def); }
181 /// The value @p v of class @p cls in register @p r.
182 const Def* in(Class cls, size_t r, const Def* v) const {
183 auto& w = v->world();
184 return w.call<mach::in>(w.lit_idx(num_regs(cls), r), v);
185 }
186 /// The register `r` of `in r v`.
187 static size_t in_reg(const Def* in) {
188 return *Lit::isa(in->as<App>()->callee()->as<App>()->callee()->as<App>()->arg());
189 }
190 /// Is @p def a `zext d v`, which reads `v`'s register?
191 static bool is_view(const Def* def) { return Axm::isa<zext>(def); }
192 /// The `v` of `zext d v`, otherwise @p def.
193 static const Def* unview(const Def* def) { return is_view(def) ? def->as<App>()->arg() : def; }
194 /// The `v` of `in r v`, `zext d v`, or `zext d (in r v)`, otherwise @p def.
195 static const Def* strip(const Def* def) {
196 def = unview(def);
197 return is_in(def) ? def->as<App>()->arg() : def;
198 }
199 ///@}
200
201 /// @name Chain
202 ///@{
203 /// The `f` of `sched f m x`.
204 static const Def* sched_insn(const App* app) { return app->callee()->as<App>()->callee()->as<App>()->arg(); }
205 static bool is_mem(const Def* def) {
206 auto type = def->type(); // Level has no type
207 return type && Axm::isa<core::M>(type);
208 }
209 /// The memory @p app continues, if @p app is an instruction on a `mem.M 0` chain.
210 const Def* prev_mem(const App* app) const {
211 if (Axm::isa<sched>(app)) return app->callee()->as<App>()->arg();
212 return is_mem_insn(app) ? app->arg(app->num_args(), 0) : nullptr;
213 }
214 ///@}
215
216 /// @name Flags
217 ///@{
218 /// Is @p def a `Flags` value or a condition tested on one, neither of which is a register value?
219 virtual bool is_flags(const Def* def) const = 0;
220 /// Is @p app the `sched` of the backend's `cc`?
221 virtual bool is_sched_cc(const App* app) const = 0;
222 /// The subtag of the backend's `cc` that holds iff the one with subtag @p cc does not.
223 virtual size_t negate_cc(size_t cc) const = 0;
224 /// The `sched` of a `cc` that yields @p cond, if any.
225 const App* isa_sched_cc(const Def* cond) const {
226 if (auto ex = cond->isa<Extract>())
227 if (auto app = ex->tuple()->isa<App>(); app && is_sched_cc(app)) return app;
228 return nullptr;
229 }
230 ///@}
231
232private:
233 plugin_t plugin_;
234};
235
236} // namespace mim::plug::mach
const Axm * axm() const
Definition lam.h:257
auto args(Projector auto f) const noexcept
Definition lam.h:252
nat_t num_args() const noexcept
Definition lam.h:252
const Def * callee() const
Definition lam.h:237
const Def * arg() const
Definition lam.h:251
Definition axm.h:9
static auto isa(const Def *def)
Definition axm.h:112
plugin_t plugin() const
Definition axm.h:59
Base class for all Defs.
Definition def.h:285
const Def * proj(nat_t a, nat_t i) const
Similar to World::extract while assuming an arity of a, but also works on Sigmas and Arrays.
Definition def.cpp:659
World & world() const noexcept
Definition def.h:1102
constexpr flags_t flags() const noexcept
Definition def.h:307
auto projs(Projector auto f) const
Splits this Def via Def::projections into an Array (if A == std::dynamic_extent) or std::array (other...
Definition def.h:453
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
Definition def.h:1116
Some "global" variables needed all over the place.
Definition driver.h:65
const Target & target(Loc loc={})
The Target of the one loaded native backend; errors at loc if there is none or more than one.
Definition driver.cpp:201
Extracts from a Sigma or Array-typed Extract::tuple the element at position Extract::index.
Definition tuple.h:360
static std::optional< T > isa(const Def *def)
Definition def.h:974
A dependent function type.
Definition lam.h:14
The machine a native backend compiles for, as far as libmim needs to know it.
Definition target.h:11
std::string_view name() const
The plugin name, used as the prefix of diagnostics.
Definition target.h:18
static bool is_view(const Def *def)
Is def a zext d v, which reads v's register?
Definition target.h:191
virtual size_t num_regs(Class cls) const =0
static DefVec operands(const App *insn)
The operands of insn: the argument of a sched's instruction, otherwise the arguments including the me...
Definition target.h:148
static const Def * strip(const Def *def)
The v of in r v, zext d v, or zext d (in r v), otherwise def.
Definition target.h:195
virtual size_t negate_cc(size_t cc) const =0
The subtag of the backend's cc that holds iff the one with subtag cc does not.
static bool is_mem(const Def *def)
Definition target.h:205
const Def * prev_mem(const App *app) const
The memory app continues, if app is an instruction on a mem.M 0 chain.
Definition target.h:210
virtual Constraint constraint(const App *insn) const
The register constraint of insn; by default, it has none.
Definition target.h:128
static size_t in_reg(const Def *in)
The register r of in r v.
Definition target.h:187
Class
The register classes; Stack values live in a spill slot and are unbounded.
Definition target.h:48
virtual bool reserved(Class cls, size_t r) const =0
Is r never available to register allocation?
static const Def * spill(const Def *type)
Definition target.h:170
static bool is_in(const Def *def)
Definition target.h:180
Class cls(const Def *def) const
Definition target.h:105
static const Def * unview(const Def *def)
The v of zext d v, otherwise def.
Definition target.h:193
static const App * rebuild(const App *insn, const Def *m, const DefVec &ops)
insn continuing m with ops instead of its operands.
Definition target.h:158
virtual size_t num_arg_regs(Class cls) const =0
static const Def * sched_insn(const App *app)
Definition target.h:204
size_t num_caller_saved(Class cls) const
The number of those that a call may clobber.
Definition target.h:94
virtual size_t ret_reg(Class cls) const =0
virtual bool is_removable(const App *insn) const
May insn go if nothing uses its results, e.g.
Definition target.h:125
static const Target & of(Driver &driver)
The Target of the one loaded backend; every backend's Target is a mach::Target.
Definition target.h:55
virtual bool fits_imm(const Def *) const
Does the literal lit fit a hole of type Want::Imm?
Definition target.h:139
static constexpr std::string_view global_prefix()
The prefix that the object format adds to a C symbol.
Definition target.h:70
virtual bool is_flags(const Def *def) const =0
virtual const App * untie(const App *, const DefSet &, const Def *) const
An equivalent of insn that needs no copy of its tied operand if the values after the instruction hold...
Definition target.h:137
virtual bool caller_saved(Class cls, size_t r) const =0
static const Def * spill_type(const Def *type)
The type of the spill slot of a value of type.
Definition target.h:174
virtual bool is_machine_insn(const App *app) const
Is app an instruction of this backend, other than mach.sched, that belongs on a mem....
Definition target.h:113
static bool is_lowered(const Axm *axm)
Is axm an operation that instruction selection must have replaced?
Definition target.h:142
Target(std::string_view name, plugin_t plugin)
plugin is the backend's plugin id.
Definition target.h:50
virtual bool is_sched_cc(const App *app) const =0
Is app the sched of the backend's cc?
bool is_insn(const App *app) const
Is app an instruction that belongs on a mem.M 0 chain?
Definition target.h:119
nat_t ptr_size() const override
The size and alignment of a pointer in bytes; see core.trait.
Definition target.h:57
static const Def * reload(const Def *type)
The instruction that reloads a value of type from its spill slot.
Definition target.h:172
static constexpr std::string_view local_prefix()
The prefix of an assembler-local label.
Definition target.h:72
virtual bool is_mem_insn(const App *app) const
Is app an instruction that takes the mem.M 0 it continues as its first argument?
Definition target.h:122
const App * isa_sched_cc(const Def *cond) const
The sched of a cc that yields cond, if any.
Definition target.h:225
static constexpr bool is_elf()
Definition target.h:62
const Def * in(Class cls, size_t r, const Def *v) const
The value v of class cls in register r.
Definition target.h:182
size_t num_allocatable(Class cls) const
The number of registers of cls that register allocation may assign.
Definition target.h:87
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
virtual size_t arg_reg(Class cls, size_t i) const =0
The register of the i-th argument of class cls.
The mach Plugin
Definition asm.h:15
fe::Vector< size_t > clobbers
Registers written while the operands still occupy theirs.
Definition target.h:22
fe::Vector< std::pair< size_t, size_t > > results
Results, see Target::result, that land in a given register.
Definition target.h:20
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.
Definition target.h:40
DefVec ops
See Target::operands.
Definition target.h:18
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
fe::Vector< std::pair< size_t, size_t > > fixed
Non-literal operands that have to be in a given register.
Definition target.h:19
std::optional< size_t > tied
The operand whose register the (only) result takes.
Definition target.h:21
What an instruction on the chain prescribes about the registers of its operands; registers are hardwa...
Definition target.h:17
std::optional< nat_t > isa_f(const Def *def)
Definition math.h:55
Definition ast.h:16
u64 nat_t
Definition types.h:37
fe::Vector< const Def * > DefVec
Definition def.h:96
GIDSet< const Def * > DefSet
Definition def.h:92
u64 plugin_t
Definition types.h:40
static constexpr flags_t Base
Definition plugin.h:276