- See also
- mim::plug::x86_isel
The instruction-selection rules of the x86 backend. gen writes them as C++ that the x86 plugin compiles in, so the compiler itself never loads this plugin.
Dependencies
plugin core;
plugin math;
plugin mach;
import compile;
import mem;
import x86;
Guards
A guard is tt after instantiation or a conjunction of mach.lit and these atoms on meta variables.
imm
A literal that fits a sign-extended 32-bit immediate; stuck on anything but a literal.
axm imm: {s: Nat} → Idx s → Bool, normalize_imm;
Operands
addr
The address p as an address operand. isel picks its form, x86.Base or x86.Index, and the builder infers which.
axm addr: I64 → x86.Base;
reg_imm
The operand x in a register or, if it is a literal that fits, as a sign-extended 32-bit immediate.
axm reg_imm: {s: Nat} → Idx s → Idx s;
reg_sh
The shift count x in a register or, if it is a literal, as an immediate, since x86 masks it.
axm reg_sh: {s: Nat} → Idx s → Idx s;
scaled
The I64 i as an index with its scale.
axm scaled: I64 → [I64, I2];
zx
x in a register that holds it zero-extended, which mach.zext reads as a wider value; a rhs zx e yields one.
axm zx: {s: Nat} → Idx s → Idx s;
Commutativity
The operations whose two operands a rule also matches swapped.
anx let commutes = (core.wrap.add, core.wrap.mul, math.arith.add, math.arith.mul, core.bit2.and, core.bit2.or, core.bit2.xor, core.bit2.nand, core.bit2.nor, core.bit2.nxor);
Rules
Each family takes the operand size as {s: Nat}, which gen instantiates at each of sizes that the refly.checks of its instructions and its guard admit. A meta variable that the right-hand side uses and no other meta variable's type mentions is an operand, unless it is a memory. An lhs (m, e) replaces e and, by the memory of the rhs, the memory m of an inner node of e. A family that takes a marker r puts its second operand under reg_imm or reg_sh. c is the size of what the instruction yields, which the frontend cannot infer.
let mark = {s: Nat} → Idx s → Idx s;
ALU
rule alu (c: Nat → Nat) (op: Nat → {s: Nat} → «2; Idx s» → Idx s, insn: {s: Nat} → «2; Idx s» → Idx (c s)) (r: mark) {s: Nat} (m: Nat, a b: Idx s): op m (a, b) => insn (a, r b);
rule shift (op: {s: Nat} → «2; Idx s» → Idx s, insn: {s: Nat} → «2; Idx s» → Idx (x86.gpr s)) (r: mark) {s: Nat} ( a b: Idx s): op (a, b) => insn (a, r b);
rule neg (op: Nat → {s: Nat} → «2; Idx s» → Idx s, insn: {s: Nat} → «2; Idx s» → Idx (x86.logic s)) (r: mark) {s: Nat} (m: Nat, a b: Idx s): op m (a, b) => let x = insn (a, r b); x86.xor (x, core.bit1.t 0 x);
rule mul8 (m: Nat, a b: I8): core.wrap.mul m (a, b) => x86.trunc i8 (x86.imul (mach.zext i32 (zx a), x86.movzx i8 i32 (reg_imm b)));
Division
div and idiv yield both the quotient and the remainder at i; they have no 8-bit form, so i8 widens to i32. core.rem puts its divisor into a register with x86.mov, whose result type only folds for a literal size.
rule div (op: {s a: Nat} → [mem.M a, «2; Idx s»] → [mem.M a, Idx s], insn: {s: Nat} → «2; Idx s» → «2; Idx (x86.gpr16 s)», i: Idx 2) {s: Nat} (m: mem.M 0, a b: Idx s): op (m, (a, b)) => (m, (insn (a, b))#i);
rule udiv8 (op: {s a: Nat} → [mem.M a, «2; Idx s»] → [mem.M a, Idx s], i: Idx 2) (m: mem.M 0, a b: I8 ): op (m, (a, b)) => (m, x86.trunc i8 (x86.div (mach.zext i32 (zx a), mach.zext i32 (zx b)))#i);
rule sdiv8 (op: {s a: Nat} → [mem.M a, «2; Idx s»] → [mem.M a, Idx s], i: Idx 2) (m: mem.M 0, a b: I8 ): op (m, (a, b)) => (m, x86.trunc i8 (x86.idiv (x86.movsx i8 i32 a, x86.movsx i8 i32 b))#i);
rule rem8 (d: Nat, x: I8): core.rem d x => x86.trunc i8 (x86.div (mach.zext i32 (zx x), x86.mov (core.idx 0 i32 d)))#1_2;
rule rem16 (d: Nat, x: I16): core.rem d x => (x86.div (x, x86.mov (core.idx 0 i16 d)))#1_2;
rule rem32 (d: Nat, x: I32): core.rem d x => (x86.div (x, x86.mov (core.idx 0 i32 d)))#1_2;
rule rem64 (d: Nat, x: I64): core.rem d x => (x86.div (x, x86.mov (core.idx 0 i64 d)))#1_2;
Conversions
mach.legalize leaves only register sizes; each family takes the source and the destination size. A conversion between sizes of the same register width truncates; rank is the register width of a size.
lam rank (s: Nat): Nat = (((1, 2)#(core.ncmp.e (s, i16)), 3)#(core.ncmp.e (s, i32)), 4)#(core.ncmp.e (s, i64));
lam narrows (s d: Nat): Bool = core.bit2.and 0 (core.ncmp.ne (s, d), core.ncmp.le (rank d, rank s));
lam widens (s d: Nat): Bool = core.ncmp.l (rank s, rank d);
rule trunc {s: Nat} {d: Nat} (x: Idx s): core.conv.u d x when narrows (s, d) => x86.trunc d x;
rule strunc {s: Nat} {d: Nat} (x: Idx s): core.conv.s d x when narrows (s, d) => x86.trunc d x;
rule zext {s: Nat} {d: Nat} (x: Idx s): core.conv.u d x when widens (s, d) => mach.zext d (zx x);
rule sext {s: Nat} {d: Nat} (x: Idx s): core.conv.s d x when widens (s, d) => x86.movsx s d x;
rule cast_trunc {s: Nat} {d: Nat} (x: Idx s): core.bitcast (Idx d) x when narrows (s, d) => x86.trunc d x;
rule cast_zext {s: Nat} {d: Nat} (x: Idx s): core.bitcast (Idx d) x when widens (s, d) => mach.zext d (zx x);
Comparisons
Each family takes the predicate and its condition code.
rule icmp (p: {s: Nat} → «2; Idx s» → Bool, c: mach.Flags → Bool) (s: Nat, a b: Idx s): p (a, b) => c (x86.cmp (a, reg_imm b));
Selects
x86.cmovcc's normalizer fuses a condition code into it; i8 and Bool, which it has no form for, go through i32.
rule select {s: Nat} (x y: Idx s, c: Bool): (x, y)#c => x86.cmovcc.ne (x86.test (c, c), x, y);
rule select8 {s: Nat} (x y: Idx s, c: Bool): (x, y)#c when core.bit2.or 0 (core.ncmp.e (s, i1), core.ncmp.e (s, i8)) => x86.trunc s (x86.cmovcc.ne (x86.test (c, c), mach.zext i32 (zx x), mach.zext i32 (zx y)));
Floating Point
Float instructions work on math.F pe values in XMM registers. A family takes the format as {pe: «2; Nat»}, which gen instantiates at each of formats that the mach.fpe or mach.fok in its rule's type admits. f2f takes both formats explicitly, since one of the same format would convert a float to itself. A family op with an insn maps a math operation onto an x86 instruction, rm folds a sole load into its source, and swap takes the converse predicate.
pub mod float
use mach.float;
let Cmp = {pe: «2; Nat»} → «2; math.F pe» → Idx (mach.fok pe 2);
Arithmetic
rule rm (op: Op2, insn: Insn2) {pe: «2; Nat»} (k: Nat, a: math.F pe, m: mem.M 0, p: I64): let (mm, v) = mach.load (math.F pe) (m, p); (mm, op k (a, v)) => x86.rm insn (m, a, addr p);
rule sqrt_rm {pe: «2; Nat»} (k: Nat, m: mem.M 0, p: I64): let (mm, v) = mach.load (math.F pe) (m, p); (mm, math.rt.sq k v) => x86.rm1 (x86.fsqrt @ pe) (m, addr p);
anx let arithmetic = (
arith (math.arith.add, x86.fadd), arith (math.arith.sub, x86.fsub),
arith (math.arith.mul, x86.fmul), arith (math.arith.div, x86.fdiv),
unary (math.abs, x86.fabs), unary (math.rt.sq, x86.fsqrt),
rm (math.arith.add, x86.fadd), rm (math.arith.sub, x86.fsub),
rm (math.arith.mul, x86.fmul), rm (math.arith.div, x86.fdiv));
Comparisons
ucomiss compares its register operand to its source, which may be a load.
swap takes the converse predicate, so that setcc already gets the unordered case right.
rule cmp (op: Pred, c: Cmp) {pe: «2; Nat»} (k: Nat, a b: math.F pe): op k (a, b) => c (a, b);
rule swap (op: Pred, c: Cmp) {pe: «2; Nat»} (k: Nat, a b: math.F pe): op k (a, b) => c (b, a);
rule cmp_rm (op: Pred, c: Cmp) {pe: «2; Nat»} (k: Nat, a: math.F pe, m: mem.M 0, p: I64): let (mm, v) = mach.load (math.F pe) (m, p); (mm, op k (a, v)) => x86.rm c (m, a, addr p);
rule swap_rm (op: Pred, c: Cmp) {pe: «2; Nat»} (k: Nat, a: math.F pe, m: mem.M 0, p: I64): let (mm, v) = mach.load (math.F pe) (m, p); (mm, op k (v, a)) => x86.rm c (m, a, addr p);
cmps folds a load into the second operand, swaps into the first via the converse, and syms of a symmetric predicate into either.
lam cmps (op: Pred, c: Cmp) = (cmp (op, c), cmp_rm (op, c));
lam swaps (op: Pred, c: Cmp) = (swap (op, c), swap_rm (op, c));
lam syms (op: Pred, c: Cmp) = (cmps (op, c), swap_rm (op, c));
anx let comparisons = (
syms (math.cmp.e, x86.fcmp.e), syms (math.cmp.ne, x86.fcmp.ne), syms (math.cmp.o, x86.fcmp.o),
syms (math.cmp.u, x86.fcmp.u), syms (math.cmp.ue, x86.fcmp.ue), syms (math.cmp.une, x86.fcmp.une),
cmps (math.cmp.g, x86.fcmp.g), cmps (math.cmp.ge, x86.fcmp.ge),
cmps (math.cmp.ul, x86.fcmp.ul), cmps (math.cmp.ule, x86.fcmp.ule),
swaps (math.cmp.l, x86.fcmp.g), swaps (math.cmp.le, x86.fcmp.ge),
swaps (math.cmp.ug, x86.fcmp.ul), swaps (math.cmp.uge, x86.fcmp.ule));
Selects, Loads, and Stores
rule select {pe: «2; Nat»} (x y: math.F pe, c: Bool): (x, y)#c => x86.fsel (c, x, y);
rule load {pe: «2; Nat»} (m: mem.M 0, p: I64): mach.load (math.F pe) (m, p) => x86.fload pe (m, addr p);
rule store {pe: «2; Nat»} (m: mem.M 0, p: I64, x: math.F pe): mach.store (m, p, x) => x86.fstore (m, addr p, x);
Conversions
The integer size is a family parameter, since neither side determines it.
A float of the same format converts to itself, so f2f only has the two mixed pairs.
u2f and f2u have only the i32 form: not every u64 is an exact f64; u2f reads its operand twice, so it folds no load.
rule s2f (s: Nat) {pe: «2; Nat»} (x: Idx s): math.conv.s2f pe x => x86.i2f s pe x;
rule u2f (s: Nat) {pe: «2; Nat»} (x: Idx s): math.conv.u2f pe x => x86.u2f s pe x;
rule f2s (s: Nat) {pe: «2; Nat»} (x: math.F pe): math.conv.f2s s x => x86.f2i pe s x;
rule f2u (s: Nat) {pe: «2; Nat»} (x: math.F pe): math.conv.f2u s x => x86.f2u pe s x;
rule f2f (spe: «2; Nat», dpe: «2; Nat») (x: math.F spe): math.conv.f2f dpe x => x86.f2f spe dpe x;
rule s2f_rm (s: Nat) {pe: «2; Nat»} (m: mem.M 0, p: I64): let (mm, v) = mach.load (Idx s) (m, p); (mm, math.conv.s2f pe v) => x86.rm1 (x86.i2f s pe) (m, addr p);
rule f2s_rm (s: Nat) {pe: «2; Nat»} (m: mem.M 0, p: I64): let (mm, v) = mach.load (math.F pe) (m, p); (mm, math.conv.f2s s v) => x86.rm1 (x86.f2i pe s) (m, addr p);
rule f2u_rm (s: Nat) {pe: «2; Nat»} (m: mem.M 0, p: I64): let (mm, v) = mach.load (math.F pe) (m, p); (mm, math.conv.f2u s v) => x86.rm1 (x86.f2u pe s) (m, addr p);
rule f2f_rm (spe: «2; Nat», dpe: «2; Nat») (m: mem.M 0, p: I64): let (mm, v) = mach.load (math.F spe) (m, p); (mm, math.conv.f2f dpe v) => x86.rm1 (x86.f2f spe dpe) (m, addr p);
anx let conversions = (
s2f i32, s2f i64, u2f i32,
f2s i32, f2s i64, f2u i32,
f2f (math.f32, math.f64), f2f (math.f64, math.f32),
s2f_rm i32, s2f_rm i64,
f2s_rm i32, f2s_rm i64, f2u_rm i32,
f2f_rm (math.f32, math.f64), f2f_rm (math.f64, math.f32));
end
Loads and Stores
mach.legalize has turned every pointer into an I64 and every mem.load and mem.store into a mach.load and mach.store.
rule load {s: Nat} (m: mem.M 0, p: I64): mach.load (Idx s) (m, p) => let (mm, v) = x86.load s (m, addr p); (mm, zx v);
rule store (s: Nat, m: mem.M 0, p: I64, x: Idx s): mach.store (m, p, x) => x86.store (m, addr p, reg_imm x);
rule sext_rm {s: Nat} {d: Nat} (m: mem.M 0, p: I64): let (mm, v) = mach.load (Idx s) (m, p); (mm, core.conv.s d v) when core.bit2.and 0 (widens (s, d), x86.is_gpr s) => x86.rm1 (x86.movsx s d) (m, addr p);
Memory Operands
op r, m folds a load into the second operand; op m, r also stores to the address it loads from. Either takes only a size that x86.load takes.
rule rm (c: Nat → Nat) (op: Nat → {s: Nat} → «2; Idx s» → Idx s, insn: {s: Nat} → «2; Idx s» → Idx (c s)) {s: Nat} (k: Nat, a: Idx s, m: mem.M 0, p: I64): let (mm, v) = mach.load (Idx s) (m, p); (mm, op k (a, v)) when x86.is_gpr s => x86.rm insn (m, a, addr p);
rule mr (c: Nat → Nat) (op: Nat → {s: Nat} → «2; Idx s» → Idx s, insn: {s: Nat} → «2; Idx s» → Idx (c s)) {s: Nat} (k: Nat, b: Idx s, m: mem.M 0, p: I64): let (mm, v) = mach.load (Idx s) (m, p); mach.store (mm, p, op k (v, b)) when x86.is_gpr s => x86.mr (insn @ s) (m, addr p, reg_imm b);
Addresses
A product with 2, 4, or 8, or a left shift by 1, 2, or 3, is an index with its scale; an address takes any index as one, scaled by 1 if need be. Of two matches that cost the same, isel keeps the first, so larger patterns come first. The core.wrap.add normalizer pulls a literal out of a nested sum, so a displacement sits outermost.
rule scale (op: Nat → {s: Nat} → «2; Idx s» → Idx s, k: I64, sc: I2) (n: Nat, i: I64): op n (i, k) => (i, sc);
Non-Terminals
A rule whose lhs is all of its meta variables converts between non-terminals; isel chains them. x86.addr and x86.lea normalize away what a literal makes trivial. A literal converts by mach.lit: into an absolute address for free, else into a register first; the builder of mov takes any size. A register becomes Zx by zero, and Zx a register for free; a load and mov yield Zx, as x86.emit zero-extends them.
rule base (x: I64): x => x86.addr (0I32, x);
rule abs (x: I64): x when mach.lit x => x86.addr (0I32, x);
rule mov (x: I64): x when mach.lit x => x86.mov x;
rule movz (x: I64): x when mach.lit x => zx (x86.mov x);
rule fmov (pe: «2; Nat») (x: math.F pe): x when mach.lit x => x86.fmov x;
rule lea (i: Bool) (a: x86.Addr i): a => x86.lea a;
rule scale1 (i: I64): i => (i, x86.scale_1);
rule unscale (s: [I64, I2]): s => x86.lea (x86.index (x86.addr (0I32, 0I64)) s);
rule zero (s: Nat) (x: Idx s): x => zx (x86.zero x);
rule unzx (s: Nat) (x: Idx s): zx x => x;
Instances
The values of a family's {pe: «2; Nat»}; the mach.fpes in its rule's type keep those x86 supports.
anx let formats = (math.f16, math.f32, math.f64, math.bf16, math.nvtf32, math.amdfp24, math.pxr24);
The values of a family's {s: Nat}: the register sizes that mach.legalize leaves.
anx let sizes = (i1, i8, i16, i32, i64);
let icmps = (
icmp (core.icmp.e, x86.cc.e), icmp (core.icmp.ne, x86.cc.ne), icmp (core.icmp.ul, x86.cc.b), icmp (core.icmp.ule, x86.cc.be),
icmp (core.icmp.ug, x86.cc.a), icmp (core.icmp.uge, x86.cc.ae), icmp (core.icmp.sl, x86.cc.l), icmp (core.icmp.sle, x86.cc.le),
icmp (core.icmp.sg, x86.cc.g), icmp (core.icmp.sge, x86.cc.ge));
imul has no 8-bit form, so mul8 widens to i32; the logical instructions also take Bool. x86 has no negated logical instructions, so nand, nor, and nxor complement with an xor.
anx let arithmetic = (
alu x86.gpr (core.wrap.add, x86.add) reg_imm,
alu x86.gpr (core.wrap.add, x86.add3) reg_imm,
alu x86.gpr (core.wrap.sub, x86.sub) reg_imm,
mul8,
alu x86.gpr16 (core.wrap.mul, x86.imul) reg_imm,
alu x86.logic (core.bit2.and, x86.and) reg_imm,
alu x86.logic (core.bit2.or, x86.or) reg_imm,
alu x86.logic (core.bit2.xor, x86.xor) reg_imm,
neg (core.bit2.nand, x86.and) reg_imm,
neg (core.bit2.nor, x86.or) reg_imm,
neg (core.bit2.nxor, x86.xor) reg_imm,
alu x86.gpr (core.wrap.shl, x86.shl) reg_sh,
shift (core.shr.l, x86.shr) reg_sh,
shift (core.shr.a, x86.sar) reg_sh);
A closed rule is an annex of its own, so the address forms follow arithmetic to lose a tie against its add.
rule index_disp (m n: Nat, p i c: I64): core.wrap.add n (core.wrap.add m (p, i), c) when imm c => x86.index (x86.addr (core.conv.u i32 c, p)) (scaled i);
rule base_index (m n: Nat, p x i: I64): core.wrap.add n (p, core.wrap.add m (x, i)) => x86.index (x86.addr (0I32, x86.lea (x86.index (x86.addr (0I32, p)) (x, x86.scale_1)))) (scaled i);
rule disp (m: Nat, p c: I64): core.wrap.add m (p, c) when imm c => x86.addr (core.conv.u i32 c, p);
rule index (m: Nat, p i: I64): core.wrap.add m (p, i) => x86.index (x86.addr (0I32, p)) (scaled i);
anx let compares = icmps;
anx let division = (
udiv8 (core.div.udiv, 0_2), div (core.div.udiv, x86.div, 0_2),
udiv8 (core.div.urem, 1_2), div (core.div.urem, x86.div, 1_2),
sdiv8 (core.div.sdiv, 0_2), div (core.div.sdiv, x86.idiv, 0_2),
sdiv8 (core.div.srem, 1_2), div (core.div.srem, x86.idiv, 1_2),
rem8, rem16, rem32, rem64);
anx let nonterminals = (base, abs, mov, fmov math.f32, fmov math.f64, lea ff, lea tt, scale1, unscale, movz, zero i8, unzx i8);
anx let addresses = (
scale (core.wrap.mul, 2I64, x86.scale_2), scale (core.wrap.mul, 4I64, x86.scale_4), scale (core.wrap.mul, 8I64, x86.scale_8),
scale (core.wrap.shl, 1I64, x86.scale_2), scale (core.wrap.shl, 2I64, x86.scale_4), scale (core.wrap.shl, 3I64, x86.scale_8),
index_disp, base_index, disp, index);
imul has no op m, r form.
anx let mem_ops = (
rm x86.gpr (core.wrap.add, x86.add),
rm x86.gpr (core.wrap.sub, x86.sub),
rm x86.gpr16 (core.wrap.mul, x86.imul),
rm x86.logic (core.bit2.and, x86.and),
rm x86.logic (core.bit2.or, x86.or),
rm x86.logic (core.bit2.xor, x86.xor),
mr x86.gpr (core.wrap.add, x86.add),
mr x86.gpr (core.wrap.sub, x86.sub),
mr x86.logic (core.bit2.and, x86.and),
mr x86.logic (core.bit2.or, x86.or),
mr x86.logic (core.bit2.xor, x86.xor));
Phases
gen
Writes the closed rules of this plugin as C++ matchers and builders (mim::plug::x86::rules) to -X x86_isel:o=<file>, or to stdout.