Skip to content

Commit

Permalink
Futher optimization for binary expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank Laub committed Feb 13, 2010
1 parent 5547973 commit 108a5e4
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/DotWeb.Decompiler/Core/Interpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -890,9 +890,45 @@ private CodeExpression OptimizeBinaryExpression(CodeExpression lhs, CodeBinaryOp
throw new NotSupportedException();
}
}

if (IsBooleanExpression(lhs) && IsFalse(rhs)) {
return lhs.Invert();
}

return new CodeBinaryExpression(lhs, op, rhs);
}

bool IsBooleanExpression(CodeExpression expression) {
if (expression is CodeBinaryExpression) {
return IsComparisonOperator(((CodeBinaryExpression)expression).Operator);
}
else if (expression is CodeInvokeExpression) {
var reference = ((CodeInvokeExpression)expression).Method.Reference;
if (reference != null)
return reference.ReturnType.ReturnType.FullName == Constants.Boolean;
}
return false;
}

static bool IsComparisonOperator(CodeBinaryOperator op) {
switch (op) {
case CodeBinaryOperator.GreaterThan:
case CodeBinaryOperator.LessThan:
case CodeBinaryOperator.GreaterThanOrEqual:
case CodeBinaryOperator.LessThanOrEqual:
case CodeBinaryOperator.IdentityEquality:
case CodeBinaryOperator.IdentityInequality:
return true;
}
return false;
}

static bool IsFalse(CodeExpression expression) {
var literal = expression as CodePrimitiveExpression;
if (literal == null)
return false;
return 0.Equals(literal.Value);
}
#endregion
}
}

0 comments on commit 108a5e4

Please sign in to comment.