From f539bee362e1298709d77c83d7cd4939576dad93 Mon Sep 17 00:00:00 2001 From: Chicken-Bones Date: Wed, 18 Jul 2018 11:07:24 +1000 Subject: [PATCH] Move binary->logical AND to ExpressionTransforms and use SemanticHelper.IsPure --- .../Transforms/EarlyExpressionTransforms.cs | 44 ------------------- .../IL/Transforms/ExpressionTransforms.cs | 13 ++++++ 2 files changed, 13 insertions(+), 44 deletions(-) diff --git a/ICSharpCode.Decompiler/IL/Transforms/EarlyExpressionTransforms.cs b/ICSharpCode.Decompiler/IL/Transforms/EarlyExpressionTransforms.cs index 5c394b9da..bba216c13 100644 --- a/ICSharpCode.Decompiler/IL/Transforms/EarlyExpressionTransforms.cs +++ b/ICSharpCode.Decompiler/IL/Transforms/EarlyExpressionTransforms.cs @@ -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; - } } } diff --git a/ICSharpCode.Decompiler/IL/Transforms/ExpressionTransforms.cs b/ICSharpCode.Decompiler/IL/Transforms/ExpressionTransforms.cs index 146e22831..515d21a15 100644 --- a/ICSharpCode.Decompiler/IL/Transforms/ExpressionTransforms.cs +++ b/ICSharpCode.Decompiler/IL/Transforms/ExpressionTransforms.cs @@ -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);