bitwright 0.27.0

Hash-consed fixed-width bit-vector expressions: exact evaluation, bit-level facts, and verified simplification.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
//! The invert pass: equalities through invertible maps (see `crate::invert`).
//!
//! At `a == b` and `a != b`:
//!
//! - both sides one injective layer over different operands (`f(x) == f(y)`, `f` fixed by the
//!   operands the sides share): the operands are compared instead, layer after layer;
//! - against a constant (`f(x) == c`): `x` is compared with the preimage `f⁻¹(c)`, through
//!   every layer whose parameters are constants, and the comparison is a constant when `c` has
//!   no preimage;
//! - when neither of those decides it, truncations and slices (`extract<lo, k>(f(x))`) pushed
//!   into their operands and tried again at their own width, where a map that is no bijection at
//!   the full width can be one of the bits read (`trunc<17>(2x² + x) == trunc<17>(2y² + y)` is
//!   `trunc<17>(x) == trunc<17>(y)`): committed only when the result is a smaller tree;
//! - `a₁ | … | aₙ == 0` (and `a₁ & … & aₙ == ones`): every `aᵢ` is solved against the constant
//!   on its own, and the answers are intersected, so one value compared with two different
//!   constants is false;
//! - the sides `select` trees: their leaves are peeled together through one common injective
//!   layer, a constant leaf by its preimage (`select(c, x + 1, y + 1) == z + 1` is
//!   `select(c, x, y) == z`).
//!
//! At `a <u b`, `a <=u b`, `a <s b` and `a <=s b` (see `crate::invert::monotone`):
//!
//! - both sides one strictly monotone layer over different operands (`f(x) <u f(y)`), or
//!   `select` trees whose leaves all are: the operands (the trees over the inner values) are
//!   compared instead, in the order the layer is monotone from, swapped for a decreasing layer,
//!   layer after layer (`zext(x) <s zext(y)` is `x <u y`, `~x <u ~y` is `y <u x`, `x + k <u
//!   y + k` is `x <u y` where neither sum wraps, `select(c, ~x, ~y) <u ~z` is
//!   `z <u select(c, x, y)`);
//! - against a constant on either side (`f(x) <u c`): `x` is compared with the threshold where
//!   the comparison changes, found by bisection over `x`'s range, or the comparison is a
//!   constant (`zext<16>(x) <u 300` at 8 bits is true).
//!
//! A bijection does not preserve order, so the injective layers above are never used for
//! orderings; a strictly monotone layer is injective, but its monotonicity usually rests on
//! facts, so equalities keep to the layers above. A rewrite replaces a comparison by one of proper subterms of its operands and a
//! constant, or by a constant, so like a rule it strictly decreases the termination order, and
//! it is committed whether or not the operands are shared elsewhere; a split is committed only
//! when its result is a smaller tree.

use super::{Accept, By, Fin, PassKind, Reject, Runner, Step, Stop, discard, facts};
use crate::BitVec;
use crate::engine::budget::Counter;
use crate::expr::{Context, OpCode};
use crate::facts::Facts;
use crate::hash::IdMap;
use crate::invert::monotone::{self, Inner, Order};
use crate::invert::{self, MAX_SPLIT, Oracle, Solved};
use crate::ops::{BinOp, CmpOp};

/// Facts and work for the analysis, from the call: facts under the run's assumptions and fact
/// budget (what they rely on, and whether a cap cut them, collected in `fin`), work charged to
/// `pass_work`.
struct Calls<'x, 'r, 'a> {
    r: &'x mut Runner<'r, 'a>,
    fin: Fin,
}

impl Oracle for Calls<'_, '_, '_> {
    type Err = Stop;

    fn facts(&mut self, cx: &mut Context, i: u32) -> Result<Option<Facts>, Stop> {
        let (f, fin) = facts(self.r, cx, i)?;
        self.fin = self.fin.and(fin);
        Ok(f)
    }

    fn charge(&mut self, units: u64) -> Result<(), Stop> {
        self.r.meter.charge(Counter::PassWork, units)?;
        Ok(())
    }
}

fn count<'x>(r: &'x mut Runner<'_, '_>) -> &'x mut crate::engine::PassCounts {
    r.stats.passes.entry(PassKind::Invert.name()).or_default()
}

/// The invert pass at `n`.
pub(super) fn step(r: &mut Runner<'_, '_>, cx: &mut Context, n: u32) -> Result<Step, Stop> {
    let node = cx.node(n);
    let Some(op) = node.op.as_cmp() else {
        return Ok(Step::Normal(Fin::FINAL));
    };
    let before = cx.len() as u32;
    let mut o = Calls { r, fin: Fin::FINAL };
    let e = if op.is_commutative() {
        // Constants are on the right of a stored equality.
        match cx.const_val(node.b) {
            Some(c) => {
                let join = cx.node(node.a).op;
                if (join == OpCode::Or && c.is_zero()) || (join == OpCode::And && c.is_ones()) {
                    split(cx, &mut o, op, n, node.a, &c, before)?
                } else {
                    match solve(cx, &mut o, op, node.a, &c)? {
                        Some(e) => Some(e),
                        None => match narrowed(cx, &mut o, node.a)? {
                            Some(a) => solve(cx, &mut o, op, a, &c)?,
                            None => None,
                        }
                        .filter(|&e| smaller(cx, e, n)),
                    }
                }
            }
            None => match cancel(cx, &mut o, op, node.a, node.b)? {
                Some(e) => Some(e),
                None => match (narrowed(cx, &mut o, node.a)?, narrowed(cx, &mut o, node.b)?) {
                    (Some(a), Some(b)) => cancel(cx, &mut o, op, a, b)?,
                    _ => None,
                }
                .filter(|&e| smaller(cx, e, n)),
            },
        }
    } else {
        match (cx.const_val(node.a), cx.const_val(node.b)) {
            (_, Some(c)) => solve_order(cx, &mut o, op, node.a, &c, false)?,
            (Some(c), None) => solve_order(cx, &mut o, op, node.b, &c, true)?,
            (None, None) => cancel_order(cx, &mut o, op, node.a, node.b)?,
        }
    };
    let fin = o.fin;
    let e = match e {
        Some(e) if e != n => e,
        _ => {
            // What a narrowing built for nothing.
            discard(r, cx, before);
            count(r).noop += 1;
            return Ok(Step::Normal(fin));
        }
    };
    match r.accept(cx, By::Pass(PassKind::Invert.name()), true, n, e, fin.rel)? {
        Accept::Yes => {
            count(r).changed += 1;
            Ok(Step::To(e, fin))
        }
        Accept::Vetoed => {
            count(r).rejected += 1;
            discard(r, cx, before);
            Ok(Step::Normal(fin))
        }
        Accept::Rejected(reason) => {
            count(r).rejected += 1;
            discard(r, cx, before);
            if matches!(reason, Reject::Width | Reject::Verify | Reject::Tripwire) {
                r.quarantined_passes.push(PassKind::Invert);
                r.stats.quarantined += 1;
            }
            Ok(Step::Normal(fin))
        }
    }
}

/// A truncation or slice `extract<lo, k>(x)` pushed into `x` (see `casts`), when that is
/// another node: a map that is no bijection at `x`'s width can be one of the bits the slice
/// reads (`trunc<17>(2x² + x)` is `2t² + t` over `t = trunc<17>(x)`, a bijection of `t`).
fn narrowed(cx: &mut Context, o: &mut Calls<'_, '_, '_>, s: u32) -> Result<Option<u32>, Stop> {
    let node = cx.node(s);
    if node.op != OpCode::Extract {
        return Ok(None);
    }
    let Some((e, fin)) = super::casts::narrowed(o.r, cx, node.a, node.b as u16, node.width)? else {
        return Ok(None);
    };
    o.fin = o.fin.and(fin);
    Ok((e != s).then_some(e))
}

/// Whether `e` is a smaller tree than `n`: a comparison of narrowed sides is a comparison of
/// subterms of new nodes, committed only when it is smaller (which also orders it).
fn smaller(cx: &Context, e: u32, n: u32) -> bool {
    let size = |i: u32| cx.tree_size(cx.handle(i)).unwrap_or(u32::MAX);
    size(e) < size(n)
}

fn truth(o: &mut Calls<'_, '_, '_>, cx: &mut Context, v: bool) -> Result<u32, Stop> {
    o.r.build(cx, |cx| cx.mk_const(&BitVec::from_bool(v)))
}

/// `x op v`.
fn compare(
    o: &mut Calls<'_, '_, '_>,
    cx: &mut Context,
    op: CmpOp,
    x: u32,
    v: &BitVec,
) -> Result<u32, Stop> {
    let v = *v;
    o.r.build(cx, |cx| {
        let k = cx.mk_const(&v)?;
        cx.c_cmp(op, x, k)
    })
}

/// The leaves of the `select` trees at `a` and `b` (the nodes under their arms that are not
/// selects, without repeats), and whether any select was seen; `None` past [`MAX_SPLIT`]
/// leaves.
fn leaves(
    cx: &Context,
    o: &mut Calls<'_, '_, '_>,
    a: u32,
    b: u32,
) -> Result<Option<(Vec<u32>, bool)>, Stop> {
    let mut out = Vec::new();
    let mut seen = false;
    let mut stack = vec![b, a];
    while let Some(i) = stack.pop() {
        o.charge(1)?;
        let node = cx.node(i);
        if node.op == OpCode::Select {
            seen = true;
            stack.push(node.c);
            stack.push(node.b);
        } else if out.contains(&i) {
        } else if out.len() >= MAX_SPLIT {
            return Ok(None);
        } else {
            out.push(i);
        }
    }
    Ok(Some((out, seen)))
}

/// `t` with each leaf replaced by its inner value (`map`), its selects rebuilt.
fn relabel(
    cx: &mut Context,
    o: &mut Calls<'_, '_, '_>,
    t: u32,
    map: &IdMap<u32, u32>,
) -> Result<u32, Stop> {
    if let Some(&x) = map.get(&t) {
        return Ok(x);
    }
    let node = cx.node(t);
    debug_assert_eq!(node.op, OpCode::Select);
    let (c, p, q) = (node.a, node.b, node.c);
    let p = relabel(cx, o, p, map)?;
    let q = relabel(cx, o, q, map)?;
    o.r.build(cx, |cx| cx.c_select(c, p, q))
}

/// The leaves of `a` and `b` as their inner values: the two trees over them.
fn relabelled(
    cx: &mut Context,
    o: &mut Calls<'_, '_, '_>,
    a: u32,
    b: u32,
    leaves: &[u32],
    inners: &[Inner],
) -> Result<(u32, u32), Stop> {
    let mut map = IdMap::default();
    for (&l, x) in leaves.iter().zip(inners) {
        let x = match x {
            Inner::Node(x) => *x,
            Inner::Const(v) => {
                let v = *v;
                o.r.build(cx, |cx| cx.mk_const(&v))?
            }
        };
        map.insert(l, x);
    }
    Ok((relabel(cx, o, a, &map)?, relabel(cx, o, b, &map)?))
}

/// `f(x) op f(y)` as `x op y`, through every common injective layer; then, when the sides are
/// `select` trees, their leaves peeled together through every common injective layer.
fn cancel(
    cx: &mut Context,
    o: &mut Calls<'_, '_, '_>,
    op: CmpOp,
    a: u32,
    b: u32,
) -> Result<Option<u32>, Stop> {
    let (mut x, mut y) = (a, b);
    let mut peeled = false;
    while let Some((p, q)) = invert::cancel_layer(cx, o, x, y)? {
        o.charge(1)?;
        (x, y) = (p, q);
        peeled = true;
    }
    if !peeled {
        while let Some((ls, true)) = leaves(cx, o, x, y)? {
            let Some(inners) = invert::cancel_leaves(cx, o, &ls)? else {
                break;
            };
            (x, y) = relabelled(cx, o, x, y, &ls, &inners)?;
            peeled = true;
        }
    }
    if !peeled {
        return Ok(None);
    }
    Ok(Some(o.r.build(cx, |cx| cx.c_cmp(op, x, y))?))
}

/// `f(x) op c` as `x op f⁻¹(c)`, or a constant.
fn solve(
    cx: &mut Context,
    o: &mut Calls<'_, '_, '_>,
    op: CmpOp,
    lhs: u32,
    c: &BitVec,
) -> Result<Option<u32>, Stop> {
    let mut peels = Vec::new();
    let explaining = o.r.explaining();
    let solved = invert::solve_eq_peels(cx, o, lhs, c, explaining.then_some(&mut peels))?;
    if explaining && solved.is_some() {
        let layers = peels
            .into_iter()
            .map(|(node, l, preimage)| crate::engine::Peel {
                node: cx.handle(node),
                inner: cx.handle(l.inner),
                map: l.map.spelling(),
                bijective: l.kind == invert::Kind::Bijective,
                preimage,
            })
            .collect();
        o.r.explain_with(|| crate::engine::Explanation::Invert { layers });
    }
    Ok(match solved {
        None => None,
        Some(Solved::Const(eq)) => Some(truth(o, cx, eq == (op == CmpOp::Eq))?),
        Some(Solved::Eq(x, v)) => Some(compare(o, cx, op, x, &v)?),
    })
}

/// `f(x) op f(y)` as `x op' y`, through every common strictly monotone layer, the sides
/// `select` trees whose leaves are peeled together.
fn cancel_order(
    cx: &mut Context,
    o: &mut Calls<'_, '_, '_>,
    op: CmpOp,
    a: u32,
    b: u32,
) -> Result<Option<u32>, Stop> {
    let Some((mut order, strict)) = Order::of(op) else {
        return Ok(None);
    };
    let (mut x, mut y) = (a, b);
    let mut peeled = false;
    while let Some((ls, _)) = leaves(cx, o, x, y)? {
        let Some((inners, m)) = monotone::cancel_many(cx, o, &ls, order)? else {
            break;
        };
        let (p, q) = relabelled(cx, o, x, y, &ls, &inners)?;
        (x, y) = if m.increasing { (p, q) } else { (q, p) };
        order = m.input;
        peeled = true;
    }
    if !peeled {
        return Ok(None);
    }
    Ok(Some(o.r.build(cx, |cx| cx.c_cmp(order.cmp(strict), x, y))?))
}

/// `f(x) op c` (or `c op f(x)`, with `const_left`) as a comparison of `x` with a threshold, or
/// a constant.
fn solve_order(
    cx: &mut Context,
    o: &mut Calls<'_, '_, '_>,
    op: CmpOp,
    lhs: u32,
    c: &BitVec,
    const_left: bool,
) -> Result<Option<u32>, Stop> {
    Ok(match monotone::solve(cx, o, op, lhs, c, const_left)? {
        None => None,
        Some(monotone::Solved::Const(v)) => Some(truth(o, cx, v)?),
        Some(monotone::Solved::Cmp {
            op,
            x,
            t,
            const_left,
        }) => Some(o.r.build(cx, |cx| {
            let t = cx.mk_const(&t)?;
            if const_left {
                cx.c_cmp(op, t, x)
            } else {
                cx.c_cmp(op, x, t)
            }
        })?),
    })
}

/// `a₁ | … | aₙ op 0` (or `a₁ & … & aₙ op ones`): each `aᵢ op c` solved alone, then
/// intersected (for `==`; `!=` is its negation).
fn split(
    cx: &mut Context,
    o: &mut Calls<'_, '_, '_>,
    op: CmpOp,
    n: u32,
    lhs: u32,
    c: &BitVec,
    before: u32,
) -> Result<Option<u32>, Stop> {
    let join = cx.node(lhs).op;
    let mut leaves = Vec::new();
    let mut stack = vec![lhs];
    while let Some(i) = stack.pop() {
        o.charge(1)?;
        let node = cx.node(i);
        if node.op == join {
            stack.push(node.b);
            stack.push(node.a);
        } else if leaves.len() >= MAX_SPLIT {
            return Ok(None);
        } else {
            leaves.push(i);
        }
    }
    let eq = op == CmpOp::Eq;
    // What each leaf being `c` means: `x == v`, in leaf order, without repeats.
    let mut conj: Vec<(u32, BitVec)> = Vec::new();
    let mut solved = false;
    for leaf in leaves {
        let (x, v) = match invert::solve_eq(cx, o, leaf, c)? {
            // One leaf can never be `c`: the conjunction is false.
            Some(Solved::Const(false)) => return Ok(Some(truth(o, cx, !eq)?)),
            Some(Solved::Const(true)) => {
                solved = true;
                continue;
            }
            Some(Solved::Eq(x, v)) => {
                solved = true;
                (x, v)
            }
            None => (leaf, *c),
        };
        match conj.iter().find(|(y, _)| *y == x) {
            // One value equal to two different constants.
            Some((_, w)) if *w != v => return Ok(Some(truth(o, cx, !eq)?)),
            Some(_) => {}
            None => conj.push((x, v)),
        }
    }
    if !solved {
        return Ok(None);
    }
    let mut out: Option<u32> = None;
    for (x, v) in &conj {
        let t = compare(o, cx, op, *x, v)?;
        out = Some(match out {
            None => t,
            Some(acc) => {
                let join = if eq { BinOp::And } else { BinOp::Or };
                o.r.build(cx, |cx| cx.c_bin(join, acc, t))?
            }
        });
    }
    let e = match out {
        Some(e) => e,
        None => truth(o, cx, eq)?,
    };
    // Kept only when smaller as a tree (a saturated size decides nothing).
    let tree = |i: u32| cx.meta[i as usize].tree;
    if tree(n) == u32::MAX || tree(e) >= tree(n) {
        discard(o.r, cx, before);
        return Ok(None);
    }
    Ok(Some(e))
}