Sha256: 607dd0f78067ed69da6c8fa1dde915b2fed1f71a39195086531ed6617e819544

Contents?: true

Size: 1.97 KB

Versions: 34

Compression:

Stored size: 1.97 KB

Contents

//! Unreachable code elimination.

use cranelift_entity::EntitySet;

use crate::cursor::{Cursor, FuncCursor};
use crate::dominator_tree::DominatorTree;
use crate::flowgraph::ControlFlowGraph;
use crate::timing;
use crate::{ir, trace};

/// Eliminate unreachable code.
///
/// This pass deletes whole blocks that can't be reached from the entry block. It does not delete
/// individual instructions whose results are unused.
///
/// The reachability analysis is performed by the dominator tree analysis.
pub fn eliminate_unreachable_code(
    func: &mut ir::Function,
    cfg: &mut ControlFlowGraph,
    domtree: &DominatorTree,
) {
    let _tt = timing::unreachable_code();
    let mut pos = FuncCursor::new(func);
    let mut used_tables = EntitySet::with_capacity(pos.func.stencil.dfg.jump_tables.len());
    while let Some(block) = pos.next_block() {
        if domtree.is_reachable(block) {
            let inst = pos.func.layout.last_inst(block).unwrap();
            if let ir::InstructionData::BranchTable { table, .. } = pos.func.dfg.insts[inst] {
                used_tables.insert(table);
            }
            continue;
        }

        trace!("Eliminating unreachable {}", block);
        // Move the cursor out of the way and make sure the next lop iteration goes to the right
        // block.
        pos.prev_block();

        // Remove all instructions from `block`.
        while let Some(inst) = pos.func.layout.first_inst(block) {
            trace!(" - {}", pos.func.dfg.display_inst(inst));
            pos.func.layout.remove_inst(inst);
        }

        // Once the block is completely empty, we can update the CFG which removes it from any
        // predecessor lists.
        cfg.recompute_block(pos.func, block);

        // Finally, remove the block from the layout.
        pos.func.layout.remove_block(block);
    }

    for (table, jt_data) in func.stencil.dfg.jump_tables.iter_mut() {
        if !used_tables.contains(table) {
            jt_data.clear();
        }
    }
}

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
wasmtime-30.0.2 ./ext/cargo-vendor/cranelift-codegen-0.117.2/src/unreachable_code.rs
wasmtime-29.0.0 ./ext/cargo-vendor/cranelift-codegen-0.116.0/src/unreachable_code.rs
wasmtime-28.0.0 ./ext/cargo-vendor/cranelift-codegen-0.115.0/src/unreachable_code.rs
wasmtime-27.0.0 ./ext/cargo-vendor/cranelift-codegen-0.114.0/src/unreachable_code.rs
wasmtime-26.0.0 ./ext/cargo-vendor/cranelift-codegen-0.113.0/src/unreachable_code.rs
wasmtime-25.0.2 ./ext/cargo-vendor/cranelift-codegen-0.112.2/src/unreachable_code.rs
wasmtime-25.0.1 ./ext/cargo-vendor/cranelift-codegen-0.112.1/src/unreachable_code.rs
wasmtime-25.0.0 ./ext/cargo-vendor/cranelift-codegen-0.112.0/src/unreachable_code.rs
wasmtime-24.0.0 ./ext/cargo-vendor/cranelift-codegen-0.111.0/src/unreachable_code.rs
wasmtime-23.0.2 ./ext/cargo-vendor/cranelift-codegen-0.110.2/src/unreachable_code.rs
wasmtime-22.0.0 ./ext/cargo-vendor/cranelift-codegen-0.109.0/src/unreachable_code.rs
wasmtime-21.0.1 ./ext/cargo-vendor/cranelift-codegen-0.108.1/src/unreachable_code.rs
wasmtime-20.0.2 ./ext/cargo-vendor/cranelift-codegen-0.107.2/src/unreachable_code.rs
wasmtime-20.0.0 ./ext/cargo-vendor/cranelift-codegen-0.107.2/src/unreachable_code.rs
wasmtime-18.0.3 ./ext/cargo-vendor/cranelift-codegen-0.105.3/src/unreachable_code.rs
wasmtime-17.0.1 ./ext/cargo-vendor/cranelift-codegen-0.104.1/src/unreachable_code.rs
wasmtime-17.0.0 ./ext/cargo-vendor/cranelift-codegen-0.104.0/src/unreachable_code.rs
wasmtime-16.0.0 ./ext/cargo-vendor/cranelift-codegen-0.103.0/src/unreachable_code.rs
wasmtime-15.0.1 ./ext/cargo-vendor/cranelift-codegen-0.102.1/src/unreachable_code.rs
wasmtime-15.0.0 ./ext/cargo-vendor/cranelift-codegen-0.102.1/src/unreachable_code.rs