def test_optimize_code_with_variable_redefinition_expect_not_update(self): """ x gets redefined at 3rd statement, result in the 4th statement not optimized :return: """ as_tree = ast.parse( ms("""\ c = d + e e = 5 d = d + e d = d + e c = d + e c = d + e""")) lvn_test = Lvn() ssa_code = SsaCode(as_tree) ssa_code = lvn_test.optimize(ssa_code) self.assertEqual( str(ssa_code), ms("""\ c_0 = d_0 + e_0 e_1 = 5 d_1 = d_0 + 5 d_2 = 5 + d_1 c_1 = 5 + d_2 """))
def test_optimize_code_with_different_operator(self): as_tree = ast.parse( ms("""\ c = d + e d = d + e f = g | h i = s ^ 3 k = g | h p = s ^ 3 q = 3 < x l = 3 < x""")) lvn_test = Lvn() ssa_code = SsaCode(as_tree) ssa_code = lvn_test.optimize(ssa_code) self.assertEqual( str(ssa_code), ms("""\ c_0 = d_0 + e_0 d_1 = c_0 f_0 = g_0 | h_0 i_0 = s_0 ^ 3 k_0 = f_0 p_0 = i_0 q_0 = 3 < x_0 l_0 = q_0 """))
def xtest_problems_redefining(self): """ input: expected output a = x + y a = x + y b = x + y b = a a = 17 a = 17 c = x + y c = b """ ast_tree = ast.parse( ms("""\ a = x + y b = x + y a = 17 c = x + y""")) print(astor.to_source(ast_tree)) lvn_test = Lvn() optimized_tree = lvn_test.lvn_optimize(ast_tree) # self.assertEqual(lvn_test.value_number_dict['x'], 0) # self.assertEqual(lvn_test.value_number_dict['y'], 1) # self.assertEqual(lvn_test.value_number_dict['a'], 2) # self.assertEqual(lvn_test.value_number_dict['b'], 3) # # self.assertTrue('0Sub1' in lvn_test.lvnDict) # self.assertTrue(not isinstance(optimized_tree.body[3].value, ast.Name)) self.assert_source_generated( optimized_tree, ms("""\ a = x + y b = a a = 17 c = b """))
def test_optimize_redundant_stmt_1_stmt_d_d(self): as_tree = ast.parse(ms("""\ d = d""")) lvn_test = Lvn() ssa_code = SsaCode(as_tree) ssa_code = lvn_test.optimize(ssa_code) self.assertEqual(str(ssa_code), ms("""\ """))
def test_lvn_dict_find_substitute_found_substitute(self): lvn_handler = Lvn() lvn_handler.lvn_dict["0Add1"] = 2 lvn_stmt = LvnStatement(3, 0, 'Add', 1) lvn_stmt_result = lvn_handler.lvn_dict.find_substitute(lvn_stmt) expected_lvn_stmt_tuple = (3, 2, None, None) self.assert_lvn_stmt(lvn_stmt_result, expected_lvn_stmt_tuple, "0")
def test_valueAssignToVar_1_stmt(self): """ example: b = 2 """ ast_tree = ast.parse("b = 2") lvn_test = Lvn() lvn_test.lvn_optimize(ast_tree) self.assertEqual(lvn_test.value_number_dict['b'], 0)
def test_valueAssignToVar_updateVal(self): ast_tree = ast.parse( ms("""\ b = 2 c = 3 b = 4""")) lvn_test = Lvn() lvn_test.lvn_optimize(ast_tree) expected_value_dict = {'b': 2, 'c': 1} self.assertDictEqual(expected_value_dict, lvn_test.value_number_dict)
def test_optimize_redundant_stmt_2_stmt_a_a(self): as_tree = ast.parse(ms("""\ a = b a = a""")) lvn_test = Lvn() ssa_code = SsaCode(as_tree) ssa_code = lvn_test.optimize(ssa_code) self.assertEqual(str(ssa_code), ms("""\ a_0 = b_0 """))
def test_optimize_redundant_stmt_substituted(self): as_tree = ast.parse(ms("""\ d = a a = d""")) lvn_test = Lvn() ssa_code = SsaCode(as_tree) ssa_code = lvn_test.optimize(ssa_code) self.assertEqual(str(ssa_code), ms("""\ d_0 = a_0 """))
def test_optimize_simple_assignment_expect_substitute_single_var(self): as_tree = ast.parse(ms("""\ a = b c = a""")) lvn_test = Lvn() ssa_code = SsaCode(as_tree) ssa_code = lvn_test.optimize(ssa_code) self.assertEqual( str(ssa_code), ms("""\ a_0 = b_0 c_0 = b_0 """))
def test_optimize_reordering_operands_Sub_op_expect_no_subs(self): as_tree = ast.parse( ms("""\ a = b - c d = c - b""")) lvn_test = Lvn() ssa_code = SsaCode(as_tree) ssa_code = lvn_test.optimize(ssa_code) self.assertEqual( str(ssa_code), ms("""\ a_0 = b_0 - c_0 d_0 = c_0 - b_0 """))
def test_alg_identities_a_mult_0_0(self): ast_tree = ast.parse(ms("""\ b = a * 0""")) lvn_test = Lvn() optimized_tree = lvn_test.lvn_optimize(ast_tree) expected_value_dict = {'a': 0, '0': 1, 'b': 2} self.assertDictEqual(expected_value_dict, lvn_test.value_number_dict) self.assert_source_generated( optimized_tree, ms("""\ b = 0 """))
def test_optimize_reordering_operands_BitOr_op(self): as_tree = ast.parse( ms("""\ a = b | c d = c | b""")) lvn_test = Lvn() ssa_code = SsaCode(as_tree) ssa_code = lvn_test.optimize(ssa_code) self.assertEqual( str(ssa_code), ms("""\ a_0 = b_0 | c_0 d_0 = a_0 """))
def test_valueAssignToVar_0_1(self): ast_tree = ast.parse( ms("""\ b = 2 c = 3 a = b + c""")) lvn_test = Lvn() lvn_test.lvn_optimize(ast_tree) # Expected value expected_value_dict = {'b': 0, 'c': 1, 'a': 2} expected_assign_dict = {'0Add1': 2} self.assertDictEqual(expected_value_dict, lvn_test.value_number_dict) self.assertDictEqual(expected_assign_dict, lvn_test.lvnDict)
def test_optimize_code_with_bin_op(self): as_tree = ast.parse( ms("""\ f = g | h k = g | j""")) lvn_test = Lvn() ssa_code = SsaCode(as_tree) ssa_code = lvn_test.optimize(ssa_code) self.assertEqual( str(ssa_code), ms("""\ f_0 = g_0 | h_0 k_0 = g_0 | j_0 """))
def test_optimize_code_with_xor(self): as_tree = ast.parse( ms("""\ f = g ^ 33 k = g ^ h""")) lvn_test = Lvn() ssa_code = SsaCode(as_tree) ssa_code = lvn_test.optimize(ssa_code) self.assertEqual( str(ssa_code), ms("""\ f_0 = g_0 ^ 33 k_0 = g_0 ^ h_0 """))
def test_optimize_code_with_2_lvn_stmt_same_expect_not_updated(self): as_tree = ast.parse( ms("""\ f = g + j # 0 + 1 k = g + 1 """)) lvn_test = Lvn() ssa_code = SsaCode(as_tree) ssa_code = lvn_test.optimize(ssa_code) self.assertEqual( str(ssa_code), ms("""\ f_0 = g_0 + j_0 k_0 = g_0 + 1 """))
def test_simple_assign_find_substitute(self): lvn_handler = Lvn() self.assertEqual( lvn_handler.lvn_dict.simple_assign_dict.find_substitute(0), 0) lvn_handler.lvn_dict.simple_assign_dict.update_simp_assgn(0, 5) self.assertEqual( lvn_handler.lvn_dict.simple_assign_dict.find_substitute(0), 5)
def test_lvn_dict_find_substitute_given_simple_expr(self): lvn_handler = Lvn() lvn_handler.lvn_dict["0 + 1"] = 2 # 3 = 0 lvn_stmt = LvnStatement(3, 0, None, None) lvn_stmt_result = lvn_handler.lvn_dict.find_substitute(lvn_stmt) expected_lvn_stmt_tuple = (3, 0, None, None) self.assert_lvn_stmt(lvn_stmt_result, expected_lvn_stmt_tuple, "0") # 3 = 0 (0 is constant) lvn_stmt = LvnStatement(3, 0, None, None) lvn_stmt_result = lvn_handler.lvn_dict.find_substitute(lvn_stmt) expected_lvn_stmt_tuple = (3, 0, None, None) self.assert_lvn_stmt(lvn_stmt_result, expected_lvn_stmt_tuple, "0")
def test_lvn_code_tuple_list_append_lvn_stmt(self): lvn_handler = Lvn() lvn_stmt = LvnStatement(3, 0, 'Add', 1) lvn_handler.lvn_dict.lvn_code_tuples_list.append_lvn_stmt(lvn_stmt) expected_lvn_code_tup_list = [(3, 0, 'Add', 1)] self.assertTupleEqual(lvn_handler.lvn_dict.lvn_code_tuples_list[0], expected_lvn_code_tup_list[0])
def test_lvn_dict_find_substitute_given_dict_empty(self): lvn_handler = Lvn() # lvn_dict is empty lvn_stmt = LvnStatement(3, 0, 'Add', 1) lvn_stmt_result = lvn_handler.lvn_dict.find_substitute(lvn_stmt) expected_lvn_stmt_tuple = (3, 0, 'Add', 1) self.assert_lvn_stmt(lvn_stmt_result, expected_lvn_stmt_tuple, "0")
def test_optimize_simple_assignment_given_3_stmt_same(self): as_tree = ast.parse( ms("""\ a=3 b=a c=b""")) lvn_test = Lvn() ssa_code = SsaCode(as_tree) ssa_code = lvn_test.optimize(ssa_code) self.assertEqual( str(ssa_code), ms("""\ a_0 = 3 b_0 = 3 c_0 = 3 """))
def test_optimize_simple_assignment_given_constant(self): as_tree = ast.parse( ms("""\ a = 33 + y b = 33 c = b d = c + y""")) lvn_test = Lvn() ssa_code = SsaCode(as_tree) ssa_code = lvn_test.optimize(ssa_code) self.assertEqual( str(ssa_code), ms("""\ a_0 = 33 + y_0 b_0 = 33 c_0 = 33 d_0 = a_0 """))
def test_optimize_simple_assignment_expect_substituted(self): as_tree = ast.parse( ms("""\ z = a + y b = a c = b d = c + y""")) lvn_test = Lvn() ssa_code = SsaCode(as_tree) ssa_code = lvn_test.optimize(ssa_code) self.assertEqual( str(ssa_code), ms("""\ z_0 = a_0 + y_0 b_0 = a_0 c_0 = a_0 d_0 = z_0 """))
def test_optimize_simple_assignment_expect_constant_folding(self): as_tree = ast.parse( ms("""\ a = 5 b = a c = b + 9 d = b * c""")) lvn_test = Lvn() ssa_code = SsaCode(as_tree) ssa_code = lvn_test.optimize(ssa_code) self.assertEqual( str(ssa_code), ms("""\ a_0 = 5 b_0 = 5 c_0 = 14 d_0 = 70 """))
def test_optimize_redundant_stmt_2_stmt(self): as_tree = ast.parse( ms("""\ d = b + 0 e = b f = b - 0 g = c * 0""")) lvn_test = Lvn() ssa_code = SsaCode(as_tree) ssa_code = lvn_test.optimize(ssa_code) self.assertEqual( str(ssa_code), ms("""\ d_0 = b_0 e_0 = b_0 f_0 = b_0 g_0 = 0 """))
def test_optimize_tree_given_var_add_const_expect_substituted(self): ast_tree = ast.parse( ms("""\ a = b + 12 d = b + 12""")) lvn_test = Lvn() optimized_tree = lvn_test.lvn_optimize(ast_tree) expected_value_dict = {'b': 0, '12': 1, 'a': 2, 'd': 3} expected_assign_dict = {'0Add1': 2} self.assertDictEqual(expected_value_dict, lvn_test.value_number_dict) self.assertDictEqual(expected_assign_dict, lvn_test.lvnDict) self.assert_source_generated( optimized_tree, ms("""\ a = b + 12 d = a """))
def test_alg_identities_a_add_a_expect_substituted(self): ast_tree = ast.parse( ms("""\ b = a + a c = a + a""")) lvn_test = Lvn() optimized_tree = lvn_test.lvn_optimize(ast_tree) expected_value_dict = {'a': 0, 'b': 1, 'c': 2} expected_assign_dict = {'0Add0': 1} self.assertDictEqual(expected_value_dict, lvn_test.value_number_dict) self.assertDictEqual(expected_assign_dict, lvn_test.lvnDict) self.assert_source_generated( optimized_tree, ms("""\ b = a + a c = b """))
def test_optimize_simple_assignment_expect_constant_fold_and_replaced( self): as_tree = ast.parse( ms("""\ a = 14 + c d = 8 e = d + 6 # is 14 f = e + c""")) lvn_test = Lvn() ssa_code = SsaCode(as_tree) ssa_code = lvn_test.optimize(ssa_code) self.assertEqual( str(ssa_code), ms("""\ a_0 = 14 + c_0 d_0 = 8 e_0 = 14 f_0 = a_0 """))
def test_valueAssignToVar_commutative_minus_operation(self): ast_tree = ast.parse( ms("""\ a = x - y b = y - x""")) lvn_test = Lvn() optimized_tree = lvn_test.lvn_optimize(ast_tree) expected_value_dict = {'a': 2, 'b': 3, 'x': 0, 'y': 1} expected_assign_dict = {'0Sub1': 2, '1Sub0': 3} self.assertDictEqual(expected_value_dict, lvn_test.value_number_dict) self.assertDictEqual(expected_assign_dict, lvn_test.lvnDict) self.assert_source_generated( optimized_tree, ms("""\ a = x - y b = y - x """))