use super::{
BinOp, Expr, Instruction, LaneOp, LaneParity, Lifter, Operand, Operation, VectorHalf, a64,
absolute_difference, by_halves, complement, doubling_mul_high, halves, immediate,
immediate_lane, lane, lane_shift, lane_value, lanewise, mask_bits, narrowed, replicate,
replicated, reverse_lanes, rounding_shift, shift_count, simd, source_halves, unzip, vector,
widened, window, write_narrow,
};
#[derive(Clone, Copy)]
enum Widening {
Add,
Sub,
AddWide,
SubWide,
Mul,
MulAdd,
MulSub,
AbsoluteDifference,
AbsoluteDifferenceAdd,
}
fn widening(opcode: a64::Opcode) -> Option<(Widening, bool, bool)> {
use Widening as W;
use a64::Opcode as O;
Some(match opcode {
O::SADDL => (W::Add, true, false),
O::SADDL2 => (W::Add, true, true),
O::UADDL => (W::Add, false, false),
O::UADDL2 => (W::Add, false, true),
O::SSUBL => (W::Sub, true, false),
O::SSUBL2 => (W::Sub, true, true),
O::USUBL => (W::Sub, false, false),
O::USUBL2 => (W::Sub, false, true),
O::SADDW => (W::AddWide, true, false),
O::SADDW2 => (W::AddWide, true, true),
O::UADDW => (W::AddWide, false, false),
O::UADDW2 => (W::AddWide, false, true),
O::SSUBW => (W::SubWide, true, false),
O::SSUBW2 => (W::SubWide, true, true),
O::USUBW => (W::SubWide, false, false),
O::USUBW2 => (W::SubWide, false, true),
O::SMULL => (W::Mul, true, false),
O::SMULL2 => (W::Mul, true, true),
O::UMULL => (W::Mul, false, false),
O::UMULL2 => (W::Mul, false, true),
O::SMLAL => (W::MulAdd, true, false),
O::SMLAL2 => (W::MulAdd, true, true),
O::UMLAL => (W::MulAdd, false, false),
O::UMLAL2 => (W::MulAdd, false, true),
O::SMLSL => (W::MulSub, true, false),
O::SMLSL2 => (W::MulSub, true, true),
O::UMLSL => (W::MulSub, false, false),
O::UMLSL2 => (W::MulSub, false, true),
O::SABDL => (W::AbsoluteDifference, true, false),
O::SABDL2 => (W::AbsoluteDifference, true, true),
O::UABDL => (W::AbsoluteDifference, false, false),
O::UABDL2 => (W::AbsoluteDifference, false, true),
O::SABAL => (W::AbsoluteDifferenceAdd, true, false),
O::SABAL2 => (W::AbsoluteDifferenceAdd, true, true),
O::UABAL => (W::AbsoluteDifferenceAdd, false, false),
O::UABAL2 => (W::AbsoluteDifferenceAdd, false, true),
_ => return None,
})
}
#[derive(Clone, Copy)]
enum Narrowing {
Truncate,
SaturateSigned { unsigned: bool },
SaturateUnsigned,
Shift { rounded: bool },
High { op: LaneOp, rounded: bool },
}
fn narrowing(opcode: a64::Opcode) -> Option<(Narrowing, bool)> {
use Narrowing as N;
use a64::Opcode as O;
let high = |op, rounded| N::High { op, rounded };
Some(match opcode {
O::XTN => (N::Truncate, false),
O::XTN2 => (N::Truncate, true),
O::SQXTN => (N::SaturateSigned { unsigned: false }, false),
O::SQXTN2 => (N::SaturateSigned { unsigned: false }, true),
O::SQXTUN => (N::SaturateSigned { unsigned: true }, false),
O::SQXTUN2 => (N::SaturateSigned { unsigned: true }, true),
O::UQXTN => (N::SaturateUnsigned, false),
O::UQXTN2 => (N::SaturateUnsigned, true),
O::SHRN => (N::Shift { rounded: false }, false),
O::SHRN2 => (N::Shift { rounded: false }, true),
O::RSHRN => (N::Shift { rounded: true }, false),
O::RSHRN2 => (N::Shift { rounded: true }, true),
O::ADDHN => (high(LaneOp::Add, false), false),
O::ADDHN2 => (high(LaneOp::Add, false), true),
O::RADDHN => (high(LaneOp::Add, true), false),
O::RADDHN2 => (high(LaneOp::Add, true), true),
O::SUBHN => (high(LaneOp::Sub, false), false),
O::SUBHN2 => (high(LaneOp::Sub, false), true),
O::RSUBHN => (high(LaneOp::Sub, true), false),
O::RSUBHN2 => (high(LaneOp::Sub, true), true),
_ => return None,
})
}
pub(super) fn lift_vector(lifter: &mut Lifter, instruction: &Instruction) -> Option<()> {
use a64::Opcode as O;
let operands = instruction.operands;
let lanes = |op, bits, a: Expr, b: Expr| Expr::apply(Operation::Lanes(op, bits), vec![a, b]);
match instruction.opcode {
O::SQDMULH => {
let Some(dest) = vector(operands[0]) else {
return None;
};
let Some(a) = vector(operands[1]) else {
return None;
};
let Some(b) = vector(operands[2]) else {
return None;
};
if !matches!(dest.2, 16 | 32)
|| (a.1, a.2) != (dest.1, dest.2)
|| (b.1, b.2) != (dest.1, dest.2)
{
return None;
}
let (low, high) =
by_halves(halves(a), halves(b), |a, b| doubling_mul_high(a, b, dest.2));
lifter.write_vector(dest, low, high);
}
O::ADD
| O::SUB
| O::MUL
| O::CMEQ
| O::CMGT
| O::CMHI
| O::CMGE
| O::CMHS
| O::SMIN
| O::SMAX
| O::UMIN
| O::UMAX
| O::SQADD
| O::UQADD
| O::SQSUB
| O::UQSUB
| O::URHADD
if vector(operands[0]).is_some() =>
{
let Some(dest) = vector(operands[0]) else {
return None;
};
let bits = dest.2;
let Some(first_source) = vector(operands[1]) else {
return None;
};
let Some(second_halves) = source_halves(operands[2]) else {
return None;
};
let (a, b) = (halves(first_source), second_halves);
let op = match instruction.opcode {
O::ADD => LaneOp::Add,
O::SUB => LaneOp::Sub,
O::MUL => LaneOp::Mul,
O::CMEQ => LaneOp::Equal,
O::CMGT => LaneOp::GreaterSigned,
O::CMHI => LaneOp::GreaterUnsigned,
O::CMGE => LaneOp::GreaterEqualSigned,
O::CMHS => LaneOp::GreaterEqualUnsigned,
O::SMIN => LaneOp::MinSigned,
O::SMAX => LaneOp::MaxSigned,
O::UMIN => LaneOp::MinUnsigned,
O::SQADD => LaneOp::AddSaturatingSigned,
O::UQADD => LaneOp::AddSaturatingUnsigned,
O::SQSUB => LaneOp::SubSaturatingSigned,
O::UQSUB => LaneOp::SubSaturatingUnsigned,
O::URHADD => LaneOp::Average,
_ => LaneOp::MaxUnsigned,
};
let low = lanes(op, bits, a.0, b.0);
let high = lanes(op, bits, a.1, b.1);
lifter.write_vector(dest, low, high);
}
O::ORR | O::BIC
if vector(operands[0]).is_some()
&& matches!(
operands[1],
Operand::ImmShift(..) | Operand::ImmShiftMSL(..)
) =>
{
let Some(dest) = vector(operands[0]) else {
return None;
};
let Some(lane) = immediate_lane(operands[1], false, dest.2) else {
return None;
};
let bits = Expr::constant(replicated(lane, dest.2));
let (low, high) = halves(dest);
let combine = |half: Expr| match instruction.opcode {
O::ORR => Expr::bin(BinOp::Or, half, bits.clone()),
_ => Expr::bin(
BinOp::And,
half,
Expr::bin(BinOp::Xor, bits.clone(), Expr::constant(u64::MAX)),
),
};
lifter.write_vector(dest, combine(low), combine(high));
}
O::CNT => {
let Some(dest) = vector(operands[0]) else {
return None;
};
let Some(first_source) = vector(operands[1]) else {
return None;
};
let (low, high) = halves(first_source);
lifter.write_vector(
dest,
lanes(LaneOp::PopCount, 8, low, Expr::constant(0)),
lanes(LaneOp::PopCount, 8, high, Expr::constant(0)),
);
}
O::UADDLV | O::SADDLV => {
let Some((slot, bits)) = simd(operands[0]) else {
return None;
};
let Some(source) = vector(operands[1]) else {
return None;
};
let (low, high) = halves(source);
let sum = Expr::apply(
Operation::SumLanesLong {
bits: source.2,
signed: instruction.opcode == O::SADDLV,
},
vec![low, high],
);
lifter.write_simd(slot, bits, (sum, Expr::constant(0)));
}
O::AND | O::ORR | O::EOR | O::BIC | O::ORN if vector(operands[0]).is_some() => {
let Some(dest) = vector(operands[0]) else {
return None;
};
let Some(first_source) = vector(operands[1]) else {
return None;
};
let Some(second_source) = vector(operands[2]) else {
return None;
};
let (a, b) = (halves(first_source), halves(second_source));
let combine = |x: Expr, y: Expr| match instruction.opcode {
O::AND => Expr::bin(BinOp::And, x, y),
O::ORR => Expr::bin(BinOp::Or, x, y),
O::EOR => Expr::bin(BinOp::Xor, x, y),
O::BIC => Expr::bin(
BinOp::And,
x,
Expr::bin(BinOp::Xor, y, Expr::constant(u64::MAX)),
),
_ => Expr::bin(
BinOp::Or,
x,
Expr::bin(BinOp::Xor, y, Expr::constant(u64::MAX)),
),
};
let (low, high) = (combine(a.0, b.0), combine(a.1, b.1));
lifter.write_vector(dest, low, high);
}
O::ADDV => {
let Some((slot, bits)) = simd(operands[0]) else {
return None;
};
let Some(source) = vector(operands[1]) else {
return None;
};
let (low, high) = halves(source);
let sum = Expr::apply(Operation::SumLanes(source.2), vec![low, high]);
lifter.write_simd(slot, bits, (sum, Expr::constant(0)));
}
O::UMAXV => {
let Some((slot, bits)) = simd(operands[0]) else {
return None;
};
let Some(source) = vector(operands[1]) else {
return None;
};
let (low, high) = halves(source);
let mut maximum = if source.1 {
lanes(LaneOp::MaxUnsigned, source.2, low, high)
} else {
low
};
let mut shift = 32;
while shift >= source.2 {
let upper = Expr::bin(BinOp::Shr, maximum.clone(), Expr::constant(shift.into()));
maximum = lanes(LaneOp::MaxUnsigned, source.2, maximum, upper);
shift /= 2;
}
lifter.write_simd(slot, bits, (maximum, Expr::constant(0)));
}
O::ADDP if simd(operands[0]).is_some() => {
let Some((slot, bits)) = simd(operands[0]) else {
return None;
};
let Some(first_source) = vector(operands[1]) else {
return None;
};
let (low, high) = halves(first_source);
lifter.write_simd(
slot,
bits,
(Expr::bin(BinOp::Add, low, high), Expr::constant(0)),
);
}
O::DUP if vector(operands[0]).is_some() => {
let Some(dest) = vector(operands[0]) else {
return None;
};
let value = match lane(operands[1]) {
Some(source) => lane_value(source),
None => match lifter.operand(operands[1], dest.2.max(32)) {
Some(value) => value,
None => return None,
},
};
let half = replicate(value, dest.2);
lifter.write_vector(dest, half.clone(), half);
}
O::SHL | O::USHR | O::SSHR if vector(operands[0]).is_some() => {
let Some(dest) = vector(operands[0]) else {
return None;
};
let Some(first_source) = vector(operands[1]) else {
return None;
};
let source = halves(first_source);
let Some(count) = shift_count(operands[2]) else {
return None;
};
let op = match instruction.opcode {
O::SHL => LaneOp::ShiftLeft,
O::USHR => LaneOp::ShiftRight,
_ => LaneOp::ShiftRightSigned,
};
let count = Expr::constant(count);
let (low, high) = (
lanes(op, dest.2, source.0, count.clone()),
lanes(op, dest.2, source.1, count),
);
lifter.write_vector(dest, low, high);
}
O::EXT => {
let Some(dest) = vector(operands[0]) else {
return None;
};
let Some(first_source) = vector(operands[1]) else {
return None;
};
let Some(second_source) = vector(operands[2]) else {
return None;
};
let (n, m) = (halves(first_source), halves(second_source));
let start = match operands[3] {
Operand::Imm64(value) => value,
other => match immediate(other) {
Some(value) => value,
None => return None,
},
};
let parts = if dest.1 {
vec![n.0, n.1, m.0, m.1]
} else {
vec![n.0, m.0]
};
let start = 8 * start;
let low = window(&parts, start);
let high = if dest.1 {
window(&parts, start + 64)
} else {
Expr::constant(0)
};
lifter.write_vector(dest, low, high);
}
O::CMLT | O::CMLE if vector(operands[0]).is_some() => {
let Some(dest) = vector(operands[0]) else {
return None;
};
let op = if instruction.opcode == O::CMLT {
LaneOp::GreaterSigned
} else {
LaneOp::GreaterEqualSigned
};
let Some(source) = vector(operands[1]) else {
return None;
};
let (low, high) = by_halves(
(Expr::constant(0), Expr::constant(0)),
halves(source),
|zero, x| lanewise(op, dest.2, zero, x),
);
lifter.write_vector(dest, low, high);
}
O::CMTST => {
let Some(dest) = vector(operands[0]) else {
return None;
};
let Some(first_source) = vector(operands[1]) else {
return None;
};
let Some(second_source) = vector(operands[2]) else {
return None;
};
let (low, high) = by_halves(halves(first_source), halves(second_source), |x, y| {
complement(lanewise(
LaneOp::Equal,
dest.2,
Expr::bin(BinOp::And, x, y),
Expr::constant(0),
))
});
lifter.write_vector(dest, low, high);
}
O::MLA | O::MLS => {
let Some(dest) = vector(operands[0]) else {
return None;
};
let bits = dest.2;
let Some(first_source) = vector(operands[1]) else {
return None;
};
let Some(second_halves) = source_halves(operands[2]) else {
return None;
};
let product = by_halves(halves(first_source), second_halves, |x, y| {
lanewise(LaneOp::Mul, bits, x, y)
});
let op = if instruction.opcode == O::MLA {
LaneOp::Add
} else {
LaneOp::Sub
};
let (low, high) = by_halves(halves(dest), product, |acc, p| lanewise(op, bits, acc, p));
lifter.write_vector(dest, low, high);
}
O::SABD | O::UABD | O::SABA | O::UABA => {
let Some(dest) = vector(operands[0]) else {
return None;
};
let bits = dest.2;
let signed = matches!(instruction.opcode, O::SABD | O::SABA);
let Some(first_source) = vector(operands[1]) else {
return None;
};
let Some(second_source) = vector(operands[2]) else {
return None;
};
let difference = by_halves(halves(first_source), halves(second_source), |x, y| {
absolute_difference(bits, signed, x, y)
});
let (low, high) = if matches!(instruction.opcode, O::SABA | O::UABA) {
by_halves(halves(dest), difference, |acc, d| {
lanewise(LaneOp::Add, bits, acc, d)
})
} else {
difference
};
lifter.write_vector(dest, low, high);
}
O::SHADD | O::UHADD | O::SRHADD | O::SHSUB | O::UHSUB => {
let Some(dest) = vector(operands[0]) else {
return None;
};
let bits = dest.2;
let signed = matches!(instruction.opcode, O::SHADD | O::SRHADD | O::SHSUB);
let half = |value: Expr| {
let op = if signed {
LaneOp::ShiftRightSigned
} else {
LaneOp::ShiftRight
};
lane_shift(op, bits, value, 1)
};
let Some(first_source) = vector(operands[1]) else {
return None;
};
let Some(second_source) = vector(operands[2]) else {
return None;
};
let (low, high) =
by_halves(
halves(first_source),
halves(second_source),
|x, y| match instruction.opcode {
O::SHADD | O::UHADD => lanewise(
LaneOp::Add,
bits,
Expr::bin(BinOp::And, x.clone(), y.clone()),
half(Expr::bin(BinOp::Xor, x, y)),
),
O::SRHADD => lanewise(
LaneOp::Sub,
bits,
Expr::bin(BinOp::Or, x.clone(), y.clone()),
half(Expr::bin(BinOp::Xor, x, y)),
),
_ => lanewise(
LaneOp::Sub,
bits,
lanewise(LaneOp::Sub, bits, half(x.clone()), half(y.clone())),
Expr::bin(
BinOp::And,
Expr::bin(BinOp::And, complement(x), y),
Expr::constant(replicated(1, bits)),
),
),
},
);
lifter.write_vector(dest, low, high);
}
O::ADDP | O::SMAXP | O::UMAXP | O::SMINP | O::UMINP if vector(operands[0]).is_some() => {
let Some(dest) = vector(operands[0]) else {
return None;
};
let bits = dest.2;
let Some(first_source) = vector(operands[1]) else {
return None;
};
let Some(second_source) = vector(operands[2]) else {
return None;
};
let (a, b) = (halves(first_source), halves(second_source));
let even = unzip(a.clone(), b.clone(), bits, dest.1, LaneParity::Even);
let odd = unzip(a, b, bits, dest.1, LaneParity::Odd);
let op = match instruction.opcode {
O::ADDP => LaneOp::Add,
O::SMAXP => LaneOp::MaxSigned,
O::UMAXP => LaneOp::MaxUnsigned,
O::SMINP => LaneOp::MinSigned,
_ => LaneOp::MinUnsigned,
};
let (low, high) = if bits == 64 {
by_halves(even, odd, |x, y| Expr::bin(BinOp::Add, x, y))
} else {
by_halves(even, odd, |x, y| lanewise(op, bits, x, y))
};
lifter.write_vector(dest, low, high);
}
O::NOT | O::NEG | O::ABS | O::SQNEG | O::SQABS | O::CLZ | O::CLS
if vector(operands[0]).is_some() =>
{
let Some(dest) = vector(operands[0]) else {
return None;
};
let bits = dest.2;
let negate = |x: Expr| lanewise(LaneOp::Sub, bits, Expr::constant(0), x);
let saturating_negate =
|x: Expr| lanewise(LaneOp::SubSaturatingSigned, bits, Expr::constant(0), x);
let unary_operation = |x: Expr| match instruction.opcode {
O::NOT => complement(x),
O::NEG => negate(x),
O::ABS => lanewise(LaneOp::MaxSigned, bits, x.clone(), negate(x)),
O::SQNEG => saturating_negate(x),
O::SQABS => lanewise(LaneOp::MaxSigned, bits, x.clone(), saturating_negate(x)),
O::CLZ => lanewise(LaneOp::CountLeadingZeros, bits, x, Expr::constant(0)),
_ => {
let neighbours = Expr::bin(
BinOp::Xor,
x.clone(),
lane_shift(LaneOp::ShiftRightSigned, bits, x, 1),
);
lanewise(
LaneOp::Sub,
bits,
lanewise(
LaneOp::CountLeadingZeros,
bits,
neighbours,
Expr::constant(0),
),
Expr::constant(replicated(1, bits)),
)
}
};
let Some(first_source) = vector(operands[1]) else {
return None;
};
let (low, high) = halves(first_source);
lifter.write_vector(dest, unary_operation(low), unary_operation(high));
}
O::BSL | O::BIT | O::BIF => {
let Some(dest) = vector(operands[0]) else {
return None;
};
let select = |mask: Expr, set: Expr, clear: Expr| {
Expr::bin(
BinOp::Or,
Expr::bin(BinOp::And, set, mask.clone()),
Expr::bin(BinOp::And, clear, complement(mask)),
)
};
let Some(first_source) = vector(operands[1]) else {
return None;
};
let Some(second_source) = vector(operands[2]) else {
return None;
};
let (d, n, m) = (halves(dest), halves(first_source), halves(second_source));
let select_bits = |d: Expr, n: Expr, m: Expr| match instruction.opcode {
O::BSL => select(d, n, m),
O::BIT => select(m, n, d),
_ => select(m, d, n),
};
let (low, high) = (select_bits(d.0, n.0, m.0), select_bits(d.1, n.1, m.1));
lifter.write_vector(dest, low, high);
}
O::USRA | O::SSRA | O::URSHR | O::SRSHR | O::URSRA | O::SRSRA
if vector(operands[0]).is_some() =>
{
let Some(dest) = vector(operands[0]) else {
return None;
};
let bits = dest.2;
let Some(count) = shift_count(operands[2]) else {
return None;
};
let signed = matches!(instruction.opcode, O::SSRA | O::SRSHR | O::SRSRA);
let rounded = matches!(
instruction.opcode,
O::URSHR | O::SRSHR | O::URSRA | O::SRSRA
);
let shift = |x: Expr| {
if rounded {
rounding_shift(bits, x, count, signed)
} else if signed {
lane_shift(LaneOp::ShiftRightSigned, bits, x, count)
} else {
lane_shift(LaneOp::ShiftRight, bits, x, count)
}
};
let Some(first_source) = vector(operands[1]) else {
return None;
};
let (x, y) = halves(first_source);
let shifted = (shift(x), shift(y));
let (low, high) = if matches!(instruction.opcode, O::URSHR | O::SRSHR) {
shifted
} else {
by_halves(halves(dest), shifted, |acc, s| {
lanewise(LaneOp::Add, bits, acc, s)
})
};
lifter.write_vector(dest, low, high);
}
O::SLI | O::SRI if vector(operands[0]).is_some() => {
let Some(dest) = vector(operands[0]) else {
return None;
};
let bits = dest.2;
let Some(count) = shift_count(operands[2]) else {
return None;
};
let lane = mask_bits(u64::from(bits));
let (op, inserted) = if instruction.opcode == O::SLI {
(LaneOp::ShiftLeft, (lane << count) & lane)
} else {
(
LaneOp::ShiftRight,
lane.checked_shr(count as u32).unwrap_or(0),
)
};
let kept = Expr::constant(!replicated(inserted, bits));
let Some(first_source) = vector(operands[1]) else {
return None;
};
let (low, high) = by_halves(halves(dest), halves(first_source), |d, x| {
Expr::bin(
BinOp::Or,
Expr::bin(BinOp::And, d, kept.clone()),
lane_shift(op, bits, x, count),
)
});
lifter.write_vector(dest, low, high);
}
O::ZIP1 | O::ZIP2 => {
let Some(dest) = vector(operands[0]) else {
return None;
};
let bits = dest.2;
let Some(first_source) = vector(operands[1]) else {
return None;
};
let Some(second_source) = vector(operands[2]) else {
return None;
};
let (a, b) = (halves(first_source), halves(second_source));
let second = instruction.opcode == O::ZIP2;
let interleave = |op: fn(u8) -> Operation, x: &Expr, y: &Expr| {
Expr::apply(op(bits), vec![x.clone(), y.clone()])
};
let (low, high) = if bits == 64 {
if second { (a.1, b.1) } else { (a.0, b.0) }
} else if dest.1 {
let (x, y) = if second { (a.1, b.1) } else { (a.0, b.0) };
(
interleave(Operation::InterleaveLow, &x, &y),
interleave(Operation::InterleaveHigh, &x, &y),
)
} else if second {
(
interleave(Operation::InterleaveHigh, &a.0, &b.0),
Expr::constant(0),
)
} else {
(
interleave(Operation::InterleaveLow, &a.0, &b.0),
Expr::constant(0),
)
};
lifter.write_vector(dest, low, high);
}
O::UZP1 | O::UZP2 => {
let Some(dest) = vector(operands[0]) else {
return None;
};
let Some(first_source) = vector(operands[1]) else {
return None;
};
let Some(second_source) = vector(operands[2]) else {
return None;
};
let (a, b) = (halves(first_source), halves(second_source));
let (low, high) = unzip(
a,
b,
dest.2,
dest.1,
if instruction.opcode == O::UZP2 {
LaneParity::Odd
} else {
LaneParity::Even
},
);
lifter.write_vector(dest, low, high);
}
O::TRN1 | O::TRN2 => {
let Some(dest) = vector(operands[0]) else {
return None;
};
let bits = dest.2;
let Some(first_source) = vector(operands[1]) else {
return None;
};
let Some(second_source) = vector(operands[2]) else {
return None;
};
let (a, b) = (halves(first_source), halves(second_source));
let (low, high) = if bits == 64 {
if instruction.opcode == O::TRN1 {
(a.0, b.0)
} else {
(a.1, b.1)
}
} else {
let even = replicated(mask_bits(u64::from(bits)), 2 * bits);
let count = Expr::constant(u64::from(bits));
by_halves(a, b, |x, y| {
if instruction.opcode == O::TRN1 {
Expr::bin(
BinOp::Or,
Expr::bin(BinOp::And, x, Expr::constant(even)),
Expr::bin(
BinOp::Shl,
Expr::bin(BinOp::And, y, Expr::constant(even)),
count.clone(),
),
)
} else {
Expr::bin(
BinOp::Or,
Expr::bin(
BinOp::And,
Expr::bin(BinOp::Shr, x, count.clone()),
Expr::constant(even),
),
Expr::bin(BinOp::And, y, Expr::constant(!even)),
)
}
})
};
lifter.write_vector(dest, low, high);
}
O::SSHLL | O::SSHLL2 | O::USHLL | O::USHLL2 | O::SHLL | O::SHLL2 => {
let Some(dest) = vector(operands[0]) else {
return None;
};
let bits = dest.2 / 2;
let count = shift_count(operands[2]).unwrap_or(u64::from(bits));
let upper = matches!(instruction.opcode, O::SSHLL2 | O::USHLL2 | O::SHLL2);
let signed = matches!(instruction.opcode, O::SSHLL | O::SSHLL2);
let Some(first_source) = vector(operands[1]) else {
return None;
};
let (low, high) = widened(
halves(first_source),
if upper {
VectorHalf::Upper
} else {
VectorHalf::Lower
},
bits,
signed,
);
let shift = |x: Expr| {
if count == 0 {
x
} else {
lane_shift(LaneOp::ShiftLeft, dest.2, x, count)
}
};
lifter.write_vector(dest, shift(low), shift(high));
}
_ if let Some((form, signed, upper)) = widening(instruction.opcode)
&& vector(operands[0]).is_some() =>
{
let Some(dest) = vector(operands[0]) else {
return None;
};
let wide = dest.2;
let extend = |operand: (Expr, Expr)| {
widened(
operand,
if upper {
VectorHalf::Upper
} else {
VectorHalf::Lower
},
wide / 2,
signed,
)
};
let Some(first_source) = vector(operands[1]) else {
return None;
};
let first = halves(first_source);
let first = if matches!(form, Widening::AddWide | Widening::SubWide) {
first
} else {
extend(first)
};
let Some(second_halves) = source_halves(operands[2]) else {
return None;
};
let second = extend(second_halves);
let (low, high) = by_halves(first, second, |x, y| match form {
Widening::Add | Widening::AddWide => lanewise(LaneOp::Add, wide, x, y),
Widening::Sub | Widening::SubWide => lanewise(LaneOp::Sub, wide, x, y),
Widening::AbsoluteDifference | Widening::AbsoluteDifferenceAdd => {
absolute_difference(wide, signed, x, y)
}
Widening::Mul | Widening::MulAdd | Widening::MulSub => {
lanewise(LaneOp::Mul, wide, x, y)
}
});
let accumulate = match form {
Widening::MulAdd | Widening::AbsoluteDifferenceAdd => Some(LaneOp::Add),
Widening::MulSub => Some(LaneOp::Sub),
_ => None,
};
let (low, high) = match accumulate {
Some(op) => by_halves(halves(dest), (low, high), |acc, x| {
lanewise(op, wide, acc, x)
}),
None => (low, high),
};
lifter.write_vector(dest, low, high);
}
_ if let Some((form, upper)) = narrowing(instruction.opcode)
&& vector(operands[0]).is_some() =>
{
let Some(dest) = vector(operands[0]) else {
return None;
};
let Some(first_source) = vector(operands[1]) else {
return None;
};
let wide = first_source.2;
let narrow = u64::from(wide / 2);
let Some(first_source) = vector(operands[1]) else {
return None;
};
let source = halves(first_source);
let value = match form {
Narrowing::Truncate => narrowed(source, wide),
Narrowing::SaturateSigned { unsigned } => Expr::apply(
Operation::Pack {
from: wide,
unsigned,
},
vec![source.0, source.1],
),
Narrowing::SaturateUnsigned => {
let limit = Expr::constant(replicated(mask_bits(narrow), wide));
narrowed(
by_halves(source, (limit.clone(), limit), |x, l| {
lanewise(LaneOp::MinUnsigned, wide, x, l)
}),
wide,
)
}
Narrowing::Shift { rounded } => {
let Some(count) = shift_count(operands[2]) else {
return None;
};
let shift = |x: Expr| {
let x = if rounded {
let round = Expr::constant(replicated(1 << (count - 1), wide));
lanewise(LaneOp::Add, wide, x, round)
} else {
x
};
lane_shift(LaneOp::ShiftRight, wide, x, count)
};
narrowed((shift(source.0), shift(source.1)), wide)
}
Narrowing::High { op, rounded } => {
let round = Expr::constant(replicated(1 << (narrow - 1), wide));
let Some(second_source) = vector(operands[2]) else {
return None;
};
let each = by_halves(source, halves(second_source), |x, y| {
let value = lanewise(op, wide, x, y);
let value = if rounded {
lanewise(LaneOp::Add, wide, value, round.clone())
} else {
value
};
lane_shift(LaneOp::ShiftRight, wide, value, narrow)
});
narrowed(each, wide)
}
};
write_narrow(lifter, dest, upper, value);
}
O::SADDLP | O::UADDLP | O::SADALP | O::UADALP => {
let Some(dest) = vector(operands[0]) else {
return None;
};
let wide = dest.2;
let bits = u64::from(wide / 2);
let signed = matches!(instruction.opcode, O::SADDLP | O::SADALP);
let pairs = |x: Expr| {
let (even, odd) = if signed {
(
lane_shift(
LaneOp::ShiftRightSigned,
wide,
lane_shift(LaneOp::ShiftLeft, wide, x.clone(), bits),
bits,
),
lane_shift(LaneOp::ShiftRightSigned, wide, x, bits),
)
} else {
(
Expr::bin(
BinOp::And,
x.clone(),
Expr::constant(replicated(mask_bits(bits), wide)),
),
lane_shift(LaneOp::ShiftRight, wide, x, bits),
)
};
lanewise(LaneOp::Add, wide, even, odd)
};
let Some(first_source) = vector(operands[1]) else {
return None;
};
let (x, y) = halves(first_source);
let sums = (pairs(x), pairs(y));
let (low, high) = if matches!(instruction.opcode, O::SADALP | O::UADALP) {
by_halves(halves(dest), sums, |acc, s| {
lanewise(LaneOp::Add, wide, acc, s)
})
} else {
sums
};
lifter.write_vector(dest, low, high);
}
O::REV16 | O::REV32 | O::REV64 | O::RBIT if vector(operands[0]).is_some() => {
let Some(dest) = vector(operands[0]) else {
return None;
};
let each = |half: Expr| match instruction.opcode {
O::REV16 => reverse_lanes(half, dest.2, 16),
O::REV32 => reverse_lanes(half, dest.2, 32),
O::REV64 => reverse_lanes(half, dest.2, 64),
_ => Expr::apply(Operation::ReverseBits(64), vec![reverse_lanes(half, 8, 64)]),
};
let Some(first_source) = vector(operands[1]) else {
return None;
};
let (low, high) = halves(first_source);
lifter.write_vector(dest, each(low), each(high));
}
_ => return None,
}
Some(())
}