From e8fc5aa7887e34fa7984ade8540cbde46575ed6c Mon Sep 17 00:00:00 2001 From: Sam Zhou Date: Sat, 21 Oct 2023 10:00:52 -0700 Subject: [PATCH] [optimization] Optimize trivial if else returning bool literals --- .../conditional_constant_propagation.rs | 14 ++++++++ .../conditional_constant_propagation_tests.rs | 34 +++++++++++++++---- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/crates/samlang-core/src/optimization/conditional_constant_propagation.rs b/crates/samlang-core/src/optimization/conditional_constant_propagation.rs index 1a3dd40d..b4f12931 100644 --- a/crates/samlang-core/src/optimization/conditional_constant_propagation.rs +++ b/crates/samlang-core/src/optimization/conditional_constant_propagation.rs @@ -276,6 +276,20 @@ fn optimize_stmt( } return false; } + if s1.is_empty() && s2.is_empty() && final_assignments.len() == 1 { + let (n, _, e1, e2) = &final_assignments[0]; + match (e1, e2) { + (Expression::IntLiteral(1), Expression::IntLiteral(0)) => { + value_cx.checked_bind(*n, condition); + return false; + } + (Expression::IntLiteral(0), Expression::IntLiteral(1)) => { + collector.push(Statement::binary(*n, Operator::XOR, condition, ONE)); + return false; + } + _ => {} + } + } push_scope(value_cx, index_access_cx, binary_expr_cx); let mut s1_collector = vec![]; optimize_stmts(s1, heap, value_cx, index_access_cx, binary_expr_cx, &mut s1_collector); diff --git a/crates/samlang-core/src/optimization/conditional_constant_propagation_tests.rs b/crates/samlang-core/src/optimization/conditional_constant_propagation_tests.rs index b22db57c..3595b9b5 100644 --- a/crates/samlang-core/src/optimization/conditional_constant_propagation_tests.rs +++ b/crates/samlang-core/src/optimization/conditional_constant_propagation_tests.rs @@ -769,30 +769,50 @@ return 0;"#, Expression::var_name(heap.alloc_str_for_test("a22"), INT_TYPE), )], }, + Statement::IfElse { + condition: Expression::var_name(heap.alloc_str_for_test("ma2"), INT_TYPE), + s1: vec![], + s2: vec![], + final_assignments: vec![(heap.alloc_str_for_test("ma3"), INT_TYPE, ONE, ZERO)], + }, + Statement::IfElse { + condition: Expression::var_name(heap.alloc_str_for_test("ma2"), INT_TYPE), + s1: vec![], + s2: vec![], + final_assignments: vec![(heap.alloc_str_for_test("ma4"), INT_TYPE, ZERO, ONE)], + }, Statement::binary( heap.alloc_str_for_test("r1"), Operator::EQ, Expression::var_name(heap.alloc_str_for_test("ma1"), INT_TYPE), Expression::var_name(heap.alloc_str_for_test("ma2"), INT_TYPE), ), - Statement::binary(heap.alloc_str_for_test("r2"), Operator::NE, ONE, ZERO), - Statement::binary(heap.alloc_str_for_test("r3"), Operator::XOR, ONE, ZERO), - Statement::binary(heap.alloc_str_for_test("r4"), Operator::NE, ONE, ZERO), Statement::binary( - heap.alloc_str_for_test("r5"), + heap.alloc_str_for_test("r2"), + Operator::EQ, + Expression::var_name(heap.alloc_str_for_test("ma3"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("ma4"), INT_TYPE), + ), + Statement::binary(heap.alloc_str_for_test("r3"), Operator::NE, ONE, ZERO), + Statement::binary(heap.alloc_str_for_test("r4"), Operator::XOR, ONE, ZERO), + Statement::binary(heap.alloc_str_for_test("r5"), Operator::NE, ONE, ZERO), + Statement::binary( + heap.alloc_str_for_test("r6"), Operator::EQ, - Expression::var_name(heap.alloc_str_for_test("r4"), INT_TYPE), - Expression::var_name(heap.alloc_str_for_test("r2"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("r5"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("r3"), INT_TYPE), ), ], - Expression::var_name(heap.alloc_str_for_test("r5"), INT_TYPE), + Expression::var_name(heap.alloc_str_for_test("r6"), INT_TYPE), heap, table, r#"__$foo(); __$bar(); let a1: int = __$foo(); let a22: int = __$bar(); +let ma4 = (a22: int) ^ 1; let r1 = (a22: int) == (a1: int); +let r2 = (ma4: int) == (a22: int); return 1;"#, );