use crate::zigir::ops::{BinOp, LogicalOp, UnaOp};
use crate::zigir::passes::{IrPass, PassResult};
use crate::zigir::types::{IrAssignTarget, IrBlock, IrExpr, IrModule, IrStmt};
use std::cell::RefCell;
use super::walk;
pub struct ConstantFoldPass;
impl ConstantFoldPass {
pub fn new() -> Self {
Self
}
fn try_fold(expr: &mut IrExpr) -> bool {
match expr {
IrExpr::Binary {
op, left, right, ..
} => {
let mut changed = false;
if Self::try_fold(left) {
changed = true;
}
if Self::try_fold(right) {
changed = true;
}
if let Some(result) = fold_binary(*op, left, right) {
*expr = result;
true
} else {
changed
}
}
IrExpr::Unary { op, operand, .. } => {
let changed = Self::try_fold(operand);
if let Some(result) = fold_unary(*op, operand) {
*expr = result;
true
} else {
changed
}
}
IrExpr::Logical {
op, left, right, ..
} => {
let mut changed = false;
if Self::try_fold(left) {
changed = true;
}
if Self::try_fold(right) {
changed = true;
}
if let Some(result) = fold_logical(*op, left, right) {
*expr = result;
true
} else {
changed
}
}
IrExpr::Conditional { cond, then, else_ } => {
let mut changed = false;
if Self::try_fold(cond) {
changed = true;
}
let cond_bool = if let IrExpr::BoolLiteral(b) = cond.as_ref() {
Some(*b)
} else {
None
};
if Self::try_fold(then) {
changed = true;
}
if Self::try_fold(else_) {
changed = true;
}
if let Some(b) = cond_bool {
let replacement = if b {
(**then).clone()
} else {
(**else_).clone()
};
*expr = replacement;
return true;
}
changed
}
IrExpr::Paren(inner) => {
let mut changed = Self::try_fold(inner);
if matches!(
inner.as_ref(),
IrExpr::IntLiteral(_)
| IrExpr::FloatLiteral(_)
| IrExpr::BoolLiteral(_)
| IrExpr::BigIntLiteral(_)
) {
*expr = (**inner).clone();
changed = true;
}
changed
}
IrExpr::Sequence(exprs) => Self::try_fold_iter(exprs),
IrExpr::AllocPrint { fmt, args } => {
let changed = Self::try_fold_iter(args);
if args.is_empty() {
let s = fmt.clone();
*expr = IrExpr::StringLiteral(s);
return true;
}
changed
}
IrExpr::Typeof(inner) => {
let typeof_result = typeof_literal(inner);
let changed = Self::try_fold(inner);
if let Some(s) = typeof_result {
*expr = IrExpr::StringLiteral(s);
return true;
}
changed
}
IrExpr::ArrayLiteral(arr) => Self::try_fold_iter(&mut arr.elements),
IrExpr::ObjectLiteral(obj) => Self::try_fold_object_items(&mut obj.items),
IrExpr::TemplateLiteral { exprs, .. } => Self::try_fold_iter(exprs),
IrExpr::Spread(e) | IrExpr::Void(e) => Self::try_fold(e),
IrExpr::PowExpr { base, exp, .. } => {
let mut changed = Self::try_fold(base);
if Self::try_fold(exp) {
changed = true;
}
changed
}
IrExpr::RemExpr { left, right, .. } | IrExpr::DivExpr { left, right, .. } => {
let mut changed = Self::try_fold(left);
if Self::try_fold(right) {
changed = true;
}
changed
}
_ => false,
}
}
fn try_fold_iter(exprs: &mut [IrExpr]) -> bool {
let mut changed = false;
for e in exprs {
if Self::try_fold(e) {
changed = true;
}
}
changed
}
fn try_fold_object_items(items: &mut [crate::zigir::types::IrObjectItem]) -> bool {
use crate::zigir::types::IrObjectItem;
let mut changed = false;
for item in items {
let expr = match item {
IrObjectItem::Field(f) => &mut f.value,
IrObjectItem::Spread(e) => e,
};
if Self::try_fold(expr) {
changed = true;
}
}
changed
}
fn fold_stmt(stmt: &mut IrStmt) -> bool {
let changed = RefCell::new(false);
walk::for_each_stmt_child_mut(
stmt,
&mut |block| {
*changed.borrow_mut() |= Self::fold_block(block);
},
&mut |s| {
*changed.borrow_mut() |= Self::fold_stmt(s);
},
&mut |e| {
*changed.borrow_mut() |= Self::try_fold(e);
},
&mut |t| {
*changed.borrow_mut() |= Self::fold_target(t);
},
);
changed.into_inner()
}
fn fold_target(target: &mut IrAssignTarget) -> bool {
let mut changed = false;
walk::for_each_target_child_mut(target, &mut |e| {
changed |= Self::try_fold(e);
});
changed
}
fn fold_block(block: &mut IrBlock) -> bool {
let mut changed = false;
for stmt in &mut block.stmts {
if Self::fold_stmt(stmt) {
changed = true;
}
}
changed
}
}
impl IrPass for ConstantFoldPass {
fn name(&self) -> &'static str {
"constant-fold"
}
fn description(&self) -> &'static str {
"Folds constant arithmetic, string, and boolean expressions"
}
fn run(&mut self, module: &mut IrModule) -> PassResult {
let mut changed = false;
for decl in &mut module.declarations {
let ch = RefCell::new(false);
walk::for_each_decl_child_mut(
decl,
&mut |block| {
*ch.borrow_mut() |= Self::fold_block(block);
},
&mut |e| {
*ch.borrow_mut() |= Self::try_fold(e);
},
);
if ch.into_inner() {
changed = true;
}
}
for cs in &mut module.closure_structs {
if Self::fold_block(&mut cs.body) {
changed = true;
}
}
if changed {
PassResult::changed()
} else {
PassResult::unchanged()
}
}
}
impl Default for ConstantFoldPass {
fn default() -> Self {
Self::new()
}
}
fn fold_binary(op: BinOp, left: &IrExpr, right: &IrExpr) -> Option<IrExpr> {
match (left, right) {
(IrExpr::IntLiteral(a), IrExpr::IntLiteral(b)) => {
let result = match op {
BinOp::Add => a.checked_add(*b)?,
BinOp::Sub => a.checked_sub(*b)?,
BinOp::Mul => a.checked_mul(*b)?,
BinOp::Div => {
return None;
}
BinOp::Mod => {
if *b == 0 {
return None;
}
return Some(IrExpr::FloatLiteral(*a as f64 % *b as f64));
}
BinOp::BitAnd => (*a as i32 & *b as i32) as i64,
BinOp::BitOr => (*a as i32 | *b as i32) as i64,
BinOp::BitXor => (*a as i32 ^ *b as i32) as i64,
BinOp::Shl => (*a as i32).wrapping_shl(*b as u32) as i64,
BinOp::Shr => (*a as i32).wrapping_shr(*b as u32) as i64,
BinOp::Eq | BinOp::StrictEq => return Some(IrExpr::BoolLiteral(a == b)),
BinOp::Ne | BinOp::StrictNe => return Some(IrExpr::BoolLiteral(a != b)),
BinOp::Lt => return Some(IrExpr::BoolLiteral(a < b)),
BinOp::Le => return Some(IrExpr::BoolLiteral(a <= b)),
BinOp::Gt => return Some(IrExpr::BoolLiteral(a > b)),
BinOp::Ge => return Some(IrExpr::BoolLiteral(a >= b)),
_ => return None,
};
Some(IrExpr::IntLiteral(result))
}
(IrExpr::FloatLiteral(a), IrExpr::FloatLiteral(b)) => {
let result = match op {
BinOp::Add => a + b,
BinOp::Sub => a - b,
BinOp::Mul => a * b,
BinOp::Div => {
if *b == 0.0 {
return None;
}
a / b
}
BinOp::Mod => a % b,
BinOp::Eq | BinOp::StrictEq => return Some(IrExpr::BoolLiteral(a == b)),
BinOp::Ne | BinOp::StrictNe => return Some(IrExpr::BoolLiteral(a != b)),
BinOp::Lt => return Some(IrExpr::BoolLiteral(a < b)),
BinOp::Le => return Some(IrExpr::BoolLiteral(a <= b)),
BinOp::Gt => return Some(IrExpr::BoolLiteral(a > b)),
BinOp::Ge => return Some(IrExpr::BoolLiteral(a >= b)),
_ => return None,
};
Some(IrExpr::FloatLiteral(result))
}
(IrExpr::IntLiteral(a), IrExpr::FloatLiteral(b)) => {
let a_f = *a as f64;
let result = match op {
BinOp::Add => a_f + b,
BinOp::Sub => a_f - b,
BinOp::Mul => a_f * b,
BinOp::Div => a_f / b,
_ => return None,
};
Some(IrExpr::FloatLiteral(result))
}
(IrExpr::FloatLiteral(a), IrExpr::IntLiteral(b)) => {
let b_f = *b as f64;
let result = match op {
BinOp::Add => a + b_f,
BinOp::Sub => a - b_f,
BinOp::Mul => a * b_f,
BinOp::Div => a / b_f,
_ => return None,
};
Some(IrExpr::FloatLiteral(result))
}
(IrExpr::StringLiteral(a), IrExpr::StringLiteral(b)) => {
if op == BinOp::Add {
Some(IrExpr::StringLiteral(format!("{}{}", a, b)))
} else {
None
}
}
(IrExpr::BoolLiteral(a), IrExpr::BoolLiteral(b)) => {
let result = match op {
BinOp::Eq | BinOp::StrictEq => a == b,
BinOp::Ne | BinOp::StrictNe => a != b,
_ => return None,
};
Some(IrExpr::BoolLiteral(result))
}
(IrExpr::Null, IrExpr::Null) => {
if op == BinOp::Eq || op == BinOp::StrictEq {
Some(IrExpr::BoolLiteral(true))
} else if op == BinOp::Ne || op == BinOp::StrictNe {
Some(IrExpr::BoolLiteral(false))
} else {
None
}
}
_ => None,
}
}
fn fold_unary(op: UnaOp, operand: &IrExpr) -> Option<IrExpr> {
match (op, operand) {
(UnaOp::Neg, IrExpr::IntLiteral(n)) => {
n.checked_neg().map(IrExpr::IntLiteral)
}
(UnaOp::Neg, IrExpr::FloatLiteral(n)) => Some(IrExpr::FloatLiteral(-n)),
(UnaOp::Not, IrExpr::BoolLiteral(b)) => Some(IrExpr::BoolLiteral(!b)),
(UnaOp::BitNot, IrExpr::IntLiteral(n)) => Some(IrExpr::IntLiteral(!(*n as i32) as i64)),
(
UnaOp::Not,
IrExpr::Unary {
op: UnaOp::Not,
operand: inner,
..
},
) => {
if matches!(inner.as_ref(), IrExpr::BoolLiteral(_)) {
Some((**inner).clone())
} else {
None
}
}
_ => None,
}
}
fn fold_logical(op: LogicalOp, left: &IrExpr, right: &IrExpr) -> Option<IrExpr> {
match (op, left, right) {
(LogicalOp::And, IrExpr::BoolLiteral(false), _) => Some(IrExpr::BoolLiteral(false)),
(LogicalOp::And, IrExpr::BoolLiteral(true), _) => {
Some(right.clone())
}
(LogicalOp::Or, IrExpr::BoolLiteral(true), _) => Some(IrExpr::BoolLiteral(true)),
(LogicalOp::Or, IrExpr::BoolLiteral(false), _) => {
Some(right.clone())
}
(LogicalOp::Nullish, IrExpr::Null, right) => Some(right.clone()),
(LogicalOp::Nullish, IrExpr::Undefined, right) => Some(right.clone()),
_ => None,
}
}
fn typeof_literal(expr: &IrExpr) -> Option<String> {
match expr {
IrExpr::IntLiteral(_) | IrExpr::FloatLiteral(_) => Some("number".to_string()),
IrExpr::StringLiteral(_) => Some("string".to_string()),
IrExpr::BoolLiteral(_) => Some("boolean".to_string()),
IrExpr::Null => Some("object".to_string()), IrExpr::Undefined => Some("undefined".to_string()),
IrExpr::ArrowFn(_) | IrExpr::Closure(_) | IrExpr::FnExpr(_) => Some("function".to_string()),
_ => None,
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::types::ZigType;
use crate::zigir::ident::IrIdent;
use crate::zigir::types::{IrBlock, IrDecl, IrFnDecl, IrStmt};
fn first_return_value(module: &IrModule) -> &IrExpr {
let IrDecl::Fn(f) = &module.declarations[0] else {
panic!("expected FnDecl at declarations[0]");
};
let IrStmt::Return { value: Some(expr) } = &f.body.stmts[0] else {
panic!("expected Return with value at stmts[0]");
};
expr
}
#[test]
fn test_fold_int_add() {
let mut module = make_module_with_body(vec![IrStmt::Return {
value: Some(IrExpr::Binary {
op: BinOp::Add,
left: Box::new(IrExpr::IntLiteral(1)),
right: Box::new(IrExpr::IntLiteral(2)),
left_type: None,
right_type: None,
}),
}]);
let mut pass = ConstantFoldPass::new();
let result = pass.run(&mut module);
assert!(result.changed);
match first_return_value(&module) {
IrExpr::IntLiteral(n) => assert_eq!(*n, 3),
other => panic!("expected IntLiteral(3), got {:?}", other),
}
}
#[test]
fn test_fold_float_mul() {
let mut module = make_module_with_body(vec![IrStmt::Return {
value: Some(IrExpr::Binary {
op: BinOp::Mul,
left: Box::new(IrExpr::FloatLiteral(1.5)),
right: Box::new(IrExpr::FloatLiteral(2.0)),
left_type: None,
right_type: None,
}),
}]);
let mut pass = ConstantFoldPass::new();
let result = pass.run(&mut module);
assert!(result.changed);
match first_return_value(&module) {
IrExpr::FloatLiteral(n) => assert_eq!(*n, 3.0),
other => panic!("expected FloatLiteral(3.0), got {:?}", other),
}
}
#[test]
fn test_fold_string_concat() {
let mut module = make_module_with_body(vec![IrStmt::Return {
value: Some(IrExpr::Binary {
op: BinOp::Add,
left: Box::new(IrExpr::StringLiteral("Hello, ".to_string())),
right: Box::new(IrExpr::StringLiteral("world!".to_string())),
left_type: None,
right_type: None,
}),
}]);
let mut pass = ConstantFoldPass::new();
let result = pass.run(&mut module);
assert!(result.changed);
match first_return_value(&module) {
IrExpr::StringLiteral(s) => assert_eq!(s, "Hello, world!"),
other => panic!("expected StringLiteral, got {:?}", other),
}
}
#[test]
fn test_fold_unary_negate() {
let mut module = make_module_with_body(vec![IrStmt::Return {
value: Some(IrExpr::Unary {
op: UnaOp::Neg,
operand: Box::new(IrExpr::IntLiteral(42)),
operand_type: Some(ZigType::I64),
}),
}]);
let mut pass = ConstantFoldPass::new();
let result = pass.run(&mut module);
assert!(result.changed);
match first_return_value(&module) {
IrExpr::IntLiteral(n) => assert_eq!(*n, -42),
other => panic!("expected IntLiteral(-42), got {:?}", other),
}
}
#[test]
fn test_fold_not_bool() {
let mut module = make_module_with_body(vec![IrStmt::Return {
value: Some(IrExpr::Unary {
op: UnaOp::Not,
operand: Box::new(IrExpr::BoolLiteral(true)),
operand_type: Some(ZigType::Bool),
}),
}]);
let mut pass = ConstantFoldPass::new();
let result = pass.run(&mut module);
assert!(result.changed);
match first_return_value(&module) {
IrExpr::BoolLiteral(b) => assert!(!b),
other => panic!("expected BoolLiteral(false), got {:?}", other),
}
}
#[test]
fn test_fold_conditional_true() {
let mut module = make_module_with_body(vec![IrStmt::Return {
value: Some(IrExpr::Conditional {
cond: Box::new(IrExpr::BoolLiteral(true)),
then: Box::new(IrExpr::IntLiteral(1)),
else_: Box::new(IrExpr::IntLiteral(2)),
}),
}]);
let mut pass = ConstantFoldPass::new();
let result = pass.run(&mut module);
assert!(result.changed);
match first_return_value(&module) {
IrExpr::IntLiteral(n) => assert_eq!(*n, 1),
other => panic!("expected IntLiteral(1), got {:?}", other),
}
}
#[test]
fn test_fold_and_false() {
let mut module = make_module_with_body(vec![IrStmt::Return {
value: Some(IrExpr::Logical {
op: LogicalOp::And,
left: Box::new(IrExpr::BoolLiteral(false)),
right: Box::new(IrExpr::Ident(IrIdent::new("x"))),
left_type: Some(crate::types::ZigType::Bool),
right_type: None,
}),
}]);
let mut pass = ConstantFoldPass::new();
let result = pass.run(&mut module);
assert!(result.changed);
match first_return_value(&module) {
IrExpr::BoolLiteral(b) => assert!(!b),
other => panic!("expected BoolLiteral(false), got {:?}", other),
}
}
#[test]
fn test_fold_typeof_int() {
let mut module = make_module_with_body(vec![IrStmt::Return {
value: Some(IrExpr::Typeof(Box::new(IrExpr::IntLiteral(42)))),
}]);
let mut pass = ConstantFoldPass::new();
let result = pass.run(&mut module);
assert!(result.changed);
match first_return_value(&module) {
IrExpr::StringLiteral(s) => assert_eq!(s, "number"),
other => panic!("expected StringLiteral(\"number\"), got {:?}", other),
}
}
#[test]
fn test_no_fold_dynamic() {
let mut module = make_module_with_body(vec![IrStmt::Return {
value: Some(IrExpr::Binary {
op: BinOp::Add,
left: Box::new(IrExpr::Ident(IrIdent::new("x"))),
right: Box::new(IrExpr::IntLiteral(1)),
left_type: None,
right_type: None,
}),
}]);
let mut pass = ConstantFoldPass::new();
let result = pass.run(&mut module);
assert!(!result.changed); }
#[test]
fn test_fold_nested() {
let mut module = make_module_with_body(vec![IrStmt::Return {
value: Some(IrExpr::Binary {
op: BinOp::Add,
left: Box::new(IrExpr::Binary {
op: BinOp::Add,
left: Box::new(IrExpr::IntLiteral(1)),
right: Box::new(IrExpr::IntLiteral(2)),
left_type: None,
right_type: None,
}),
right: Box::new(IrExpr::Binary {
op: BinOp::Add,
left: Box::new(IrExpr::IntLiteral(3)),
right: Box::new(IrExpr::IntLiteral(4)),
left_type: None,
right_type: None,
}),
left_type: None,
right_type: None,
}),
}]);
let mut pass = ConstantFoldPass::new();
let result = pass.run(&mut module);
assert!(result.changed);
match first_return_value(&module) {
IrExpr::IntLiteral(n) => assert_eq!(*n, 10),
other => panic!("expected IntLiteral(10), got {:?}", other),
}
}
#[test]
fn test_fold_allocprint_no_args() {
let mut module = make_module_with_body(vec![IrStmt::Return {
value: Some(IrExpr::AllocPrint {
fmt: "hello".to_string(),
args: vec![],
}),
}]);
let mut pass = ConstantFoldPass::new();
let result = pass.run(&mut module);
assert!(result.changed);
match first_return_value(&module) {
IrExpr::StringLiteral(s) => assert_eq!(s, "hello"),
other => panic!("expected StringLiteral(\"hello\"), got {:?}", other),
}
}
#[test]
fn test_fold_null_equality() {
let mut module = make_module_with_body(vec![IrStmt::Return {
value: Some(IrExpr::Binary {
op: BinOp::Eq,
left: Box::new(IrExpr::Null),
right: Box::new(IrExpr::Null),
left_type: None,
right_type: None,
}),
}]);
let mut pass = ConstantFoldPass::new();
let result = pass.run(&mut module);
assert!(result.changed);
match first_return_value(&module) {
IrExpr::BoolLiteral(b) => assert!(b),
other => panic!("expected BoolLiteral(true), got {:?}", other),
}
}
fn assert_fold_binary_int(op: BinOp, a: i64, b: i64, expected: i64) {
let mut module = make_module_with_body(vec![IrStmt::Return {
value: Some(IrExpr::Binary {
op,
left: Box::new(IrExpr::IntLiteral(a)),
right: Box::new(IrExpr::IntLiteral(b)),
left_type: None,
right_type: None,
}),
}]);
let mut pass = ConstantFoldPass::new();
let result = pass.run(&mut module);
assert!(result.changed, "expected fold to fire");
match first_return_value(&module) {
IrExpr::IntLiteral(n) => assert_eq!(
*n, expected,
"binary fold of ({a}, {b}) gave {n}, expected {expected}"
),
other => panic!("expected IntLiteral({expected}), got {:?}", other),
}
}
fn assert_fold_unary_int(op: UnaOp, n: i64, expected: i64) {
let mut module = make_module_with_body(vec![IrStmt::Return {
value: Some(IrExpr::Unary {
op,
operand: Box::new(IrExpr::IntLiteral(n)),
operand_type: Some(ZigType::I64),
}),
}]);
let mut pass = ConstantFoldPass::new();
let result = pass.run(&mut module);
assert!(result.changed, "expected fold to fire");
match first_return_value(&module) {
IrExpr::IntLiteral(got) => assert_eq!(
*got, expected,
"unary fold of ({n}) gave {got}, expected {expected}"
),
other => panic!("expected IntLiteral({expected}), got {:?}", other),
}
}
#[test]
fn test_fold_bitand_int32() {
assert_fold_binary_int(BinOp::BitAnd, 0xFFFF_FFFF, 0xFFFF_FFFF, -1);
}
#[test]
fn test_fold_bitand_truncates_above_i32() {
assert_fold_binary_int(BinOp::BitAnd, 0x1_0000_0000, 0xFFFF_FFFF, 0);
}
#[test]
fn test_fold_bitor_int32() {
assert_fold_binary_int(BinOp::BitOr, 0x8000_0000, 0, -2147483648);
}
#[test]
fn test_fold_bitxor_int32() {
assert_fold_binary_int(BinOp::BitXor, 0xFFFF_FFFF, 0xFFFF_FFFF, 0);
}
#[test]
fn test_fold_shl_int32() {
assert_fold_binary_int(BinOp::Shl, 1, 31, -2147483648);
}
#[test]
fn test_fold_shl_masks_count() {
assert_fold_binary_int(BinOp::Shl, 1, 32, 1);
}
#[test]
fn test_fold_shr_int32() {
assert_fold_binary_int(BinOp::Shr, -1, 1, -1);
}
#[test]
fn test_fold_bitnot_int32() {
assert_fold_unary_int(UnaOp::BitNot, 0, -1);
assert_fold_unary_int(UnaOp::BitNot, 0xFFFF_FFFF, 0);
}
fn make_module_with_body(body: Vec<IrStmt>) -> IrModule {
let mut module = IrModule::new("test".to_string());
module.declarations.push(IrDecl::Fn(IrFnDecl {
name: IrIdent::new("test_fn"),
params: vec![],
return_type: ZigType::I64,
body: IrBlock::new(body),
is_export: false,
is_async: false,
can_throw: false,
is_cabi: false,
typeof_return_body: None,
}));
module
}
}