bitwright 0.32.9

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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
//! The compares pass: boolean combinations (`& | ^ ~` on 1-bit values) of comparisons.
//!
//! *Same operand pair.* Two values `x`, `y` stand in exactly one of five relations: equal, or
//! one of {less, greater} unsigned × {less, greater} signed. Every predicate over `(x, y)` (in
//! either operand order) is a set of those relations, and `& | ^ ~` are set operations, so any
//! combination is a set; it is emitted when that set is one predicate, true or false. At
//! W = 1 only three relations can occur (`0 <u 1` but `0 >s -1`), which is accounted for.
//!
//! *One operand against constants.* A comparison of `x` with a constant is a set of values of
//! `x`: a union of unsigned intervals (signed comparisons split at the sign boundary).
//! Combinations are exact interval-set operations, emitted when the set is empty, everything, a
//! single comparison, or one wrapped interval `[lo, hi]` as the range check `x - lo <=u hi - lo`.
//! A comparison of `t + k`, `k - t`, `-t`, `~t`, `t ^ smin`, `t & (2^j - 1)`, `t | smin` or an
//! extension of `t` is one of `t` as well (the values of `t` such a map sends into an interval
//! set are an interval set), so comparisons of two such terms combine on the nearest term both
//! are functions of: a range check `x - 4 <=u 5` with `x == 12`, or the floating-point
//! classification tests, which compare `x & ~sign` or `x ^ sign`, on `x`.
//!
//! *Floats.* A floating-point comparison with a constant is a set of encodings of its other
//! operand (in the order `key` puts encodings in, the values below a constant are an interval),
//! so it combines with the rest; a set that is one comparison of that format with a constant,
//! or its negation, can be emitted as that. Two floats `x`, `y` stand in one of six relations:
//! equal, less, greater, or unordered because `x`, `y` or both are NaN. Every comparison of
//! the two and the NaN test of either is a set of those.
//!
//! All of it is exact at every width; the result replaces the node only when smaller.

use core::cmp::Ordering;

use super::{Fin, PassKind, Runner, Step, Stop, finish};
use crate::engine::budget::Counter;
use crate::expr::{Context, OpCode};
use crate::fp::node::{Desc, Kind};
use crate::fp::{FpFormat, FpOp};
use crate::ops::{BinOp, CmpOp, UnOp};
use crate::{BitVec, Width};

/// The most intervals an interval set keeps before its node is treated as opaque.
const MAX_INTERVALS: usize = 8;

/// The longest chain of terms `peel` follows.
const MAX_PEEL: usize = 6;

// ----- relation sets ------------------------------------------------------------------------------

/// Relations of the first operand to the second: bit 0 equal, bit 1 (<u, <s), bit 2 (<u, >s),
/// bit 3 (>u, <s), bit 4 (>u, >s).
const ALL: u8 = 0b11111;

fn pred_set(op: CmpOp) -> u8 {
    match op {
        CmpOp::Eq => 0b00001,
        CmpOp::Ne => 0b11110,
        CmpOp::Ult => 0b00110,
        CmpOp::Ule => 0b00111,
        CmpOp::Slt => 0b01010,
        CmpOp::Sle => 0b01011,
    }
}

/// The same set with the operands exchanged.
fn swap_set(s: u8) -> u8 {
    let bit = |k: u8| (s >> k) & 1;
    bit(0) | (bit(4) << 1) | (bit(3) << 2) | (bit(2) << 3) | (bit(1) << 4)
}

/// The relations that can occur at width `w`.
fn realizable(w: u16) -> u8 {
    if w == 1 { 0b01101 } else { ALL }
}

/// Relations of float `x` to float `y`: bit 0 equal, bit 1 less, bit 2 greater, bit 3 `x` NaN
/// and `y` not, bit 4 `y` NaN and `x` not, bit 5 both NaN. All six occur in every format.
const FLOAT_ALL: u8 = 0b111111;

/// `x` is a NaN.
const X_NAN: u8 = 0b101000;

/// `y` is a NaN.
const Y_NAN: u8 = 0b110000;

fn float_pred_set(k: Kind) -> u8 {
    match k {
        Kind::Eq => 0b001,
        Kind::Lt => 0b010,
        _ => 0b011,
    }
}

/// The same set with the floats exchanged.
fn float_swap(s: u8) -> u8 {
    let bit = |k: u8| (s >> k) & 1;
    bit(0) | (bit(2) << 1) | (bit(1) << 2) | (bit(4) << 3) | (bit(3) << 4) | (bit(5) << 5)
}

// ----- interval sets ------------------------------------------------------------------------------

/// Sorted, disjoint, non-adjacent unsigned intervals.
type Set = Vec<(BitVec, BitVec)>;

fn one(w: Width) -> BitVec {
    BitVec::one(w)
}

fn inc(v: &BitVec) -> BitVec {
    BitVec::bin_unchecked(BinOp::Add, v, &one(v.width()))
}

fn dec(v: &BitVec) -> BitVec {
    BitVec::bin_unchecked(BinOp::Sub, v, &one(v.width()))
}

fn add(a: &BitVec, b: &BitVec) -> BitVec {
    BitVec::bin_unchecked(BinOp::Add, a, b)
}

fn sub(a: &BitVec, b: &BitVec) -> BitVec {
    BitVec::bin_unchecked(BinOp::Sub, a, b)
}

fn ult(a: &BitVec, b: &BitVec) -> bool {
    BitVec::cmp_unchecked(CmpOp::Ult, a, b)
}

fn ule(a: &BitVec, b: &BitVec) -> bool {
    BitVec::cmp_unchecked(CmpOp::Ule, a, b)
}

fn full(w: Width) -> Set {
    vec![(BitVec::zero(w), BitVec::ones(w))]
}

fn complement(s: &Set, w: Width) -> Set {
    let mut out = Vec::new();
    let mut next = Some(BitVec::zero(w));
    for (lo, hi) in s {
        if let Some(n) = next
            && ult(&n, lo)
        {
            out.push((n, dec(lo)));
        }
        next = if hi.is_ones() { None } else { Some(inc(hi)) };
    }
    if let Some(n) = next {
        out.push((n, BitVec::ones(w)));
    }
    out
}

fn intersect(a: &Set, b: &Set) -> Set {
    let mut out = Vec::new();
    let (mut i, mut j) = (0, 0);
    while i < a.len() && j < b.len() {
        let lo = if ult(&a[i].0, &b[j].0) {
            b[j].0
        } else {
            a[i].0
        };
        let hi = if ult(&a[i].1, &b[j].1) {
            a[i].1
        } else {
            b[j].1
        };
        if ule(&lo, &hi) {
            out.push((lo, hi));
        }
        if ult(&a[i].1, &b[j].1) {
            i += 1;
        } else {
            j += 1;
        }
    }
    out
}

fn union(a: &Set, b: &Set, w: Width) -> Set {
    complement(&intersect(&complement(a, w), &complement(b, w)), w)
}

fn sym_diff(a: &Set, b: &Set, w: Width) -> Set {
    union(
        &intersect(a, &complement(b, w)),
        &intersect(&complement(a, w), b),
        w,
    )
}

/// The cyclic interval from `a` up to `b`: one interval, or two touching both ends.
fn push_cyclic(out: &mut Set, a: BitVec, b: BitVec) {
    if ule(&a, &b) {
        out.push((a, b));
    } else {
        let w = a.width();
        out.push((BitVec::zero(w), b));
        out.push((a, BitVec::ones(w)));
    }
}

/// Disjoint intervals in any order as a set: sorted, adjacent ones merged.
fn normalize(mut v: Set) -> Set {
    v.sort_by(|p, q| {
        if ult(&p.0, &q.0) {
            Ordering::Less
        } else if p.0 == q.0 {
            Ordering::Equal
        } else {
            Ordering::Greater
        }
    });
    let mut out: Set = Vec::with_capacity(v.len());
    for (lo, hi) in v {
        if let Some(last) = out.last_mut()
            && (last.1.is_ones() || ule(&lo, &inc(&last.1)))
        {
            if ult(&last.1, &hi) {
                last.1 = hi;
            }
            continue;
        }
        out.push((lo, hi));
    }
    out
}

/// The values `x` for which `x op c` holds.
fn interval_of(op: CmpOp, c: &BitVec, x_first: bool) -> Set {
    let w = c.width();
    let zero = BitVec::zero(w);
    let max = BitVec::ones(w);
    let smin = BitVec::smin(w);
    let smax = BitVec::smax(w);
    // A signed interval [a, b] (a <=s b) as unsigned intervals.
    let signed = |a: BitVec, b: BitVec| -> Set {
        if a.msb() == b.msb() {
            vec![(a, b)]
        } else {
            vec![(zero, b), (a, max)]
        }
    };
    match (op, x_first) {
        (CmpOp::Eq, _) => vec![(*c, *c)],
        (CmpOp::Ne, _) => complement(&vec![(*c, *c)], w),
        // x <u c / x <=u c
        (CmpOp::Ult, true) if c.is_zero() => Vec::new(),
        (CmpOp::Ult, true) => vec![(zero, dec(c))],
        (CmpOp::Ule, true) => vec![(zero, *c)],
        // c <u x / c <=u x
        (CmpOp::Ult, false) if c.is_ones() => Vec::new(),
        (CmpOp::Ult, false) => vec![(inc(c), max)],
        (CmpOp::Ule, false) => vec![(*c, max)],
        // x <s c / x <=s c
        (CmpOp::Slt, true) if *c == smin => Vec::new(),
        (CmpOp::Slt, true) => signed(smin, dec(c)),
        (CmpOp::Sle, true) => signed(smin, *c),
        // c <s x / c <=s x
        (CmpOp::Slt, false) if *c == smax => Vec::new(),
        (CmpOp::Slt, false) => signed(inc(c), smax),
        (CmpOp::Sle, false) => signed(*c, smax),
    }
}

// ----- terms that are functions of another -------------------------------------------------------

/// A function of one operand `t` under which the values of `t` sent into an interval set form
/// an interval set.
#[derive(Clone, Copy, Debug)]
enum Map {
    /// `t + k`.
    Shift(BitVec),
    /// `k - t`.
    Reflect(BitVec),
    /// `t & (2^j - 1)`, `0 < j < W`.
    Low(u16),
    /// `t | smin`.
    SetTop,
    /// Zero extension of `t`.
    Zext,
    /// Sign extension of `t`.
    Sext,
    /// The low bits of `t`, as a narrower value.
    Trunc,
}

/// The operand node `e` is a function of, and the function, if it is one of `Map`'s.
fn peel(cx: &Context, e: u32) -> Option<(u32, Map)> {
    let n = cx.node(e);
    let w = cx.width_of(e);
    match n.op {
        OpCode::Neg => return Some((n.a, Map::Reflect(BitVec::zero(w)))),
        OpCode::Not => return Some((n.a, Map::Reflect(BitVec::ones(w)))),
        OpCode::Zext => return Some((n.a, Map::Zext)),
        OpCode::Sext => return Some((n.a, Map::Sext)),
        OpCode::Extract if n.b == 0 => return Some((n.a, Map::Trunc)),
        OpCode::Add | OpCode::Sub | OpCode::Xor | OpCode::And | OpCode::Or => {}
        _ => return None,
    }
    let (t, k, k_first) = match (cx.const_val(n.a), cx.const_val(n.b)) {
        (None, Some(k)) => (n.a, k, false),
        (Some(k), None) => (n.b, k, true),
        _ => return None,
    };
    let smin = BitVec::smin(w);
    let map = match n.op {
        OpCode::Add => Map::Shift(k),
        OpCode::Sub if k_first => Map::Reflect(k),
        OpCode::Sub => Map::Shift(BitVec::un_unchecked(UnOp::Neg, &k)),
        OpCode::Xor if k == smin => Map::Shift(k),
        OpCode::Xor if k.is_ones() => Map::Reflect(k),
        OpCode::And
            if !k.is_zero()
                && !k.is_ones()
                && BitVec::bin_unchecked(BinOp::And, &k, &inc(&k)).is_zero() =>
        {
            let j = BitVec::un_unchecked(UnOp::Popcnt, &k).to_u64()?;
            Map::Low(u16::try_from(j).ok()?)
        }
        OpCode::Or if k == smin => Map::SetTop,
        _ => return None,
    };
    Some((t, map))
}

/// The values of `t` (width `tw`) that `m` sends into `s` (a set at width `w`), unless that
/// takes more than `MAX_INTERVALS` intervals.
fn preimage(m: Map, s: &Set, w: Width, tw: Width) -> Option<Set> {
    let mut out = Set::new();
    match m {
        Map::Shift(k) => {
            for (lo, hi) in s {
                push_cyclic(&mut out, sub(lo, &k), sub(hi, &k));
            }
        }
        Map::Reflect(k) => {
            for (lo, hi) in s {
                push_cyclic(&mut out, sub(&k, hi), sub(&k, lo));
            }
        }
        Map::Low(j) => {
            let step = BitVec::bin_unchecked(
                BinOp::Shl,
                &one(w),
                &BitVec::wrapping_from_u64(w, u64::from(j)),
            );
            let mask = dec(&step);
            let low = intersect(s, &vec![(BitVec::zero(w), mask)]);
            if low.is_empty() {
                return Some(out);
            }
            if low == [(BitVec::zero(w), mask)] {
                return Some(full(w));
            }
            // One copy of `low` for every value of the bits the mask clears.
            let copies = 1u64
                .checked_shl(u32::from(w.bits() - j))
                .filter(|&c| c.saturating_mul(low.len() as u64) <= MAX_INTERVALS as u64)?;
            let mut base = BitVec::zero(w);
            for _ in 0..copies {
                for (lo, hi) in &low {
                    out.push((add(lo, &base), add(hi, &base)));
                }
                base = add(&base, &step);
            }
        }
        Map::SetTop => {
            let smin = BitVec::smin(w);
            for (lo, hi) in intersect(s, &vec![(smin, BitVec::ones(w))]) {
                out.push((sub(&lo, &smin), sub(&hi, &smin)));
                out.push((lo, hi));
            }
        }
        Map::Zext | Map::Sext => {
            // The values an extension takes: every value of `t` below the sign boundary, and
            // for a sign extension the ones above it, as the top of the wide range.
            let pos = if matches!(m, Map::Zext) {
                BitVec::ones(tw)
            } else {
                BitVec::smax(tw)
            };
            let mut parts = vec![(BitVec::zero(w), pos.zext(w).ok()?)];
            if matches!(m, Map::Sext) {
                parts.push((BitVec::smin(tw).sext(w).ok()?, BitVec::ones(w)));
            }
            for (lo, hi) in intersect(s, &parts) {
                out.push((lo.trunc(tw).ok()?, hi.trunc(tw).ok()?));
            }
        }
        Map::Trunc => {
            if s.is_empty() {
                return Some(out);
            }
            if *s == full(w) {
                return Some(full(tw));
            }
            // One copy of `s` for every value of the bits the truncation drops.
            let copies = 1u64
                .checked_shl(u32::from(tw.bits() - w.bits()))
                .filter(|&c| c.saturating_mul(s.len() as u64) <= MAX_INTERVALS as u64)?;
            let step = BitVec::bin_unchecked(
                BinOp::Shl,
                &one(tw),
                &BitVec::wrapping_from_u64(tw, u64::from(w.bits())),
            );
            let mut base = BitVec::zero(tw);
            for _ in 0..copies {
                for (lo, hi) in s {
                    out.push((
                        add(&lo.zext(tw).ok()?, &base),
                        add(&hi.zext(tw).ok()?, &base),
                    ));
                }
                base = add(&base, &step);
            }
        }
    }
    let out = normalize(out);
    (out.len() <= MAX_INTERVALS).then_some(out)
}

/// `e` and the terms `peel` reaches from it, each `maps[i]` of the next.
fn chain(cx: &Context, e: u32) -> (Vec<u32>, Vec<Map>) {
    let (mut nodes, mut maps) = (vec![e], Vec::new());
    let mut cur = e;
    while maps.len() < MAX_PEEL
        && let Some((t, m)) = peel(cx, cur)
    {
        nodes.push(t);
        maps.push(m);
        cur = t;
    }
    (nodes, maps)
}

/// `s`, a set of values of `nodes[0]`, as a set of values of `nodes[k]`.
fn pull(cx: &Context, nodes: &[u32], maps: &[Map], k: usize, s: &Set) -> Option<Set> {
    let mut s = s.clone();
    for i in 0..k {
        s = preimage(
            maps[i],
            &s,
            cx.width_of(nodes[i]),
            cx.width_of(nodes[i + 1]),
        )?;
    }
    Some(s)
}

/// A set of values of `x` and one of `y` as sets of values of the nearest term both are
/// functions of.
fn common(cx: &Context, x: u32, s: &Set, y: u32, t: &Set) -> Option<(u32, Set, Set)> {
    if x == y {
        return Some((x, s.clone(), t.clone()));
    }
    let (xs, xm) = chain(cx, x);
    let (ys, ym) = chain(cx, y);
    let (i, j) = xs
        .iter()
        .enumerate()
        .find_map(|(i, n)| ys.iter().position(|m| m == n).map(|j| (i, j)))?;
    Some((xs[i], pull(cx, &xs, &xm, i, s)?, pull(cx, &ys, &ym, j, t)?))
}

// ----- floating-point encodings -------------------------------------------------------------------

/// The keys of the encodings in `s`. Keys put encodings in the order of the values they stand
/// for: a positive encoding `e` is at `e + smin`, a negative one at `~e`. So `−∞ … −0, +0 … +∞`
/// is one interval of keys, with `−0` and `+0` adjacent, the negative NaNs below it and the
/// positive ones above.
fn key_set(s: &Set, w: Width) -> Set {
    let (smin, smax) = (BitVec::smin(w), BitVec::smax(w));
    let mut out = Set::new();
    for (lo, hi) in intersect(s, &vec![(BitVec::zero(w), smax)]) {
        out.push((add(&lo, &smin), add(&hi, &smin)));
    }
    for (lo, hi) in intersect(s, &vec![(smin, BitVec::ones(w))]) {
        out.push((
            BitVec::un_unchecked(UnOp::Not, &hi),
            BitVec::un_unchecked(UnOp::Not, &lo),
        ));
    }
    normalize(out)
}

/// The encodings at the keys in `k`.
fn unkey_set(k: &Set, w: Width) -> Set {
    let (smin, smax) = (BitVec::smin(w), BitVec::smax(w));
    let mut out = Set::new();
    for (lo, hi) in intersect(k, &vec![(smin, BitVec::ones(w))]) {
        out.push((sub(&lo, &smin), sub(&hi, &smin)));
    }
    for (lo, hi) in intersect(k, &vec![(BitVec::zero(w), smax)]) {
        out.push((
            BitVec::un_unchecked(UnOp::Not, &hi),
            BitVec::un_unchecked(UnOp::Not, &lo),
        ));
    }
    normalize(out)
}

fn key(e: &BitVec) -> BitVec {
    if e.msb() {
        BitVec::un_unchecked(UnOp::Not, e)
    } else {
        add(e, &BitVec::smin(e.width()))
    }
}

fn unkey(k: &BitVec) -> BitVec {
    if k.msb() {
        sub(k, &BitVec::smin(k.width()))
    } else {
        BitVec::un_unchecked(UnOp::Not, k)
    }
}

/// The keys of −∞ and +∞ in `f`, the ends of the non-NaN values.
fn key_ends(f: FpFormat) -> (BitVec, BitVec) {
    (key(&f.inf(true)), key(&f.inf(false)))
}

/// The keys of −0 and +0.
fn key_zeros(w: Width) -> (BitVec, BitVec) {
    (BitVec::smax(w), BitVec::smin(w))
}

/// The encodings of `f` that are not NaNs.
fn non_nan(f: FpFormat) -> Set {
    let (lo, hi) = key_ends(f);
    unkey_set(&vec![(lo, hi)], f.width())
}

/// The encodings `x` of `f` for which `x op c` holds (`c op x` unless `x_first`), `op` one of
/// `Eq`, `Lt`, `Le`.
fn float_interval(f: FpFormat, op: Kind, c: &BitVec, x_first: bool) -> Set {
    let w = f.width();
    let (kninf, kpinf) = key_ends(f);
    let kc = key(c);
    if ult(&kc, &kninf) || ult(&kpinf, &kc) {
        // A NaN compares false.
        return Vec::new();
    }
    // The keys of the values equal to c: both zeros for a zero.
    let (klo, khi) = if BitVec::bin_unchecked(BinOp::And, c, &BitVec::smax(w)).is_zero() {
        key_zeros(w)
    } else {
        (kc, kc)
    };
    let (a, b) = match (op, x_first) {
        (Kind::Eq, _) => (klo, khi),
        (Kind::Lt, true) if klo == kninf => return Vec::new(),
        (Kind::Lt, true) => (kninf, dec(&klo)),
        (Kind::Lt, false) if khi == kpinf => return Vec::new(),
        (Kind::Lt, false) => (inc(&khi), kpinf),
        (_, true) => (kninf, khi),
        (_, false) => (klo, kpinf),
    };
    unkey_set(&vec![(a, b)], w)
}

/// The number of trailing zero bits of the magnitude of the float `e` (a zero has them all):
/// the roundest constant is the one to print.
fn roundness(e: &BitVec) -> u64 {
    let m = BitVec::bin_unchecked(BinOp::And, e, &BitVec::smax(e.width()));
    BitVec::un_unchecked(UnOp::Ctz, &m).to_u64().unwrap_or(0)
}

/// A comparison of floats of `f` that holds exactly for the encodings `x` in `s`: `x op c`, or
/// `c op x` if the flag is false, or `x == x` (no constant) for every non-NaN.
fn float_form(f: FpFormat, s: &Set) -> Option<(Kind, Option<BitVec>, bool)> {
    let w = f.width();
    let keys = key_set(s, w);
    let [(a, b)] = keys.as_slice() else {
        return None;
    };
    let (kninf, kpinf) = key_ends(f);
    let (kmz, kpz) = key_zeros(w);
    if ult(a, &kninf) || ult(&kpinf, b) {
        return None;
    }
    // A zero constant is written +0.
    let value = |k: &BitVec| {
        let e = unkey(k);
        if e == BitVec::smin(w) {
            BitVec::zero(w)
        } else {
            e
        }
    };
    // Of `x <= u` and `x < v` (or `u <= x`, `v < x`), the one with the rounder constant.
    let pick = |u: BitVec, v: BitVec, x_first: bool| {
        Some(if roundness(&v) > roundness(&u) {
            (Kind::Lt, Some(v), x_first)
        } else {
            (Kind::Le, Some(u), x_first)
        })
    };
    match (*a == kninf, *b == kpinf) {
        (true, true) => Some((Kind::Eq, None, true)),
        // Holding at one zero and not the other is no comparison.
        (true, false) if *b == kmz => None,
        (true, false) => pick(value(b), value(&inc(b)), true),
        (false, true) if *a == kpz => None,
        (false, true) => pick(value(a), value(&dec(a)), false),
        (false, false) if *a == kmz && *b == kpz => Some((Kind::Eq, Some(BitVec::zero(w)), true)),
        (false, false) => None,
    }
}

// ----- per-node descriptions ------------------------------------------------------------------------

/// What a 1-bit node says, if it is in a fragment.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum Info {
    /// A constant truth value.
    Const(bool),
    /// A set of relations of `x` to `y` (`x < y` by index).
    Pair(u32, u32, u8),
    /// A set of relations of the floats `x` and `y` of a format (`x < y` by index).
    Floats(u32, u32, FpFormat, u8),
    /// A set of values of `x`, and a float format of `x`'s width some of the comparisons were
    /// in (the set may be one comparison in it).
    Values(u32, Set, Option<FpFormat>),
    /// Neither (or over a cap).
    Opaque,
}

fn boolean_op(op: OpCode) -> bool {
    matches!(op, OpCode::And | OpCode::Or | OpCode::Xor | OpCode::Not)
}

fn float_cmp(op: OpCode) -> bool {
    matches!(op, OpCode::FEq | OpCode::FLt | OpCode::FLe)
}

fn leaf(cx: &Context, i: u32) -> Info {
    let n = cx.node(i);
    if let Some(v) = cx.const_val(i) {
        return if n.width == 1 {
            Info::Const(!v.is_zero())
        } else {
            Info::Opaque
        };
    }
    if float_cmp(n.op) {
        return float_leaf(cx, i);
    }
    let Some(op) = n.op.as_cmp() else {
        return Info::Opaque;
    };
    // `a - (b + k) == -k` is `a == b` (the VM's `x <u y`, written `~((y - x == 0) | (y <u
    // x))`, once a constant both sides added is folded into the equality alone).
    let equal = |t: u32, c: &BitVec| {
        matches!(op, CmpOp::Eq | CmpOp::Ne)
            .then(|| difference(cx, t))
            .flatten()
            .filter(|(_, _, k)| *k == c.limbs()[0])
            .map(|(a, b, _)| Info::Pair(a.min(b), a.max(b), pred_set(op)))
    };
    match (cx.const_val(n.a), cx.const_val(n.b)) {
        (Some(_), Some(_)) => Info::Opaque,
        (None, Some(c)) => {
            equal(n.a, &c).unwrap_or_else(|| Info::Values(n.a, interval_of(op, &c, true), None))
        }
        (Some(c), None) => {
            equal(n.b, &c).unwrap_or_else(|| Info::Values(n.b, interval_of(op, &c, false), None))
        }
        (None, None) if n.a == n.b => Info::Opaque,
        (None, None) if n.a < n.b => Info::Pair(n.a, n.b, pred_set(op)),
        (None, None) => Info::Pair(n.b, n.a, swap_set(pred_set(op))),
    }
}

/// `(a, b, k)` when `t` (of at most 64 bits) is `a - b + k`: a difference of two values, a
/// constant added to it or to either of them (`a - (b + k)`; the constructors keep constants
/// on the right of a sum).
fn difference(cx: &Context, t: u32) -> Option<(u32, u32, u64)> {
    let w = cx.wid(t);
    if w > 64 {
        return None;
    }
    // `x` without the constants added to it, and their sum.
    let bare = |mut x: u32| {
        let mut k = 0u64;
        loop {
            let node = cx.node(x);
            // (Only a sum's second field is an operand.)
            let added = (node.op == OpCode::Add)
                .then(|| cx.const_val(node.b))
                .flatten();
            match added {
                Some(c) => {
                    k = k.wrapping_add(c.limbs()[0]);
                    x = node.a;
                }
                None => return (x, k),
            }
        }
    };
    let (d, k) = bare(t);
    let node = cx.node(d);
    if node.op != OpCode::Sub {
        return None;
    }
    let ((a, ka), (b, kb)) = (bare(node.a), bare(node.b));
    let k = k.wrapping_add(ka).wrapping_sub(kb) & (u64::MAX >> (64 - w));
    (a != b && !cx.is_const(a) && !cx.is_const(b)).then_some((a, b, k))
}

fn float_leaf(cx: &Context, i: u32) -> Info {
    let n = cx.node(i);
    let Some(Desc { op, format: f }) = cx.fp_desc(i) else {
        return Info::Opaque;
    };
    let kind = match op {
        FpOp::Eq => Kind::Eq,
        FpOp::Lt => Kind::Lt,
        FpOp::Le => Kind::Le,
        _ => return Info::Opaque,
    };
    match (cx.const_val(n.a), cx.const_val(n.b)) {
        (Some(_), Some(_)) => Info::Opaque,
        (None, Some(c)) => Info::Values(n.a, float_interval(f, kind, &c, true), Some(f)),
        (Some(c), None) => Info::Values(n.b, float_interval(f, kind, &c, false), Some(f)),
        // `x < x` never holds, `x == x` and `x <= x` unless x is a NaN.
        (None, None) if n.a == n.b => {
            let s = if kind == Kind::Lt {
                Vec::new()
            } else {
                non_nan(f)
            };
            Info::Values(n.a, s, Some(f))
        }
        (None, None) if n.a < n.b => Info::Floats(n.a, n.b, f, float_pred_set(kind)),
        (None, None) => Info::Floats(n.b, n.a, f, float_swap(float_pred_set(kind))),
    }
}

/// A set of values of `z` as a set of relations of floats `x`, `y` of `f`, if `z` is one of
/// them (or a function of one) and the set is a NaN test.
fn nan_relations(cx: &Context, x: u32, y: u32, f: FpFormat, z: u32, s: &Set) -> Option<u8> {
    let (zs, zm) = chain(cx, z);
    let (k, nan) = [(x, X_NAN), (y, Y_NAN)]
        .into_iter()
        .find_map(|(v, nan)| zs.iter().position(|&n| n == v).map(|k| (k, nan)))?;
    let s = pull(cx, &zs, &zm, k, s)?;
    let w = f.width();
    let numbers = non_nan(f);
    if s.is_empty() {
        Some(0)
    } else if s == full(w) {
        Some(FLOAT_ALL)
    } else if s == numbers {
        Some(!nan & FLOAT_ALL)
    } else if s == complement(&numbers, w) {
        Some(nan)
    } else {
        None
    }
}

/// The format whose NaN encodings at width `w` are exactly `s`, if there is one.
fn nan_format(s: &Set, w: Width) -> Option<FpFormat> {
    let [(a, _), _] = s.as_slice() else {
        return None;
    };
    // The positive NaNs start just above +∞, whose encoding is all exponent bits.
    let inf = dec(a);
    let eb = u32::try_from(BitVec::un_unchecked(UnOp::Popcnt, &inf).to_u64()?).ok()?;
    let f = FpFormat::new(eb, u32::from(w.bits()).checked_sub(eb)?).ok()?;
    (f.inf(false) == inf && complement(&non_nan(f), w) == *s).then_some(f)
}

/// The float the set `s` of values of `z` tests for being a NaN (`true`) or not (`false`), and
/// its format: `z` itself, or the nearest term `z` is a function of for which the set is one.
fn nan_test(cx: &Context, z: u32, s: &Set) -> Option<(u32, FpFormat, bool)> {
    let (zs, zm) = chain(cx, z);
    for k in 0..zs.len() {
        let t = pull(cx, &zs, &zm, k, s)?;
        let w = cx.width_of(zs[k]);
        if let Some(f) = nan_format(&t, w) {
            return Some((zs[k], f, true));
        }
        if let Some(f) = nan_format(&complement(&t, w), w) {
            return Some((zs[k], f, false));
        }
    }
    None
}

/// NaN tests of two different floats of one format as sets of relations of the two: the floats
/// (by index) and the format, then each test's set.
fn nan_pair(
    cx: &Context,
    x: u32,
    s: &Set,
    y: u32,
    t: &Set,
) -> Option<(u32, u32, FpFormat, u8, u8)> {
    let (a, f, p) = nan_test(cx, x, s)?;
    let (b, g, q) = nan_test(cx, y, t)?;
    if a == b || f != g {
        return None;
    }
    let (lo, hi) = (a.min(b), a.max(b));
    let rel = |v: u32, nan: bool| {
        let bits = if v == lo { X_NAN } else { Y_NAN };
        if nan { bits } else { !bits & FLOAT_ALL }
    };
    Some((lo, hi, f, rel(a, p), rel(b, q)))
}

/// The float format hint of a combination on `base`: one of the operands', if its width is
/// still the base's.
fn hint(cx: &Context, base: u32, a: Option<FpFormat>, b: Option<FpFormat>) -> Option<FpFormat> {
    let w = cx.width_of(base);
    a.into_iter().chain(b).find(|f| f.width() == w)
}

/// Combines operand descriptions under a boolean operator (`None` operand for `Not`).
fn combine(cx: &Context, op: OpCode, a: &Info, b: Option<&Info>) -> Info {
    // A constant becomes the neutral description of the other side's fragment.
    let lift = |c: bool, other: &Info| -> Info {
        match other {
            Info::Pair(x, y, _) => Info::Pair(*x, *y, if c { ALL } else { 0 }),
            Info::Floats(x, y, f, _) => Info::Floats(*x, *y, *f, if c { FLOAT_ALL } else { 0 }),
            Info::Values(x, _, h) => {
                let w = cx.width_of(*x);
                Info::Values(*x, if c { full(w) } else { Vec::new() }, *h)
            }
            _ => Info::Const(c),
        }
    };
    let (a, b) = match (a, b) {
        (Info::Const(c), Some(o)) => (lift(*c, o), Some(o.clone())),
        (o, Some(Info::Const(c))) => (o.clone(), Some(lift(*c, o))),
        (o, b) => (o.clone(), b.cloned()),
    };
    // A NaN test joins the relations of the floats it tests.
    let (a, b) = match (a, b) {
        (Info::Floats(x, y, f, s), Some(Info::Values(z, t, _))) => {
            match nan_relations(cx, x, y, f, z, &t) {
                Some(t) => (Info::Floats(x, y, f, s), Some(Info::Floats(x, y, f, t))),
                None => return Info::Opaque,
            }
        }
        (Info::Values(z, t, _), Some(Info::Floats(x, y, f, s))) => {
            match nan_relations(cx, x, y, f, z, &t) {
                Some(t) => (Info::Floats(x, y, f, t), Some(Info::Floats(x, y, f, s))),
                None => return Info::Opaque,
            }
        }
        (a, b) => (a, b),
    };
    let bool_op = |x: bool, y: bool| match op {
        OpCode::And => x & y,
        OpCode::Or => x | y,
        _ => x ^ y,
    };
    let bits_op = |s: u8, t: u8| match op {
        OpCode::And => s & t,
        OpCode::Or => s | t,
        _ => s ^ t,
    };
    match (op, &a, &b) {
        (OpCode::Not, Info::Const(c), None) => Info::Const(!c),
        (OpCode::Not, Info::Pair(x, y, s), None) => Info::Pair(*x, *y, !s & ALL),
        (OpCode::Not, Info::Floats(x, y, f, s), None) => Info::Floats(*x, *y, *f, !s & FLOAT_ALL),
        (OpCode::Not, Info::Values(x, s, h), None) => {
            Info::Values(*x, complement(s, cx.width_of(*x)), *h)
        }
        (_, Info::Const(p), Some(Info::Const(q))) => Info::Const(bool_op(*p, *q)),
        (_, Info::Pair(x, y, s), Some(Info::Pair(x2, y2, t))) if x == x2 && y == y2 => {
            Info::Pair(*x, *y, bits_op(*s, *t))
        }
        (_, Info::Floats(x, y, f, s), Some(Info::Floats(x2, y2, f2, t)))
            if x == x2 && y == y2 && f == f2 =>
        {
            Info::Floats(*x, *y, *f, bits_op(*s, *t))
        }
        (_, Info::Values(x, s, h), Some(Info::Values(x2, t, h2))) => {
            let Some((base, s, t)) = common(cx, *x, s, *x2, t) else {
                // Unrelated terms: NaN tests of two floats are relations of the two.
                return match nan_pair(cx, *x, s, *x2, t) {
                    Some((x, y, f, s, t)) => Info::Floats(x, y, f, bits_op(s, t)),
                    None => Info::Opaque,
                };
            };
            let w = cx.width_of(base);
            let r = match op {
                OpCode::And => intersect(&s, &t),
                OpCode::Or => union(&s, &t, w),
                _ => sym_diff(&s, &t, w),
            };
            if r.len() > MAX_INTERVALS {
                Info::Opaque
            } else {
                Info::Values(base, r, hint(cx, base, *h, *h2))
            }
        }
        _ => Info::Opaque,
    }
}

pub(in crate::engine) fn info_of(
    r: &mut Runner<'_, '_>,
    cx: &Context,
    root: u32,
) -> Result<Info, Stop> {
    let mut stack: Vec<(u32, bool)> = vec![(root, false)];
    while let Some((i, expanded)) = stack.pop() {
        if r.compares.contains_key(&i) {
            continue;
        }
        let node = cx.node(i);
        if node.width != 1 || !boolean_op(node.op) {
            r.compares.insert(i, leaf(cx, i));
            continue;
        }
        if !expanded {
            stack.push((i, true));
            for c in node.children() {
                if !r.compares.contains_key(&c) {
                    stack.push((c, false));
                }
            }
            continue;
        }
        r.meter.charge(Counter::PassWork, 1)?;
        r.meter.check()?;
        let a = r.compares[&node.a].clone();
        let info = if node.op == OpCode::Not {
            combine(cx, OpCode::Not, &a, None)
        } else {
            let b = r.compares[&node.b].clone();
            combine(cx, node.op, &a, Some(&b))
        };
        r.compares.insert(i, info);
    }
    Ok(r.compares[&root].clone())
}

// ----- emission -------------------------------------------------------------------------------------

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

/// A comparison `x op c` or `c op x`.
fn cmp_const(
    r: &mut Runner<'_, '_>,
    cx: &mut Context,
    op: CmpOp,
    x: u32,
    c: &BitVec,
    x_first: bool,
) -> Result<u32, Stop> {
    let c = *c;
    r.build(cx, |cx| {
        let k = cx.mk_const(&c)?;
        if x_first {
            cx.c_cmp(op, x, k)
        } else {
            cx.c_cmp(op, k, x)
        }
    })
}

/// The float comparison `x op y` in `f`, negated if `not`.
fn float_cmp_node(
    r: &mut Runner<'_, '_>,
    cx: &mut Context,
    f: FpFormat,
    op: Kind,
    x: u32,
    y: u32,
    not: bool,
) -> Result<u32, Stop> {
    let op = match op {
        Kind::Eq => FpOp::Eq,
        Kind::Lt => FpOp::Lt,
        _ => FpOp::Le,
    };
    r.build(cx, |cx| {
        let c = cx.c_fp(Desc { op, format: f }, &[x, y])?;
        if not { cx.c_un(UnOp::Not, c) } else { Ok(c) }
    })
}

fn emit_pair(
    r: &mut Runner<'_, '_>,
    cx: &mut Context,
    x: u32,
    y: u32,
    s: u8,
) -> Result<Option<u32>, Stop> {
    let real = realizable(cx.wid(x));
    let s = s & real;
    if s == 0 {
        return bool_const(r, cx, false).map(Some);
    }
    if s == real {
        return bool_const(r, cx, true).map(Some);
    }
    for op in [
        CmpOp::Eq,
        CmpOp::Ne,
        CmpOp::Ult,
        CmpOp::Ule,
        CmpOp::Slt,
        CmpOp::Sle,
    ] {
        if pred_set(op) & real == s {
            return r.build(cx, |cx| cx.c_cmp(op, x, y)).map(Some);
        }
        if swap_set(pred_set(op)) & real == s {
            return r.build(cx, |cx| cx.c_cmp(op, y, x)).map(Some);
        }
    }
    Ok(None)
}

/// A set of relations of floats: false, true, one comparison, or one negated.
fn emit_floats(
    r: &mut Runner<'_, '_>,
    cx: &mut Context,
    x: u32,
    y: u32,
    f: FpFormat,
    s: u8,
) -> Result<Option<u32>, Stop> {
    if s == 0 {
        return bool_const(r, cx, false).map(Some);
    }
    if s == FLOAT_ALL {
        return bool_const(r, cx, true).map(Some);
    }
    for not in [false, true] {
        for op in [Kind::Eq, Kind::Lt, Kind::Le] {
            let p = float_pred_set(op);
            let p = if not { !p & FLOAT_ALL } else { p };
            if p == s {
                return float_cmp_node(r, cx, f, op, x, y, not).map(Some);
            }
            if float_swap(p) == s {
                return float_cmp_node(r, cx, f, op, y, x, not).map(Some);
            }
        }
    }
    Ok(None)
}

/// Whether [`emit_values`] writes `s` as one comparison with a constant (or a truth value).
fn simple_set(s: &Set, w: Width) -> bool {
    if s.is_empty() || *s == full(w) {
        return true;
    }
    let wrapped = match s.as_slice() {
        [(lo, hi)] => Some((*lo, *hi)),
        [(z, hi), (lo, m)] if z.is_zero() && m.is_ones() => Some((*lo, *hi)),
        _ => None,
    };
    let Some((lo, hi)) = wrapped else {
        return false;
    };
    if lo == hi {
        return true;
    }
    if let [(a, b)] = complement(s, w).as_slice()
        && a == b
    {
        return true;
    }
    lo.is_zero() || hi.is_ones() || lo == BitVec::smin(w) || hi == BitVec::smax(w)
}

fn emit_values(
    r: &mut Runner<'_, '_>,
    cx: &mut Context,
    x: u32,
    s: &Set,
    hint: Option<FpFormat>,
) -> Result<Option<u32>, Stop> {
    let w = cx.width_of(x);
    let (zero, max, smin, smax) = (
        BitVec::zero(w),
        BitVec::ones(w),
        BitVec::smin(w),
        BitVec::smax(w),
    );
    if s.is_empty() {
        return bool_const(r, cx, false).map(Some);
    }
    if *s == full(w) {
        return bool_const(r, cx, true).map(Some);
    }
    // One wrapped interval [lo, hi]: either one interval, or two touching both ends.
    let wrapped = match s.as_slice() {
        [(lo, hi)] => Some((*lo, *hi)),
        [(z, hi), (lo, m)] if z.is_zero() && m.is_ones() => Some((*lo, *hi)),
        _ => None,
    };
    // One comparison with a constant.
    if let Some((lo, hi)) = wrapped {
        let comp = complement(s, w);
        let simple = if lo == hi {
            Some((CmpOp::Eq, lo, true))
        } else if let [(a, b)] = comp.as_slice()
            && a == b
        {
            Some((CmpOp::Ne, *a, true))
        } else if lo == smin && hi == max {
            // The negative values: a sign test, not `smin <=u x`.
            Some((CmpOp::Sle, max, true))
        } else if lo == zero && hi == smax {
            Some((CmpOp::Sle, zero, false))
        } else if lo == zero {
            Some((CmpOp::Ule, hi, true))
        } else if hi == max {
            Some((CmpOp::Ule, lo, false))
        } else if lo == smin {
            Some((CmpOp::Sle, hi, true))
        } else if hi == smax {
            Some((CmpOp::Sle, lo, false))
        } else {
            None
        };
        if let Some((op, c, x_first)) = simple {
            return cmp_const(r, cx, op, x, &c, x_first).map(Some);
        }
    }
    // One float comparison with a constant, or its negation.
    if let Some(f) = hint {
        for not in [false, true] {
            let t = if not { complement(s, w) } else { s.clone() };
            if let Some((op, c, x_first)) = float_form(f, &t) {
                let e = r.build(cx, |cx| {
                    let k = match c {
                        Some(c) => cx.mk_const(&c)?,
                        None => x,
                    };
                    let op = match op {
                        Kind::Eq => FpOp::Eq,
                        Kind::Lt => FpOp::Lt,
                        _ => FpOp::Le,
                    };
                    let args = if x_first { [x, k] } else { [k, x] };
                    let e = cx.c_fp(Desc { op, format: f }, &args)?;
                    if not { cx.c_un(UnOp::Not, e) } else { Ok(e) }
                })?;
                return Ok(Some(e));
            }
        }
    }
    let Some((lo, hi)) = wrapped else {
        return Ok(None);
    };
    // x - lo <=u hi - lo (wrapping).
    let span = BitVec::bin_unchecked(BinOp::Sub, &hi, &lo);
    r.build(cx, |cx| {
        let l = cx.mk_const(&lo)?;
        let d = cx.c_bin(BinOp::Sub, x, l)?;
        let k = cx.mk_const(&span)?;
        cx.c_cmp(CmpOp::Ule, d, k)
    })
    .map(Some)
}

/// An OR of every value a masked word can take is true; when every test is
/// conjoined with the same condition, it is that condition. VM case tables
/// spell `c` as `(x & m == k0) & c | …` for every submask `k` of `m`.
fn exhaustive_mask_cases(
    r: &mut Runner<'_, '_>,
    cx: &Context,
    root: u32,
) -> Result<Option<Option<u32>>, Stop> {
    const MAX_TERMS: usize = 32;

    let mut terms = Vec::new();
    let mut todo = vec![root];
    while let Some(e) = todo.pop() {
        r.meter.charge(Counter::PassWork, 1)?;
        let n = cx.node(e);
        if n.op == OpCode::Or && n.width == 1 {
            todo.push(n.a);
            todo.push(n.b);
        } else {
            terms.push(e);
            if terms.len() > MAX_TERMS {
                return Ok(None);
            }
        }
    }
    if terms.len() < 2 {
        return Ok(None);
    }

    let first = cx.node(terms[0]);
    let candidates = if first.op == OpCode::And && first.width == 1 {
        [Some(first.a), Some(first.b), None]
    } else {
        [None, None, None]
    };
    for common in candidates.into_iter().flatten().map(Some).chain([None]) {
        let mut expr = None;
        let mut values = Vec::new();
        let mut valid = true;
        for &term in &terms {
            let test = if let Some(c) = common {
                let n = cx.node(term);
                if n.op != OpCode::And || n.width != 1 {
                    valid = false;
                    break;
                }
                if n.a == c {
                    n.b
                } else if n.b == c {
                    n.a
                } else {
                    valid = false;
                    break;
                }
            } else {
                term
            };
            let n = cx.node(test);
            if n.op != OpCode::Eq {
                valid = false;
                break;
            }
            let (e, k) = match (cx.const_val(n.a), cx.const_val(n.b)) {
                (None, Some(k)) => (n.a, k),
                (Some(k), None) => (n.b, k),
                _ => {
                    valid = false;
                    break;
                }
            };
            if expr.replace(e).is_some_and(|old| old != e) {
                valid = false;
                break;
            }
            let Some(k) = k.to_u64() else {
                valid = false;
                break;
            };
            values.push(k);
        }
        let Some(e) = expr.filter(|_| valid) else {
            continue;
        };
        let n = cx.node(e);
        if n.op != OpCode::And {
            continue;
        }
        let mask = match (cx.const_val(n.a), cx.const_val(n.b)) {
            (Some(k), None) | (None, Some(k)) => k,
            _ => continue,
        };
        let Some(mask) = mask.to_u64() else {
            continue;
        };
        let bits = mask.count_ones();
        if bits > 5 || values.iter().any(|v| v & !mask != 0) {
            continue;
        }
        values.sort_unstable();
        values.dedup();
        if values.len() == 1usize << bits {
            return Ok(Some(common));
        }
    }
    Ok(None)
}

/// The compares pass at `n`.
pub(super) fn step(r: &mut Runner<'_, '_>, cx: &mut Context, n: u32) -> Result<Step, Stop> {
    let node = cx.node(n);
    if node.op == OpCode::Select {
        // What `arms_read` declines contributes to the result (see the linear pass's `step`).
        let declined = match arms_read(r, cx, n)? {
            Some(Step::Normal(f)) => f,
            Some(step) => return Ok(step),
            None => Fin::FINAL,
        };
        return Ok(match order_step(r, cx, n)? {
            Step::Normal(f) => Step::Normal(f.and(declined)),
            step => step,
        });
    }
    if node.width != 1 || !(boolean_op(node.op) || node.op.as_cmp().is_some() || float_cmp(node.op))
    {
        return Ok(Step::Normal(Fin::FINAL));
    }
    if let Some(common) = exhaustive_mask_cases(r, cx, n)? {
        let before = cx.len() as u32;
        let (e, atoms) = match common {
            Some(c) => (c, vec![c]),
            None => (bool_const(r, cx, true)?, Vec::new()),
        };
        return finish(r, cx, PassKind::Compares, n, e, before, &atoms, Fin::FINAL);
    }
    // A test of one bit through a mask: `(x & 2^k) != 0` is bit k of x.
    if let Some((x, k, set)) = bit_test(cx, n) {
        let before = cx.len() as u32;
        let e = r.build(cx, |cx| {
            let b = cx.c_extract(x, k, 1)?;
            if set { Ok(b) } else { cx.c_un(UnOp::Not, b) }
        })?;
        return finish(r, cx, PassKind::Compares, n, e, before, &[x], Fin::FINAL);
    }
    let info = info_of(r, cx, n)?;
    let before = cx.len() as u32;
    let (e, atoms) = match &info {
        Info::Pair(x, y, s) => (emit_pair(r, cx, *x, *y, *s)?, vec![*x, *y]),
        Info::Floats(x, y, f, s) => (emit_floats(r, cx, *x, *y, *f, *s)?, vec![*x, *y]),
        // A set of values of `x`: emitted on the deepest term `x` is a function of where it
        // is one comparison with a constant (`sext(a) <s 5` is `a <s 5`, `zext(a) <u 5` is
        // `a <u 5`), else on `x` (`(x + 3) <u 10` stays: on `x` it is a range check). Decided
        // before anything is built: a candidate built and dropped would count as a use of
        // the nodes it shares with the chosen one.
        Info::Values(x, s, h) => {
            let (nodes, maps) = chain(cx, *x);
            let mut at = 0usize;
            let mut set = s.clone();
            let mut best = u32::MAX;
            for k in 0..nodes.len() {
                let t = if k == 0 {
                    Some(s.clone())
                } else {
                    pull(cx, &nodes, &maps, k, s)
                };
                let Some(t) = t else {
                    continue;
                };
                // The term, and one comparison with a constant (two nodes) or a range check
                // (a subtraction, two constants and the comparison).
                let term = match cx.dag_size(&[cx.handle(nodes[k])], 256) {
                    Ok(crate::expr::Bounded::Exact(n)) => n,
                    _ => continue,
                };
                let cost = term
                    + if simple_set(&t, cx.width_of(nodes[k])) {
                        2
                    } else {
                        4
                    };
                if cost < best {
                    (at, set, best) = (k, t, cost);
                }
            }
            let hint = if at == 0 { *h } else { None };
            (emit_values(r, cx, nodes[at], &set, hint)?, vec![nodes[at]])
        }
        Info::Const(v) if boolean_op(node.op) => (Some(bool_const(r, cx, *v)?), Vec::new()),
        _ => (None, Vec::new()),
    };
    let e = match e {
        Some(e) if e != n => e,
        _ => {
            if matches!(node.op, OpCode::Eq | OpCode::Ne)
                && cx.wid(node.a) > 1
                && super::poly::equal(r, cx, node.a, node.b)?
            {
                let before = cx.len() as u32;
                let c = bool_const(r, cx, node.op == OpCode::Eq)?;
                return finish(r, cx, PassKind::Compares, n, c, before, &[], Fin::FINAL);
            }
            if matches!(node.op, OpCode::Eq | OpCode::Ne)
                && cx.wid(node.a) > 1
                && let Some(same) = super::bitwise::equal(r, cx, node.a, node.b)?
            {
                let before = cx.len() as u32;
                let c = bool_const(r, cx, same == (node.op == OpCode::Eq))?;
                return finish(r, cx, PassKind::Compares, n, c, before, &[], Fin::FINAL);
            }
            if matches!(node.op, OpCode::Eq | OpCode::Ne)
                && cx.wid(node.a) > 1
                && super::linear_mba::equal(r, cx, node.a, node.b)?
            {
                let before = cx.len() as u32;
                let c = bool_const(r, cx, node.op == OpCode::Eq)?;
                return finish(r, cx, PassKind::Compares, n, c, before, &[], Fin::FINAL);
            }
            if let Some(b) = residue_decide(r, cx, n)? {
                let before = cx.len() as u32;
                let c = bool_const(r, cx, b)?;
                return finish(r, cx, PassKind::Compares, n, c, before, &[], Fin::FINAL);
            }
            if let Some(b) = super::predicate::decide(r, cx, n)? {
                let before = cx.len() as u32;
                let c = bool_const(r, cx, b)?;
                return finish(r, cx, PassKind::Compares, n, c, before, &[], Fin::FINAL);
            }
            if let Some((x, j, truth)) = super::predicate::recover(r, cx, n)? {
                let before = cx.len() as u32;
                let t = r.build(cx, |cx| cx.c_extract(x, 0, j))?;
                let e = if j == 1 {
                    // One residue of two is true (the other, or it would be constant, is not).
                    Some(if truth[1] {
                        t
                    } else {
                        r.build(cx, |cx| cx.c_un(UnOp::Not, t))?
                    })
                } else {
                    emit_values(r, cx, t, &residue_set(&truth, cx.width_of(t)), None)?
                };
                if let Some(e) = e {
                    return finish(r, cx, PassKind::Compares, n, e, before, &[x], Fin::FINAL);
                }
                super::discard(r, cx, before);
            }
            return order_step(r, cx, n);
        }
    };
    match &info {
        Info::Pair(x, y, s) => r.explain_with(|| crate::engine::Explanation::Relations {
            x: cx.handle(*x),
            y: cx.handle(*y),
            set: *s,
        }),
        Info::Values(x, s, _) => r.explain_with(|| crate::engine::Explanation::Intervals {
            x: cx.handle(*x),
            set: s.clone(),
        }),
        _ => {}
    }
    let step = finish(r, cx, PassKind::Compares, n, e, before, &atoms, Fin::FINAL)?;
    let Step::Normal(fin) = step else {
        return Ok(step);
    };
    // Not smaller (`0 <=s x·x` as `x·x <=u smax`): residues may still show it constant.
    if let Some(b) = super::predicate::decide(r, cx, n)? {
        let before = cx.len() as u32;
        let c = bool_const(r, cx, b)?;
        return Ok(
            match finish(r, cx, PassKind::Compares, n, c, before, &[], Fin::FINAL)? {
                Step::Normal(f) => Step::Normal(f.and(fin)),
                to => to,
            },
        );
    }
    Ok(Step::Normal(fin))
}

/// `(x & 2^k) != 0` (`Some((x, k, true))`) or `(x & 2^k) == 0` (`false`).
fn bit_test(cx: &Context, n: u32) -> Option<(u32, u16, bool)> {
    let node = cx.node(n);
    let set = match node.op {
        OpCode::Ne => true,
        OpCode::Eq => false,
        _ => return None,
    };
    if !cx.const_val(node.b)?.is_zero() {
        return None;
    }
    let and = cx.node(node.a);
    if and.op != OpCode::And {
        return None;
    }
    let m = cx.const_val(and.b)?;
    let one = BitVec::apply_un(UnOp::Popcnt, &m).ok()?.to_u64()? == 1;
    let k = BitVec::apply_un(UnOp::Ctz, &m).ok()?.to_u64()? as u16;
    one.then_some((and.a, k, set))
}

/// `e == c` or `e != c` decided by the residues of `e`: when `c`'s low bits are none of the
/// values `e` takes there (`x·x == 2`, `(x·x & 7) == 5`).
fn residue_decide(r: &mut Runner<'_, '_>, cx: &mut Context, n: u32) -> Result<Option<bool>, Stop> {
    let node = cx.node(n);
    if !matches!(node.op, OpCode::Eq | OpCode::Ne) {
        return Ok(None);
    }
    let (e, c) = match (cx.const_val(node.a), cx.const_val(node.b)) {
        (None, Some(c)) => (node.a, c),
        (Some(c), None) => (node.b, c),
        _ => return Ok(None),
    };
    let k = u32::from(cx.wid(e)).min(4);
    let Some(t) = super::residue::residues(r, cx, e, k)? else {
        return Ok(None);
    };
    let low = c.limbs()[0] & t.mask;
    if t.values.contains(&low) {
        return Ok(None);
    }
    Ok(Some(node.op == OpCode::Ne))
}

/// The values `r` below `2^W` where `truth[r]`, as an interval set.
fn residue_set(truth: &[bool], w: Width) -> Set {
    let mut out = Set::new();
    let mut r = 0;
    while r < truth.len() {
        if !truth[r] {
            r += 1;
            continue;
        }
        let lo = r;
        while r + 1 < truth.len() && truth[r + 1] {
            r += 1;
        }
        out.push((
            BitVec::wrapping_from_u64(w, lo as u64),
            BitVec::wrapping_from_u64(w, r as u64),
        ));
        r += 1;
    }
    out
}

/// The order reading of `n` (see `order`).
/// The most nodes above its condition an arm may have for [`arms_read`].
const MAX_ARM: usize = 64;

/// Whether `e` reads `c` among at most [`MAX_ARM`] nodes made after `c`.
fn reads(cx: &Context, e: u32, c: u32) -> bool {
    let mut seen = crate::hash::IdMap::<u32, ()>::default();
    let mut todo = vec![e];
    while let Some(v) = todo.pop() {
        if v == c {
            return true;
        }
        if v < c || seen.insert(v, ()).is_some() {
            continue;
        }
        if seen.len() > MAX_ARM {
            return false;
        }
        todo.extend(cx.node(v).children());
    }
    false
}

/// `select(c, t, f)` that is one of its values for every input (see
/// [`one_value`]).
fn arms_read(r: &mut Runner<'_, '_>, cx: &mut Context, n: u32) -> Result<Option<Step>, Stop> {
    let node = cx.node(n);
    let (c, t, f) = (node.a, node.b, node.c);
    if cx.const_val(c).is_some() {
        return Ok(None);
    }
    let before = cx.len() as u32;
    let Some(e) = one_value(r, cx, c, t, f)? else {
        super::discard(r, cx, before);
        return Ok(None);
    };
    // The nodes from before the rewrite that it reads.
    let mut stops = Vec::new();
    let mut todo = vec![e];
    let mut seen = crate::hash::IdMap::<u32, ()>::default();
    while let Some(i) = todo.pop() {
        if seen.insert(i, ()).is_some() {
            continue;
        }
        if i < before {
            stops.push(i);
        } else {
            todo.extend(cx.node(i).children());
        }
    }
    finish(r, cx, PassKind::Compares, n, e, before, &stops, Fin::FINAL).map(Some)
}

/// The one value `select(c, t, f)` takes, when it is one:
///
/// - arms that read `c` (a flag a `setcc` and a carry both spell): `t` with `c` true and `f`
///   with `c` false, when they come out alike (`select(c, zext(c), zext(c) + 1)` is 1);
/// - a value chosen by a test of `p` against a constant `k`: the arm taken when `p != k`,
///   when with `p` at `k` it is the other arm (a shift's flags by its count, `count != 0 ?
///   r == 0 : x == 0`, with `x << 0` being `x`; the shift's result itself likewise).
///
/// Only one value: a select with its arms rewritten is no smaller, and the commit rule would
/// take it on one run and not another.
fn one_value(
    r: &mut Runner<'_, '_>,
    cx: &mut Context,
    c: u32,
    t: u32,
    f: u32,
) -> Result<Option<u32>, Stop> {
    let with = |r: &mut Runner<'_, '_>, cx: &mut Context, e: u32, from: u32, to: u32| {
        let mut repl = crate::hash::IdMap::default();
        repl.insert(from, to);
        r.build(cx, |cx| Ok(cx.substitute_ids(&[e], &repl)?[0]))
    };
    if reads(cx, t, c) || reads(cx, f, c) {
        let one = bool_const(r, cx, true)?;
        let zero = bool_const(r, cx, false)?;
        let t2 = with(r, cx, t, c, one)?;
        let f2 = with(r, cx, f, c, zero)?;
        if t2 == f2 {
            return Ok(Some(t2));
        }
    }
    // (Of any width: the arm in place of the select is the select's nodes fewer, and the
    // commit rule weighs what it shares with the code around it. A shift's flags are often
    // met after their select has been taken out of the comparisons, `select(count != 0, x
    // << count, x) == 0`.)
    let cn = cx.node(c);
    if !matches!(cn.op, OpCode::Eq | OpCode::Ne) {
        return Ok(None);
    }
    let (p, k) = match (cx.const_val(cn.a), cx.const_val(cn.b)) {
        (None, Some(_)) => (cn.a, cn.b),
        (Some(_), None) => (cn.b, cn.a),
        _ => return Ok(None),
    };
    let (held, other) = if cn.op == OpCode::Eq { (t, f) } else { (f, t) };
    // The value tested, and the same value as the arm may spell it: a test of the low bits
    // `trunc<n>(y)` is of `y & (2^n − 1)` too (the count of a shift, tested at its own
    // width and used at the word's).
    let mut repl = crate::hash::IdMap::default();
    if reads(cx, other, p) {
        repl.insert(p, k);
    }
    let pn = cx.node(p);
    if pn.op == OpCode::Extract
        && pn.b == 0
        && pn.width < 64
        && let Some(kv) = cx.const_val(k).and_then(|v| v.to_u64())
    {
        let (y, low) = (pn.a, (1u64 << pn.width) - 1);
        let mut seen = crate::hash::IdMap::<u32, ()>::default();
        let mut todo = vec![other];
        let mut masks = Vec::new();
        while let Some(v) = todo.pop() {
            if v < y || seen.insert(v, ()).is_some() || seen.len() > MAX_ARM {
                continue;
            }
            let vn = cx.node(v);
            let masked =
                |a: u32, b: u32| a == y && cx.const_val(b).and_then(|c| c.to_u64()) == Some(low);
            if vn.op == OpCode::And && (masked(vn.a, vn.b) || masked(vn.b, vn.a)) {
                masks.push(v);
                continue;
            }
            todo.extend(vn.children());
        }
        for m in masks {
            let to = r.build(cx, |cx| cx.mk_const_u64(cx.wid(m), kv))?;
            repl.insert(m, to);
        }
    }
    if repl.is_empty() {
        return Ok(None);
    }
    let at = r.build(cx, |cx| Ok(cx.substitute_ids(&[other], &repl)?[0]))?;
    Ok((at == held).then_some(other))
}

fn order_step(r: &mut Runner<'_, '_>, cx: &mut Context, n: u32) -> Result<Step, Stop> {
    let before = cx.len() as u32;
    match super::order::decide(r, cx, n)? {
        Some((e, atoms, fin)) => finish(r, cx, PassKind::Compares, n, e, before, &atoms, fin),
        None => Ok(Step::Normal(Fin::FINAL)),
    }
}