Skip to content

Commit

Permalink
Performance enhancement for unused local stripper. Closes #5
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Byass committed Oct 10, 2021
1 parent f7a0ba4 commit 5d0735b
Show file tree
Hide file tree
Showing 43 changed files with 97 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ public Arm64FieldReadToRegAction(MethodAnalysis<Arm64Instruction> context, Arm64
if(FieldRead == null)
return;

RegisterUsedLocal(ReadFrom);
RegisterUsedLocal(ReadFrom, context);

LocalWritten = context.MakeLocal(FieldRead.GetFinalType()!, reg: destReg);

RegisterUsedLocal(LocalWritten);
RegisterUsedLocal(LocalWritten, context);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public Arm64ImmediateToFieldAction(MethodAnalysis<Arm64Instruction> context, Arm
if(InstanceBeingSetOn?.Type == null)
return;

RegisterUsedLocal(InstanceBeingSetOn);
RegisterUsedLocal(InstanceBeingSetOn, context);

FieldWritten = FieldUtils.GetFieldBeingAccessed(InstanceBeingSetOn.Type, (ulong)instruction.MemoryOffset(), false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private void HandleReturnType(MethodAnalysis<Arm64Instruction> context)
{
CreateLocalForReturnType(context);

RegisterLocals();
RegisterLocals(context);

if (ManagedMethodBeingCalled?.FullName == "System.Void System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(System.Array,System.RuntimeFieldHandle)")
{
Expand Down
2 changes: 1 addition & 1 deletion Cpp2IL.Core/Analysis/Actions/ARM64/Arm64NewObjectAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public Arm64NewObjectAction(MethodAnalysis<Arm64Instruction> context, Arm64Instr

LocalReturned = context.MakeLocal(TypeCreated, reg: "x0");

RegisterUsedLocal(LocalReturned);
RegisterUsedLocal(LocalReturned, context);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ public Arm64RegisterToFieldAction(MethodAnalysis<Arm64Instruction> context, Arm6
if(InstanceBeingSetOn?.Type == null)
return;

RegisterUsedLocal(InstanceBeingSetOn);
RegisterUsedLocal(InstanceBeingSetOn, context);

if(_sourceOperand is LocalDefinition l)
RegisterUsedLocal(l);
RegisterUsedLocal(l, context);

FieldWritten = FieldUtils.GetFieldBeingAccessed(InstanceBeingSetOn.Type, (ulong)instruction.MemoryOffset(), sourceReg[0] == 'v');
}
Expand Down
2 changes: 1 addition & 1 deletion Cpp2IL.Core/Analysis/Actions/ARM64/Arm64ReturnAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public Arm64ReturnAction(MethodAnalysis<Arm64Instruction> context, Arm64Instruct
};

if (returnValue is LocalDefinition l)
RegisterUsedLocal(l);
RegisterUsedLocal(l, context);
}
}
}
8 changes: 4 additions & 4 deletions Cpp2IL.Core/Analysis/Actions/Base/AbstractCallAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ protected void CreateLocalForReturnType(MethodAnalysis<T> context)
ReturnedLocal = context.MakeLocal(returnType, reg: destReg);

//todo maybe improve?
RegisterUsedLocal(ReturnedLocal);
RegisterUsedLocal(ReturnedLocal, context);
}
}

Expand Down Expand Up @@ -212,11 +212,11 @@ private TypeReference ResolveGenericReturnTypeIfNeeded(TypeReference returnType,
return returnType;
}

protected void RegisterLocals()
protected void RegisterLocals(MethodAnalysis<T> context)
{
Arguments?.Where(o => o is LocalDefinition).ToList().ForEach(o => RegisterUsedLocal((LocalDefinition) o!));
Arguments?.Where(o => o is LocalDefinition).ToList().ForEach(o => RegisterUsedLocal((LocalDefinition) o!, context));
if (InstanceBeingCalledOn != null)
RegisterUsedLocal(InstanceBeingCalledOn);
RegisterUsedLocal(InstanceBeingCalledOn, context);
}

public List<Mono.Cecil.Cil.Instruction> GetILToLoadParams(MethodAnalysis<T> context, ILProcessor processor, bool includeThis = true)
Expand Down
4 changes: 2 additions & 2 deletions Cpp2IL.Core/Analysis/Actions/Base/AbstractComparisonAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ protected AbstractComparisonAction(MethodAnalysis<T> context, T associatedInstru
}

if (ArgumentOne is LocalDefinition l1)
RegisterUsedLocal(l1);
RegisterUsedLocal(l1, context);

if (ArgumentTwo is LocalDefinition l2)
RegisterUsedLocal(l2);
RegisterUsedLocal(l2, context);
}

public override Instruction[] ToILInstructions(MethodAnalysis<T> context, ILProcessor processor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,18 @@ protected AbstractConditionalJumpAction(MethodAnalysis<T> context, ulong branchT
// }

if (associatedCompare?.ArgumentOne is LocalDefinition l)
RegisterUsedLocal(l);
RegisterUsedLocal(l, context);
else if (associatedCompare?.ArgumentOne is ComparisonDirectFieldAccess a)
RegisterUsedLocal(a.localAccessedOn);
RegisterUsedLocal(a.localAccessedOn, context);
else if (associatedCompare?.ArgumentOne is ComparisonDirectPropertyAccess p)
RegisterUsedLocal(p.localAccessedOn);
RegisterUsedLocal(p.localAccessedOn, context);

if (associatedCompare?.ArgumentTwo is LocalDefinition l2)
RegisterUsedLocal(l2);
RegisterUsedLocal(l2, context);
else if (associatedCompare?.ArgumentTwo is ComparisonDirectFieldAccess a2)
RegisterUsedLocal(a2.localAccessedOn);
RegisterUsedLocal(a2.localAccessedOn, context);
else if (associatedCompare?.ArgumentTwo is ComparisonDirectPropertyAccess p2)
RegisterUsedLocal(p2.localAccessedOn);
RegisterUsedLocal(p2.localAccessedOn, context);

var (currBlockStart, currBlockEnd) = context.GetMostRecentBlock(AssociatedInstruction.GetInstructionAddress());

Expand Down
3 changes: 2 additions & 1 deletion Cpp2IL.Core/Analysis/Actions/Base/BaseAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ public List<LocalDefinition> GetUsedLocals()
return UsedLocals;
}

protected void RegisterUsedLocal(LocalDefinition l)
protected void RegisterUsedLocal(LocalDefinition l, MethodAnalysis<T> context)
{
UsedLocals.Add(l);
context.UnusedLocals.Remove(l);
}

public List<LocalDefinition> GetRegisteredLocalsWithoutSideEffects()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public ConstantArrayOffsetPointerToRegAction(MethodAnalysis<Instruction> context

if (_arrayLocal?.Type?.IsArray != true) return;

RegisterUsedLocal(_arrayLocal);
RegisterUsedLocal(_arrayLocal, context);

_index = (int) ((arrayOffset - Il2CppArrayUtils.FirstItemOffset) / Utils.GetPointerSizeBytes());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public ConstantToStackOffsetAction(MethodAnalysis<Instruction> context, Instruct

_newLocal = context.MakeLocal(Utils.TryLookupTypeDefKnownNotGeneric(_sourceConstant.Type.FullName)!, knownInitialValue: _sourceConstant.Value);
context.StackStoredLocals[(int) _stackOffset] = _newLocal;
RegisterUsedLocal(_newLocal);
RegisterUsedLocal(_newLocal, context);
}

public override Mono.Cecil.Cil.Instruction[] ToILInstructions(MethodAnalysis<Instruction> context, ILProcessor processor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public ImmediateToStackOffsetAction(MethodAnalysis<Instruction> context, Instruc

_newLocal = context.MakeLocal(Utils.UInt64Reference, knownInitialValue: _sourceImmediate);
context.StackStoredLocals[(int) _stackOffset] = _newLocal;
RegisterUsedLocal(_newLocal);
RegisterUsedLocal(_newLocal, context);
}

public override Mono.Cecil.Cil.Instruction[] ToILInstructions(MethodAnalysis<Instruction> context, ILProcessor processor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public AddRegToRegAction(MethodAnalysis<Instruction> context, Instruction instru
_secondOp = context.GetOperandInRegister(secondReg);

if(_firstOp != null)
RegisterUsedLocal(_firstOp);
RegisterUsedLocal(_firstOp, context);

if(_secondOp is LocalDefinition l)
RegisterUsedLocal(l);
RegisterUsedLocal(l, context);
}

public override Mono.Cecil.Cil.Instruction[] ToILInstructions(MethodAnalysis<Instruction> context, ILProcessor processor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public AllocateArrayAction(MethodAnalysis<Instruction> context, Instruction inst

if (sizeOperand is LocalDefinition local && (local.KnownInitialValue is ulong || local.KnownInitialValue is uint))
{
RegisterUsedLocal(local);
RegisterUsedLocal(local, context);
SizeAllocated = Convert.ToInt32(local.KnownInitialValue);
}
else if (sizeOperand is ConstantDefinition {Value: ulong sizeC})
Expand All @@ -46,7 +46,7 @@ public AllocateArrayAction(MethodAnalysis<Instruction> context, Instruction inst
if (TypeOfArray is not ArrayType arrayType) return;

LocalWritten = context.MakeLocal(arrayType, reg: "rax", knownInitialValue: new AllocatedArray(SizeAllocated, arrayType));
RegisterUsedLocal(LocalWritten); //Used implicitly until I can find out what's causing these issues
RegisterUsedLocal(LocalWritten, context); //Used implicitly until I can find out what's causing these issues
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public AllocateInstanceAction(MethodAnalysis<Instruction> context, Instruction i
LocalReturned = context.MakeLocal(TypeCreated, reg: "rax");

//Keeping this as used implicitly because we have to create instances of things.
RegisterUsedLocal(LocalReturned);
RegisterUsedLocal(LocalReturned, context);

if (LibCpp2IlMain.Binary.is32Bit)
context.Stack.Pop(); //Pop off the type created
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public ArrayElementReadToRegAction(MethodAnalysis<Instruction> context, Instruct

LocalMade = context.MakeLocal(ArrType.ElementType, reg: destReg);

RegisterUsedLocal(ArrayLocal);
RegisterUsedLocal(ArrayLocal, context);

if(OffsetLocal != null)
RegisterUsedLocal(OffsetLocal);
RegisterUsedLocal(OffsetLocal, context);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ private void HandleReturnType(MethodAnalysis<Instruction> context)
{
CreateLocalForReturnType(context);

RegisterLocals();
RegisterLocals(context);

if (ManagedMethodBeingCalled?.FullName == "System.Void System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(System.Array,System.RuntimeFieldHandle)")
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public CallManagedFunctionInRegAction(MethodAnalysis<Instruction> context, Instr

CreateLocalForReturnType(context);

RegisterLocals();
RegisterLocals(context);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public CallMethodSpecAction(MethodAnalysis<Instruction> context, Instruction ins
ManagedMethodBeingCalled = ManagedMethodBeingCalled.MakeMethodOnGenericType(methodSpec.GenericClassParams.Select(p => Utils.TryResolveTypeReflectionData(p, ManagedMethodBeingCalled)).ToArray()!);

CreateLocalForReturnType(context);
RegisterLocals();
RegisterLocals(context);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public CallVirtualMethodAction(MethodAnalysis<Instruction> context, Instruction
AddComment("Arguments are incorrect?");

CreateLocalForReturnType(context);
RegisterLocals();
RegisterLocals(context);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public ConstantArrayOffsetToRegAction(MethodAnalysis<Instruction> context, Instr

if (_arrayLocal?.Type?.IsArray != true) return;

RegisterUsedLocal(_arrayLocal);
RegisterUsedLocal(_arrayLocal, context);

_index = (int) ((arrayOffset - Il2CppArrayUtils.FirstItemOffset) / Utils.GetPointerSizeBytes());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public EbpOffsetToLocalAction(MethodAnalysis<Instruction> context, Instruction i

if (localBeingRead == null) return;

RegisterUsedLocal(localBeingRead);
RegisterUsedLocal(localBeingRead, context);

_destReg = Utils.GetRegisterNameNew(instruction.Op0Register);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ public FieldToLocalAction(MethodAnalysis<Instruction> context, Instruction instr
{
readFromType = result.castTo;
ReadFrom = result.original;
RegisterUsedLocal(ReadFrom);
RegisterUsedLocal(ReadFrom, context);
}
else if(readFrom is LocalDefinition {IsMethodInfoParam: false} l && l.Type?.Resolve() != null)
{
ReadFrom = l;
readFromType = ReadFrom!.Type!;
RegisterUsedLocal(ReadFrom);
RegisterUsedLocal(ReadFrom, context);
} else
{
AddComment($"This shouldn't be a field read? Op in reg {sourceRegName} is {context.GetOperandInRegister(sourceRegName)}, offset is {sourceFieldOffset} (0x{sourceFieldOffset:X})");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public ImmediateToFieldAction(MethodAnalysis<Instruction> context, Instruction i

if(InstanceBeingSetOn?.Type?.Resolve() == null) return;

RegisterUsedLocal(InstanceBeingSetOn);
RegisterUsedLocal(InstanceBeingSetOn, context);

FieldWritten = FieldUtils.GetFieldBeingAccessed(InstanceBeingSetOn.Type, destFieldOffset, false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public Implicit4ByteFieldReadAction(MethodAnalysis<Instruction> context, Instruc
if(_readOn == null)
return;

RegisterUsedLocal(_readOn);
RegisterUsedLocal(_readOn, context);
_read = FieldUtils.GetFieldBeingAccessed(_readOn.Type!, 0, false);

if(_read == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ public RegOffsetArrayValueReadRegToRegAction(MethodAnalysis<Instruction> context

if (_arrayLocal?.Type?.IsArray != true) return;

RegisterUsedLocal(_arrayLocal);
RegisterUsedLocal(_arrayLocal, context);

_indexLocal = context.GetLocalInReg(indexReg);

if(_indexLocal != null)
RegisterUsedLocal(_indexLocal);
RegisterUsedLocal(_indexLocal, context);

//Regardless of if we have an index local, we can still work out the type of the array and make a local.
//Resolve() turns array types into non-array types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public RegToConstantArrayOffsetAction(MethodAnalysis<Instruction> context, Instr
_opRead = context.GetOperandInRegister(regRead);

if(TheArray != null)
RegisterUsedLocal(TheArray);
RegisterUsedLocal(TheArray, context);

if(_opRead is LocalDefinition l)
RegisterUsedLocal(l);
RegisterUsedLocal(l, context);
}

protected override int GetOffsetWritten() => (int)_offsetIdx;
Expand Down
10 changes: 5 additions & 5 deletions Cpp2IL.Core/Analysis/Actions/x86/Important/RegToFieldAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public RegToFieldAction(MethodAnalysis<Instruction> context, Instruction instruc
InstanceBeingSetOn = context.GetLocalInReg(destRegName);

if(ValueRead is LocalDefinition loc)
RegisterUsedLocal(loc);
RegisterUsedLocal(loc, context);

if (ValueRead is ConstantDefinition { Value: StackPointer s })
{
Expand All @@ -36,14 +36,14 @@ public RegToFieldAction(MethodAnalysis<Instruction> context, Instruction instruc
if (context.GetConstantInReg(destRegName) is {Value: FieldPointer p})
{
InstanceBeingSetOn = p.OnWhat;
RegisterUsedLocal(InstanceBeingSetOn);
RegisterUsedLocal(InstanceBeingSetOn, context);
FieldWritten = p.Field;
}

return;
}

RegisterUsedLocal(InstanceBeingSetOn);
RegisterUsedLocal(InstanceBeingSetOn, context);

FieldWritten = FieldUtils.GetFieldBeingAccessed(InstanceBeingSetOn.Type, destFieldOffset, false);
}
Expand All @@ -56,8 +56,8 @@ internal RegToFieldAction(MethodAnalysis<Instruction> context, Instruction instr
InstanceBeingSetOn = instanceWrittenOn;
ValueRead = readFrom;

RegisterUsedLocal(InstanceBeingSetOn);
RegisterUsedLocal(readFrom);
RegisterUsedLocal(InstanceBeingSetOn, context);
RegisterUsedLocal(readFrom, context);
}

protected override string? GetValuePseudocode() => ValueRead?.GetPseudocodeRepresentation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public RegToStaticFieldAction(MethodAnalysis<Instruction> context, Instruction i
return;

if (_sourceOperand is LocalDefinition l)
RegisterUsedLocal(l);
RegisterUsedLocal(l, context);

_theField = FieldUtils.GetStaticFieldByOffset(staticFieldsPtr, staticFieldOffset);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public ReturnFromFunctionAction(MethodAnalysis<Instruction> context, Instruction
returnValue = returnType.ShouldBeInFloatingPointRegister() ? context.GetOperandInRegister("xmm0") : context.GetOperandInRegister("rax");

if (returnValue is LocalDefinition l)
RegisterUsedLocal(l);
RegisterUsedLocal(l, context);
}
}
}
Loading

0 comments on commit 5d0735b

Please sign in to comment.