Skip to main content

luaur_code_gen/functions/
mark_dead_stores_in_block.rs

1use crate::functions::mark_dead_stores_in_inst::mark_dead_stores_in_inst;
2use crate::macros::codegen_assert::CODEGEN_ASSERT;
3use crate::records::ir_block::IrBlock;
4use crate::records::ir_builder::IrBuilder;
5use crate::records::ir_function::IrFunction;
6use crate::records::ir_inst::IrInst;
7use crate::records::remove_dead_store_state::RemoveDeadStoreState;
8
9// IrData.h: `inline constexpr uint8_t kBlockFlagSafeEnvCheck = 1 << 0;`
10const K_BLOCK_FLAG_SAFE_ENV_CHECK: u8 = 1 << 0;
11
12pub fn mark_dead_stores_in_block(
13    build: &mut IrBuilder,
14    block: &mut IrBlock,
15    state: &mut RemoveDeadStoreState,
16) {
17    let function: *mut IrFunction = &mut build.function;
18
19    // Block might establish a safe environment right at the start and might take a VM exit
20    if (block.flags & K_BLOCK_FLAG_SAFE_ENV_CHECK) != 0 {
21        state.read_all_regs();
22    }
23
24    let start = block.start;
25    let finish = block.finish;
26
27    let mut index = start;
28    while index <= finish {
29        CODEGEN_ASSERT!((index as usize) < unsafe { (*function).instructions.len() });
30
31        let inst_ptr: *mut IrInst = unsafe { &mut (&mut (*function).instructions)[index as usize] };
32
33        mark_dead_stores_in_inst(
34            state,
35            build,
36            unsafe { &mut *function },
37            block,
38            unsafe { &mut *inst_ptr },
39            index,
40        );
41
42        index += 1;
43    }
44}