MimIR
MimIR is my Intermediate Representation
Loading...
Searching...
No Matches
The arm_isel Plugin

See also
mim::plug::arm_isel

The instruction-selection rules of the arm backend. gen writes them as C++ that the arm plugin compiles in, so the compiler itself never loads this plugin. AArch64 is three-address, and the emitter materializes a literal operand where the encoding has no immediate, so every hole is a register or a Zx.

Dependencies

plugin core;
plugin math;
import compile;
import mem;
plugin mach;
import arm;

Operand Markers

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, 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 becomes a meta variable of its rules, since no instruction checks it.

ALU

rule alu (op: Nat → {s: Nat} → «2; Idx s» → Idx s, insn: {s: Nat} → «2; Idx s» → Idx s) {s: Nat} (m: Nat, a b: Idx s): op m (a, b) => insn (a, b);
rule shift (op: {s: Nat} → «2; Idx s» → Idx s, insn: {s: Nat} → «2; Idx s» → Idx s) {s: Nat} (a b: Idx s): op (a, b) => insn (a, b);
rule neg (op: Nat → {s: Nat} → «2; Idx s» → Idx s, insn: {s: Nat} → «2; Idx s» → Idx s) {s: Nat} (m: Nat, a b: Idx s): op m (a, b) => let x = insn (a, b); arm.xor (x, core.bit1.t 0 x);

nand, nor, and nxor complement with an eor.

anx let arithmetic = (
alu (core.wrap.add, arm.add), alu (core.wrap.sub, arm.sub), alu (core.wrap.mul, arm.mul), alu (core.wrap.shl, arm.shl),
alu (core.bit2.and, arm.and), alu (core.bit2.or, arm.or), alu (core.bit2.xor, arm.xor),
neg (core.bit2.nand, arm.and), neg (core.bit2.nor, arm.or), neg (core.bit2.nxor, arm.xor),
shift (core.shr.l, arm.shr), shift (core.shr.a, arm.sar));

A sum with a product that nothing else uses is one madd.

rule madd {s: Nat} (m n: Nat, a b c: Idx s): core.wrap.add m (core.wrap.mul n (a, b), c) => arm.madd (a, b, c);

Division

udiv/sdiv yield the quotient only; a remainder is a mul and a sub around them. core.rem has a literal divisor.

let Div = {s a: Nat} → [mem.M a, «2; Idx s»] → [mem.M a, Idx s];
let Insn = {s: Nat} → «2; Idx s» → Idx s;
rule quot (op: Div, insn: Insn) {s: Nat} (m: mem.M 0, a b: Idx s): op (m, (a, b)) => (m, insn (a, b));
rule remainder (op: Div, insn: Insn) {s: Nat} (m: mem.M 0, a b: Idx s): op (m, (a, b)) => (m, arm.sub (a, arm.mul (insn (a, b), b)));
rule modulo {s: Nat} (d: Nat, x: Idx s): core.rem d x => arm.sub (x, arm.mul (arm.udiv (x, core.idx 0 s d), core.idx 0 s d));
anx let division = (
quot (core.div.udiv, arm.udiv), quot (core.div.sdiv, arm.sdiv),
remainder (core.div.urem, arm.udiv), remainder (core.div.srem, arm.sdiv));

Conversions

Each family takes the source and the destination size; rank orders the sizes by bit width.

lam rank (s: Nat): Nat = ((((0, 1)#(core.ncmp.e (s, i8)), 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.ncmp.l (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) => arm.trunc d x;
rule strunc {s: Nat} {d: Nat} (x: Idx s): core.conv.s d x when narrows (s, d) => arm.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) => arm.sext d x;
rule cast_trunc {s: Nat} {d: Nat} (x: Idx s): core.bitcast (Idx d) x when narrows (s, d) => arm.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 and Selects

Each icmp takes the predicate and its condition code. arm.csel's normalizer fuses a condition code into it, so a select on a comparison is one cmp and one csel. arm.cmp compares whole w registers, whose upper bits an i8 or i16 result of arithmetic leaves undefined, so a narrow comparison widens both operands first: as a Zx for an unsigned or equality predicate (icmp_zx), sign-extended for a signed one (icmp_sx).

lam narrow (s: Nat): Bool = core.bit2.or 0 (core.ncmp.e (s, i8), core.ncmp.e (s, i16));
lam wide (s: Nat): Bool = core.bit2.or 0 (core.ncmp.e (s, i1), core.bit2.or 0 (core.ncmp.e (s, i32), core.ncmp.e (s, i64)));
let ICmp = {s: Nat} → «2; Idx s» → Bool;
rule icmp (p: ICmp, c: mach.Flags → Bool) {s: Nat} (a b: Idx s): p (a, b) when wide s => c (arm.cmp (a, b));
rule icmp_zx (p: ICmp, c: mach.Flags → Bool) {s: Nat} (a b: Idx s): p (a, b) when narrow s => c (arm.cmp (mach.zext i32 (zx a), mach.zext i32 (zx b)));
rule icmp_sx (p: ICmp, c: mach.Flags → Bool) {s: Nat} (a b: Idx s): p (a, b) when narrow s => c (arm.cmp (arm.sext i32 a, arm.sext i32 b));
lam ucmps (p: ICmp, c: mach.Flags → Bool) = (icmp (p, c), icmp_zx (p, c));
lam scmps (p: ICmp, c: mach.Flags → Bool) = (icmp (p, c), icmp_sx (p, c));
anx let compares = (
ucmps (core.icmp.e, arm.cc.eq), ucmps (core.icmp.ne, arm.cc.ne), ucmps (core.icmp.ul, arm.cc.cc), ucmps (core.icmp.ule, arm.cc.ls),
ucmps (core.icmp.ug, arm.cc.hi), ucmps (core.icmp.uge, arm.cc.cs), scmps (core.icmp.sl, arm.cc.lt), scmps (core.icmp.sle, arm.cc.le),
scmps (core.icmp.sg, arm.cc.gt), scmps (core.icmp.sge, arm.cc.ge));
rule select {s: Nat} (x y: Idx s, c: Bool): (x, y)#c => arm.csel.ne (arm.cmp (c, 0_2), x, y);

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. A load yields a Zx, as ldrb/ldrh/ldr w clear the rest of the register.

rule load {s: Nat} (m: mem.M 0, p: I64): mach.load (Idx s) (m, p) => let (mm, v) = arm.load s (m, p); (mm, zx v);
rule store (s: Nat, m: mem.M 0, p: I64, x: Idx s): mach.store (m, p, x) => arm.store s (m, p, x);

Floating Point

Float instructions work on math.F pe values in the registers of arm.fpr. 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 admits. AArch64 has no memory operand, so no rule folds a load.

pub mod float
use mach.float;

Arithmetic

anx let arithmetic = (
arith (math.arith.add, arm.fadd), arith (math.arith.sub, arm.fsub),
arith (math.arith.mul, arm.fmul), arith (math.arith.div, arm.fdiv),
unary (math.abs, arm.fabs), unary (math.rt.sq, arm.fsqrt));

Comparisons

Each predicate is a condition code on the flags of arm.fcmp, except ne and ue, which take two.

rule cmp (op: Pred, c: mach.Flags → Bool) {pe: «2; Nat»} (k: Nat, a b: math.F pe): op k (a, b) => mach.fok pe (c (arm.fcmp (a, b)));
rule cmp2 (op: Pred, c d: mach.Flags → Bool) {pe: «2; Nat»} (k: Nat, a b: math.F pe): op k (a, b) => mach.fok pe (arm.or (c (arm.fcmp (a, b)), d (arm.fcmp (a, b))));
anx let comparisons = (
cmp (math.cmp.e, arm.cc.eq), cmp (math.cmp.l, arm.cc.mi), cmp (math.cmp.le, arm.cc.ls), cmp (math.cmp.g, arm.cc.gt),
cmp (math.cmp.ge, arm.cc.ge), cmp (math.cmp.o, arm.cc.vc), cmp (math.cmp.u, arm.cc.vs), cmp (math.cmp.ul, arm.cc.lt),
cmp (math.cmp.ule, arm.cc.le), cmp (math.cmp.ug, arm.cc.hi), cmp (math.cmp.uge, arm.cc.cs), cmp (math.cmp.une, arm.cc.ne),
cmp2 (math.cmp.ne, arm.cc.mi, arm.cc.gt), cmp2 (math.cmp.ue, arm.cc.eq, arm.cc.vs));

Selects, Loads, and Stores

rule select {pe: «2; Nat»} (x y: math.F pe, c: Bool): (x, y)#c => arm.fcsel.ne (arm.cmp (c, 0_2), x, y);
rule load {pe: «2; Nat»} (m: mem.M 0, p: I64): mach.load (math.F pe) (m, p) => arm.fload pe (m, p);
rule store {pe: «2; Nat»} (m: mem.M 0, p: I64, x: math.F pe): mach.store (m, p, x) => arm.fstore (m, p, x);

Conversions

The integer size is a family parameter, since neither side determines it.

f2f takes both formats explicitly and only has the two mixed pairs, since a float of the same format converts to itself.

rule s2f (s: Nat) {pe: «2; Nat»} (x: Idx s): math.conv.s2f pe x => arm.i2f s pe x;
rule u2f (s: Nat) {pe: «2; Nat»} (x: Idx s): math.conv.u2f pe x => arm.u2f s pe x;
rule f2s (s: Nat) {pe: «2; Nat»} (x: math.F pe): math.conv.f2s s x => arm.f2i pe s x;
rule f2u (s: Nat) {pe: «2; Nat»} (x: math.F pe): math.conv.f2u s x => arm.f2u pe s x;
rule f2f (spe: «2; Nat», dpe: «2; Nat») (x: math.F spe): math.conv.f2f dpe x => arm.f2f spe dpe x;

A narrower integer converts through a w register.

rule s2f_ext (s: Nat) {pe: «2; Nat»} (x: Idx s): math.conv.s2f pe x => arm.i2f i32 pe (arm.sext i32 x);
rule u2f_ext (s: Nat) {pe: «2; Nat»} (x: Idx s): math.conv.u2f pe x => arm.u2f i32 pe (mach.zext i32 (zx x));
rule f2s_trunc (s: Nat) {pe: «2; Nat»} (x: math.F pe): math.conv.f2s s x => arm.trunc s (arm.f2i pe i32 x);
rule f2u_trunc (s: Nat) {pe: «2; Nat»} (x: math.F pe): math.conv.f2u s x => arm.trunc s (arm.f2u pe i32 x);
anx let conversions = (
s2f i32, s2f i64, u2f i32, u2f i64,
f2s i32, f2s i64, f2u i32, f2u i64,
s2f_ext i8, s2f_ext i16, u2f_ext i8, u2f_ext i16,
f2s_trunc i8, f2s_trunc i16, f2u_trunc i8, f2u_trunc i16,
f2f (math.f32, math.f64), f2f (math.f64, math.f32));
end

A float literal becomes a register with fmov.

rule fmov {pe: «2; Nat»} (x: math.F pe): x when mach.lit x => arm.fmov x;

Non-Terminals

A literal stays a literal operand; the emitter puts it into a register where the encoding has no immediate. A register becomes a Zx by zero, which folds a literal, and a Zx a register for free.

rule mov (x: I64): x when mach.lit x => x;
rule zero (s: Nat) (x: Idx s): x => zx (arm.zero x);
rule unzx (s: Nat) (x: Idx s): zx x => x;
anx let nonterminals = (zero i8, unzx i8);

Instances

The values of a family's {s: Nat}: the register sizes that mach.legalize leaves.

anx let sizes = (i1, i8, i16, i32, i64);

The values of a family's {pe: «2; Nat»}.

anx let formats = (math.f32, math.f64);

Phases

gen

Writes the closed rules of this plugin as C++ matchers and builders (mim::plug::arm::rules) to -X arm_isel:o=<file>, or to stdout.

axm gen: compile.Phase;