MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
imm.h
Go to the documentation of this file.
1#pragma once
2
3#include <climits>
4
5#include <algorithm>
6#include <bit>
7#include <optional>
8
9#include <mim/def.h>
10
11namespace mim::plug::x86 {
12
13/// Width of the register holding an `Idx size`.
14inline nat_t reg_width(nat_t size) { return std::bit_ceil(std::max<nat_t>(Idx::size2bitwidth(size), 8)); }
15
16inline bool fits_i32(s64 x) { return INT32_MIN <= x && x <= INT32_MAX; }
17
18/// Does the literal @p val fit a sign-extended 32-bit immediate, if it is an `Idx size`?
19inline bool fits_imm(std::optional<nat_t> size, nat_t val) {
20 return (size && reg_width(*size) < 64) || fits_i32(s64(val));
21}
22
23/// Does the literal @p lit fit a sign-extended 32-bit immediate?
24inline bool fits_imm(const Def* lit) { return fits_imm(Idx::isa_lit(lit->type()), *Lit::isa(lit)); }
25
26} // namespace mim::plug::x86
Base class for all Defs.
Definition def.h:285
static constexpr nat_t size2bitwidth(nat_t n)
Definition def.h:1038
static std::optional< nat_t > isa_lit(const Def *def)
Definition def.cpp:689
static std::optional< T > isa(const Def *def)
Definition def.h:974
The x86 Plugin
Definition emit.h:9
bool fits_i32(s64 x)
Definition imm.h:16
nat_t reg_width(nat_t size)
Width of the register holding an Idx size.
Definition imm.h:14
bool fits_imm(std::optional< nat_t > size, nat_t val)
Does the literal val fit a sign-extended 32-bit immediate, if it is an Idx size?
Definition imm.h:19
u64 nat_t
Definition types.h:37
int64_t s64
Definition types.h:27