use super::expr::{MOp, MbaExpr};
use crate::ops::BinOp;
use crate::{BitVec, Width};
fn demanded(p: &MbaExpr) -> (Vec<u16>, Vec<u16>) {
let nodes = p.nodes();
let mut d = vec![0u16; nodes.len()];
let mut least = vec![u16::MAX; nodes.len()];
if let Some(root) = nodes.last() {
d[nodes.len() - 1] = root.width.bits();
least[nodes.len() - 1] = root.width.bits();
}
for i in (0..nodes.len()).rev() {
let n = nodes[i];
let need = d[i];
if need == 0 {
continue;
}
let (a, b) = (n.args[0] as usize, n.args[1] as usize);
let mut raise = |k: usize, by: u16| {
let cap = nodes[k].width.bits();
let by = by.min(cap);
d[k] = d[k].max(by);
if by > 0 {
least[k] = least[k].min(by);
}
};
match n.op {
MOp::Const(_) | MOp::Var(_) => {}
MOp::Add | MOp::Sub | MOp::Mul | MOp::Or | MOp::Xor => {
raise(a, need);
raise(b, need);
}
MOp::And => {
let len = |k: usize| match nodes[k].op {
MOp::Const(c) => Some(bit_length(&c)),
_ => None,
};
raise(a, len(b).map_or(need, |l| need.min(l)));
raise(b, len(a).map_or(need, |l| need.min(l)));
}
MOp::Neg | MOp::Not | MOp::Trunc => raise(a, need),
MOp::Shl(k) => raise(a, need.saturating_sub(k)),
MOp::LShr(k) => raise(a, need.saturating_add(k)),
MOp::Zext => raise(a, need),
MOp::Sext => {
let aw = nodes[a].width.bits();
raise(a, if need > aw { aw } else { need });
}
}
}
(d, least)
}
fn bit_length(c: &BitVec) -> u16 {
let w = c.width().bits();
let clz = BitVec::apply_un(crate::ops::UnOp::Clz, c)
.ok()
.and_then(|v| v.to_u64())
.unwrap_or(u64::from(w));
w.saturating_sub(clz as u16)
}
fn narrow_registers(p: &MbaExpr, w: Width) -> Vec<Option<Width>> {
let nodes = p.nodes();
let polynomial = nodes.iter().any(|n| {
n.op == MOp::Mul
&& n.args[..2]
.iter()
.all(|&k| !matches!(nodes[k as usize].op, MOp::Const(_)))
});
if !polynomial {
return vec![None; nodes.len()];
}
let (d, least) = demanded(p);
let mut per_var: Vec<(u16, u16)> = vec![(0, u16::MAX); p.vars().len()];
for (i, n) in nodes.iter().enumerate() {
if let MOp::Var(v) = n.op
&& n.width == w
&& d[i] > 0
&& let Some(m) = per_var.get_mut(v as usize)
{
m.0 = m.0.max(d[i]);
m.1 = m.1.min(least[i]);
}
}
nodes
.iter()
.map(|n| match n.op {
MOp::Var(v) if n.width == w => {
let (most, least) = per_var.get(v as usize).copied().unwrap_or((0, 0));
(most > 0 && most == least && most < w.bits())
.then(|| Width::new(most).ok())
.flatten()
}
_ => None,
})
.collect()
}
pub(crate) fn decast(p: &MbaExpr) -> Option<MbaExpr> {
decast_in(p, false).map(|(m, _)| m)
}
pub(crate) fn decast_narrow(p: &MbaExpr) -> Option<(MbaExpr, bool)> {
decast_in(p, true)
}
fn decast_in(p: &MbaExpr, narrowing: bool) -> Option<(MbaExpr, bool)> {
let w = p.width()?;
let nodes = p.nodes();
let narrow = if narrowing {
narrow_registers(p, w)
} else {
vec![None; nodes.len()]
};
let narrowed = narrow.iter().any(Option::is_some);
if !nodes
.iter()
.any(|n| matches!(n.op, MOp::Zext | MOp::Sext | MOp::Trunc))
&& !narrowed
{
return None;
}
let mut m = MbaExpr::new(p.vars().to_vec());
let mut low: Vec<u32> = Vec::with_capacity(nodes.len());
let mut zeros_from: Vec<Option<u16>> = Vec::with_capacity(nodes.len());
let mut exact: Vec<Option<u32>> = vec![None; nodes.len()];
let wb = w.bits();
let mut exact_of =
|m: &mut MbaExpr, low: &[u32], zf: &[Option<u16>], i: usize, nw: Width| -> Option<u32> {
if nw >= w || zf[i].is_some_and(|z| z <= nw.bits()) {
return Some(low[i]);
}
if let Some(e) = exact[i] {
return Some(e);
}
let mask = BitVec::ones(nw).zext(w).ok()?;
let k = m.push(MOp::Const(mask), &[]).ok()?;
let e = m.push(MOp::And, &[low[i], k]).ok()?;
exact[i] = Some(e);
Some(e)
};
let lowmask_of = |k: usize| -> Option<u16> {
match nodes[k].op {
MOp::Const(c) => {
let n = bit_length(&c);
(n > 0
&& n < c.width().bits()
&& c == BitVec::ones(Width::new(n).ok()?).zext(c.width()).ok()?)
.then_some(n)
}
_ => None,
}
};
for (i, n) in nodes.iter().enumerate() {
let nw = n.width;
let a = n.args[0] as usize;
let b = n.args[1] as usize;
let (v, z): (u32, Option<u16>) = match n.op {
MOp::Const(c) => {
let c = if nw > w { c.trunc(w) } else { c.zext(w) }.ok()?;
(m.push(MOp::Const(c), &[]).ok()?, Some(bit_length(&c)))
}
MOp::Var(k) => {
let x = m.push(MOp::Var(k), &[]).ok()?;
match narrow[i] {
Some(nn) => {
let t = m.push_cast(MOp::Trunc, x, nn).ok()?;
(m.push_cast(MOp::Zext, t, w).ok()?, Some(nn.bits()))
}
None if nw == w => (x, None),
None if nw > w => (m.push_cast(MOp::Trunc, x, w).ok()?, None),
None => (m.push_cast(MOp::Zext, x, w).ok()?, Some(nw.bits())),
}
}
MOp::Add | MOp::Sub | MOp::Mul => (m.push(n.op, &[low[a], low[b]]).ok()?, None),
MOp::And => {
let dropped = match (lowmask_of(a), lowmask_of(b)) {
(Some(k), _) if zeros_from[b].is_some_and(|z| z <= k) => Some(b),
(_, Some(k)) if zeros_from[a].is_some_and(|z| z <= k) => Some(a),
_ => None,
};
match dropped {
Some(t) => (low[t], zeros_from[t]),
None => (
m.push(n.op, &[low[a], low[b]]).ok()?,
match (zeros_from[a], zeros_from[b]) {
(Some(x), Some(y)) => Some(x.min(y)),
(x, y) => x.or(y),
},
),
}
}
MOp::Or | MOp::Xor => (
m.push(n.op, &[low[a], low[b]]).ok()?,
match (zeros_from[a], zeros_from[b]) {
(Some(x), Some(y)) => Some(x.max(y)),
_ => None,
},
),
MOp::Shl(k) if k >= wb => (m.push(MOp::Const(BitVec::zero(w)), &[]).ok()?, Some(0)),
MOp::Neg | MOp::Not | MOp::Shl(_) => (m.push(n.op, &[low[a]]).ok()?, None),
MOp::LShr(_) if nodes[a].width > w => return None,
MOp::LShr(k) => {
let e = exact_of(&mut m, &low, &zeros_from, a, nodes[a].width)?;
(
m.push(n.op, &[e]).ok()?,
Some(nodes[a].width.bits().saturating_sub(k)),
)
}
MOp::Trunc => (low[a], zeros_from[a].filter(|&z| z <= nw.bits())),
MOp::Zext | MOp::Sext if nodes[a].width >= w => (low[a], zeros_from[a]),
MOp::Zext => (
exact_of(&mut m, &low, &zeros_from, a, nodes[a].width)?,
Some(nodes[a].width.bits()),
),
MOp::Sext => {
let aw = nodes[a].width;
let e = exact_of(&mut m, &low, &zeros_from, a, aw)?;
let one = BitVec::wrapping_from_u64(w, 1);
let by = BitVec::wrapping_from_u64(w, u64::from(aw.bits() - 1));
let s = BitVec::bin_unchecked(BinOp::Shl, &one, &by);
let k = m.push(MOp::Const(s), &[]).ok()?;
let f = m.push(MOp::Xor, &[e, k]).ok()?;
(m.push(MOp::Sub, &[f, k]).ok()?, None)
}
};
low.push(v);
zeros_from.push(z.map(|z| z.min(wb)));
}
let r = exact_of(&mut m, &low, &zeros_from, nodes.len() - 1, w)?;
if m.root() != Some(r) {
let z = m.push(MOp::Const(BitVec::zero(w)), &[]).ok()?;
m.push(MOp::Add, &[r, z]).ok()?;
}
Some((m, narrowed))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::hash::combine;
fn same_values(p: &MbaExpr) -> MbaExpr {
let (d, narrowed) = decast_narrow(p).expect("casts");
let plain = decast(p);
assert_eq!(
plain.is_some(),
!narrowed
|| p.nodes()
.iter()
.any(|n| matches!(n.op, MOp::Zext | MOp::Sext | MOp::Trunc))
);
if let Some(plain) = &plain {
assert!(!plain.nodes().iter().any(|n| n.op == MOp::Trunc));
}
let nodes = d.nodes();
assert!(nodes.iter().all(|n| n.op != MOp::Sext));
for (i, n) in nodes.iter().enumerate() {
if n.op == MOp::Trunc {
assert!(
nodes
.iter()
.any(|u| u.op == MOp::Zext && u.args[0] as usize == i),
"a truncation outside an atom in {d:?}"
);
assert!(matches!(nodes[n.args[0] as usize].op, MOp::Var(_)));
}
}
for s in 0..64u64 {
let vals: Vec<BitVec> = p
.vars()
.iter()
.enumerate()
.map(|(v, &w)| {
BitVec::wrapping_from_u64(
w,
combine(s, v as u64).wrapping_mul(0x9e37_79b9_7f4a_7c15),
)
})
.collect();
assert_eq!(p.eval(&vals), d.eval(&vals), "{p:?} at sample {s}");
if let Some(plain) = &plain {
assert_eq!(p.eval(&vals), plain.eval(&vals), "{p:?} at sample {s}");
}
}
d
}
#[test]
fn a_question_without_its_casts_has_its_values() {
let (w8, w32, w64) = (Width::W8, Width::new(32).unwrap(), Width::W64);
let mut p = MbaExpr::new(vec![w64, w64, Width::new(1).unwrap()]);
let x = p.push(MOp::Var(0), &[]).unwrap();
let y = p.push(MOp::Var(1), &[]).unwrap();
let f = p.push(MOp::Var(2), &[]).unwrap();
let t = p.push_cast(MOp::Trunc, x, w32).unwrap();
let five = p
.push(MOp::Const(BitVec::wrapping_from_u64(w32, 5)), &[])
.unwrap();
let m = p.push(MOp::Mul, &[t, five]).unwrap();
let s = p.push(MOp::LShr(3), &[t]).unwrap();
let a = p.push(MOp::Add, &[m, s]).unwrap();
let a = p.push_cast(MOp::Sext, a, w64).unwrap();
let kk = p
.push(MOp::Const(BitVec::wrapping_from_u64(w32, 0xdead_beef)), &[])
.unwrap();
let xk = p.push(MOp::Xor, &[t, kk]).unwrap();
let b8 = p.push_cast(MOp::Trunc, xk, w8).unwrap();
let nb = p.push(MOp::Not, &[b8]).unwrap();
let z = p.push_cast(MOp::Zext, nb, w64).unwrap();
let fz = p.push_cast(MOp::Zext, f, w64).unwrap();
let fz = p.push(MOp::Shl(6), &[fz]).unwrap();
let r = p.push(MOp::Add, &[a, z]).unwrap();
let r = p.push(MOp::Sub, &[r, y]).unwrap();
p.push(MOp::Or, &[r, fz]).unwrap();
same_values(&p);
}
#[test]
fn a_root_that_is_an_extension_is_the_last_node() {
let mut p = MbaExpr::new(vec![Width::W64]);
let x = p.push(MOp::Var(0), &[]).unwrap();
let t = p.push_cast(MOp::Trunc, x, Width::new(16).unwrap()).unwrap();
p.push_cast(MOp::Zext, t, Width::W64).unwrap();
same_values(&p);
}
#[test]
fn registers_read_through_masks_are_narrow_atoms() {
let w = Width::W64;
let mut p = MbaExpr::new(vec![w, w]);
let x = p.push(MOp::Var(0), &[]).unwrap();
let y = p.push(MOp::Var(1), &[]).unwrap();
let mask = p
.push(MOp::Const(BitVec::wrapping_from_u64(w, 0xffff_ffff)), &[])
.unwrap();
let and = p.push(MOp::And, &[x, y]).unwrap();
let and = p.push(MOp::And, &[and, mask]).unwrap();
let or = p.push(MOp::Or, &[x, y]).unwrap();
let or = p.push(MOp::And, &[or, mask]).unwrap();
p.push(MOp::Mul, &[and, or]).unwrap();
let d = same_values(&p);
let ops: Vec<MOp> = d.nodes().iter().map(|n| n.op).collect();
assert_eq!(ops.iter().filter(|o| **o == MOp::Trunc).count(), 2);
assert_eq!(ops.iter().filter(|o| **o == MOp::Zext).count(), 2);
assert_eq!(ops.iter().filter(|o| **o == MOp::And).count(), 1);
assert!(!d.nodes().iter().any(|n| {
n.op == MOp::And
&& n.args
.iter()
.any(|&a| matches!(ops[a as usize], MOp::Const(_)))
}));
}
#[test]
fn a_register_read_whole_is_not_narrowed() {
let w = Width::W64;
let mut p = MbaExpr::new(vec![w]);
let x = p.push(MOp::Var(0), &[]).unwrap();
let mask = p
.push(MOp::Const(BitVec::wrapping_from_u64(w, 0xffff_ffff)), &[])
.unwrap();
let t = p.push_cast(MOp::Trunc, x, Width::new(32).unwrap()).unwrap();
let z = p.push_cast(MOp::Zext, t, w).unwrap();
let _ = mask;
p.push(MOp::Add, &[z, x]).unwrap();
let d = same_values(&p);
assert!(!d.nodes().iter().any(|n| n.op == MOp::Trunc));
}
#[test]
fn reads_at_two_widths_keep_the_register() {
let w = Width::W64;
let mut p = MbaExpr::new(vec![w]);
let x = p.push(MOp::Var(0), &[]).unwrap();
let t8 = p.push_cast(MOp::Trunc, x, Width::W8).unwrap();
let t32 = p.push_cast(MOp::Trunc, x, Width::new(32).unwrap()).unwrap();
let z8 = p.push_cast(MOp::Zext, t8, w).unwrap();
let z32 = p.push_cast(MOp::Zext, t32, w).unwrap();
p.push(MOp::Mul, &[z8, z32]).unwrap();
let d = same_values(&p);
assert!(!d.nodes().iter().any(|n| n.op == MOp::Trunc));
}
#[test]
fn a_right_shift_raises_the_demand() {
let w = Width::W64;
let mut p = MbaExpr::new(vec![w, w]);
let x = p.push(MOp::Var(0), &[]).unwrap();
let y = p.push(MOp::Var(1), &[]).unwrap();
let s = p.push(MOp::LShr(8), &[x]).unwrap();
let mask = p
.push(MOp::Const(BitVec::wrapping_from_u64(w, 0xff)), &[])
.unwrap();
let hx = p.push(MOp::And, &[s, mask]).unwrap();
let mask16 = p
.push(MOp::Const(BitVec::wrapping_from_u64(w, 0xffff)), &[])
.unwrap();
let ly = p.push(MOp::And, &[y, mask16]).unwrap();
p.push(MOp::Mul, &[hx, ly]).unwrap();
let d = same_values(&p);
let atoms: Vec<Width> = d
.nodes()
.iter()
.filter(|n| n.op == MOp::Trunc)
.map(|n| n.width)
.collect();
assert_eq!(atoms, vec![Width::new(16).unwrap(); 2]);
}
#[test]
fn a_semi_linear_question_keeps_its_registers() {
let w = Width::W64;
let mut p = MbaExpr::new(vec![w, w]);
let x = p.push(MOp::Var(0), &[]).unwrap();
let y = p.push(MOp::Var(1), &[]).unwrap();
let mask = p
.push(MOp::Const(BitVec::wrapping_from_u64(w, 0xffff_ffff)), &[])
.unwrap();
let mx = p.push(MOp::And, &[x, mask]).unwrap();
let my = p.push(MOp::And, &[y, mask]).unwrap();
p.push(MOp::Xor, &[mx, my]).unwrap();
assert!(decast_narrow(&p).is_none());
}
}