Skip to main content

Module optimize

Module optimize 

Source
Expand description

DECY-196: HIR optimization passes.

Three optimization passes run between ownership inference and codegen:

  1. Constant folding: Evaluate literal arithmetic at compile time
  2. Dead branch removal: Eliminate if(1) / if(0) branches
  3. Temporary elimination: Remove single-use let bindings

Passes run in a fixed-point loop (max 3 iterations).

§Examples

use decy_hir::{HirFunction, HirType, HirStatement, HirExpression, BinaryOperator};

// Create function with `return 2 + 3;`
let func = HirFunction::new_with_body(
    "test".to_string(),
    HirType::Int,
    vec![],
    vec![HirStatement::Return(Some(HirExpression::BinaryOp {
        op: BinaryOperator::Add,
        left: Box::new(HirExpression::IntLiteral(2)),
        right: Box::new(HirExpression::IntLiteral(3)),
    }))],
);

let optimized = decy_core::optimize::optimize_function(&func);
// After constant folding: `return 5;`
assert_eq!(optimized.body().len(), 1);

Functions§

optimize_function
Run all optimization passes on a function.