Expand description

The Dead Code Elimination pass traverses the AST and eliminates unused code, specifically assignment statements, within the boundary of transitions and functions. The pass is run after the Function Inlining pass.

See https://en.wikipedia.org/wiki/Dead-code_elimination for more information.

Consider the following flattened Leo code.

function main(flag: u8, value: u8) -> u8 {
    $var$0 = flag == 0u8;
    $var$4$5 = value * value;
    $var$1 = $var$4$5;
    value$2 = $var$1;
    value$3 = $var$0 ? value$2 : value;
    value$6 = $var$1 * $var$1;
    return value$3;
}

The dead code elimination pass produces the following code.

function main(flag: u8, value: u8) -> u8 {
    $var$0 = flag == 0u8;
    $var$4$5 = value * value;
    $var$1 = $var$4$5;
    value$2 = $var$1;
    value$3 = $var$0 ? value$2 : value;
    return value$3;
}

Note this pass relies on the following invariants:

  • No shadowing for all variables, struct names, function names, etc.
  • Unique variable names (provided by SSA)
  • Flattened code (provided by the flattening pass)

Re-exports§

Modules§