Browse Source

Add support for switch on (ReadOnly)Span<char> using a compiler-generated hash function.

pull/3187/head
Siegfried Pammer 2 years ago
parent
commit
969e3e546a
  1. 4
      ICSharpCode.Decompiler/IL/ControlFlow/SwitchDetection.cs
  2. 39
      ICSharpCode.Decompiler/IL/Transforms/SwitchOnStringTransform.cs

4
ICSharpCode.Decompiler/IL/ControlFlow/SwitchDetection.cs

@ -16,12 +16,10 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE. // DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using ICSharpCode.Decompiler.CSharp.Transforms;
using ICSharpCode.Decompiler.FlowAnalysis; using ICSharpCode.Decompiler.FlowAnalysis;
using ICSharpCode.Decompiler.IL.Transforms; using ICSharpCode.Decompiler.IL.Transforms;
using ICSharpCode.Decompiler.TypeSystem; using ICSharpCode.Decompiler.TypeSystem;
@ -361,7 +359,7 @@ namespace ICSharpCode.Decompiler.IL.ControlFlow
private bool MatchRoslynSwitchOnString() private bool MatchRoslynSwitchOnString()
{ {
var insns = analysis.RootBlock.Instructions; var insns = analysis.RootBlock.Instructions;
return insns.Count >= 3 && SwitchOnStringTransform.MatchComputeStringHashCall(insns[insns.Count - 3], analysis.SwitchVariable, out var switchLdLoc); return insns.Count >= 3 && SwitchOnStringTransform.MatchComputeStringOrReadOnlySpanHashCall(insns[insns.Count - 3], analysis.SwitchVariable, out _);
} }
/// <summary> /// <summary>

39
ICSharpCode.Decompiler/IL/Transforms/SwitchOnStringTransform.cs

@ -294,6 +294,16 @@ namespace ICSharpCode.Decompiler.IL.Transforms
keepAssignmentBefore = true; keepAssignmentBefore = true;
switchValue = new LdLoc(switchValueVar); switchValue = new LdLoc(switchValueVar);
} }
if (!switchValueVar.Type.IsKnownType(KnownTypeCode.String))
{
if (!context.Settings.SwitchOnReadOnlySpanChar)
return false;
if (!switchValueVar.Type.IsKnownType(KnownTypeCode.ReadOnlySpanOfT)
&& !switchValueVar.Type.IsKnownType(KnownTypeCode.SpanOfT))
{
return false;
}
}
// if instruction must be followed by a branch to the next case // if instruction must be followed by a branch to the next case
if (!nextCaseJump.MatchBranch(out Block currentCaseBlock)) if (!nextCaseJump.MatchBranch(out Block currentCaseBlock))
return false; return false;
@ -335,7 +345,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms
int offset = firstBlock == null ? 1 : 0; int offset = firstBlock == null ? 1 : 0;
var sections = new List<SwitchSection>(values.Skip(offset).SelectWithIndex((index, s) => new SwitchSection { Labels = new LongSet(index), Body = s.Item2 is Block b ? new Branch(b) : s.Item2.Clone() })); var sections = new List<SwitchSection>(values.Skip(offset).SelectWithIndex((index, s) => new SwitchSection { Labels = new LongSet(index), Body = s.Item2 is Block b ? new Branch(b) : s.Item2.Clone() }));
sections.Add(new SwitchSection { Labels = new LongSet(new LongInterval(0, sections.Count)).Invert(), Body = currentCaseBlock != null ? (ILInstruction)new Branch(currentCaseBlock) : new Leave((BlockContainer)nextCaseBlock) }); sections.Add(new SwitchSection { Labels = new LongSet(new LongInterval(0, sections.Count)).Invert(), Body = currentCaseBlock != null ? (ILInstruction)new Branch(currentCaseBlock) : new Leave((BlockContainer)nextCaseBlock) });
var stringToInt = new StringToInt(switchValue, values.Skip(offset).Select(item => item.Item1).ToArray(), context.TypeSystem.FindType(KnownTypeCode.String)); var stringToInt = new StringToInt(switchValue, values.Skip(offset).Select(item => item.Item1).ToArray(), switchValueVar.Type);
var inst = new SwitchInstruction(stringToInt); var inst = new SwitchInstruction(stringToInt);
inst.Sections.AddRange(sections); inst.Sections.AddRange(sections);
if (removeExtraLoad) if (removeExtraLoad)
@ -1008,7 +1018,7 @@ namespace ICSharpCode.Decompiler.IL.Transforms
if (!(switchBlockInstructionsOffset + 1 < switchBlockInstructions.Count if (!(switchBlockInstructionsOffset + 1 < switchBlockInstructions.Count
&& switchBlockInstructions[switchBlockInstructionsOffset + 1] is SwitchInstruction switchInst && switchBlockInstructions[switchBlockInstructionsOffset + 1] is SwitchInstruction switchInst
&& switchInst.Value.MatchLdLoc(out var switchValueVar) && switchInst.Value.MatchLdLoc(out var switchValueVar)
&& MatchComputeStringHashCall(switchBlockInstructions[switchBlockInstructionsOffset], && MatchComputeStringOrReadOnlySpanHashCall(switchBlockInstructions[switchBlockInstructionsOffset],
switchValueVar, out LdLoc switchValueLoad))) switchValueVar, out LdLoc switchValueLoad)))
{ {
return false; return false;
@ -1643,19 +1653,28 @@ namespace ICSharpCode.Decompiler.IL.Transforms
} }
/// <summary> /// <summary>
/// Matches 'stloc(targetVar, call ComputeStringHash(ldloc switchValue))' /// Matches
/// 'stloc(targetVar, call ComputeStringHash(ldloc switchValue))'
/// - or -
/// 'stloc(targetVar, call ComputeSpanHash(ldloc switchValue))'
/// - or -
/// 'stloc(targetVar, call ComputeReadOnlySpanHash(ldloc switchValue))'
/// </summary> /// </summary>
internal static bool MatchComputeStringHashCall(ILInstruction inst, ILVariable targetVar, out LdLoc switchValue) internal static bool MatchComputeStringOrReadOnlySpanHashCall(ILInstruction inst, ILVariable targetVar, out LdLoc switchValue)
{ {
switchValue = null; switchValue = null;
if (!inst.MatchStLoc(targetVar, out var value)) if (!inst.MatchStLoc(targetVar, out var value))
return false; return false;
if (!(value is Call c && c.Arguments.Count == 1 && c.Method.Name == "ComputeStringHash" && c.Method.IsCompilerGeneratedOrIsInCompilerGeneratedClass())) if (value is Call c && c.Arguments.Count == 1
return false; && c.Method.Name is "ComputeStringHash" or "ComputeSpanHash" or "ComputeReadOnlySpanHash"
if (!(c.Arguments[0] is LdLoc)) && c.Method.IsCompilerGeneratedOrIsInCompilerGeneratedClass())
return false; {
switchValue = (LdLoc)c.Arguments[0]; if (c.Arguments[0] is not LdLoc ldloc)
return true; return false;
switchValue = ldloc;
return true;
}
return false;
} }
/// <summary> /// <summary>

Loading…
Cancel
Save