celox-backend-x86 0.3.1

Celox x86-64 machine-code backend
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
//! Assign allocator-planned W/S homes to packed simulation-state words.
//!
//! This is deliberately a thin extension of the production Braun--Hack
//! allocator.  It does not run the interval allocator and it does not invent
//! a second liveness fixed point.  The existing spill plan decides where a
//! home is established and consumed; this module only proves that replacing
//! one stack home by one physical SimState word is valid at every reload.

use std::collections::{BTreeMap, BTreeSet};

use crate::HashMap;
use crate::native::mir::{BaseReg, BlockId, MFunction, MInst, PackedStateHome, SpillKind, VReg};

use super::cfg::NormalizedCfg;
use super::reload::{PointUse, ResolvedBase, ResolvedRecipe};
use super::spill_plan::{LogicalValue, PlannedEdgeOp, PlannedOp, SpillHome, SpillPlan};

type ReloadKey = (BlockId, usize, LogicalValue);
type SpillInsertion = (LogicalValue, SpillHome);

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) struct PlannedSpillInsertion {
    pub block: usize,
    pub instruction: usize,
    pub value: LogicalValue,
    pub home: SpillHome,
    /// Reconstruction emits this store as part of an atomic source-home to
    /// destination-home transfer; consumers still count the destination home
    /// definition, but must not emit a second standalone store.
    pub edge_transfer: bool,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct StateHomeError {
    pub rule: &'static str,
    pub block: Option<BlockId>,
    pub instruction: Option<usize>,
    pub values: Vec<VReg>,
    pub message: String,
}

impl StateHomeError {
    fn new(
        rule: &'static str,
        block: Option<BlockId>,
        instruction: Option<usize>,
        values: Vec<VReg>,
        message: impl Into<String>,
    ) -> Self {
        Self {
            rule,
            block,
            instruction,
            values,
            message: message.into(),
        }
    }

    fn reload(error: super::reload::ReloadRecipeError) -> Self {
        Self::new(
            error.rule,
            error.block,
            error.instruction,
            error.value.into_iter().collect(),
            error.message,
        )
    }
}

struct Probe {
    func: MFunction,
    points: BTreeMap<ReloadKey, PointUse>,
}

/// Select physical homes with two sparse MemorySSA probes.
///
/// Candidate discovery and probe construction are O(V + I + P), where P is
/// the number of planned spill/reload operations.  Each probe delegates to
/// the existing sparse byte MemorySSA.  At most one cloned MIR and one probe
/// analysis are live at a time; there is no cell-by-block or layer-by-range
/// dense product.
pub(super) fn select(
    func: &MFunction,
    cfg: &NormalizedCfg,
    plan: &mut SpillPlan,
) -> Result<(), StateHomeError> {
    plan.state_homes.clear();
    plan.state_reload_recipes.clear();

    let reloads = reload_locations(func, cfg, plan)?;
    let candidates = candidate_homes(func, plan, reloads.keys().copied());
    if candidates.is_empty() {
        return Ok(());
    }

    // Probe all candidate stores together.  Physically overlapping homes are
    // selected as one component: accepting only part of such a component
    // would make validity depend on stores that selection subsequently
    // removes.
    let first = build_probe(func, cfg, plan, &candidates, &reloads)?;
    let requested = first.points.values().copied().collect::<BTreeSet<_>>();
    let analysis = super::reload::analyze_with_queries(&first.func, cfg, &requested)
        .map_err(StateHomeError::reload)?;
    let individually_valid = candidates
        .iter()
        .filter_map(|(&home, &physical)| {
            let uses = reloads.get(&home)?;
            (!uses.is_empty()
                && uses.iter().all(|key| {
                    first.points.get(key).is_some_and(|point| {
                        analysis
                            .resolved_recipe_at_point(*point)
                            .is_some_and(|recipe| direct_home_recipe(recipe, physical))
                    })
                }))
            .then_some(home)
        })
        .collect::<BTreeSet<_>>();

    let mut selected = BTreeMap::<SpillHome, PackedStateHome>::new();
    for component in overlap_components(&candidates) {
        if component
            .iter()
            .all(|home| individually_valid.contains(home))
        {
            for home in component {
                selected.insert(home, candidates[&home]);
            }
        }
    }
    drop(analysis);
    drop(first);
    if selected.is_empty() {
        return Ok(());
    }

    // Removing rejected stores changes per-block MemorySSA write ordinals.
    // Rebuild once with exactly the stores reconstruction will emit and retain
    // those recipes, rather than adjusting structural versions heuristically.
    let final_probe = build_probe(func, cfg, plan, &selected, &reloads)?;
    let requested = final_probe
        .points
        .values()
        .copied()
        .collect::<BTreeSet<_>>();
    let final_analysis = super::reload::analyze_with_queries(&final_probe.func, cfg, &requested)
        .map_err(StateHomeError::reload)?;
    let mut recipes = BTreeMap::<ReloadKey, ResolvedRecipe>::new();
    for (&home, &physical) in &selected {
        let uses = &reloads[&home];
        for &key in uses {
            let point = final_probe.points.get(&key).copied().ok_or_else(|| {
                StateHomeError::new(
                    "STATE_HOME.RELOAD_POINT",
                    Some(key.0),
                    Some(key.1),
                    vec![VReg(key.2.0)],
                    "selected state-home reload has no probe point",
                )
            })?;
            let recipe = final_analysis
                .resolved_recipe_at_point(point)
                .filter(|recipe| direct_home_recipe(recipe, physical))
                .cloned()
                .ok_or_else(|| {
                    StateHomeError::new(
                        "STATE_HOME.FINAL_MEMORY_SSA",
                        Some(key.0),
                        Some(key.1),
                        vec![VReg(key.2.0)],
                        format!(
                            "state home {home:?} at offset {} ({:?}) is not the exact reaching definition",
                            physical.offset, physical.size
                        ),
                    )
                })?;
            recipes.insert(key, recipe);
        }
    }

    plan.state_homes = selected;
    plan.state_reload_recipes = recipes;
    verify(func, cfg, plan)
}

/// Check the selected-home table independently from candidate selection.
pub(super) fn verify(
    func: &MFunction,
    cfg: &NormalizedCfg,
    plan: &SpillPlan,
) -> Result<(), StateHomeError> {
    if let Some(home) = plan
        .state_homes
        .keys()
        .find(|home| plan.recipe_homes.contains(home))
    {
        return Err(StateHomeError::new(
            "STATE_HOME.EXCLUSIVE_KIND",
            None,
            None,
            Vec::new(),
            format!("spill home {home:?} is both state-backed and recipe-only"),
        ));
    }

    let reloads = reload_locations(func, cfg, plan)?;
    let candidates = candidate_homes(func, plan, plan.state_homes.keys().copied());
    for (&home, &physical) in &plan.state_homes {
        if candidates.get(&home) != Some(&physical) {
            return Err(StateHomeError::new(
                "STATE_HOME.DESCRIPTOR_MATCH",
                None,
                None,
                plan.homes.members(home).collect(),
                format!("selected state home {home:?} no longer matches its MIR descriptors"),
            ));
        }
        let Some(uses) = reloads.get(&home) else {
            return Err(StateHomeError::new(
                "STATE_HOME.HAS_RELOAD",
                None,
                None,
                Vec::new(),
                format!("selected state home {home:?} has no planned reload"),
            ));
        };
        for &key in uses {
            let Some(recipe) = plan.state_reload_recipes.get(&key) else {
                return Err(StateHomeError::new(
                    "STATE_HOME.RECIPE_COVERS_RELOAD",
                    Some(key.0),
                    Some(key.1),
                    vec![VReg(key.2.0)],
                    format!("selected state home {home:?} has an uncovered reload"),
                ));
            };
            if !direct_home_recipe(recipe, physical) {
                return Err(StateHomeError::new(
                    "STATE_HOME.RECIPE_SHAPE",
                    Some(key.0),
                    Some(key.1),
                    vec![VReg(key.2.0)],
                    format!("selected state home {home:?} has a non-direct reload recipe"),
                ));
            }
        }
    }
    for (&key, recipe) in &plan.state_reload_recipes {
        let matching = reloads.iter().find_map(|(&home, uses)| {
            (uses.contains(&key) && plan.state_homes.contains_key(&home)).then_some(home)
        });
        let Some(home) = matching else {
            return Err(StateHomeError::new(
                "STATE_HOME.RECIPE_NAMES_RELOAD",
                Some(key.0),
                Some(key.1),
                vec![VReg(key.2.0)],
                "state-home recipe has no matching selected reload",
            ));
        };
        if !direct_home_recipe(recipe, plan.state_homes[&home]) {
            return Err(StateHomeError::new(
                "STATE_HOME.RECIPE_SHAPE",
                Some(key.0),
                Some(key.1),
                vec![VReg(key.2.0)],
                format!("state-home recipe does not load selected home {home:?}"),
            ));
        }
    }
    Ok(())
}

fn candidate_homes(
    func: &MFunction,
    plan: &SpillPlan,
    homes: impl IntoIterator<Item = SpillHome>,
) -> BTreeMap<SpillHome, PackedStateHome> {
    let mut result = BTreeMap::new();
    for home in homes {
        let mut physical = None::<PackedStateHome>;
        let mut compatible = true;
        for member in plan.homes.members(home) {
            let Some(descriptor) = func.spill_desc(member) else {
                continue;
            };
            if matches!(descriptor.kind, SpillKind::Remat { .. }) {
                compatible = false;
                break;
            }
            let Some(candidate) = descriptor.deferred_state_home else {
                continue;
            };
            if candidate.live_on_entry || candidate.byte_range().is_none() {
                compatible = false;
                break;
            }
            match physical {
                Some(previous)
                    if previous.offset != candidate.offset || previous.size != candidate.size =>
                {
                    compatible = false;
                    break;
                }
                Some(previous) => {
                    if candidate.id < previous.id {
                        physical = Some(PackedStateHome {
                            id: candidate.id,
                            ..previous
                        });
                    }
                }
                None => physical = Some(candidate),
            }
        }
        if compatible && let Some(physical) = physical {
            result.insert(home, physical);
        }
    }
    result
}

fn reload_locations(
    func: &MFunction,
    cfg: &NormalizedCfg,
    plan: &SpillPlan,
) -> Result<BTreeMap<SpillHome, BTreeSet<ReloadKey>>, StateHomeError> {
    let mut result = BTreeMap::<SpillHome, BTreeSet<ReloadKey>>::new();
    for &(point, operation) in &plan.point_ops {
        let PlannedOp::Reload { value, home } = operation else {
            continue;
        };
        result
            .entry(home)
            .or_default()
            .insert((point.block, point.instruction, value));
    }
    for (&(predecessor, successor), operations) in &plan.edge_ops {
        let predecessor_block = func.blocks.get(predecessor).ok_or_else(|| {
            StateHomeError::new(
                "STATE_HOME.EDGE_PREDECESSOR",
                None,
                None,
                Vec::new(),
                format!("edge predecessor index {predecessor} is outside MIR"),
            )
        })?;
        if func.blocks.get(successor).is_none() {
            return Err(StateHomeError::new(
                "STATE_HOME.EDGE_SUCCESSOR",
                Some(predecessor_block.id),
                None,
                Vec::new(),
                format!("edge successor index {successor} is outside MIR"),
            ));
        }
        let insertion = super::cfg::edge_insertion_point(func, cfg, predecessor, successor)
            .ok_or_else(|| {
                StateHomeError::new(
                    "STATE_HOME.EDGE_POINT",
                    Some(predecessor_block.id),
                    None,
                    Vec::new(),
                    "edge reload has no single-edge materialization point",
                )
            })?;
        let block = &func.blocks[insertion.block];
        for &operation in operations {
            if let PlannedEdgeOp::Reload {
                source,
                source_home,
                ..
            } = operation
            {
                result.entry(source_home).or_default().insert((
                    block.id,
                    insertion.instruction,
                    source,
                ));
            }
        }
    }
    Ok(result)
}

/// Expand the abstract W/S spill operations into their actual point stores.
/// Both the MemorySSA probe and reconstruction consume this one ordered list;
/// keeping two implementations here would make structural write versions
/// depend on subtly different SpillPhi expansion.
pub(super) fn planned_spills(
    func: &MFunction,
    cfg: &NormalizedCfg,
    plan: &SpillPlan,
) -> Result<Vec<PlannedSpillInsertion>, StateHomeError> {
    let mut result = Vec::new();
    let spilled_phis = plan
        .point_ops
        .iter()
        .filter_map(|(_, operation)| match operation {
            PlannedOp::SpillPhi { value, .. } => Some(*value),
            _ => None,
        })
        .collect::<BTreeSet<_>>();
    for (successor, block) in func.blocks.iter().enumerate() {
        for phi in &block.phis {
            let logical = plan.logical.of(phi.dst);
            if !spilled_phis.contains(&logical) {
                continue;
            }
            let home = plan.homes.of_vreg(phi.dst);
            if plan.recipe_homes.contains(&home) {
                continue;
            }
            for &(predecessor, source) in &phi.sources {
                let predecessor = cfg.block_index.get(&predecessor).copied().ok_or_else(|| {
                    StateHomeError::new(
                        "STATE_HOME.PHI_PREDECESSOR",
                        Some(block.id),
                        None,
                        vec![phi.dst, source],
                        "spilled phi predecessor is outside normalized CFG",
                    )
                })?;
                let source = plan.logical.of(source);
                let has_explicit_transfer = plan
                    .edge_ops
                    .get(&(predecessor, successor))
                    .is_some_and(|operations| {
                        operations.iter().any(|operation| {
                            matches!(
                                operation,
                                PlannedEdgeOp::Spill {
                                    destination,
                                    destination_home,
                                    ..
                                } if *destination == logical && *destination_home == home
                            )
                        })
                    });
                if has_explicit_transfer {
                    continue;
                }
                if plan.homes.of_logical(source) == home
                    && plan.s_exit[predecessor].contains(&source)
                {
                    continue;
                }
                let insertion = super::cfg::edge_insertion_point(func, cfg, predecessor, successor)
                    .ok_or_else(|| {
                        StateHomeError::new(
                            "STATE_HOME.PHI_EDGE_POINT",
                            Some(block.id),
                            None,
                            vec![phi.dst, VReg(source.0)],
                            "spilled phi edge has no single-edge materialization point",
                        )
                    })?;
                result.push(PlannedSpillInsertion {
                    block: insertion.block,
                    instruction: insertion.instruction,
                    value: source,
                    home,
                    edge_transfer: false,
                });
            }
        }
    }
    for &(point, operation) in &plan.point_ops {
        let PlannedOp::Spill { value, home } = operation else {
            continue;
        };
        let block = cfg.block_index.get(&point.block).copied().ok_or_else(|| {
            StateHomeError::new(
                "STATE_HOME.POINT_BLOCK",
                Some(point.block),
                Some(point.instruction),
                vec![VReg(value.0)],
                "spill point is outside normalized CFG",
            )
        })?;
        result.push(PlannedSpillInsertion {
            block,
            instruction: point.instruction,
            value,
            home,
            edge_transfer: false,
        });
    }
    for (&(predecessor, successor), operations) in &plan.edge_ops {
        let insertion = super::cfg::edge_insertion_point(func, cfg, predecessor, successor)
            .ok_or_else(|| {
                StateHomeError::new(
                    "STATE_HOME.EDGE_POINT",
                    func.blocks.get(predecessor).map(|block| block.id),
                    None,
                    Vec::new(),
                    "spill edge has no single-edge materialization point",
                )
            })?;
        for (operation_index, &operation) in operations.iter().enumerate() {
            match operation {
                PlannedEdgeOp::Spill {
                    source,
                    destination,
                    destination_home,
                } => {
                    let is_home_transfer = operation_index != 0
                        && matches!(
                            operations[operation_index - 1],
                            PlannedEdgeOp::Reload {
                                destination: reload_destination,
                                ..
                            } if reload_destination == destination && source == destination
                        );
                    result.push(PlannedSpillInsertion {
                        block: insertion.block,
                        instruction: insertion.instruction,
                        value: source,
                        home: destination_home,
                        edge_transfer: is_home_transfer,
                    });
                }
                PlannedEdgeOp::Reload { .. } => {}
            }
        }
    }
    Ok(result)
}

fn build_probe(
    func: &MFunction,
    cfg: &NormalizedCfg,
    plan: &SpillPlan,
    homes: &BTreeMap<SpillHome, PackedStateHome>,
    reloads: &BTreeMap<SpillHome, BTreeSet<ReloadKey>>,
) -> Result<Probe, StateHomeError> {
    let mut insertions = HashMap::<(usize, usize), Vec<SpillInsertion>>::default();
    for spill in planned_spills(func, cfg, plan)? {
        if spill.edge_transfer {
            // The probe has no synthetic VReg for the atomic source-home
            // reload.  Conservatively leave this destination out of the
            // deferred-state candidate proof instead of pretending its
            // successor logical value is resident on the predecessor edge.
            continue;
        }
        if homes.contains_key(&spill.home) {
            insertions
                .entry((spill.block, spill.instruction))
                .or_default()
                .push((spill.value, spill.home));
        }
    }

    let mut probe = func.clone();
    let mut shifted = vec![Vec::<usize>::new(); probe.blocks.len()];
    for (block, (probe_block, shifted_block)) in
        probe.blocks.iter_mut().zip(&mut shifted).enumerate()
    {
        let original = std::mem::take(&mut probe_block.insts);
        shifted_block.reserve(original.len());
        let extra = insertions
            .iter()
            .filter(|((owner, _), _)| *owner == block)
            .map(|(_, stores)| stores.len())
            .sum::<usize>();
        let mut rewritten = Vec::with_capacity(original.len().saturating_add(extra));
        for (instruction, inst) in original.into_iter().enumerate() {
            for &(value, home) in insertions.get(&(block, instruction)).into_iter().flatten() {
                let physical = homes[&home];
                rewritten.push(MInst::Store {
                    base: BaseReg::SimState,
                    offset: physical.offset,
                    src: VReg(value.0),
                    size: physical.size,
                });
            }
            shifted_block.push(rewritten.len());
            rewritten.push(inst);
        }
        probe_block.insts = rewritten;
    }
    if let Some((&(block, instruction), stores)) =
        insertions.iter().find(|((block, instruction), _)| {
            shifted
                .get(*block)
                .is_none_or(|indices| *instruction >= indices.len())
        })
    {
        return Err(StateHomeError::new(
            "STATE_HOME.SPILL_POINT_RANGE",
            func.blocks.get(block).map(|owner| owner.id),
            Some(instruction),
            stores.iter().map(|(value, _)| VReg(value.0)).collect(),
            "state-home spill point is outside its MIR block",
        ));
    }

    let mut points = BTreeMap::<ReloadKey, PointUse>::new();
    for home in homes.keys() {
        let Some(uses) = reloads.get(home) else {
            continue;
        };
        for &key in uses {
            let block = cfg.block_index.get(&key.0).copied().ok_or_else(|| {
                StateHomeError::new(
                    "STATE_HOME.RELOAD_BLOCK",
                    Some(key.0),
                    Some(key.1),
                    vec![VReg(key.2.0)],
                    "state-home reload block is outside normalized CFG",
                )
            })?;
            let instruction = shifted
                .get(block)
                .and_then(|indices| indices.get(key.1))
                .copied()
                .ok_or_else(|| {
                    StateHomeError::new(
                        "STATE_HOME.RELOAD_POINT_RANGE",
                        Some(key.0),
                        Some(key.1),
                        vec![VReg(key.2.0)],
                        "state-home reload point is outside its MIR block",
                    )
                })?;
            points.insert(
                key,
                PointUse {
                    block: key.0,
                    instruction,
                    value: VReg(key.2.0),
                },
            );
        }
    }
    Ok(Probe {
        func: probe,
        points,
    })
}

fn direct_home_recipe(recipe: &ResolvedRecipe, home: PackedStateHome) -> bool {
    recipe.steps.is_empty()
        && matches!(
            &recipe.base,
            ResolvedBase::State(state)
                if state.load.offset == home.offset && state.load.size == home.size
        )
}

fn overlap_components(homes: &BTreeMap<SpillHome, PackedStateHome>) -> Vec<Vec<SpillHome>> {
    let mut intervals = homes
        .iter()
        .filter_map(|(&home, &physical)| {
            physical
                .byte_range()
                .map(|range| (range.start, range.end, home))
        })
        .collect::<Vec<_>>();
    intervals.sort_unstable();
    let mut result = Vec::<Vec<SpillHome>>::new();
    let mut current = Vec::<SpillHome>::new();
    let mut current_end = i64::MIN;
    for (start, end, home) in intervals {
        if current.is_empty() || start < current_end {
            current.push(home);
            current_end = current_end.max(end);
        } else {
            result.push(std::mem::take(&mut current));
            current.push(home);
            current_end = end;
        }
    }
    if !current.is_empty() {
        result.push(current);
    }
    result
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::native::mir::{MBlock, OpSize, SpillDesc, StateHomeId, VRegAllocator};
    use crate::native::regalloc::{cfg, next_use, reconstruct, reload, spill_plan};

    fn home(id: u32, offset: i32) -> PackedStateHome {
        PackedStateHome {
            id: StateHomeId(id),
            offset,
            size: OpSize::S64,
            live_on_entry: false,
        }
    }

    fn blank_plan(
        func: &MFunction,
        cfg: &NormalizedCfg,
        next_use: &super::super::next_use::NextUseAnalysis,
    ) -> SpillPlan {
        let mut plan = spill_plan::plan(func, cfg, next_use, 32).unwrap();
        plan.point_ops.clear();
        plan.edge_ops.clear();
        plan.recipe_reloads.clear();
        plan.recipe_homes.clear();
        plan.state_homes.clear();
        plan.state_reload_recipes.clear();
        plan
    }

    fn point(instruction: usize) -> super::super::spill_plan::ProgramPoint {
        super::super::spill_plan::ProgramPoint {
            block: BlockId(0),
            instruction,
            side: super::super::spill_plan::PointSide::Before,
        }
    }

    #[test]
    fn branch_edge_operations_use_the_single_predecessor_successor_entry() {
        let mut vregs = VRegAllocator::new();
        let condition = vregs.alloc();
        let value = vregs.alloc();
        let mut func = MFunction::new(vregs, vec![SpillDesc::transient(); 2]);

        let mut entry = MBlock::new(BlockId(0));
        entry.push(MInst::LoadImm {
            dst: condition,
            value: 1,
        });
        entry.push(MInst::LoadImm {
            dst: value,
            value: 0x1234,
        });
        entry.push(MInst::Branch {
            cond: condition,
            true_bb: BlockId(1),
            false_bb: BlockId(2),
        });
        let mut left = MBlock::new(BlockId(1));
        left.push(MInst::Return);
        let mut right = MBlock::new(BlockId(2));
        right.push(MInst::Return);
        func.blocks = vec![entry, left, right];

        let cfg = cfg::normalize(&mut func).unwrap();
        let next_use = next_use::analyze(&func, &cfg).unwrap();
        let mut plan = blank_plan(&func, &cfg, &next_use);
        let entry = cfg.block_index[&BlockId(0)];
        let successor = cfg.successors[entry][0];
        assert_eq!(cfg.predecessors[successor], [entry]);

        let logical = plan.logical.of(value);
        let spill_home = plan.homes.of_vreg(value);
        plan.edge_ops.insert(
            (entry, successor),
            vec![
                PlannedEdgeOp::Spill {
                    source: logical,
                    destination: logical,
                    destination_home: spill_home,
                },
                PlannedEdgeOp::Reload {
                    source: logical,
                    source_home: spill_home,
                    destination: logical,
                },
            ],
        );

        assert_eq!(
            planned_spills(&func, &cfg, &plan).unwrap(),
            vec![PlannedSpillInsertion {
                block: successor,
                instruction: 0,
                value: logical,
                home: spill_home,
                edge_transfer: false,
            }]
        );
        let queries = super::super::ssa::planner_reload_queries(&func, &cfg, &plan).unwrap();
        assert_eq!(
            queries,
            BTreeSet::from([PointUse {
                block: func.blocks[successor].id,
                instruction: 0,
                value: VReg(logical.0),
            }])
        );
    }

    #[test]
    fn selected_home_reconstructs_as_verified_state_store_and_load() {
        let mut vregs = VRegAllocator::new();
        let value = vregs.alloc();
        let marker = vregs.alloc();
        let mut func = MFunction::new(
            vregs,
            vec![
                SpillDesc::transient().with_deferred_state_home(home(0, 0)),
                SpillDesc::transient(),
            ],
        );
        let mut block = MBlock::new(BlockId(0));
        block.push(MInst::LoadImm {
            dst: value,
            value: 0x1234,
        });
        block.push(MInst::LoadImm {
            dst: marker,
            value: 1,
        });
        block.push(MInst::Store {
            base: BaseReg::SimState,
            offset: 16,
            src: value,
            size: OpSize::S64,
        });
        block.push(MInst::Return);
        func.blocks.push(block);

        let cfg = cfg::normalize(&mut func).unwrap();
        let next_use = next_use::analyze(&func, &cfg).unwrap();
        let mut plan = blank_plan(&func, &cfg, &next_use);
        let logical = plan.logical.of(value);
        let spill_home = plan.homes.of_vreg(value);
        plan.point_ops = vec![
            (
                point(1),
                PlannedOp::Spill {
                    value: logical,
                    home: spill_home,
                },
            ),
            (
                point(2),
                PlannedOp::Reload {
                    value: logical,
                    home: spill_home,
                },
            ),
        ];

        select(&func, &cfg, &mut plan).unwrap();
        assert_eq!(plan.state_homes.get(&spill_home), Some(&home(0, 0)));
        assert_eq!(plan.state_reload_recipes.len(), 1);

        let requested = super::super::ssa::planner_reload_queries(&func, &cfg, &plan).unwrap();
        let ordinary_recipes = reload::analyze_with_queries(&func, &cfg, &requested).unwrap();
        let result = reconstruct::reconstruct(
            &mut func,
            &cfg,
            &plan,
            &next_use,
            &ordinary_recipes,
            false,
            true,
        )
        .unwrap();
        assert_eq!(result.frame_size, 0);
        assert!(func.blocks[0].insts.iter().any(|inst| {
            matches!(
                inst,
                MInst::Store {
                    base: BaseReg::SimState,
                    offset: 0,
                    size: OpSize::S64,
                    ..
                }
            )
        }));
        assert!(func.blocks[0].insts.iter().any(|inst| {
            matches!(
                inst,
                MInst::Load {
                    base: BaseReg::SimState,
                    offset: 0,
                    size: OpSize::S64,
                    ..
                }
            )
        }));
        assert!(!func.blocks[0].insts.iter().any(|inst| {
            matches!(
                inst,
                MInst::Store {
                    base: BaseReg::StackFrame,
                    ..
                } | MInst::Load {
                    base: BaseReg::StackFrame,
                    ..
                }
            )
        }));
        super::super::materialized_state_home::verify_materialized_state_homes(
            &func,
            &cfg,
            &result.state_stores,
            &result.state_reloads,
        )
        .unwrap();
        reload::verify_expected_materialized_reloads(&func, &cfg, &result.recipe_reloads).unwrap();
    }

    #[test]
    fn overlapping_original_write_rejects_deferred_state_home() {
        let mut vregs = VRegAllocator::new();
        let value = vregs.alloc();
        let clobber = vregs.alloc();
        let mut func = MFunction::new(
            vregs,
            vec![
                SpillDesc::transient().with_deferred_state_home(home(0, 0)),
                SpillDesc::transient(),
            ],
        );
        let mut block = MBlock::new(BlockId(0));
        block.push(MInst::LoadImm {
            dst: value,
            value: 0x1234,
        });
        block.push(MInst::LoadImm {
            dst: clobber,
            value: 0x5678,
        });
        block.push(MInst::Store {
            base: BaseReg::SimState,
            offset: 0,
            src: clobber,
            size: OpSize::S64,
        });
        block.push(MInst::Store {
            base: BaseReg::SimState,
            offset: 16,
            src: value,
            size: OpSize::S64,
        });
        block.push(MInst::Return);
        func.blocks.push(block);

        let cfg = cfg::normalize(&mut func).unwrap();
        let next_use = next_use::analyze(&func, &cfg).unwrap();
        let mut plan = blank_plan(&func, &cfg, &next_use);
        let logical = plan.logical.of(value);
        let spill_home = plan.homes.of_vreg(value);
        plan.point_ops = vec![
            (
                point(1),
                PlannedOp::Spill {
                    value: logical,
                    home: spill_home,
                },
            ),
            (
                point(3),
                PlannedOp::Reload {
                    value: logical,
                    home: spill_home,
                },
            ),
        ];

        select(&func, &cfg, &mut plan).unwrap();
        assert!(plan.state_homes.is_empty());
        assert!(plan.state_reload_recipes.is_empty());
    }
}