Coverage Report

Created: 2026-11-23 07:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/regalloc2/src/ssa.rs
Line
Count
Source
1
/*
2
 * Released under the terms of the Apache 2.0 license with LLVM
3
 * exception. See `LICENSE` for details.
4
 */
5
6
//! SSA-related utilities.
7
8
use alloc::vec;
9
10
use crate::cfg::CFGInfo;
11
use crate::{domtree, Block, Function, FxHashSet, Inst, OperandKind, RegAllocError, VReg};
12
13
742
pub fn validate_ssa<F: Function>(f: &F, cfginfo: &CFGInfo) -> Result<(), RegAllocError> {
14
742
    let mut idom = vec![];
15
742
    domtree::calculate(
16
742
        f.num_blocks(),
17
105k
        |block| f.block_preds(block),
18
742
        &cfginfo.postorder,
19
742
        &mut vec![],
20
742
        &mut idom,
21
742
        f.entry_block(),
22
    );
23
24
    // For every block param and inst def, check that this is the only def.
25
742
    let mut defined_in = vec![Block::invalid(); f.num_vregs()];
26
22.1k
    for block in 0..f.num_blocks() {
27
22.1k
        let block = Block::new(block);
28
665k
        let mut def = |vreg: VReg, inst| {
29
665k
            if vreg.vreg() >= defined_in.len() {
30
0
                trace!("VRegs not numbered consecutively {:?}", vreg);
31
0
                return Err(RegAllocError::SSA(vreg, inst));
32
665k
            }
33
665k
            if defined_in[vreg.vreg()].is_valid() {
34
0
                trace!("Multiple def constraints for {:?}", vreg);
35
0
                Err(RegAllocError::SSA(vreg, inst))
36
            } else {
37
665k
                defined_in[vreg.vreg()] = block;
38
665k
                Ok(())
39
            }
40
665k
        };
41
22.1k
        for &param in f.block_params(block) {
42
6.40k
            def(param, Inst::invalid())?;
43
        }
44
242k
        for inst in f.block_insns(block).iter() {
45
1.37M
            for operand in f.inst_operands(inst) {
46
1.37M
                if let OperandKind::Def = operand.kind() {
47
659k
                    def(operand.vreg(), inst)?;
48
716k
                }
49
            }
50
        }
51
    }
52
53
    // Walk the blocks in arbitrary order. Check, for every use, that
54
    // the def is either in the same block in an earlier inst, or is
55
    // defined (by inst or blockparam) in some other block that
56
    // dominates this one.
57
742
    let mut local = FxHashSet::default();
58
22.1k
    for block in 0..f.num_blocks() {
59
22.1k
        let block = Block::new(block);
60
22.1k
        local.clear();
61
22.1k
        local.extend(f.block_params(block));
62
63
242k
        for iix in f.block_insns(block).iter() {
64
242k
            let operands = f.inst_operands(iix);
65
1.37M
            for operand in operands {
66
                // Fixed registers uses will likely not be SSA, but they also
67
                // won't receive assignments.
68
1.37M
                if operand.as_fixed_nonallocatable().is_some() {
69
7.08k
                    continue;
70
1.36M
                }
71
72
1.36M
                match operand.kind() {
73
                    OperandKind::Use => {
74
709k
                        let def_block = defined_in[operand.vreg().vreg()];
75
709k
                        let okay = def_block.is_valid()
76
709k
                            && if def_block == block {
77
458k
                                local.contains(&operand.vreg())
78
                            } else {
79
250k
                                domtree::dominates(&idom, def_block, block)
80
                            };
81
709k
                        if !okay {
82
0
                            trace!("Invalid use {:?}", operand.vreg());
83
0
                            return Err(RegAllocError::SSA(operand.vreg(), iix));
84
709k
                        }
85
                    }
86
659k
                    OperandKind::Def => {
87
659k
                        // Check all the uses in this instruction
88
659k
                        // first, before recording its defs below.
89
659k
                    }
90
                }
91
            }
92
93
            // In SSA form, an instruction can't use a VReg that it
94
            // also defines. So only record this instruction's defs
95
            // after its uses have been checked.
96
1.37M
            for operand in operands {
97
1.37M
                if let OperandKind::Def = operand.kind() {
98
659k
                    local.insert(operand.vreg());
99
716k
                }
100
            }
101
        }
102
    }
103
104
    // Check that the length of branch args matches the sum of the
105
    // number of blockparams in their succs, and that the end of every
106
    // block ends in this branch or in a ret, and that there are no
107
    // other branches or rets in the middle of the block.
108
22.1k
    for block in 0..f.num_blocks() {
109
22.1k
        let block = Block::new(block);
110
22.1k
        let insns = f.block_insns(block);
111
242k
        for insn in insns.iter() {
112
242k
            if insn == insns.last() {
113
22.1k
                if !(f.is_branch(insn) || f.is_ret(insn)) {
114
0
                    trace!("block {:?} is not terminated by a branch or ret!", block);
115
0
                    return Err(RegAllocError::BB(block));
116
22.1k
                }
117
22.1k
                if f.is_branch(insn) {
118
27.1k
                    for (i, &succ) in f.block_succs(block).iter().enumerate() {
119
27.1k
                        let blockparams_in = f.block_params(succ);
120
27.1k
                        let blockparams_out = f.branch_blockparams(block, insn, i);
121
27.1k
                        if blockparams_in.len() != blockparams_out.len() {
122
0
                            trace!(
123
                                "Mismatch on block params, found {} expected {}",
124
0
                                blockparams_out.len(),
125
0
                                blockparams_in.len()
126
                            );
127
0
                            return Err(RegAllocError::Branch(insn));
128
27.1k
                        }
129
                    }
130
742
                }
131
            } else {
132
220k
                if f.is_branch(insn) || f.is_ret(insn) {
133
0
                    trace!("Block terminator found in the middle of a block");
134
0
                    return Err(RegAllocError::BB(block));
135
220k
                }
136
            }
137
        }
138
    }
139
140
    // Check that the entry block has no block args: otherwise it is
141
    // undefined what their value would be.
142
742
    if f.block_params(f.entry_block()).len() > 0 {
143
0
        trace!("Entry block contains block args");
144
0
        return Err(RegAllocError::BB(f.entry_block()));
145
742
    }
146
147
742
    Ok(())
148
742
}