private void OptimizeBlock(BasicBlock block) { var currentTable = In[block].Copy(); for (int i = 0; i < block.Instructions.Count; ++i) { if (block.Instructions[i].Operation.IsArithmetic()) { var expression = new AvailableExpression( block.Instructions[i].Argument1, block.Instructions[i].Argument2, block.Instructions[i].Operation); if (currentTable.ExpressionsTable.ContainsKey(expression)) { block.Instructions[i].Operation = "="; block.Instructions[i].Argument1 = currentTable.ExpressionsTable[expression]; block.Instructions[i].Argument2 = ""; } else { currentTable.AddExpression(expression, block.Instructions[i]); } } if (block.Instructions[i].Result.IsVariable()) { currentTable.RemoveExpressions(block.Instructions[i].Result); } } }
public void AddExpression(AvailableExpression newExpression, TACInstruction instrution) { if (!ExpressionsTable.ContainsKey(newExpression)) { ExpressionsTable.Add(newExpression, instrution.Result); } }
public void InstanceIsNotEqualToNull() { var expression = new MultiplyExpression(new IntegerExpression(1), new VariableExpression("x")); var available = new AvailableExpression(_CreateFlowNode(), expression); Assert.AreNotEqual(available, null); }
public void InstanceHasSameHashCodeAsItself() { var expression = new MultiplyExpression(new IntegerExpression(1), new VariableExpression("x")); var available = new AvailableExpression(_CreateFlowNode(), expression); Assert.AreEqual(available.GetHashCode(), available.GetHashCode()); }
public void InstanceIsNotEqualToCompletelyDifferent() { var expression1 = new AddExpression(new IntegerExpression(1), new IntegerExpression(2)); var expression2 = new ModuloExpression(new VariableExpression("y"), new VariableExpression("z")); var available1 = new AvailableExpression(_CreateFlowNode(), expression1); var available2 = new AvailableExpression(_CreateFlowNode(), expression2); Assert.AreNotEqual(available1, available2); }
public void InstanceIsNotEqualToOtherWithDifferentNode() { var expression1 = new DivideExpression(new IntegerExpression(5), new VariableExpression("y")); var expression2 = new DivideExpression(new IntegerExpression(5), new VariableExpression("y")); var available1 = new AvailableExpression(_CreateFlowNode(), expression1); var available2 = new AvailableExpression(_CreateFlowNode(), expression2); Assert.AreNotEqual(available1, available2); }
public void InstanceHasNotSameHashCodeAsCompletelyDifferent() { var expression1 = new ModuloExpression(new IntegerExpression(3), new VariableExpression("y")); var expression2 = new DivideExpression(new IntegerExpression(4), new VariableExpression("z")); var available1 = new AvailableExpression(_CreateFlowNode(), expression1); var available2 = new AvailableExpression(_CreateFlowNode(), expression2); Assert.AreNotEqual(available1.GetHashCode(), available2.GetHashCode()); }
public void InstanceHasNotSameHashCodeAsOtherWithDifferenNode() { var expression1 = new DivideExpression(new IntegerExpression(5), new VariableExpression("y")); var expression2 = new DivideExpression(new IntegerExpression(5), new VariableExpression("y")); var available1 = new AvailableExpression(_CreateFlowNode(), expression1); var available2 = new AvailableExpression(_CreateFlowNode(), expression2); Assert.AreNotEqual(available1.GetHashCode(), available2.GetHashCode()); }
public void InstanceIsNotEqualToOtherWithDifferentRightOperand() { var node = _CreateFlowNode(); var expression1 = new AddExpression(new VariableExpression("y"), new VariableExpression("y")); var expression2 = new AddExpression(new VariableExpression("y"), new VariableExpression("z")); var available1 = new AvailableExpression(node, expression1); var available2 = new AvailableExpression(node, expression2); Assert.AreNotEqual(available1, available2); }
public void InstanceIsNotEqualToOtherWithOperator() { var node = _CreateFlowNode(); var expression1 = new AddExpression(new IntegerExpression(5), new VariableExpression("y")); var expression2 = new MultiplyExpression(new IntegerExpression(5), new VariableExpression("y")); var available1 = new AvailableExpression(node, expression1); var available2 = new AvailableExpression(node, expression2); Assert.AreNotEqual(available1, available2); }
public void InstanceIsEqualToOtherWithSameInformation() { var node = _CreateFlowNode(); var expression1 = new DivideExpression(new IntegerExpression(5), new VariableExpression("y")); var expression2 = new DivideExpression(new IntegerExpression(5), new VariableExpression("y")); var available1 = new AvailableExpression(node, expression1); var available2 = new AvailableExpression(node, expression2); Assert.AreEqual(available1, available2); }
public void InstanceHasNotSameHashCodeAsOtherWithDifferenLeftRight() { var node = _CreateFlowNode(); var expression1 = new ModuloExpression(new IntegerExpression(3), new VariableExpression("y")); var expression2 = new ModuloExpression(new IntegerExpression(3), new VariableExpression("z")); var available1 = new AvailableExpression(node, expression1); var available2 = new AvailableExpression(node, expression2); Assert.AreNotEqual(available1.GetHashCode(), available2.GetHashCode()); }
public void InstanceHasNotSameHashCodeAsOtherWithDifferentOperator() { var node = _CreateFlowNode(); var expression1 = new AddExpression(new IntegerExpression(5), new VariableExpression("y")); var expression2 = new MultiplyExpression(new IntegerExpression(5), new VariableExpression("y")); var available1 = new AvailableExpression(node, expression1); var available2 = new AvailableExpression(node, expression2); Assert.AreNotEqual(available1.GetHashCode(), available2.GetHashCode()); }