Skip to content

Commit

Permalink
BUGFIX: Optimize ldloca and ldarga to their short forms if possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
Washi1337 committed Jan 3, 2024
1 parent d2f8ec7 commit 9ab4e94
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 11 deletions.
25 changes: 17 additions & 8 deletions src/AsmResolver.DotNet/Code/Cil/CilInstructionCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ private bool TryOptimizeVariable(CilInstruction instruction)
{
var variable = instruction.GetLocalVariable(Owner.LocalVariables);

CilOpCode code = instruction.OpCode;
var code = instruction.OpCode;
object? operand = instruction.Operand;

if (instruction.IsLdloc())
Expand All @@ -470,7 +470,7 @@ private bool TryOptimizeVariable(CilInstruction instruction)
1 => (CilOpCodes.Ldloc_1, null),
2 => (CilOpCodes.Ldloc_2, null),
3 => (CilOpCodes.Ldloc_3, null),
{} x when x >= byte.MinValue && x <= byte.MaxValue => (CilOpCodes.Ldloc_S, variable),
<= byte.MaxValue and >= byte.MinValue => (CilOpCodes.Ldloc_S, variable),
_ => (CilOpCodes.Ldloc, variable),
};
}
Expand All @@ -482,10 +482,15 @@ private bool TryOptimizeVariable(CilInstruction instruction)
1 => (CilOpCodes.Stloc_1, null),
2 => (CilOpCodes.Stloc_2, null),
3 => (CilOpCodes.Stloc_3, null),
{} x when x >= byte.MinValue && x <= byte.MaxValue => (CilOpCodes.Stloc_S, variable),
<= byte.MaxValue and >= byte.MinValue => (CilOpCodes.Stloc_S, variable),
_ => (CilOpCodes.Stloc, variable),
};
}
else if (instruction.OpCode.Code == CilCode.Ldloca)
{
if (variable.Index is >= byte.MinValue and <= byte.MaxValue)
code = CilOpCodes.Ldloca_S;
}

if (code != instruction.OpCode)
{
Expand All @@ -501,7 +506,7 @@ private bool TryOptimizeArgument(CilInstruction instruction)
{
var parameter = instruction.GetParameter(Owner.Owner.Parameters);

CilOpCode code = instruction.OpCode;
var code = instruction.OpCode;
object? operand = instruction.Operand;

if (instruction.IsLdarg())
Expand All @@ -512,15 +517,19 @@ private bool TryOptimizeArgument(CilInstruction instruction)
1 => (CilOpCodes.Ldarg_1, null),
2 => (CilOpCodes.Ldarg_2, null),
3 => (CilOpCodes.Ldarg_3, null),
{} x when x >= byte.MinValue && x <= byte.MaxValue => (CilOpCodes.Ldarg_S, parameter),
>= byte.MinValue and <= byte.MaxValue => (CilOpCodes.Ldarg_S, parameter),
_ => (CilOpCodes.Ldarg, parameter),
};
}
else if (instruction.IsStarg())
{
code = parameter.MethodSignatureIndex <= byte.MaxValue
? CilOpCodes.Starg_S
: CilOpCodes.Starg;
if (parameter.MethodSignatureIndex <= byte.MaxValue)
code = CilOpCodes.Starg_S;
}
else if (instruction.OpCode.Code == CilCode.Ldarga)
{
if (parameter.Index is >= byte.MinValue and <= byte.MaxValue)
code = CilOpCodes.Ldarga_S;
}

if (code != instruction.OpCode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void OptimizeFirst4ArgumentsToMacros(int index, CilCode expectedMacro)
public void OptimizeHiddenThisToLdarg0()
{
var instructions = CreateDummyMethod(true, 0, 0);
instructions.Add(CilOpCodes.Ldarg, instructions.Owner.Owner.Parameters.ThisParameter);
instructions.Add(CilOpCodes.Ldarg, instructions.Owner.Owner.Parameters.ThisParameter!);
instructions.Add(CilOpCodes.Ret);

instructions.OptimizeMacros();
Expand Down Expand Up @@ -183,6 +183,23 @@ public void OptimizeLoadLocalInstructions(int index, CilCode expected)
Assert.Equal(expected, instructions[0].OpCode.Code);
}

[Theory]
[InlineData(0, CilCode.Ldloca_S)]
[InlineData(1, CilCode.Ldloca_S)]
[InlineData(255, CilCode.Ldloca_S)]
[InlineData(256, CilCode.Ldloca)]
public void OptimizeLoadLocalAddressInstructions(int index, CilCode expected)
{
var instructions = CreateDummyMethod(false, 0, index + 1);

instructions.Add(CilOpCodes.Ldloca, instructions.Owner.LocalVariables[index]);
instructions.Add(CilOpCodes.Ret);

instructions.OptimizeMacros();

Assert.Equal(expected, instructions[0].OpCode.Code);
}

[Theory]
[InlineData(0, CilCode.Stloc_0)]
[InlineData(1, CilCode.Stloc_1)]
Expand Down Expand Up @@ -225,6 +242,24 @@ public void OptimizeLoadArgInstructions(int index, CilCode expected)
Assert.Equal(expected, instructions[0].OpCode.Code);
}

[Theory]
[InlineData(0, CilCode.Ldarga_S)]
[InlineData(1, CilCode.Ldarga_S)]
[InlineData(255, CilCode.Ldarga_S)]
[InlineData(256, CilCode.Ldarga)]
public void OptimizeLoadArgAddressInstructions(int index, CilCode expected)
{
var instructions = CreateDummyMethod(false, index + 1, 0);
var method = instructions.Owner.Owner;

instructions.Add(CilOpCodes.Ldarga, method.Parameters[index]);
instructions.Add(CilOpCodes.Ret);

instructions.OptimizeMacros();

Assert.Equal(expected, instructions[0].OpCode.Code);
}

[Theory]
[InlineData(0, CilCode.Starg_S)]
[InlineData(255, CilCode.Starg_S)]
Expand Down Expand Up @@ -374,7 +409,7 @@ public void OptimizeShouldUpdateOffsets()
instructions.OptimizeMacros();

int[] offsets = instructions.Select(i => i.Offset).ToArray();
Assert.NotEqual(offsets, instructions.Select(i => 0));
Assert.All(offsets.Skip(1), offset => Assert.NotEqual(0, offset));
instructions.CalculateOffsets();
Assert.Equal(offsets, instructions.Select(i => i.Offset));
}
Expand All @@ -393,7 +428,7 @@ public void ExpandShouldUpdateOffsets()
instructions.ExpandMacros();

int[] offsets = instructions.Select(i => i.Offset).ToArray();
Assert.NotEqual(offsets, instructions.Select(i => 0));
Assert.All(offsets.Skip(1), offset => Assert.NotEqual(0, offset));
instructions.CalculateOffsets();
Assert.Equal(offsets, instructions.Select(i => i.Offset));
}
Expand Down

0 comments on commit 9ab4e94

Please sign in to comment.