MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
insn.h
Go to the documentation of this file.
1#pragma once
2
3#include <bit>
4#include <optional>
5
6#include <mim/axm.h>
7#include <mim/lam.h>
8
10
12#include "mim/plug/x86/imm.h"
13
14/// Machine facts about the instructions of the `x86` plugin, shared by `x86_isel.gen` and the backend.
16
17inline constexpr u32 Load = 4;
18
19/// The operand whose register the result of a two-address instruction @p base takes.
20inline std::optional<size_t> tied(flags_t base) {
21 // clang-format off
22 if (base == Annex::Base<add> || base == Annex::Base<sub> || base == Annex::Base<and_> || base == Annex::Base<or_>
23 || base == Annex::Base<xor_> || base == Annex::Base<imul> || base == Annex::Base<shl> || base == Annex::Base<shr>
24 || base == Annex::Base<sar>) return 0;
25 if (base == Annex::Base<fadd> || base == Annex::Base<fsub> || base == Annex::Base<fmul>
26 || base == Annex::Base<fdiv> || base == Annex::Base<fabs>) return 0;
27 // clang-format on
28 if (base == Annex::Base<cmovcc> || base == Annex::Base<fsel> || base == Annex::Base<rsub>) return 1;
29 return {};
30}
31
32/// Does the two-address instruction @p base compute the same with its operands swapped?
33inline bool commutes(flags_t base) {
34 return base == Annex::Base<add> || base == Annex::Base<and_> || base == Annex::Base<or_>
35 || base == Annex::Base<xor_> || base == Annex::Base<imul> || base == Annex::Base<fadd>
36 || base == Annex::Base<fmul>;
37}
38
39/// The operation `f` of the memory form `x86.rm f`, `x86.rm1 f`, or `x86.mr f` @p app.
40inline flags_t op(const App* app) { return std::get<0>(Axm::get(app->callee()->as<App>()->arg()))->base(); }
41
42/// The operand whose register the result of the two-address instruction @p app takes.
43/// `x86.rm f (m, a, addr)` overwrites `a` if `f` overwrites its first operand.
44inline std::optional<size_t> tied(const App* app) {
45 auto base = app->axm()->base();
46 if (base != Annex::Base<rm>) return tied(base);
47 if (tied(op(app)) == 0) return 1;
48 return {};
49}
50
51/// The latency in cycles of the instruction @p base on `s`-bit operands, roughly as on recent cores (uops.info).
52inline u32 latency(flags_t base, nat_t s) {
53 if (base == Annex::Base<imul> || base == Annex::Base<imul3>) return 3;
54 if (base == Annex::Base<rsub>) return 2;
55 if (base == Annex::Base<div> || base == Annex::Base<idiv>) return reg_width(s) == 64 ? 18 : 14;
56 if (base == Annex::Base<fdiv>) return 11;
57 if (base == Annex::Base<fsqrt>) return 14;
58 if (base == Annex::Base<load> || base == Annex::Base<fload>) return Load;
59 if (base == Annex::Base<mach::slot>) return 0;
60 return 1;
61}
62
63/// The cost of the instruction @p app: its latency plus one, so of two equally fast covers the shorter wins.
64/// An address is no instruction and costs nothing.
65inline u32 cost(const App* app) {
66 auto axm = app->axm();
67 auto base = axm->base();
68 if (Axm::isa<Addr>(app->type()) || base == Annex::Base<index>) return 0;
69 auto s = [](const Def* x) { return Idx::isa_lit(x->type()).value_or(64); };
70 if (base == Annex::Base<rm> || base == Annex::Base<mr>) {
71 auto [m, x, y] = app->args<3>();
72 auto res = Load + latency(op(app), s(base == Annex::Base<rm> ? x : y)) + 1;
73 return base == Annex::Base<mr> ? res + 1 : res;
74 }
75 if (base == Annex::Base<rm1>) return Load + latency(op(app), s(app->proj(2, 1))) + 1;
76 if (base == Annex::Base<trunc>) {
77 auto n = *Idx::isa_lit(app->type());
78 if (n != 0 && !std::has_single_bit(n)) return latency(Annex::Base<div>, n) + 4;
79 auto w = reg_width(n);
80 return n == 0 || (w < 64 && n == 1_n << w) ? 0 : 2;
81 }
82 auto arg = app->arg();
83 return latency(base, s(arg->num_projs() > 1 ? arg->proj(arg->num_projs(), arg->num_projs() - 1) : arg)) + 1;
84}
85
86} // namespace mim::plug::x86::insn
const Axm * axm() const
Definition lam.h:257
auto args(Projector auto f) const noexcept
Definition lam.h:252
const Def * callee() const
Definition lam.h:237
const Def * arg() const
Definition lam.h:251
static auto isa(const Def *def)
Definition axm.h:112
flags_t base() const
Definition axm.h:62
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
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
const Def * type() const noexcept
Yields the "raw" type of this Def (maybe nullptr).
Definition def.h:1116
static std::optional< nat_t > isa_lit(const Def *def)
Definition def.cpp:689
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
constexpr u32 Load
Definition insn.h:17
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
u32 latency(flags_t base, nat_t s)
The latency in cycles of the instruction base on s-bit operands, roughly as on recent cores (uops....
Definition insn.h:52
u32 cost(const App *app)
The cost of the instruction app: its latency plus one, so of two equally fast covers the shorter wins...
Definition insn.h:65
flags_t op(const App *app)
The operation f of the memory form x86.rm f, x86.rm1 f, or x86.mr f app.
Definition insn.h:40
nat_t reg_width(nat_t size)
Width of the register holding an Idx size.
Definition imm.h:14
u64 nat_t
Definition types.h:37
u64 flags_t
Definition types.h:39
uint32_t u32
Definition types.h:27
static constexpr flags_t Base
Definition plugin.h:276