MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
rule_table.h
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <span>
5#include <string_view>
6#include <utility>
7
8#include <mim/util/types.h>
9
10namespace mim {
11class Def;
12class World;
13} // namespace mim
14
15/// The closed rewrite rules of a backend's isel plugin as C++; its `gen` writes them to the backend's `isel_rules.h`.
17
18#define MIM_MACH_RULE_WANTS(X) \
19 X(Param) X(Lit) X(Reg) X(FReg) X(Imm) X(Sh) X(RegImm) X(RegSh) X(Base) X(Index) X(Addr) X(Scaled) X(Zx)
20
21/// What a rule wants of the term a meta variable matches; a Param is bound but no operand.
22/// A Lit is a Param that must be a literal.
23/// Reg is a general-purpose register, FReg a floating-point one, and Base/Index an address without/with an index.
24/// Addr is either address form, and Scaled an index with its scale.
25/// Zx is a general-purpose register that holds its value zero-extended, which `mach.zext` reads without an instruction.
26/// Imm is a literal that fits the backend's immediates, Sh a literal shift count, which the machine masks.
27/// RegImm and RegSh are a Reg, or an Imm and a Sh for a literal that is one.
28enum class Want : u8 {
29#define X(w) w,
31#undef X
32};
33
34#define MIM_MACH_RULE_TYS(X) X(Reg) X(FReg) X(Base) X(Index) X(Scaled) X(Zx) X(Lit)
35
36/// The non-terminal a rule yields by the type of its rhs: a register of either class, an address without/with an index,
37/// `[I64, I2]`, or a register that holds its value zero-extended. Lit is no rule's type but that of a literal operand.
38enum class Ty : u8 {
39#define X(t) t,
41#undef X
42};
43
44/// Matches the root @p def with the commuting nodes in @p swap swapped and binds @p metas and @p refs.
45using MatchFn = bool (*)(const Def* def, u32 swap, const Def** metas, const Def** refs);
46/// Builds the rhs from @p holes, one per meta variable, already in the new world.
47using BuildFn = const Def* (*)(World&, const Def* const* holes);
48
49/// How the lhs refers to one of its inner nodes.
50enum class Ref : u8 {
51 Value, ///< As an operand of its owner, which nothing outside the match uses.
52 Mem, ///< By its memory, which only the match uses.
53};
54
55/// A ref of a matcher: its cover and how the lhs refers to it.
56struct RefInfo {
59};
60
61/// An operand position of a matcher in preorder: meta variable or Value ref @p index of the node @p owner.
62struct Use {
65 bool ref;
66};
67
68/// A matcher and the shape of its lhs; a cover's operands follow it in preorder.
77
78/// A closed rule `lhs ↦ rhs`, whose metas want `num_metas` consecutive entries of `wants`.
79struct Rewrite {
80 std::string_view name;
81 u32 cost; ///< The summed instruction costs of its rhs.
82 flags_t root; ///< The axiom at the head of its lhs, or 0 for an Extract.
83 Ty type; ///< The non-terminal its rhs yields.
84 const Matcher* match;
88 u32 swaps; ///< The swaps of `match` that may yield another match.
89 u32 copy_swaps; ///< Those of `swaps` whose operands differ only as a RegImm or RegSh, which a literal never fills.
90 u32 tied; ///< The meta variables that a two-address instruction of the rhs overwrites.
91 u8 maps; ///< `k + 1` if the rhs replaces cover `k` as a whole, or 0.
92};
93
94/// The rewrites `rewrites[first, first + num)` with the root axiom @p flags; `roots` is sorted by it.
100
101/// A conversion rule `x ↦ rhs`, whose lhs is all of its meta variables: a value of non-terminal `from` as one of `to`.
102/// Its builder takes the `arity` projections of the value.
104 std::string_view name;
109};
110
111/// @name Costs
112/// Shared by an isel plugin's `gen`, which checks the rule set with them, and isel.
113///@{
114inline constexpr u32 Inf = u32(-1);
115
116#define X(_) +1
117inline constexpr size_t Num_Tys = 0 MIM_MACH_RULE_TYS(X);
118#undef X
119
120/// A register-to-register copy, which move elimination makes free of latency.
121inline constexpr u32 Copy = 1;
122
123constexpr u32 add(u32 a, u32 b) { return a == Inf || b == Inf ? Inf : a + b; }
124
125/// Is a meta variable that @p w wants bound to its rewrite rather than an operand?
126constexpr bool is_param(Want w) { return w == Want::Param || w == Want::Lit; }
127
128/// Does a hole of type @p want take a literal rather than a replacement?
129constexpr bool is_literal(Want want) { return want == Want::Imm || want == Want::Sh; }
130
131/// The literal form of a hole of type @p want, or @p want itself.
132constexpr Want literal_of(Want want) {
133 return want == Want::RegImm ? Want::Imm : want == Want::RegSh ? Want::Sh : want;
134}
135
136/// Does a value of non-terminal @p ty fill a hole of type @p want as it is?
137constexpr bool fills(Ty ty, Want want) {
138 switch (want) {
139 case Want::Reg:
140 case Want::RegImm:
141 case Want::RegSh: return ty == Ty::Reg;
142 case Want::FReg: return ty == Ty::FReg;
143 case Want::Base: return ty == Ty::Base;
144 case Want::Index: return ty == Ty::Index;
145 case Want::Addr: return ty == Ty::Base || ty == Ty::Index;
146 case Want::Scaled: return ty == Ty::Scaled;
147 case Want::Zx: return ty == Ty::Zx;
148 default: return false;
149 }
150}
151
152/// The register hole that a value of non-terminal @p ty fills.
153constexpr Want reg_want(Ty ty) { return ty == Ty::FReg ? Want::FReg : Want::Reg; }
154
155/// Is a value of non-terminal @p ty in the same register class as a hole of type @p want?
156/// Conversions never cross between general-purpose and floating-point registers.
157constexpr bool same_class(Ty ty, Want want) {
158 if (want == Want::FReg) return ty == Ty::FReg;
159 return ty != Ty::FReg;
160}
161
162/// The cheapest chains of conversions between two non-terminals: their costs and each one's first conversion.
163struct Chains {
164 std::array<std::array<u32, Num_Tys>, Num_Tys> cost{};
165 std::array<std::array<u32, Num_Tys>, Num_Tys> first{};
166 /// The non-terminals of the backend: Reg, Lit, and those its conversions mention.
167 std::array<bool, Num_Tys> used{};
168
169 /// The closure of @p conversions, a range of Conversion-like rules with `from`, `to`, and `cost`.
170 constexpr Chains(const auto& conversions) {
171 for (size_t i = 0; i != Num_Tys; ++i)
172 for (size_t j = 0; j != Num_Tys; ++j)
173 cost[i][j] = i == j ? 0 : Inf, first[i][j] = Inf;
174 used[size_t(Ty::Reg)] = used[size_t(Ty::Lit)] = true;
175 u32 n = 0;
176 for (const auto& c : conversions) {
177 used[size_t(c.from)] = used[size_t(c.to)] = true;
178 if (auto& d = cost[size_t(c.from)][size_t(c.to)]; u32(c.cost) < d)
179 d = u32(c.cost), first[size_t(c.from)][size_t(c.to)] = n;
180 ++n;
181 }
182 for (size_t k = 0; k != Num_Tys; ++k)
183 for (size_t i = 0; i != Num_Tys; ++i)
184 for (size_t j = 0; j != Num_Tys; ++j)
185 if (auto c = add(cost[i][k], cost[k][j]); c < cost[i][j]) cost[i][j] = c, first[i][j] = first[i][k];
186 }
187
188 /// The non-terminal that @p from converts to most cheaply for a hole of type @p want, and the cost.
189 constexpr std::pair<Ty, u32> target(Ty from, Want want) const {
190 auto res = std::pair(from, Inf);
191 for (size_t t = 0; t != Num_Tys; ++t)
192 if (auto c = cost[size_t(from)][t]; fills(Ty(t), want) && c < res.second) res = {Ty(t), c};
193 return res;
194 }
195
196 /// Cost of converting a replacement or operand of non-terminal @p from into a hole of type @p want.
197 constexpr u32 conv(Ty from, Want want) const {
198 if (is_literal(want)) return from == Ty::Lit ? 0 : Inf;
199 return target(from, want).second;
200 }
201
202 /// Does every non-terminal of @p w's register class convert into a hole of type @p w, as an atomic rule's holes
203 /// must?
204 constexpr bool total(Want w) const {
205 for (size_t t = 0; t != Num_Tys; ++t)
206 if (used[t] && same_class(Ty(t), w) && conv(Ty(t), w) == Inf) return false;
207 return true;
208 }
209
210 /// Does some non-terminal of the register class of a rule convert into a hole of type @p w?
211 constexpr bool reachable(Want w) const {
212 for (size_t t = 0; t != Num_Tys; ++t)
213 if (Ty(t) != Ty::Lit && same_class(Ty(t), w) && conv(Ty(t), w) != Inf) return true;
214 return false;
215 }
216};
217///@}
218
219/// The generated tables of one backend, as its `isel_rules.h` defines them.
220struct Table {
221 std::span<const Want> wants;
222 std::span<const Use> uses;
223 std::span<const RefInfo> refs;
224 std::span<const Matcher> matchers;
225 std::span<const Rewrite> rewrites;
226 std::span<const Root> roots;
227 std::span<const Conversion> conversions;
230};
231
232} // namespace mim::plug::mach::rules
Base class for all Defs.
Definition def.h:285
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:43
The closed rewrite rules of a backend's isel plugin as C++; its gen writes them to the backend's isel...
Definition rule_table.h:16
constexpr u32 Copy
A register-to-register copy, which move elimination makes free of latency.
Definition rule_table.h:121
std::span< const Use > uses
Definition rule_table.h:222
std::span< const Conversion > conversions
Definition rule_table.h:227
std::span< const Root > roots
Definition rule_table.h:226
u32 cost
The summed instruction costs of its rhs.
Definition rule_table.h:81
const Def *(*)(World &, const Def *const *holes) BuildFn
Builds the rhs from holes, one per meta variable, already in the new world.
Definition rule_table.h:47
constexpr bool is_literal(Want want)
Does a hole of type want take a literal rather than a replacement?
Definition rule_table.h:129
constexpr size_t Num_Tys
Definition rule_table.h:117
constexpr bool fills(Ty ty, Want want)
Does a value of non-terminal ty fill a hole of type want as it is?
Definition rule_table.h:137
constexpr bool is_param(Want w)
Is a meta variable that w wants bound to its rewrite rather than an operand?
Definition rule_table.h:126
Ref
How the lhs refers to one of its inner nodes.
Definition rule_table.h:50
@ Value
As an operand of its owner, which nothing outside the match uses.
Definition rule_table.h:51
@ Mem
By its memory, which only the match uses.
Definition rule_table.h:52
u32 swaps
The swaps of match that may yield another match.
Definition rule_table.h:88
std::span< const RefInfo > refs
Definition rule_table.h:223
Want
What a rule wants of the term a meta variable matches; a Param is bound but no operand.
Definition rule_table.h:28
Ty type
The non-terminal its rhs yields.
Definition rule_table.h:83
std::span< const Rewrite > rewrites
Definition rule_table.h:225
constexpr Want literal_of(Want want)
The literal form of a hole of type want, or want itself.
Definition rule_table.h:132
u32 copy_swaps
Those of swaps whose operands differ only as a RegImm or RegSh, which a literal never fills.
Definition rule_table.h:89
std::span< const Want > wants
Definition rule_table.h:221
bool(*)(const Def *def, u32 swap, const Def **metas, const Def **refs) MatchFn
Matches the root def with the commuting nodes in swap swapped and binds metas and refs.
Definition rule_table.h:45
u32 tied
The meta variables that a two-address instruction of the rhs overwrites.
Definition rule_table.h:90
constexpr u32 add(u32 a, u32 b)
Definition rule_table.h:123
constexpr Want reg_want(Ty ty)
The register hole that a value of non-terminal ty fills.
Definition rule_table.h:153
constexpr bool same_class(Ty ty, Want want)
Is a value of non-terminal ty in the same register class as a hole of type want?
Definition rule_table.h:157
u8 maps
k + 1 if the rhs replaces cover k as a whole, or 0.
Definition rule_table.h:91
Ty
The non-terminal a rule yields by the type of its rhs: a register of either class,...
Definition rule_table.h:38
std::span< const Matcher > matchers
Definition rule_table.h:224
flags_t root
The axiom at the head of its lhs, or 0 for an Extract.
Definition rule_table.h:82
A conversion rule x ↦ rhs, whose lhs is all of its meta variables: a value of non-terminal from as on...
Definition rule_table.h:103
A matcher and the shape of its lhs; a cover's operands follow it in preorder.
Definition rule_table.h:69
A ref of a matcher: its cover and how the lhs refers to it.
Definition rule_table.h:56
A closed rule lhs ↦ rhs, whose metas want num_metas consecutive entries of wants.
Definition rule_table.h:79
The rewrites rewrites[first, first + num) with the root axiom flags; roots is sorted by it.
Definition rule_table.h:95
The generated tables of one backend, as its isel_rules.h defines them.
Definition rule_table.h:220
An operand position of a matcher in preorder: meta variable or Value ref index of the node owner.
Definition rule_table.h:62
Definition ast.h:16
u64 flags_t
Definition types.h:39
uint32_t u32
Definition types.h:27
uint8_t u8
Definition types.h:27
#define X(w)
Definition rule_table.h:29
#define MIM_MACH_RULE_WANTS(X)
Definition rule_table.h:18
#define MIM_MACH_RULE_TYS(X)
Definition rule_table.h:34
The cheapest chains of conversions between two non-terminals: their costs and each one's first conver...
Definition rule_table.h:163
constexpr u32 conv(Ty from, Want want) const
Cost of converting a replacement or operand of non-terminal from into a hole of type want.
Definition rule_table.h:197
std::array< bool, Num_Tys > used
The non-terminals of the backend: Reg, Lit, and those its conversions mention.
Definition rule_table.h:167
constexpr bool reachable(Want w) const
Does some non-terminal of the register class of a rule convert into a hole of type w?
Definition rule_table.h:211
constexpr std::pair< Ty, u32 > target(Ty from, Want want) const
The non-terminal that from converts to most cheaply for a hole of type want, and the cost.
Definition rule_table.h:189
std::array< std::array< u32, Num_Tys >, Num_Tys > first
Definition rule_table.h:165
constexpr bool total(Want w) const
Does every non-terminal of w's register class convert into a hole of type w, as an atomic rule's hole...
Definition rule_table.h:204
std::array< std::array< u32, Num_Tys >, Num_Tys > cost
Definition rule_table.h:164
constexpr Chains(const auto &conversions)
The closure of conversions, a range of Conversion-like rules with from, to, and cost.
Definition rule_table.h:170