MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
target.cpp
Go to the documentation of this file.
2
3#include "mim/plug/x86/insn.h"
4
5namespace mim::plug::x86 {
6
7bool Target::is_mem_insn(const App* app) const {
9 || Axm::isa<mr>(app) || Axm::isa<rm1>(app) || Axm::isa<fload>(app) || Axm::isa<fstore>(app);
10}
11
14 if (Axm::isa<rm>(insn)) {
15 res.tied = insn::tied(insn);
16 return res;
17 }
18 if (!Axm::isa<mach::sched>(insn)) return res;
19
20 auto axm = std::get<0>(Axm::get(Target::sched_insn(insn)));
21 if (!axm) return res;
22 auto id = axm->base();
23 if (id == Annex::Base<div> || id == Annex::Base<idiv>) {
24 res.fixed.emplace_back(0, encoding(reg::rax));
25 res.results.emplace_back(0, encoding(reg::rax));
26 res.results.emplace_back(1, encoding(reg::rdx));
27 res.clobbers.emplace_back(encoding(reg::rdx));
28 } else if (id == Annex::Base<shl> || id == Annex::Base<shr> || id == Annex::Base<sar>) {
29 res.fixed.emplace_back(1, encoding(reg::rcx));
30 }
31 res.tied = insn::tied(id);
32 std::erase_if(res.fixed, [&](auto fixed) { return Lit::isa(res.ops[fixed.first]); });
33 if (res.tied && Lit::isa(res.ops[*res.tied])) res.tied.reset();
34 return res;
35}
36
37const App* Target::untie(const App* insn, const DefSet& after, const Def* phi) const {
38 if (!Axm::isa<mach::sched>(insn)) return nullptr;
39 auto f = sched_insn(insn);
40 auto id = std::get<0>(Axm::get(f))->base();
41 if (!insn::commutes(id) && id != Annex::Base<sub>) return nullptr;
42 auto& w = insn->world();
43 auto m = insn->callee()->as<App>()->arg();
44 auto [x, y] = insn->arg()->projs<2>();
45 auto a = unview(x);
46 auto b = unview(y);
47 auto live = after.contains(a);
48 // Overwrite `b` instead, which dies here.
49 auto onto_b = !Lit::isa(b) && b != a && !after.contains(b) && (live || (b == phi && a != phi));
50 auto as = [&](const Def* annex) {
51 return w.call<mach::sched>(w.app(annex, f->as<App>()->arg()), m, insn->arg())->as<App>();
52 };
53 if (onto_b && insn::commutes(id)) return w.call<mach::sched>(f, m, Defs{y, x})->as<App>();
54 if (onto_b && id == Annex::Base<sub>) return as(w.annex<rsub>());
55 if (live && id == Annex::Base<imul> && Lit::isa(b)) return as(w.annex<imul3>());
56 if (live && id == Annex::Base<add>) return as(w.annex<add3>());
57 return nullptr;
58}
59
64
65} // namespace mim::plug::x86
const Def * arg() const
Definition lam.h:251
static auto isa(const Def *def)
Definition axm.h:112
static std::tuple< const Axm *, u8, u8 > get(const Def *def)
Yields currying counter of def.
Definition axm.cpp:38
Base class for all Defs.
Definition def.h:285
static std::optional< T > isa(const Def *def)
Definition def.h:974
virtual Constraint constraint(const App *insn) const
The register constraint of insn; by default, it has none.
Definition target.h:128
static const Def * unview(const Def *def)
The v of zext d v, otherwise def.
Definition target.h:193
static const Def * sched_insn(const App *app)
Definition target.h:204
virtual bool is_removable(const App *insn) const
May insn go if nothing uses its results, e.g.
Definition target.h:125
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
bool is_mem_insn(const App *app) const final
Is app an instruction that takes the mem.M 0 it continues as its first argument?
Definition target.cpp:7
const App * untie(const App *insn, const DefSet &after, const Def *phi) const final
Swaps a commutative operation whose other operand dies and turns such a sub into an rsub.
Definition target.cpp:37
bool is_removable(const App *insn) const final
May insn go if nothing uses its results, e.g.
Definition target.cpp:60
mach::Constraint constraint(const App *insn) const final
The register constraint of insn; by default, it has none.
Definition target.cpp:12
What an instruction on the chain prescribes about the registers of its operands; registers are hardwa...
Definition target.h:17
Machine facts about the instructions of the x86 plugin, shared by x86_isel.gen and the backend.
Definition insn.h:15
bool commutes(flags_t base)
Does the two-address instruction base compute the same with its operands swapped?
Definition insn.h:33
std::optional< size_t > tied(flags_t base)
The operand whose register the result of a two-address instruction base takes.
Definition insn.h:20
The x86 Plugin
Definition emit.h:9
constexpr size_t encoding(Reg r)
The hardware encoding of the register r of a backend's module reg, whose subtags are in encoding orde...
Definition mach.h:15
fe::View< const Def * > Defs
Definition def.h:94
GIDSet< const Def * > DefSet
Definition def.h:92
static constexpr flags_t Base
Definition plugin.h:276