MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
isel.h
Go to the documentation of this file.
1#pragma once
2
3#include <optional>
4#include <span>
5
6#include <pbqp/graph.h>
7
8#include <mim/driver.h>
9#include <mim/phase.h>
10
13
14namespace mim::plug::mach {
15
16/// @name Rules
17/// A rule rewrites a pattern over `core`/`mach`/`math` into a backend's term; the holes of the pattern are typed.
18///@{
19
20using rules::Ty;
21using rules::Want;
22
23/// What a match needs of the operand @p def: a hole of type @p want or, if @p inner is set, that inner pattern node.
24/// While matching, @p inner is one plus the index of its Cover; `analyze()` turns it into the id of the inner
25/// alternative.
26struct Demand {
27 const Def* def;
28 Want want = Want::Reg;
29 size_t inner = 0;
30
31 bool operator==(const Demand&) const = default;
32};
33
34/// An inner node a match covers, with what it needs of its own operands.
35struct Cover {
36 const Def* def;
37 size_t inner = 0;
38 fe::Vector<Demand> demands;
39 bool through = false; ///< The match only looks through it, since something else uses it.
40};
41
42/// An alternative of a PBQP node: a match rooted there, or the class of all matches covering it as the same inner node.
43struct Alt {
44 const rules::Rewrite* rule = nullptr; ///< `nullptr` for an inner alternative, whose id is its index plus one.
45 fe::Vector<Demand> demands;
46 fe::Vector<Cover> covers;
47 DefVec metas; ///< What the meta variables of its rule match.
48 u32 copies = 0; ///< The operands its two-address instructions overwrite although something else uses them.
49};
50
51using Alts = fe::Vector<Alt>;
52
53///@}
54
55/// A new ISel for the annex @p annex of a backend, with the rules @p table; compiled once into `libmim_mach`.
56extern "C" MIM_EXPORT Phase* mim_mach_isel(World&, flags_t annex, const rules::Table& table);
57
58/// Registers ISel with the rules @p table as the Phase of the backend's annex @p A.
59template<class A>
60void hook_isel(Flags2Phases& phases, const rules::Table& table) {
61 fe::assert_emplace(phases, Annex::base<A>(), [&table](World& world) {
62 return std::unique_ptr<Phase>(
63 world.driver().GET_FUN_PTR("mach", mim_mach_isel)(world, Annex::base<A>(), table));
64 });
65}
66
67/// Rewrites the `core`, `mach`, and `math` operations of a world that `mach.legalize` lowered into the
68/// instructions of a backend by the rules of its generated rules::Table. Everything this phase does not know is an
69/// error.
70///
71/// Selection is a cover of the program by matches of rules, found as a PBQP (Buchwald & Zwinkau, CASES 2010), where
72/// the holes of a pattern are typed.
73/// Every operation is a node; its alternatives are the matches rooted there and the inner nodes other matches cover it
74/// with.
75/// An edge prices the conversion between the type of the operand's rule and the type of the user's hole.
76/// The first alternative of every node is an atomic rule, so choosing it everywhere is a valid cover.
77class ISel : public RWPhase {
78public:
79 ISel(World& world, flags_t annex, const Target& target, const rules::Table& table)
81 , target_(target)
82 , table_(table) {}
83
84private:
85 /// @name Table
86 ///@{
87 std::span<const rules::Rewrite> rewrites(flags_t flags) const;
88 std::optional<flags_t> root_of(const Def* def) const;
89 Want want_of(const rules::Rewrite& r, size_t i) const { return table_.wants[r.wants + i]; }
90 bool atomic(const rules::Rewrite& r) const;
91 pbqp::Cost conv(Ty from, Want want) const;
92 Ty leaf_ty(const Def* def) const;
93 pbqp::Cost leaf(const Def* def, Want want) const { return conv(leaf_ty(def), want); }
94 bool literal(const Def* def, Want want) const {
95 return Lit::isa(def) && (want == Want::Sh || target_.fits_imm(def));
96 }
97 Want resolve(const Def* def, Want want) const;
98 ///@}
99
100 /// @name For Rules
101 ///@{
102 size_t uses(const Def* def) const {
103 auto n = fe::lookup(uses_, def);
104 return n ? *n : 0;
105 }
106 /// Does overwriting @p def cost a copy, since something else uses it?
107 /// The other uses of a parameter are mostly in other continuations, so it never does.
108 bool overwritten(const Def* def) const;
109 /// The memory operation of old_world() that @p def is the value of, if nothing else uses this value.
110 const App* sole_value(const Def* def) const;
111 /// The old_world() operand @p def as @p want in new_world().
112 const Def* hole(const Def* def, Want want = Want::Reg);
113 /// Builds the rhs of the rule of @p alt.
114 const Def* build(const Alt& alt);
115 ///@}
116
117 bool analyze() final;
118 /// Counts in `uses_` how often each Def of old_world() occurs as an operand.
119 void count(const Def*);
120 /// Appends the matches rooted at @p def to @p alts.
121 void match(const Def* def, Alts& alts);
122 /// Appends the matches of @p rule rooted at @p root to @p alts.
123 void append(const rules::Rewrite& rule, const Def* root, Alts& alts);
124 /// Appends the match of @p rule rooted at @p root with the commuting nodes in @p swap swapped and yields its
125 /// copies. A swap of `copy_swaps` only counts if it needs fewer copies than the @p rival without it.
126 std::optional<u32>
127 append(const rules::Rewrite& rule, const Def* root, u32 swap, std::optional<u32> rival, Alts& alts);
128 /// The inner node the operand @p def of @p parent stands for, if nothing outside the match uses it.
129 const App* sole(const Def* parent, const Def* def) const;
130 /// Does the match @p alt meet the side condition of its rule?
131 bool holds(const Alt& alt);
132 /// The node Def @p def stands for: a memory operation stands for its value.
133 const Def* key(const Def* def) const;
134 /// The chosen alternative of the node @p def.
135 const Alt* choice(const Def* def) const;
136 /// The replacement of the node @p def as its rule built it.
137 const Def* select(const Def* def);
138 /// The replacement of @p def in registers, if @p def is a node.
139 const Def* selected(const Def* def);
140 /// @p x of non-terminal @p from as @p want, by the cheapest chain of conversion rules.
141 const Def* convert(const Def* x, Ty from, Want want);
142
143 const Def* rewrite_imm_App(const App*) final;
144 const Def* rewrite_imm_Extract(const Extract*) final;
145
146 const Target& target_;
147 const rules::Table& table_;
148 DefMap<size_t> uses_;
149 DefSet counted_;
150 DefMap<Alts> alts_;
151 DefMap<size_t> choices_;
152 Def2Def roots_; ///< inner node → root of the match covering it
153 Def2Def repls_;
154};
155
156} // namespace mim::plug::mach
Base class for all Defs.
Definition def.h:285
static std::optional< T > isa(const Def *def)
Definition def.h:974
A Phase performs one self-contained task over the whole World.
Definition phase.h:25
flags_t annex() const
Definition phase.h:64
RWPhase(World &world, std::string name, Analysis *analysis=nullptr)
Definition phase.h:360
World & world()=delete
Hides both and forbids direct access.
friend void swap(Rewriter &rw1, Rewriter &rw2) noexcept
Definition rewrite.h:154
The World represents the whole program and manages creation of MimIR nodes (Defs).
Definition world.h:43
Driver & driver()
Definition world.h:107
const Def * rewrite_imm_App(const App *) final
Definition isel.cpp:410
ISel(World &world, flags_t annex, const Target &target, const rules::Table &table)
Definition isel.h:79
bool analyze() final
Runs the optional pre-analysis on old_world(), typically to a fixed point, before rewriting begins.
Definition isel.cpp:216
const Def * rewrite_imm_Extract(const Extract *) final
Definition isel.cpp:405
The machine facts of a native backend that the phases of mach need.
Definition target.h:45
virtual bool fits_imm(const Def *) const
Does the literal lit fit a hole of type Want::Imm?
Definition target.h:139
A saturating cost; Cost::inf forbids an option or option pair.
Definition graph.h:31
#define MIM_EXPORT
Definition config.h:21
Want
What a rule wants of the term a meta variable matches; a Param is bound but no operand.
Definition rule_table.h:28
std::span< const Want > wants
Definition rule_table.h:221
Ty
The non-terminal a rule yields by the type of its rhs: a register of either class,...
Definition rule_table.h:38
A closed rule lhs ↦ rhs, whose metas want num_metas consecutive entries of wants.
Definition rule_table.h:79
The generated tables of one backend, as its isel_rules.h defines them.
Definition rule_table.h:220
The mach Plugin
Definition asm.h:15
void hook_isel(Flags2Phases &phases, const rules::Table &table)
Registers ISel with the rules table as the Phase of the backend's annex A.
Definition isel.h:60
fe::Vector< Cover > covers
Definition isel.h:46
const Def * def
Definition isel.h:36
u32 copies
The operands its two-address instructions overwrite although something else uses them.
Definition isel.h:48
const rules::Rewrite * rule
nullptr for an inner alternative, whose id is its index plus one.
Definition isel.h:44
fe::Vector< Demand > demands
Definition isel.h:45
fe::Vector< Demand > demands
Definition isel.h:38
Phase * mim_mach_isel(World &, flags_t annex, const rules::Table &table)
A new ISel for the annex annex of a backend, with the rules table; compiled once into libmim_mach.
Definition isel.cpp:421
fe::Vector< Alt > Alts
Definition isel.h:51
DefVec metas
What the meta variables of its rule match.
Definition isel.h:47
bool through
The match only looks through it, since something else uses it.
Definition isel.h:39
An alternative of a PBQP node: a match rooted there, or the class of all matches covering it as the s...
Definition isel.h:43
An inner node a match covers, with what it needs of its own operands.
Definition isel.h:35
DefMap< const Def * > Def2Def
Definition def.h:93
u64 flags_t
Definition types.h:39
GIDMap< const Def *, To > DefMap
Definition def.h:91
fe::Vector< const Def * > DefVec
Definition def.h:96
GIDSet< const Def * > DefSet
Definition def.h:92
uint32_t u32
Definition types.h:27
ankerl::unordered_dense::map< flags_t, std::function< std::unique_ptr< Phase >(World &)> > Flags2Phases
Maps an axiom of a Phase to a function that creates one.
Definition plugin.h:31
@ App
Definition def.h:123
static consteval flags_t base()
Definition plugin.h:278
What a match needs of the operand def: a hole of type want or, if inner is set, that inner pattern no...
Definition isel.h:26
const Def * def
Definition isel.h:27
bool operator==(const Demand &) const =default