Skip to main content

luaur_code_gen/functions/
update_use_counts.rs

1use crate::enums::ir_op_kind::IrOpKind;
2use crate::records::ir_function::IrFunction;
3use crate::records::ir_inst::IrInst;
4use crate::records::ir_op::IrOp;
5
6pub fn update_use_counts(function: &mut IrFunction) {
7    for block in &mut function.blocks {
8        block.use_count = 0;
9    }
10
11    for inst in &mut function.instructions {
12        inst.use_count = 0;
13    }
14
15    for inst_idx in 0..function.instructions.len() {
16        let ops = function.instructions[inst_idx].ops.clone();
17
18        for op in ops.iter() {
19            let op: IrOp = *op;
20            match op.kind() {
21                IrOpKind::Inst => {
22                    let target: &mut IrInst = &mut function.instructions[op.index() as usize];
23                    debug_assert!(target.use_count < 0xffff);
24                    target.use_count += 1;
25                }
26                IrOpKind::Block => {
27                    let target = &mut function.blocks[op.index() as usize];
28                    debug_assert!(target.use_count < 0xffff);
29                    target.use_count += 1;
30                }
31                _ => {}
32            }
33        }
34    }
35}