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
//! Normalized CFG information shared by every allocation phase.

use std::collections::BTreeSet;

use celox_analysis::cfg::ForwardControlFlowGraph;
pub(super) use celox_analysis::cfg::NaturalLoop;
use celox_analysis::ssa::SsaCfg;

use crate::HashMap;
use crate::native::mir::{BlockId, MBlock, MFunction, MInst};

#[derive(Debug)]
pub(super) struct NormalizedCfg {
    pub block_index: HashMap<BlockId, usize>,
    pub predecessors: Vec<Vec<usize>>,
    pub successors: Vec<Vec<usize>>,
    pub idom: Vec<Option<usize>>,
    pub dominator_children: Vec<Vec<usize>>,
    pub dominance_frontier: Vec<BTreeSet<usize>>,
    pub loops: Vec<NaturalLoop>,
    pub loop_for_header: HashMap<usize, usize>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) struct EdgeInsertionPoint {
    pub block: usize,
    pub instruction: usize,
}

/// Return the concrete point that executes on exactly one normalized CFG edge.
/// A single-successor predecessor uses its terminator; a branch edge uses the
/// entry of its dedicated single-predecessor successor block. Edge-local
/// transfers may be placed in that successor, so its edge identity must not
/// depend on the block remaining syntactically Jump-only.
pub(super) fn edge_insertion_point(
    func: &MFunction,
    cfg: &NormalizedCfg,
    predecessor: usize,
    successor: usize,
) -> Option<EdgeInsertionPoint> {
    if !cfg.successors.get(predecessor)?.contains(&successor) {
        return None;
    }
    if cfg.successors[predecessor].len() == 1 {
        return Some(EdgeInsertionPoint {
            block: predecessor,
            instruction: func.blocks.get(predecessor)?.insts.len().checked_sub(1)?,
        });
    }
    if cfg.predecessors.get(successor)?.as_slice() == [predecessor]
        && !func.blocks.get(successor)?.insts.is_empty()
    {
        return Some(EdgeInsertionPoint {
            block: successor,
            instruction: 0,
        });
    }
    None
}

impl SsaCfg for NormalizedCfg {
    type FrontierIter<'a> = std::iter::Copied<std::collections::btree_set::Iter<'a, usize>>;

    fn root(&self) -> usize {
        0
    }

    fn predecessors(&self) -> &[Vec<usize>] {
        &self.predecessors
    }

    fn successors(&self) -> &[Vec<usize>] {
        &self.successors
    }

    fn dominator_children(&self) -> &[Vec<usize>] {
        &self.dominator_children
    }

    fn dominance_frontier_len(&self) -> usize {
        self.dominance_frontier.len()
    }

    fn dominance_frontier(&self, block: usize) -> Self::FrontierIter<'_> {
        self.dominance_frontier[block].iter().copied()
    }
}

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

impl CfgError {
    pub(super) fn new(
        rule: &'static str,
        block: Option<BlockId>,
        message: impl Into<String>,
    ) -> Self {
        Self {
            rule,
            block,
            message: message.into(),
        }
    }
}

impl NormalizedCfg {
    pub(super) fn verify(&self, func: &MFunction) -> Result<(), CfgError> {
        let blocks = func.blocks.len();
        if blocks == 0 {
            return Err(CfgError::new(
                "CFG.NON_EMPTY",
                None,
                "normalized CFG cannot describe an empty function",
            ));
        }
        if self.block_index.len() != blocks
            || self.predecessors.len() != blocks
            || self.successors.len() != blocks
            || self.idom.len() != blocks
            || self.dominator_children.len() != blocks
            || self.dominance_frontier.len() != blocks
        {
            return Err(CfgError::new(
                "CFG.MODEL_SHAPE",
                None,
                "normalized CFG tables do not cover every MIR block",
            ));
        }
        for (index, block) in func.blocks.iter().enumerate() {
            if self.block_index.get(&block.id) != Some(&index) {
                return Err(CfgError::new(
                    "CFG.BLOCK_INDEX_BIJECTION",
                    Some(block.id),
                    format!(
                        "block-index table maps {} to {:?}, expected {index}",
                        block.id,
                        self.block_index.get(&block.id)
                    ),
                ));
            }
        }
        for (block, predecessors) in self.predecessors.iter().enumerate() {
            if let Some(&predecessor) = predecessors.iter().find(|&&index| index >= blocks) {
                return Err(CfgError::new(
                    "CFG.PREDECESSOR_RANGE",
                    Some(func.blocks[block].id),
                    format!("predecessor index {predecessor} is outside the function"),
                ));
            }
        }
        for (block, successors) in self.successors.iter().enumerate() {
            if let Some(&successor) = successors.iter().find(|&&index| index >= blocks) {
                return Err(CfgError::new(
                    "CFG.SUCCESSOR_RANGE",
                    Some(func.blocks[block].id),
                    format!("successor index {successor} is outside the function"),
                ));
            }
        }

        let mut expected_predecessors = vec![Vec::new(); blocks];
        for (block, mir_block) in func.blocks.iter().enumerate() {
            let mut expected_successors = Vec::new();
            for successor_id in mir_block.successors() {
                let Some(&successor) = self.block_index.get(&successor_id) else {
                    return Err(CfgError::new(
                        "CFG.MIR_TARGET_EXISTS",
                        Some(mir_block.id),
                        format!("terminator targets missing block {successor_id}"),
                    ));
                };
                if !expected_successors.contains(&successor) {
                    expected_successors.push(successor);
                    expected_predecessors[successor].push(block);
                }
            }
            if self.successors[block] != expected_successors {
                return Err(CfgError::new(
                    "CFG.SUCCESSORS_MATCH_MIR",
                    Some(mir_block.id),
                    format!(
                        "normalized successors {:?} differ from MIR successors {expected_successors:?}",
                        self.successors[block]
                    ),
                ));
            }
        }
        for (block, expected) in expected_predecessors.iter().enumerate() {
            if self.predecessors[block] != *expected {
                return Err(CfgError::new(
                    "CFG.EDGE_RECIPROCITY",
                    Some(func.blocks[block].id),
                    format!(
                        "normalized predecessors {:?} differ from incoming successor edges {:?}",
                        self.predecessors[block], expected
                    ),
                ));
            }
        }
        if !self.idom.first().is_some_and(Option::is_none) {
            return Err(CfgError::new(
                "CFG.ENTRY_IDOM",
                func.blocks.first().map(|block| block.id),
                "entry block must not have an immediate dominator",
            ));
        }
        if !self.predecessors.first().is_some_and(Vec::is_empty) {
            return Err(CfgError::new(
                "CFG.ENTRY_HAS_NO_PREDECESSORS",
                func.blocks.first().map(|block| block.id),
                "entry block must be the unique predecessor-free CFG root",
            ));
        }
        if let Some((block, _)) = self
            .idom
            .iter()
            .enumerate()
            .skip(1)
            .find(|(_, idom)| idom.is_none())
        {
            return Err(CfgError::new(
                "CFG.REACHABLE_IDOM",
                Some(func.blocks[block].id),
                "reachable non-entry block has no immediate dominator",
            ));
        }
        for block in 1..blocks {
            let Some(parent) = self.idom[block] else {
                return Err(CfgError::new(
                    "CFG.REACHABLE_IDOM",
                    Some(func.blocks[block].id),
                    "reachable non-entry block has no immediate dominator",
                ));
            };
            if parent >= blocks {
                return Err(CfgError::new(
                    "CFG.IDOM_RANGE",
                    Some(func.blocks[block].id),
                    format!("immediate dominator index {parent} is outside the function"),
                ));
            }
            let mut current = block;
            let mut reaches_entry = false;
            for _ in 0..blocks {
                let Some(parent) = self.idom[current] else {
                    reaches_entry = current == 0;
                    break;
                };
                current = parent;
            }
            if !reaches_entry {
                return Err(CfgError::new(
                    "CFG.IDOM_TREE",
                    Some(func.blocks[block].id),
                    "immediate-dominator links do not form a tree rooted at entry",
                ));
            }
        }
        let expected = analyze_shared_graph(self.successors.clone())?;
        if self.idom != expected.dominators.idom {
            return Err(CfgError::new(
                "CFG.IDOM_MATCHES_GRAPH",
                None,
                "immediate-dominator table does not match the normalized graph",
            ));
        }
        if self.dominator_children != expected.dominators.children {
            return Err(CfgError::new(
                "CFG.DOMINATOR_CHILDREN_MATCH_GRAPH",
                None,
                "dominator-tree children do not match the normalized graph",
            ));
        }
        for (block, frontier) in self.dominance_frontier.iter().enumerate() {
            if frontier.iter().any(|member| *member >= blocks) {
                return Err(CfgError::new(
                    "CFG.DOMINANCE_FRONTIER_RANGE",
                    Some(func.blocks[block].id),
                    "dominance frontier contains a block outside the function",
                ));
            }
        }
        let expected_frontier = expected
            .dominance_frontier
            .iter()
            .map(|frontier| frontier.iter().copied().collect::<BTreeSet<_>>())
            .collect::<Vec<_>>();
        if self.dominance_frontier != expected_frontier {
            return Err(CfgError::new(
                "CFG.DOMINANCE_FRONTIER_MATCHES_GRAPH",
                None,
                "dominance-frontier table does not match the normalized graph",
            ));
        }

        if self.loop_for_header.len() != self.loops.len() {
            return Err(CfgError::new(
                "CFG.LOOP_HEADER_INDEX",
                None,
                "loop-header index is not a bijection over natural loops",
            ));
        }
        for (loop_index, natural_loop) in self.loops.iter().enumerate() {
            if natural_loop.header >= blocks || !natural_loop.blocks.contains(&natural_loop.header)
            {
                return Err(CfgError::new(
                    "CFG.LOOP_CONTAINS_HEADER",
                    func.blocks.get(natural_loop.header).map(|block| block.id),
                    "natural loop does not contain a valid header",
                ));
            }
            if let Some(&member) = natural_loop.blocks.iter().find(|&&member| member >= blocks) {
                return Err(CfgError::new(
                    "CFG.LOOP_MEMBER_RANGE",
                    Some(func.blocks[natural_loop.header].id),
                    format!("natural loop contains out-of-range block index {member}"),
                ));
            }
            if self.loop_for_header.get(&natural_loop.header) != Some(&loop_index) {
                return Err(CfgError::new(
                    "CFG.LOOP_HEADER_INDEX",
                    Some(func.blocks[natural_loop.header].id),
                    "loop header index does not point back to its loop",
                ));
            }
            if let Some(parent) = natural_loop.parent {
                if parent >= self.loops.len() || parent <= loop_index {
                    return Err(CfgError::new(
                        "CFG.LOOP_FOREST",
                        Some(func.blocks[natural_loop.header].id),
                        format!(
                            "loop parent {parent} must be a later valid loop than child {loop_index}"
                        ),
                    ));
                }
                if !self.loops[parent].blocks.is_superset(&natural_loop.blocks) {
                    return Err(CfgError::new(
                        "CFG.LOOP_PARENT_CONTAINS_CHILD",
                        Some(func.blocks[natural_loop.header].id),
                        "loop parent does not contain every child block",
                    ));
                }
            }
        }
        if self.loops != expected.loops {
            return Err(CfgError::new(
                "CFG.LOOPS_MATCH_GRAPH",
                None,
                "natural-loop forest does not match the normalized graph",
            ));
        }
        for (block, successors) in self.successors.iter().enumerate() {
            if successors.len() < 2 {
                continue;
            }
            for &successor in successors {
                if self.predecessors[successor].as_slice() != [block]
                    || !func.blocks[successor].phis.is_empty()
                    || !matches!(
                        func.blocks[successor].insts.as_slice(),
                        [MInst::Jump { .. }]
                    )
                {
                    return Err(CfgError::new(
                        "CFG.BRANCH_EDGE_ISOLATED",
                        Some(func.blocks[successor].id),
                        "branch successor is not a dedicated one-predecessor edge block",
                    ));
                }
            }
        }
        Ok(())
    }
}

pub(super) fn normalize(func: &mut MFunction) -> Result<NormalizedCfg, CfgError> {
    if func.blocks.is_empty() {
        return Err(CfgError::new(
            "CFG.NON_EMPTY",
            None,
            "cannot normalize an empty function",
        ));
    }
    let entry = func.blocks[0].id;
    if func
        .blocks
        .iter()
        .any(|block| block.successors().contains(&entry))
    {
        return Err(CfgError::new(
            "CFG.ENTRY_HAS_NO_PREDECESSORS",
            Some(entry),
            "entry block must be the unique predecessor-free CFG root",
        ));
    }
    split_critical_edges(func)?;
    super::reorder_blocks_rpo(func)?;
    let (block_index, _, successors) = graph(func);
    let analysis = analyze_shared_graph(successors)?;
    let dominance_frontier = analysis
        .dominance_frontier
        .iter()
        .map(|frontier| frontier.iter().copied().collect::<BTreeSet<_>>())
        .collect();
    let loop_for_header = analysis
        .loops
        .iter()
        .enumerate()
        .map(|(loop_index, natural_loop)| (natural_loop.header, loop_index))
        .collect();
    Ok(NormalizedCfg {
        block_index,
        predecessors: analysis.predecessors,
        successors: analysis.successors,
        idom: analysis.dominators.idom,
        dominator_children: analysis.dominators.children,
        dominance_frontier,
        loops: analysis.loops,
        loop_for_header,
    })
}

fn analyze_shared_graph(successors: Vec<Vec<usize>>) -> Result<ForwardControlFlowGraph, CfgError> {
    ForwardControlFlowGraph::analyze(successors, 0).map_err(|error| {
        CfgError::new(
            "CFG.SHARED_ANALYSIS",
            None,
            format!("IR-independent CFG analysis failed: {error}"),
        )
    })
}

fn split_critical_edges(func: &mut MFunction) -> Result<(), CfgError> {
    for block in &mut func.blocks {
        if let Some((true_bb, false_bb)) = block.insts.last().and_then(MInst::branch_targets)
            && true_bb == false_bb
        {
            let target = true_bb;
            if let Some(terminator) = block.insts.last_mut() {
                *terminator = MInst::Jump { target };
            }
        }
    }
    let (block_index, predecessors, _) = graph(func);
    let mut edges = Vec::<(BlockId, BlockId)>::new();
    for predecessor in &func.blocks {
        let mut successors = predecessor.successors();
        successors.sort();
        successors.dedup();
        if successors.len() < 2 {
            continue;
        }
        for successor in successors {
            // Every branch edge gets a dedicated insertion block.  Critical
            // edge splitting alone is insufficient for edge-local spill and
            // parallel-copy operations when the successor has one predecessor.
            let successor_index = block_index[&successor];
            let successor_block = &func.blocks[successor_index];
            let already_edge_block = predecessors[successor_index].len() == 1
                && successor_block.phis.is_empty()
                && matches!(successor_block.insts.as_slice(), [MInst::Jump { .. }]);
            if already_edge_block {
                continue;
            }
            edges.push((predecessor.id, successor));
        }
    }
    if edges.is_empty() {
        return Ok(());
    }

    let Some(mut next_id) = func
        .blocks
        .iter()
        .map(|block| block.id.0)
        .max()
        .unwrap_or(0)
        .checked_add(1)
    else {
        return Err(CfgError::new(
            "CFG.BLOCK_ID_RANGE",
            None,
            "BlockId overflow while splitting branch edges",
        ));
    };
    for (predecessor, successor) in edges {
        let edge = BlockId(next_id);
        let Some(next) = next_id.checked_add(1) else {
            return Err(CfgError::new(
                "CFG.BLOCK_ID_RANGE",
                Some(predecessor),
                "BlockId overflow while splitting branch edges",
            ));
        };
        next_id = next;
        // New blocks are appended, so every original block keeps the index
        // recorded by the graph built above.  Looking both endpoints up in that
        // index avoids an O(blocks) scan for every split branch edge.
        let predecessor_index = block_index[&predecessor];
        let Some(terminator) = func.blocks[predecessor_index].insts.last_mut() else {
            return Err(CfgError::new(
                "CFG.EDGE_PREDECESSOR_TERMINATED",
                Some(predecessor),
                "branch-edge predecessor has no terminator",
            ));
        };
        rewrite_target(terminator, successor, edge, predecessor)?;
        let successor_index = block_index[&successor];
        for phi in &mut func.blocks[successor_index].phis {
            let Some(source) = phi
                .sources
                .iter_mut()
                .find(|(source_predecessor, _)| *source_predecessor == predecessor)
            else {
                return Err(CfgError::new(
                    "CFG.PHI_COVERS_SPLIT_EDGE",
                    Some(successor),
                    "phi has no source for branch edge being split",
                ));
            };
            source.0 = edge;
        }
        let mut edge_block = MBlock::new(edge);
        edge_block.push(MInst::Jump { target: successor });
        func.blocks.push(edge_block);
    }
    Ok(())
}

fn rewrite_target(
    terminator: &mut MInst,
    old: BlockId,
    new: BlockId,
    predecessor: BlockId,
) -> Result<(), CfgError> {
    let mut rewritten = false;
    terminator.rewrite_successors(|target| {
        if target == old {
            rewritten = true;
            new
        } else {
            target
        }
    });
    if !rewritten {
        return Err(CfgError::new(
            "CFG.TERMINATOR_NAMES_SPLIT_EDGE",
            Some(predecessor),
            "branch edge is not named by predecessor terminator",
        ));
    }
    Ok(())
}

fn graph(func: &MFunction) -> (HashMap<BlockId, usize>, Vec<Vec<usize>>, Vec<Vec<usize>>) {
    let block_index = func
        .blocks
        .iter()
        .enumerate()
        .map(|(index, block)| (block.id, index))
        .collect::<HashMap<_, _>>();
    let mut predecessors = vec![Vec::new(); func.blocks.len()];
    let mut successors = vec![Vec::new(); func.blocks.len()];
    for (index, block) in func.blocks.iter().enumerate() {
        for successor in block.successors() {
            let successor = block_index[&successor];
            if !successors[index].contains(&successor) {
                successors[index].push(successor);
                predecessors[successor].push(index);
            }
        }
    }
    (block_index, predecessors, successors)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::native::mir::{PhiNode, SpillDesc, VRegAllocator};

    fn two_block_cfg() -> (MFunction, NormalizedCfg) {
        let mut func = MFunction::new(VRegAllocator::new(), Vec::new());
        let mut entry = MBlock::new(BlockId(0));
        entry.push(MInst::Jump { target: BlockId(1) });
        let mut exit = MBlock::new(BlockId(1));
        exit.push(MInst::Return);
        func.blocks = vec![entry, exit];
        func.verify_result().unwrap();
        let cfg = normalize(&mut func).unwrap();
        (func, cfg)
    }

    fn natural_loop_cfg() -> (MFunction, NormalizedCfg) {
        let mut vregs = VRegAllocator::new();
        let condition = vregs.alloc();
        let mut func = MFunction::new(vregs, vec![SpillDesc::transient()]);
        let mut entry = MBlock::new(BlockId(0));
        entry.push(MInst::LoadImm {
            dst: condition,
            value: 1,
        });
        entry.push(MInst::Jump { target: BlockId(1) });
        let mut header = MBlock::new(BlockId(1));
        header.push(MInst::Branch {
            cond: condition,
            true_bb: BlockId(2),
            false_bb: BlockId(3),
        });
        let mut body = MBlock::new(BlockId(2));
        body.push(MInst::Jump { target: BlockId(1) });
        let mut exit = MBlock::new(BlockId(3));
        exit.push(MInst::Return);
        func.blocks = vec![entry, header, body, exit];
        func.verify_result().unwrap();
        let cfg = normalize(&mut func).unwrap();
        assert!(!cfg.loops.is_empty());
        (func, cfg)
    }

    #[test]
    fn malformed_block_index_is_a_structured_error() {
        let (func, mut cfg) = two_block_cfg();
        cfg.block_index.insert(func.blocks[0].id, 1);

        let error = cfg.verify(&func).unwrap_err();

        assert_eq!(error.rule, "CFG.BLOCK_INDEX_BIJECTION");
        assert_eq!(error.block, Some(func.blocks[0].id));
    }

    #[test]
    fn out_of_range_predecessor_is_a_structured_error() {
        let (func, mut cfg) = two_block_cfg();
        cfg.predecessors[1].push(func.blocks.len());

        let error = cfg.verify(&func).unwrap_err();

        assert_eq!(error.rule, "CFG.PREDECESSOR_RANGE");
        assert_eq!(error.block, Some(func.blocks[1].id));
    }

    #[test]
    fn nonreciprocal_edge_is_a_structured_error() {
        let (func, mut cfg) = two_block_cfg();
        cfg.predecessors[1].clear();

        let error = cfg.verify(&func).unwrap_err();

        assert_eq!(error.rule, "CFG.EDGE_RECIPROCITY");
        assert_eq!(error.block, Some(func.blocks[1].id));
    }

    #[test]
    fn out_of_range_idom_is_a_structured_error() {
        let (func, mut cfg) = two_block_cfg();
        cfg.idom[1] = Some(func.blocks.len());

        let error = cfg.verify(&func).unwrap_err();

        assert_eq!(error.rule, "CFG.IDOM_RANGE");
        assert_eq!(error.block, Some(func.blocks[1].id));
    }

    #[test]
    fn incorrect_dominance_frontier_is_a_structured_error() {
        let (func, mut cfg) = two_block_cfg();
        cfg.dominance_frontier[0].insert(1);

        let error = cfg.verify(&func).unwrap_err();

        assert_eq!(error.rule, "CFG.DOMINANCE_FRONTIER_MATCHES_GRAPH");
    }

    #[test]
    fn out_of_range_loop_member_is_a_structured_error() {
        let (func, mut cfg) = natural_loop_cfg();
        cfg.loops[0].blocks.insert(func.blocks.len());

        let error = cfg.verify(&func).unwrap_err();

        assert_eq!(error.rule, "CFG.LOOP_MEMBER_RANGE");
    }

    #[test]
    fn cyclic_loop_parent_is_a_structured_error() {
        let (func, mut cfg) = natural_loop_cfg();
        cfg.loops[0].parent = Some(0);

        let error = cfg.verify(&func).unwrap_err();

        assert_eq!(error.rule, "CFG.LOOP_FOREST");
    }

    #[test]
    fn non_bijective_rpo_input_is_a_structured_error() {
        let mut func = MFunction::new(VRegAllocator::new(), Vec::new());
        let mut first = MBlock::new(BlockId(0));
        first.push(MInst::Return);
        let mut duplicate = MBlock::new(BlockId(0));
        duplicate.push(MInst::Return);
        func.blocks = vec![first, duplicate];

        let error = normalize(&mut func).unwrap_err();

        assert_eq!(error.rule, "CFG.RPO_BIJECTION");
    }

    #[test]
    fn branch_edge_block_id_overflow_is_a_structured_error() {
        let mut vregs = VRegAllocator::new();
        let condition = vregs.alloc();
        let mut func = MFunction::new(vregs, vec![SpillDesc::transient()]);
        let mut entry = MBlock::new(BlockId(u32::MAX));
        entry.push(MInst::LoadImm {
            dst: condition,
            value: 1,
        });
        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];
        func.verify_result().unwrap();

        let error = normalize(&mut func).unwrap_err();

        assert_eq!(error.rule, "CFG.BLOCK_ID_RANGE");
        assert_eq!(error.block, None);
    }

    #[test]
    fn entry_predecessors_are_a_structured_error_before_normalization() {
        let mut vregs = VRegAllocator::new();
        let condition = vregs.alloc();
        let mut func = MFunction::new(vregs, vec![SpillDesc::transient()]);
        let mut entry = MBlock::new(BlockId(0));
        entry.push(MInst::LoadImm {
            dst: condition,
            value: 1,
        });
        entry.push(MInst::Branch {
            cond: condition,
            true_bb: BlockId(1),
            false_bb: BlockId(2),
        });
        let mut left = MBlock::new(BlockId(1));
        left.push(MInst::Jump { target: BlockId(0) });
        let mut right = MBlock::new(BlockId(2));
        right.push(MInst::Jump { target: BlockId(0) });
        func.blocks = vec![entry, left, right];

        let original = func
            .blocks
            .iter()
            .map(|block| (block.id, block.successors()))
            .collect::<Vec<_>>();
        let error = normalize(&mut func).unwrap_err();

        assert_eq!(error.rule, "CFG.ENTRY_HAS_NO_PREDECESSORS");
        assert_eq!(error.block, Some(BlockId(0)));
        assert_eq!(
            func.blocks
                .iter()
                .map(|block| (block.id, block.successors()))
                .collect::<Vec<_>>(),
            original,
            "rejection must precede CFG mutation"
        );
    }

    #[test]
    fn splits_critical_edge_and_rewrites_phi_predecessor() {
        let mut vregs = VRegAllocator::new();
        let condition = vregs.alloc();
        let left = vregs.alloc();
        let right = vregs.alloc();
        let merged = vregs.alloc();
        let mut func = MFunction::new(vregs, vec![SpillDesc::transient(); 4]);

        let mut entry = MBlock::new(BlockId(0));
        entry.push(MInst::LoadImm {
            dst: condition,
            value: 1,
        });
        entry.push(MInst::LoadImm {
            dst: left,
            value: 2,
        });
        entry.push(MInst::Branch {
            cond: condition,
            true_bb: BlockId(1),
            false_bb: BlockId(2),
        });
        let mut other = MBlock::new(BlockId(1));
        other.push(MInst::LoadImm {
            dst: right,
            value: 3,
        });
        other.push(MInst::Jump { target: BlockId(3) });
        let mut critical_pred = MBlock::new(BlockId(2));
        critical_pred.push(MInst::Branch {
            cond: condition,
            true_bb: BlockId(3),
            false_bb: BlockId(4),
        });
        let mut join = MBlock::new(BlockId(3));
        join.phis.push(PhiNode {
            dst: merged,
            sources: vec![(BlockId(1), right), (BlockId(2), left)],
        });
        join.push(MInst::Return);
        let mut exit = MBlock::new(BlockId(4));
        exit.push(MInst::Return);
        func.blocks = vec![entry, other, critical_pred, join, exit];

        let cfg = normalize(&mut func).unwrap();
        assert_eq!(func.blocks.len(), 9);
        let entry = cfg.block_index[&BlockId(0)];
        for &successor in &cfg.successors[entry] {
            assert_eq!(
                edge_insertion_point(&func, &cfg, entry, successor),
                Some(EdgeInsertionPoint {
                    block: successor,
                    instruction: 0,
                })
            );
        }
        let join = &func.blocks[cfg.block_index[&BlockId(3)]];
        let split_predecessor = join.phis[0]
            .sources
            .iter()
            .find(|(_, value)| *value == left)
            .unwrap()
            .0;
        assert_ne!(split_predecessor, BlockId(2));
        assert_eq!(cfg.predecessors[cfg.block_index[&BlockId(3)]].len(), 2);
    }
}