MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
normalizers.cpp
Go to the documentation of this file.
1#include <mim/world.h>
2
4
5#include "mim/plug/x86/imm.h"
6#include "mim/plug/x86/x86.h"
7
8namespace mim::plug::x86 {
9
10const Def* normalize_movzx(const Def* type, const Def*, const Def* x) { return mach::fold_ext(type, x, false); }
11const Def* normalize_movsx(const Def* type, const Def*, const Def* x) { return mach::fold_ext(type, x, true); }
12const Def* normalize_trunc(const Def* type, const Def*, const Def* x) { return mach::fold_ext(type, x, false); }
13
14const Def* normalize_addr(const Def* type, const Def*, const Def* arg) {
15 auto& w = type->world();
16 auto [disp, base] = arg->projs<2>();
17 auto d = Lit::isa(disp);
18 auto b = Lit::isa(base);
19 if (!d || !b || *b == 0) return nullptr;
20 if (auto sum = s64(s32(*d)) + s64(*b); fits_i32(sum)) return w.call<addr>(Defs{w.lit_i32(u32(sum)), w.lit_i64(0)});
21 return w.call<addr>(Defs{disp, w.call<mov>(base)});
22}
23
24const Def* normalize_lea(const Def* type, const Def*, const Def* a) {
25 if (auto ad = Axm::isa<addr>(a)) {
26 auto [disp, base] = ad->args<2>();
27 auto d = Lit::isa(disp);
28 auto b = Lit::isa(base);
29 if (d && *d == 0) return base;
30 if (d && b && *b == 0) return type->world().lit_i64(u64(s64(s32(*d))));
31 }
32 return nullptr;
33}
34
35template<cmovcc id>
36const Def* normalize_cmovcc(const Def* type, const Def*, const Def* arg) {
37 if constexpr (id == cmovcc::ne) {
38 auto [f, x, y] = arg->projs<3>();
39 if (auto t = Axm::isa<test>(f)) {
40 auto [a, b] = t->args<2>();
41 if (auto c = Axm::isa<cc>(a); c && a == b)
42 return type->world().call(static_cast<cmovcc>(Annex::Base<cmovcc> | c.sub()), Defs{c->arg(), x, y});
43 }
44 }
45 return nullptr;
46}
47
49
50} // namespace mim::plug::x86
static auto isa(const Def *def)
Definition axm.h:112
Base class for all Defs.
Definition def.h:285
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
static std::optional< T > isa(const Def *def)
Definition def.h:974
const Def * fold_ext(const Def *type, const Def *x, bool sign)
x of Idx s as a literal of type, sign-extended if sign.
Definition mach.h:20
The x86 Plugin
Definition emit.h:9
bool fits_i32(s64 x)
Definition imm.h:16
const Def * normalize_trunc(const Def *type, const Def *, const Def *x)
const Def * normalize_addr(const Def *type, const Def *, const Def *arg)
const Def * normalize_cmovcc(const Def *type, const Def *, const Def *arg)
const Def * normalize_lea(const Def *type, const Def *, const Def *a)
const Def * normalize_movzx(const Def *type, const Def *, const Def *x)
const Def * normalize_movsx(const Def *type, const Def *, const Def *x)
fe::View< const Def * > Defs
Definition def.h:94
int32_t s32
Definition types.h:27
int64_t s64
Definition types.h:27
uint64_t u64
Definition types.h:27
static constexpr flags_t Base
Definition plugin.h:276
#define MIM_x86_NORMALIZER_IMPL
Definition autogen.h:536