Skip to main content

Module dead_code

Module dead_code 

Source
Expand description

Dead code elimination optimization pass.

This module provides optimizations that remove unreachable or redundant code from TLExpr expressions. Examples include:

  • if true then A else BA
  • if false then A else BB
  • AND(false, x)false (short-circuit)
  • OR(true, x)true (short-circuit)
  • EXISTS x. constantconstant (if x is not free in constant)
  • Remove unused subexpressions that don’t affect the result

§Examples

use tensorlogic_compiler::optimize::eliminate_dead_code;
use tensorlogic_ir::{TLExpr, Term};

// if true then A else B → A
let a = TLExpr::pred("a", vec![Term::var("i")]);
let b = TLExpr::pred("b", vec![Term::var("i")]);
let expr = TLExpr::IfThenElse {
    condition: Box::new(TLExpr::Constant(1.0)),
    then_branch: Box::new(a.clone()),
    else_branch: Box::new(b),
};
let (optimized, stats) = eliminate_dead_code(&expr);
assert!(stats.branches_eliminated > 0);

Structs§

DeadCodeStats
Statistics from dead code elimination.

Functions§

eliminate_dead_code
Apply dead code elimination to an expression.