Files
wasmtime/cranelift/codegen/src/dce.rs
Trevor Elliott 1e6c13d83e cranelift: Rework block instructions to use BlockCall (#5464)
Add a new type BlockCall that represents the pair of a block name with arguments to be passed to it. (The mnemonic here is that it looks a bit like a function call.) Rework the implementation of jump, brz, and brnz to use BlockCall instead of storing the block arguments as varargs in the instruction's ValueList.

To ensure that we're processing block arguments from BlockCall values in instructions, three new functions have been introduced on DataFlowGraph that both sets of arguments:

inst_values - returns an iterator that traverses values in the instruction and block arguments
map_inst_values - applies a function to each value in the instruction and block arguments
overwrite_inst_values - overwrite all values in an instruction and block arguments with values from the iterator

Co-authored-by: Jamey Sharp <jamey@minilop.net>
2023-01-17 16:31:15 -08:00

38 lines
1.2 KiB
Rust

//! A Dead-Code Elimination (DCE) pass.
//!
//! Dead code here means instructions that have no side effects and have no
//! result values used by other instructions.
use crate::cursor::{Cursor, FuncCursor};
use crate::dominator_tree::DominatorTree;
use crate::entity::EntityRef;
use crate::inst_predicates::{any_inst_results_used, has_side_effect};
use crate::ir::Function;
use crate::timing;
/// Perform DCE on `func`.
pub fn do_dce(func: &mut Function, domtree: &DominatorTree) {
let _tt = timing::dce();
debug_assert!(domtree.is_valid());
let mut live = vec![false; func.dfg.num_values()];
for &block in domtree.cfg_postorder() {
let mut pos = FuncCursor::new(func).at_bottom(block);
while let Some(inst) = pos.prev_inst() {
{
if has_side_effect(pos.func, inst)
|| any_inst_results_used(inst, &live, &pos.func.dfg)
{
for arg in pos.func.dfg.inst_values(inst) {
let v = pos.func.dfg.resolve_aliases(arg);
live[v.index()] = true;
}
continue;
}
}
pos.remove_inst();
}
}
}