Browse Source

Move binary->logical AND to ExpressionTransforms and use SemanticHelper.IsPure

pull/1213/head
Chicken-Bones 8 years ago
parent
commit
f539bee362
  1. 44
      ICSharpCode.Decompiler/IL/Transforms/EarlyExpressionTransforms.cs
  2. 13
      ICSharpCode.Decompiler/IL/Transforms/ExpressionTransforms.cs

44
ICSharpCode.Decompiler/IL/Transforms/EarlyExpressionTransforms.cs

@ -111,49 +111,5 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -111,49 +111,5 @@ namespace ICSharpCode.Decompiler.IL.Transforms
}
return null;
}
protected internal override void VisitBinaryNumericInstruction(BinaryNumericInstruction inst)
{
if (inst.Operator != BinaryNumericOperator.BitAnd)
return;
if (PotentialSideEffects(inst.Left) || PotentialSideEffects(inst.Right))
return;
if (!IsBoolean(inst.Left) || !IsBoolean(inst.Right))
return;
context.Step("Replace bit.and with logic.and", inst);
var expr = IfInstruction.LogicAnd(inst.Left, inst.Right);
inst.ReplaceWith(expr);
expr.AcceptVisitor(this);
}
private bool IsBoolean(ILInstruction inst) => inst is Comp || inst.InferType().IsKnownType(KnownTypeCode.Boolean);
private bool PotentialSideEffects(ILInstruction inst)
{
switch (inst) {
case LdLoc _:
case LdLoca _:
case LdcF4 _:
case LdcF8 _:
case LdcI4 _:
case LdcI8 _:
case LdcDecimal _:
return false;
case Comp c:
return PotentialSideEffects(c.Left) || PotentialSideEffects(c.Right);
case BinaryNumericInstruction b:
return PotentialSideEffects(b.Left) || PotentialSideEffects(b.Right);
case BitNot b:
return PotentialSideEffects(b.Argument);
case Conv c:
return PotentialSideEffects(c.Argument);
}
return true;
}
}
}

13
ICSharpCode.Decompiler/IL/Transforms/ExpressionTransforms.cs

@ -477,9 +477,22 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -477,9 +477,22 @@ namespace ICSharpCode.Decompiler.IL.Transforms
inst.Right = lhs;
}
break;
case BinaryNumericOperator.BitAnd:
if (IsBoolean(inst.Left) && IsBoolean(inst.Right) && SemanticHelper.IsPure(inst.Right.Flags))
{
context.Step("Replace bit.and with logic.and", inst);
var expr = IfInstruction.LogicAnd(inst.Left, inst.Right);
inst.ReplaceWith(expr);
expr.AcceptVisitor(this);
}
break;
}
}
private static bool IsBoolean(ILInstruction inst) =>
inst is Comp c && c.ResultType == StackType.I4 ||
inst.InferType().IsKnownType(KnownTypeCode.Boolean);
protected internal override void VisitTryCatchHandler(TryCatchHandler inst)
{
base.VisitTryCatchHandler(inst);

Loading…
Cancel
Save