Исправлено упрощение выражений

This commit is contained in:
Victor Melnik 2018-10-01 23:06:43 +03:00 committed by GitHub
parent a4d5b43003
commit fe7ac6a9b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -58,26 +58,26 @@ public class ExpressionSimplification extends OptimizationVisitor<Void> implemen
if (expr1IsZero || isIntegerValue(s.expr2, 0)) { if (expr1IsZero || isIntegerValue(s.expr2, 0)) {
switch (s.operation) { switch (s.operation) {
case ADD: case ADD:
// 0 + x2 to x2, x1 + 0 to x1 // 0 + x = x + 0 = x
simplificationsCount++; simplificationsCount++;
return expr1IsZero ? s.expr2 : s.expr1; return expr1IsZero ? s.expr2 : s.expr1;
case SUBTRACT: case SUBTRACT:
simplificationsCount++; simplificationsCount++;
if (expr1IsZero) { if (expr1IsZero) {
// 0 - x2 to -x2 // 0 - x = -x
return new UnaryExpression(UnaryExpression.Operator.NEGATE, s.expr2); return new UnaryExpression(UnaryExpression.Operator.NEGATE, s.expr2);
} }
// x1 - 0 to x1 // x - 0 = x
return s.expr1; return s.expr1;
case MULTIPLY: case MULTIPLY:
// 0 * x2 to 0, x1 * 0 to 0 // 0 * x = x * 0 = 0
simplificationsCount++; simplificationsCount++;
return new ValueExpression(0); return new ValueExpression(0);
case DIVIDE: case DIVIDE:
// 0 / x2 to 0 // 0 / x = 0
if (expr1IsZero) { if (expr1IsZero) {
simplificationsCount++; simplificationsCount++;
return new ValueExpression(0); return new ValueExpression(0);
@ -91,12 +91,12 @@ public class ExpressionSimplification extends OptimizationVisitor<Void> implemen
if (expr1IsOne || isIntegerValue(s.expr2, 1)) { if (expr1IsOne || isIntegerValue(s.expr2, 1)) {
switch (s.operation) { switch (s.operation) {
case MULTIPLY: case MULTIPLY:
// 1 * x2 to x2, x1 * 1 to x1 // 1 * x = x * 1 = x
simplificationsCount++; simplificationsCount++;
return expr1IsOne ? s.expr2 : s.expr1; return expr1IsOne ? s.expr2 : s.expr1;
case DIVIDE: case DIVIDE:
// x1 / 1 to x1 // x / 1 = x
if (!expr1IsOne) { if (!expr1IsOne) {
simplificationsCount++; simplificationsCount++;
return s.expr1; return s.expr1;
@ -105,19 +105,26 @@ public class ExpressionSimplification extends OptimizationVisitor<Void> implemen
} }
} }
// x1 / -1 to -x1 // x / -1 = -x
if (isIntegerValue(s.expr2, -1)) { if (isIntegerValue(s.expr2, -1) && s.operation == BinaryExpression.Operator.DIVIDE) {
simplificationsCount++; simplificationsCount++;
return new UnaryExpression(UnaryExpression.Operator.NEGATE, s.expr1); return new UnaryExpression(UnaryExpression.Operator.NEGATE, s.expr1);
} }
// x - x to 0 // -1 * x = x * -1 = -x
final boolean expr1IsMinusOne = isIntegerValue(s.expr1, -1);
if ((expr1IsMinusOne || isIntegerValue(s.expr2, -1)) && s.operation == BinaryExpression.Operator.MULTIPLY) {
simplificationsCount++;
return new UnaryExpression(UnaryExpression.Operator.NEGATE, expr1IsMinusOne ? s.expr2 : s.expr1);
}
// x - x = 0
if (isSameVariables(s.expr1, s.expr2) && s.operation == BinaryExpression.Operator.SUBTRACT) { if (isSameVariables(s.expr1, s.expr2) && s.operation == BinaryExpression.Operator.SUBTRACT) {
simplificationsCount++; simplificationsCount++;
return new ValueExpression(0); return new ValueExpression(0);
} }
// x >> 0 to x, x << 0 to x // x >> 0 = x, x << 0 = x
if (isIntegerValue(s.expr2, 0) && if (isIntegerValue(s.expr2, 0) &&
(s.operation == BinaryExpression.Operator.LSHIFT || (s.operation == BinaryExpression.Operator.LSHIFT ||
s.operation == BinaryExpression.Operator.RSHIFT)) { s.operation == BinaryExpression.Operator.RSHIFT)) {
@ -134,12 +141,12 @@ public class ExpressionSimplification extends OptimizationVisitor<Void> implemen
return super.visit(s, t); return super.visit(s, t);
} }
if (isIntegerValue(s.expr1, 0) && s.operation == ConditionalExpression.Operator.AND) { if (isIntegerValue(s.expr1, 0) && s.operation == ConditionalExpression.Operator.AND) {
// 0 && x2 to 0 // 0 && x = 0
simplificationsCount++; simplificationsCount++;
return new ValueExpression(0); return new ValueExpression(0);
} }
if (isIntegerValue(s.expr1, 1) && s.operation == ConditionalExpression.Operator.OR) { if (isIntegerValue(s.expr1, 1) && s.operation == ConditionalExpression.Operator.OR) {
// 1 || x2 to 1 // 1 || x = 1
simplificationsCount++; simplificationsCount++;
return new ValueExpression(1); return new ValueExpression(1);
} }