Browse Source

Support leave instructions in RemoveInfeasiblePathTransform and transform match(x) ? true : false to match(x).

pull/2461/head
Siegfried Pammer 4 years ago
parent
commit
cd0c76d7b1
  1. 10
      ICSharpCode.Decompiler/IL/Transforms/ExpressionTransforms.cs
  2. 12
      ICSharpCode.Decompiler/IL/Transforms/RemoveInfeasiblePathTransform.cs

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

@ -589,11 +589,19 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -589,11 +589,19 @@ namespace ICSharpCode.Decompiler.IL.Transforms
return;
}
}
if (MatchInstruction.IsPatternMatch(inst.Condition, out _)
&& inst.TrueInst.MatchLdcI4(1) && inst.FalseInst.MatchLdcI4(0))
{
context.Step("match(x) ? true : false -> match(x)", inst);
inst.Condition.AddILRange(inst);
inst.ReplaceWith(inst.Condition);
return;
}
}
IfInstruction HandleConditionalOperator(IfInstruction inst)
{
// if (cond) stloc (A, V1) else stloc (A, V2) --> stloc (A, if (cond) V1 else V2)
// if (cond) stloc A(V1) else stloc A(V2) --> stloc A(if (cond) V1 else V2)
Block trueInst = inst.TrueInst as Block;
if (trueInst == null || trueInst.Instructions.Count != 1)
return inst;

12
ICSharpCode.Decompiler/IL/Transforms/RemoveInfeasiblePathTransform.cs

@ -60,10 +60,10 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -60,10 +60,10 @@ namespace ICSharpCode.Decompiler.IL.Transforms
{
if (!MatchBlock1(block, out var s, out int value, out var br))
return false;
if (!MatchBlock2(br.TargetBlock, s, value, out var targetBlock))
if (!MatchBlock2(br.TargetBlock, s, value, out var exitInst))
return false;
context.Step("RemoveInfeasiblePath", br);
br.TargetBlock = targetBlock;
br.ReplaceWith(exitInst.Clone());
s.RemoveIfRedundant = true;
return true;
}
@ -99,9 +99,9 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -99,9 +99,9 @@ namespace ICSharpCode.Decompiler.IL.Transforms
// if (logic.not(ldloc s)) br IL_0027
// br IL_001d
// }
bool MatchBlock2(Block block, ILVariable s, int constantValue, [NotNullWhen(true)] out Block? targetBlock)
bool MatchBlock2(Block block, ILVariable s, int constantValue, [NotNullWhen(true)] out ILInstruction? exitInst)
{
targetBlock = null;
exitInst = null;
if (block.Instructions.Count != 2)
return false;
if (block.IncomingEdgeCount <= 1)
@ -110,8 +110,8 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -110,8 +110,8 @@ namespace ICSharpCode.Decompiler.IL.Transforms
return false;
if (!load.MatchLdLoc(s))
return false;
ILInstruction target = constantValue != 0 ? trueInst : falseInst;
return target.MatchBranch(out targetBlock);
exitInst = constantValue != 0 ? trueInst : falseInst;
return exitInst is Branch or Leave { Value: Nop };
}
}
}

Loading…
Cancel
Save